Merge "Implementing app-centric Shelf."
diff --git a/Android.mk b/Android.mk
index 40da134..505b12d 100644
--- a/Android.mk
+++ b/Android.mk
@@ -160,6 +160,7 @@
 	core/java/android/hardware/fingerprint/IFingerprintDaemon.aidl \
 	core/java/android/hardware/fingerprint/IFingerprintDaemonCallback.aidl \
 	core/java/android/hardware/fingerprint/IFingerprintService.aidl \
+	core/java/android/hardware/fingerprint/IFingerprintServiceLockoutResetCallback.aidl \
 	core/java/android/hardware/fingerprint/IFingerprintServiceReceiver.aidl \
 	core/java/android/hardware/hdmi/IHdmiControlCallback.aidl \
 	core/java/android/hardware/hdmi/IHdmiControlService.aidl \
@@ -172,6 +173,7 @@
 	core/java/android/hardware/hdmi/IHdmiVendorCommandListener.aidl \
 	core/java/android/hardware/input/IInputManager.aidl \
 	core/java/android/hardware/input/IInputDevicesChangedListener.aidl \
+	core/java/android/hardware/input/ITabletModeChangedListener.aidl \
 	core/java/android/hardware/location/IActivityRecognitionHardware.aidl \
 	core/java/android/hardware/location/IActivityRecognitionHardwareClient.aidl \
 	core/java/android/hardware/location/IActivityRecognitionHardwareSink.aidl \
diff --git a/CleanSpec.mk b/CleanSpec.mk
index 48be749..6e44d77 100644
--- a/CleanSpec.mk
+++ b/CleanSpec.mk
@@ -235,6 +235,7 @@
 $(call add-clean-step, rm -rf $(PRODUCT_OUT)/obj/SHARED_LIBRARIES/libinputflingerhost_intermediates $(PRODUCT_OUT)/obj_arm/SHARED_LIBRARIES/libinputflingerhost_intermediates)
 $(call add-clean-step, rm -rf $(PRODUCT_OUT)/target/common/obj/framework.aidl)
 $(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/JAVA_LIBRARIES/framework_intermediates)
+$(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/APPS/DocumentsUI_intermediates)
 
 # ******************************************************************
 # NEWER CLEAN STEPS MUST BE AT THE END OF THE LIST ABOVE THIS BANNER
diff --git a/api/current.txt b/api/current.txt
index e9c5727..98517a7 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -241,10 +241,10 @@
     field public static final int activatedBackgroundIndicator = 16843517; // 0x10102fd
     field public static final int activityCloseEnterAnimation = 16842938; // 0x10100ba
     field public static final int activityCloseExitAnimation = 16842939; // 0x10100bb
-    field public static final int activityHeight = 16844019; // 0x10104f3
+    field public static final int activityHeight = 16844021; // 0x10104f5
     field public static final int activityOpenEnterAnimation = 16842936; // 0x10100b8
     field public static final int activityOpenExitAnimation = 16842937; // 0x10100b9
-    field public static final int activityWidth = 16844018; // 0x10104f2
+    field public static final int activityWidth = 16844020; // 0x10104f4
     field public static final int addPrintersActivity = 16843750; // 0x10103e6
     field public static final int addStatesFromChildren = 16842992; // 0x10100f0
     field public static final int adjustViewBounds = 16843038; // 0x101011e
@@ -786,6 +786,7 @@
     field public static final int listChoiceIndicatorSingle = 16843289; // 0x1010219
     field public static final int listDivider = 16843284; // 0x1010214
     field public static final int listDividerAlertDialog = 16843525; // 0x1010305
+    field public static final int listMenuViewStyle = 16844018; // 0x10104f2
     field public static final int listPopupWindowStyle = 16843519; // 0x10102ff
     field public static final int listPreferredItemHeight = 16842829; // 0x101004d
     field public static final int listPreferredItemHeightLarge = 16843654; // 0x1010386
@@ -991,6 +992,7 @@
     field public static final int resizeClip = 16843983; // 0x10104cf
     field public static final int resizeMode = 16843619; // 0x1010363
     field public static final int resizeable = 16843405; // 0x101028d
+    field public static final int resizeableActivity = 16844022; // 0x10104f6
     field public static final int resource = 16842789; // 0x1010025
     field public static final int restoreAnyVersion = 16843450; // 0x10102ba
     field public static final deprecated int restoreNeedsApplication = 16843421; // 0x101029d
@@ -1148,6 +1150,7 @@
     field public static final int strokeLineJoin = 16843788; // 0x101040c
     field public static final int strokeMiterLimit = 16843789; // 0x101040d
     field public static final int strokeWidth = 16843783; // 0x1010407
+    field public static final int subMenuArrow = 16844019; // 0x10104f3
     field public static final int submitBackground = 16843912; // 0x1010488
     field public static final int subtitle = 16843473; // 0x10102d1
     field public static final int subtitleTextAppearance = 16843823; // 0x101042f
@@ -9663,12 +9666,14 @@
     method public int diff(android.content.res.Configuration);
     method public boolean equals(android.content.res.Configuration);
     method public int getLayoutDirection();
+    method public android.util.LocaleList getLocales();
     method public boolean isLayoutSizeAtLeast(int);
     method public boolean isScreenRound();
     method public static boolean needNewResources(int, int);
     method public void readFromParcel(android.os.Parcel);
     method public void setLayoutDirection(java.util.Locale);
     method public void setLocale(java.util.Locale);
+    method public void setLocales(android.util.LocaleList);
     method public void setTo(android.content.res.Configuration);
     method public void setToDefaults();
     method public int updateFrom(android.content.res.Configuration);
@@ -9742,7 +9747,7 @@
     field public int hardKeyboardHidden;
     field public int keyboard;
     field public int keyboardHidden;
-    field public java.util.Locale locale;
+    field public deprecated java.util.Locale locale;
     field public int mcc;
     field public int mnc;
     field public int navigation;
@@ -11586,6 +11591,7 @@
     method public void getTextBounds(java.lang.String, int, int, android.graphics.Rect);
     method public void getTextBounds(char[], int, int, android.graphics.Rect);
     method public java.util.Locale getTextLocale();
+    method public android.util.LocaleList getTextLocales();
     method public void getTextPath(char[], int, int, float, float, android.graphics.Path);
     method public void getTextPath(java.lang.String, int, int, float, float, android.graphics.Path);
     method public float getTextScaleX();
@@ -11641,6 +11647,7 @@
     method public void setSubpixelText(boolean);
     method public void setTextAlign(android.graphics.Paint.Align);
     method public void setTextLocale(java.util.Locale);
+    method public void setTextLocales(android.util.LocaleList);
     method public void setTextScaleX(float);
     method public void setTextSize(float);
     method public void setTextSkewX(float);
@@ -12296,6 +12303,7 @@
     method public int getIntrinsicWidth();
     method public int getLayoutDirection();
     method public final int getLevel();
+    method public final float getLevelFloat();
     method public int getMinimumHeight();
     method public int getMinimumWidth();
     method public abstract int getOpacity();
@@ -12315,6 +12323,7 @@
     method protected void onBoundsChange(android.graphics.Rect);
     method public boolean onLayoutDirectionChanged(int);
     method protected boolean onLevelChange(int);
+    method protected boolean onLevelChange(float);
     method protected boolean onStateChange(int[]);
     method public static int resolveOpacity(int, int);
     method public void scheduleSelf(java.lang.Runnable, long);
@@ -12332,12 +12341,15 @@
     method public void setHotspotBounds(int, int, int, int);
     method public final boolean setLayoutDirection(int);
     method public final boolean setLevel(int);
+    method public final boolean setLevel(float);
     method public boolean setState(int[]);
     method public void setTint(int);
     method public void setTintList(android.content.res.ColorStateList);
     method public void setTintMode(android.graphics.PorterDuff.Mode);
     method public boolean setVisible(boolean, boolean);
     method public void unscheduleSelf(java.lang.Runnable);
+    field public static final int MAX_LEVEL = 10000; // 0x2710
+    field public static final float MAX_LEVEL_FLOAT = 10000.0f;
   }
 
   public static abstract interface Drawable.Callback {
@@ -18164,6 +18176,7 @@
     method public android.mtp.MtpObjectInfo.Builder setImagePixWidth(int);
     method public android.mtp.MtpObjectInfo.Builder setKeywords(java.lang.String);
     method public android.mtp.MtpObjectInfo.Builder setName(java.lang.String);
+    method public android.mtp.MtpObjectInfo.Builder setObjectHandle(int);
     method public android.mtp.MtpObjectInfo.Builder setParent(int);
     method public android.mtp.MtpObjectInfo.Builder setProtectionStatus(int);
     method public android.mtp.MtpObjectInfo.Builder setSequenceNumber(int);
@@ -34200,11 +34213,16 @@
 
   public final class LocaleList {
     ctor public LocaleList();
+    ctor public LocaleList(java.util.Locale);
     ctor public LocaleList(java.util.Locale[]);
+    method public static android.util.LocaleList forLanguageTags(java.lang.String);
     method public java.util.Locale get(int);
+    method public static android.util.LocaleList getDefault();
+    method public static android.util.LocaleList getEmptyLocaleList();
     method public java.util.Locale getPrimary();
     method public boolean isEmpty();
     method public int size();
+    method public java.lang.String toLanguageTags();
   }
 
   public final class Log {
@@ -35352,6 +35370,7 @@
     field public static final int KEYCODE_SLEEP = 223; // 0xdf
     field public static final int KEYCODE_SOFT_LEFT = 1; // 0x1
     field public static final int KEYCODE_SOFT_RIGHT = 2; // 0x2
+    field public static final int KEYCODE_SOFT_SLEEP = 276; // 0x114
     field public static final int KEYCODE_SPACE = 62; // 0x3e
     field public static final int KEYCODE_STAR = 17; // 0x11
     field public static final int KEYCODE_STB_INPUT = 180; // 0xb4
@@ -41712,6 +41731,7 @@
     method public java.lang.CharSequence getText();
     method public final android.content.res.ColorStateList getTextColors();
     method public java.util.Locale getTextLocale();
+    method public android.util.LocaleList getTextLocales();
     method public float getTextScaleX();
     method public float getTextSize();
     method public int getTotalPaddingBottom();
@@ -41824,6 +41844,7 @@
     method public final void setTextKeepState(java.lang.CharSequence);
     method public final void setTextKeepState(java.lang.CharSequence, android.widget.TextView.BufferType);
     method public void setTextLocale(java.util.Locale);
+    method public void setTextLocales(android.util.LocaleList);
     method public void setTextScaleX(float);
     method public void setTextSize(float);
     method public void setTextSize(int, float);
diff --git a/api/system-current.txt b/api/system-current.txt
index e234970..e694bc7 100644
--- a/api/system-current.txt
+++ b/api/system-current.txt
@@ -333,10 +333,10 @@
     field public static final int activatedBackgroundIndicator = 16843517; // 0x10102fd
     field public static final int activityCloseEnterAnimation = 16842938; // 0x10100ba
     field public static final int activityCloseExitAnimation = 16842939; // 0x10100bb
-    field public static final int activityHeight = 16844019; // 0x10104f3
+    field public static final int activityHeight = 16844021; // 0x10104f5
     field public static final int activityOpenEnterAnimation = 16842936; // 0x10100b8
     field public static final int activityOpenExitAnimation = 16842937; // 0x10100b9
-    field public static final int activityWidth = 16844018; // 0x10104f2
+    field public static final int activityWidth = 16844020; // 0x10104f4
     field public static final int addPrintersActivity = 16843750; // 0x10103e6
     field public static final int addStatesFromChildren = 16842992; // 0x10100f0
     field public static final int adjustViewBounds = 16843038; // 0x101011e
@@ -878,6 +878,7 @@
     field public static final int listChoiceIndicatorSingle = 16843289; // 0x1010219
     field public static final int listDivider = 16843284; // 0x1010214
     field public static final int listDividerAlertDialog = 16843525; // 0x1010305
+    field public static final int listMenuViewStyle = 16844018; // 0x10104f2
     field public static final int listPopupWindowStyle = 16843519; // 0x10102ff
     field public static final int listPreferredItemHeight = 16842829; // 0x101004d
     field public static final int listPreferredItemHeightLarge = 16843654; // 0x1010386
@@ -1083,6 +1084,7 @@
     field public static final int resizeClip = 16843983; // 0x10104cf
     field public static final int resizeMode = 16843619; // 0x1010363
     field public static final int resizeable = 16843405; // 0x101028d
+    field public static final int resizeableActivity = 16844022; // 0x10104f6
     field public static final int resource = 16842789; // 0x1010025
     field public static final int restoreAnyVersion = 16843450; // 0x10102ba
     field public static final deprecated int restoreNeedsApplication = 16843421; // 0x101029d
@@ -1244,6 +1246,7 @@
     field public static final int strokeLineJoin = 16843788; // 0x101040c
     field public static final int strokeMiterLimit = 16843789; // 0x101040d
     field public static final int strokeWidth = 16843783; // 0x1010407
+    field public static final int subMenuArrow = 16844019; // 0x10104f3
     field public static final int submitBackground = 16843912; // 0x1010488
     field public static final int subtitle = 16843473; // 0x10102d1
     field public static final int subtitleTextAppearance = 16843823; // 0x101042f
@@ -10000,12 +10003,14 @@
     method public int diff(android.content.res.Configuration);
     method public boolean equals(android.content.res.Configuration);
     method public int getLayoutDirection();
+    method public android.util.LocaleList getLocales();
     method public boolean isLayoutSizeAtLeast(int);
     method public boolean isScreenRound();
     method public static boolean needNewResources(int, int);
     method public void readFromParcel(android.os.Parcel);
     method public void setLayoutDirection(java.util.Locale);
     method public void setLocale(java.util.Locale);
+    method public void setLocales(android.util.LocaleList);
     method public void setTo(android.content.res.Configuration);
     method public void setToDefaults();
     method public int updateFrom(android.content.res.Configuration);
@@ -10079,7 +10084,7 @@
     field public int hardKeyboardHidden;
     field public int keyboard;
     field public int keyboardHidden;
-    field public java.util.Locale locale;
+    field public deprecated java.util.Locale locale;
     field public int mcc;
     field public int mnc;
     field public int navigation;
@@ -11923,6 +11928,7 @@
     method public void getTextBounds(java.lang.String, int, int, android.graphics.Rect);
     method public void getTextBounds(char[], int, int, android.graphics.Rect);
     method public java.util.Locale getTextLocale();
+    method public android.util.LocaleList getTextLocales();
     method public void getTextPath(char[], int, int, float, float, android.graphics.Path);
     method public void getTextPath(java.lang.String, int, int, float, float, android.graphics.Path);
     method public float getTextScaleX();
@@ -11978,6 +11984,7 @@
     method public void setSubpixelText(boolean);
     method public void setTextAlign(android.graphics.Paint.Align);
     method public void setTextLocale(java.util.Locale);
+    method public void setTextLocales(android.util.LocaleList);
     method public void setTextScaleX(float);
     method public void setTextSize(float);
     method public void setTextSkewX(float);
@@ -12633,6 +12640,7 @@
     method public int getIntrinsicWidth();
     method public int getLayoutDirection();
     method public final int getLevel();
+    method public final float getLevelFloat();
     method public int getMinimumHeight();
     method public int getMinimumWidth();
     method public abstract int getOpacity();
@@ -12652,6 +12660,7 @@
     method protected void onBoundsChange(android.graphics.Rect);
     method public boolean onLayoutDirectionChanged(int);
     method protected boolean onLevelChange(int);
+    method protected boolean onLevelChange(float);
     method protected boolean onStateChange(int[]);
     method public static int resolveOpacity(int, int);
     method public void scheduleSelf(java.lang.Runnable, long);
@@ -12669,12 +12678,15 @@
     method public void setHotspotBounds(int, int, int, int);
     method public final boolean setLayoutDirection(int);
     method public final boolean setLevel(int);
+    method public final boolean setLevel(float);
     method public boolean setState(int[]);
     method public void setTint(int);
     method public void setTintList(android.content.res.ColorStateList);
     method public void setTintMode(android.graphics.PorterDuff.Mode);
     method public boolean setVisible(boolean, boolean);
     method public void unscheduleSelf(java.lang.Runnable);
+    field public static final int MAX_LEVEL = 10000; // 0x2710
+    field public static final float MAX_LEVEL_FLOAT = 10000.0f;
   }
 
   public static abstract interface Drawable.Callback {
@@ -19676,6 +19688,7 @@
     method public android.mtp.MtpObjectInfo.Builder setImagePixWidth(int);
     method public android.mtp.MtpObjectInfo.Builder setKeywords(java.lang.String);
     method public android.mtp.MtpObjectInfo.Builder setName(java.lang.String);
+    method public android.mtp.MtpObjectInfo.Builder setObjectHandle(int);
     method public android.mtp.MtpObjectInfo.Builder setParent(int);
     method public android.mtp.MtpObjectInfo.Builder setProtectionStatus(int);
     method public android.mtp.MtpObjectInfo.Builder setSequenceNumber(int);
@@ -36494,11 +36507,16 @@
 
   public final class LocaleList {
     ctor public LocaleList();
+    ctor public LocaleList(java.util.Locale);
     ctor public LocaleList(java.util.Locale[]);
+    method public static android.util.LocaleList forLanguageTags(java.lang.String);
     method public java.util.Locale get(int);
+    method public static android.util.LocaleList getDefault();
+    method public static android.util.LocaleList getEmptyLocaleList();
     method public java.util.Locale getPrimary();
     method public boolean isEmpty();
     method public int size();
+    method public java.lang.String toLanguageTags();
   }
 
   public final class Log {
@@ -37646,6 +37664,7 @@
     field public static final int KEYCODE_SLEEP = 223; // 0xdf
     field public static final int KEYCODE_SOFT_LEFT = 1; // 0x1
     field public static final int KEYCODE_SOFT_RIGHT = 2; // 0x2
+    field public static final int KEYCODE_SOFT_SLEEP = 276; // 0x114
     field public static final int KEYCODE_SPACE = 62; // 0x3e
     field public static final int KEYCODE_STAR = 17; // 0x11
     field public static final int KEYCODE_STB_INPUT = 180; // 0xb4
@@ -44320,6 +44339,7 @@
     method public java.lang.CharSequence getText();
     method public final android.content.res.ColorStateList getTextColors();
     method public java.util.Locale getTextLocale();
+    method public android.util.LocaleList getTextLocales();
     method public float getTextScaleX();
     method public float getTextSize();
     method public int getTotalPaddingBottom();
@@ -44432,6 +44452,7 @@
     method public final void setTextKeepState(java.lang.CharSequence);
     method public final void setTextKeepState(java.lang.CharSequence, android.widget.TextView.BufferType);
     method public void setTextLocale(java.util.Locale);
+    method public void setTextLocales(android.util.LocaleList);
     method public void setTextScaleX(float);
     method public void setTextSize(float);
     method public void setTextSize(int, float);
diff --git a/core/java/android/animation/AnimatorSet.java b/core/java/android/animation/AnimatorSet.java
index 5b88c8e..16f825d 100644
--- a/core/java/android/animation/AnimatorSet.java
+++ b/core/java/android/animation/AnimatorSet.java
@@ -408,15 +408,18 @@
 
     /**
      * Returns true if any of the child animations of this AnimatorSet have been started and have
-     * not yet ended.
-     * @return Whether this AnimatorSet has been started and has not yet ended.
+     * not yet ended. Child animations will not be started until the AnimatorSet has gone past
+     * its initial delay set through {@link #setStartDelay(long)}.
+     *
+     * @return Whether this AnimatorSet has gone past the initial delay, and at least one child
+     *         animation has been started and not yet ended.
      */
     @Override
     public boolean isRunning() {
         int size = mNodes.size();
         for (int i = 0; i < size; i++) {
             Node node = mNodes.get(i);
-            if (node != mRootNode && node.mAnimation.isRunning()) {
+            if (node != mRootNode && node.mAnimation.isStarted()) {
                 return true;
             }
         }
diff --git a/core/java/android/app/ActivityOptions.java b/core/java/android/app/ActivityOptions.java
index 2406985..933f98d 100644
--- a/core/java/android/app/ActivityOptions.java
+++ b/core/java/android/app/ActivityOptions.java
@@ -19,6 +19,7 @@
 import android.content.Context;
 import android.content.Intent;
 import android.graphics.Bitmap;
+import android.graphics.Rect;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.IRemoteCallback;
@@ -59,6 +60,13 @@
     public static final String KEY_PACKAGE_NAME = "android:activity.packageName";
 
     /**
+     * The bounds that the activity should be started in. Set to null explicitly
+     * for full screen. If the key is not found, previous bounds will be preserved.
+     * @hide
+     */
+    public static final String KEY_BOUNDS = "android:activity.bounds";
+
+    /**
      * Type of animation that arguments specify.
      * @hide
      */
@@ -163,6 +171,8 @@
     public static final int ANIM_CLIP_REVEAL = 11;
 
     private String mPackageName;
+    private boolean mHasBounds;
+    private Rect mBounds;
     private int mAnimationType = ANIM_NONE;
     private int mCustomEnterResId;
     private int mCustomExitResId;
@@ -631,6 +641,10 @@
         } catch (RuntimeException e) {
             Slog.w(TAG, e);
         }
+        mHasBounds = opts.containsKey(KEY_BOUNDS);
+        if (mHasBounds) {
+            mBounds = opts.getParcelable(KEY_BOUNDS);
+        }
         mAnimationType = opts.getInt(KEY_ANIM_TYPE);
         switch (mAnimationType) {
             case ANIM_CUSTOM:
@@ -677,11 +691,28 @@
     }
 
     /** @hide */
+    public ActivityOptions setBounds(Rect bounds) {
+        mHasBounds = true;
+        mBounds = bounds;
+        return this;
+    }
+
+    /** @hide */
     public String getPackageName() {
         return mPackageName;
     }
 
     /** @hide */
+    public boolean hasBounds() {
+        return mHasBounds;
+    }
+
+    /** @hide */
+    public Rect getBounds(){
+        return mBounds;
+    }
+
+    /** @hide */
     public int getAnimationType() {
         return mAnimationType;
     }
@@ -867,6 +898,9 @@
         if (mPackageName != null) {
             b.putString(KEY_PACKAGE_NAME, mPackageName);
         }
+        if (mHasBounds) {
+            b.putParcelable(KEY_BOUNDS, mBounds);
+        }
         b.putInt(KEY_ANIM_TYPE, mAnimationType);
         if (mUsageTimeReport != null) {
             b.putParcelable(KEY_USAGE_TIME_REPORT, mUsageTimeReport);
diff --git a/core/java/android/app/ActivityThread.java b/core/java/android/app/ActivityThread.java
index 412e3cd..67dee7f 100644
--- a/core/java/android/app/ActivityThread.java
+++ b/core/java/android/app/ActivityThread.java
@@ -4593,27 +4593,6 @@
         }
         updateDefaultDensity();
 
-        final ContextImpl appContext = ContextImpl.createAppContext(this, data.info);
-        if (!Process.isIsolated()) {
-            final File cacheDir = appContext.getCacheDir();
-
-            if (cacheDir != null) {
-                // Provide a usable directory for temporary files
-                System.setProperty("java.io.tmpdir", cacheDir.getAbsolutePath());
-            } else {
-                Log.v(TAG, "Unable to initialize \"java.io.tmpdir\" property due to missing cache directory");
-            }
-
-            // Use codeCacheDir to store generated/compiled graphics code
-            final File codeCacheDir = appContext.getCodeCacheDir();
-            if (codeCacheDir != null) {
-                setupGraphicsSupport(data.info, codeCacheDir);
-            } else {
-                Log.e(TAG, "Unable to setupGraphicsSupport due to missing code-cache directory");
-            }
-        }
-
-
         final boolean is24Hr = "24".equals(mCoreSettings.getString(Settings.System.TIME_12_24));
         DateFormat.set24HourTimePref(is24Hr);
 
@@ -4685,29 +4664,28 @@
         /**
          * Initialize the default http proxy in this process for the reasons we set the time zone.
          */
-        IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
+        final IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
         if (b != null) {
             // In pre-boot mode (doing initial launch to collect password), not
             // all system is up.  This includes the connectivity service, so don't
             // crash if we can't get it.
-            IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
+            final IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
             try {
                 final ProxyInfo proxyInfo = service.getProxyForNetwork(null);
                 Proxy.setHttpProxySystemProperty(proxyInfo);
             } catch (RemoteException e) {}
         }
 
+        // Instrumentation info affects the class loader, so load it before
+        // setting up the app context.
+        final InstrumentationInfo ii;
         if (data.instrumentationName != null) {
-            InstrumentationInfo ii = null;
             try {
-                ii = appContext.getPackageManager().
-                    getInstrumentationInfo(data.instrumentationName, 0);
+                ii = new ApplicationPackageManager(null, getPackageManager())
+                        .getInstrumentationInfo(data.instrumentationName, 0);
             } catch (PackageManager.NameNotFoundException e) {
-            }
-            if (ii == null) {
                 throw new RuntimeException(
-                    "Unable to find instrumentation info for: "
-                    + data.instrumentationName);
+                        "Unable to find instrumentation info for: " + data.instrumentationName);
             }
 
             mInstrumentationPackageName = ii.packageName;
@@ -4717,8 +4695,33 @@
             mInstrumentedAppDir = data.info.getAppDir();
             mInstrumentedSplitAppDirs = data.info.getSplitAppDirs();
             mInstrumentedLibDir = data.info.getLibDir();
+        } else {
+            ii = null;
+        }
 
-            ApplicationInfo instrApp = new ApplicationInfo();
+        final ContextImpl appContext = ContextImpl.createAppContext(this, data.info);
+        if (!Process.isIsolated()) {
+            final File cacheDir = appContext.getCacheDir();
+            if (cacheDir != null) {
+                // Provide a usable directory for temporary files
+                System.setProperty("java.io.tmpdir", cacheDir.getAbsolutePath());
+            } else {
+                Log.v(TAG, "Unable to initialize \"java.io.tmpdir\" property "
+                        + "due to missing cache directory");
+            }
+
+            // Use codeCacheDir to store generated/compiled graphics code
+            final File codeCacheDir = appContext.getCodeCacheDir();
+            if (codeCacheDir != null) {
+                setupGraphicsSupport(data.info, codeCacheDir);
+            } else {
+                Log.e(TAG, "Unable to setupGraphicsSupport due to missing code-cache directory");
+            }
+        }
+
+        // Continue loading instrumentation.
+        if (ii != null) {
+            final ApplicationInfo instrApp = new ApplicationInfo();
             instrApp.packageName = ii.packageName;
             instrApp.sourceDir = ii.sourceDir;
             instrApp.publicSourceDir = ii.publicSourceDir;
@@ -4726,12 +4729,13 @@
             instrApp.splitPublicSourceDirs = ii.splitPublicSourceDirs;
             instrApp.dataDir = ii.dataDir;
             instrApp.nativeLibraryDir = ii.nativeLibraryDir;
-            LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
+
+            final LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
                     appContext.getClassLoader(), false, true, false);
-            ContextImpl instrContext = ContextImpl.createAppContext(this, pi);
+            final ContextImpl instrContext = ContextImpl.createAppContext(this, pi);
 
             try {
-                java.lang.ClassLoader cl = instrContext.getClassLoader();
+                final ClassLoader cl = instrContext.getClassLoader();
                 mInstrumentation = (Instrumentation)
                     cl.loadClass(data.instrumentationName.getClassName()).newInstance();
             } catch (Exception e) {
@@ -4740,18 +4744,17 @@
                     + data.instrumentationName + ": " + e.toString(), e);
             }
 
-            mInstrumentation.init(this, instrContext, appContext,
-                   new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher,
-                   data.instrumentationUiAutomationConnection);
+            final ComponentName component = new ComponentName(ii.packageName, ii.name);
+            mInstrumentation.init(this, instrContext, appContext, component,
+                    data.instrumentationWatcher, data.instrumentationUiAutomationConnection);
 
             if (mProfiler.profileFile != null && !ii.handleProfiling
                     && mProfiler.profileFd == null) {
                 mProfiler.handlingProfiling = true;
-                File file = new File(mProfiler.profileFile);
+                final File file = new File(mProfiler.profileFile);
                 file.getParentFile().mkdirs();
                 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
             }
-
         } else {
             mInstrumentation = new Instrumentation();
         }
diff --git a/core/java/android/app/FragmentManager.java b/core/java/android/app/FragmentManager.java
index 3b026d2..132ffef 100644
--- a/core/java/android/app/FragmentManager.java
+++ b/core/java/android/app/FragmentManager.java
@@ -2196,6 +2196,7 @@
             fragment.mTag = tag;
             fragment.mInLayout = true;
             fragment.mFragmentManager = this;
+            fragment.mHost = mHost;
             fragment.onInflate(mHost.getContext(), attrs, fragment.mSavedFragmentState);
             addFragment(fragment, true);
         } else if (fragment.mInLayout) {
diff --git a/core/java/android/app/admin/DevicePolicyManager.java b/core/java/android/app/admin/DevicePolicyManager.java
index e3414d9..7ffac0a 100644
--- a/core/java/android/app/admin/DevicePolicyManager.java
+++ b/core/java/android/app/admin/DevicePolicyManager.java
@@ -213,7 +213,8 @@
 
      * @see DeviceAdminReceiver
      * @deprecated Use {@link #EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME}. This extra is still
-     * supported.
+     * supported, but only if there is only one device admin receiver in the package that requires
+     * the permission {@link android.Manifest.permission#BIND_DEVICE_ADMIN}.
      */
     @Deprecated
     public static final String EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME
diff --git a/core/java/android/content/pm/ApplicationInfo.java b/core/java/android/content/pm/ApplicationInfo.java
index 6feb860..914945b 100644
--- a/core/java/android/content/pm/ApplicationInfo.java
+++ b/core/java/android/content/pm/ApplicationInfo.java
@@ -531,12 +531,14 @@
 
     /**
      * String retrieved from the seinfo tag found in selinux policy. This value
-     * is useful in setting an SELinux security context on the process as well
-     * as its data directory.
+     * can be overridden with a value set through the mac_permissions.xml policy
+     * construct. This value is useful in setting an SELinux security context on
+     * the process as well as its data directory. The String default is being used
+     * here to represent a catchall label when no policy matches.
      *
      * {@hide}
      */
-    public String seinfo;
+    public String seinfo = "default";
 
     /**
      * Paths to all shared libraries this application is linked against.  This
diff --git a/core/java/android/content/res/Configuration.java b/core/java/android/content/res/Configuration.java
index fd60476..927c02f 100644
--- a/core/java/android/content/res/Configuration.java
+++ b/core/java/android/content/res/Configuration.java
@@ -22,11 +22,13 @@
 import org.xmlpull.v1.XmlPullParserException;
 import org.xmlpull.v1.XmlSerializer;
 
+import android.annotation.Nullable;
 import android.content.pm.ActivityInfo;
 import android.os.Build;
 import android.os.Parcel;
 import android.os.Parcelable;
 import android.text.TextUtils;
+import android.util.LocaleList;
 import android.view.View;
 
 import java.io.IOException;
@@ -36,7 +38,7 @@
 /**
  * This class describes all device configuration information that can
  * impact the resources the application retrieves.  This includes both
- * user-specified configuration options (locale and scaling) as well
+ * user-specified configuration options (locale list and scaling) as well
  * as device configurations (such as input modes, screen size and screen orientation).
  * <p>You can acquire this object from {@link Resources}, using {@link
  * Resources#getConfiguration}. Thus, from an activity, you can get it by chaining the request
@@ -78,8 +80,13 @@
      * Current user preference for the locale, corresponding to
      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#LocaleQualifier">locale</a>
      * resource qualifier.
+     *
+     * @deprecated Do not set or read this directly. Use {@link #getLocales()} and
+     * {@link #setLocales(LocaleList)}.
      */
-    public Locale locale;
+    @Deprecated public Locale locale;
+
+    private LocaleList mLocaleList;
 
     /**
      * Locale should persist on setting.  This is hidden because it is really
@@ -648,6 +655,15 @@
         setTo(o);
     }
 
+    /* This brings mLocaleList in sync with locale in case a user of the older API who doesn't know
+     * about setLocales() has changed locale directly. */
+    private void fixUpLocaleList() {
+        if ((locale == null && !mLocaleList.isEmpty()) ||
+                (locale != null && !locale.equals(mLocaleList.getPrimary()))) {
+            mLocaleList = new LocaleList(locale);
+        }
+    }
+
     public void setTo(Configuration o) {
         fontScale = o.fontScale;
         mcc = o.mcc;
@@ -655,6 +671,8 @@
         if (o.locale != null) {
             locale = (Locale) o.locale.clone();
         }
+        o.fixUpLocaleList();
+        mLocaleList = o.mLocaleList;
         userSetLocale = o.userSetLocale;
         touchscreen = o.touchscreen;
         keyboard = o.keyboard;
@@ -692,11 +710,12 @@
         } else {
             sb.append("?mnc");
         }
-        if (locale != null) {
+        fixUpLocaleList();
+        if (!mLocaleList.isEmpty()) {
             sb.append(" ");
-            sb.append(locale);
+            sb.append(mLocaleList);
         } else {
-            sb.append(" ?locale");
+            sb.append(" ?localeList");
         }
         int layoutDir = (screenLayout&SCREENLAYOUT_LAYOUTDIR_MASK);
         switch (layoutDir) {
@@ -819,6 +838,7 @@
     public void setToDefaults() {
         fontScale = 1;
         mcc = mnc = 0;
+        mLocaleList = LocaleList.getEmptyLocaleList();
         locale = null;
         userSetLocale = false;
         touchscreen = TOUCHSCREEN_UNDEFINED;
@@ -864,16 +884,20 @@
             changed |= ActivityInfo.CONFIG_MNC;
             mnc = delta.mnc;
         }
-        if (delta.locale != null
-                && (locale == null || !locale.equals(delta.locale))) {
+        fixUpLocaleList();
+        delta.fixUpLocaleList();
+        if (!delta.mLocaleList.isEmpty() && !mLocaleList.equals(delta.mLocaleList)) {
             changed |= ActivityInfo.CONFIG_LOCALE;
-            locale = delta.locale != null
-                    ? (Locale) delta.locale.clone() : null;
-            // If locale has changed, then layout direction is also changed ...
-            changed |= ActivityInfo.CONFIG_LAYOUT_DIRECTION;
-            // ... and we need to update the layout direction (represented by the first
-            // 2 most significant bits in screenLayout).
-            setLayoutDirection(locale);
+            mLocaleList = delta.mLocaleList;
+            // delta.locale can't be null, since delta.mLocaleList is not empty.
+            if (!delta.locale.equals(locale)) {
+                locale = (Locale) delta.locale.clone();
+                // If locale has changed, then layout direction is also changed ...
+                changed |= ActivityInfo.CONFIG_LAYOUT_DIRECTION;
+                // ... and we need to update the layout direction (represented by the first
+                // 2 most significant bits in screenLayout).
+                setLayoutDirection(locale);
+            }
         }
         final int deltaScreenLayoutDir = delta.screenLayout & SCREENLAYOUT_LAYOUTDIR_MASK;
         if (deltaScreenLayoutDir != SCREENLAYOUT_LAYOUTDIR_UNDEFINED &&
@@ -1023,8 +1047,9 @@
         if (delta.mnc != 0 && mnc != delta.mnc) {
             changed |= ActivityInfo.CONFIG_MNC;
         }
-        if (delta.locale != null
-                && (locale == null || !locale.equals(delta.locale))) {
+        fixUpLocaleList();
+        delta.fixUpLocaleList();
+        if (!delta.mLocaleList.isEmpty() && !mLocaleList.equals(delta.mLocaleList)) {
             changed |= ActivityInfo.CONFIG_LOCALE;
             changed |= ActivityInfo.CONFIG_LAYOUT_DIRECTION;
         }
@@ -1146,14 +1171,15 @@
         dest.writeFloat(fontScale);
         dest.writeInt(mcc);
         dest.writeInt(mnc);
-        if (locale == null) {
-            dest.writeInt(0);
-        } else {
-            dest.writeInt(1);
-            dest.writeString(locale.getLanguage());
-            dest.writeString(locale.getCountry());
-            dest.writeString(locale.getVariant());
+
+        fixUpLocaleList();
+        final int localeListSize = mLocaleList.size();
+        dest.writeInt(localeListSize);
+        for (int i = 0; i < localeListSize; ++i) {
+            final Locale l = mLocaleList.get(i);
+            dest.writeString(l.toLanguageTag());
         }
+
         if(userSetLocale) {
             dest.writeInt(1);
         } else {
@@ -1182,10 +1208,15 @@
         fontScale = source.readFloat();
         mcc = source.readInt();
         mnc = source.readInt();
-        if (source.readInt() != 0) {
-            locale = new Locale(source.readString(), source.readString(),
-                    source.readString());
+
+        final int localeListSize = source.readInt();
+        final Locale[] localeArray = new Locale[localeListSize];
+        for (int i = 0; i < localeListSize; ++i) {
+            localeArray[i] = Locale.forLanguageTag(source.readString());
         }
+        mLocaleList = new LocaleList(localeArray);
+        locale = mLocaleList.getPrimary();
+
         userSetLocale = (source.readInt()==1);
         touchscreen = source.readInt();
         keyboard = source.readInt();
@@ -1234,18 +1265,33 @@
         if (n != 0) return n;
         n = this.mnc - that.mnc;
         if (n != 0) return n;
-        if (this.locale == null) {
-            if (that.locale != null) return 1;
-        } else if (that.locale == null) {
+
+        fixUpLocaleList();
+        that.fixUpLocaleList();
+        // for backward compatibility, we consider an empty locale list to be greater
+        // than any non-empty locale list.
+        if (this.mLocaleList.isEmpty()) {
+            if (!that.mLocaleList.isEmpty()) return 1;
+        } else if (that.mLocaleList.isEmpty()) {
             return -1;
         } else {
-            n = this.locale.getLanguage().compareTo(that.locale.getLanguage());
-            if (n != 0) return n;
-            n = this.locale.getCountry().compareTo(that.locale.getCountry());
-            if (n != 0) return n;
-            n = this.locale.getVariant().compareTo(that.locale.getVariant());
+            final int minSize = Math.min(this.mLocaleList.size(), that.mLocaleList.size());
+            for (int i = 0; i < minSize; ++i) {
+                final Locale thisLocale = this.mLocaleList.get(i);
+                final Locale thatLocale = that.mLocaleList.get(i);
+                n = thisLocale.getLanguage().compareTo(thatLocale.getLanguage());
+                if (n != 0) return n;
+                n = thisLocale.getCountry().compareTo(thatLocale.getCountry());
+                if (n != 0) return n;
+                n = thisLocale.getVariant().compareTo(thatLocale.getVariant());
+                if (n != 0) return n;
+                n = thisLocale.toLanguageTag().compareTo(thatLocale.toLanguageTag());
+                if (n != 0) return n;
+            }
+            n = this.mLocaleList.size() - that.mLocaleList.size();
             if (n != 0) return n;
         }
+
         n = this.touchscreen - that.touchscreen;
         if (n != 0) return n;
         n = this.keyboard - that.keyboard;
@@ -1288,13 +1334,13 @@
         }
         return false;
     }
-    
+
     public int hashCode() {
         int result = 17;
         result = 31 * result + Float.floatToIntBits(fontScale);
         result = 31 * result + mcc;
         result = 31 * result + mnc;
-        result = 31 * result + (locale != null ? locale.hashCode() : 0);
+        result = 31 * result + mLocaleList.hashCode();
         result = 31 * result + touchscreen;
         result = 31 * result + keyboard;
         result = 31 * result + keyboardHidden;
@@ -1312,14 +1358,47 @@
     }
 
     /**
-     * Set the locale. This is the preferred way for setting up the locale (instead of using the
-     * direct accessor). This will also set the layout direction according to the locale.
+     * Get the locale list. This is the preferred way for getting the locales (instead of using
+     * the direct accessor to {@link #locale}, which would only provide the primary locale).
+     *
+     * @return The locale list.
+     */
+    public LocaleList getLocales() {
+        fixUpLocaleList();
+        return mLocaleList;
+    }
+
+    /**
+     * Set the locale list. This is the preferred way for setting up the locales (instead of using
+     * the direct accessor or {@link #setLocale(Locale)}). This will also set the layout direction
+     * according to the first locale in the list.
+     *
+     * Note that the layout direction will always come from the first locale in the locale list,
+     * even if the locale is not supported by the resources (the resources may only support
+     * another locale further down the list which has a different direction).
+     *
+     * @param locales The locale list. If null, an empty LocaleList will be assigned.
+     */
+    public void setLocales(@Nullable LocaleList locales) {
+        mLocaleList = locales == null ? LocaleList.getEmptyLocaleList() : locales;
+        locale = mLocaleList.getPrimary();
+        setLayoutDirection(locale);
+    }
+
+    /**
+     * Set the locale list to a list of just one locale. This will also set the layout direction
+     * according to the locale.
+     *
+     * Note that after this is run, calling <code>.equals()</code> on the input locale and the
+     * {@link #locale} attribute would return <code>true</code> if they are not null, but there is
+     * no guarantee that they would be the same object.
+     *
+     * See also the note about layout direction in {@link #setLocales(LocaleList)}.
      *
      * @param loc The locale. Can be null.
      */
-    public void setLocale(Locale loc) {
-        locale = loc;
-        setLayoutDirection(locale);
+    public void setLocale(@Nullable Locale loc) {
+        setLocales(new LocaleList(loc));
     }
 
     /**
@@ -1335,19 +1414,19 @@
     }
 
     /**
-     * Set the layout direction from the Locale.
+     * Set the layout direction from a Locale.
      *
-     * @param locale The Locale. If null will set the layout direction to
+     * @param loc The Locale. If null will set the layout direction to
      * {@link View#LAYOUT_DIRECTION_LTR}. If not null will set it to the layout direction
      * corresponding to the Locale.
      *
      * @see View#LAYOUT_DIRECTION_LTR
      * @see View#LAYOUT_DIRECTION_RTL
      */
-    public void setLayoutDirection(Locale locale) {
+    public void setLayoutDirection(Locale loc) {
         // There is a "1" difference between the configuration values for
         // layout direction and View constants for layout direction, just add "1".
-        final int layoutDirection = 1 + TextUtils.getLayoutDirectionFromLocale(locale);
+        final int layoutDirection = 1 + TextUtils.getLayoutDirectionFromLocale(loc);
         screenLayout = (screenLayout&~SCREENLAYOUT_LAYOUTDIR_MASK)|
                 (layoutDirection << SCREENLAYOUT_LAYOUTDIR_SHIFT);
     }
@@ -1370,21 +1449,21 @@
      *
      * @hide
      */
-    public static String localeToResourceQualifier(Locale locale) {
+    public static String localeToResourceQualifier(Locale loc) {
         StringBuilder sb = new StringBuilder();
-        boolean l = (locale.getLanguage().length() != 0);
-        boolean c = (locale.getCountry().length() != 0);
-        boolean s = (locale.getScript().length() != 0);
-        boolean v = (locale.getVariant().length() != 0);
-
+        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) {
-            sb.append(locale.getLanguage());
+            sb.append(loc.getLanguage());
             if (c) {
-                sb.append("-r").append(locale.getCountry());
+                sb.append("-r").append(loc.getCountry());
                 if (s) {
-                    sb.append("-s").append(locale.getScript());
+                    sb.append("-s").append(loc.getScript());
                     if (v) {
-                        sb.append("-v").append(locale.getVariant());
+                        sb.append("-v").append(loc.getVariant());
                     }
                 }
             }
@@ -1409,6 +1488,7 @@
             }
         }
 
+        // TODO: send the whole locale list
         if (config.locale != null && !config.locale.getLanguage().isEmpty()) {
             parts.add(localeToResourceQualifier(config.locale));
         }
@@ -1646,8 +1726,10 @@
             delta.mnc = change.mnc;
         }
 
-        if ((base.locale == null && change.locale != null) ||
-                (base.locale != null && !base.locale.equals(change.locale)))  {
+        base.fixUpLocaleList();
+        change.fixUpLocaleList();
+        if (!base.mLocaleList.equals(change.mLocaleList))  {
+            delta.mLocaleList = change.mLocaleList;
             delta.locale = change.locale;
         }
 
@@ -1724,7 +1806,7 @@
     private static final String XML_ATTR_FONT_SCALE = "fs";
     private static final String XML_ATTR_MCC = "mcc";
     private static final String XML_ATTR_MNC = "mnc";
-    private static final String XML_ATTR_LOCALE = "locale";
+    private static final String XML_ATTR_LOCALES = "locales";
     private static final String XML_ATTR_TOUCHSCREEN = "touch";
     private static final String XML_ATTR_KEYBOARD = "key";
     private static final String XML_ATTR_KEYBOARD_HIDDEN = "keyHid";
@@ -1754,10 +1836,9 @@
         configOut.mcc = XmlUtils.readIntAttribute(parser, XML_ATTR_MCC, 0);
         configOut.mnc = XmlUtils.readIntAttribute(parser, XML_ATTR_MNC, 0);
 
-        final String localeStr = XmlUtils.readStringAttribute(parser, XML_ATTR_LOCALE);
-        if (localeStr != null) {
-            configOut.locale = Locale.forLanguageTag(localeStr);
-        }
+        final String localesStr = XmlUtils.readStringAttribute(parser, XML_ATTR_LOCALES);
+        configOut.mLocaleList = LocaleList.forLanguageTags(localesStr);
+        configOut.locale = configOut.mLocaleList.getPrimary();
 
         configOut.touchscreen = XmlUtils.readIntAttribute(parser, XML_ATTR_TOUCHSCREEN,
                 TOUCHSCREEN_UNDEFINED);
@@ -1807,8 +1888,9 @@
         if (config.mnc != 0) {
             XmlUtils.writeIntAttribute(xml, XML_ATTR_MNC, config.mnc);
         }
-        if (config.locale != null) {
-            XmlUtils.writeStringAttribute(xml, XML_ATTR_LOCALE, config.locale.toLanguageTag());
+        config.fixUpLocaleList();
+        if (!config.mLocaleList.isEmpty()) {
+           XmlUtils.writeStringAttribute(xml, XML_ATTR_LOCALES, config.mLocaleList.toLanguageTags());
         }
         if (config.touchscreen != TOUCHSCREEN_UNDEFINED) {
             XmlUtils.writeIntAttribute(xml, XML_ATTR_TOUCHSCREEN, config.touchscreen);
diff --git a/core/java/android/hardware/fingerprint/FingerprintManager.java b/core/java/android/hardware/fingerprint/FingerprintManager.java
index 7cff11b..1f23c0a 100644
--- a/core/java/android/hardware/fingerprint/FingerprintManager.java
+++ b/core/java/android/hardware/fingerprint/FingerprintManager.java
@@ -392,6 +392,18 @@
     };
 
     /**
+     * @hide
+     */
+    public static abstract class LockoutResetCallback {
+
+        /**
+         * Called when lockout period expired and clients are allowed to listen for fingerprint
+         * again.
+         */
+        public void onLockoutReset() { }
+    };
+
+    /**
      * Request authentication of a crypto object. This call warms up the fingerprint hardware
      * and starts scanning for a fingerprint. It terminates when
      * {@link AuthenticationCallback#onAuthenticationError(int, CharSequence)} or
@@ -680,10 +692,37 @@
             try {
                 mService.resetTimeout(token);
             } catch (RemoteException e) {
-                Log.v(TAG, "Remote exception in getAuthenticatorId(): ", e);
+                Log.v(TAG, "Remote exception in resetTimeout(): ", e);
             }
         } else {
-            Log.w(TAG, "getAuthenticatorId(): Service not connected!");
+            Log.w(TAG, "resetTimeout(): Service not connected!");
+        }
+    }
+
+    /**
+     * @hide
+     */
+    public void addLockoutResetCallback(final LockoutResetCallback callback) {
+        if (mService != null) {
+            try {
+                mService.addLockoutResetCallback(
+                        new IFingerprintServiceLockoutResetCallback.Stub() {
+
+                    @Override
+                    public void onLockoutReset(long deviceId) throws RemoteException {
+                        mHandler.post(new Runnable() {
+                            @Override
+                            public void run() {
+                                callback.onLockoutReset();
+                            }
+                        });
+                    }
+                });
+            } catch (RemoteException e) {
+                Log.v(TAG, "Remote exception in addLockoutResetCallback(): ", e);
+            }
+        } else {
+            Log.w(TAG, "addLockoutResetCallback(): Service not connected!");
         }
     }
 
diff --git a/core/java/android/hardware/fingerprint/IFingerprintService.aidl b/core/java/android/hardware/fingerprint/IFingerprintService.aidl
index 3356354..690a751 100644
--- a/core/java/android/hardware/fingerprint/IFingerprintService.aidl
+++ b/core/java/android/hardware/fingerprint/IFingerprintService.aidl
@@ -17,6 +17,7 @@
 
 import android.os.Bundle;
 import android.hardware.fingerprint.IFingerprintServiceReceiver;
+import android.hardware.fingerprint.IFingerprintServiceLockoutResetCallback;
 import android.hardware.fingerprint.Fingerprint;
 import java.util.List;
 
@@ -71,4 +72,7 @@
 
     // Reset the timeout when user authenticates with strong auth (e.g. PIN, pattern or password)
     void resetTimeout(in byte [] cryptoToken);
+
+    // Add a callback which gets notified when the fingerprint lockout period expired.
+    void addLockoutResetCallback(IFingerprintServiceLockoutResetCallback callback);
 }
diff --git a/core/java/android/hardware/fingerprint/IFingerprintServiceLockoutResetCallback.aidl b/core/java/android/hardware/fingerprint/IFingerprintServiceLockoutResetCallback.aidl
new file mode 100644
index 0000000..c9a5d59
--- /dev/null
+++ b/core/java/android/hardware/fingerprint/IFingerprintServiceLockoutResetCallback.aidl
@@ -0,0 +1,28 @@
+/*
+ * 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 android.hardware.fingerprint;
+
+import android.hardware.fingerprint.Fingerprint;
+import android.os.Bundle;
+import android.os.UserHandle;
+
+/**
+ * Callback when lockout period expired and clients are allowed to authenticate again.
+ * @hide
+ */
+oneway interface IFingerprintServiceLockoutResetCallback {
+    void onLockoutReset(long deviceId);
+}
diff --git a/core/java/android/hardware/input/IInputManager.aidl b/core/java/android/hardware/input/IInputManager.aidl
index 465d142..c8b45c7 100644
--- a/core/java/android/hardware/input/IInputManager.aidl
+++ b/core/java/android/hardware/input/IInputManager.aidl
@@ -19,6 +19,7 @@
 import android.hardware.input.InputDeviceIdentifier;
 import android.hardware.input.KeyboardLayout;
 import android.hardware.input.IInputDevicesChangedListener;
+import android.hardware.input.ITabletModeChangedListener;
 import android.hardware.input.TouchCalibration;
 import android.os.IBinder;
 import android.view.InputDevice;
@@ -60,6 +61,9 @@
     // Registers an input devices changed listener.
     void registerInputDevicesChangedListener(IInputDevicesChangedListener listener);
 
+    // Registers a tablet mode change listener
+    void registerTabletModeChangedListener(ITabletModeChangedListener listener);
+
     // Input device vibrator control.
     void vibrate(int deviceId, in long[] pattern, int repeat, IBinder token);
     void cancelVibrate(int deviceId, IBinder token);
diff --git a/core/java/android/hardware/input/ITabletModeChangedListener.aidl b/core/java/android/hardware/input/ITabletModeChangedListener.aidl
new file mode 100644
index 0000000..a8559a7
--- /dev/null
+++ b/core/java/android/hardware/input/ITabletModeChangedListener.aidl
@@ -0,0 +1,23 @@
+/*
+ * 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 android.hardware.input;
+
+/** @hide */
+interface ITabletModeChangedListener {
+    /* Called when the device enters or exits tablet mode. */
+    oneway void onTabletModeChanged(long whenNanos, boolean inTabletMode);
+}
diff --git a/core/java/android/hardware/input/InputManager.java b/core/java/android/hardware/input/InputManager.java
index 444f020..bae5757 100644
--- a/core/java/android/hardware/input/InputManager.java
+++ b/core/java/android/hardware/input/InputManager.java
@@ -16,6 +16,7 @@
 
 package android.hardware.input;
 
+import com.android.internal.os.SomeArgs;
 import com.android.internal.util.ArrayUtils;
 
 import android.annotation.SdkConstant;
@@ -29,6 +30,7 @@
 import android.os.Message;
 import android.os.RemoteException;
 import android.os.ServiceManager;
+import android.os.SystemClock;
 import android.os.Vibrator;
 import android.provider.Settings;
 import android.provider.Settings.SettingNotFoundException;
@@ -38,6 +40,7 @@
 import android.view.InputEvent;
 
 import java.util.ArrayList;
+import java.util.List;
 
 /**
  * Provides information about input devices and available key layouts.
@@ -67,6 +70,11 @@
     private final ArrayList<InputDeviceListenerDelegate> mInputDeviceListeners =
             new ArrayList<InputDeviceListenerDelegate>();
 
+    // Guarded by mTabletModeLock
+    private final Object mTabletModeLock = new Object();
+    private TabletModeChangedListener mTabletModeChangedListener;
+    private List<OnTabletModeChangedListenerDelegate> mOnTabletModeChangedListeners;
+
     /**
      * Broadcast Action: Query available keyboard layouts.
      * <p>
@@ -291,6 +299,7 @@
         }
 
         synchronized (mInputDevicesLock) {
+            populateInputDevicesLocked();
             int index = findInputDeviceListenerLocked(listener);
             if (index < 0) {
                 mInputDeviceListeners.add(new InputDeviceListenerDelegate(listener, handler));
@@ -331,6 +340,72 @@
     }
 
     /**
+     * Register a tablet mode changed listener.
+     *
+     * @param listener The listener to register.
+     * @param handler The handler on which the listener should be invoked, or null
+     * if the listener should be invoked on the calling thread's looper.
+     * @hide
+     */
+    public void registerOnTabletModeChangedListener(
+            OnTabletModeChangedListener listener, Handler handler) {
+        if (listener == null) {
+            throw new IllegalArgumentException("listener must not be null");
+        }
+        synchronized (mTabletModeLock) {
+            if (mOnTabletModeChangedListeners == null) {
+                initializeTabletModeListenerLocked();
+            }
+            int idx = findOnTabletModeChangedListenerLocked(listener);
+            if (idx < 0) {
+                OnTabletModeChangedListenerDelegate d =
+                    new OnTabletModeChangedListenerDelegate(listener, handler);
+                mOnTabletModeChangedListeners.add(d);
+            }
+        }
+    }
+
+    /**
+     * Unregister a tablet mode changed listener.
+     *
+     * @param listener The listener to unregister.
+     * @hide
+     */
+    public void unregisterOnTabletModeChangedListener(OnTabletModeChangedListener listener) {
+        if (listener == null) {
+            throw new IllegalArgumentException("listener must not be null");
+        }
+        synchronized (mTabletModeLock) {
+            int idx = findOnTabletModeChangedListenerLocked(listener);
+            if (idx >= 0) {
+                OnTabletModeChangedListenerDelegate d = mOnTabletModeChangedListeners.remove(idx);
+                d.removeCallbacksAndMessages(null);
+            }
+        }
+    }
+
+    private void initializeTabletModeListenerLocked() {
+        final TabletModeChangedListener listener = new TabletModeChangedListener();
+        try {
+            mIm.registerTabletModeChangedListener(listener);
+        } catch (RemoteException ex) {
+            throw new RuntimeException("Could not register tablet mode changed listener", ex);
+        }
+        mTabletModeChangedListener = listener;
+        mOnTabletModeChangedListeners = new ArrayList<>();
+    }
+
+    private int findOnTabletModeChangedListenerLocked(OnTabletModeChangedListener listener) {
+        final int N = mOnTabletModeChangedListeners.size();
+        for (int i = 0; i < N; i++) {
+            if (mOnTabletModeChangedListeners.get(i).mListener == listener) {
+                return i;
+            }
+        }
+        return -1;
+    }
+
+    /**
      * Gets information about all supported keyboard layouts.
      * <p>
      * The input manager consults the built-in keyboard layouts as well
@@ -769,6 +844,22 @@
         return false;
     }
 
+
+    private void onTabletModeChanged(long whenNanos, boolean inTabletMode) {
+        if (DEBUG) {
+            Log.d(TAG, "Received tablet mode changed: "
+                    + "whenNanos=" + whenNanos + ", inTabletMode=" + inTabletMode);
+        }
+        synchronized (mTabletModeLock) {
+            final int N = mOnTabletModeChangedListeners.size();
+            for (int i = 0; i < N; i++) {
+                OnTabletModeChangedListenerDelegate listener =
+                        mOnTabletModeChangedListeners.get(i);
+                listener.sendTabletModeChanged(whenNanos, inTabletMode);
+            }
+        }
+    }
+
     /**
      * Gets a vibrator service associated with an input device, assuming it has one.
      * @return The vibrator, never null.
@@ -838,6 +929,57 @@
         }
     }
 
+    /** @hide */
+    public interface OnTabletModeChangedListener {
+        /**
+         * Called whenever the device goes into or comes out of tablet mode.
+         *
+         * @param whenNanos The time at which the device transitioned into or
+         * out of tablet mode. This is given in nanoseconds in the
+         * {@link SystemClock#uptimeMillis} time base.
+         */
+        void onTabletModeChanged(long whenNanos, boolean inTabletMode);
+    }
+
+    private final class TabletModeChangedListener extends ITabletModeChangedListener.Stub {
+        @Override
+        public void onTabletModeChanged(long whenNanos, boolean inTabletMode) {
+            InputManager.this.onTabletModeChanged(whenNanos, inTabletMode);
+        }
+    }
+
+    private static final class OnTabletModeChangedListenerDelegate extends Handler {
+        private static final int MSG_TABLET_MODE_CHANGED = 0;
+
+        public final OnTabletModeChangedListener mListener;
+
+        public OnTabletModeChangedListenerDelegate(
+                OnTabletModeChangedListener listener, Handler handler) {
+            super(handler != null ? handler.getLooper() : Looper.myLooper());
+            mListener = listener;
+        }
+
+        public void sendTabletModeChanged(long whenNanos, boolean inTabletMode) {
+            SomeArgs args = SomeArgs.obtain();
+            args.argi1 = (int) (whenNanos & 0xFFFFFFFF);
+            args.argi2 = (int) (whenNanos >> 32);
+            args.arg1 = (Boolean) inTabletMode;
+            obtainMessage(MSG_TABLET_MODE_CHANGED, args).sendToTarget();
+        }
+
+        @Override
+        public void handleMessage(Message msg) {
+            switch (msg.what) {
+                case MSG_TABLET_MODE_CHANGED:
+                    SomeArgs args = (SomeArgs) msg.obj;
+                    long whenNanos = (args.argi1 & 0xFFFFFFFFl) | ((long) args.argi2 << 32);
+                    boolean inTabletMode = (boolean) args.arg1;
+                    mListener.onTabletModeChanged(whenNanos, inTabletMode);
+                    break;
+            }
+        }
+    }
+
     private final class InputDeviceVibrator extends Vibrator {
         private final int mDeviceId;
         private final Binder mToken;
diff --git a/core/java/android/net/ConnectivityManager.java b/core/java/android/net/ConnectivityManager.java
index ec0cc6d..d4e6c82 100644
--- a/core/java/android/net/ConnectivityManager.java
+++ b/core/java/android/net/ConnectivityManager.java
@@ -100,6 +100,16 @@
     public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
 
     /**
+     * A temporary hack until SUPL system can get off the legacy APIS.
+     * They do too many network requests and the long list of apps listening
+     * and waking due to the CONNECTIVITY_ACTION bcast makes it expensive.
+     * Use this bcast intent instead for SUPL requests.
+     * @hide
+     */
+    public static final String CONNECTIVITY_ACTION_SUPL =
+            "android.net.conn.CONNECTIVITY_CHANGE_SUPL";
+
+    /**
      * The device has connected to a network that has presented a captive
      * portal, which is blocking Internet connectivity. The user was presented
      * with a notification that network sign in is required,
diff --git a/core/java/android/os/BatteryStats.java b/core/java/android/os/BatteryStats.java
index 4ad9d6d..bad94fc 100644
--- a/core/java/android/os/BatteryStats.java
+++ b/core/java/android/os/BatteryStats.java
@@ -468,6 +468,7 @@
          * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
          * @see BatteryStats#getCpuSpeedSteps()
          */
+        @Deprecated
         public abstract long getTimeAtCpuSpeed(int step, int which);
 
         public static abstract class Sensor {
diff --git a/core/java/android/os/PowerManagerInternal.java b/core/java/android/os/PowerManagerInternal.java
index e742f98..70cff00 100644
--- a/core/java/android/os/PowerManagerInternal.java
+++ b/core/java/android/os/PowerManagerInternal.java
@@ -108,6 +108,12 @@
     public abstract void setUserActivityTimeoutOverrideFromWindowManager(long timeoutMillis);
 
     /**
+     * Used by the window manager to tell the power manager that the user is no longer actively
+     * using the device.
+     */
+    public abstract void setUserInactiveOverrideFromWindowManager();
+
+    /**
      * Used by device administration to set the maximum screen off timeout.
      *
      * This method must only be called by the device administration policy manager.
diff --git a/core/java/android/os/Trace.java b/core/java/android/os/Trace.java
index 4da88ee..7529c52 100644
--- a/core/java/android/os/Trace.java
+++ b/core/java/android/os/Trace.java
@@ -77,6 +77,8 @@
     public static final long TRACE_TAG_POWER = 1L << 17;
     /** @hide */
     public static final long TRACE_TAG_PACKAGE_MANAGER = 1L << 18;
+    /** @hide */
+    public static final long TRACE_TAG_SYSTEM_SERVER = 1L << 19;
 
     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/util/LocaleList.java b/core/java/android/util/LocaleList.java
index 017735a..379651e 100644
--- a/core/java/android/util/LocaleList.java
+++ b/core/java/android/util/LocaleList.java
@@ -16,15 +16,19 @@
 
 package android.util;
 
+import android.annotation.NonNull;
 import android.annotation.Nullable;
+import android.annotation.Size;
+
+import com.android.internal.annotations.GuardedBy;
 
 import java.util.HashSet;
 import java.util.Locale;
 
 // TODO: We don't except too many LocaleLists to exist at the same time, and
 // we need access to the data at native level, so we should pass the data
-// down to the native level, create a mapt of every list seen there, take a
-// pointer back, and just keep that pointed in the Java-level object, so
+// down to the native level, create a map of every list seen there, take a
+// pointer back, and just keep that pointer in the Java-level object, so
 // things could be copied very quickly.
 
 /**
@@ -34,6 +38,7 @@
 public final class LocaleList {
     private final Locale[] mList;
     private static final Locale[] sEmptyList = new Locale[0];
+    private static final LocaleList sEmptyLocaleList = new LocaleList();
 
     public Locale get(int location) {
         return location < mList.length ? mList[location] : null;
@@ -51,6 +56,60 @@
         return mList.length;
     }
 
+    @Override
+    public boolean equals(Object other) {
+        if (other == this)
+            return true;
+        if (!(other instanceof LocaleList))
+            return false;
+        final Locale[] otherList = ((LocaleList) other).mList;
+        if (mList.length != otherList.length)
+            return false;
+        for (int i = 0; i < mList.length; ++i) {
+            if (!mList[i].equals(otherList[i]))
+                return false;
+        }
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = 1;
+        for (int i = 0; i < mList.length; ++i) {
+            result = 31 * result + mList[i].hashCode();
+        }
+        return result;
+    }
+
+    @Override
+    public String toString() {
+        StringBuilder sb = new StringBuilder();
+        sb.append("[");
+        for (int i = 0; i < mList.length; ++i) {
+            sb.append(mList[i]);
+            if (i < mList.length - 1) {
+                sb.append(',');
+            }
+        }
+        sb.append("]");
+        return sb.toString();
+    }
+
+    public String toLanguageTags() {
+        StringBuilder sb = new StringBuilder();
+        for (int i = 0; i < mList.length; ++i) {
+            sb.append(mList[i].toLanguageTag());
+            if (i < mList.length - 1) {
+                sb.append(',');
+            }
+        }
+        return sb.toString();
+    }
+
+    /**
+     * It is almost always better to call {@link #getEmptyLocaleList()} instead which returns
+     * a pre-constructed empty locale list.
+     */
     public LocaleList() {
         mList = sEmptyList;
     }
@@ -59,6 +118,19 @@
      * @throws NullPointerException if any of the input locales is <code>null</code>.
      * @throws IllegalArgumentException if any of the input locales repeat.
      */
+    public LocaleList(@Nullable Locale locale) {
+        if (locale == null) {
+            mList = sEmptyList;
+        } else {
+            mList = new Locale[1];
+            mList[0] = (Locale) locale.clone();
+        }
+    }
+
+    /**
+     * @throws NullPointerException if any of the input locales is <code>null</code>.
+     * @throws IllegalArgumentException if any of the input locales repeat.
+     */
     public LocaleList(@Nullable Locale[] list) {
         if (list == null || list.length == 0) {
             mList = sEmptyList;
@@ -79,4 +151,39 @@
             mList = localeList;
         }
     }
+
+    public static LocaleList getEmptyLocaleList() {
+        return sEmptyLocaleList;
+    }
+
+    public static LocaleList forLanguageTags(@Nullable String list) {
+        if (list == null || list.equals("")) {
+            return getEmptyLocaleList();
+        } else {
+            final String[] tags = list.split(",");
+            final Locale[] localeArray = new Locale[tags.length];
+            for (int i = 0; i < localeArray.length; ++i) {
+                localeArray[i] = Locale.forLanguageTag(tags[i]);
+            }
+            return new LocaleList(localeArray);
+        }
+    }
+
+    private final static Object sLock = new Object();
+
+    @GuardedBy("sLock")
+    private static LocaleList sDefaultLocaleList;
+
+    // TODO: fix this to return the default system locale list once we have that
+    @NonNull @Size(min=1)
+    public static LocaleList getDefault() {
+        Locale defaultLocale = Locale.getDefault();
+        synchronized (sLock) {
+            if (sDefaultLocaleList == null || sDefaultLocaleList.size() != 1
+                    || !defaultLocale.equals(sDefaultLocaleList.getPrimary())) {
+                sDefaultLocaleList = new LocaleList(defaultLocale);
+            }
+        }
+        return sDefaultLocaleList;
+    }
 }
diff --git a/core/java/android/view/HandlerActionQueue.java b/core/java/android/view/HandlerActionQueue.java
new file mode 100644
index 0000000..4758a34
--- /dev/null
+++ b/core/java/android/view/HandlerActionQueue.java
@@ -0,0 +1,127 @@
+/*
+ * 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 android.view;
+
+import com.android.internal.util.GrowingArrayUtils;
+
+import android.os.Handler;
+
+import java.util.ArrayList;
+
+/**
+ * Class used to enqueue pending work from Views when no Handler is attached.
+ *
+ * @hide Exposed for test framework only.
+ */
+public class HandlerActionQueue {
+    private HandlerAction[] mActions;
+    private int mCount;
+
+    public void post(Runnable action) {
+        postDelayed(action, 0);
+    }
+
+    public void postDelayed(Runnable action, long delayMillis) {
+        final HandlerAction handlerAction = new HandlerAction(action, delayMillis);
+
+        synchronized (this) {
+            if (mActions == null) {
+                mActions = new HandlerAction[4];
+            }
+            mActions = GrowingArrayUtils.append(mActions, mCount, handlerAction);
+            mCount++;
+        }
+    }
+
+    public void removeCallbacks(Runnable action) {
+        synchronized (this) {
+            final int count = mCount;
+            int j = 0;
+
+            final HandlerAction[] actions = mActions;
+            for (int i = 0; i < count; i++) {
+                if (actions[i].matches(action)) {
+                    // Remove this action by overwriting it within
+                    // this loop or nulling it out later.
+                    continue;
+                }
+
+                if (j != i) {
+                    // At least one previous entry was removed, so
+                    // this one needs to move to the "new" list.
+                    actions[j] = actions[i];
+                }
+
+                j++;
+            }
+
+            // The "new" list only has j entries.
+            mCount = j;
+
+            // Null out any remaining entries.
+            for (; j < count; j++) {
+                actions[j] = null;
+            }
+        }
+    }
+
+    public void executeActions(Handler handler) {
+        synchronized (this) {
+            final HandlerAction[] actions = mActions;
+            for (int i = 0, count = mCount; i < count; i++) {
+                final HandlerAction handlerAction = actions[i];
+                handler.postDelayed(handlerAction.action, handlerAction.delay);
+            }
+
+            mActions = null;
+            mCount = 0;
+        }
+    }
+
+    public int size() {
+        return mCount;
+    }
+
+    public Runnable getRunnable(int index) {
+        if (index >= mCount) {
+            throw new IndexOutOfBoundsException();
+        }
+        return mActions[index].action;
+    }
+
+    public long getDelay(int index) {
+        if (index >= mCount) {
+            throw new IndexOutOfBoundsException();
+        }
+        return mActions[index].delay;
+    }
+
+    private static class HandlerAction {
+        final Runnable action;
+        final long delay;
+
+        public HandlerAction(Runnable action, long delay) {
+            this.action = action;
+            this.delay = delay;
+        }
+
+        public boolean matches(Runnable otherAction) {
+            return otherAction == null && action == null
+                    || action != null && action.equals(otherAction);
+        }
+    }
+}
diff --git a/core/java/android/view/IWindowManager.aidl b/core/java/android/view/IWindowManager.aidl
index f86adfe..33c51ff 100644
--- a/core/java/android/view/IWindowManager.aidl
+++ b/core/java/android/view/IWindowManager.aidl
@@ -97,21 +97,21 @@
      * @param launchTaskBehind True if the token is been launched from behind.
      * @param taskBounds Bounds to use when creating a new Task with the input task Id if
      *                   the task doesn't exist yet.
-     * @return The configuration of the task if it was newly created. null otherwise.
+     * @param configuration Configuration that is being used with this task.
      */
-    Configuration addAppToken(int addPos, IApplicationToken token, int taskId, int stackId,
+    void addAppToken(int addPos, IApplicationToken token, int taskId, int stackId,
             int requestedOrientation, boolean fullscreen, boolean showWhenLocked, int userId,
             int configChanges, boolean voiceInteraction, boolean launchTaskBehind,
-            in Rect taskBounds);
+            in Rect taskBounds, in Configuration configuration);
     /**
      *
      * @param token The token we are adding to the input task Id.
      * @param taskId The Id of the task we are adding the token to.
      * @param taskBounds Bounds to use when creating a new Task with the input task Id if
      *                   the task doesn't exist yet.
-     * @return The configuration of the task if it was newly created. null otherwise.
+     * @param config Configuration that is being used with this task.
      */
-    Configuration setAppTask(IBinder token, int taskId, in Rect taskBounds);
+    void setAppTask(IBinder token, int taskId, in Rect taskBounds, in Configuration config);
     void setAppOrientation(IApplicationToken token, int requestedOrientation);
     int getAppOrientation(IApplicationToken token);
     void setFocusedApp(IBinder token, boolean moveFocusNow);
diff --git a/core/java/android/view/KeyEvent.java b/core/java/android/view/KeyEvent.java
index 6d89824..1c20cab 100644
--- a/core/java/android/view/KeyEvent.java
+++ b/core/java/android/view/KeyEvent.java
@@ -788,8 +788,10 @@
     /** Key code constant: Step backward media key.
      * Steps media backward, one frame at a time. */
     public static final int KEYCODE_MEDIA_STEP_BACKWARD = 275;
+    /** Key code constant: put device to sleep unless a wakelock is held. */
+    public static final int KEYCODE_SOFT_SLEEP = 276;
 
-    private static final int LAST_KEYCODE = KEYCODE_MEDIA_STEP_BACKWARD;
+    private static final int LAST_KEYCODE = KEYCODE_SOFT_SLEEP;
 
     // NOTE: If you add a new keycode here you must also add it to:
     //  isSystem()
diff --git a/core/java/android/view/SurfaceControl.java b/core/java/android/view/SurfaceControl.java
index 5970c3f..bcf9b2c 100644
--- a/core/java/android/view/SurfaceControl.java
+++ b/core/java/android/view/SurfaceControl.java
@@ -484,6 +484,7 @@
         public boolean secure;
         public long appVsyncOffsetNanos;
         public long presentationDeadlineNanos;
+        public int colorTransform;
 
         public PhysicalDisplayInfo() {
         }
@@ -507,7 +508,8 @@
                     && yDpi == other.yDpi
                     && secure == other.secure
                     && appVsyncOffsetNanos == other.appVsyncOffsetNanos
-                    && presentationDeadlineNanos == other.presentationDeadlineNanos;
+                    && presentationDeadlineNanos == other.presentationDeadlineNanos
+                    && colorTransform == other.colorTransform;
         }
 
         @Override
@@ -525,6 +527,7 @@
             secure = other.secure;
             appVsyncOffsetNanos = other.appVsyncOffsetNanos;
             presentationDeadlineNanos = other.presentationDeadlineNanos;
+            colorTransform = other.colorTransform;
         }
 
         // For debugging purposes
@@ -533,7 +536,8 @@
             return "PhysicalDisplayInfo{" + width + " x " + height + ", " + refreshRate + " fps, "
                     + "density " + density + ", " + xDpi + " x " + yDpi + " dpi, secure " + secure
                     + ", appVsyncOffset " + appVsyncOffsetNanos
-                    + ", bufferDeadline " + presentationDeadlineNanos + "}";
+                    + ", bufferDeadline " + presentationDeadlineNanos
+                    + ", colorTransform " + colorTransform + "}";
         }
     }
 
diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java
index 16d9bf3..ed48319 100644
--- a/core/java/android/view/View.java
+++ b/core/java/android/view/View.java
@@ -3823,6 +3823,12 @@
     private static SparseArray<String> mAttributeMap;
 
     /**
+     * Queue of pending runnables. Used to postpone calls to post() until this
+     * view is attached and has a handler.
+     */
+    private HandlerActionQueue mRunQueue;
+
+    /**
      * @hide
      */
     String mStartActivityRequestWho;
@@ -6681,14 +6687,15 @@
     }
 
     /**
-     * Gets the {@link View} description. It briefly describes the view and is
-     * primarily used for accessibility support. Set this property to enable
-     * better accessibility support for your application. This is especially
-     * true for views that do not have textual representation (For example,
-     * ImageButton).
+     * Returns the {@link View}'s content description.
+     * <p>
+     * <strong>Note:</strong> Do not override this method, as it will have no
+     * effect on the content description presented to accessibility services.
+     * You must call {@link #setContentDescription(CharSequence)} to modify the
+     * content description.
      *
-     * @return The content description.
-     *
+     * @return the content description
+     * @see #setContentDescription(CharSequence)
      * @attr ref android.R.styleable#View_contentDescription
      */
     @ViewDebug.ExportedProperty(category = "accessibility")
@@ -6697,14 +6704,19 @@
     }
 
     /**
-     * Sets the {@link View} description. It briefly describes the view and is
-     * primarily used for accessibility support. Set this property to enable
-     * better accessibility support for your application. This is especially
-     * true for views that do not have textual representation (For example,
-     * ImageButton).
+     * Sets the {@link View}'s content description.
+     * <p>
+     * A content description briefly describes the view and is primarily used
+     * for accessibility support to determine how a view should be presented to
+     * the user. In the case of a view with no textual representation, such as
+     * {@link android.widget.ImageButton}, a useful content description
+     * explains what the view does. For example, an image button with a phone
+     * icon that is used to place a call may use "Call" as its content
+     * description. An image of a floppy disk that is used to save a file may
+     * use "Save".
      *
      * @param contentDescription The content description.
-     *
+     * @see #getContentDescription()
      * @attr ref android.R.styleable#View_contentDescription
      */
     @RemotableViewMethod
@@ -13010,6 +13022,18 @@
     }
 
     /**
+     * Returns the queue of runnable for this view.
+     *
+     * @return the queue of runnables for this view
+     */
+    private HandlerActionQueue getRunQueue() {
+        if (mRunQueue == null) {
+            mRunQueue = new HandlerActionQueue();
+        }
+        return mRunQueue;
+    }
+
+    /**
      * Gets the view root associated with the View.
      * @return The view root, or null if none.
      * @hide
@@ -13046,8 +13070,10 @@
         if (attachInfo != null) {
             return attachInfo.mHandler.post(action);
         }
-        // Assume that post will succeed later
-        ViewRootImpl.getRunQueue().post(action);
+
+        // Postpone the runnable until we know on which thread it needs to run.
+        // Assume that the runnable will be successfully placed after attach.
+        getRunQueue().post(action);
         return true;
     }
 
@@ -13075,8 +13101,10 @@
         if (attachInfo != null) {
             return attachInfo.mHandler.postDelayed(action, delayMillis);
         }
-        // Assume that post will succeed later
-        ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
+
+        // Postpone the runnable until we know on which thread it needs to run.
+        // Assume that the runnable will be successfully placed after attach.
+        getRunQueue().postDelayed(action, delayMillis);
         return true;
     }
 
@@ -13095,8 +13123,9 @@
             attachInfo.mViewRootImpl.mChoreographer.postCallback(
                     Choreographer.CALLBACK_ANIMATION, action, null);
         } else {
-            // Assume that post will succeed later
-            ViewRootImpl.getRunQueue().post(action);
+            // Postpone the runnable until we know
+            // on which thread it needs to run.
+            getRunQueue().post(action);
         }
     }
 
@@ -13118,8 +13147,9 @@
             attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
                     Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
         } else {
-            // Assume that post will succeed later
-            ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
+            // Postpone the runnable until we know
+            // on which thread it needs to run.
+            getRunQueue().postDelayed(action, delayMillis);
         }
     }
 
@@ -13146,8 +13176,7 @@
                 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
                         Choreographer.CALLBACK_ANIMATION, action, null);
             }
-            // Assume that post will succeed later
-            ViewRootImpl.getRunQueue().removeCallbacks(action);
+            getRunQueue().removeCallbacks(action);
         }
         return true;
     }
@@ -14565,7 +14594,6 @@
      *        this view
      */
     void dispatchAttachedToWindow(AttachInfo info, int visibility) {
-        //System.out.println("Attached! " + this);
         mAttachInfo = info;
         if (mOverlay != null) {
             mOverlay.getOverlayView().dispatchAttachedToWindow(info, visibility);
@@ -14581,6 +14609,11 @@
             mAttachInfo.mScrollContainers.add(this);
             mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
         }
+        // Transfer all pending runnables.
+        if (mRunQueue != null) {
+            mRunQueue.executeActions(info.mHandler);
+            mRunQueue = null;
+        }
         performCollectViewAttributes(mAttachInfo, visibility);
         onAttachedToWindow();
 
@@ -16866,7 +16899,9 @@
                         Choreographer.CALLBACK_ANIMATION, what, who,
                         Choreographer.subtractFrameDelay(delay));
             } else {
-                ViewRootImpl.getRunQueue().postDelayed(what, delay);
+                // Postpone the runnable until we know
+                // on which thread it needs to run.
+                getRunQueue().postDelayed(what, delay);
             }
         }
     }
@@ -16884,7 +16919,7 @@
                 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
                         Choreographer.CALLBACK_ANIMATION, what, who);
             }
-            ViewRootImpl.getRunQueue().removeCallbacks(what);
+            getRunQueue().removeCallbacks(what);
         }
     }
 
diff --git a/core/java/android/view/ViewRootImpl.java b/core/java/android/view/ViewRootImpl.java
index 8bbaf36..d26e914 100644
--- a/core/java/android/view/ViewRootImpl.java
+++ b/core/java/android/view/ViewRootImpl.java
@@ -28,7 +28,6 @@
 import android.content.res.Resources;
 import android.graphics.Canvas;
 import android.graphics.Matrix;
-import android.graphics.Paint;
 import android.graphics.PixelFormat;
 import android.graphics.Point;
 import android.graphics.PointF;
@@ -131,7 +130,7 @@
      */
     static final int MAX_TRACKBALL_DELAY = 250;
 
-    static final ThreadLocal<RunQueue> sRunQueues = new ThreadLocal<RunQueue>();
+    static final ThreadLocal<HandlerActionQueue> sRunQueues = new ThreadLocal<HandlerActionQueue>();
 
     static final ArrayList<Runnable> sFirstDrawHandlers = new ArrayList<Runnable>();
     static boolean sFirstDrawComplete = false;
@@ -6759,90 +6758,17 @@
         }
     }
 
-    static RunQueue getRunQueue() {
-        RunQueue rq = sRunQueues.get();
+    static HandlerActionQueue getRunQueue() {
+        HandlerActionQueue rq = sRunQueues.get();
         if (rq != null) {
             return rq;
         }
-        rq = new RunQueue();
+        rq = new HandlerActionQueue();
         sRunQueues.set(rq);
         return rq;
     }
 
     /**
-     * The run queue is used to enqueue pending work from Views when no Handler is
-     * attached.  The work is executed during the next call to performTraversals on
-     * the thread.
-     * @hide
-     */
-    static final class RunQueue {
-        private final ArrayList<HandlerAction> mActions = new ArrayList<HandlerAction>();
-
-        void post(Runnable action) {
-            postDelayed(action, 0);
-        }
-
-        void postDelayed(Runnable action, long delayMillis) {
-            HandlerAction handlerAction = new HandlerAction();
-            handlerAction.action = action;
-            handlerAction.delay = delayMillis;
-
-            synchronized (mActions) {
-                mActions.add(handlerAction);
-            }
-        }
-
-        void removeCallbacks(Runnable action) {
-            final HandlerAction handlerAction = new HandlerAction();
-            handlerAction.action = action;
-
-            synchronized (mActions) {
-                final ArrayList<HandlerAction> actions = mActions;
-
-                while (actions.remove(handlerAction)) {
-                    // Keep going
-                }
-            }
-        }
-
-        void executeActions(Handler handler) {
-            synchronized (mActions) {
-                final ArrayList<HandlerAction> actions = mActions;
-                final int count = actions.size();
-
-                for (int i = 0; i < count; i++) {
-                    final HandlerAction handlerAction = actions.get(i);
-                    handler.postDelayed(handlerAction.action, handlerAction.delay);
-                }
-
-                actions.clear();
-            }
-        }
-
-        private static class HandlerAction {
-            Runnable action;
-            long delay;
-
-            @Override
-            public boolean equals(Object o) {
-                if (this == o) return true;
-                if (o == null || getClass() != o.getClass()) return false;
-
-                HandlerAction that = (HandlerAction) o;
-                return !(action != null ? !action.equals(that.action) : that.action != null);
-
-            }
-
-            @Override
-            public int hashCode() {
-                int result = action != null ? action.hashCode() : 0;
-                result = 31 * result + (int) (delay ^ (delay >>> 32));
-                return result;
-            }
-        }
-    }
-
-    /**
      * Class for managing the accessibility interaction connection
      * based on the global accessibility state.
      */
diff --git a/core/java/android/view/accessibility/AccessibilityManager.java b/core/java/android/view/accessibility/AccessibilityManager.java
index e77b862..72971e8 100644
--- a/core/java/android/view/accessibility/AccessibilityManager.java
+++ b/core/java/android/view/accessibility/AccessibilityManager.java
@@ -199,10 +199,7 @@
                 } else {
                     userId = UserHandle.myUserId();
                 }
-                IBinder iBinder = ServiceManager.getService(Context.ACCESSIBILITY_SERVICE);
-                IAccessibilityManager service = iBinder == null
-                        ? null : IAccessibilityManager.Stub.asInterface(iBinder);
-                sInstance = new AccessibilityManager(context, service, userId);
+                sInstance = new AccessibilityManager(context, null, userId);
             }
         }
         return sInstance;
@@ -219,10 +216,9 @@
      */
     public AccessibilityManager(Context context, IAccessibilityManager service, int userId) {
         mHandler = new MyHandler(context.getMainLooper());
-        mService = service;
         mUserId = userId;
         synchronized (mLock) {
-            tryConnectToServiceLocked();
+            tryConnectToServiceLocked(service);
         }
     }
 
@@ -612,17 +608,20 @@
 
     private  IAccessibilityManager getServiceLocked() {
         if (mService == null) {
-            tryConnectToServiceLocked();
+            tryConnectToServiceLocked(null);
         }
         return mService;
     }
 
-    private void tryConnectToServiceLocked() {
-        IBinder iBinder = ServiceManager.getService(Context.ACCESSIBILITY_SERVICE);
-        if (iBinder == null) {
-            return;
+    private void tryConnectToServiceLocked(IAccessibilityManager service) {
+        if (service == null) {
+            IBinder iBinder = ServiceManager.getService(Context.ACCESSIBILITY_SERVICE);
+            if (iBinder == null) {
+                return;
+            }
+            service = IAccessibilityManager.Stub.asInterface(iBinder);
         }
-        IAccessibilityManager service = IAccessibilityManager.Stub.asInterface(iBinder);
+
         try {
             final int stateFlags = service.addClient(mClient, mUserId);
             setStateLocked(stateFlags);
diff --git a/core/java/android/widget/ActionMenuPresenter.java b/core/java/android/widget/ActionMenuPresenter.java
index a5696ee..64c4103 100644
--- a/core/java/android/widget/ActionMenuPresenter.java
+++ b/core/java/android/widget/ActionMenuPresenter.java
@@ -37,7 +37,6 @@
 import android.view.ViewGroup;
 import android.view.ViewTreeObserver;
 import android.view.accessibility.AccessibilityNodeInfo;
-import android.widget.ListPopupWindow.ForwardingListener;
 import com.android.internal.view.ActionBarPolicy;
 import com.android.internal.view.menu.ActionMenuItemView;
 import com.android.internal.view.menu.BaseMenuPresenter;
@@ -45,6 +44,7 @@
 import com.android.internal.view.menu.MenuItemImpl;
 import com.android.internal.view.menu.MenuPopupHelper;
 import com.android.internal.view.menu.MenuView;
+import com.android.internal.view.menu.ShowableListMenu;
 import com.android.internal.view.menu.SubMenuBuilder;
 
 import java.util.ArrayList;
@@ -828,7 +828,7 @@
 
             setOnTouchListener(new ForwardingListener(this) {
                 @Override
-                public ListPopupWindow getPopup() {
+                public ShowableListMenu getPopup() {
                     if (mOverflowPopup == null) {
                         return null;
                     }
@@ -1003,7 +1003,7 @@
 
     private class ActionMenuPopupCallback extends ActionMenuItemView.PopupCallback {
         @Override
-        public ListPopupWindow getPopup() {
+        public ShowableListMenu getPopup() {
             return mActionButtonPopup != null ? mActionButtonPopup.getPopup() : null;
         }
     }
diff --git a/core/java/android/widget/ActivityChooserView.java b/core/java/android/widget/ActivityChooserView.java
index f34ad71..f5c46db 100644
--- a/core/java/android/widget/ActivityChooserView.java
+++ b/core/java/android/widget/ActivityChooserView.java
@@ -17,6 +17,7 @@
 package android.widget;
 
 import com.android.internal.R;
+import com.android.internal.view.menu.ShowableListMenu;
 
 import android.annotation.StringRes;
 import android.content.Context;
@@ -37,7 +38,6 @@
 import android.view.ViewTreeObserver.OnGlobalLayoutListener;
 import android.view.accessibility.AccessibilityNodeInfo;
 import android.widget.ActivityChooserModel.ActivityChooserModelClient;
-import android.widget.ListPopupWindow.ForwardingListener;
 
 /**
  * This class is a view for choosing an activity for handling a given {@link Intent}.
@@ -263,7 +263,7 @@
         });
         expandButton.setOnTouchListener(new ForwardingListener(expandButton) {
             @Override
-            public ListPopupWindow getPopup() {
+            public ShowableListMenu getPopup() {
                 return getListPopupWindow();
             }
 
diff --git a/core/java/android/widget/DropDownListView.java b/core/java/android/widget/DropDownListView.java
new file mode 100644
index 0000000..5536513
--- /dev/null
+++ b/core/java/android/widget/DropDownListView.java
@@ -0,0 +1,344 @@
+/*
+ * 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 android.widget;
+
+
+import com.android.internal.widget.AutoScrollHelper.AbsListViewAutoScroller;
+
+import android.animation.Animator;
+import android.animation.AnimatorListenerAdapter;
+import android.animation.ObjectAnimator;
+import android.content.Context;
+import android.graphics.drawable.Drawable;
+import android.util.IntProperty;
+import android.util.Log;
+import android.view.MotionEvent;
+import android.view.View;
+import android.view.accessibility.AccessibilityManager;
+import android.view.animation.AccelerateDecelerateInterpolator;
+import android.widget.TextView;
+import android.widget.ListView;
+
+
+/**
+ * Wrapper class for a ListView. This wrapper can hijack the focus to
+ * make sure the list uses the appropriate drawables and states when
+ * displayed on screen within a drop down. The focus is never actually
+ * passed to the drop down in this mode; the list only looks focused.
+ *
+ * @hide
+ */
+public class DropDownListView extends ListView {
+    /** Duration in milliseconds of the drag-to-open click animation. */
+    private static final long CLICK_ANIM_DURATION = 150;
+
+    /** Target alpha value for drag-to-open click animation. */
+    private static final int CLICK_ANIM_ALPHA = 0x80;
+
+    /** Wrapper around Drawable's <code>alpha</code> property. */
+    private static final IntProperty<Drawable> DRAWABLE_ALPHA =
+            new IntProperty<Drawable>("alpha") {
+                @Override
+                public void setValue(Drawable object, int value) {
+                    object.setAlpha(value);
+                }
+
+                @Override
+                public Integer get(Drawable object) {
+                    return object.getAlpha();
+                }
+            };
+
+    /*
+     * WARNING: This is a workaround for a touch mode issue.
+     *
+     * Touch mode is propagated lazily to windows. This causes problems in
+     * the following scenario:
+     * - Type something in the AutoCompleteTextView and get some results
+     * - Move down with the d-pad to select an item in the list
+     * - Move up with the d-pad until the selection disappears
+     * - Type more text in the AutoCompleteTextView *using the soft keyboard*
+     *   and get new results; you are now in touch mode
+     * - The selection comes back on the first item in the list, even though
+     *   the list is supposed to be in touch mode
+     *
+     * Using the soft keyboard triggers the touch mode change but that change
+     * is propagated to our window only after the first list layout, therefore
+     * after the list attempts to resurrect the selection.
+     *
+     * The trick to work around this issue is to pretend the list is in touch
+     * mode when we know that the selection should not appear, that is when
+     * we know the user moved the selection away from the list.
+     *
+     * This boolean is set to true whenever we explicitly hide the list's
+     * selection and reset to false whenever we know the user moved the
+     * selection back to the list.
+     *
+     * When this boolean is true, isInTouchMode() returns true, otherwise it
+     * returns super.isInTouchMode().
+     */
+    private boolean mListSelectionHidden;
+
+    /**
+     * True if this wrapper should fake focus.
+     */
+    private boolean mHijackFocus;
+
+    /** Whether to force drawing of the pressed state selector. */
+    private boolean mDrawsInPressedState;
+
+    /** Current drag-to-open click animation, if any. */
+    private Animator mClickAnimation;
+
+    /** Helper for drag-to-open auto scrolling. */
+    private AbsListViewAutoScroller mScrollHelper;
+
+    /**
+     * Creates a new list view wrapper.
+     *
+     * @param context this view's context
+     */
+    public DropDownListView(Context context, boolean hijackFocus) {
+        this(context, hijackFocus, com.android.internal.R.attr.dropDownListViewStyle);
+    }
+
+    /**
+     * Creates a new list view wrapper.
+     *
+     * @param context this view's context
+     */
+    public DropDownListView(Context context, boolean hijackFocus, int defStyleAttr) {
+        super(context, null, defStyleAttr);
+        mHijackFocus = hijackFocus;
+        // TODO: Add an API to control this
+        setCacheColorHint(0); // Transparent, since the background drawable could be anything.
+    }
+
+    /**
+     * Handles forwarded events.
+     *
+     * @param activePointerId id of the pointer that activated forwarding
+     * @return whether the event was handled
+     */
+    public boolean onForwardedEvent(MotionEvent event, int activePointerId) {
+        boolean handledEvent = true;
+        boolean clearPressedItem = false;
+
+        final int actionMasked = event.getActionMasked();
+        switch (actionMasked) {
+            case MotionEvent.ACTION_CANCEL:
+                handledEvent = false;
+                break;
+            case MotionEvent.ACTION_UP:
+                handledEvent = false;
+                // $FALL-THROUGH$
+            case MotionEvent.ACTION_MOVE:
+                final int activeIndex = event.findPointerIndex(activePointerId);
+                if (activeIndex < 0) {
+                    handledEvent = false;
+                    break;
+                }
+
+                final int x = (int) event.getX(activeIndex);
+                final int y = (int) event.getY(activeIndex);
+                final int position = pointToPosition(x, y);
+                if (position == INVALID_POSITION) {
+                    clearPressedItem = true;
+                    break;
+                }
+
+                final View child = getChildAt(position - getFirstVisiblePosition());
+                setPressedItem(child, position, x, y);
+                handledEvent = true;
+
+                if (actionMasked == MotionEvent.ACTION_UP) {
+                    clickPressedItem(child, position);
+                }
+                break;
+        }
+
+        // Failure to handle the event cancels forwarding.
+        if (!handledEvent || clearPressedItem) {
+            clearPressedItem();
+        }
+
+        // Manage automatic scrolling.
+        if (handledEvent) {
+            if (mScrollHelper == null) {
+                mScrollHelper = new AbsListViewAutoScroller(this);
+            }
+            mScrollHelper.setEnabled(true);
+            mScrollHelper.onTouch(this, event);
+        } else if (mScrollHelper != null) {
+            mScrollHelper.setEnabled(false);
+        }
+
+        return handledEvent;
+    }
+
+    /**
+     * Sets whether the list selection is hidden, as part of a workaround for a touch mode issue
+     * (see the declaration for mListSelectionHidden).
+     * @param listSelectionHidden
+     */
+    public void setListSelectionHidden(boolean listSelectionHidden) {
+        this.mListSelectionHidden = listSelectionHidden;
+    }
+
+    /**
+     * Starts an alpha animation on the selector. When the animation ends,
+     * the list performs a click on the item.
+     */
+    private void clickPressedItem(final View child, final int position) {
+        final long id = getItemIdAtPosition(position);
+        final Animator anim = ObjectAnimator.ofInt(
+                mSelector, DRAWABLE_ALPHA, 0xFF, CLICK_ANIM_ALPHA, 0xFF);
+        anim.setDuration(CLICK_ANIM_DURATION);
+        anim.setInterpolator(new AccelerateDecelerateInterpolator());
+        anim.addListener(new AnimatorListenerAdapter() {
+                @Override
+            public void onAnimationEnd(Animator animation) {
+                performItemClick(child, position, id);
+            }
+        });
+        anim.start();
+
+        if (mClickAnimation != null) {
+            mClickAnimation.cancel();
+        }
+        mClickAnimation = anim;
+    }
+
+    private void clearPressedItem() {
+        mDrawsInPressedState = false;
+        setPressed(false);
+        updateSelectorState();
+
+        final View motionView = getChildAt(mMotionPosition - mFirstPosition);
+        if (motionView != null) {
+            motionView.setPressed(false);
+        }
+
+        if (mClickAnimation != null) {
+            mClickAnimation.cancel();
+            mClickAnimation = null;
+        }
+    }
+
+    private void setPressedItem(View child, int position, float x, float y) {
+        mDrawsInPressedState = true;
+
+        // Ordering is essential. First, update the container's pressed state.
+        drawableHotspotChanged(x, y);
+        if (!isPressed()) {
+            setPressed(true);
+        }
+
+        // Next, run layout if we need to stabilize child positions.
+        if (mDataChanged) {
+            layoutChildren();
+        }
+
+        // Manage the pressed view based on motion position. This allows us to
+        // play nicely with actual touch and scroll events.
+        final View motionView = getChildAt(mMotionPosition - mFirstPosition);
+        if (motionView != null && motionView != child && motionView.isPressed()) {
+            motionView.setPressed(false);
+        }
+        mMotionPosition = position;
+
+        // Offset for child coordinates.
+        final float childX = x - child.getLeft();
+        final float childY = y - child.getTop();
+        child.drawableHotspotChanged(childX, childY);
+        if (!child.isPressed()) {
+            child.setPressed(true);
+        }
+
+        // Ensure that keyboard focus starts from the last touched position.
+        setSelectedPositionInt(position);
+        positionSelectorLikeTouch(position, child, x, y);
+
+        // Refresh the drawable state to reflect the new pressed state,
+        // which will also update the selector state.
+        refreshDrawableState();
+
+        if (mClickAnimation != null) {
+            mClickAnimation.cancel();
+            mClickAnimation = null;
+        }
+    }
+
+    @Override
+    boolean touchModeDrawsInPressedState() {
+        return mDrawsInPressedState || super.touchModeDrawsInPressedState();
+    }
+
+    /**
+     * Avoids jarring scrolling effect by ensuring that list elements
+     * made of a text view fit on a single line.
+     *
+     * @param position the item index in the list to get a view for
+     * @return the view for the specified item
+     */
+    @Override
+    View obtainView(int position, boolean[] isScrap) {
+        View view = super.obtainView(position, isScrap);
+
+        if (view instanceof TextView) {
+            ((TextView) view).setHorizontallyScrolling(true);
+        }
+
+        return view;
+    }
+
+    @Override
+    public boolean isInTouchMode() {
+        // WARNING: Please read the comment where mListSelectionHidden is declared
+        return (mHijackFocus && mListSelectionHidden) || super.isInTouchMode();
+    }
+
+    /**
+     * Returns the focus state in the drop down.
+     *
+     * @return true always if hijacking focus
+     */
+    @Override
+    public boolean hasWindowFocus() {
+        return mHijackFocus || super.hasWindowFocus();
+    }
+
+    /**
+     * Returns the focus state in the drop down.
+     *
+     * @return true always if hijacking focus
+     */
+    @Override
+    public boolean isFocused() {
+        return mHijackFocus || super.isFocused();
+    }
+
+    /**
+     * Returns the focus state in the drop down.
+     *
+     * @return true always if hijacking focus
+     */
+    @Override
+    public boolean hasFocus() {
+        return mHijackFocus || super.hasFocus();
+    }
+}
\ No newline at end of file
diff --git a/core/java/android/widget/ForwardingListener.java b/core/java/android/widget/ForwardingListener.java
new file mode 100644
index 0000000..7ddeff9
--- /dev/null
+++ b/core/java/android/widget/ForwardingListener.java
@@ -0,0 +1,299 @@
+/*
+ * 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 android.widget;
+
+import android.os.SystemClock;
+import android.view.MotionEvent;
+import android.view.View;
+import android.view.ViewConfiguration;
+import android.view.ViewParent;
+
+import com.android.internal.view.menu.ShowableListMenu;
+
+/**
+ * Abstract class that forwards touch events to a {@link ShowableListMenu}.
+ *
+ * @hide
+ */
+public abstract class ForwardingListener
+        implements View.OnTouchListener, View.OnAttachStateChangeListener {
+
+    /** Scaled touch slop, used for detecting movement outside bounds. */
+    private final float mScaledTouchSlop;
+
+    /** Timeout before disallowing intercept on the source's parent. */
+    private final int mTapTimeout;
+
+    /** Timeout before accepting a long-press to start forwarding. */
+    private final int mLongPressTimeout;
+
+    /** Source view from which events are forwarded. */
+    private final View mSrc;
+
+    /** Runnable used to prevent conflicts with scrolling parents. */
+    private Runnable mDisallowIntercept;
+
+    /** Runnable used to trigger forwarding on long-press. */
+    private Runnable mTriggerLongPress;
+
+    /** Whether this listener is currently forwarding touch events. */
+    private boolean mForwarding;
+
+    /**
+     * Whether forwarding was initiated by a long-press. If so, we won't
+     * force the window to dismiss when the touch stream ends.
+     */
+    private boolean mWasLongPress;
+
+    /** The id of the first pointer down in the current event stream. */
+    private int mActivePointerId;
+
+    public ForwardingListener(View src) {
+        mSrc = src;
+        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);
+    }
+
+    /**
+     * Returns the popup to which this listener is forwarding events.
+     * <p>
+     * Override this to return the correct popup. If the popup is displayed
+     * asynchronously, you may also need to override
+     * {@link #onForwardingStopped} to prevent premature cancellation of
+     * forwarding.
+     *
+     * @return the popup to which this listener is forwarding events
+     */
+    public abstract ShowableListMenu getPopup();
+
+    @Override
+    public boolean onTouch(View v, MotionEvent event) {
+        final boolean wasForwarding = mForwarding;
+        final boolean forwarding;
+        if (wasForwarding) {
+            forwarding = onTouchForwarded(event) || !onForwardingStopped();
+        } else {
+            forwarding = onTouchObserved(event) && onForwardingStarted();
+
+            if (forwarding) {
+                // Make sure we cancel any ongoing source event stream.
+                final long now = SystemClock.uptimeMillis();
+                final MotionEvent e = MotionEvent.obtain(now, now, MotionEvent.ACTION_CANCEL,
+                        0.0f, 0.0f, 0);
+                mSrc.onTouchEvent(e);
+                e.recycle();
+            }
+        }
+
+        mForwarding = forwarding;
+        return forwarding || wasForwarding;
+    }
+
+    @Override
+    public void onViewAttachedToWindow(View v) {
+    }
+
+    @Override
+    public void onViewDetachedFromWindow(View v) {
+        mForwarding = false;
+        mActivePointerId = MotionEvent.INVALID_POINTER_ID;
+
+        if (mDisallowIntercept != null) {
+            mSrc.removeCallbacks(mDisallowIntercept);
+        }
+    }
+
+    /**
+     * Called when forwarding would like to start.
+     * <p>
+     * By default, this will show the popup returned by {@link #getPopup()}.
+     * It may be overridden to perform another action, like clicking the
+     * source view or preparing the popup before showing it.
+     *
+     * @return true to start forwarding, false otherwise
+     */
+    protected boolean onForwardingStarted() {
+        final ShowableListMenu popup = getPopup();
+        if (popup != null && !popup.isShowing()) {
+            popup.show();
+        }
+        return true;
+    }
+
+    /**
+     * Called when forwarding would like to stop.
+     * <p>
+     * By default, this will dismiss the popup returned by
+     * {@link #getPopup()}. It may be overridden to perform some other
+     * action.
+     *
+     * @return true to stop forwarding, false otherwise
+     */
+    protected boolean onForwardingStopped() {
+        final ShowableListMenu popup = getPopup();
+        if (popup != null && popup.isShowing()) {
+            popup.dismiss();
+        }
+        return true;
+    }
+
+    /**
+     * Observes motion events and determines when to start forwarding.
+     *
+     * @param srcEvent motion event in source view coordinates
+     * @return true to start forwarding motion events, false otherwise
+     */
+    private boolean onTouchObserved(MotionEvent srcEvent) {
+        final View src = mSrc;
+        if (!src.isEnabled()) {
+            return false;
+        }
+
+        final int actionMasked = srcEvent.getActionMasked();
+        switch (actionMasked) {
+            case MotionEvent.ACTION_DOWN:
+                mActivePointerId = srcEvent.getPointerId(0);
+                mWasLongPress = false;
+
+                if (mDisallowIntercept == null) {
+                    mDisallowIntercept = new DisallowIntercept();
+                }
+                src.postDelayed(mDisallowIntercept, mTapTimeout);
+
+                if (mTriggerLongPress == null) {
+                    mTriggerLongPress = new TriggerLongPress();
+                }
+                src.postDelayed(mTriggerLongPress, mLongPressTimeout);
+                break;
+            case MotionEvent.ACTION_MOVE:
+                final int activePointerIndex = srcEvent.findPointerIndex(mActivePointerId);
+                if (activePointerIndex >= 0) {
+                    final float x = srcEvent.getX(activePointerIndex);
+                    final float y = srcEvent.getY(activePointerIndex);
+
+                    // Has the pointer moved outside of the view?
+                    if (!src.pointInView(x, y, mScaledTouchSlop)) {
+                        clearCallbacks();
+
+                        // Don't let the parent intercept our events.
+                        src.getParent().requestDisallowInterceptTouchEvent(true);
+                        return true;
+                    }
+                }
+                break;
+            case MotionEvent.ACTION_CANCEL:
+            case MotionEvent.ACTION_UP:
+                clearCallbacks();
+                break;
+        }
+
+        return false;
+    }
+
+    private void clearCallbacks() {
+        if (mTriggerLongPress != null) {
+            mSrc.removeCallbacks(mTriggerLongPress);
+        }
+
+        if (mDisallowIntercept != null) {
+            mSrc.removeCallbacks(mDisallowIntercept);
+        }
+    }
+
+    private void onLongPress() {
+        clearCallbacks();
+
+        final View src = mSrc;
+        if (!src.isEnabled() || src.isLongClickable()) {
+            // Ignore long-press if the view is disabled or has its own
+            // handler.
+            return;
+        }
+
+        if (!onForwardingStarted()) {
+            return;
+        }
+
+        // Don't let the parent intercept our events.
+        src.getParent().requestDisallowInterceptTouchEvent(true);
+
+        // Make sure we cancel any ongoing source event stream.
+        final long now = SystemClock.uptimeMillis();
+        final MotionEvent e = MotionEvent.obtain(now, now, MotionEvent.ACTION_CANCEL, 0, 0, 0);
+        src.onTouchEvent(e);
+        e.recycle();
+
+        mForwarding = true;
+        mWasLongPress = true;
+    }
+
+    /**
+     * Handles forwarded motion events and determines when to stop
+     * forwarding.
+     *
+     * @param srcEvent motion event in source view coordinates
+     * @return true to continue forwarding motion events, false to cancel
+     */
+    private boolean onTouchForwarded(MotionEvent srcEvent) {
+        final View src = mSrc;
+        final ShowableListMenu popup = getPopup();
+        if (popup == null || !popup.isShowing()) {
+            return false;
+        }
+
+        final DropDownListView dst = (DropDownListView) popup.getListView();
+        if (dst == null || !dst.isShown()) {
+            return false;
+        }
+
+        // Convert event to destination-local coordinates.
+        final MotionEvent dstEvent = MotionEvent.obtainNoHistory(srcEvent);
+        src.toGlobalMotionEvent(dstEvent);
+        dst.toLocalMotionEvent(dstEvent);
+
+        // Forward converted event to destination view, then recycle it.
+        final boolean handled = dst.onForwardedEvent(dstEvent, mActivePointerId);
+        dstEvent.recycle();
+
+        // Always cancel forwarding when the touch stream ends.
+        final int action = srcEvent.getActionMasked();
+        final boolean keepForwarding = action != MotionEvent.ACTION_UP
+                && action != MotionEvent.ACTION_CANCEL;
+
+        return handled && keepForwarding;
+    }
+
+    private class DisallowIntercept implements Runnable {
+        @Override
+        public void run() {
+            final ViewParent parent = mSrc.getParent();
+            parent.requestDisallowInterceptTouchEvent(true);
+        }
+    }
+
+    private class TriggerLongPress implements Runnable {
+        @Override
+        public void run() {
+            onLongPress();
+        }
+    }
+}
\ No newline at end of file
diff --git a/core/java/android/widget/ListPopupWindow.java b/core/java/android/widget/ListPopupWindow.java
index a02efcf..3d07d87 100644
--- a/core/java/android/widget/ListPopupWindow.java
+++ b/core/java/android/widget/ListPopupWindow.java
@@ -25,7 +25,6 @@
 import android.graphics.Rect;
 import android.graphics.drawable.Drawable;
 import android.os.Handler;
-import android.os.SystemClock;
 import android.text.TextUtils;
 import android.util.AttributeSet;
 import android.util.IntProperty;
@@ -36,13 +35,13 @@
 import android.view.View;
 import android.view.View.MeasureSpec;
 import android.view.View.OnTouchListener;
-import android.view.ViewConfiguration;
 import android.view.ViewGroup;
 import android.view.ViewParent;
 import android.view.WindowManager;
 import android.view.animation.AccelerateDecelerateInterpolator;
 
 import com.android.internal.R;
+import com.android.internal.view.menu.ShowableListMenu;
 import com.android.internal.widget.AutoScrollHelper.AbsListViewAutoScroller;
 
 import java.util.Locale;
@@ -58,7 +57,7 @@
  * @see android.widget.AutoCompleteTextView
  * @see android.widget.Spinner
  */
-public class ListPopupWindow {
+public class ListPopupWindow implements ShowableListMenu {
     private static final String TAG = "ListPopupWindow";
     private static final boolean DEBUG = false;
 
@@ -580,6 +579,7 @@
      * Show the popup list. If the list is already showing, this method
      * will recalculate the popup's size and position.
      */
+    @Override
     public void show() {
         int height = buildDropDown();
 
@@ -671,6 +671,7 @@
     /**
      * Dismiss the popup window.
      */
+    @Override
     public void dismiss() {
         mPopup.dismiss();
         removePromptView();
@@ -732,7 +733,7 @@
     public void setSelection(int position) {
         DropDownListView list = mDropDownList;
         if (isShowing() && list != null) {
-            list.mListSelectionHidden = false;
+            list.setListSelectionHidden(false);
             list.setSelection(position);
             if (list.getChoiceMode() != ListView.CHOICE_MODE_NONE) {
                 list.setItemChecked(position, true);
@@ -748,7 +749,7 @@
         final DropDownListView list = mDropDownList;
         if (list != null) {
             // WARNING: Please read the comment where mListSelectionHidden is declared
-            list.mListSelectionHidden = true;
+            list.setListSelectionHidden(true);
             list.hideSelector();
             list.requestLayout();
         }
@@ -757,6 +758,7 @@
     /**
      * @return {@code true} if the popup is currently showing, {@code false} otherwise.
      */
+    @Override
     public boolean isShowing() {
         return mPopup.isShowing();
     }
@@ -842,6 +844,7 @@
      * @return The {@link ListView} displayed within the popup window.
      * Only valid when {@link #isShowing()} == {@code true}.
      */
+    @Override
     public ListView getListView() {
         return mDropDownList;
     }
@@ -911,7 +914,7 @@
                 } else {
                     // WARNING: Please read the comment where mListSelectionHidden
                     //          is declared
-                    mDropDownList.mListSelectionHidden = false;
+                    mDropDownList.setListSelectionHidden(false);
                 }
 
                 consumed = mDropDownList.onKeyDown(keyCode, event);
@@ -1037,7 +1040,7 @@
     public OnTouchListener createDragToOpenListener(View src) {
         return new ForwardingListener(src) {
             @Override
-            public ListPopupWindow getPopup() {
+            public ShowableListMenu getPopup() {
                 return ListPopupWindow.this;
             }
         };
@@ -1088,7 +1091,7 @@
                         DropDownListView dropDownList = mDropDownList;
 
                         if (dropDownList != null) {
-                            dropDownList.mListSelectionHidden = false;
+                            dropDownList.setListSelectionHidden(false);
                         }
                     }
                 }
@@ -1219,568 +1222,6 @@
         return listContent + otherHeights;
     }
 
-    /**
-     * Abstract class that forwards touch events to a {@link ListPopupWindow}.
-     *
-     * @hide
-     */
-    public static abstract class ForwardingListener
-            implements View.OnTouchListener, View.OnAttachStateChangeListener {
-        /** Scaled touch slop, used for detecting movement outside bounds. */
-        private final float mScaledTouchSlop;
-
-        /** Timeout before disallowing intercept on the source's parent. */
-        private final int mTapTimeout;
-
-        /** Timeout before accepting a long-press to start forwarding. */
-        private final int mLongPressTimeout;
-
-        /** Source view from which events are forwarded. */
-        private final View mSrc;
-
-        /** Runnable used to prevent conflicts with scrolling parents. */
-        private Runnable mDisallowIntercept;
-
-        /** Runnable used to trigger forwarding on long-press. */
-        private Runnable mTriggerLongPress;
-
-        /** Whether this listener is currently forwarding touch events. */
-        private boolean mForwarding;
-
-        /**
-         * Whether forwarding was initiated by a long-press. If so, we won't
-         * force the window to dismiss when the touch stream ends.
-         */
-        private boolean mWasLongPress;
-
-        /** The id of the first pointer down in the current event stream. */
-        private int mActivePointerId;
-
-        public ForwardingListener(View src) {
-            mSrc = src;
-            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);
-        }
-
-        /**
-         * Returns the popup to which this listener is forwarding events.
-         * <p>
-         * Override this to return the correct popup. If the popup is displayed
-         * asynchronously, you may also need to override
-         * {@link #onForwardingStopped} to prevent premature cancelation of
-         * forwarding.
-         *
-         * @return the popup to which this listener is forwarding events
-         */
-        public abstract ListPopupWindow getPopup();
-
-        @Override
-        public boolean onTouch(View v, MotionEvent event) {
-            final boolean wasForwarding = mForwarding;
-            final boolean forwarding;
-            if (wasForwarding) {
-                forwarding = onTouchForwarded(event) || !onForwardingStopped();
-            } else {
-                forwarding = onTouchObserved(event) && onForwardingStarted();
-
-                if (forwarding) {
-                    // Make sure we cancel any ongoing source event stream.
-                    final long now = SystemClock.uptimeMillis();
-                    final MotionEvent e = MotionEvent.obtain(now, now, MotionEvent.ACTION_CANCEL,
-                            0.0f, 0.0f, 0);
-                    mSrc.onTouchEvent(e);
-                    e.recycle();
-                }
-            }
-
-            mForwarding = forwarding;
-            return forwarding || wasForwarding;
-        }
-
-        @Override
-        public void onViewAttachedToWindow(View v) {
-        }
-
-        @Override
-        public void onViewDetachedFromWindow(View v) {
-            mForwarding = false;
-            mActivePointerId = MotionEvent.INVALID_POINTER_ID;
-
-            if (mDisallowIntercept != null) {
-                mSrc.removeCallbacks(mDisallowIntercept);
-            }
-        }
-
-        /**
-         * Called when forwarding would like to start.
-         * <p>
-         * By default, this will show the popup returned by {@link #getPopup()}.
-         * It may be overridden to perform another action, like clicking the
-         * source view or preparing the popup before showing it.
-         *
-         * @return true to start forwarding, false otherwise
-         */
-        protected boolean onForwardingStarted() {
-            final ListPopupWindow popup = getPopup();
-            if (popup != null && !popup.isShowing()) {
-                popup.show();
-            }
-            return true;
-        }
-
-        /**
-         * Called when forwarding would like to stop.
-         * <p>
-         * By default, this will dismiss the popup returned by
-         * {@link #getPopup()}. It may be overridden to perform some other
-         * action.
-         *
-         * @return true to stop forwarding, false otherwise
-         */
-        protected boolean onForwardingStopped() {
-            final ListPopupWindow popup = getPopup();
-            if (popup != null && popup.isShowing()) {
-                popup.dismiss();
-            }
-            return true;
-        }
-
-        /**
-         * Observes motion events and determines when to start forwarding.
-         *
-         * @param srcEvent motion event in source view coordinates
-         * @return true to start forwarding motion events, false otherwise
-         */
-        private boolean onTouchObserved(MotionEvent srcEvent) {
-            final View src = mSrc;
-            if (!src.isEnabled()) {
-                return false;
-            }
-
-            final int actionMasked = srcEvent.getActionMasked();
-            switch (actionMasked) {
-                case MotionEvent.ACTION_DOWN:
-                    mActivePointerId = srcEvent.getPointerId(0);
-                    mWasLongPress = false;
-
-                    if (mDisallowIntercept == null) {
-                        mDisallowIntercept = new DisallowIntercept();
-                    }
-                    src.postDelayed(mDisallowIntercept, mTapTimeout);
-
-                    if (mTriggerLongPress == null) {
-                        mTriggerLongPress = new TriggerLongPress();
-                    }
-                    src.postDelayed(mTriggerLongPress, mLongPressTimeout);
-                    break;
-                case MotionEvent.ACTION_MOVE:
-                    final int activePointerIndex = srcEvent.findPointerIndex(mActivePointerId);
-                    if (activePointerIndex >= 0) {
-                        final float x = srcEvent.getX(activePointerIndex);
-                        final float y = srcEvent.getY(activePointerIndex);
-
-                        // Has the pointer has moved outside of the view?
-                        if (!src.pointInView(x, y, mScaledTouchSlop)) {
-                            clearCallbacks();
-
-                            // Don't let the parent intercept our events.
-                            src.getParent().requestDisallowInterceptTouchEvent(true);
-                            return true;
-                        }
-                    }
-                    break;
-                case MotionEvent.ACTION_CANCEL:
-                case MotionEvent.ACTION_UP:
-                    clearCallbacks();
-                    break;
-            }
-
-            return false;
-        }
-
-        private void clearCallbacks() {
-            if (mTriggerLongPress != null) {
-                mSrc.removeCallbacks(mTriggerLongPress);
-            }
-
-            if (mDisallowIntercept != null) {
-                mSrc.removeCallbacks(mDisallowIntercept);
-            }
-        }
-
-        private void onLongPress() {
-            clearCallbacks();
-
-            final View src = mSrc;
-            if (!src.isEnabled() || src.isLongClickable()) {
-                // Ignore long-press if the view is disabled or has its own
-                // handler.
-                return;
-            }
-
-            if (!onForwardingStarted()) {
-                return;
-            }
-
-            // Don't let the parent intercept our events.
-            src.getParent().requestDisallowInterceptTouchEvent(true);
-
-            // Make sure we cancel any ongoing source event stream.
-            final long now = SystemClock.uptimeMillis();
-            final MotionEvent e = MotionEvent.obtain(now, now, MotionEvent.ACTION_CANCEL, 0, 0, 0);
-            src.onTouchEvent(e);
-            e.recycle();
-
-            mForwarding = true;
-            mWasLongPress = true;
-        }
-
-        /**
-         * Handled forwarded motion events and determines when to stop
-         * forwarding.
-         *
-         * @param srcEvent motion event in source view coordinates
-         * @return true to continue forwarding motion events, false to cancel
-         */
-        private boolean onTouchForwarded(MotionEvent srcEvent) {
-            final View src = mSrc;
-            final ListPopupWindow popup = getPopup();
-            if (popup == null || !popup.isShowing()) {
-                return false;
-            }
-
-            final DropDownListView dst = popup.mDropDownList;
-            if (dst == null || !dst.isShown()) {
-                return false;
-            }
-
-            // Convert event to destination-local coordinates.
-            final MotionEvent dstEvent = MotionEvent.obtainNoHistory(srcEvent);
-            src.toGlobalMotionEvent(dstEvent);
-            dst.toLocalMotionEvent(dstEvent);
-
-            // Forward converted event to destination view, then recycle it.
-            final boolean handled = dst.onForwardedEvent(dstEvent, mActivePointerId);
-            dstEvent.recycle();
-
-            // Always cancel forwarding when the touch stream ends.
-            final int action = srcEvent.getActionMasked();
-            final boolean keepForwarding = action != MotionEvent.ACTION_UP
-                    && action != MotionEvent.ACTION_CANCEL;
-
-            return handled && keepForwarding;
-        }
-
-        private class DisallowIntercept implements Runnable {
-            @Override
-            public void run() {
-                final ViewParent parent = mSrc.getParent();
-                parent.requestDisallowInterceptTouchEvent(true);
-            }
-        }
-
-        private class TriggerLongPress implements Runnable {
-            @Override
-            public void run() {
-                onLongPress();
-            }
-        }
-    }
-
-    /**
-     * <p>Wrapper class for a ListView. This wrapper can hijack the focus to
-     * make sure the list uses the appropriate drawables and states when
-     * displayed on screen within a drop down. The focus is never actually
-     * passed to the drop down in this mode; the list only looks focused.</p>
-     */
-    static class DropDownListView extends ListView {
-        /** Duration in milliseconds of the drag-to-open click animation. */
-        private static final long CLICK_ANIM_DURATION = 150;
-
-        /** Target alpha value for drag-to-open click animation. */
-        private static final int CLICK_ANIM_ALPHA = 0x80;
-
-        /** Wrapper around Drawable's <code>alpha</code> property. */
-        private static final IntProperty<Drawable> DRAWABLE_ALPHA =
-                new IntProperty<Drawable>("alpha") {
-                    @Override
-                    public void setValue(Drawable object, int value) {
-                        object.setAlpha(value);
-                    }
-
-                    @Override
-                    public Integer get(Drawable object) {
-                        return object.getAlpha();
-                    }
-                };
-
-        /*
-         * WARNING: This is a workaround for a touch mode issue.
-         *
-         * Touch mode is propagated lazily to windows. This causes problems in
-         * the following scenario:
-         * - Type something in the AutoCompleteTextView and get some results
-         * - Move down with the d-pad to select an item in the list
-         * - Move up with the d-pad until the selection disappears
-         * - Type more text in the AutoCompleteTextView *using the soft keyboard*
-         *   and get new results; you are now in touch mode
-         * - The selection comes back on the first item in the list, even though
-         *   the list is supposed to be in touch mode
-         *
-         * Using the soft keyboard triggers the touch mode change but that change
-         * is propagated to our window only after the first list layout, therefore
-         * after the list attempts to resurrect the selection.
-         *
-         * The trick to work around this issue is to pretend the list is in touch
-         * mode when we know that the selection should not appear, that is when
-         * we know the user moved the selection away from the list.
-         *
-         * This boolean is set to true whenever we explicitly hide the list's
-         * selection and reset to false whenever we know the user moved the
-         * selection back to the list.
-         *
-         * When this boolean is true, isInTouchMode() returns true, otherwise it
-         * returns super.isInTouchMode().
-         */
-        private boolean mListSelectionHidden;
-        
-        /**
-         * True if this wrapper should fake focus.
-         */
-        private boolean mHijackFocus;
-
-        /** Whether to force drawing of the pressed state selector. */
-        private boolean mDrawsInPressedState;
-
-        /** Current drag-to-open click animation, if any. */
-        private Animator mClickAnimation;
-
-        /** Helper for drag-to-open auto scrolling. */
-        private AbsListViewAutoScroller mScrollHelper;
-
-        /**
-         * <p>Creates a new list view wrapper.</p>
-         *
-         * @param context this view's context
-         */
-        public DropDownListView(Context context, boolean hijackFocus) {
-            super(context, null, com.android.internal.R.attr.dropDownListViewStyle);
-            mHijackFocus = hijackFocus;
-            // TODO: Add an API to control this
-            setCacheColorHint(0); // Transparent, since the background drawable could be anything.
-        }
-
-        /**
-         * Handles forwarded events.
-         *
-         * @param activePointerId id of the pointer that activated forwarding
-         * @return whether the event was handled
-         */
-        public boolean onForwardedEvent(MotionEvent event, int activePointerId) {
-            boolean handledEvent = true;
-            boolean clearPressedItem = false;
-
-            final int actionMasked = event.getActionMasked();
-            switch (actionMasked) {
-                case MotionEvent.ACTION_CANCEL:
-                    handledEvent = false;
-                    break;
-                case MotionEvent.ACTION_UP:
-                    handledEvent = false;
-                    // $FALL-THROUGH$
-                case MotionEvent.ACTION_MOVE:
-                    final int activeIndex = event.findPointerIndex(activePointerId);
-                    if (activeIndex < 0) {
-                        handledEvent = false;
-                        break;
-                    }
-
-                    final int x = (int) event.getX(activeIndex);
-                    final int y = (int) event.getY(activeIndex);
-                    final int position = pointToPosition(x, y);
-                    if (position == INVALID_POSITION) {
-                        clearPressedItem = true;
-                        break;
-                    }
-
-                    final View child = getChildAt(position - getFirstVisiblePosition());
-                    setPressedItem(child, position, x, y);
-                    handledEvent = true;
-
-                    if (actionMasked == MotionEvent.ACTION_UP) {
-                        clickPressedItem(child, position);
-                    }
-                    break;
-            }
-
-            // Failure to handle the event cancels forwarding.
-            if (!handledEvent || clearPressedItem) {
-                clearPressedItem();
-            }
-
-            // Manage automatic scrolling.
-            if (handledEvent) {
-                if (mScrollHelper == null) {
-                    mScrollHelper = new AbsListViewAutoScroller(this);
-                }
-                mScrollHelper.setEnabled(true);
-                mScrollHelper.onTouch(this, event);
-            } else if (mScrollHelper != null) {
-                mScrollHelper.setEnabled(false);
-            }
-
-            return handledEvent;
-        }
-
-        /**
-         * Starts an alpha animation on the selector. When the animation ends,
-         * the list performs a click on the item.
-         */
-        private void clickPressedItem(final View child, final int position) {
-            final long id = getItemIdAtPosition(position);
-            final Animator anim = ObjectAnimator.ofInt(
-                    mSelector, DRAWABLE_ALPHA, 0xFF, CLICK_ANIM_ALPHA, 0xFF);
-            anim.setDuration(CLICK_ANIM_DURATION);
-            anim.setInterpolator(new AccelerateDecelerateInterpolator());
-            anim.addListener(new AnimatorListenerAdapter() {
-                    @Override
-                public void onAnimationEnd(Animator animation) {
-                    performItemClick(child, position, id);
-                }
-            });
-            anim.start();
-
-            if (mClickAnimation != null) {
-                mClickAnimation.cancel();
-            }
-            mClickAnimation = anim;
-        }
-
-        private void clearPressedItem() {
-            mDrawsInPressedState = false;
-            setPressed(false);
-            updateSelectorState();
-
-            final View motionView = getChildAt(mMotionPosition - mFirstPosition);
-            if (motionView != null) {
-                motionView.setPressed(false);
-            }
-
-            if (mClickAnimation != null) {
-                mClickAnimation.cancel();
-                mClickAnimation = null;
-            }
-        }
-
-        private void setPressedItem(View child, int position, float x, float y) {
-            mDrawsInPressedState = true;
-
-            // Ordering is essential. First, update the container's pressed state.
-            drawableHotspotChanged(x, y);
-            if (!isPressed()) {
-                setPressed(true);
-            }
-
-            // Next, run layout if we need to stabilize child positions.
-            if (mDataChanged) {
-                layoutChildren();
-            }
-
-            // Manage the pressed view based on motion position. This allows us to
-            // play nicely with actual touch and scroll events.
-            final View motionView = getChildAt(mMotionPosition - mFirstPosition);
-            if (motionView != null && motionView != child && motionView.isPressed()) {
-                motionView.setPressed(false);
-            }
-            mMotionPosition = position;
-
-            // Offset for child coordinates.
-            final float childX = x - child.getLeft();
-            final float childY = y - child.getTop();
-            child.drawableHotspotChanged(childX, childY);
-            if (!child.isPressed()) {
-                child.setPressed(true);
-            }
-
-            // Ensure that keyboard focus starts from the last touched position.
-            setSelectedPositionInt(position);
-            positionSelectorLikeTouch(position, child, x, y);
-
-            // Refresh the drawable state to reflect the new pressed state,
-            // which will also update the selector state.
-            refreshDrawableState();
-
-            if (mClickAnimation != null) {
-                mClickAnimation.cancel();
-                mClickAnimation = null;
-            }
-        }
-
-        @Override
-        boolean touchModeDrawsInPressedState() {
-            return mDrawsInPressedState || super.touchModeDrawsInPressedState();
-        }
-
-        /**
-         * <p>Avoids jarring scrolling effect by ensuring that list elements
-         * made of a text view fit on a single line.</p>
-         *
-         * @param position the item index in the list to get a view for
-         * @return the view for the specified item
-         */
-        @Override
-        View obtainView(int position, boolean[] isScrap) {
-            View view = super.obtainView(position, isScrap);
-
-            if (view instanceof TextView) {
-                ((TextView) view).setHorizontallyScrolling(true);
-            }
-
-            return view;
-        }
-
-        @Override
-        public boolean isInTouchMode() {
-            // WARNING: Please read the comment where mListSelectionHidden is declared
-            return (mHijackFocus && mListSelectionHidden) || super.isInTouchMode();
-        }
-
-        /**
-         * <p>Returns the focus state in the drop down.</p>
-         *
-         * @return true always if hijacking focus
-         */
-        @Override
-        public boolean hasWindowFocus() {
-            return mHijackFocus || super.hasWindowFocus();
-        }
-
-        /**
-         * <p>Returns the focus state in the drop down.</p>
-         *
-         * @return true always if hijacking focus
-         */
-        @Override
-        public boolean isFocused() {
-            return mHijackFocus || super.isFocused();
-        }
-
-        /**
-         * <p>Returns the focus state in the drop down.</p>
-         *
-         * @return true always if hijacking focus
-         */
-        @Override
-        public boolean hasFocus() {
-            return mHijackFocus || super.hasFocus();
-        }
-    }
-
     private class PopupDataSetObserver extends DataSetObserver {
         @Override
         public void onChanged() {
diff --git a/core/java/android/widget/MenuPopupWindow.java b/core/java/android/widget/MenuPopupWindow.java
index 8d42c73..9e47e85 100644
--- a/core/java/android/widget/MenuPopupWindow.java
+++ b/core/java/android/widget/MenuPopupWindow.java
@@ -36,11 +36,11 @@
     }
 
     @Override
-    ListPopupWindow.DropDownListView createDropDownListView(Context context, boolean hijackFocus) {
+    DropDownListView createDropDownListView(Context context, boolean hijackFocus) {
         return new MenuDropDownListView(context, hijackFocus);
     }
 
-    static class MenuDropDownListView extends ListPopupWindow.DropDownListView {
+    static class MenuDropDownListView extends DropDownListView {
         private boolean mHoveredOnDisabledItem = false;
         private AccessibilityManager mAccessibilityManager;
 
diff --git a/core/java/android/widget/PopupMenu.java b/core/java/android/widget/PopupMenu.java
index 1507dfb..3b2d60d 100644
--- a/core/java/android/widget/PopupMenu.java
+++ b/core/java/android/widget/PopupMenu.java
@@ -20,6 +20,7 @@
 import com.android.internal.view.menu.MenuBuilder;
 import com.android.internal.view.menu.MenuPopupHelper;
 import com.android.internal.view.menu.MenuPresenter;
+import com.android.internal.view.menu.ShowableListMenu;
 import com.android.internal.view.menu.SubMenuBuilder;
 
 import android.annotation.MenuRes;
@@ -30,7 +31,6 @@
 import android.view.MenuItem;
 import android.view.View;
 import android.view.View.OnTouchListener;
-import android.widget.ListPopupWindow.ForwardingListener;
 
 /**
  * A PopupMenu displays a {@link Menu} in a modal popup window anchored to a {@link View}.
@@ -170,7 +170,7 @@
                 }
 
                 @Override
-                public ListPopupWindow getPopup() {
+                public ShowableListMenu getPopup() {
                     // This will be null until show() is called.
                     return mPopup.getPopup();
                 }
diff --git a/core/java/android/widget/Spinner.java b/core/java/android/widget/Spinner.java
index f3cf61c..c79e184 100644
--- a/core/java/android/widget/Spinner.java
+++ b/core/java/android/widget/Spinner.java
@@ -17,6 +17,7 @@
 package android.widget;
 
 import com.android.internal.R;
+import com.android.internal.view.menu.ShowableListMenu;
 
 import android.annotation.DrawableRes;
 import android.annotation.Nullable;
@@ -44,7 +45,6 @@
 import android.view.ViewTreeObserver;
 import android.view.ViewTreeObserver.OnGlobalLayoutListener;
 import android.view.accessibility.AccessibilityNodeInfo;
-import android.widget.ListPopupWindow.ForwardingListener;
 import android.widget.PopupWindow.OnDismissListener;
 
 /**
@@ -278,7 +278,7 @@
                 mPopup = popup;
                 mForwardingListener = new ForwardingListener(this) {
                     @Override
-                    public ListPopupWindow getPopup() {
+                    public ShowableListMenu getPopup() {
                         return popup;
                     }
 
diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java
index 7a64377..61402ab 100644
--- a/core/java/android/widget/TextView.java
+++ b/core/java/android/widget/TextView.java
@@ -21,6 +21,7 @@
 import android.annotation.DrawableRes;
 import android.annotation.NonNull;
 import android.annotation.Nullable;
+import android.annotation.Size;
 import android.annotation.StringRes;
 import android.annotation.StyleRes;
 import android.annotation.XmlRes;
@@ -103,6 +104,7 @@
 import android.text.style.UpdateAppearance;
 import android.text.util.Linkify;
 import android.util.AttributeSet;
+import android.util.LocaleList;
 import android.util.Log;
 import android.util.TypedValue;
 import android.view.AccessibilityIterators.TextSegmentIterator;
@@ -553,7 +555,7 @@
     private final TextPaint mTextPaint;
     private boolean mUserSetTextScaleX;
     private Layout mLayout;
-    private boolean mLocaleChanged = false;
+    private boolean mLocalesChanged = false;
 
     @ViewDebug.ExportedProperty(category = "text")
     private int mGravity = Gravity.TOP | Gravity.START;
@@ -2817,32 +2819,58 @@
     }
 
     /**
-     * Get the default {@link Locale} of the text in this TextView.
-     * @return the default {@link Locale} of the text in this TextView.
+     * Get the default primary {@link Locale} of the text in this TextView. This will always be
+     * the first member of {@link #getTextLocales()}.
+     * @return the default primary {@link Locale} of the text in this TextView.
      */
+    @NonNull
     public Locale getTextLocale() {
         return mTextPaint.getTextLocale();
     }
 
     /**
-     * Set the default {@link Locale} of the text in this TextView to the given value. This value
-     * is used to choose appropriate typefaces for ambiguous characters. Typically used for CJK
-     * locales to disambiguate Hanzi/Kanji/Hanja characters.
+     * Get the default {@link LocaleList} of the text in this TextView.
+     * @return the default {@link LocaleList} of the text in this TextView.
+     */
+    @NonNull @Size(min=1)
+    public LocaleList getTextLocales() {
+        return mTextPaint.getTextLocales();
+    }
+
+    /**
+     * Set the default {@link LocaleList} of the text in this TextView to a one-member list
+     * containing just the given value.
      *
      * @param locale the {@link Locale} for drawing text, must not be null.
      *
-     * @see Paint#setTextLocale
+     * @see #setTextLocales
      */
-    public void setTextLocale(Locale locale) {
-        mLocaleChanged = true;
+    public void setTextLocale(@NonNull Locale locale) {
+        mLocalesChanged = true;
         mTextPaint.setTextLocale(locale);
     }
 
+    /**
+     * Set the default {@link LocaleList} of the text in this TextView to the given value.
+     *
+     * This value is used to choose appropriate typefaces for ambiguous characters (typically used
+     * for CJK locales to disambiguate Hanzi/Kanji/Hanja characters). It also affects
+     * other aspects of text display, including line breaking.
+     *
+     * @param locales the {@link LocaleList} for drawing text, must not be null or empty.
+     *
+     * @see Paint#setTextLocales
+     */
+    public void setTextLocales(@NonNull @Size(min=1) LocaleList locales) {
+        mLocalesChanged = true;
+        mTextPaint.setTextLocales(locales);
+    }
+
     @Override
     protected void onConfigurationChanged(Configuration newConfig) {
         super.onConfigurationChanged(newConfig);
-        if (!mLocaleChanged) {
-            mTextPaint.setTextLocale(Locale.getDefault());
+        if (!mLocalesChanged) {
+            mTextPaint.setTextLocales(LocaleList.getDefault());
         }
     }
 
diff --git a/core/java/com/android/internal/os/BatteryStatsHelper.java b/core/java/com/android/internal/os/BatteryStatsHelper.java
index f178c8c..4f4d3e0 100644
--- a/core/java/com/android/internal/os/BatteryStatsHelper.java
+++ b/core/java/com/android/internal/os/BatteryStatsHelper.java
@@ -338,7 +338,7 @@
         }
 
         if (mCpuPowerCalculator == null) {
-            mCpuPowerCalculator = new CpuPowerCalculator(mPowerProfile);
+            mCpuPowerCalculator = new CpuPowerCalculator();
         }
         mCpuPowerCalculator.reset();
 
diff --git a/core/java/com/android/internal/os/CpuPowerCalculator.java b/core/java/com/android/internal/os/CpuPowerCalculator.java
index a3ef612..d62f7a6 100644
--- a/core/java/com/android/internal/os/CpuPowerCalculator.java
+++ b/core/java/com/android/internal/os/CpuPowerCalculator.java
@@ -23,54 +23,14 @@
     private static final String TAG = "CpuPowerCalculator";
     private static final boolean DEBUG = BatteryStatsHelper.DEBUG;
 
-    private final double[] mPowerCpuNormal;
-
-    /**
-     * Reusable array for calculations.
-     */
-    private final long[] mSpeedStepTimes;
-
-    public CpuPowerCalculator(PowerProfile profile) {
-        final int speedSteps = profile.getNumSpeedSteps();
-        mPowerCpuNormal = new double[speedSteps];
-        mSpeedStepTimes = new long[speedSteps];
-        for (int p = 0; p < speedSteps; p++) {
-            mPowerCpuNormal[p] = profile.getAveragePower(PowerProfile.POWER_CPU_ACTIVE, p);
-        }
-    }
-
     @Override
     public void calculateApp(BatterySipper app, BatteryStats.Uid u, long rawRealtimeUs,
                              long rawUptimeUs, int statsType) {
-        final int speedSteps = mSpeedStepTimes.length;
-
-        long totalTimeAtSpeeds = 0;
-        for (int step = 0; step < speedSteps; step++) {
-            mSpeedStepTimes[step] = u.getTimeAtCpuSpeed(step, statsType);
-            totalTimeAtSpeeds += mSpeedStepTimes[step];
-        }
-        totalTimeAtSpeeds = Math.max(totalTimeAtSpeeds, 1);
-
         app.cpuTimeMs = (u.getUserCpuTimeUs(statsType) + u.getSystemCpuTimeUs(statsType)) / 1000;
-        if (DEBUG && app.cpuTimeMs != 0) {
-            Log.d(TAG, "UID " + u.getUid() + ": CPU time " + app.cpuTimeMs + " ms");
-        }
-
-        double cpuPowerMaMs = 0;
-        for (int step = 0; step < speedSteps; step++) {
-            final double ratio = (double) mSpeedStepTimes[step] / totalTimeAtSpeeds;
-            final double cpuSpeedStepPower = ratio * app.cpuTimeMs * mPowerCpuNormal[step];
-            if (DEBUG && ratio != 0) {
-                Log.d(TAG, "UID " + u.getUid() + ": CPU step #"
-                        + step + " ratio=" + BatteryStatsHelper.makemAh(ratio) + " power="
-                        + BatteryStatsHelper.makemAh(cpuSpeedStepPower / (60 * 60 * 1000)));
-            }
-            cpuPowerMaMs += cpuSpeedStepPower;
-        }
-
-        if (DEBUG && cpuPowerMaMs != 0) {
-            Log.d(TAG, "UID " + u.getUid() + ": cpu total power="
-                    + BatteryStatsHelper.makemAh(cpuPowerMaMs / (60 * 60 * 1000)));
+        app.cpuPowerMah = (double) u.getCpuPowerMaUs(statsType) / (60.0 * 60.0 * 1000.0 * 1000.0);
+        if (DEBUG && (app.cpuTimeMs != 0 || app.cpuPowerMah != 0)) {
+            Log.d(TAG, "UID " + u.getUid() + ": CPU time=" + app.cpuTimeMs + " ms power="
+                    + BatteryStatsHelper.makemAh(app.cpuPowerMah));
         }
 
         // Keep track of the package with highest drain.
@@ -108,8 +68,5 @@
             // Statistics may not have been gathered yet.
             app.cpuTimeMs = app.cpuFgTimeMs;
         }
-
-        // Convert the CPU power to mAh
-        app.cpuPowerMah = cpuPowerMaMs / (60 * 60 * 1000);
     }
 }
diff --git a/core/java/com/android/internal/os/ZygoteInit.java b/core/java/com/android/internal/os/ZygoteInit.java
index 9ca937c..af73097 100644
--- a/core/java/com/android/internal/os/ZygoteInit.java
+++ b/core/java/com/android/internal/os/ZygoteInit.java
@@ -179,9 +179,15 @@
 
     static void preload() {
         Log.d(TAG, "begin preload");
+        Trace.traceBegin(Trace.TRACE_TAG_DALVIK, "PreloadClasses");
         preloadClasses();
+        Trace.traceEnd(Trace.TRACE_TAG_DALVIK);
+        Trace.traceBegin(Trace.TRACE_TAG_DALVIK, "PreloadResources");
         preloadResources();
+        Trace.traceEnd(Trace.TRACE_TAG_DALVIK);
+        Trace.traceBegin(Trace.TRACE_TAG_DALVIK, "PreloadOpenGL");
         preloadOpenGL();
+        Trace.traceEnd(Trace.TRACE_TAG_DALVIK);
         preloadSharedLibraries();
         preloadTextResources();
         // Ask the WebViewFactory to do any initialization that must run in the zygote process,
@@ -265,6 +271,7 @@
                     continue;
                 }
 
+                Trace.traceBegin(Trace.TRACE_TAG_DALVIK, "PreloadClass " + line);
                 try {
                     if (false) {
                         Log.v(TAG, "Preloading " + line + "...");
@@ -290,6 +297,7 @@
                     }
                     throw new RuntimeException(t);
                 }
+                Trace.traceEnd(Trace.TRACE_TAG_DALVIK);
             }
 
             Log.i(TAG, "...preloaded " + count + " classes in "
@@ -302,7 +310,9 @@
             runtime.setTargetHeapUtilization(defaultUtilization);
 
             // Fill in dex caches with classes, fields, and methods brought in by preloading.
+            Trace.traceBegin(Trace.TRACE_TAG_DALVIK, "PreloadDexCaches");
             runtime.preloadDexCaches();
+            Trace.traceEnd(Trace.TRACE_TAG_DALVIK);
 
             // Bring back root. We'll need it later if we're in the zygote.
             if (droppedPriviliges) {
@@ -564,6 +574,7 @@
 
     public static void main(String argv[]) {
         try {
+            Trace.traceBegin(Trace.TRACE_TAG_DALVIK, "ZygoteInit");
             RuntimeInit.enableDdms();
             // Start profiling the zygote initialization.
             SamplingProfilerIntegration.start();
@@ -588,17 +599,23 @@
             }
 
             registerZygoteSocket(socketName);
+            Trace.traceBegin(Trace.TRACE_TAG_DALVIK, "ZygotePreload");
             EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_START,
                 SystemClock.uptimeMillis());
             preload();
             EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_END,
                 SystemClock.uptimeMillis());
+            Trace.traceEnd(Trace.TRACE_TAG_DALVIK);
 
             // Finish profiling the zygote initialization.
             SamplingProfilerIntegration.writeZygoteSnapshot();
 
             // Do an initial gc to clean up after startup
+            Trace.traceBegin(Trace.TRACE_TAG_DALVIK, "PostZygoteInitGC");
             gcAndFinalize();
+            Trace.traceEnd(Trace.TRACE_TAG_DALVIK);
+
+            Trace.traceEnd(Trace.TRACE_TAG_DALVIK);
 
             // Disable tracing so that forked processes do not inherit stale tracing tags from
             // Zygote.
diff --git a/core/java/com/android/internal/view/FloatingActionMode.java b/core/java/com/android/internal/view/FloatingActionMode.java
index 9761661..44df0ce 100644
--- a/core/java/com/android/internal/view/FloatingActionMode.java
+++ b/core/java/com/android/internal/view/FloatingActionMode.java
@@ -76,6 +76,15 @@
         mMenu = new MenuBuilder(context).setDefaultShowAsAction(
                 MenuItem.SHOW_AS_ACTION_IF_ROOM);
         setType(ActionMode.TYPE_FLOATING);
+        mMenu.setCallback(new MenuBuilder.Callback() {
+            @Override
+            public void onMenuModeChange(MenuBuilder menu) {}
+
+            @Override
+            public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
+                return mCallback.onActionItemClicked(FloatingActionMode.this, item);
+            }
+        });
         mContentRect = new Rect();
         mContentRectOnScreen = new Rect();
         mPreviousContentRectOnScreen = new Rect();
@@ -99,7 +108,7 @@
                 .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                         @Override
                     public boolean onMenuItemClick(MenuItem item) {
-                        return mCallback.onActionItemClicked(FloatingActionMode.this, item);
+                        return mMenu.performItemAction(item, 0);
                     }
                 });
         mFloatingToolbarVisibilityHelper = new FloatingToolbarVisibilityHelper(mFloatingToolbar);
diff --git a/core/java/com/android/internal/view/menu/ActionMenuItemView.java b/core/java/com/android/internal/view/menu/ActionMenuItemView.java
index 8db363d..ce5bc90 100644
--- a/core/java/com/android/internal/view/menu/ActionMenuItemView.java
+++ b/core/java/com/android/internal/view/menu/ActionMenuItemView.java
@@ -29,10 +29,10 @@
 import android.view.View;
 import android.view.accessibility.AccessibilityEvent;
 import android.widget.ActionMenuView;
+import android.widget.ForwardingListener;
 import android.widget.ListPopupWindow;
 import android.widget.TextView;
 import android.widget.Toast;
-import android.widget.ListPopupWindow.ForwardingListener;
 
 /**
  * @hide
@@ -320,7 +320,7 @@
         }
 
         @Override
-        public ListPopupWindow getPopup() {
+        public ShowableListMenu getPopup() {
             if (mPopupCallback != null) {
                 return mPopupCallback.getPopup();
             }
@@ -331,7 +331,7 @@
         protected boolean onForwardingStarted() {
             // Call the invoker, then check if the expected popup is showing.
             if (mItemInvoker != null && mItemInvoker.invokeItem(mItemData)) {
-                final ListPopupWindow popup = getPopup();
+                final ShowableListMenu popup = getPopup();
                 return popup != null && popup.isShowing();
             }
             return false;
@@ -339,7 +339,7 @@
 
         @Override
         protected boolean onForwardingStopped() {
-            final ListPopupWindow popup = getPopup();
+            final ShowableListMenu popup = getPopup();
             if (popup != null) {
                 popup.dismiss();
                 return true;
@@ -349,6 +349,6 @@
     }
 
     public static abstract class PopupCallback {
-        public abstract ListPopupWindow getPopup();
+        public abstract ShowableListMenu getPopup();
     }
 }
diff --git a/core/java/com/android/internal/view/menu/ListMenuItemView.java b/core/java/com/android/internal/view/menu/ListMenuItemView.java
index 29ac3f3..2526393 100644
--- a/core/java/com/android/internal/view/menu/ListMenuItemView.java
+++ b/core/java/com/android/internal/view/menu/ListMenuItemView.java
@@ -43,11 +43,13 @@
     private TextView mTitleView;
     private CheckBox mCheckBox;
     private TextView mShortcutView;
+    private ImageView mSubMenuArrowView;
     
     private Drawable mBackground;
     private int mTextAppearance;
     private Context mTextAppearanceContext;
     private boolean mPreserveIconSpacing;
+    private Drawable mSubMenuArrow;
     
     private int mMenuType;
     
@@ -68,6 +70,7 @@
         mPreserveIconSpacing = a.getBoolean(
                 com.android.internal.R.styleable.MenuView_preserveIconSpacing, false);
         mTextAppearanceContext = context;
+        mSubMenuArrow = a.getDrawable(com.android.internal.R.styleable.MenuView_subMenuArrow);
         
         a.recycle();
     }
@@ -77,7 +80,7 @@
     }
 
     public ListMenuItemView(Context context, AttributeSet attrs) {
-        this(context, attrs, 0);
+        this(context, attrs, com.android.internal.R.attr.listMenuViewStyle);
     }
 
     @Override
@@ -93,6 +96,10 @@
         }
         
         mShortcutView = (TextView) findViewById(com.android.internal.R.id.shortcut);
+        mSubMenuArrowView = (ImageView) findViewById(com.android.internal.R.id.submenuarrow);
+        if (mSubMenuArrowView != null) {
+            mSubMenuArrowView.setImageDrawable(mSubMenuArrow);
+        }
     }
 
     public void initialize(MenuItemImpl itemData, int menuType) {
@@ -106,6 +113,7 @@
         setShortcut(itemData.shouldShowShortcut(), itemData.getShortcut());
         setIcon(itemData.getIcon());
         setEnabled(itemData.isEnabled());
+        setSubMenuArrowVisible(itemData.hasSubMenu());
     }
 
     public void setForceShowIcon(boolean forceShow) {
@@ -186,6 +194,12 @@
         compoundButton.setChecked(checked);
     }
 
+    private void setSubMenuArrowVisible(boolean hasSubmenu) {
+        if (mSubMenuArrowView != null) {
+            mSubMenuArrowView.setVisibility(hasSubmenu ? View.VISIBLE : View.GONE);
+        }
+    }
+
     public void setShortcut(boolean showShortcut, char shortcutKey) {
         final int newVisibility = (showShortcut && mItemData.shouldShowShortcut())
                 ? VISIBLE : GONE;
diff --git a/core/java/com/android/internal/view/menu/MenuAdapter.java b/core/java/com/android/internal/view/menu/MenuAdapter.java
new file mode 100644
index 0000000..1e03b1f
--- /dev/null
+++ b/core/java/com/android/internal/view/menu/MenuAdapter.java
@@ -0,0 +1,109 @@
+/*
+ * 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.internal.view.menu;
+
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.BaseAdapter;
+
+import java.util.ArrayList;
+
+public class MenuAdapter extends BaseAdapter {
+    static final int ITEM_LAYOUT = com.android.internal.R.layout.popup_menu_item_layout;
+
+    MenuBuilder mAdapterMenu;
+
+    private int mExpandedIndex = -1;
+
+    private boolean mForceShowIcon;
+    private final boolean mOverflowOnly;
+    private final LayoutInflater mInflater;
+
+    public MenuAdapter(MenuBuilder menu, LayoutInflater inflater, boolean overflowOnly) {
+        mOverflowOnly = overflowOnly;
+        mInflater = inflater;
+        mAdapterMenu = menu;
+        findExpandedIndex();
+    }
+
+    public void setForceShowIcon(boolean forceShow) {
+        mForceShowIcon = forceShow;
+    }
+
+    public int getCount() {
+        ArrayList<MenuItemImpl> items = mOverflowOnly ?
+                mAdapterMenu.getNonActionItems() : mAdapterMenu.getVisibleItems();
+        if (mExpandedIndex < 0) {
+            return items.size();
+        }
+        return items.size() - 1;
+    }
+
+    public MenuBuilder getAdapterMenu() {
+        return mAdapterMenu;
+    }
+
+    public MenuItemImpl getItem(int position) {
+        ArrayList<MenuItemImpl> items = mOverflowOnly ?
+                mAdapterMenu.getNonActionItems() : mAdapterMenu.getVisibleItems();
+        if (mExpandedIndex >= 0 && position >= mExpandedIndex) {
+            position++;
+        }
+        return items.get(position);
+    }
+
+    public long getItemId(int position) {
+        // Since a menu item's ID is optional, we'll use the position as an
+        // ID for the item in the AdapterView
+        return position;
+    }
+
+    public View getView(int position, View convertView, ViewGroup parent) {
+        if (convertView == null) {
+            convertView = mInflater.inflate(ITEM_LAYOUT, parent, false);
+        }
+
+        MenuView.ItemView itemView = (MenuView.ItemView) convertView;
+        if (mForceShowIcon) {
+            ((ListMenuItemView) convertView).setForceShowIcon(true);
+        }
+        itemView.initialize(getItem(position), 0);
+        return convertView;
+    }
+
+    void findExpandedIndex() {
+        final MenuItemImpl expandedItem = mAdapterMenu.getExpandedItem();
+        if (expandedItem != null) {
+            final ArrayList<MenuItemImpl> items = mAdapterMenu.getNonActionItems();
+            final int count = items.size();
+            for (int i = 0; i < count; i++) {
+                final MenuItemImpl item = items.get(i);
+                if (item == expandedItem) {
+                    mExpandedIndex = i;
+                    return;
+                }
+            }
+        }
+        mExpandedIndex = -1;
+    }
+
+    @Override
+    public void notifyDataSetChanged() {
+        findExpandedIndex();
+        super.notifyDataSetChanged();
+    }
+}
\ No newline at end of file
diff --git a/core/java/com/android/internal/view/menu/MenuPopup.java b/core/java/com/android/internal/view/menu/MenuPopup.java
new file mode 100644
index 0000000..91788ca
--- /dev/null
+++ b/core/java/com/android/internal/view/menu/MenuPopup.java
@@ -0,0 +1,122 @@
+/*
+ * 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.internal.view.menu;
+
+import android.content.Context;
+import android.view.View;
+import android.view.View.MeasureSpec;
+import android.view.ViewGroup;
+import android.widget.FrameLayout;
+import android.widget.ListAdapter;
+import android.widget.PopupWindow;
+
+/**
+ * Base class for a menu popup abstraction - i.e., some type of menu, housed in a popup window
+ * environment.
+ *
+ * @hide
+ */
+public abstract class MenuPopup implements ShowableListMenu, MenuPresenter {
+
+    public abstract void setForceShowIcon(boolean forceShow);
+
+    /**
+     * Adds the given menu to the popup. If this is the first menu shown it'll be displayed; if it's
+     * a submenu it will be displayed adjacent to the most recent menu (if supported by the
+     * implementation).
+     *
+     * @param menu
+     */
+    public abstract void addMenu(MenuBuilder menu);
+
+    public abstract void setGravity(int dropDownGravity);
+
+    public abstract void setAnchorView(View anchor);
+
+    /**
+     * Set a listener to receive a callback when the popup is dismissed.
+     *
+     * @param listener Listener that will be notified when the popup is dismissed.
+     */
+    public abstract void setOnDismissListener(PopupWindow.OnDismissListener listener);
+
+    @Override
+    public void initForMenu(Context context, MenuBuilder menu) {
+        // Don't need to do anything; we added as a presenter in the constructor.
+    }
+
+    @Override
+    public MenuView getMenuView(ViewGroup root) {
+        throw new UnsupportedOperationException("MenuPopups manage their own views");
+    }
+
+    @Override
+    public boolean expandItemActionView(MenuBuilder menu, MenuItemImpl item) {
+        return false;
+    }
+
+    @Override
+    public boolean collapseItemActionView(MenuBuilder menu, MenuItemImpl item) {
+        return false;
+    }
+
+    @Override
+    public int getId() {
+        return 0;
+    }
+
+    /**
+     * Measures the width of the given menu view.
+     *
+     * @param view The view to measure.
+     * @return The width.
+     */
+    protected static int measureIndividualMenuWidth(ListAdapter adapter, ViewGroup parent,
+            Context context, int maxAllowedWidth) {
+        // Menus don't tend to be long, so this is more sane than it looks.
+        int maxWidth = 0;
+        View itemView = null;
+        int itemType = 0;
+
+        final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
+        final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
+        final int count = adapter.getCount();
+        for (int i = 0; i < count; i++) {
+            final int positionType = adapter.getItemViewType(i);
+            if (positionType != itemType) {
+                itemType = positionType;
+                itemView = null;
+            }
+
+            if (parent == null) {
+                parent = new FrameLayout(context);
+            }
+
+            itemView = adapter.getView(i, itemView, parent);
+            itemView.measure(widthMeasureSpec, heightMeasureSpec);
+
+            final int itemWidth = itemView.getMeasuredWidth();
+            if (itemWidth >= maxAllowedWidth) {
+                return maxAllowedWidth;
+            } else if (itemWidth > maxWidth) {
+                maxWidth = itemWidth;
+            }
+        }
+
+        return maxWidth;
+    }
+}
\ No newline at end of file
diff --git a/core/java/com/android/internal/view/menu/MenuPopupHelper.java b/core/java/com/android/internal/view/menu/MenuPopupHelper.java
index 13654a6..9a47fc1 100644
--- a/core/java/com/android/internal/view/menu/MenuPopupHelper.java
+++ b/core/java/com/android/internal/view/menu/MenuPopupHelper.java
@@ -17,57 +17,29 @@
 package com.android.internal.view.menu;
 
 import android.content.Context;
-import android.content.res.Resources;
 import android.os.Parcelable;
 import android.view.Gravity;
-import android.view.KeyEvent;
-import android.view.LayoutInflater;
-import android.view.MenuItem;
 import android.view.View;
-import android.view.View.MeasureSpec;
 import android.view.ViewGroup;
 import android.view.ViewTreeObserver;
-import android.widget.AdapterView;
-import android.widget.BaseAdapter;
-import android.widget.FrameLayout;
-import android.widget.ListAdapter;
-import android.widget.MenuPopupWindow;
 import android.widget.PopupWindow;
 
-import java.util.ArrayList;
-
 /**
  * Presents a menu as a small, simple popup anchored to another view.
+ *
  * @hide
  */
-public class MenuPopupHelper implements AdapterView.OnItemClickListener, View.OnKeyListener,
-        ViewTreeObserver.OnGlobalLayoutListener, PopupWindow.OnDismissListener,
-        View.OnAttachStateChangeListener, MenuPresenter {
-    static final int ITEM_LAYOUT = com.android.internal.R.layout.popup_menu_item_layout;
-
+public class MenuPopupHelper implements ViewTreeObserver.OnGlobalLayoutListener,
+        PopupWindow.OnDismissListener, View.OnAttachStateChangeListener, MenuPresenter {
     private final Context mContext;
-    private final LayoutInflater mInflater;
     private final MenuBuilder mMenu;
-    private final MenuAdapter mAdapter;
     private final boolean mOverflowOnly;
-    private final int mPopupMaxWidth;
     private final int mPopupStyleAttr;
     private final int mPopupStyleRes;
 
     private View mAnchorView;
-    private MenuPopupWindow mPopup;
+    private MenuPopup mPopup;
     private ViewTreeObserver mTreeObserver;
-    private Callback mPresenterCallback;
-
-    boolean mForceShowIcon;
-
-    private ViewGroup mMeasureParent;
-
-    /** Whether the cached content width value is valid. */
-    private boolean mHasContentWidth;
-
-    /** Cached content width from {@link #measureContentWidth}. */
-    private int mContentWidth;
 
     private int mDropDownGravity = Gravity.NO_GRAVITY;
 
@@ -87,33 +59,37 @@
     public MenuPopupHelper(Context context, MenuBuilder menu, View anchorView,
             boolean overflowOnly, int popupStyleAttr, int popupStyleRes) {
         mContext = context;
-        mInflater = LayoutInflater.from(context);
         mMenu = menu;
-        mAdapter = new MenuAdapter(mMenu);
         mOverflowOnly = overflowOnly;
         mPopupStyleAttr = popupStyleAttr;
         mPopupStyleRes = popupStyleRes;
-
-        final Resources res = context.getResources();
-        mPopupMaxWidth = Math.max(res.getDisplayMetrics().widthPixels / 2,
-                res.getDimensionPixelSize(com.android.internal.R.dimen.config_prefDialogWidth));
-
         mAnchorView = anchorView;
+        mPopup = createMenuPopup();
+    }
 
-        // Present the menu using our context, not the menu builder's context.
-        menu.addMenuPresenter(this, context);
+    private MenuPopup createMenuPopup() {
+        if (mContext.getResources().getBoolean(
+                com.android.internal.R.bool.config_enableCascadingSubmenus)) {
+            // TODO: Return a Cascading implementation of MenuPopup instead.
+            return new StandardMenuPopup(
+                    mContext, mMenu, mAnchorView, mPopupStyleAttr, mPopupStyleRes, mOverflowOnly);
+        }
+        return new StandardMenuPopup(
+                mContext, mMenu, mAnchorView, mPopupStyleAttr, mPopupStyleRes, mOverflowOnly);
     }
 
     public void setAnchorView(View anchor) {
         mAnchorView = anchor;
+        mPopup.setAnchorView(anchor);
     }
 
     public void setForceShowIcon(boolean forceShow) {
-        mForceShowIcon = forceShow;
+        mPopup.setForceShowIcon(forceShow);
     }
 
     public void setGravity(int gravity) {
         mDropDownGravity = gravity;
+        mPopup.setGravity(gravity);
     }
 
     public int getGravity() {
@@ -126,28 +102,21 @@
         }
     }
 
-    public MenuPopupWindow getPopup() {
+    public ShowableListMenu getPopup() {
         return mPopup;
     }
 
     /**
-     * Attempts to show the popup anchored to the view specified by
-     * {@link #setAnchorView(View)}.
+     * Attempts to show the popup anchored to the view specified by {@link #setAnchorView(View)}.
      *
-     * @return {@code true} if the popup was shown or was already showing prior
-     *         to calling this method, {@code false} otherwise
+     * @return {@code true} if the popup was shown or was already showing prior to calling this
+     *         method, {@code false} otherwise
      */
     public boolean tryShow() {
         if (isShowing()) {
             return true;
         }
 
-        mPopup = new MenuPopupWindow(mContext, null, mPopupStyleAttr, mPopupStyleRes);
-        mPopup.setOnDismissListener(this);
-        mPopup.setOnItemClickListener(this);
-        mPopup.setAdapter(mAdapter);
-        mPopup.setModal(true);
-
         final View anchor = mAnchorView;
         if (anchor != null) {
             final boolean addGlobalListener = mTreeObserver == null;
@@ -155,20 +124,19 @@
             if (addGlobalListener) mTreeObserver.addOnGlobalLayoutListener(this);
             anchor.addOnAttachStateChangeListener(this);
             mPopup.setAnchorView(anchor);
-            mPopup.setDropDownGravity(mDropDownGravity);
+            mPopup.setGravity(mDropDownGravity);
         } else {
             return false;
         }
 
-        if (!mHasContentWidth) {
-            mContentWidth = measureContentWidth();
-            mHasContentWidth = true;
-        }
+        // In order for subclasses of MenuPopupHelper to satisfy the OnDismissedListener interface,
+        // we must set the listener to this outer Helper rather than to the inner MenuPopup.
+        // Not to worry -- the inner MenuPopup will call our own #onDismiss method after it's done
+        // its own handling.
+        mPopup.setOnDismissListener(this);
 
-        mPopup.setContentWidth(mContentWidth);
-        mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
+        mPopup.addMenu(mMenu);
         mPopup.show();
-        mPopup.getListView().setOnKeyListener(this);
         return true;
     }
 
@@ -181,7 +149,6 @@
     @Override
     public void onDismiss() {
         mPopup = null;
-        mMenu.close();
         if (mTreeObserver != null) {
             if (!mTreeObserver.isAlive()) mTreeObserver = mAnchorView.getViewTreeObserver();
             mTreeObserver.removeGlobalOnLayoutListener(this);
@@ -195,56 +162,6 @@
     }
 
     @Override
-    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
-        MenuAdapter adapter = mAdapter;
-        adapter.mAdapterMenu.performItemAction(adapter.getItem(position), 0);
-    }
-
-    @Override
-    public boolean onKey(View v, int keyCode, KeyEvent event) {
-        if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_MENU) {
-            dismiss();
-            return true;
-        }
-        return false;
-    }
-
-    private int measureContentWidth() {
-        // Menus don't tend to be long, so this is more sane than it looks.
-        int maxWidth = 0;
-        View itemView = null;
-        int itemType = 0;
-
-        final ListAdapter adapter = mAdapter;
-        final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
-        final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
-        final int count = adapter.getCount();
-        for (int i = 0; i < count; i++) {
-            final int positionType = adapter.getItemViewType(i);
-            if (positionType != itemType) {
-                itemType = positionType;
-                itemView = null;
-            }
-
-            if (mMeasureParent == null) {
-                mMeasureParent = new FrameLayout(mContext);
-            }
-
-            itemView = adapter.getView(i, itemView, mMeasureParent);
-            itemView.measure(widthMeasureSpec, heightMeasureSpec);
-
-            final int itemWidth = itemView.getMeasuredWidth();
-            if (itemWidth >= mPopupMaxWidth) {
-                return mPopupMaxWidth;
-            } else if (itemWidth > maxWidth) {
-                maxWidth = itemWidth;
-            }
-        }
-
-        return maxWidth;
-    }
-
-    @Override
     public void onGlobalLayout() {
         if (isShowing()) {
             final View anchor = mAnchorView;
@@ -282,54 +199,22 @@
 
     @Override
     public void updateMenuView(boolean cleared) {
-        mHasContentWidth = false;
-
-        if (mAdapter != null) {
-            mAdapter.notifyDataSetChanged();
-        }
+        mPopup.updateMenuView(cleared);
     }
 
     @Override
     public void setCallback(Callback cb) {
-        mPresenterCallback = cb;
+        mPopup.setCallback(cb);
     }
 
     @Override
     public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
-        if (subMenu.hasVisibleItems()) {
-            MenuPopupHelper subPopup = new MenuPopupHelper(mContext, subMenu, mAnchorView);
-            subPopup.setCallback(mPresenterCallback);
-
-            boolean preserveIconSpacing = false;
-            final int count = subMenu.size();
-            for (int i = 0; i < count; i++) {
-                MenuItem childItem = subMenu.getItem(i);
-                if (childItem.isVisible() && childItem.getIcon() != null) {
-                    preserveIconSpacing = true;
-                    break;
-                }
-            }
-            subPopup.setForceShowIcon(preserveIconSpacing);
-
-            if (subPopup.tryShow()) {
-                if (mPresenterCallback != null) {
-                    mPresenterCallback.onOpenSubMenu(subMenu);
-                }
-                return true;
-            }
-        }
-        return false;
+        return mPopup.onSubMenuSelected(subMenu);
     }
 
     @Override
     public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
-        // Only care about the (sub)menu we're presenting.
-        if (menu != mMenu) return;
-
-        dismiss();
-        if (mPresenterCallback != null) {
-            mPresenterCallback.onCloseMenu(menu, allMenusAreClosing);
-        }
+        mPopup.onCloseMenu(menu, allMenusAreClosing);
     }
 
     @Override
@@ -337,10 +222,12 @@
         return false;
     }
 
+    @Override
     public boolean expandItemActionView(MenuBuilder menu, MenuItemImpl item) {
         return false;
     }
 
+    @Override
     public boolean collapseItemActionView(MenuBuilder menu, MenuItemImpl item) {
         return false;
     }
@@ -358,73 +245,4 @@
     @Override
     public void onRestoreInstanceState(Parcelable state) {
     }
-
-    private class MenuAdapter extends BaseAdapter {
-        private MenuBuilder mAdapterMenu;
-        private int mExpandedIndex = -1;
-
-        public MenuAdapter(MenuBuilder menu) {
-            mAdapterMenu = menu;
-            findExpandedIndex();
-        }
-
-        public int getCount() {
-            ArrayList<MenuItemImpl> items = mOverflowOnly ?
-                    mAdapterMenu.getNonActionItems() : mAdapterMenu.getVisibleItems();
-            if (mExpandedIndex < 0) {
-                return items.size();
-            }
-            return items.size() - 1;
-        }
-
-        public MenuItemImpl getItem(int position) {
-            ArrayList<MenuItemImpl> items = mOverflowOnly ?
-                    mAdapterMenu.getNonActionItems() : mAdapterMenu.getVisibleItems();
-            if (mExpandedIndex >= 0 && position >= mExpandedIndex) {
-                position++;
-            }
-            return items.get(position);
-        }
-
-        public long getItemId(int position) {
-            // Since a menu item's ID is optional, we'll use the position as an
-            // ID for the item in the AdapterView
-            return position;
-        }
-
-        public View getView(int position, View convertView, ViewGroup parent) {
-            if (convertView == null) {
-                convertView = mInflater.inflate(ITEM_LAYOUT, parent, false);
-            }
-
-            MenuView.ItemView itemView = (MenuView.ItemView) convertView;
-            if (mForceShowIcon) {
-                ((ListMenuItemView) convertView).setForceShowIcon(true);
-            }
-            itemView.initialize(getItem(position), 0);
-            return convertView;
-        }
-
-        void findExpandedIndex() {
-            final MenuItemImpl expandedItem = mMenu.getExpandedItem();
-            if (expandedItem != null) {
-                final ArrayList<MenuItemImpl> items = mMenu.getNonActionItems();
-                final int count = items.size();
-                for (int i = 0; i < count; i++) {
-                    final MenuItemImpl item = items.get(i);
-                    if (item == expandedItem) {
-                        mExpandedIndex = i;
-                        return;
-                    }
-                }
-            }
-            mExpandedIndex = -1;
-        }
-
-        @Override
-        public void notifyDataSetChanged() {
-            findExpandedIndex();
-            super.notifyDataSetChanged();
-        }
-    }
 }
diff --git a/core/java/com/android/internal/view/menu/ShowableListMenu.java b/core/java/com/android/internal/view/menu/ShowableListMenu.java
new file mode 100644
index 0000000..ca158fd
--- /dev/null
+++ b/core/java/com/android/internal/view/menu/ShowableListMenu.java
@@ -0,0 +1,35 @@
+/*
+ * 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.internal.view.menu;
+
+import android.widget.ListView;
+
+/**
+ * A list menu which can be shown and hidden and which is internally represented by a ListView.
+ */
+public interface ShowableListMenu {
+    public void show();
+
+    public void dismiss();
+
+    public boolean isShowing();
+
+    /**
+     * @return The internal ListView for the visible menu.
+     */
+    public ListView getListView();
+}
diff --git a/core/java/com/android/internal/view/menu/StandardMenuPopup.java b/core/java/com/android/internal/view/menu/StandardMenuPopup.java
new file mode 100644
index 0000000..9a30ffa
--- /dev/null
+++ b/core/java/com/android/internal/view/menu/StandardMenuPopup.java
@@ -0,0 +1,246 @@
+package com.android.internal.view.menu;
+
+import android.content.Context;
+import android.content.res.Resources;
+import android.os.Parcelable;
+import android.view.Gravity;
+import android.view.KeyEvent;
+import android.view.LayoutInflater;
+import android.view.MenuItem;
+import android.view.View;
+import android.view.ViewGroup;
+import android.view.View.OnKeyListener;
+import android.widget.AdapterView;
+import android.widget.ListView;
+import android.widget.MenuPopupWindow;
+import android.widget.PopupWindow;
+import android.widget.AdapterView.OnItemClickListener;
+import android.widget.PopupWindow.OnDismissListener;
+
+import com.android.internal.util.Preconditions;
+
+/**
+ * A standard menu popup in which when a submenu is opened, it replaces its parent menu in the
+ * viewport.
+ */
+final class StandardMenuPopup extends MenuPopup implements OnDismissListener, OnItemClickListener,
+        MenuPresenter, OnKeyListener {
+
+    private final Context mContext;
+    private final LayoutInflater mInflater;
+    private final MenuBuilder mMenu;
+    private final MenuAdapter mAdapter;
+    private final boolean mOverflowOnly;
+    private final int mPopupMaxWidth;
+    private final int mPopupStyleAttr;
+    private final int mPopupStyleRes;
+
+    private PopupWindow.OnDismissListener mOnDismissListener;
+
+    private View mAnchorView;
+    private MenuPopupWindow mPopup;
+    private Callback mPresenterCallback;
+
+    private ViewGroup mMeasureParent;
+
+    /** Whether the cached content width value is valid. */
+    private boolean mHasContentWidth;
+
+    /** Cached content width. */
+    private int mContentWidth;
+
+    private int mDropDownGravity = Gravity.NO_GRAVITY;
+
+    public StandardMenuPopup(Context context, MenuBuilder menu, View anchorView, int popupStyleAttr,
+            int popupStyleRes, boolean overflowOnly) {
+        mContext = Preconditions.checkNotNull(context);
+        mInflater = LayoutInflater.from(context);
+        mMenu = menu;
+        mOverflowOnly = overflowOnly;
+        mAdapter = new MenuAdapter(menu, mInflater, mOverflowOnly);
+        mPopupStyleAttr = popupStyleAttr;
+        mPopupStyleRes = popupStyleRes;
+
+        final Resources res = context.getResources();
+        mPopupMaxWidth = Math.max(res.getDisplayMetrics().widthPixels / 2,
+                res.getDimensionPixelSize(com.android.internal.R.dimen.config_prefDialogWidth));
+
+        mAnchorView = anchorView;
+
+        mPopup = new MenuPopupWindow(mContext, null, mPopupStyleAttr, mPopupStyleRes);
+
+        // Present the menu using our context, not the menu builder's context.
+        menu.addMenuPresenter(this, context);
+    }
+
+    @Override
+    public void setForceShowIcon(boolean forceShow) {
+        mAdapter.setForceShowIcon(forceShow);
+    }
+
+    @Override
+    public void setGravity(int gravity) {
+        mDropDownGravity = gravity;
+    }
+
+    private boolean tryShow() {
+        if (isShowing()) {
+            return true;
+        }
+
+        mPopup.setOnDismissListener(this);
+        mPopup.setOnItemClickListener(this);
+        mPopup.setAdapter(mAdapter);
+        mPopup.setModal(true);
+
+        final View anchor = mAnchorView;
+        if (anchor != null) {
+            mPopup.setAnchorView(anchor);
+            mPopup.setDropDownGravity(mDropDownGravity);
+        } else {
+            return false;
+        }
+
+        if (!mHasContentWidth) {
+            mContentWidth = measureIndividualMenuWidth(
+                    mAdapter, mMeasureParent, mContext, mPopupMaxWidth);
+            mHasContentWidth = true;
+        }
+
+        mPopup.setContentWidth(mContentWidth);
+        mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
+        mPopup.show();
+        mPopup.getListView().setOnKeyListener(this);
+        return true;
+    }
+
+    @Override
+    public void show() {
+        if (!tryShow()) {
+            throw new IllegalStateException("MenuPopupHelper cannot be used without an anchor");
+        }
+    }
+
+    @Override
+    public void dismiss() {
+        if (isShowing()) {
+            mPopup.dismiss();
+        }
+    }
+
+    @Override
+    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
+        MenuAdapter adapter = mAdapter;
+        adapter.mAdapterMenu.performItemAction(adapter.getItem(position), 0);
+    }
+
+    @Override
+    public void addMenu(MenuBuilder menu) {
+        // No-op: standard implementation has only one menu which is set in the constructor.
+    }
+
+    @Override
+    public boolean isShowing() {
+        return mPopup != null && mPopup.isShowing();
+    }
+
+    @Override
+    public void onDismiss() {
+        mPopup = null;
+        mMenu.close();
+
+        mOnDismissListener.onDismiss();
+    }
+
+    @Override
+    public void updateMenuView(boolean cleared) {
+        mHasContentWidth = false;
+
+        if (mAdapter != null) {
+            mAdapter.notifyDataSetChanged();
+        }
+    }
+
+    @Override
+    public void setCallback(Callback cb) {
+        mPresenterCallback = cb;
+    }
+
+    @Override
+    public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
+        if (subMenu.hasVisibleItems()) {
+            MenuPopupHelper subPopup = new MenuPopupHelper(
+                    mContext, subMenu, mAnchorView, mOverflowOnly, mPopupStyleAttr, mPopupStyleRes);
+            subPopup.setCallback(mPresenterCallback);
+
+            boolean preserveIconSpacing = false;
+            final int count = subMenu.size();
+            for (int i = 0; i < count; i++) {
+                MenuItem childItem = subMenu.getItem(i);
+                if (childItem.isVisible() && childItem.getIcon() != null) {
+                    preserveIconSpacing = true;
+                    break;
+                }
+            }
+            subPopup.setForceShowIcon(preserveIconSpacing);
+
+            if (subPopup.tryShow()) {
+                if (mPresenterCallback != null) {
+                    mPresenterCallback.onOpenSubMenu(subMenu);
+                }
+                return true;
+            }
+        }
+        return false;
+    }
+
+    @Override
+    public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
+        // Only care about the (sub)menu we're presenting.
+        if (menu != mMenu) return;
+
+        dismiss();
+        if (mPresenterCallback != null) {
+            mPresenterCallback.onCloseMenu(menu, allMenusAreClosing);
+        }
+    }
+
+    @Override
+    public boolean flagActionItems() {
+        return false;
+    }
+
+
+    @Override
+    public Parcelable onSaveInstanceState() {
+        return null;
+    }
+
+    @Override
+    public void onRestoreInstanceState(Parcelable state) {
+    }
+
+    @Override
+    public void setAnchorView(View anchor) {
+        mAnchorView = anchor;
+    }
+
+    @Override
+    public boolean onKey(View v, int keyCode, KeyEvent event) {
+        if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_MENU) {
+            dismiss();
+            return true;
+        }
+        return false;
+    }
+
+    @Override
+    public void setOnDismissListener(OnDismissListener listener) {
+        mOnDismissListener = listener;
+    }
+
+    @Override
+    public ListView getListView() {
+        return mPopup.getListView();
+    }
+}
\ No newline at end of file
diff --git a/core/java/com/android/internal/widget/AbsActionBarView.java b/core/java/com/android/internal/widget/AbsActionBarView.java
index 35eeca7..582c4f1 100644
--- a/core/java/com/android/internal/widget/AbsActionBarView.java
+++ b/core/java/com/android/internal/widget/AbsActionBarView.java
@@ -19,6 +19,7 @@
 
 import android.util.TypedValue;
 import android.view.ContextThemeWrapper;
+import android.view.MotionEvent;
 import android.widget.ActionMenuPresenter;
 import android.widget.ActionMenuView;
 
@@ -53,6 +54,9 @@
 
     protected Animator mVisibilityAnim;
 
+    private boolean mEatingTouch;
+    private boolean mEatingHover;
+
     public AbsActionBarView(Context context) {
         this(context, null);
     }
@@ -97,6 +101,57 @@
         }
     }
 
+    @Override
+    public boolean onTouchEvent(MotionEvent ev) {
+        // ActionBarViews always eat touch events, but should still respect the touch event dispatch
+        // contract. If the normal View implementation doesn't want the events, we'll just silently
+        // eat the rest of the gesture without reporting the events to the default implementation
+        // since that's what it expects.
+
+        final int action = ev.getActionMasked();
+        if (action == MotionEvent.ACTION_DOWN) {
+            mEatingTouch = false;
+        }
+
+        if (!mEatingTouch) {
+            final boolean handled = super.onTouchEvent(ev);
+            if (action == MotionEvent.ACTION_DOWN && !handled) {
+                mEatingTouch = true;
+            }
+        }
+
+        if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
+            mEatingTouch = false;
+        }
+
+        return true;
+    }
+
+    @Override
+    public boolean onHoverEvent(MotionEvent ev) {
+        // Same deal as onTouchEvent() above. Eat all hover events, but still
+        // respect the touch event dispatch contract.
+
+        final int action = ev.getActionMasked();
+        if (action == MotionEvent.ACTION_HOVER_ENTER) {
+            mEatingHover = false;
+        }
+
+        if (!mEatingHover) {
+            final boolean handled = super.onHoverEvent(ev);
+            if (action == MotionEvent.ACTION_HOVER_ENTER && !handled) {
+                mEatingHover = true;
+            }
+        }
+
+        if (action == MotionEvent.ACTION_HOVER_EXIT
+                || action == MotionEvent.ACTION_CANCEL) {
+            mEatingHover = false;
+        }
+
+        return true;
+    }
+
     /**
      * Sets whether the bar should be split right now, no questions asked.
      * @param split true if the bar should split
diff --git a/core/java/com/android/internal/widget/LockPatternUtils.java b/core/java/com/android/internal/widget/LockPatternUtils.java
index 073a2ad..d96a909 100644
--- a/core/java/com/android/internal/widget/LockPatternUtils.java
+++ b/core/java/com/android/internal/widget/LockPatternUtils.java
@@ -232,7 +232,7 @@
     public void reportFailedPasswordAttempt(int userId) {
         getDevicePolicyManager().reportFailedPasswordAttempt(userId);
         getTrustManager().reportUnlockAttempt(false /* authenticated */, userId);
-        requireCredentialEntry(userId);
+        requireStrongAuth(StrongAuthTracker.SOME_AUTH_REQUIRED_AFTER_WRONG_CREDENTIAL, userId);
     }
 
     public void reportSuccessfulPasswordAttempt(int userId) {
@@ -1178,7 +1178,7 @@
      * @param userId either an explicit user id or {@link android.os.UserHandle#USER_ALL}
      */
     public void requireCredentialEntry(int userId) {
-        requireStrongAuth(StrongAuthTracker.SOME_AUTH_REQUIRED_AFTER_USER_REQUEST, userId);
+        requireStrongAuth(StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_USER_REQUEST, userId);
     }
 
     /**
@@ -1260,7 +1260,7 @@
                 value = { STRONG_AUTH_NOT_REQUIRED,
                         STRONG_AUTH_REQUIRED_AFTER_BOOT,
                         STRONG_AUTH_REQUIRED_AFTER_DPM_LOCK_NOW,
-                        SOME_AUTH_REQUIRED_AFTER_USER_REQUEST})
+                        STRONG_AUTH_REQUIRED_AFTER_USER_REQUEST})
         @Retention(RetentionPolicy.SOURCE)
         public @interface StrongAuthFlags {}
 
@@ -1275,14 +1275,14 @@
         public static final int STRONG_AUTH_REQUIRED_AFTER_BOOT = 0x1;
 
         /**
-         * Strong authentication is required because a device admin has requested it.
+         * Strong authentication is required because a device admin has temporarily requested it.
          */
         public static final int STRONG_AUTH_REQUIRED_AFTER_DPM_LOCK_NOW = 0x2;
 
         /**
-         * Some authentication is required because the user has temporarily disabled trust.
+         * Strong authentication is required because the user has temporarily requested it.
          */
-        public static final int SOME_AUTH_REQUIRED_AFTER_USER_REQUEST = 0x4;
+        public static final int STRONG_AUTH_REQUIRED_AFTER_USER_REQUEST = 0x4;
 
         /**
          * Strong authentication is required because the user has been locked out after too many
@@ -1290,11 +1290,17 @@
          */
         public static final int STRONG_AUTH_REQUIRED_AFTER_LOCKOUT = 0x8;
 
+        /**
+         * Some authentication is required because the user has entered a wrong credential.
+         */
+        public static final int SOME_AUTH_REQUIRED_AFTER_WRONG_CREDENTIAL = 0x10;
+
         public static final int DEFAULT = STRONG_AUTH_REQUIRED_AFTER_BOOT;
-        private static final int ALLOWING_FINGERPRINT = SOME_AUTH_REQUIRED_AFTER_USER_REQUEST;
 
-        final SparseIntArray mStrongAuthRequiredForUser = new SparseIntArray();
+        private static final int ALLOWING_FINGERPRINT = STRONG_AUTH_NOT_REQUIRED
+                | SOME_AUTH_REQUIRED_AFTER_WRONG_CREDENTIAL;
 
+        private final SparseIntArray mStrongAuthRequiredForUser = new SparseIntArray();
         private final H mHandler;
 
         public StrongAuthTracker() {
diff --git a/core/jni/AndroidRuntime.cpp b/core/jni/AndroidRuntime.cpp
index f7a2f9f..e4bc800 100644
--- a/core/jni/AndroidRuntime.cpp
+++ b/core/jni/AndroidRuntime.cpp
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#define ATRACE_TAG ATRACE_TAG_DALVIK
 #define LOG_TAG "AndroidRuntime"
 //#define LOG_NDEBUG 0
 
@@ -23,6 +24,7 @@
 #include <binder/IServiceManager.h>
 #include <utils/Log.h>
 #include <utils/misc.h>
+#include <utils/Trace.h>
 #include <binder/Parcel.h>
 #include <utils/threads.h>
 #include <cutils/properties.h>
@@ -1437,6 +1439,7 @@
  */
 /*static*/ int AndroidRuntime::startReg(JNIEnv* env)
 {
+    ATRACE_NAME("RegisterAndroidNatives");
     /*
      * This hook causes all future threads created in this process to be
      * attached to the JavaVM.  (This needs to go away in favor of JNI
diff --git a/core/jni/android_view_SurfaceControl.cpp b/core/jni/android_view_SurfaceControl.cpp
index 54be410..d1acb59 100644
--- a/core/jni/android_view_SurfaceControl.cpp
+++ b/core/jni/android_view_SurfaceControl.cpp
@@ -57,6 +57,7 @@
     jfieldID secure;
     jfieldID appVsyncOffsetNanos;
     jfieldID presentationDeadlineNanos;
+    jfieldID colorTransform;
 } gPhysicalDisplayInfoClassInfo;
 
 static struct {
@@ -394,6 +395,8 @@
                 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);
     }
@@ -656,6 +659,8 @@
             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/com_android_internal_os_Zygote.cpp b/core/jni/com_android_internal_os_Zygote.cpp
index 414cbb4..aef70be 100644
--- a/core/jni/com_android_internal_os_Zygote.cpp
+++ b/core/jni/com_android_internal_os_Zygote.cpp
@@ -83,6 +83,14 @@
   pid_t pid;
   int status;
 
+  // It's necessary to save and restore the errno during this function.
+  // Since errno is stored per thread, changing it here modifies the errno
+  // on the thread on which this signal handler executes. If a signal occurs
+  // between a call and an errno check, it's possible to get the errno set
+  // here.
+  // See b/23572286 for extra information.
+  int saved_errno = errno;
+
   while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
      // Log process-death status that we care about.  In general it is
      // not safe to call LOG(...) from a signal handler because of
@@ -118,6 +126,8 @@
   if (pid < 0 && errno != ECHILD) {
     ALOGW("Zygote SIGCHLD error in waitpid: %s", strerror(errno));
   }
+
+  errno = saved_errno;
 }
 
 // Configures the SIGCHLD handler for the zygote process. This is configured
diff --git a/core/res/AndroidManifest.xml b/core/res/AndroidManifest.xml
index b0621e9..d6657dd 100644
--- a/core/res/AndroidManifest.xml
+++ b/core/res/AndroidManifest.xml
@@ -2069,6 +2069,12 @@
     <permission android:name="android.permission.SET_KEYBOARD_LAYOUT"
         android:protectionLevel="signature" />
 
+    <!-- Allows an application to monitor changes in tablet mode.
+         <p>Not for use by third-party applications.
+         @hide -->
+    <permission android:name="android.permission.TABLET_MODE_LISTENER"
+        android:protectionLevel="signature" />
+
     <!-- Allows an application to request installing packages. Apps
          targeting APIs greater than 22 must hold this permission in
          order to use {@link android.content.Intent#ACTION_INSTALL_PACKAGE}.
diff --git a/core/res/res/drawable/ic_arrow_drop_right_black_24dp.xml b/core/res/res/drawable/ic_arrow_drop_right_black_24dp.xml
new file mode 100644
index 0000000..2dd0540
--- /dev/null
+++ b/core/res/res/drawable/ic_arrow_drop_right_black_24dp.xml
@@ -0,0 +1,34 @@
+<?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.
+-->
+
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+    android:height="25.0dp"
+    android:viewportHeight="25.0"
+    android:viewportWidth="24.0"
+    android:width="25.0dp"
+    android:tint="?attr/colorControlNormal"
+    android:autoMirrored="true">
+
+    <group
+        android:name="arrow"
+        android:rotation="90.0"
+        android:pivotX="12.0"
+        android:pivotY="13.0"
+        android:translateY="1.0">
+        <path android:fillColor="#000000" android:pathData="M7,14 L12,9 L17,14 L7,14 Z" />
+        <path android:pathData="M0,0 L24,0 L24,24 L0,24 L0,0 Z" />
+    </group>
+</vector>
\ No newline at end of file
diff --git a/core/res/res/layout/popup_menu_item_layout.xml b/core/res/res/layout/popup_menu_item_layout.xml
index 0bc636f..8b8c93a 100644
--- a/core/res/res/layout/popup_menu_item_layout.xml
+++ b/core/res/res/layout/popup_menu_item_layout.xml
@@ -57,6 +57,15 @@
 
     </RelativeLayout>
 
+    <ImageView
+        android:id="@+id/submenuarrow"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_gravity="center"
+        android:layout_marginStart="8dp"
+        android:scaleType="center"
+        android:visibility="gone" />
+
     <!-- Checkbox, and/or radio button will be inserted here. -->
     
 </com.android.internal.view.menu.ListMenuItemView>
diff --git a/core/res/res/values-ca-watch/strings.xml b/core/res/res/values-ca-watch/strings.xml
index b01ca63..231450f 100644
--- a/core/res/res/values-ca-watch/strings.xml
+++ b/core/res/res/values-ca-watch/strings.xml
@@ -31,7 +31,7 @@
     <string name="permgrouplab_camerawear" msgid="4543951283103407017">"fer fotos i enregistrar vídeos"</string>
     <string name="permgrouplab_phonewear" msgid="134365036753766126">"fer i gestionar trucades telefòniques"</string>
     <string name="permgrouplab_sensorswear" msgid="1429324744329327663">"accedir a les dades del sensor sobre els signes vitals"</string>
-    <string name="permlab_statusBarServicewear" msgid="2469402818964691034">"ser la barra d\'estat"</string>
+    <string name="permlab_statusBarServicewear" msgid="2469402818964691034">"aparèixer a la barra d\'estat"</string>
     <string name="permlab_bodySensorswear" msgid="7857941041202791873">"accedir als sensors corporals (com ara monitors de freqüència cardíaca)"</string>
     <string name="permlab_accessFineLocationwear" msgid="5584423486924377563">"accedir a la ubicació precisa (basada en el GPS i la xarxa)"</string>
     <string name="permlab_accessCoarseLocationwear" msgid="5880746016230166090">"accedir a la ubicació aproximada (basada en la xarxa)"</string>
@@ -40,7 +40,7 @@
     <string name="permlab_manageProfileAndDeviceOwnerswear" msgid="7313340516937821847">"gestionar els propietaris del perfil i del dispositiu"</string>
     <string name="permlab_changeWimaxStatewear" msgid="3828470843939853744">"canviar l\'estat de WiMAX"</string>
     <string name="permlab_handoverStatuswear" msgid="4835786819716499249">"rebre l\'estat de la transferència d\'Android Beam"</string>
-    <string name="permlab_route_media_outputwear" msgid="8737024341474587192">"indicar la ruta de sortida del contingut multimèdia"</string>
+    <string name="permlab_route_media_outputwear" msgid="8737024341474587192">"indicar la sortida del fitxer multimèdia"</string>
     <string name="permlab_readInstallSessionswear" msgid="9059478058685861989">"llegir les sessions d\'instal·lació"</string>
-    <string name="permlab_requestInstallPackageswear" msgid="4982025836783539503">"sol·licitar els paquets d\'instal·lació"</string>
+    <string name="permlab_requestInstallPackageswear" msgid="4982025836783539503">"sol·licitar la instal·lació de paquets"</string>
 </resources>
diff --git a/core/res/res/values-el-watch/strings.xml b/core/res/res/values-el-watch/strings.xml
index 8081013..83d9c3d 100644
--- a/core/res/res/values-el-watch/strings.xml
+++ b/core/res/res/values-el-watch/strings.xml
@@ -42,5 +42,5 @@
     <string name="permlab_handoverStatuswear" msgid="4835786819716499249">"λήψη κατάστασης μεταφοράς Android Beam"</string>
     <string name="permlab_route_media_outputwear" msgid="8737024341474587192">"δρομολόγηση εξόδου μέσων"</string>
     <string name="permlab_readInstallSessionswear" msgid="9059478058685861989">"ανάγνωση περιόδων σύνδεσης εγκατάστασης"</string>
-    <string name="permlab_requestInstallPackageswear" msgid="4982025836783539503">"αίτημα εγκατάστασης πακέτων."</string>
+    <string name="permlab_requestInstallPackageswear" msgid="4982025836783539503">"αίτημα εγκατάστασης πακέτων"</string>
 </resources>
diff --git a/core/res/res/values-eu-rES-watch/strings.xml b/core/res/res/values-eu-rES-watch/strings.xml
index 011105e..0fca702 100644
--- a/core/res/res/values-eu-rES-watch/strings.xml
+++ b/core/res/res/values-eu-rES-watch/strings.xml
@@ -26,7 +26,7 @@
     <string name="permgrouplab_locationwear" msgid="6275317222482780209">"Atzitu erlojuaren kokapena"</string>
     <string name="permgrouplab_calendarwear" msgid="441900844045065081">"Atzitu egutegia"</string>
     <string name="permgrouplab_smswear" msgid="6849506550342974220">"Bidali eta ikusi SMS mezuak"</string>
-    <string name="permgrouplab_storagewear" msgid="1003807594193602313">"Atzitu erlojuko argazkiak, multimedia-elementuak eta fitxategiak"</string>
+    <string name="permgrouplab_storagewear" msgid="1003807594193602313">"Atzitu erlojuko argazkiak, multimedia-edukia eta fitxategiak"</string>
     <string name="permgrouplab_microphonewear" msgid="1047561180980891136">"Grabatu audioa"</string>
     <string name="permgrouplab_camerawear" msgid="4543951283103407017">"Atera argazkiak eta grabatu bideoak"</string>
     <string name="permgrouplab_phonewear" msgid="134365036753766126">"Egin eta kudeatu telefono-deiak"</string>
diff --git a/core/res/res/values-mcc204-mnc12/config.xml b/core/res/res/values-mcc204-mnc12/config.xml
new file mode 100644
index 0000000..80432d7
--- /dev/null
+++ b/core/res/res/values-mcc204-mnc12/config.xml
@@ -0,0 +1,25 @@
+<?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 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>20408</item>
+    </string-array>
+</resources>
diff --git a/core/res/res/values-mcc450-mnc06/config.xml b/core/res/res/values-mcc450-mnc06/config.xml
index 63f9823..819c2a5 100644
--- a/core/res/res/values-mcc450-mnc06/config.xml
+++ b/core/res/res/values-mcc450-mnc06/config.xml
@@ -25,4 +25,7 @@
     -->
     <integer name="config_mobile_mtu">1428</integer>
 
+    <!-- Do not set the system language as value of EF LI/EF PL -->
+    <bool name="config_use_sim_language_file">false</bool>
+
 </resources>
diff --git a/core/res/res/values-mcc450-mnc08/config.xml b/core/res/res/values-mcc450-mnc08/config.xml
index 950c62b..ca26ec1 100644
--- a/core/res/res/values-mcc450-mnc08/config.xml
+++ b/core/res/res/values-mcc450-mnc08/config.xml
@@ -25,4 +25,7 @@
     -->
     <integer name="config_mobile_mtu">1450</integer>
 
+    <!-- Do not set the system language as value of EF LI/EF PL -->
+    <bool name="config_use_sim_language_file">false</bool>
+
 </resources>
diff --git a/core/res/res/values-mcc730-mnc01/config.xml b/core/res/res/values-mcc730-mnc01/config.xml
new file mode 100644
index 0000000..22f4027
--- /dev/null
+++ b/core/res/res/values-mcc730-mnc01/config.xml
@@ -0,0 +1,25 @@
+<?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 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>73010</item>
+    </string-array>
+</resources>
diff --git a/core/res/res/values-mcc730-mnc07/config.xml b/core/res/res/values-mcc730-mnc07/config.xml
new file mode 100644
index 0000000..836ddf9
--- /dev/null
+++ b/core/res/res/values-mcc730-mnc07/config.xml
@@ -0,0 +1,25 @@
+<?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 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>73002</item>
+    </string-array>
+</resources>
diff --git a/core/res/res/values-mcc730-mnc08/config.xml b/core/res/res/values-mcc730-mnc08/config.xml
new file mode 100644
index 0000000..836ddf9
--- /dev/null
+++ b/core/res/res/values-mcc730-mnc08/config.xml
@@ -0,0 +1,25 @@
+<?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 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>73002</item>
+    </string-array>
+</resources>
diff --git a/core/res/res/values-mcc730-mnc10/config.xml b/core/res/res/values-mcc730-mnc10/config.xml
new file mode 100644
index 0000000..58b7d78
--- /dev/null
+++ b/core/res/res/values-mcc730-mnc10/config.xml
@@ -0,0 +1,25 @@
+<?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 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>73001</item>
+    </string-array>
+</resources>
diff --git a/core/res/res/values-ml-rIN/strings.xml b/core/res/res/values-ml-rIN/strings.xml
index 43264a7..2bdc9c8 100644
--- a/core/res/res/values-ml-rIN/strings.xml
+++ b/core/res/res/values-ml-rIN/strings.xml
@@ -1149,7 +1149,7 @@
     <string name="sync_really_delete" msgid="2572600103122596243">"ഇനങ്ങൾ ഇല്ലാതാക്കുക"</string>
     <string name="sync_undo_deletes" msgid="2941317360600338602">"ഇല്ലാതാക്കിയവ പഴയപടിയാക്കുക"</string>
     <string name="sync_do_nothing" msgid="3743764740430821845">"ഇപ്പോൾ ഒന്നും ചെയ്യേണ്ടതില്ല"</string>
-    <string name="choose_account_label" msgid="5655203089746423927">"ഒരു അക്കൗണ്ട് തിരഞ്ഞെടുക്കുക"</string>
+    <string name="choose_account_label" msgid="5655203089746423927">"അക്കൗണ്ട് തിരഞ്ഞെടുക്കൂ"</string>
     <string name="add_account_label" msgid="2935267344849993553">"ഒരു അക്കൗണ്ട് ചേർക്കുക"</string>
     <string name="add_account_button_label" msgid="3611982894853435874">"അക്കൗണ്ട് ചേർക്കുക"</string>
     <string name="number_picker_increment_button" msgid="2412072272832284313">"വർദ്ധിപ്പിക്കുക"</string>
diff --git a/core/res/res/values-ne-rNP-watch/strings.xml b/core/res/res/values-ne-rNP-watch/strings.xml
index a38de99..1b4ffc9 100644
--- a/core/res/res/values-ne-rNP-watch/strings.xml
+++ b/core/res/res/values-ne-rNP-watch/strings.xml
@@ -26,7 +26,7 @@
     <string name="permgrouplab_locationwear" msgid="6275317222482780209">"यो घडीको स्थान पहुँच गर्नुहोस्"</string>
     <string name="permgrouplab_calendarwear" msgid="441900844045065081">"आफ्नो पात्रोमा पहुँच गर्नुहोस्"</string>
     <string name="permgrouplab_smswear" msgid="6849506550342974220">"SMS सन्देशहरू पठाउनुहोस् र हेर्नुहोस्"</string>
-    <string name="permgrouplab_storagewear" msgid="1003807594193602313">"आफ्नो समयमा फोटो, मिडिया, र फाइलहरू हेर्नुहोस्"</string>
+    <string name="permgrouplab_storagewear" msgid="1003807594193602313">"आफ्नो समयमा तस्बिर, मिडिया, र फाइलहरू हेर्नुहोस्"</string>
     <string name="permgrouplab_microphonewear" msgid="1047561180980891136">"अडियो रेकर्ड गर्नुहोस्"</string>
     <string name="permgrouplab_camerawear" msgid="4543951283103407017">"तस्बिरहरू खिच्नुहोस् र भिडियो रेकर्ड गर्नुहोस्"</string>
     <string name="permgrouplab_phonewear" msgid="134365036753766126">"फोन कलहरू गर्नुहोस् र व्यवस्थापन गर्नुहोस्"</string>
diff --git a/core/res/res/values-ta-rIN-watch/strings.xml b/core/res/res/values-ta-rIN-watch/strings.xml
index 63e072f..629ca27 100644
--- a/core/res/res/values-ta-rIN-watch/strings.xml
+++ b/core/res/res/values-ta-rIN-watch/strings.xml
@@ -29,7 +29,7 @@
     <string name="permgrouplab_storagewear" msgid="1003807594193602313">"உங்கள் வாட்சில் உள்ள படங்கள், மீடியா மற்றும் கோப்புகளை அணுகும்"</string>
     <string name="permgrouplab_microphonewear" msgid="1047561180980891136">"ஆடியோவைப் பதிவுசெய்யும்"</string>
     <string name="permgrouplab_camerawear" msgid="4543951283103407017">"படங்களை எடுக்கும், வீடியோவைப் பதிவுசெய்யும்"</string>
-    <string name="permgrouplab_phonewear" msgid="134365036753766126">"மொபைல் அழைப்புகளைச் செய்யும், பெறும்"</string>
+    <string name="permgrouplab_phonewear" msgid="134365036753766126">"மொபைல் அழைப்புகளைச் செய்யும், நிர்வகிக்கும்"</string>
     <string name="permgrouplab_sensorswear" msgid="1429324744329327663">"உங்கள் உடலியக்கக் குறிகள் பற்றிய உணர்வித் தரவை அணுகும்"</string>
     <string name="permlab_statusBarServicewear" msgid="2469402818964691034">"நிலைப் பட்டியில் இருக்கும்"</string>
     <string name="permlab_bodySensorswear" msgid="7857941041202791873">"உடல் உணர்விகளை (இதயத் துடிப்பு மானிட்டர்கள் போன்றவை) அணுகும்"</string>
diff --git a/core/res/res/values-ur-rPK/strings.xml b/core/res/res/values-ur-rPK/strings.xml
index aa46949..e6e20f1 100644
--- a/core/res/res/values-ur-rPK/strings.xml
+++ b/core/res/res/values-ur-rPK/strings.xml
@@ -429,7 +429,7 @@
     <string name="fingerprint_error_no_space" msgid="1055819001126053318">"فنگر پرنٹ اسٹور نہیں کیا جا سکتا ہے۔ براہ کرم ایک موجودہ فنگر پرنٹ ہٹائیں۔"</string>
     <string name="fingerprint_error_timeout" msgid="3927186043737732875">"فنگر پرنٹ کی میعاد ختم ہوگئی۔ دوبارہ کوشش کریں۔"</string>
     <string name="fingerprint_error_canceled" msgid="4402024612660774395">"فنگر پرنٹ کی کارروائی منسوخ ہوگئی۔"</string>
-    <string name="fingerprint_error_lockout" msgid="5536934748136933450">"کافی زیادہ کوششیں کی گئیں۔ بعد میں دوباہ کوشش کریں۔"</string>
+    <string name="fingerprint_error_lockout" msgid="5536934748136933450">"کافی زیادہ کوششیں کی گئیں۔ بعد میں دوبارہ کوشش کریں۔"</string>
     <string name="fingerprint_error_unable_to_process" msgid="6107816084103552441">"دوبارہ کوشش کریں۔"</string>
     <string name="fingerprint_name_template" msgid="5870957565512716938">"انگلی <xliff:g id="FINGERID">%d</xliff:g>"</string>
   <string-array name="fingerprint_error_vendor">
diff --git a/core/res/res/values/attrs.xml b/core/res/res/values/attrs.xml
index 61af6c5..48bfe28 100644
--- a/core/res/res/values/attrs.xml
+++ b/core/res/res/values/attrs.xml
@@ -639,6 +639,8 @@
         <attr name="imageButtonStyle" format="reference" />
         <!-- The style resource to use for an ImageButton that is an image well. -->
         <attr name="imageWellStyle" format="reference" />
+        <!-- Default menu-style ListView style. -->
+        <attr name="listMenuViewStyle" format="reference" />
         <!-- Default ListView style. -->
         <attr name="listViewStyle" format="reference" />
         <!-- ListView with white background. -->
@@ -1818,6 +1820,7 @@
         <enum name="KEYCODE_MEDIA_SKIP_BACKWARD" value="273" />
         <enum name="KEYCODE_MEDIA_STEP_FORWARD" value="274" />
         <enum name="KEYCODE_MEDIA_STEP_BACKWARD" value="275" />
+        <enum name="KEYCODE_SOFT_SLEEP" value="276" />
     </attr>
 
     <!-- ***************************************************************** -->
@@ -3784,6 +3787,8 @@
         <attr name="itemIconDisabledAlpha" format="float" />
         <!-- Whether space should be reserved in layout when an icon is missing. -->
         <attr name="preserveIconSpacing" format="boolean" />
+        <!-- Drawable for the arrow icon indicating a particular item is a submenu. -->
+        <attr name="subMenuArrow" format="reference" />
     </declare-styleable>
     <declare-styleable name="IconMenuView">
         <!-- Defines the height of each row. -->
diff --git a/core/res/res/values/attrs_manifest.xml b/core/res/res/values/attrs_manifest.xml
index 4531f75..091d57f 100644
--- a/core/res/res/values/attrs_manifest.xml
+++ b/core/res/res/values/attrs_manifest.xml
@@ -24,7 +24,7 @@
     <!-- The overall theme to use for an activity.  Use with either the
          application tag (to supply a default theme for all activities) or
          the activity tag (to supply a specific theme for that activity).
-    
+
          <p>This automatically sets
          your activity's Context to use this theme, and may also be used
          for "starting" animations prior to the activity being launched (to
@@ -39,7 +39,7 @@
          tag (to supply a specific label for that component).  It may also be
          used with the intent-filter tag to supply a label to show to the
          user when an activity is being selected based on a particular Intent.
-    
+
          <p>The given label will be used wherever the user sees information
          about its associated component; for example, as the name of a
          main activity that is displayed in the launcher.  You should
@@ -47,7 +47,7 @@
          it can be localized, however it is also allowed to supply a plain
          string for quick and dirty programming. -->
     <attr name="label" format="reference|string" />
-    
+
     <!-- A Drawable resource providing a graphical representation of its
          associated item.  Use with the
          application tag (to supply a default icon for all application
@@ -55,7 +55,7 @@
          tag (to supply a specific icon for that component).  It may also be
          used with the intent-filter tag to supply an icon to show to the
          user when an activity is being selected based on a particular Intent.
-    
+
          <p>The given icon will be used to display to the user a graphical
          representation of its associated component; for example, as the icon
          for main activity that is displayed in the launcher.  This must be
@@ -95,15 +95,15 @@
 
     <!-- Name of the activity to be launched to manage application's space on
          device. The specified activity gets automatically launched when the
-         application's space needs to be managed and is usually invoked 
+         application's space needs to be managed and is usually invoked
          through user actions. Applications can thus provide their own custom
          behavior for managing space for various scenarios like out of memory
          conditions. This is an optional attribute and
-         applications can choose not to specify a default activity to 
+         applications can choose not to specify a default activity to
          manage space. -->
     <attr name="manageSpaceActivity" format="string" />
 
-    <!-- Option to let applications specify that user data can/cannot be 
+    <!-- Option to let applications specify that user data can/cannot be
          cleared. This flag is turned on by default.
          <em>This attribute is usable only by applications
          included in the system image. Third-party apps cannot use it.</em> -->
@@ -122,31 +122,31 @@
          kind of application can not be installed without the
          INSTALL_ALLOW_TEST flag, which means only through adb install.  -->
     <attr name="testOnly" format="boolean" />
-    
+
     <!-- A unique name for the given item.  This must use a Java-style naming
          convention to ensure the name is unique, for example
-         "com.mycompany.MyName". -->  
+         "com.mycompany.MyName". -->
     <attr name="name" format="string" />
-    
+
     <!-- Specify a permission that a client is required to have in order to
     	 use the associated object.  If the client does not hold the named
     	 permission, its request will fail.  See the
          <a href="{@docRoot}guide/topics/security/security.html">Security and Permissions</a>
          document for more information on permissions. -->
     <attr name="permission" format="string" />
-    
+
     <!-- A specific {@link android.R.attr#permission} name for read-only
          access to a {@link android.content.ContentProvider}.  See the
          <a href="{@docRoot}guide/topics/security/security.html">Security and Permissions</a>
          document for more information on permissions. -->
     <attr name="readPermission" format="string" />
-    
+
     <!-- A specific {@link android.R.attr#permission} name for write
          access to a {@link android.content.ContentProvider}.  See the
          <a href="{@docRoot}guide/topics/security/security.html">Security and Permissions</a>
          document for more information on permissions. -->
     <attr name="writePermission" format="string" />
-    
+
     <!-- If true, the {@link android.content.Context#grantUriPermission
          Context.grantUriPermission} or corresponding Intent flags can
          be used to allow others to access specific URIs in the content
@@ -156,7 +156,7 @@
          Context.revokeUriPermission} when URIs are deleted from your
          provider.-->
     <attr name="grantUriPermissions" format="boolean" />
-    
+
     <!-- Characterizes the potential risk implied in a permission and
          indicates the procedure the system should follow when determining
          whether to grant the permission to an application requesting it. {@link
@@ -257,7 +257,7 @@
          with.  The group must have been defined with the
          {@link android.R.styleable#AndroidManifestPermissionGroup permission-group} tag. -->
     <attr name="permissionGroup" format="string" />
-    
+
     <!-- Specify the name of a user ID that will be shared between multiple
          packages.  By default, each package gets its own unique user-id.
          By setting this value on two or more packages, each of these packages
@@ -265,13 +265,13 @@
          in the same process.  Note that for them to actually get the same
          user ID, they must also be signed with the same signature. -->
     <attr name="sharedUserId" format="string" />
-    
+
     <!-- Specify a label for the shared user UID of this package.  This is
          only used if you have also used android:sharedUserId.  This must
          be a reference to a string resource; it can not be an explicit
          string. -->
     <attr name="sharedUserLabel" format="reference" />
-    
+
     <!-- Internal version code.  This is the number used to determine whether
          one version is more recent than another: it has no other meaning than
          that higher numbers are more recent.  You could use this number to
@@ -279,7 +279,7 @@
          number, simply increase it by one each time a new version is
          released, or define it however else you want, as long as each
          successive version has a higher number.  This is not a version
-         number generally shown to the user, that is usually supplied 
+         number generally shown to the user, that is usually supplied
          with {@link android.R.attr#versionName}.  When an app is delivered
          as multiple split APKs, each APK must have the exact same versionCode. -->
     <attr name="versionCode" format="integer" />
@@ -296,7 +296,7 @@
          is used for no other purpose than display to the user; the actual
          significant version number is given by {@link android.R.attr#versionCode}. -->
     <attr name="versionName" format="string" />
-    
+
     <!-- Flag to control special persistent mode of an application.  This should
          not normally be used by applications; it requires that the system keep
          your application running at all times. -->
@@ -309,7 +309,7 @@
     <!-- Flag indicating whether the application can be debugged, even when
          running on a device that is running in user mode. -->
     <attr name="debuggable" format="boolean" />
-    
+
     <!-- Flag indicating whether the application requests the VM to operate in
          the safe mode.  -->
     <attr name="vmSafeMode" format="boolean" />
@@ -367,7 +367,7 @@
          Use with the application tag (to supply a default process for all
          application components), or with the activity, receiver, service,
          or provider tag (to supply a specific icon for that component).
-    
+
          <p>Application components are normally run in a single process that
          is created for the entire application.  You can use this tag to modify
          where they run.  If the process name begins with a ':' character,
@@ -378,12 +378,12 @@
          provided that you have permission to do so, allowing multiple
          applications to share one process to reduce resource usage. -->
     <attr name="process" format="string" />
-    
+
     <!-- Specify a task name that activities have an "affinity" to.
          Use with the application tag (to supply a default affinity for all
          activities in the application), or with the activity tag (to supply
          a specific affinity for that component).
-    
+
          <p>The default value for this attribute is the same as the package
          name, indicating that all activities in the manifest should generally
          be considered a single "application" to the user.  You can use this
@@ -392,13 +392,13 @@
          task from the user's perspective, or using an empty string for
          activities that have no affinity to a task. -->
     <attr name="taskAffinity" format="string" />
-    
+
     <!-- Specify that an activity can be moved out of a task it is in to
          the task it has an affinity for when appropriate.  Use with the
          application tag (to supply a default for all activities in the
          application), or with an activity tag (to supply a specific
          setting for that component).
-    
+
          <p>Normally when an application is started, it is associated with
          the task of the activity that started it and stays there for its
          entire lifetime.  You can use the allowTaskReparenting feature to force an
@@ -422,17 +422,17 @@
          applications' processes. On devices that support multiple instruction sets,
          this implies the code might be loaded into a process that's using any of the devices
          supported instruction sets.
-         
+
          <p> The system might treat such applications specially, for eg., by
          extracting the application's native libraries for all supported instruction
          sets or by compiling the application's dex code for all supported instruction
          sets. -->
     <attr name="multiArch" format ="boolean" />
-    
+
     <!-- Specify whether a component is allowed to have multiple instances
          of itself running in different processes.  Use with the activity
          and provider tags.
-    
+
          <p>Normally the system will ensure that all instances of a particular
          component are only running in a single process.  You can use this
          attribute to disable that behavior, allowing the system to create
@@ -441,28 +441,28 @@
          of a provider can be created in each client process, allowing them
          to be used without performing IPC.  -->
     <attr name="multiprocess" format="boolean" />
-    
+
     <!-- Specify whether an activity should be finished when its task is
          brought to the foreground by relaunching from the home screen.
-         
+
          <p>If both this option and {@link android.R.attr#allowTaskReparenting} are
          specified, the finish trumps the affinity: the affinity will be
          ignored and the activity simply finished. -->
     <attr name="finishOnTaskLaunch" format="boolean" />
-    
+
     <!-- Specify whether an activity should be finished when a "close system
          windows" request has been made.  This happens, for example, when
          the home key is pressed, when the device is locked, when a system
          dialog showing recent applications is displayed, etc. -->
     <attr name="finishOnCloseSystemDialogs" format="boolean" />
-    
+
     <!-- Specify whether an activity's task should be cleared when it
          is re-launched from the home screen.  As a result, every time the
          user starts the task, they will be brought to its root activity,
          regardless of whether they used BACK or HOME to last leave it.
          This flag only applies to activities that
          are used to start the root of a new task.
-         
+
          <p>An example of the use of this flag would be for the case where
          a user launches activity A from home, and from there goes to
          activity B.  They now press home, and then return to activity A.
@@ -471,7 +471,7 @@
          then upon going to the background all of the tasks on top of it (B
          in this case) are removed, so when the user next returns to A they
          will restart at its original activity.
-         
+
          <p>When this option is used in conjunction with
          {@link android.R.attr#allowTaskReparenting}, the allowTaskReparenting trumps the
          clear.  That is, all activities above the root activity of the
@@ -479,30 +479,30 @@
          to the task they are associated with, otherwise they will simply
          be dropped as described here. -->
     <attr name="clearTaskOnLaunch" format="boolean" />
-    
+
     <!-- Specify whether an activity should be kept in its history stack.
          If this attribute is set, then as soon as the user navigates away
          from the activity it will be finished and they will no longer be
          able to return to it. -->
     <attr name="noHistory" format="boolean" />
-    
+
     <!-- Specify whether an acitivty's task state should always be maintained
          by the system, or if it is allowed to reset the task to its initial
          state in certain situations.
-         
+
          <p>Normally the system will reset a task (remove all activities from
          the stack and reset the root activity) in certain situations when
          the user re-selects that task from the home screen.  Typically this
          will be done if the user hasn't visited that task for a certain
          amount of time, such as 30 minutes.
-         
+
          <p>By setting this attribute, the user will always return to your
          task in its last state, regardless of how they get there.  This is
          useful, for example, in an application like the web browser where there
          is a lot of state (such as multiple open tabs) that the application
          would not like to lose. -->
     <attr name="alwaysRetainTaskState" format="boolean" />
-    
+
     <!-- Indicates that an Activity does not need to have its freeze state
          (as returned by {@link android.app.Activity#onSaveInstanceState}
          retained in order to be restarted.  Generally you use this for activities
@@ -512,7 +512,7 @@
          it normally would.  Instead, the next time the user navigates to
          it its {@link android.app.Activity#onCreate} method will be called
          with a null icicle, just like it was starting for the first time.
-         
+
          <p>This is used by the Home activity to make sure it does not get
          removed if it crashes for some reason. -->
     <attr name="stateNotNeeded" format="boolean" />
@@ -539,11 +539,11 @@
          in order to avoid conflicts.  Typically this name is the same
          as the class implementation describing the provider's data structure. -->
     <attr name="authorities" format="string" />
-    
+
     <!-- Flag indicating whether this content provider would like to
          participate in data synchronization. -->
     <attr name="syncable" format="boolean" />
-    
+
     <!-- Flag declaring this activity to be 'immersive'; immersive activities
          should not be interrupted with other activities or notifications. -->
     <attr name="immersive" format="boolean" />
@@ -555,7 +555,7 @@
          The value is a simple integer, with higher numbers being
          initialized first. -->
     <attr name="initOrder" format="integer" />
-    
+
     <!-- Specify the relative importance or ability in handling a particular
          Intent.  For receivers, this controls the order in which they are
          executed to receive a broadcast (note that for
@@ -564,18 +564,18 @@
          Intent; when multiple activities match an intent and have different
          priorities, only those with the higher priority value will be
          considered a match.
-         
+
          <p>Only use if you really need to impose some specific
          order in which the broadcasts are received, or want to forcibly
          place an activity to always be preferred over others.  The value is a
          single integer, with higher numbers considered to be better. -->
     <attr name="priority" format="integer" />
-    
+
     <!-- Specify how an activity should be launched.  See the
          <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
          Stack</a> document for important information on how these options impact
          the behavior of your application.
-         
+
          <p>If this attribute is not specified, <code>standard</code> launch
          mode will be used.  Note that the particular launch behavior can
          be changed in some ways at runtime through the
@@ -612,19 +612,19 @@
             <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
             Stack</a> document for more details about tasks.-->
         <enum name="singleTask" value="2" />
-        <!-- Only allow one instance of this activity to ever be 
-            running.  This activity gets a unique task with only itself running 
-            in it; if it is ever launched again with the same Intent, then that 
-            task will be brought forward and its 
+        <!-- Only allow one instance of this activity to ever be
+            running.  This activity gets a unique task with only itself running
+            in it; if it is ever launched again with the same Intent, then that
+            task will be brought forward and its
             {@link android.app.Activity#onNewIntent Activity.onNewIntent()}
-            method called.  If this 
-            activity tries to start a new activity, that new activity will be 
+            method called.  If this
+            activity tries to start a new activity, that new activity will be
             launched in a separate task.  See the
             <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
             Stack</a> document for more details about tasks.-->
         <enum name="singleInstance" value="3" />
     </attr>
-    
+
     <!-- Specify the orientation an activity should be run in.  If not
          specified, it will run in the current preferred orientation
          of the screen.
@@ -638,7 +638,7 @@
              if this activity is the bottom of a task. If the user
              explicitly turned off sensor based orientation through settings
              sensor based device rotation will be ignored. If not by default
-             sensor based orientation will be taken into account and the 
+             sensor based orientation will be taken into account and the
              orientation will changed based on how the user rotates the device.
              Corresponds to
              {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}. -->
@@ -726,19 +726,19 @@
              {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_LOCKED}. -->
         <enum name="locked" value="14" />
     </attr>
-    
+
     <!-- Specify one or more configuration changes that the activity will
          handle itself.  If not specified, the activity will be restarted
          if any of these configuration changes happen in the system.  Otherwise,
          the activity will remain running and its
          {@link android.app.Activity#onConfigurationChanged Activity.onConfigurationChanged}
          method called with the new configuration.
-         
+
          <p>Note that all of these configuration changes can impact the
          resource values seen by the application, so you will generally need
          to re-retrieve all resources (including view layouts, drawables, etc)
          to correctly handle any configuration change.
-         
+
          <p>These values must be kept in sync with those in
          {@link android.content.pm.ActivityInfo} and
          include/utils/ResourceTypes.h. -->
@@ -804,18 +804,18 @@
 
     <!-- Descriptive text for the associated data. -->
     <attr name="description" format="reference" />
-    
+
     <!-- The name of the application package that an Instrumentation object
          will run against. -->
     <attr name="targetPackage" format="string" />
-    
+
     <!-- Flag indicating that an Instrumentation class wants to take care
          of starting/stopping profiling itself, rather than relying on
          the default behavior of profiling the complete time it is running.
          This allows it to target profiling data at a specific set of
          operations. -->
     <attr name="handleProfiling" format="boolean" />
-    
+
     <!-- Flag indicating that an Instrumentation class should be run as a
          functional test. -->
     <attr name="functionalTest" format="boolean" />
@@ -1069,8 +1069,7 @@
 
          <p>NOTE: The value of {@link android.R.attr#screenOrientation} will be ignored for
          resizeable activities as the system doesn't support fixed orientation on a resizeable
-         activity.
-         @hide -->
+         activity. -->
     <attr name="resizeableActivity" format="boolean" />
 
     <!-- This value indicates how tasks rooted at this activity will behave in lockTask mode.
@@ -1271,7 +1270,7 @@
          features in your package (or other packages).  See the
          <a href="{@docRoot}guide/topics/security/security.html">Security and Permissions</a>
          document for more information on permissions.
-         
+
          <p>This appears as a child tag of the root
          {@link #AndroidManifest manifest} tag. -->
     <declare-styleable name="AndroidManifestPermission" parent="AndroidManifest">
@@ -1290,15 +1289,15 @@
         <attr name="protectionLevel" />
         <attr name="permissionFlags" />
     </declare-styleable>
-    
+
     <!-- The <code>permission-group</code> tag declares a logical grouping of
          related permissions.
-         
+
          <p>Note that this tag does not declare a permission itself, only
          a namespace in which further permissions can be placed.  See
          the {@link #AndroidManifestPermission &lt;permission&gt;} tag for
          more information.
-         
+
          <p>This appears as a child tag of the root
          {@link #AndroidManifest manifest} tag. -->
     <declare-styleable name="AndroidManifestPermissionGroup" parent="AndroidManifest">
@@ -1316,7 +1315,7 @@
         <attr name="permissionGroupFlags" />
         <attr name="priority" />
     </declare-styleable>
-    
+
     <!-- The <code>permission-tree</code> tag declares the base of a tree of
          permission values: it declares that this package has ownership of
          the given permission name, as well as all names underneath it
@@ -1324,12 +1323,12 @@
          {@link android.content.pm.PackageManager#addPermission
          PackageManager.addPermission()} method to dynamically add new
          permissions under this tree.
-         
+
          <p>Note that this tag does not declare a permission itself, only
          a namespace in which further permissions can be placed.  See
          the {@link #AndroidManifestPermission &lt;permission&gt;} tag for
          more information.
-         
+
          <p>This appears as a child tag of the root
          {@link #AndroidManifest manifest} tag. -->
     <declare-styleable name="AndroidManifestPermissionTree" parent="AndroidManifest">
@@ -1346,7 +1345,7 @@
         <attr name="banner" />
         <attr name="logo" />
     </declare-styleable>
-    
+
     <!-- The <code>uses-permission</code> tag requests a
          {@link #AndroidManifestPermission &lt;permission&gt;} that the containing
          package must be granted in order for it to operate correctly. For runtime
@@ -1359,7 +1358,7 @@
          document for more information on permissions.  Also available is a
          {@link android.Manifest.permission list of permissions} included
          with the base platform.
-         
+
          <p>This appears as a child tag of the root
          {@link #AndroidManifest manifest} tag. -->
     <declare-styleable name="AndroidManifestUsesPermission" parent="AndroidManifest">
@@ -1436,7 +1435,7 @@
 
     <!-- The <code>uses-sdk</code> tag describes the SDK features that the
          containing package must be running on to operate correctly.
-         
+
          <p>This appears as a child tag of the root
          {@link #AndroidManifest manifest} tag. -->
     <declare-styleable name="AndroidManifestUsesSdk" parent="AndroidManifest">
@@ -1464,7 +1463,7 @@
              incompatibility with them. -->
         <attr name="maxSdkVersion" />
     </declare-styleable>
-    
+
     <!-- The <code>library</code> tag declares that this apk is providing itself
          as a shared library for other applications to use.  It can only be used
          with apks that are built in to the system image.  Other apks can link to
@@ -1483,7 +1482,7 @@
     <!-- The <code>uses-libraries</code> specifies a shared library that this
          package requires to be linked against.  Specifying this flag tells the
          system to include this library's code in your class loader.
-         
+
          <p>This appears as a child tag of the
          {@link #AndroidManifestApplication application} tag. -->
     <declare-styleable name="AndroidManifestUsesLibrary" parent="AndroidManifestApplication">
@@ -1498,7 +1497,7 @@
               dynamically at runtime. -->
         <attr name="required" />
     </declare-styleable>
-    
+
     <!-- The <code>supports-screens</code> specifies the screen dimensions an
          application supports.  By default a modern application supports all
          screen sizes and must explicitly disable certain screen sizes here;
@@ -1506,7 +1505,7 @@
          (HVGA) screen size.  Note that screen size is a separate axis from
          density, and is determined as the available pixels to an application
          after density scaling has been applied.
-         
+
          <p>This appears as a child tag of the
          {@link #AndroidManifest manifest} tag. -->
     <declare-styleable name="AndroidManifestSupportsScreens" parent="AndroidManifest">
@@ -1609,7 +1608,7 @@
          {@link android.content.ContentProvider} class that is available
          as part of the package's application components, supplying structured
          access to data managed by the application.
-         
+
          <p>This appears as a child tag of the
          {@link #AndroidManifestApplication application} tag. -->
     <declare-styleable name="AndroidManifestProvider" parent="AndroidManifestApplication">
@@ -1641,7 +1640,7 @@
         <attr name="exported" />
         <attr name="singleUser" />
     </declare-styleable>
-    
+
     <!-- Attributes that can be supplied in an AndroidManifest.xml
          <code>grant-uri-permission</code> tag, a child of the
          {@link #AndroidManifestProvider provider} tag, describing a specific
@@ -1658,7 +1657,7 @@
         <attr name="pathPrefix" format="string" />
         <!-- Specify a URI path that matches a simple pattern, as per
              {@link android.os.PatternMatcher} with
-             {@link android.os.PatternMatcher#PATTERN_SIMPLE_GLOB}. 
+             {@link android.os.PatternMatcher#PATTERN_SIMPLE_GLOB}.
              Note that because '\' is used as an escape character when
              reading the string from XML (before it is parsed as a pattern),
              you will need to double-escape: for example a literal "*" would
@@ -1667,7 +1666,7 @@
              write if constructing the string in Java code. -->
         <attr name="pathPattern" format="string" />
     </declare-styleable>
-    
+
     <!-- Attributes that can be supplied in an AndroidManifest.xml
          <code>path-permission</code> tag, a child of the
          {@link #AndroidManifestProvider provider} tag, describing a permission
@@ -1681,13 +1680,13 @@
         <attr name="readPermission" />
         <attr name="writePermission" />
     </declare-styleable>
-    
+
     <!-- The <code>service</code> tag declares a
          {@link android.app.Service} class that is available
          as part of the package's application components, implementing
          long-running background operations or a rich communication API
          that can be called by other packages.
-         
+
          <p>Zero or more {@link #AndroidManifestIntentFilter intent-filter}
          tags can be included inside of a service, to specify the Intents
          that can connect with it.  If none are specified, the service can
@@ -1724,13 +1723,13 @@
         <attr name="isolatedProcess" format="boolean" />
         <attr name="singleUser" />
     </declare-styleable>
-    
+
     <!-- The <code>receiver</code> tag declares an
          {@link android.content.BroadcastReceiver} class that is available
          as part of the package's application components, allowing the
          application to receive actions or data broadcast by other
          applications even if it is not currently running.
-         
+
          <p>Zero or more {@link #AndroidManifestIntentFilter intent-filter}
          tags can be included inside of a receiver, to specify the Intents
          it will receive.  If none are specified, the receiver will only
@@ -1826,7 +1825,6 @@
         <attr name="autoRemoveFromRecents" />
         <attr name="relinquishTaskIdentity" />
         <attr name="resumeWhilePausing" />
-        <!-- @hide -->
         <attr name="resizeableActivity" />
         <attr name="lockTaskMode" />
         <attr name="showForAllUsers" />
@@ -1878,7 +1876,7 @@
          data by the system.  You may supply the data through either the
          <code>value</code> or <code>resource</code> attribute; if both
          are given, then <code>resource</code> will be used.
-         
+
          <p>It is highly recommended that you avoid supplying related data as
          multiple separate meta-data entries.  Instead, if you have complex
          data to associate with a component, then use the <code>resource</code>
@@ -1907,22 +1905,22 @@
              Bundle through {@link android.os.Bundle#getInt Bundle.getInt}. -->
         <attr name="resource" format="reference" />
     </declare-styleable>
-    
+
     <!-- The <code>intent-filter</code> tag is used to construct an
          {@link android.content.IntentFilter} object that will be used
          to determine which component can handle a particular
          {@link android.content.Intent} that has been given to the system.
          It can be used as a child of the
          {@link #AndroidManifestActivity activity},
-         {@link #AndroidManifestReceiver receiver} and 
+         {@link #AndroidManifestReceiver receiver} and
          {@link #AndroidManifestService service}
          tags.
-         
+
          <p> Zero or more {@link #AndroidManifestAction action},
          {@link #AndroidManifestCategory category}, and/or
          {@link #AndroidManifestData data} tags should be
          included inside to describe the contents of the filter.
-         
+
          <p> The optional label and icon attributes here are used with
          an activity to supply an alternative description of that activity
          when it is being started through an Intent matching this filter. -->
@@ -1935,7 +1933,7 @@
         <attr name="priority" />
         <attr name="autoVerify" />
     </declare-styleable>
-    
+
     <!-- Attributes that can be supplied in an AndroidManifest.xml
          <code>action</code> tag, a child of the
          {@link #AndroidManifestIntentFilter intent-filter} tag.
@@ -1950,7 +1948,7 @@
              package name. -->
         <attr name="name" />
     </declare-styleable>
-    
+
     <!-- Attributes that can be supplied in an AndroidManifest.xml
          <code>data</code> tag, a child of the
          {@link #AndroidManifestIntentFilter intent-filter} tag, describing
@@ -2022,7 +2020,7 @@
         <!-- Specify a URI path that matches a simple pattern, as per
              {@link android.content.IntentFilter#addDataPath
              IntentFilter.addDataPath()} with
-             {@link android.os.PatternMatcher#PATTERN_SIMPLE_GLOB}. 
+             {@link android.os.PatternMatcher#PATTERN_SIMPLE_GLOB}.
              Note that because '\' is used as an escape character when
              reading the string from XML (before it is parsed as a pattern),
              you will need to double-escape: for example a literal "*" would
@@ -2031,10 +2029,10 @@
              write if constructing the string in Java code. -->
         <attr name="pathPattern" />
     </declare-styleable>
-    
+
     <!-- Attributes that can be supplied in an AndroidManifest.xml
          <code>category</code> tag, a child of the
-         {@link #AndroidManifestIntentFilter intent-filter} tag. 
+         {@link #AndroidManifestIntentFilter intent-filter} tag.
          See {@link android.content.IntentFilter#addCategory} for
          more information. -->
     <declare-styleable name="AndroidManifestCategory" parent="AndroidManifestIntentFilter">
@@ -2046,7 +2044,7 @@
              package name. -->
         <attr name="name" />
     </declare-styleable>
-    
+
     <!-- Attributes that can be supplied in an AndroidManifest.xml
          <code>instrumentation</code> tag, a child of the root
          {@link #AndroidManifest manifest} tag. -->
@@ -2065,7 +2063,7 @@
         <attr name="handleProfiling" />
         <attr name="functionalTest" />
     </declare-styleable>
-    
+
     <!-- Attributes that can be supplied in an AndroidManifest.xml
          <code>screen</code> tag, a child of <code>compatible-screens</code>,
          which is itself a child of the root
@@ -2162,14 +2160,14 @@
             {@link android.content.Intent#setComponent Intent.setComponent()}. -->
         <attr name="targetClass" format="string" />
     </declare-styleable>
-    
+
     <!-- A category to add to an Intent, as per
             {@link android.content.Intent#addCategory Intent.addCategory()}. -->
     <declare-styleable name="IntentCategory" parent="Intent">
         <!-- Required name of the category. -->
         <attr name="name" />
     </declare-styleable>
-    
+
     <!-- An extra data value to place into a an extra/name value pair held
             in a Bundle, as per {@link android.os.Bundle}. -->
     <declare-styleable name="Extra" parent="Intent">
diff --git a/core/res/res/values/config.xml b/core/res/res/values/config.xml
index ef4e261..e43a8d3 100755
--- a/core/res/res/values/config.xml
+++ b/core/res/res/values/config.xml
@@ -170,6 +170,21 @@
          so that applications can still use their own mechanisms. -->
     <bool name="config_enableAutoPowerModes">false</bool>
 
+    <!-- The threshold angle for any motion detection in auto-power save modes.
+         In hundreths of a degree. -->
+    <integer name="config_autoPowerModeThresholdAngle">200</integer>
+
+    <!-- The sensor id of an "any motion" sensor used in auto-power save modes.
+         0 indicates this sensor is not available. -->
+    <integer name="config_autoPowerModeAnyMotionSensor">0</integer>
+
+    <!-- If an any motion sensor is not available, prefer the wrist tilt detector over the
+         SMD. -->
+    <bool name="config_autoPowerModePreferWristTilt">false</bool>
+
+    <!-- If a location should be pre-fetched when going into device idle. -->
+    <bool name="config_autoPowerModePrefetchLocation">true</bool>
+
     <!-- The duration (in milliseconds) that the radio will scan for a signal
          when there's no network connection. If the scan doesn't timeout, use zero -->
     <integer name="config_radioScanningTimeout">0</integer>
@@ -2283,4 +2298,8 @@
     <!-- The OEM specified sensor string type for the gesture to launch camera app, this value
          must match the value of config_cameraLaunchGestureSensorType in OEM's HAL -->
     <string translatable="false" name="config_cameraLaunchGestureSensorStringType"></string>
+
+    <!-- Whether to open UI submenus side by side with the top menu (as opposed to
+         replacing the top menu). -->
+    <bool name="config_enableCascadingSubmenus">false</bool>
 </resources>
diff --git a/core/res/res/values/public.xml b/core/res/res/values/public.xml
index d2089cd..a2b6a7b 100644
--- a/core/res/res/values/public.xml
+++ b/core/res/res/values/public.xml
@@ -2660,6 +2660,9 @@
        =============================================================== -->
     <eat-comment />
 
+    <public type="attr" name="listMenuViewStyle" />
+    <public type="attr" name="subMenuArrow" />
+
     <public type="style" name="Theme.Material.DayNight" />
     <public type="style" name="Theme.Material.DayNight.DarkActionBar" />
     <public type="style" name="Theme.Material.DayNight.Dialog" />
@@ -2681,5 +2684,6 @@
     <public type="id" name="accessibilityActionSetProgress" />
     <public type="attr" name="activityWidth" />
     <public type="attr" name="activityHeight" />
+    <public type="attr" name="resizeableActivity" />
 
 </resources>
diff --git a/core/res/res/values/styles_material.xml b/core/res/res/values/styles_material.xml
index 2369c9b..5dc14e3 100644
--- a/core/res/res/values/styles_material.xml
+++ b/core/res/res/values/styles_material.xml
@@ -685,6 +685,10 @@
 
     <style name="Widget.Material.ListView.White"/>
 
+    <style name="Widget.Material.ListMenuView">
+        <item name="subMenuArrow">@drawable/ic_arrow_drop_right_black_24dp</item>
+    </style>
+
     <style name="Widget.Material.PopupWindow" parent="Widget.PopupWindow"/>
 
     <style name="Widget.Material.PopupWindow.ActionMode">
diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml
index e04c743..48a7a1c5 100755
--- a/core/res/res/values/symbols.xml
+++ b/core/res/res/values/symbols.xml
@@ -158,6 +158,7 @@
   <java-symbol type="id" name="shortcut" />
   <java-symbol type="id" name="skip_button" />
   <java-symbol type="id" name="split_action_bar" />
+  <java-symbol type="id" name="submenuarrow" />
   <java-symbol type="id" name="submit_area" />
   <java-symbol type="id" name="switch_new" />
   <java-symbol type="id" name="switch_old" />
@@ -253,6 +254,10 @@
   <java-symbol type="bool" name="config_cellBroadcastAppLinks" />
   <java-symbol type="bool" name="config_duplicate_port_omadm_wappush" />
   <java-symbol type="bool" name="config_enableAutoPowerModes" />
+  <java-symbol type="integer" name="config_autoPowerModeThresholdAngle" />
+  <java-symbol type="integer" name="config_autoPowerModeAnyMotionSensor" />
+  <java-symbol type="bool" name="config_autoPowerModePreferWristTilt" />
+  <java-symbol type="bool" name="config_autoPowerModePrefetchLocation" />
   <java-symbol type="bool" name="config_enable_emergency_call_while_sim_locked" />
   <java-symbol type="bool" name="config_enable_puk_unlock_screen" />
   <java-symbol type="bool" name="config_enableBurnInProtection" />
@@ -2185,6 +2190,8 @@
   <java-symbol type="string" name="prohibit_manual_network_selection_in_gobal_mode" />
   <java-symbol type="id" name="profile_button" />
 
+  <java-symbol type="bool" name="config_enableCascadingSubmenus" />
+
   <!-- From SignalStrength -->
   <java-symbol type="integer" name="config_LTE_RSRP_threshold_type" />
 
diff --git a/core/res/res/values/themes_material.xml b/core/res/res/values/themes_material.xml
index e406ae0..3010190 100644
--- a/core/res/res/values/themes_material.xml
+++ b/core/res/res/values/themes_material.xml
@@ -238,6 +238,7 @@
         <item name="gridViewStyle">@style/Widget.Material.GridView</item>
         <item name="imageButtonStyle">@style/Widget.Material.ImageButton</item>
         <item name="imageWellStyle">@style/Widget.Material.ImageWell</item>
+        <item name="listMenuViewStyle">@style/Widget.Material.ListMenuView</item>
         <item name="listViewStyle">@style/Widget.Material.ListView</item>
         <item name="listViewWhiteStyle">@style/Widget.Material.ListView.White</item>
         <item name="popupWindowStyle">@style/Widget.Material.PopupWindow</item>
@@ -594,6 +595,7 @@
         <item name="gridViewStyle">@style/Widget.Material.Light.GridView</item>
         <item name="imageButtonStyle">@style/Widget.Material.Light.ImageButton</item>
         <item name="imageWellStyle">@style/Widget.Material.Light.ImageWell</item>
+        <item name="listMenuViewStyle">@style/Widget.Material.ListMenuView</item>
         <item name="listViewStyle">@style/Widget.Material.Light.ListView</item>
         <item name="listViewWhiteStyle">@style/Widget.Material.Light.ListView.White</item>
         <item name="popupWindowStyle">@style/Widget.Material.Light.PopupWindow</item>
diff --git a/core/tests/bluetoothtests/AndroidManifest.xml b/core/tests/bluetoothtests/AndroidManifest.xml
index e43ba12..7f9d874 100644
--- a/core/tests/bluetoothtests/AndroidManifest.xml
+++ b/core/tests/bluetoothtests/AndroidManifest.xml
@@ -21,6 +21,7 @@
     <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
     <uses-permission android:name="android.permission.BROADCAST_STICKY" />
     <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
+    <uses-permission android:name="android.permission.LOCAL_MAC_ADDRESS" />
     <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
     <uses-permission android:name="android.permission.WRITE_SETTINGS" />
diff --git a/core/tests/coretests/src/android/animation/AutoCancelTest.java b/core/tests/coretests/src/android/animation/AutoCancelTest.java
index b1f88db..5810818 100644
--- a/core/tests/coretests/src/android/animation/AutoCancelTest.java
+++ b/core/tests/coretests/src/android/animation/AutoCancelTest.java
@@ -18,10 +18,12 @@
 import android.os.Handler;
 import android.test.ActivityInstrumentationTestCase2;
 import android.test.suitebuilder.annotation.SmallTest;
+import android.test.suitebuilder.annotation.Suppress;
 
 import java.util.HashMap;
 import java.util.concurrent.TimeUnit;
 
+@Suppress  // Failing
 public class AutoCancelTest extends ActivityInstrumentationTestCase2<BasicAnimatorActivity> {
 
     boolean mAnimX1Canceled = false;
diff --git a/core/tests/coretests/src/android/provider/SettingsProviderTest.java b/core/tests/coretests/src/android/provider/SettingsProviderTest.java
index 2615a28..e6d3158 100644
--- a/core/tests/coretests/src/android/provider/SettingsProviderTest.java
+++ b/core/tests/coretests/src/android/provider/SettingsProviderTest.java
@@ -28,7 +28,6 @@
 import android.net.Uri;
 import android.os.UserHandle;
 import android.os.UserManager;
-import android.provider.Settings;
 import android.test.AndroidTestCase;
 import android.test.suitebuilder.annotation.MediumTest;
 import android.test.suitebuilder.annotation.SmallTest;
@@ -37,7 +36,6 @@
 import java.util.List;
 
 /** Unit test for SettingsProvider. */
-@Suppress  // Failing.
 public class SettingsProviderTest extends AndroidTestCase {
     @MediumTest
     public void testNameValueCache() {
@@ -53,84 +51,108 @@
         assertEquals(1, r.delete(Settings.Secure.getUriFor("test_service"), null, null));
         assertEquals(null, Settings.Secure.getString(r, "test_service"));
 
-        // Try all the same things in the System table
-        Settings.System.putString(r, "test_setting", "Value");
-        assertEquals("Value", Settings.System.getString(r, "test_setting"));
-
-        Settings.System.putString(r, "test_setting", "New");
-        assertEquals("New", Settings.System.getString(r, "test_setting"));
-
-        assertEquals(1, r.delete(Settings.System.getUriFor("test_setting"), null, null));
-        assertEquals(null, Settings.System.getString(r, "test_setting"));
+        // Apps should not be able to use System settings.
+        try {
+            Settings.System.putString(r, "test_setting", "Value");
+            fail("IllegalArgumentException expected");
+        } catch (java.lang.IllegalArgumentException e) {
+            // expected
+        }
     }
 
     @MediumTest
-    public void testRowNameContentUri() {
+    public void testRowNameContentUriForSecure() {
+        final String testKey = "testRowNameContentUriForSecure";
+        final String testValue = "testValue";
+        final String secondTestValue = "testValueNew";
+
+        try {
+            testRowNameContentUri(Settings.Secure.CONTENT_URI, Settings.Secure.NAME,
+                    Settings.Secure.VALUE, testKey, testValue, secondTestValue);
+        } finally {
+            // clean up
+            Settings.Secure.putString(getContext().getContentResolver(), testKey, null);
+        }
+    }
+
+    @MediumTest
+    public void testRowNameContentUriForSystem() {
+        final String testKey = Settings.System.VIBRATE_ON;
+        assertTrue("Settings.System.PUBLIC_SETTINGS cannot be empty.  We need to use one of it"
+                + " for testing.  Only settings key in this collection will be accepted by the"
+                + " framework.", Settings.System.PUBLIC_SETTINGS.contains(testKey));
+        final String testValue = "0";
+        final String secondTestValue = "1";
+        final String oldValue =
+                Settings.System.getString(getContext().getContentResolver(), testKey);
+
+        try {
+            testRowNameContentUri(Settings.System.CONTENT_URI, Settings.System.NAME,
+                    Settings.System.VALUE, testKey, testValue, secondTestValue);
+        } finally {
+            // restore old value
+            if (oldValue != null) {
+                Settings.System.putString(getContext().getContentResolver(), testKey, oldValue);
+            }
+        }
+    }
+
+    private void testRowNameContentUri(Uri table, String nameField, String valueField,
+            String testKey, String testValue, String secondTestValue) {
         ContentResolver r = getContext().getContentResolver();
 
-        assertEquals("content://settings/system/test_setting",
-                Settings.System.getUriFor("test_setting").toString());
-        assertEquals("content://settings/secure/test_service",
-                Settings.Secure.getUriFor("test_service").toString());
+        ContentValues v = new ContentValues();
+        v.put(nameField, testKey);
+        v.put(valueField, testValue);
 
-        // These tables use the row name (not ID) as their content URI.
-        Uri tables[] = { Settings.System.CONTENT_URI, Settings.Secure.CONTENT_URI };
-        for (Uri table : tables) {
-            ContentValues v = new ContentValues();
-            v.put(Settings.System.NAME, "test_key");
-            v.put(Settings.System.VALUE, "Test");
-            Uri uri = r.insert(table, v);
-            assertEquals(table.toString() + "/test_key", uri.toString());
+        r.insert(table, v);
+        Uri uri = Uri.parse(table.toString() + "/" + testKey);
 
-            // Query with a specific URI and no WHERE clause succeeds.
-            Cursor c = r.query(uri, null, null, null, null);
-            try {
-                assertTrue(c.moveToNext());
-                assertEquals("test_key", c.getString(c.getColumnIndex(Settings.System.NAME)));
-                assertEquals("Test", c.getString(c.getColumnIndex(Settings.System.VALUE)));
-                assertFalse(c.moveToNext());
-            } finally {
-                c.close();
-            }
-
-            // Query with a specific URI and a WHERE clause fails.
-            try {
-                r.query(uri, null, "1", null, null);
-                fail("UnsupportedOperationException expected");
-            } catch (UnsupportedOperationException e) {
-                if (!e.toString().contains("WHERE clause")) throw e;
-            }
-
-            // Query with a tablewide URI and a WHERE clause succeeds.
-            c = r.query(table, null, "name='test_key'", null, null);
-            try {
-                assertTrue(c.moveToNext());
-                assertEquals("test_key", c.getString(c.getColumnIndex(Settings.System.NAME)));
-                assertEquals("Test", c.getString(c.getColumnIndex(Settings.System.VALUE)));
-                assertFalse(c.moveToNext());
-            } finally {
-                c.close();
-            }
-
-            v = new ContentValues();
-            v.put(Settings.System.VALUE, "Toast");
-            assertEquals(1, r.update(uri, v, null, null));
-
-            c = r.query(uri, null, null, null, null);
-            try {
-                assertTrue(c.moveToNext());
-                assertEquals("test_key", c.getString(c.getColumnIndex(Settings.System.NAME)));
-                assertEquals("Toast", c.getString(c.getColumnIndex(Settings.System.VALUE)));
-                assertFalse(c.moveToNext());
-            } finally {
-                c.close();
-            }
-
-            assertEquals(1, r.delete(uri, null, null));
+        // Query with a specific URI and no WHERE clause succeeds.
+        Cursor c = r.query(uri, null, null, null, null);
+        try {
+            assertTrue(c.moveToNext());
+            assertEquals(testKey, c.getString(c.getColumnIndex(nameField)));
+            assertEquals(testValue, c.getString(c.getColumnIndex(valueField)));
+            assertFalse(c.moveToNext());
+        } finally {
+            c.close();
         }
 
-        assertEquals(null, Settings.System.getString(r, "test_key"));
-        assertEquals(null, Settings.Secure.getString(r, "test_key"));
+        // Query with a specific URI and a WHERE clause fails.
+        try {
+            r.query(uri, null, "1", null, null);
+            fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+
+        // Query with a tablewide URI and a WHERE clause succeeds.
+        c = r.query(table, null, "name='" + testKey + "'", null, null);
+        try {
+            assertTrue(c.moveToNext());
+            assertEquals(testKey, c.getString(c.getColumnIndex(nameField)));
+            assertEquals(testValue, c.getString(c.getColumnIndex(valueField)));
+            assertFalse(c.moveToNext());
+        } finally {
+            c.close();
+        }
+
+        v = new ContentValues();
+        // NAME is still needed, although the uri should be specific enough. Why?
+        v.put(nameField, testKey);
+        v.put(valueField, secondTestValue);
+        assertEquals(1, r.update(uri, v, null, null));
+
+        c = r.query(uri, null, null, null, null);
+        try {
+            assertTrue(c.moveToNext());
+            assertEquals(testKey, c.getString(c.getColumnIndex(nameField)));
+            assertEquals(secondTestValue, c.getString(c.getColumnIndex(valueField)));
+            assertFalse(c.moveToNext());
+        } finally {
+            c.close();
+        }
     }
 
     @MediumTest
@@ -139,7 +161,7 @@
         ContentResolver r = getContext().getContentResolver();
 
         // Make sure there's an owner
-        assertTrue(findUser(um, UserHandle.USER_OWNER));
+        assertTrue(findUser(um, UserHandle.USER_SYSTEM));
 
         // create a new user to use for testing
         UserInfo otherUser = um.createUser("TestUser1", UserInfo.FLAG_GUEST);
@@ -148,21 +170,17 @@
             assertNotSame("Current calling user id should not be the new guest user",
                     otherUser.id, UserHandle.getCallingUserId());
 
-            Settings.Secure.putString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "gps");
-            Settings.Secure.putStringForUser(r,
-                    Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "network", otherUser.id);
+            final String testKey = "testSettingsChangeForOtherUser";
+            final String testValue1 = "value1";
+            final String testValue2 = "value2";
+            Settings.Secure.putString(r, testKey, testValue1);
+            Settings.Secure.putStringForUser(r, testKey, testValue2, otherUser.id);
 
-            assertEquals("gps",
-                    Settings.Secure.getString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED));
-            assertEquals("network", Settings.Secure.getStringForUser(
-                    r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, otherUser.id));
+            assertEquals(testValue1, Settings.Secure.getString(r, testKey));
+            assertEquals(testValue2, Settings.Secure.getStringForUser(r, testKey, otherUser.id));
 
             assertNotSame("Current calling user id should not be the new guest user",
                     otherUser.id, UserHandle.getCallingUserId());
-            Settings.Secure.setLocationProviderEnabledForUser(r, "network", false, otherUser.id);
-            assertEquals("", Settings.Secure.getStringForUser(
-                    r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, otherUser.id));
-
         } finally {
             // Tidy up
             um.removeUser(otherUser.id);
@@ -170,6 +188,7 @@
     }
 
     @MediumTest
+    @Suppress  // Settings.Bookmarks uses a query format that's not supported now.
     public void testRowNumberContentUri() {
         ContentResolver r = getContext().getContentResolver();
 
@@ -196,47 +215,56 @@
     public void testParseProviderList() {
         ContentResolver r = getContext().getContentResolver();
 
-        // Make sure we get out what we put in.
-        Settings.Secure.putString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
-                "test1,test2,test3");
-        assertEquals(Settings.Secure.getString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED),
-                "test1,test2,test3");
-
+        // We only accept "+value" and "-value"
         // Test adding a value
-        Settings.Secure.putString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
-                "");
         Settings.Secure.putString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "+test1");
-        assertEquals("test1",
-                Settings.Secure.getString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED));
+        assertTrue(Settings.Secure.getString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED)
+                .contains("test1"));
 
         // Test adding a second value
         Settings.Secure.putString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "+test2");
-        assertEquals("test1,test2",
-                Settings.Secure.getString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED));
+        assertTrue(Settings.Secure.getString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED)
+                .contains("test1"));
+        assertTrue(Settings.Secure.getString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED)
+                .contains("test2"));
 
         // Test adding a third value
         Settings.Secure.putString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "+test3");
-        assertEquals("test1,test2,test3",
-                Settings.Secure.getString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED));
+        assertTrue(Settings.Secure.getString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED)
+                .contains("test1"));
+        assertTrue(Settings.Secure.getString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED)
+                .contains("test2"));
+        assertTrue(Settings.Secure.getString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED)
+                .contains("test3"));
 
         // Test deleting the first value in a 3 item list
         Settings.Secure.putString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "-test1");
-        assertEquals("test2,test3",
-                Settings.Secure.getString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED));
+        assertFalse(Settings.Secure.getString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED)
+                .contains("test1"));
 
         // Test deleting the middle value in a 3 item list
-        Settings.Secure.putString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
-                "test1,test2,test3");
-        Settings.Secure.putString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "-test2");
-        assertEquals("test1,test3",
-                Settings.Secure.getString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED));
+        Settings.Secure.putString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "+test4");
+        assertTrue(Settings.Secure.getString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED)
+                .contains("test2"));
+        assertTrue(Settings.Secure.getString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED)
+                .contains("test3"));
+        assertTrue(Settings.Secure.getString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED)
+                .contains("test4"));
+        Settings.Secure.putString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "-test3");
+        assertFalse(Settings.Secure.getString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED)
+                .contains("test3"));
 
         // Test deleting the last value in a 3 item list
-        Settings.Secure.putString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
-                "test1,test2,test3");
-        Settings.Secure.putString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "-test3");
-        assertEquals("test1,test2",
-                Settings.Secure.getString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED));
+        Settings.Secure.putString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "+test5");
+        assertTrue(Settings.Secure.getString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED)
+                .contains("test2"));
+        assertTrue(Settings.Secure.getString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED)
+                .contains("test4"));
+        assertTrue(Settings.Secure.getString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED)
+                .contains("test5"));
+        Settings.Secure.putString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "-test5");
+        assertFalse(Settings.Secure.getString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED)
+                .contains("test5"));
      }
 
     private boolean findUser(UserManager um, int userHandle) {
@@ -254,7 +282,7 @@
         ContentResolver r = getContext().getContentResolver();
 
         // Make sure there's an owner
-        assertTrue(findUser(um, UserHandle.USER_OWNER));
+        assertTrue(findUser(um, UserHandle.USER_SYSTEM));
 
         // create a new user to use for testing
         UserInfo user = um.createUser("TestUser1", UserInfo.FLAG_GUEST);
@@ -266,12 +294,12 @@
             final int SELF_VALUE = 40;
             final int OTHER_VALUE = 27;
 
-            Settings.System.putInt(r, TEST_KEY, SELF_VALUE);
-            Settings.System.putIntForUser(r, TEST_KEY, OTHER_VALUE, user.id);
+            Settings.Secure.putInt(r, TEST_KEY, SELF_VALUE);
+            Settings.Secure.putIntForUser(r, TEST_KEY, OTHER_VALUE, user.id);
 
             // Verify that they read back as intended
-            int myValue = Settings.System.getInt(r, TEST_KEY, 0);
-            int otherValue = Settings.System.getIntForUser(r, TEST_KEY, 0, user.id);
+            int myValue = Settings.Secure.getInt(r, TEST_KEY, 0);
+            int otherValue = Settings.Secure.getIntForUser(r, TEST_KEY, 0, user.id);
             assertTrue("Running as user " + UserHandle.myUserId()
                     + " and reading/writing as user " + user.id
                     + ", expected to read " + SELF_VALUE + " but got " + myValue,
@@ -310,7 +338,8 @@
         assertCanBeHandled(new Intent(Settings.ACTION_MEMORY_CARD_SETTINGS));
         assertCanBeHandled(new Intent(Settings.ACTION_NETWORK_OPERATOR_SETTINGS));
         assertCanBeHandled(new Intent(Settings.ACTION_PRIVACY_SETTINGS));
-        assertCanBeHandled(new Intent(Settings.ACTION_QUICK_LAUNCH_SETTINGS));
+        //TODO: seems no one is using this anymore.
+//        assertCanBeHandled(new Intent(Settings.ACTION_QUICK_LAUNCH_SETTINGS));
         assertCanBeHandled(new Intent(Settings.ACTION_SEARCH_SETTINGS));
         assertCanBeHandled(new Intent(Settings.ACTION_SECURITY_SETTINGS));
         assertCanBeHandled(new Intent(Settings.ACTION_SETTINGS));
diff --git a/core/tests/coretests/src/android/view/HandlerActionQueueTest.java b/core/tests/coretests/src/android/view/HandlerActionQueueTest.java
new file mode 100644
index 0000000..fd8f23a
--- /dev/null
+++ b/core/tests/coretests/src/android/view/HandlerActionQueueTest.java
@@ -0,0 +1,80 @@
+/*
+ * 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 android.view;
+
+import android.test.AndroidTestCase;
+import android.test.suitebuilder.annotation.SmallTest;
+
+public class HandlerActionQueueTest extends AndroidTestCase {
+
+    @SmallTest
+    public void testPostAndRemove() {
+        HandlerActionQueue runQueue = new HandlerActionQueue();
+        MockRunnable runnable1 = new MockRunnable();
+        MockRunnable runnable2 = new MockRunnable();
+        MockRunnable runnable3 = new MockRunnable();
+
+        runQueue.post(runnable1);
+        runQueue.post(runnable1);
+        runQueue.post(runnable2);
+        runQueue.postDelayed(runnable1, 100);
+        runQueue.postDelayed(null, 500);
+        assertEquals(5, runQueue.size());
+        assertEquals(0, runQueue.getDelay(0));
+        assertEquals(0, runQueue.getDelay(1));
+        assertEquals(0, runQueue.getDelay(2));
+        assertEquals(100, runQueue.getDelay(3));
+        assertEquals(500, runQueue.getDelay(4));
+        assertEquals(500, runQueue.getDelay(4));
+        assertEquals(runnable1, runQueue.getRunnable(0));
+        assertEquals(runnable1, runQueue.getRunnable(1));
+        assertEquals(runnable2, runQueue.getRunnable(2));
+        assertEquals(runnable1, runQueue.getRunnable(3));
+        assertEquals(null, runQueue.getRunnable(4));
+
+        runQueue.removeCallbacks(runnable1);
+        assertEquals(2, runQueue.size());
+        assertEquals(0, runQueue.getDelay(0));
+        assertEquals(500, runQueue.getDelay(1));
+        assertEquals(runnable2, runQueue.getRunnable(0));
+        assertEquals(null, runQueue.getRunnable(1));
+
+        try {
+            assertNull(runQueue.getRunnable(2));
+            assertFalse(true);
+        } catch (IndexOutOfBoundsException e) {
+            // Should throw an exception.
+        }
+
+        runQueue.removeCallbacks(runnable3);
+        assertEquals(2, runQueue.size());
+
+        runQueue.removeCallbacks(runnable2);
+        assertEquals(1, runQueue.size());
+        assertEquals(null, runQueue.getRunnable(0));
+
+        runQueue.removeCallbacks(null);
+        assertEquals(0, runQueue.size());
+    }
+
+    private static class MockRunnable implements Runnable {
+        @Override
+        public void run() {
+
+        }
+    }
+}
diff --git a/core/tests/coretests/src/android/widget/listview/ListManagedCursorTest.java b/core/tests/coretests/src/android/widget/listview/ListManagedCursorTest.java
index 7938cba..bc3776c 100644
--- a/core/tests/coretests/src/android/widget/listview/ListManagedCursorTest.java
+++ b/core/tests/coretests/src/android/widget/listview/ListManagedCursorTest.java
@@ -17,8 +17,7 @@
 package android.widget.listview;
 
 import android.app.Instrumentation;
-import android.test.ActivityInstrumentationTestCase;
-import android.test.FlakyTest;
+import android.test.ActivityInstrumentationTestCase2;
 import android.test.suitebuilder.annotation.LargeTest;
 import android.test.suitebuilder.annotation.MediumTest;
 import android.view.KeyEvent;
@@ -28,12 +27,12 @@
 /**
  * Tests restoring the scroll position in a list with a managed cursor.
  */
-public class ListManagedCursorTest extends ActivityInstrumentationTestCase<ListManagedCursor> {
+public class ListManagedCursorTest extends ActivityInstrumentationTestCase2<ListManagedCursor> {
     private ListManagedCursor mActivity;
     private ListView mListView;
 
     public ListManagedCursorTest() {
-        super("com.android.frameworks.coretests", ListManagedCursor.class);
+        super(ListManagedCursor.class);
     }
 
     @Override
@@ -48,86 +47,46 @@
     public void testPreconditions() {
         assertNotNull(mActivity);
         assertNotNull(mListView);
-        
+
         assertEquals(0, mListView.getFirstVisiblePosition());
     }
-    
+
     /**
      * Scroll the list using arrows, launch new activity, hit back, make sure we're still scrolled.
      */
     @LargeTest
     public void testKeyScrolling() {
         Instrumentation inst = getInstrumentation();
-        
+
         int firstVisiblePosition = arrowScroll(inst);
-        
+
         inst.sendCharacterSync(KeyEvent.KEYCODE_BACK);
         inst.waitForIdleSync();
-        
-        assertTrue("List changed to touch mode", !mListView.isInTouchMode()); 
-        assertTrue("List did not preserve scroll position", 
-                firstVisiblePosition == mListView.getFirstVisiblePosition()); 
+
+        assertTrue("List changed to touch mode", !mListView.isInTouchMode());
+        assertTrue("List did not preserve scroll position",
+                firstVisiblePosition == mListView.getFirstVisiblePosition());
     }
 
     /**
-     * Scroll the list using touch, launch new activity, hit back, make sure we're still scrolled.
-     */
-    @LargeTest
-    public void testTouchScrolling() {
-        Instrumentation inst = getInstrumentation();
-        
-       int firstVisiblePosition = touchScroll(inst);
-        
-        inst.sendCharacterSync(KeyEvent.KEYCODE_BACK);
-        inst.waitForIdleSync();
-        
-        assertTrue("List not in touch mode", mListView.isInTouchMode()); 
-        assertTrue("List did not preserve scroll position", 
-                firstVisiblePosition == mListView.getFirstVisiblePosition()); 
-    }
-    
-    /**
      * Scroll the list using arrows, launch new activity, change to touch mode, hit back, make sure
      * we're still scrolled.
      */
     @LargeTest
     public void testKeyScrollingToTouchMode() {
         Instrumentation inst = getInstrumentation();
-        
+
         int firstVisiblePosition = arrowScroll(inst);
-        
-        TouchUtils.dragQuarterScreenUp(this);
+
+        TouchUtils.dragQuarterScreenUp(this, getActivity());
         inst.sendCharacterSync(KeyEvent.KEYCODE_BACK);
         inst.waitForIdleSync();
-        
-        assertTrue("List did not change to touch mode", mListView.isInTouchMode()); 
-        assertTrue("List did not preserve scroll position", 
-                firstVisiblePosition == mListView.getFirstVisiblePosition()); 
+
+        assertTrue("List did not change to touch mode", mListView.isInTouchMode());
+        assertTrue("List did not preserve scroll position",
+                firstVisiblePosition == mListView.getFirstVisiblePosition());
     }
 
-
-    /**
-     * Scroll the list using touch, launch new activity, change to trackball mode, hit back, make
-     * sure we're still scrolled.
-     */
-    @FlakyTest(tolerance=3)
-    @LargeTest
-    public void testTouchScrollingToTrackballMode() {
-        Instrumentation inst = getInstrumentation();
-
-        int firstVisiblePosition = touchScroll(inst);
-
-        inst.sendCharacterSync(KeyEvent.KEYCODE_DPAD_DOWN);
-        inst.waitForIdleSync();
-        inst.sendCharacterSync(KeyEvent.KEYCODE_DPAD_DOWN);
-        inst.waitForIdleSync();
-        inst.sendCharacterSync(KeyEvent.KEYCODE_BACK);
-        inst.waitForIdleSync();
-        assertTrue("List not in trackball mode", !mListView.isInTouchMode());
-        assertTrue("List did not preserve scroll position", firstVisiblePosition == mListView
-                .getFirstVisiblePosition());
-    }
-    
     public int arrowScroll(Instrumentation inst) {
         int count = mListView.getChildCount();
 
@@ -151,30 +110,4 @@
 
         return firstVisiblePosition;
     }
-
-    public int touchScroll(Instrumentation inst) {
-        TouchUtils.dragQuarterScreenUp(this);
-        inst.waitForIdleSync();
-        TouchUtils.dragQuarterScreenUp(this);
-        inst.waitForIdleSync();
-        TouchUtils.dragQuarterScreenUp(this);
-        inst.waitForIdleSync();
-        TouchUtils.dragQuarterScreenUp(this);
-        inst.waitForIdleSync();
-
-        int firstVisiblePosition = mListView.getFirstVisiblePosition();
-        assertTrue("Touch scroll did not happen", firstVisiblePosition > 0);
-        assertTrue("List not in touch mode", mListView.isInTouchMode());
-
-        TouchUtils.clickView(this, mListView.getChildAt(mListView.getChildCount() - 1));
-        inst.waitForIdleSync();
-
-        try {
-            Thread.sleep(3000);
-        } catch (InterruptedException e) {
-            e.printStackTrace();
-        }
-
-        return firstVisiblePosition;
-    }
 }
diff --git a/graphics/java/android/graphics/Paint.java b/graphics/java/android/graphics/Paint.java
index c5d68bd..ce35b87 100644
--- a/graphics/java/android/graphics/Paint.java
+++ b/graphics/java/android/graphics/Paint.java
@@ -17,10 +17,13 @@
 package android.graphics;
 
 import android.annotation.ColorInt;
+import android.annotation.NonNull;
+import android.annotation.Size;
 import android.text.GraphicsOperations;
 import android.text.SpannableString;
 import android.text.SpannedString;
 import android.text.TextUtils;
+import android.util.LocaleList;
 
 import java.util.Locale;
 
@@ -50,7 +53,7 @@
     private float       mCompatScaling;
     private float       mInvCompatScaling;
 
-    private Locale      mLocale;
+    private LocaleList  mLocales;
     private String      mFontFeatureSettings;
 
     /**
@@ -434,7 +437,7 @@
         // setHinting(DisplayMetrics.DENSITY_DEVICE >= DisplayMetrics.DENSITY_TV
         //        ? HINTING_OFF : HINTING_ON);
         mCompatScaling = mInvCompatScaling = 1;
-        setTextLocale(Locale.getDefault());
+        setTextLocales(LocaleList.getDefault());
     }
 
     /**
@@ -474,7 +477,7 @@
         mInvCompatScaling = 1;
 
         mBidiFlags = BIDI_DEFAULT_LTR;
-        setTextLocale(Locale.getDefault());
+        setTextLocales(LocaleList.getDefault());
         setElegantTextHeight(false);
         mFontFeatureSettings = null;
     }
@@ -512,7 +515,7 @@
         mInvCompatScaling = paint.mInvCompatScaling;
 
         mBidiFlags = paint.mBidiFlags;
-        mLocale = paint.mLocale;
+        mLocales = paint.mLocales;
         mFontFeatureSettings = paint.mFontFeatureSettings;
     }
 
@@ -1174,47 +1177,80 @@
     }
 
     /**
-     * Get the text Locale.
+     * Get the text's primary Locale. Note that this is not all of the locale-related information
+     * Paint has. Use {@link #getTextLocales()} to get the complete list.
      *
-     * @return the paint's Locale used for drawing text, never null.
+     * @return the paint's primary Locale used for drawing text, never null.
      */
+    @NonNull
     public Locale getTextLocale() {
-        return mLocale;
+        return mLocales.getPrimary();
     }
 
     /**
-     * Set the text locale.
+     * Get the text locale list.
      *
-     * The text locale affects how the text is drawn for some languages.
+     * @return the paint's LocaleList used for drawing text, never null or empty.
+     */
+    @NonNull @Size(min=1)
+    public LocaleList getTextLocales() {
+        return mLocales;
+    }
+
+    /**
+     * Set the text locale list to a one-member list consisting of just the locale.
      *
-     * For example, if the locale is {@link Locale#CHINESE} or {@link Locale#CHINA},
+     * See {@link #setTextLocales(LocaleList)} for how the locale list affects
+     * the way the text is drawn for some languages.
+     *
+     * @param locale the paint's locale value for drawing text, must not be null.
+     */
+    public void setTextLocale(@NonNull Locale locale) {
+        if (locale == null) {
+            throw new IllegalArgumentException("locale cannot be null");
+        }
+        if (mLocales != null && mLocales.size() == 1 && locale.equals(mLocales.getPrimary())) {
+            return;
+        }
+        mLocales = new LocaleList(locale);
+        native_setTextLocale(mNativePaint, locale.toString());
+    }
+
+    /**
+     * Set the text locale list.
+     *
+     * The text locale list affects how the text is drawn for some languages.
+     *
+     * For example, if the locale list contains {@link Locale#CHINESE} or {@link Locale#CHINA},
      * then the text renderer will prefer to draw text using a Chinese font. Likewise,
-     * if the locale is {@link Locale#JAPANESE} or {@link Locale#JAPAN}, then the text
-     * renderer will prefer to draw text using a Japanese font.
+     * if the locale list contains {@link Locale#JAPANESE} or {@link Locale#JAPAN}, then the text
+     * renderer will prefer to draw text using a Japanese font. If the locale list contains both,
+     * the order those locales appear in the list is considered for deciding the font.
      *
      * This distinction is important because Chinese and Japanese text both use many
      * of the same Unicode code points but their appearance is subtly different for
      * each language.
      *
-     * By default, the text locale is initialized to the system locale (as returned
-     * by {@link Locale#getDefault}). This assumes that the text to be rendered will
-     * most likely be in the user's preferred language.
+     * By default, the text locale list is initialized to a one-member list just containing the
+     * system locale (as returned by {@link LocaleList#getDefault()}). This assumes that the text to
+     * be rendered will most likely be in the user's preferred language.
      *
-     * If the actual language of the text is known, then it can be provided to the
-     * text renderer using this method. The text renderer may attempt to guess the
+     * If the actual language or languages of the text is/are known, then they can be provided to
+     * the text renderer using this method. The text renderer may attempt to guess the
      * language script based on the contents of the text to be drawn independent of
-     * the text locale here. Specifying the text locale just helps it do a better
-     * job in certain ambiguous cases
+     * the text locale here. Specifying the text locales just helps it do a better
+     * job in certain ambiguous cases.
      *
-     * @param locale the paint's locale value for drawing text, must not be null.
+     * @param locales the paint's locale list for drawing text, must not be null or empty.
      */
-    public void setTextLocale(Locale locale) {
-        if (locale == null) {
-            throw new IllegalArgumentException("locale cannot be null");
+    public void setTextLocales(@NonNull @Size(min=1) LocaleList locales) {
+        if (locales == null || locales.isEmpty()) {
+            throw new IllegalArgumentException("locales cannot be null or empty");
         }
-        if (locale.equals(mLocale)) return;
-        mLocale = locale;
-        native_setTextLocale(mNativePaint, locale.toString());
+        if (locales.equals(mLocales)) return;
+        mLocales = locales;
+        // TODO: Pass the whole LocaleList to native code
+        native_setTextLocale(mNativePaint, locales.getPrimary().toString());
     }
 
     /**
diff --git a/graphics/java/android/graphics/drawable/AnimatedVectorDrawable.java b/graphics/java/android/graphics/drawable/AnimatedVectorDrawable.java
index 1857345..abb51db 100644
--- a/graphics/java/android/graphics/drawable/AnimatedVectorDrawable.java
+++ b/graphics/java/android/graphics/drawable/AnimatedVectorDrawable.java
@@ -217,7 +217,7 @@
     }
 
     @Override
-    protected boolean onLevelChange(int level) {
+    protected boolean onLevelChange(float level) {
         return mAnimatedVectorState.mVectorDrawable.setLevel(level);
     }
 
diff --git a/graphics/java/android/graphics/drawable/AnimationDrawable.java b/graphics/java/android/graphics/drawable/AnimationDrawable.java
index e1975c9..521c74b 100644
--- a/graphics/java/android/graphics/drawable/AnimationDrawable.java
+++ b/graphics/java/android/graphics/drawable/AnimationDrawable.java
@@ -105,10 +105,12 @@
     /**
      * Sets whether this AnimationDrawable is visible.
      * <p>
-     * When the drawable becomes invisible, it will pause its animation. A
-     * subsequent change to visible with <code>restart</code> set to true will
-     * restart the animation from the first frame. If <code>restart</code> is
-     * false, the animation will resume from the most recent frame.
+     * When the drawable becomes invisible, it will pause its animation. A subsequent change to
+     * visible with <code>restart</code> set to true will restart the animation from the
+     * first frame. If <code>restart</code> is false, the drawable will resume from the most recent
+     * frame. If the drawable has already reached the last frame, it will then loop back to the
+     * first frame, unless it's a one shot drawable (set through {@link #setOneShot(boolean)}),
+     * in which case, it will stay on the last frame.
      *
      * @param visible true if visible, false otherwise
      * @param restart when visible, true to force the animation to restart
@@ -120,7 +122,7 @@
         final boolean changed = super.setVisible(visible, restart);
         if (visible) {
             if (restart || changed) {
-                boolean startFromZero = restart || !mRunning ||
+                boolean startFromZero = restart || (!mRunning && !mAnimationState.mOneShot) ||
                         mCurFrame >= mAnimationState.getChildCount();
                 setFrame(startFromZero ? 0 : mCurFrame, true, mAnimating);
             }
@@ -131,7 +133,7 @@
     }
 
     /**
-     * Starts the animation, looping if necessary. This method has no effect
+     * Starts the animation from the first frame, looping if necessary. This method has no effect
      * if the animation is running.
      * <p>
      * <strong>Note:</strong> Do not call this in the
@@ -158,7 +160,7 @@
     }
 
     /**
-     * Stops the animation. This method has no effect if the animation is not
+     * Stops the animation at the current frame. This method has no effect if the animation is not
      * running.
      *
      * @see #isRunning()
@@ -169,6 +171,7 @@
         mAnimating = false;
 
         if (isRunning()) {
+            mCurFrame = 0;
             unscheduleSelf(this);
         }
     }
@@ -196,7 +199,6 @@
 
     @Override
     public void unscheduleSelf(Runnable what) {
-        mCurFrame = 0;
         mRunning = false;
         super.unscheduleSelf(what);
     }
diff --git a/graphics/java/android/graphics/drawable/ClipDrawable.java b/graphics/java/android/graphics/drawable/ClipDrawable.java
index 31fccd0..3b92507 100644
--- a/graphics/java/android/graphics/drawable/ClipDrawable.java
+++ b/graphics/java/android/graphics/drawable/ClipDrawable.java
@@ -52,8 +52,6 @@
     public static final int HORIZONTAL = 1;
     public static final int VERTICAL = 2;
 
-    private static final int MAX_LEVEL = 10000;
-
     private final Rect mTmpRect = new Rect();
 
     private ClipState mState;
@@ -143,7 +141,7 @@
     }
 
     @Override
-    protected boolean onLevelChange(int level) {
+    protected boolean onLevelChange(float level) {
         super.onLevelChange(level);
         invalidateSelf();
         return true;
@@ -153,12 +151,12 @@
     public int getOpacity() {
         final Drawable dr = getDrawable();
         final int opacity = dr.getOpacity();
-        if (opacity == PixelFormat.TRANSPARENT || dr.getLevel() == 0) {
+        if (opacity == PixelFormat.TRANSPARENT || dr.getLevelFloat() == 0) {
             return PixelFormat.TRANSPARENT;
         }
 
-        final int level = getLevel();
-        if (level >= MAX_LEVEL) {
+        final float level = getLevelFloat();
+        if (level >= MAX_LEVEL_FLOAT) {
             return dr.getOpacity();
         }
 
@@ -169,24 +167,24 @@
     @Override
     public void draw(Canvas canvas) {
         final Drawable dr = getDrawable();
-        if (dr.getLevel() == 0) {
+        if (dr.getLevelFloat() == 0) {
             return;
         }
 
         final Rect r = mTmpRect;
         final Rect bounds = getBounds();
-        final int level = getLevel();
+        final float level = getLevelFloat();
 
         int w = bounds.width();
-        final int iw = 0; //mState.mDrawable.getIntrinsicWidth();
+        final int iw = 0;
         if ((mState.mOrientation & HORIZONTAL) != 0) {
-            w -= (w - iw) * (MAX_LEVEL - level) / MAX_LEVEL;
+            w -= Math.round((w - iw) * (MAX_LEVEL_FLOAT - level) / MAX_LEVEL_FLOAT);
         }
 
         int h = bounds.height();
-        final int ih = 0; //mState.mDrawable.getIntrinsicHeight();
+        final int ih = 0;
         if ((mState.mOrientation & VERTICAL) != 0) {
-            h -= (h - ih) * (MAX_LEVEL - level) / MAX_LEVEL;
+            h -= Math.round((h - ih) * (MAX_LEVEL_FLOAT - level) / MAX_LEVEL_FLOAT);
         }
 
         final int layoutDirection = getLayoutDirection();
diff --git a/graphics/java/android/graphics/drawable/Drawable.java b/graphics/java/android/graphics/drawable/Drawable.java
index b95c183..fb77155 100644
--- a/graphics/java/android/graphics/drawable/Drawable.java
+++ b/graphics/java/android/graphics/drawable/Drawable.java
@@ -41,6 +41,7 @@
 import android.os.Trace;
 import android.util.AttributeSet;
 import android.util.DisplayMetrics;
+import android.util.FloatProperty;
 import android.util.StateSet;
 import android.util.TypedValue;
 import android.util.Xml;
@@ -126,12 +127,19 @@
  * document.</p></div>
  */
 public abstract class Drawable {
+
     private static final Rect ZERO_BOUNDS_RECT = new Rect();
 
     static final PorterDuff.Mode DEFAULT_TINT_MODE = PorterDuff.Mode.SRC_IN;
 
+    /** The standard maximum value for calls to {@link #setLevel(int)}. */
+    public static final int MAX_LEVEL = 10000;
+
+    /** The standard maximum value for calls to {@link #setLevel(float)}. */
+    public static final float MAX_LEVEL_FLOAT = 10000.0f;
+
     private int[] mStateSet = StateSet.WILD_CARD;
-    private int mLevel = 0;
+    private float mLevel = 0.0f;
     private int mChangingConfigurations = 0;
     private Rect mBounds = ZERO_BOUNDS_RECT;  // lazily becomes a new Rect()
     private WeakReference<Callback> mCallback = null;
@@ -711,22 +719,63 @@
     }
 
     /**
-     * Specify the level for the drawable.  This allows a drawable to vary its
-     * imagery based on a continuous controller, for example to show progress
-     * or volume level.
+     * Sets the level for the drawable as an integer value where typically the
+     * minimum level is 0 and the maximum is 10000 {@link #MAX_LEVEL}; however,
+     * the range may vary based on the Drawable implementation and is not
+     * clamped.
+     * <p>
+     * This allows a drawable to vary its imagery based on a continuous
+     * controller. For example, it may be used to show progress or volume
+     * level.
+     * <p>
+     * Use #setLevelFloat(float) to set the level as a high-precision
+     * floating-point value.
      *
-     * <p>If the new level you are supplying causes the appearance of the
-     * Drawable to change, then it is responsible for calling
-     * {@link #invalidateSelf} in order to have itself redrawn, <em>and</em>
-     * true will be returned from this function.
-     *
-     * @param level The new level, from 0 (minimum) to 10000 (maximum).
-     *
-     * @return Returns true if this change in level has caused the appearance
-     * of the Drawable to change (hence requiring an invalidate), otherwise
-     * returns false.
+     * @param level the new level, typically between 0 and 10000
+     * @return {@code true} if this change in level has caused the appearance
+     *         of the drawable to change and will require a subsequent call to
+     *         invalidate, {@code false} otherwise
+     * @see #getLevel()
+     * @see #setLevel(float)
+     * @see #onLevelChange(int)
      */
     public final boolean setLevel(int level) {
+        return setLevel((float) level);
+    }
+
+    /**
+     * Returns the current level as a rounded integer value.
+     * <p>
+     * Use #getLevelFloat() to return the level as a high-precision
+     * floating-point value.
+     *
+     * @return the current level, typically between 0 and 10000
+     * @see #setLevel(int)
+     * @see #getLevelFloat()
+     */
+    public final int getLevel() {
+        return Math.round(mLevel);
+    }
+
+    /**
+     * Sets the level for the drawable as a floating-point value where
+     * typically the minimum level is 0.0 and the maximum is 10000.0
+     * {@link #MAX_LEVEL_FLOAT}; however, the range may vary based on the
+     * Drawable implementation and is not clamped.
+     * <p>
+     * This allows a drawable to vary its imagery based on a continuous
+     * controller. For example, it may be used to show progress or volume
+     * level.
+     *
+     * @param level the new level, typically between 0.0 and 10000.0
+     *              ({@link #MAX_LEVEL_FLOAT})
+     * @return {@code true} if this change in level has caused the appearance
+     *         of the drawable to change and will require a subsequent call to
+     *         invalidate, {@code false} otherwise
+     * @see #getLevelFloat()
+     * @see #onLevelChange(float)
+     */
+    public final boolean setLevel(float level) {
         if (mLevel != level) {
             mLevel = level;
             return onLevelChange(level);
@@ -735,11 +784,13 @@
     }
 
     /**
-     * Retrieve the current level.
+     * Returns the current level as a floating-point value.
      *
-     * @return int Current level, from 0 (minimum) to 10000 (maximum).
+     * @return the current level, typically between 0.0 and 10000.0
+     *         ({@link #MAX_LEVEL_FLOAT})
+     * @see #setLevel(float)
      */
-    public final int getLevel() {
+    public final float getLevelFloat() {
         return mLevel;
     }
 
@@ -894,14 +945,47 @@
      * last state.
      */
     protected boolean onStateChange(int[] state) { return false; }
-    /** Override this in your subclass to change appearance if you vary based
-     *  on level.
-     * @return Returns true if the level change has caused the appearance of
-     * the Drawable to change (that is, it needs to be drawn), else false
-     * if it looks the same and there is no need to redraw it since its
-     * last level.
+
+    /**
+     * Called when the drawable level changes.
+     * <p>
+     * Override this in your subclass to change appearance if you vary based on
+     * level and do not need floating-point accuracy. To handle changes with
+     * higher accuracy, override {@link #onLevelChange(float)} instead.
+     * <p>
+     * <strong>Note:</strong> Do not override both this method and
+     * {@link #onLevelChange(float)}. Only override one method.
+     *
+     * @param level the level as an integer value, typically between 0
+     *              (minimum) and 10000 ({@link #MAX_LEVEL})
+     * @return {@code true} if the level change has caused the appearance of
+     *         the drawable to change such that it needs to be redrawn, or
+     *         {@code false} if there is no need to redraw
      */
     protected boolean onLevelChange(int level) { return false; }
+
+    /**
+     * Called when the drawable level changes.
+     * <p>
+     * Override this in your subclass to change appearance if you vary based on
+     * level and need floating-point accuracy.
+     * <p>
+     * <strong>Note:</strong> Do not override both this method and
+     * {@link #onLevelChange(int)}. Only override one method. If your app
+     * targets SDK <= 23 ({@link android.os.Build.VERSION_CODES#M M}), you
+     * will need to override {@link #onLevelChange(int)} to receive callbacks
+     * on devices running Android M and below.
+     *
+     * @param level the level as a floating-point value, typically between 0.0
+     *              and 10000.0 ({@link #MAX_LEVEL_FLOAT})
+     * @return {@code true} if the level change has caused the appearance of
+     *         the drawable to change such that it needs to be redrawn, or
+     *         {@code false} if there is no need to redraw
+     */
+    protected boolean onLevelChange(float level) {
+        return onLevelChange(Math.round(level));
+    }
+
     /**
      * Override this in your subclass to change appearance if you vary based on
      * the bounds.
@@ -1328,6 +1412,23 @@
     }
 
     /**
+     * Animatable property for Drawable level.
+     *
+     * @hide Until Drawable animations have been cleaned up.
+     */
+    public static final FloatProperty<Drawable> LEVEL = new FloatProperty<Drawable>("levelFloat") {
+        @Override
+        public Float get(Drawable object) {
+            return object.getLevelFloat();
+        }
+
+        @Override
+        public void setValue(Drawable object, float value) {
+            object.setLevel(value);
+        }
+    };
+
+    /**
      * Obtains styled attributes from the theme, if available, or unstyled
      * resources if the theme is null.
      */
diff --git a/graphics/java/android/graphics/drawable/DrawableContainer.java b/graphics/java/android/graphics/drawable/DrawableContainer.java
index 1915dd7..0210ddb 100644
--- a/graphics/java/android/graphics/drawable/DrawableContainer.java
+++ b/graphics/java/android/graphics/drawable/DrawableContainer.java
@@ -322,7 +322,7 @@
     }
 
     @Override
-    protected boolean onLevelChange(int level) {
+    protected boolean onLevelChange(float level) {
         if (mLastDrawable != null) {
             return mLastDrawable.setLevel(level);
         }
@@ -510,7 +510,7 @@
         d.setVisible(isVisible(), true);
         d.setDither(mDrawableContainerState.mDither);
         d.setState(getState());
-        d.setLevel(getLevel());
+        d.setLevel(getLevelFloat());
         d.setBounds(getBounds());
         d.setLayoutDirection(getLayoutDirection());
         d.setAutoMirrored(mDrawableContainerState.mAutoMirrored);
diff --git a/graphics/java/android/graphics/drawable/DrawableWrapper.java b/graphics/java/android/graphics/drawable/DrawableWrapper.java
index 9185e1a..57b4db2 100644
--- a/graphics/java/android/graphics/drawable/DrawableWrapper.java
+++ b/graphics/java/android/graphics/drawable/DrawableWrapper.java
@@ -92,7 +92,7 @@
             // Only call setters for data that's stored in the base Drawable.
             dr.setVisible(isVisible(), true);
             dr.setState(getState());
-            dr.setLevel(getLevel());
+            dr.setLevel(getLevelFloat());
             dr.setBounds(getBounds());
             dr.setLayoutDirection(getLayoutDirection());
 
@@ -286,7 +286,7 @@
     }
 
     @Override
-    protected boolean onLevelChange(int level) {
+    protected boolean onLevelChange(float level) {
         return mDrawable != null && mDrawable.setLevel(level);
     }
 
diff --git a/graphics/java/android/graphics/drawable/GradientDrawable.java b/graphics/java/android/graphics/drawable/GradientDrawable.java
index d7fd8a5..15295a0 100644
--- a/graphics/java/android/graphics/drawable/GradientDrawable.java
+++ b/graphics/java/android/graphics/drawable/GradientDrawable.java
@@ -530,8 +530,8 @@
      *                 {@code false} otherwise
      *
      * @see #mutate()
-     * @see #setLevel(int)
-     * @see #getLevel()
+     * @see #setLevel(float)
+     * @see #getLevelFloat()
      * @see #isUseLevel()
      */
     public void setUseLevel(boolean useLevel) {
@@ -764,7 +764,7 @@
         if (mRingPath != null && (!st.mUseLevelForShape || !mPathIsDirty)) return mRingPath;
         mPathIsDirty = false;
 
-        float sweep = st.mUseLevelForShape ? (360.0f * getLevel() / 10000.0f) : 360f;
+        float sweep = st.mUseLevelForShape ? (360.0f * getLevelFloat() / MAX_LEVEL_FLOAT) : 360f;
 
         RectF bounds = new RectF(mRect);
 
@@ -990,7 +990,7 @@
     }
 
     @Override
-    protected boolean onLevelChange(int level) {
+    protected boolean onLevelChange(float level) {
         super.onLevelChange(level);
         mGradientIsDirty = true;
         mPathIsDirty = true;
@@ -1026,7 +1026,7 @@
                 final float x0, x1, y0, y1;
 
                 if (st.mGradient == LINEAR_GRADIENT) {
-                    final float level = st.mUseLevel ? getLevel() / 10000.0f : 1.0f;
+                    final float level = st.mUseLevel ? getLevelFloat() / MAX_LEVEL_FLOAT : 1.0f;
                     switch (st.mOrientation) {
                     case TOP_BOTTOM:
                         x0 = r.left;            y0 = r.top;
@@ -1080,7 +1080,7 @@
                     }
 
                     if (st.mUseLevel) {
-                        radius *= getLevel() / 10000.0f;
+                        radius *= getLevelFloat() / MAX_LEVEL_FLOAT;
                     }
 
                     mGradientRadius = radius;
@@ -1115,7 +1115,7 @@
                             tempPositions = st.mTempPositions = new float[length + 1];
                         }
 
-                        final float level = getLevel() / 10000.0f;
+                        final float level = getLevelFloat() / MAX_LEVEL_FLOAT;
                         for (int i = 0; i < length; i++) {
                             tempPositions[i] = i * fraction * level;
                         }
diff --git a/graphics/java/android/graphics/drawable/LayerDrawable.java b/graphics/java/android/graphics/drawable/LayerDrawable.java
index 1a0ba6f..c9e38b9 100644
--- a/graphics/java/android/graphics/drawable/LayerDrawable.java
+++ b/graphics/java/android/graphics/drawable/LayerDrawable.java
@@ -1400,7 +1400,7 @@
     }
 
     @Override
-    protected boolean onLevelChange(int level) {
+    protected boolean onLevelChange(float level) {
         boolean changed = false;
 
         final ChildDrawable[] array = mLayerState.mChildren;
@@ -1733,7 +1733,7 @@
                 clone.setCallback(owner);
                 clone.setLayoutDirection(dr.getLayoutDirection());
                 clone.setBounds(dr.getBounds());
-                clone.setLevel(dr.getLevel());
+                clone.setLevel(dr.getLevelFloat());
             } else {
                 clone = null;
             }
diff --git a/graphics/java/android/graphics/drawable/LevelListDrawable.java b/graphics/java/android/graphics/drawable/LevelListDrawable.java
index b01c643..09d8b6f 100644
--- a/graphics/java/android/graphics/drawable/LevelListDrawable.java
+++ b/graphics/java/android/graphics/drawable/LevelListDrawable.java
@@ -69,15 +69,16 @@
         if (drawable != null) {
             mLevelListState.addLevel(low, high, drawable);
             // in case the new state matches our current state...
-            onLevelChange(getLevel());
+            onLevelChange(getLevelFloat());
         }
     }
 
     // overrides from Drawable
 
     @Override
-    protected boolean onLevelChange(int level) {
-        int idx = mLevelListState.indexOfLevel(level);
+    protected boolean onLevelChange(float level) {
+        final int nearestLevel = Math.round(level);
+        final int idx = mLevelListState.indexOfLevel(nearestLevel);
         if (selectDrawable(idx)) {
             return true;
         }
@@ -141,7 +142,7 @@
             mLevelListState.addLevel(low, high, dr);
         }
 
-        onLevelChange(getLevel());
+        onLevelChange(getLevelFloat());
     }
 
     @Override
@@ -240,7 +241,7 @@
     private LevelListDrawable(LevelListState state, Resources res) {
         final LevelListState as = new LevelListState(state, this, res);
         setConstantState(as);
-        onLevelChange(getLevel());
+        onLevelChange(getLevelFloat());
     }
 }
 
diff --git a/graphics/java/android/graphics/drawable/RotateDrawable.java b/graphics/java/android/graphics/drawable/RotateDrawable.java
index 036a078..71c9977 100644
--- a/graphics/java/android/graphics/drawable/RotateDrawable.java
+++ b/graphics/java/android/graphics/drawable/RotateDrawable.java
@@ -303,10 +303,10 @@
     }
 
     @Override
-    protected boolean onLevelChange(int level) {
+    protected boolean onLevelChange(float level) {
         super.onLevelChange(level);
 
-        final float value = level / (float) MAX_LEVEL;
+        final float value = level / (float) MAX_LEVEL_FLOAT;
         final float degrees = MathUtils.lerp(mState.mFromDegrees, mState.mToDegrees, value);
         mState.mCurrentDegrees = degrees;
 
diff --git a/graphics/java/android/graphics/drawable/ScaleDrawable.java b/graphics/java/android/graphics/drawable/ScaleDrawable.java
index 0acbeda..38c6b80 100644
--- a/graphics/java/android/graphics/drawable/ScaleDrawable.java
+++ b/graphics/java/android/graphics/drawable/ScaleDrawable.java
@@ -50,8 +50,6 @@
  * @attr ref android.R.styleable#ScaleDrawable_drawable
  */
 public class ScaleDrawable extends DrawableWrapper {
-    private static final int MAX_LEVEL = 10000;
-
     private final Rect mTmpRect = new Rect();
 
     private ScaleState mState;
@@ -170,7 +168,7 @@
     @Override
     public void draw(Canvas canvas) {
         final Drawable d = getDrawable();
-        if (d != null && d.getLevel() != 0) {
+        if (d != null && d.getLevelFloat() != 0) {
             d.draw(canvas);
         }
     }
@@ -178,12 +176,12 @@
     @Override
     public int getOpacity() {
         final Drawable d = getDrawable();
-        if (d.getLevel() == 0) {
+        if (d.getLevelFloat() == 0) {
             return PixelFormat.TRANSPARENT;
         }
 
         final int opacity = d.getOpacity();
-        if (opacity == PixelFormat.OPAQUE && d.getLevel() < MAX_LEVEL) {
+        if (opacity == PixelFormat.OPAQUE && d.getLevelFloat() < MAX_LEVEL_FLOAT) {
             return PixelFormat.TRANSLUCENT;
         }
 
@@ -191,7 +189,7 @@
     }
 
     @Override
-    protected boolean onLevelChange(int level) {
+    protected boolean onLevelChange(float level) {
         super.onLevelChange(level);
         onBoundsChange(getBounds());
         invalidateSelf();
@@ -203,18 +201,20 @@
         final Drawable d = getDrawable();
         final Rect r = mTmpRect;
         final boolean min = mState.mUseIntrinsicSizeAsMin;
-        final int level = getLevel();
+        final float level = getLevelFloat();
 
         int w = bounds.width();
         if (mState.mScaleWidth > 0) {
             final int iw = min ? d.getIntrinsicWidth() : 0;
-            w -= (int) ((w - iw) * (MAX_LEVEL - level) * mState.mScaleWidth / MAX_LEVEL);
+            w -= (int) ((w - iw) * (MAX_LEVEL_FLOAT - level)
+                    * mState.mScaleWidth / MAX_LEVEL_FLOAT);
         }
 
         int h = bounds.height();
         if (mState.mScaleHeight > 0) {
             final int ih = min ? d.getIntrinsicHeight() : 0;
-            h -= (int) ((h - ih) * (MAX_LEVEL - level) * mState.mScaleHeight / MAX_LEVEL);
+            h -= (int) ((h - ih) * (MAX_LEVEL_FLOAT - level)
+                    * mState.mScaleHeight / MAX_LEVEL_FLOAT);
         }
 
         final int layoutDirection = getLayoutDirection();
diff --git a/graphics/java/android/graphics/drawable/VectorDrawable.java b/graphics/java/android/graphics/drawable/VectorDrawable.java
index 1cfccc4..1747225 100644
--- a/graphics/java/android/graphics/drawable/VectorDrawable.java
+++ b/graphics/java/android/graphics/drawable/VectorDrawable.java
@@ -726,7 +726,7 @@
         Log.v(LOGTAG, indent + "matrix is :" + currentGroup.getLocalMatrix().toString());
         // Then print all the children groups
         for (int i = 0; i < currentGroup.mChildren.size(); i++) {
-            Object child = currentGroup.mChildren.get(i);
+            final VObject child = currentGroup.mChildren.get(i);
             if (child instanceof VGroup) {
                 printGroupTree((VGroup) child, level + 1);
             }
@@ -783,12 +783,6 @@
                 mThemeAttrs = copy.mThemeAttrs;
                 mChangingConfigurations = copy.mChangingConfigurations;
                 mVPathRenderer = new VPathRenderer(copy.mVPathRenderer);
-                if (copy.mVPathRenderer.mFillPaint != null) {
-                    mVPathRenderer.mFillPaint = new Paint(copy.mVPathRenderer.mFillPaint);
-                }
-                if (copy.mVPathRenderer.mStrokePaint != null) {
-                    mVPathRenderer.mStrokePaint = new Paint(copy.mVPathRenderer.mStrokePaint);
-                }
                 mTint = copy.mTint;
                 mTintMode = copy.mTintMode;
                 mAutoMirrored = copy.mAutoMirrored;
@@ -913,13 +907,7 @@
          */
         // Variables that only used temporarily inside the draw() call, so there
         // is no need for deep copying.
-        private final Path mPath;
-        private final Path mRenderPath;
-        private final Matrix mFinalPathMatrix = new Matrix();
-
-        private Paint mStrokePaint;
-        private Paint mFillPaint;
-        private PathMeasure mPathMeasure;
+        private final TempState mTempState = new TempState();
 
         /////////////////////////////////////////////////////
         // Variables below need to be copied (deep copy if applicable) for mutation.
@@ -935,12 +923,10 @@
 
         int mTargetDensity = DisplayMetrics.DENSITY_DEFAULT;
 
-        final ArrayMap<String, Object> mVGTargetsMap = new ArrayMap<String, Object>();
+        final ArrayMap<String, Object> mVGTargetsMap = new ArrayMap<>();
 
         public VPathRenderer() {
             mRootGroup = new VGroup();
-            mPath = new Path();
-            mRenderPath = new Path();
         }
 
         public void setRootAlpha(int alpha) {
@@ -964,8 +950,6 @@
 
         public VPathRenderer(VPathRenderer copy) {
             mRootGroup = new VGroup(copy.mRootGroup, mVGTargetsMap);
-            mPath = new Path(copy.mPath);
-            mRenderPath = new Path(copy.mRenderPath);
             mBaseWidth = copy.mBaseWidth;
             mBaseHeight = copy.mBaseHeight;
             mViewportWidth = copy.mViewportWidth;
@@ -981,215 +965,28 @@
         }
 
         public boolean canApplyTheme() {
-            // If one of the paths can apply theme, then return true;
-            return recursiveCanApplyTheme(mRootGroup);
-        }
-
-        private boolean recursiveCanApplyTheme(VGroup currentGroup) {
-            // We can do a tree traverse here, if there is one path return true,
-            // then we return true for the whole tree.
-            final ArrayList<Object> children = currentGroup.mChildren;
-
-            for (int i = 0; i < children.size(); i++) {
-                Object child = children.get(i);
-                if (child instanceof VGroup) {
-                    VGroup childGroup = (VGroup) child;
-                    if (childGroup.canApplyTheme()
-                            || recursiveCanApplyTheme(childGroup)) {
-                        return true;
-                    }
-                } else if (child instanceof VPath) {
-                    VPath childPath = (VPath) child;
-                    if (childPath.canApplyTheme()) {
-                        return true;
-                    }
-                }
-            }
-            return false;
+            return mRootGroup.canApplyTheme();
         }
 
         public void applyTheme(Theme t) {
-            // Apply theme to every path of the tree.
-            recursiveApplyTheme(mRootGroup, t);
-        }
-
-        private void recursiveApplyTheme(VGroup currentGroup, Theme t) {
-            // We can do a tree traverse here, apply theme to all paths which
-            // can apply theme.
-            final ArrayList<Object> children = currentGroup.mChildren;
-            for (int i = 0; i < children.size(); i++) {
-                Object child = children.get(i);
-                if (child instanceof VGroup) {
-                    VGroup childGroup = (VGroup) child;
-                    if (childGroup.canApplyTheme()) {
-                        childGroup.applyTheme(t);
-                    }
-                    recursiveApplyTheme(childGroup, t);
-                } else if (child instanceof VPath) {
-                    VPath childPath = (VPath) child;
-                    if (childPath.canApplyTheme()) {
-                        childPath.applyTheme(t);
-                    }
-                }
-            }
-        }
-
-        private void drawGroupTree(VGroup currentGroup, Matrix currentMatrix,
-                Canvas canvas, int w, int h, ColorFilter filter) {
-            // Calculate current group's matrix by preConcat the parent's and
-            // and the current one on the top of the stack.
-            // Basically the Mfinal = Mviewport * M0 * M1 * M2;
-            // Mi the local matrix at level i of the group tree.
-            currentGroup.mStackedMatrix.set(currentMatrix);
-            currentGroup.mStackedMatrix.preConcat(currentGroup.mLocalMatrix);
-
-            // Save the current clip information, which is local to this group.
-            canvas.save();
-            // Draw the group tree in the same order as the XML file.
-            for (int i = 0; i < currentGroup.mChildren.size(); i++) {
-                Object child = currentGroup.mChildren.get(i);
-                if (child instanceof VGroup) {
-                    VGroup childGroup = (VGroup) child;
-                    drawGroupTree(childGroup, currentGroup.mStackedMatrix,
-                            canvas, w, h, filter);
-                } else if (child instanceof VPath) {
-                    VPath childPath = (VPath) child;
-                    drawPath(currentGroup, childPath, canvas, w, h, filter);
-                }
-            }
-            canvas.restore();
+            mRootGroup.applyTheme(t);
         }
 
         public void draw(Canvas canvas, int w, int h, ColorFilter filter) {
-            // Travese the tree in pre-order to draw.
-            drawGroupTree(mRootGroup, Matrix.IDENTITY_MATRIX, canvas, w, h, filter);
-        }
-
-        private void drawPath(VGroup vGroup, VPath vPath, Canvas canvas, int w, int h,
-                ColorFilter filter) {
             final float scaleX = w / mViewportWidth;
             final float scaleY = h / mViewportHeight;
-            final float minScale = Math.min(scaleX, scaleY);
-            final Matrix groupStackedMatrix = vGroup.mStackedMatrix;
-
-            mFinalPathMatrix.set(groupStackedMatrix);
-            mFinalPathMatrix.postScale(scaleX, scaleY);
-
-            final float matrixScale = getMatrixScale(groupStackedMatrix);
-            if (matrixScale == 0) {
-                // When either x or y is scaled to 0, we don't need to draw anything.
-                return;
-            }
-            vPath.toPath(mPath);
-            final Path path = mPath;
-
-            mRenderPath.reset();
-
-            if (vPath.isClipPath()) {
-                mRenderPath.addPath(path, mFinalPathMatrix);
-                canvas.clipPath(mRenderPath);
-            } else {
-                VFullPath fullPath = (VFullPath) vPath;
-                if (fullPath.mTrimPathStart != 0.0f || fullPath.mTrimPathEnd != 1.0f) {
-                    float start = (fullPath.mTrimPathStart + fullPath.mTrimPathOffset) % 1.0f;
-                    float end = (fullPath.mTrimPathEnd + fullPath.mTrimPathOffset) % 1.0f;
-
-                    if (mPathMeasure == null) {
-                        mPathMeasure = new PathMeasure();
-                    }
-                    mPathMeasure.setPath(mPath, false);
-
-                    float len = mPathMeasure.getLength();
-                    start = start * len;
-                    end = end * len;
-                    path.reset();
-                    if (start > end) {
-                        mPathMeasure.getSegment(start, len, path, true);
-                        mPathMeasure.getSegment(0f, end, path, true);
-                    } else {
-                        mPathMeasure.getSegment(start, end, path, true);
-                    }
-                    path.rLineTo(0, 0); // fix bug in measure
-                }
-                mRenderPath.addPath(path, mFinalPathMatrix);
-
-                if (fullPath.mFillColor != Color.TRANSPARENT) {
-                    if (mFillPaint == null) {
-                        mFillPaint = new Paint();
-                        mFillPaint.setStyle(Paint.Style.FILL);
-                        mFillPaint.setAntiAlias(true);
-                    }
-
-                    final Paint fillPaint = mFillPaint;
-                    fillPaint.setColor(applyAlpha(fullPath.mFillColor, fullPath.mFillAlpha));
-                    fillPaint.setColorFilter(filter);
-                    canvas.drawPath(mRenderPath, fillPaint);
-                }
-
-                if (fullPath.mStrokeColor != Color.TRANSPARENT) {
-                    if (mStrokePaint == null) {
-                        mStrokePaint = new Paint();
-                        mStrokePaint.setStyle(Paint.Style.STROKE);
-                        mStrokePaint.setAntiAlias(true);
-                    }
-
-                    final Paint strokePaint = mStrokePaint;
-                    if (fullPath.mStrokeLineJoin != null) {
-                        strokePaint.setStrokeJoin(fullPath.mStrokeLineJoin);
-                    }
-
-                    if (fullPath.mStrokeLineCap != null) {
-                        strokePaint.setStrokeCap(fullPath.mStrokeLineCap);
-                    }
-
-                    strokePaint.setStrokeMiter(fullPath.mStrokeMiterlimit);
-                    strokePaint.setColor(applyAlpha(fullPath.mStrokeColor, fullPath.mStrokeAlpha));
-                    strokePaint.setColorFilter(filter);
-                    final float finalStrokeScale = minScale * matrixScale;
-                    strokePaint.setStrokeWidth(fullPath.mStrokeWidth * finalStrokeScale);
-                    canvas.drawPath(mRenderPath, strokePaint);
-                }
-            }
-        }
-
-        private float getMatrixScale(Matrix groupStackedMatrix) {
-            // Given unit vectors A = (0, 1) and B = (1, 0).
-            // After matrix mapping, we got A' and B'. Let theta = the angel b/t A' and B'.
-            // Therefore, the final scale we want is min(|A'| * sin(theta), |B'| * sin(theta)),
-            // which is (|A'| * |B'| * sin(theta)) / max (|A'|, |B'|);
-            // If  max (|A'|, |B'|) = 0, that means either x or y has a scale of 0.
-            //
-            // For non-skew case, which is most of the cases, matrix scale is computing exactly the
-            // scale on x and y axis, and take the minimal of these two.
-            // For skew case, an unit square will mapped to a parallelogram. And this function will
-            // return the minimal height of the 2 bases.
-            float[] unitVectors = new float[] {0, 1, 1, 0};
-            groupStackedMatrix.mapVectors(unitVectors);
-            float scaleX = MathUtils.mag(unitVectors[0], unitVectors[1]);
-            float scaleY = MathUtils.mag(unitVectors[2], unitVectors[3]);
-            float crossProduct = MathUtils.cross(unitVectors[0], unitVectors[1],
-                    unitVectors[2], unitVectors[3]);
-            float maxScale = MathUtils.max(scaleX, scaleY);
-
-            float matrixScale = 0;
-            if (maxScale > 0) {
-                matrixScale = MathUtils.abs(crossProduct) / maxScale;
-            }
-            if (DBG_VECTOR_DRAWABLE) {
-                Log.d(LOGTAG, "Scale x " + scaleX + " y " + scaleY + " final " + matrixScale);
-            }
-            return matrixScale;
+            mRootGroup.draw(canvas, mTempState, Matrix.IDENTITY_MATRIX, filter, scaleX, scaleY);
         }
     }
 
-    private static class VGroup {
+    private static class VGroup implements VObject {
         // mStackedMatrix is only used temporarily when drawing, it combines all
         // the parents' local matrices with the current one.
         private final Matrix mStackedMatrix = new Matrix();
 
         /////////////////////////////////////////////////////
         // Variables below need to be copied (deep copy if applicable) for mutation.
-        final ArrayList<Object> mChildren = new ArrayList<Object>();
+        final ArrayList<VObject> mChildren = new ArrayList<>();
 
         private float mRotate = 0;
         private float mPivotX = 0;
@@ -1223,14 +1020,14 @@
 
             mLocalMatrix.set(copy.mLocalMatrix);
 
-            final ArrayList<Object> children = copy.mChildren;
+            final ArrayList<VObject> children = copy.mChildren;
             for (int i = 0; i < children.size(); i++) {
-                Object copyChild = children.get(i);
+                final VObject copyChild = children.get(i);
                 if (copyChild instanceof VGroup) {
-                    VGroup copyGroup = (VGroup) copyChild;
+                    final VGroup copyGroup = (VGroup) copyChild;
                     mChildren.add(new VGroup(copyGroup, targetsMap));
                 } else {
-                    VPath newPath = null;
+                    final VPath newPath;
                     if (copyChild instanceof VFullPath) {
                         newPath = new VFullPath((VFullPath) copyChild);
                     } else if (copyChild instanceof VClipPath) {
@@ -1257,6 +1054,30 @@
             return mLocalMatrix;
         }
 
+        @Override
+        public void draw(Canvas canvas, TempState temp, Matrix currentMatrix,
+                ColorFilter filter, float scaleX, float scaleY) {
+            // Calculate current group's matrix by preConcat the parent's and
+            // and the current one on the top of the stack.
+            // Basically the Mfinal = Mviewport * M0 * M1 * M2;
+            // Mi the local matrix at level i of the group tree.
+            mStackedMatrix.set(currentMatrix);
+            mStackedMatrix.preConcat(mLocalMatrix);
+
+            // Save the current clip information, which is local to this group.
+            canvas.save();
+
+            // Draw the group tree in the same order as the XML file.
+            for (int i = 0, count = mChildren.size(); i < count; i++) {
+                final VObject child = mChildren.get(i);
+                child.draw(canvas, temp, mStackedMatrix, filter, scaleX, scaleY);
+            }
+
+            // Restore the previous clip information.
+            canvas.restore();
+        }
+
+        @Override
         public void inflate(Resources res, AttributeSet attrs, Theme theme) {
             final TypedArray a = obtainAttributes(res, theme, attrs,
                     R.styleable.VectorDrawableGroup);
@@ -1287,18 +1108,39 @@
             updateLocalMatrix();
         }
 
+        @Override
         public boolean canApplyTheme() {
-            return mThemeAttrs != null;
-        }
-
-        public void applyTheme(Theme t) {
-            if (mThemeAttrs == null) {
-                return;
+            if (mThemeAttrs != null) {
+                return true;
             }
 
-            final TypedArray a = t.resolveAttributes(mThemeAttrs, R.styleable.VectorDrawableGroup);
-            updateStateFromTypedArray(a);
-            a.recycle();
+            final ArrayList<VObject> children = mChildren;
+            for (int i = 0, count = children.size(); i < count; i++) {
+                final VObject child = children.get(i);
+                if (child.canApplyTheme()) {
+                    return true;
+                }
+            }
+
+            return false;
+        }
+
+        @Override
+        public void applyTheme(Theme t) {
+            if (mThemeAttrs != null) {
+                final TypedArray a = t.resolveAttributes(mThemeAttrs,
+                        R.styleable.VectorDrawableGroup);
+                updateStateFromTypedArray(a);
+                a.recycle();
+            }
+
+            final ArrayList<VObject> children = mChildren;
+            for (int i = 0, count = children.size(); i < count; i++) {
+                final VObject child = children.get(i);
+                if (child.canApplyTheme()) {
+                    child.applyTheme(t);
+                }
+            }
         }
 
         private void updateLocalMatrix() {
@@ -1407,7 +1249,7 @@
     /**
      * Common Path information for clip path and normal path.
      */
-    private static class VPath {
+    private static abstract class VPath implements VObject {
         protected PathParser.PathDataNode[] mNodes = null;
         String mPathName;
         int mChangingConfigurations;
@@ -1422,24 +1264,10 @@
             mNodes = PathParser.deepCopyNodes(copy.mNodes);
         }
 
-        public void toPath(Path path) {
-            path.reset();
-            if (mNodes != null) {
-                PathParser.PathDataNode.nodesToPath(mNodes, path);
-            }
-        }
-
         public String getPathName() {
             return mPathName;
         }
 
-        public boolean canApplyTheme() {
-            return false;
-        }
-
-        public void applyTheme(Theme t) {
-        }
-
         public boolean isClipPath() {
             return false;
         }
@@ -1459,6 +1287,79 @@
                 PathParser.updateNodes(mNodes, nodes);
             }
         }
+
+        @Override
+        public final void draw(Canvas canvas, TempState temp, Matrix groupStackedMatrix,
+                ColorFilter filter, float scaleX, float scaleY) {
+            final float matrixScale = VPath.getMatrixScale(groupStackedMatrix);
+            if (matrixScale == 0) {
+                // When either x or y is scaled to 0, we don't need to draw anything.
+                return;
+            }
+
+            final Path path = temp.path;
+            path.reset();
+            toPath(temp, path);
+
+            final Matrix pathMatrix = temp.pathMatrix;
+            pathMatrix.set(groupStackedMatrix);
+            pathMatrix.postScale(scaleX, scaleY);
+
+            final Path renderPath = temp.renderPath;
+            renderPath.reset();
+            renderPath.addPath(path, pathMatrix);
+
+            final float minScale = Math.min(scaleX, scaleY);
+            final float strokeScale = minScale * matrixScale;
+            drawPath(temp, renderPath, canvas, filter, strokeScale);
+        }
+
+        /**
+         * Writes the path's nodes to an output Path for rendering.
+         *
+         * @param temp temporary state variables
+         * @param outPath the output path
+         */
+        protected void toPath(TempState temp, Path outPath) {
+            if (mNodes != null) {
+                PathParser.PathDataNode.nodesToPath(mNodes, outPath);
+            }
+        }
+
+        /**
+         * Draws the specified path into the supplied canvas.
+         */
+        protected abstract void drawPath(TempState temp, Path path, Canvas canvas,
+                ColorFilter filter, float strokeScale);
+
+        private static float getMatrixScale(Matrix groupStackedMatrix) {
+            // Given unit vectors A = (0, 1) and B = (1, 0).
+            // After matrix mapping, we got A' and B'. Let theta = the angel b/t A' and B'.
+            // Therefore, the final scale we want is min(|A'| * sin(theta), |B'| * sin(theta)),
+            // which is (|A'| * |B'| * sin(theta)) / max (|A'|, |B'|);
+            // If  max (|A'|, |B'|) = 0, that means either x or y has a scale of 0.
+            //
+            // For non-skew case, which is most of the cases, matrix scale is computing exactly the
+            // scale on x and y axis, and take the minimal of these two.
+            // For skew case, an unit square will mapped to a parallelogram. And this function will
+            // return the minimal height of the 2 bases.
+            float[] unitVectors = new float[] {0, 1, 1, 0};
+            groupStackedMatrix.mapVectors(unitVectors);
+            float scaleX = MathUtils.mag(unitVectors[0], unitVectors[1]);
+            float scaleY = MathUtils.mag(unitVectors[2], unitVectors[3]);
+            float crossProduct = MathUtils.cross(unitVectors[0], unitVectors[1],
+                    unitVectors[2], unitVectors[3]);
+            float maxScale = MathUtils.max(scaleX, scaleY);
+
+            float matrixScale = 0;
+            if (maxScale > 0) {
+                matrixScale = MathUtils.abs(crossProduct) / maxScale;
+            }
+            if (DBG_VECTOR_DRAWABLE) {
+                Log.d(LOGTAG, "Scale x " + scaleX + " y " + scaleY + " final " + matrixScale);
+            }
+            return matrixScale;
+        }
     }
 
     /**
@@ -1473,6 +1374,13 @@
             super(copy);
         }
 
+        @Override
+        protected void drawPath(TempState temp, Path renderPath, Canvas canvas, ColorFilter filter,
+                float strokeScale) {
+            canvas.clipPath(renderPath);
+        }
+
+        @Override
         public void inflate(Resources r, AttributeSet attrs, Theme theme) {
             final TypedArray a = obtainAttributes(r, theme, attrs,
                     R.styleable.VectorDrawableClipPath);
@@ -1480,6 +1388,16 @@
             a.recycle();
         }
 
+        @Override
+        public boolean canApplyTheme() {
+            return false;
+        }
+
+        @Override
+        public void applyTheme(Theme theme) {
+            // No-op.
+        }
+
         private void updateStateFromTypedArray(TypedArray a) {
             // Account for any configuration changes.
             mChangingConfigurations |= a.getChangingConfigurations();
@@ -1574,10 +1492,104 @@
         }
 
         @Override
-        public boolean canApplyTheme() {
-            return mThemeAttrs != null;
+        public void toPath(TempState temp, Path path) {
+            super.toPath(temp, path);
+
+            if (mTrimPathStart != 0.0f || mTrimPathEnd != 1.0f) {
+                VFullPath.applyTrim(temp, path, mTrimPathStart, mTrimPathEnd, mTrimPathOffset);
+            }
         }
 
+        @Override
+        protected void drawPath(TempState temp, Path path, Canvas canvas, ColorFilter filter,
+                float strokeScale) {
+            drawPathFill(temp, path, canvas, filter);
+            drawPathStroke(temp, path, canvas, filter, strokeScale);
+        }
+
+        /**
+         * Draws this path's fill, if necessary.
+         */
+        private void drawPathFill(TempState temp, Path path, Canvas canvas, ColorFilter filter) {
+            if (mFillColor == Color.TRANSPARENT) {
+                return;
+            }
+
+            if (temp.mFillPaint == null) {
+                temp.mFillPaint = new Paint();
+                temp.mFillPaint.setStyle(Paint.Style.FILL);
+                temp.mFillPaint.setAntiAlias(true);
+            }
+
+            final Paint fillPaint = temp.mFillPaint;
+            fillPaint.setColor(applyAlpha(mFillColor, mFillAlpha));
+            fillPaint.setColorFilter(filter);
+            canvas.drawPath(path, fillPaint);
+        }
+
+        /**
+         * Draws this path's stroke, if necessary.
+         */
+        private void drawPathStroke(TempState temp, Path path, Canvas canvas, ColorFilter filter,
+                float strokeScale) {
+            if (mStrokeColor == Color.TRANSPARENT) {
+                return;
+            }
+
+            if (temp.mStrokePaint == null) {
+                temp.mStrokePaint = new Paint();
+                temp.mStrokePaint.setStyle(Paint.Style.STROKE);
+                temp.mStrokePaint.setAntiAlias(true);
+            }
+
+            final Paint strokePaint = temp.mStrokePaint;
+            if (mStrokeLineJoin != null) {
+                strokePaint.setStrokeJoin(mStrokeLineJoin);
+            }
+
+            if (mStrokeLineCap != null) {
+                strokePaint.setStrokeCap(mStrokeLineCap);
+            }
+
+            strokePaint.setStrokeMiter(mStrokeMiterlimit);
+            strokePaint.setColor(applyAlpha(mStrokeColor, mStrokeAlpha));
+            strokePaint.setColorFilter(filter);
+            strokePaint.setStrokeWidth(mStrokeWidth * strokeScale);
+            canvas.drawPath(path, strokePaint);
+        }
+
+        /**
+         * Applies trimming to the specified path.
+         */
+        private static void applyTrim(TempState temp, Path path, float mTrimPathStart,
+                float mTrimPathEnd, float mTrimPathOffset) {
+            if (mTrimPathStart == 0.0f && mTrimPathEnd == 1.0f) {
+                // No trimming necessary.
+                return;
+            }
+
+            if (temp.mPathMeasure == null) {
+                temp.mPathMeasure = new PathMeasure();
+            }
+            final PathMeasure pathMeasure = temp.mPathMeasure;
+            pathMeasure.setPath(path, false);
+
+            final float len = pathMeasure.getLength();
+            final float start = len * ((mTrimPathStart + mTrimPathOffset) % 1.0f);
+            final float end = len * ((mTrimPathEnd + mTrimPathOffset) % 1.0f);
+            path.reset();
+            if (start > end) {
+                pathMeasure.getSegment(start, len, path, true);
+                pathMeasure.getSegment(0, end, path, true);
+            } else {
+                pathMeasure.getSegment(start, end, path, true);
+            }
+
+            // Fix bug in measure.
+            path.rLineTo(0, 0);
+        }
+
+        @Override
         public void inflate(Resources r, AttributeSet attrs, Theme theme) {
             final TypedArray a = obtainAttributes(r, theme, attrs,
                     R.styleable.VectorDrawablePath);
@@ -1627,6 +1639,11 @@
         }
 
         @Override
+        public boolean canApplyTheme() {
+            return mThemeAttrs != null;
+        }
+
+        @Override
         public void applyTheme(Theme t) {
             if (mThemeAttrs == null) {
                 return;
@@ -1718,4 +1735,22 @@
             mTrimPathOffset = trimPathOffset;
         }
     }
+
+    static class TempState {
+        final Matrix pathMatrix = new Matrix();
+        final Path path = new Path();
+        final Path renderPath = new Path();
+
+        PathMeasure mPathMeasure;
+        Paint mFillPaint;
+        Paint mStrokePaint;
+    }
+
+    interface VObject {
+        void draw(Canvas canvas, TempState temp, Matrix currentMatrix,
+                ColorFilter filter, float scaleX, float scaleY);
+        void inflate(Resources r, AttributeSet attrs, Theme theme);
+        boolean canApplyTheme();
+        void applyTheme(Theme t);
+    }
 }
diff --git a/libs/androidfw/Android.mk b/libs/androidfw/Android.mk
index 2f28700..f682fb8 100644
--- a/libs/androidfw/Android.mk
+++ b/libs/androidfw/Android.mk
@@ -40,10 +40,9 @@
 # For the host
 # =====================================================
 include $(CLEAR_VARS)
-LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
 
 LOCAL_MODULE:= libandroidfw
-LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE_HOST_OS := darwin linux windows
 LOCAL_CFLAGS += -DSTATIC_ANDROIDFW_FOR_TOOLS
 LOCAL_CFLAGS += -Wall -Werror -Wunused -Wunreachable-code
 LOCAL_SRC_FILES:= $(hostSources)
@@ -56,13 +55,10 @@
 # =====================================================
 
 include $(CLEAR_VARS)
-LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
 
 LOCAL_MODULE:= libandroidfw
-LOCAL_MODULE_TAGS := optional
 LOCAL_SRC_FILES:= $(deviceSources)
 LOCAL_C_INCLUDES := \
-    external/zlib \
     system/core/include
 LOCAL_STATIC_LIBRARIES := libziparchive libbase
 LOCAL_SHARED_LIBRARIES := \
diff --git a/libs/hwui/Android.mk b/libs/hwui/Android.mk
index 676ab1c..3eb13ab 100644
--- a/libs/hwui/Android.mk
+++ b/libs/hwui/Android.mk
@@ -165,6 +165,7 @@
 LOCAL_CFLAGS := $(hwui_cflags)
 
 LOCAL_SRC_FILES += \
+    unit_tests/CanvasStateTests.cpp \
     unit_tests/ClipAreaTests.cpp \
     unit_tests/DamageAccumulatorTests.cpp \
     unit_tests/LinearAllocatorTests.cpp
diff --git a/libs/hwui/unit_tests/CanvasStateTests.cpp b/libs/hwui/unit_tests/CanvasStateTests.cpp
new file mode 100644
index 0000000..79852be
--- /dev/null
+++ b/libs/hwui/unit_tests/CanvasStateTests.cpp
@@ -0,0 +1,166 @@
+/*
+ * 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.
+ */
+
+#include "CanvasState.h"
+
+#include "Matrix.h"
+#include "Rect.h"
+#include "utils/LinearAllocator.h"
+
+#include <gtest/gtest.h>
+#include <SkPath.h>
+#include <SkRegion.h>
+#include <SkCanvas.h>
+
+namespace android {
+namespace uirenderer {
+
+class NullClient: public CanvasStateClient {
+    void onViewportInitialized() override {}
+    void onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {}
+    GLuint getTargetFbo() const override { return 0; }
+};
+
+static NullClient sNullClient;
+
+static bool approxEqual(const Matrix4& a, const Matrix4& b) {
+    for (int i = 0; i < 16; i++) {
+        if (!MathUtils::areEqual(a[i], b[i])) {
+            return false;
+        }
+    }
+    return true;
+}
+
+TEST(CanvasState, gettersAndSetters) {
+    CanvasState state(sNullClient);
+    state.setViewport(200, 200);
+    state.initializeSaveStack(0, 0, state.getWidth(), state.getHeight(), Vector3());
+
+    ASSERT_EQ(state.getWidth(), 200);
+    ASSERT_EQ(state.getHeight(), 200);
+
+    Matrix4 simpleTranslate;
+    simpleTranslate.loadTranslate(10, 20, 0);
+    state.setMatrix(simpleTranslate);
+
+    ASSERT_EQ(state.getRenderTargetClipBounds(), Rect(0, 0, 200, 200));
+    ASSERT_EQ(state.getLocalClipBounds(), Rect(-10, -20, 190, 180));
+    EXPECT_TRUE(approxEqual(*state.currentTransform(), simpleTranslate));
+    EXPECT_TRUE(state.clipIsSimple());
+}
+
+TEST(CanvasState, simpleClipping) {
+    CanvasState state(sNullClient);
+    state.setViewport(200, 200);
+    state.initializeSaveStack(0, 0, state.getWidth(), state.getHeight(), Vector3());
+
+    state.clipRect(0, 0, 100, 100, SkRegion::kIntersect_Op);
+    ASSERT_EQ(state.getRenderTargetClipBounds(), Rect(0, 0, 100, 100));
+
+    state.clipRect(10, 10, 200, 200, SkRegion::kIntersect_Op);
+    ASSERT_EQ(state.getRenderTargetClipBounds(), Rect(10, 10, 100, 100));
+
+    state.clipRect(50, 50, 150, 150, SkRegion::kReplace_Op);
+    ASSERT_EQ(state.getRenderTargetClipBounds(), Rect(50, 50, 150, 150));
+}
+
+TEST(CanvasState, complexClipping) {
+    CanvasState state(sNullClient);
+    state.setViewport(200, 200);
+    state.initializeSaveStack(0, 0, state.getWidth(), state.getHeight(), Vector3());
+
+    state.save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
+    {
+        // rotated clip causes complex clip
+        state.rotate(10);
+        EXPECT_TRUE(state.clipIsSimple());
+        state.clipRect(0, 0, 200, 200, SkRegion::kIntersect_Op);
+        EXPECT_FALSE(state.clipIsSimple());
+    }
+    state.restore();
+
+    state.save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
+    {
+        // subtracted clip causes complex clip
+        EXPECT_TRUE(state.clipIsSimple());
+        state.clipRect(50, 50, 150, 150, SkRegion::kDifference_Op);
+        EXPECT_FALSE(state.clipIsSimple());
+    }
+    state.restore();
+
+    state.save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
+    {
+        // complex path causes complex clip
+        SkPath path;
+        path.addOval(SkRect::MakeWH(200, 200));
+        EXPECT_TRUE(state.clipIsSimple());
+        state.clipPath(&path, SkRegion::kDifference_Op);
+        EXPECT_FALSE(state.clipIsSimple());
+    }
+    state.restore();
+}
+
+TEST(CanvasState, saveAndRestore) {
+    CanvasState state(sNullClient);
+    state.setViewport(200, 200);
+    state.initializeSaveStack(0, 0, state.getWidth(), state.getHeight(), Vector3());
+
+    state.save(SkCanvas::kClip_SaveFlag);
+    {
+        state.clipRect(0, 0, 10, 10, SkRegion::kIntersect_Op);
+        ASSERT_EQ(state.getRenderTargetClipBounds(), Rect(0, 0, 10, 10));
+    }
+    state.restore();
+    ASSERT_EQ(state.getRenderTargetClipBounds(), Rect(0, 0, 200, 200)); // verify restore
+
+    Matrix4 simpleTranslate;
+    simpleTranslate.loadTranslate(10, 10, 0);
+    state.save(SkCanvas::kMatrix_SaveFlag);
+    {
+        state.translate(10, 10, 0);
+        EXPECT_TRUE(approxEqual(*state.currentTransform(), simpleTranslate));
+    }
+    state.restore();
+    EXPECT_FALSE(approxEqual(*state.currentTransform(), simpleTranslate));
+}
+
+TEST(CanvasState, saveAndRestoreButNotTooMuch) {
+    CanvasState state(sNullClient);
+    state.setViewport(200, 200);
+    state.initializeSaveStack(0, 0, state.getWidth(), state.getHeight(), Vector3());
+
+    state.save(SkCanvas::kMatrix_SaveFlag); // Note: clip not saved
+    {
+        state.clipRect(0, 0, 10, 10, SkRegion::kIntersect_Op);
+        ASSERT_EQ(state.getRenderTargetClipBounds(), Rect(0, 0, 10, 10));
+    }
+    state.restore();
+    ASSERT_EQ(state.getRenderTargetClipBounds(), Rect(0, 0, 10, 10)); // verify not restored
+
+    Matrix4 simpleTranslate;
+    simpleTranslate.loadTranslate(10, 10, 0);
+    state.save(SkCanvas::kClip_SaveFlag); // NOTE: matrix not saved
+    {
+        state.translate(10, 10, 0);
+        EXPECT_TRUE(approxEqual(*state.currentTransform(), simpleTranslate));
+    }
+    state.restore();
+    EXPECT_TRUE(approxEqual(*state.currentTransform(), simpleTranslate)); // verify not restored
+}
+
+}
+}
diff --git a/libs/hwui/utils/LinearAllocator.cpp b/libs/hwui/utils/LinearAllocator.cpp
index 01cf5d2..0abe88b 100644
--- a/libs/hwui/utils/LinearAllocator.cpp
+++ b/libs/hwui/utils/LinearAllocator.cpp
@@ -140,7 +140,7 @@
 }
 
 void* LinearAllocator::start(Page* p) {
-    return ALIGN_PTR(((size_t*)p) + sizeof(Page));
+    return ALIGN_PTR((size_t)p + sizeof(Page));
 }
 
 void* LinearAllocator::end(Page* p) {
diff --git a/media/java/android/mtp/MtpObjectInfo.java b/media/java/android/mtp/MtpObjectInfo.java
index a080c73..64aa997 100644
--- a/media/java/android/mtp/MtpObjectInfo.java
+++ b/media/java/android/mtp/MtpObjectInfo.java
@@ -294,6 +294,11 @@
             mObjectInfo.mThumbPixWidth = objectInfo.mThumbPixWidth;
         }
 
+        public Builder setObjectHandle(int value) {
+            mObjectInfo.mHandle = value;
+            return this;
+        }
+
         public Builder setAssociationDesc(int value) {
             mObjectInfo.mAssociationDesc = value;
             return this;
diff --git a/packages/DocumentsUI/Android.mk b/packages/DocumentsUI/Android.mk
index c21c5cc..1a4e3eb 100644
--- a/packages/DocumentsUI/Android.mk
+++ b/packages/DocumentsUI/Android.mk
@@ -13,17 +13,21 @@
 LOCAL_STATIC_JAVA_LIBRARIES += android-support-v7-recyclerview
 LOCAL_STATIC_JAVA_LIBRARIES += guava
 
-LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res 
+LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res
 # Not quite sure why it is necessary to explicitly pull in resources from the
 # appcompat lib, but the demo code indicates it's necessary (see
 # development/samples/Support7Demos/Android.mk)
-LOCAL_RESOURCE_DIR += frameworks/support/v7/appcompat/res
-LOCAL_RESOURCE_DIR += frameworks/support/design/res
+LOCAL_RESOURCE_DIR += \
+  frameworks/support/v7/appcompat/res \
+  frameworks/support/design/res \
+  frameworks/support/v7/recyclerview/res
 
 # Again, required to pull in appcompat resources.  See abovementioned demo code.
-LOCAL_AAPT_FLAGS := --auto-add-overlay
-LOCAL_AAPT_FLAGS += --extra-packages android.support.v7.appcompat
-LOCAL_AAPT_FLAGS += --extra-packages android.support.design
+LOCAL_AAPT_FLAGS := \
+  --auto-add-overlay \
+  --extra-packages android.support.v7.appcompat \
+  --extra-packages android.support.design \
+  --extra-packages android.support.v7.recyclerview
 
 LOCAL_PACKAGE_NAME := DocumentsUI
 LOCAL_CERTIFICATE := platform
diff --git a/packages/DocumentsUI/res/values/strings.xml b/packages/DocumentsUI/res/values/strings.xml
index 616f4dd..4b44d7c 100644
--- a/packages/DocumentsUI/res/values/strings.xml
+++ b/packages/DocumentsUI/res/values/strings.xml
@@ -139,6 +139,13 @@
         <item quantity="one">Moving <xliff:g id="count" example="1">%1$d</xliff:g> file.</item>
         <item quantity="other">Moving <xliff:g id="count" example="3">%1$d</xliff:g> files.</item>
     </plurals>
+    <!-- Text shown when files are deleted -->
+    <plurals name="deleting">
+        <item quantity="one">Deleting <xliff:g id="count" example="1">%1$d</xliff:g> file.</item>
+        <item quantity="other">Deleting <xliff:g id="count" example="3">%1$d</xliff:g> files.</item>
+    </plurals>
+    <!-- Text shown for the undo button -->
+    <string name="undo">Undo</string>
     <!-- Text shown on the notification while DocumentsUI performs setup in preparation for copying files [CHAR LIMIT=32] -->
     <string name="copy_preparing">Preparing for copy\u2026</string>
     <!-- Text shown on the notification while DocumentsUI performs setup in preparation for moving files [CHAR LIMIT=32] -->
diff --git a/packages/DocumentsUI/src/com/android/documentsui/CopyService.java b/packages/DocumentsUI/src/com/android/documentsui/CopyService.java
index 8b92331..f8ec8f1 100644
--- a/packages/DocumentsUI/src/com/android/documentsui/CopyService.java
+++ b/packages/DocumentsUI/src/com/android/documentsui/CopyService.java
@@ -56,6 +56,7 @@
 
 public class CopyService extends IntentService {
     public static final String TAG = "CopyService";
+    public static final boolean DEBUG = false;
 
     private static final String EXTRA_CANCEL = "com.android.documentsui.CANCEL";
     public static final String EXTRA_SRC_LIST = "com.android.documentsui.SRC_LIST";
@@ -159,6 +160,7 @@
             // Catch-all to prevent any copy errors from wedging the app.
             Log.e(TAG, "Exceptions occurred during copying", e);
         } finally {
+            if (DEBUG) Log.d(TAG, "Cleaning up after copy");
             ContentProviderClient.releaseQuietly(mSrcClient);
             ContentProviderClient.releaseQuietly(mDstClient);
 
@@ -166,10 +168,12 @@
             mNotificationManager.cancel(mJobId, 0);
 
             if (mFailedFiles.size() > 0) {
+                Log.e(TAG, mFailedFiles.size() + " files failed to copy");
                 final Context context = getApplicationContext();
                 final Intent navigateIntent = new Intent(context, FilesActivity.class);
                 navigateIntent.putExtra(EXTRA_STACK, (Parcelable) stack);
                 navigateIntent.putExtra(EXTRA_FAILURE, FAILURE_COPY);
+                navigateIntent.putExtra(EXTRA_TRANSFER_MODE, transferMode);
                 navigateIntent.putParcelableArrayListExtra(EXTRA_SRC_LIST, mFailedFiles);
 
                 final int titleResourceId = (transferMode == TRANSFER_MODE_COPY ?
@@ -186,6 +190,7 @@
                         .setAutoCancel(true);
                 mNotificationManager.notify(mJobId, 0, errorBuilder.build());
             }
+            if (DEBUG) Log.d(TAG, "Done cleaning up");
         }
     }
 
@@ -398,6 +403,9 @@
      */
     private void copy(DocumentInfo srcInfo, DocumentInfo dstDirInfo, int mode)
             throws RemoteException {
+        if (DEBUG) Log.d(TAG, "Copying " + srcInfo.displayName + " (" + srcInfo.derivedUri + ")" +
+            " to " + dstDirInfo.displayName + " (" + dstDirInfo.derivedUri + ")");
+
         final Uri dstUri = DocumentsContract.createDocument(mDstClient, dstDirInfo.derivedUri,
                 srcInfo.mimeType, srcInfo.displayName);
         if (dstUri == null) {
@@ -499,10 +507,28 @@
             srcFile.checkError();
         } catch (IOException e) {
             copyError = e;
+
             try {
-                dstFile.closeWithError(copyError.getMessage());
-            } catch (IOException closeError) {
-                Log.e(TAG, "Error closing destination", closeError);
+                DocumentInfo info = DocumentInfo.fromUri(getContentResolver(), srcUri);
+                mFailedFiles.add(info);
+                Log.e(TAG, "Error while copying " + info.displayName + " (" + info.derivedUri + ")",
+                        copyError);
+            } catch (FileNotFoundException ignore) {
+                // Generate a dummy DocumentInfo so an error still gets reflected in the UI for this
+                // file.
+                DocumentInfo info = new DocumentInfo();
+                info.derivedUri = srcUri;
+                info.displayName = "Unknown [" + srcUri + "]";
+                mFailedFiles.add(info);
+                Log.e(TAG, "Error while copying " + srcUri, copyError);
+            }
+
+            if (dstFile != null) {
+                try {
+                    dstFile.closeWithError(copyError.getMessage());
+                } catch (IOException closeError) {
+                    Log.e(TAG, "Error closing destination", closeError);
+                }
             }
         } finally {
             // This also ensures the file descriptors are closed.
@@ -510,16 +536,6 @@
             IoUtils.closeQuietly(dst);
         }
 
-        if (copyError != null) {
-            // Log errors.
-            Log.e(TAG, "Error while copying " + srcUri.toString(), copyError);
-            try {
-                mFailedFiles.add(DocumentInfo.fromUri(getContentResolver(), srcUri));
-            } catch (FileNotFoundException ignore) {
-                Log.w(TAG, "Source file gone: " + srcUri, copyError);
-                // The source file is gone.
-            }
-        }
 
         if (copyError != null || mIsCancelled) {
             // Clean up half-copied files.
diff --git a/packages/DocumentsUI/src/com/android/documentsui/DirectoryFragment.java b/packages/DocumentsUI/src/com/android/documentsui/DirectoryFragment.java
index c28806b..edf829d 100644
--- a/packages/DocumentsUI/src/com/android/documentsui/DirectoryFragment.java
+++ b/packages/DocumentsUI/src/com/android/documentsui/DirectoryFragment.java
@@ -61,6 +61,8 @@
 import android.os.SystemProperties;
 import android.provider.DocumentsContract;
 import android.provider.DocumentsContract.Document;
+import android.support.annotation.Nullable;
+import android.support.design.widget.Snackbar;
 import android.support.v7.widget.GridLayoutManager;
 import android.support.v7.widget.LinearLayoutManager;
 import android.support.v7.widget.RecyclerView;
@@ -73,6 +75,7 @@
 import android.text.format.Time;
 import android.util.Log;
 import android.util.SparseArray;
+import android.util.SparseBooleanArray;
 import android.view.ActionMode;
 import android.view.DragEvent;
 import android.view.GestureDetector;
@@ -83,6 +86,7 @@
 import android.view.View;
 import android.view.View.OnLayoutChangeListener;
 import android.view.ViewGroup;
+import android.view.ViewParent;
 import android.widget.ImageView;
 import android.widget.TextView;
 import android.widget.Toast;
@@ -96,7 +100,6 @@
 import com.android.documentsui.model.DocumentStack;
 import com.android.documentsui.model.RootInfo;
 import com.android.internal.util.Preconditions;
-
 import com.google.common.collect.Lists;
 
 import java.util.ArrayList;
@@ -129,6 +132,8 @@
     private static final String EXTRA_QUERY = "query";
     private static final String EXTRA_IGNORE_STATE = "ignoreState";
 
+    private final Model mModel = new Model();
+
     private final Handler mHandler = new Handler(Looper.getMainLooper());
 
     private View mEmptyView;
@@ -147,7 +152,6 @@
     private LoaderCallbacks<DirectoryResult> mCallbacks;
     private FragmentTuner mFragmentTuner;
     private DocumentClipper mClipper;
-    private MultiSelectManager mSelectionManager;
     // These are lazily initialized.
     private LinearLayoutManager mListLayout;
     private GridLayoutManager mGridLayout;
@@ -261,7 +265,7 @@
         }
 
         // Clear any outstanding selection
-        mSelectionManager.clearSelection();
+        mModel.clearSelection();
     }
 
     @Override
@@ -290,14 +294,17 @@
                     }
                 };
 
-        mSelectionManager = new MultiSelectManager(
+        // TODO: instead of inserting the view into the constructor, extract listener-creation code
+        // and set the listener on the view after the fact.  Then the view doesn't need to be passed
+        // into the selection manager which is passed into the model.
+        MultiSelectManager selMgr= new MultiSelectManager(
                 mRecView,
                 listener,
                 state.allowMultiple
                     ? MultiSelectManager.MODE_MULTIPLE
                     : MultiSelectManager.MODE_SINGLE);
-
-        mSelectionManager.addCallback(new SelectionModeListener());
+        selMgr.addCallback(new SelectionModeListener());
+        mModel.setSelectionManager(selMgr);
 
         mType = getArguments().getInt(EXTRA_TYPE);
         mStateKey = buildStateKey(root, doc);
@@ -367,7 +374,9 @@
 
                 if (!isAdded()) return;
 
-                mAdapter.replaceResult(result);
+                // TODO: make the adapter listen to the model
+                mModel.update(result);
+                mAdapter.update();
 
                 // Push latest state up to UI
                 // TODO: if mode change was racing with us, don't overwrite it
@@ -380,7 +389,7 @@
                 updateDisplayState();
 
                 // When launched into empty recents, show drawer
-                if (mType == TYPE_RECENT_OPEN && mAdapter.isEmpty() && !state.stackTouched &&
+                if (mType == TYPE_RECENT_OPEN && mModel.isEmpty() && !state.stackTouched &&
                         context instanceof DocumentsActivity) {
                     ((DocumentsActivity) context).setRootsDrawerOpen(true);
                 }
@@ -398,7 +407,9 @@
 
             @Override
             public void onLoaderReset(Loader<DirectoryResult> loader) {
-                mAdapter.replaceResult(null);
+                // TODO: make the adapter listen to the model.
+                mModel.update(null);
+                mAdapter.update();
             }
         };
 
@@ -433,7 +444,7 @@
     }
 
     private boolean onSingleTapUp(MotionEvent e) {
-        if (Events.isTouchEvent(e) && mSelectionManager.getSelection().isEmpty()) {
+        if (Events.isTouchEvent(e) && mModel.getSelection().isEmpty()) {
             int position = getEventAdapterPosition(e);
             if (position != RecyclerView.NO_POSITION) {
                 return handleViewItem(position);
@@ -454,14 +465,14 @@
     }
 
     private boolean handleViewItem(int position) {
-        final Cursor cursor = mAdapter.getItem(position);
+        final Cursor cursor = mModel.getItem(position);
         checkNotNull(cursor, "Cursor cannot be null.");
         final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
         final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
         if (isDocumentEnabled(docMimeType, docFlags)) {
             final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
-            ((BaseActivity) getActivity()).onDocumentPicked(doc, mAdapter);
-            mSelectionManager.clearSelection();
+            ((BaseActivity) getActivity()).onDocumentPicked(doc, mModel);
+            mModel.clearSelection();
             return true;
         }
         return false;
@@ -565,6 +576,9 @@
         }
 
         mRecView.setLayoutManager(layout);
+        // TODO: Once b/23691541 is resolved, use a listener within MultiSelectManager instead of
+        // imperatively calling this function.
+        mModel.mSelectionManager.handleLayoutChanged();
         // setting layout manager automatically invalidates existing ViewHolders.
         mThumbSize = new Point(thumbSize, thumbSize);
     }
@@ -598,7 +612,7 @@
         public boolean onBeforeItemStateChange(int position, boolean selected) {
             // Directories and footer items cannot be checked
             if (selected) {
-                final Cursor cursor = mAdapter.getItem(position);
+                final Cursor cursor = mModel.getItem(position);
                 checkNotNull(cursor, "Cursor cannot be null.");
                 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
                 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
@@ -610,7 +624,7 @@
         @Override
         public void onItemStateChanged(int position, boolean selected) {
 
-            final Cursor cursor = mAdapter.getItem(position);
+            final Cursor cursor = mModel.getItem(position);
             checkNotNull(cursor, "Cursor cannot be null.");
 
             final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
@@ -621,7 +635,7 @@
 
         @Override
         public void onSelectionChanged() {
-            mSelectionManager.getSelection(mSelected);
+            mModel.getSelection(mSelected);
             if (mSelected.size() > 0) {
                 if (DEBUG) Log.d(TAG, "Maybe starting action mode.");
                 if (mActionMode == null) {
@@ -651,7 +665,7 @@
             if (DEBUG) Log.d(TAG, "Handling action mode destroyed.");
             mActionMode = null;
             // clear selection
-            mSelectionManager.clearSelection();
+            mModel.clearSelection();
             mSelected.clear();
             mNoDeleteCount = 0;
         }
@@ -659,8 +673,8 @@
         @Override
         public boolean onCreateActionMode(ActionMode mode, Menu menu) {
             mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
-            mode.setTitle(TextUtils.formatSelectedCount(mSelectionManager.getSelection().size()));
-            return mSelectionManager.getSelection().size() > 0;
+            mode.setTitle(TextUtils.formatSelectedCount(mModel.getSelection().size()));
+            return mModel.getSelection().size() > 0;
         }
 
         @Override
@@ -679,8 +693,7 @@
         @Override
         public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
 
-            Selection selection = new Selection();
-            mSelectionManager.getSelection(selection);
+            Selection selection = mModel.getSelection(new Selection());
 
             final int id = item.getItemId();
             if (id == R.id.menu_open) {
@@ -793,41 +806,33 @@
     }
 
     private void deleteDocuments(final Selection selected) {
-        final Context context = getActivity();
-        final ContentResolver resolver = context.getContentResolver();
+        Context context = getActivity();
+        ContentResolver resolver = context.getContentResolver();
+        String message = Shared.getQuantityString(context, R.plurals.deleting, selected.size());
 
-        new GetDocumentsTask() {
-            @Override
-            void onDocumentsReady(List<DocumentInfo> docs) {
-                boolean hadTrouble = false;
-                for (DocumentInfo doc : docs) {
-                    if (!doc.isDeleteSupported()) {
-                        Log.w(TAG, "Skipping " + doc);
-                        hadTrouble = true;
-                        continue;
-                    }
+        mModel.markForDeletion(selected);
 
-                    ContentProviderClient client = null;
-                    try {
-                        client = DocumentsApplication.acquireUnstableProviderOrThrow(
-                                resolver, doc.derivedUri.getAuthority());
-                        DocumentsContract.deleteDocument(client, doc.derivedUri);
-                    } catch (Exception e) {
-                        Log.w(TAG, "Failed to delete " + doc);
-                        hadTrouble = true;
-                    } finally {
-                        ContentProviderClient.releaseQuietly(client);
-                    }
-                }
-
-                if (hadTrouble) {
-                    Toast.makeText(
-                            context,
-                            R.string.toast_failed_delete,
-                            Toast.LENGTH_SHORT).show();
-                }
-            }
-        }.execute(selected);
+        Activity activity = getActivity();
+        Snackbar.make(this.getView(), message, Snackbar.LENGTH_LONG)
+                .setAction(
+                        R.string.undo,
+                        new android.view.View.OnClickListener() {
+                            @Override
+                            public void onClick(View view) {}
+                        })
+                .setCallback(
+                        new Snackbar.Callback() {
+                            @Override
+                            public void onDismissed(Snackbar snackbar, int event) {
+                                if (event == Snackbar.Callback.DISMISS_EVENT_ACTION) {
+                                    mModel.undoDeletion();
+                                } else {
+                                    mModel.finalizeDeletion();
+                                }
+                                ;
+                            }
+                        })
+                .show();
     }
 
     private void transferDocuments(final Selection selected, final int mode) {
@@ -948,50 +953,31 @@
         }
     }
 
-    private final class DocumentsAdapter extends RecyclerView.Adapter<DocumentHolder>
-            implements DocumentContext {
+    private final class DocumentsAdapter extends RecyclerView.Adapter<DocumentHolder> {
 
         private final Context mContext;
         private final LayoutInflater mInflater;
         // TODO: Bring back support for footers.
         private final List<Footer> mFooters = new ArrayList<>();
 
-        private Cursor mCursor;
-        private int mCursorCount;
-
         public DocumentsAdapter(Context context) {
             mContext = context;
             mInflater = LayoutInflater.from(context);
         }
 
-        public void replaceResult(DirectoryResult result) {
-            if (DEBUG) Log.i(TAG, "Updating adapter with new result set.");
-            mCursor = result != null ? result.cursor : null;
-            mCursorCount = mCursor != null ? mCursor.getCount() : 0;
-
+        public void update() {
             mFooters.clear();
-
-            final Bundle extras = mCursor != null ? mCursor.getExtras() : null;
-            if (extras != null) {
-                final String info = extras.getString(DocumentsContract.EXTRA_INFO);
-                if (info != null) {
-                    mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, info));
-                }
-                final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
-                if (error != null) {
-                    mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error));
-                }
-                if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
-                    mFooters.add(new LoadingFooter());
-                }
+            if (mModel.info != null) {
+                mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, mModel.info));
+            }
+            if (mModel.error != null) {
+                mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, mModel.error));
+            }
+            if (mModel.isLoading()) {
+                mFooters.add(new LoadingFooter());
             }
 
-            if (result != null && result.exception != null) {
-                mFooters.add(new MessageFooter(
-                        3, R.drawable.ic_dialog_alert, getString(R.string.query_error)));
-            }
-
-            if (isEmpty()) {
+            if (mModel.isEmpty()) {
                 mEmptyView.setVisibility(View.VISIBLE);
             } else {
                 mEmptyView.setVisibility(View.GONE);
@@ -1025,7 +1011,7 @@
             final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
                     context, mThumbSize);
 
-            final Cursor cursor = getItem(position);
+            final Cursor cursor = mModel.getItem(position);
             checkNotNull(cursor, "Cursor cannot be null.");
 
             final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
@@ -1041,7 +1027,7 @@
 
             holder.docId = docId;
             final View itemView = holder.view;
-            itemView.setActivated(mSelectionManager.getSelection().contains(position));
+            itemView.setActivated(mModel.isSelected(position));
 
             final View line1 = itemView.findViewById(R.id.line1);
             final View line2 = itemView.findViewById(R.id.line2);
@@ -1214,44 +1200,21 @@
         }
 
         @Override
-        public Cursor getCursor() {
-            if (Looper.myLooper() != Looper.getMainLooper()) {
-                throw new IllegalStateException("Can't call getCursor from non-main thread.");
-            }
-            return mCursor;
-        }
-
-        private Cursor getItem(int position) {
-            if (position < mCursorCount) {
-                mCursor.moveToPosition(position);
-                return mCursor;
-            }
-
-            Log.w(TAG, "Returning null cursor for position: " + position);
-            if (DEBUG) Log.d(TAG, "...Adapter size: " + mCursorCount);
-            if (DEBUG) Log.d(TAG, "...Footer size: " + mFooters.size());
-            return null;
-        }
-
-        @Override
         public int getItemCount() {
-            return mCursorCount;
+            return mModel.getItemCount();
             // return mCursorCount + mFooters.size();
         }
 
         @Override
         public int getItemViewType(int position) {
-            if (position < mCursorCount) {
+            final int itemCount = mModel.getItemCount();
+            if (position < itemCount) {
                 return 0;
             } else {
-                position -= mCursorCount;
+                position -= itemCount;
                 return mFooters.get(position).getItemViewType();
             }
         }
-
-        private boolean isEmpty() {
-            return getItemCount() > 0;
-        }
     }
 
     private static String formatTime(Context context, long when) {
@@ -1328,27 +1291,6 @@
         return MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
     }
 
-    private List<DocumentInfo> getSelectedDocuments() {
-        Selection sel = mSelectionManager.getSelection(new Selection());
-        return getItemsAsDocuments(sel);
-    }
-
-    private List<DocumentInfo> getItemsAsDocuments(Selection items) {
-        if (items == null || items.size() == 0) {
-            return new ArrayList<>(0);
-        }
-
-        final List<DocumentInfo> docs =  new ArrayList<>(items.size());
-        final int size = items.size();
-        for (int i = 0; i < size; i++) {
-            final Cursor cursor = mAdapter.getItem(items.get(i));
-            checkNotNull(cursor, "Cursor cannot be null.");
-            final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
-            docs.add(doc);
-        }
-        return docs;
-    }
-
     private void copyFromClipboard() {
         new AsyncTask<Void, Void, List<DocumentInfo>>() {
 
@@ -1426,7 +1368,7 @@
     }
 
     void copySelectedToClipboard() {
-        Selection sel = mSelectionManager.getSelection(new Selection());
+        Selection sel = mModel.getSelection(new Selection());
         copySelectionToClipboard(sel);
     }
 
@@ -1458,7 +1400,7 @@
      * @return true if the list of files can be copied to destination.
      */
     boolean canCopy(List<DocumentInfo> files, DocumentInfo dest) {
-        BaseActivity activity = (BaseActivity)getActivity();
+        BaseActivity activity = (BaseActivity) getActivity();
 
         final RootInfo root = activity.getCurrentRoot();
 
@@ -1475,7 +1417,7 @@
     }
 
     void selectAllFiles() {
-        boolean changed = mSelectionManager.setItemsSelected(0, mAdapter.getItemCount(), true);
+        boolean changed = mModel.selectAll();
         if (changed) {
             updateDisplayState();
         }
@@ -1518,10 +1460,10 @@
                     return true;
 
                 case DragEvent.ACTION_DROP:
-                    int dstPosition = mRecView.getChildAdapterPosition(v);
+                    int dstPosition = mRecView.getChildAdapterPosition(getContainingItemView(v));
                     DocumentInfo dstDir = null;
                     if (dstPosition != android.widget.AdapterView.INVALID_POSITION) {
-                        Cursor dstCursor = mAdapter.getItem(dstPosition);
+                        Cursor dstCursor = mModel.getItem(dstPosition);
                         checkNotNull(dstCursor, "Cursor cannot be null.");
                         dstDir = DocumentInfo.fromDirectoryCursor(dstCursor);
                         // TODO: Do not drop into the directory where the documents came from.
@@ -1533,6 +1475,19 @@
         }
     };
 
+    private View getContainingItemView(View view) {
+        while (true) {
+            if (view.getLayoutParams() instanceof RecyclerView.LayoutParams) {
+                return view;
+            }
+            ViewParent parent = view.getParent();
+            if (parent == null || !(parent instanceof View)) {
+                return null;
+            }
+            view = (View) parent;
+        }
+    }
+
     private View.OnLongClickListener mLongClickListener = new View.OnLongClickListener() {
         @Override
         public boolean onLongClick(View v) {
@@ -1551,21 +1506,21 @@
     };
 
     private List<DocumentInfo> getDraggableDocuments(View currentItemView) {
-        int position = mRecView.getChildAdapterPosition(currentItemView);
+        int position = mRecView.getChildAdapterPosition(getContainingItemView(currentItemView));
         if (position == android.widget.AdapterView.INVALID_POSITION) {
             return Collections.EMPTY_LIST;
         }
 
-        final List<DocumentInfo> selectedDocs = getSelectedDocuments();
+        final List<DocumentInfo> selectedDocs = mModel.getSelectedDocuments();
         if (!selectedDocs.isEmpty()) {
-            if (!mSelectionManager.getSelection().contains(position)) {
+            if (!mModel.isSelected(position)) {
                 // There is a selection that does not include the current item, drag nothing.
                 return Collections.EMPTY_LIST;
             }
             return selectedDocs;
         }
 
-        final Cursor cursor = mAdapter.getItem(position);
+        final Cursor cursor = mModel.getItem(position);
         checkNotNull(cursor, "Cursor cannot be null.");
         final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
 
@@ -1671,12 +1626,14 @@
             mShadow.setBounds(0, 0, mShadowDimension, mShadowDimension);
         }
 
+        @Override
         public void onProvideShadowMetrics(
                 Point shadowSize, Point shadowTouchPoint) {
             shadowSize.set(mShadowDimension, mShadowDimension);
             shadowTouchPoint.set(mShadowDimension / 2, mShadowDimension / 2);
         }
 
+        @Override
         public void onDrawShadow(Canvas canvas) {
             mShadow.draw(canvas);
         }
@@ -1706,7 +1663,7 @@
             extends AsyncTask<Selection, Void, List<DocumentInfo>> {
         @Override
         protected final List<DocumentInfo> doInBackground(Selection... selected) {
-            return getItemsAsDocuments(selected[0]);
+            return mModel.getDocuments(selected[0]);
         }
 
         @Override
@@ -1775,4 +1732,269 @@
         @Override
         public void afterActivityCreated(DirectoryFragment fragment) {}
     }
+
+    /**
+     * The data model for the current loaded directory.
+     */
+    private final class Model implements DocumentContext {
+        private MultiSelectManager mSelectionManager;
+        private int mCursorCount;
+        private boolean mIsLoading;
+        @Nullable private Cursor mCursor;
+        @Nullable private String info;
+        @Nullable private String error;
+        private SparseBooleanArray mMarkedForDeletion = new SparseBooleanArray();
+
+        /**
+         * Sets the selection manager used by the model.
+         * TODO: the model should instantiate the selection manager.  See onActivityCreated.
+         */
+        void setSelectionManager(MultiSelectManager mgr) {
+            mSelectionManager = mgr;
+        }
+
+        /**
+         * Selects all files in the current directory.
+         * @return true if the selection state changed for any files.
+         */
+        boolean selectAll() {
+            return mSelectionManager.setItemsSelected(0, mCursorCount, true);
+        }
+
+        /**
+         * Clones the current selection into the given Selection object.
+         * @param selection
+         * @return The selection that was passed in, for convenience.
+         */
+        Selection getSelection(Selection selection) {
+            return mSelectionManager.getSelection(selection);
+        }
+
+        /**
+         * @return The current selection (the live instance, not a copy).
+         */
+        Selection getSelection() {
+            return mSelectionManager.getSelection();
+        }
+
+        boolean isSelected(int position) {
+            return mSelectionManager.getSelection().contains(position);
+        }
+
+        void clearSelection() {
+            mSelectionManager.clearSelection();
+        }
+
+        void update(DirectoryResult result) {
+            if (DEBUG) Log.i(TAG, "Updating model with new result set.");
+
+            if (result == null) {
+                mCursor = null;
+                mCursorCount = 0;
+                info = null;
+                error = null;
+                mIsLoading = false;
+                return;
+            }
+
+            if (result.exception != null) {
+                Log.e(TAG, "Error while loading directory contents", result.exception);
+                error = getString(R.string.query_error);
+                return;
+            }
+
+            mCursor = result.cursor;
+            mCursorCount = mCursor.getCount();
+
+            final Bundle extras = mCursor.getExtras();
+            if (extras != null) {
+                info = extras.getString(DocumentsContract.EXTRA_INFO);
+                error = extras.getString(DocumentsContract.EXTRA_ERROR);
+                mIsLoading = extras.getBoolean(DocumentsContract.EXTRA_LOADING, false);
+            }
+        }
+
+        private int getItemCount() {
+            return mCursorCount - mMarkedForDeletion.size();
+        }
+
+        private Cursor getItem(int position) {
+            // Items marked for deletion are masked out of the UI.  To do this, for every marked
+            // item whose position is less than the requested item position, advance the requested
+            // position by 1.
+            final int originalPos = position;
+            final int size = mMarkedForDeletion.size();
+            for (int i = 0; i <= size; ++i) {
+                // It'd be more concise, but less efficient, to iterate over positions while calling
+                // mMarkedForDeletion.get.  Instead, iterate over deleted entries.
+                if (mMarkedForDeletion.keyAt(i) <= position && mMarkedForDeletion.valueAt(i)) {
+                    ++position;
+                }
+            }
+
+            if (DEBUG) {
+                Log.d(TAG, "Item position adjusted for deletion.  Original: " + originalPos
+                        + "  Adjusted: " + position);
+            }
+
+            if (position >= mCursorCount) {
+                throw new IndexOutOfBoundsException("Attempt to retrieve " + position + " of " +
+                        mCursorCount + " items");
+            }
+
+            mCursor.moveToPosition(position);
+            return mCursor;
+        }
+
+        private boolean isEmpty() {
+            return mCursorCount == 0;
+        }
+
+        private boolean isLoading() {
+            return mIsLoading;
+        }
+
+        private List<DocumentInfo> getSelectedDocuments() {
+            Selection sel = getSelection(new Selection());
+            return getDocuments(sel);
+        }
+
+        private List<DocumentInfo> getDocuments(Selection items) {
+            final int size = (items != null) ? items.size() : 0;
+
+            final List<DocumentInfo> docs =  new ArrayList<>(size);
+            for (int i = 0; i < size; i++) {
+                final Cursor cursor = getItem(items.get(i));
+                checkNotNull(cursor, "Cursor cannot be null.");
+                final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
+                docs.add(doc);
+            }
+            return docs;
+        }
+
+        @Override
+        public Cursor getCursor() {
+            if (Looper.myLooper() != Looper.getMainLooper()) {
+                throw new IllegalStateException("Can't call getCursor from non-main thread.");
+            }
+            return mCursor;
+        }
+
+        private List<DocumentInfo> getDocumentsMarkedForDeletion() {
+            final int size = mMarkedForDeletion.size();
+            List<DocumentInfo> docs =  new ArrayList<>(size);
+
+            for (int i = 0; i < size; ++i) {
+                final int position = mMarkedForDeletion.keyAt(i);
+                checkState(position < mCursorCount);
+                mCursor.moveToPosition(position);
+                final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(mCursor);
+                docs.add(doc);
+            }
+            return docs;
+        }
+
+        /**
+         * Marks the given files for deletion. This will remove them from the UI. Clients must then
+         * call either {@link #undoDeletion()} or {@link #finalizeDeletion()} to cancel or confirm
+         * the deletion, respectively. Only one deletion operation is allowed at a time.
+         *
+         * @param selected A selection representing the files to delete.
+         */
+        public void markForDeletion(Selection selected) {
+            // Only one deletion operation at a time.
+            checkState(mMarkedForDeletion.size() == 0);
+            // There should never be more to delete than what exists.
+            checkState(mCursorCount >= selected.size());
+
+            final int size = selected.size();
+            for (int i = 0; i < size; ++i) {
+                int position = selected.get(i);
+                if (DEBUG) Log.d(TAG, "Marked position " + position + " for deletion");
+                mMarkedForDeletion.append(position, true);
+                mAdapter.notifyItemRemoved(position);
+            }
+        }
+
+        /**
+         * Cancels an ongoing deletion operation. All files currently marked for deletion will be
+         * unmarked, and restored in the UI.  See {@link #markForDeletion(Selection)}.
+         */
+        public void undoDeletion() {
+            // Iterate over deleted items, temporarily marking them false in the deletion list, and
+            // re-adding them to the UI.
+            final int size = mMarkedForDeletion.size();
+            for (int i = 0; i < size; ++i) {
+                final int position = mMarkedForDeletion.keyAt(i);
+                mMarkedForDeletion.put(position, false);
+                mAdapter.notifyItemInserted(position);
+            }
+
+            // Then, clear the deletion list.
+            mMarkedForDeletion.clear();
+        }
+
+        /**
+         * Finalizes an ongoing deletion operation. All files currently marked for deletion will be
+         * deleted.  See {@link #markForDeletion(Selection)}.
+         */
+        public void finalizeDeletion() {
+            final Context context = getActivity();
+            final ContentResolver resolver = context.getContentResolver();
+            new DeleteFilesTask(resolver).execute();
+        }
+
+        /**
+         * A Task which collects the DocumentInfo for documents that have been marked for deletion,
+         * and actually deletes them.
+         */
+        private class DeleteFilesTask extends AsyncTask<Void, Void, List<DocumentInfo>> {
+            private ContentResolver mResolver;
+
+            public DeleteFilesTask(ContentResolver resolver) {
+                mResolver = resolver;
+            }
+
+            @Override
+            protected List<DocumentInfo> doInBackground(Void... params) {
+                return getDocumentsMarkedForDeletion();
+            }
+
+            @Override
+            protected void onPostExecute(List<DocumentInfo> docs) {
+                boolean hadTrouble = false;
+                for (DocumentInfo doc : docs) {
+                    if (!doc.isDeleteSupported()) {
+                        Log.w(TAG, doc + " could not be deleted.  Skipping...");
+                        hadTrouble = true;
+                        continue;
+                    }
+
+                    ContentProviderClient client = null;
+                    try {
+                        if (DEBUG) Log.d(TAG, "Deleting: " + doc.displayName);
+                        client = DocumentsApplication.acquireUnstableProviderOrThrow(
+                            mResolver, doc.derivedUri.getAuthority());
+                        DocumentsContract.deleteDocument(client, doc.derivedUri);
+                    } catch (Exception e) {
+                        Log.w(TAG, "Failed to delete " + doc);
+                        hadTrouble = true;
+                    } finally {
+                        ContentProviderClient.releaseQuietly(client);
+                    }
+                }
+
+                if (hadTrouble) {
+                    // TODO show which files failed?
+                    Snackbar.make(DirectoryFragment.this.getView(),
+                       R.string.toast_failed_delete,
+                       Snackbar.LENGTH_LONG).show();
+                    if (DEBUG) Log.d(TAG, "Deletion task completed.  Some deletions failed.");
+                } else {
+                    if (DEBUG) Log.d(TAG, "Deletion task completed successfully.");
+                }
+                mMarkedForDeletion.clear();
+            }
+        }
+    }
 }
diff --git a/packages/DocumentsUI/src/com/android/documentsui/MultiSelectManager.java b/packages/DocumentsUI/src/com/android/documentsui/MultiSelectManager.java
index e972566..30a5a47 100644
--- a/packages/DocumentsUI/src/com/android/documentsui/MultiSelectManager.java
+++ b/packages/DocumentsUI/src/com/android/documentsui/MultiSelectManager.java
@@ -184,13 +184,13 @@
 
     /**
      * Returns a Selection object that provides a live view
-     * on the current selection. Callers wishing to get
+     * on the current selection.
      *
-     * @see #getSelectionSnapshot() on how to get a snapshot
+     * @see #getSelection(Selection) on how to get a snapshot
      *     of the selection that will not reflect future changes
      *     to selection.
      *
-     * @return The current seleciton.
+     * @return The current selection.
      */
     public Selection getSelection() {
         return mSelection;
@@ -250,6 +250,10 @@
         notifySelectionChanged();
     }
 
+    public void handleLayoutChanged() {
+        mBandSelectManager.handleLayoutChanged();
+    }
+
     /**
      * Clears the selection, without notifying anyone.
      */
@@ -335,7 +339,7 @@
         }
 
         handlePositionChanged(position, metaState);
-        return false;
+        return true;
     }
 
     /**
@@ -1192,6 +1196,18 @@
         }
 
         /**
+         * Handle a change in layout by cleaning up and getting rid of the old model and creating
+         * a new model which will track the new layout.
+         */
+        public void handleLayoutChanged() {
+            mModel.removeOnSelectionChangedListener(this);
+            mModel.stopListening();
+
+            mModel = new BandSelectModel((RuntimeRecyclerViewHelper) mHelper);
+            mModel.addOnSelectionChangedListener(this);
+        }
+
+        /**
          * Processes a MotionEvent by starting, ending, or resizing the band select overlay.
          * @param e
          */
diff --git a/packages/DocumentsUI/src/com/android/documentsui/Shared.java b/packages/DocumentsUI/src/com/android/documentsui/Shared.java
index b414ee3..2bf0562 100644
--- a/packages/DocumentsUI/src/com/android/documentsui/Shared.java
+++ b/packages/DocumentsUI/src/com/android/documentsui/Shared.java
@@ -16,9 +16,18 @@
 
 package com.android.documentsui;
 
+import android.content.Context;
+
 /**
  * @hide
  */
 public final class Shared {
     public static final String TAG = "Documents";
+
+    /**
+     * Generates a formatted quantity string.
+     */
+    public static final String getQuantityString(Context context, int resourceId, int quantity) {
+        return context.getResources().getQuantityString(resourceId, quantity, quantity);
+    }
 }
diff --git a/packages/Keyguard/src/com/android/keyguard/KeyguardPinBasedInputView.java b/packages/Keyguard/src/com/android/keyguard/KeyguardPinBasedInputView.java
index ce2d11a..f51e10f 100644
--- a/packages/Keyguard/src/com/android/keyguard/KeyguardPinBasedInputView.java
+++ b/packages/Keyguard/src/com/android/keyguard/KeyguardPinBasedInputView.java
@@ -64,7 +64,7 @@
 
     @Override
     protected void resetState() {
-        mPasswordEntry.setEnabled(true);
+        setPasswordEntryEnabled(true);
     }
 
     @Override
diff --git a/packages/Keyguard/src/com/android/keyguard/KeyguardUpdateMonitor.java b/packages/Keyguard/src/com/android/keyguard/KeyguardUpdateMonitor.java
index fc6117f..b752c9b 100644
--- a/packages/Keyguard/src/com/android/keyguard/KeyguardUpdateMonitor.java
+++ b/packages/Keyguard/src/com/android/keyguard/KeyguardUpdateMonitor.java
@@ -29,6 +29,7 @@
 import android.content.IntentFilter;
 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;
@@ -472,6 +473,10 @@
         }
     }
 
+    private void handleFingerprintLockoutReset() {
+        updateFingerprintListeningState();
+    }
+
     private void setFingerprintRunningState(int fingerprintRunningState) {
         boolean wasRunning = mFingerprintRunningState == FINGERPRINT_STATE_RUNNING;
         boolean isRunning = fingerprintRunningState == FINGERPRINT_STATE_RUNNING;
@@ -681,6 +686,14 @@
         }
     };
 
+    private final FingerprintManager.LockoutResetCallback mLockoutResetCallback
+            = new FingerprintManager.LockoutResetCallback() {
+        @Override
+        public void onLockoutReset() {
+            handleFingerprintLockoutReset();
+        }
+    };
+
     private FingerprintManager.AuthenticationCallback mAuthenticationCallback
             = new AuthenticationCallback() {
 
@@ -1003,6 +1016,9 @@
 
         mFpm = (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
         updateFingerprintListeningState();
+        if (mFpm != null) {
+            mFpm.addLockoutResetCallback(mLockoutResetCallback);
+        }
     }
 
     private void updateFingerprintListeningState() {
@@ -1332,9 +1348,7 @@
      */
     private void handleKeyguardReset() {
         if (DEBUG) Log.d(TAG, "handleKeyguardReset");
-        if (!isUnlockingWithFingerprintAllowed()) {
-            updateFingerprintListeningState();
-        }
+        updateFingerprintListeningState();
     }
 
     /**
diff --git a/packages/MtpDocumentsProvider/src/com/android/mtp/CursorHelper.java b/packages/MtpDocumentsProvider/src/com/android/mtp/CursorHelper.java
new file mode 100644
index 0000000..b5694b7
--- /dev/null
+++ b/packages/MtpDocumentsProvider/src/com/android/mtp/CursorHelper.java
@@ -0,0 +1,100 @@
+/*
+ * 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.mtp;
+
+import android.database.MatrixCursor;
+import android.mtp.MtpConstants;
+import android.mtp.MtpObjectInfo;
+import android.provider.DocumentsContract;
+import android.provider.DocumentsContract.Document;
+
+import java.util.Date;
+
+final class CursorHelper {
+    static final int DUMMY_HANDLE_FOR_ROOT = 0;
+
+    private CursorHelper() {
+    }
+
+    static void addToCursor(MtpRoot root, MatrixCursor.RowBuilder builder) {
+        final Identifier identifier = new Identifier(
+                root.mDeviceId, root.mStorageId, DUMMY_HANDLE_FOR_ROOT);
+        builder.add(Document.COLUMN_DOCUMENT_ID, identifier.toDocumentId());
+        builder.add(Document.COLUMN_DISPLAY_NAME, root.mDescription);
+        builder.add(Document.COLUMN_MIME_TYPE, DocumentsContract.Document.MIME_TYPE_DIR);
+        builder.add(Document.COLUMN_LAST_MODIFIED, null);
+        builder.add(Document.COLUMN_FLAGS, 0);
+        builder.add(Document.COLUMN_SIZE,
+                (int) Math.min(root.mMaxCapacity - root.mFreeSpace, Integer.MAX_VALUE));
+    }
+
+    static void addToCursor(MtpObjectInfo objectInfo, Identifier rootIdentifier,
+            MatrixCursor.RowBuilder builder) {
+        final Identifier identifier = new Identifier(
+                rootIdentifier.mDeviceId, rootIdentifier.mStorageId, objectInfo.getObjectHandle());
+        final String mimeType = formatTypeToMimeType(objectInfo.getFormat());
+
+        int flag = 0;
+        if (objectInfo.getProtectionStatus() == 0) {
+            flag |= DocumentsContract.Document.FLAG_SUPPORTS_DELETE |
+                    DocumentsContract.Document.FLAG_SUPPORTS_WRITE;
+            if (mimeType == DocumentsContract.Document.MIME_TYPE_DIR) {
+                flag |= DocumentsContract.Document.FLAG_DIR_SUPPORTS_CREATE;
+            }
+        }
+        if (objectInfo.getThumbCompressedSize() > 0) {
+            flag |= DocumentsContract.Document.FLAG_SUPPORTS_THUMBNAIL;
+        }
+
+        builder.add(Document.COLUMN_DOCUMENT_ID, identifier.toDocumentId());
+        builder.add(Document.COLUMN_DISPLAY_NAME, objectInfo.getName());
+        builder.add(Document.COLUMN_MIME_TYPE, mimeType);
+        builder.add(
+                Document.COLUMN_LAST_MODIFIED,
+                objectInfo.getDateModified() != 0 ? objectInfo.getDateModified() : null);
+        builder.add(Document.COLUMN_FLAGS, flag);
+        builder.add(Document.COLUMN_SIZE, objectInfo.getCompressedSize());
+    }
+
+    static String formatTypeToMimeType(int format) {
+        // TODO: Add complete list of mime types.
+        switch (format) {
+            case MtpConstants.FORMAT_ASSOCIATION:
+                return DocumentsContract.Document.MIME_TYPE_DIR;
+            case MtpConstants.FORMAT_MP3:
+                return "audio/mp3";
+            case MtpConstants.FORMAT_EXIF_JPEG:
+                return "image/jpeg";
+            default:
+                return "application/octet-stream";
+        }
+    }
+
+    static int mimeTypeToFormatType(String mimeType) {
+        // TODO: Add complete list of mime types.
+        switch (mimeType.toLowerCase()) {
+            case Document.MIME_TYPE_DIR:
+                return MtpConstants.FORMAT_ASSOCIATION;
+            case "audio/mp3":
+                return MtpConstants.FORMAT_MP3;
+            case "image/jpeg":
+                return MtpConstants.FORMAT_EXIF_JPEG;
+            default:
+                return MtpConstants.FORMAT_UNDEFINED;
+        }
+    }
+}
diff --git a/packages/MtpDocumentsProvider/src/com/android/mtp/DocumentLoader.java b/packages/MtpDocumentsProvider/src/com/android/mtp/DocumentLoader.java
index c430def..d4c4331 100644
--- a/packages/MtpDocumentsProvider/src/com/android/mtp/DocumentLoader.java
+++ b/packages/MtpDocumentsProvider/src/com/android/mtp/DocumentLoader.java
@@ -19,6 +19,7 @@
 import android.content.ContentResolver;
 import android.database.Cursor;
 import android.database.MatrixCursor;
+import android.mtp.MtpObjectInfo;
 import android.net.Uri;
 import android.os.Bundle;
 import android.os.Process;
@@ -34,6 +35,7 @@
  * Loader for MTP document.
  * At the first request, the loader returns only first NUM_INITIAL_ENTRIES. Then it launches
  * background thread to load the rest documents and caches its result for next requests.
+ * TODO: Rename this class to ObjectInfoLoader
  */
 class DocumentLoader {
     static final int NUM_INITIAL_ENTRIES = 10;
@@ -50,13 +52,13 @@
         mResolver = resolver;
     }
 
-    private static MtpDocument[] loadDocuments(MtpManager manager, int deviceId, int[] handles)
+    private static MtpObjectInfo[] loadDocuments(MtpManager manager, int deviceId, int[] handles)
             throws IOException {
-        final MtpDocument[] documents = new MtpDocument[handles.length];
+        final MtpObjectInfo[] objectInfos = new MtpObjectInfo[handles.length];
         for (int i = 0; i < handles.length; i++) {
-            documents[i] = manager.getDocument(deviceId, handles[i]);
+            objectInfos[i] = manager.getObjectInfo(deviceId, handles[i]);
         }
-        return documents;
+        return objectInfos;
     }
 
     synchronized Cursor queryChildDocuments(String[] columnNames, Identifier parent)
@@ -66,7 +68,7 @@
             int parentHandle = parent.mObjectHandle;
             // Need to pass the special value MtpManager.OBJECT_HANDLE_ROOT_CHILDREN to
             // getObjectHandles if we would like to obtain children under the root.
-            if (parentHandle == MtpDocument.DUMMY_HANDLE_FOR_ROOT) {
+            if (parentHandle == CursorHelper.DUMMY_HANDLE_FOR_ROOT) {
                 parentHandle = MtpManager.OBJECT_HANDLE_ROOT_CHILDREN;
             }
             task = new LoaderTask(parent, mMtpManager.getObjectHandles(
@@ -89,14 +91,18 @@
         return task.createCursor(mResolver, columnNames);
     }
 
-    synchronized void clearCache(int deviceId) {
+    synchronized void clearTasks(int deviceId) {
         mTaskList.clearTaskForDevice(deviceId);
     }
 
-    synchronized void clearCache() {
+    synchronized void clearCompletedTasks() {
         mTaskList.clearCompletedTask();
     }
 
+    synchronized void clearTask(Identifier parentIdentifier) {
+        mTaskList.clearTask(parentIdentifier);
+    }
+
     private class BackgroundLoaderThread extends Thread {
         @Override
         public void run() {
@@ -114,16 +120,16 @@
                     deviceId = task.mIdentifier.mDeviceId;
                     handles = task.getUnloadedObjectHandles(NUM_LOADING_ENTRIES);
                 }
-                MtpDocument[] documents;
+                MtpObjectInfo[] objectInfos;
                 try {
-                    documents = loadDocuments(mMtpManager, deviceId, handles);
+                    objectInfos = loadDocuments(mMtpManager, deviceId, handles);
                 } catch (IOException exception) {
-                    documents = null;
+                    objectInfos = null;
                     Log.d(MtpDocumentsProvider.TAG, exception.getMessage());
                 }
                 synchronized (DocumentLoader.this) {
-                    if (documents != null) {
-                        task.fillDocuments(documents);
+                    if (objectInfos != null) {
+                        task.fillDocuments(objectInfos);
                         final boolean shouldNotify =
                                 task.mLastNotified.getTime() <
                                 new Date().getTime() - NOTIFY_PERIOD_MS ||
@@ -177,19 +183,30 @@
                 }
             }
         }
+
+        void clearTask(Identifier parentIdentifier) {
+            for (int i = 0; i < size(); i++) {
+                final LoaderTask task = get(i);
+                if (task.mIdentifier.mDeviceId == parentIdentifier.mDeviceId &&
+                        task.mIdentifier.mObjectHandle == parentIdentifier.mObjectHandle) {
+                    remove(i);
+                    return;
+                }
+            }
+        }
     }
 
     private static class LoaderTask {
         final Identifier mIdentifier;
         final int[] mObjectHandles;
-        final MtpDocument[] mDocuments;
+        final MtpObjectInfo[] mObjectInfos;
         Date mLastNotified;
         int mNumLoaded;
 
         LoaderTask(Identifier identifier, int[] objectHandles) {
             mIdentifier = identifier;
             mObjectHandles = objectHandles;
-            mDocuments = new MtpDocument[mObjectHandles.length];
+            mObjectInfos = new MtpObjectInfo[mObjectHandles.length];
             mNumLoaded = 0;
             mLastNotified = new Date();
         }
@@ -199,7 +216,7 @@
             final Identifier rootIdentifier = new Identifier(
                     mIdentifier.mDeviceId, mIdentifier.mStorageId);
             for (int i = 0; i < mNumLoaded; i++) {
-                mDocuments[i].addToCursor(rootIdentifier, cursor.newRow());
+                CursorHelper.addToCursor(mObjectInfos[i], rootIdentifier, cursor.newRow());
             }
             final Bundle extras = new Bundle();
             extras.putBoolean(DocumentsContract.EXTRA_LOADING, !completed());
@@ -209,7 +226,7 @@
         }
 
         boolean completed() {
-            return mNumLoaded == mDocuments.length;
+            return mNumLoaded == mObjectInfos.length;
         }
 
         int[] getUnloadedObjectHandles(int count) {
@@ -224,9 +241,9 @@
             mLastNotified = new Date();
         }
 
-        void fillDocuments(MtpDocument[] documents) {
-            for (int i = 0; i < documents.length; i++) {
-                mDocuments[mNumLoaded++] = documents[i];
+        void fillDocuments(MtpObjectInfo[] objectInfos) {
+            for (int i = 0; i < objectInfos.length; i++) {
+                mObjectInfos[mNumLoaded++] = objectInfos[i];
             }
         }
 
diff --git a/packages/MtpDocumentsProvider/src/com/android/mtp/Identifier.java b/packages/MtpDocumentsProvider/src/com/android/mtp/Identifier.java
index bd0c837..ae29f52 100644
--- a/packages/MtpDocumentsProvider/src/com/android/mtp/Identifier.java
+++ b/packages/MtpDocumentsProvider/src/com/android/mtp/Identifier.java
@@ -41,7 +41,7 @@
 
 
     Identifier(int deviceId, int storageId) {
-        this(deviceId, storageId, MtpDocument.DUMMY_HANDLE_FOR_ROOT);
+        this(deviceId, storageId, CursorHelper.DUMMY_HANDLE_FOR_ROOT);
     }
 
     Identifier(int deviceId, int storageId, int objectHandle) {
diff --git a/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocument.java b/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocument.java
deleted file mode 100644
index c1d9609..0000000
--- a/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocument.java
+++ /dev/null
@@ -1,132 +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.mtp;
-
-import android.database.MatrixCursor;
-import android.mtp.MtpObjectInfo;
-import android.provider.DocumentsContract;
-import android.provider.DocumentsContract.Document;
-
-import java.util.Date;
-
-class MtpDocument {
-    static final int DUMMY_HANDLE_FOR_ROOT = 0;
-
-    private final int mObjectHandle;
-    private final int mFormat;
-    private final String mName;
-    private final Date mDateModified;
-    private final int mSize;
-    private final int mThumbSize;
-    private final boolean mReadOnly;
-
-    /**
-     * Constructor for root document.
-     */
-    MtpDocument(MtpRoot root) {
-        this(DUMMY_HANDLE_FOR_ROOT,
-             0x3001,  // Directory.
-             root.mDescription,
-             null,    // Unknown name.
-             (int) Math.min(root.mMaxCapacity - root.mFreeSpace, Integer.MAX_VALUE),
-             0,       // Total size.
-             true);   // Writable.
-    }
-
-    MtpDocument(MtpObjectInfo objectInfo) {
-        this(objectInfo.getObjectHandle(),
-             objectInfo.getFormat(),
-             objectInfo.getName(),
-             objectInfo.getDateModified() != 0 ? new Date(objectInfo.getDateModified()) : null,
-             objectInfo.getCompressedSize(),
-             objectInfo.getThumbCompressedSize(),
-             objectInfo.getProtectionStatus() != 0);
-    }
-
-    MtpDocument(int objectHandle,
-                int format,
-                String name,
-                Date dateModified,
-                int size,
-                int thumbSize,
-                boolean readOnly) {
-        this.mObjectHandle = objectHandle;
-        this.mFormat = format;
-        this.mName = name;
-        this.mDateModified = dateModified;
-        this.mSize = size;
-        this.mThumbSize = thumbSize;
-        this.mReadOnly = readOnly;
-    }
-
-    void addToCursor(Identifier rootIdentifier, MatrixCursor.RowBuilder builder) {
-        final Identifier identifier = new Identifier(
-                rootIdentifier.mDeviceId, rootIdentifier.mStorageId, mObjectHandle);
-        final String mimeType = formatTypeToMimeType(mFormat);
-
-        int flag = 0;
-        if (mObjectHandle != DUMMY_HANDLE_FOR_ROOT) {
-            if (mThumbSize > 0) {
-                flag |= DocumentsContract.Document.FLAG_SUPPORTS_THUMBNAIL;
-            }
-            if (!mReadOnly) {
-                flag |= DocumentsContract.Document.FLAG_SUPPORTS_DELETE |
-                        DocumentsContract.Document.FLAG_SUPPORTS_WRITE;
-            }
-        }
-        if (mimeType == DocumentsContract.Document.MIME_TYPE_DIR && !mReadOnly) {
-            flag |= DocumentsContract.Document.FLAG_DIR_SUPPORTS_CREATE;
-        }
-
-        builder.add(Document.COLUMN_DOCUMENT_ID, identifier.toDocumentId());
-        builder.add(Document.COLUMN_DISPLAY_NAME, mName);
-        builder.add(Document.COLUMN_MIME_TYPE, mimeType); 
-        builder.add(
-                Document.COLUMN_LAST_MODIFIED,
-                mDateModified != null ? mDateModified.getTime() : null);
-        builder.add(Document.COLUMN_FLAGS, flag);
-        builder.add(Document.COLUMN_SIZE, mSize);
-    }
-
-    static String formatTypeToMimeType(int format) {
-        // TODO: Add complete list of mime types.
-        switch (format) {
-            case 0x3001:
-                return DocumentsContract.Document.MIME_TYPE_DIR;
-            case 0x3009:
-                return "audio/mp3";
-            case 0x3801:
-                return "image/jpeg";
-            default:
-                return "application/octet-stream";
-        }
-    }
-
-    static int mimeTypeToFormatType(String mimeType) {
-        // TODO: Add complete list of mime types.
-        switch (mimeType.toLowerCase()) {
-            case Document.MIME_TYPE_DIR:
-                return 0x3001;
-            case "audio/mp3":
-                return 0x3009;
-            case "image/jpeg":
-                return 0x3801;
-            default:
-                return 0x3000;  // Undefined object.
-        }
-    }
-}
diff --git a/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocumentsProvider.java b/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocumentsProvider.java
index a3cf3e2..78ed161 100644
--- a/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocumentsProvider.java
+++ b/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocumentsProvider.java
@@ -21,11 +21,12 @@
 import android.database.Cursor;
 import android.database.MatrixCursor;
 import android.graphics.Point;
+import android.mtp.MtpObjectInfo;
 import android.os.CancellationSignal;
 import android.os.ParcelFileDescriptor;
-import android.provider.DocumentsContract;
 import android.provider.DocumentsContract.Document;
 import android.provider.DocumentsContract.Root;
+import android.provider.DocumentsContract;
 import android.provider.DocumentsProvider;
 import android.util.Log;
 
@@ -116,37 +117,37 @@
         }
         final Identifier identifier = Identifier.createFromDocumentId(documentId);
 
-        MtpDocument document = null;
-        if (identifier.mObjectHandle != MtpDocument.DUMMY_HANDLE_FOR_ROOT) {
+        if (identifier.mObjectHandle != CursorHelper.DUMMY_HANDLE_FOR_ROOT) {
+            MtpObjectInfo objectInfo;
             try {
-                document = mMtpManager.getDocument(identifier.mDeviceId, identifier.mObjectHandle);
+                objectInfo = mMtpManager.getObjectInfo(
+                        identifier.mDeviceId, identifier.mObjectHandle);
             } catch (IOException e) {
                 throw new FileNotFoundException(e.getMessage());
             }
+            final MatrixCursor cursor = new MatrixCursor(projection);
+            CursorHelper.addToCursor(
+                    objectInfo,
+                    new Identifier(identifier.mDeviceId, identifier.mStorageId),
+                    cursor.newRow());
+            return cursor;
         } else {
             MtpRoot[] roots;
             try {
                 roots = mMtpManager.getRoots(identifier.mDeviceId);
-                if (roots != null) {
-                    for (final MtpRoot root : roots) {
-                        if (identifier.mStorageId == root.mStorageId) {
-                            document = new MtpDocument(root);
-                            break;
-                        }
-                    }
-                }
-                if (document == null) {
-                    throw new FileNotFoundException();
-                }
             } catch (IOException e) {
                 throw new FileNotFoundException(e.getMessage());
             }
+            for (final MtpRoot root : roots) {
+                if (identifier.mStorageId != root.mStorageId)
+                    continue;
+                final MatrixCursor cursor = new MatrixCursor(projection);
+                CursorHelper.addToCursor(root, cursor.newRow());
+                return cursor;
+            }
         }
 
-        final MatrixCursor cursor = new MatrixCursor(projection);
-        document.addToCursor(
-                new Identifier(identifier.mDeviceId, identifier.mStorageId), cursor.newRow());
-        return cursor;
+        throw new FileNotFoundException();
     }
 
     @Override
@@ -173,6 +174,8 @@
                 case "r":
                     return mPipeManager.readDocument(mMtpManager, identifier);
                 case "w":
+                    // TODO: Clear the parent document loader task (if exists) and call notify
+                    // when writing is completed.
                     return mPipeManager.writeDocument(getContext(), mMtpManager, identifier);
                 default:
                     // TODO: Add support for seekable files.
@@ -207,8 +210,10 @@
             final int parentHandle =
                     mMtpManager.getParent(identifier.mDeviceId, identifier.mObjectHandle);
             mMtpManager.deleteDocument(identifier.mDeviceId, identifier.mObjectHandle);
-            notifyChildDocumentsChange(new Identifier(
-                    identifier.mDeviceId, identifier.mStorageId, parentHandle).toDocumentId());
+            final Identifier parentIdentifier = new Identifier(
+                    identifier.mDeviceId, identifier.mStorageId, parentHandle);
+            mDocumentLoader.clearTask(parentIdentifier);
+            notifyChildDocumentsChange(parentIdentifier.toDocumentId());
         } catch (IOException error) {
             throw new FileNotFoundException(error.getMessage());
         }
@@ -216,7 +221,7 @@
 
     @Override
     public void onTrimMemory(int level) {
-        mDocumentLoader.clearCache();
+        mDocumentLoader.clearCompletedTasks();
     }
 
     @Override
@@ -224,11 +229,19 @@
             throws FileNotFoundException {
         try {
             final Identifier parentId = Identifier.createFromDocumentId(parentDocumentId);
+            final ParcelFileDescriptor pipe[] = ParcelFileDescriptor.createReliablePipe();
+            pipe[0].close();  // 0 bytes for a new document.
             final int objectHandle = mMtpManager.createDocument(
-                    parentId.mDeviceId, parentId.mStorageId, parentId.mObjectHandle, mimeType,
-                    displayName);
-            final String documentId =  new Identifier(parentId.mDeviceId, parentId.mStorageId,
+                    parentId.mDeviceId,
+                    new MtpObjectInfo.Builder()
+                            .setStorageId(parentId.mStorageId)
+                            .setParent(parentId.mObjectHandle)
+                            .setFormat(CursorHelper.mimeTypeToFormatType(mimeType))
+                            .setName(displayName)
+                            .build(), pipe[1]);
+            final String documentId = new Identifier(parentId.mDeviceId, parentId.mStorageId,
                    objectHandle).toDocumentId();
+            mDocumentLoader.clearTask(parentId);
             notifyChildDocumentsChange(parentDocumentId);
             return documentId;
         } catch (IOException error) {
@@ -244,7 +257,7 @@
 
     void closeDevice(int deviceId) throws IOException {
         mMtpManager.closeDevice(deviceId);
-        mDocumentLoader.clearCache(deviceId);
+        mDocumentLoader.clearTasks(deviceId);
         mRootScanner.scanNow();
     }
 
@@ -253,7 +266,7 @@
         for (int deviceId : mMtpManager.getOpenedDeviceIds()) {
             try {
                 mMtpManager.closeDevice(deviceId);
-                mDocumentLoader.clearCache(deviceId);
+                mDocumentLoader.clearTasks(deviceId);
                 closed = true;
             } catch (IOException d) {
                 Log.d(TAG, "Failed to close the MTP device: " + deviceId);
diff --git a/packages/MtpDocumentsProvider/src/com/android/mtp/MtpManager.java b/packages/MtpDocumentsProvider/src/com/android/mtp/MtpManager.java
index 6647009..ca78a3e 100644
--- a/packages/MtpDocumentsProvider/src/com/android/mtp/MtpManager.java
+++ b/packages/MtpDocumentsProvider/src/com/android/mtp/MtpManager.java
@@ -20,6 +20,7 @@
 import android.hardware.usb.UsbDevice;
 import android.hardware.usb.UsbDeviceConnection;
 import android.hardware.usb.UsbManager;
+import android.mtp.MtpConstants;
 import android.mtp.MtpDevice;
 import android.mtp.MtpObjectInfo;
 import android.os.ParcelFileDescriptor;
@@ -108,16 +109,12 @@
         return results;
     }
 
-    synchronized MtpObjectInfo getObjectInfo(int deviceId, int objectHandle) throws IOException {
+    synchronized MtpObjectInfo getObjectInfo(int deviceId, int objectHandle)
+            throws IOException {
         final MtpDevice device = getDevice(deviceId);
         return device.getObjectInfo(objectHandle);
     }
 
-    synchronized MtpDocument getDocument(int deviceId, int objectHandle) throws IOException {
-        final MtpDevice device = getDevice(deviceId);
-        return new MtpDocument(device.getObjectInfo(objectHandle));
-    }
-
     synchronized int[] getObjectHandles(int deviceId, int storageId, int parentObjectHandle)
             throws IOException {
         final MtpDevice device = getDevice(deviceId);
@@ -142,25 +139,20 @@
         }
     }
 
-    // TODO: Remove this method.
-    synchronized int createDocument(int deviceId, int storageId, int parentObjectHandle,
-            String mimeType, String name) throws IOException {
-        final MtpObjectInfo objectInfo = new MtpObjectInfo.Builder()
-                .setName(name)
-                .setStorageId(storageId)
-                .setParent(parentObjectHandle)
-                .setFormat(MtpDocument.mimeTypeToFormatType(mimeType))
-                .build();
-        return createDocument(deviceId, objectInfo);
-    }
-
-    synchronized int createDocument(int deviceId, MtpObjectInfo objectInfo) throws IOException {
+    synchronized int createDocument(int deviceId, MtpObjectInfo objectInfo,
+            ParcelFileDescriptor source) throws IOException {
         final MtpDevice device = getDevice(deviceId);
-        final MtpObjectInfo result = device.sendObjectInfo(objectInfo);
-        if (result == null) {
+        final MtpObjectInfo sendObjectInfoResult = device.sendObjectInfo(objectInfo);
+        if (sendObjectInfoResult == null) {
             throw new IOException("Failed to create a document");
         }
-        return result.getObjectHandle();
+        if (objectInfo.getFormat() != MtpConstants.FORMAT_ASSOCIATION) {
+            if (!device.sendObject(sendObjectInfoResult.getObjectHandle(),
+                    sendObjectInfoResult.getCompressedSize(), source)) {
+                throw new IOException("Failed to send contents of a document");
+            }
+        }
+        return sendObjectInfoResult.getObjectHandle();
     }
 
     synchronized int getParent(int deviceId, int objectHandle) throws IOException {
@@ -178,13 +170,6 @@
         device.importFile(objectHandle, target);
     }
 
-    synchronized void sendObject(int deviceId, int objectHandle, int size,
-            ParcelFileDescriptor source) throws IOException {
-        final MtpDevice device = getDevice(deviceId);
-        if (!device.sendObject(objectHandle, size, source))
-            throw new IOException("Failed to send a document");
-    }
-
     private MtpDevice getDevice(int deviceId) throws IOException {
         final MtpDevice device = mDevices.get(deviceId);
         if (device == null) {
diff --git a/packages/MtpDocumentsProvider/src/com/android/mtp/PipeManager.java b/packages/MtpDocumentsProvider/src/com/android/mtp/PipeManager.java
index 53f1b65..cd38f1e 100644
--- a/packages/MtpDocumentsProvider/src/com/android/mtp/PipeManager.java
+++ b/packages/MtpDocumentsProvider/src/com/android/mtp/PipeManager.java
@@ -139,20 +139,15 @@
                 // Delete the target object info if it already exists (as a placeholder).
                 mManager.deleteDocument(mIdentifier.mDeviceId, mIdentifier.mObjectHandle);
 
-                // Create the target object info with a correct file size.
-                final int targetObjectHandle =
-                        mManager.createDocument(
-                                mIdentifier.mDeviceId,
-                                new MtpObjectInfo.Builder(placeholderObjectInfo)
-                                        .setCompressedSize((int) tempFile.length())
-                                        .build());
-
-                // Upload the object.
+                // Create the target object info with a correct file size and upload the file.
+                final MtpObjectInfo targetObjectInfo =
+                        new MtpObjectInfo.Builder(placeholderObjectInfo)
+                                .setCompressedSize((int) tempFile.length())
+                                .build();
                 final ParcelFileDescriptor tempInputDescriptor = ParcelFileDescriptor.open(
                         tempFile, ParcelFileDescriptor.MODE_READ_ONLY);
-                mManager.sendObject(mIdentifier.mDeviceId,
-                        targetObjectHandle, (int) tempFile.length(), tempInputDescriptor);
-
+                mManager.createDocument(mIdentifier.mDeviceId,
+                        targetObjectInfo, tempInputDescriptor);
             } catch (IOException error) {
                 Log.w(MtpDocumentsProvider.TAG,
                         "Failed to send a file because of: " + error.getMessage());
diff --git a/packages/MtpDocumentsProvider/tests/src/com/android/mtp/DocumentLoaderTest.java b/packages/MtpDocumentsProvider/tests/src/com/android/mtp/DocumentLoaderTest.java
index 1e015bd..a012d7f 100644
--- a/packages/MtpDocumentsProvider/tests/src/com/android/mtp/DocumentLoaderTest.java
+++ b/packages/MtpDocumentsProvider/tests/src/com/android/mtp/DocumentLoaderTest.java
@@ -18,6 +18,7 @@
 
 import android.content.Context;
 import android.database.Cursor;
+import android.mtp.MtpObjectInfo;
 import android.net.Uri;
 import android.provider.DocumentsContract;
 import android.test.AndroidTestCase;
@@ -31,14 +32,14 @@
 
 @SmallTest
 public class DocumentLoaderTest extends AndroidTestCase {
-    private BlockableTestMtpMaanger mManager;
+    private BlockableTestMtpManager mManager;
     private TestContentResolver mResolver;
     private DocumentLoader mLoader;
     final private Identifier mParentIdentifier = new Identifier(0, 0, 0);
 
     @Override
     public void setUp() {
-        mManager = new BlockableTestMtpMaanger(getContext());
+        mManager = new BlockableTestMtpManager(getContext());
         mResolver = new TestContentResolver();
         mLoader = new DocumentLoader(mManager, mResolver);
     }
@@ -85,22 +86,17 @@
         for (int i = 0; i < childDocuments.length; i++) {
             final int objectHandle = i + 1;
             childDocuments[i] = objectHandle;
-            manager.setDocument(0, objectHandle, new MtpDocument(
-                    objectHandle,
-                    0 /* format */,
-                    "file" + objectHandle,
-                    new Date(),
-                    1024,
-                    0 /* thumbnail size */,
-                    false /* not read only */));
+            manager.setObjectInfo(0, new MtpObjectInfo.Builder()
+                    .setObjectHandle(objectHandle)
+                    .build());
         }
         manager.setObjectHandles(0, 0, MtpManager.OBJECT_HANDLE_ROOT_CHILDREN, childDocuments);
     }
 
-    private static class BlockableTestMtpMaanger extends TestMtpManager {
+    private static class BlockableTestMtpManager extends TestMtpManager {
         final private Map<String, CountDownLatch> blockedDocuments = new HashMap<>();
 
-        BlockableTestMtpMaanger(Context context) {
+        BlockableTestMtpManager(Context context) {
             super(context);
         }
 
@@ -113,7 +109,7 @@
         }
 
         @Override
-        MtpDocument getDocument(int deviceId, int objectHandle) throws IOException {
+        MtpObjectInfo getObjectInfo(int deviceId, int objectHandle) throws IOException {
             final CountDownLatch latch = blockedDocuments.get(pack(deviceId, objectHandle));
             if (latch != null) {
                 try {
@@ -122,7 +118,7 @@
                     fail();
                 }
             }
-            return super.getDocument(deviceId, objectHandle);
+            return super.getObjectInfo(deviceId, objectHandle);
         }
     }
 }
diff --git a/packages/MtpDocumentsProvider/tests/src/com/android/mtp/MtpDocumentsProviderTest.java b/packages/MtpDocumentsProvider/tests/src/com/android/mtp/MtpDocumentsProviderTest.java
index 1826bd0..9b316be 100644
--- a/packages/MtpDocumentsProvider/tests/src/com/android/mtp/MtpDocumentsProviderTest.java
+++ b/packages/MtpDocumentsProvider/tests/src/com/android/mtp/MtpDocumentsProviderTest.java
@@ -17,9 +17,12 @@
 package com.android.mtp;
 
 import android.database.Cursor;
+import android.mtp.MtpConstants;
+import android.mtp.MtpObjectInfo.Builder;
+import android.mtp.MtpObjectInfo;
 import android.net.Uri;
-import android.provider.DocumentsContract;
 import android.provider.DocumentsContract.Root;
+import android.provider.DocumentsContract;
 import android.test.AndroidTestCase;
 import android.test.suitebuilder.annotation.SmallTest;
 
@@ -204,14 +207,14 @@
     }
 
     public void testQueryDocument() throws IOException {
-        mMtpManager.setDocument(0, 2, new MtpDocument(
-                2 /* object handle */,
-                0x3801 /* JPEG */,
-                "image.jpg" /* display name */,
-                new Date(1422716400000L) /* modified date */,
-                1024 * 1024 * 5 /* file size */,
-                1024 * 50 /* thumbnail size */,
-                false /* read only */));
+        mMtpManager.setObjectInfo(0, new MtpObjectInfo.Builder()
+                .setObjectHandle(2)
+                .setFormat(MtpConstants.FORMAT_EXIF_JPEG)
+                .setName("image.jpg")
+                .setDateModified(1422716400000L)
+                .setCompressedSize(1024 * 1024 * 5)
+                .setThumbCompressedSize(1024 * 50)
+                .build());
         final Cursor cursor = mProvider.queryDocument("0_1_2", null);
         assertEquals(1, cursor.getCount());
 
@@ -229,14 +232,12 @@
     }
 
     public void testQueryDocument_directory() throws IOException {
-        mMtpManager.setDocument(0, 2, new MtpDocument(
-                2 /* object handle */,
-                0x3001 /* directory */,
-                "directory" /* display name */,
-                new Date(1422716400000L) /* modified date */,
-                0 /* file size */,
-                0 /* thumbnail size */,
-                false /* read only */));
+        mMtpManager.setObjectInfo(0, new MtpObjectInfo.Builder()
+                .setObjectHandle(2)
+                .setFormat(MtpConstants.FORMAT_ASSOCIATION)
+                .setName("directory")
+                .setDateModified(1422716400000L)
+                .build());
         final Cursor cursor = mProvider.queryDocument("0_1_2", null);
         assertEquals(1, cursor.getCount());
 
@@ -278,14 +279,14 @@
     public void testQueryChildDocuments() throws Exception {
         mMtpManager.setObjectHandles(0, 0, -1, new int[] { 1 });
 
-        mMtpManager.setDocument(0, 1, new MtpDocument(
-                1 /* object handle */,
-                0x3801 /* JPEG */,
-                "image.jpg" /* display name */,
-                new Date(0) /* modified date */,
-                1024 * 1024 * 5 /* file size */,
-                1024 * 50 /* thumbnail size */,
-                true /* read only */));
+        mMtpManager.setObjectInfo(0, new MtpObjectInfo.Builder()
+                .setObjectHandle(1)
+                .setFormat(MtpConstants.FORMAT_EXIF_JPEG)
+                .setName("image.jpg")
+                .setCompressedSize(1024 * 1024 * 5)
+                .setThumbCompressedSize(5 * 1024)
+                .setProtectionStatus(MtpConstants.PROTECTION_STATUS_READ_ONLY)
+                .build());
 
         final Cursor cursor = mProvider.queryChildDocuments("0_0_0", null, null);
         assertEquals(1, cursor.getCount());
@@ -321,15 +322,10 @@
     }
 
     public void testDeleteDocument() throws FileNotFoundException {
-        mMtpManager.setDocument(0, 1, new MtpDocument(
-                1 /* object handle */,
-                0x3801 /* JPEG */,
-                "image.jpg" /* display name */,
-                new Date(1422716400000L) /* modified date */,
-                1024 * 1024 * 5 /* file size */,
-                1024 * 50 /* thumbnail size */,
-                false /* not read only */));
-        mMtpManager.setParent(0, 1, 2);
+        mMtpManager.setObjectInfo(0, new MtpObjectInfo.Builder()
+                .setObjectHandle(1)
+                .setParent(2)
+                .build());
         mProvider.deleteDocument("0_0_1");
         assertEquals(1, mResolver.getChangeCount(
                 DocumentsContract.buildChildDocumentsUri(
@@ -337,7 +333,9 @@
     }
 
     public void testDeleteDocument_error() {
-        mMtpManager.setParent(0, 1, 2);
+        mMtpManager.setObjectInfo(0, new MtpObjectInfo.Builder()
+                .setObjectHandle(2)
+                .build());
         try {
             mProvider.deleteDocument("0_0_1");
             fail();
diff --git a/packages/MtpDocumentsProvider/tests/src/com/android/mtp/PipeManagerTest.java b/packages/MtpDocumentsProvider/tests/src/com/android/mtp/PipeManagerTest.java
index cfb8b04..53018cc 100644
--- a/packages/MtpDocumentsProvider/tests/src/com/android/mtp/PipeManagerTest.java
+++ b/packages/MtpDocumentsProvider/tests/src/com/android/mtp/PipeManagerTest.java
@@ -16,6 +16,7 @@
 
 package com.android.mtp;
 
+import android.mtp.MtpObjectInfo;
 import android.os.ParcelFileDescriptor;
 import android.test.AndroidTestCase;
 import android.test.suitebuilder.annotation.SmallTest;
@@ -56,7 +57,9 @@
 
     public void testWriteDocument_basic() throws Exception {
         // Create a placeholder file which should be replaced by a real file later.
-        mtpManager.setDocument(0, 1, new MtpDocument(1, 0, "", new Date(), 0, 0, false));
+        mtpManager.setObjectInfo(0, new MtpObjectInfo.Builder()
+                .setObjectHandle(1)
+                .build());
 
         // Upload testing bytes.
         final ParcelFileDescriptor descriptor = mPipeManager.writeDocument(
@@ -69,14 +72,14 @@
 
         // Check if the placeholder file is removed.
         try {
-            final MtpDocument placeholderDocument = mtpManager.getDocument(0, 1);
+            final MtpObjectInfo placeholderDocument = mtpManager.getObjectInfo(0, 1);
             fail();  // The placeholder file has not been deleted.
         } catch (IOException e) {
             // Expected error, as the file is gone.
         }
 
         // Confirm that the target file is created.
-        final MtpDocument targetDocument = mtpManager.getDocument(
+        final MtpObjectInfo targetDocument = mtpManager.getObjectInfo(
                 0, TestMtpManager.CREATED_DOCUMENT_HANDLE);
         assertTrue(targetDocument != null);
 
diff --git a/packages/MtpDocumentsProvider/tests/src/com/android/mtp/TestMtpManager.java b/packages/MtpDocumentsProvider/tests/src/com/android/mtp/TestMtpManager.java
index 94b5ba0..5605388 100644
--- a/packages/MtpDocumentsProvider/tests/src/com/android/mtp/TestMtpManager.java
+++ b/packages/MtpDocumentsProvider/tests/src/com/android/mtp/TestMtpManager.java
@@ -40,10 +40,9 @@
     private final Set<Integer> mValidDevices = new HashSet<>();
     private final Set<Integer> mOpenedDevices = new TreeSet<>();
     private final Map<Integer, MtpRoot[]> mRoots = new HashMap<>();
-    private final Map<String, MtpDocument> mDocuments = new HashMap<>();
+    private final Map<String, MtpObjectInfo> mObjectInfos = new HashMap<>();
     private final Map<String, int[]> mObjectHandles = new HashMap<>();
     private final Map<String, byte[]> mThumbnailBytes = new HashMap<>();
-    private final Map<String, Integer> mParents = new HashMap<>();
     private final Map<String, byte[]> mImportFileBytes = new HashMap<>();
 
     TestMtpManager(Context context) {
@@ -54,16 +53,16 @@
         mValidDevices.add(deviceId);
     }
 
-    void setObjectHandles(int deviceId, int storageId, int objectHandle, int[] documents) {
-        mObjectHandles.put(pack(deviceId, storageId, objectHandle), documents);
+    void setObjectHandles(int deviceId, int storageId, int parentHandle, int[] objectHandles) {
+        mObjectHandles.put(pack(deviceId, storageId, parentHandle), objectHandles);
     }
 
     void setRoots(int deviceId, MtpRoot[] roots) {
         mRoots.put(deviceId, roots);
     }
 
-    void setDocument(int deviceId, int objectHandle, MtpDocument document) {
-        mDocuments.put(pack(deviceId, objectHandle), document);
+    void setObjectInfo(int deviceId, MtpObjectInfo objectInfo) {
+        mObjectInfos.put(pack(deviceId, objectInfo.getObjectHandle()), objectInfo);
     }
 
     void setImportFileBytes(int deviceId, int objectHandle, byte[] bytes) {
@@ -78,10 +77,6 @@
         mThumbnailBytes.put(pack(deviceId, objectHandle), bytes);
     }
 
-    void setParent(int deviceId, int objectHandle, int parentObjectHandle) {
-        mParents.put(pack(deviceId, objectHandle), parentObjectHandle);
-    }
-
     @Override
     void openDevice(int deviceId) throws IOException {
         if (!mValidDevices.contains(deviceId) || mOpenedDevices.contains(deviceId)) {
@@ -108,22 +103,13 @@
     }
 
     @Override
-    MtpDocument getDocument(int deviceId, int objectHandle) throws IOException {
-        final String key = pack(deviceId, objectHandle);
-        if (mDocuments.containsKey(key)) {
-            return mDocuments.get(key);
-        } else {
-            throw new IOException("getDocument error: " + key);
-        }
-    }
-
-    @Override
     MtpObjectInfo getObjectInfo(int deviceId, int objectHandle) throws IOException {
-        final MtpDocument document = getDocument(deviceId, objectHandle);
-        // It's impossible to set an object id of MtpObjectInfo at this stage. Also,
-        // it's hard to get any information from MtpDocument, as it's designed to return them
-        // only via cursors. Rework these.
-        return new MtpObjectInfo.Builder().build();
+        final String key = pack(deviceId, objectHandle);
+        if (mObjectInfos.containsKey(key)) {
+            return mObjectInfos.get(key);
+        } else {
+            throw new IOException("getObjectInfo error: " + key);
+        }
     }
 
     @Override
@@ -151,44 +137,29 @@
     }
 
     @Override
-    int createDocument(int deviceId, MtpObjectInfo objectInfo) throws IOException {
-        // For simplicity, it allows to create only one document, and it always has the hardcoded
-        // CREATED_DOCUMENT_HANDLE document handle.
+    int createDocument(int deviceId, MtpObjectInfo objectInfo, ParcelFileDescriptor source)
+            throws IOException {
         final String key = pack(deviceId, CREATED_DOCUMENT_HANDLE);
-        if (!mDocuments.containsKey(key)) {
-            mDocuments.put(key, new MtpDocument(
-                  CREATED_DOCUMENT_HANDLE,
-                  objectInfo.getFormat(),
-                  objectInfo.getName(),
-                  new Date(objectInfo.getDateModified()),
-                  objectInfo.getCompressedSize(),
-                  objectInfo.getThumbCompressedSize(),
-                  false /* Always writable for testing. */));
-        } else {
+        if (mObjectInfos.containsKey(key)) {
             throw new IOException();
         }
+        mObjectInfos.put(key, objectInfo);
+        if (objectInfo.getFormat() != 0x3001) {
+            try (final ParcelFileDescriptor.AutoCloseInputStream inputStream =
+                    new ParcelFileDescriptor.AutoCloseInputStream(source)) {
+                final byte[] buffer = new byte[objectInfo.getCompressedSize()];
+                if (inputStream.read(buffer, 0, objectInfo.getCompressedSize()) !=
+                        objectInfo.getCompressedSize()) {
+                    throw new IOException();
+                }
+
+                mImportFileBytes.put(pack(deviceId, CREATED_DOCUMENT_HANDLE), buffer);
+            }
+        }
         return CREATED_DOCUMENT_HANDLE;
     }
 
     @Override
-    void sendObject(int deviceId, int objectHandle, int size, ParcelFileDescriptor source)
-            throws IOException {
-        final String key = pack(deviceId, objectHandle);
-        if (!mDocuments.containsKey(key)) {
-            throw new IOException();
-        }
-
-        ParcelFileDescriptor.AutoCloseInputStream inputStream =
-                new ParcelFileDescriptor.AutoCloseInputStream(source);
-        byte[] buffer = new byte[size];
-        if (inputStream.read(buffer, 0, size) != size) {
-            throw new IOException();
-        }
-
-        mImportFileBytes.put(pack(deviceId, objectHandle), buffer);
-    }
-
-    @Override
     byte[] getThumbnail(int deviceId, int objectHandle) throws IOException {
         final String key = pack(deviceId, objectHandle);
         if (mThumbnailBytes.containsKey(key)) {
@@ -201,18 +172,18 @@
     @Override
     void deleteDocument(int deviceId, int objectHandle) throws IOException {
         final String key = pack(deviceId, objectHandle);
-        if (mDocuments.containsKey(key)) {
-            mDocuments.remove(key);
+        if (mObjectInfos.containsKey(key)) {
+            mObjectInfos.remove(key);
         } else {
             throw new IOException();
         }
     }
 
     @Override
-    synchronized int getParent(int deviceId, int objectHandle) throws IOException {
+    int getParent(int deviceId, int objectHandle) throws IOException {
         final String key = pack(deviceId, objectHandle);
-        if (mParents.containsKey(key)) {
-            return mParents.get(key);
+        if (mObjectInfos.containsKey(key)) {
+            return mObjectInfos.get(key).getParent();
         } else {
             throw new IOException();
         }
diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java
index b0429ef..e4b1ed8 100644
--- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java
+++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java
@@ -539,10 +539,10 @@
          * Otherwise, allow the connect on UUID change.
          */
         if (!mProfiles.isEmpty()
-                && ((mConnectAttempted + timeout) > SystemClock.elapsedRealtime()
-                || (mConnectAttempted == 0))) {
+                && ((mConnectAttempted + timeout) > SystemClock.elapsedRealtime())) {
             connectWithoutResettingTimer(false);
         }
+
         dispatchAttributesChanged();
     }
 
diff --git a/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java b/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
index 5a14967..ee296d9 100644
--- a/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
+++ b/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
@@ -109,7 +109,7 @@
 
     static String dbNameForUser(final int userHandle) {
         // The owner gets the unadorned db name;
-        if (userHandle == UserHandle.USER_OWNER) {
+        if (userHandle == UserHandle.USER_SYSTEM) {
             return DATABASE_NAME;
         } else {
             // Place the database in the user-specific data tree so that it's
@@ -186,8 +186,8 @@
 
         createSecureTable(db);
 
-        // Only create the global table for the singleton 'owner' user
-        if (mUserHandle == UserHandle.USER_OWNER) {
+        // Only create the global table for the singleton 'owner/system' user
+        if (mUserHandle == UserHandle.USER_SYSTEM) {
             createGlobalTable(db);
         }
 
@@ -1252,7 +1252,7 @@
 
         if (upgradeVersion == 82) {
             // Move to per-user settings dbs
-            if (mUserHandle == UserHandle.USER_OWNER) {
+            if (mUserHandle == UserHandle.USER_SYSTEM) {
 
                 db.beginTransaction();
                 SQLiteStatement stmt = null;
@@ -1306,7 +1306,7 @@
         }
 
         if (upgradeVersion == 84) {
-            if (mUserHandle == UserHandle.USER_OWNER) {
+            if (mUserHandle == UserHandle.USER_SYSTEM) {
                 db.beginTransaction();
                 SQLiteStatement stmt = null;
                 try {
@@ -1331,7 +1331,7 @@
         }
 
         if (upgradeVersion == 85) {
-            if (mUserHandle == UserHandle.USER_OWNER) {
+            if (mUserHandle == UserHandle.USER_SYSTEM) {
                 db.beginTransaction();
                 try {
                     // Fix up the migration, ignoring already-migrated elements, to snap up to
@@ -1348,7 +1348,7 @@
         }
 
         if (upgradeVersion == 86) {
-            if (mUserHandle == UserHandle.USER_OWNER) {
+            if (mUserHandle == UserHandle.USER_SYSTEM) {
                 db.beginTransaction();
                 try {
                     String[] settingsToMove = {
@@ -1367,7 +1367,7 @@
         }
 
         if (upgradeVersion == 87) {
-            if (mUserHandle == UserHandle.USER_OWNER) {
+            if (mUserHandle == UserHandle.USER_SYSTEM) {
                 db.beginTransaction();
                 try {
                     String[] settingsToMove = {
@@ -1386,7 +1386,7 @@
         }
 
         if (upgradeVersion == 88) {
-            if (mUserHandle == UserHandle.USER_OWNER) {
+            if (mUserHandle == UserHandle.USER_SYSTEM) {
                 db.beginTransaction();
                 try {
                     String[] settingsToMove = {
@@ -1432,7 +1432,7 @@
         }
 
         if (upgradeVersion == 89) {
-            if (mUserHandle == UserHandle.USER_OWNER) {
+            if (mUserHandle == UserHandle.USER_SYSTEM) {
                 db.beginTransaction();
                 try {
                     String[] prefixesToMove = {
@@ -1452,7 +1452,7 @@
         }
 
         if (upgradeVersion == 90) {
-            if (mUserHandle == UserHandle.USER_OWNER) {
+            if (mUserHandle == UserHandle.USER_SYSTEM) {
                 db.beginTransaction();
                 try {
                     String[] systemToGlobal = {
@@ -1485,7 +1485,7 @@
         }
 
         if (upgradeVersion == 91) {
-            if (mUserHandle == UserHandle.USER_OWNER) {
+            if (mUserHandle == UserHandle.USER_SYSTEM) {
                 db.beginTransaction();
                 try {
                     // Move ringer mode from system to global settings
@@ -1505,7 +1505,7 @@
             try {
                 stmt = db.compileStatement("INSERT OR IGNORE INTO secure(name,value)"
                         + " VALUES(?,?);");
-                if (mUserHandle == UserHandle.USER_OWNER) {
+                if (mUserHandle == UserHandle.USER_SYSTEM) {
                     // consider existing primary users to have made it through user setup
                     // if the globally-scoped device-provisioned bit is set
                     // (indicating they already made it through setup as primary)
@@ -1526,7 +1526,7 @@
 
         if (upgradeVersion == 93) {
             // Redo this step, since somehow it didn't work the first time for some users
-            if (mUserHandle == UserHandle.USER_OWNER) {
+            if (mUserHandle == UserHandle.USER_SYSTEM) {
                 db.beginTransaction();
                 try {
                     // Migrate now-global settings
@@ -1547,7 +1547,7 @@
 
         if (upgradeVersion == 94) {
             // Add wireless charging started sound setting
-            if (mUserHandle == UserHandle.USER_OWNER) {
+            if (mUserHandle == UserHandle.USER_SYSTEM) {
                 db.beginTransaction();
                 SQLiteStatement stmt = null;
                 try {
@@ -1565,7 +1565,7 @@
         }
 
         if (upgradeVersion == 95) {
-            if (mUserHandle == UserHandle.USER_OWNER) {
+            if (mUserHandle == UserHandle.USER_SYSTEM) {
                 db.beginTransaction();
                 try {
                     String[] settingsToMove = { Settings.Global.BUGREPORT_IN_POWER_MENU };
@@ -1584,7 +1584,7 @@
         }
 
         if (upgradeVersion == 97) {
-            if (mUserHandle == UserHandle.USER_OWNER) {
+            if (mUserHandle == UserHandle.USER_SYSTEM) {
                 db.beginTransaction();
                 SQLiteStatement stmt = null;
                 try {
@@ -1613,7 +1613,7 @@
 
         if (upgradeVersion == 100) {
             // note: LOCK_SCREEN_SHOW_NOTIFICATIONS now handled in version 106
-            if (mUserHandle == UserHandle.USER_OWNER) {
+            if (mUserHandle == UserHandle.USER_SYSTEM) {
                 db.beginTransaction();
                 SQLiteStatement stmt = null;
                 try {
@@ -1631,7 +1631,7 @@
         }
 
         if (upgradeVersion == 101) {
-            if (mUserHandle == UserHandle.USER_OWNER) {
+            if (mUserHandle == UserHandle.USER_SYSTEM) {
                 db.beginTransaction();
                 SQLiteStatement stmt = null;
                 try {
@@ -1653,7 +1653,7 @@
             try {
                 // The INSTALL_NON_MARKET_APPS setting is becoming per-user rather
                 // than device-global.
-                if (mUserHandle == UserHandle.USER_OWNER) {
+                if (mUserHandle == UserHandle.USER_SYSTEM) {
                     // In the owner user, the global table exists so we can migrate the
                     // entry from there to the secure table, preserving its value.
                     String[] globalToSecure = {
@@ -1693,7 +1693,7 @@
         }
 
         if (upgradeVersion < 105) {
-            if (mUserHandle == UserHandle.USER_OWNER) {
+            if (mUserHandle == UserHandle.USER_SYSTEM) {
                 db.beginTransaction();
                 SQLiteStatement stmt = null;
                 try {
@@ -1719,7 +1719,7 @@
                         + " VALUES(?,?);");
                 loadIntegerSetting(stmt, Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
                         R.integer.def_lock_screen_show_notifications);
-                if (mUserHandle == UserHandle.USER_OWNER) {
+                if (mUserHandle == UserHandle.USER_SYSTEM) {
                     final int oldShow = getIntValueFromTable(db,
                             TABLE_GLOBAL, Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, -1);
                     if (oldShow >= 0) {
@@ -1741,7 +1741,7 @@
 
         if (upgradeVersion < 107) {
             // Add trusted sound setting
-            if (mUserHandle == UserHandle.USER_OWNER) {
+            if (mUserHandle == UserHandle.USER_SYSTEM) {
                 db.beginTransaction();
                 SQLiteStatement stmt = null;
                 try {
@@ -1816,7 +1816,7 @@
 
         if (upgradeVersion < 111) {
             // reset ringer mode, so it doesn't force zen mode to follow
-            if (mUserHandle == UserHandle.USER_OWNER) {
+            if (mUserHandle == UserHandle.USER_SYSTEM) {
                 db.beginTransaction();
                 SQLiteStatement stmt = null;
                 try {
@@ -1833,7 +1833,7 @@
         }
 
         if (upgradeVersion < 112) {
-            if (mUserHandle == UserHandle.USER_OWNER) {
+            if (mUserHandle == UserHandle.USER_SYSTEM) {
                 // When device name was added, we went with Manufacturer + Model, device name should
                 // actually be Model only.
                 // Update device name to Model if it wasn't modified by user.
@@ -1874,7 +1874,7 @@
         // We skipped 114 to handle a merge conflict with the introduction of theater mode.
 
         if (upgradeVersion < 115) {
-            if (mUserHandle == UserHandle.USER_OWNER) {
+            if (mUserHandle == UserHandle.USER_SYSTEM) {
                 db.beginTransaction();
                 SQLiteStatement stmt = null;
                 try {
@@ -1892,7 +1892,7 @@
         }
 
         if (upgradeVersion < 116) {
-            if (mUserHandle == UserHandle.USER_OWNER) {
+            if (mUserHandle == UserHandle.USER_SYSTEM) {
                 db.beginTransaction();
                 SQLiteStatement stmt = null;
                 try {
@@ -2066,7 +2066,7 @@
                     LockPatternUtils lpu = new LockPatternUtils(mContext);
                     List<LockPatternView.Cell> cellPattern =
                             LockPatternUtils.stringToPattern(lockPattern);
-                    lpu.saveLockPattern(cellPattern, null, UserHandle.USER_OWNER);
+                    lpu.saveLockPattern(cellPattern, null, UserHandle.USER_SYSTEM);
                 } catch (IllegalArgumentException e) {
                     // Don't want corrupted lock pattern to hang the reboot process
                 }
@@ -2343,8 +2343,8 @@
     private void loadSettings(SQLiteDatabase db) {
         loadSystemSettings(db);
         loadSecureSettings(db);
-        // The global table only exists for the 'owner' user
-        if (mUserHandle == UserHandle.USER_OWNER) {
+        // The global table only exists for the 'owner/system' user
+        if (mUserHandle == UserHandle.USER_SYSTEM) {
             loadGlobalSettings(db);
         }
     }
diff --git a/packages/SettingsProvider/src/com/android/providers/settings/SettingsHelper.java b/packages/SettingsProvider/src/com/android/providers/settings/SettingsHelper.java
index 952b220..1d71346 100644
--- a/packages/SettingsProvider/src/com/android/providers/settings/SettingsHelper.java
+++ b/packages/SettingsProvider/src/com/android/providers/settings/SettingsHelper.java
@@ -123,7 +123,8 @@
         }
 
         if (sBroadcastOnRestore.contains(name)) {
-            oldValue = table.lookup(cr, name, UserHandle.USER_OWNER);
+            // TODO: http://b/22388012
+            oldValue = table.lookup(cr, name, UserHandle.USER_SYSTEM);
             sendBroadcast = true;
         }
 
diff --git a/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java b/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
index 73971ad..8b1caf9 100644
--- a/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
+++ b/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
@@ -18,7 +18,7 @@
 
 import android.Manifest;
 import android.app.ActivityManager;
-import android.app.AppOpsManager;
+import android.app.AppGlobals;
 import android.app.backup.BackupManager;
 import android.content.BroadcastReceiver;
 import android.content.ContentProvider;
@@ -27,6 +27,7 @@
 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.UserInfo;
@@ -47,6 +48,7 @@
 import android.os.Message;
 import android.os.ParcelFileDescriptor;
 import android.os.Process;
+import android.os.RemoteException;
 import android.os.SystemProperties;
 import android.os.UserHandle;
 import android.os.UserManager;
@@ -208,13 +210,13 @@
     private volatile UserManager mUserManager;
 
     // We have to call in the package manager with no lock held,
-    private volatile PackageManager mPackageManager;
+    private volatile IPackageManager mPackageManager;
 
     @Override
     public boolean onCreate() {
         synchronized (mLock) {
-            mUserManager = (UserManager) getContext().getSystemService(Context.USER_SERVICE);
-            mPackageManager = getContext().getPackageManager();
+            mUserManager = UserManager.get(getContext());
+            mPackageManager = AppGlobals.getPackageManager();
             mSettingsRegistry = new SettingsRegistry();
         }
         registerBroadcastReceivers();
@@ -496,7 +498,7 @@
     }
 
     private void dumpForUser(int userId, PrintWriter pw) {
-        if (userId == UserHandle.USER_OWNER) {
+        if (userId == UserHandle.USER_SYSTEM) {
             pw.println("GLOBAL SETTINGS (user " + userId + ")");
             Cursor globalCursor = getAllGlobalSettings(ALL_COLUMNS);
             dumpSettings(globalCursor, pw);
@@ -547,7 +549,7 @@
             @Override
             public void onReceive(Context context, Intent intent) {
                 final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
-                        UserHandle.USER_OWNER);
+                        UserHandle.USER_SYSTEM);
 
                 switch (intent.getAction()) {
                     case Intent.ACTION_USER_REMOVED: {
@@ -584,7 +586,7 @@
         synchronized (mLock) {
             // Get the settings.
             SettingsState settingsState = mSettingsRegistry.getSettingsLocked(
-                    SettingsRegistry.SETTINGS_TYPE_GLOBAL, UserHandle.USER_OWNER);
+                    SettingsRegistry.SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
 
             List<String> names = settingsState.getSettingNamesLocked();
 
@@ -612,7 +614,7 @@
         // Get the value.
         synchronized (mLock) {
             return mSettingsRegistry.getSettingLocked(SettingsRegistry.SETTINGS_TYPE_GLOBAL,
-                    UserHandle.USER_OWNER, name);
+                    UserHandle.USER_SYSTEM, name);
         }
     }
 
@@ -656,19 +658,19 @@
                 case MUTATION_OPERATION_INSERT: {
                     return mSettingsRegistry
                             .insertSettingLocked(SettingsRegistry.SETTINGS_TYPE_GLOBAL,
-                                    UserHandle.USER_OWNER, name, value, getCallingPackage());
+                                    UserHandle.USER_SYSTEM, name, value, getCallingPackage());
                 }
 
                 case MUTATION_OPERATION_DELETE: {
                     return mSettingsRegistry.deleteSettingLocked(
                             SettingsRegistry.SETTINGS_TYPE_GLOBAL,
-                            UserHandle.USER_OWNER, name);
+                            UserHandle.USER_SYSTEM, name);
                 }
 
                 case MUTATION_OPERATION_UPDATE: {
                     return mSettingsRegistry
                             .updateSettingLocked(SettingsRegistry.SETTINGS_TYPE_GLOBAL,
-                                    UserHandle.USER_OWNER, name, value, getCallingPackage());
+                                    UserHandle.USER_SYSTEM, name, value, getCallingPackage());
                 }
             }
         }
@@ -903,7 +905,7 @@
         }
 
         // Enforce what the calling package can mutate the system settings.
-        enforceRestrictedSystemSettingsMutationForCallingPackage(operation, name);
+        enforceRestrictedSystemSettingsMutationForCallingPackage(operation, name, runAsUserId);
 
         // Resolve the userId on whose behalf the call is made.
         final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(runAsUserId);
@@ -1001,7 +1003,7 @@
     }
 
     private void enforceRestrictedSystemSettingsMutationForCallingPackage(int operation,
-            String name) {
+            String name, int userId) {
         // System/root/shell can mutate whatever secure settings they want.
         final int callingUid = Binder.getCallingUid();
         if (callingUid == android.os.Process.SYSTEM_UID
@@ -1019,7 +1021,7 @@
                 }
 
                 // The calling package is already verified.
-                PackageInfo packageInfo = getCallingPackageInfoOrThrow();
+                PackageInfo packageInfo = getCallingPackageInfoOrThrow(userId);
 
                 // Privileged apps can do whatever they want.
                 if ((packageInfo.applicationInfo.privateFlags
@@ -1039,7 +1041,7 @@
                 }
 
                 // The calling package is already verified.
-                PackageInfo packageInfo = getCallingPackageInfoOrThrow();
+                PackageInfo packageInfo = getCallingPackageInfoOrThrow(userId);
 
                 // Privileged apps can do whatever they want.
                 if ((packageInfo.applicationInfo.privateFlags &
@@ -1053,17 +1055,17 @@
         }
     }
 
-    private PackageInfo getCallingPackageInfoOrThrow() {
+    private PackageInfo getCallingPackageInfoOrThrow(int userId) {
         try {
-            return mPackageManager.getPackageInfo(getCallingPackage(), 0);
-        } catch (PackageManager.NameNotFoundException e) {
+            return mPackageManager.getPackageInfo(getCallingPackage(), 0, userId);
+        } catch (RemoteException e) {
             throw new IllegalStateException("Calling package doesn't exist");
         }
     }
 
     private int getGroupParentLocked(int userId) {
         // Most frequent use case.
-        if (userId == UserHandle.USER_OWNER) {
+        if (userId == UserHandle.USER_SYSTEM) {
             return userId;
         }
         // We are in the same process with the user manager and the returned
@@ -1401,8 +1403,8 @@
             migrateLegacySettingsForUserIfNeededLocked(userId);
 
             // Ensure global settings loaded if owner.
-            if (userId == UserHandle.USER_OWNER) {
-                final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_OWNER);
+            if (userId == UserHandle.USER_SYSTEM) {
+                final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
                 ensureSettingsStateLocked(globalKey);
             }
 
@@ -1541,7 +1543,7 @@
 
         private void migrateAllLegacySettingsIfNeeded() {
             synchronized (mLock) {
-                final int key = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_OWNER);
+                final int key = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
                 File globalFile = getSettingsFile(key);
                 if (globalFile.exists()) {
                     return;
@@ -1591,7 +1593,7 @@
         private void migrateLegacySettingsForUserLocked(DatabaseHelper dbHelper,
                 SQLiteDatabase database, int userId) {
             // Move over the global settings if owner.
-            if (userId == UserHandle.USER_OWNER) {
+            if (userId == UserHandle.USER_SYSTEM) {
                 final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, userId);
                 ensureSettingsStateLocked(globalKey);
                 SettingsState globalSettings = mSettingsStates.get(globalKey);
@@ -1898,7 +1900,7 @@
                 }
 
                 // Set the global settings version if owner.
-                if (mUserId == UserHandle.USER_OWNER) {
+                if (mUserId == UserHandle.USER_SYSTEM) {
                     SettingsState globalSettings = getSettingsLocked(
                             SettingsRegistry.SETTINGS_TYPE_GLOBAL, mUserId);
                     globalSettings.setVersionLocked(newVersion);
@@ -1914,7 +1916,7 @@
             }
 
             private SettingsState getGlobalSettingsLocked() {
-                return getSettingsLocked(SETTINGS_TYPE_GLOBAL, UserHandle.USER_OWNER);
+                return getSettingsLocked(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
             }
 
             private SettingsState getSecureSettingsLocked(int userId) {
@@ -1960,7 +1962,7 @@
 
                 // v119: Reset zen + ringer mode.
                 if (currentVersion == 118) {
-                    if (userId == UserHandle.USER_OWNER) {
+                    if (userId == UserHandle.USER_SYSTEM) {
                         final SettingsState globalSettings = getGlobalSettingsLocked();
                         globalSettings.updateSettingLocked(Settings.Global.ZEN_MODE,
                                 Integer.toString(Settings.Global.ZEN_MODE_OFF),
diff --git a/packages/SettingsProvider/test/src/com/android/providers/settings/BaseSettingsProviderTest.java b/packages/SettingsProvider/test/src/com/android/providers/settings/BaseSettingsProviderTest.java
index c7cc89b..8e56f47 100644
--- a/packages/SettingsProvider/test/src/com/android/providers/settings/BaseSettingsProviderTest.java
+++ b/packages/SettingsProvider/test/src/com/android/providers/settings/BaseSettingsProviderTest.java
@@ -48,7 +48,7 @@
             Settings.NameValueTable.NAME, Settings.NameValueTable.VALUE
     };
 
-    protected int mSecondaryUserId = UserHandle.USER_OWNER;
+    protected int mSecondaryUserId = UserHandle.USER_SYSTEM;
 
     @Override
     public void setContext(Context context) {
diff --git a/packages/SettingsProvider/test/src/com/android/providers/settings/SettingsProviderPerformanceTest.java b/packages/SettingsProvider/test/src/com/android/providers/settings/SettingsProviderPerformanceTest.java
index d581f3b..a09d5fe 100644
--- a/packages/SettingsProvider/test/src/com/android/providers/settings/SettingsProviderPerformanceTest.java
+++ b/packages/SettingsProvider/test/src/com/android/providers/settings/SettingsProviderPerformanceTest.java
@@ -47,7 +47,7 @@
 
                 // Make sure the setting changed.
                 String firstValue = getStringViaFrontEndApiSetting(SETTING_TYPE_GLOBAL,
-                        FAKE_SETTING_NAME, UserHandle.USER_OWNER);
+                        FAKE_SETTING_NAME, UserHandle.USER_SYSTEM);
                 assertEquals("Setting value didn't change", FAKE_SETTING_VALUE, firstValue);
 
                 // Set the setting to its second value.
@@ -56,7 +56,7 @@
 
                 // Make sure the setting changed.
                 String secondValue = getStringViaFrontEndApiSetting(SETTING_TYPE_GLOBAL,
-                        FAKE_SETTING_NAME, UserHandle.USER_OWNER);
+                        FAKE_SETTING_NAME, UserHandle.USER_SYSTEM);
                 assertEquals("Setting value didn't change", FAKE_SETTING_VALUE_1, secondValue);
             }
         } finally {
@@ -86,20 +86,20 @@
             for (int i = 0; i < ITERATION_COUNT; i++) {
                 // Set the setting to its first value.
                 setStringViaFrontEndApiSetting(SETTING_TYPE_GLOBAL, FAKE_SETTING_NAME,
-                        FAKE_SETTING_VALUE, UserHandle.USER_OWNER);
+                        FAKE_SETTING_VALUE, UserHandle.USER_SYSTEM);
 
                 // Make sure the setting changed.
                 String firstValue = getStringViaFrontEndApiSetting(SETTING_TYPE_GLOBAL,
-                        FAKE_SETTING_NAME, UserHandle.USER_OWNER);
+                        FAKE_SETTING_NAME, UserHandle.USER_SYSTEM);
                 assertEquals("Setting value didn't change", FAKE_SETTING_VALUE, firstValue);
 
                 // Set the setting to its second value.
                 setStringViaFrontEndApiSetting(SETTING_TYPE_GLOBAL, FAKE_SETTING_NAME,
-                        FAKE_SETTING_VALUE_1, UserHandle.USER_OWNER);
+                        FAKE_SETTING_VALUE_1, UserHandle.USER_SYSTEM);
 
                 // Make sure the setting changed.
                 String secondValue = getStringViaFrontEndApiSetting(SETTING_TYPE_GLOBAL,
-                        FAKE_SETTING_NAME, UserHandle.USER_OWNER);
+                        FAKE_SETTING_NAME, UserHandle.USER_SYSTEM);
                 assertEquals("Setting value didn't change", FAKE_SETTING_VALUE_1, secondValue);
             }
         } finally {
diff --git a/packages/SettingsProvider/test/src/com/android/providers/settings/SettingsProviderTest.java b/packages/SettingsProvider/test/src/com/android/providers/settings/SettingsProviderTest.java
index ad56b9d..8ca1b46 100644
--- a/packages/SettingsProvider/test/src/com/android/providers/settings/SettingsProviderTest.java
+++ b/packages/SettingsProvider/test/src/com/android/providers/settings/SettingsProviderTest.java
@@ -46,40 +46,40 @@
 
     private final Object mLock = new Object();
 
-    public void testSetAndGetGlobalViaFrontEndApiForOwnerUser() throws Exception {
-        performSetAndGetSettingTestViaFrontEndApi(SETTING_TYPE_GLOBAL, UserHandle.USER_OWNER);
+    public void testSetAndGetGlobalViaFrontEndApiForSystemUser() throws Exception {
+        performSetAndGetSettingTestViaFrontEndApi(SETTING_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
     }
 
-    public void testSetAndGetGlobalViaFrontEndApiForNonOwnerUser() throws Exception {
-        if (mSecondaryUserId == UserHandle.USER_OWNER) {
+    public void testSetAndGetGlobalViaFrontEndApiForNonSystemUser() throws Exception {
+        if (mSecondaryUserId == UserHandle.USER_SYSTEM) {
             Log.w(LOG_TAG, "No secondary user. Skipping "
-                    + "testSetAndGetGlobalViaFrontEndApiForNonOwnerUser");
+                    + "testSetAndGetGlobalViaFrontEndApiForNonSystemUser");
             return;
         }
         performSetAndGetSettingTestViaFrontEndApi(SETTING_TYPE_GLOBAL, mSecondaryUserId);
     }
 
-    public void testSetAndGetSecureViaFrontEndApiForOwnerUser() throws Exception {
-        performSetAndGetSettingTestViaFrontEndApi(SETTING_TYPE_SECURE, UserHandle.USER_OWNER);
+    public void testSetAndGetSecureViaFrontEndApiForSystemUser() throws Exception {
+        performSetAndGetSettingTestViaFrontEndApi(SETTING_TYPE_SECURE, UserHandle.USER_SYSTEM);
     }
 
-    public void testSetAndGetSecureViaFrontEndApiForNonOwnerUser() throws Exception {
-        if (mSecondaryUserId == UserHandle.USER_OWNER) {
+    public void testSetAndGetSecureViaFrontEndApiForNonSystemUser() throws Exception {
+        if (mSecondaryUserId == UserHandle.USER_SYSTEM) {
             Log.w(LOG_TAG, "No secondary user. Skipping "
-                    + "testSetAndGetSecureViaFrontEndApiForNonOwnerUser");
+                    + "testSetAndGetSecureViaFrontEndApiForNonSystemUser");
             return;
         }
         performSetAndGetSettingTestViaFrontEndApi(SETTING_TYPE_SECURE, mSecondaryUserId);
     }
 
-    public void testSetAndGetSystemViaFrontEndApiForOwnerUser() throws Exception {
-        performSetAndGetSettingTestViaFrontEndApi(SETTING_TYPE_SYSTEM, UserHandle.USER_OWNER);
+    public void testSetAndGetSystemViaFrontEndApiForSystemUser() throws Exception {
+        performSetAndGetSettingTestViaFrontEndApi(SETTING_TYPE_SYSTEM, UserHandle.USER_SYSTEM);
     }
 
-    public void testSetAndGetSystemViaFrontEndApiForNonOwnerUser() throws Exception {
-        if (mSecondaryUserId == UserHandle.USER_OWNER) {
+    public void testSetAndGetSystemViaFrontEndApiForNonSystemUser() throws Exception {
+        if (mSecondaryUserId == UserHandle.USER_SYSTEM) {
             Log.w(LOG_TAG, "No secondary user. Skipping "
-                    + "testSetAndGetSystemViaFrontEndApiForNonOwnerUser");
+                    + "testSetAndGetSystemViaFrontEndApiForNonSystemUser");
             return;
         }
         performSetAndGetSettingTestViaFrontEndApi(SETTING_TYPE_SYSTEM, mSecondaryUserId);
@@ -357,7 +357,7 @@
             public void run() {
                 insertStringViaProviderApi(type, name, value, withTableRowUri);
             }
-        }, type, name, value, UserHandle.USER_OWNER);
+        }, type, name, value, UserHandle.USER_SYSTEM);
     }
 
     private void setSettingAndAssertSuccessfulChange(Runnable setCommand, final int type,
diff --git a/packages/Shell/AndroidManifest.xml b/packages/Shell/AndroidManifest.xml
index e00b55e..05591cc 100644
--- a/packages/Shell/AndroidManifest.xml
+++ b/packages/Shell/AndroidManifest.xml
@@ -39,6 +39,7 @@
     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
     <uses-permission android:name="android.permission.BLUETOOTH" />
     <uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />
+    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
     <!-- System tool permissions granted to the shell. -->
     <uses-permission android:name="android.permission.REAL_GET_TASKS" />
     <uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
diff --git a/packages/SystemUI/res/drawable/ic_qs_circle.xml b/packages/SystemUI/res/drawable/ic_qs_circle.xml
new file mode 100644
index 0000000..57223cf
--- /dev/null
+++ b/packages/SystemUI/res/drawable/ic_qs_circle.xml
@@ -0,0 +1,30 @@
+<!--
+    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="64dp"
+        android:height="64dp"
+        android:viewportWidth="2.2"
+        android:viewportHeight="2.2">
+
+    <path
+        android:strokeColor="#4DFFFFFF"
+        android:strokeWidth=".05"
+        android:pathData="M.1,1.1
+        c0,.55  .45,1   1,1
+        c.55,0  1,-.45  1,-1
+        c0,-.55 -.45,-1 -1,-1
+        c-.55,0 -1,.45  -1,1"/>
+</vector>
diff --git a/packages/SystemUI/res/layout/qs_paged_page.xml b/packages/SystemUI/res/layout/qs_paged_page.xml
new file mode 100644
index 0000000..eef08ba
--- /dev/null
+++ b/packages/SystemUI/res/layout/qs_paged_page.xml
@@ -0,0 +1,22 @@
+<?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.
+-->
+<view
+    class="com.android.systemui.qs.PagedTileLayout$TilePage"
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/tile_page"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content" />
diff --git a/packages/SystemUI/res/layout/qs_paged_tile_layout.xml b/packages/SystemUI/res/layout/qs_paged_tile_layout.xml
new file mode 100644
index 0000000..6c236ea
--- /dev/null
+++ b/packages/SystemUI/res/layout/qs_paged_tile_layout.xml
@@ -0,0 +1,53 @@
+<?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.qs.PagedTileLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content">
+
+    <view
+        class="com.android.systemui.qs.PagedTileLayout$FirstPage"
+        android:id="@+id/first_page"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:orientation="vertical">
+
+        <com.android.systemui.qs.QuickTileLayout
+            android:id="@+id/quick_tile_layout"
+            android:layout_width="match_parent"
+            android:layout_height="@dimen/qs_quick_actions_height"
+            android:orientation="horizontal"
+            android:paddingLeft="@dimen/qs_quick_actions_padding"
+            android:paddingRight="@dimen/qs_quick_actions_padding" />
+
+        <view
+            class="com.android.systemui.qs.PagedTileLayout$TilePage"
+            android:id="@+id/tile_page"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content" />
+
+    </view>
+
+    <com.android.systemui.qs.PageIndicator
+        android:id="@+id/page_indicator"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_gravity="center_horizontal|bottom"
+        android:gravity="center" />
+
+</com.android.systemui.qs.PagedTileLayout>
diff --git a/packages/SystemUI/res/layout/qs_tile_layout.xml b/packages/SystemUI/res/layout/qs_tile_layout.xml
new file mode 100644
index 0000000..b5d1a1e
--- /dev/null
+++ b/packages/SystemUI/res/layout/qs_tile_layout.xml
@@ -0,0 +1,21 @@
+<?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.qs.TileLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_height="wrap_content"
+    android:layout_width="match_parent" />
diff --git a/packages/SystemUI/res/values-af/strings.xml b/packages/SystemUI/res/values-af/strings.xml
index 9294a16..1473f24 100644
--- a/packages/SystemUI/res/values-af/strings.xml
+++ b/packages/SystemUI/res/values-af/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Program is nie op jou toestel geïnstalleer nie"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Wys horlosiesekondes"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Wys horlosiesekondes op die statusbalk. Sal batterylewe dalk beïnvloed."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Herrangskik Kitsinstellings"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Wys helderheid in Kitsinstellings"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Gebruik verdeling in Kitsinstellings"</string>
+    <string name="experimental" msgid="6198182315536726162">"Eksperimenteel"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-am/strings.xml b/packages/SystemUI/res/values-am/strings.xml
index 13f768d..7ab6af9 100644
--- a/packages/SystemUI/res/values-am/strings.xml
+++ b/packages/SystemUI/res/values-am/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"መተግበሪያ በእርስዎ መሣሪያ ላይ አልተጫነም"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"የሰዓት ሰከንዶችን አሳይ"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"የሰዓት ሰከንዶችን በሁኔታ አሞሌ ውስጥ አሳይ። በባትሪ ዕድሜ ላይ ተጽዕኖ ሊኖርው ይችል ይሆናል።"</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"ፈጣን ቅንብሮችን ዳግም ያደራጁ"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"በፈጣን ቅንብሮች ውስጥ ብሩህነትን አሳይ"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"በፈጣን ቅንብሮች ውስጥ ምልክት መጥሪያን ይጠቀሙ"</string>
+    <string name="experimental" msgid="6198182315536726162">"የሙከራ"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-ar/strings.xml b/packages/SystemUI/res/values-ar/strings.xml
index 6889a43..bd02650 100644
--- a/packages/SystemUI/res/values-ar/strings.xml
+++ b/packages/SystemUI/res/values-ar/strings.xml
@@ -439,4 +439,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"التطبيق غير مثبّت على جهازك"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"عرض ثواني الساعة"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"عرض ثواني الساعة في شريط الحالة. قد يؤثر ذلك في عمر البطارية."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"إعادة ترتيب الإعدادات السريعة"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"عرض السطوع في الإعدادات السريعة"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"استخدام ترقيم الصفحات في الإعدادات السريعة"</string>
+    <string name="experimental" msgid="6198182315536726162">"إعدادات تجريبية"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-az-rAZ/strings.xml b/packages/SystemUI/res/values-az-rAZ/strings.xml
index 51513bd..a0d7d2b 100644
--- a/packages/SystemUI/res/values-az-rAZ/strings.xml
+++ b/packages/SystemUI/res/values-az-rAZ/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Tətbiq cihazınızda quraşdırılmayıb"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Saatın saniyəsini göstərin"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Saatın saniyəsini status panelində göstərin. Batareyaya təsir edə bilər."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Sürətli Ayarları yenidən tənzimləyin"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Sürətli ayarlarda parlaqlılığı göstərin"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Sürətli Ayarlarda səhifə nömrələməsindən istifadə edin"</string>
+    <string name="experimental" msgid="6198182315536726162">"Eksperimental"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-bg/strings.xml b/packages/SystemUI/res/values-bg/strings.xml
index 014d802..a14d0fc 100644
--- a/packages/SystemUI/res/values-bg/strings.xml
+++ b/packages/SystemUI/res/values-bg/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Приложението не е инсталирано на устройството ви"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Показване на секундите на часовника"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Показване на секундите на часовника в лентата на състоянието. Може да се отрази на живота на батерията."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Пренареждане на бързите настройки"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Показване на яркостта от бързите настройки"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Използване на разделянето на страници от бързите настройки"</string>
+    <string name="experimental" msgid="6198182315536726162">"Експериментални"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-bn-rBD/strings.xml b/packages/SystemUI/res/values-bn-rBD/strings.xml
index 0860019..e819d54 100644
--- a/packages/SystemUI/res/values-bn-rBD/strings.xml
+++ b/packages/SystemUI/res/values-bn-rBD/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"আপনার ডিভাইসে অ্যাপ্লিকেশান ইনস্টল করা নেই"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"ঘড়ির সেকেন্ড দেখায়"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"স্থিতি দন্ডে ঘড়ির সেকেন্ড দেখায়৷ ব্যাটারি লাইফকে প্রভাবিত করতে পারে৷"</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"দ্রুত সেটিংসে পুনরায় সাজান"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"দ্রুত সেটিংসে উজ্জ্বলতা দেখান"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"দ্রুত সেটিংসে পেজিং ব্যবহার করুন"</string>
+    <string name="experimental" msgid="6198182315536726162">"পরীক্ষামূলক"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-ca/strings.xml b/packages/SystemUI/res/values-ca/strings.xml
index 9c32052..7def415f 100644
--- a/packages/SystemUI/res/values-ca/strings.xml
+++ b/packages/SystemUI/res/values-ca/strings.xml
@@ -437,4 +437,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"L\'aplicació no està instal·lada al dispositiu"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Mostra els segons del rellotge"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Mostra els segons del rellotge a la barra d\'estat. Això pot afectar la durada de la bateria."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Torna a ordenar la Configuració ràpida"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Mostra la brillantor a la Configuració ràpida"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Utilitza la paginació a la Configuració ràpida"</string>
+    <string name="experimental" msgid="6198182315536726162">"Experimental"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-cs/strings.xml b/packages/SystemUI/res/values-cs/strings.xml
index 4d96a01..a73947f 100644
--- a/packages/SystemUI/res/values-cs/strings.xml
+++ b/packages/SystemUI/res/values-cs/strings.xml
@@ -439,4 +439,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Aplikace není v zařízení nainstalována."</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Zobrazit sekundovou ručičku"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Na stavovém řádku se bude zobrazovat sekundová ručička. Může být ovlivněna výdrž baterie."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Změnit uspořádání Rychlého nastavení"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Zobrazit jas v Rychlém nastavení"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Použít v Rychlém nastavení stránkování"</string>
+    <string name="experimental" msgid="6198182315536726162">"Experimentální"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-da/strings.xml b/packages/SystemUI/res/values-da/strings.xml
index 7f005a3..6f49187 100644
--- a/packages/SystemUI/res/values-da/strings.xml
+++ b/packages/SystemUI/res/values-da/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Applikationen er ikke installeret på din enhed."</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Vis sekunder"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Vis sekunder i statuslinjen. Dette kan påvirke batteriets levetid."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Omarranger Hurtige indstillinger"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Vis lysstyrke i Hurtige indstillinger"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Brug sidelayout i Hurtige indstillinger"</string>
+    <string name="experimental" msgid="6198182315536726162">"Eksperimentel"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-de/strings.xml b/packages/SystemUI/res/values-de/strings.xml
index 36f3c98..a659077 100644
--- a/packages/SystemUI/res/values-de/strings.xml
+++ b/packages/SystemUI/res/values-de/strings.xml
@@ -437,4 +437,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Die App ist nicht auf Ihrem Gerät installiert."</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Uhrsekunden anzeigen"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Uhrsekunden in der Statusleiste anzeigen. Kann sich auf die Akkulaufzeit auswirken."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Schnelleinstellungen neu anordnen"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Helligkeit in den Schnelleinstellungen anzeigen"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Seitenlayout in den Schnelleinstellungen verwenden"</string>
+    <string name="experimental" msgid="6198182315536726162">"Experimentell"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-el/strings.xml b/packages/SystemUI/res/values-el/strings.xml
index 8e73b21..bcfc458 100644
--- a/packages/SystemUI/res/values-el/strings.xml
+++ b/packages/SystemUI/res/values-el/strings.xml
@@ -437,4 +437,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Η εφαρμογή δεν έχει εγκατασταθεί στη συσκευή σας"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Εμφάνιση δευτερολέπτων ρολογιού"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Εμφάνιση δευτερολέπτων ρολογιού στη γραμμή κατάστασης. Ενδέχεται να επηρεάσει τη διάρκεια ζωής της μπαταρίας."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Αναδιάταξη Γρήγορων ρυθμίσεων"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Εμφάνιση φωτεινότητας στις Γρήγορες ρυθμίσεις"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Χρήση τηλεειδοποίησης στις Γρήγορες ρυθμίσεις"</string>
+    <string name="experimental" msgid="6198182315536726162">"Σε πειραματικό στάδιο"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-en-rAU/strings.xml b/packages/SystemUI/res/values-en-rAU/strings.xml
index 0b49955..fb781d4 100644
--- a/packages/SystemUI/res/values-en-rAU/strings.xml
+++ b/packages/SystemUI/res/values-en-rAU/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Application is not installed on your device"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Show clock seconds"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Show clock seconds in the status bar. May impact battery life."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Rearrange Quick Settings"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Show brightness in Quick Settings"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Use paging in Quick Settings"</string>
+    <string name="experimental" msgid="6198182315536726162">"Experimental"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-en-rGB/strings.xml b/packages/SystemUI/res/values-en-rGB/strings.xml
index 0b49955..fb781d4 100644
--- a/packages/SystemUI/res/values-en-rGB/strings.xml
+++ b/packages/SystemUI/res/values-en-rGB/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Application is not installed on your device"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Show clock seconds"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Show clock seconds in the status bar. May impact battery life."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Rearrange Quick Settings"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Show brightness in Quick Settings"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Use paging in Quick Settings"</string>
+    <string name="experimental" msgid="6198182315536726162">"Experimental"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-en-rIN/strings.xml b/packages/SystemUI/res/values-en-rIN/strings.xml
index 0b49955..fb781d4 100644
--- a/packages/SystemUI/res/values-en-rIN/strings.xml
+++ b/packages/SystemUI/res/values-en-rIN/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Application is not installed on your device"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Show clock seconds"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Show clock seconds in the status bar. May impact battery life."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Rearrange Quick Settings"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Show brightness in Quick Settings"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Use paging in Quick Settings"</string>
+    <string name="experimental" msgid="6198182315536726162">"Experimental"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-es-rUS/strings.xml b/packages/SystemUI/res/values-es-rUS/strings.xml
index c4dca9c..10834a2 100644
--- a/packages/SystemUI/res/values-es-rUS/strings.xml
+++ b/packages/SystemUI/res/values-es-rUS/strings.xml
@@ -437,4 +437,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"La aplicación no está instalada en el dispositivo"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Mostrar los segundos del reloj"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Muestra los segundos del reloj en la barra de estado. Puede afectar la duración de la batería."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Reorganizar la Configuración rápida"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Mostrar el brillo en la Configuración rápida"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Usar la paginación en la Configuración rápida"</string>
+    <string name="experimental" msgid="6198182315536726162">"Experimental"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-es/strings.xml b/packages/SystemUI/res/values-es/strings.xml
index e316a94..37c81f4 100644
--- a/packages/SystemUI/res/values-es/strings.xml
+++ b/packages/SystemUI/res/values-es/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"La aplicación no está instalada en tu dispositivo"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Mostrar los segundos del reloj"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Muestra los segundos del reloj en la barra de estado. Puede afectar a la duración de la batería."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Reorganizar Ajustes rápidos"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Mostrar brillo en Ajustes rápidos"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Utilizar paginación en Ajustes rápidos"</string>
+    <string name="experimental" msgid="6198182315536726162">"Experimental"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-et-rEE/strings.xml b/packages/SystemUI/res/values-et-rEE/strings.xml
index a151bce..f4218a3 100644
--- a/packages/SystemUI/res/values-et-rEE/strings.xml
+++ b/packages/SystemUI/res/values-et-rEE/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Rakendust pole teie seadmesse installitud"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Kella sekundite kuvamine"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Olekuribal kella sekundite kuvamine. See võib mõjutada aku kasutusaega."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Korralda kiirseaded ümber"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Kuva kiirseadetes heledus"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Kasuta kiirseadetes lehe paigutust"</string>
+    <string name="experimental" msgid="6198182315536726162">"Eksperimentaalne"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-eu-rES/strings.xml b/packages/SystemUI/res/values-eu-rES/strings.xml
index 37cdee7..fbf0eb42 100644
--- a/packages/SystemUI/res/values-eu-rES/strings.xml
+++ b/packages/SystemUI/res/values-eu-rES/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Aplikazioa ez dago gailuan instalatuta"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Erakutsi erlojuko segundoak"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Erakutsi erlojuko segundoak egoera-barran. Baliteke bateria gehiago erabiltzea."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Berrantolatu ezarpen bizkorrak"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Erakutsi ezarpen bizkorren distira"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Erabili ezarpen bizkorretako orriak pasatzeko diseinu berria"</string>
+    <string name="experimental" msgid="6198182315536726162">"Esperimentala"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-fa/strings.xml b/packages/SystemUI/res/values-fa/strings.xml
index 80d0918..fa01c47 100644
--- a/packages/SystemUI/res/values-fa/strings.xml
+++ b/packages/SystemUI/res/values-fa/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"برنامه در دستگاه شما نصب نیست"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"نمایش ثانیه‌های ساعت"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"ثانیه‌های ساعت را در نوار وضعیت نشان می‌دهد. ممکن است بر ماندگاری باتری تأثیر بگذارد."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"ترتیب مجدد در تنظیمات سریع"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"نمایش روشنایی در تنظیمات سریع"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"استفاده از صفحه‌بندی در تنظیمات سریع"</string>
+    <string name="experimental" msgid="6198182315536726162">"آزمایشی"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-fi/strings.xml b/packages/SystemUI/res/values-fi/strings.xml
index b86247a..6c4a029 100644
--- a/packages/SystemUI/res/values-fi/strings.xml
+++ b/packages/SystemUI/res/values-fi/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Sovellusta ei ole asennettu laitteellesi."</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Näytä sekunnit kellossa"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Näytä sekunnit tilapalkin kellossa. Tämä voi vaikuttaa akun kestoon."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Järjestä pika-asetukset uudelleen"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Näytä kirkkaus pika-asetuksissa"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Käytä sivutusta pika-asetuksissa"</string>
+    <string name="experimental" msgid="6198182315536726162">"Kokeellinen"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-fr-rCA/strings.xml b/packages/SystemUI/res/values-fr-rCA/strings.xml
index f99447c..07a2fc3 100644
--- a/packages/SystemUI/res/values-fr-rCA/strings.xml
+++ b/packages/SystemUI/res/values-fr-rCA/strings.xml
@@ -437,4 +437,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"L\'application n\'est pas installée sur votre appareil"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Afficher les secondes sur l\'horloge"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Afficher les secondes sur l\'horloge dans la barre d\'état. Cela peut réduire l\'autonomie de la pile."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Réorganiser les paramètres rapides"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Afficher la luminosité dans les paramètres rapides"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Utiliser la pagination dans les paramètres rapides"</string>
+    <string name="experimental" msgid="6198182315536726162">"Fonctions expérimentales"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-fr/strings.xml b/packages/SystemUI/res/values-fr/strings.xml
index aada1eb..0aa45ae 100644
--- a/packages/SystemUI/res/values-fr/strings.xml
+++ b/packages/SystemUI/res/values-fr/strings.xml
@@ -437,4 +437,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"L\'application n\'est pas installée sur votre appareil."</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Afficher les secondes sur l\'horloge"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Afficher les secondes dans la barre d\'état. Cela risque de réduire l\'autonomie de la batterie."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Réorganiser la fenêtre de configuration rapide"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Afficher la luminosité dans fenêtre de configuration rapide"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Utiliser mise en page dans fenêtre de configuration rapide"</string>
+    <string name="experimental" msgid="6198182315536726162">"Paramètres expérimentaux"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-gl-rES/strings.xml b/packages/SystemUI/res/values-gl-rES/strings.xml
index 951809f..ec64f22 100644
--- a/packages/SystemUI/res/values-gl-rES/strings.xml
+++ b/packages/SystemUI/res/values-gl-rES/strings.xml
@@ -437,4 +437,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"A aplicación non está instalada no teu dispositivo"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Mostrar segundos do reloxo"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Mostra os segundos do reloxo na barra de estado. Pode influír na duración da batería."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Reorganizar Configuración rápida"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Mostrar brillo en Configuración rápida"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Utilizar paxinación en Configuración rápida"</string>
+    <string name="experimental" msgid="6198182315536726162">"Experimental"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-gu-rIN/strings.xml b/packages/SystemUI/res/values-gu-rIN/strings.xml
index 02e4e52..e873c95 100644
--- a/packages/SystemUI/res/values-gu-rIN/strings.xml
+++ b/packages/SystemUI/res/values-gu-rIN/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"તમારા ઉપકરણ પર એપ્લિકેશન ઇન્સ્ટોલ થયેલ નથી"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"ઘડિયાળ સેકન્ડ બતાવો"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"ઘડિયાળ સેકન્ડ સ્થિતિ બારમાં બતાવો. બૅટરીની આવરદા પર અસર કરી શકે છે."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"ઝડપી સેટિંગ્સને ફરીથી ગોઠવો"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"ઝડપી સેટિંગ્સમાં તેજ બતાવો"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"ઝડપી સેટિંગ્સમાં પૃષ્ઠાંકનનો ઉપયોગ કરો"</string>
+    <string name="experimental" msgid="6198182315536726162">"પ્રાયોગિક"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-hi/strings.xml b/packages/SystemUI/res/values-hi/strings.xml
index 3ed090c..83ac46e 100644
--- a/packages/SystemUI/res/values-hi/strings.xml
+++ b/packages/SystemUI/res/values-hi/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"ऐप्लिकेशन आपके डिवाइस पर इंस्टॉल नहीं है"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"घड़ी के सेकंड दिखाएं"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"स्थिति बार में घड़ी के सेकंड दिखाएं. इससे बैटरी के जीवनकाल पर प्रभाव पड़ सकता है."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"त्वरित सेटिंग को पुन: व्यवस्थित करें"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"त्वरित सेटिंग में स्क्रीन की रोशनी दिखाएं"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"त्वरित सेटिंग में पेजिंग का उपयोग करें"</string>
+    <string name="experimental" msgid="6198182315536726162">"प्रयोगात्मक"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-hr/strings.xml b/packages/SystemUI/res/values-hr/strings.xml
index 9698aba..b5d9931 100644
--- a/packages/SystemUI/res/values-hr/strings.xml
+++ b/packages/SystemUI/res/values-hr/strings.xml
@@ -436,4 +436,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Aplikacija nije instalirana na vašem uređaju"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Prikaži sekunde na satu"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Prikazuju se sekunde na satu na traci statusa. Može utjecati na trajanje baterije."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Promijeni raspored Brzih postavki"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Prikaži svjetlinu u Brzim postavkama"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Upotrijebi redni broj stranice u Brzim postavkama"</string>
+    <string name="experimental" msgid="6198182315536726162">"Eksperimentalno"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-hu/strings.xml b/packages/SystemUI/res/values-hu/strings.xml
index 37cb0a8..79390a6 100644
--- a/packages/SystemUI/res/values-hu/strings.xml
+++ b/packages/SystemUI/res/values-hu/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Az alkalmazás nincs telepítve eszközén."</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Másodpercek megjelenítése az órán"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Másodpercek megjelenítése az állapotsor óráján. Ez hatással lehet az akkumulátor üzemidejére."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Gyorsbeállítások átrendezése"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Fényerő megjelenítése a gyorsbeállításokban"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Oldalelrendezés használata a gyorsbeállításokban"</string>
+    <string name="experimental" msgid="6198182315536726162">"Kísérleti"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-hy-rAM/strings.xml b/packages/SystemUI/res/values-hy-rAM/strings.xml
index e31cf49..9f48b26 100644
--- a/packages/SystemUI/res/values-hy-rAM/strings.xml
+++ b/packages/SystemUI/res/values-hy-rAM/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Հավելվածը տեղադրված չէ սարքի վրա"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Ցույց տալ ժամացույցի վայրկյանները"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Ցույց տալ ժամացույցի վայրկյանները կարգավիճակի տողում: Կարող է ազդել մարտկոցի աշխատանքի ժամանակի վրա:"</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Վերադասավորել Արագ կարգավորումները"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Ցույց տալ պայծառությունն Արագ կարգավորումներում"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Օգտագործել էջերի դասավորությունը Արագ կարգավորումներում"</string>
+    <string name="experimental" msgid="6198182315536726162">"Փորձնական"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-in/strings.xml b/packages/SystemUI/res/values-in/strings.xml
index ad8141b..60024115 100644
--- a/packages/SystemUI/res/values-in/strings.xml
+++ b/packages/SystemUI/res/values-in/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Aplikasi tidak dipasang di perangkat"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Tampilkan detik jam"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Tampilkan detik jam di bilah status. Dapat memengaruhi masa pakai baterai."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Atur Ulang Setelan Cepat"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Tampilkan kecerahan di Setelan Cepat"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Gunakan pembagian laman di Setelan Cepat"</string>
+    <string name="experimental" msgid="6198182315536726162">"Eksperimental"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-is-rIS/strings.xml b/packages/SystemUI/res/values-is-rIS/strings.xml
index 02caf17..e8e0530 100644
--- a/packages/SystemUI/res/values-is-rIS/strings.xml
+++ b/packages/SystemUI/res/values-is-rIS/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Forritið er ekki uppsett í tækinu."</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Sýna sekúndur á klukku"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Sýna sekúndur á klukku í stöðustikunni. Getur haft áhrif á endingu rafhlöðu."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Endurraða flýtistillingum"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Sýna birtustig í flýtistillingum"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Nota síðuskoðun í flýtistillingum"</string>
+    <string name="experimental" msgid="6198182315536726162">"Tilraunastillingar"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-it/strings.xml b/packages/SystemUI/res/values-it/strings.xml
index ab5d8be..8ee5f07 100644
--- a/packages/SystemUI/res/values-it/strings.xml
+++ b/packages/SystemUI/res/values-it/strings.xml
@@ -437,4 +437,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Applicazione non installata sul dispositivo"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Mostra i secondi nell\'orologio"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Mostra i secondi nell\'orologio nella barra di stato. Ciò potrebbe ridurre la durata della carica della batteria."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Riorganizza Impostazioni rapide"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Mostra luminosità in Impostazioni rapide"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Utilizza nuovo layout in Impostazioni rapide"</string>
+    <string name="experimental" msgid="6198182315536726162">"Sperimentali"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-iw/strings.xml b/packages/SystemUI/res/values-iw/strings.xml
index 9dad8aa..e38501d 100644
--- a/packages/SystemUI/res/values-iw/strings.xml
+++ b/packages/SystemUI/res/values-iw/strings.xml
@@ -437,4 +437,12 @@
     <string name="activity_not_found" msgid="348423244327799974">"האפליקציה אינה מותקנת במכשיר"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"הצג שניות בשעון"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"הצג שניות בשעון בשורת הסטטוס. פעולה זו עשויה להשפיע על אורך חיי הסוללה."</string>
+    <!-- no translation found for qs_rearrange (8060918697551068765) -->
+    <skip />
+    <!-- no translation found for show_brightness (6613930842805942519) -->
+    <skip />
+    <!-- no translation found for qs_paging (7020133150248666132) -->
+    <skip />
+    <!-- no translation found for experimental (6198182315536726162) -->
+    <skip />
 </resources>
diff --git a/packages/SystemUI/res/values-ja/strings.xml b/packages/SystemUI/res/values-ja/strings.xml
index 9cb2fc8..a73f4c1 100644
--- a/packages/SystemUI/res/values-ja/strings.xml
+++ b/packages/SystemUI/res/values-ja/strings.xml
@@ -437,4 +437,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"アプリが端末にインストールされていません"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"時計の秒を表示"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"ステータスバーに時計の秒を表示します。電池使用量に影響する可能性があります。"</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"クイック設定を並べ替え"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"クイック設定に明るさ調整バーを表示する"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"クイック設定でページ設定を使用する"</string>
+    <string name="experimental" msgid="6198182315536726162">"試験運用版"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-ka-rGE/strings.xml b/packages/SystemUI/res/values-ka-rGE/strings.xml
index a9728b2..f1035a8 100644
--- a/packages/SystemUI/res/values-ka-rGE/strings.xml
+++ b/packages/SystemUI/res/values-ka-rGE/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"აპლიკაცია თქვენს მოწყობილობაზე დაყენებული არ არის"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"საათის წამების ჩვენება"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"საათის წამების ჩვენება სტატუსის ზოლში. შეიძლება გავლენა იქონიოს ბატარეაზე."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"სწრაფი პარამეტრების გადაწყობა"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"სიკაშკაშის ჩვენება სწრაფ პარამეტრებში"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"გამოიყენეთ გვერდების დანომვრა სწრაფ პარამეტრებში"</string>
+    <string name="experimental" msgid="6198182315536726162">"ექსპერიმენტული"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-kk-rKZ/strings.xml b/packages/SystemUI/res/values-kk-rKZ/strings.xml
index 1d61372..8b82e5f 100644
--- a/packages/SystemUI/res/values-kk-rKZ/strings.xml
+++ b/packages/SystemUI/res/values-kk-rKZ/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Қолданба құрылғыда орнатылмаған"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Сағат секундтарын көрсету"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Күйін көрсету жолағында сағат секундтарын көрсету. Батареяның қызмет көрсету мерзіміне әсер етуі мүмкін."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Жылдам параметрлерді қайта реттеу"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Жылдам параметрлерде жарықтықты көрсету"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Жылдам параметрлерде беттерді нөмірлеуді пайдалану"</string>
+    <string name="experimental" msgid="6198182315536726162">"Эксперименттік"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-km-rKH/strings.xml b/packages/SystemUI/res/values-km-rKH/strings.xml
index c233f83..b8db00f 100644
--- a/packages/SystemUI/res/values-km-rKH/strings.xml
+++ b/packages/SystemUI/res/values-km-rKH/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"កម្មវិធីមិនបានដំឡើងនៅលើឧបករណ៍របស់អ្នកទេ"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"បង្ហាញវិនាទី"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"បង្ហាញវិនាទីនៅលើរបារស្ថានភាពអាចនឹងប៉ះពាល់ដល់ថាមពលថ្ម។"</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"រៀបចំការកំណត់រហ័សឡើងវិញ"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"បង្ហាញកម្រិតពន្លឺនៅក្នុងការកំណត់រហ័ស"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"ប្រើការចុះទំព័រនៅក្នុងការកំណត់រហ័ស"</string>
+    <string name="experimental" msgid="6198182315536726162">"ពិសោធន៍"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-kn-rIN/strings.xml b/packages/SystemUI/res/values-kn-rIN/strings.xml
index d486e56..316c04f 100644
--- a/packages/SystemUI/res/values-kn-rIN/strings.xml
+++ b/packages/SystemUI/res/values-kn-rIN/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"ನಿಮ್ಮ ಸಾಧನದಲ್ಲಿ ಅಪ್ಲಿಕೇಶನ್ ಅನ್ನು ಸ್ಥಾಪಿಸಲಾಗಿಲ್ಲ"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"ಗಡಿಯಾರದ ಸೆಕೆಂಡುಗಳನ್ನು ತೋರಿಸು"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"ಸ್ಥಿತಿ ಪಟ್ಟಿಯಲ್ಲಿ ಗಡಿಯಾರ ಸೆಕೆಂಡುಗಳನ್ನು ತೋರಿಸು. ಇದಕ್ಕೆ ಬ್ಯಾಟರಿ ಬಾಳಿಕೆಯು ಪರಿಣಾಮಬೀರಬಹುದು."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"ತ್ವರಿತ ಸೆಟ್ಟಿಂಗ್‌‌ಗಳನ್ನು ಮರುಹೊಂದಿಸಿ"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"ತ್ವರಿತ ಸೆಟ್ಟಿಂಗ್‌‌ಗಳಲ್ಲಿ ಪ್ರಖರತೆಯನ್ನು ತೋರಿಸಿ"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"ತ್ವರಿತ ಸೆಟ್ಟಿಂಗ್‌‌ಗಳಲ್ಲಿ ಪುಟಗಳನ್ನು ಬಳಸಿ"</string>
+    <string name="experimental" msgid="6198182315536726162">"ಪ್ರಾಯೋಗಿಕ"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-ko/strings.xml b/packages/SystemUI/res/values-ko/strings.xml
index 3505754..d7f1543 100644
--- a/packages/SystemUI/res/values-ko/strings.xml
+++ b/packages/SystemUI/res/values-ko/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"기기에 애플리케이션이 설치되어 있지 않습니다."</string>
     <string name="clock_seconds" msgid="7689554147579179507">"시계 초 단위 표시"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"상태 표시줄에 시계 초 단위를 표시합니다. 배터리 수명에 영향을 줄 수도 있습니다."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"빠른 설정 재정렬"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"빠른 설정에서 밝기 표시"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"빠른 설정에서 페이지 레이아웃 사용"</string>
+    <string name="experimental" msgid="6198182315536726162">"베타"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-ky-rKG/strings.xml b/packages/SystemUI/res/values-ky-rKG/strings.xml
index 968a65a..efe699b 100644
--- a/packages/SystemUI/res/values-ky-rKG/strings.xml
+++ b/packages/SystemUI/res/values-ky-rKG/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Колдонмо сиздин түзмөгүңүздө орнотулган эмес"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Сааттын секунддары көрсөтүлсүн"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Абал тилкесинен сааттын секунддары көрсөтүлсүн. Батареянын кубаты көбүрөөк сарпталышы мүмкүн."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Ыкчам жөндөөлөрдү кайра коюу"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Ыкчам жөндөөлөрдөн жарыктыгын көрсөтүү"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Ыкчам жөндөөлөрдөн баракты номерлөөнү колдонуу"</string>
+    <string name="experimental" msgid="6198182315536726162">"Сынамык"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-lo-rLA/strings.xml b/packages/SystemUI/res/values-lo-rLA/strings.xml
index f34c70e..50b4522 100644
--- a/packages/SystemUI/res/values-lo-rLA/strings.xml
+++ b/packages/SystemUI/res/values-lo-rLA/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"ແອັບ​ພ​ລິ​ເຄ​ຊັນ​ບໍ່​ຖືກ​ຕິດ​ຕັ້ງ​ຢູ່​ໃນ​ອຸ​ປະ​ກອນ​ຂອງ​ທ່ານ"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"ສະ​ແດງວິ​ນາ​ທີ​ໂມງ"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"ສະ​ແດງວິ​ນາ​ທີ​ໂມງ​ຢູ່​ໃນ​ແຖບ​ສະ​ຖາ​ນະ. ອາດ​ຈະ​ມີ​ຜົນ​ກະ​ທົບ​ຕໍ່​ອາ​ຍຸ​ແບັດ​ເຕີ​ຣີ."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"ຈັດ​ວາງ​ການ​ຕັ້ງ​ຄ່າ​ດ່ວນ​ຄືນ​ໃໝ່"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"ສະ​ແດງ​ຄວາມ​ແຈ້ງ​ຢູ່​ໃນ​ການ​ຕັ້ງ​ຄ່າ​ດ່ວນ"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"ໃຊ້​ການ​ໃສ່​ໜ້າ​ຢູ່​ໃນ​ການ​ຕັ້ງ​ຄ່າ​ດ່ວນ"</string>
+    <string name="experimental" msgid="6198182315536726162">"ຍັງຢູ່ໃນການທົດລອງ"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-lt/strings.xml b/packages/SystemUI/res/values-lt/strings.xml
index cf70d6b..9082778 100644
--- a/packages/SystemUI/res/values-lt/strings.xml
+++ b/packages/SystemUI/res/values-lt/strings.xml
@@ -437,4 +437,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Programa neįdiegta įrenginyje"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Rodyti laikrodžio sekundes"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Rodyti laikrodžio sekundes būsenos juostoje. Tai gali paveikti akumuliatoriaus naudojimo laiką."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Pertvarkyti sparčiuosius nustatymus"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Rodyti skaistį sparčiuosiuose nustatymuose"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Naudoti puslapių kaitą sparčiuosiuose nustatymuose"</string>
+    <string name="experimental" msgid="6198182315536726162">"Eksperimentinė versija"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-lv/strings.xml b/packages/SystemUI/res/values-lv/strings.xml
index 5e2db80..5fb4bb0 100644
--- a/packages/SystemUI/res/values-lv/strings.xml
+++ b/packages/SystemUI/res/values-lv/strings.xml
@@ -436,4 +436,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Lietojumprogramma nav instalēta jūsu ierīcē."</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Rādīt pulksteņa sekundes"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Statusa joslā rādīt pulksteņa sekundes. Var ietekmēt akumulatora darbības laiku."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Pārkārtot ātros iestatījumus"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Rādīt spilgtumu ātrajos iestatījumos"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Izmantot lapošanu ātrajos iestatījumos"</string>
+    <string name="experimental" msgid="6198182315536726162">"Eksperimentāli"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-mk-rMK/strings.xml b/packages/SystemUI/res/values-mk-rMK/strings.xml
index fe781b6..8571b3f 100644
--- a/packages/SystemUI/res/values-mk-rMK/strings.xml
+++ b/packages/SystemUI/res/values-mk-rMK/strings.xml
@@ -437,4 +437,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Апликацијата не е инсталирана на уредот"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Прикажи ги секундите на часовникот"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Прикажи ги секундите на часовникот на статусната лента. Може да влијае на траењето на батеријата."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Преуредете ги Брзи поставки"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Прикажете ја осветленоста во Брзи поставки"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Користете прелистување страници во Брзи поставки"</string>
+    <string name="experimental" msgid="6198182315536726162">"Експериментално"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-ml-rIN/strings.xml b/packages/SystemUI/res/values-ml-rIN/strings.xml
index 6e6fc55..4b11962 100644
--- a/packages/SystemUI/res/values-ml-rIN/strings.xml
+++ b/packages/SystemUI/res/values-ml-rIN/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"നിങ്ങളുടെ ഉപകരണത്തിൽ അപ്ലിക്കേഷൻ ഇൻസ്റ്റാൾ ചെയ്തിട്ടില്ല"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"ക്ലോക്ക് സെക്കൻഡ് കാണിക്കുക"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"സ്റ്റാറ്റസ് ബാറിൽ ക്ലോക്ക് സെക്കൻഡ് കാണിക്കുന്നത് ബാറ്ററിയുടെ ലൈഫിനെ ബാധിക്കാം."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"ദ്രുത ക്രമീകരണം പുനഃസജ്ജീകരിക്കുക"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"ദ്രുത ക്രമീകരണത്തിൽ തെളിച്ചം കാണിക്കുക"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"ദ്രുത്ര ക്രമീകരണത്തിൽ പേജിംഗ് ഉപയോഗിക്കുക"</string>
+    <string name="experimental" msgid="6198182315536726162">"പരീക്ഷണാത്മകം!"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-mn-rMN/strings.xml b/packages/SystemUI/res/values-mn-rMN/strings.xml
index 65250bb..d1dbd6d 100644
--- a/packages/SystemUI/res/values-mn-rMN/strings.xml
+++ b/packages/SystemUI/res/values-mn-rMN/strings.xml
@@ -433,4 +433,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Апп-ыг таны төхөөрөмжид суулгаагүй байна"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Цагийн секундыг харуулах"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Статус талбарт цагийн секундыг харуулах. Энэ нь тэжээлийн цэнэгт нөлөөлж болно."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Түргэн тохиргоог дахин засварлах"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Түргэн тохиргоонд гэрэлтүүлэг харах"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Хуудаслалтыг түргэн тохиргоонд ашиглаарай"</string>
+    <string name="experimental" msgid="6198182315536726162">"Туршилтын"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-mr-rIN/strings.xml b/packages/SystemUI/res/values-mr-rIN/strings.xml
index 0f71b4b..fe50f90 100644
--- a/packages/SystemUI/res/values-mr-rIN/strings.xml
+++ b/packages/SystemUI/res/values-mr-rIN/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"अनुप्रयोग आपल्या डिव्हाइसवर स्थापित केलेला नाही"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"घड्‍याळ सेकंद दर्शवा"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"स्टेटस बारमध्‍ये घड्‍याळ सेकंद दर्शवा. कदाचित बॅटरी आयुष्‍य प्रभावित होऊ शकते."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"दृत सेटिंग्जची पुनर्रचना करा"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"दृत सेटिंग्जमध्‍ये चमक दर्शवा"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"द्रुत सेटिंग्जमध्ये लिखाण वापरा"</string>
+    <string name="experimental" msgid="6198182315536726162">"प्रायोगिक"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-ms-rMY/strings.xml b/packages/SystemUI/res/values-ms-rMY/strings.xml
index 596275c..01f7edf 100644
--- a/packages/SystemUI/res/values-ms-rMY/strings.xml
+++ b/packages/SystemUI/res/values-ms-rMY/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Aplikasi tidak dipasang pada peranti anda"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Tunjukkan saat jam"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Tunjukkan saat jam dalam bar status. Mungkin menjejaskan hayat bateri."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Susun Semula Tetapan Pantas"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Tunjukkan kecerahan dalam Tetapan Pantas"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Gunakan penghalaman dalam Tetapan Pantas"</string>
+    <string name="experimental" msgid="6198182315536726162">"Percubaan"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-my-rMM/strings.xml b/packages/SystemUI/res/values-my-rMM/strings.xml
index 3147d0c..d0eaccf 100644
--- a/packages/SystemUI/res/values-my-rMM/strings.xml
+++ b/packages/SystemUI/res/values-my-rMM/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"အပလီကေးရှင်းကို သင်၏ ကိရိယာထဲသို့ တပ်ဆင်မပေးရသေးပါ။"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"နာရီ စက္ကန့်များကို ပြရန်"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"အခြေအနေပြနေရာမှာ နာရီ စက္ကန့်များကို ပြပါ။ ဘက်ထရီ သက်တမ်းကို အကျိုးသက်ရောက်နိုင်တယ်။"</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"အမြန် ဆက်တင်များကို ပြန်စီစဉ်ရန်"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"အမြန် ဆက်တင်များထဲက တောက်ပမှုကို ပြရန်"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"အမြန် ဆက်တင်များထဲတွင် စာမျက်နှာ ပုံစံချမှုကို အသုံးပြုပါ"</string>
+    <string name="experimental" msgid="6198182315536726162">"စမ်းသပ်ရေး"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-nb/strings.xml b/packages/SystemUI/res/values-nb/strings.xml
index f1bfbe1..e582039 100644
--- a/packages/SystemUI/res/values-nb/strings.xml
+++ b/packages/SystemUI/res/values-nb/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Appen er ikke installert på enheten din"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Vis sekunder på klokken"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Vis sekunder i statusfeltet på klokken. Det kan påvirke batteritiden."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Omorganiser hurtiginnstillingene"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Vis lysstyrke i hurtiginnstillingene"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Bruk sidetall i hurtiginnstillingene"</string>
+    <string name="experimental" msgid="6198182315536726162">"På forsøksstadiet"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-ne-rNP/strings.xml b/packages/SystemUI/res/values-ne-rNP/strings.xml
index 461a15f..c3dc703 100644
--- a/packages/SystemUI/res/values-ne-rNP/strings.xml
+++ b/packages/SystemUI/res/values-ne-rNP/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"तपाईँको यन्त्रमा अनुप्रयोग स्थापना भएको छैन"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"घडीमा सेकेन्ड देखाउनुहोस्"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"वस्तुस्थिति पट्टीको घडीमा सेकेन्ड देखाउनुहोस्। ब्याट्री आयु प्रभावित हुन सक्छ।"</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"द्रुत सेटिङहरू पुनः व्यवस्थित गर्नुहोस्"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"द्रुत सेटिङहरूमा उज्यालो देखाउनुहोस्"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"द्रुत सेटिङहरूमा पेजिंग प्रयोग गर्नुहोस्"</string>
+    <string name="experimental" msgid="6198182315536726162">"प्रयोगात्मक"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-nl/strings.xml b/packages/SystemUI/res/values-nl/strings.xml
index b74f91d..13f02d5 100644
--- a/packages/SystemUI/res/values-nl/strings.xml
+++ b/packages/SystemUI/res/values-nl/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Deze app is niet geïnstalleerd op uw apparaat"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Klokseconden weergeven"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Klokseconden op de statusbalk weergeven. Kan van invloed zijn op de accuduur."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Snelle instellingen opnieuw indelen"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Helderheid weergeven in Snelle instellingen"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Paginering gebruiken in Snelle instellingen"</string>
+    <string name="experimental" msgid="6198182315536726162">"Experimenteel"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-pa-rIN/strings.xml b/packages/SystemUI/res/values-pa-rIN/strings.xml
index 06746cc..ef3d2f3 100644
--- a/packages/SystemUI/res/values-pa-rIN/strings.xml
+++ b/packages/SystemUI/res/values-pa-rIN/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"ਐਪਲੀਕੇਸ਼ਨ ਤੁਹਾਡੀ ਡਿਵਾਈਸ ਤੇ ਇੰਸਟੌਲ ਨਹੀਂ ਕੀਤੀ ਗਈ ਹੈ"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"ਘੜੀ ਸਕਿੰਟ ਦਿਖਾਓ"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"ਸਥਿਤੀ ਬਾਰ ਵਿੱਚ ਘੜੀ ਸਕਿੰਟ ਦਿਖਾਓ। ਬੈਟਰੀ ਸਮਰੱਥਾ ਤੇ ਅਸਰ ਪੈ ਸਕਦਾ ਹੈ।"</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"ਤਤਕਾਲ ਸੈਟਿੰਗਾਂ ਨੂੰ ਦੁਬਾਰਾ ਕ੍ਰਮ ਦਿਓ"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"ਤਤਕਾਲ ਸੈਟਿੰਗਾਂ ਵਿੱਚ ਚਮਕ ਦਿਖਾਓ"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"ਤਤਕਾਲ ਸੈਟਿੰਗਾਂ ਵਿੱਚ ਪੇਜਿੰਗ ਵਰਤੋ"</string>
+    <string name="experimental" msgid="6198182315536726162">"ਪ੍ਰਯੋਗਾਤਮਿਕ"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-pl/strings.xml b/packages/SystemUI/res/values-pl/strings.xml
index 66100c1..8c2c5c4 100644
--- a/packages/SystemUI/res/values-pl/strings.xml
+++ b/packages/SystemUI/res/values-pl/strings.xml
@@ -437,4 +437,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Aplikacja nie jest zainstalowana na urządzeniu"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Pokaż sekundy na zegarku"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Pokaż sekundy na zegarku na pasku stanu. Może mieć wpływ na czas pracy baterii."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Uporządkuj Szybkie ustawienia"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Pokaż jasność w Szybkich ustawieniach"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Użyj stronicowania w Szybkich ustawieniach"</string>
+    <string name="experimental" msgid="6198182315536726162">"Eksperymentalne"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-pt-rBR/strings.xml b/packages/SystemUI/res/values-pt-rBR/strings.xml
index 53cea01..3803b81 100644
--- a/packages/SystemUI/res/values-pt-rBR/strings.xml
+++ b/packages/SystemUI/res/values-pt-rBR/strings.xml
@@ -437,4 +437,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"O app não está instalado no seu dispositivo"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Mostrar segundos do relógio"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Mostrar segundos do relógio na barra de status. Pode afetar a duração da bateria."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Reorganizar \"Configurações rápidas\""</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Mostrar brilho nas \"Configurações rápidas\""</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Usar paginação nas \"Configurações rápidas\""</string>
+    <string name="experimental" msgid="6198182315536726162">"Experimentais"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-pt-rPT/strings.xml b/packages/SystemUI/res/values-pt-rPT/strings.xml
index 5006413..be42e8a 100644
--- a/packages/SystemUI/res/values-pt-rPT/strings.xml
+++ b/packages/SystemUI/res/values-pt-rPT/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"A aplicação não está instalada no dispositivo"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Mostrar segundos do relógio"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Mostrar segundos do relógio na barra de estado. Pode afetar a autonomia da bateria."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Reorganizar Definições rápidas"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Mostrar luminosidade nas Definições rápidas"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Utilizar paginação nas Definições rápidas"</string>
+    <string name="experimental" msgid="6198182315536726162">"Experimental"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-pt/strings.xml b/packages/SystemUI/res/values-pt/strings.xml
index 53cea01..3803b81 100644
--- a/packages/SystemUI/res/values-pt/strings.xml
+++ b/packages/SystemUI/res/values-pt/strings.xml
@@ -437,4 +437,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"O app não está instalado no seu dispositivo"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Mostrar segundos do relógio"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Mostrar segundos do relógio na barra de status. Pode afetar a duração da bateria."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Reorganizar \"Configurações rápidas\""</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Mostrar brilho nas \"Configurações rápidas\""</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Usar paginação nas \"Configurações rápidas\""</string>
+    <string name="experimental" msgid="6198182315536726162">"Experimentais"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-ro/strings.xml b/packages/SystemUI/res/values-ro/strings.xml
index d97e16b..bb8f2c3 100644
--- a/packages/SystemUI/res/values-ro/strings.xml
+++ b/packages/SystemUI/res/values-ro/strings.xml
@@ -436,4 +436,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Aplicația nu este instalată pe dispozitiv"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Afișează secundele pe ceas"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Afișează secundele pe ceas în bara de stare. Poate afecta autonomia bateriei."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Rearanjați Setările rapide"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Afișați luminozitatea în Setările rapide"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Folosiți paginarea în Setările rapide"</string>
+    <string name="experimental" msgid="6198182315536726162">"Experimentale"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-ru/strings.xml b/packages/SystemUI/res/values-ru/strings.xml
index d67ad38..9609f40 100644
--- a/packages/SystemUI/res/values-ru/strings.xml
+++ b/packages/SystemUI/res/values-ru/strings.xml
@@ -269,7 +269,7 @@
     <string name="quick_settings_wifi_no_network" msgid="2221993077220856376">"Нет сети"</string>
     <string name="quick_settings_wifi_off_label" msgid="7558778100843885864">"Wi-Fi выкл."</string>
     <string name="quick_settings_wifi_detail_empty_text" msgid="269990350383909226">"Не удалось найти доступные сети Wi-Fi"</string>
-    <string name="quick_settings_cast_title" msgid="7709016546426454729">"Wi-Fi-монитор"</string>
+    <string name="quick_settings_cast_title" msgid="7709016546426454729">"Трансляция"</string>
     <string name="quick_settings_casting" msgid="6601710681033353316">"Передача изображения"</string>
     <string name="quick_settings_cast_device_default_name" msgid="5367253104742382945">"Безымянное устройство"</string>
     <string name="quick_settings_cast_device_default_description" msgid="2484573682378634413">"Готово к передаче"</string>
@@ -439,4 +439,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Приложение не установлено на вашем устройстве"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Показывать секунды"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Показывать в строке состояния время с точностью до секунды (заряд батареи может расходоваться быстрее)."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Изменить порядок Быстрых настроек"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Добавить яркость в Быстрые настройки"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Разбить Быстрые настройки на страницы"</string>
+    <string name="experimental" msgid="6198182315536726162">"Экспериментальная функция"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-si-rLK/strings.xml b/packages/SystemUI/res/values-si-rLK/strings.xml
index 06cb626..9e78834 100644
--- a/packages/SystemUI/res/values-si-rLK/strings.xml
+++ b/packages/SystemUI/res/values-si-rLK/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"යෙදුම ඔබේ උපාංගය මත ස්ථාපනය කර නැත"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"ඔරලෝසු තත්පර පෙන්වන්න"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"තත්ත්ව තීරුවෙහි ඔරලෝසු තත්පර පෙන්වන්න. බැටරි ආයු කාලයට බලපෑමට හැකිය."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"ඉක්මන් සැකසීම් යළි පිළිවෙළට සකසන්න"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"ඉක්මන් සැකසීම්වල දීප්තිය පෙන්වන්න"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"ඉක්මන් සැකසීම්වල පිටු පිරිසැලසුම් භාවිත කරන්න"</string>
+    <string name="experimental" msgid="6198182315536726162">"පරීක්ෂණාත්මක"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-sk/strings.xml b/packages/SystemUI/res/values-sk/strings.xml
index 037074b..e3f0fb3 100644
--- a/packages/SystemUI/res/values-sk/strings.xml
+++ b/packages/SystemUI/res/values-sk/strings.xml
@@ -439,4 +439,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Aplikácia nie je nainštalovaná na zariadení"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Zobraziť sekundy"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Zobrazí sekundy v stavovom riadku. Môže to ovplyvňovať výdrž batérie."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Zmeniť usporiadanie Rýchlych nastavení"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Zobraziť jas v Rýchlych nastaveniach"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Použite stránkovanie v Rýchlych nastaveniach"</string>
+    <string name="experimental" msgid="6198182315536726162">"Experimentálne"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-sl/strings.xml b/packages/SystemUI/res/values-sl/strings.xml
index 31fe5d8..4dffcfa 100644
--- a/packages/SystemUI/res/values-sl/strings.xml
+++ b/packages/SystemUI/res/values-sl/strings.xml
@@ -437,4 +437,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Aplikacija ni nameščena v napravi"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Prikaz sekund pri uri"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Prikaže sekunde pri uri v vrstici stanja. To lahko vpliva na čas delovanja pri akumulatorskem napajanju."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Preuredi hitre nastavitve"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Prikaz svetlosti v hitrih nastavitvah"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Uporaba postavitve strani v hitrih nastavitvah"</string>
+    <string name="experimental" msgid="6198182315536726162">"Poskusno"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-sq-rAL/strings.xml b/packages/SystemUI/res/values-sq-rAL/strings.xml
index ffb8bea..20c3426 100644
--- a/packages/SystemUI/res/values-sq-rAL/strings.xml
+++ b/packages/SystemUI/res/values-sq-rAL/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Aplikacioni nuk është instaluar në pajisjen tënde."</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Trego sekondat e orës"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Trego sekondat e orës në shiritin e statusit. Mund të ndikojë te jeta e baterisë."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Risistemo Cilësimet e shpejta"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Shfaq ndriçimin te Cilësimet e shpejta"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Përdor faqosjen te Cilësimet e shpejta"</string>
+    <string name="experimental" msgid="6198182315536726162">"Eksperimentale"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-sr/strings.xml b/packages/SystemUI/res/values-sr/strings.xml
index bae0284..1dcde3a 100644
--- a/packages/SystemUI/res/values-sr/strings.xml
+++ b/packages/SystemUI/res/values-sr/strings.xml
@@ -436,4 +436,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Апликација није инсталирана на уређају"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Приказуј секунде на сату"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Секунде на сату се приказују на статусној траци. То може да утиче на трајање батерије."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Преуреди Брза подешавања"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Прикажи осветљеност у Брзим подешавањима"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Користи листање страница у Брзим подешавањима"</string>
+    <string name="experimental" msgid="6198182315536726162">"Експериментално"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-sv/strings.xml b/packages/SystemUI/res/values-sv/strings.xml
index 6588f10..0abeb1e 100644
--- a/packages/SystemUI/res/values-sv/strings.xml
+++ b/packages/SystemUI/res/values-sv/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Appen är inte installerad på enheten"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Visa klocksekunder"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Visa klocksekunder i statusfältet. Detta kan påverka batteritiden."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Ordna snabbinställningarna"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Visa ljusstyrka i snabbinställningarna"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Använd sidindelning i snabbinställningarna"</string>
+    <string name="experimental" msgid="6198182315536726162">"Experimentella"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-sw/strings.xml b/packages/SystemUI/res/values-sw/strings.xml
index 623f12c..d000004 100644
--- a/packages/SystemUI/res/values-sw/strings.xml
+++ b/packages/SystemUI/res/values-sw/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Programu haijasakinishwa kwenye kifaa chako"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Onyesha sekunde za saa"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Onyesha sekunde za saa katika sehemu ya arifa. Inaweza kuathiri muda wa matumizi ya betri."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Panga Upya Mipangilio ya Haraka"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Onyesha unga\'avu katika Mipangilio ya Haraka"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Tumia nambari za ukurasa katika Mipangilio ya Haraka"</string>
+    <string name="experimental" msgid="6198182315536726162">"Ya majaribio"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-ta-rIN/strings.xml b/packages/SystemUI/res/values-ta-rIN/strings.xml
index c6cb337..7b1ded3 100644
--- a/packages/SystemUI/res/values-ta-rIN/strings.xml
+++ b/packages/SystemUI/res/values-ta-rIN/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"சாதனத்தில் பயன்பாடு நிறுவப்படவில்லை"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"கடிகார வினாடிகளைக் காட்டு"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"நிலைப் பட்டியில் கடிகார வினாடிகளைக் காட்டும். பேட்டரியின் ஆயுளைக் குறைக்கலாம்."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"விரைவு அமைப்புகளை மறுவரிசைப்படுத்து"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"விரைவு அமைப்புகளில் ஒளிர்வுப் பட்டியைக் காட்டு"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"விரைவு அமைப்புகளில் புதிய பக்கத் தளவமைப்பைப் பயன்படுத்து"</string>
+    <string name="experimental" msgid="6198182315536726162">"சோதனை முயற்சி"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-te-rIN/strings.xml b/packages/SystemUI/res/values-te-rIN/strings.xml
index c0717c1..06762bd 100644
--- a/packages/SystemUI/res/values-te-rIN/strings.xml
+++ b/packages/SystemUI/res/values-te-rIN/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"అనువర్తనం మీ పరికరంలో ఇన్‌స్టాల్ చేయలేదు"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"గడియారం సెకన్లు చూపు"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"స్థితి పట్టీలో గడియారం సెకన్లు చూపుతుంది. బ్యాటరీ శక్తి ప్రభావితం చేయవచ్చు."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"శీఘ్ర సెట్టింగ్‌ల ఏర్పాటు క్రమం మార్చు"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"శీఘ్ర సెట్టింగ్‌ల్లో ప్రకాశం చూపు"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"శీఘ్ర సెట్టింగ్‌ల్లో పేజింగ్‌ను ఉపయోగించు"</string>
+    <string name="experimental" msgid="6198182315536726162">"ప్రయోగాత్మకం"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-th/strings.xml b/packages/SystemUI/res/values-th/strings.xml
index e16fdd2..52c7af7 100644
--- a/packages/SystemUI/res/values-th/strings.xml
+++ b/packages/SystemUI/res/values-th/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"ยังไม่ได้ติดตั้งแอปพลิเคชันบนอุปกรณ์ของคุณ"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"แสดงวินาทีของนาฬิกา"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"แสดงวินาทีของนาฬิกาในแถบสถานะ อาจส่งผลต่ออายุแบตเตอรี"</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"จัดเรียงการตั้งค่าด่วนใหม่"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"แสดงความสว่างในการตั้งค่าด่วน"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"ใช้การแบ่งหน้าในการตั้งค่าด่วน"</string>
+    <string name="experimental" msgid="6198182315536726162">"ทดสอบ"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-tl/strings.xml b/packages/SystemUI/res/values-tl/strings.xml
index d970084..66056d1 100644
--- a/packages/SystemUI/res/values-tl/strings.xml
+++ b/packages/SystemUI/res/values-tl/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Hindi naka-install ang application sa iyong device"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Ipakita ang mga segundo ng orasan"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Ipakita ang mga segundo ng orasan sa status bar. Maaaring makaapekto sa tagal ng baterya."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Ayusing Muli ang Mga Mabilisang Setting"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Ipakita ang liwanag sa Mga Mabilisang Setting"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Gamitin ang paging sa Mga Mabilisang Setting"</string>
+    <string name="experimental" msgid="6198182315536726162">"Pang-eksperimento"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-tr/strings.xml b/packages/SystemUI/res/values-tr/strings.xml
index b22b4fb..fdad0d5 100644
--- a/packages/SystemUI/res/values-tr/strings.xml
+++ b/packages/SystemUI/res/values-tr/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Uygulama, cihazınızda yüklü değil"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Saatin saniyelerini göster"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Durum çubuğunda saatin saniyelerini gösterir. Pil ömrünü etkileyebilir."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Hızlı Ayarlar\'ı Yeniden Düzenle"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Hızlı Ayarlar\'da parlaklığı göster"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Hızlı Ayarlar\'da sayfa ayırmayı kullan"</string>
+    <string name="experimental" msgid="6198182315536726162">"Deneysel"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-uk/strings.xml b/packages/SystemUI/res/values-uk/strings.xml
index 898611d..9dde801 100644
--- a/packages/SystemUI/res/values-uk/strings.xml
+++ b/packages/SystemUI/res/values-uk/strings.xml
@@ -437,4 +437,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Додаток не встановлено на вашому пристрої"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Показувати секунди на годиннику"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Показувати секунди на годиннику в рядку стану. Акумулятор може розряджатися швидше."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Упорядкувати швидкі налаштування"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Показувати панель яскравості у швидких налаштуваннях"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Показувати швидкі налаштування у вигляді сторінок"</string>
+    <string name="experimental" msgid="6198182315536726162">"Експериментальні налаштування"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-ur-rPK/strings.xml b/packages/SystemUI/res/values-ur-rPK/strings.xml
index d5683e1..a4ef16a 100644
--- a/packages/SystemUI/res/values-ur-rPK/strings.xml
+++ b/packages/SystemUI/res/values-ur-rPK/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"ایپلیکیشن آپ کے آلہ پر انسٹال نہیں ہے"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"گھڑی کے سیکنڈز دکھائیں"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"گھڑی کے سیکنڈز اسٹیٹس بار میں دکھائیں۔ اس کا بیٹری کی زندگی پر اثر پڑ سکتا ہے۔"</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"فوری ترتیبات کو دوبارہ ترتیب دیں"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"فوری ترتیبات میں چمکیلا پن دکھائیں"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"فوری ترتیبات میں پیجنگ استعمال کریں"</string>
+    <string name="experimental" msgid="6198182315536726162">"تجرباتی"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-uz-rUZ/strings.xml b/packages/SystemUI/res/values-uz-rUZ/strings.xml
index e31af6b..118c4b8 100644
--- a/packages/SystemUI/res/values-uz-rUZ/strings.xml
+++ b/packages/SystemUI/res/values-uz-rUZ/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Ilova qurilmangizga o‘rnatilmagan"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Soat soniyalari ko‘rsatilsin"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Holat panelida soat soniyalari ko‘rsatilsin. Bu batareya resursiga ta’sir qilishi mumkin."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Tezkor sozlamalarni qayta tartiblash"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Tezkor sozlamalarda yorqinlikni ko‘rsatish"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Tezkor sozlamalarda peyjingdan foydalanish"</string>
+    <string name="experimental" msgid="6198182315536726162">"Tajribaviy"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-vi/strings.xml b/packages/SystemUI/res/values-vi/strings.xml
index 030bbbe..460d57a 100644
--- a/packages/SystemUI/res/values-vi/strings.xml
+++ b/packages/SystemUI/res/values-vi/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Ứng dụng chưa được cài đặt trên thiết bị của bạn"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Hiển thị giây đồng hồ"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Hiển thị giây đồng hồ trong thanh trạng thái. Có thể ảnh hưởng đến thời lượng pin."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Sắp xếp lại Cài đặt nhanh"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Hiển thị độ sáng trong Cài đặt nhanh"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Sử dụng đánh số trang trong Cài đặt nhanh"</string>
+    <string name="experimental" msgid="6198182315536726162">"Thử nghiệm"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-zh-rCN/strings.xml b/packages/SystemUI/res/values-zh-rCN/strings.xml
index b046415..05644e7 100644
--- a/packages/SystemUI/res/values-zh-rCN/strings.xml
+++ b/packages/SystemUI/res/values-zh-rCN/strings.xml
@@ -437,4 +437,12 @@
     <string name="activity_not_found" msgid="348423244327799974">"您的设备中未安装此应用"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"显示时钟的秒数"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"在状态栏中显示时钟的秒数。这可能会影响电池的续航时间。"</string>
+    <!-- no translation found for qs_rearrange (8060918697551068765) -->
+    <skip />
+    <!-- no translation found for show_brightness (6613930842805942519) -->
+    <skip />
+    <!-- no translation found for qs_paging (7020133150248666132) -->
+    <skip />
+    <!-- no translation found for experimental (6198182315536726162) -->
+    <skip />
 </resources>
diff --git a/packages/SystemUI/res/values-zh-rHK/strings.xml b/packages/SystemUI/res/values-zh-rHK/strings.xml
index d568dba..f7cac90 100644
--- a/packages/SystemUI/res/values-zh-rHK/strings.xml
+++ b/packages/SystemUI/res/values-zh-rHK/strings.xml
@@ -437,4 +437,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"尚未在裝置安裝應用程式"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"顯示時鐘秒數"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"在狀態列中顯示時鐘秒數,但可能會影響電池壽命。"</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"重新排列快速設定"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"在快速設定顯示亮度"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"在快速設定使用分頁"</string>
+    <string name="experimental" msgid="6198182315536726162">"實驗版"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-zh-rTW/strings.xml b/packages/SystemUI/res/values-zh-rTW/strings.xml
index 702ad53..369172f 100644
--- a/packages/SystemUI/res/values-zh-rTW/strings.xml
+++ b/packages/SystemUI/res/values-zh-rTW/strings.xml
@@ -437,4 +437,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"您的裝置未安裝這個應用程式"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"顯示時鐘秒數"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"在狀態列中顯示時鐘秒數。這可能會影響電池續航力。"</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"重新排列快速設定"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"在快速設定中顯示亮度"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"在快速設定中使用分頁功能"</string>
+    <string name="experimental" msgid="6198182315536726162">"實驗性"</string>
 </resources>
diff --git a/packages/SystemUI/res/values-zu/strings.xml b/packages/SystemUI/res/values-zu/strings.xml
index 8774036..2a14a9f 100644
--- a/packages/SystemUI/res/values-zu/strings.xml
+++ b/packages/SystemUI/res/values-zu/strings.xml
@@ -435,4 +435,8 @@
     <string name="activity_not_found" msgid="348423244327799974">"Uhlelo lokusebenza alufakiwe kudivayisi yakho"</string>
     <string name="clock_seconds" msgid="7689554147579179507">"Bonisa amasekhondi wewashi"</string>
     <string name="clock_seconds_desc" msgid="6282693067130470675">"Bonisa amasekhondi wewashi kubha yesimo. Ingathinta impilo yebhethri."</string>
+    <string name="qs_rearrange" msgid="8060918697551068765">"Hlela kabusha izilungiselelo ezisheshayo"</string>
+    <string name="show_brightness" msgid="6613930842805942519">"Bonisa ukukhanya kuzilungiselelo ezisheshayo"</string>
+    <string name="qs_paging" msgid="7020133150248666132">"Sebenzisa i-paging kuzilungiselelo ezisheshayo"</string>
+    <string name="experimental" msgid="6198182315536726162">"Okokulinga"</string>
 </resources>
diff --git a/packages/SystemUI/res/values/attrs.xml b/packages/SystemUI/res/values/attrs.xml
index 527248c..c070a0e 100644
--- a/packages/SystemUI/res/values/attrs.xml
+++ b/packages/SystemUI/res/values/attrs.xml
@@ -90,5 +90,9 @@
     <declare-styleable name="AlphaOptimizedImageView">
         <attr name="hasOverlappingRendering" format="boolean" />
     </declare-styleable>
+
+    <declare-styleable name="TunerSwitch">
+        <attr name="defValue" format="boolean" />
+    </declare-styleable>
 </resources>
 
diff --git a/packages/SystemUI/res/values/dimens.xml b/packages/SystemUI/res/values/dimens.xml
index 03ea73c..3817741 100644
--- a/packages/SystemUI/res/values/dimens.xml
+++ b/packages/SystemUI/res/values/dimens.xml
@@ -164,6 +164,9 @@
     <dimen name="pull_span_min">25dp</dimen>
 
     <dimen name="qs_tile_height">88dp</dimen>
+    <dimen name="qs_quick_actions_height">88dp</dimen>
+    <dimen name="qs_quick_actions_padding">25dp</dimen>
+    <dimen name="qs_page_indicator_size">12dp</dimen>
     <dimen name="qs_tile_icon_size">24dp</dimen>
     <dimen name="qs_tile_text_size">12sp</dimen>
     <dimen name="qs_tile_divider_height">1dp</dimen>
diff --git a/packages/SystemUI/res/values/strings.xml b/packages/SystemUI/res/values/strings.xml
index 8b3f2d8..b732e99 100644
--- a/packages/SystemUI/res/values/strings.xml
+++ b/packages/SystemUI/res/values/strings.xml
@@ -1145,4 +1145,15 @@
     <!-- Description of setting to show clock seconds [CHAR LIMIT=NONE] -->
     <string name="clock_seconds_desc">Show clock seconds in the status bar. May impact battery life.</string>
 
+    <!-- Button that leads to page to rearrange quick settings tiles [CHAR LIMIT=60] -->
+    <string name="qs_rearrange">Rearrange Quick Settings</string>
+    <!-- Option to show brightness bar in quick settings [CHAR LIMIT=60] -->
+    <string name="show_brightness">Show brightness in Quick Settings</string>
+    <!-- Option to use new paging layout in quick settings [CHAR LIMIT=60] -->
+    <string name="qs_paging">Use paging in Quick Settings</string>
+
+    <!-- Category in the System UI Tuner settings, where new/experimental
+         settings are -->
+    <string name="experimental">Experimental</string>
+
 </resources>
diff --git a/packages/SystemUI/res/xml/tuner_prefs.xml b/packages/SystemUI/res/xml/tuner_prefs.xml
index beb863c..5980108 100644
--- a/packages/SystemUI/res/xml/tuner_prefs.xml
+++ b/packages/SystemUI/res/xml/tuner_prefs.xml
@@ -15,11 +15,32 @@
 -->
 
 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:sysui="http://schemas.android.com/apk/res-auto"
     android:title="@string/system_ui_tuner">
 
-    <Preference
-        android:key="qs_tuner"
-        android:title="@string/quick_settings" />
+    <PreferenceScreen
+        android:title="@string/quick_settings">
+
+        <Preference
+            android:key="qs_tuner"
+            android:title="@string/qs_rearrange" />
+
+        <PreferenceCategory
+            android:title="@string/experimental">
+
+            <com.android.systemui.tuner.TunerSwitch
+                android:key="qs_show_brightness"
+                android:title="@string/show_brightness"
+                sysui:defValue="true" />
+
+            <com.android.systemui.tuner.QSPagingSwitch
+                android:key="qs_paged_panel"
+                android:title="@string/qs_paging" />
+
+        </PreferenceCategory>
+
+    </PreferenceScreen>
+
 
     <PreferenceScreen
         android:title="@string/status_bar" >
diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java b/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java
index 6f49fd6..0be3069 100644
--- a/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java
+++ b/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java
@@ -1051,6 +1051,7 @@
                 // Without this, settings is not enabled until the lock screen first appears
                 setShowingLocked(false);
                 hideLocked();
+                mUpdateMonitor.reportSuccessfulStrongAuthUnlockAttempt();
                 return;
             }
         }
diff --git a/packages/SystemUI/src/com/android/systemui/qs/PageIndicator.java b/packages/SystemUI/src/com/android/systemui/qs/PageIndicator.java
new file mode 100644
index 0000000..1200266
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/qs/PageIndicator.java
@@ -0,0 +1,86 @@
+package com.android.systemui.qs;
+
+import android.content.Context;
+import android.graphics.Canvas;
+import android.graphics.Paint;
+import android.util.AttributeSet;
+import android.view.Gravity;
+import android.view.View;
+import android.widget.LinearLayout;
+
+import com.android.systemui.R;
+
+public class PageIndicator extends LinearLayout {
+
+    private final int mPageIndicatorSize;
+
+    public PageIndicator(Context context, AttributeSet attrs) {
+        super(context, attrs);
+        setGravity(Gravity.CENTER);
+        mPageIndicatorSize =
+                (int) mContext.getResources().getDimension(R.dimen.qs_page_indicator_size);
+    }
+
+    public void setNumPages(int numPages) {
+        while (numPages < getChildCount()) {
+            removeViewAt(getChildCount() - 1);
+        }
+        while (numPages > getChildCount()) {
+            SinglePageIndicator v = new SinglePageIndicator(mContext);
+            v.setAmount(0);
+            addView(v, new LayoutParams(mPageIndicatorSize, mPageIndicatorSize));
+        }
+    }
+
+    public void setLocation(float location) {
+        int index = (int) location;
+        location -= index;
+
+        final int N = getChildCount();
+        for (int i = 0; i < N; i++) {
+            float amount = 0;
+            if (i == index) {
+                amount = 1 - location;
+            } else if (i == index + 1) {
+                amount = location;
+            }
+            ((SinglePageIndicator) getChildAt(i)).setAmount(amount);
+        }
+    }
+
+    // This could be done with a circle drawable and an ImageView, but this seems
+    // easier for now.
+    public static class SinglePageIndicator extends View {
+        private static final int MIN_ALPHA = 0x4d;
+        private static final int MAX_ALPHA = 0xff;
+
+        private static final float MIN_SIZE = .55f;
+        private static final float MAX_SIZE = .7f;
+
+        private final Paint mPaint;
+        private float mSize;
+
+        public SinglePageIndicator(Context context) {
+            super(context);
+            mPaint = new Paint();
+            mPaint.setColor(0xffffffff);
+            mPaint.setAlpha(MAX_ALPHA);
+        }
+
+        public void setAmount(float amount) {
+            mSize = amount * (MAX_SIZE - MIN_SIZE) + MIN_SIZE;
+            int alpha = (int) (amount * (MAX_ALPHA - MIN_ALPHA)) + MIN_ALPHA;
+            mPaint.setAlpha(alpha);
+            postInvalidate();
+        }
+
+        @Override
+        public void draw(Canvas canvas) {
+            int minDimen = Math.min(getWidth(), getHeight()) / 2;
+            float radius = mSize * minDimen;
+            float x = getWidth() / 2f;
+            float y = getHeight() / 2f;
+            canvas.drawCircle(x, y, radius, mPaint);
+        }
+    }
+}
diff --git a/packages/SystemUI/src/com/android/systemui/qs/PagedTileLayout.java b/packages/SystemUI/src/com/android/systemui/qs/PagedTileLayout.java
new file mode 100644
index 0000000..ece7022
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/qs/PagedTileLayout.java
@@ -0,0 +1,221 @@
+package com.android.systemui.qs;
+
+import android.content.Context;
+import android.util.AttributeSet;
+import android.util.Log;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.LinearLayout;
+
+import com.android.internal.widget.PagerAdapter;
+import com.android.internal.widget.ViewPager;
+import com.android.systemui.R;
+import com.android.systemui.qs.QSPanel.QSTileLayout;
+import com.android.systemui.qs.QSPanel.TileRecord;
+
+import java.util.ArrayList;
+
+public class PagedTileLayout extends ViewPager implements QSTileLayout {
+
+    private static final boolean DEBUG = false;
+
+    private static final String TAG = "PagedTileLayout";
+
+    private final ArrayList<TileRecord> mTiles = new ArrayList<TileRecord>();
+    private final ArrayList<TilePage> mPages = new ArrayList<TilePage>();
+
+    private FirstPage mFirstPage;
+    private PageIndicator mPageIndicator;
+
+    private int mNumPages;
+
+    public PagedTileLayout(Context context, AttributeSet attrs) {
+        super(context, attrs);
+        setAdapter(mAdapter);
+        setOnPageChangeListener(new OnPageChangeListener() {
+            @Override
+            public void onPageSelected(int position) {
+                if (mPageIndicator == null) return;
+                mPageIndicator.setLocation(position);
+            }
+
+            @Override
+            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
+                if (mPageIndicator == null) return;
+                mPageIndicator.setLocation(position + positionOffset);
+            }
+
+            @Override
+            public void onPageScrollStateChanged(int state) {
+            }
+        });
+        setCurrentItem(0);
+    }
+
+    @Override
+    protected void onFinishInflate() {
+        super.onFinishInflate();
+        mPageIndicator = (PageIndicator) findViewById(R.id.page_indicator);
+        ((LayoutParams) mPageIndicator.getLayoutParams()).isDecor = true;
+
+        mFirstPage = (FirstPage) findViewById(R.id.first_page);
+        removeView(mFirstPage); // We don't actually want this on the view yet, just inflated.
+        mPages.add(mFirstPage.mTilePage);
+    }
+
+    @Override
+    public int getOffsetTop(TileRecord tile) {
+        if (tile.tileView.getParent() == mFirstPage.mTilePage) {
+            return mFirstPage.getTop() + mFirstPage.mTilePage.getTop();
+        }
+        return ((ViewGroup) tile.tileView.getParent()).getTop();
+    }
+
+    @Override
+    public void setTileVisibility(TileRecord tile, int visibility) {
+        tile.tileView.setVisibility(visibility);
+        // TODO: Do something smarter here.
+        distributeTiles();
+    }
+
+    @Override
+    public void addTile(TileRecord tile) {
+        mTiles.add(tile);
+        distributeTiles();
+    }
+
+    @Override
+    public void removeTile(TileRecord tile) {
+        if (mTiles.remove(tile)) {
+            distributeTiles();
+        }
+    }
+
+    private void distributeTiles() {
+        if (DEBUG) Log.d(TAG, "Distributing tiles");
+        mFirstPage.mQuickQuickTiles.removeAllViews();
+        final int NP = mPages.size();
+        for (int i = 0; i < NP; i++) {
+            mPages.get(i).clear();
+        }
+        int index = 0;
+        final int NT = mTiles.size();
+        for (int i = 0; i < NT; i++) {
+            TileRecord tile = mTiles.get(i);
+            if (tile.tile.getTileType() == QSTileView.QS_TYPE_QUICK) {
+                tile.tileView.setType(QSTileView.QS_TYPE_QUICK);
+                mFirstPage.mQuickQuickTiles.addView(tile.tileView);
+                continue;
+            }
+            if (mPages.get(index).isFull()) {
+                if (++index == mPages.size()) {
+                    if (DEBUG) Log.d(TAG, "Adding page for " + tile.tile.getClass().getSimpleName());
+                    mPages.add((TilePage) LayoutInflater.from(mContext)
+                            .inflate(R.layout.qs_paged_page, this, false));
+                }
+            }
+            if (DEBUG) Log.d(TAG, "Adding " + tile.tile.getClass().getSimpleName() + " to "
+                    + index);
+            mPages.get(index).addTile(tile);
+        }
+        if (mNumPages != index + 1) {
+            mNumPages = index + 1;
+            mPageIndicator.setNumPages(mNumPages);
+            mAdapter.notifyDataSetChanged();
+        }
+    }
+
+    @Override
+    public void updateResources() {
+        for (int i = 0; i < mPages.size(); i++) {
+            mPages.get(i).updateResources();
+        }
+    }
+
+    @Override
+    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
+        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
+        // The ViewPager likes to eat all of the space, instead force it to wrap to the max height
+        // of the pages.
+        int maxHeight = 0;
+        final int N = getChildCount();
+        for (int i = 0; i < N; i++) {
+            int height = getChildAt(i).getMeasuredHeight();
+            if (height > maxHeight) {
+                maxHeight = height;
+            }
+        }
+        setMeasuredDimension(getMeasuredWidth(), maxHeight + mPageIndicator.getMeasuredHeight());
+    }
+
+    public static class FirstPage extends LinearLayout {
+        private LinearLayout mQuickQuickTiles;
+        private TilePage mTilePage;
+
+        public FirstPage(Context context, AttributeSet attrs) {
+            super(context, attrs);
+        }
+
+        @Override
+        protected void onFinishInflate() {
+            super.onFinishInflate();
+            mQuickQuickTiles = (LinearLayout) findViewById(R.id.quick_tile_layout);
+            mTilePage = (TilePage) findViewById(R.id.tile_page);
+            // Less rows on first page, because it needs room for the quick tiles.
+            mTilePage.mMaxRows = 3;
+        }
+
+        @Override
+        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
+            // The ViewPager will try to make us taller, don't do it unless we need to.
+            heightMeasureSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(heightMeasureSpec),
+                    MeasureSpec.AT_MOST);
+            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
+        }
+    }
+
+    public static class TilePage extends TileLayout {
+        private int mMaxRows = 4;
+
+        public TilePage(Context context, AttributeSet attrs) {
+            super(context, attrs);
+            mAllowDual = false;
+        }
+
+        private void clear() {
+            if (DEBUG) Log.d(TAG, "Clearing page");
+            removeAllViews();
+            mRecords.clear();
+        }
+
+        private boolean isFull() {
+            return mRecords.size() >= mColumns * mMaxRows;
+        }
+    }
+
+    private final PagerAdapter mAdapter = new PagerAdapter() {
+        public void destroyItem(ViewGroup container, int position, Object object) {
+            if (DEBUG) Log.d(TAG, "Destantiating " + position);
+            // TODO: Find way to clean up the extra pages.
+            container.removeView((View) object);
+        }
+
+        public Object instantiateItem(ViewGroup container, int position) {
+            if (DEBUG) Log.d(TAG, "Instantiating " + position);
+            ViewGroup view = position == 0 ? mFirstPage : mPages.get(position);
+            container.addView(view);
+            return view;
+        }
+
+        @Override
+        public int getCount() {
+            return mNumPages;
+        }
+
+        @Override
+        public boolean isViewFromObject(View view, Object object) {
+            return view == object;
+        }
+    };
+}
diff --git a/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java b/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java
index b640cf1..683af97 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java
@@ -43,12 +43,17 @@
 import com.android.systemui.settings.ToggleSlider;
 import com.android.systemui.statusbar.phone.QSTileHost;
 import com.android.systemui.statusbar.policy.BrightnessMirrorController;
+import com.android.systemui.tuner.TunerService;
+import com.android.systemui.tuner.TunerService.Tunable;
 
 import java.util.ArrayList;
 import java.util.Collection;
 
 /** View that represents the quick settings tile panel. **/
-public class QSPanel extends FrameLayout {
+public class QSPanel extends FrameLayout implements Tunable {
+
+    public static final String QS_SHOW_BRIGHTNESS = "qs_show_brightness";
+    public static final String QS_PAGED_PANEL = "qs_paged_panel";
 
     private final Context mContext;
     protected final ArrayList<TileRecord> mRecords = new ArrayList<TileRecord>();
@@ -75,7 +80,7 @@
     private boolean mGridContentVisible = true;
 
     private LinearLayout mQsContainer;
-    private TileLayout mTileLayout;
+    private QSTileLayout mTileLayout;
 
     public QSPanel(Context context) {
         this(context, null);
@@ -104,10 +109,7 @@
 
         addView(mQsContainer);
 
-        mTileLayout = new TileLayout(mContext, mRecords);
-
         mQsContainer.addView(mBrightnessView);
-        mQsContainer.addView(mTileLayout);
         mQsContainer.addView(mFooter.getView());
         mClipper = new QSDetailClipper(mDetail);
         updateResources();
@@ -126,6 +128,41 @@
         });
     }
 
+    @Override
+    protected void onAttachedToWindow() {
+        super.onAttachedToWindow();
+        TunerService.get(mContext).addTunable(this, QS_SHOW_BRIGHTNESS, QS_PAGED_PANEL);
+    }
+
+    @Override
+    protected void onDetachedFromWindow() {
+        TunerService.get(mContext).removeTunable(this);
+        super.onDetachedFromWindow();
+    }
+
+    @Override
+    public void onTuningChanged(String key, String newValue) {
+        if (QS_SHOW_BRIGHTNESS.equals(key)) {
+            mBrightnessView.setVisibility(newValue == null || Integer.parseInt(newValue) != 0
+                    ? VISIBLE : GONE);
+        } else if (QS_PAGED_PANEL.equals(key)) {
+            if (mTileLayout != null) {
+                for (int i = 0; i < mRecords.size(); i++) {
+                    mTileLayout.removeTile(mRecords.get(i));
+                }
+                mQsContainer.removeView((View) mTileLayout);
+            }
+            int layout = newValue != null && Integer.parseInt(newValue) != 0
+                    ? R.layout.qs_paged_tile_layout : R.layout.qs_tile_layout;
+            mTileLayout =
+                    (QSTileLayout) LayoutInflater.from(mContext).inflate(layout, mQsContainer, false);
+            mQsContainer.addView((View) mTileLayout, 1 /* Between brightness and footer */);
+            for (int i = 0; i < mRecords.size(); i++) {
+                mTileLayout.addTile(mRecords.get(i));
+            }
+        }
+    }
+
     private void updateDetailText() {
         mDetailDoneButton.setText(R.string.quick_settings_done);
         mDetailSettingsButton.setText(R.string.quick_settings_more_settings);
@@ -164,7 +201,9 @@
             refreshAllTiles();
         }
         updateDetailText();
-        mTileLayout.updateResources();
+        if (mTileLayout != null) {
+            mTileLayout.updateResources();
+        }
     }
 
     @Override
@@ -240,18 +279,18 @@
         mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget();
     }
 
-    private void setTileVisibility(View v, int visibility) {
-        mHandler.obtainMessage(H.SET_TILE_VISIBILITY, visibility, 0, v).sendToTarget();
+    private void setTileVisibility(TileRecord record, int visibility) {
+        mHandler.obtainMessage(H.SET_TILE_VISIBILITY, visibility, 0, record).sendToTarget();
     }
 
-    private void handleSetTileVisibility(View v, int visibility) {
-        if (visibility == v.getVisibility()) return;
-        v.setVisibility(visibility);
+    private void handleSetTileVisibility(TileRecord tile, int visibility) {
+        if (visibility == tile.tileView.getVisibility()) return;
+        mTileLayout.setTileVisibility(tile, visibility);
     }
 
     public void setTiles(Collection<QSTile<?>> tiles) {
         for (TileRecord record : mRecords) {
-            removeView(record.tileView);
+            mTileLayout.removeTile(record);
         }
         mRecords.clear();
         for (QSTile<?> tile : tiles) {
@@ -264,7 +303,7 @@
 
     private void drawTile(TileRecord r, QSTile.State state) {
         final int visibility = state.visible ? VISIBLE : GONE;
-        setTileVisibility(r.tileView, visibility);
+        setTileVisibility(r, visibility);
         r.tileView.onStateChanged(state);
     }
 
@@ -329,7 +368,9 @@
         r.tile.refreshState();
         mRecords.add(r);
 
-        mTileLayout.addView(r.tileView);
+        if (mTileLayout != null) {
+            mTileLayout.addTile(r);
+        }
     }
 
     public boolean isShowingDetail() {
@@ -371,7 +412,7 @@
         }
         r.tile.setDetailListening(show);
         int x = r.tileView.getLeft() + r.tileView.getWidth() / 2;
-        int y = r.tileView.getTop() + mTileLayout.getTop() + r.tileView.getHeight() / 2;
+        int y = r.tileView.getTop() + mTileLayout.getOffsetTop(r) + r.tileView.getHeight() / 2;
         handleShowDetailImpl(r, show, x, y);
     }
 
@@ -474,7 +515,7 @@
             if (msg.what == SHOW_DETAIL) {
                 handleShowDetail((Record)msg.obj, msg.arg1 != 0);
             } else if (msg.what == SET_TILE_VISIBILITY) {
-                handleSetTileVisibility((View)msg.obj, msg.arg1);
+                handleSetTileVisibility((TileRecord) msg.obj, msg.arg1);
             }
         }
     }
@@ -534,4 +575,12 @@
         void onToggleStateChanged(boolean state);
         void onScanStateChanged(boolean state);
     }
+
+    public interface QSTileLayout {
+        void addTile(TileRecord tile);
+        void removeTile(TileRecord tile);
+        void setTileVisibility(TileRecord tile, int visibility);
+        int getOffsetTop(TileRecord tile);
+        void updateResources();
+    }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/qs/QSTile.java b/packages/SystemUI/src/com/android/systemui/qs/QSTile.java
index b330582..9b3372c3 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/QSTile.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/QSTile.java
@@ -83,9 +83,13 @@
         mContext = host.getContext();
         mHandler = new H(host.getLooper());
     }
+    
+    public int getTileType() {
+        return QSTileView.QS_TYPE_NORMAL;
+    }
 
-    public boolean supportsDualTargets() {
-        return false;
+    public final boolean supportsDualTargets() {
+        return getTileType() == QSTileView.QS_TYPE_DUAL;
     }
 
     public Host getHost() {
diff --git a/packages/SystemUI/src/com/android/systemui/qs/QSTileView.java b/packages/SystemUI/src/com/android/systemui/qs/QSTileView.java
index 6d26a3b..08cdc1e 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/QSTileView.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/QSTileView.java
@@ -48,6 +48,10 @@
     private static final Typeface CONDENSED = Typeface.create("sans-serif-condensed",
             Typeface.NORMAL);
 
+    public static final int QS_TYPE_NORMAL = 0;
+    public static final int QS_TYPE_DUAL   = 1;
+    public static final int QS_TYPE_QUICK  = 2;
+
     protected final Context mContext;
     private final View mIcon;
     private final View mDivider;
@@ -61,13 +65,15 @@
 
     private TextView mLabel;
     private QSDualTileLabel mDualLabel;
-    private boolean mDual;
+    private int mType;
     private OnClickListener mClickPrimary;
     private OnClickListener mClickSecondary;
     private OnLongClickListener mLongClick;
     private Drawable mTileBackground;
     private RippleDrawable mRipple;
 
+    private View mCircle;
+
     public QSTileView(Context context) {
         super(context);
 
@@ -89,6 +95,9 @@
         mIcon = createIcon();
         addView(mIcon);
 
+        mCircle = createCircleIcon();
+        addView(mCircle);
+
         mDivider = new View(mContext);
         mDivider.setBackgroundColor(context.getColor(R.color.qs_tile_divider));
         final int dh = res.getDimensionPixelSize(R.dimen.qs_tile_divider_height);
@@ -131,12 +140,12 @@
         }
         if (mDualLabel != null) {
             labelText = mDualLabel.getText();
-            labelDescription = mLabel.getContentDescription();
+            labelDescription = mLabel != null ? mLabel.getContentDescription() : null;
             removeView(mDualLabel);
             mDualLabel = null;
         }
         final Resources res = mContext.getResources();
-        if (mDual) {
+        if (mType == QS_TYPE_DUAL) {
             mDualLabel = new QSDualTileLabel(mContext);
             mDualLabel.setId(View.generateViewId());
             mDualLabel.setBackgroundResource(R.drawable.btn_borderless_rect);
@@ -157,7 +166,7 @@
             }
             addView(mDualLabel);
             mDualLabel.setAccessibilityTraversalAfter(mTopBackgroundView.getId());
-        } else {
+        } else if (mType == QS_TYPE_NORMAL) {
             mLabel = new TextView(mContext);
             mLabel.setTextColor(mContext.getColor(R.color.qs_tile_text));
             mLabel.setGravity(Gravity.CENTER_HORIZONTAL);
@@ -174,16 +183,16 @@
         }
     }
 
-    public boolean setDual(boolean dual) {
-        final boolean changed = dual != mDual;
-        mDual = dual;
+    public boolean setType(int type) {
+        final boolean changed = mType != type;
+        mType = type;
         if (changed) {
             recreateLabel();
         }
         if (mTileBackground instanceof RippleDrawable) {
             setRipple((RippleDrawable) mTileBackground);
         }
-        if (dual) {
+        if (mType == QS_TYPE_DUAL) {
             mTopBackgroundView.setOnClickListener(mClickPrimary);
             setOnClickListener(null);
             setClickable(false);
@@ -197,9 +206,10 @@
             setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
             setBackground(mTileBackground);
         }
-        mTopBackgroundView.setFocusable(dual);
-        setFocusable(!dual);
-        mDivider.setVisibility(dual ? VISIBLE : GONE);
+        mTopBackgroundView.setFocusable(mType == QS_TYPE_DUAL);
+        setFocusable(mType != QS_TYPE_DUAL);
+        mDivider.setVisibility(mType == QS_TYPE_DUAL ? VISIBLE : GONE);
+        mCircle.setVisibility(mType == QS_TYPE_QUICK ? VISIBLE : GONE);
         postInvalidate();
         return changed;
     }
@@ -225,6 +235,21 @@
         return icon;
     }
 
+    protected View createCircleIcon() {
+        final ImageView icon = new ImageView(mContext);
+        icon.setImageResource(R.drawable.ic_qs_circle);
+        // TODO: Not this.
+        icon.setPadding(20, 20, 20, 20);
+        return icon;
+    }
+
+    protected View createCircle() {
+        final ImageView icon = new ImageView(mContext);
+        icon.setId(android.R.id.icon);
+        icon.setScaleType(ScaleType.CENTER_INSIDE);
+        return icon;
+    }
+
     private Drawable newTileBackground() {
         final int[] attrs = new int[] { android.R.attr.selectableItemBackgroundBorderless };
         final TypedArray ta = mContext.obtainStyledAttributes(attrs);
@@ -234,7 +259,7 @@
     }
 
     private View labelView() {
-        return mDual ? mDualLabel : mLabel;
+        return mType == QS_TYPE_DUAL ? mDualLabel : mLabel;
     }
 
     @Override
@@ -243,9 +268,18 @@
         final int h = MeasureSpec.getSize(heightMeasureSpec);
         final int iconSpec = exactly(mIconSizePx);
         mIcon.measure(MeasureSpec.makeMeasureSpec(w, MeasureSpec.AT_MOST), iconSpec);
-        labelView().measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(h, MeasureSpec.AT_MOST));
-        if (mDual) {
-            mDivider.measure(widthMeasureSpec, exactly(mDivider.getLayoutParams().height));
+        switch (mType) {
+            case QS_TYPE_QUICK:
+                mCircle.measure(
+                        MeasureSpec.makeMeasureSpec(w, MeasureSpec.EXACTLY),
+                        MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY));
+                break;
+            case QS_TYPE_DUAL:
+                mDivider.measure(widthMeasureSpec, exactly(mDivider.getLayoutParams().height));
+            default:
+                labelView().measure(widthMeasureSpec,
+                        MeasureSpec.makeMeasureSpec(h, MeasureSpec.AT_MOST));
+                break;
         }
         int heightSpec = exactly(
                 mIconSizePx + mTilePaddingBelowIconPx + mTilePaddingTopPx);
@@ -268,6 +302,10 @@
         top += mTileSpacingPx;
         top += mTilePaddingTopPx;
         final int iconLeft = (w - mIcon.getMeasuredWidth()) / 2;
+        if (mType == QS_TYPE_QUICK) {
+            top = (h - mIcon.getMeasuredHeight()) / 2;
+            layout(mCircle, 0, 0);
+        }
         layout(mIcon, iconLeft, top);
         if (mRipple != null) {
             updateRippleSize(w, h);
@@ -275,17 +313,19 @@
         }
         top = mIcon.getBottom();
         top += mTilePaddingBelowIconPx;
-        if (mDual) {
+        if (mType == QS_TYPE_DUAL) {
             layout(mDivider, 0, top);
             top = mDivider.getBottom();
         }
-        layout(labelView(), 0, top);
+        if (mType != QS_TYPE_QUICK) {
+            layout(labelView(), 0, top);
+        }
     }
 
     private void updateRippleSize(int width, int height) {
         // center the touch feedback on the center of the icon, and dial it down a bit
         final int cx = width / 2;
-        final int cy = mDual ? mIcon.getTop() + mIcon.getHeight() / 2 : height / 2;
+        final int cy = mType == QS_TYPE_DUAL ? mIcon.getTop() + mIcon.getHeight() / 2 : height / 2;
         final int rad = (int)(mIcon.getHeight() * 1.25f);
         mRipple.setHotspotBounds(cx - rad, cy - rad, cx + rad, cy + rad);
     }
@@ -298,11 +338,11 @@
         if (mIcon instanceof ImageView) {
             setIcon((ImageView) mIcon, state);
         }
-        if (mDual) {
+        if (mType == QS_TYPE_DUAL) {
             mDualLabel.setText(state.label);
             mDualLabel.setContentDescription(state.dualLabelContentDescription);
             mTopBackgroundView.setContentDescription(state.contentDescription);
-        } else {
+        } else if (mType == QS_TYPE_NORMAL) {
             mLabel.setText(state.label);
             setContentDescription(state.contentDescription);
         }
@@ -338,7 +378,7 @@
     public View updateAccessibilityOrder(View previousView) {
         View firstView;
         View lastView;
-        if (mDual) {
+        if (mType == QS_TYPE_DUAL) {
             lastView = mDualLabel;
             firstView = mTopBackgroundView;
         } else {
diff --git a/packages/SystemUI/src/com/android/systemui/qs/QuickTileLayout.java b/packages/SystemUI/src/com/android/systemui/qs/QuickTileLayout.java
new file mode 100644
index 0000000..bb2340c
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/qs/QuickTileLayout.java
@@ -0,0 +1,28 @@
+package com.android.systemui.qs;
+
+import android.content.Context;
+import android.util.AttributeSet;
+import android.view.Gravity;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.LinearLayout;
+
+public class QuickTileLayout extends LinearLayout {
+
+    public QuickTileLayout(Context context) {
+        this(context, null);
+    }
+
+    public QuickTileLayout(Context context, AttributeSet attrs) {
+        super(context, attrs);
+        setGravity(Gravity.CENTER);
+    }
+
+    @Override
+    public void addView(View child, int index, ViewGroup.LayoutParams params) {
+        // Make everything square at the height of this view.
+        params = new LayoutParams(params.height, params.height);
+        ((LinearLayout.LayoutParams) params).weight = 1;
+        super.addView(child, index, params);
+    }
+}
diff --git a/packages/SystemUI/src/com/android/systemui/qs/TileLayout.java b/packages/SystemUI/src/com/android/systemui/qs/TileLayout.java
index 02b8fe3..e0c39c5 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/TileLayout.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/TileLayout.java
@@ -2,35 +2,63 @@
 
 import android.content.Context;
 import android.content.res.Resources;
+import android.util.AttributeSet;
 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;
 
 import java.util.ArrayList;
 
-public class TileLayout extends ViewGroup {
+public class TileLayout extends ViewGroup implements QSTileLayout {
 
     private static final float TILE_ASPECT = 1.2f;
 
     private static final String TAG = "TileLayout";
 
     private int mDualTileUnderlap;
-    private int mColumns;
+    protected int mColumns;
     private int mCellWidth;
     private int mCellHeight;
     private int mLargeCellWidth;
     private int mLargeCellHeight;
 
-    private final ArrayList<TileRecord> mRecords;
+    protected boolean mAllowDual = true;
 
-    public TileLayout(Context context, ArrayList<TileRecord> records) {
-        super(context);
-        mRecords = records;
+    protected final ArrayList<TileRecord> mRecords = new ArrayList<>();
+
+    public TileLayout(Context context) {
+        this(context, null);
+    }
+
+    public TileLayout(Context context, AttributeSet attrs) {
+        super(context, attrs);
         updateResources();
     }
 
+    @Override
+    public int getOffsetTop(TileRecord tile) {
+        return getTop();
+    }
+
+    public void addTile(TileRecord tile) {
+        mRecords.add(tile);
+        addView(tile.tileView);
+    }
+
+    @Override
+    public void removeTile(TileRecord tile) {
+        mRecords.remove(tile);
+        removeView(tile.tileView);
+    }
+
+    @Override
+    public void setTileVisibility(TileRecord tile, int visibility) {
+        tile.tileView.setVisibility(visibility);
+    }
+
     public void updateResources() {
         final Resources res = mContext.getResources();
         final int columns = Math.max(1, res.getInteger(R.integer.quick_settings_num_columns));
@@ -56,10 +84,11 @@
             if (record.tileView.getVisibility() == GONE) continue;
             // wrap to next column if we've reached the max # of columns
             // also don't allow dual + single tiles on the same row
-            if (r == -1 || c == (mColumns - 1) || rowIsDual != record.tile.supportsDualTargets()) {
+            if (r == -1 || c == (mColumns - 1)
+                    || rowIsDual != (mAllowDual && record.tile.supportsDualTargets())) {
                 r++;
                 c = 0;
-                rowIsDual = record.tile.supportsDualTargets();
+                rowIsDual = mAllowDual && record.tile.supportsDualTargets();
             } else {
                 c++;
             }
@@ -70,7 +99,8 @@
 
         View previousView = this;
         for (TileRecord record : mRecords) {
-            if (record.tileView.setDual(record.tile.supportsDualTargets())) {
+            if (record.tileView.setType(mAllowDual ? record.tile.getTileType()
+                    : QSTileView.QS_TYPE_NORMAL)) {
                 record.tileView.handleStateChanged(record.tile.getState());
             }
             if (record.tileView.getVisibility() == GONE) continue;
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 abce31f..fd70d02 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/tiles/BluetoothTile.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/BluetoothTile.java
@@ -31,6 +31,7 @@
 import com.android.systemui.qs.QSDetailItems;
 import com.android.systemui.qs.QSDetailItems.Item;
 import com.android.systemui.qs.QSTile;
+import com.android.systemui.qs.QSTileView;
 import com.android.systemui.statusbar.policy.BluetoothController;
 
 import java.util.Collection;
@@ -43,15 +44,18 @@
     private final BluetoothController mController;
     private final BluetoothDetailAdapter mDetailAdapter;
 
-    public BluetoothTile(Host host) {
+    private final boolean mAlwaysDetail;
+
+    public BluetoothTile(Host host, boolean alwaysDetail) {
         super(host);
+        mAlwaysDetail = alwaysDetail;
         mController = host.getBluetoothController();
         mDetailAdapter = new BluetoothDetailAdapter();
     }
 
     @Override
-    public boolean supportsDualTargets() {
-        return true;
+    public int getTileType() {
+        return QSTileView.QS_TYPE_DUAL;
     }
 
     @Override
@@ -75,6 +79,10 @@
 
     @Override
     protected void handleClick() {
+        if (mAlwaysDetail) {
+            handleSecondaryClick();
+            return;
+        }
         final boolean isEnabled = (Boolean)mState.value;
         MetricsLogger.action(mContext, getMetricsCategory(), !isEnabled);
         mController.setBluetoothEnabled(!isEnabled);
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/QAirplaneTile.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/QAirplaneTile.java
new file mode 100644
index 0000000..13ccaaa
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/QAirplaneTile.java
@@ -0,0 +1,32 @@
+/*
+ * 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.qs.tiles;
+
+import com.android.systemui.qs.QSTileView;
+
+/** Quick settings tile: Wifi **/
+public class QAirplaneTile extends AirplaneModeTile {
+
+    public QAirplaneTile(Host host) {
+        super(host);
+    }
+
+    @Override
+    public int getTileType() {
+        return QSTileView.QS_TYPE_QUICK;
+    }
+}
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/QBluetoothTile.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/QBluetoothTile.java
new file mode 100644
index 0000000..02975cb
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/QBluetoothTile.java
@@ -0,0 +1,32 @@
+/*
+ * 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.qs.tiles;
+
+import com.android.systemui.qs.QSTileView;
+
+/** Quick settings tile: Bluetooth **/
+public class QBluetoothTile extends BluetoothTile  {
+
+    public QBluetoothTile(Host host) {
+        super(host, false);
+    }
+
+    @Override
+    public int getTileType() {
+        return QSTileView.QS_TYPE_QUICK;
+    }
+}
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/QFlashlightTile.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/QFlashlightTile.java
new file mode 100644
index 0000000..31035cd
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/QFlashlightTile.java
@@ -0,0 +1,32 @@
+/*
+ * 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.qs.tiles;
+
+import com.android.systemui.qs.QSTileView;
+
+/** Quick settings tile: Wifi **/
+public class QFlashlightTile extends FlashlightTile {
+
+    public QFlashlightTile(Host host) {
+        super(host);
+    }
+
+    @Override
+    public int getTileType() {
+        return QSTileView.QS_TYPE_QUICK;
+    }
+}
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/QRotationLockTile.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/QRotationLockTile.java
new file mode 100644
index 0000000..e066bab
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/QRotationLockTile.java
@@ -0,0 +1,32 @@
+/*
+ * 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.qs.tiles;
+
+import com.android.systemui.qs.QSTileView;
+
+/** Quick settings tile: Rotation **/
+public class QRotationLockTile extends RotationLockTile {
+
+    public QRotationLockTile(Host host) {
+        super(host);
+    }
+
+    @Override
+    public int getTileType() {
+        return QSTileView.QS_TYPE_QUICK;
+    }
+}
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/QWifiTile.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/QWifiTile.java
new file mode 100644
index 0000000..87194fb
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/QWifiTile.java
@@ -0,0 +1,32 @@
+/*
+ * 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.qs.tiles;
+
+import com.android.systemui.qs.QSTileView;
+
+/** Quick settings tile: Wifi **/
+public class QWifiTile extends WifiTile {
+
+    public QWifiTile(Host host) {
+        super(host, false);
+    }
+
+    @Override
+    public int getTileType() {
+        return QSTileView.QS_TYPE_QUICK;
+    }
+}
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 e654efd..3295e14 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/tiles/WifiTile.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/WifiTile.java
@@ -50,16 +50,19 @@
 
     private final WifiSignalCallback mSignalCallback = new WifiSignalCallback();
 
-    public WifiTile(Host host) {
+    private final boolean mAlwaysDetail;
+
+    public WifiTile(Host host, boolean alwaysDetail) {
         super(host);
+        mAlwaysDetail = alwaysDetail;
         mController = host.getNetworkController();
         mWifiController = mController.getAccessPointController();
         mDetailAdapter = new WifiDetailAdapter();
     }
 
     @Override
-    public boolean supportsDualTargets() {
-        return true;
+    public int getTileType() {
+        return QSTileView.QS_TYPE_DUAL;
     }
 
     @Override
@@ -97,6 +100,10 @@
 
     @Override
     protected void handleClick() {
+        if (mAlwaysDetail) {
+            handleSecondaryClick();
+            return;
+        }
         mState.copyTo(mStateBeforeClick);
         MetricsLogger.action(mContext, getMetricsCategory(), !mState.enabled);
         mController.setWifiEnabled(!mState.enabled);
diff --git a/packages/SystemUI/src/com/android/systemui/recents/RecentsResizeTaskDialog.java b/packages/SystemUI/src/com/android/systemui/recents/RecentsResizeTaskDialog.java
index b701e0b..300ea2a 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/RecentsResizeTaskDialog.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/RecentsResizeTaskDialog.java
@@ -197,6 +197,7 @@
                 break;
             case PLACE_FULL:
                 // Nothing to change.
+                mBounds[0] = null;
                 break;
         }
 
@@ -213,10 +214,13 @@
         dismiss();
         mRecentsActivity.dismissRecentsToHomeWithoutTransitionAnimation();
 
-        // Resize all tasks beginning from the "oldest" one.
-        for (int i = additionalTasks; i >= 0; --i) {
-            if (mTasks[i] != null) {
-               mSsp.resizeTask(mTasks[i].key.id, mBounds[i]);
+        // In debug mode, we force all task to be resizeable regardless of the
+        // current app configuration.
+        if (RecentsConfiguration.getInstance().multiStackEnabled) {
+            for (int i = additionalTasks; i >= 0; --i) {
+                if (mTasks[i] != null) {
+                    mSsp.setTaskResizeable(mTasks[i].key.id);
+                }
             }
         }
 
@@ -224,7 +228,7 @@
         // the focus ends on the selected one.
         for (int i = additionalTasks; i >= 0; --i) {
             if (mTasks[i] != null) {
-                mRecentsView.launchTask(mTasks[i]);
+                mRecentsView.launchTask(mTasks[i], mBounds[i]);
             }
         }
     }
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 89aeabc..bead1b0 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/misc/SystemServicesProxy.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/misc/SystemServicesProxy.java
@@ -271,17 +271,12 @@
         return null;
     }
 
-    /** Resize a given task. */
-    public void resizeTask(int taskId, Rect bounds) {
+    /** Allow a task to resize. */
+    public void setTaskResizeable(int taskId) {
         if (mIam == null) return;
 
         try {
-            if (RecentsConfiguration.getInstance().multiStackEnabled) {
-                // In debug mode, we force all task to be resizeable regardless of the
-                // current app configuration.
-                mIam.setTaskResizeable(taskId, true);
-            }
-            mIam.resizeTask(taskId, bounds);
+            mIam.setTaskResizeable(taskId, true);
         } catch (RemoteException e) {
             e.printStackTrace();
         }
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 00ac5f9..651b29a 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/views/RecentsView.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/views/RecentsView.java
@@ -165,7 +165,7 @@
 
     /** Gets the next task in the stack - or if the last - the top task */
     public Task getNextTaskOrTopTask(Task taskToSearch) {
-        Task returnTask = null; 
+        Task returnTask = null;
         boolean found = false;
         List<TaskStackView> stackViews = getTaskStackViews();
         int stackCount = stackViews.size();
@@ -203,7 +203,7 @@
                 TaskView tv = taskViews.get(j);
                 Task task = tv.getTask();
                 if (tv.isFocusedTask()) {
-                    onTaskViewClicked(stackView, tv, stack, task, false);
+                    onTaskViewClicked(stackView, tv, stack, task, false, false, null);
                     return true;
                 }
             }
@@ -212,7 +212,7 @@
     }
 
     /** Launches a given task. */
-    public boolean launchTask(Task task) {
+    public boolean launchTask(Task task, Rect taskBounds) {
         // Get the first stack view
         List<TaskStackView> stackViews = getTaskStackViews();
         int stackCount = stackViews.size();
@@ -225,7 +225,7 @@
             for (int j = 0; j < taskViewCount; j++) {
                 TaskView tv = taskViews.get(j);
                 if (tv.getTask() == task) {
-                    onTaskViewClicked(stackView, tv, stack, task, false);
+                    onTaskViewClicked(stackView, tv, stack, task, false, true, taskBounds);
                     return true;
                 }
             }
@@ -250,7 +250,7 @@
                     if (tasks.get(j).isLaunchTarget) {
                         Task task = tasks.get(j);
                         TaskView tv = stackView.getChildViewForTask(task);
-                        onTaskViewClicked(stackView, tv, stack, task, false);
+                        onTaskViewClicked(stackView, tv, stack, task, false, false, null);
                         return true;
                     }
                 }
@@ -373,7 +373,7 @@
                     searchBarSpaceBounds.right, searchBarSpaceBounds.bottom);
         }
 
-        // Layout each TaskStackView with the full width and height of the window since the 
+        // Layout each TaskStackView with the full width and height of the window since the
         // transition view is a child of that stack view
         List<TaskStackView> stackViews = getTaskStackViews();
         int stackCount = stackViews.size();
@@ -604,7 +604,8 @@
 
     @Override
     public void onTaskViewClicked(final TaskStackView stackView, final TaskView tv,
-                                  final TaskStack stack, final Task task, final boolean lockToTask) {
+                                  final TaskStack stack, final Task task, final boolean lockToTask,
+                                  final boolean boundsValid, final Rect bounds) {
 
         // Notify any callbacks of the launching of a new task
         if (mCb != null) {
@@ -632,9 +633,9 @@
         final SystemServicesProxy ssp =
                 RecentsTaskLoader.getInstance().getSystemServicesProxy();
         ActivityOptions opts = null;
+        ActivityOptions.OnAnimationStartedListener animStartedListener = null;
         if (task.thumbnail != null && task.thumbnail.getWidth() > 0 &&
                 task.thumbnail.getHeight() > 0) {
-            ActivityOptions.OnAnimationStartedListener animStartedListener = null;
             if (lockToTask) {
                 animStartedListener = new ActivityOptions.OnAnimationStartedListener() {
                     boolean mTriggered = false;
@@ -665,9 +666,14 @@
                         offsetX, offsetY, transform.rect.width(), transform.rect.height(),
                         sourceView.getHandler(), animStartedListener);
             }
+        } else {
+            opts = ActivityOptions.makeBasic();
         }
-
+        if (boundsValid) {
+            opts.setBounds(bounds);
+        }
         final ActivityOptions launchOpts = opts;
+        final boolean screenPinningRequested = (animStartedListener == null) && lockToTask;
         final Runnable launchRunnable = new Runnable() {
             @Override
             public void run() {
@@ -677,7 +683,7 @@
                 } else {
                     if (ssp.startActivityFromRecents(getContext(), task.key.id,
                             task.activityLabel, launchOpts)) {
-                        if (launchOpts == null && lockToTask) {
+                        if (screenPinningRequested) {
                             mCb.onScreenPinningRequest();
                         }
                     } else {
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 0068f84..4e82c8a 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackView.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackView.java
@@ -59,7 +59,7 @@
     /** The TaskView callbacks */
     interface TaskStackViewCallbacks {
         public void onTaskViewClicked(TaskStackView stackView, TaskView tv, TaskStack stack, Task t,
-                                      boolean lockToTask);
+                                      boolean lockToTask, boolean boundsValid, Rect bounds);
         public void onTaskViewAppInfoClicked(Task t);
         public void onTaskViewDismissed(Task t);
         public void onAllTaskViewsDismissed(ArrayList<Task> removedTasks);
@@ -1377,7 +1377,7 @@
         mUIDozeTrigger.stopDozing();
 
         if (mCb != null) {
-            mCb.onTaskViewClicked(this, tv, mStack, task, lockToTask);
+            mCb.onTaskViewClicked(this, tv, mStack, task, lockToTask, false, null);
         }
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/BaseStatusBar.java b/packages/SystemUI/src/com/android/systemui/statusbar/BaseStatusBar.java
index de6c4b1..2c964be 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/BaseStatusBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/BaseStatusBar.java
@@ -1754,11 +1754,13 @@
     protected void handleVisibleToUserChanged(boolean visibleToUser) {
         try {
             if (visibleToUser) {
-                boolean clearNotificationEffects = !isPanelFullyCollapsed() &&
-                    (mShowLockscreenNotifications ||
-                        (mState == StatusBarState.SHADE || mState == StatusBarState.SHADE_LOCKED));
+                boolean pinnedHeadsUp = mHeadsUpManager.hasPinnedHeadsUp();
+                boolean clearNotificationEffects =
+                    ((mShowLockscreenNotifications && mState == StatusBarState.KEYGUARD) ||
+                            (!pinnedHeadsUp && (mState == StatusBarState.SHADE
+                                    || mState == StatusBarState.SHADE_LOCKED)));
                 int notificationLoad = mNotificationData.getActiveNotifications().size();
-                if (mHeadsUpManager.hasPinnedHeadsUp() && isPanelFullyCollapsed())  {
+                if (pinnedHeadsUp && isPanelFullyCollapsed())  {
                     notificationLoad = 1;
                 } else {
                     MetricsLogger.histogram(mContext, "note_load", notificationLoad);
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 4e69999..84082db 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/FingerprintUnlockController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/FingerprintUnlockController.java
@@ -227,7 +227,7 @@
                 return MODE_DISMISS_BOUNCER;
             } else if (unlockingAllowed) {
                 return MODE_UNLOCK;
-            } else {
+            } else if (!mStatusBarKeyguardViewManager.isBouncerShowing()) {
                 return MODE_SHOW_BOUNCER;
             }
         }
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 d30411a..f4439bf 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardBottomAreaView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardBottomAreaView.java
@@ -262,14 +262,21 @@
         return (secure && !canSkipBouncer) ? SECURE_CAMERA_INTENT : INSECURE_CAMERA_INTENT;
     }
 
+    /**
+     * Resolves the intent to launch the camera application.
+     */
+    public ResolveInfo resolveCameraIntent() {
+        return mContext.getPackageManager().resolveActivityAsUser(getCameraIntent(),
+                PackageManager.MATCH_DEFAULT_ONLY,
+                KeyguardUpdateMonitor.getCurrentUser());
+    }
+
     private void updateCameraVisibility() {
         if (mCameraImageView == null) {
             // Things are not set up yet; reply hazy, ask again later
             return;
         }
-        ResolveInfo resolved = mContext.getPackageManager().resolveActivityAsUser(getCameraIntent(),
-                PackageManager.MATCH_DEFAULT_ONLY,
-                KeyguardUpdateMonitor.getCurrentUser());
+        ResolveInfo resolved = resolveCameraIntent();
         boolean visible = !isCameraDisabledByDpm() && resolved != null
                 && getResources().getBoolean(R.bool.config_keyguardShowCameraAffordance)
                 && mUserSetupComplete;
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 93ecb06..030501b 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/LockIcon.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/LockIcon.java
@@ -166,10 +166,13 @@
         if (mAccessibilityController == null) {
             return;
         }
+        boolean trustManagedOrFingerprintAllowed = mUnlockMethodCache.isTrustManaged()
+                || KeyguardUpdateMonitor.getInstance(mContext).isUnlockingWithFingerprintAllowed();
+
         boolean clickToUnlock = mAccessibilityController.isTouchExplorationEnabled();
-        boolean clickToForceLock = mUnlockMethodCache.isTrustManaged()
+        boolean clickToForceLock = trustManagedOrFingerprintAllowed
                 && !mAccessibilityController.isAccessibilityEnabled();
-        boolean longClickToForceLock = mUnlockMethodCache.isTrustManaged()
+        boolean longClickToForceLock = trustManagedOrFingerprintAllowed
                 && !clickToForceLock;
         setClickable(clickToForceLock || clickToUnlock);
         setLongClickable(longClickToForceLock);
@@ -245,10 +248,10 @@
             return STATE_FINGERPRINT_ERROR;
         } else if (mUnlockMethodCache.canSkipBouncer()) {
             return STATE_LOCK_OPEN;
-        } else if (fingerprintRunning && unlockingAllowed) {
-            return STATE_FINGERPRINT;
         } else if (mUnlockMethodCache.isFaceUnlockRunning()) {
             return STATE_FACE_UNLOCK;
+        } else if (fingerprintRunning && unlockingAllowed) {
+            return STATE_FINGERPRINT;
         } else {
             return STATE_LOCKED;
         }
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 950b162..310625e 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationGroupManager.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationGroupManager.java
@@ -78,6 +78,13 @@
         Notification notif = sbn.getNotification();
         String groupKey = sbn.getGroupKey();
         final NotificationGroup group = mGroupMap.get(groupKey);
+        if (group == null) {
+            // When an app posts 2 different notifications as summary of the same group, then a
+            // cancellation of the first notification removes this group.
+            // This situation is not supported and we will not allow such notifications anymore in
+            // the close future. See b/23676310 for reference.
+            return;
+        }
         if (notif.isGroupSummary()) {
             group.summary = null;
         } else {
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 3ad7246..814a65e 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationPanelView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationPanelView.java
@@ -21,7 +21,10 @@
 import android.animation.ObjectAnimator;
 import android.animation.PropertyValuesHolder;
 import android.animation.ValueAnimator;
+import android.app.ActivityManager;
+import android.app.ActivityManager.RunningTaskInfo;
 import android.content.Context;
+import android.content.pm.ResolveInfo;
 import android.content.res.Configuration;
 import android.graphics.Canvas;
 import android.graphics.Color;
@@ -62,6 +65,8 @@
 import com.android.systemui.statusbar.stack.NotificationStackScrollLayout;
 import com.android.systemui.statusbar.stack.StackStateAnimator;
 
+import java.util.List;
+
 public class NotificationPanelView extends PanelView implements
         ExpandableView.OnHeightChangedListener, ObservableScrollView.Listener,
         View.OnClickListener, NotificationStackScrollLayout.OnOverscrollTopChangedListener,
@@ -2462,7 +2467,28 @@
         getCenterIcon().setLaunchingAffordance(launchingAffordance);
     }
 
-    public boolean canCameraGestureBeLaunched() {
-        return !mAfforanceHelper.isSwipingInProgress();
+    /**
+     * Whether the camera application can be launched for the camera launch gesture.
+     *
+     * @param keyguardIsShowing whether keyguard is being shown
+     */
+    public boolean canCameraGestureBeLaunched(boolean keyguardIsShowing) {
+        ResolveInfo resolveInfo = mKeyguardBottomArea.resolveCameraIntent();
+        String packageToLaunch = (resolveInfo == null || resolveInfo.activityInfo == null)
+                ? null : resolveInfo.activityInfo.packageName;
+        return packageToLaunch != null &&
+               (keyguardIsShowing || !isForegroundApp(packageToLaunch)) &&
+               !mAfforanceHelper.isSwipingInProgress();
+    }
+
+    /**
+     * Return true if the applications with the package name is running in foreground.
+     *
+     * @param pkgName application package name.
+     */
+    private boolean isForegroundApp(String pkgName) {
+        ActivityManager am = getContext().getSystemService(ActivityManager.class);
+        List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
+        return !tasks.isEmpty() && pkgName.equals(tasks.get(0).topActivity.getPackageName());
     }
 }
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 7b04da0..55fbb2a 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
@@ -3794,12 +3794,9 @@
         // down on the lockscreen), clear notification LED, vibration,
         // ringing.
         // Other transitions are covered in handleVisibleToUserChanged().
-        if (state != mState && mVisible && state == StatusBarState.SHADE_LOCKED) {
-            try {
-                mBarService.clearNotificationEffects();
-            } catch (RemoteException e) {
-                // Ignore.
-            }
+        if (state != mState && mVisible && (state == StatusBarState.SHADE_LOCKED
+                || (state == StatusBarState.SHADE && isGoingToNotificationShade()))) {
+            clearNotificationEffects();
         }
         mState = state;
         mGroupManager.setStatusBarState(state);
@@ -4150,7 +4147,8 @@
 
     @Override
     public void onCameraLaunchGestureDetected() {
-        if (!mNotificationPanel.canCameraGestureBeLaunched()) {
+        if (!mNotificationPanel.canCameraGestureBeLaunched(
+                mStatusBarKeyguardViewManager.isShowing() && mExpandedVisible)) {
             return;
         }
         if (!mDeviceInteractive) {
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 12434ac..385c5d5 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/QSTileHost.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/QSTileHost.java
@@ -36,6 +36,11 @@
 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.QAirplaneTile;
+import com.android.systemui.qs.tiles.QBluetoothTile;
+import com.android.systemui.qs.tiles.QFlashlightTile;
+import com.android.systemui.qs.tiles.QRotationLockTile;
+import com.android.systemui.qs.tiles.QWifiTile;
 import com.android.systemui.qs.tiles.RotationLockTile;
 import com.android.systemui.qs.tiles.WifiTile;
 import com.android.systemui.statusbar.policy.BluetoothController;
@@ -64,7 +69,7 @@
     private static final String TAG = "QSTileHost";
     private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
 
-    protected static final String TILES_SETTING = "sysui_qs_tiles";
+    public static final String TILES_SETTING = "sysui_qs_tiles";
 
     private final Context mContext;
     private final PhoneStatusBar mStatusBar;
@@ -243,8 +248,8 @@
     }
 
     protected QSTile<?> createTile(String tileSpec) {
-        if (tileSpec.equals("wifi")) return new WifiTile(this);
-        else if (tileSpec.equals("bt")) return new BluetoothTile(this);
+        if (tileSpec.equals("wifi")) return new WifiTile(this, false);
+        else if (tileSpec.equals("bt")) return new BluetoothTile(this, false);
         else if (tileSpec.equals("inversion")) return new ColorInversionTile(this);
         else if (tileSpec.equals("cell")) return new CellularTile(this);
         else if (tileSpec.equals("airplane")) return new AirplaneModeTile(this);
@@ -254,6 +259,16 @@
         else if (tileSpec.equals("location")) return new LocationTile(this);
         else if (tileSpec.equals("cast")) return new CastTile(this);
         else if (tileSpec.equals("hotspot")) return new HotspotTile(this);
+        // Detail only versions of wifi and bluetooth.
+        else if (tileSpec.equals("dwifi")) return new WifiTile(this, true);
+        else if (tileSpec.equals("dbt")) return new BluetoothTile(this, true);
+        // Quick tiles, no text.
+        else if (tileSpec.equals("qwifi")) return new QWifiTile(this);
+        else if (tileSpec.equals("qbt")) return new QBluetoothTile(this);
+        else if (tileSpec.equals("qairplane")) return new QAirplaneTile(this);
+        else if (tileSpec.equals("qrotation")) return new QRotationLockTile(this);
+        else if (tileSpec.equals("qflashlight")) return new QFlashlightTile(this);
+        // Intent tiles.
         else if (tileSpec.startsWith(IntentTile.PREFIX)) return IntentTile.create(this,tileSpec);
         else throw new IllegalArgumentException("Bad tile spec: " + tileSpec);
     }
diff --git a/packages/SystemUI/src/com/android/systemui/tuner/QSPagingSwitch.java b/packages/SystemUI/src/com/android/systemui/tuner/QSPagingSwitch.java
new file mode 100644
index 0000000..343a231
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/tuner/QSPagingSwitch.java
@@ -0,0 +1,26 @@
+package com.android.systemui.tuner;
+
+import android.content.Context;
+import android.provider.Settings;
+import android.util.AttributeSet;
+
+import com.android.systemui.statusbar.phone.QSTileHost;
+
+public class QSPagingSwitch extends TunerSwitch {
+
+    private static final String QS_PAGE_TILES =
+            "dwifi,dbt,inversion,dnd,cell,airplane,rotation,flashlight,location,"
+             + "hotspot,qwifi,qbt,qrotation,qflashlight,qairplane,cast";
+
+    public QSPagingSwitch(Context context, AttributeSet attrs) {
+        super(context, attrs);
+    }
+
+    @Override
+    protected boolean persistBoolean(boolean value) {
+        Settings.Secure.putString(getContext().getContentResolver(), QSTileHost.TILES_SETTING,
+                value ? QS_PAGE_TILES : "default");
+        return super.persistBoolean(value);
+    }
+
+}
diff --git a/packages/SystemUI/src/com/android/systemui/tuner/QsTuner.java b/packages/SystemUI/src/com/android/systemui/tuner/QsTuner.java
index 37ac098..772f866 100644
--- a/packages/SystemUI/src/com/android/systemui/tuner/QsTuner.java
+++ b/packages/SystemUI/src/com/android/systemui/tuner/QsTuner.java
@@ -389,10 +389,11 @@
             mView = super.createTileView(context);
             return mView;
         }
-
+        
         @Override
-        public boolean supportsDualTargets() {
-            return "wifi".equals(mSpec) || "bt".equals(mSpec);
+        public int getTileType() {
+            return "wifi".equals(mSpec) || "bt".equals(mSpec) ? QSTileView.QS_TYPE_DUAL
+                    : QSTileView.QS_TYPE_NORMAL;
         }
 
         @Override
diff --git a/packages/SystemUI/src/com/android/systemui/tuner/TunerFragment.java b/packages/SystemUI/src/com/android/systemui/tuner/TunerFragment.java
index 96ad756..920f875 100644
--- a/packages/SystemUI/src/com/android/systemui/tuner/TunerFragment.java
+++ b/packages/SystemUI/src/com/android/systemui/tuner/TunerFragment.java
@@ -36,6 +36,8 @@
 
 import com.android.internal.logging.MetricsLogger;
 import com.android.systemui.R;
+import com.android.systemui.qs.QSPanel;
+import com.android.systemui.tuner.TunerService.Tunable;
 
 import static com.android.systemui.BatteryMeterView.SHOW_PERCENT_SETTING;
 
@@ -55,6 +57,8 @@
 
     private SwitchPreference mBatteryPct;
 
+    private Preference mQsTuner;
+
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 
@@ -62,7 +66,8 @@
         getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
         setHasOptionsMenu(true);
 
-        findPreference(KEY_QS_TUNER).setOnPreferenceClickListener(new OnPreferenceClickListener() {
+        mQsTuner = findPreference(KEY_QS_TUNER);
+        mQsTuner.setOnPreferenceClickListener(new OnPreferenceClickListener() {
             @Override
             public boolean onPreferenceClick(Preference preference) {
                 FragmentTransaction ft = getFragmentManager().beginTransaction();
@@ -96,6 +101,13 @@
                         }
                     }).show();
         }
+        TunerService.get(getContext()).addTunable(mQsPaging, QSPanel.QS_PAGED_PANEL);
+    }
+
+    @Override
+    public void onDestroy() {
+        super.onDestroy();
+        TunerService.get(getContext()).removeTunable(mQsPaging);
     }
 
     @Override
@@ -167,4 +179,12 @@
             return true;
         }
     };
+
+    private final Tunable mQsPaging = new Tunable() {
+        @Override
+        public void onTuningChanged(String key, String newValue) {
+            // Only enable QS rearranging when paging is off, because its very broken.
+            mQsTuner.setEnabled(newValue == null || Integer.parseInt(newValue) == 0);
+        }
+    };
 }
diff --git a/packages/SystemUI/src/com/android/systemui/tuner/TunerSwitch.java b/packages/SystemUI/src/com/android/systemui/tuner/TunerSwitch.java
index 0740e08..54078b0 100644
--- a/packages/SystemUI/src/com/android/systemui/tuner/TunerSwitch.java
+++ b/packages/SystemUI/src/com/android/systemui/tuner/TunerSwitch.java
@@ -1,16 +1,23 @@
 package com.android.systemui.tuner;
 
 import android.content.Context;
+import android.content.res.TypedArray;
 import android.preference.SwitchPreference;
 import android.provider.Settings;
 import android.util.AttributeSet;
 
+import com.android.systemui.R;
 import com.android.systemui.tuner.TunerService.Tunable;
 
 public class TunerSwitch extends SwitchPreference implements Tunable {
 
+    private final boolean mDefault;
+
     public TunerSwitch(Context context, AttributeSet attrs) {
         super(context, attrs);
+
+        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TunerSwitch);
+        mDefault = a.getBoolean(R.styleable.TunerSwitch_defValue, false);
     }
 
     @Override
@@ -27,7 +34,7 @@
 
     @Override
     public void onTuningChanged(String key, String newValue) {
-        setChecked(newValue != null && Integer.parseInt(newValue) != 0);
+        setChecked(newValue != null ? Integer.parseInt(newValue) != 0 : mDefault);
     }
 
     @Override
diff --git a/packages/SystemUI/src/com/android/systemui/volume/VolumeDialog.java b/packages/SystemUI/src/com/android/systemui/volume/VolumeDialog.java
index 95a8d39..6475cbf 100644
--- a/packages/SystemUI/src/com/android/systemui/volume/VolumeDialog.java
+++ b/packages/SystemUI/src/com/android/systemui/volume/VolumeDialog.java
@@ -421,8 +421,9 @@
                         }
                     }
                 } else {
-                    final boolean vmute = row.ss.level == 0;
-                    mController.setStreamVolume(stream, vmute ? row.lastAudibleLevel : 0);
+                    final boolean vmute = row.ss.level == row.ss.levelMin;
+                    mController.setStreamVolume(stream,
+                            vmute ? row.lastAudibleLevel : row.ss.levelMin);
                 }
                 row.userAttempt = 0;  // reset the grace period, slider should update immediately
             }
@@ -1024,6 +1025,7 @@
                 final int minProgress = mRow.ss.levelMin * 100;
                 if (progress < minProgress) {
                     seekBar.setProgress(minProgress);
+                    progress = minProgress;
                 }
             }
             final int userLevel = getImpliedLevel(seekBar, progress);
diff --git a/services/accessibility/java/com/android/server/accessibility/AccessibilityManagerService.java b/services/accessibility/java/com/android/server/accessibility/AccessibilityManagerService.java
index 57769e7..7051b52 100644
--- a/services/accessibility/java/com/android/server/accessibility/AccessibilityManagerService.java
+++ b/services/accessibility/java/com/android/server/accessibility/AccessibilityManagerService.java
@@ -205,9 +205,7 @@
 
     private final UserManager mUserManager;
 
-    private final LockPatternUtils mLockPatternUtils;
-
-    private int mCurrentUserId = UserHandle.USER_OWNER;
+    private int mCurrentUserId = UserHandle.USER_SYSTEM;
 
     //TODO: Remove this hack
     private boolean mInitialized;
@@ -230,7 +228,6 @@
         mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
         mSecurityPolicy = new SecurityPolicy();
         mMainHandler = new MainHandler(mContext.getMainLooper());
-        mLockPatternUtils = new LockPatternUtils(context);
         registerBroadcastReceivers();
         new AccessibilityContentObserver(mMainHandler).register(
                 context.getContentResolver());
@@ -866,17 +863,18 @@
     }
 
     // Called only during settings restore; currently supports only the owner user
+    // TODO: http://b/22388012
     void restoreEnabledAccessibilityServicesLocked(String oldSetting, String newSetting) {
         readComponentNamesFromStringLocked(oldSetting, mTempComponentNameSet, false);
         readComponentNamesFromStringLocked(newSetting, mTempComponentNameSet, true);
 
-        UserState userState = getUserStateLocked(UserHandle.USER_OWNER);
+        UserState userState = getUserStateLocked(UserHandle.USER_SYSTEM);
         userState.mEnabledServices.clear();
         userState.mEnabledServices.addAll(mTempComponentNameSet);
         persistComponentNamesToSettingLocked(
                 Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
                 userState.mEnabledServices,
-                UserHandle.USER_OWNER);
+                UserHandle.USER_SYSTEM);
         onUserStateChangedLocked(userState);
     }
 
@@ -1646,10 +1644,6 @@
         DisplayAdjustmentUtils.applyAdjustments(mContext, userState.mUserId);
     }
 
-    private boolean hasRunningServicesLocked(UserState userState) {
-        return !userState.mBoundServices.isEmpty() || !userState.mBindingServices.isEmpty();
-    }
-
     private MagnificationSpec getCompatibleMagnificationSpecLocked(int windowId) {
         IBinder windowToken = mGlobalWindowTokens.get(windowId);
         if (windowToken == null) {
diff --git a/services/core/java/com/android/server/AlarmManagerService.java b/services/core/java/com/android/server/AlarmManagerService.java
index 4288fa2..882899e 100644
--- a/services/core/java/com/android/server/AlarmManagerService.java
+++ b/services/core/java/com/android/server/AlarmManagerService.java
@@ -177,7 +177,7 @@
 
         private static final long DEFAULT_MIN_FUTURITY = 5 * 1000;
         private static final long DEFAULT_MIN_INTERVAL = 60 * 1000;
-        private static final long DEFAULT_ALLOW_WHILE_IDLE_SHORT_TIME = 60*1000;
+        private static final long DEFAULT_ALLOW_WHILE_IDLE_SHORT_TIME = DEFAULT_MIN_FUTURITY;
         private static final long DEFAULT_ALLOW_WHILE_IDLE_LONG_TIME = 15*60*1000;
         private static final long DEFAULT_ALLOW_WHILE_IDLE_WHITELIST_DURATION = 10*1000;
 
diff --git a/services/core/java/com/android/server/AnyMotionDetector.java b/services/core/java/com/android/server/AnyMotionDetector.java
index 6a67316..a0b5c15 100644
--- a/services/core/java/com/android/server/AnyMotionDetector.java
+++ b/services/core/java/com/android/server/AnyMotionDetector.java
@@ -58,9 +58,6 @@
     /** Current measurement state. */
     private int mState;
 
-    /** Threshold angle in degrees beyond which the device is considered moving. */
-    private final float THRESHOLD_ANGLE = 2f;
-
     /** Threshold energy above which the device is considered moving. */
     private final float THRESHOLD_ENERGY = 5f;
 
@@ -88,6 +85,9 @@
     private SensorManager mSensorManager;
     private PowerManager.WakeLock mWakeLock;
 
+    /** Threshold angle in degrees beyond which the device is considered moving. */
+    private final float mThresholdAngle;
+
     /** The minimum number of samples required to detect AnyMotion. */
     private int mNumSufficientSamples;
 
@@ -106,7 +106,7 @@
     private DeviceIdleCallback mCallback = null;
 
     public AnyMotionDetector(PowerManager pm, Handler handler, SensorManager sm,
-            DeviceIdleCallback callback) {
+            DeviceIdleCallback callback, float thresholdAngle) {
         if (DEBUG) Slog.d(TAG, "AnyMotionDetector instantiated.");
         mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
         mWakeLock.setReferenceCounted(false);
@@ -116,6 +116,7 @@
         mMeasurementInProgress = false;
         mState = STATE_INACTIVE;
         mCallback = callback;
+        mThresholdAngle = thresholdAngle;
         mRunningStats = new RunningSignalStats();
         mNumSufficientSamples = (int) Math.ceil(
                 ((double)ORIENTATION_MEASUREMENT_DURATION_MILLIS / SAMPLING_INTERVAL_MILLIS));
@@ -224,8 +225,9 @@
         Vector3 previousGravityVectorNormalized = mPreviousGravityVector.normalized();
         Vector3 currentGravityVectorNormalized = mCurrentGravityVector.normalized();
         float angle = previousGravityVectorNormalized.angleBetween(currentGravityVectorNormalized);
-        if (DEBUG) Slog.d(TAG, "getStationaryStatus: angle = " + angle);
-        if ((angle < THRESHOLD_ANGLE) && (mRunningStats.getEnergy() < THRESHOLD_ENERGY)) {
+        if (DEBUG) Slog.d(TAG, "getStationaryStatus: angle = " + angle
+                + " energy = " + mRunningStats.getEnergy());
+        if ((angle < mThresholdAngle) && (mRunningStats.getEnergy() < THRESHOLD_ENERGY)) {
             return RESULT_STATIONARY;
         } else if (Float.isNaN(angle)) {
           /**
diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java
index e19447d..2bcee91 100644
--- a/services/core/java/com/android/server/ConnectivityService.java
+++ b/services/core/java/com/android/server/ConnectivityService.java
@@ -1524,10 +1524,14 @@
 
             final long ident = Binder.clearCallingIdentity();
             if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
+                final NetworkInfo ni = intent.getParcelableExtra(
+                        ConnectivityManager.EXTRA_NETWORK_INFO);
+                if (ni.getType() == ConnectivityManager.TYPE_MOBILE_SUPL) {
+                    intent.setAction(ConnectivityManager.CONNECTIVITY_ACTION_SUPL);
+                    intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
+                }
                 final IBatteryStats bs = BatteryStatsService.getService();
                 try {
-                    NetworkInfo ni = intent.getParcelableExtra(
-                            ConnectivityManager.EXTRA_NETWORK_INFO);
                     bs.noteConnectivityChanged(intent.getIntExtra(
                             ConnectivityManager.EXTRA_NETWORK_TYPE, ConnectivityManager.TYPE_NONE),
                             ni != null ? ni.getState().toString() : "?");
diff --git a/services/core/java/com/android/server/DeviceIdleController.java b/services/core/java/com/android/server/DeviceIdleController.java
index 80fd441..46fd28a 100644
--- a/services/core/java/com/android/server/DeviceIdleController.java
+++ b/services/core/java/com/android/server/DeviceIdleController.java
@@ -128,7 +128,8 @@
     private boolean mNotMoving;
     private boolean mLocating;
     private boolean mLocated;
-    private boolean mHaveGps;
+    private boolean mHasGps;
+    private boolean mHasNetworkLocation;
     private Location mLastGenericLocation;
     private Location mLastGpsLocation;
 
@@ -882,17 +883,37 @@
                 mDisplayManager = (DisplayManager) getContext().getSystemService(
                         Context.DISPLAY_SERVICE);
                 mSensorManager = (SensorManager) getContext().getSystemService(Context.SENSOR_SERVICE);
-                mSigMotionSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION);
-                mLocationManager = (LocationManager) getContext().getSystemService(
-                        Context.LOCATION_SERVICE);
-                mLocationRequest = new LocationRequest()
-                    .setQuality(LocationRequest.ACCURACY_FINE)
-                    .setInterval(0)
-                    .setFastestInterval(0)
-                    .setNumUpdates(1);
+                int sigMotionSensorId = getContext().getResources().getInteger(
+                        com.android.internal.R.integer.config_autoPowerModeAnyMotionSensor);
+                if (sigMotionSensorId > 0) {
+                    mSigMotionSensor = mSensorManager.getDefaultSensor(sigMotionSensorId, true);
+                }
+                if (mSigMotionSensor == null && getContext().getResources().getBoolean(
+                        com.android.internal.R.bool.config_autoPowerModePreferWristTilt)) {
+                    mSigMotionSensor = mSensorManager.getDefaultSensor(
+                            Sensor.TYPE_WRIST_TILT_GESTURE);
+                }
+                if (mSigMotionSensor == null) {
+                    // As a last ditch, fall back to SMD.
+                    mSigMotionSensor = mSensorManager.getDefaultSensor(
+                            Sensor.TYPE_SIGNIFICANT_MOTION);
+                }
+                if (getContext().getResources().getBoolean(
+                        com.android.internal.R.bool.config_autoPowerModePrefetchLocation)) {
+                    mLocationManager = (LocationManager) getContext().getSystemService(
+                            Context.LOCATION_SERVICE);
+                    mLocationRequest = new LocationRequest()
+                        .setQuality(LocationRequest.ACCURACY_FINE)
+                        .setInterval(0)
+                        .setFastestInterval(0)
+                        .setNumUpdates(1);
+                }
+
+                float angleThreshold = getContext().getResources().getInteger(
+                        com.android.internal.R.integer.config_autoPowerModeThresholdAngle) / 100f;
                 mAnyMotionDetector = new AnyMotionDetector(
                         (PowerManager) getContext().getSystemService(Context.POWER_SERVICE),
-                        mHandler, mSensorManager, this);
+                        mHandler, mSensorManager, this, angleThreshold);
 
                 Intent intent = new Intent(ACTION_STEP_IDLE_STATE)
                         .setPackage("android")
@@ -1279,17 +1300,30 @@
                 EventLogTags.writeDeviceIdle(mState, "step");
                 cancelSensingAlarmLocked();
                 scheduleSensingAlarmLocked(mConstants.LOCATING_TIMEOUT);
-                mLocating = true;
-                mLocationManager.requestLocationUpdates(mLocationRequest, mGenericLocationListener,
-                        mHandler.getLooper());
-                if (mLocationManager.getProvider(LocationManager.GPS_PROVIDER) != null) {
-                    mHaveGps = true;
+                if (mLocationManager != null
+                        && mLocationManager.getProvider(LocationManager.NETWORK_PROVIDER) != null) {
+                    mLocationManager.requestLocationUpdates(mLocationRequest,
+                            mGenericLocationListener, mHandler.getLooper());
+                    mLocating = true;
+                } else {
+                    mHasNetworkLocation = false;
+                }
+                if (mLocationManager != null
+                        && mLocationManager.getProvider(LocationManager.GPS_PROVIDER) != null) {
+                    mHasGps = true;
                     mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 5,
                             mGpsLocationListener, mHandler.getLooper());
+                    mLocating = true;
                 } else {
-                    mHaveGps = false;
+                    mHasGps = false;
                 }
-                break;
+                // If we have a location provider, we're all set, the listeners will move state
+                // forward.
+                if (mLocating) {
+                    break;
+                }
+
+                // Otherwise, we have to move from locating into idle maintenance.
             case STATE_LOCATING:
                 cancelSensingAlarmLocked();
                 cancelLocatingLocked();
@@ -1346,7 +1380,7 @@
         }
         if (DEBUG) Slog.d(TAG, "Generic location: " + location);
         mLastGenericLocation = new Location(location);
-        if (location.getAccuracy() > mConstants.LOCATION_ACCURACY && mHaveGps) {
+        if (location.getAccuracy() > mConstants.LOCATION_ACCURACY && mHasGps) {
             return;
         }
         mLocated = true;
@@ -1413,9 +1447,9 @@
     void scheduleAlarmLocked(long delay, boolean idleUntil) {
         if (DEBUG) Slog.d(TAG, "scheduleAlarmLocked(" + delay + ", " + idleUntil + ")");
         if (mSigMotionSensor == null) {
-            // If there is no significant motion sensor on this device, then we won't schedule
+            // If there is no motion sensor on this device, then we won't schedule
             // alarms, because we can't determine if the device is not moving.  This effectively
-            // turns off normal exeuction of device idling, although it is still possible to
+            // turns off normal execution of device idling, although it is still possible to
             // manually poke it by pretending like the alarm is going off.
             return;
         }
@@ -1902,8 +1936,9 @@
             pw.print("  mSigMotionActive="); pw.println(mSigMotionActive);
             pw.print("  mSensing="); pw.print(mSensing); pw.print(" mNotMoving=");
                     pw.println(mNotMoving);
-            pw.print("  mLocating="); pw.print(mLocating); pw.print(" mHaveGps=");
-                    pw.print(mHaveGps); pw.print(" mLocated="); pw.println(mLocated);
+            pw.print("  mLocating="); pw.print(mLocating); pw.print(" mHasGps=");
+                    pw.print(mHasGps); pw.print(" mHasNetwork=");
+                    pw.print(mHasNetworkLocation); pw.print(" mLocated="); pw.println(mLocated);
             if (mLastGenericLocation != null) {
                 pw.print("  mLastGenericLocation="); pw.println(mLastGenericLocation);
             }
diff --git a/services/core/java/com/android/server/PersistentDataBlockService.java b/services/core/java/com/android/server/PersistentDataBlockService.java
index 94316fe..e8b90d8 100644
--- a/services/core/java/com/android/server/PersistentDataBlockService.java
+++ b/services/core/java/com/android/server/PersistentDataBlockService.java
@@ -25,6 +25,7 @@
 import android.os.RemoteException;
 import android.os.SystemProperties;
 import android.os.UserHandle;
+import android.os.UserManager;
 import android.service.persistentdata.IPersistentDataBlockService;
 import android.util.Slog;
 
@@ -84,7 +85,7 @@
         mContext = context;
         mDataBlockFile = SystemProperties.get(PERSISTENT_DATA_BLOCK_PROP);
         mBlockDeviceSize = -1; // Load lazily
-        mAllowedUid = getAllowedUid(UserHandle.USER_OWNER);
+        mAllowedUid = getAllowedUid(UserHandle.USER_SYSTEM);
     }
 
     private int getAllowedUid(int userHandle) {
@@ -131,9 +132,12 @@
         }
     }
 
-    private void enforceIsOwner() {
-        if (!Binder.getCallingUserHandle().isOwner()) {
-            throw new SecurityException("Only the Owner is allowed to change OEM unlock state");
+    private void enforceIsAdmin() {
+        final int userId = UserHandle.getCallingUserId();
+        final boolean isAdmin = UserManager.get(mContext).isUserAdmin(userId);
+        if (!isAdmin) {
+            throw new SecurityException(
+                    "Only the Admin user is allowed to change OEM unlock state");
         }
     }
     private int getTotalDataSizeLocked(DataInputStream inputStream) throws IOException {
@@ -434,7 +438,7 @@
                 return;
             }
             enforceOemUnlockPermission();
-            enforceIsOwner();
+            enforceIsAdmin();
 
             synchronized (mLock) {
                 doSetOemUnlockEnabledLocked(enabled);
diff --git a/services/core/java/com/android/server/SystemServiceManager.java b/services/core/java/com/android/server/SystemServiceManager.java
index fda6479..92e6814 100644
--- a/services/core/java/com/android/server/SystemServiceManager.java
+++ b/services/core/java/com/android/server/SystemServiceManager.java
@@ -17,6 +17,7 @@
 package com.android.server;
 
 import android.content.Context;
+import android.os.Trace;
 import android.util.Slog;
 
 import java.lang.reflect.Constructor;
@@ -75,43 +76,48 @@
      */
     @SuppressWarnings("unchecked")
     public <T extends SystemService> T startService(Class<T> serviceClass) {
-        final String name = serviceClass.getName();
-        Slog.i(TAG, "Starting " + name);
-
-        // Create the service.
-        if (!SystemService.class.isAssignableFrom(serviceClass)) {
-            throw new RuntimeException("Failed to create " + name
-                    + ": service must extend " + SystemService.class.getName());
-        }
-        final T service;
         try {
-            Constructor<T> constructor = serviceClass.getConstructor(Context.class);
-            service = constructor.newInstance(mContext);
-        } catch (InstantiationException ex) {
-            throw new RuntimeException("Failed to create service " + name
-                    + ": service could not be instantiated", ex);
-        } catch (IllegalAccessException ex) {
-            throw new RuntimeException("Failed to create service " + name
-                    + ": service must have a public constructor with a Context argument", ex);
-        } catch (NoSuchMethodException ex) {
-            throw new RuntimeException("Failed to create service " + name
-                    + ": service must have a public constructor with a Context argument", ex);
-        } catch (InvocationTargetException ex) {
-            throw new RuntimeException("Failed to create service " + name
-                    + ": service constructor threw an exception", ex);
-        }
+            final String name = serviceClass.getName();
+            Slog.i(TAG, "Starting " + name);
+            Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartService " + name);
 
-        // Register it.
-        mServices.add(service);
+            // Create the service.
+            if (!SystemService.class.isAssignableFrom(serviceClass)) {
+                throw new RuntimeException("Failed to create " + name
+                        + ": service must extend " + SystemService.class.getName());
+            }
+            final T service;
+            try {
+                Constructor<T> constructor = serviceClass.getConstructor(Context.class);
+                service = constructor.newInstance(mContext);
+            } catch (InstantiationException ex) {
+                throw new RuntimeException("Failed to create service " + name
+                        + ": service could not be instantiated", ex);
+            } catch (IllegalAccessException ex) {
+                throw new RuntimeException("Failed to create service " + name
+                        + ": service must have a public constructor with a Context argument", ex);
+            } catch (NoSuchMethodException ex) {
+                throw new RuntimeException("Failed to create service " + name
+                        + ": service must have a public constructor with a Context argument", ex);
+            } catch (InvocationTargetException ex) {
+                throw new RuntimeException("Failed to create service " + name
+                        + ": service constructor threw an exception", ex);
+            }
 
-        // Start it.
-        try {
-            service.onStart();
-        } catch (RuntimeException ex) {
-            throw new RuntimeException("Failed to start service " + name
-                    + ": onStart threw an exception", ex);
+            // Register it.
+            mServices.add(service);
+
+            // Start it.
+            try {
+                service.onStart();
+            } catch (RuntimeException ex) {
+                throw new RuntimeException("Failed to start service " + name
+                        + ": onStart threw an exception", ex);
+            }
+            return service;
+        } finally {
+            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
         }
-        return service;
     }
 
     /**
@@ -127,18 +133,22 @@
         mCurrentPhase = phase;
 
         Slog.i(TAG, "Starting phase " + mCurrentPhase);
-
-        final int serviceLen = mServices.size();
-        for (int i = 0; i < serviceLen; i++) {
-            final SystemService service = mServices.get(i);
-            try {
-                service.onBootPhase(mCurrentPhase);
-            } catch (Exception ex) {
-                throw new RuntimeException("Failed to boot service "
-                        + service.getClass().getName()
-                        + ": onBootPhase threw an exception during phase "
-                        + mCurrentPhase, ex);
+        try {
+            Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "OnBootPhase " + phase);
+            final int serviceLen = mServices.size();
+            for (int i = 0; i < serviceLen; i++) {
+                final SystemService service = mServices.get(i);
+                try {
+                    service.onBootPhase(mCurrentPhase);
+                } catch (Exception ex) {
+                    throw new RuntimeException("Failed to boot service "
+                            + service.getClass().getName()
+                            + ": onBootPhase threw an exception during phase "
+                            + mCurrentPhase, ex);
+                }
             }
+        } finally {
+            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
         }
     }
 
diff --git a/services/core/java/com/android/server/am/ActiveServices.java b/services/core/java/com/android/server/am/ActiveServices.java
index 0119000..4949138 100755
--- a/services/core/java/com/android/server/am/ActiveServices.java
+++ b/services/core/java/com/android/server/am/ActiveServices.java
@@ -2085,7 +2085,8 @@
     }
 
     private boolean collectPackageServicesLocked(String packageName, Set<String> filterByClasses,
-            boolean evenPersistent, boolean doit, ArrayMap<ComponentName, ServiceRecord> services) {
+            boolean evenPersistent, boolean doit, boolean killProcess,
+            ArrayMap<ComponentName, ServiceRecord> services) {
         boolean didSomething = false;
         for (int i = services.size() - 1; i >= 0; i--) {
             ServiceRecord service = services.valueAt(i);
@@ -2101,7 +2102,7 @@
                 didSomething = true;
                 Slog.i(TAG, "  Force stopping service " + service);
                 if (service.app != null) {
-                    service.app.removed = true;
+                    service.app.removed = killProcess;
                     if (!service.app.persistent) {
                         service.app.services.remove(service);
                     }
@@ -2118,7 +2119,7 @@
     }
 
     boolean bringDownDisabledPackageServicesLocked(String packageName, Set<String> filterByClasses,
-            int userId, boolean evenPersistent, boolean doit) {
+            int userId, boolean evenPersistent, boolean killProcess, boolean doit) {
         boolean didSomething = false;
 
         if (mTmpCollectionResults != null) {
@@ -2128,7 +2129,7 @@
         if (userId == UserHandle.USER_ALL) {
             for (int i = mServiceMap.size() - 1; i >= 0; i--) {
                 didSomething |= collectPackageServicesLocked(packageName, filterByClasses,
-                        evenPersistent, doit, mServiceMap.valueAt(i).mServicesByName);
+                        evenPersistent, doit, killProcess, mServiceMap.valueAt(i).mServicesByName);
                 if (!doit && didSomething) {
                     return true;
                 }
@@ -2138,7 +2139,7 @@
             if (smap != null) {
                 ArrayMap<ComponentName, ServiceRecord> items = smap.mServicesByName;
                 didSomething = collectPackageServicesLocked(packageName, filterByClasses,
-                        evenPersistent, doit, items);
+                        evenPersistent, doit, killProcess, items);
             }
         }
 
diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java
index 4aef23b..81936ee 100644
--- a/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -215,6 +215,7 @@
 import android.os.StrictMode;
 import android.os.SystemClock;
 import android.os.SystemProperties;
+import android.os.Trace;
 import android.os.UpdateLock;
 import android.os.UserHandle;
 import android.os.UserManager;
@@ -406,6 +407,17 @@
     private static final int PERSISTENT_MASK =
             ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT;
 
+
+    // Delay to disable app launch boost
+    static final int APP_BOOST_MESSAGE_DELAY = 3000;
+    // Lower delay than APP_BOOST_MESSAGE_DELAY to disable the boost
+    static final int APP_BOOST_TIMEOUT = 2500;
+
+    private static native int nativeMigrateToBoost();
+    private static native int nativeMigrateFromBoost();
+    private boolean mIsBoosted = false;
+    private long mBoostStartTime = 0;
+
     /** All system services */
     SystemServiceManager mSystemServiceManager;
 
@@ -1367,6 +1379,7 @@
     static final int REPORT_TIME_TRACKER_MSG = 55;
     static final int REPORT_USER_SWITCH_COMPLETE_MSG = 56;
     static final int SHUTDOWN_UI_AUTOMATION_CONNECTION_MSG = 57;
+    static final int APP_BOOST_DEACTIVATE_MSG = 58;
 
     static final int FIRST_ACTIVITY_STACK_MSG = 100;
     static final int FIRST_BROADCAST_QUEUE_MSG = 200;
@@ -1885,7 +1898,9 @@
             }
             case FINISH_BOOTING_MSG: {
                 if (msg.arg1 != 0) {
+                    Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "FinishBooting");
                     finishBooting();
+                    Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
                 }
                 if (msg.arg2 != 0) {
                     enableScreenAfterBoot();
@@ -2045,6 +2060,20 @@
                 // it is finished we make sure it is reset to its default.
                 mUserIsMonkey = false;
             } break;
+            case APP_BOOST_DEACTIVATE_MSG : {
+                synchronized(ActivityManagerService.this) {
+                    if (mIsBoosted) {
+                        if (mBoostStartTime < (SystemClock.uptimeMillis() - APP_BOOST_TIMEOUT)) {
+                            nativeMigrateFromBoost();
+                            mIsBoosted = false;
+                            mBoostStartTime = 0;
+                        } else {
+                            Message newmsg = mHandler.obtainMessage(APP_BOOST_DEACTIVATE_MSG);
+                            mHandler.sendMessageDelayed(newmsg, APP_BOOST_TIMEOUT);
+                        }
+                    }
+                }
+            } break;
             }
         }
     };
@@ -3177,6 +3206,16 @@
             app = null;
         }
 
+        // app launch boost for big.little configurations
+        // use cpusets to migrate freshly launched tasks to big cores
+        synchronized(ActivityManagerService.this) {
+            nativeMigrateToBoost();
+            mIsBoosted = true;
+            mBoostStartTime = SystemClock.uptimeMillis();
+            Message msg = mHandler.obtainMessage(APP_BOOST_DEACTIVATE_MSG);
+            mHandler.sendMessageDelayed(msg, APP_BOOST_MESSAGE_DELAY);
+        }
+
         // We don't have to do anything more if:
         // (1) There is an existing application record; and
         // (2) The caller doesn't think it is dead, OR there is no thread
@@ -4186,7 +4225,7 @@
                 throw new IllegalArgumentException("Task " + taskId + " not found.");
             }
             if (task.getRootActivity() != null) {
-                moveTaskToFrontLocked(task.taskId, 0, null);
+                moveTaskToFrontLocked(task.taskId, 0, options);
                 return ActivityManager.START_TASK_TO_FRONT;
             }
             callingUid = task.mCallingUid;
@@ -5608,7 +5647,7 @@
     }
 
     private void cleanupDisabledPackageComponentsLocked(
-            String packageName, int userId, String[] changedClasses) {
+            String packageName, int userId, boolean killProcess, String[] changedClasses) {
 
         Set<String> disabledClasses = null;
         boolean packageDisabled = false;
@@ -5678,7 +5717,7 @@
 
         // Clean-up disabled services.
         mServices.bringDownDisabledPackageServicesLocked(
-                packageName, disabledClasses, userId, false, true);
+                packageName, disabledClasses, userId, false, killProcess, true);
 
         // Clean-up disabled providers.
         ArrayList<ContentProviderRecord> providers = new ArrayList<>();
@@ -5763,7 +5802,7 @@
         }
 
         if (mServices.bringDownDisabledPackageServicesLocked(
-                packageName, null, userId, evenPersistent, doit)) {
+                packageName, null, userId, evenPersistent, true, doit)) {
             if (!doit) {
                 return true;
             }
@@ -6450,7 +6489,9 @@
             mBootAnimationComplete = true;
         }
         if (callFinishBooting) {
+            Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "FinishBooting");
             finishBooting();
+            Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
         }
     }
 
@@ -6465,7 +6506,9 @@
         }
 
         if (booting) {
+            Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "FinishBooting");
             finishBooting();
+            Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
         }
 
         if (enableScreen) {
@@ -16849,7 +16892,9 @@
                                 boolean removed = Intent.ACTION_PACKAGE_REMOVED.equals(action);
                                 boolean fullUninstall = removed &&
                                         !intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
-                                if (!intent.getBooleanExtra(Intent.EXTRA_DONT_KILL_APP, false)) {
+                                final boolean killProcess =
+                                        !intent.getBooleanExtra(Intent.EXTRA_DONT_KILL_APP, false);
+                                if (killProcess) {
                                     forceStopPackageLocked(ssp, UserHandle.getAppId(
                                             intent.getIntExtra(Intent.EXTRA_UID, -1)),
                                             false, true, true, false, fullUninstall, userId,
@@ -16869,7 +16914,7 @@
                                         mBatteryStatsService.notePackageUninstalled(ssp);
                                     }
                                 } else {
-                                    cleanupDisabledPackageComponentsLocked(ssp, userId,
+                                    cleanupDisabledPackageComponentsLocked(ssp, userId, killProcess,
                                             intent.getStringArrayExtra(
                                                     Intent.EXTRA_CHANGED_COMPONENT_NAME_LIST));
                                 }
@@ -17411,8 +17456,10 @@
         }
 
         // Can't call out of the system process with a lock held, so post a message.
-        mHandler.obtainMessage(SHUTDOWN_UI_AUTOMATION_CONNECTION_MSG,
-                app.instrumentationUiAutomationConnection).sendToTarget();
+        if (app.instrumentationUiAutomationConnection != null) {
+            mHandler.obtainMessage(SHUTDOWN_UI_AUTOMATION_CONNECTION_MSG,
+                    app.instrumentationUiAutomationConnection).sendToTarget();
+        }
 
         app.instrumentationWatcher = null;
         app.instrumentationUiAutomationConnection = null;
diff --git a/services/core/java/com/android/server/am/ActivityStack.java b/services/core/java/com/android/server/am/ActivityStack.java
index f50df3a..751acb6 100644
--- a/services/core/java/com/android/server/am/ActivityStack.java
+++ b/services/core/java/com/android/server/am/ActivityStack.java
@@ -18,7 +18,6 @@
 
 import static android.app.ActivityManager.DOCKED_STACK_ID;
 import static android.app.ActivityManager.FREEFORM_WORKSPACE_STACK_ID;
-import static android.app.ActivityManager.FULLSCREEN_WORKSPACE_STACK_ID;
 import static android.app.ActivityManager.HOME_STACK_ID;
 import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
 
@@ -391,7 +390,9 @@
 
     void setBounds(Rect bounds) {
         mBounds = mFullscreen ? null : new Rect(bounds);
-        mTaskPositioner.configure(bounds);
+        if (mTaskPositioner != null) {
+            mTaskPositioner.configure(bounds);
+        }
     }
 
     boolean okToShowLocked(ActivityRecord r) {
@@ -3975,8 +3976,7 @@
      * for whatever reason.  Ensures the HistoryRecord is updated with the
      * correct configuration and all other bookkeeping is handled.
      */
-    final boolean ensureActivityConfigurationLocked(ActivityRecord r,
-            int globalChanges) {
+    final boolean ensureActivityConfigurationLocked(ActivityRecord r, int globalChanges) {
         if (mConfigWillChange) {
             if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
                     "Skipping config check (will change): " + r);
@@ -4549,10 +4549,11 @@
             boolean toTop) {
         TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
                 voiceInteractor);
+        // add the task to stack first, mTaskPositioner might need the stack association
+        addTask(task, toTop, false);
         if (mTaskPositioner != null) {
             mTaskPositioner.updateDefaultBounds(task, mTaskHistory, info.initialLayout);
         }
-        addTask(task, toTop, false);
         return task;
     }
 
@@ -4589,25 +4590,20 @@
 
     void addConfigOverride(ActivityRecord r, TaskRecord task) {
         final Rect bounds = task.getLaunchBounds();
-        final Configuration config =
-                mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
-                        r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
-                        (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId,
-                        r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind,
-                        bounds);
-        if (config != null) {
-            task.updateOverrideConfiguration(config, bounds);
-        }
+        task.updateOverrideConfiguration(bounds);
+        mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
+                r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
+                (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId,
+                r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind,
+                bounds, task.mOverrideConfig);
         r.taskConfigOverride = task.mOverrideConfig;
     }
 
     private void setAppTask(ActivityRecord r, TaskRecord task) {
         final Rect bounds = task.getLaunchBounds();
-        final Configuration config =
-                mWindowManager.setAppTask(r.appToken, task.taskId, task.getLaunchBounds());
-        if (config != null) {
-            task.updateOverrideConfiguration(config, bounds);
-        }
+        task.updateOverrideConfiguration(bounds);
+        mWindowManager.setAppTask(
+                r.appToken, task.taskId, task.getLaunchBounds(), task.mOverrideConfig);
         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 c86056b..b7c55d4 100644
--- a/services/core/java/com/android/server/am/ActivityStackSupervisor.java
+++ b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
@@ -97,7 +97,6 @@
 import android.util.ArrayMap;
 import android.util.ArraySet;
 import android.util.EventLog;
-import android.util.IntArray;
 import android.util.Slog;
 import android.util.SparseArray;
 
@@ -334,6 +333,9 @@
     // temp. rect used during resize calculation so we don't need to create a new object each time.
     private final Rect tempRect = new Rect();
 
+    private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
+    private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
+
     /**
      * Description of a request to start a new activity, which has been held
      * due to app switches being disabled.
@@ -1762,7 +1764,7 @@
         return ACTIVITY_RESTRICTION_NONE;
     }
 
-    ActivityStack computeStackFocus(ActivityRecord r, boolean newTask) {
+    ActivityStack computeStackFocus(ActivityRecord r, boolean newTask, Rect bounds) {
         final TaskRecord task = r.task;
 
         // On leanback only devices we should keep all activities in the same stack.
@@ -1813,10 +1815,10 @@
             }
 
             // If there is no suitable dynamic stack then we figure out which static stack to use.
-            stack = getStack(
-                    task != null
-                            ? task.getLaunchStackId(mFocusedStack) : FULLSCREEN_WORKSPACE_STACK_ID,
-                    CREATE_IF_NEEDED, ON_TOP);
+            int stackId = task != null ? task.getLaunchStackId() :
+                        bounds != null ? FREEFORM_WORKSPACE_STACK_ID :
+                                         FULLSCREEN_WORKSPACE_STACK_ID;
+            stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
             if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
                     + r + " stackId=" + stack.mStackId);
             return stack;
@@ -1844,6 +1846,22 @@
         final Intent intent = r.intent;
         final int callingUid = r.launchedFromUid;
 
+        boolean overrideBounds = false;
+        Rect newBounds = null;
+        if (r.info.resizeable || (inTask != null && inTask.mResizeable)) {
+            if (intent.hasExtra(ActivityOptions.KEY_BOUNDS)) {
+                overrideBounds = true;
+                newBounds = Rect.unflattenFromString(
+                        intent.getStringExtra(ActivityOptions.KEY_BOUNDS));
+            } else if (options != null) {
+                ActivityOptions opts = new ActivityOptions(options);
+                if (opts.hasBounds()) {
+                    overrideBounds = true;
+                    newBounds = opts.getBounds();
+                }
+            }
+        }
+
         // In some flows in to this function, we retrieve the task record and hold on to it
         // without a lock before calling back in to here...  so the task at this point may
         // not actually be in recents.  Check for that, and if it isn't in recents just
@@ -2197,7 +2215,8 @@
                             if (task != null && task.stack == null) {
                                 // Target stack got cleared when we all activities were removed
                                 // above. Go ahead and reset it.
-                                targetStack = computeStackFocus(sourceRecord, false /* newTask */);
+                                targetStack = computeStackFocus(
+                                        sourceRecord, false /* newTask */, null /* bounds */);
                                 targetStack.addTask(
                                         task, !launchTaskBehind /* toTop */, false /* moving */);
                             }
@@ -2323,7 +2342,7 @@
         if (r.resultTo == null && inTask == null && !addingToTask
                 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
             newTask = true;
-            targetStack = computeStackFocus(r, newTask);
+            targetStack = computeStackFocus(r, newTask, newBounds);
             if (doResume) {
                 targetStack.moveToFront("startingNewTask");
             }
@@ -2334,6 +2353,9 @@
                         newTaskIntent != null ? newTaskIntent : intent,
                         voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
                         taskToAffiliate);
+                if (overrideBounds) {
+                    r.task.updateOverrideConfiguration(newBounds);
+                }
                 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
                         "Starting new activity " + r + " in new task " + r.task);
             } else {
@@ -2418,6 +2440,14 @@
                 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
                 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
             }
+            if (overrideBounds) {
+                inTask.updateOverrideConfiguration(newBounds);
+                int stackId = inTask.getLaunchStackId();
+                if (stackId != inTask.stack.mStackId) {
+                    moveTaskToStackUncheckedLocked(
+                            inTask, stackId, ON_TOP, !FORCE_FOCUS, "inTaskToFront");
+                }
+            }
             targetStack = inTask.stack;
             targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
                     "inTaskToFront");
@@ -2455,7 +2485,7 @@
             // This not being started from an existing activity, and not part
             // of a new task...  just put it in the top task, though these days
             // this case should never happen.
-            targetStack = computeStackFocus(r, newTask);
+            targetStack = computeStackFocus(r, newTask, null /* bounds */);
             if (doResume) {
                 targetStack.moveToFront("addingToTopTask");
             }
@@ -2814,9 +2844,26 @@
                     + task + " to front. Stack is null");
             return;
         }
-        task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
+
+        int stackId = task.stack.mStackId;
+        if (task.mResizeable && options != null) {
+            ActivityOptions opts = new ActivityOptions(options);
+            if (opts.hasBounds()) {
+                Rect bounds = opts.getBounds();
+                task.updateOverrideConfiguration(bounds);
+                mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, false);
+                stackId = task.getLaunchStackId();
+            }
+        }
+
+        if (stackId != task.stack.mStackId) {
+            moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, FORCE_FOCUS, reason);
+        } else {
+            task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
                 task.getTopActivity() == null ? null : task.getTopActivity().appTimeTracker,
                 reason);
+        }
+
         if (DEBUG_STACK) Slog.d(TAG_STACK,
                 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
     }
@@ -2917,19 +2964,19 @@
         ActivityRecord r = stack.topRunningActivityLocked(null);
         final boolean resizeTasks = r != null && r.task.mResizeable;
 
-        final IntArray changedTaskIds = new IntArray(stack.numTasks());
-        final List<Configuration> newTaskConfigs = new ArrayList<>(stack.numTasks());
-        stack.mFullscreen = mWindowManager.resizeStack(
-                stackId, bounds, resizeTasks, changedTaskIds, newTaskConfigs);
-        for (int i = changedTaskIds.size() - 1; i >= 0; i--) {
-            final TaskRecord task = anyTaskForIdLocked(changedTaskIds.get(i), false);
-            if (task == null) {
-                Slog.wtf(TAG, "Task in WindowManager, but not in ActivityManager???");
-                continue;
+        mTmpBounds.clear();
+        mTmpConfigs.clear();
+        if (resizeTasks) {
+            ArrayList<TaskRecord> tasks = stack.getAllTasks();
+            for (int i = tasks.size() - 1; i >= 0; i--) {
+                TaskRecord task = tasks.get(i);
+                task.updateOverrideConfiguration(bounds);
+                mTmpConfigs.put(task.taskId, task.mOverrideConfig);
+                mTmpBounds.put(task.taskId, task.mBounds);
             }
-            task.updateOverrideConfiguration(newTaskConfigs.get(i), bounds);
         }
-
+        stack.mFullscreen = mWindowManager.resizeStack(stackId, bounds, resizeTasks, mTmpConfigs,
+                mTmpBounds);
         if (stack.mStackId == DOCKED_STACK_ID) {
             // Dock stack funness...Yay!
             if (stack.mFullscreen) {
@@ -3005,7 +3052,7 @@
             // Task doesn't exist in window manager yet (e.g. was restored from recents).
             // All we can do for now is update the bounds so it can be used when the task is
             // added to window manager.
-            task.mBounds = task.mLastNonFullscreenBounds = new Rect(bounds);
+            task.updateOverrideConfiguration(bounds);
             if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
                 // re-restore the task so it can have the proper stack association.
                 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
@@ -3024,25 +3071,33 @@
             stackId = FREEFORM_WORKSPACE_STACK_ID;
         }
         if (stackId != task.stack.mStackId) {
-            final String reason = "resizeTask";
-            final ActivityStack stack =
-                    moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, !FORCE_FOCUS, reason);
+            moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, !FORCE_FOCUS, "resizeTask");
         }
 
-        final Configuration overrideConfig = mWindowManager.resizeTask(task.taskId, bounds);
-        if (task.updateOverrideConfiguration(overrideConfig, bounds)) {
+        final Configuration overrideConfig =  task.updateOverrideConfiguration(bounds);
+        // This variable holds information whether the configuration didn't change in a signficant
+        // way and the activity was kept the way it was. If it's false, it means the activity had
+        // to be relaunched due to configuration change.
+        boolean kept = true;
+        if (overrideConfig != null) {
             ActivityRecord r = task.topRunningActivityLocked(null);
             if (r != null) {
                 final ActivityStack stack = task.stack;
-                final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
-                // And we need to make sure at this point that all other activities
-                // are made visible with the correct configuration.
+                kept = stack.ensureActivityConfigurationLocked(r, 0);
+                // All other activities must be made visible with their correct configuration.
                 ensureActivitiesVisibleLocked(r, 0);
-                if (!updated) {
+                if (!kept) {
                     resumeTopActivitiesLocked(stack, null, null);
+                    // We are about to relaunch the activity because its configuration changed due
+                    // to size change. The activity will first remove the old window and then add a
+                    // new one. This call will tell window manager about this, so it can preserve
+                    // the old window until the new one is drawn. This prevents having a gap between
+                    // the removal and addition, in which no window is visible.
+                    mWindowManager.setReplacingWindow(r.appToken);
                 }
             }
         }
+        mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept);
     }
 
     ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
@@ -3077,8 +3132,7 @@
      */
     private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
         if (stackId == INVALID_STACK_ID) {
-            stackId = mLeanbackOnlyDevice ?
-                    mHomeStack.mStackId : task.getLaunchStackId(mFocusedStack);
+            stackId = mLeanbackOnlyDevice ? mHomeStack.mStackId : task.getLaunchStackId();
         }
         if (task.stack != null) {
             // Task has already been restored once. See if we need to do anything more
diff --git a/services/core/java/com/android/server/am/BroadcastQueue.java b/services/core/java/com/android/server/am/BroadcastQueue.java
index a956c56..960cbf1 100644
--- a/services/core/java/com/android/server/am/BroadcastQueue.java
+++ b/services/core/java/com/android/server/am/BroadcastQueue.java
@@ -509,7 +509,7 @@
                     break;
                 }
                 int appOp = AppOpsManager.permissionToOpCode(requiredPermission);
-                if (appOp != r.appOp
+                if (appOp != AppOpsManager.OP_NONE && appOp != r.appOp
                         && mService.mAppOpsService.noteOperation(appOp,
                         filter.receiverList.uid, filter.packageName)
                         != AppOpsManager.MODE_ALLOWED) {
diff --git a/services/core/java/com/android/server/am/LaunchingTaskPositioner.java b/services/core/java/com/android/server/am/LaunchingTaskPositioner.java
index 5c4fd13..3d45915 100644
--- a/services/core/java/com/android/server/am/LaunchingTaskPositioner.java
+++ b/services/core/java/com/android/server/am/LaunchingTaskPositioner.java
@@ -234,7 +234,7 @@
                 break;
             }
         }
-        task.setInitialBounds(proposal);
+        task.updateOverrideConfiguration(proposal);
     }
 
     private boolean shiftedToFar(Rect start, int shiftPolicy) {
diff --git a/services/core/java/com/android/server/am/TaskRecord.java b/services/core/java/com/android/server/am/TaskRecord.java
index 9cbaec5..5694e704 100644
--- a/services/core/java/com/android/server/am/TaskRecord.java
+++ b/services/core/java/com/android/server/am/TaskRecord.java
@@ -52,6 +52,7 @@
 import android.os.RemoteException;
 import android.os.UserHandle;
 import android.service.voice.IVoiceInteractionSession;
+import android.util.DisplayMetrics;
 import android.util.Slog;
 import com.android.internal.app.IVoiceInteractor;
 import com.android.internal.util.XmlUtils;
@@ -63,6 +64,7 @@
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.util.ArrayList;
+import java.util.Objects;
 
 final class TaskRecord {
     private static final String TAG = TAG_WITH_CLASS_NAME ? "TaskRecord" : TAG_AM;
@@ -106,6 +108,13 @@
 
     static final int INVALID_TASK_ID = -1;
 
+    // The height/width divide used when fitting a task within a bounds with method
+    // {@link #fitWithinBounds}.
+    // We always want the task to to be visible in the bounds without affecting its size when
+    // fitting. To make sure this is the case, we don't adjust the task left or top side pass
+    // the input bounds right or bottom side minus the width or height divided by this value.
+    private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
+
     final int taskId;       // Unique identifier for this task.
     String affinity;        // The affinity name for this task, or null; may change identity.
     String rootAffinity;    // Initial base affinity, or null; does not change from initial root.
@@ -271,8 +280,7 @@
             long _firstActiveTime, long _lastActiveTime, long lastTimeMoved,
             boolean neverRelinquishIdentity, TaskDescription _lastTaskDescription,
             int taskAffiliation, int prevTaskId, int nextTaskId, int taskAffiliationColor,
-            int callingUid, String callingPackage, boolean resizeable, boolean privileged,
-            Rect bounds) {
+            int callingUid, String callingPackage, boolean resizeable, boolean privileged) {
         mService = service;
         mFilename = String.valueOf(_taskId) + TASK_THUMBNAIL_SUFFIX +
                 TaskPersister.IMAGE_EXTENSION;
@@ -309,7 +317,6 @@
         mCallingPackage = callingPackage;
         mResizeable = resizeable;
         mPrivileged = privileged;
-        mBounds = mLastNonFullscreenBounds = bounds;
     }
 
     void touchActiveTime() {
@@ -1163,7 +1170,8 @@
                 autoRemoveRecents, askedCompatMode, taskType, userId, effectiveUid, lastDescription,
                 activities, firstActiveTime, lastActiveTime, lastTimeOnTop, neverRelinquishIdentity,
                 taskDescription, taskAffiliation, prevTaskId, nextTaskId, taskAffiliationColor,
-                callingUid, callingPackage, resizeable, privileged, bounds);
+                callingUid, callingPackage, resizeable, privileged);
+        task.updateOverrideConfiguration(bounds);
 
         for (int activityNdx = activities.size() - 1; activityNdx >=0; --activityNdx) {
             activities.get(activityNdx).task = task;
@@ -1173,41 +1181,63 @@
         return task;
     }
 
-    boolean updateOverrideConfiguration(Configuration newConfig, Rect bounds) {
+    /**
+     * Update task's override configuration based on the bounds.
+     * @return Update configuration or null if there is no change.
+     */
+    Configuration updateOverrideConfiguration(Rect bounds) {
+        if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
+            // For freeform stack we don't adjust the size of the tasks to match that of the
+            // stack, but we do try to make sure the tasks are still contained with the
+            // bounds of the stack.
+            fitWithinBounds(bounds, stack.mBounds);
+        }
+        if (Objects.equals(mBounds, bounds)) {
+            return null;
+        }
         Configuration oldConfig = mOverrideConfig;
-        mOverrideConfig = (newConfig == null) ? Configuration.EMPTY : newConfig;
-        // We override the configuration only when the task's dimensions are different from the
-        // display. In this manner, we know that if the override configuration is empty, the task
-        // is necessarily fullscreen.
-        mFullscreen = Configuration.EMPTY.equals(mOverrideConfig);
+
+        mFullscreen = bounds == null;
         if (mFullscreen) {
             if (mBounds != null && stack.mStackId != DOCKED_STACK_ID) {
                 mLastNonFullscreenBounds = mBounds;
             }
             mBounds = null;
+            mOverrideConfig = Configuration.EMPTY;
         } else {
             mBounds = new Rect(bounds);
             if (stack.mStackId != DOCKED_STACK_ID) {
                 mLastNonFullscreenBounds = mBounds;
             }
+
+            final Configuration serviceConfig = mService.mConfiguration;
+            mOverrideConfig = new Configuration(serviceConfig);
+            // TODO(multidisplay): Update Dp to that of display stack is on.
+            final float density = serviceConfig.densityDpi * DisplayMetrics.DENSITY_DEFAULT_SCALE;
+            mOverrideConfig.screenWidthDp =
+                    Math.min((int)(mBounds.width() / density), serviceConfig.screenWidthDp);
+            mOverrideConfig.screenHeightDp =
+                    Math.min((int)(mBounds.height() / density), serviceConfig.screenHeightDp);
+            mOverrideConfig.smallestScreenWidthDp =
+                    Math.min(mOverrideConfig.screenWidthDp, mOverrideConfig.screenHeightDp);
+            mOverrideConfig.orientation =
+                    (mOverrideConfig.screenWidthDp <= mOverrideConfig.screenHeightDp)
+                            ? Configuration.ORIENTATION_PORTRAIT
+                            : Configuration.ORIENTATION_LANDSCAPE;
         }
-        return !mOverrideConfig.equals(oldConfig);
+        return !mOverrideConfig.equals(oldConfig) ? mOverrideConfig : null;
     }
 
-    /** Returns the stack that should be used to launch this task. */
-    int getLaunchStackId(ActivityStack focusStack) {
-        if (stack != null) {
-            // We are already in a stack silly...
-            return stack.mStackId;
-        }
-        if (isHomeTask()) {
+    /**
+     * Returns the correct stack to use based on task type and currently set bounds,
+     * regardless of the focused stack and current stack association of the task.
+     * The task will be moved (and stack focus changed) later if necessary.
+     */
+    int getLaunchStackId() {
+        if (!isApplicationTask()) {
             return HOME_STACK_ID;
         }
-        if (focusStack != null && focusStack.mStackId != HOME_STACK_ID) {
-            // Like it or not you are going in the focused stack!
-            return focusStack.mStackId;
-        }
-        if (mBounds != null || mLastNonFullscreenBounds != null) {
+        if (mBounds != null) {
             return FREEFORM_WORKSPACE_STACK_ID;
         }
         return FULLSCREEN_WORKSPACE_STACK_ID;
@@ -1225,12 +1255,43 @@
         return mLastNonFullscreenBounds;
     }
 
-    void setInitialBounds(Rect rect) {
-        if (mBounds == null) {
-            mBounds = new Rect();
+    /**
+     * Adjust bounds to stay within stack bounds.
+     *
+     * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
+     * that keep them unchanged, but be contained within the stack bounds.
+     *
+     * @param bounds Bounds to be adjusted.
+     * @param stackBounds Bounds within which the other bounds should remain.
+     */
+    private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
+        if (stackBounds == null || stackBounds.contains(bounds)) {
+            return;
         }
-        mBounds.set(rect);
-        mLastNonFullscreenBounds = mBounds;
+
+        if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
+            final int maxRight = stackBounds.right
+                    - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
+            int horizontalDiff = stackBounds.left - bounds.left;
+            if ((horizontalDiff < 0 && bounds.left >= maxRight)
+                    || (bounds.left + horizontalDiff >= maxRight)) {
+                horizontalDiff = maxRight - bounds.left;
+            }
+            bounds.left += horizontalDiff;
+            bounds.right += horizontalDiff;
+        }
+
+        if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
+            final int maxBottom = stackBounds.bottom
+                    - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
+            int verticalDiff = stackBounds.top - bounds.top;
+            if ((verticalDiff < 0 && bounds.top >= maxBottom)
+                    || (bounds.top + verticalDiff >= maxBottom)) {
+                verticalDiff = maxBottom - bounds.top;
+            }
+            bounds.top += verticalDiff;
+            bounds.bottom += verticalDiff;
+        }
     }
 
     void dump(PrintWriter pw, String prefix) {
diff --git a/services/core/java/com/android/server/fingerprint/FingerprintService.java b/services/core/java/com/android/server/fingerprint/FingerprintService.java
index 7e46db8..0023258 100644
--- a/services/core/java/com/android/server/fingerprint/FingerprintService.java
+++ b/services/core/java/com/android/server/fingerprint/FingerprintService.java
@@ -18,13 +18,18 @@
 
 import android.Manifest;
 import android.app.ActivityManager;
+import android.app.ActivityManager.RunningAppProcessInfo;
+import android.app.ActivityManager.RunningTaskInfo;
 import android.app.ActivityManagerNative;
 import android.app.AppOpsManager;
 import android.app.IUserSwitchObserver;
+import android.content.ComponentName;
 import android.content.Context;
 import android.content.pm.PackageManager;
 import android.content.pm.UserInfo;
+import android.hardware.fingerprint.IFingerprintServiceLockoutResetCallback;
 import android.os.Binder;
+import android.os.DeadObjectException;
 import android.os.Environment;
 import android.os.Handler;
 import android.os.IBinder;
@@ -51,8 +56,8 @@
 import android.hardware.fingerprint.IFingerprintDaemon;
 import android.hardware.fingerprint.IFingerprintDaemonCallback;
 import android.hardware.fingerprint.IFingerprintServiceReceiver;
-import android.view.Display;
 
+import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
 import static android.Manifest.permission.MANAGE_FINGERPRINT;
 import static android.Manifest.permission.RESET_FINGERPRINT_LOCKOUT;
 import static android.Manifest.permission.USE_FINGERPRINT;
@@ -60,6 +65,7 @@
 import java.io.File;
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
@@ -83,12 +89,15 @@
     private ClientMonitor mAuthClient = null;
     private ClientMonitor mEnrollClient = null;
     private ClientMonitor mRemoveClient = null;
+    private final ArrayList<FingerprintServiceLockoutResetMonitor> mLockoutMonitors =
+            new ArrayList<>();
     private final AppOpsManager mAppOps;
 
     private static final long MS_PER_SEC = 1000;
     private static final long FAIL_LOCKOUT_TIMEOUT_MS = 30*1000;
     private static final int MAX_FAILED_ATTEMPTS = 5;
     private static final int FINGERPRINT_ACQUIRED_GOOD = 0;
+    private final String mKeyguardPackage;
 
     Handler mHandler = new Handler() {
         @Override
@@ -121,6 +130,8 @@
     public FingerprintService(Context context) {
         super(context);
         mContext = context;
+        mKeyguardPackage = ComponentName.unflattenFromString(context.getResources().getString(
+                com.android.internal.R.string.config_keyguardComponent)).getPackageName();
         mAppOps = context.getSystemService(AppOpsManager.class);
         mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
     }
@@ -248,7 +259,7 @@
     }
 
     private boolean inLockoutMode() {
-        return mFailedAttempts > MAX_FAILED_ATTEMPTS;
+        return mFailedAttempts >= MAX_FAILED_ATTEMPTS;
     }
 
     private void resetFailedAttempts() {
@@ -259,11 +270,12 @@
         // If we're asked to reset failed attempts externally (i.e. from Keyguard), the runnable
         // may still be in the queue; remove it.
         mHandler.removeCallbacks(mLockoutReset);
+        notifyLockoutResetMonitors();
     }
 
     private boolean handleFailedAttempt(ClientMonitor clientMonitor) {
         mFailedAttempts++;
-        if (mFailedAttempts > MAX_FAILED_ATTEMPTS) {
+        if (inLockoutMode()) {
             // Failing multiple times will continue to push out the lockout time.
             mHandler.removeCallbacks(mLockoutReset);
             mHandler.postDelayed(mLockoutReset, FAIL_LOCKOUT_TIMEOUT_MS);
@@ -493,10 +505,67 @@
         return false;
     }
 
-    private boolean canUseFingerprint(String opPackageName) {
+    private boolean isForegroundActivity(int uid, int pid) {
+        try {
+            List<RunningAppProcessInfo> procs =
+                    ActivityManagerNative.getDefault().getRunningAppProcesses();
+            int N = procs.size();
+            for (int i = 0; i < N; i++) {
+                RunningAppProcessInfo proc = procs.get(i);
+                if (proc.pid == pid && proc.uid == uid
+                        && proc.importance == IMPORTANCE_FOREGROUND) {
+                    return true;
+                }
+            }
+        } catch (RemoteException e) {
+            Slog.w(TAG, "am.getRunningAppProcesses() failed");
+        }
+        return false;
+    }
+
+    /**
+     * @param opPackageName name of package for caller
+     * @param foregroundOnly 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) {
         checkPermission(USE_FINGERPRINT);
-        return mAppOps.noteOp(AppOpsManager.OP_USE_FINGERPRINT, Binder.getCallingUid(),
-                opPackageName) == AppOpsManager.MODE_ALLOWED;
+        final int uid = Binder.getCallingUid();
+        final int pid = Binder.getCallingPid();
+        if (opPackageName.equals(mKeyguardPackage)) {
+            return true; // Keyguard is always allowed
+        }
+        if (!isCurrentUserOrProfile(UserHandle.getCallingUserId())) {
+            Slog.w(TAG,"Rejecting " + opPackageName + " ; not a current user or profile");
+            return false;
+        }
+        if (mAppOps.noteOp(AppOpsManager.OP_USE_FINGERPRINT, uid, opPackageName)
+                != AppOpsManager.MODE_ALLOWED) {
+            Slog.v(TAG, "Rejecting " + opPackageName + " ; permission denied");
+            return false;
+        }
+        if (foregroundOnly && !isForegroundActivity(uid, pid)) {
+            Slog.v(TAG, "Rejecting " + opPackageName + " ; not in foreground");
+            return false;
+        }
+        return true;
+    }
+
+    private void addLockoutResetMonitor(FingerprintServiceLockoutResetMonitor monitor) {
+        if (!mLockoutMonitors.contains(monitor)) {
+            mLockoutMonitors.add(monitor);
+        }
+    }
+
+    private void removeLockoutResetCallback(
+            FingerprintServiceLockoutResetMonitor monitor) {
+        mLockoutMonitors.remove(monitor);
+    }
+
+    private void notifyLockoutResetMonitors() {
+        for (int i = 0; i < mLockoutMonitors.size(); i++) {
+            mLockoutMonitors.get(i).sendLockoutReset();
+        }
     }
 
     private class ClientMonitor implements IBinder.DeathRecipient {
@@ -614,7 +683,7 @@
                     FingerprintUtils.vibrateFingerprintSuccess(getContext());
                 }
                 result |= true; // we have a valid fingerprint
-                mLockoutReset.run();
+                mHandler.post(mLockoutReset);
             }
             return result;
         }
@@ -654,6 +723,36 @@
         }
     }
 
+    private class FingerprintServiceLockoutResetMonitor {
+
+        private final IFingerprintServiceLockoutResetCallback mCallback;
+
+        public FingerprintServiceLockoutResetMonitor(
+                IFingerprintServiceLockoutResetCallback callback) {
+            mCallback = callback;
+        }
+
+        public void sendLockoutReset() {
+            if (mCallback != null) {
+                try {
+                    mCallback.onLockoutReset(mHalDeviceId);
+                } catch (DeadObjectException e) {
+                    Slog.w(TAG, "Death object while invoking onLockoutReset: ", e);
+                    mHandler.post(mRemoveCallbackRunnable);
+                } catch (RemoteException e) {
+                    Slog.w(TAG, "Failed to invoke onLockoutReset: ", e);
+                }
+            }
+        }
+
+        private final Runnable mRemoveCallbackRunnable = new Runnable() {
+            @Override
+            public void run() {
+                removeLockoutResetCallback(FingerprintServiceLockoutResetMonitor.this);
+            }
+        };
+    }
+
     private IFingerprintDaemonCallback mDaemonCallback = new IFingerprintDaemonCallback.Stub() {
 
         @Override
@@ -782,12 +881,7 @@
         public void authenticate(final IBinder token, final long opId, final int groupId,
                 final IFingerprintServiceReceiver receiver, final int flags,
                 final String opPackageName) {
-            if (!isCurrentUserOrProfile(UserHandle.getCallingUserId())) {
-                Slog.w(TAG, "Can't authenticate non-current user");
-                return;
-            }
-            if (!canUseFingerprint(opPackageName)) {
-                Slog.w(TAG, "Calling not granted permission to use fingerprint");
+            if (!canUseFingerprint(opPackageName, true /* foregroundOnly */)) {
                 return;
             }
 
@@ -807,7 +901,7 @@
 
         @Override // Binder call
         public void cancelAuthentication(final IBinder token, String opPackageName) {
-            if (!canUseFingerprint(opPackageName)) {
+            if (!canUseFingerprint(opPackageName, false /* foregroundOnly */)) {
                 return;
             }
             mHandler.post(new Runnable() {
@@ -838,7 +932,7 @@
 
         @Override // Binder call
         public boolean isHardwareDetected(long deviceId, String opPackageName) {
-            if (!canUseFingerprint(opPackageName)) {
+            if (!canUseFingerprint(opPackageName, false /* foregroundOnly */)) {
                 return false;
             }
             return mHalDeviceId != 0;
@@ -862,7 +956,7 @@
 
         @Override // Binder call
         public List<Fingerprint> getEnrolledFingerprints(int userId, String opPackageName) {
-            if (!canUseFingerprint(opPackageName)) {
+            if (!canUseFingerprint(opPackageName, false /* foregroundOnly */)) {
                 return Collections.emptyList();
             }
             int effectiveUserId = getEffectiveUserId(userId);
@@ -872,7 +966,7 @@
 
         @Override // Binder call
         public boolean hasEnrolledFingerprints(int userId, String opPackageName) {
-            if (!canUseFingerprint(opPackageName)) {
+            if (!canUseFingerprint(opPackageName, false /* foregroundOnly */)) {
                 return false;
             }
 
@@ -922,7 +1016,19 @@
         public void resetTimeout(byte [] token) {
             checkPermission(RESET_FINGERPRINT_LOCKOUT);
             // TODO: confirm security token when we move timeout management into the HAL layer.
-            mLockoutReset.run();
+            mHandler.post(mLockoutReset);
+        }
+
+        @Override
+        public void addLockoutResetCallback(final IFingerprintServiceLockoutResetCallback callback)
+                throws RemoteException {
+            mHandler.post(new Runnable() {
+                @Override
+                public void run() {
+                    addLockoutResetMonitor(
+                            new FingerprintServiceLockoutResetMonitor(callback));
+                }
+            });
         }
     }
 
diff --git a/services/core/java/com/android/server/input/InputManagerService.java b/services/core/java/com/android/server/input/InputManagerService.java
index 17b4f9c..5a13672 100644
--- a/services/core/java/com/android/server/input/InputManagerService.java
+++ b/services/core/java/com/android/server/input/InputManagerService.java
@@ -17,6 +17,7 @@
 package com.android.server.input;
 
 import android.view.Display;
+import com.android.internal.os.SomeArgs;
 import com.android.internal.R;
 import com.android.internal.util.XmlUtils;
 import com.android.server.DisplayThread;
@@ -52,6 +53,7 @@
 import android.hardware.input.InputDeviceIdentifier;
 import android.hardware.input.InputManager;
 import android.hardware.input.InputManagerInternal;
+import android.hardware.input.ITabletModeChangedListener;
 import android.hardware.input.KeyboardLayout;
 import android.hardware.input.TouchCalibration;
 import android.os.Binder;
@@ -93,6 +95,7 @@
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.List;
 
 import libcore.io.Streams;
 import libcore.util.Objects;
@@ -112,6 +115,7 @@
     private static final int MSG_RELOAD_KEYBOARD_LAYOUTS = 3;
     private static final int MSG_UPDATE_KEYBOARD_LAYOUTS = 4;
     private static final int MSG_RELOAD_DEVICE_ALIASES = 5;
+    private static final int MSG_DELIVER_TABLET_MODE_CHANGED = 6;
 
     // Pointer to native input manager service object.
     private final long mPtr;
@@ -124,6 +128,13 @@
     private boolean mSystemReady;
     private NotificationManager mNotificationManager;
 
+    private final Object mTabletModeLock = new Object();
+    // List of currently registered tablet mode changed listeners by process id
+    private final SparseArray<TabletModeChangedListenerRecord> mTabletModeChangedListeners =
+            new SparseArray<>(); // guarded by mTabletModeLock
+    private final List<TabletModeChangedListenerRecord> mTempTabletModeChangedListenersToNotify =
+            new ArrayList<>();
+
     // Persistent data store.  Must be locked each time during use.
     private final PersistentDataStore mDataStore = new PersistentDataStore();
 
@@ -227,6 +238,11 @@
     /** Switch code: Lid switch.  When set, lid is shut. */
     public static final int SW_LID = 0x00;
 
+    /** Switch code: Tablet mode switch.
+     * When set, the device is in tablet mode (i.e. no keyboard is connected).
+     */
+    public static final int SW_TABLET_MODE = 0x01;
+
     /** Switch code: Keypad slide.  When set, keyboard is exposed. */
     public static final int SW_KEYPAD_SLIDE = 0x0a;
 
@@ -246,6 +262,7 @@
     public static final int SW_CAMERA_LENS_COVER = 0x09;
 
     public static final int SW_LID_BIT = 1 << SW_LID;
+    public static final int SW_TABLET_MODE_BIT = 1 << SW_TABLET_MODE;
     public static final int SW_KEYPAD_SLIDE_BIT = 1 << SW_KEYPAD_SLIDE;
     public static final int SW_HEADPHONE_INSERT_BIT = 1 << SW_HEADPHONE_INSERT;
     public static final int SW_MICROPHONE_INSERT_BIT = 1 << SW_MICROPHONE_INSERT;
@@ -774,6 +791,57 @@
         }
     }
 
+    @Override // Binder call
+    public void registerTabletModeChangedListener(ITabletModeChangedListener listener) {
+        if (!checkCallingPermission(android.Manifest.permission.TABLET_MODE_LISTENER,
+                "registerTabletModeChangedListener()")) {
+            throw new SecurityException("Requires TABLET_MODE_LISTENER permission");
+        }
+        if (listener == null) {
+            throw new IllegalArgumentException("listener must not be null");
+        }
+
+        synchronized (mTabletModeLock) {
+            final int callingPid = Binder.getCallingPid();
+            if (mTabletModeChangedListeners.get(callingPid) != null) {
+                throw new IllegalStateException("The calling process has already registered "
+                        + "a TabletModeChangedListener.");
+            }
+            TabletModeChangedListenerRecord record =
+                    new TabletModeChangedListenerRecord(callingPid, listener);
+            try {
+                IBinder binder = listener.asBinder();
+                binder.linkToDeath(record, 0);
+            } catch (RemoteException ex) {
+                throw new RuntimeException(ex);
+            }
+            mTabletModeChangedListeners.put(callingPid, record);
+        }
+    }
+
+    private void onTabletModeChangedListenerDied(int pid) {
+        synchronized (mTabletModeLock) {
+            mTabletModeChangedListeners.remove(pid);
+        }
+    }
+
+    // Must be called on handler
+    private void deliverTabletModeChanged(long whenNanos, boolean inTabletMode) {
+        mTempTabletModeChangedListenersToNotify.clear();
+        final int numListeners;
+        synchronized (mTabletModeLock) {
+            numListeners = mTabletModeChangedListeners.size();
+            for (int i = 0; i < numListeners; i++) {
+                mTempTabletModeChangedListenersToNotify.add(
+                        mTabletModeChangedListeners.valueAt(i));
+            }
+        }
+        for (int i = 0; i < numListeners; i++) {
+            mTempTabletModeChangedListenersToNotify.get(i).notifyTabletModeChanged(
+                    whenNanos, inTabletMode);
+        }
+    }
+
     // Must be called on handler.
     private void showMissingKeyboardLayoutNotification(InputDevice device) {
         if (!mKeyboardLayoutNotificationShown) {
@@ -1419,6 +1487,15 @@
             mWiredAccessoryCallbacks.notifyWiredAccessoryChanged(whenNanos, switchValues,
                     switchMask);
         }
+
+        if ((switchMask & SW_TABLET_MODE) != 0) {
+            SomeArgs args = SomeArgs.obtain();
+            args.argi1 = (int) (whenNanos & 0xFFFFFFFF);
+            args.argi2 = (int) (whenNanos >> 32);
+            args.arg1 = Boolean.valueOf((switchValues & SW_TABLET_MODE_BIT) != 0);
+            mHandler.obtainMessage(MSG_DELIVER_TABLET_MODE_CHANGED,
+                    args).sendToTarget();
+        }
     }
 
     // Native callback.
@@ -1664,6 +1741,12 @@
                 case MSG_RELOAD_DEVICE_ALIASES:
                     reloadDeviceAliases();
                     break;
+                case MSG_DELIVER_TABLET_MODE_CHANGED:
+                    SomeArgs args = (SomeArgs) msg.obj;
+                    long whenNanos = (args.argi1 & 0xFFFFFFFFl) | ((long) args.argi2 << 32);
+                    boolean inTabletMode = (boolean) args.arg1;
+                    deliverTabletModeChanged(whenNanos, inTabletMode);
+                    break;
             }
         }
     }
@@ -1755,6 +1838,34 @@
         }
     }
 
+    private final class TabletModeChangedListenerRecord implements DeathRecipient {
+        private final int mPid;
+        private final ITabletModeChangedListener mListener;
+
+        public TabletModeChangedListenerRecord(int pid, ITabletModeChangedListener listener) {
+            mPid = pid;
+            mListener = listener;
+        }
+
+        @Override
+        public void binderDied() {
+            if (DEBUG) {
+                Slog.d(TAG, "Tablet mode changed listener for pid " + mPid + " died.");
+            }
+            onTabletModeChangedListenerDied(mPid);
+        }
+
+        public void notifyTabletModeChanged(long whenNanos, boolean inTabletMode) {
+            try {
+                mListener.onTabletModeChanged(whenNanos, inTabletMode);
+            } catch (RemoteException ex) {
+                Slog.w(TAG, "Failed to notify process " + mPid +
+                        " that tablet mode changed, assuming it died.", ex);
+                binderDied();
+            }
+        }
+    }
+
     private final class VibratorToken implements DeathRecipient {
         public final int mDeviceId;
         public final IBinder mToken;
diff --git a/services/core/java/com/android/server/location/GpsLocationProvider.java b/services/core/java/com/android/server/location/GpsLocationProvider.java
index 5c60a61..5e5a55c 100644
--- a/services/core/java/com/android/server/location/GpsLocationProvider.java
+++ b/services/core/java/com/android/server/location/GpsLocationProvider.java
@@ -434,8 +434,11 @@
     private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
         @Override public void onReceive(Context context, Intent intent) {
             String action = intent.getAction();
-
             if (DEBUG) Log.d(TAG, "receive broadcast intent, action: " + action);
+            if (action == null) {
+                return;
+            }
+
             if (action.equals(ALARM_WAKEUP)) {
                 startNavigating(false);
             } else if (action.equals(ALARM_TIMEOUT)) {
@@ -444,7 +447,8 @@
                 checkSmsSuplInit(intent);
             } else if (action.equals(Intents.WAP_PUSH_RECEIVED_ACTION)) {
                 checkWapSuplInit(intent);
-            } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
+            } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)
+                    || action.equals(ConnectivityManager.CONNECTIVITY_ACTION_SUPL)) {
                 // retrieve NetworkType result for this UID
                 int networkType = intent.getIntExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, -1);
                 if (DEBUG) Log.d(TAG, "Connectivity action, type=" + networkType);
@@ -490,21 +494,27 @@
 
     private void checkSmsSuplInit(Intent intent) {
         SmsMessage[] messages = Intents.getMessagesFromIntent(intent);
-
         if (messages == null) {
             Log.e(TAG, "Message does not exist in the intent.");
             return;
         }
 
-        for (int i=0; i <messages.length; i++) {
-            byte[] supl_init = messages[i].getUserData();
-            native_agps_ni_message(supl_init,supl_init.length);
+        for (SmsMessage message : messages) {
+            if (message != null && message.mWrappedSmsMessage != null) {
+                byte[] suplInit = message.getUserData();
+                if (suplInit != null) {
+                    native_agps_ni_message(suplInit, suplInit.length);
+                }
+            }
         }
     }
 
     private void checkWapSuplInit(Intent intent) {
-        byte[] supl_init = (byte[]) intent.getExtra("data");
-        native_agps_ni_message(supl_init,supl_init.length);
+        byte[] suplInit = intent.getByteArrayExtra("data");
+        if (suplInit == null) {
+            return;
+        }
+        native_agps_ni_message(suplInit,suplInit.length);
     }
 
     private void updateLowPowerMode() {
@@ -2058,6 +2068,7 @@
             intentFilter.addAction(ALARM_WAKEUP);
             intentFilter.addAction(ALARM_TIMEOUT);
             intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
+            intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION_SUPL);
             intentFilter.addAction(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED);
             intentFilter.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
             intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
diff --git a/services/core/java/com/android/server/net/NetworkPolicyManagerService.java b/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
index e69dda1..3991489 100644
--- a/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
+++ b/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
@@ -978,13 +978,12 @@
         }
 
         // TODO: move to NotificationManager once we can mock it
-        // XXX what to do about multi-user?
         try {
             final String packageName = mContext.getPackageName();
             final int[] idReceived = new int[1];
             mNotifManager.enqueueNotificationWithTag(
                     packageName, packageName, tag, 0x0, builder.getNotification(), idReceived,
-                    UserHandle.USER_OWNER);
+                    UserHandle.USER_ALL);
             mActiveNotifs.add(tag);
         } catch (RemoteException e) {
             // ignored; service lives in system_server
@@ -1016,12 +1015,11 @@
                 PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
 
         // TODO: move to NotificationManager once we can mock it
-        // XXX what to do about multi-user?
         try {
             final String packageName = mContext.getPackageName();
             final int[] idReceived = new int[1];
             mNotifManager.enqueueNotificationWithTag(packageName, packageName, tag,
-                    0x0, builder.getNotification(), idReceived, UserHandle.USER_OWNER);
+                    0x0, builder.getNotification(), idReceived, UserHandle.USER_ALL);
             mActiveNotifs.add(tag);
         } catch (RemoteException e) {
             // ignored; service lives in system_server
@@ -1030,11 +1028,10 @@
 
     private void cancelNotification(String tag) {
         // TODO: move to NotificationManager once we can mock it
-        // XXX what to do about multi-user?
         try {
             final String packageName = mContext.getPackageName();
             mNotifManager.cancelNotificationWithTag(
-                    packageName, tag, 0x0, UserHandle.USER_OWNER);
+                    packageName, tag, 0x0, UserHandle.USER_ALL);
         } catch (RemoteException e) {
             // ignored; service lives in system_server
         }
@@ -1418,7 +1415,8 @@
                         final int policy = readIntAttribute(in, ATTR_POLICY);
 
                         // TODO: set for other users during upgrade
-                        final int uid = UserHandle.getUid(UserHandle.USER_OWNER, appId);
+                        // 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);
                         } else {
diff --git a/services/core/java/com/android/server/notification/ZenModeHelper.java b/services/core/java/com/android/server/notification/ZenModeHelper.java
index 0a12d5a..cbe61c3 100644
--- a/services/core/java/com/android/server/notification/ZenModeHelper.java
+++ b/services/core/java/com/android/server/notification/ZenModeHelper.java
@@ -46,7 +46,6 @@
 import android.service.notification.ZenModeConfig.EventInfo;
 import android.service.notification.ZenModeConfig.ScheduleInfo;
 import android.service.notification.ZenModeConfig.ZenRule;
-import android.util.ArraySet;
 import android.util.Log;
 import android.util.SparseArray;
 
@@ -149,6 +148,7 @@
             mAudioManager.setRingerModeDelegate(mRingerModeDelegate);
         }
         mHandler.postMetricsTimer();
+        evaluateZenMode("onSystemReady", true);
     }
 
     public void onUserSwitched(int user) {
@@ -330,13 +330,14 @@
         }
         mConditions.evaluateConfig(config, false /*processSubscriptions*/);  // may modify config
         mConfigs.put(config.user, config);
-        if (config.equals(mConfig)) return true;
         if (DEBUG) Log.d(TAG, "setConfig reason=" + reason, new Throwable());
         ZenLog.traceConfig(reason, mConfig, config);
         final boolean policyChanged = !Objects.equals(getNotificationPolicy(mConfig),
                 getNotificationPolicy(config));
         mConfig = config;
-        dispatchOnConfigChanged();
+        if (config.equals(mConfig)) {
+            dispatchOnConfigChanged();
+        }
         if (policyChanged){
             dispatchOnPolicyChanged();
         }
@@ -370,9 +371,7 @@
 
     private boolean evaluateZenMode(String reason, boolean setRingerMode) {
         if (DEBUG) Log.d(TAG, "evaluateZenMode");
-        final ArraySet<ZenRule> automaticRules = new ArraySet<ZenRule>();
-        final int zen = computeZenMode(automaticRules);
-        if (zen == mZenMode) return false;
+        final int zen = computeZenMode();
         ZenLog.traceSetZenMode(zen, reason);
         mZenMode = zen;
         updateRingerModeAffectedStreams();
@@ -381,7 +380,9 @@
             applyZenToRingerMode();
         }
         applyRestrictions();
-        mHandler.postDispatchOnZenModeChanged();
+        if (zen != mZenMode) {
+            mHandler.postDispatchOnZenModeChanged();
+        }
         return true;
     }
 
@@ -391,7 +392,7 @@
         }
     }
 
-    private int computeZenMode(ArraySet<ZenRule> automaticRulesOut) {
+    private int computeZenMode() {
         if (mConfig == null) return Global.ZEN_MODE_OFF;
         if (mConfig.manualRule != null) return mConfig.manualRule.zenMode;
         int zen = Global.ZEN_MODE_OFF;
diff --git a/services/core/java/com/android/server/pm/PackageManagerService.java b/services/core/java/com/android/server/pm/PackageManagerService.java
index e1f3528..82afdd7 100644
--- a/services/core/java/com/android/server/pm/PackageManagerService.java
+++ b/services/core/java/com/android/server/pm/PackageManagerService.java
@@ -2319,16 +2319,16 @@
             }
             updatePermissionsLPw(null, null, updateFlags);
             ver.sdkVersion = mSdkVersion;
-            // clear only after permissions have been updated
-            mExistingSystemPackages.clear();
-            mPromoteSystemApps = false;
 
-            // If this is the first boot, and it is a normal boot, then
-            // we need to initialize the default preferred apps.
-            if (!mRestoredSettings && !onlyCore) {
-                mSettings.applyDefaultPreferredAppsLPw(this, UserHandle.USER_OWNER);
-                applyFactoryDefaultBrowserLPw(UserHandle.USER_OWNER);
-                primeDomainVerificationsLPw(UserHandle.USER_OWNER);
+            // If this is the first boot or an update from pre-M, and it is a normal
+            // boot, then we need to initialize the default preferred apps across
+            // all defined users.
+            if (!onlyCore && (mPromoteSystemApps || !mRestoredSettings)) {
+                for (UserInfo user : sUserManager.getUsers(true)) {
+                    mSettings.applyDefaultPreferredAppsLPw(this, user.id);
+                    applyFactoryDefaultBrowserLPw(user.id);
+                    primeDomainVerificationsLPw(user.id);
+                }
             }
 
             // If this is first boot after an OTA, and a normal boot, then
@@ -2346,6 +2346,10 @@
 
             checkDefaultBrowser();
 
+            // clear only after permissions and other defaults have been updated
+            mExistingSystemPackages.clear();
+            mPromoteSystemApps = false;
+
             // All the changes are done during package scanning.
             ver.databaseVersion = Settings.CURRENT_DATABASE_VERSION;
 
@@ -5995,12 +5999,6 @@
          */
         if (shouldHideSystemApp) {
             synchronized (mPackages) {
-                /*
-                 * We have to grant systems permissions before we hide, because
-                 * grantPermissions will assume the package update is trying to
-                 * expand its permissions.
-                 */
-                grantPermissionsLPw(pkg, true, pkg.packageName);
                 mSettings.disableSystemPackageLPw(pkg.packageName);
             }
         }
diff --git a/services/core/java/com/android/server/pm/SELinuxMMAC.java b/services/core/java/com/android/server/pm/SELinuxMMAC.java
index c75a1d3..66170d4 100644
--- a/services/core/java/com/android/server/pm/SELinuxMMAC.java
+++ b/services/core/java/com/android/server/pm/SELinuxMMAC.java
@@ -136,9 +136,6 @@
                     case "signer":
                         policies.add(readSignerOrThrow(parser));
                         break;
-                    case "default":
-                        policies.add(readDefaultOrThrow(parser));
-                        break;
                     default:
                         skip(parser);
                 }
@@ -233,45 +230,6 @@
     }
 
     /**
-     * Loop over a default element looking for seinfo child tags. A {@link Policy}
-     * instance will be created and returned in the process. All other tags encountered
-     * will be skipped.
-     *
-     * @param parser an XmlPullParser object representing a default element.
-     * @return the constructed {@link Policy} instance
-     * @throws IOException
-     * @throws XmlPullParserException
-     * @throws IllegalArgumentException if any of the validation checks fail while
-     *         parsing tag values.
-     * @throws IllegalStateException if any of the invariants fail when constructing
-     *         the {@link Policy} instance.
-     */
-    private static Policy readDefaultOrThrow(XmlPullParser parser) throws IOException,
-            XmlPullParserException {
-
-        parser.require(XmlPullParser.START_TAG, null, "default");
-        Policy.PolicyBuilder pb = new Policy.PolicyBuilder();
-        pb.setAsDefaultPolicy();
-
-        while (parser.next() != XmlPullParser.END_TAG) {
-            if (parser.getEventType() != XmlPullParser.START_TAG) {
-                continue;
-            }
-
-            String tagName = parser.getName();
-            if ("seinfo".equals(tagName)) {
-                String seinfo = parser.getAttributeValue(null, "value");
-                pb.setGlobalSeinfoOrThrow(seinfo);
-                readSeinfo(parser);
-            } else {
-                skip(parser);
-            }
-        }
-
-        return pb.build();
-    }
-
-    /**
      * Loop over a package element looking for seinfo child tags. If found return the
      * value attribute of the seinfo tag, otherwise return null. All other tags encountered
      * will be skipped.
@@ -337,35 +295,28 @@
 
     /**
      * Applies a security label to a package based on an seinfo tag taken from a matched
-     * policy. All signature based policy stanzas are consulted first and, if no match
-     * is found, the default policy stanza is then consulted. The security label is
-     * attached to the ApplicationInfo instance of the package in the event that a matching
-     * policy was found.
+     * policy. All signature based policy stanzas are consulted and, if no match is
+     * found, the default seinfo label of 'default' (set in ApplicationInfo object) is
+     * used. The security label is attached to the ApplicationInfo instance of the package
+     * in the event that a matching policy was found.
      *
      * @param pkg object representing the package to be labeled.
-     * @return boolean which determines whether a non null seinfo label was assigned
-     *         to the package. A null value simply represents that no policy matched.
      */
-    public static boolean assignSeinfoValue(PackageParser.Package pkg) {
+    public static void assignSeinfoValue(PackageParser.Package pkg) {
         synchronized (sPolicies) {
             for (Policy policy : sPolicies) {
                 String seinfo = policy.getMatchedSeinfo(pkg);
                 if (seinfo != null) {
                     pkg.applicationInfo.seinfo = seinfo;
-                    if (DEBUG_POLICY_INSTALL) {
-                        Slog.i(TAG, "package (" + pkg.packageName + ") labeled with " +
-                               "seinfo=" + seinfo);
-                    }
-                    return true;
+                    break;
                 }
             }
         }
 
         if (DEBUG_POLICY_INSTALL) {
-            Slog.i(TAG, "package (" + pkg.packageName + ") doesn't match any policy; " +
-                   "seinfo will remain null");
+            Slog.i(TAG, "package (" + pkg.packageName + ") labeled with " +
+                    "seinfo=" + pkg.applicationInfo.seinfo);
         }
-        return false;
     }
 
     /**
@@ -506,30 +457,16 @@
  *         .build();
  * }
  * </pre>
- * <p>
- * The following is an example of how to use {@link Policy.PolicyBuilder} to create a
- * default based Policy instance.
- * </p>
- * <pre>
- * {@code
- * Policy policy = new Policy.PolicyBuilder()
- *         .setAsDefaultPolicy()
- *         .setGlobalSeinfoOrThrow("default")
- *         .build();
- * }
- * </pre>
  */
 final class Policy {
 
     private final String mSeinfo;
-    private final boolean mDefaultStanza;
     private final Set<Signature> mCerts;
     private final Map<String, String> mPkgMap;
 
     // Use the PolicyBuilder pattern to instantiate
     private Policy(PolicyBuilder builder) {
         mSeinfo = builder.mSeinfo;
-        mDefaultStanza = builder.mDefaultStanza;
         mCerts = Collections.unmodifiableSet(builder.mCerts);
         mPkgMap = Collections.unmodifiableMap(builder.mPkgMap);
     }
@@ -545,15 +482,6 @@
     }
 
     /**
-     * Return whether this policy object represents a default stanza.
-     *
-     * @return A boolean indicating if this object represents a default policy stanza.
-     */
-    public boolean isDefaultStanza() {
-        return mDefaultStanza;
-    }
-
-    /**
      * Return whether this policy object contains package name mapping refinements.
      *
      * @return A boolean indicating if this object has inner package name mappings.
@@ -584,10 +512,6 @@
     @Override
     public String toString() {
         StringBuilder sb = new StringBuilder();
-        if (mDefaultStanza) {
-            sb.append("defaultStanza=true ");
-        }
-
         for (Signature cert : mCerts) {
             sb.append("cert=" + cert.toCharsString().substring(0, 11) + "... ");
         }
@@ -609,22 +533,15 @@
      * is determined using the following steps:
      * </p>
      * <ul>
-     *   <li> If this Policy instance is defined as a default stanza:
-     *       <ul><li>Return the global seinfo value</li></ul>
+     *   <li> All certs used to sign the apk and all certs stored with this policy
+     *     instance are tested for set equality. If this fails then null is returned.
      *   </li>
-     *   <li> If this Policy instance is defined as a signer stanza:
-     *     <ul>
-     *       <li> All certs used to sign the apk and all certs stored with this policy
-     *         instance are tested for set equality. If this fails then null is returned.
-     *       </li>
-     *       <li> If all certs match then an appropriate inner package stanza is
-     *         searched based on package name alone. If matched, the stored seinfo
-     *         value for that mapping is returned.
-     *       </li>
-     *       <li> If all certs matched and no inner package stanza matches then return
-     *         the global seinfo value. The returned value can be null in this case.
-     *       </li>
-     *     </ul>
+     *   <li> If all certs match then an appropriate inner package stanza is
+     *     searched based on package name alone. If matched, the stored seinfo
+     *     value for that mapping is returned.
+     *   </li>
+     *   <li> If all certs matched and no inner package stanza matches then return
+     *     the global seinfo value. The returned value can be null in this case.
      *   </li>
      * </ul>
      * <p>
@@ -636,37 +553,34 @@
      *         A value of null can also be returned if no match occured.
      */
     public String getMatchedSeinfo(PackageParser.Package pkg) {
-        if (!mDefaultStanza) {
-            // Check for exact signature matches across all certs.
-            Signature[] certs = mCerts.toArray(new Signature[0]);
-            if (!Signature.areExactMatch(certs, pkg.mSignatures)) {
-                return null;
-            }
-
-            // Check for inner package name matches given that the
-            // signature checks already passed.
-            String seinfoValue = mPkgMap.get(pkg.packageName);
-            if (seinfoValue != null) {
-                return seinfoValue;
-            }
+        // Check for exact signature matches across all certs.
+        Signature[] certs = mCerts.toArray(new Signature[0]);
+        if (!Signature.areExactMatch(certs, pkg.mSignatures)) {
+            return null;
         }
 
-        // Return the global seinfo value (even if it's null).
+        // Check for inner package name matches given that the
+        // signature checks already passed.
+        String seinfoValue = mPkgMap.get(pkg.packageName);
+        if (seinfoValue != null) {
+            return seinfoValue;
+        }
+
+        // Return the global seinfo value.
         return mSeinfo;
     }
 
     /**
      * A nested builder class to create {@link Policy} instances. A {@link Policy}
      * class instance represents one valid policy stanza found in a mac_permissions.xml
-     * file. A valid policy stanza is defined to be either a signer or default stanza
-     * which obeys the rules outlined in external/sepolicy/mac_permissions.xml. The
-     * {@link #build} method ensures a set of invariants are upheld enforcing the correct
-     * stanza structure before returning a valid Policy object.
+     * file. A valid policy stanza is defined to be a signer stanza which obeys the rules
+     * outlined in external/sepolicy/mac_permissions.xml. The {@link #build} method
+     * ensures a set of invariants are upheld enforcing the correct stanza structure
+     * before returning a valid Policy object.
      */
     public static final class PolicyBuilder {
 
         private String mSeinfo;
-        private boolean mDefaultStanza;
         private final Set<Signature> mCerts;
         private final Map<String, String> mPkgMap;
 
@@ -676,19 +590,6 @@
         }
 
         /**
-         * Sets this stanza as a default stanza. All policy stanzas are assumed to
-         * be signer stanzas unless this method is explicitly called. Default stanzas
-         * are treated differently with respect to allowable child tags, ordering and
-         * when and how policy decisions are enforced.
-         *
-         * @return The reference to this PolicyBuilder.
-         */
-        public PolicyBuilder setAsDefaultPolicy() {
-            mDefaultStanza = true;
-            return this;
-        }
-
-        /**
          * Adds a signature to the set of certs used for validation checks. The purpose
          * being that all contained certs will need to be matched against all certs
          * contained with an apk.
@@ -710,11 +611,8 @@
 
         /**
          * Set the global seinfo tag for this policy stanza. The global seinfo tag
-         * represents the seinfo element that is used in one of two ways depending on
-         * its context. When attached to a signer tag the global seinfo represents an
-         * assignment when there isn't a further inner package refinement in policy.
-         * When used with a default tag, it represents the only allowable assignment
-         * value.
+         * when attached to a signer tag represents the assignment when there isn't a
+         * further inner package refinement in policy.
          *
          * @param seinfo the seinfo value given as a String.
          * @return The reference to this PolicyBuilder.
@@ -740,9 +638,7 @@
         /**
          * Create a package name to seinfo value mapping. Each mapping represents
          * the seinfo value that will be assigned to the described package name.
-         * These localized mappings allow the global seinfo to be overriden. This
-         * mapping provides no value when used in conjunction with a default stanza;
-         * enforced through the {@link #build} method.
+         * These localized mappings allow the global seinfo to be overriden.
          *
          * @param pkgName the android package name given to the app
          * @param seinfo the seinfo value that will be assigned to the passed pkgName
@@ -799,51 +695,25 @@
          * about the expected structure of a policy stanza.
          * Those invariants are:
          * </p>
-         *    <ul>
-         *      <li> If a default stanza
-         *        <ul>
-         *          <li> an attached global seinfo tag must be present </li>
-         *          <li> no signatures and no package names can be present </li>
-         *        </ul>
-         *      </li>
-         *      <li> If a signer stanza
-         *        <ul>
-         *           <li> at least one cert must be found </li>
-         *           <li> either a global seinfo value is present OR at least one
-         *           inner package mapping must be present BUT not both. </li>
-         *        </ul>
-         *      </li>
-         *    </ul>
-         *
+         * <ul>
+         *   <li> at least one cert must be found </li>
+         *   <li> either a global seinfo value is present OR at least one
+         *     inner package mapping must be present BUT not both. </li>
+         * </ul>
          * @return an instance of {@link Policy} with the options set from this builder
          * @throws IllegalStateException if an invariant is violated.
          */
         public Policy build() {
             Policy p = new Policy(this);
 
-            if (p.mDefaultStanza) {
-                if (p.mSeinfo == null) {
-                    String err = "Missing global seinfo tag with default stanza.";
-                    throw new IllegalStateException(err);
-                }
-                if (p.mCerts.size() != 0) {
-                    String err = "Certs not allowed with default stanza.";
-                    throw new IllegalStateException(err);
-                }
-                if (!p.mPkgMap.isEmpty()) {
-                    String err = "Inner package mappings not allowed with default stanza.";
-                    throw new IllegalStateException(err);
-                }
-            } else {
-                if (p.mCerts.size() == 0) {
-                    String err = "Missing certs with signer tag. Expecting at least one.";
-                    throw new IllegalStateException(err);
-                }
-                if (!(p.mSeinfo == null ^ p.mPkgMap.isEmpty())) {
-                    String err = "Only seinfo tag XOR package tags are allowed within " +
-                            "a signer stanza.";
-                    throw new IllegalStateException(err);
-                }
+            if (p.mCerts.isEmpty()) {
+                String err = "Missing certs with signer tag. Expecting at least one.";
+                throw new IllegalStateException(err);
+            }
+            if (!(p.mSeinfo == null ^ p.mPkgMap.isEmpty())) {
+                String err = "Only seinfo tag XOR package tags are allowed within " +
+                        "a signer stanza.";
+                throw new IllegalStateException(err);
             }
 
             return p;
@@ -858,7 +728,6 @@
  * <ul>
  *   <li> signer stanzas with inner package mappings </li>
  *   <li> signer stanzas with global seinfo tags </li>
- *   <li> default stanza </li>
  * </ul>
  * This comparison also checks for duplicate entries on the input selectors. Any
  * found duplicates will be flagged and can be checked with {@link #foundDuplicate}.
@@ -875,11 +744,6 @@
     @Override
     public int compare(Policy p1, Policy p2) {
 
-        // Give precedence to signature stanzas over default stanzas
-        if (p1.isDefaultStanza() != p2.isDefaultStanza()) {
-            return p1.isDefaultStanza() ? 1 : -1;
-        }
-
         // Give precedence to stanzas with inner package mappings
         if (p1.hasInnerPackages() != p2.hasInnerPackages()) {
             return p1.hasInnerPackages() ? -1 : 1;
@@ -887,7 +751,7 @@
 
         // Check for duplicate entries
         if (p1.getSignatures().equals(p2.getSignatures())) {
-            // Checks if default stanza or a signer w/o inner package names
+            // Checks if signer w/o inner package names
             if (p1.hasGlobalSeinfo()) {
                 duplicateFound = true;
                 Slog.e(SELinuxMMAC.TAG, "Duplicate policy entry: " + p1.toString());
diff --git a/services/core/java/com/android/server/policy/PhoneWindowManager.java b/services/core/java/com/android/server/policy/PhoneWindowManager.java
index 9ddbcca..4d67702 100644
--- a/services/core/java/com/android/server/policy/PhoneWindowManager.java
+++ b/services/core/java/com/android/server/policy/PhoneWindowManager.java
@@ -65,6 +65,7 @@
 import android.os.Message;
 import android.os.Messenger;
 import android.os.PowerManager;
+import android.os.PowerManagerInternal;
 import android.os.Process;
 import android.os.RemoteException;
 import android.os.ServiceManager;
@@ -265,6 +266,7 @@
     WindowManagerFuncs mWindowManagerFuncs;
     WindowManagerInternal mWindowManagerInternal;
     PowerManager mPowerManager;
+    PowerManagerInternal mPowerManagerInternal;
     ActivityManagerInternal mActivityManagerInternal;
     DreamManagerInternal mDreamManagerInternal;
     IStatusBarService mStatusBarService;
@@ -1335,6 +1337,7 @@
         mActivityManagerInternal = LocalServices.getService(ActivityManagerInternal.class);
         mDreamManagerInternal = LocalServices.getService(DreamManagerInternal.class);
         mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
+        mPowerManagerInternal = LocalServices.getService(PowerManagerInternal.class);
 
         // Init display burn-in protection
         boolean burnInProtectionEnabled = context.getResources().getBoolean(
@@ -4462,7 +4465,7 @@
                     if (mAppsToBeHidden.isEmpty()) {
                         if (dismissKeyguard && !mKeyguardSecure) {
                             mAppsThatDismissKeyguard.add(appToken);
-                        } else if (win.isDrawnLw()) {
+                        } else if (win.isDrawnLw() || win.hasAppShownWindows()) {
                             mWinShowWhenLocked = win;
                             mHideLockScreen = true;
                             mForceStatusBarFromKeyguard = false;
@@ -4496,7 +4499,8 @@
                         mWinDismissingKeyguard = win;
                         mSecureDismissingKeyguard = mKeyguardSecure;
                         mForceStatusBarFromKeyguard = mShowingLockscreen && mKeyguardSecure;
-                    } else if (mAppsToBeHidden.isEmpty() && showWhenLocked && win.isDrawnLw()) {
+                    } else if (mAppsToBeHidden.isEmpty() && showWhenLocked
+                            && (win.isDrawnLw() || win.hasAppShownWindows())) {
                         if (DEBUG_LAYOUT) Slog.v(TAG,
                                 "Setting mHideLockScreen to true by win " + win);
                         mHideLockScreen = true;
@@ -5124,6 +5128,15 @@
                 break;
             }
 
+            case KeyEvent.KEYCODE_SOFT_SLEEP: {
+                result &= ~ACTION_PASS_TO_USER;
+                isWakeKey = false;
+                if (!down) {
+                    mPowerManagerInternal.setUserInactiveOverrideFromWindowManager();
+                }
+                break;
+            }
+
             case KeyEvent.KEYCODE_WAKEUP: {
                 result &= ~ACTION_PASS_TO_USER;
                 isWakeKey = true;
@@ -6632,6 +6645,11 @@
         if (mForcingShowNavBar && win.getSurfaceLayer() < mForcingShowNavBarLayer) {
             tmpVisibility &= ~PolicyControl.adjustClearableFlags(win, View.SYSTEM_UI_CLEARABLE_FLAGS);
         }
+
+        if (mUiMode == Configuration.UI_MODE_TYPE_CAR) {
+            tmpVisibility |= StatusBarManager.DISABLE_RECENT;
+        }
+
         tmpVisibility = updateLightStatusBarLw(tmpVisibility);
         final int visibility = updateSystemBarsLw(win, mLastSystemUiFlags, tmpVisibility);
         final int diff = visibility ^ mLastSystemUiFlags;
diff --git a/services/core/java/com/android/server/power/PowerManagerService.java b/services/core/java/com/android/server/power/PowerManagerService.java
index 7ebb7f8..4e702aa 100644
--- a/services/core/java/com/android/server/power/PowerManagerService.java
+++ b/services/core/java/com/android/server/power/PowerManagerService.java
@@ -393,6 +393,10 @@
     // Use -1 to disable.
     private int mScreenBrightnessOverrideFromWindowManager = -1;
 
+    // The window manager has determined the user to be inactive via other means.
+    // Set this to false to disable.
+    private boolean mUserInactiveOverrideFromWindowManager;
+
     // The user activity timeout override from the window manager
     // to allow the current foreground activity to override the user activity timeout.
     // Use -1 to disable.
@@ -1028,6 +1032,10 @@
 
             mNotifier.onUserActivity(event, uid);
 
+            if (mUserInactiveOverrideFromWindowManager) {
+                mUserInactiveOverrideFromWindowManager = false;
+            }
+
             if (mWakefulness == WAKEFULNESS_ASLEEP
                     || mWakefulness == WAKEFULNESS_DOZING
                     || (flags & PowerManager.USER_ACTIVITY_FLAG_INDIRECT) != 0) {
@@ -1525,6 +1533,7 @@
                 final int sleepTimeout = getSleepTimeoutLocked();
                 final int screenOffTimeout = getScreenOffTimeoutLocked(sleepTimeout);
                 final int screenDimDuration = getScreenDimDurationLocked(screenOffTimeout);
+                final boolean userInactiveOverride = mUserInactiveOverrideFromWindowManager;
 
                 mUserActivitySummary = 0;
                 if (mLastUserActivityTime >= mLastWakeTime) {
@@ -1550,6 +1559,7 @@
                         }
                     }
                 }
+
                 if (mUserActivitySummary == 0) {
                     if (sleepTimeout >= 0) {
                         final long anyUserActivity = Math.max(mLastUserActivityTime,
@@ -1565,6 +1575,12 @@
                         nextTimeout = -1;
                     }
                 }
+
+                if (mUserActivitySummary != USER_ACTIVITY_SCREEN_DREAM && userInactiveOverride) {
+                    mUserActivitySummary = USER_ACTIVITY_SCREEN_DREAM;
+                    nextTimeout = -1;
+                }
+
                 if (mUserActivitySummary != 0 && nextTimeout >= 0) {
                     Message msg = mHandler.obtainMessage(MSG_USER_ACTIVITY_TIMEOUT);
                     msg.setAsynchronous(true);
@@ -2494,6 +2510,14 @@
         }
     }
 
+    private void setUserInactiveOverrideFromWindowManagerInternal() {
+        synchronized (mLock) {
+            mUserInactiveOverrideFromWindowManager = true;
+            mDirty |= DIRTY_USER_ACTIVITY;
+            updatePowerStateLocked();
+        }
+    }
+
     private void setUserActivityTimeoutOverrideFromWindowManagerInternal(long timeoutMillis) {
         synchronized (mLock) {
             if (mUserActivityTimeoutOverrideFromWindowManager != timeoutMillis) {
@@ -2688,6 +2712,8 @@
                     + mScreenBrightnessOverrideFromWindowManager);
             pw.println("  mUserActivityTimeoutOverrideFromWindowManager="
                     + mUserActivityTimeoutOverrideFromWindowManager);
+            pw.println("  mUserInactiveOverrideFromWindowManager="
+                    + mUserInactiveOverrideFromWindowManager);
             pw.println("  mTemporaryScreenBrightnessSettingOverride="
                     + mTemporaryScreenBrightnessSettingOverride);
             pw.println("  mTemporaryScreenAutoBrightnessAdjustmentSettingOverride="
@@ -3492,6 +3518,11 @@
         }
 
         @Override
+        public void setUserInactiveOverrideFromWindowManager() {
+            setUserInactiveOverrideFromWindowManagerInternal();
+        }
+
+        @Override
         public void setUserActivityTimeoutOverrideFromWindowManager(long timeoutMillis) {
             setUserActivityTimeoutOverrideFromWindowManagerInternal(timeoutMillis);
         }
diff --git a/services/core/java/com/android/server/wm/AppWindowToken.java b/services/core/java/com/android/server/wm/AppWindowToken.java
index a210223..6ee2c39 100644
--- a/services/core/java/com/android/server/wm/AppWindowToken.java
+++ b/services/core/java/com/android/server/wm/AppWindowToken.java
@@ -112,6 +112,11 @@
     boolean mLaunchTaskBehind;
     boolean mEnteringAnimation;
 
+    // This application will have its window replaced due to relaunch. This allows window manager
+    // to differentiate between simple removal of a window and replacement. In the latter case it
+    // will preserve the old window until the new one is drawn.
+    boolean mReplacingWindow;
+
     AppWindowToken(WindowManagerService _service, IApplicationToken _token,
             boolean _voiceInteraction) {
         super(_service, _token.asBinder(),
diff --git a/services/core/java/com/android/server/wm/Task.java b/services/core/java/com/android/server/wm/Task.java
index 554af28..666d902 100644
--- a/services/core/java/com/android/server/wm/Task.java
+++ b/services/core/java/com/android/server/wm/Task.java
@@ -23,7 +23,6 @@
 
 import android.content.res.Configuration;
 import android.graphics.Rect;
-import android.util.DisplayMetrics;
 import android.util.EventLog;
 import android.util.Slog;
 import android.util.SparseArray;
@@ -41,13 +40,6 @@
      * when no window animation is driving it. */
     private static final int DEFAULT_DIM_DURATION = 200;
 
-    // The amount we divide the height/width of the bounds we are trying to fit the task within
-    // when using the method {@link #fitWithinBounds}.
-    // We always want the task to to be visible in the bounds without affecting its size when
-    // fitting. To make sure this is the case, we don't adjust the task left or top side pass
-    // the input bounds right or bottom side minus the width or height divided by this value.
-    private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
-
     TaskStack mStack;
     final AppTokenList mAppTokens = new AppTokenList();
     final int mTaskId;
@@ -84,13 +76,13 @@
     // of creating a new object per fullscreen task on a display.
     private static final SparseArray<DimLayer> sSharedFullscreenDimLayers = new SparseArray<>();
 
-    Task(int taskId, TaskStack stack, int userId, WindowManagerService service, Rect bounds) {
+    Task(int taskId, TaskStack stack, int userId, WindowManagerService service, Rect bounds,
+            Configuration config) {
         mTaskId = taskId;
         mStack = stack;
         mUserId = userId;
         mService = service;
-        mOverrideConfig = Configuration.EMPTY;
-        setBounds(bounds);
+        setBounds(bounds, config);
     }
 
     DisplayContent getDisplayContent() {
@@ -172,43 +164,18 @@
         }
     }
 
-    /** Fits the tasks within the input bounds adjusting the task bounds as needed.
-     *  @param bounds Bounds to fit the task within. Nothing is done if null.
-     *  @return Returns true if the task bounds was adjusted in any way.
-     *  */
-    boolean fitWithinBounds(Rect bounds) {
-        if (bounds == null || bounds.contains(mBounds)) {
-            return false;
-        }
-        mTmpRect2.set(mBounds);
-
-        if (mBounds.left < bounds.left || mBounds.right > bounds.right) {
-            final int maxRight = bounds.right - (bounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
-            int horizontalDiff = bounds.left - mBounds.left;
-            if ((horizontalDiff < 0 && mBounds.left >= maxRight)
-                    || (mBounds.left + horizontalDiff >= maxRight)) {
-                horizontalDiff = maxRight - mBounds.left;
-            }
-            mTmpRect2.left += horizontalDiff;
-            mTmpRect2.right += horizontalDiff;
-        }
-
-        if (mBounds.top < bounds.top || mBounds.bottom > bounds.bottom) {
-            final int maxBottom = bounds.bottom - (bounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
-            int verticalDiff = bounds.top - mBounds.top;
-            if ((verticalDiff < 0 && mBounds.top >= maxBottom)
-                    || (mBounds.top + verticalDiff >= maxBottom)) {
-                verticalDiff = maxBottom - mBounds.top;
-            }
-            mTmpRect2.top += verticalDiff;
-            mTmpRect2.bottom += verticalDiff;
-        }
-
-        return setBounds(mTmpRect2);
-    }
-
     /** Set the task bounds. Passing in null sets the bounds to fullscreen. */
-    boolean setBounds(Rect bounds) {
+    boolean setBounds(Rect bounds, Configuration config) {
+        if (config == null) {
+            config = Configuration.EMPTY;
+        }
+        if (bounds == null && !Configuration.EMPTY.equals(config)) {
+            throw new IllegalArgumentException("null bounds but non empty configuration: "
+                    + config);
+        }
+        if (bounds != null && Configuration.EMPTY.equals(config)) {
+            throw new IllegalArgumentException("non null bounds, but empty configuration");
+        }
         boolean oldFullscreen = mFullscreen;
         int rotation = Surface.ROTATION_0;
         final DisplayContent displayContent = mStack.getDisplayContent();
@@ -241,7 +208,7 @@
         mBounds.set(bounds);
         mRotation = rotation;
         updateDimLayer();
-        updateOverrideConfiguration();
+        mOverrideConfig = mFullscreen ? Configuration.EMPTY : config;
         return true;
     }
 
@@ -249,36 +216,12 @@
         out.set(mBounds);
     }
 
-    private void updateOverrideConfiguration() {
-        final Configuration serviceConfig = mService.mCurConfiguration;
-        if (mFullscreen) {
-            mOverrideConfig = Configuration.EMPTY;
-            return;
-        }
-
-        if (mOverrideConfig == Configuration.EMPTY) {
-            mOverrideConfig  = new Configuration();
-        }
-
-        // TODO(multidisplay): Update Dp to that of display stack is on.
-        final float density = serviceConfig.densityDpi * DisplayMetrics.DENSITY_DEFAULT_SCALE;
-        mOverrideConfig.screenWidthDp =
-                Math.min((int)(mBounds.width() / density), serviceConfig.screenWidthDp);
-        mOverrideConfig.screenHeightDp =
-                Math.min((int)(mBounds.height() / density), serviceConfig.screenHeightDp);
-        mOverrideConfig.smallestScreenWidthDp =
-                Math.min(mOverrideConfig.screenWidthDp, mOverrideConfig.screenHeightDp);
-        mOverrideConfig.orientation =
-                (mOverrideConfig.screenWidthDp <= mOverrideConfig.screenHeightDp)
-                        ? Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
-    }
-
     void updateDisplayInfo(final DisplayContent displayContent) {
         if (displayContent == null) {
             return;
         }
         if (mFullscreen) {
-            setBounds(null);
+            setBounds(null, Configuration.EMPTY);
             return;
         }
         final int newRotation = displayContent.getDisplayInfo().rotation;
@@ -313,7 +256,7 @@
                 mTmpRect2.bottom = mTmpRect2.top + mBounds.width();
                 break;
         }
-        setBounds(mTmpRect2);
+        setBounds(mTmpRect2, mOverrideConfig);
     }
 
     /** Updates the dim layer bounds, recreating it if needed. */
diff --git a/services/core/java/com/android/server/wm/TaskStack.java b/services/core/java/com/android/server/wm/TaskStack.java
index 25a71d9..f21f00a 100644
--- a/services/core/java/com/android/server/wm/TaskStack.java
+++ b/services/core/java/com/android/server/wm/TaskStack.java
@@ -27,6 +27,7 @@
 import android.util.EventLog;
 import android.util.IntArray;
 import android.util.Slog;
+import android.util.SparseArray;
 import android.view.DisplayInfo;
 
 import com.android.server.EventLogTags;
@@ -96,15 +97,15 @@
 
     /**
      * Set the bounds of the stack and its containing tasks.
-     * @param bounds New stack bounds. Passing in null sets the bounds to fullscreen.
+     * @param stackBounds New stack bounds. Passing in null sets the bounds to fullscreen.
      * @param resizeTasks If true, the tasks within the stack will also be resized.
-     * @param changedTaskIds Output list of Ids of tasks that changed in bounds.
-     * @param newTaskConfigs Output list of new Configuation of the tasks that changed.
+     * @param configs Configuration for individual tasks, keyed by task id.
+     * @param taskBounds Bounds for individual tasks, keyed by task id.
      * @return True if the stack bounds was changed.
      * */
-    boolean setBounds(Rect bounds, boolean resizeTasks, IntArray changedTaskIds,
-            List<Configuration> newTaskConfigs) {
-        if (!setBounds(bounds)) {
+    boolean setBounds(Rect stackBounds, boolean resizeTasks, SparseArray<Configuration> configs,
+            SparseArray<Rect> taskBounds) {
+        if (!setBounds(stackBounds)) {
             return false;
         }
 
@@ -113,20 +114,17 @@
         }
 
         // Update bounds of containing tasks.
-        final Rect newBounds = mFullscreen ? null : mBounds;
         for (int taskNdx = mTasks.size() - 1; taskNdx >= 0; --taskNdx) {
             final Task task = mTasks.get(taskNdx);
-            if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
-                // For freeform stack we don't adjust the size of the tasks to match that of the
-                // stack, but we do try to make sure the tasks are still contained with the
-                // bounds of the stack.
-                if (task.fitWithinBounds(newBounds)) {
-                    changedTaskIds.add(task.mTaskId);
-                    newTaskConfigs.add(task.mOverrideConfig);
+            Configuration config = configs.get(task.mTaskId);
+            if (config != null) {
+                Rect bounds = taskBounds.get(task.mTaskId);
+                if (bounds == null) {
+                    bounds = stackBounds;
                 }
-            } else if (task.setBounds(newBounds)) {
-                changedTaskIds.add(task.mTaskId);
-                newTaskConfigs.add(task.mOverrideConfig);
+                task.setBounds(bounds, config);
+            } else {
+                Slog.wtf(TAG, "No config for task: " + task + ", is there a mismatch with AM?");
             }
         }
         return true;
@@ -407,7 +405,10 @@
             for (int appNdx = appWindowTokens.size() - 1; appNdx >= 0; --appNdx) {
                 final WindowList appWindows = appWindowTokens.get(appNdx).allAppWindows;
                 for (int winNdx = appWindows.size() - 1; winNdx >= 0; --winNdx) {
-                    mService.removeWindowInnerLocked(appWindows.get(winNdx));
+                    // We are in the middle of changing the state of displays/stacks/tasks. We need
+                    // to finish that, before we let layout interfere with it.
+                    mService.removeWindowInnerLocked(appWindows.get(winNdx),
+                            false /* performLayout */);
                     doAnotherLayoutPass = true;
                 }
             }
diff --git a/services/core/java/com/android/server/wm/WallpaperController.java b/services/core/java/com/android/server/wm/WallpaperController.java
index d0962f4..fc23fd1 100644
--- a/services/core/java/com/android/server/wm/WallpaperController.java
+++ b/services/core/java/com/android/server/wm/WallpaperController.java
@@ -750,7 +750,7 @@
     }
 
     boolean adjustWallpaperWindows() {
-        mService.mInnerFields.mWallpaperMayChange = false;
+        mService.mWindowPlacerLocked.mWallpaperMayChange = false;
 
         final WindowList windows = mService.getDefaultWindowListLocked();
         // First find top-most window that has asked to be on top of the wallpaper;
diff --git a/services/core/java/com/android/server/wm/WindowAnimator.java b/services/core/java/com/android/server/wm/WindowAnimator.java
index e6a1be1..b334a05 100644
--- a/services/core/java/com/android/server/wm/WindowAnimator.java
+++ b/services/core/java/com/android/server/wm/WindowAnimator.java
@@ -23,11 +23,11 @@
 import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_SYSTEM_ERROR;
 import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
 import static com.android.server.wm.WindowManagerService.DEBUG_KEYGUARD;
-import static com.android.server.wm.WindowManagerService.LayoutFields.SET_UPDATE_ROTATION;
-import static com.android.server.wm.WindowManagerService.LayoutFields.SET_WALLPAPER_MAY_CHANGE;
-import static com.android.server.wm.WindowManagerService.LayoutFields.SET_FORCE_HIDING_CHANGED;
-import static com.android.server.wm.WindowManagerService.LayoutFields.SET_ORIENTATION_CHANGE_COMPLETE;
-import static com.android.server.wm.WindowManagerService.LayoutFields.SET_WALLPAPER_ACTION_PENDING;
+import static com.android.server.wm.WindowSurfacePlacer.SET_UPDATE_ROTATION;
+import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
+import static com.android.server.wm.WindowSurfacePlacer.SET_FORCE_HIDING_CHANGED;
+import static com.android.server.wm.WindowSurfacePlacer.SET_ORIENTATION_CHANGE_COMPLETE;
+import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_ACTION_PENDING;
 
 import android.content.Context;
 import android.os.RemoteException;
@@ -41,8 +41,6 @@
 import android.view.animation.Animation;
 import android.view.Choreographer;
 
-import com.android.server.wm.WindowManagerService.LayoutFields;
-
 import java.io.PrintWriter;
 import java.util.ArrayList;
 
@@ -84,8 +82,7 @@
     int mBulkUpdateParams = 0;
     Object mLastWindowFreezeSource;
 
-    SparseArray<DisplayContentsAnimator> mDisplayContentsAnimators =
-            new SparseArray<DisplayContentsAnimator>(2);
+    SparseArray<DisplayContentsAnimator> mDisplayContentsAnimators = new SparseArray<>(2);
 
     boolean mInitialized = false;
 
@@ -179,10 +176,12 @@
                     mAnimating = mAppWindowAnimating = true;
                 } else if (appAnimator.wasAnimating) {
                     // stopped animating, do one more pass through the layout
-                    setAppLayoutChanges(appAnimator, WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER,
-                        "exiting appToken " + appAnimator.mAppToken + " done", displayId);
+                    setAppLayoutChanges(appAnimator,
+                            WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER,
+                            "exiting appToken " + appAnimator.mAppToken + " done", displayId);
                     if (WindowManagerService.DEBUG_ANIM) Slog.v(TAG,
-                            "updateWindowsApps...: done animating exiting " + appAnimator.mAppToken);
+                            "updateWindowsApps...: done animating exiting "
+                                    + appAnimator.mAppToken);
                 }
             }
         }
@@ -301,7 +300,8 @@
                     setPendingLayoutChanges(Display.DEFAULT_DISPLAY,
                             WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER);
                     if (WindowManagerService.DEBUG_LAYOUT_REPEATS) {
-                        mService.debugLayoutRepeats("updateWindowsAndWallpaperLocked 2",
+                        mService.mWindowPlacerLocked.debugLayoutRepeats(
+                                "updateWindowsAndWallpaperLocked 2",
                                 getPendingLayoutChanges(Display.DEFAULT_DISPLAY));
                     }
                 }
@@ -315,7 +315,8 @@
                         setPendingLayoutChanges(displayId,
                                 WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER);
                         if (WindowManagerService.DEBUG_LAYOUT_REPEATS) {
-                            mService.debugLayoutRepeats("updateWindowsAndWallpaperLocked 3",
+                            mService.mWindowPlacerLocked.debugLayoutRepeats(
+                                    "updateWindowsAndWallpaperLocked 3",
                                     getPendingLayoutChanges(displayId));
                         }
                         mService.mFocusMayChange = true;
@@ -409,7 +410,8 @@
                         setPendingLayoutChanges(Display.DEFAULT_DISPLAY,
                                 WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER);
                         if (WindowManagerService.DEBUG_LAYOUT_REPEATS) {
-                            mService.debugLayoutRepeats("updateWindowsAndWallpaperLocked 4",
+                            mService.mWindowPlacerLocked.debugLayoutRepeats(
+                                    "updateWindowsAndWallpaperLocked 4",
                                     getPendingLayoutChanges(Display.DEFAULT_DISPLAY));
                         }
                     }
@@ -433,7 +435,8 @@
                         setPendingLayoutChanges(displayId,
                                 WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM);
                         if (WindowManagerService.DEBUG_LAYOUT_REPEATS) {
-                            mService.debugLayoutRepeats("updateWindowsAndWallpaperLocked 5",
+                            mService.mWindowPlacerLocked.debugLayoutRepeats(
+                                    "updateWindowsAndWallpaperLocked 5",
                                     getPendingLayoutChanges(displayId));
                         }
                     }
@@ -607,7 +610,8 @@
                                     "Setting mOrientationChangeComplete=true because wtoken "
                                     + wtoken + " numInteresting=" + wtoken.numInterestingWindows
                                     + " numDrawn=" + wtoken.numDrawnWindows);
-                            // This will set mOrientationChangeComplete and cause a pass through layout.
+                            // This will set mOrientationChangeComplete and cause a pass through
+                            // layout.
                             setAppLayoutChanges(appAnimator,
                                     WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER,
                                     "testTokenMayBeDrawnLocked: freezingScreen", displayId);
@@ -737,7 +741,7 @@
 
         boolean doRequest = false;
         if (mBulkUpdateParams != 0) {
-            doRequest = mService.copyAnimToLayoutParamsLocked();
+            doRequest = mService.mWindowPlacerLocked.copyAnimToLayoutParamsLocked();
         }
 
         if (hasPendingLayoutChanges || doRequest) {
@@ -755,21 +759,21 @@
         }
     }
 
-    static String bulkUpdateParamsToString(int bulkUpdateParams) {
+    private static String bulkUpdateParamsToString(int bulkUpdateParams) {
         StringBuilder builder = new StringBuilder(128);
-        if ((bulkUpdateParams & LayoutFields.SET_UPDATE_ROTATION) != 0) {
+        if ((bulkUpdateParams & WindowSurfacePlacer.SET_UPDATE_ROTATION) != 0) {
             builder.append(" UPDATE_ROTATION");
         }
-        if ((bulkUpdateParams & LayoutFields.SET_WALLPAPER_MAY_CHANGE) != 0) {
+        if ((bulkUpdateParams & WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE) != 0) {
             builder.append(" WALLPAPER_MAY_CHANGE");
         }
-        if ((bulkUpdateParams & LayoutFields.SET_FORCE_HIDING_CHANGED) != 0) {
+        if ((bulkUpdateParams & WindowSurfacePlacer.SET_FORCE_HIDING_CHANGED) != 0) {
             builder.append(" FORCE_HIDING_CHANGED");
         }
-        if ((bulkUpdateParams & LayoutFields.SET_ORIENTATION_CHANGE_COMPLETE) != 0) {
+        if ((bulkUpdateParams & WindowSurfacePlacer.SET_ORIENTATION_CHANGE_COMPLETE) != 0) {
             builder.append(" ORIENTATION_CHANGE_COMPLETE");
         }
-        if ((bulkUpdateParams & LayoutFields.SET_TURN_ON_SCREEN) != 0) {
+        if ((bulkUpdateParams & WindowSurfacePlacer.SET_TURN_ON_SCREEN) != 0) {
             builder.append(" TURN_ON_SCREEN");
         }
         return builder.toString();
@@ -846,7 +850,8 @@
             if (displayId == windows.get(i).getDisplayId()) {
                 setPendingLayoutChanges(displayId, changes);
                 if (WindowManagerService.DEBUG_LAYOUT_REPEATS) {
-                    mService.debugLayoutRepeats(reason, getPendingLayoutChanges(displayId));
+                    mService.mWindowPlacerLocked.debugLayoutRepeats(reason,
+                            getPendingLayoutChanges(displayId));
                 }
                 break;
             }
diff --git a/services/core/java/com/android/server/wm/WindowManagerService.java b/services/core/java/com/android/server/wm/WindowManagerService.java
index cf690a5..ebabc52 100644
--- a/services/core/java/com/android/server/wm/WindowManagerService.java
+++ b/services/core/java/com/android/server/wm/WindowManagerService.java
@@ -34,8 +34,6 @@
 import android.content.res.Configuration;
 import android.database.ContentObserver;
 import android.graphics.Bitmap;
-import android.graphics.Bitmap.Config;
-import android.graphics.Canvas;
 import android.graphics.PixelFormat;
 import android.graphics.Point;
 import android.graphics.Rect;
@@ -70,7 +68,6 @@
 import android.util.ArraySet;
 import android.util.DisplayMetrics;
 import android.util.EventLog;
-import android.util.IntArray;
 import android.util.Log;
 import android.util.Pair;
 import android.util.Slog;
@@ -112,7 +109,6 @@
 import android.view.WindowManagerPolicy;
 import android.view.WindowManagerPolicy.PointerEventListener;
 import android.view.animation.Animation;
-import android.view.animation.AnimationUtils;
 
 import static com.android.server.wm.WindowState.BOUNDS_FOR_TOUCH;
 import com.android.internal.app.IAssistScreenshotReceiver;
@@ -157,16 +153,12 @@
 import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW;
 import static android.view.WindowManager.LayoutParams.FIRST_SUB_WINDOW;
 import static android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
-import static android.view.WindowManager.LayoutParams.FLAG_DIM_BEHIND;
-import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
 import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
 import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
 import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
 import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
 import static android.view.WindowManager.LayoutParams.LAST_SUB_WINDOW;
 import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_COMPATIBLE_WINDOW;
-import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
-import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_NO_MOVE_ANIMATION;
 import static android.view.WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
 import static android.view.WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST;
 import static android.view.WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY;
@@ -177,11 +169,8 @@
 import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
 import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
 import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
-import static android.view.WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG;
 import static android.view.WindowManager.LayoutParams.TYPE_PRIVATE_PRESENTATION;
 import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
-import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
-import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
 import static android.view.WindowManager.LayoutParams.TYPE_VOICE_INTERACTION;
 import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
 import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
@@ -313,6 +302,7 @@
             }
         }
     };
+    final WindowSurfacePlacer mWindowPlacerLocked;
 
     /**
      * Current user when multi-user is enabled. Don't show windows of
@@ -341,8 +331,6 @@
 
     final IActivityManager mActivityManager;
 
-    final IBatteryStats mBatteryStats;
-
     final AppOpsManager mAppOps;
 
     final DisplaySettings mDisplaySettings;
@@ -444,7 +432,6 @@
 
     final float[] mTmpFloats = new float[9];
     final Rect mTmpContentRect = new Rect();
-    private final Rect mTmpStartRect = new Rect();
 
     boolean mDisplayReady;
     boolean mSafeMode;
@@ -491,7 +478,7 @@
     final static int WINDOWS_FREEZING_SCREENS_NONE = 0;
     final static int WINDOWS_FREEZING_SCREENS_ACTIVE = 1;
     final static int WINDOWS_FREEZING_SCREENS_TIMEOUT = 2;
-    private int mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_NONE;
+    int mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_NONE;
 
     boolean mClientFreezingScreen = false;
     int mAppsFreezingScreen = 0;
@@ -614,51 +601,11 @@
     // For frozen screen animations.
     int mExitAnimId, mEnterAnimId;
 
-    /** Pulled out of performLayoutAndPlaceSurfacesLockedInner in order to refactor into multiple
-     * methods. */
-    class LayoutFields {
-        static final int SET_UPDATE_ROTATION                = 1 << 0;
-        static final int SET_WALLPAPER_MAY_CHANGE           = 1 << 1;
-        static final int SET_FORCE_HIDING_CHANGED           = 1 << 2;
-        static final int SET_ORIENTATION_CHANGE_COMPLETE    = 1 << 3;
-        static final int SET_TURN_ON_SCREEN                 = 1 << 4;
-        static final int SET_WALLPAPER_ACTION_PENDING       = 1 << 5;
-
-        boolean mWallpaperForceHidingChanged = false;
-        boolean mWallpaperMayChange = false;
-        boolean mOrientationChangeComplete = true;
-        Object mLastWindowFreezeSource = null;
-        private Session mHoldScreen = null;
-        private boolean mObscured = false;
-        private boolean mSyswin = false;
-        private float mScreenBrightness = -1;
-        private float mButtonBrightness = -1;
-        private long mUserActivityTimeout = -1;
-        private boolean mUpdateRotation = false;
-        boolean mWallpaperActionPending = false;
-
-        // Set to true when the display contains content to show the user.
-        // When false, the display manager may choose to mirror or blank the display.
-        boolean mDisplayHasContent = false;
-
-        // Only set while traversing the default display based on its content.
-        // Affects the behavior of mirroring on secondary displays.
-        boolean mObscureApplicationContentOnSecondaryDisplays = false;
-
-        float mPreferredRefreshRate = 0;
-
-        int mPreferredModeId = 0;
-    }
-    final LayoutFields mInnerFields = new LayoutFields();
-
     boolean mAnimationScheduled;
 
     /** Skip repeated AppWindowTokens initialization. Note that AppWindowsToken's version of this
      * is a long initialized to Long.MIN_VALUE so that it doesn't match this value on startup. */
-    private int mTransactionSequence;
-
-    /** Only do a maximum of 6 repeated layouts. After that quit */
-    private int mLayoutRepeatCount;
+    int mTransactionSequence;
 
     final WindowAnimator mAnimator;
 
@@ -768,7 +715,7 @@
     boolean mInTouchMode;
 
     private ViewServer mViewServer;
-    private final ArrayList<WindowChangeListener> mWindowChangeListeners = new ArrayList<>();
+    final ArrayList<WindowChangeListener> mWindowChangeListeners = new ArrayList<>();
     boolean mWindowsChanged = false;
 
     public interface WindowChangeListener {
@@ -788,7 +735,7 @@
 
     // List of clients without a transtiton animation that we notify once we are done transitioning
     // since they won't be notified through the app window animator.
-    private final List<IBinder> mNoAnimationNotifyOnTransitionFinished = new ArrayList<>();
+    final List<IBinder> mNoAnimationNotifyOnTransitionFinished = new ArrayList<>();
 
     /** Listener to notify activity manager about app transitions. */
     private final WindowManagerInternal.AppTransitionListener mActivityManagerAppTransitionNotifier
@@ -866,6 +813,9 @@
         mDisplaySettings = new DisplaySettings();
         mDisplaySettings.readSettingsLocked();
 
+        mWallpaperControllerLocked = new WallpaperController(this);
+        mWindowPlacerLocked = new WindowSurfacePlacer(this);
+
         LocalServices.addService(WindowManagerPolicy.class, mPolicy);
 
         mPointerEventDispatcher = new PointerEventDispatcher(mInputManager.monitorInput(TAG));
@@ -902,7 +852,6 @@
         mAppTransition.registerListenerLocked(mActivityManagerAppTransitionNotifier);
 
         mActivityManager = ActivityManagerNative.getDefault();
-        mBatteryStats = BatteryStatsService.getService();
         mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
         AppOpsManager.OnOpChangedInternalListener opListener =
                 new AppOpsManager.OnOpChangedInternalListener() {
@@ -953,8 +902,6 @@
 
         updateCircularDisplayMaskIfNeeded();
         showEmulatorDisplayOverlayIfNeeded();
-
-        mWallpaperControllerLocked = new WallpaperController(this);
     }
 
     public InputMonitor getInputMonitor() {
@@ -2118,6 +2065,15 @@
         // If the display is frozen, just remove immediately, since the
         // animation wouldn't be seen.
         if (win.mHasSurface && okToDisplay()) {
+            final AppWindowToken appToken = win.mAppToken;
+            if (appToken != null && appToken.mReplacingWindow) {
+                // This window is going to be replaced. We need to kepp it around until the new one
+                // gets added, then we will get rid of this one.
+                if (DEBUG_ADD_REMOVE) Slog.v(TAG, "Preserving " + win + " until the new one is "
+                        + "added");
+                win.mExiting = true;
+                return;
+            }
             // If we are not currently running the exit animation, we
             // need to see about starting one.
             wasVisible = win.isWinVisibleLw();
@@ -2137,7 +2093,6 @@
                     mAccessibilityController.onWindowTransitionLocked(win, transit);
                 }
             }
-            final AppWindowToken appToken = win.mAppToken;
             final boolean isAnimating = win.mWinAnimator.isAnimating();
             // The starting window is the last window in this app token and it isn't animating.
             // Allow it to be removed now as there is no additional window or animation that will
@@ -2154,7 +2109,7 @@
                 }
                 final boolean focusChanged = updateFocusedWindowLocked(
                         UPDATE_FOCUS_WILL_PLACE_SURFACES, false /*updateInputWindows*/);
-                performLayoutAndPlaceSurfacesLocked();
+                mWindowPlacerLocked.performSurfacePlacement();
                 if (appToken != null) {
                     appToken.updateReportedVisibilityLocked();
                 }
@@ -2177,6 +2132,10 @@
     }
 
     void removeWindowInnerLocked(WindowState win) {
+        removeWindowInnerLocked(win, true);
+    }
+
+    void removeWindowInnerLocked(WindowState win, boolean performLayout) {
         if (win.mRemoved) {
             // Nothing to do.
             return;
@@ -2268,13 +2227,15 @@
         final WindowList windows = win.getWindowList();
         if (windows != null) {
             windows.remove(win);
-            if (!mInLayout) {
+            if (!mWindowPlacerLocked.isInLayout()) {
                 assignLayersLocked(windows);
                 final DisplayContent displayContent = win.getDisplayContent();
                 if (displayContent != null) {
                     displayContent.layoutNeeded = true;
                 }
-                performLayoutAndPlaceSurfacesLocked();
+                if (performLayout) {
+                    mWindowPlacerLocked.performSurfacePlacement();
+                }
                 if (win.mAppToken != null) {
                     win.mAppToken.updateReportedVisibilityLocked();
                 }
@@ -2357,7 +2318,7 @@
                     if (displayContent != null) {
                         displayContent.layoutNeeded = true;
                     }
-                    performLayoutAndPlaceSurfacesLocked();
+                    mWindowPlacerLocked.performSurfacePlacement();
                 }
             }
         } finally {
@@ -2686,7 +2647,7 @@
             }
             win.mGivenInsetsPending = (flags&WindowManagerGlobal.RELAYOUT_INSETS_PENDING) != 0;
             configChanged = updateOrientationFromAppTokensLocked(false);
-            performLayoutAndPlaceSurfacesLocked();
+            mWindowPlacerLocked.performSurfacePlacement();
             if (toBeDisplayed && win.mIsWallpaper) {
                 DisplayInfo displayInfo = getDefaultDisplayInfoLocked();
                 mWallpaperControllerLocked.updateWallpaperOffset(
@@ -3019,7 +2980,7 @@
                     wtoken.hidden = true;
 
                     if (changed) {
-                        performLayoutAndPlaceSurfacesLocked();
+                        mWindowPlacerLocked.performSurfacePlacement();
                         updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL,
                                 false /*updateInputWindows*/);
                     }
@@ -3041,8 +3002,8 @@
         Binder.restoreCallingIdentity(origId);
     }
 
-    private Task createTaskLocked(
-            int taskId, int stackId, int userId, AppWindowToken atoken, Rect bounds) {
+    private Task createTaskLocked(int taskId, int stackId, int userId, AppWindowToken atoken,
+            Rect bounds, Configuration config) {
         if (DEBUG_STACK) Slog.i(TAG, "createTaskLocked: taskId=" + taskId + " stackId=" + stackId
                 + " atoken=" + atoken + " bounds=" + bounds);
         final TaskStack stack = mStackIdToStack.get(stackId);
@@ -3050,17 +3011,17 @@
             throw new IllegalArgumentException("addAppToken: invalid stackId=" + stackId);
         }
         EventLog.writeEvent(EventLogTags.WM_TASK_CREATED, taskId, stackId);
-        Task task = new Task(taskId, stack, userId, this, bounds);
+        Task task = new Task(taskId, stack, userId, this, bounds, config);
         mTaskIdToTask.put(taskId, task);
         stack.addTask(task, !atoken.mLaunchTaskBehind /* toTop */, atoken.showForAllUsers);
         return task;
     }
 
     @Override
-    public Configuration addAppToken(int addPos, IApplicationToken token, int taskId, int stackId,
+    public void addAppToken(int addPos, IApplicationToken token, int taskId, int stackId,
             int requestedOrientation, boolean fullscreen, boolean showForAllUsers, int userId,
             int configChanges, boolean voiceInteraction, boolean launchTaskBehind,
-            Rect taskBounds) {
+            Rect taskBounds, Configuration config) {
         if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
                 "addAppToken()")) {
             throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
@@ -3084,7 +3045,7 @@
             AppWindowToken atoken = findAppWindowToken(token.asBinder());
             if (atoken != null) {
                 Slog.w(TAG, "Attempted to add existing app token: " + token);
-                return null;
+                return;
             }
             atoken = new AppWindowToken(this, token, voiceInteraction);
             atoken.inputDispatchingTimeoutNanos = inputDispatchingTimeoutNanos;
@@ -3098,10 +3059,8 @@
                     + " to stack=" + stackId + " task=" + taskId + " at " + addPos);
 
             Task task = mTaskIdToTask.get(taskId);
-            Configuration outConfig = null;
             if (task == null) {
-                task = createTaskLocked(taskId, stackId, userId, atoken, taskBounds);
-                outConfig = task.mOverrideConfig;
+                task = createTaskLocked(taskId, stackId, userId, atoken, taskBounds, config);
             }
             task.addAppToken(addPos, atoken);
 
@@ -3110,13 +3069,11 @@
             // Application tokens start out hidden.
             atoken.hidden = true;
             atoken.hiddenRequested = true;
-
-            return outConfig;
         }
     }
 
     @Override
-    public Configuration setAppTask(IBinder token, int taskId, Rect taskBounds) {
+    public void setAppTask(IBinder token, int taskId, Rect taskBounds, Configuration config) {
         if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
                 "setAppTask()")) {
             throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
@@ -3126,20 +3083,18 @@
             final AppWindowToken atoken = findAppWindowToken(token);
             if (atoken == null) {
                 Slog.w(TAG, "Attempted to set task id of non-existing app token: " + token);
-                return null;
+                return;
             }
             final Task oldTask = atoken.mTask;
             oldTask.removeAppToken(atoken);
 
             Task newTask = mTaskIdToTask.get(taskId);
-            Configuration outConfig = null;
             if (newTask == null) {
                 newTask = createTaskLocked(
-                        taskId, oldTask.mStack.mStackId, oldTask.mUserId, atoken, taskBounds);
-                outConfig = newTask.mOverrideConfig;
+                        taskId, oldTask.mStack.mStackId, oldTask.mUserId, atoken, taskBounds,
+                        config);
             }
             newTask.addAppToken(Integer.MAX_VALUE /* at top */, atoken);
-            return outConfig;
         }
     }
 
@@ -3391,7 +3346,7 @@
                 mWaitingForConfig = false;
                 mLastFinishedFreezeSource = "new-config";
             }
-            performLayoutAndPlaceSurfacesLocked();
+            mWindowPlacerLocked.performSurfacePlacement();
         }
     }
 
@@ -3589,7 +3544,7 @@
                 mAppTransition.setReady();
                 final long origId = Binder.clearCallingIdentity();
                 try {
-                    performLayoutAndPlaceSurfacesLocked();
+                    mWindowPlacerLocked.performSurfacePlacement();
                 } finally {
                     Binder.restoreCallingIdentity(origId);
                 }
@@ -3696,7 +3651,7 @@
                         updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
                                 true /*updateInputWindows*/);
                         getDefaultDisplayContentLocked().layoutNeeded = true;
-                        performLayoutAndPlaceSurfacesLocked();
+                        mWindowPlacerLocked.performSurfacePlacement();
                         Binder.restoreCallingIdentity(origId);
                         return;
                     } else if (ttoken.startingData != null) {
@@ -3962,7 +3917,7 @@
                 if (performLayout) {
                     updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
                             false /*updateInputWindows*/);
-                    performLayoutAndPlaceSurfacesLocked();
+                    mWindowPlacerLocked.performSurfacePlacement();
                 }
                 mInputMonitor.updateInputWindowsLw(false /*force*/);
             }
@@ -4100,7 +4055,7 @@
                             && mWindowsFreezingScreen != WINDOWS_FREEZING_SCREENS_TIMEOUT) {
                         if (DEBUG_ORIENTATION) Slog.v(TAG, "set mOrientationChanging of " + w);
                         w.mOrientationChanging = true;
-                        mInnerFields.mOrientationChangeComplete = false;
+                        mWindowPlacerLocked.mOrientationChangeComplete = false;
                     }
                     w.mLastFreezeDuration = 0;
                     unfrozeWindows = true;
@@ -4120,7 +4075,7 @@
             }
             if (unfreezeSurfaceNow) {
                 if (unfrozeWindows) {
-                    performLayoutAndPlaceSurfacesLocked();
+                    mWindowPlacerLocked.performSurfacePlacement();
                 }
                 stopFreezingDisplayLocked();
             }
@@ -4408,7 +4363,7 @@
         }
 
         mInputMonitor.setUpdateInputWindowsNeededLw();
-        performLayoutAndPlaceSurfacesLocked();
+        mWindowPlacerLocked.performSurfacePlacement();
         mInputMonitor.updateInputWindowsLw(false /*force*/);
 
         //dump();
@@ -4531,7 +4486,9 @@
     }
 
     public void removeStack(int stackId) {
-        mStackIdToStack.remove(stackId);
+        synchronized (mWindowMap) {
+            mStackIdToStack.remove(stackId);
+        }
     }
 
     public void removeTask(int taskId) {
@@ -4558,7 +4515,7 @@
             stack.addTask(task, toTop);
             final DisplayContent displayContent = stack.getDisplayContent();
             displayContent.layoutNeeded = true;
-            performLayoutAndPlaceSurfacesLocked();
+            mWindowPlacerLocked.performSurfacePlacement();
         }
     }
 
@@ -4579,7 +4536,7 @@
             task.moveTaskToStack(stack, toTop);
             final DisplayContent displayContent = stack.getDisplayContent();
             displayContent.layoutNeeded = true;
-            performLayoutAndPlaceSurfacesLocked();
+            mWindowPlacerLocked.performSurfacePlacement();
         }
     }
 
@@ -4599,22 +4556,22 @@
      * @param stackId Id of stack to resize.
      * @param bounds New stack bounds. Passing in null sets the bounds to fullscreen.
      * @param resizeTasks If true, the tasks within the stack will also be resized.
-     * @param changedTaskIds Output list of Ids of tasks that changed in bounds due to resize.
-     * @param newTaskConfigs Output list of new Configuation of the tasks that changed.
+     * @param configs Configurations for tasks in the resized stack, keyed by task id.
+     * @param taskBounds Bounds for tasks in the resized stack, keyed by task id.
      * @return True if the stack is now fullscreen.
      * */
     public boolean resizeStack(int stackId, Rect bounds, boolean resizeTasks,
-            IntArray changedTaskIds, List<Configuration> newTaskConfigs) {
+            SparseArray<Configuration> configs, SparseArray<Rect> taskBounds) {
         synchronized (mWindowMap) {
             final TaskStack stack = mStackIdToStack.get(stackId);
             if (stack == null) {
                 throw new IllegalArgumentException("resizeStack: stackId " + stackId
                         + " not found.");
             }
-            if (stack.setBounds(bounds, resizeTasks, changedTaskIds, newTaskConfigs)) {
+            if (stack.setBounds(bounds, resizeTasks, configs, taskBounds)) {
                 stack.resizeWindows();
                 stack.getDisplayContent().layoutNeeded = true;
-                performLayoutAndPlaceSurfacesLocked();
+                mWindowPlacerLocked.performSurfacePlacement();
             }
             return stack.isFullscreen();
         }
@@ -4639,7 +4596,7 @@
             task.positionTaskInStack(stack, position);
             final DisplayContent displayContent = stack.getDisplayContent();
             displayContent.layoutNeeded = true;
-            performLayoutAndPlaceSurfacesLocked();
+            mWindowPlacerLocked.performSurfacePlacement();
         }
     }
 
@@ -4648,19 +4605,20 @@
      * Returns a {@link Configuration} object that contains configurations settings
      * that should be overridden due to the operation.
      */
-    public Configuration resizeTask(int taskId, Rect bounds) {
+    public void resizeTask(int taskId, Rect bounds, Configuration configuration, boolean relayout) {
         synchronized (mWindowMap) {
             Task task = mTaskIdToTask.get(taskId);
             if (task == null) {
                 throw new IllegalArgumentException("resizeTask: taskId " + taskId
                         + " not found.");
             }
-            if (task.setBounds(bounds)) {
+            if (task.setBounds(bounds, configuration)) {
                 task.resizeWindows();
-                task.getDisplayContent().layoutNeeded = true;
-                performLayoutAndPlaceSurfacesLocked();
+                if (relayout) {
+                    task.getDisplayContent().layoutNeeded = true;
+                    mWindowPlacerLocked.performSurfacePlacement();
+                }
             }
-            return new Configuration(task.mOverrideConfig);
         }
     }
 
@@ -5051,7 +5009,7 @@
                 displayContent.switchUserStacks();
                 rebuildAppWindowListLocked(displayContent);
             }
-            performLayoutAndPlaceSurfacesLocked();
+            mWindowPlacerLocked.performSurfacePlacement();
         }
     }
 
@@ -5890,7 +5848,7 @@
             changed = updateRotationUncheckedLocked(false);
             if (!changed || forceRelayout) {
                 getDefaultDisplayContentLocked().layoutNeeded = true;
-                performLayoutAndPlaceSurfacesLocked();
+                mWindowPlacerLocked.performSurfacePlacement();
             }
         }
 
@@ -6024,7 +5982,7 @@
             if (w.mHasSurface) {
                 if (DEBUG_ORIENTATION) Slog.v(TAG, "Set mOrientationChanging of " + w);
                 w.mOrientationChanging = true;
-                mInnerFields.mOrientationChangeComplete = false;
+                mWindowPlacerLocked.mOrientationChangeComplete = false;
             }
             w.mLastFreezeDuration = 0;
         }
@@ -7175,7 +7133,7 @@
                 case DO_TRAVERSAL: {
                     synchronized(mWindowMap) {
                         mTraversalScheduled = false;
-                        performLayoutAndPlaceSurfacesLocked();
+                        mWindowPlacerLocked.performSurfacePlacement();
                     }
                 } break;
 
@@ -7347,7 +7305,7 @@
                                 Slog.w(TAG, "Force clearing orientation change: " + w);
                             }
                         }
-                        performLayoutAndPlaceSurfacesLocked();
+                        mWindowPlacerLocked.performSurfacePlacement();
                     }
                     break;
                 }
@@ -7361,7 +7319,7 @@
                                     + " mOpeningApps.size()=" + mOpeningApps.size()
                                     + " mClosingApps.size()=" + mClosingApps.size());
                             mAppTransition.setTimeout();
-                            performLayoutAndPlaceSurfacesLocked();
+                            mWindowPlacerLocked.performSurfacePlacement();
                         }
                     }
                     break;
@@ -7624,7 +7582,7 @@
                 case WALLPAPER_DRAW_PENDING_TIMEOUT: {
                     synchronized (mWindowMap) {
                         if (mWallpaperControllerLocked.processWallpaperDrawPendingTimeout()) {
-                            performLayoutAndPlaceSurfacesLocked();
+                            mWindowPlacerLocked.performSurfacePlacement();
                         }
                     }
                 }
@@ -8016,7 +7974,7 @@
             mH.sendEmptyMessage(H.SEND_NEW_CONFIGURATION);
         }
 
-        performLayoutAndPlaceSurfacesLocked();
+        mWindowPlacerLocked.performSurfacePlacement();
     }
 
     private void configureDisplayPolicyLocked(DisplayContent displayContent) {
@@ -8204,7 +8162,7 @@
         Arrays.fill(mRebuildTmp, null);
     }
 
-    private final void assignLayersLocked(WindowList windows) {
+    final void assignLayersLocked(WindowList windows) {
         int N = windows.size();
         int curBaseLayer = 0;
         int curLayer = 0;
@@ -8274,253 +8232,6 @@
         }
     }
 
-    private final void performLayoutAndPlaceSurfacesLocked() {
-        int loopCount = 6;
-        do {
-            mTraversalScheduled = false;
-            performLayoutAndPlaceSurfacesLockedLoop();
-            mH.removeMessages(H.DO_TRAVERSAL);
-            loopCount--;
-        } while (mTraversalScheduled && loopCount > 0);
-        mInnerFields.mWallpaperActionPending = false;
-    }
-
-    private boolean mInLayout = false;
-    private final void performLayoutAndPlaceSurfacesLockedLoop() {
-        if (mInLayout) {
-            if (DEBUG) {
-                throw new RuntimeException("Recursive call!");
-            }
-            Slog.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout. Callers="
-                    + Debug.getCallers(3));
-            return;
-        }
-
-        if (mWaitingForConfig) {
-            // Our configuration has changed (most likely rotation), but we
-            // don't yet have the complete configuration to report to
-            // applications.  Don't do any window layout until we have it.
-            return;
-        }
-
-        if (!mDisplayReady) {
-            // Not yet initialized, nothing to do.
-            return;
-        }
-
-        Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "wmLayout");
-        mInLayout = true;
-
-        boolean recoveringMemory = false;
-        if (!mForceRemoves.isEmpty()) {
-            recoveringMemory = true;
-            // Wait a little bit for things to settle down, and off we go.
-            while (!mForceRemoves.isEmpty()) {
-                WindowState ws = mForceRemoves.remove(0);
-                Slog.i(TAG, "Force removing: " + ws);
-                removeWindowInnerLocked(ws);
-            }
-            Slog.w(TAG, "Due to memory failure, waiting a bit for next layout");
-            Object tmp = new Object();
-            synchronized (tmp) {
-                try {
-                    tmp.wait(250);
-                } catch (InterruptedException e) {
-                }
-            }
-        }
-
-        try {
-            performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
-
-            mInLayout = false;
-
-            if (needsLayout()) {
-                if (++mLayoutRepeatCount < 6) {
-                    requestTraversalLocked();
-                } else {
-                    Slog.e(TAG, "Performed 6 layouts in a row. Skipping");
-                    mLayoutRepeatCount = 0;
-                }
-            } else {
-                mLayoutRepeatCount = 0;
-            }
-
-            if (mWindowsChanged && !mWindowChangeListeners.isEmpty()) {
-                mH.removeMessages(H.REPORT_WINDOWS_CHANGE);
-                mH.sendEmptyMessage(H.REPORT_WINDOWS_CHANGE);
-            }
-        } catch (RuntimeException e) {
-            mInLayout = false;
-            Slog.wtf(TAG, "Unhandled exception while laying out windows", e);
-        }
-
-        Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
-    }
-
-    private final void performLayoutLockedInner(final DisplayContent displayContent,
-                                    boolean initial, boolean updateInputWindows) {
-        if (!displayContent.layoutNeeded) {
-            return;
-        }
-        displayContent.layoutNeeded = false;
-        WindowList windows = displayContent.getWindowList();
-        boolean isDefaultDisplay = displayContent.isDefaultDisplay;
-
-        DisplayInfo displayInfo = displayContent.getDisplayInfo();
-        final int dw = displayInfo.logicalWidth;
-        final int dh = displayInfo.logicalHeight;
-
-        if (mInputConsumer != null) {
-            mInputConsumer.layout(dw, dh);
-        }
-
-        final int N = windows.size();
-        int i;
-
-        if (DEBUG_LAYOUT) {
-            Slog.v(TAG, "-------------------------------------");
-            Slog.v(TAG, "performLayout: needed="
-                    + displayContent.layoutNeeded + " dw=" + dw + " dh=" + dh);
-        }
-
-        mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mRotation);
-        if (isDefaultDisplay) {
-            // Not needed on non-default displays.
-            mSystemDecorLayer = mPolicy.getSystemDecorLayerLw();
-            mScreenRect.set(0, 0, dw, dh);
-        }
-
-        mPolicy.getContentRectLw(mTmpContentRect);
-        displayContent.resize(mTmpContentRect);
-
-        int seq = mLayoutSeq+1;
-        if (seq < 0) seq = 0;
-        mLayoutSeq = seq;
-
-        boolean behindDream = false;
-
-        // First perform layout of any root windows (not attached
-        // to another window).
-        int topAttached = -1;
-        for (i = N-1; i >= 0; i--) {
-            final WindowState win = windows.get(i);
-
-            // Don't do layout of a window if it is not visible, or
-            // soon won't be visible, to avoid wasting time and funky
-            // changes while a window is animating away.
-            final boolean gone = (behindDream && mPolicy.canBeForceHidden(win, win.mAttrs))
-                    || win.isGoneForLayoutLw();
-
-            if (DEBUG_LAYOUT && !win.mLayoutAttached) {
-                Slog.v(TAG, "1ST PASS " + win
-                        + ": gone=" + gone + " mHaveFrame=" + win.mHaveFrame
-                        + " mLayoutAttached=" + win.mLayoutAttached
-                        + " screen changed=" + win.isConfigChanged());
-                final AppWindowToken atoken = win.mAppToken;
-                if (gone) Slog.v(TAG, "  GONE: mViewVisibility="
-                        + win.mViewVisibility + " mRelayoutCalled="
-                        + win.mRelayoutCalled + " hidden="
-                        + win.mRootToken.hidden + " hiddenRequested="
-                        + (atoken != null && atoken.hiddenRequested)
-                        + " mAttachedHidden=" + win.mAttachedHidden);
-                else Slog.v(TAG, "  VIS: mViewVisibility="
-                        + win.mViewVisibility + " mRelayoutCalled="
-                        + win.mRelayoutCalled + " hidden="
-                        + win.mRootToken.hidden + " hiddenRequested="
-                        + (atoken != null && atoken.hiddenRequested)
-                        + " mAttachedHidden=" + win.mAttachedHidden);
-            }
-
-            // If this view is GONE, then skip it -- keep the current
-            // frame, and let the caller know so they can ignore it
-            // if they want.  (We do the normal layout for INVISIBLE
-            // windows, since that means "perform layout as normal,
-            // just don't display").
-            if (!gone || !win.mHaveFrame || win.mLayoutNeeded
-                    || ((win.isConfigChanged() || win.setInsetsChanged()) &&
-                            ((win.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
-                            (win.mHasSurface && win.mAppToken != null &&
-                            win.mAppToken.layoutConfigChanges)))) {
-                if (!win.mLayoutAttached) {
-                    if (initial) {
-                        //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
-                        win.mContentChanged = false;
-                    }
-                    if (win.mAttrs.type == TYPE_DREAM) {
-                        // Don't layout windows behind a dream, so that if it
-                        // does stuff like hide the status bar we won't get a
-                        // bad transition when it goes away.
-                        behindDream = true;
-                    }
-                    win.mLayoutNeeded = false;
-                    win.prelayout();
-                    mPolicy.layoutWindowLw(win, null);
-                    win.mLayoutSeq = seq;
-                    if (DEBUG_LAYOUT) Slog.v(TAG, "  LAYOUT: mFrame="
-                            + win.mFrame + " mContainingFrame="
-                            + win.mContainingFrame + " mDisplayFrame="
-                            + win.mDisplayFrame);
-                } else {
-                    if (topAttached < 0) topAttached = i;
-                }
-            }
-        }
-
-        boolean attachedBehindDream = false;
-
-        // Now perform layout of attached windows, which usually
-        // depend on the position of the window they are attached to.
-        // XXX does not deal with windows that are attached to windows
-        // that are themselves attached.
-        for (i = topAttached; i >= 0; i--) {
-            final WindowState win = windows.get(i);
-
-            if (win.mLayoutAttached) {
-                if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + win
-                        + " mHaveFrame=" + win.mHaveFrame
-                        + " mViewVisibility=" + win.mViewVisibility
-                        + " mRelayoutCalled=" + win.mRelayoutCalled);
-                // If this view is GONE, then skip it -- keep the current
-                // frame, and let the caller know so they can ignore it
-                // if they want.  (We do the normal layout for INVISIBLE
-                // windows, since that means "perform layout as normal,
-                // just don't display").
-                if (attachedBehindDream && mPolicy.canBeForceHidden(win, win.mAttrs)) {
-                    continue;
-                }
-                if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
-                        || !win.mHaveFrame || win.mLayoutNeeded) {
-                    if (initial) {
-                        //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
-                        win.mContentChanged = false;
-                    }
-                    win.mLayoutNeeded = false;
-                    win.prelayout();
-                    mPolicy.layoutWindowLw(win, win.mAttachedWindow);
-                    win.mLayoutSeq = seq;
-                    if (DEBUG_LAYOUT) Slog.v(TAG, "  LAYOUT: mFrame="
-                            + win.mFrame + " mContainingFrame="
-                            + win.mContainingFrame + " mDisplayFrame="
-                            + win.mDisplayFrame);
-                }
-            } else if (win.mAttrs.type == TYPE_DREAM) {
-                // Don't layout windows behind a dream, so that if it
-                // does stuff like hide the status bar we won't get a
-                // bad transition when it goes away.
-                attachedBehindDream = behindDream;
-            }
-        }
-
-        // Window frames may have changed.  Tell the input dispatcher about it.
-        mInputMonitor.setUpdateInputWindowsNeededLw();
-        if (updateInputWindows) {
-            mInputMonitor.updateInputWindowsLw(false /*force*/);
-        }
-
-        mPolicy.finishLayoutLw();
-    }
-
     void makeWindowFreezingScreenIfNeededLocked(WindowState w) {
         // If the screen is currently frozen or off, then keep
         // it frozen/off until this window draws at its new
@@ -8529,7 +8240,7 @@
             if (DEBUG_ORIENTATION) Slog.v(TAG, "Changing surface while display frozen: " + w);
             w.mOrientationChanging = true;
             w.mLastFreezeDuration = 0;
-            mInnerFields.mOrientationChangeComplete = false;
+            mWindowPlacerLocked.mOrientationChangeComplete = false;
             if (mWindowsFreezingScreen == WINDOWS_FREEZING_SCREENS_NONE) {
                 mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
                 // XXX should probably keep timeout from
@@ -8542,411 +8253,9 @@
     }
 
     /**
-     * Extracted from {@link #performLayoutAndPlaceSurfacesLockedInner} to reduce size of method.
-     * @param windows List of windows on default display.
      * @return bitmap indicating if another pass through layout must be made.
      */
-    public int handleAppTransitionReadyLocked(WindowList windows) {
-        int appsCount = mOpeningApps.size();
-        if (!checkIfTransitionGoodToGo(appsCount)) {
-            return 0;
-        }
-        if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "**** GOOD TO GO");
-        int transit = mAppTransition.getAppTransition();
-        if (mSkipAppTransitionAnimation) {
-            transit = AppTransition.TRANSIT_UNSET;
-        }
-        mSkipAppTransitionAnimation = false;
-        mNoAnimationNotifyOnTransitionFinished.clear();
-
-        mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
-
-        rebuildAppWindowListLocked();
-
-        mInnerFields.mWallpaperMayChange = false;
-
-        // The top-most window will supply the layout params,
-        // and we will determine it below.
-        LayoutParams animLp = null;
-        int bestAnimLayer = -1;
-        boolean fullscreenAnim = false;
-        boolean voiceInteraction = false;
-
-        final WindowState lowerWallpaperTarget =
-                mWallpaperControllerLocked.getLowerWallpaperTarget();
-        final WindowState upperWallpaperTarget =
-                mWallpaperControllerLocked.getUpperWallpaperTarget();
-
-        boolean openingAppHasWallpaper = false;
-        boolean closingAppHasWallpaper = false;
-        final AppWindowToken lowerWallpaperAppToken;
-        final AppWindowToken upperWallpaperAppToken;
-        if (lowerWallpaperTarget == null) {
-            lowerWallpaperAppToken = upperWallpaperAppToken = null;
-        } else {
-            lowerWallpaperAppToken = lowerWallpaperTarget.mAppToken;
-            upperWallpaperAppToken = upperWallpaperTarget.mAppToken;
-        }
-
-        int i;
-        // Do a first pass through the tokens for two
-        // things:
-        // (1) Determine if both the closing and opening
-        // app token sets are wallpaper targets, in which
-        // case special animations are needed
-        // (since the wallpaper needs to stay static
-        // behind them).
-        // (2) Find the layout params of the top-most
-        // application window in the tokens, which is
-        // what will control the animation theme.
-        final int closingAppsCount = mClosingApps.size();
-        appsCount = closingAppsCount + mOpeningApps.size();
-        for (i = 0; i < appsCount; i++) {
-            final AppWindowToken wtoken;
-            if (i < closingAppsCount) {
-                wtoken = mClosingApps.valueAt(i);
-                if (wtoken == lowerWallpaperAppToken || wtoken == upperWallpaperAppToken) {
-                    closingAppHasWallpaper = true;
-                }
-            } else {
-                wtoken = mOpeningApps.valueAt(i - closingAppsCount);
-                if (wtoken == lowerWallpaperAppToken || wtoken == upperWallpaperAppToken) {
-                    openingAppHasWallpaper = true;
-                }
-            }
-
-            voiceInteraction |= wtoken.voiceInteraction;
-
-            if (wtoken.appFullscreen) {
-                WindowState ws = wtoken.findMainWindow();
-                if (ws != null) {
-                    animLp = ws.mAttrs;
-                    bestAnimLayer = ws.mLayer;
-                    fullscreenAnim = true;
-                }
-            } else if (!fullscreenAnim) {
-                WindowState ws = wtoken.findMainWindow();
-                if (ws != null) {
-                    if (ws.mLayer > bestAnimLayer) {
-                        animLp = ws.mAttrs;
-                        bestAnimLayer = ws.mLayer;
-                    }
-                }
-            }
-        }
-
-        transit = maybeUpdateTransitToWallpaper(transit, openingAppHasWallpaper,
-                closingAppHasWallpaper, lowerWallpaperTarget, upperWallpaperTarget);
-
-        // If all closing windows are obscured, then there is
-        // no need to do an animation.  This is the case, for
-        // example, when this transition is being done behind
-        // the lock screen.
-        if (!mPolicy.allowAppAnimationsLw()) {
-            if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
-                    "Animations disallowed by keyguard or dream.");
-            animLp = null;
-        }
-
-        processApplicationsAnimatingInPlace(transit);
-
-        AppWindowToken topClosingApp = null;
-        int topClosingLayer = 0;
-        appsCount = mClosingApps.size();
-        for (i = 0; i < appsCount; i++) {
-            AppWindowToken wtoken = mClosingApps.valueAt(i);
-            final AppWindowAnimator appAnimator = wtoken.mAppAnimator;
-            if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "Now closing app " + wtoken);
-            appAnimator.clearThumbnail();
-            appAnimator.animation = null;
-            wtoken.inPendingTransaction = false;
-            setTokenVisibilityLocked(wtoken, animLp, false, transit, false, voiceInteraction);
-            wtoken.updateReportedVisibilityLocked();
-            // Force the allDrawn flag, because we want to start
-            // this guy's animations regardless of whether it's
-            // gotten drawn.
-            wtoken.allDrawn = true;
-            wtoken.deferClearAllDrawn = false;
-            // Ensure that apps that are mid-starting are also scheduled to have their
-            // starting windows removed after the animation is complete
-            if (wtoken.startingWindow != null && !wtoken.startingWindow.mExiting) {
-                scheduleRemoveStartingWindowLocked(wtoken);
-            }
-            mAnimator.mAppWindowAnimating |= appAnimator.isAnimating();
-
-            if (animLp != null) {
-                int layer = -1;
-                for (int j = 0; j < wtoken.windows.size(); j++) {
-                    WindowState win = wtoken.windows.get(j);
-                    if (win.mWinAnimator.mAnimLayer > layer) {
-                        layer = win.mWinAnimator.mAnimLayer;
-                    }
-                }
-                if (topClosingApp == null || layer > topClosingLayer) {
-                    topClosingApp = wtoken;
-                    topClosingLayer = layer;
-                }
-            }
-        }
-
-        AppWindowToken topOpeningApp = null;
-        appsCount = mOpeningApps.size();
-        for (i = 0; i < appsCount; i++) {
-            AppWindowToken wtoken = mOpeningApps.valueAt(i);
-            final AppWindowAnimator appAnimator = wtoken.mAppAnimator;
-            if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "Now opening app" + wtoken);
-
-            if (!appAnimator.usingTransferredAnimation) {
-                appAnimator.clearThumbnail();
-                appAnimator.animation = null;
-            }
-            wtoken.inPendingTransaction = false;
-            if (!setTokenVisibilityLocked(
-                    wtoken, animLp, true, transit, false, voiceInteraction)){
-                // This token isn't going to be animating. Add it to the list of tokens to
-                // be notified of app transition complete since the notification will not be
-                // sent be the app window animator.
-                mNoAnimationNotifyOnTransitionFinished.add(wtoken.token);
-            }
-            wtoken.updateReportedVisibilityLocked();
-            wtoken.waitingToShow = false;
-
-            appAnimator.mAllAppWinAnimators.clear();
-            final int windowsCount = wtoken.allAppWindows.size();
-            for (int j = 0; j < windowsCount; j++) {
-                appAnimator.mAllAppWinAnimators.add(wtoken.allAppWindows.get(j).mWinAnimator);
-            }
-            mAnimator.mAnimating |= appAnimator.showAllWindowsLocked();
-            mAnimator.mAppWindowAnimating |= appAnimator.isAnimating();
-
-            int topOpeningLayer = 0;
-            if (animLp != null) {
-                int layer = -1;
-                for (int j = 0; j < wtoken.windows.size(); j++) {
-                    WindowState win = wtoken.windows.get(j);
-                    if (win.mWinAnimator.mAnimLayer > layer) {
-                        layer = win.mWinAnimator.mAnimLayer;
-                    }
-                }
-                if (topOpeningApp == null || layer > topOpeningLayer) {
-                    topOpeningApp = wtoken;
-                    topOpeningLayer = layer;
-                }
-            }
-            createThumbnailAppAnimator(transit, wtoken, topOpeningLayer, topClosingLayer);
-        }
-
-        AppWindowAnimator openingAppAnimator = (topOpeningApp == null) ?  null :
-                topOpeningApp.mAppAnimator;
-        AppWindowAnimator closingAppAnimator = (topClosingApp == null) ? null :
-                topClosingApp.mAppAnimator;
-
-        mAppTransition.goodToGo(openingAppAnimator, closingAppAnimator);
-        mAppTransition.postAnimationCallback();
-        mAppTransition.clear();
-
-        mOpeningApps.clear();
-        mClosingApps.clear();
-
-        // This has changed the visibility of windows, so perform
-        // a new layout to get them all up-to-date.
-        getDefaultDisplayContentLocked().layoutNeeded = true;
-
-        // TODO(multidisplay): IMEs are only supported on the default display.
-        if (windows == getDefaultWindowListLocked()
-                && !moveInputMethodWindowsIfNeededLocked(true)) {
-            assignLayersLocked(windows);
-        }
-        updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES, true /*updateInputWindows*/);
-        mFocusMayChange = false;
-        notifyActivityDrawnForKeyguard();
-        return WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT
-                | WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
-
-    }
-
-    private int maybeUpdateTransitToWallpaper(int transit, boolean openingAppHasWallpaper,
-            boolean closingAppHasWallpaper, WindowState lowerWallpaperTarget,
-            WindowState upperWallpaperTarget) {
-        // if wallpaper is animating in or out set oldWallpaper to null else to wallpaper
-        final WindowState wallpaperTarget = mWallpaperControllerLocked.getWallpaperTarget();
-        final WindowState oldWallpaper = mWallpaperControllerLocked.isWallpaperTargetAnimating()
-                ? null : wallpaperTarget;
-        if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
-                "New wallpaper target=" + wallpaperTarget
-                        + ", oldWallpaper=" + oldWallpaper
-                        + ", lower target=" + lowerWallpaperTarget
-                        + ", upper target=" + upperWallpaperTarget);
-        mAnimateWallpaperWithTarget = false;
-        if (closingAppHasWallpaper && openingAppHasWallpaper) {
-            if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "Wallpaper animation!");
-            switch (transit) {
-                case AppTransition.TRANSIT_ACTIVITY_OPEN:
-                case AppTransition.TRANSIT_TASK_OPEN:
-                case AppTransition.TRANSIT_TASK_TO_FRONT:
-                    transit = AppTransition.TRANSIT_WALLPAPER_INTRA_OPEN;
-                    break;
-                case AppTransition.TRANSIT_ACTIVITY_CLOSE:
-                case AppTransition.TRANSIT_TASK_CLOSE:
-                case AppTransition.TRANSIT_TASK_TO_BACK:
-                    transit = AppTransition.TRANSIT_WALLPAPER_INTRA_CLOSE;
-                    break;
-            }
-            if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
-                    "New transit: " + AppTransition.appTransitionToString(transit));
-        } else if ((oldWallpaper != null) && !mOpeningApps.isEmpty()
-                && !mOpeningApps.contains(oldWallpaper.mAppToken)) {
-            // We are transitioning from an activity with
-            // a wallpaper to one without.
-            transit = AppTransition.TRANSIT_WALLPAPER_CLOSE;
-            if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
-                    "New transit away from wallpaper: "
-                    + AppTransition.appTransitionToString(transit));
-        } else if (wallpaperTarget != null && wallpaperTarget.isVisibleLw()) {
-            // We are transitioning from an activity without
-            // a wallpaper to now showing the wallpaper
-            transit = AppTransition.TRANSIT_WALLPAPER_OPEN;
-            if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
-                    "New transit into wallpaper: "
-                    + AppTransition.appTransitionToString(transit));
-        } else {
-            mAnimateWallpaperWithTarget = true;
-        }
-        return transit;
-    }
-
-    private void processApplicationsAnimatingInPlace(int transit) {
-        if (transit == AppTransition.TRANSIT_TASK_IN_PLACE) {
-            // Find the focused window
-            final WindowState win =
-                    findFocusedWindowLocked(getDefaultDisplayContentLocked());
-            if (win != null) {
-                final AppWindowToken wtoken = win.mAppToken;
-                final AppWindowAnimator appAnimator = wtoken.mAppAnimator;
-                if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "Now animating app in place " + wtoken);
-                appAnimator.clearThumbnail();
-                appAnimator.animation = null;
-                updateTokenInPlaceLocked(wtoken, transit);
-                wtoken.updateReportedVisibilityLocked();
-
-                appAnimator.mAllAppWinAnimators.clear();
-                final int N = wtoken.allAppWindows.size();
-                for (int j = 0; j < N; j++) {
-                    appAnimator.mAllAppWinAnimators.add(wtoken.allAppWindows.get(j).mWinAnimator);
-                }
-                mAnimator.mAppWindowAnimating |= appAnimator.isAnimating();
-                mAnimator.mAnimating |= appAnimator.showAllWindowsLocked();
-            }
-        }
-    }
-
-    private boolean checkIfTransitionGoodToGo(int appsCount) {
-        if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
-                "Checking " + appsCount + " opening apps (frozen="
-                        + mDisplayFrozen + " timeout="
-                        + mAppTransition.isTimeout() + ")...");
-        if (!mAppTransition.isTimeout()) {
-            for (int i = 0; i < appsCount; i++) {
-                AppWindowToken wtoken = mOpeningApps.valueAt(i);
-                if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
-                        "Check opening app=" + wtoken + ": allDrawn="
-                        + wtoken.allDrawn + " startingDisplayed="
-                        + wtoken.startingDisplayed + " startingMoved="
-                        + wtoken.startingMoved);
-                if (!wtoken.allDrawn && !wtoken.startingDisplayed && !wtoken.startingMoved) {
-                    return false;
-                }
-            }
-
-            // If the wallpaper is visible, we need to check it's ready too.
-            return !mWallpaperControllerLocked.isWallpaperVisible() ||
-                    mWallpaperControllerLocked.wallpaperTransitionReady();
-        }
-        return true;
-    }
-
-    private void createThumbnailAppAnimator(int transit, AppWindowToken appToken,
-            int openingLayer, int closingLayer) {
-        AppWindowAnimator openingAppAnimator = (appToken == null) ? null : appToken.mAppAnimator;
-        if (openingAppAnimator == null || openingAppAnimator.animation == null) {
-            return;
-        }
-        final int taskId = appToken.mTask.mTaskId;
-        Bitmap thumbnailHeader = mAppTransition.getAppTransitionThumbnailHeader(taskId);
-        if (thumbnailHeader == null || thumbnailHeader.getConfig() == Config.ALPHA_8) {
-            return;
-        }
-        // This thumbnail animation is very special, we need to have
-        // an extra surface with the thumbnail included with the animation.
-        Rect dirty = new Rect(0, 0, thumbnailHeader.getWidth(), thumbnailHeader.getHeight());
-        try {
-            // TODO(multi-display): support other displays
-            final DisplayContent displayContent = getDefaultDisplayContentLocked();
-            final Display display = displayContent.getDisplay();
-            final DisplayInfo displayInfo = displayContent.getDisplayInfo();
-
-            // Create a new surface for the thumbnail
-            SurfaceControl surfaceControl = new SurfaceControl(mFxSession,
-                    "thumbnail anim", dirty.width(), dirty.height(),
-                    PixelFormat.TRANSLUCENT, SurfaceControl.HIDDEN);
-            surfaceControl.setLayerStack(display.getLayerStack());
-            if (SHOW_TRANSACTIONS) {
-                Slog.i(TAG, "  THUMBNAIL " + surfaceControl + ": CREATE");
-            }
-
-            // Draw the thumbnail onto the surface
-            Surface drawSurface = new Surface();
-            drawSurface.copyFrom(surfaceControl);
-            Canvas c = drawSurface.lockCanvas(dirty);
-            c.drawBitmap(thumbnailHeader, 0, 0, null);
-            drawSurface.unlockCanvasAndPost(c);
-            drawSurface.release();
-
-            // Get the thumbnail animation
-            Animation anim;
-            if (mAppTransition.isNextThumbnailTransitionAspectScaled()) {
-                // If this is a multi-window scenario, we use the windows frame as
-                // destination of the thumbnail header animation. If this is a full screen
-                // window scenario, we use the whole display as the target.
-                WindowState win = appToken.findMainWindow();
-                Rect appRect = win != null ? win.getContentFrameLw() :
-                        new Rect(0, 0, displayInfo.appWidth, displayInfo.appHeight);
-                // For the new aspect-scaled transition, we want it to always show
-                // above the animating opening/closing window, and we want to
-                // synchronize its thumbnail surface with the surface for the
-                // open/close animation (only on the way down)
-                anim = mAppTransition.createThumbnailAspectScaleAnimationLocked(appRect,
-                        thumbnailHeader, taskId);
-                Log.d(TAG, "assigning thumbnail force above layer: " + openingLayer + " " +
-                        closingLayer);
-                openingAppAnimator.thumbnailForceAboveLayer = Math.max(openingLayer, closingLayer);
-                openingAppAnimator.deferThumbnailDestruction =
-                        !mAppTransition.isNextThumbnailTransitionScaleUp();
-            } else {
-                anim = mAppTransition.createThumbnailScaleAnimationLocked(
-                        displayInfo.appWidth, displayInfo.appHeight, transit, thumbnailHeader);
-            }
-            anim.restrictDuration(MAX_ANIMATION_DURATION);
-            anim.scaleCurrentDuration(getTransitionAnimationScaleLocked());
-
-            openingAppAnimator.thumbnail = surfaceControl;
-            openingAppAnimator.thumbnailLayer = openingLayer;
-            openingAppAnimator.thumbnailAnimation = anim;
-            mAppTransition.getNextAppTransitionStartRect(taskId, mTmpStartRect);
-            openingAppAnimator.thumbnailX = mTmpStartRect.left;
-            openingAppAnimator.thumbnailY = mTmpStartRect.top;
-        } catch (OutOfResourcesException e) {
-            Slog.e(TAG, "Can't allocate thumbnail/Canvas surface w=" + dirty.width()
-                    + " h=" + dirty.height(), e);
-            openingAppAnimator.clearThumbnail();
-        }
-    }
-
-    /**
-     * Extracted from {@link #performLayoutAndPlaceSurfacesLockedInner} to reduce size of method.
-     * @return bitmap indicating if another pass through layout must be made.
-     */
-    private int handleAnimatingStoppedAndTransitionLocked() {
+    int handleAnimatingStoppedAndTransitionLocked() {
         int changes = 0;
 
         mAppTransition.setIdle();
@@ -8976,7 +8285,7 @@
         if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
                 "Wallpaper layer changed: assigning layers + relayout");
         moveInputMethodWindowsIfNeededLocked(true);
-        mInnerFields.mWallpaperMayChange = true;
+        mWindowPlacerLocked.mWallpaperMayChange = true;
         // Since the window list has been rebuilt, focus might
         // have to be recomputed since the actual order of windows
         // might have changed again.
@@ -8985,7 +8294,7 @@
         return changes;
     }
 
-    private void updateResizingWindows(final WindowState w) {
+    void updateResizingWindows(final WindowState w) {
         final WindowStateAnimator winAnimator = w.mWinAnimator;
         if (w.mHasSurface && w.mLayoutSeq == mLayoutSeq) {
             w.setInsetsChanged();
@@ -9057,707 +8366,6 @@
         }
     }
 
-    /**
-     * Extracted from {@link #performLayoutAndPlaceSurfacesLockedInner} to reduce size of method.
-     * @param w WindowState this method is applied to.
-     * @param innerDw Width of app window.
-     * @param innerDh Height of app window.
-     */
-    private void handleNotObscuredLocked(final WindowState w,
-            final int innerDw, final int innerDh) {
-        final WindowManager.LayoutParams attrs = w.mAttrs;
-        final int attrFlags = attrs.flags;
-        final boolean canBeSeen = w.isDisplayedLw();
-        final boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
-
-        if (opaqueDrawn && w.isFullscreen(innerDw, innerDh)) {
-            // This window completely covers everything behind it,
-            // so we want to leave all of them as undimmed (for
-            // performance reasons).
-            mInnerFields.mObscured = true;
-        }
-
-        if (w.mHasSurface) {
-            if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
-                mInnerFields.mHoldScreen = w.mSession;
-            }
-            if (!mInnerFields.mSyswin && w.mAttrs.screenBrightness >= 0
-                    && mInnerFields.mScreenBrightness < 0) {
-                mInnerFields.mScreenBrightness = w.mAttrs.screenBrightness;
-            }
-            if (!mInnerFields.mSyswin && w.mAttrs.buttonBrightness >= 0
-                    && mInnerFields.mButtonBrightness < 0) {
-                mInnerFields.mButtonBrightness = w.mAttrs.buttonBrightness;
-            }
-            if (!mInnerFields.mSyswin && w.mAttrs.userActivityTimeout >= 0
-                    && mInnerFields.mUserActivityTimeout < 0) {
-                mInnerFields.mUserActivityTimeout = w.mAttrs.userActivityTimeout;
-            }
-
-            final int type = attrs.type;
-            if (canBeSeen
-                    && (type == TYPE_SYSTEM_DIALOG
-                     || type == TYPE_SYSTEM_ERROR
-                     || (attrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0)) {
-                mInnerFields.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) {
-                        mInnerFields.mObscureApplicationContentOnSecondaryDisplays = true;
-                    }
-                    mInnerFields.mDisplayHasContent = true;
-                } else if (displayContent != null &&
-                        (!mInnerFields.mObscureApplicationContentOnSecondaryDisplays
-                        || (mInnerFields.mObscured && type == TYPE_KEYGUARD_DIALOG))) {
-                    // Allow full screen keyguard presentation dialogs to be seen.
-                    mInnerFields.mDisplayHasContent = true;
-                }
-                if (mInnerFields.mPreferredRefreshRate == 0
-                        && w.mAttrs.preferredRefreshRate != 0) {
-                    mInnerFields.mPreferredRefreshRate = w.mAttrs.preferredRefreshRate;
-                }
-                if (mInnerFields.mPreferredModeId == 0
-                        && w.mAttrs.preferredDisplayModeId != 0) {
-                    mInnerFields.mPreferredModeId = w.mAttrs.preferredDisplayModeId;
-                }
-            }
-        }
-    }
-
-    private void handleFlagDimBehind(WindowState w) {
-        final WindowManager.LayoutParams attrs = w.mAttrs;
-        if ((attrs.flags & FLAG_DIM_BEHIND) != 0
-                && w.isDisplayedLw()
-                && !w.mExiting) {
-            final WindowStateAnimator winAnimator = w.mWinAnimator;
-            final Task task = w.getTask();
-            if (task == null) {
-                return;
-            }
-            task.setContinueDimming();
-            if (!task.isDimming(winAnimator)) {
-                if (localLOGV) Slog.v(TAG, "Win " + w + " start dimming.");
-                task.startDimmingIfNeeded(winAnimator);
-            }
-        }
-    }
-
-    private void updateAllDrawnLocked(DisplayContent displayContent) {
-        // See if any windows have been drawn, so they (and others
-        // associated with them) can now be shown.
-        ArrayList<TaskStack> stacks = displayContent.getStacks();
-        for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
-            final ArrayList<Task> tasks = stacks.get(stackNdx).getTasks();
-            for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
-                final AppTokenList tokens = tasks.get(taskNdx).mAppTokens;
-                for (int tokenNdx = tokens.size() - 1; tokenNdx >= 0; --tokenNdx) {
-                    final AppWindowToken wtoken = tokens.get(tokenNdx);
-                    if (!wtoken.allDrawn) {
-                        int numInteresting = wtoken.numInterestingWindows;
-                        if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
-                            if (DEBUG_VISIBILITY) Slog.v(TAG,
-                                    "allDrawn: " + wtoken
-                                    + " interesting=" + numInteresting
-                                    + " drawn=" + wtoken.numDrawnWindows);
-                            wtoken.allDrawn = true;
-                            // Force an additional layout pass where WindowStateAnimator#
-                            // commitFinishDrawingLocked() will call performShowLocked().
-                            displayContent.layoutNeeded = true;
-                            mH.obtainMessage(H.NOTIFY_ACTIVITY_DRAWN, wtoken.token).sendToTarget();
-                        }
-                    }
-                }
-            }
-        }
-    }
-
-    // "Something has changed!  Let's make it correct now."
-    private final void performLayoutAndPlaceSurfacesLockedInner(boolean recoveringMemory) {
-        if (DEBUG_WINDOW_TRACE) {
-            Slog.v(TAG, "performLayoutAndPlaceSurfacesLockedInner: entry. Called by "
-                    + Debug.getCallers(3));
-        }
-
-        int i;
-        boolean updateInputWindowsNeeded = false;
-
-        if (mFocusMayChange) {
-            mFocusMayChange = false;
-            updateInputWindowsNeeded = updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
-                    false /*updateInputWindows*/);
-        }
-
-        // Initialize state of exiting tokens.
-        final int numDisplays = mDisplayContents.size();
-        for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
-            final DisplayContent displayContent = mDisplayContents.valueAt(displayNdx);
-            for (i=displayContent.mExitingTokens.size()-1; i>=0; i--) {
-                displayContent.mExitingTokens.get(i).hasVisible = false;
-            }
-        }
-
-        for (int stackNdx = mStackIdToStack.size() - 1; stackNdx >= 0; --stackNdx) {
-            // Initialize state of exiting applications.
-            final AppTokenList exitingAppTokens =
-                    mStackIdToStack.valueAt(stackNdx).mExitingAppTokens;
-            for (int tokenNdx = exitingAppTokens.size() - 1; tokenNdx >= 0; --tokenNdx) {
-                exitingAppTokens.get(tokenNdx).hasVisible = false;
-            }
-        }
-
-        mInnerFields.mHoldScreen = null;
-        mInnerFields.mScreenBrightness = -1;
-        mInnerFields.mButtonBrightness = -1;
-        mInnerFields.mUserActivityTimeout = -1;
-        mInnerFields.mObscureApplicationContentOnSecondaryDisplays = false;
-
-        mTransactionSequence++;
-
-        final DisplayContent defaultDisplay = getDefaultDisplayContentLocked();
-        final DisplayInfo defaultInfo = defaultDisplay.getDisplayInfo();
-        final int defaultDw = defaultInfo.logicalWidth;
-        final int defaultDh = defaultInfo.logicalHeight;
-
-        if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
-                ">>> OPEN TRANSACTION performLayoutAndPlaceSurfaces");
-        SurfaceControl.openTransaction();
-        try {
-
-            if (mWatermark != null) {
-                mWatermark.positionSurface(defaultDw, defaultDh);
-            }
-            if (mStrictModeFlash != null) {
-                mStrictModeFlash.positionSurface(defaultDw, defaultDh);
-            }
-            if (mCircularDisplayMask != null) {
-                mCircularDisplayMask.positionSurface(defaultDw, defaultDh, mRotation);
-            }
-            if (mEmulatorDisplayOverlay != null) {
-                mEmulatorDisplayOverlay.positionSurface(defaultDw, defaultDh, mRotation);
-            }
-
-            boolean focusDisplayed = false;
-
-            for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
-                final DisplayContent displayContent = mDisplayContents.valueAt(displayNdx);
-                boolean updateAllDrawn = false;
-                WindowList windows = displayContent.getWindowList();
-                DisplayInfo displayInfo = displayContent.getDisplayInfo();
-                final int displayId = displayContent.getDisplayId();
-                final int dw = displayInfo.logicalWidth;
-                final int dh = displayInfo.logicalHeight;
-                final int innerDw = displayInfo.appWidth;
-                final int innerDh = displayInfo.appHeight;
-                final boolean isDefaultDisplay = (displayId == Display.DEFAULT_DISPLAY);
-
-                // Reset for each display.
-                mInnerFields.mDisplayHasContent = false;
-                mInnerFields.mPreferredRefreshRate = 0;
-                mInnerFields.mPreferredModeId = 0;
-
-                int repeats = 0;
-                do {
-                    repeats++;
-                    if (repeats > 6) {
-                        Slog.w(TAG, "Animation repeat aborted after too many iterations");
-                        displayContent.layoutNeeded = false;
-                        break;
-                    }
-
-                    if (DEBUG_LAYOUT_REPEATS) debugLayoutRepeats("On entry to LockedInner",
-                        displayContent.pendingLayoutChanges);
-
-                    if ((displayContent.pendingLayoutChanges &
-                            WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER) != 0 &&
-                            mWallpaperControllerLocked.adjustWallpaperWindows()) {
-                        assignLayersLocked(windows);
-                        displayContent.layoutNeeded = true;
-                    }
-
-                    if (isDefaultDisplay && (displayContent.pendingLayoutChanges
-                            & WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
-                        if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
-                        if (updateOrientationFromAppTokensLocked(true)) {
-                            displayContent.layoutNeeded = true;
-                            mH.sendEmptyMessage(H.SEND_NEW_CONFIGURATION);
-                        }
-                    }
-
-                    if ((displayContent.pendingLayoutChanges
-                            & WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT) != 0) {
-                        displayContent.layoutNeeded = true;
-                    }
-
-                    // FIRST LOOP: Perform a layout, if needed.
-                    if (repeats < LAYOUT_REPEAT_THRESHOLD) {
-                        performLayoutLockedInner(displayContent, repeats == 1,
-                                false /*updateInputWindows*/);
-                    } else {
-                        Slog.w(TAG, "Layout repeat skipped after too many iterations");
-                    }
-
-                    // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think
-                    // it is animating.
-                    displayContent.pendingLayoutChanges = 0;
-
-                    if (isDefaultDisplay) {
-                        mPolicy.beginPostLayoutPolicyLw(dw, dh);
-                        for (i = windows.size() - 1; i >= 0; i--) {
-                            WindowState w = windows.get(i);
-                            if (w.mHasSurface) {
-                                mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.mAttachedWindow);
-                            }
-                        }
-                        displayContent.pendingLayoutChanges |= mPolicy.finishPostLayoutPolicyLw();
-                        if (DEBUG_LAYOUT_REPEATS) debugLayoutRepeats(
-                            "after finishPostLayoutPolicyLw", displayContent.pendingLayoutChanges);
-                    }
-                } while (displayContent.pendingLayoutChanges != 0);
-
-                mInnerFields.mObscured = false;
-                mInnerFields.mSyswin = false;
-                displayContent.resetDimming();
-
-                // Only used if default window
-                final boolean someoneLosingFocus = !mLosingFocus.isEmpty();
-
-                final int N = windows.size();
-                for (i=N-1; i>=0; i--) {
-                    WindowState w = windows.get(i);
-                    final Task task = w.getTask();
-                    if (task == null && w.getAttrs().type != TYPE_PRIVATE_PRESENTATION) {
-                        continue;
-                    }
-
-                    final boolean obscuredChanged = w.mObscured != mInnerFields.mObscured;
-
-                    // Update effect.
-                    w.mObscured = mInnerFields.mObscured;
-                    if (!mInnerFields.mObscured) {
-                        handleNotObscuredLocked(w, innerDw, innerDh);
-                    }
-
-                    if (task != null && !task.getContinueDimming()) {
-                        handleFlagDimBehind(w);
-                    }
-
-                    if (isDefaultDisplay && obscuredChanged
-                            && mWallpaperControllerLocked.isWallpaperTarget(w) && w.isVisibleLw()) {
-                        // This is the wallpaper target and its obscured state
-                        // changed... make sure the current wallaper's visibility
-                        // has been updated accordingly.
-                        mWallpaperControllerLocked.updateWallpaperVisibility();
-                    }
-
-                    final WindowStateAnimator winAnimator = w.mWinAnimator;
-
-                    // If the window has moved due to its containing content frame changing, then
-                    // notify the listeners and optionally animate it.
-                    if (w.hasMoved()) {
-                        // Frame has moved, containing content frame has also moved, and we're not
-                        // currently animating... let's do something.
-                        final int left = w.mFrame.left;
-                        final int top = w.mFrame.top;
-                        if ((w.mAttrs.privateFlags & PRIVATE_FLAG_NO_MOVE_ANIMATION) == 0) {
-                            Animation a = AnimationUtils.loadAnimation(mContext,
-                                    com.android.internal.R.anim.window_move_from_decor);
-                            winAnimator.setAnimation(a);
-                            winAnimator.mAnimDw = w.mLastFrame.left - left;
-                            winAnimator.mAnimDh = w.mLastFrame.top - top;
-                            winAnimator.mAnimateMove = true;
-                            winAnimator.mAnimatingMove = true;
-                        }
-
-                        //TODO (multidisplay): Accessibility supported only for the default display.
-                        if (mAccessibilityController != null
-                                && displayId == Display.DEFAULT_DISPLAY) {
-                            mAccessibilityController.onSomeWindowResizedOrMovedLocked();
-                        }
-
-                        try {
-                            w.mClient.moved(left, top);
-                        } catch (RemoteException e) {
-                        }
-                    }
-
-                    //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
-                    w.mContentChanged = false;
-
-                    // Moved from updateWindowsAndWallpaperLocked().
-                    if (w.mHasSurface) {
-                        // Take care of the window being ready to display.
-                        final boolean committed =
-                                winAnimator.commitFinishDrawingLocked();
-                        if (isDefaultDisplay && committed) {
-                            if (w.mAttrs.type == TYPE_DREAM) {
-                                // HACK: When a dream is shown, it may at that
-                                // point hide the lock screen.  So we need to
-                                // redo the layout to let the phone window manager
-                                // make this happen.
-                                displayContent.pendingLayoutChanges |=
-                                        WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
-                                if (DEBUG_LAYOUT_REPEATS) {
-                                    debugLayoutRepeats(
-                                            "dream and commitFinishDrawingLocked true",
-                                            displayContent.pendingLayoutChanges);
-                                }
-                            }
-                            if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
-                                if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
-                                            "First draw done in potential wallpaper target " + w);
-                                mInnerFields.mWallpaperMayChange = true;
-                                displayContent.pendingLayoutChanges |=
-                                        WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
-                                if (DEBUG_LAYOUT_REPEATS) {
-                                    debugLayoutRepeats(
-                                            "wallpaper and commitFinishDrawingLocked true",
-                                            displayContent.pendingLayoutChanges);
-                                }
-                            }
-                        }
-
-                        winAnimator.setSurfaceBoundariesLocked(recoveringMemory);
-                    }
-
-                    final AppWindowToken atoken = w.mAppToken;
-                    if (DEBUG_STARTING_WINDOW && atoken != null
-                            && w == atoken.startingWindow) {
-                        Slog.d(TAG, "updateWindows: starting " + w + " isOnScreen="
-                            + w.isOnScreen() + " allDrawn=" + atoken.allDrawn
-                            + " freezingScreen=" + atoken.mAppAnimator.freezingScreen);
-                    }
-                    if (atoken != null
-                            && (!atoken.allDrawn || atoken.mAppAnimator.freezingScreen)) {
-                        if (atoken.lastTransactionSequence != mTransactionSequence) {
-                            atoken.lastTransactionSequence = mTransactionSequence;
-                            atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
-                            atoken.startingDisplayed = false;
-                        }
-                        if ((w.isOnScreenIgnoringKeyguard()
-                                || winAnimator.mAttrType == TYPE_BASE_APPLICATION)
-                                && !w.mExiting && !w.mDestroying) {
-                            if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
-                                Slog.v(TAG, "Eval win " + w + ": isDrawn=" + w.isDrawnLw()
-                                        + ", isAnimating=" + winAnimator.isAnimating());
-                                if (!w.isDrawnLw()) {
-                                    Slog.v(TAG, "Not displayed: s=" + winAnimator.mSurfaceControl
-                                            + " pv=" + w.mPolicyVisibility
-                                            + " mDrawState=" + winAnimator.drawStateToString()
-                                            + " ah=" + w.mAttachedHidden
-                                            + " th=" + atoken.hiddenRequested
-                                            + " a=" + winAnimator.mAnimating);
-                                }
-                            }
-                            if (w != atoken.startingWindow) {
-                                if (!atoken.mAppAnimator.freezingScreen || !w.mAppFreezing) {
-                                    atoken.numInterestingWindows++;
-                                    if (w.isDrawnLw()) {
-                                        atoken.numDrawnWindows++;
-                                        if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Slog.v(TAG,
-                                                "tokenMayBeDrawn: " + atoken
-                                                + " freezingScreen=" + atoken.mAppAnimator.freezingScreen
-                                                + " mAppFreezing=" + w.mAppFreezing);
-                                        updateAllDrawn = true;
-                                    }
-                                }
-                            } else if (w.isDrawnLw()) {
-                                atoken.startingDisplayed = true;
-                            }
-                        }
-                    }
-
-                    if (isDefaultDisplay && someoneLosingFocus && (w == mCurrentFocus)
-                            && w.isDisplayedLw()) {
-                        focusDisplayed = true;
-                    }
-
-                    updateResizingWindows(w);
-                }
-
-                mDisplayManagerInternal.setDisplayProperties(displayId,
-                        mInnerFields.mDisplayHasContent, mInnerFields.mPreferredRefreshRate,
-                        mInnerFields.mPreferredModeId,
-                        true /* inTraversal, must call performTraversalInTrans... below */);
-
-                getDisplayContentLocked(displayId).stopDimmingIfNeeded();
-
-                if (updateAllDrawn) {
-                    updateAllDrawnLocked(displayContent);
-                }
-            }
-
-            if (focusDisplayed) {
-                mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
-            }
-
-            // Give the display manager a chance to adjust properties
-            // like display rotation if it needs to.
-            mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
-
-        } catch (RuntimeException e) {
-            Slog.wtf(TAG, "Unhandled exception in Window Manager", e);
-        } finally {
-            SurfaceControl.closeTransaction();
-            if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
-                    "<<< CLOSE TRANSACTION performLayoutAndPlaceSurfaces");
-        }
-
-        final WindowList defaultWindows = defaultDisplay.getWindowList();
-
-        // If we are ready to perform an app transition, check through
-        // all of the app tokens to be shown and see if they are ready
-        // to go.
-        if (mAppTransition.isReady()) {
-            defaultDisplay.pendingLayoutChanges |= handleAppTransitionReadyLocked(defaultWindows);
-            if (DEBUG_LAYOUT_REPEATS) debugLayoutRepeats("after handleAppTransitionReadyLocked",
-                    defaultDisplay.pendingLayoutChanges);
-        }
-
-        if (!mAnimator.mAppWindowAnimating && mAppTransition.isRunning()) {
-            // We have finished the animation of an app transition.  To do
-            // this, we have delayed a lot of operations like showing and
-            // hiding apps, moving apps in Z-order, etc.  The app token list
-            // reflects the correct Z-order, but the window list may now
-            // be out of sync with it.  So here we will just rebuild the
-            // entire app window list.  Fun!
-            defaultDisplay.pendingLayoutChanges |= handleAnimatingStoppedAndTransitionLocked();
-            if (DEBUG_LAYOUT_REPEATS) debugLayoutRepeats("after handleAnimStopAndXitionLock",
-                defaultDisplay.pendingLayoutChanges);
-        }
-
-        if (mInnerFields.mWallpaperForceHidingChanged && defaultDisplay.pendingLayoutChanges == 0
-                && !mAppTransition.isReady()) {
-            // At this point, there was a window with a wallpaper that
-            // was force hiding other windows behind it, but now it
-            // is going away.  This may be simple -- just animate
-            // away the wallpaper and its window -- or it may be
-            // hard -- the wallpaper now needs to be shown behind
-            // something that was hidden.
-            defaultDisplay.pendingLayoutChanges |= WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
-            if (DEBUG_LAYOUT_REPEATS) debugLayoutRepeats("after animateAwayWallpaperLocked",
-                defaultDisplay.pendingLayoutChanges);
-        }
-        mInnerFields.mWallpaperForceHidingChanged = false;
-
-        if (mInnerFields.mWallpaperMayChange) {
-            if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, "Wallpaper may change!  Adjusting");
-            defaultDisplay.pendingLayoutChanges |=
-                    WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
-            if (DEBUG_LAYOUT_REPEATS) debugLayoutRepeats("WallpaperMayChange",
-                    defaultDisplay.pendingLayoutChanges);
-        }
-
-        if (mFocusMayChange) {
-            mFocusMayChange = false;
-            if (updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES,
-                    false /*updateInputWindows*/)) {
-                updateInputWindowsNeeded = true;
-                defaultDisplay.pendingLayoutChanges |= WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
-            }
-        }
-
-        if (needsLayout()) {
-            defaultDisplay.pendingLayoutChanges |= WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
-            if (DEBUG_LAYOUT_REPEATS) debugLayoutRepeats("mLayoutNeeded",
-                    defaultDisplay.pendingLayoutChanges);
-        }
-
-        for (i = mResizingWindows.size() - 1; i >= 0; i--) {
-            WindowState win = mResizingWindows.get(i);
-            if (win.mAppFreezing) {
-                // Don't remove this window until rotation has completed.
-                continue;
-            }
-            win.reportResized();
-            mResizingWindows.remove(i);
-        }
-
-        if (DEBUG_ORIENTATION && mDisplayFrozen) Slog.v(TAG,
-                "With display frozen, orientationChangeComplete="
-                + mInnerFields.mOrientationChangeComplete);
-        if (mInnerFields.mOrientationChangeComplete) {
-            if (mWindowsFreezingScreen != WINDOWS_FREEZING_SCREENS_NONE) {
-                mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_NONE;
-                mLastFinishedFreezeSource = mInnerFields.mLastWindowFreezeSource;
-                mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
-            }
-            stopFreezingDisplayLocked();
-        }
-
-        // Destroy the surface of any windows that are no longer visible.
-        boolean wallpaperDestroyed = false;
-        i = mDestroySurface.size();
-        if (i > 0) {
-            do {
-                i--;
-                WindowState win = mDestroySurface.get(i);
-                win.mDestroying = false;
-                if (mInputMethodWindow == win) {
-                    mInputMethodWindow = null;
-                }
-                if (mWallpaperControllerLocked.isWallpaperTarget(win)) {
-                    wallpaperDestroyed = true;
-                }
-                win.mWinAnimator.destroySurfaceLocked();
-            } while (i > 0);
-            mDestroySurface.clear();
-        }
-
-        // Time to remove any exiting tokens?
-        for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
-            final DisplayContent displayContent = mDisplayContents.valueAt(displayNdx);
-            ArrayList<WindowToken> exitingTokens = displayContent.mExitingTokens;
-            for (i = exitingTokens.size() - 1; i >= 0; i--) {
-                WindowToken token = exitingTokens.get(i);
-                if (!token.hasVisible) {
-                    exitingTokens.remove(i);
-                    if (token.windowType == TYPE_WALLPAPER) {
-                        mWallpaperControllerLocked.removeWallpaperToken(token);
-                    }
-                }
-            }
-        }
-
-        // Time to remove any exiting applications?
-        for (int stackNdx = mStackIdToStack.size() - 1; stackNdx >= 0; --stackNdx) {
-            // Initialize state of exiting applications.
-            final AppTokenList exitingAppTokens =
-                    mStackIdToStack.valueAt(stackNdx).mExitingAppTokens;
-            for (i = exitingAppTokens.size() - 1; i >= 0; i--) {
-                AppWindowToken token = exitingAppTokens.get(i);
-                if (!token.hasVisible && !mClosingApps.contains(token) &&
-                        (!token.mIsExiting || token.allAppWindows.isEmpty())) {
-                    // Make sure there is no animation running on this token,
-                    // so any windows associated with it will be removed as
-                    // soon as their animations are complete
-                    token.mAppAnimator.clearAnimation();
-                    token.mAppAnimator.animating = false;
-                    if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
-                            "performLayout: App token exiting now removed" + token);
-                    token.removeAppFromTaskLocked();
-                }
-            }
-        }
-
-        if (wallpaperDestroyed) {
-            defaultDisplay.pendingLayoutChanges |=
-                    WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
-            defaultDisplay.layoutNeeded = true;
-        }
-
-        for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
-            final DisplayContent displayContent = mDisplayContents.valueAt(displayNdx);
-            if (displayContent.pendingLayoutChanges != 0) {
-                displayContent.layoutNeeded = true;
-            }
-        }
-
-        // Finally update all input windows now that the window changes have stabilized.
-        mInputMonitor.updateInputWindowsLw(true /*force*/);
-
-        setHoldScreenLocked(mInnerFields.mHoldScreen);
-        if (!mDisplayFrozen) {
-            if (mInnerFields.mScreenBrightness < 0 || mInnerFields.mScreenBrightness > 1.0f) {
-                mPowerManagerInternal.setScreenBrightnessOverrideFromWindowManager(-1);
-            } else {
-                mPowerManagerInternal.setScreenBrightnessOverrideFromWindowManager(
-                        toBrightnessOverride(mInnerFields.mScreenBrightness));
-            }
-            if (mInnerFields.mButtonBrightness < 0 || mInnerFields.mButtonBrightness > 1.0f) {
-                mPowerManagerInternal.setButtonBrightnessOverrideFromWindowManager(-1);
-            } else {
-                mPowerManagerInternal.setButtonBrightnessOverrideFromWindowManager(
-                        toBrightnessOverride(mInnerFields.mButtonBrightness));
-            }
-            mPowerManagerInternal.setUserActivityTimeoutOverrideFromWindowManager(
-                    mInnerFields.mUserActivityTimeout);
-        }
-
-        if (mTurnOnScreen) {
-            if (mAllowTheaterModeWakeFromLayout
-                    || Settings.Global.getInt(mContext.getContentResolver(),
-                        Settings.Global.THEATER_MODE_ON, 0) == 0) {
-                if (DEBUG_VISIBILITY || DEBUG_POWER) {
-                    Slog.v(TAG, "Turning screen on after layout!");
-                }
-                mPowerManager.wakeUp(SystemClock.uptimeMillis(), "android.server.wm:TURN_ON");
-            }
-            mTurnOnScreen = false;
-        }
-
-        if (mInnerFields.mUpdateRotation) {
-            if (DEBUG_ORIENTATION) Slog.d(TAG, "Performing post-rotate rotation");
-            if (updateRotationUncheckedLocked(false)) {
-                mH.sendEmptyMessage(H.SEND_NEW_CONFIGURATION);
-            } else {
-                mInnerFields.mUpdateRotation = false;
-            }
-        }
-
-        if (mWaitingForDrawnCallback != null ||
-                (mInnerFields.mOrientationChangeComplete && !defaultDisplay.layoutNeeded &&
-                        !mInnerFields.mUpdateRotation)) {
-            checkDrawnWindowsLocked();
-        }
-
-        final int N = mPendingRemove.size();
-        if (N > 0) {
-            if (mPendingRemoveTmp.length < N) {
-                mPendingRemoveTmp = new WindowState[N+10];
-            }
-            mPendingRemove.toArray(mPendingRemoveTmp);
-            mPendingRemove.clear();
-            DisplayContentList displayList = new DisplayContentList();
-            for (i = 0; i < N; i++) {
-                WindowState w = mPendingRemoveTmp[i];
-                removeWindowInnerLocked(w);
-                final DisplayContent displayContent = w.getDisplayContent();
-                if (displayContent != null && !displayList.contains(displayContent)) {
-                    displayList.add(displayContent);
-                }
-            }
-
-            for (DisplayContent displayContent : displayList) {
-                assignLayersLocked(displayContent.getWindowList());
-                displayContent.layoutNeeded = true;
-            }
-        }
-
-        // Remove all deferred displays stacks, tasks, and activities.
-        for (int displayNdx = mDisplayContents.size() - 1; displayNdx >= 0; --displayNdx) {
-            mDisplayContents.valueAt(displayNdx).checkForDeferredActions();
-        }
-
-        if (updateInputWindowsNeeded) {
-            mInputMonitor.updateInputWindowsLw(false /*force*/);
-        }
-        setFocusTaskRegion();
-
-        // Check to see if we are now in a state where the screen should
-        // be enabled, because the window obscured flags have changed.
-        enableScreenIfNeededLocked();
-
-        scheduleAnimationLocked();
-
-        if (DEBUG_WINDOW_TRACE) {
-            Slog.e(TAG, "performLayoutAndPlaceSurfacesLockedInner exit: animating="
-                    + mAnimator.mAnimating);
-        }
-    }
-
-    private int toBrightnessOverride(float value) {
-        return (int)(value * PowerManager.BRIGHTNESS_ON);
-    }
-
     void checkDrawnWindowsLocked() {
         if (mWaitingForDrawn.isEmpty() || mWaitingForDrawnCallback == null) {
             return;
@@ -9826,7 +8434,7 @@
         }
     }
 
-    private boolean needsLayout() {
+    boolean needsLayout() {
         final int numDisplays = mDisplayContents.size();
         for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
             final DisplayContent displayContent = mDisplayContents.valueAt(displayNdx);
@@ -9837,41 +8445,6 @@
         return false;
     }
 
-    boolean copyAnimToLayoutParamsLocked() {
-        boolean doRequest = false;
-
-        final int bulkUpdateParams = mAnimator.mBulkUpdateParams;
-        if ((bulkUpdateParams & LayoutFields.SET_UPDATE_ROTATION) != 0) {
-            mInnerFields.mUpdateRotation = true;
-            doRequest = true;
-        }
-        if ((bulkUpdateParams & LayoutFields.SET_WALLPAPER_MAY_CHANGE) != 0) {
-            mInnerFields.mWallpaperMayChange = true;
-            doRequest = true;
-        }
-        if ((bulkUpdateParams & LayoutFields.SET_FORCE_HIDING_CHANGED) != 0) {
-            mInnerFields.mWallpaperForceHidingChanged = true;
-            doRequest = true;
-        }
-        if ((bulkUpdateParams & LayoutFields.SET_ORIENTATION_CHANGE_COMPLETE) == 0) {
-            mInnerFields.mOrientationChangeComplete = false;
-        } else {
-            mInnerFields.mOrientationChangeComplete = true;
-            mInnerFields.mLastWindowFreezeSource = mAnimator.mLastWindowFreezeSource;
-            if (mWindowsFreezingScreen != WINDOWS_FREEZING_SCREENS_NONE) {
-                doRequest = true;
-            }
-        }
-        if ((bulkUpdateParams & LayoutFields.SET_TURN_ON_SCREEN) != 0) {
-            mTurnOnScreen = true;
-        }
-        if ((bulkUpdateParams & LayoutFields.SET_WALLPAPER_ACTION_PENDING) != 0) {
-            mInnerFields.mWallpaperActionPending = true;
-        }
-
-        return doRequest;
-    }
-
     /** If a window that has an animation specifying a colored background and the current wallpaper
      * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
      * suddenly disappear. */
@@ -9995,7 +8568,7 @@
         return leakedSurface || killedApps;
     }
 
-    private boolean updateFocusedWindowLocked(int mode, boolean updateInputWindows) {
+    boolean updateFocusedWindowLocked(int mode, boolean updateInputWindows) {
         WindowState newFocus = computeFocusedWindowLocked();
         if (mCurrentFocus != newFocus) {
             Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "wmUpdateFocus");
@@ -10024,7 +8597,8 @@
             if (imWindowChanged && oldFocus != mInputMethodWindow) {
                 // Focus of the input method window changed. Perform layout if needed.
                 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
-                    performLayoutLockedInner(displayContent, true /*initial*/, updateInputWindows);
+                    mWindowPlacerLocked.performLayoutLockedInner(displayContent, true /*initial*/,
+                            updateInputWindows);
                     focusChanged &= ~WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
                 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
                     // Client will do the layout, but we need to assign layers
@@ -10037,7 +8611,8 @@
                 // The change in focus caused us to need to do a layout.  Okay.
                 displayContent.layoutNeeded = true;
                 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
-                    performLayoutLockedInner(displayContent, true /*initial*/, updateInputWindows);
+                    mWindowPlacerLocked.performLayoutLockedInner(displayContent, true /*initial*/,
+                            updateInputWindows);
                 }
             }
 
@@ -10065,7 +8640,7 @@
         return null;
     }
 
-    private WindowState findFocusedWindowLocked(DisplayContent displayContent) {
+    WindowState findFocusedWindowLocked(DisplayContent displayContent) {
         final WindowList windows = displayContent.getWindowList();
         for (int i = windows.size() - 1; i >= 0; i--) {
             final WindowState win = windows.get(i);
@@ -10189,7 +8764,7 @@
         }
     }
 
-    private void stopFreezingDisplayLocked() {
+    void stopFreezingDisplayLocked() {
         if (!mDisplayFrozen) {
             return;
         }
@@ -10396,7 +8971,7 @@
         synchronized (mWindowMap) {
             int visibility = mPolicy.adjustSystemUiVisibilityLw(mLastStatusBarVisibility);
             updateStatusBarVisibilityLocked(visibility);
-            performLayoutAndPlaceSurfacesLocked();
+            mWindowPlacerLocked.performSurfacePlacement();
         }
     }
 
@@ -11010,13 +9585,6 @@
         synchronized (mWindowMap) { }
     }
 
-    void debugLayoutRepeats(final String msg, int pendingLayoutChanges) {
-        if (mLayoutRepeatCount >= LAYOUT_REPEAT_THRESHOLD) {
-            Slog.v(TAG, "Layouts looping: " + msg + ", mPendingLayoutChanges = 0x" +
-                    Integer.toHexString(pendingLayoutChanges));
-        }
-    }
-
     private DisplayContent newDisplayContentLocked(final Display display) {
         DisplayContent displayContent = new DisplayContent(display, this);
         final int displayId = display.getDisplayId();
@@ -11155,6 +9723,17 @@
         return mWindowMap;
     }
 
+    public void setReplacingWindow(IBinder token) {
+        synchronized (mWindowMap) {
+            AppWindowToken appWindowToken = findAppWindowToken(token);
+            if (appWindowToken == null) {
+                Slog.w(TAG, "Attempted to set replacing window on non-existing app token " + token);
+                return;
+            }
+            appWindowToken.mReplacingWindow = true;
+        }
+    }
+
     private final class LocalService extends WindowManagerInternal {
         @Override
         public void requestTraversalFromDisplayManager() {
diff --git a/services/core/java/com/android/server/wm/WindowState.java b/services/core/java/com/android/server/wm/WindowState.java
index 761e5eb..49d260e 100644
--- a/services/core/java/com/android/server/wm/WindowState.java
+++ b/services/core/java/com/android/server/wm/WindowState.java
@@ -18,6 +18,7 @@
 
 import static android.app.ActivityManager.FREEFORM_WORKSPACE_STACK_ID;
 import static android.view.WindowManager.LayoutParams.FIRST_SUB_WINDOW;
+import static android.view.WindowManager.LayoutParams.FLAG_DIM_BEHIND;
 import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
 import static android.view.WindowManager.LayoutParams.LAST_SUB_WINDOW;
 import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_COMPATIBLE_WINDOW;
@@ -1255,6 +1256,33 @@
         mInputWindowHandle.inputChannel = null;
     }
 
+    void handleFlagDimBehind() {
+        if ((mAttrs.flags & FLAG_DIM_BEHIND) != 0 && isDisplayedLw() && !mExiting) {
+            final Task task = getTask();
+            if (task == null) {
+                return;
+            }
+            task.setContinueDimming();
+            if (!task.isDimming(mWinAnimator)) {
+                if (WindowManagerService.localLOGV) Slog.v(TAG, "Win " + this + " start dimming.");
+                task.startDimmingIfNeeded(mWinAnimator);
+            }
+        }
+    }
+
+    void maybeRemoveReplacedWindow() {
+        AppWindowToken token = mAppToken;
+        if (token != null && token.mReplacingWindow) {
+            token.mReplacingWindow = false;
+            for (int i = token.allAppWindows.size() - 1; i >= 0; i--) {
+                WindowState win = token.allAppWindows.get(i);
+                if (win.mExiting) {
+                    mService.removeWindowInnerLocked(win);
+                }
+            }
+        }
+    }
+
     private class DeathRecipient implements IBinder.DeathRecipient {
         @Override
         public void binderDied() {
diff --git a/services/core/java/com/android/server/wm/WindowStateAnimator.java b/services/core/java/com/android/server/wm/WindowStateAnimator.java
index 109e627..efad8bf 100644
--- a/services/core/java/com/android/server/wm/WindowStateAnimator.java
+++ b/services/core/java/com/android/server/wm/WindowStateAnimator.java
@@ -27,8 +27,8 @@
 import static com.android.server.wm.WindowManagerService.SHOW_LIGHT_TRANSACTIONS;
 import static com.android.server.wm.WindowManagerService.SHOW_SURFACE_ALLOC;
 import static com.android.server.wm.WindowManagerService.localLOGV;
-import static com.android.server.wm.WindowManagerService.LayoutFields.SET_ORIENTATION_CHANGE_COMPLETE;
-import static com.android.server.wm.WindowManagerService.LayoutFields.SET_TURN_ON_SCREEN;
+import static com.android.server.wm.WindowSurfacePlacer.SET_ORIENTATION_CHANGE_COMPLETE;
+import static com.android.server.wm.WindowSurfacePlacer.SET_TURN_ON_SCREEN;
 
 import android.content.Context;
 import android.graphics.Matrix;
@@ -424,8 +424,9 @@
         finishExit();
         final int displayId = mWin.getDisplayId();
         mAnimator.setPendingLayoutChanges(displayId, WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM);
-        if (WindowManagerService.DEBUG_LAYOUT_REPEATS) mService.debugLayoutRepeats(
-                "WindowStateAnimator", mAnimator.getPendingLayoutChanges(displayId));
+        if (WindowManagerService.DEBUG_LAYOUT_REPEATS)
+            mService.mWindowPlacerLocked.debugLayoutRepeats(
+                    "WindowStateAnimator", mAnimator.getPendingLayoutChanges(displayId));
 
         if (mWin.mAppToken != null) {
             mWin.mAppToken.updateReportedVisibilityLocked();
@@ -1184,7 +1185,7 @@
                     + " screen=" + (screenAnimation ?
                             screenRotationAnimation.getEnterTransformation().getAlpha() : "null"));
             return;
-        } else if (mIsWallpaper && mService.mInnerFields.mWallpaperActionPending) {
+        } else if (mIsWallpaper && mService.mWindowPlacerLocked.mWallpaperActionPending) {
             return;
         }
 
@@ -1764,6 +1765,8 @@
                 mWin.mAppToken.updateReportedVisibilityLocked();
             }
 
+            mWin.maybeRemoveReplacedWindow();
+
             return true;
         }
 
diff --git a/services/core/java/com/android/server/wm/WindowSurfacePlacer.java b/services/core/java/com/android/server/wm/WindowSurfacePlacer.java
new file mode 100644
index 0000000..ce07a9d
--- /dev/null
+++ b/services/core/java/com/android/server/wm/WindowSurfacePlacer.java
@@ -0,0 +1,1523 @@
+package com.android.server.wm;
+
+import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
+import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
+import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
+import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_NO_MOVE_ANIMATION;
+import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
+import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
+import static android.view.WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG;
+import static android.view.WindowManager.LayoutParams.TYPE_PRIVATE_PRESENTATION;
+import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
+import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
+import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
+import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
+import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
+import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
+import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
+import static com.android.server.wm.WindowManagerService.DEBUG;
+import static com.android.server.wm.WindowManagerService.DEBUG_ADD_REMOVE;
+import static com.android.server.wm.WindowManagerService.DEBUG_APP_TRANSITIONS;
+import static com.android.server.wm.WindowManagerService.DEBUG_LAYOUT;
+import static com.android.server.wm.WindowManagerService.DEBUG_LAYOUT_REPEATS;
+import static com.android.server.wm.WindowManagerService.DEBUG_ORIENTATION;
+import static com.android.server.wm.WindowManagerService.DEBUG_POWER;
+import static com.android.server.wm.WindowManagerService.DEBUG_STARTING_WINDOW;
+import static com.android.server.wm.WindowManagerService.DEBUG_TOKEN_MOVEMENT;
+import static com.android.server.wm.WindowManagerService.DEBUG_VISIBILITY;
+import static com.android.server.wm.WindowManagerService.DEBUG_WALLPAPER_LIGHT;
+import static com.android.server.wm.WindowManagerService.DEBUG_WINDOW_TRACE;
+import static com.android.server.wm.WindowManagerService.H.*;
+import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
+import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
+import static com.android.server.wm.WindowManagerService.SHOW_LIGHT_TRANSACTIONS;
+import static com.android.server.wm.WindowManagerService.SHOW_TRANSACTIONS;
+import static com.android.server.wm.WindowManagerService.TAG;
+import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_PLACING_SURFACES;
+import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
+import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_NONE;
+
+import android.graphics.Bitmap;
+import android.graphics.Canvas;
+import android.graphics.PixelFormat;
+import android.graphics.Rect;
+import android.os.Debug;
+import android.os.PowerManager;
+import android.os.RemoteException;
+import android.os.SystemClock;
+import android.os.Trace;
+import android.provider.Settings;
+import android.util.Log;
+import android.util.Slog;
+import android.view.Display;
+import android.view.DisplayInfo;
+import android.view.Surface;
+import android.view.SurfaceControl;
+import android.view.View;
+import android.view.WindowManager;
+import android.view.animation.Animation;
+import android.view.animation.AnimationUtils;
+
+import java.util.ArrayList;
+
+/**
+ * Positions windows and their surfaces.
+ *
+ * It sets positions of windows by calculating their frames and then applies this by positioning
+ * surfaces according to these frames. Z layer is still assigned withing WindowManagerService.
+ */
+class WindowSurfacePlacer {
+    private final WindowManagerService mService;
+    private final WallpaperController mWallpaperControllerLocked;
+
+    private boolean mInLayout = false;
+
+    /** Only do a maximum of 6 repeated layouts. After that quit */
+    private int mLayoutRepeatCount;
+
+    static final int SET_UPDATE_ROTATION                = 1 << 0;
+    static final int SET_WALLPAPER_MAY_CHANGE           = 1 << 1;
+    static final int SET_FORCE_HIDING_CHANGED           = 1 << 2;
+    static final int SET_ORIENTATION_CHANGE_COMPLETE    = 1 << 3;
+    static final int SET_TURN_ON_SCREEN                 = 1 << 4;
+    static final int SET_WALLPAPER_ACTION_PENDING       = 1 << 5;
+
+    boolean mWallpaperMayChange = false;
+    boolean mOrientationChangeComplete = true;
+    boolean mWallpaperActionPending = false;
+
+    private boolean mWallpaperForceHidingChanged = false;
+    private Object mLastWindowFreezeSource = null;
+    private Session mHoldScreen = null;
+    private boolean mObscured = false;
+    private boolean mSyswin = false;
+    private float mScreenBrightness = -1;
+    private float mButtonBrightness = -1;
+    private long mUserActivityTimeout = -1;
+    private boolean mUpdateRotation = false;
+    private final Rect mTmpStartRect = new Rect();
+
+    // Set to true when the display contains content to show the user.
+    // When false, the display manager may choose to mirror or blank the display.
+    private boolean mDisplayHasContent = false;
+
+    // Only set while traversing the default display based on its content.
+    // Affects the behavior of mirroring on secondary displays.
+    private boolean mObscureApplicationContentOnSecondaryDisplays = false;
+
+    private float mPreferredRefreshRate = 0;
+
+    private int mPreferredModeId = 0;
+
+    public WindowSurfacePlacer(WindowManagerService service) {
+        mService = service;
+        mWallpaperControllerLocked = mService.mWallpaperControllerLocked;
+    }
+
+    final void performSurfacePlacement() {
+        int loopCount = 6;
+        do {
+            mService.mTraversalScheduled = false;
+            performSurfacePlacementLoop();
+            mService.mH.removeMessages(DO_TRAVERSAL);
+            loopCount--;
+        } while (mService.mTraversalScheduled && loopCount > 0);
+        mWallpaperActionPending = false;
+    }
+
+    private void performSurfacePlacementLoop() {
+        if (mInLayout) {
+            if (DEBUG) {
+                throw new RuntimeException("Recursive call!");
+            }
+            Slog.w(TAG,
+                    "performLayoutAndPlaceSurfacesLocked called while in layout. Callers="
+                    + Debug.getCallers(3));
+            return;
+        }
+
+        if (mService.mWaitingForConfig) {
+            // Our configuration has changed (most likely rotation), but we
+            // don't yet have the complete configuration to report to
+            // applications.  Don't do any window layout until we have it.
+            return;
+        }
+
+        if (!mService.mDisplayReady) {
+            // Not yet initialized, nothing to do.
+            return;
+        }
+
+        Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "wmLayout");
+        mInLayout = true;
+
+        boolean recoveringMemory = false;
+        if (!mService.mForceRemoves.isEmpty()) {
+            recoveringMemory = true;
+            // Wait a little bit for things to settle down, and off we go.
+            while (!mService.mForceRemoves.isEmpty()) {
+                WindowState ws = mService.mForceRemoves.remove(0);
+                Slog.i(TAG, "Force removing: " + ws);
+                mService.removeWindowInnerLocked(ws);
+            }
+            Slog.w(TAG,
+                    "Due to memory failure, waiting a bit for next layout");
+            Object tmp = new Object();
+            synchronized (tmp) {
+                try {
+                    tmp.wait(250);
+                } catch (InterruptedException e) {
+                }
+            }
+        }
+
+        try {
+            performSurfacePlacementInner(recoveringMemory);
+
+            mInLayout = false;
+
+            if (mService.needsLayout()) {
+                if (++mLayoutRepeatCount < 6) {
+                    mService.requestTraversalLocked();
+                } else {
+                    Slog.e(TAG, "Performed 6 layouts in a row. Skipping");
+                    mLayoutRepeatCount = 0;
+                }
+            } else {
+                mLayoutRepeatCount = 0;
+            }
+
+            if (mService.mWindowsChanged && !mService.mWindowChangeListeners.isEmpty()) {
+                mService.mH.removeMessages(REPORT_WINDOWS_CHANGE);
+                mService.mH.sendEmptyMessage(REPORT_WINDOWS_CHANGE);
+            }
+        } catch (RuntimeException e) {
+            mInLayout = false;
+            Slog.wtf(TAG, "Unhandled exception while laying out windows", e);
+        }
+
+        Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
+    }
+
+    void debugLayoutRepeats(final String msg, int pendingLayoutChanges) {
+        if (mLayoutRepeatCount >= LAYOUT_REPEAT_THRESHOLD) {
+            Slog.v(TAG, "Layouts looping: " + msg +
+                    ", mPendingLayoutChanges = 0x" + Integer.toHexString(pendingLayoutChanges));
+        }
+    }
+
+    // "Something has changed!  Let's make it correct now."
+    private void performSurfacePlacementInner(boolean recoveringMemory) {
+        if (DEBUG_WINDOW_TRACE) {
+            Slog.v(TAG,
+                    "performSurfacePlacementInner: entry. Called by "
+                    + Debug.getCallers(3));
+        }
+
+        int i;
+        boolean updateInputWindowsNeeded = false;
+
+        if (mService.mFocusMayChange) {
+            mService.mFocusMayChange = false;
+            updateInputWindowsNeeded = mService.updateFocusedWindowLocked(
+                    UPDATE_FOCUS_WILL_PLACE_SURFACES, false /*updateInputWindows*/);
+        }
+
+        // Initialize state of exiting tokens.
+        final int numDisplays = mService.mDisplayContents.size();
+        for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
+            final DisplayContent displayContent = mService.mDisplayContents.valueAt(displayNdx);
+            for (i=displayContent.mExitingTokens.size()-1; i>=0; i--) {
+                displayContent.mExitingTokens.get(i).hasVisible = false;
+            }
+        }
+
+        for (int stackNdx = mService.mStackIdToStack.size() - 1; stackNdx >= 0; --stackNdx) {
+            // Initialize state of exiting applications.
+            final AppTokenList exitingAppTokens =
+                    mService.mStackIdToStack.valueAt(stackNdx).mExitingAppTokens;
+            for (int tokenNdx = exitingAppTokens.size() - 1; tokenNdx >= 0; --tokenNdx) {
+                exitingAppTokens.get(tokenNdx).hasVisible = false;
+            }
+        }
+
+        mHoldScreen = null;
+        mScreenBrightness = -1;
+        mButtonBrightness = -1;
+        mUserActivityTimeout = -1;
+        mObscureApplicationContentOnSecondaryDisplays = false;
+
+        mService.mTransactionSequence++;
+
+        final DisplayContent defaultDisplay = mService.getDefaultDisplayContentLocked();
+        final DisplayInfo defaultInfo = defaultDisplay.getDisplayInfo();
+        final int defaultDw = defaultInfo.logicalWidth;
+        final int defaultDh = defaultInfo.logicalHeight;
+
+        if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
+                ">>> OPEN TRANSACTION performLayoutAndPlaceSurfaces");
+        SurfaceControl.openTransaction();
+        try {
+
+            if (mService.mWatermark != null) {
+                mService.mWatermark.positionSurface(defaultDw, defaultDh);
+            }
+            if (mService.mStrictModeFlash != null) {
+                mService.mStrictModeFlash.positionSurface(defaultDw, defaultDh);
+            }
+            if (mService.mCircularDisplayMask != null) {
+                mService.mCircularDisplayMask.positionSurface(defaultDw, defaultDh,
+                        mService.mRotation);
+            }
+            if (mService.mEmulatorDisplayOverlay != null) {
+                mService.mEmulatorDisplayOverlay.positionSurface(defaultDw, defaultDh,
+                        mService.mRotation);
+            }
+
+            boolean focusDisplayed = false;
+
+            for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
+                final DisplayContent displayContent = mService.mDisplayContents.valueAt(displayNdx);
+                boolean updateAllDrawn = false;
+                WindowList windows = displayContent.getWindowList();
+                DisplayInfo displayInfo = displayContent.getDisplayInfo();
+                final int displayId = displayContent.getDisplayId();
+                final int dw = displayInfo.logicalWidth;
+                final int dh = displayInfo.logicalHeight;
+                final int innerDw = displayInfo.appWidth;
+                final int innerDh = displayInfo.appHeight;
+                final boolean isDefaultDisplay = (displayId == Display.DEFAULT_DISPLAY);
+
+                // Reset for each display.
+                mDisplayHasContent = false;
+                mPreferredRefreshRate = 0;
+                mPreferredModeId = 0;
+
+                int repeats = 0;
+                do {
+                    repeats++;
+                    if (repeats > 6) {
+                        Slog.w(TAG, "Animation repeat aborted after too many iterations");
+                        displayContent.layoutNeeded = false;
+                        break;
+                    }
+
+                    if (DEBUG_LAYOUT_REPEATS) debugLayoutRepeats(
+                            "On entry to LockedInner", displayContent.pendingLayoutChanges);
+
+                    if ((displayContent.pendingLayoutChanges &
+                            FINISH_LAYOUT_REDO_WALLPAPER) != 0 &&
+                            mWallpaperControllerLocked.adjustWallpaperWindows()) {
+                        mService.assignLayersLocked(windows);
+                        displayContent.layoutNeeded = true;
+                    }
+
+                    if (isDefaultDisplay && (displayContent.pendingLayoutChanges
+                            & FINISH_LAYOUT_REDO_CONFIG) != 0) {
+                        if (DEBUG_LAYOUT) Slog.v(TAG,
+                                "Computing new config from layout");
+                        if (mService.updateOrientationFromAppTokensLocked(true)) {
+                            displayContent.layoutNeeded = true;
+                            mService.mH.sendEmptyMessage(
+                                    SEND_NEW_CONFIGURATION);
+                        }
+                    }
+
+                    if ((displayContent.pendingLayoutChanges
+                            & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
+                        displayContent.layoutNeeded = true;
+                    }
+
+                    // FIRST LOOP: Perform a layout, if needed.
+                    if (repeats < LAYOUT_REPEAT_THRESHOLD) {
+                        performLayoutLockedInner(displayContent, repeats == 1,
+                                false /*updateInputWindows*/);
+                    } else {
+                        Slog.w(TAG, "Layout repeat skipped after too many iterations");
+                    }
+
+                    // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think
+                    // it is animating.
+                    displayContent.pendingLayoutChanges = 0;
+
+                    if (isDefaultDisplay) {
+                        mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
+                        for (i = windows.size() - 1; i >= 0; i--) {
+                            WindowState w = windows.get(i);
+                            if (w.mHasSurface) {
+                                mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs,
+                                        w.mAttachedWindow);
+                            }
+                        }
+                        displayContent.pendingLayoutChanges |=
+                                mService.mPolicy.finishPostLayoutPolicyLw();
+                        if (DEBUG_LAYOUT_REPEATS) debugLayoutRepeats(
+                                "after finishPostLayoutPolicyLw",
+                                displayContent.pendingLayoutChanges);
+                    }
+                } while (displayContent.pendingLayoutChanges != 0);
+
+                mObscured = false;
+                mSyswin = false;
+                displayContent.resetDimming();
+
+                // Only used if default window
+                final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
+
+                final int N = windows.size();
+                for (i=N-1; i>=0; i--) {
+                    WindowState w = windows.get(i);
+                    final Task task = w.getTask();
+                    if (task == null && w.getAttrs().type != TYPE_PRIVATE_PRESENTATION) {
+                        continue;
+                    }
+
+                    final boolean obscuredChanged = w.mObscured != mObscured;
+
+                    // Update effect.
+                    w.mObscured = mObscured;
+                    if (!mObscured) {
+                        handleNotObscuredLocked(w, innerDw, innerDh);
+                    }
+
+                    if (task != null && !task.getContinueDimming()) {
+                        w.handleFlagDimBehind();
+                    }
+
+                    if (isDefaultDisplay && obscuredChanged
+                            && mWallpaperControllerLocked.isWallpaperTarget(w)
+                            && w.isVisibleLw()) {
+                        // This is the wallpaper target and its obscured state
+                        // changed... make sure the current wallaper's visibility
+                        // has been updated accordingly.
+                        mWallpaperControllerLocked.updateWallpaperVisibility();
+                    }
+
+                    final WindowStateAnimator winAnimator = w.mWinAnimator;
+
+                    // If the window has moved due to its containing content frame changing, then
+                    // notify the listeners and optionally animate it.
+                    if (w.hasMoved()) {
+                        // Frame has moved, containing content frame has also moved, and we're not
+                        // currently animating... let's do something.
+                        final int left = w.mFrame.left;
+                        final int top = w.mFrame.top;
+                        if ((w.mAttrs.privateFlags & PRIVATE_FLAG_NO_MOVE_ANIMATION) == 0) {
+                            Animation a = AnimationUtils.loadAnimation(mService.mContext,
+                                    com.android.internal.R.anim.window_move_from_decor);
+                            winAnimator.setAnimation(a);
+                            winAnimator.mAnimDw = w.mLastFrame.left - left;
+                            winAnimator.mAnimDh = w.mLastFrame.top - top;
+                            winAnimator.mAnimateMove = true;
+                            winAnimator.mAnimatingMove = true;
+                        }
+
+                        //TODO (multidisplay): Accessibility supported only for the default display.
+                        if (mService.mAccessibilityController != null
+                                && displayId == Display.DEFAULT_DISPLAY) {
+                            mService.mAccessibilityController.onSomeWindowResizedOrMovedLocked();
+                        }
+
+                        try {
+                            w.mClient.moved(left, top);
+                        } catch (RemoteException e) {
+                        }
+                    }
+
+                    //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
+                    w.mContentChanged = false;
+
+                    // Moved from updateWindowsAndWallpaperLocked().
+                    if (w.mHasSurface) {
+                        // Take care of the window being ready to display.
+                        final boolean committed =
+                                winAnimator.commitFinishDrawingLocked();
+                        if (isDefaultDisplay && committed) {
+                            if (w.mAttrs.type == TYPE_DREAM) {
+                                // HACK: When a dream is shown, it may at that
+                                // point hide the lock screen.  So we need to
+                                // redo the layout to let the phone window manager
+                                // make this happen.
+                                displayContent.pendingLayoutChanges |=
+                                        FINISH_LAYOUT_REDO_LAYOUT;
+                                if (DEBUG_LAYOUT_REPEATS) {
+                                    debugLayoutRepeats(
+                                            "dream and commitFinishDrawingLocked true",
+                                            displayContent.pendingLayoutChanges);
+                                }
+                            }
+                            if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
+                                if (DEBUG_WALLPAPER_LIGHT)
+                                    Slog.v(TAG,
+                                            "First draw done in potential wallpaper target " + w);
+                                mWallpaperMayChange = true;
+                                displayContent.pendingLayoutChanges |=
+                                        FINISH_LAYOUT_REDO_WALLPAPER;
+                                if (DEBUG_LAYOUT_REPEATS) {
+                                    debugLayoutRepeats(
+                                            "wallpaper and commitFinishDrawingLocked true",
+                                            displayContent.pendingLayoutChanges);
+                                }
+                            }
+                        }
+
+                        winAnimator.setSurfaceBoundariesLocked(recoveringMemory);
+                    }
+
+                    final AppWindowToken atoken = w.mAppToken;
+                    if (DEBUG_STARTING_WINDOW && atoken != null
+                            && w == atoken.startingWindow) {
+                        Slog.d(TAG, "updateWindows: starting " + w
+                                + " isOnScreen=" + w.isOnScreen() + " allDrawn=" + atoken.allDrawn
+                                + " freezingScreen=" + atoken.mAppAnimator.freezingScreen);
+                    }
+                    if (atoken != null
+                            && (!atoken.allDrawn || atoken.mAppAnimator.freezingScreen)) {
+                        if (atoken.lastTransactionSequence != mService.mTransactionSequence) {
+                            atoken.lastTransactionSequence = mService.mTransactionSequence;
+                            atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
+                            atoken.startingDisplayed = false;
+                        }
+                        if ((w.isOnScreenIgnoringKeyguard()
+                                || winAnimator.mAttrType == TYPE_BASE_APPLICATION)
+                                && !w.mExiting && !w.mDestroying) {
+                            if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
+                                Slog.v(TAG, "Eval win " + w + ": isDrawn="
+                                        + w.isDrawnLw()
+                                        + ", isAnimating=" + winAnimator.isAnimating());
+                                if (!w.isDrawnLw()) {
+                                    Slog.v(TAG, "Not displayed: s="
+                                            + winAnimator.mSurfaceControl
+                                            + " pv=" + w.mPolicyVisibility
+                                            + " mDrawState=" + winAnimator.drawStateToString()
+                                            + " ah=" + w.mAttachedHidden
+                                            + " th=" + atoken.hiddenRequested
+                                            + " a=" + winAnimator.mAnimating);
+                                }
+                            }
+                            if (w != atoken.startingWindow) {
+                                if (!atoken.mAppAnimator.freezingScreen || !w.mAppFreezing) {
+                                    atoken.numInterestingWindows++;
+                                    if (w.isDrawnLw()) {
+                                        atoken.numDrawnWindows++;
+                                        if (DEBUG_VISIBILITY
+                                                || DEBUG_ORIENTATION)
+                                            Slog.v(TAG,
+                                                "tokenMayBeDrawn: " + atoken
+                                                + " freezingScreen="
+                                                + atoken.mAppAnimator.freezingScreen
+                                                + " mAppFreezing=" + w.mAppFreezing);
+                                        updateAllDrawn = true;
+                                    }
+                                }
+                            } else if (w.isDrawnLw()) {
+                                atoken.startingDisplayed = true;
+                            }
+                        }
+                    }
+
+                    if (isDefaultDisplay && someoneLosingFocus && (w == mService.mCurrentFocus)
+                            && w.isDisplayedLw()) {
+                        focusDisplayed = true;
+                    }
+
+                    mService.updateResizingWindows(w);
+                }
+
+                mService.mDisplayManagerInternal.setDisplayProperties(displayId,
+                        mDisplayHasContent,
+                        mPreferredRefreshRate,
+                        mPreferredModeId,
+                        true /* inTraversal, must call performTraversalInTrans... below */);
+
+                mService.getDisplayContentLocked(displayId).stopDimmingIfNeeded();
+
+                if (updateAllDrawn) {
+                    updateAllDrawnLocked(displayContent);
+                }
+            }
+
+            if (focusDisplayed) {
+                mService.mH.sendEmptyMessage(REPORT_LOSING_FOCUS);
+            }
+
+            // Give the display manager a chance to adjust properties
+            // like display rotation if it needs to.
+            mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
+
+        } catch (RuntimeException e) {
+            Slog.wtf(TAG, "Unhandled exception in Window Manager", e);
+        } finally {
+            SurfaceControl.closeTransaction();
+            if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
+                    "<<< CLOSE TRANSACTION performLayoutAndPlaceSurfaces");
+        }
+
+        final WindowList defaultWindows = defaultDisplay.getWindowList();
+
+        // If we are ready to perform an app transition, check through
+        // all of the app tokens to be shown and see if they are ready
+        // to go.
+        if (mService.mAppTransition.isReady()) {
+            defaultDisplay.pendingLayoutChanges |= handleAppTransitionReadyLocked(defaultWindows);
+            if (DEBUG_LAYOUT_REPEATS)
+                debugLayoutRepeats("after handleAppTransitionReadyLocked",
+                        defaultDisplay.pendingLayoutChanges);
+        }
+
+        if (!mService.mAnimator.mAppWindowAnimating && mService.mAppTransition.isRunning()) {
+            // We have finished the animation of an app transition.  To do
+            // this, we have delayed a lot of operations like showing and
+            // hiding apps, moving apps in Z-order, etc.  The app token list
+            // reflects the correct Z-order, but the window list may now
+            // be out of sync with it.  So here we will just rebuild the
+            // entire app window list.  Fun!
+            defaultDisplay.pendingLayoutChanges |=
+                    mService.handleAnimatingStoppedAndTransitionLocked();
+            if (DEBUG_LAYOUT_REPEATS)
+                debugLayoutRepeats("after handleAnimStopAndXitionLock",
+                        defaultDisplay.pendingLayoutChanges);
+        }
+
+        if (mWallpaperForceHidingChanged && defaultDisplay.pendingLayoutChanges == 0
+                && !mService.mAppTransition.isReady()) {
+            // At this point, there was a window with a wallpaper that
+            // was force hiding other windows behind it, but now it
+            // is going away.  This may be simple -- just animate
+            // away the wallpaper and its window -- or it may be
+            // hard -- the wallpaper now needs to be shown behind
+            // something that was hidden.
+            defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
+            if (DEBUG_LAYOUT_REPEATS)
+                debugLayoutRepeats("after animateAwayWallpaperLocked",
+                        defaultDisplay.pendingLayoutChanges);
+        }
+        mWallpaperForceHidingChanged = false;
+
+        if (mWallpaperMayChange) {
+            if (DEBUG_WALLPAPER_LIGHT)
+                Slog.v(TAG, "Wallpaper may change!  Adjusting");
+            defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
+            if (DEBUG_LAYOUT_REPEATS) debugLayoutRepeats("WallpaperMayChange",
+                    defaultDisplay.pendingLayoutChanges);
+        }
+
+        if (mService.mFocusMayChange) {
+            mService.mFocusMayChange = false;
+            if (mService.updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES,
+                    false /*updateInputWindows*/)) {
+                updateInputWindowsNeeded = true;
+                defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
+            }
+        }
+
+        if (mService.needsLayout()) {
+            defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
+            if (DEBUG_LAYOUT_REPEATS) debugLayoutRepeats("mLayoutNeeded",
+                    defaultDisplay.pendingLayoutChanges);
+        }
+
+        for (i = mService.mResizingWindows.size() - 1; i >= 0; i--) {
+            WindowState win = mService.mResizingWindows.get(i);
+            if (win.mAppFreezing) {
+                // Don't remove this window until rotation has completed.
+                continue;
+            }
+            win.reportResized();
+            mService.mResizingWindows.remove(i);
+        }
+
+        if (DEBUG_ORIENTATION && mService.mDisplayFrozen)
+            Slog.v(TAG,
+                "With display frozen, orientationChangeComplete="
+                + mOrientationChangeComplete);
+        if (mOrientationChangeComplete) {
+            if (mService.mWindowsFreezingScreen != WINDOWS_FREEZING_SCREENS_NONE) {
+                mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_NONE;
+                mService.mLastFinishedFreezeSource = mLastWindowFreezeSource;
+                mService.mH.removeMessages(WINDOW_FREEZE_TIMEOUT);
+            }
+            mService.stopFreezingDisplayLocked();
+        }
+
+        // Destroy the surface of any windows that are no longer visible.
+        boolean wallpaperDestroyed = false;
+        i = mService.mDestroySurface.size();
+        if (i > 0) {
+            do {
+                i--;
+                WindowState win = mService.mDestroySurface.get(i);
+                win.mDestroying = false;
+                if (mService.mInputMethodWindow == win) {
+                    mService.mInputMethodWindow = null;
+                }
+                if (mWallpaperControllerLocked.isWallpaperTarget(win)) {
+                    wallpaperDestroyed = true;
+                }
+                win.mWinAnimator.destroySurfaceLocked();
+            } while (i > 0);
+            mService.mDestroySurface.clear();
+        }
+
+        // Time to remove any exiting tokens?
+        for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
+            final DisplayContent displayContent = mService.mDisplayContents.valueAt(displayNdx);
+            ArrayList<WindowToken> exitingTokens = displayContent.mExitingTokens;
+            for (i = exitingTokens.size() - 1; i >= 0; i--) {
+                WindowToken token = exitingTokens.get(i);
+                if (!token.hasVisible) {
+                    exitingTokens.remove(i);
+                    if (token.windowType == TYPE_WALLPAPER) {
+                        mWallpaperControllerLocked.removeWallpaperToken(token);
+                    }
+                }
+            }
+        }
+
+        // Time to remove any exiting applications?
+        for (int stackNdx = mService.mStackIdToStack.size() - 1; stackNdx >= 0; --stackNdx) {
+            // Initialize state of exiting applications.
+            final AppTokenList exitingAppTokens =
+                    mService.mStackIdToStack.valueAt(stackNdx).mExitingAppTokens;
+            for (i = exitingAppTokens.size() - 1; i >= 0; i--) {
+                AppWindowToken token = exitingAppTokens.get(i);
+                if (!token.hasVisible && !mService.mClosingApps.contains(token) &&
+                        (!token.mIsExiting || token.allAppWindows.isEmpty())) {
+                    // Make sure there is no animation running on this token,
+                    // so any windows associated with it will be removed as
+                    // soon as their animations are complete
+                    token.mAppAnimator.clearAnimation();
+                    token.mAppAnimator.animating = false;
+                    if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT)
+                        Slog.v(TAG,
+                                "performLayout: App token exiting now removed" + token);
+                    token.removeAppFromTaskLocked();
+                }
+            }
+        }
+
+        if (wallpaperDestroyed) {
+            defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
+            defaultDisplay.layoutNeeded = true;
+        }
+
+        for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
+            final DisplayContent displayContent = mService.mDisplayContents.valueAt(displayNdx);
+            if (displayContent.pendingLayoutChanges != 0) {
+                displayContent.layoutNeeded = true;
+            }
+        }
+
+        // Finally update all input windows now that the window changes have stabilized.
+        mService.mInputMonitor.updateInputWindowsLw(true /*force*/);
+
+        mService.setHoldScreenLocked(mHoldScreen);
+        if (!mService.mDisplayFrozen) {
+            if (mScreenBrightness < 0 || mScreenBrightness > 1.0f) {
+                mService.mPowerManagerInternal.setScreenBrightnessOverrideFromWindowManager(-1);
+            } else {
+                mService.mPowerManagerInternal.setScreenBrightnessOverrideFromWindowManager(
+                        toBrightnessOverride(mScreenBrightness));
+            }
+            if (mButtonBrightness < 0
+                    || mButtonBrightness > 1.0f) {
+                mService.mPowerManagerInternal.setButtonBrightnessOverrideFromWindowManager(-1);
+            } else {
+                mService.mPowerManagerInternal.setButtonBrightnessOverrideFromWindowManager(
+                        toBrightnessOverride(mButtonBrightness));
+            }
+            mService.mPowerManagerInternal.setUserActivityTimeoutOverrideFromWindowManager(
+                    mUserActivityTimeout);
+        }
+
+        if (mService.mTurnOnScreen) {
+            if (mService.mAllowTheaterModeWakeFromLayout
+                    || Settings.Global.getInt(mService.mContext.getContentResolver(),
+                        Settings.Global.THEATER_MODE_ON, 0) == 0) {
+                if (DEBUG_VISIBILITY || DEBUG_POWER) {
+                    Slog.v(TAG, "Turning screen on after layout!");
+                }
+                mService.mPowerManager.wakeUp(SystemClock.uptimeMillis(),
+                        "android.server.wm:TURN_ON");
+            }
+            mService.mTurnOnScreen = false;
+        }
+
+        if (mUpdateRotation) {
+            if (DEBUG_ORIENTATION) Slog.d(TAG,
+                    "Performing post-rotate rotation");
+            if (mService.updateRotationUncheckedLocked(false)) {
+                mService.mH.sendEmptyMessage(SEND_NEW_CONFIGURATION);
+            } else {
+                mUpdateRotation = false;
+            }
+        }
+
+        if (mService.mWaitingForDrawnCallback != null ||
+                (mOrientationChangeComplete && !defaultDisplay.layoutNeeded &&
+                        !mUpdateRotation)) {
+            mService.checkDrawnWindowsLocked();
+        }
+
+        final int N = mService.mPendingRemove.size();
+        if (N > 0) {
+            if (mService.mPendingRemoveTmp.length < N) {
+                mService.mPendingRemoveTmp = new WindowState[N+10];
+            }
+            mService.mPendingRemove.toArray(mService.mPendingRemoveTmp);
+            mService.mPendingRemove.clear();
+            DisplayContentList displayList = new DisplayContentList();
+            for (i = 0; i < N; i++) {
+                WindowState w = mService.mPendingRemoveTmp[i];
+                mService.removeWindowInnerLocked(w);
+                final DisplayContent displayContent = w.getDisplayContent();
+                if (displayContent != null && !displayList.contains(displayContent)) {
+                    displayList.add(displayContent);
+                }
+            }
+
+            for (DisplayContent displayContent : displayList) {
+                mService.assignLayersLocked(displayContent.getWindowList());
+                displayContent.layoutNeeded = true;
+            }
+        }
+
+        // Remove all deferred displays stacks, tasks, and activities.
+        for (int displayNdx = mService.mDisplayContents.size() - 1; displayNdx >= 0; --displayNdx) {
+            mService.mDisplayContents.valueAt(displayNdx).checkForDeferredActions();
+        }
+
+        if (updateInputWindowsNeeded) {
+            mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
+        }
+        mService.setFocusTaskRegion();
+
+        // Check to see if we are now in a state where the screen should
+        // be enabled, because the window obscured flags have changed.
+        mService.enableScreenIfNeededLocked();
+
+        mService.scheduleAnimationLocked();
+
+        if (DEBUG_WINDOW_TRACE) {
+            Slog.e(TAG,
+                    "performSurfacePlacementInner exit: animating="
+                            + mService.mAnimator.mAnimating);
+        }
+    }
+
+    boolean isInLayout() {
+        return mInLayout;
+    }
+
+    final void performLayoutLockedInner(final DisplayContent displayContent,
+            boolean initial, boolean updateInputWindows) {
+        if (!displayContent.layoutNeeded) {
+            return;
+        }
+        displayContent.layoutNeeded = false;
+        WindowList windows = displayContent.getWindowList();
+        boolean isDefaultDisplay = displayContent.isDefaultDisplay;
+
+        DisplayInfo displayInfo = displayContent.getDisplayInfo();
+        final int dw = displayInfo.logicalWidth;
+        final int dh = displayInfo.logicalHeight;
+
+        if (mService.mInputConsumer != null) {
+            mService.mInputConsumer.layout(dw, dh);
+        }
+
+        final int N = windows.size();
+        int i;
+
+        if (DEBUG_LAYOUT) {
+            Slog.v(TAG, "-------------------------------------");
+            Slog.v(TAG, "performLayout: needed="
+                    + displayContent.layoutNeeded + " dw=" + dw + " dh=" + dh);
+        }
+
+        mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mService.mRotation);
+        if (isDefaultDisplay) {
+            // Not needed on non-default displays.
+            mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
+            mService.mScreenRect.set(0, 0, dw, dh);
+        }
+
+        mService.mPolicy.getContentRectLw(mService.mTmpContentRect);
+        displayContent.resize(mService.mTmpContentRect);
+
+        int seq = mService.mLayoutSeq+1;
+        if (seq < 0) seq = 0;
+        mService.mLayoutSeq = seq;
+
+        boolean behindDream = false;
+
+        // First perform layout of any root windows (not attached
+        // to another window).
+        int topAttached = -1;
+        for (i = N-1; i >= 0; i--) {
+            final WindowState win = windows.get(i);
+
+            // Don't do layout of a window if it is not visible, or
+            // soon won't be visible, to avoid wasting time and funky
+            // changes while a window is animating away.
+            final boolean gone = (behindDream && mService.mPolicy.canBeForceHidden(win, win.mAttrs))
+                    || win.isGoneForLayoutLw();
+
+            if (DEBUG_LAYOUT && !win.mLayoutAttached) {
+                Slog.v(TAG, "1ST PASS " + win
+                        + ": gone=" + gone + " mHaveFrame=" + win.mHaveFrame
+                        + " mLayoutAttached=" + win.mLayoutAttached
+                        + " screen changed=" + win.isConfigChanged());
+                final AppWindowToken atoken = win.mAppToken;
+                if (gone) Slog.v(TAG, "  GONE: mViewVisibility="
+                        + win.mViewVisibility + " mRelayoutCalled="
+                        + win.mRelayoutCalled + " hidden="
+                        + win.mRootToken.hidden + " hiddenRequested="
+                        + (atoken != null && atoken.hiddenRequested)
+                        + " mAttachedHidden=" + win.mAttachedHidden);
+                else Slog.v(TAG, "  VIS: mViewVisibility="
+                        + win.mViewVisibility + " mRelayoutCalled="
+                        + win.mRelayoutCalled + " hidden="
+                        + win.mRootToken.hidden + " hiddenRequested="
+                        + (atoken != null && atoken.hiddenRequested)
+                        + " mAttachedHidden=" + win.mAttachedHidden);
+            }
+
+            // If this view is GONE, then skip it -- keep the current
+            // frame, and let the caller know so they can ignore it
+            // if they want.  (We do the normal layout for INVISIBLE
+            // windows, since that means "perform layout as normal,
+            // just don't display").
+            if (!gone || !win.mHaveFrame || win.mLayoutNeeded
+                    || ((win.isConfigChanged() || win.setInsetsChanged()) &&
+                            ((win.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
+                            (win.mHasSurface && win.mAppToken != null &&
+                            win.mAppToken.layoutConfigChanges)))) {
+                if (!win.mLayoutAttached) {
+                    if (initial) {
+                        //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
+                        win.mContentChanged = false;
+                    }
+                    if (win.mAttrs.type == TYPE_DREAM) {
+                        // Don't layout windows behind a dream, so that if it
+                        // does stuff like hide the status bar we won't get a
+                        // bad transition when it goes away.
+                        behindDream = true;
+                    }
+                    win.mLayoutNeeded = false;
+                    win.prelayout();
+                    mService.mPolicy.layoutWindowLw(win, null);
+                    win.mLayoutSeq = seq;
+                    if (DEBUG_LAYOUT) Slog.v(TAG,
+                            "  LAYOUT: mFrame="
+                            + win.mFrame + " mContainingFrame="
+                            + win.mContainingFrame + " mDisplayFrame="
+                            + win.mDisplayFrame);
+                } else {
+                    if (topAttached < 0) topAttached = i;
+                }
+            }
+        }
+
+        boolean attachedBehindDream = false;
+
+        // Now perform layout of attached windows, which usually
+        // depend on the position of the window they are attached to.
+        // XXX does not deal with windows that are attached to windows
+        // that are themselves attached.
+        for (i = topAttached; i >= 0; i--) {
+            final WindowState win = windows.get(i);
+
+            if (win.mLayoutAttached) {
+                if (DEBUG_LAYOUT) Slog.v(TAG,
+                        "2ND PASS " + win + " mHaveFrame=" + win.mHaveFrame + " mViewVisibility="
+                        + win.mViewVisibility + " mRelayoutCalled=" + win.mRelayoutCalled);
+                // If this view is GONE, then skip it -- keep the current
+                // frame, and let the caller know so they can ignore it
+                // if they want.  (We do the normal layout for INVISIBLE
+                // windows, since that means "perform layout as normal,
+                // just don't display").
+                if (attachedBehindDream && mService.mPolicy.canBeForceHidden(win, win.mAttrs)) {
+                    continue;
+                }
+                if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
+                        || !win.mHaveFrame || win.mLayoutNeeded) {
+                    if (initial) {
+                        //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
+                        win.mContentChanged = false;
+                    }
+                    win.mLayoutNeeded = false;
+                    win.prelayout();
+                    mService.mPolicy.layoutWindowLw(win, win.mAttachedWindow);
+                    win.mLayoutSeq = seq;
+                    if (DEBUG_LAYOUT) Slog.v(TAG,
+                            "  LAYOUT: mFrame=" + win.mFrame + " mContainingFrame="
+                            + win.mContainingFrame + " mDisplayFrame=" + win.mDisplayFrame);
+                }
+            } else if (win.mAttrs.type == TYPE_DREAM) {
+                // Don't layout windows behind a dream, so that if it
+                // does stuff like hide the status bar we won't get a
+                // bad transition when it goes away.
+                attachedBehindDream = behindDream;
+            }
+        }
+
+        // Window frames may have changed.  Tell the input dispatcher about it.
+        mService.mInputMonitor.setUpdateInputWindowsNeededLw();
+        if (updateInputWindows) {
+            mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
+        }
+
+        mService.mPolicy.finishLayoutLw();
+    }
+
+    /**
+     * @param windows List of windows on default display.
+     * @return bitmap indicating if another pass through layout must be made.
+     */
+    private int handleAppTransitionReadyLocked(WindowList windows) {
+        int appsCount = mService.mOpeningApps.size();
+        if (!transitionGoodToGo(appsCount)) {
+            return 0;
+        }
+        if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "**** GOOD TO GO");
+        int transit = mService.mAppTransition.getAppTransition();
+        if (mService.mSkipAppTransitionAnimation) {
+            transit = AppTransition.TRANSIT_UNSET;
+        }
+        mService.mSkipAppTransitionAnimation = false;
+        mService.mNoAnimationNotifyOnTransitionFinished.clear();
+
+        mService.mH.removeMessages(APP_TRANSITION_TIMEOUT);
+
+        mService.rebuildAppWindowListLocked();
+
+        mWallpaperMayChange = false;
+
+        // The top-most window will supply the layout params,
+        // and we will determine it below.
+        WindowManager.LayoutParams animLp = null;
+        int bestAnimLayer = -1;
+        boolean fullscreenAnim = false;
+        boolean voiceInteraction = false;
+
+        final WindowState lowerWallpaperTarget =
+                mWallpaperControllerLocked.getLowerWallpaperTarget();
+        final WindowState upperWallpaperTarget =
+                mWallpaperControllerLocked.getUpperWallpaperTarget();
+
+        boolean openingAppHasWallpaper = false;
+        boolean closingAppHasWallpaper = false;
+        final AppWindowToken lowerWallpaperAppToken;
+        final AppWindowToken upperWallpaperAppToken;
+        if (lowerWallpaperTarget == null) {
+            lowerWallpaperAppToken = upperWallpaperAppToken = null;
+        } else {
+            lowerWallpaperAppToken = lowerWallpaperTarget.mAppToken;
+            upperWallpaperAppToken = upperWallpaperTarget.mAppToken;
+        }
+
+        int i;
+        // Do a first pass through the tokens for two
+        // things:
+        // (1) Determine if both the closing and opening
+        // app token sets are wallpaper targets, in which
+        // case special animations are needed
+        // (since the wallpaper needs to stay static
+        // behind them).
+        // (2) Find the layout params of the top-most
+        // application window in the tokens, which is
+        // what will control the animation theme.
+        final int closingAppsCount = mService.mClosingApps.size();
+        appsCount = closingAppsCount + mService.mOpeningApps.size();
+        for (i = 0; i < appsCount; i++) {
+            final AppWindowToken wtoken;
+            if (i < closingAppsCount) {
+                wtoken = mService.mClosingApps.valueAt(i);
+                if (wtoken == lowerWallpaperAppToken || wtoken == upperWallpaperAppToken) {
+                    closingAppHasWallpaper = true;
+                }
+            } else {
+                wtoken = mService.mOpeningApps.valueAt(i - closingAppsCount);
+                if (wtoken == lowerWallpaperAppToken || wtoken == upperWallpaperAppToken) {
+                    openingAppHasWallpaper = true;
+                }
+            }
+
+            voiceInteraction |= wtoken.voiceInteraction;
+
+            if (wtoken.appFullscreen) {
+                WindowState ws = wtoken.findMainWindow();
+                if (ws != null) {
+                    animLp = ws.mAttrs;
+                    bestAnimLayer = ws.mLayer;
+                    fullscreenAnim = true;
+                }
+            } else if (!fullscreenAnim) {
+                WindowState ws = wtoken.findMainWindow();
+                if (ws != null) {
+                    if (ws.mLayer > bestAnimLayer) {
+                        animLp = ws.mAttrs;
+                        bestAnimLayer = ws.mLayer;
+                    }
+                }
+            }
+        }
+
+        transit = maybeUpdateTransitToWallpaper(transit, openingAppHasWallpaper,
+                closingAppHasWallpaper, lowerWallpaperTarget, upperWallpaperTarget);
+
+        // If all closing windows are obscured, then there is
+        // no need to do an animation.  This is the case, for
+        // example, when this transition is being done behind
+        // the lock screen.
+        if (!mService.mPolicy.allowAppAnimationsLw()) {
+            if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
+                    "Animations disallowed by keyguard or dream.");
+            animLp = null;
+        }
+
+        processApplicationsAnimatingInPlace(transit);
+
+        AppWindowToken topClosingApp = null;
+        int topClosingLayer = 0;
+        appsCount = mService.mClosingApps.size();
+        for (i = 0; i < appsCount; i++) {
+            AppWindowToken wtoken = mService.mClosingApps.valueAt(i);
+            final AppWindowAnimator appAnimator = wtoken.mAppAnimator;
+            if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
+                    "Now closing app " + wtoken);
+            appAnimator.clearThumbnail();
+            appAnimator.animation = null;
+            wtoken.inPendingTransaction = false;
+            mService.setTokenVisibilityLocked(wtoken, animLp, false, transit, false,
+                    voiceInteraction);
+            wtoken.updateReportedVisibilityLocked();
+            // Force the allDrawn flag, because we want to start
+            // this guy's animations regardless of whether it's
+            // gotten drawn.
+            wtoken.allDrawn = true;
+            wtoken.deferClearAllDrawn = false;
+            // Ensure that apps that are mid-starting are also scheduled to have their
+            // starting windows removed after the animation is complete
+            if (wtoken.startingWindow != null && !wtoken.startingWindow.mExiting) {
+                mService.scheduleRemoveStartingWindowLocked(wtoken);
+            }
+            mService.mAnimator.mAppWindowAnimating |= appAnimator.isAnimating();
+
+            if (animLp != null) {
+                int layer = -1;
+                for (int j = 0; j < wtoken.windows.size(); j++) {
+                    WindowState win = wtoken.windows.get(j);
+                    if (win.mWinAnimator.mAnimLayer > layer) {
+                        layer = win.mWinAnimator.mAnimLayer;
+                    }
+                }
+                if (topClosingApp == null || layer > topClosingLayer) {
+                    topClosingApp = wtoken;
+                    topClosingLayer = layer;
+                }
+            }
+        }
+
+        AppWindowToken topOpeningApp = null;
+        appsCount = mService.mOpeningApps.size();
+        for (i = 0; i < appsCount; i++) {
+            AppWindowToken wtoken = mService.mOpeningApps.valueAt(i);
+            final AppWindowAnimator appAnimator = wtoken.mAppAnimator;
+            if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
+                    "Now opening app" + wtoken);
+
+            if (!appAnimator.usingTransferredAnimation) {
+                appAnimator.clearThumbnail();
+                appAnimator.animation = null;
+            }
+            wtoken.inPendingTransaction = false;
+            if (!mService.setTokenVisibilityLocked(
+                    wtoken, animLp, true, transit, false, voiceInteraction)){
+                // This token isn't going to be animating. Add it to the list of tokens to
+                // be notified of app transition complete since the notification will not be
+                // sent be the app window animator.
+                mService.mNoAnimationNotifyOnTransitionFinished.add(wtoken.token);
+            }
+            wtoken.updateReportedVisibilityLocked();
+            wtoken.waitingToShow = false;
+
+            appAnimator.mAllAppWinAnimators.clear();
+            final int windowsCount = wtoken.allAppWindows.size();
+            for (int j = 0; j < windowsCount; j++) {
+                appAnimator.mAllAppWinAnimators.add(wtoken.allAppWindows.get(j).mWinAnimator);
+            }
+            mService.mAnimator.mAnimating |= appAnimator.showAllWindowsLocked();
+            mService.mAnimator.mAppWindowAnimating |= appAnimator.isAnimating();
+
+            int topOpeningLayer = 0;
+            if (animLp != null) {
+                int layer = -1;
+                for (int j = 0; j < wtoken.windows.size(); j++) {
+                    WindowState win = wtoken.windows.get(j);
+                    if (win.mWinAnimator.mAnimLayer > layer) {
+                        layer = win.mWinAnimator.mAnimLayer;
+                    }
+                }
+                if (topOpeningApp == null || layer > topOpeningLayer) {
+                    topOpeningApp = wtoken;
+                    topOpeningLayer = layer;
+                }
+            }
+            createThumbnailAppAnimator(transit, wtoken, topOpeningLayer, topClosingLayer);
+        }
+
+        AppWindowAnimator openingAppAnimator = (topOpeningApp == null) ?  null :
+                topOpeningApp.mAppAnimator;
+        AppWindowAnimator closingAppAnimator = (topClosingApp == null) ? null :
+                topClosingApp.mAppAnimator;
+
+        mService.mAppTransition.goodToGo(openingAppAnimator, closingAppAnimator);
+        mService.mAppTransition.postAnimationCallback();
+        mService.mAppTransition.clear();
+
+        mService.mOpeningApps.clear();
+        mService.mClosingApps.clear();
+
+        // This has changed the visibility of windows, so perform
+        // a new layout to get them all up-to-date.
+        mService.getDefaultDisplayContentLocked().layoutNeeded = true;
+
+        // TODO(multidisplay): IMEs are only supported on the default display.
+        if (windows == mService.getDefaultWindowListLocked()
+                && !mService.moveInputMethodWindowsIfNeededLocked(true)) {
+            mService.assignLayersLocked(windows);
+        }
+        mService.updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES,
+                true /*updateInputWindows*/);
+        mService.mFocusMayChange = false;
+        mService.notifyActivityDrawnForKeyguard();
+        return FINISH_LAYOUT_REDO_LAYOUT
+                | FINISH_LAYOUT_REDO_CONFIG;
+
+    }
+
+    private boolean transitionGoodToGo(int appsCount) {
+        if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
+                "Checking " + appsCount + " opening apps (frozen="
+                        + mService.mDisplayFrozen + " timeout="
+                        + mService.mAppTransition.isTimeout() + ")...");
+        if (!mService.mAppTransition.isTimeout()) {
+            for (int i = 0; i < appsCount; i++) {
+                AppWindowToken wtoken = mService.mOpeningApps.valueAt(i);
+                if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
+                        "Check opening app=" + wtoken + ": allDrawn="
+                        + wtoken.allDrawn + " startingDisplayed="
+                        + wtoken.startingDisplayed + " startingMoved="
+                        + wtoken.startingMoved);
+                if (!wtoken.allDrawn && !wtoken.startingDisplayed && !wtoken.startingMoved) {
+                    return false;
+                }
+            }
+
+            // If the wallpaper is visible, we need to check it's ready too.
+            return !mWallpaperControllerLocked.isWallpaperVisible() ||
+                    mWallpaperControllerLocked.wallpaperTransitionReady();
+        }
+        return true;
+    }
+
+    private int maybeUpdateTransitToWallpaper(int transit, boolean openingAppHasWallpaper,
+            boolean closingAppHasWallpaper, WindowState lowerWallpaperTarget,
+            WindowState upperWallpaperTarget) {
+        // if wallpaper is animating in or out set oldWallpaper to null else to wallpaper
+        final WindowState wallpaperTarget = mWallpaperControllerLocked.getWallpaperTarget();
+        final WindowState oldWallpaper =
+                mWallpaperControllerLocked.isWallpaperTargetAnimating()
+                        ? null : wallpaperTarget;
+        if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
+                "New wallpaper target=" + wallpaperTarget
+                        + ", oldWallpaper=" + oldWallpaper
+                        + ", lower target=" + lowerWallpaperTarget
+                        + ", upper target=" + upperWallpaperTarget);
+        mService.mAnimateWallpaperWithTarget = false;
+        if (closingAppHasWallpaper && openingAppHasWallpaper) {
+            if (DEBUG_APP_TRANSITIONS)
+                Slog.v(TAG, "Wallpaper animation!");
+            switch (transit) {
+                case AppTransition.TRANSIT_ACTIVITY_OPEN:
+                case AppTransition.TRANSIT_TASK_OPEN:
+                case AppTransition.TRANSIT_TASK_TO_FRONT:
+                    transit = AppTransition.TRANSIT_WALLPAPER_INTRA_OPEN;
+                    break;
+                case AppTransition.TRANSIT_ACTIVITY_CLOSE:
+                case AppTransition.TRANSIT_TASK_CLOSE:
+                case AppTransition.TRANSIT_TASK_TO_BACK:
+                    transit = AppTransition.TRANSIT_WALLPAPER_INTRA_CLOSE;
+                    break;
+            }
+            if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
+                    "New transit: " + AppTransition.appTransitionToString(transit));
+        } else if ((oldWallpaper != null) && !mService.mOpeningApps.isEmpty()
+                && !mService.mOpeningApps.contains(oldWallpaper.mAppToken)) {
+            // We are transitioning from an activity with
+            // a wallpaper to one without.
+            transit = AppTransition.TRANSIT_WALLPAPER_CLOSE;
+            if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
+                    "New transit away from wallpaper: "
+                    + AppTransition.appTransitionToString(transit));
+        } else if (wallpaperTarget != null && wallpaperTarget.isVisibleLw()) {
+            // We are transitioning from an activity without
+            // a wallpaper to now showing the wallpaper
+            transit = AppTransition.TRANSIT_WALLPAPER_OPEN;
+            if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
+                    "New transit into wallpaper: "
+                    + AppTransition.appTransitionToString(transit));
+        } else {
+            mService.mAnimateWallpaperWithTarget = true;
+        }
+        return transit;
+    }
+
+    /**
+     * @param w WindowState this method is applied to.
+     * @param innerDw Width of app window.
+     * @param innerDh Height of app window.
+     */
+    private void handleNotObscuredLocked(final WindowState w, final int innerDw, final int innerDh) {
+        final WindowManager.LayoutParams attrs = w.mAttrs;
+        final int attrFlags = attrs.flags;
+        final boolean canBeSeen = w.isDisplayedLw();
+        final boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
+
+        if (opaqueDrawn && w.isFullscreen(innerDw, innerDh)) {
+            // This window completely covers everything behind it,
+            // so we want to leave all of them as undimmed (for
+            // performance reasons).
+            mObscured = true;
+        }
+
+        if (w.mHasSurface) {
+            if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
+                mHoldScreen = w.mSession;
+            }
+            if (!mSyswin && w.mAttrs.screenBrightness >= 0
+                    && mScreenBrightness < 0) {
+                mScreenBrightness = w.mAttrs.screenBrightness;
+            }
+            if (!mSyswin && w.mAttrs.buttonBrightness >= 0
+                    && mButtonBrightness < 0) {
+                mButtonBrightness = w.mAttrs.buttonBrightness;
+            }
+            if (!mSyswin && w.mAttrs.userActivityTimeout >= 0
+                    && mUserActivityTimeout < 0) {
+                mUserActivityTimeout = w.mAttrs.userActivityTimeout;
+            }
+
+            final int type = attrs.type;
+            if (canBeSeen
+                    && (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;
+                }
+                if (mPreferredRefreshRate == 0
+                        && w.mAttrs.preferredRefreshRate != 0) {
+                    mPreferredRefreshRate = w.mAttrs.preferredRefreshRate;
+                }
+                if (mPreferredModeId == 0
+                        && w.mAttrs.preferredDisplayModeId != 0) {
+                    mPreferredModeId = w.mAttrs.preferredDisplayModeId;
+                }
+            }
+        }
+    }
+
+    private void updateAllDrawnLocked(DisplayContent displayContent) {
+        // See if any windows have been drawn, so they (and others
+        // associated with them) can now be shown.
+        ArrayList<TaskStack> stacks = displayContent.getStacks();
+        for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
+            final ArrayList<Task> tasks = stacks.get(stackNdx).getTasks();
+            for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
+                final AppTokenList tokens = tasks.get(taskNdx).mAppTokens;
+                for (int tokenNdx = tokens.size() - 1; tokenNdx >= 0; --tokenNdx) {
+                    final AppWindowToken wtoken = tokens.get(tokenNdx);
+                    if (!wtoken.allDrawn) {
+                        int numInteresting = wtoken.numInterestingWindows;
+                        if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
+                            if (DEBUG_VISIBILITY)
+                                Slog.v(TAG, "allDrawn: " + wtoken
+                                    + " interesting=" + numInteresting
+                                    + " drawn=" + wtoken.numDrawnWindows);
+                            wtoken.allDrawn = true;
+                            // Force an additional layout pass where WindowStateAnimator#
+                            // commitFinishDrawingLocked() will call performShowLocked().
+                            displayContent.layoutNeeded = true;
+                            mService.mH.obtainMessage(NOTIFY_ACTIVITY_DRAWN,
+                                    wtoken.token).sendToTarget();
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    private static int toBrightnessOverride(float value) {
+        return (int)(value * PowerManager.BRIGHTNESS_ON);
+    }
+
+    private void processApplicationsAnimatingInPlace(int transit) {
+        if (transit == AppTransition.TRANSIT_TASK_IN_PLACE) {
+            // Find the focused window
+            final WindowState win = mService.findFocusedWindowLocked(
+                    mService.getDefaultDisplayContentLocked());
+            if (win != null) {
+                final AppWindowToken wtoken = win.mAppToken;
+                final AppWindowAnimator appAnimator = wtoken.mAppAnimator;
+                if (DEBUG_APP_TRANSITIONS)
+                    Slog.v(TAG, "Now animating app in place " + wtoken);
+                appAnimator.clearThumbnail();
+                appAnimator.animation = null;
+                mService.updateTokenInPlaceLocked(wtoken, transit);
+                wtoken.updateReportedVisibilityLocked();
+
+                appAnimator.mAllAppWinAnimators.clear();
+                final int N = wtoken.allAppWindows.size();
+                for (int j = 0; j < N; j++) {
+                    appAnimator.mAllAppWinAnimators.add(wtoken.allAppWindows.get(j).mWinAnimator);
+                }
+                mService.mAnimator.mAppWindowAnimating |= appAnimator.isAnimating();
+                mService.mAnimator.mAnimating |= appAnimator.showAllWindowsLocked();
+            }
+        }
+    }
+
+    private void createThumbnailAppAnimator(int transit, AppWindowToken appToken,
+            int openingLayer, int closingLayer) {
+        AppWindowAnimator openingAppAnimator = (appToken == null) ? null : appToken.mAppAnimator;
+        if (openingAppAnimator == null || openingAppAnimator.animation == null) {
+            return;
+        }
+        final int taskId = appToken.mTask.mTaskId;
+        Bitmap thumbnailHeader = mService.mAppTransition.getAppTransitionThumbnailHeader(taskId);
+        if (thumbnailHeader == null || thumbnailHeader.getConfig() == Bitmap.Config.ALPHA_8) {
+            return;
+        }
+        // This thumbnail animation is very special, we need to have
+        // an extra surface with the thumbnail included with the animation.
+        Rect dirty = new Rect(0, 0, thumbnailHeader.getWidth(), thumbnailHeader.getHeight());
+        try {
+            // TODO(multi-display): support other displays
+            final DisplayContent displayContent = mService.getDefaultDisplayContentLocked();
+            final Display display = displayContent.getDisplay();
+            final DisplayInfo displayInfo = displayContent.getDisplayInfo();
+
+            // Create a new surface for the thumbnail
+            SurfaceControl surfaceControl = new SurfaceControl(mService.mFxSession,
+                    "thumbnail anim", dirty.width(), dirty.height(),
+                    PixelFormat.TRANSLUCENT, SurfaceControl.HIDDEN);
+            surfaceControl.setLayerStack(display.getLayerStack());
+            if (SHOW_TRANSACTIONS) {
+                Slog.i(TAG, "  THUMBNAIL " + surfaceControl + ": CREATE");
+            }
+
+            // Draw the thumbnail onto the surface
+            Surface drawSurface = new Surface();
+            drawSurface.copyFrom(surfaceControl);
+            Canvas c = drawSurface.lockCanvas(dirty);
+            c.drawBitmap(thumbnailHeader, 0, 0, null);
+            drawSurface.unlockCanvasAndPost(c);
+            drawSurface.release();
+
+            // Get the thumbnail animation
+            Animation anim;
+            if (mService.mAppTransition.isNextThumbnailTransitionAspectScaled()) {
+                // If this is a multi-window scenario, we use the windows frame as
+                // destination of the thumbnail header animation. If this is a full screen
+                // window scenario, we use the whole display as the target.
+                WindowState win = appToken.findMainWindow();
+                Rect appRect = win != null ? win.getContentFrameLw() :
+                        new Rect(0, 0, displayInfo.appWidth, displayInfo.appHeight);
+                // For the new aspect-scaled transition, we want it to always show
+                // above the animating opening/closing window, and we want to
+                // synchronize its thumbnail surface with the surface for the
+                // open/close animation (only on the way down)
+                anim = mService.mAppTransition.createThumbnailAspectScaleAnimationLocked(appRect,
+                        thumbnailHeader, taskId);
+                Log.d(TAG, "assigning thumbnail force above layer: "
+                        + openingLayer + " " + closingLayer);
+                openingAppAnimator.thumbnailForceAboveLayer = Math.max(openingLayer, closingLayer);
+                openingAppAnimator.deferThumbnailDestruction =
+                        !mService.mAppTransition.isNextThumbnailTransitionScaleUp();
+            } else {
+                anim = mService.mAppTransition.createThumbnailScaleAnimationLocked(
+                        displayInfo.appWidth, displayInfo.appHeight, transit, thumbnailHeader);
+            }
+            anim.restrictDuration(MAX_ANIMATION_DURATION);
+            anim.scaleCurrentDuration(mService.getTransitionAnimationScaleLocked());
+
+            openingAppAnimator.thumbnail = surfaceControl;
+            openingAppAnimator.thumbnailLayer = openingLayer;
+            openingAppAnimator.thumbnailAnimation = anim;
+            mService.mAppTransition.getNextAppTransitionStartRect(taskId, mTmpStartRect);
+            openingAppAnimator.thumbnailX = mTmpStartRect.left;
+            openingAppAnimator.thumbnailY = mTmpStartRect.top;
+        } catch (Surface.OutOfResourcesException e) {
+            Slog.e(TAG, "Can't allocate thumbnail/Canvas surface w="
+                    + dirty.width() + " h=" + dirty.height(), e);
+            openingAppAnimator.clearThumbnail();
+        }
+    }
+
+    boolean copyAnimToLayoutParamsLocked() {
+        boolean doRequest = false;
+
+        final int bulkUpdateParams = mService.mAnimator.mBulkUpdateParams;
+        if ((bulkUpdateParams & SET_UPDATE_ROTATION) != 0) {
+            mUpdateRotation = true;
+            doRequest = true;
+        }
+        if ((bulkUpdateParams & SET_WALLPAPER_MAY_CHANGE) != 0) {
+            mWallpaperMayChange = true;
+            doRequest = true;
+        }
+        if ((bulkUpdateParams & SET_FORCE_HIDING_CHANGED) != 0) {
+            mWallpaperForceHidingChanged = true;
+            doRequest = true;
+        }
+        if ((bulkUpdateParams & SET_ORIENTATION_CHANGE_COMPLETE) == 0) {
+            mOrientationChangeComplete = false;
+        } else {
+            mOrientationChangeComplete = true;
+            mLastWindowFreezeSource = mService.mAnimator.mLastWindowFreezeSource;
+            if (mService.mWindowsFreezingScreen != WINDOWS_FREEZING_SCREENS_NONE) {
+                doRequest = true;
+            }
+        }
+        if ((bulkUpdateParams & SET_TURN_ON_SCREEN) != 0) {
+            mService.mTurnOnScreen = true;
+        }
+        if ((bulkUpdateParams & SET_WALLPAPER_ACTION_PENDING) != 0) {
+            mWallpaperActionPending = true;
+        }
+
+        return doRequest;
+    }
+}
diff --git a/services/core/jni/Android.mk b/services/core/jni/Android.mk
index 9556b08..98d8d08 100644
--- a/services/core/jni/Android.mk
+++ b/services/core/jni/Android.mk
@@ -4,9 +4,16 @@
 
 LOCAL_CFLAGS += -Wall -Werror -Wno-unused-parameter
 
+ifneq ($(ENABLE_CPUSETS),)
+ifneq ($(ENABLE_SCHED_BOOST),)
+LOCAL_CFLAGS += -DUSE_SCHED_BOOST
+endif
+endif
+
 LOCAL_SRC_FILES += \
     $(LOCAL_REL_DIR)/com_android_server_AlarmManagerService.cpp \
     $(LOCAL_REL_DIR)/com_android_server_am_BatteryStatsService.cpp \
+    $(LOCAL_REL_DIR)/com_android_server_am_ActivityManagerService.cpp \
     $(LOCAL_REL_DIR)/com_android_server_AssetAtlasService.cpp \
     $(LOCAL_REL_DIR)/com_android_server_connectivity_Vpn.cpp \
     $(LOCAL_REL_DIR)/com_android_server_ConsumerIrService.cpp \
diff --git a/services/core/jni/com_android_server_am_ActivityManagerService.cpp b/services/core/jni/com_android_server_am_ActivityManagerService.cpp
new file mode 100644
index 0000000..52217b9
--- /dev/null
+++ b/services/core/jni/com_android_server_am_ActivityManagerService.cpp
@@ -0,0 +1,151 @@
+/*
+ * 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.
+ */
+
+#define LOG_TAG "ActivityManagerService"
+//#define LOG_NDEBUG 0
+
+#include <android_runtime/AndroidRuntime.h>
+#include <jni.h>
+
+#include <ScopedLocalRef.h>
+#include <ScopedPrimitiveArray.h>
+
+#include <cutils/log.h>
+#include <utils/misc.h>
+#include <utils/Log.h>
+
+#include <stdio.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <semaphore.h>
+#include <stddef.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+namespace android
+{
+
+    // migrate from foreground to foreground_boost
+    static jint migrateToBoost(JNIEnv *env, jobject _this)
+    {
+#ifdef USE_SCHED_BOOST
+        // File descriptors open to /dev/cpuset/../tasks, setup by initialize, or -1 on error
+        FILE* fg_cpuset_file = NULL;
+        int   boost_cpuset_fd = 0;
+        if (!access("/dev/cpuset/tasks", F_OK)) {
+            fg_cpuset_file = fopen("/dev/cpuset/foreground/tasks", "r+");
+            if (ferror(fg_cpuset_file)) {
+                return 0;
+            }
+            boost_cpuset_fd = open("/dev/cpuset/foreground/boost/tasks", O_WRONLY);
+            if (boost_cpuset_fd < 0) {
+                fclose(fg_cpuset_file);
+                return 0;
+            }
+
+        }
+        if (!fg_cpuset_file || !boost_cpuset_fd) {
+            fclose(fg_cpuset_file);
+            close(boost_cpuset_fd);
+            return 0;
+        }
+        char buf[17];
+        while (fgets(buf, 16, fg_cpuset_file)) {
+            int i = 0;
+            for (; i < 16; i++) {
+                if (buf[i] == '\n') {
+                    buf[i] = 0;
+                    break;
+                }
+            }
+            if (write(boost_cpuset_fd, buf, i) < 0) {
+                // ignore error
+            }
+            if (feof(fg_cpuset_file))
+                break;
+        }
+        fclose(fg_cpuset_file);
+        close(boost_cpuset_fd);
+#endif
+        return 0;
+    }
+
+    // migrate from foreground_boost to foreground
+    static jint migrateFromBoost(JNIEnv *env, jobject _this)
+    {
+#ifdef USE_SCHED_BOOST
+        // File descriptors open to /dev/cpuset/../tasks, setup by initialize, or -1 on error
+        int   fg_cpuset_fd = 0;
+        FILE* boost_cpuset_file = NULL;
+        if (!access("/dev/cpuset/tasks", F_OK)) {
+            boost_cpuset_file = fopen("/dev/cpuset/foreground/boost/tasks", "r+");
+            if (ferror(boost_cpuset_file)) {
+                return 0;
+            }
+            fg_cpuset_fd = open("/dev/cpuset/foreground/tasks", O_WRONLY);
+            if (fg_cpuset_fd < 0) {
+                fclose(boost_cpuset_file);
+                return 0;
+            }
+
+        }
+        if (!boost_cpuset_file || !fg_cpuset_fd) {
+            fclose(boost_cpuset_file);
+            close(fg_cpuset_fd);
+            return 0;
+        }
+        char buf[17];
+        char *curBuf = buf;
+        while (fgets(buf, 16, boost_cpuset_file)) {
+            //ALOGE("Appending FD %s to fg", buf);
+            int i = 0;
+            for (; i < 16; i++) {
+                if (buf[i] == '\n') {
+                    buf[i] = 0;
+                    break;
+                }
+            }
+            if (write(fg_cpuset_fd, buf, i) < 0) {
+                //ALOGE("Appending FD %s to fg ERROR", buf);
+                // handle error?
+            }
+            if (feof(boost_cpuset_file))
+                break;
+        }
+
+        close(fg_cpuset_fd);
+        fclose(boost_cpuset_file);
+
+#endif
+        return 0;
+
+    }
+
+
+    static JNINativeMethod method_table[] = {
+        { "nativeMigrateToBoost",   "()I", (void*)migrateToBoost },
+        { "nativeMigrateFromBoost", "()I", (void*)migrateFromBoost },
+    };
+
+    int register_android_server_ActivityManagerService(JNIEnv *env)
+    {
+        return jniRegisterNativeMethods(env, "com/android/server/am/ActivityManagerService",
+                                        method_table, NELEM(method_table));
+    }
+
+}
diff --git a/services/core/jni/onload.cpp b/services/core/jni/onload.cpp
index 67872da..1f3fde6 100644
--- a/services/core/jni/onload.cpp
+++ b/services/core/jni/onload.cpp
@@ -20,6 +20,7 @@
 #include "utils/misc.h"
 
 namespace android {
+int register_android_server_ActivityManagerService(JNIEnv* env);
 int register_android_server_AlarmManagerService(JNIEnv* env);
 int register_android_server_AssetAtlasService(JNIEnv* env);
 int register_android_server_BatteryStatsService(JNIEnv* env);
@@ -57,6 +58,7 @@
     }
     ALOG_ASSERT(env, "Could not retrieve the env!");
 
+    register_android_server_ActivityManagerService(env);
     register_android_server_PowerManagerService(env);
     register_android_server_SerialService(env);
     register_android_server_InputApplicationHandle(env);
@@ -80,5 +82,6 @@
     register_android_server_PersistentDataBlockService(env);
     register_android_server_Watchdog(env);
 
+
     return JNI_VERSION_1_4;
 }
diff --git a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java
index 00a3c5c..0202309 100644
--- a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java
+++ b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java
@@ -45,7 +45,6 @@
 import android.app.admin.IDevicePolicyManager;
 import android.app.admin.SystemUpdatePolicy;
 import android.app.backup.IBackupManager;
-import android.app.trust.TrustManager;
 import android.content.BroadcastReceiver;
 import android.content.ComponentName;
 import android.content.ContentResolver;
@@ -97,6 +96,8 @@
 import android.security.KeyChain.KeyChainConnection;
 import android.service.persistentdata.PersistentDataBlockManager;
 import android.text.TextUtils;
+import android.util.ArrayMap;
+import android.util.ArraySet;
 import android.util.Log;
 import android.util.PrintWriterPrinter;
 import android.util.Printer;
@@ -141,8 +142,6 @@
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.Date;
-import java.util.HashMap;
-import java.util.HashSet;
 import java.util.List;
 import java.util.Map.Entry;
 import java.util.Set;
@@ -198,7 +197,7 @@
 
     private static final Set<String> DEVICE_OWNER_USER_RESTRICTIONS;
     static {
-        DEVICE_OWNER_USER_RESTRICTIONS = new HashSet();
+        DEVICE_OWNER_USER_RESTRICTIONS = new ArraySet<>();
         DEVICE_OWNER_USER_RESTRICTIONS.add(UserManager.DISALLOW_USB_FILE_TRANSFER);
         DEVICE_OWNER_USER_RESTRICTIONS.add(UserManager.DISALLOW_CONFIG_TETHERING);
         DEVICE_OWNER_USER_RESTRICTIONS.add(UserManager.DISALLOW_NETWORK_RESET);
@@ -219,7 +218,7 @@
     // owner and profile owner.
     private static final Set<String> IMMUTABLE_USER_RESTRICTIONS;
     static {
-        IMMUTABLE_USER_RESTRICTIONS = new HashSet();
+        IMMUTABLE_USER_RESTRICTIONS = new ArraySet<>();
         IMMUTABLE_USER_RESTRICTIONS.add(UserManager.DISALLOW_WALLPAPER);
     }
 
@@ -228,16 +227,16 @@
     private static final Set<String> GLOBAL_SETTINGS_WHITELIST;
     private static final Set<String> GLOBAL_SETTINGS_DEPRECATED;
     static {
-        SECURE_SETTINGS_WHITELIST = new HashSet();
+        SECURE_SETTINGS_WHITELIST = new ArraySet<>();
         SECURE_SETTINGS_WHITELIST.add(Settings.Secure.DEFAULT_INPUT_METHOD);
         SECURE_SETTINGS_WHITELIST.add(Settings.Secure.SKIP_FIRST_USE_HINTS);
         SECURE_SETTINGS_WHITELIST.add(Settings.Secure.INSTALL_NON_MARKET_APPS);
 
-        SECURE_SETTINGS_DEVICEOWNER_WHITELIST = new HashSet();
+        SECURE_SETTINGS_DEVICEOWNER_WHITELIST = new ArraySet<>();
         SECURE_SETTINGS_DEVICEOWNER_WHITELIST.addAll(SECURE_SETTINGS_WHITELIST);
         SECURE_SETTINGS_DEVICEOWNER_WHITELIST.add(Settings.Secure.LOCATION_MODE);
 
-        GLOBAL_SETTINGS_WHITELIST = new HashSet();
+        GLOBAL_SETTINGS_WHITELIST = new ArraySet<>();
         GLOBAL_SETTINGS_WHITELIST.add(Settings.Global.ADB_ENABLED);
         GLOBAL_SETTINGS_WHITELIST.add(Settings.Global.AUTO_TIME);
         GLOBAL_SETTINGS_WHITELIST.add(Settings.Global.AUTO_TIME_ZONE);
@@ -247,7 +246,7 @@
         GLOBAL_SETTINGS_WHITELIST.add(Settings.Global.STAY_ON_WHILE_PLUGGED_IN);
         GLOBAL_SETTINGS_WHITELIST.add(Settings.Global.WIFI_DEVICE_OWNER_CONFIGS_LOCKDOWN);
 
-        GLOBAL_SETTINGS_DEPRECATED = new HashSet();
+        GLOBAL_SETTINGS_DEPRECATED = new ArraySet<>();
         GLOBAL_SETTINGS_DEPRECATED.add(Settings.Global.BLUETOOTH_ON);
         GLOBAL_SETTINGS_DEPRECATED.add(Settings.Global.DEVELOPMENT_SETTINGS_ENABLED);
         GLOBAL_SETTINGS_DEPRECATED.add(Settings.Global.MODE_RINGER);
@@ -279,7 +278,7 @@
     NotificationManager mNotificationManager;
 
     // Stores and loads state on device and profile owners.
-    private final DeviceOwner mDeviceOwner;
+    private final Owners mOwners;
 
     private final Binder mToken = new Binder();
 
@@ -327,7 +326,7 @@
         boolean mUserSetupComplete = false;
         int mPermissionPolicy;
 
-        final HashMap<ComponentName, ActiveAdmin> mAdminMap = new HashMap<>();
+        final ArrayMap<ComponentName, ActiveAdmin> mAdminMap = new ArrayMap<>();
         final ArrayList<ActiveAdmin> mAdminList = new ArrayList<>();
         final ArrayList<ComponentName> mRemovingAdmins = new ArrayList<>();
 
@@ -495,7 +494,7 @@
             }
         }
 
-        Set<String> accountTypesWithManagementDisabled = new HashSet<String>();
+        Set<String> accountTypesWithManagementDisabled = new ArraySet<>();
 
         // The list of permitted accessibility services package namesas set by a profile
         // or device owner. Null means all accessibility services are allowed, empty means
@@ -512,7 +511,7 @@
         String globalProxySpec = null;
         String globalProxyExclusionList = null;
 
-        HashMap<String, TrustAgentInfo> trustAgentInfos = new HashMap<String, TrustAgentInfo>();
+        ArrayMap<String, TrustAgentInfo> trustAgentInfos = new ArrayMap<>();
 
         List<String> crossProfileWidgetProviders;
 
@@ -835,7 +834,7 @@
                 throws XmlPullParserException, IOException {
             int outerDepthDAM = parser.getDepth();
             int typeDAM;
-            Set<String> result = new HashSet<String>();
+            Set<String> result = new ArraySet<>();
             while ((typeDAM=parser.next()) != END_DOCUMENT
                     && (typeDAM != END_TAG || parser.getDepth() > outerDepthDAM)) {
                 if (typeDAM == END_TAG || typeDAM == TEXT) {
@@ -851,11 +850,11 @@
             return result;
         }
 
-        private HashMap<String, TrustAgentInfo> getAllTrustAgentInfos(
+        private ArrayMap<String, TrustAgentInfo> getAllTrustAgentInfos(
                 XmlPullParser parser, String tag) throws XmlPullParserException, IOException {
             int outerDepthDAM = parser.getDepth();
             int typeDAM;
-            HashMap<String, TrustAgentInfo> result = new HashMap<String, TrustAgentInfo>();
+            final ArrayMap<String, TrustAgentInfo> result = new ArrayMap<>();
             while ((typeDAM=parser.next()) != END_DOCUMENT
                     && (typeDAM != END_TAG || parser.getDepth() > outerDepthDAM)) {
                 if (typeDAM == END_TAG || typeDAM == TEXT) {
@@ -1044,7 +1043,7 @@
      */
     public DevicePolicyManagerService(Context context) {
         mContext = context;
-        mDeviceOwner = new DeviceOwner(mContext);
+        mOwners = new Owners(mContext);
         mUserManager = UserManager.get(mContext);
         mHasFeature = context.getPackageManager().hasSystemFeature(
                 PackageManager.FEATURE_DEVICE_ADMIN);
@@ -1120,8 +1119,8 @@
                 Slog.w(LOG_TAG, "Tried to remove device policy file for user 0! Ignoring.");
                 return;
             }
-            mDeviceOwner.removeProfileOwner(userHandle);
-            mDeviceOwner.writeProfileOwner(userHandle);
+            mOwners.removeProfileOwner(userHandle);
+            mOwners.writeProfileOwner(userHandle);
 
             DevicePolicyData policy = mUserData.get(userHandle);
             if (policy != null) {
@@ -1137,7 +1136,7 @@
 
     void loadDeviceOwner() {
         synchronized (this) {
-            mDeviceOwner.load();
+            mOwners.load();
             updateDeviceOwnerLocked();
         }
     }
@@ -1805,15 +1804,15 @@
         Set<Integer> usersWithProfileOwners;
         Set<Integer> usersWithData;
         synchronized(this) {
-            usersWithProfileOwners = mDeviceOwner.getProfileOwnerKeys();
-            usersWithData = new HashSet<Integer>();
+            usersWithProfileOwners = mOwners.getProfileOwnerKeys();
+            usersWithData = new ArraySet<>();
             for (int i = 0; i < mUserData.size(); i++) {
                 usersWithData.add(mUserData.keyAt(i));
             }
         }
         List<UserInfo> allUsers = mUserManager.getUsers();
 
-        Set<Integer> deletedUsers = new HashSet<Integer>();
+        Set<Integer> deletedUsers = new ArraySet<>();
         deletedUsers.addAll(usersWithProfileOwners);
         deletedUsers.addAll(usersWithData);
         for (UserInfo userInfo : allUsers) {
@@ -4116,7 +4115,7 @@
             return false;
         }
         if (packageName == null
-                || !DeviceOwner.isInstalled(packageName, mContext.getPackageManager())) {
+                || !Owners.isInstalled(packageName, mContext.getPackageManager())) {
             throw new IllegalArgumentException("Invalid package name " + packageName
                     + " for device owner");
         }
@@ -4135,8 +4134,8 @@
                 Binder.restoreCallingIdentity(ident);
             }
 
-            mDeviceOwner.setDeviceOwner(packageName, ownerName);
-            mDeviceOwner.writeDeviceOwner();
+            mOwners.setDeviceOwner(packageName, ownerName);
+            mOwners.writeDeviceOwner();
             updateDeviceOwnerLocked();
             Intent intent = new Intent(DevicePolicyManager.ACTION_DEVICE_OWNER_CHANGED);
 
@@ -4156,8 +4155,8 @@
             return false;
         }
         synchronized (this) {
-            return mDeviceOwner.hasDeviceOwner()
-                    && mDeviceOwner.getDeviceOwnerPackageName().equals(packageName);
+            return mOwners.hasDeviceOwner()
+                    && mOwners.getDeviceOwnerPackageName().equals(packageName);
         }
     }
 
@@ -4167,7 +4166,7 @@
             return null;
         }
         synchronized (this) {
-            return mDeviceOwner.getDeviceOwnerPackageName();
+            return mOwners.getDeviceOwnerPackageName();
         }
     }
 
@@ -4178,10 +4177,10 @@
         }
         mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USERS, null);
         synchronized (this) {
-            if (!mDeviceOwner.hasDeviceOwner()) {
+            if (!mOwners.hasDeviceOwner()) {
                 return null;
             }
-            String deviceOwnerPackage = mDeviceOwner.getDeviceOwnerPackageName();
+            String deviceOwnerPackage = mOwners.getDeviceOwnerPackageName();
             return getApplicationLabel(deviceOwnerPackage, UserHandle.USER_OWNER);
         }
     }
@@ -4221,8 +4220,8 @@
         synchronized (this) {
             clearUserPoliciesLocked(new UserHandle(UserHandle.USER_OWNER));
 
-            mDeviceOwner.clearDeviceOwner();
-            mDeviceOwner.writeDeviceOwner();
+            mOwners.clearDeviceOwner();
+            mOwners.writeDeviceOwner();
             updateDeviceOwnerLocked();
             // Reactivate backup service.
             long ident = Binder.clearCallingIdentity();
@@ -4243,7 +4242,7 @@
         if (!mHasFeature) {
             return false;
         }
-        if (initializer == null || !DeviceOwner.isInstalled(
+        if (initializer == null || !Owners.isInstalled(
                 initializer.getPackageName(), mContext.getPackageManager())) {
             throw new IllegalArgumentException("Invalid component name " + initializer
                     + " for device initializer");
@@ -4262,15 +4261,15 @@
         synchronized (this) {
             enforceCanSetDeviceInitializer(who);
 
-            if (mDeviceOwner.hasDeviceInitializer()) {
+            if (mOwners.hasDeviceInitializer()) {
                 throw new IllegalStateException(
                         "Trying to set device initializer but device initializer is already set.");
             }
 
-            mDeviceOwner.setDeviceInitializer(initializer);
+            mOwners.setDeviceInitializer(initializer);
 
             addDeviceInitializerToLockTaskPackagesLocked(UserHandle.USER_OWNER);
-            mDeviceOwner.writeDeviceOwner();
+            mOwners.writeDeviceOwner();
             return true;
         }
     }
@@ -4294,8 +4293,8 @@
             return false;
         }
         synchronized (this) {
-            return mDeviceOwner.hasDeviceInitializer()
-                    && mDeviceOwner.getDeviceInitializerPackageName().equals(packageName);
+            return mOwners.hasDeviceInitializer()
+                    && mOwners.getDeviceInitializerPackageName().equals(packageName);
         }
     }
 
@@ -4305,8 +4304,8 @@
             return null;
         }
         synchronized (this) {
-            if (mDeviceOwner.hasDeviceInitializer()) {
-                return mDeviceOwner.getDeviceInitializerPackageName();
+            if (mOwners.hasDeviceInitializer()) {
+                return mOwners.getDeviceInitializerPackageName();
             }
         }
         return null;
@@ -4318,8 +4317,8 @@
             return null;
         }
         synchronized (this) {
-            if (mDeviceOwner.hasDeviceInitializer()) {
-                return mDeviceOwner.getDeviceInitializerComponent();
+            if (mOwners.hasDeviceInitializer()) {
+                return mOwners.getDeviceInitializerComponent();
             }
         }
         return null;
@@ -4347,8 +4346,8 @@
         synchronized (this) {
             long ident = Binder.clearCallingIdentity();
             try {
-                mDeviceOwner.clearDeviceInitializer();
-                mDeviceOwner.writeDeviceOwner();
+                mOwners.clearDeviceInitializer();
+                mOwners.writeDeviceOwner();
             } finally {
                 Binder.restoreCallingIdentity(ident);
             }
@@ -4361,14 +4360,14 @@
             return false;
         }
         if (who == null
-                || !DeviceOwner.isInstalledForUser(who.getPackageName(), userHandle)) {
+                || !Owners.isInstalledForUser(who.getPackageName(), userHandle)) {
             throw new IllegalArgumentException("Component " + who
                     + " not installed for userId:" + userHandle);
         }
         synchronized (this) {
             enforceCanSetProfileOwner(userHandle);
-            mDeviceOwner.setProfileOwner(who, ownerName, userHandle);
-            mDeviceOwner.writeProfileOwner(userHandle);
+            mOwners.setProfileOwner(who, ownerName, userHandle);
+            mOwners.writeProfileOwner(userHandle);
             return true;
         }
     }
@@ -4384,8 +4383,8 @@
         synchronized (this) {
             clearUserPoliciesLocked(callingUser);
             final int userId = callingUser.getIdentifier();
-            mDeviceOwner.removeProfileOwner(userId);
-            mDeviceOwner.writeProfileOwner(userId);
+            mOwners.removeProfileOwner(userId);
+            mOwners.writeProfileOwner(userId);
         }
     }
 
@@ -4542,14 +4541,14 @@
         }
 
         synchronized (this) {
-            return mDeviceOwner.getProfileOwnerComponent(userHandle);
+            return mOwners.getProfileOwnerComponent(userHandle);
         }
     }
 
     // Returns the active profile owner for this user or null if the current user has no
     // profile owner.
     private ActiveAdmin getProfileOwnerAdmin(int userHandle) {
-        ComponentName profileOwner = mDeviceOwner.getProfileOwnerComponent(userHandle);
+        ComponentName profileOwner = mOwners.getProfileOwnerComponent(userHandle);
         if (profileOwner == null) {
             return null;
         }
@@ -4650,7 +4649,7 @@
      * except for adb if no accounts or additional users are present on the device.
      */
     private void enforceCanSetDeviceOwner() {
-        if (mDeviceOwner != null && mDeviceOwner.hasDeviceOwner()) {
+        if (mOwners != null && mOwners.hasDeviceOwner()) {
             throw new IllegalStateException("Trying to set the device owner, but device owner "
                     + "is already set.");
         }
@@ -4751,7 +4750,7 @@
 
         synchronized (this) {
             p.println("Current Device Policy Manager state:");
-            mDeviceOwner.dump("  ", pw);
+            mOwners.dump("  ", pw);
             int userCount = mUserData.size();
             for (int u = 0; u < userCount; u++) {
                 DevicePolicyData policy = getUserData(mUserData.keyAt(u));
@@ -5697,7 +5696,7 @@
         synchronized (this) {
             DevicePolicyData policy = getUserData(userId);
             final int N = policy.mAdminList.size();
-            HashSet<String> resultSet = new HashSet<String>();
+            ArraySet<String> resultSet = new ArraySet<>();
             for (int i = 0; i < N; i++) {
                 ActiveAdmin admin = policy.mAdminList.get(i);
                 resultSet.addAll(admin.accountTypesWithManagementDisabled);
@@ -6238,10 +6237,10 @@
         @Override
         public List<String> getCrossProfileWidgetProviders(int profileId) {
             synchronized (DevicePolicyManagerService.this) {
-                if (mDeviceOwner == null) {
+                if (mOwners == null) {
                     return Collections.emptyList();
                 }
-                ComponentName ownerComponent = mDeviceOwner.getProfileOwnerComponent(profileId);
+                ComponentName ownerComponent = mOwners.getProfileOwnerComponent(profileId);
                 if (ownerComponent == null) {
                     return Collections.emptyList();
                 }
@@ -6311,11 +6310,11 @@
         synchronized (this) {
             getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_DEVICE_OWNER);
             if (policy == null) {
-                mDeviceOwner.clearSystemUpdatePolicy();
+                mOwners.clearSystemUpdatePolicy();
             } else {
-                mDeviceOwner.setSystemUpdatePolicy(policy);
+                mOwners.setSystemUpdatePolicy(policy);
             }
-            mDeviceOwner.writeDeviceOwner();
+            mOwners.writeDeviceOwner();
         }
         mContext.sendBroadcastAsUser(
                 new Intent(DevicePolicyManager.ACTION_SYSTEM_UPDATE_POLICY_CHANGED),
@@ -6325,7 +6324,7 @@
     @Override
     public SystemUpdatePolicy getSystemUpdatePolicy() {
         synchronized (this) {
-            SystemUpdatePolicy policy =  mDeviceOwner.getSystemUpdatePolicy();
+            SystemUpdatePolicy policy =  mOwners.getSystemUpdatePolicy();
             if (policy != null && !policy.isValid()) {
                 Slog.w(LOG_TAG, "Stored system update policy is invalid, return null instead.");
                 return null;
diff --git a/services/devicepolicy/java/com/android/server/devicepolicy/DeviceOwner.java b/services/devicepolicy/java/com/android/server/devicepolicy/Owners.java
similarity index 93%
rename from services/devicepolicy/java/com/android/server/devicepolicy/DeviceOwner.java
rename to services/devicepolicy/java/com/android/server/devicepolicy/Owners.java
index 791d0dd..1656716 100644
--- a/services/devicepolicy/java/com/android/server/devicepolicy/DeviceOwner.java
+++ b/services/devicepolicy/java/com/android/server/devicepolicy/Owners.java
@@ -27,11 +27,12 @@
 import android.os.Environment;
 import android.os.RemoteException;
 import android.os.UserManager;
+import android.util.ArrayMap;
 import android.util.AtomicFile;
+import android.util.Log;
 import android.util.Slog;
 import android.util.Xml;
 
-import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.util.FastXmlSerializer;
 
 import org.xmlpull.v1.XmlPullParser;
@@ -44,7 +45,6 @@
 import java.io.InputStream;
 import java.io.PrintWriter;
 import java.nio.charset.StandardCharsets;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -55,9 +55,11 @@
  * Stores and restores state for the Device and Profile owners. By definition there can be
  * only one device owner, but there may be a profile owner for each user.
  */
-class DeviceOwner {
+class Owners {
     private static final String TAG = "DevicePolicyManagerService";
 
+    private static final boolean DEBUG = false; // DO NOT SUBMIT WITH TRUE
+
     private static final String DEVICE_OWNER_XML_LEGACY = "device_owner.xml";
 
     private static final String DEVICE_OWNER_XML = "device_owner_2.xml";
@@ -87,12 +89,12 @@
     private OwnerInfo mDeviceInitializer;
 
     // Internal state for the profile owner packages.
-    private final HashMap<Integer, OwnerInfo> mProfileOwners = new HashMap<Integer, OwnerInfo>();
+    private final ArrayMap<Integer, OwnerInfo> mProfileOwners = new ArrayMap<>();
 
     // Local system update policy controllable by device owner.
     private SystemUpdatePolicy mSystemUpdatePolicy;
 
-    public DeviceOwner(Context context) {
+    public Owners(Context context) {
         mContext = context;
         mUserManager = UserManager.get(mContext);
     }
@@ -106,11 +108,18 @@
             final File legacy = getLegacyConfigFileWithTestOverride();
 
             if (readLegacyOwnerFile(legacy)) {
+                if (DEBUG) {
+                    Log.d(TAG, "Legacy config file found.");
+                }
+
                 // Legacy file exists, write to new files and remove the legacy one.
                 writeDeviceOwner();
                 for (int userId : getProfileOwnerKeys()) {
                     writeProfileOwner(userId);
                 }
+                if (DEBUG) {
+                    Log.d(TAG, "Deleting legacy config file");
+                }
                 if (!legacy.delete()) {
                     Slog.e(TAG, "Failed to remove the legacy setting file");
                 }
@@ -120,9 +129,9 @@
             // No legacy file, read from the new format files.
             new DeviceOwnerReadWriter().readFromFileLocked();
 
-            final List<UserInfo> users = mUserManager.getUsers();        // XXX double check this is the correct profile set.
+            final List<UserInfo> users = mUserManager.getUsers();
             for (UserInfo ui : users) {
-                new ProfileOwnerReadWriter(ui.id).readFromFileLocked();  // XXX double check ID is the right one.
+                new ProfileOwnerReadWriter(ui.id).readFromFileLocked();
             }
         }
     }
@@ -181,6 +190,11 @@
         return profileOwner != null ? profileOwner.name : null;
     }
 
+    String getProfileOwnerPackage(int userId) {
+        OwnerInfo profileOwner = mProfileOwners.get(userId);
+        return profileOwner != null ? profileOwner.packageName : null;
+    }
+
     Set<Integer> getProfileOwnerKeys() {
         return mProfileOwners.keySet();
     }
@@ -301,12 +315,18 @@
 
     void writeDeviceOwner() {
         synchronized (this) {
+            if (DEBUG) {
+                Log.d(TAG, "Writing to device owner file");
+            }
             new DeviceOwnerReadWriter().writeToFileLocked();
         }
     }
 
     void writeProfileOwner(int userId) {
         synchronized (this) {
+            if (DEBUG) {
+                Log.d(TAG, "Writing to profile owner file for user " + userId);
+            }
             new ProfileOwnerReadWriter(userId).writeToFileLocked();
         }
     }
@@ -322,14 +342,23 @@
 
         void writeToFileLocked() {
             if (!shouldWrite()) {
+                if (DEBUG) {
+                    Log.d(TAG, "No need to write to " + mFile);
+                }
                 // No contents, remove the file.
                 if (mFile.exists()) {
+                    if (DEBUG) {
+                        Log.d(TAG, "Deleting existing " + mFile);
+                    }
                     if (!mFile.delete()) {
                         Slog.e(TAG, "Failed to remove " + mFile.getPath());
                     }
                 }
                 return;
             }
+            if (DEBUG) {
+                Log.d(TAG, "Writing to " + mFile);
+            }
 
             final AtomicFile f = new AtomicFile(mFile);
             FileOutputStream outputStream = null;
@@ -364,8 +393,14 @@
 
         void readFromFileLocked() {
             if (!mFile.exists()) {
+                if (DEBUG) {
+                    Log.d(TAG, "" + mFile + " doesn't exist");
+                }
                 return;
             }
+            if (DEBUG) {
+                Log.d(TAG, "Reading from " + mFile);
+            }
             final AtomicFile f = new AtomicFile(mFile);
             InputStream input = null;
             try {
@@ -393,6 +428,7 @@
                             Slog.e(TAG, "Invalid root tag: " + tag);
                             return;
                         }
+                        continue;
                     }
                     // readInner() will only see START_TAG at depth >= 2.
                     if (!readInner(parser, depth, tag)) {
diff --git a/services/java/com/android/server/SystemServer.java b/services/java/com/android/server/SystemServer.java
index 21dd647b..aafaf83 100644
--- a/services/java/com/android/server/SystemServer.java
+++ b/services/java/com/android/server/SystemServer.java
@@ -38,6 +38,7 @@
 import android.os.StrictMode;
 import android.os.SystemClock;
 import android.os.SystemProperties;
+import android.os.Trace;
 import android.os.UserHandle;
 import android.os.storage.IMountService;
 import android.util.DisplayMetrics;
@@ -175,97 +176,103 @@
     }
 
     private void run() {
-        // If a device's clock is before 1970 (before 0), a lot of
-        // APIs crash dealing with negative numbers, notably
-        // java.io.File#setLastModified, so instead we fake it and
-        // hope that time from cell towers or NTP fixes it shortly.
-        if (System.currentTimeMillis() < EARLIEST_SUPPORTED_TIME) {
-            Slog.w(TAG, "System clock is before 1970; setting to 1970.");
-            SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME);
-        }
+        try {
+            Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "InitBeforeStartServices");
+            // If a device's clock is before 1970 (before 0), a lot of
+            // APIs crash dealing with negative numbers, notably
+            // java.io.File#setLastModified, so instead we fake it and
+            // hope that time from cell towers or NTP fixes it shortly.
+            if (System.currentTimeMillis() < EARLIEST_SUPPORTED_TIME) {
+                Slog.w(TAG, "System clock is before 1970; setting to 1970.");
+                SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME);
+            }
 
-        // If the system has "persist.sys.language" and friends set, replace them with
-        // "persist.sys.locale". Note that the default locale at this point is calculated
-        // using the "-Duser.locale" command line flag. That flag is usually populated by
-        // AndroidRuntime using the same set of system properties, but only the system_server
-        // and system apps are allowed to set them.
-        //
-        // NOTE: Most changes made here will need an equivalent change to
-        // core/jni/AndroidRuntime.cpp
-        if (!SystemProperties.get("persist.sys.language").isEmpty()) {
-            final String languageTag = Locale.getDefault().toLanguageTag();
+            // If the system has "persist.sys.language" and friends set, replace them with
+            // "persist.sys.locale". Note that the default locale at this point is calculated
+            // using the "-Duser.locale" command line flag. That flag is usually populated by
+            // AndroidRuntime using the same set of system properties, but only the system_server
+            // and system apps are allowed to set them.
+            //
+            // NOTE: Most changes made here will need an equivalent change to
+            // core/jni/AndroidRuntime.cpp
+            if (!SystemProperties.get("persist.sys.language").isEmpty()) {
+                final String languageTag = Locale.getDefault().toLanguageTag();
 
-            SystemProperties.set("persist.sys.locale", languageTag);
-            SystemProperties.set("persist.sys.language", "");
-            SystemProperties.set("persist.sys.country", "");
-            SystemProperties.set("persist.sys.localevar", "");
-        }
+                SystemProperties.set("persist.sys.locale", languageTag);
+                SystemProperties.set("persist.sys.language", "");
+                SystemProperties.set("persist.sys.country", "");
+                SystemProperties.set("persist.sys.localevar", "");
+            }
 
-        // Here we go!
-        Slog.i(TAG, "Entered the Android system server!");
-        EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_SYSTEM_RUN, SystemClock.uptimeMillis());
+            // Here we go!
+            Slog.i(TAG, "Entered the Android system server!");
+            EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_SYSTEM_RUN, SystemClock.uptimeMillis());
 
-        // In case the runtime switched since last boot (such as when
-        // the old runtime was removed in an OTA), set the system
-        // property so that it is in sync. We can't do this in
-        // libnativehelper's JniInvocation::Init code where we already
-        // had to fallback to a different runtime because it is
-        // running as root and we need to be the system user to set
-        // the property. http://b/11463182
-        SystemProperties.set("persist.sys.dalvik.vm.lib.2", VMRuntime.getRuntime().vmLibrary());
+            // In case the runtime switched since last boot (such as when
+            // the old runtime was removed in an OTA), set the system
+            // property so that it is in sync. We can't do this in
+            // libnativehelper's JniInvocation::Init code where we already
+            // had to fallback to a different runtime because it is
+            // running as root and we need to be the system user to set
+            // the property. http://b/11463182
+            SystemProperties.set("persist.sys.dalvik.vm.lib.2", VMRuntime.getRuntime().vmLibrary());
 
-        // Enable the sampling profiler.
-        if (SamplingProfilerIntegration.isEnabled()) {
-            SamplingProfilerIntegration.start();
-            mProfilerSnapshotTimer = new Timer();
-            mProfilerSnapshotTimer.schedule(new TimerTask() {
-                @Override
-                public void run() {
-                    SamplingProfilerIntegration.writeSnapshot("system_server", null);
-                }
-            }, SNAPSHOT_INTERVAL, SNAPSHOT_INTERVAL);
-        }
+            // Enable the sampling profiler.
+            if (SamplingProfilerIntegration.isEnabled()) {
+                SamplingProfilerIntegration.start();
+                mProfilerSnapshotTimer = new Timer();
+                mProfilerSnapshotTimer.schedule(new TimerTask() {
+                        @Override
+                        public void run() {
+                            SamplingProfilerIntegration.writeSnapshot("system_server", null);
+                        }
+                    }, SNAPSHOT_INTERVAL, SNAPSHOT_INTERVAL);
+            }
 
-        // Mmmmmm... more memory!
-        VMRuntime.getRuntime().clearGrowthLimit();
+            // Mmmmmm... more memory!
+            VMRuntime.getRuntime().clearGrowthLimit();
 
-        // The system server has to run all of the time, so it needs to be
-        // as efficient as possible with its memory usage.
-        VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
+            // The system server has to run all of the time, so it needs to be
+            // as efficient as possible with its memory usage.
+            VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
 
-        // Some devices rely on runtime fingerprint generation, so make sure
-        // we've defined it before booting further.
-        Build.ensureFingerprintProperty();
+            // Some devices rely on runtime fingerprint generation, so make sure
+            // we've defined it before booting further.
+            Build.ensureFingerprintProperty();
 
-        // Within the system server, it is an error to access Environment paths without
-        // explicitly specifying a user.
-        Environment.setUserRequired(true);
+            // Within the system server, it is an error to access Environment paths without
+            // explicitly specifying a user.
+            Environment.setUserRequired(true);
 
-        // Ensure binder calls into the system always run at foreground priority.
-        BinderInternal.disableBackgroundScheduling(true);
+            // Ensure binder calls into the system always run at foreground priority.
+            BinderInternal.disableBackgroundScheduling(true);
 
-        // Prepare the main looper thread (this thread).
-        android.os.Process.setThreadPriority(
+            // Prepare the main looper thread (this thread).
+            android.os.Process.setThreadPriority(
                 android.os.Process.THREAD_PRIORITY_FOREGROUND);
-        android.os.Process.setCanSelfBackground(false);
-        Looper.prepareMainLooper();
+            android.os.Process.setCanSelfBackground(false);
+            Looper.prepareMainLooper();
 
-        // Initialize native services.
-        System.loadLibrary("android_servers");
+            // Initialize native services.
+            System.loadLibrary("android_servers");
 
-        // Check whether we failed to shut down last time we tried.
-        // This call may not return.
-        performPendingShutdown();
+            // Check whether we failed to shut down last time we tried.
+            // This call may not return.
+            performPendingShutdown();
 
-        // Initialize the system context.
-        createSystemContext();
+            // Initialize the system context.
+            createSystemContext();
 
-        // Create the system service manager.
-        mSystemServiceManager = new SystemServiceManager(mSystemContext);
-        LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
+            // Create the system service manager.
+            mSystemServiceManager = new SystemServiceManager(mSystemContext);
+            LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
+        } finally {
+            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
+        }
 
         // Start services.
         try {
+            Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartServices");
             startBootstrapServices();
             startCoreServices();
             startOtherServices();
@@ -273,6 +280,8 @@
             Slog.e("System", "******************************************");
             Slog.e("System", "************ Failure starting system services", ex);
             throw ex;
+        } finally {
+            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
         }
 
         // For debug builds, log event loop stalls to dropbox for analysis.
@@ -340,7 +349,9 @@
 
         // Now that the power manager has been started, let the activity manager
         // initialize power management features.
+        Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "InitPowerManagement");
         mActivityManagerService.initPowerManagement();
+        Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
         // Manages LEDs and display backlight so we need it to bring up the display.
         mSystemServiceManager.startService(LightsService.class);
@@ -363,14 +374,16 @@
         }
 
         // Start the package manager.
-        Slog.i(TAG, "Package Manager");
+        traceBeginAndSlog("StartPackageManagerService");
         mPackageManagerService = PackageManagerService.main(mSystemContext, installer,
                 mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore);
         mFirstBoot = mPackageManagerService.isFirstBoot();
         mPackageManager = mSystemContext.getPackageManager();
+        Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
-        Slog.i(TAG, "User Service");
+        traceBeginAndSlog("StartUserManagerService");
         ServiceManager.addService(Context.USER_SERVICE, UserManagerService.getInstance());
+        Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
         // Initialize attribute cache used to cache resources from packages.
         AttributeCache.init(mSystemContext);
@@ -442,17 +455,20 @@
             Slog.i(TAG, "Reading configuration...");
             SystemConfig.getInstance();
 
-            Slog.i(TAG, "Scheduling Policy");
+            traceBeginAndSlog("StartSchedulingPolicyService");
             ServiceManager.addService("scheduling_policy", new SchedulingPolicyService());
+            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
             mSystemServiceManager.startService(TelecomLoaderService.class);
 
-            Slog.i(TAG, "Telephony Registry");
+            traceBeginAndSlog("StartTelephonyRegistry");
             telephonyRegistry = new TelephonyRegistry(context);
             ServiceManager.addService("telephony.registry", telephonyRegistry);
+            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
-            Slog.i(TAG, "Entropy Mixer");
+            traceBeginAndSlog("StartEntropyMixer");
             entropyMixer = new EntropyMixer(context);
+            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
             mContentResolver = context.getContentResolver();
 
@@ -460,47 +476,55 @@
             mSystemServiceManager.startService(CameraService.class);
 
             // The AccountManager must come before the ContentService
+            traceBeginAndSlog("StartAccountManagerService");
             try {
                 // TODO: seems like this should be disable-able, but req'd by ContentService
-                Slog.i(TAG, "Account Manager");
                 accountManager = new AccountManagerService(context);
                 ServiceManager.addService(Context.ACCOUNT_SERVICE, accountManager);
             } catch (Throwable e) {
                 Slog.e(TAG, "Failure starting Account Manager", e);
             }
+            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
-            Slog.i(TAG, "Content Manager");
+            traceBeginAndSlog("StartContentService");
             contentService = ContentService.main(context,
                     mFactoryTestMode == FactoryTest.FACTORY_TEST_LOW_LEVEL);
+            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
-            Slog.i(TAG, "System Content Providers");
+            traceBeginAndSlog("InstallSystemProviders");
             mActivityManagerService.installSystemProviders();
+            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
-            Slog.i(TAG, "Vibrator Service");
+            traceBeginAndSlog("StartVibratorService");
             vibrator = new VibratorService(context);
             ServiceManager.addService("vibrator", vibrator);
+            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
-            Slog.i(TAG, "Consumer IR Service");
+            traceBeginAndSlog("StartConsumerIrService");
             consumerIr = new ConsumerIrService(context);
             ServiceManager.addService(Context.CONSUMER_IR_SERVICE, consumerIr);
+            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
             mSystemServiceManager.startService(AlarmManagerService.class);
             alarm = IAlarmManager.Stub.asInterface(
                     ServiceManager.getService(Context.ALARM_SERVICE));
 
-            Slog.i(TAG, "Init Watchdog");
+            traceBeginAndSlog("InitWatchdog");
             final Watchdog watchdog = Watchdog.getInstance();
             watchdog.init(context, mActivityManagerService);
+            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
-            Slog.i(TAG, "Input Manager");
+            traceBeginAndSlog("StartInputManagerService");
             inputManager = new InputManagerService(context);
+            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
-            Slog.i(TAG, "Window Manager");
+            traceBeginAndSlog("StartWindowManagerService");
             wm = WindowManagerService.main(context, inputManager,
                     mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL,
                     !mFirstBoot, mOnlyCore);
             ServiceManager.addService(Context.WINDOW_SERVICE, wm);
             ServiceManager.addService(Context.INPUT_SERVICE, inputManager);
+            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
             mActivityManagerService.setWindowManager(wm);
 
@@ -523,7 +547,6 @@
             } else if (disableBluetooth) {
                 Slog.i(TAG, "Bluetooth Service disabled by config");
             } else {
-                Slog.i(TAG, "Bluetooth Service");
                 mSystemServiceManager.startService(BluetoothService.class);
             }
         } catch (RuntimeException e) {
@@ -544,21 +567,23 @@
 
         // Bring up services needed for UI.
         if (mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
+            traceBeginAndSlog("StartInputMethodManagerService");
             try {
-                Slog.i(TAG, "Input Method Service");
                 imm = new InputMethodManagerService(context);
                 ServiceManager.addService(Context.INPUT_METHOD_SERVICE, imm);
             } catch (Throwable e) {
                 reportWtf("starting Input Manager Service", e);
             }
+            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
+            traceBeginAndSlog("StartAccessibilityManagerService");
             try {
-                Slog.i(TAG, "Accessibility Manager");
                 ServiceManager.addService(Context.ACCESSIBILITY_SERVICE,
                         new AccessibilityManagerService(context));
             } catch (Throwable e) {
                 reportWtf("starting Accessibility Manager", e);
             }
+            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
         }
 
         try {
@@ -588,11 +613,13 @@
         // as appropriate.
         mSystemServiceManager.startService(UiModeManagerService.class);
 
+        Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "PerformBootDexOpt");
         try {
             mPackageManagerService.performBootDexOpt();
         } catch (Throwable e) {
             reportWtf("performing boot dexopt", e);
         }
+        Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
         try {
             ActivityManagerNative.getDefault().showBootMessage(
@@ -604,13 +631,14 @@
 
         if (mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
             if (!disableNonCoreServices) {
+                traceBeginAndSlog("StartLockSettingsService");
                 try {
-                    Slog.i(TAG,  "LockSettingsService");
                     lockSettings = new LockSettingsService(context);
                     ServiceManager.addService("lock_settings", lockSettings);
                 } catch (Throwable e) {
                     reportWtf("starting LockSettingsService service", e);
                 }
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
                 if (!SystemProperties.get(PERSISTENT_DATA_BLOCK_PROP).equals("")) {
                     mSystemServiceManager.startService(PersistentDataBlockService.class);
@@ -624,64 +652,70 @@
             }
 
             if (!disableSystemUI) {
+                traceBeginAndSlog("StartStatusBarManagerService");
                 try {
-                    Slog.i(TAG, "Status Bar");
                     statusBar = new StatusBarManagerService(context, wm);
                     ServiceManager.addService(Context.STATUS_BAR_SERVICE, statusBar);
                 } catch (Throwable e) {
                     reportWtf("starting StatusBarManagerService", e);
                 }
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
             }
 
             if (!disableNonCoreServices) {
+                traceBeginAndSlog("StartClipboardService");
                 try {
-                    Slog.i(TAG, "Clipboard Service");
                     ServiceManager.addService(Context.CLIPBOARD_SERVICE,
                             new ClipboardService(context));
                 } catch (Throwable e) {
                     reportWtf("starting Clipboard Service", e);
                 }
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
             }
 
             if (!disableNetwork) {
+                traceBeginAndSlog("StartNetworkManagementService");
                 try {
-                    Slog.i(TAG, "NetworkManagement Service");
                     networkManagement = NetworkManagementService.create(context);
                     ServiceManager.addService(Context.NETWORKMANAGEMENT_SERVICE, networkManagement);
                 } catch (Throwable e) {
                     reportWtf("starting NetworkManagement Service", e);
                 }
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
             }
 
             if (!disableNonCoreServices) {
+                traceBeginAndSlog("StartTextServicesManagerService");
                 try {
-                    Slog.i(TAG, "Text Service Manager Service");
                     tsms = new TextServicesManagerService(context);
                     ServiceManager.addService(Context.TEXT_SERVICES_MANAGER_SERVICE, tsms);
                 } catch (Throwable e) {
                     reportWtf("starting Text Service Manager Service", e);
                 }
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
             }
 
             if (!disableNetwork) {
+                traceBeginAndSlog("StartNetworkScoreService");
                 try {
-                    Slog.i(TAG, "Network Score Service");
                     networkScore = new NetworkScoreService(context);
                     ServiceManager.addService(Context.NETWORK_SCORE_SERVICE, networkScore);
                 } catch (Throwable e) {
                     reportWtf("starting Network Score Service", e);
                 }
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
+                traceBeginAndSlog("StartNetworkStatsService");
                 try {
-                    Slog.i(TAG, "NetworkStats Service");
                     networkStats = new NetworkStatsService(context, networkManagement, alarm);
                     ServiceManager.addService(Context.NETWORK_STATS_SERVICE, networkStats);
                 } catch (Throwable e) {
                     reportWtf("starting NetworkStats Service", e);
                 }
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
+                traceBeginAndSlog("StartNetworkPolicyManagerService");
                 try {
-                    Slog.i(TAG, "NetworkPolicy Service");
                     networkPolicy = new NetworkPolicyManagerService(
                             context, mActivityManagerService,
                             (IPowerManager)ServiceManager.getService(Context.POWER_SERVICE),
@@ -690,6 +724,7 @@
                 } catch (Throwable e) {
                     reportWtf("starting NetworkPolicy Service", e);
                 }
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
                 mSystemServiceManager.startService(WIFI_P2P_SERVICE_CLASS);
                 mSystemServiceManager.startService(WIFI_SERVICE_CLASS);
@@ -703,8 +738,8 @@
                     mSystemServiceManager.startService(ETHERNET_SERVICE_CLASS);
                 }
 
+                traceBeginAndSlog("StartConnectivityService");
                 try {
-                    Slog.i(TAG, "Connectivity Service");
                     connectivity = new ConnectivityService(
                             context, networkManagement, networkStats, networkPolicy);
                     ServiceManager.addService(Context.CONNECTIVITY_SERVICE, connectivity);
@@ -713,25 +748,28 @@
                 } catch (Throwable e) {
                     reportWtf("starting Connectivity Service", e);
                 }
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
+                traceBeginAndSlog("StartNsdService");
                 try {
-                    Slog.i(TAG, "Network Service Discovery Service");
                     serviceDiscovery = NsdService.create(context);
                     ServiceManager.addService(
                             Context.NSD_SERVICE, serviceDiscovery);
                 } catch (Throwable e) {
                     reportWtf("starting Service Discovery Service", e);
                 }
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
             }
 
             if (!disableNonCoreServices) {
+                traceBeginAndSlog("StartUpdateLockService");
                 try {
-                    Slog.i(TAG, "UpdateLock Service");
                     ServiceManager.addService(Context.UPDATE_LOCK_SERVICE,
                             new UpdateLockService(context));
                 } catch (Throwable e) {
                     reportWtf("starting UpdateLockService", e);
                 }
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
             }
 
             /*
@@ -740,25 +778,31 @@
              * first before continuing.
              */
             if (mountService != null && !mOnlyCore) {
+                Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "WaitForAsecScan");
                 try {
                     mountService.waitForAsecScan();
                 } catch (RemoteException ignored) {
                 }
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
             }
 
+            Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakeAccountManagerServiceReady");
             try {
                 if (accountManager != null)
                     accountManager.systemReady();
             } catch (Throwable e) {
                 reportWtf("making Account Manager Service ready", e);
             }
+            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
+            Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakeContentServiceReady");
             try {
                 if (contentService != null)
                     contentService.systemReady();
             } catch (Throwable e) {
                 reportWtf("making Content Service ready", e);
             }
+            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
             mSystemServiceManager.startService(NotificationManagerService.class);
             notification = INotificationManager.Stub.asInterface(
@@ -768,66 +812,72 @@
             mSystemServiceManager.startService(DeviceStorageMonitorService.class);
 
             if (!disableLocation) {
+                traceBeginAndSlog("StartLocationManagerService");
                 try {
-                    Slog.i(TAG, "Location Manager");
                     location = new LocationManagerService(context);
                     ServiceManager.addService(Context.LOCATION_SERVICE, location);
                 } catch (Throwable e) {
                     reportWtf("starting Location Manager", e);
                 }
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
+                traceBeginAndSlog("StartCountryDetectorService");
                 try {
-                    Slog.i(TAG, "Country Detector");
                     countryDetector = new CountryDetectorService(context);
                     ServiceManager.addService(Context.COUNTRY_DETECTOR, countryDetector);
                 } catch (Throwable e) {
                     reportWtf("starting Country Detector", e);
                 }
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
             }
 
             if (!disableNonCoreServices) {
+                traceBeginAndSlog("StartSearchManagerService");
                 try {
-                    Slog.i(TAG, "Search Service");
                     ServiceManager.addService(Context.SEARCH_SERVICE,
                             new SearchManagerService(context));
                 } catch (Throwable e) {
                     reportWtf("starting Search Service", e);
                 }
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
             }
 
             mSystemServiceManager.startService(DropBoxManagerService.class);
 
             if (!disableNonCoreServices && context.getResources().getBoolean(
                         R.bool.config_enableWallpaperService)) {
+                traceBeginAndSlog("StartWallpaperManagerService");
                 try {
-                    Slog.i(TAG, "Wallpaper Service");
                     wallpaper = new WallpaperManagerService(context);
                     ServiceManager.addService(Context.WALLPAPER_SERVICE, wallpaper);
                 } catch (Throwable e) {
                     reportWtf("starting Wallpaper Service", e);
                 }
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
             }
 
+            traceBeginAndSlog("StartAudioService");
             try {
-                Slog.i(TAG, "Audio Service");
                 audioService = new AudioService(context);
                 ServiceManager.addService(Context.AUDIO_SERVICE, audioService);
             } catch (Throwable e) {
                 reportWtf("starting Audio Service", e);
             }
+            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
             if (!disableNonCoreServices) {
                 mSystemServiceManager.startService(DockObserver.class);
             }
 
+            traceBeginAndSlog("StartWiredAccessoryManager");
             try {
-                Slog.i(TAG, "Wired Accessory Manager");
                 // Listen for wired headset changes
                 inputManager.setWiredAccessoryCallbacks(
                         new WiredAccessoryManager(context, inputManager));
             } catch (Throwable e) {
                 reportWtf("starting WiredAccessoryManager", e);
             }
+            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
             if (!disableNonCoreServices) {
                 if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_MIDI)) {
@@ -839,17 +889,20 @@
                         || mPackageManager.hasSystemFeature(
                                 PackageManager.FEATURE_USB_ACCESSORY)) {
                     // Manage USB host and device support
+                    Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartUsbService");
                     mSystemServiceManager.startService(USB_SERVICE_CLASS);
+                    Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
                 }
 
+                traceBeginAndSlog("StartSerialService");
                 try {
-                    Slog.i(TAG, "Serial Service");
                     // Serial port support
                     serial = new SerialService(context);
                     ServiceManager.addService(Context.SERIAL_SERVICE, serial);
                 } catch (Throwable e) {
                     Slog.e(TAG, "Failure starting SerialService", e);
                 }
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
             }
 
             mSystemServiceManager.startService(TwilightService.class);
@@ -875,49 +928,54 @@
                 }
             }
 
+            traceBeginAndSlog("StartDiskStatsService");
             try {
-                Slog.i(TAG, "DiskStats Service");
                 ServiceManager.addService("diskstats", new DiskStatsService(context));
             } catch (Throwable e) {
                 reportWtf("starting DiskStats Service", e);
             }
+            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
+            traceBeginAndSlog("StartSamplingProfilerService");
             try {
                 // need to add this service even if SamplingProfilerIntegration.isEnabled()
                 // is false, because it is this service that detects system property change and
                 // turns on SamplingProfilerIntegration. Plus, when sampling profiler doesn't work,
                 // there is little overhead for running this service.
-                Slog.i(TAG, "SamplingProfiler Service");
                 ServiceManager.addService("samplingprofiler",
                             new SamplingProfilerService(context));
             } catch (Throwable e) {
                 reportWtf("starting SamplingProfiler Service", e);
             }
+            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
             if (!disableNetwork && !disableNetworkTime) {
+                traceBeginAndSlog("StartNetworkTimeUpdateService");
                 try {
-                    Slog.i(TAG, "NetworkTimeUpdateService");
                     networkTimeUpdater = new NetworkTimeUpdateService(context);
                 } catch (Throwable e) {
                     reportWtf("starting NetworkTimeUpdate service", e);
                 }
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
             }
 
+            traceBeginAndSlog("StartCommonTimeManagementService");
             try {
-                Slog.i(TAG, "CommonTimeManagementService");
                 commonTimeMgmtService = new CommonTimeManagementService(context);
                 ServiceManager.addService("commontime_management", commonTimeMgmtService);
             } catch (Throwable e) {
                 reportWtf("starting CommonTimeManagementService service", e);
             }
+            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
             if (!disableNetwork) {
+                traceBeginAndSlog("CertBlacklister");
                 try {
-                    Slog.i(TAG, "CertBlacklister");
                     CertBlacklister blacklister = new CertBlacklister(context);
                 } catch (Throwable e) {
                     reportWtf("starting CertBlacklister", e);
                 }
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
             }
 
             if (!disableNonCoreServices) {
@@ -926,13 +984,14 @@
             }
 
             if (!disableNonCoreServices && ZygoteInit.PRELOAD_RESOURCES) {
+                traceBeginAndSlog("StartAssetAtlasService");
                 try {
-                    Slog.i(TAG, "Assets Atlas Service");
                     atlas = new AssetAtlasService(context);
                     ServiceManager.addService(AssetAtlasService.ASSET_ATLAS_SERVICE, atlas);
                 } catch (Throwable e) {
                     reportWtf("starting AssetAtlasService", e);
                 }
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
             }
 
             if (!disableNonCoreServices) {
@@ -957,24 +1016,26 @@
             }
 
             if (!disableNonCoreServices) {
+                traceBeginAndSlog("StartMediaRouterService");
                 try {
-                    Slog.i(TAG, "Media Router Service");
                     mediaRouter = new MediaRouterService(context);
                     ServiceManager.addService(Context.MEDIA_ROUTER_SERVICE, mediaRouter);
                 } catch (Throwable e) {
                     reportWtf("starting MediaRouterService", e);
                 }
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
                 mSystemServiceManager.startService(TrustManagerService.class);
 
                 mSystemServiceManager.startService(FingerprintService.class);
 
+                traceBeginAndSlog("StartBackgroundDexOptService");
                 try {
-                    Slog.i(TAG, "BackgroundDexOptService");
                     BackgroundDexOptService.schedule(context, 0);
                 } catch (Throwable e) {
                     reportWtf("starting BackgroundDexOptService", e);
                 }
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
             }
 
@@ -1002,12 +1063,15 @@
 
         // It is now time to start up the app processes...
 
+        Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakeVibratorServiceReady");
         try {
             vibrator.systemReady();
         } catch (Throwable e) {
             reportWtf("making Vibrator Service ready", e);
         }
+        Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
+        Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakeLockSettingsServiceReady");
         if (lockSettings != null) {
             try {
                 lockSettings.systemReady();
@@ -1015,17 +1079,20 @@
                 reportWtf("making Lock Settings Service ready", e);
             }
         }
+        Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
         // Needed by DevicePolicyManager for initialization
         mSystemServiceManager.startBootPhase(SystemService.PHASE_LOCK_SETTINGS_READY);
 
         mSystemServiceManager.startBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY);
 
+        Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakeWindowManagerServiceReady");
         try {
             wm.systemReady();
         } catch (Throwable e) {
             reportWtf("making Window Manager Service ready", e);
         }
+        Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
         if (safeMode) {
             mActivityManagerService.showSafeModeOverlay();
@@ -1046,25 +1113,32 @@
             systemTheme.rebase();
         }
 
+        Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakePowerManagerServiceReady");
         try {
             // TODO: use boot phase
             mPowerManagerService.systemReady(mActivityManagerService.getAppOpsService());
+            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
         } catch (Throwable e) {
             reportWtf("making Power Manager Service ready", e);
         }
+        Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
+        Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakePackageManagerServiceReady");
         try {
             mPackageManagerService.systemReady();
         } catch (Throwable e) {
             reportWtf("making Package Manager Service ready", e);
         }
+        Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
+        Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakeDisplayManagerServiceReady");
         try {
             // TODO: use boot phase and communicate these flags some other way
             mDisplayManagerService.systemReady(safeMode, mOnlyCore);
         } catch (Throwable e) {
             reportWtf("making Display Manager Service ready", e);
         }
+        Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
         // These are needed to propagate to the runnable below.
         final NetworkManagementService networkManagementF = networkManagement;
@@ -1098,55 +1172,76 @@
                 Slog.i(TAG, "Making services ready");
                 mSystemServiceManager.startBootPhase(
                         SystemService.PHASE_ACTIVITY_MANAGER_READY);
+                Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "PhaseActivityManagerReady");
 
+                Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartObservingNativeCrashes");
                 try {
                     mActivityManagerService.startObservingNativeCrashes();
                 } catch (Throwable e) {
                     reportWtf("observing native crashes", e);
                 }
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
                 Slog.i(TAG, "WebViewFactory preparation");
+                Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "WebViewFactoryPreparation");
                 WebViewFactory.prepareWebViewInSystemServer();
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
+                Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartSystemUI");
                 try {
                     startSystemUi(context);
                 } catch (Throwable e) {
                     reportWtf("starting System UI", e);
                 }
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
+                Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakeMountServiceReady");
                 try {
                     if (networkScoreF != null) networkScoreF.systemReady();
                 } catch (Throwable e) {
                     reportWtf("making Network Score Service ready", e);
                 }
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
+                Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakeNetworkManagementServiceReady");
                 try {
                     if (networkManagementF != null) networkManagementF.systemReady();
                 } catch (Throwable e) {
                     reportWtf("making Network Managment Service ready", e);
                 }
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
+                Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakeNetworkStatsServiceReady");
                 try {
                     if (networkStatsF != null) networkStatsF.systemReady();
                 } catch (Throwable e) {
                     reportWtf("making Network Stats Service ready", e);
                 }
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
+                Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakeNetworkPolicyServiceReady");
                 try {
                     if (networkPolicyF != null) networkPolicyF.systemReady();
                 } catch (Throwable e) {
                     reportWtf("making Network Policy Service ready", e);
                 }
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
+                Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakeConnectivityServiceReady");
                 try {
                     if (connectivityF != null) connectivityF.systemReady();
                 } catch (Throwable e) {
                     reportWtf("making Connectivity Service ready", e);
                 }
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
+                Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakeAudioServiceReady");
                 try {
                     if (audioServiceF != null) audioServiceF.systemReady();
                 } catch (Throwable e) {
                     reportWtf("Notifying AudioService running", e);
                 }
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
                 Watchdog.getInstance().start();
 
                 // It is now okay to let the various system services start their
                 // third party code...
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
+                Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "PhaseThirdPartyAppsCanStart");
                 mSystemServiceManager.startBootPhase(
                         SystemService.PHASE_THIRD_PARTY_APPS_CAN_START);
 
@@ -1215,6 +1310,7 @@
                 } catch (Throwable e) {
                     reportWtf("Notifying MmsService running", e);
                 }
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
             }
         });
     }
@@ -1226,4 +1322,9 @@
         //Slog.d(TAG, "Starting service: " + intent);
         context.startServiceAsUser(intent, UserHandle.SYSTEM);
     }
+
+    private static void traceBeginAndSlog(String name) {
+        Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, name);
+        Slog.i(TAG, name);
+    }
 }
diff --git a/services/net/java/android/net/dhcp/DhcpPacket.java b/services/net/java/android/net/dhcp/DhcpPacket.java
index a91ddb8..cbf8fc2 100644
--- a/services/net/java/android/net/dhcp/DhcpPacket.java
+++ b/services/net/java/android/net/dhcp/DhcpPacket.java
@@ -55,6 +55,7 @@
     public static final int MIN_PACKET_LENGTH_L3 = MIN_PACKET_LENGTH_BOOTP + 20 + 8;
     public static final int MIN_PACKET_LENGTH_L2 = MIN_PACKET_LENGTH_L3 + 14;
 
+    public static final int HWADDR_LEN = 16;
     public static final int MAX_OPTION_LEN = 255;
     /**
      * IP layer definitions.
@@ -399,7 +400,7 @@
         buf.put(mRelayIp.getAddress());
         buf.put(mClientMac);
         buf.position(buf.position() +
-                     (16 - mClientMac.length) // pad addr to 16 bytes
+                     (HWADDR_LEN - mClientMac.length) // pad addr to 16 bytes
                      + 64     // empty server host name (64 bytes)
                      + 128);  // empty boot file name (128 bytes)
         buf.putInt(0x63825363); // magic number
@@ -786,7 +787,7 @@
 
         byte type = packet.get();
         byte hwType = packet.get();
-        byte addrLen = packet.get();
+        int addrLen = packet.get() & 0xff;
         byte hops = packet.get();
         transactionId = packet.getInt();
         secs = packet.getShort();
@@ -807,6 +808,16 @@
             return null;
         }
 
+        // Some DHCP servers have been known to announce invalid client hardware address values such
+        // as 0xff. The legacy DHCP client accepted these becuause it does not check the length at
+        // all but only checks that the interface MAC address matches the first bytes of the address
+        // in the packets. We're a bit stricter: if the length is obviously invalid (i.e., bigger
+        // than the size of the field), we fudge it to 6 (Ethernet). http://b/23725795
+        // TODO: evaluate whether to make this test more liberal.
+        if (addrLen > HWADDR_LEN) {
+            addrLen = ETHER_BROADCAST.length;
+        }
+
         clientMac = new byte[addrLen];
         packet.get(clientMac);
 
diff --git a/services/tests/servicestests/assets/OwnersTest/test01/input.xml b/services/tests/servicestests/assets/OwnersTest/test01/input.xml
new file mode 100644
index 0000000..db3e974
--- /dev/null
+++ b/services/tests/servicestests/assets/OwnersTest/test01/input.xml
@@ -0,0 +1 @@
+<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
diff --git a/services/tests/servicestests/assets/OwnersTest/test02/input.xml b/services/tests/servicestests/assets/OwnersTest/test02/input.xml
new file mode 100644
index 0000000..321842b
--- /dev/null
+++ b/services/tests/servicestests/assets/OwnersTest/test02/input.xml
@@ -0,0 +1,2 @@
+<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
+<device-owner package="com.google.android.testdpc" />
diff --git a/services/tests/servicestests/assets/OwnersTest/test03/input.xml b/services/tests/servicestests/assets/OwnersTest/test03/input.xml
new file mode 100644
index 0000000..1bbfdadf
--- /dev/null
+++ b/services/tests/servicestests/assets/OwnersTest/test03/input.xml
@@ -0,0 +1,3 @@
+<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
+<profile-owner package="com.google.android.testdpc0" name="0" userId="10" component="com.google.android.testdpc/com.google.android.testdpc.DeviceAdminReceiver0" />
+<profile-owner package="com.google.android.testdpc1" name="1" userId="11" />
diff --git a/services/tests/servicestests/assets/OwnersTest/test04/input.xml b/services/tests/servicestests/assets/OwnersTest/test04/input.xml
new file mode 100644
index 0000000..8be51d9
--- /dev/null
+++ b/services/tests/servicestests/assets/OwnersTest/test04/input.xml
@@ -0,0 +1,6 @@
+<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
+<device-owner package="com.google.android.testdpc" />
+<profile-owner package="com.google.android.testdpc0" name="0" userId="10" component="com.google.android.testdpc/com.google.android.testdpc.DeviceAdminReceiver0" />
+<profile-owner package="com.google.android.testdpc1" name="1" userId="11" />
+<device-initializer package="com.google.android.testdpcx" name="di" component="com.google.android.testdpcx/receiver" />
+<system-update-policy policy_type="5" />
diff --git a/services/tests/servicestests/assets/OwnersTest/test05/input.xml b/services/tests/servicestests/assets/OwnersTest/test05/input.xml
new file mode 100644
index 0000000..dbcb858
--- /dev/null
+++ b/services/tests/servicestests/assets/OwnersTest/test05/input.xml
@@ -0,0 +1,3 @@
+<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
+<device-initializer package="com.google.android.testdpcx" name="di" component="com.google.android.testdpcx/receiver" />
+
diff --git a/services/tests/servicestests/assets/OwnersTest/test06/input.xml b/services/tests/servicestests/assets/OwnersTest/test06/input.xml
new file mode 100644
index 0000000..794622b
--- /dev/null
+++ b/services/tests/servicestests/assets/OwnersTest/test06/input.xml
@@ -0,0 +1,2 @@
+<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
+<system-update-policy policy_type="5" />
diff --git a/services/tests/servicestests/src/android/net/dhcp/DhcpPacketTest.java b/services/tests/servicestests/src/android/net/dhcp/DhcpPacketTest.java
index da1df1a..cd3b8bb 100644
--- a/services/tests/servicestests/src/android/net/dhcp/DhcpPacketTest.java
+++ b/services/tests/servicestests/src/android/net/dhcp/DhcpPacketTest.java
@@ -333,6 +333,76 @@
     }
 
     @SmallTest
+    public void testBadHwaddrLength() throws Exception {
+        final ByteBuffer packet = ByteBuffer.wrap(HexEncoding.decode((
+            // IP header.
+            "450001518d0600004011144dc0a82b01c0a82bf7" +
+            // UDP header.
+            "00430044013d9ac7" +
+            // BOOTP header.
+            "02010600dfc23d1f0002000000000000c0a82bf7c0a82b0100000000" +
+            // MAC address.
+            "30766ff2a90c00000000000000000000" +
+            // Server name.
+            "0000000000000000000000000000000000000000000000000000000000000000" +
+            "0000000000000000000000000000000000000000000000000000000000000000" +
+            // File.
+            "0000000000000000000000000000000000000000000000000000000000000000" +
+            "0000000000000000000000000000000000000000000000000000000000000000" +
+            "0000000000000000000000000000000000000000000000000000000000000000" +
+            "0000000000000000000000000000000000000000000000000000000000000000" +
+            // Options
+            "638253633501023604c0a82b01330400000e103a04000007083b0400000c4e0104ffffff00" +
+            "1c04c0a82bff0304c0a82b010604c0a82b012b0f414e44524f49445f4d455445524544ff"
+        ).toCharArray(), false));
+        String expectedClientMac = "30766FF2A90C";
+
+        final int hwAddrLenOffset = 20 + 8 + 2;
+        assertEquals(6, packet.get(hwAddrLenOffset));
+
+        // Expect the expected.
+        DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
+        assertNotNull(offerPacket);
+        assertEquals(6, offerPacket.getClientMac().length);
+        assertEquals(expectedClientMac, HexDump.toHexString(offerPacket.getClientMac()));
+
+        // Reduce the hardware address length and verify that it shortens the client MAC.
+        packet.flip();
+        packet.put(hwAddrLenOffset, (byte) 5);
+        offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
+        assertNotNull(offerPacket);
+        assertEquals(5, offerPacket.getClientMac().length);
+        assertEquals(expectedClientMac.substring(0, 10),
+                HexDump.toHexString(offerPacket.getClientMac()));
+
+        packet.flip();
+        packet.put(hwAddrLenOffset, (byte) 3);
+        offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
+        assertNotNull(offerPacket);
+        assertEquals(3, offerPacket.getClientMac().length);
+        assertEquals(expectedClientMac.substring(0, 6),
+                HexDump.toHexString(offerPacket.getClientMac()));
+
+        // Set the the hardware address length to 0xff and verify that we a) don't treat it as -1
+        // and crash, and b) hardcode it to 6.
+        packet.flip();
+        packet.put(hwAddrLenOffset, (byte) -1);
+        offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
+        assertNotNull(offerPacket);
+        assertEquals(6, offerPacket.getClientMac().length);
+        assertEquals(expectedClientMac, HexDump.toHexString(offerPacket.getClientMac()));
+
+        // Set the the hardware address length to a positive invalid value (> 16) and verify that we
+        // hardcode it to 6.
+        packet.flip();
+        packet.put(hwAddrLenOffset, (byte) 17);
+        offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
+        assertNotNull(offerPacket);
+        assertEquals(6, offerPacket.getClientMac().length);
+        assertEquals(expectedClientMac, HexDump.toHexString(offerPacket.getClientMac()));
+    }
+
+    @SmallTest
     public void testPadAndOverloadedOptionsOffer() throws Exception {
         // A packet observed in the real world that is interesting for two reasons:
         //
diff --git a/services/tests/servicestests/src/com/android/server/AccessibilityManagerServiceTest.java b/services/tests/servicestests/src/com/android/server/AccessibilityManagerServiceTest.java
index fd9fc98..51e14d3 100644
--- a/services/tests/servicestests/src/com/android/server/AccessibilityManagerServiceTest.java
+++ b/services/tests/servicestests/src/com/android/server/AccessibilityManagerServiceTest.java
@@ -54,7 +54,7 @@
      * Timeout in which we are waiting for the system to start the mock
      * accessibility services.
      */
-    private static final long TIMEOUT_START_MOCK_ACCESSIBILITY_SERVICES = 300;
+    private static final long TIMEOUT_START_MOCK_ACCESSIBILITY_SERVICES = 1000;
 
     /**
      * Timeout used for testing that a service is notified only upon a
@@ -68,6 +68,12 @@
     private IAccessibilityManager mManagerService;
 
     @Override
+    protected void setUp() throws Exception {
+        // Reset the state.
+        ensureOnlyMockServicesEnabled(getContext(), false, false);
+    }
+
+    @Override
     public void setContext(Context context) {
         super.setContext(context);
         if (MyFirstMockAccessibilityService.sComponentName == null) {
@@ -92,6 +98,9 @@
 
     @LargeTest
     public void testAddClient_AccessibilityDisabledThenEnabled() throws Exception {
+        // at least some service must be enabled, otherwise accessibility will always be disabled.
+        ensureOnlyMockServicesEnabled(mContext, true, false);
+
         // make sure accessibility is disabled
         ensureAccessibilityEnabled(mContext, false);
 
@@ -99,7 +108,8 @@
         MyMockAccessibilityManagerClient mockClient = new MyMockAccessibilityManagerClient();
 
         // invoke the method under test
-        final int stateFlagsDisabled = mManagerService.addClient(mockClient, UserHandle.USER_OWNER);
+        final int stateFlagsDisabled =
+                mManagerService.addClient(mockClient, UserHandle.USER_CURRENT);
         boolean enabledAccessibilityDisabled =
             (stateFlagsDisabled & AccessibilityManager.STATE_FLAG_ACCESSIBILITY_ENABLED) != 0;
 
@@ -111,11 +121,11 @@
         ensureAccessibilityEnabled(mContext, true);
 
         // invoke the method under test
-        final int stateFlagsEnabled = mManagerService.addClient(mockClient, UserHandle.USER_OWNER);
+        final int stateFlagsEnabled =
+                mManagerService.addClient(mockClient, UserHandle.USER_CURRENT);
         boolean enabledAccessibilityEnabled =
             (stateFlagsEnabled & AccessibilityManager.STATE_FLAG_ACCESSIBILITY_ENABLED) != 0;
 
-
         // check expected result
         assertTrue("The client must be enabled since accessibility is enabled.",
                 enabledAccessibilityEnabled);
@@ -123,6 +133,9 @@
 
     @LargeTest
     public void testAddClient_AccessibilityEnabledThenDisabled() throws Exception {
+        // at least some service must be enabled, otherwise accessibility will always be disabled.
+        ensureOnlyMockServicesEnabled(mContext, true, false);
+
         // enable accessibility before registering the client
         ensureAccessibilityEnabled(mContext, true);
 
@@ -130,7 +143,8 @@
         MyMockAccessibilityManagerClient mockClient = new MyMockAccessibilityManagerClient();
 
         // invoke the method under test
-        final int stateFlagsEnabled = mManagerService.addClient(mockClient, UserHandle.USER_OWNER);
+        final int stateFlagsEnabled =
+                mManagerService.addClient(mockClient, UserHandle.USER_CURRENT);
         boolean enabledAccessibilityEnabled =
             (stateFlagsEnabled & AccessibilityManager.STATE_FLAG_ACCESSIBILITY_ENABLED) != 0;
 
@@ -142,7 +156,8 @@
         ensureAccessibilityEnabled(mContext, false);
 
         // invoke the method under test
-        final int stateFlagsDisabled = mManagerService.addClient(mockClient, UserHandle.USER_OWNER);
+        final int stateFlagsDisabled =
+                mManagerService.addClient(mockClient, UserHandle.USER_CURRENT);
         boolean enabledAccessibilityDisabled =
             (stateFlagsDisabled & AccessibilityManager.STATE_FLAG_ACCESSIBILITY_ENABLED) != 0;
 
@@ -162,7 +177,7 @@
 
         // look for the two mock services
         for (AccessibilityServiceInfo info : mManagerService.getInstalledAccessibilityServiceList(
-                UserHandle.USER_OWNER)) {
+                UserHandle.USER_CURRENT)) {
             ServiceInfo serviceInfo = info.getResolveInfo().serviceInfo;
             if (packageName.equals(serviceInfo.packageName)) {
                 if (firstMockServiceClassName.equals(serviceInfo.name)) {
@@ -181,12 +196,12 @@
     @LargeTest
     public void testSendAccessibilityEvent_OneService_MatchingPackageAndEventType()
             throws Exception {
-        // set the accessibility setting value
-        ensureAccessibilityEnabled(mContext, true);
-
         // enable the mock accessibility service
         ensureOnlyMockServicesEnabled(mContext, true, false);
 
+        // set the accessibility setting value
+        ensureAccessibilityEnabled(mContext, true);
+
         // configure the mock service
         MockAccessibilityService service = MyFirstMockAccessibilityService.sInstance;
         service.setServiceInfo(MockAccessibilityService.createDefaultInfo());
@@ -203,7 +218,7 @@
         service.replay();
 
         // send the event
-        mManagerService.sendAccessibilityEvent(sentEvent, UserHandle.USER_OWNER);
+        mManagerService.sendAccessibilityEvent(sentEvent, UserHandle.USER_CURRENT);
 
         // verify if all expected methods have been called
         assertMockServiceVerifiedWithinTimeout(service);
@@ -211,12 +226,12 @@
 
     @LargeTest
     public void testSendAccessibilityEvent_OneService_NotMatchingPackage() throws Exception {
-        // set the accessibility setting value
-        ensureAccessibilityEnabled(mContext, true);
-
         // enable the mock accessibility service
         ensureOnlyMockServicesEnabled(mContext, true, false);
 
+        // set the accessibility setting value
+        ensureAccessibilityEnabled(mContext, true);
+
         // configure the mock service
         MockAccessibilityService service = MyFirstMockAccessibilityService.sInstance;
         service.setServiceInfo(MockAccessibilityService.createDefaultInfo());
@@ -233,7 +248,7 @@
         service.replay();
 
         // send the event
-        mManagerService.sendAccessibilityEvent(sentEvent, UserHandle.USER_OWNER);
+        mManagerService.sendAccessibilityEvent(sentEvent, UserHandle.USER_CURRENT);
 
         // verify if all expected methods have been called
         assertMockServiceVerifiedWithinTimeout(service);
@@ -241,12 +256,12 @@
 
     @LargeTest
     public void testSendAccessibilityEvent_OneService_NotMatchingEventType() throws Exception {
-        // set the accessibility setting value
-        ensureAccessibilityEnabled(mContext, true);
-
         // enable the mock accessibility service
         ensureOnlyMockServicesEnabled(mContext, true, false);
 
+        // set the accessibility setting value
+        ensureAccessibilityEnabled(mContext, true);
+
         // configure the mock service
         MockAccessibilityService service = MyFirstMockAccessibilityService.sInstance;
         service.setServiceInfo(MockAccessibilityService.createDefaultInfo());
@@ -263,7 +278,7 @@
         service.replay();
 
         // send the event
-        mManagerService.sendAccessibilityEvent(sentEvent, UserHandle.USER_OWNER);
+        mManagerService.sendAccessibilityEvent(sentEvent, UserHandle.USER_CURRENT);
 
         // verify if all expected methods have been called
         assertMockServiceVerifiedWithinTimeout(service);
@@ -271,12 +286,12 @@
 
     @LargeTest
     public void testSendAccessibilityEvent_OneService_NotifivationAfterTimeout() throws Exception {
-        // set the accessibility setting value
-        ensureAccessibilityEnabled(mContext, true);
-
         // enable the mock accessibility service
         ensureOnlyMockServicesEnabled(mContext, true, false);
 
+        // set the accessibility setting value
+        ensureAccessibilityEnabled(mContext, true);
+
         // configure the mock service
         MockAccessibilityService service = MyFirstMockAccessibilityService.sInstance;
         AccessibilityServiceInfo info = MockAccessibilityService.createDefaultInfo();
@@ -299,8 +314,8 @@
         service.replay();
 
         // send the events
-        mManagerService.sendAccessibilityEvent(firstEvent, UserHandle.USER_OWNER);
-        mManagerService.sendAccessibilityEvent(secondEvent, UserHandle.USER_OWNER);
+        mManagerService.sendAccessibilityEvent(firstEvent, UserHandle.USER_CURRENT);
+        mManagerService.sendAccessibilityEvent(secondEvent, UserHandle.USER_CURRENT);
 
         // wait for #sendAccessibilityEvent to reach the backing service
         Thread.sleep(TIMEOUT_BINDER_CALL);
@@ -322,12 +337,12 @@
     @LargeTest
     public void testSendAccessibilityEvent_TwoServices_MatchingPackageAndEventType_DiffFeedback()
             throws Exception {
-        // set the accessibility setting value
-        ensureAccessibilityEnabled(mContext, true);
-
         // enable the mock accessibility services
         ensureOnlyMockServicesEnabled(mContext, true, true);
 
+        // set the accessibility setting value
+        ensureAccessibilityEnabled(mContext, true);
+
         // configure the first mock service
         MockAccessibilityService firstService = MyFirstMockAccessibilityService.sInstance;
         AccessibilityServiceInfo firstInfo = MockAccessibilityService.createDefaultInfo();
@@ -356,7 +371,7 @@
         secondService.replay();
 
         // send the event
-        mManagerService.sendAccessibilityEvent(sentEvent, UserHandle.USER_OWNER);
+        mManagerService.sendAccessibilityEvent(sentEvent, UserHandle.USER_CURRENT);
 
         // verify if all expected methods have been called
         assertMockServiceVerifiedWithinTimeout(firstService);
@@ -366,12 +381,12 @@
     @LargeTest
     public void testSendAccessibilityEvent_TwoServices_MatchingPackageAndEventType()
             throws Exception {
-        // set the accessibility setting value
-        ensureAccessibilityEnabled(mContext, true);
-
         // enable the mock accessibility services
         ensureOnlyMockServicesEnabled(mContext, true, true);
 
+        // set the accessibility setting value
+        ensureAccessibilityEnabled(mContext, true);
+
         // configure the first mock service
         MockAccessibilityService firstService = MyFirstMockAccessibilityService.sInstance;
         firstService.setServiceInfo(MockAccessibilityService.createDefaultInfo());
@@ -395,7 +410,7 @@
         secondService.replay();
 
         // send the event
-        mManagerService.sendAccessibilityEvent(sentEvent, UserHandle.USER_OWNER);
+        mManagerService.sendAccessibilityEvent(sentEvent, UserHandle.USER_CURRENT);
 
         // verify if all expected methods have been called
         assertMockServiceVerifiedWithinTimeout(firstService);
@@ -405,12 +420,12 @@
     @LargeTest
     public void testSendAccessibilityEvent_TwoServices_MatchingPackageAndEventType_OneDefault()
             throws Exception {
-        // set the accessibility setting value
-        ensureAccessibilityEnabled(mContext, true);
-
         // enable the mock accessibility services
         ensureOnlyMockServicesEnabled(mContext, true, true);
 
+        // set the accessibility setting value
+        ensureAccessibilityEnabled(mContext, true);
+
         // configure the first mock service
         MockAccessibilityService firstService = MyFirstMockAccessibilityService.sInstance;
         AccessibilityServiceInfo firstInfo = MyFirstMockAccessibilityService.createDefaultInfo();
@@ -436,7 +451,7 @@
         secondService.replay();
 
         // send the event
-        mManagerService.sendAccessibilityEvent(sentEvent, UserHandle.USER_OWNER);
+        mManagerService.sendAccessibilityEvent(sentEvent, UserHandle.USER_CURRENT);
 
         // verify if all expected methods have been called
         assertMockServiceVerifiedWithinTimeout(firstService);
@@ -446,12 +461,12 @@
     @LargeTest
     public void testSendAccessibilityEvent_TwoServices_MatchingPackageAndEventType_TwoDefault()
             throws Exception {
-        // set the accessibility setting value
-        ensureAccessibilityEnabled(mContext, true);
-
         // enable the mock accessibility services
         ensureOnlyMockServicesEnabled(mContext, true, true);
 
+        // set the accessibility setting value
+        ensureAccessibilityEnabled(mContext, true);
+
         // configure the first mock service
         MockAccessibilityService firstService = MyFirstMockAccessibilityService.sInstance;
         AccessibilityServiceInfo firstInfo = MyFirstMockAccessibilityService.createDefaultInfo();
@@ -479,7 +494,7 @@
         secondService.replay();
 
         // send the event
-        mManagerService.sendAccessibilityEvent(sentEvent, UserHandle.USER_OWNER);
+        mManagerService.sendAccessibilityEvent(sentEvent, UserHandle.USER_CURRENT);
 
         // verify if all expected methods have been called
         assertMockServiceVerifiedWithinTimeout(firstService);
@@ -488,12 +503,12 @@
 
     @LargeTest
     public void testInterrupt() throws Exception {
-        // set the accessibility setting value
-        ensureAccessibilityEnabled(mContext, true);
-
         // enable the mock accessibility services
         ensureOnlyMockServicesEnabled(mContext, true, true);
 
+        // set the accessibility setting value
+        ensureAccessibilityEnabled(mContext, true);
+
         // configure the first mock service
         MockAccessibilityService firstService = MyFirstMockAccessibilityService.sInstance;
         firstService.setServiceInfo(MockAccessibilityService.createDefaultInfo());
@@ -514,7 +529,7 @@
         secondService.replay();
 
         // call the method under test
-        mManagerService.interrupt(UserHandle.USER_OWNER);
+        mManagerService.interrupt(UserHandle.USER_CURRENT);
 
         // verify if all expected methods have been called
         assertMockServiceVerifiedWithinTimeout(firstService);
@@ -534,7 +549,7 @@
         sentEvent.setContentDescription("ContentDescription");
         sentEvent.setCurrentItemIndex(1);
         sentEvent.setEnabled(true);
-        sentEvent.setEventType(AccessibilityEvent.TYPE_VIEW_CLICKED);
+        sentEvent.setEventType(AccessibilityEvent.TYPE_ANNOUNCEMENT);
         sentEvent.setEventTime(1000);
         sentEvent.setFromIndex(1);
         sentEvent.setFullScreen(true);
@@ -568,8 +583,8 @@
      * @throws Exception If any error occurs.
      */
     private void ensureAccessibilityEnabled(Context context, boolean enabled) throws Exception {
-        boolean isEnabled = (Settings.Secure.getInt(context.getContentResolver(),
-                Settings.Secure.ACCESSIBILITY_ENABLED, 0) == 1 ? true : false);
+        boolean isEnabled = Settings.Secure.getInt(context.getContentResolver(),
+                Settings.Secure.ACCESSIBILITY_ENABLED, 0) == 1;
 
         if (isEnabled == enabled) {
             return;
@@ -608,13 +623,14 @@
             servicesToEnable.append(MySecondMockAccessibilityService.sComponentName).append(":");
         }
 
+        Settings.Secure.putString(context.getContentResolver(),
+                Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, servicesToEnable.toString());
+
+        // Optimization. If things will not change, we don't have to do anything.
         if (servicesToEnable.equals(enabledServices)) {
             return;
         }
 
-        Settings.Secure.putString(context.getContentResolver(),
-                Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, servicesToEnable.toString());
-
         // we have enabled the services of interest and need to wait until they
         // are instantiated and started (if needed) and the system binds to them
         boolean firstMockServiceOK = false;
@@ -664,13 +680,13 @@
             throws Exception {
         Exception lastVerifyException = null;
         long beginTime = SystemClock.uptimeMillis();
-        long pollTmeout = TIMEOUT_BINDER_CALL / 5;
+        long pollTimeout = TIMEOUT_BINDER_CALL / 5;
 
         // poll until the timeout has elapsed
         while (SystemClock.uptimeMillis() - beginTime < TIMEOUT_BINDER_CALL) {
             // sleep first since immediate call will always fail
             try {
-                Thread.sleep(pollTmeout);
+                Thread.sleep(pollTimeout);
             } catch (InterruptedException ie) {
                 /* ignore */
             }
diff --git a/services/tests/servicestests/src/com/android/server/AccessibilityManagerTest.java b/services/tests/servicestests/src/com/android/server/AccessibilityManagerTest.java
index e7366ea..026a2ad 100644
--- a/services/tests/servicestests/src/com/android/server/AccessibilityManagerTest.java
+++ b/services/tests/servicestests/src/com/android/server/AccessibilityManagerTest.java
@@ -16,14 +16,11 @@
 
 package com.android.server;
 
-import static org.easymock.EasyMock.createStrictMock;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.replay;
-import static org.easymock.EasyMock.reportMatcher;
-import static org.easymock.EasyMock.reset;
-import static org.easymock.EasyMock.verify;
-
-import org.easymock.IArgumentMatcher;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyInt;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
 import android.accessibilityservice.AccessibilityServiceInfo;
 import android.os.UserHandle;
@@ -35,6 +32,9 @@
 import android.view.accessibility.IAccessibilityManager;
 import android.view.accessibility.IAccessibilityManagerClient;
 
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
 import java.util.ArrayList;
 import java.util.List;
 
@@ -49,78 +49,65 @@
      */
     public static final long TIMEOUT_BINDER_CALL = 50;
 
-    /**
-     * The reusable mock {@link IAccessibilityManager}.
-     */
-    private final IAccessibilityManager mMockServiceInterface =
-        createStrictMock(IAccessibilityManager.class);
+    @Mock
+    private IAccessibilityManager mMockService;
 
     @Override
     public void setUp() throws Exception {
-        reset(mMockServiceInterface);
+        MockitoAnnotations.initMocks(this);
+    }
+
+    private AccessibilityManager createManager(boolean enabled) throws Exception {
+        if (enabled) {
+            when(mMockService.addClient(any(IAccessibilityManagerClient.class), anyInt()))
+                    .thenReturn(AccessibilityManager.STATE_FLAG_ACCESSIBILITY_ENABLED);
+        } else {
+            when(mMockService.addClient(any(IAccessibilityManagerClient.class), anyInt()))
+                    .thenReturn(0);
+        }
+
+        AccessibilityManager manager =
+                new AccessibilityManager(mContext, mMockService, UserHandle.USER_CURRENT);
+
+        verify(mMockService).addClient(any(IAccessibilityManagerClient.class), anyInt());
+
+        return manager;
     }
 
     @MediumTest
     public void testGetAccessibilityServiceList() throws Exception {
         // create a list of installed accessibility services the mock service returns
-        List<AccessibilityServiceInfo> expectedServices = new ArrayList<AccessibilityServiceInfo>();
+        List<AccessibilityServiceInfo> expectedServices = new ArrayList<>();
         AccessibilityServiceInfo accessibilityServiceInfo = new AccessibilityServiceInfo();
         accessibilityServiceInfo.packageNames = new String[] { "foo.bar" };
         expectedServices.add(accessibilityServiceInfo);
 
         // configure the mock service behavior
-        IAccessibilityManager mockServiceInterface = mMockServiceInterface;
-        expect(mockServiceInterface.addClient(anyIAccessibilityManagerClient(),
-                UserHandle.USER_OWNER)).andReturn(
-                AccessibilityManager.STATE_FLAG_ACCESSIBILITY_ENABLED);
-        expect(mockServiceInterface.getInstalledAccessibilityServiceList(UserHandle.USER_OWNER))
-                .andReturn(expectedServices);
-        replay(mockServiceInterface);
+        when(mMockService.getInstalledAccessibilityServiceList(anyInt()))
+                .thenReturn(expectedServices);
 
         // invoke the method under test
-        AccessibilityManager manager = new AccessibilityManager(mContext, mockServiceInterface,
-                UserHandle.USER_OWNER);
+        AccessibilityManager manager = createManager(true);
         List<AccessibilityServiceInfo> receivedServices =
-            manager.getInstalledAccessibilityServiceList();
+                manager.getInstalledAccessibilityServiceList();
 
+        verify(mMockService).getInstalledAccessibilityServiceList(UserHandle.USER_CURRENT);
         // check expected result (list equals() compares it contents as well)
-        assertEquals("All expected services must be returned", receivedServices, expectedServices);
-
-        // verify the mock service was properly called
-        verify(mockServiceInterface);
+        assertEquals("All expected services must be returned", expectedServices, receivedServices);
     }
 
     @MediumTest
     public void testInterrupt() throws Exception {
-        // configure the mock service behavior
-        IAccessibilityManager mockServiceInterface = mMockServiceInterface;
-        expect(mockServiceInterface.addClient(anyIAccessibilityManagerClient(),
-                UserHandle.USER_OWNER)).andReturn(
-                        AccessibilityManager.STATE_FLAG_ACCESSIBILITY_ENABLED);
-        mockServiceInterface.interrupt(UserHandle.USER_OWNER);
-        replay(mockServiceInterface);
-
-        // invoke the method under test
-        AccessibilityManager manager = new AccessibilityManager(mContext, mockServiceInterface,
-                UserHandle.USER_OWNER);
+        AccessibilityManager manager = createManager(true);
         manager.interrupt();
 
-        // verify the mock service was properly called
-        verify(mockServiceInterface);
+        verify(mMockService).interrupt(UserHandle.USER_CURRENT);
     }
 
     @LargeTest
     public void testIsEnabled() throws Exception {
-        // configure the mock service behavior
-        IAccessibilityManager mockServiceInterface = mMockServiceInterface;
-        expect(mockServiceInterface.addClient(anyIAccessibilityManagerClient(),
-                UserHandle.USER_OWNER)).andReturn(
-                        AccessibilityManager.STATE_FLAG_ACCESSIBILITY_ENABLED);
-        replay(mockServiceInterface);
-
         // invoke the method under test
-        AccessibilityManager manager = new AccessibilityManager(mContext, mockServiceInterface,
-                UserHandle.USER_OWNER);
+        AccessibilityManager manager = createManager(true);
         boolean isEnabledServiceEnabled = manager.isEnabled();
 
         // check expected result
@@ -138,63 +125,32 @@
         // check expected result
         assertFalse("Must be disabled since the mock service is disabled",
                 isEnabledServcieDisabled);
-
-        // verify the mock service was properly called
-        verify(mockServiceInterface);
     }
 
     @MediumTest
     public void testSendAccessibilityEvent_AccessibilityEnabled() throws Exception {
-        // create an event to be dispatched
         AccessibilityEvent sentEvent = AccessibilityEvent.obtain();
 
-        // configure the mock service behavior
-        IAccessibilityManager mockServiceInterface = mMockServiceInterface;
-        expect(mockServiceInterface.addClient(anyIAccessibilityManagerClient(),
-                UserHandle.USER_OWNER)).andReturn(
-                        AccessibilityManager.STATE_FLAG_ACCESSIBILITY_ENABLED);
-        expect(mockServiceInterface.sendAccessibilityEvent(eqAccessibilityEvent(sentEvent),
-                UserHandle.USER_OWNER)).andReturn(true);
-        expect(mockServiceInterface.sendAccessibilityEvent(eqAccessibilityEvent(sentEvent),
-                UserHandle.USER_OWNER)).andReturn(false);
-        replay(mockServiceInterface);
+        when(mMockService.sendAccessibilityEvent(eq(sentEvent), anyInt()))
+                .thenReturn(true  /* should recycle event object */)
+                .thenReturn(false /* should not recycle event object */);
 
-        // invoke the method under test (manager and service in different processes)
-        AccessibilityManager manager = new AccessibilityManager(mContext, mockServiceInterface,
-                UserHandle.USER_OWNER);
+        AccessibilityManager manager = createManager(true);
         manager.sendAccessibilityEvent(sentEvent);
 
-        // check expected result
-        AccessibilityEvent nextEventDifferentProcesses = AccessibilityEvent.obtain();
-        assertSame("The manager and the service are in different processes, so the event must be " +
-                "recycled", sentEvent, nextEventDifferentProcesses);
+        assertSame("The event should be recycled.", sentEvent, AccessibilityEvent.obtain());
 
-        // invoke the method under test (manager and service in the same process)
         manager.sendAccessibilityEvent(sentEvent);
 
-        // check expected result
-        AccessibilityEvent nextEventSameProcess = AccessibilityEvent.obtain();
-        assertNotSame("The manager and the service are in the same process, so the event must not" +
-                "be recycled", sentEvent, nextEventSameProcess);
-
-        // verify the mock service was properly called
-        verify(mockServiceInterface);
+        assertNotSame("The event should not be recycled.", sentEvent, AccessibilityEvent.obtain());
     }
 
     @MediumTest
     public void testSendAccessibilityEvent_AccessibilityDisabled() throws Exception {
-        // create an event to be dispatched
         AccessibilityEvent sentEvent = AccessibilityEvent.obtain();
 
-        // configure the mock service behavior
-        IAccessibilityManager mockServiceInterface = mMockServiceInterface;
-        expect(mockServiceInterface.addClient(anyIAccessibilityManagerClient(),
-                UserHandle.USER_OWNER)).andReturn(0);
-        replay(mockServiceInterface);
+        AccessibilityManager manager = createManager(false  /* disabled */);
 
-        // invoke the method under test (accessibility disabled)
-        AccessibilityManager manager = new AccessibilityManager(mContext, mockServiceInterface,
-                UserHandle.USER_OWNER);
         try {
             manager.sendAccessibilityEvent(sentEvent);
             fail("No accessibility events are sent if accessibility is disabled");
@@ -202,73 +158,5 @@
             // check expected result
             assertEquals("Accessibility off. Did you forget to check that?", ise.getMessage());
         }
-
-        // verify the mock service was properly called
-        verify(mockServiceInterface);
-    }
-
-    /**
-     * Determines if an {@link AccessibilityEvent} passed as a method argument
-     * matches expectations.
-     *
-     * @param matched The event to check.
-     * @return True if expectations are matched.
-     */
-    private static AccessibilityEvent eqAccessibilityEvent(AccessibilityEvent matched) {
-        reportMatcher(new AccessibilityEventMather(matched));
-        return null;
-    }
-
-    /**
-     * Determines if an {@link IAccessibilityManagerClient} passed as a method argument
-     * matches expectations which in this case are that any instance is accepted.
-     *
-     * @return <code>null</code>.
-     */
-    private static IAccessibilityManagerClient anyIAccessibilityManagerClient() {
-        reportMatcher(new AnyIAccessibilityManagerClientMather());
-        return null;
-    }
-
-    /**
-     * Matcher for {@link AccessibilityEvent}s.
-     */
-    private static class AccessibilityEventMather implements IArgumentMatcher {
-        private AccessibilityEvent mExpectedEvent;
-
-        public AccessibilityEventMather(AccessibilityEvent expectedEvent) {
-            mExpectedEvent = expectedEvent;
-        }
-
-        public boolean matches(Object matched) {
-            if (!(matched instanceof AccessibilityEvent)) {
-                return false;
-            }
-            AccessibilityEvent receivedEvent = (AccessibilityEvent) matched;
-            return mExpectedEvent.getEventType() == receivedEvent.getEventType();
-        }
-
-        public void appendTo(StringBuffer buffer) {
-            buffer.append("sendAccessibilityEvent()");
-            buffer.append(" with event type \"");
-            buffer.append(mExpectedEvent.getEventType());
-            buffer.append("\"");
-        }
-    }
-
-    /**
-     * Matcher for {@link IAccessibilityManagerClient}s.
-     */
-    private static class AnyIAccessibilityManagerClientMather implements IArgumentMatcher {
-        public boolean matches(Object matched) {
-            if (!(matched instanceof IAccessibilityManagerClient)) {
-                return false;
-            }
-            return true;
-        }
-
-        public void appendTo(StringBuffer buffer) {
-            buffer.append("addClient() with any IAccessibilityManagerClient");
-        }
     }
 }
diff --git a/services/tests/servicestests/src/com/android/server/MockAccessibilityService.java b/services/tests/servicestests/src/com/android/server/MockAccessibilityService.java
index 1bc9b86..e1c5cee 100644
--- a/services/tests/servicestests/src/com/android/server/MockAccessibilityService.java
+++ b/services/tests/servicestests/src/com/android/server/MockAccessibilityService.java
@@ -62,7 +62,7 @@
      */
     public static AccessibilityServiceInfo createDefaultInfo() {
         AccessibilityServiceInfo defaultInfo = new AccessibilityServiceInfo();
-        defaultInfo.eventTypes = AccessibilityEvent.TYPE_VIEW_CLICKED;
+        defaultInfo.eventTypes = AccessibilityEvent.TYPE_ANNOUNCEMENT;
         defaultInfo.feedbackType = AccessibilityServiceInfo.FEEDBACK_AUDIBLE;
         defaultInfo.flags = 0;
         defaultInfo.notificationTimeout = 0;
diff --git a/services/tests/servicestests/src/com/android/server/devicepolicy/DeviceOwnerTest.java b/services/tests/servicestests/src/com/android/server/devicepolicy/DeviceOwnerTest.java
deleted file mode 100644
index 8ad7eea..0000000
--- a/services/tests/servicestests/src/com/android/server/devicepolicy/DeviceOwnerTest.java
+++ /dev/null
@@ -1,67 +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.server.devicepolicy;
-
-import android.content.ComponentName;
-import android.content.Context;
-import android.test.AndroidTestCase;
-import android.test.suitebuilder.annotation.SmallTest;
-
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.File;
-
-/**
- * Tests for the DeviceOwner object that saves & loads device and policy owner information.
- * run this test with:
- *   make -j FrameworksServicesTests
- *   runtest --path frameworks/base/services/tests/servicestests/ \
- *       src/com/android/server/devicepolicy/DeviceOwnerTest.java
- */
-public class DeviceOwnerTest extends AndroidTestCase {
-
-    private static class DeviceOwnerSub extends DeviceOwner{
-        private final File mLegacyFile;
-        private final File mDeviceOwnerFile;
-        private final File mProfileOwnerBase;
-
-        public DeviceOwnerSub(Context context, File legacyFile, File deviceOwnerFile,
-                File profileOwnerBase) {
-            super(context);
-            mLegacyFile = legacyFile;
-            mDeviceOwnerFile = deviceOwnerFile;
-            mProfileOwnerBase = profileOwnerBase;
-        }
-
-        @Override
-        File getLegacyConfigFileWithTestOverride() {
-            return mLegacyFile;
-        }
-
-        @Override
-        File getDeviceOwnerFileWithTestOverride() {
-            return mDeviceOwnerFile;
-        }
-
-        @Override
-        File getProfileOwnerFileWithTestOverride(int userId) {
-            return new File(mDeviceOwnerFile.getAbsoluteFile() + "-" + userId);
-        }
-    }
-
-    // TODO Write tests
-}
diff --git a/services/tests/servicestests/src/com/android/server/devicepolicy/DpmMockContext.java b/services/tests/servicestests/src/com/android/server/devicepolicy/DpmMockContext.java
new file mode 100644
index 0000000..c2b8981
--- /dev/null
+++ b/services/tests/servicestests/src/com/android/server/devicepolicy/DpmMockContext.java
@@ -0,0 +1,46 @@
+/*
+ * 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.server.devicepolicy;
+
+import android.content.Context;
+import android.content.ContextWrapper;
+import android.os.UserManager;
+
+import static org.mockito.Mockito.mock;
+
+public class DpmMockContext extends ContextWrapper {
+    private final UserManager mMockUserManager;
+
+
+    public DpmMockContext(Context context) {
+        super(context);
+        mMockUserManager = mock(UserManager.class);
+    }
+
+    public UserManager getMockUserManager() {
+        return mMockUserManager;
+    }
+
+    @Override
+    public Object getSystemService(String name) {
+        switch (name) {
+            case Context.USER_SERVICE:
+                return mMockUserManager;
+        }
+        return super.getSystemService(name);
+    }
+}
diff --git a/services/tests/servicestests/src/com/android/server/devicepolicy/DpmTestBase.java b/services/tests/servicestests/src/com/android/server/devicepolicy/DpmTestBase.java
new file mode 100644
index 0000000..445260b
--- /dev/null
+++ b/services/tests/servicestests/src/com/android/server/devicepolicy/DpmTestBase.java
@@ -0,0 +1,36 @@
+/*
+ * 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.server.devicepolicy;
+
+import android.content.Context;
+import android.test.AndroidTestCase;
+
+public class DpmTestBase extends AndroidTestCase {
+    private DpmMockContext mMockContext;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        mMockContext = new DpmMockContext(super.getContext());
+    }
+
+    @Override
+    public DpmMockContext getContext() {
+        return mMockContext;
+    }
+}
diff --git a/services/tests/servicestests/src/com/android/server/devicepolicy/OwnersTest.java b/services/tests/servicestests/src/com/android/server/devicepolicy/OwnersTest.java
new file mode 100644
index 0000000..a284ca0
--- /dev/null
+++ b/services/tests/servicestests/src/com/android/server/devicepolicy/OwnersTest.java
@@ -0,0 +1,401 @@
+/*
+ * 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.server.devicepolicy;
+
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.pm.UserInfo;
+import android.os.FileUtils;
+import android.test.AndroidTestCase;
+import android.util.Log;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+
+import junit.framework.Assert;
+
+import static org.mockito.Mockito.when;
+
+/**
+ * Tests for the DeviceOwner object that saves & loads device and policy owner information.
+ * run this test with:
+ mmma frameworks/base/services/tests/servicestests/ &&
+ adb install \
+   -r out/target/product/hammerhead/data/app/FrameworksServicesTests/FrameworksServicesTests.apk &&
+ adb shell am instrument -e class com.android.server.devicepolicy.OwnersTest \
+   -w com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner
+ */
+public class OwnersTest extends DpmTestBase {
+    private static final String TAG = "DeviceOwnerTest";
+
+    private static final String LEGACY_FILE = "legacy.xml";
+    private static final String DEVICE_OWNER_FILE = "device_owner2.xml";
+    private static final String PROFILE_OWNER_FILE_BASE = "profile_owner.xml";
+
+    private File mDataDir;
+
+    private class OwnersSub extends Owners {
+        final File mLegacyFile;
+        final File mDeviceOwnerFile;
+        final File mProfileOwnerBase;
+
+        public OwnersSub() {
+            super(getContext());
+            mLegacyFile = new File(mDataDir, LEGACY_FILE);
+            mDeviceOwnerFile = new File(mDataDir, DEVICE_OWNER_FILE);
+            mProfileOwnerBase = new File(mDataDir, PROFILE_OWNER_FILE_BASE);
+        }
+
+        @Override
+        File getLegacyConfigFileWithTestOverride() {
+            return mLegacyFile;
+        }
+
+        @Override
+        File getDeviceOwnerFileWithTestOverride() {
+            return mDeviceOwnerFile;
+        }
+
+        @Override
+        File getProfileOwnerFileWithTestOverride(int userId) {
+            return new File(mDeviceOwnerFile.getAbsoluteFile() + "-" + userId);
+        }
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        mDataDir = new File(getContext().getCacheDir(), "OwnersTest");
+        if (mDataDir.exists()) {
+            assertTrue("failed to delete dir", FileUtils.deleteContents(mDataDir));
+        }
+        mDataDir.mkdirs();
+        Log.i(TAG, "Created " + mDataDir);
+    }
+
+    private String readAsset(String assetPath) throws IOException {
+        final StringBuilder sb = new StringBuilder();
+        try (BufferedReader br = new BufferedReader(
+                new InputStreamReader((getContext().getResources().getAssets().open(assetPath))))) {
+            String line;
+            while ((line = br.readLine()) != null) {
+                sb.append(line);
+                sb.append(System.lineSeparator());
+            }
+        }
+        return sb.toString();
+    }
+
+    private void createLegacyFile(File path, String content)
+            throws IOException {
+        path.getParentFile().mkdirs();
+
+        try (FileWriter writer = new FileWriter(path)) {
+            Log.i(TAG, "Writing to " + path);
+            Log.i(TAG, content);
+            writer.write(content);
+        }
+    }
+
+    private void addUsersToUserManager(int... userIds) {
+        final ArrayList<UserInfo> userInfos = new ArrayList<>();
+        for (int userId : userIds) {
+            final UserInfo ui = new UserInfo();
+            ui.id = userId;
+            userInfos.add(ui);
+        }
+        when(getContext().getMockUserManager().getUsers()).thenReturn(userInfos);
+    }
+
+    public void testUpgrade01() throws Exception {
+        addUsersToUserManager(10, 11, 20, 21);
+
+        // First, migrate.
+        {
+            final OwnersSub owners = new OwnersSub();
+
+            createLegacyFile(owners.mLegacyFile, readAsset("OwnersTest/test01/input.xml"));
+
+            owners.load();
+
+            // The legacy file should be removed.
+            assertFalse(owners.getLegacyConfigFileWithTestOverride().exists());
+
+            // File was empty, so no new files should be created.
+            assertFalse(owners.getDeviceOwnerFileWithTestOverride().exists());
+
+            assertFalse(owners.getProfileOwnerFileWithTestOverride(10).exists());
+            assertFalse(owners.getProfileOwnerFileWithTestOverride(11).exists());
+            assertFalse(owners.getProfileOwnerFileWithTestOverride(20).exists());
+            assertFalse(owners.getProfileOwnerFileWithTestOverride(21).exists());
+        }
+
+        // Then re-read and check.
+        {
+            final OwnersSub owners = new OwnersSub();
+            owners.load();
+
+            assertFalse(owners.hasDeviceOwner());
+            assertFalse(owners.hasDeviceInitializer());
+            assertNull(owners.getSystemUpdatePolicy());
+            assertEquals(0, owners.getProfileOwnerKeys().size());
+        }
+    }
+
+    public void testUpgrade02() throws Exception {
+        addUsersToUserManager(10, 11, 20, 21);
+
+        // First, migrate.
+        {
+            final OwnersSub owners = new OwnersSub();
+
+            createLegacyFile(owners.mLegacyFile, readAsset("OwnersTest/test02/input.xml"));
+
+            owners.load();
+
+            // The legacy file should be removed.
+            assertFalse(owners.getLegacyConfigFileWithTestOverride().exists());
+
+            assertTrue(owners.getDeviceOwnerFileWithTestOverride().exists()); // TODO Check content
+
+            assertFalse(owners.getProfileOwnerFileWithTestOverride(10).exists());
+            assertFalse(owners.getProfileOwnerFileWithTestOverride(11).exists());
+            assertFalse(owners.getProfileOwnerFileWithTestOverride(20).exists());
+            assertFalse(owners.getProfileOwnerFileWithTestOverride(21).exists());
+        }
+
+        // Then re-read and check.
+        {
+            final OwnersSub owners = new OwnersSub();
+            owners.load();
+
+            assertTrue(owners.hasDeviceOwner());
+            assertEquals(null, owners.getDeviceOwnerName());
+            assertEquals("com.google.android.testdpc", owners.getDeviceOwnerPackageName());
+
+            assertFalse(owners.hasDeviceInitializer());
+            assertNull(owners.getSystemUpdatePolicy());
+            assertEquals(0, owners.getProfileOwnerKeys().size());
+        }
+    }
+
+    public void testUpgrade03() throws Exception {
+        addUsersToUserManager(10, 11, 20, 21);
+
+        // First, migrate.
+        {
+            final OwnersSub owners = new OwnersSub();
+
+            createLegacyFile(owners.mLegacyFile, readAsset("OwnersTest/test03/input.xml"));
+
+            owners.load();
+
+            // The legacy file should be removed.
+            assertFalse(owners.getLegacyConfigFileWithTestOverride().exists());
+
+            assertFalse(owners.getDeviceOwnerFileWithTestOverride().exists());
+
+            assertTrue(owners.getProfileOwnerFileWithTestOverride(10).exists());
+            assertTrue(owners.getProfileOwnerFileWithTestOverride(11).exists());
+            assertFalse(owners.getProfileOwnerFileWithTestOverride(20).exists());
+            assertFalse(owners.getProfileOwnerFileWithTestOverride(21).exists());
+        }
+
+        // Then re-read and check.
+        {
+            final OwnersSub owners = new OwnersSub();
+            owners.load();
+
+            assertFalse(owners.hasDeviceOwner());
+            assertFalse(owners.hasDeviceInitializer());
+            assertNull(owners.getSystemUpdatePolicy());
+
+            assertEquals(2, owners.getProfileOwnerKeys().size());
+            assertEquals(new ComponentName("com.google.android.testdpc",
+                            "com.google.android.testdpc.DeviceAdminReceiver0"),
+                    owners.getProfileOwnerComponent(10));
+            assertEquals("0", owners.getProfileOwnerName(10));
+            assertEquals("com.google.android.testdpc", owners.getProfileOwnerPackage(10));
+
+            assertEquals(new ComponentName("com.google.android.testdpc1", ""),
+                    owners.getProfileOwnerComponent(11));
+            assertEquals("1", owners.getProfileOwnerName(11));
+            assertEquals("com.google.android.testdpc1", owners.getProfileOwnerPackage(11));
+        }
+    }
+
+    public void testUpgrade04() throws Exception {
+        addUsersToUserManager(10, 11, 20, 21);
+
+        // First, migrate.
+        {
+            final OwnersSub owners = new OwnersSub();
+
+            createLegacyFile(owners.mLegacyFile, readAsset("OwnersTest/test04/input.xml"));
+
+            owners.load();
+
+            // The legacy file should be removed.
+            assertFalse(owners.getLegacyConfigFileWithTestOverride().exists());
+
+            assertTrue(owners.getDeviceOwnerFileWithTestOverride().exists());
+
+            assertTrue(owners.getProfileOwnerFileWithTestOverride(10).exists());
+            assertTrue(owners.getProfileOwnerFileWithTestOverride(11).exists());
+            assertFalse(owners.getProfileOwnerFileWithTestOverride(20).exists());
+            assertFalse(owners.getProfileOwnerFileWithTestOverride(21).exists());
+        }
+
+        // Then re-read and check.
+        {
+            final OwnersSub owners = new OwnersSub();
+            owners.load();
+
+            assertTrue(owners.hasDeviceOwner());
+            assertEquals(null, owners.getDeviceOwnerName());
+            assertEquals("com.google.android.testdpc", owners.getDeviceOwnerPackageName());
+
+            assertTrue(owners.hasDeviceInitializer());
+            assertEquals("com.google.android.testdpcx", owners.getDeviceInitializerPackageName());
+            assertNotNull(owners.getSystemUpdatePolicy());
+            assertEquals(5, owners.getSystemUpdatePolicy().getPolicyType());
+
+            assertEquals(2, owners.getProfileOwnerKeys().size());
+            assertEquals(new ComponentName("com.google.android.testdpc",
+                            "com.google.android.testdpc.DeviceAdminReceiver0"),
+                    owners.getProfileOwnerComponent(10));
+            assertEquals("0", owners.getProfileOwnerName(10));
+            assertEquals("com.google.android.testdpc", owners.getProfileOwnerPackage(10));
+
+            assertEquals(new ComponentName("com.google.android.testdpc1", ""),
+                    owners.getProfileOwnerComponent(11));
+            assertEquals("1", owners.getProfileOwnerName(11));
+            assertEquals("com.google.android.testdpc1", owners.getProfileOwnerPackage(11));
+        }
+    }
+
+    public void testUpgrade05() throws Exception {
+        addUsersToUserManager(10, 11, 20, 21);
+
+        // First, migrate.
+        {
+            final OwnersSub owners = new OwnersSub();
+
+            createLegacyFile(owners.mLegacyFile, readAsset("OwnersTest/test05/input.xml"));
+
+            owners.load();
+
+            // The legacy file should be removed.
+            assertFalse(owners.getLegacyConfigFileWithTestOverride().exists());
+
+            assertTrue(owners.getDeviceOwnerFileWithTestOverride().exists());
+
+            assertFalse(owners.getProfileOwnerFileWithTestOverride(10).exists());
+            assertFalse(owners.getProfileOwnerFileWithTestOverride(11).exists());
+            assertFalse(owners.getProfileOwnerFileWithTestOverride(20).exists());
+        }
+
+        // Then re-read and check.
+        {
+            final OwnersSub owners = new OwnersSub();
+            owners.load();
+
+            assertFalse(owners.hasDeviceOwner());
+
+            assertTrue(owners.hasDeviceInitializer());
+            assertEquals("com.google.android.testdpcx", owners.getDeviceInitializerPackageName());
+
+            assertNull(owners.getSystemUpdatePolicy());
+            assertEquals(0, owners.getProfileOwnerKeys().size());
+        }
+    }
+
+    public void testUpgrade06() throws Exception {
+        addUsersToUserManager(10, 11, 20, 21);
+
+        // First, migrate.
+        {
+            final OwnersSub owners = new OwnersSub();
+
+            createLegacyFile(owners.mLegacyFile, readAsset("OwnersTest/test06/input.xml"));
+
+            owners.load();
+
+            // The legacy file should be removed.
+            assertFalse(owners.getLegacyConfigFileWithTestOverride().exists());
+
+            assertTrue(owners.getDeviceOwnerFileWithTestOverride().exists());
+
+            assertFalse(owners.getProfileOwnerFileWithTestOverride(10).exists());
+            assertFalse(owners.getProfileOwnerFileWithTestOverride(11).exists());
+            assertFalse(owners.getProfileOwnerFileWithTestOverride(20).exists());
+        }
+
+        // Then re-read and check.
+        {
+            final OwnersSub owners = new OwnersSub();
+            owners.load();
+
+            assertFalse(owners.hasDeviceOwner());
+            assertFalse(owners.hasDeviceInitializer());
+            assertEquals(0, owners.getProfileOwnerKeys().size());
+
+            assertNotNull(owners.getSystemUpdatePolicy());
+            assertEquals(5, owners.getSystemUpdatePolicy().getPolicyType());
+        }
+    }
+
+    public void testRemoveExistingFiles() throws Exception {
+        addUsersToUserManager(10, 11, 20, 21);
+
+        final OwnersSub owners = new OwnersSub();
+
+        // First, migrate to create new-style config files.
+        createLegacyFile(owners.mLegacyFile, readAsset("OwnersTest/test04/input.xml"));
+
+        owners.load();
+
+        assertFalse(owners.getLegacyConfigFileWithTestOverride().exists());
+
+        assertTrue(owners.getDeviceOwnerFileWithTestOverride().exists());
+        assertTrue(owners.getProfileOwnerFileWithTestOverride(10).exists());
+        assertTrue(owners.getProfileOwnerFileWithTestOverride(11).exists());
+
+        // Then clear all information and save.
+        owners.clearDeviceInitializer();
+        owners.clearDeviceOwner();
+        owners.clearSystemUpdatePolicy();
+        owners.removeProfileOwner(10);
+        owners.removeProfileOwner(11);
+
+        owners.writeDeviceOwner();
+        owners.writeProfileOwner(10);
+        owners.writeProfileOwner(11);
+        owners.writeProfileOwner(20);
+        owners.writeProfileOwner(21);
+
+        // Now all files should be removed.
+        assertFalse(owners.getDeviceOwnerFileWithTestOverride().exists());
+        assertFalse(owners.getProfileOwnerFileWithTestOverride(10).exists());
+        assertFalse(owners.getProfileOwnerFileWithTestOverride(11).exists());
+    }
+}
diff --git a/telecomm/java/android/telecom/Call.java b/telecomm/java/android/telecom/Call.java
index 2d31a78..8a1a553 100644
--- a/telecomm/java/android/telecom/Call.java
+++ b/telecomm/java/android/telecom/Call.java
@@ -206,8 +206,14 @@
          */
         public static final int CAPABILITY_CAN_PAUSE_VIDEO = 0x00100000;
 
+        /**
+         * Call sends responses through connection.
+         * @hide
+         */
+        public static final int CAPABILITY_CAN_SEND_RESPONSE_VIA_CONNECTION = 0x00400000;
+
         //******************************************************************************************
-        // Next CAPABILITY value: 0x00004000
+        // Next CAPABILITY value: 0x00800000
         //******************************************************************************************
 
         /**
diff --git a/telecomm/java/android/telecom/Connection.java b/telecomm/java/android/telecom/Connection.java
index 7b277c5..430760a 100644
--- a/telecomm/java/android/telecom/Connection.java
+++ b/telecomm/java/android/telecom/Connection.java
@@ -248,8 +248,15 @@
      */
     public static final int CAPABILITY_CONFERENCE_HAS_NO_CHILDREN = 0x00200000;
 
+    /**
+     * Indicates that the connection itself wants to handle any sort of reply response, rather than
+     * relying on SMS.
+     * @hide
+     */
+    public static final int CAPABILITY_CAN_SEND_RESPONSE_VIA_CONNECTION = 0x00400000;
+
     //**********************************************************************************************
-    // Next CAPABILITY value: 0x00400000
+    // Next CAPABILITY value: 0x00800000
     //**********************************************************************************************
 
     /**
@@ -388,6 +395,10 @@
         if (can(capabilities, CAPABILITY_CONFERENCE_HAS_NO_CHILDREN)) {
             builder.append(" CAPABILITY_SINGLE_PARTY_CONFERENCE");
         }
+        if (can(capabilities, CAPABILITY_CAN_SEND_RESPONSE_VIA_CONNECTION)) {
+            builder.append(" CAPABILITY_CAN_SEND_RESPONSE_VIA_CONNECTION");
+        }
+
         builder.append("]");
         return builder.toString();
     }
@@ -1763,6 +1774,13 @@
     public void onReject() {}
 
     /**
+     * Notifies ths Connection of a request reject with a message.
+     *
+     * @hide
+     */
+    public void onReject(String replyMessage) {}
+
+    /**
      * Notifies this Connection whether the user wishes to proceed with the post-dial DTMF codes.
      */
     public void onPostDialContinue(boolean proceed) {}
diff --git a/telecomm/java/android/telecom/ConnectionService.java b/telecomm/java/android/telecom/ConnectionService.java
index 383e45b..4e330bdb 100644
--- a/telecomm/java/android/telecom/ConnectionService.java
+++ b/telecomm/java/android/telecom/ConnectionService.java
@@ -101,6 +101,7 @@
     private static final int MSG_ANSWER_VIDEO = 17;
     private static final int MSG_MERGE_CONFERENCE = 18;
     private static final int MSG_SWAP_CONFERENCE = 19;
+    private static final int MSG_REJECT_WITH_MESSAGE = 20;
 
     private static Connection sNullConnection;
 
@@ -166,6 +167,14 @@
         }
 
         @Override
+        public void rejectWithMessage(String callId, String message) {
+            SomeArgs args = SomeArgs.obtain();
+            args.arg1 = callId;
+            args.arg2 = message;
+            mHandler.obtainMessage(MSG_REJECT_WITH_MESSAGE, args).sendToTarget();
+        }
+
+        @Override
         public void disconnect(String callId) {
             mHandler.obtainMessage(MSG_DISCONNECT, callId).sendToTarget();
         }
@@ -296,6 +305,15 @@
                 case MSG_REJECT:
                     reject((String) msg.obj);
                     break;
+                case MSG_REJECT_WITH_MESSAGE: {
+                    SomeArgs args = (SomeArgs) msg.obj;
+                    try {
+                        reject((String) args.arg1, (String) args.arg2);
+                    } finally {
+                        args.recycle();
+                    }
+                    break;
+                }
                 case MSG_DISCONNECT:
                     disconnect((String) msg.obj);
                     break;
@@ -681,6 +699,11 @@
         findConnectionForAction(callId, "reject").onReject();
     }
 
+    private void reject(String callId, String rejectWithMessage) {
+        Log.d(this, "reject %s with message", callId);
+        findConnectionForAction(callId, "reject").onReject(rejectWithMessage);
+    }
+
     private void disconnect(String callId) {
         Log.d(this, "disconnect %s", callId);
         if (mConnectionById.containsKey(callId)) {
diff --git a/telecomm/java/com/android/internal/telecom/IConnectionService.aidl b/telecomm/java/com/android/internal/telecom/IConnectionService.aidl
index c2e8530..dd253cf 100644
--- a/telecomm/java/com/android/internal/telecom/IConnectionService.aidl
+++ b/telecomm/java/com/android/internal/telecom/IConnectionService.aidl
@@ -50,6 +50,8 @@
 
     void reject(String callId);
 
+    void rejectWithMessage(String callId, String message);
+
     void disconnect(String callId);
 
     void hold(String callId);
diff --git a/telephony/java/android/telephony/CarrierConfigManager.java b/telephony/java/android/telephony/CarrierConfigManager.java
index bfb7a50..aa750ef 100644
--- a/telephony/java/android/telephony/CarrierConfigManager.java
+++ b/telephony/java/android/telephony/CarrierConfigManager.java
@@ -419,6 +419,15 @@
     public static final String KEY_MMS_UA_PROF_URL_STRING = "uaProfUrl";
     public static final String KEY_MMS_USER_AGENT_STRING = "userAgent";
 
+    /**
+     * Determines whether the carrier supports making non-emergency phone calls while the phone is
+     * in emergency callback mode.  Default value is {@code true}, meaning that non-emergency calls
+     * are allowed in emergency callback mode.
+     * @hide
+     */
+    public static final String KEY_ALLOW_NON_EMERGENCY_CALLS_IN_ECM_BOOL =
+            "allow_non_emergency_calls_in_ecm_bool";
+
     /** The default value for every variable. */
     private final static PersistableBundle sDefaults;
 
@@ -513,6 +522,7 @@
         sDefaults.putString(KEY_MMS_UA_PROF_TAG_NAME_STRING, "x-wap-profile");
         sDefaults.putString(KEY_MMS_UA_PROF_URL_STRING, "");
         sDefaults.putString(KEY_MMS_USER_AGENT_STRING, "");
+        sDefaults.putBoolean(KEY_ALLOW_NON_EMERGENCY_CALLS_IN_ECM_BOOL, true);
     }
 
     /**
diff --git a/telephony/java/android/telephony/PhoneNumberUtils.java b/telephony/java/android/telephony/PhoneNumberUtils.java
index 273cc93..b430340 100644
--- a/telephony/java/android/telephony/PhoneNumberUtils.java
+++ b/telephony/java/android/telephony/PhoneNumberUtils.java
@@ -1457,10 +1457,15 @@
         String result = null;
         try {
             PhoneNumber pn = util.parseAndKeepRawInput(phoneNumber, defaultCountryIso);
+            /**
+             * Need to reformat any local Korean phone numbers (when the user is in Korea) with
+             * country code to corresponding national format which would replace the leading
+             * +82 with 0.
+             */
             if (KOREA_ISO_COUNTRY_CODE.equals(defaultCountryIso) &&
-                    (pn.getCountryCode() == util.getCountryCodeForRegion(KOREA_ISO_COUNTRY_CODE))) {
-                // Format local Korean phone numbers with country code to corresponding national
-                // format which would replace the leading +82 with 0.
+                    (pn.getCountryCode() == util.getCountryCodeForRegion(KOREA_ISO_COUNTRY_CODE)) &&
+                    (pn.getCountryCodeSource() ==
+                            PhoneNumber.CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN)) {
                 result = util.format(pn, PhoneNumberUtil.PhoneNumberFormat.NATIONAL);
             } else {
                 result = util.formatInOriginalFormat(pn, defaultCountryIso);
diff --git a/telephony/java/android/telephony/RadioAccessFamily.java b/telephony/java/android/telephony/RadioAccessFamily.java
index 0c5c557..2bfaf1b 100644
--- a/telephony/java/android/telephony/RadioAccessFamily.java
+++ b/telephony/java/android/telephony/RadioAccessFamily.java
@@ -185,6 +185,36 @@
             case RILConstants.NETWORK_MODE_GLOBAL:
                 raf = GSM | WCDMA | CDMA | EVDO;
                 break;
+            case RILConstants.NETWORK_MODE_TDSCDMA_ONLY:
+                raf = RAF_TD_SCDMA;
+                break;
+            case RILConstants.NETWORK_MODE_TDSCDMA_WCDMA:
+                raf = RAF_TD_SCDMA | WCDMA;
+                break;
+            case RILConstants.NETWORK_MODE_LTE_TDSCDMA:
+                raf = 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;
+                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;
+                break;
+            case RILConstants.NETWORK_MODE_LTE_TDSCDMA_GSM_WCDMA:
+                raf = 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;
+                break;
             default:
                 raf = RAF_UNKNOWN;
                 break;
@@ -248,6 +278,36 @@
             case (GSM | WCDMA | CDMA | EVDO):
                 type = RILConstants.NETWORK_MODE_GLOBAL;
                 break;
+            case RAF_TD_SCDMA:
+                type = RILConstants.NETWORK_MODE_TDSCDMA_ONLY;
+                break;
+            case (RAF_TD_SCDMA | WCDMA):
+                type = RILConstants.NETWORK_MODE_TDSCDMA_WCDMA;
+                break;
+            case (RAF_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):
+                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):
+                type = RILConstants.NETWORK_MODE_LTE_TDSCDMA_WCDMA;
+                break;
+            case (RAF_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):
+                type = RILConstants.NETWORK_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA;
+                break;
             default:
                 type = RILConstants.PREFERRED_NETWORK_MODE ;
                 break;
diff --git a/telephony/java/android/telephony/ServiceState.java b/telephony/java/android/telephony/ServiceState.java
index 80515cf..1337487 100644
--- a/telephony/java/android/telephony/ServiceState.java
+++ b/telephony/java/android/telephony/ServiceState.java
@@ -731,6 +731,9 @@
             case RIL_RADIO_TECHNOLOGY_IWLAN:
                 rtString = "IWLAN";
                 break;
+            case RIL_RADIO_TECHNOLOGY_TD_SCDMA:
+                rtString = "TD-SCDMA";
+                break;
             default:
                 rtString = "Unexpected";
                 Rlog.w(LOG_TAG, "Unexpected radioTechnology=" + rt);
@@ -1068,6 +1071,8 @@
             return TelephonyManager.NETWORK_TYPE_HSPAP;
         case ServiceState.RIL_RADIO_TECHNOLOGY_GSM:
             return TelephonyManager.NETWORK_TYPE_GSM;
+        case ServiceState.RIL_RADIO_TECHNOLOGY_TD_SCDMA:
+            return TelephonyManager.NETWORK_TYPE_TD_SCDMA;
         case ServiceState.RIL_RADIO_TECHNOLOGY_IWLAN:
             return TelephonyManager.NETWORK_TYPE_IWLAN;
         default:
diff --git a/telephony/java/android/telephony/SignalStrength.java b/telephony/java/android/telephony/SignalStrength.java
index f02d109..f535e5d 100644
--- a/telephony/java/android/telephony/SignalStrength.java
+++ b/telephony/java/android/telephony/SignalStrength.java
@@ -68,6 +68,7 @@
     private int mLteRsrq;
     private int mLteRssnr;
     private int mLteCqi;
+    private int mTdScdmaRscp;
 
     private boolean isGsm; // This value is set by the ServiceStateTracker onSignalStrengthResult
 
@@ -107,6 +108,7 @@
         mLteRsrq = INVALID;
         mLteRssnr = INVALID;
         mLteCqi = INVALID;
+        mTdScdmaRscp = INVALID;
         isGsm = true;
     }
 
@@ -131,6 +133,7 @@
         mLteRsrq = INVALID;
         mLteRssnr = INVALID;
         mLteCqi = INVALID;
+        mTdScdmaRscp = INVALID;
         isGsm = gsmFlag;
     }
 
@@ -143,6 +146,22 @@
             int cdmaDbm, int cdmaEcio,
             int evdoDbm, int evdoEcio, int evdoSnr,
             int lteSignalStrength, int lteRsrp, int lteRsrq, int lteRssnr, int lteCqi,
+            int tdScdmaRscp, boolean gsmFlag) {
+        initialize(gsmSignalStrength, gsmBitErrorRate, cdmaDbm, cdmaEcio,
+                evdoDbm, evdoEcio, evdoSnr, lteSignalStrength, lteRsrp,
+                lteRsrq, lteRssnr, lteCqi, gsmFlag);
+        mTdScdmaRscp = tdScdmaRscp;
+    }
+
+    /**
+     * Constructor
+     *
+     * @hide
+     */
+    public SignalStrength(int gsmSignalStrength, int gsmBitErrorRate,
+            int cdmaDbm, int cdmaEcio,
+            int evdoDbm, int evdoEcio, int evdoSnr,
+            int lteSignalStrength, int lteRsrp, int lteRsrq, int lteRssnr, int lteCqi,
             boolean gsmFlag) {
         initialize(gsmSignalStrength, gsmBitErrorRate, cdmaDbm, cdmaEcio,
                 evdoDbm, evdoEcio, evdoSnr, lteSignalStrength, lteRsrp,
@@ -233,6 +252,7 @@
         mLteRsrq = lteRsrq;
         mLteRssnr = lteRssnr;
         mLteCqi = lteCqi;
+        mTdScdmaRscp = INVALID;
         isGsm = gsm;
         if (DBG) log("initialize: " + toString());
     }
@@ -253,6 +273,7 @@
         mLteRsrq = s.mLteRsrq;
         mLteRssnr = s.mLteRssnr;
         mLteCqi = s.mLteCqi;
+        mTdScdmaRscp = s.mTdScdmaRscp;
         isGsm = s.isGsm;
     }
 
@@ -276,6 +297,7 @@
         mLteRsrq = in.readInt();
         mLteRssnr = in.readInt();
         mLteCqi = in.readInt();
+        mTdScdmaRscp = in.readInt();
         isGsm = (in.readInt() != 0);
     }
 
@@ -302,7 +324,7 @@
         ss.mLteRsrq = in.readInt();
         ss.mLteRssnr = in.readInt();
         ss.mLteCqi = in.readInt();
-
+        ss.mTdScdmaRscp = in.readInt();
         return ss;
     }
 
@@ -322,6 +344,7 @@
         out.writeInt(mLteRsrq);
         out.writeInt(mLteRssnr);
         out.writeInt(mLteCqi);
+        out.writeInt(mTdScdmaRscp);
         out.writeInt(isGsm ? 1 : 0);
     }
 
@@ -377,6 +400,9 @@
         mLteRsrq = ((mLteRsrq >= 3) && (mLteRsrq <= 20)) ? -mLteRsrq : SignalStrength.INVALID;
         mLteRssnr = ((mLteRssnr >= -200) && (mLteRssnr <= 300)) ? mLteRssnr
                 : SignalStrength.INVALID;
+
+        mTdScdmaRscp = ((mTdScdmaRscp >= 25) && (mTdScdmaRscp <= 120))
+                ? -mTdScdmaRscp : SignalStrength.INVALID;
         // Cqi no change
         if (DBG) log("Signal after validate=" + this);
     }
@@ -477,12 +503,15 @@
      *     while 4 represents a very strong signal strength.
      */
     public int getLevel() {
-        int level;
+        int level = 0;
 
         if (isGsm) {
             level = getLteLevel();
             if (level == SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
-                level = getGsmLevel();
+                level = getTdScdmaLevel();
+                if (level == SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
+                    level = getGsmLevel();
+                }
             }
         } else {
             int cdmaLevel = getCdmaLevel();
@@ -508,10 +537,14 @@
      * @hide
      */
     public int getAsuLevel() {
-        int asuLevel;
+        int asuLevel = 0;
         if (isGsm) {
             if (getLteLevel() == SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
-                asuLevel = getGsmAsuLevel();
+                if (getTdScdmaLevel() == SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
+                    asuLevel = getGsmAsuLevel();
+                } else {
+                    asuLevel = getTdScdmaAsuLevel();
+                }
             } else {
                 asuLevel = getLteAsuLevel();
             }
@@ -539,12 +572,16 @@
      * @hide
      */
     public int getDbm() {
-        int dBm;
+        int dBm = INVALID;
 
         if(isGsm()) {
             dBm = getLteDbm();
             if (dBm == INVALID) {
-                dBm = getGsmDbm();
+                if (getTdScdmaLevel() == SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
+                    dBm = getGsmDbm();
+                } else {
+                    dBm = getTdScdmaDbm();
+                }
             }
         } else {
             int cdmaDbm = getCdmaDbm();
@@ -849,6 +886,54 @@
     }
 
     /**
+     * @return get TD_SCDMA dbm
+     *
+     * @hide
+     */
+    public int getTdScdmaDbm() {
+        return this.mTdScdmaRscp;
+    }
+
+    /**
+     * Get TD-SCDMA as level 0..4
+     * Range : 25 to 120
+     * INT_MAX: 0x7FFFFFFF denotes invalid value
+     * Reference: 3GPP TS 25.123, section 9.1.1.1
+     *
+     * @hide
+     */
+    public int getTdScdmaLevel() {
+        final int tdScdmaDbm = getTdScdmaDbm();
+        int level;
+
+        if ((tdScdmaDbm > -25) || (tdScdmaDbm == SignalStrength.INVALID))
+                level = SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
+        else if (tdScdmaDbm >= -49) level = SIGNAL_STRENGTH_GREAT;
+        else if (tdScdmaDbm >= -73) level = SIGNAL_STRENGTH_GOOD;
+        else if (tdScdmaDbm >= -97) level = SIGNAL_STRENGTH_MODERATE;
+        else if (tdScdmaDbm >= -120) level = SIGNAL_STRENGTH_POOR;
+        else level = SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
+
+        if (DBG) log("getTdScdmaLevel = " + level);
+        return level;
+     }
+
+    /**
+     * Get the TD-SCDMA signal level as an asu value.
+     *
+     * @hide
+     */
+    public int getTdScdmaAsuLevel() {
+        final int tdScdmaDbm = getTdScdmaDbm();
+        int tdScdmaAsuLevel;
+
+        if (tdScdmaDbm == INVALID) tdScdmaAsuLevel = 255;
+        else tdScdmaAsuLevel = tdScdmaDbm + 120;
+        if (DBG) log("TD-SCDMA Asu level: " + tdScdmaAsuLevel);
+        return tdScdmaAsuLevel;
+    }
+
+   /**
      * @return hash code
      */
     @Override
@@ -860,7 +945,7 @@
                 + (mEvdoDbm * primeNum) + (mEvdoEcio * primeNum) + (mEvdoSnr * primeNum)
                 + (mLteSignalStrength * primeNum) + (mLteRsrp * primeNum)
                 + (mLteRsrq * primeNum) + (mLteRssnr * primeNum) + (mLteCqi * primeNum)
-                + (isGsm ? 1 : 0));
+                + (mTdScdmaRscp * primeNum) + (isGsm ? 1 : 0));
     }
 
     /**
@@ -892,6 +977,7 @@
                 && mLteRsrq == s.mLteRsrq
                 && mLteRssnr == s.mLteRssnr
                 && mLteCqi == s.mLteCqi
+                && mTdScdmaRscp == s.mTdScdmaRscp
                 && isGsm == s.isGsm);
     }
 
@@ -913,6 +999,7 @@
                 + " " + mLteRsrq
                 + " " + mLteRssnr
                 + " " + mLteCqi
+                + " " + mTdScdmaRscp
                 + " " + (isGsm ? "gsm|lte" : "cdma"));
     }
 
@@ -935,6 +1022,7 @@
         mLteRsrq = m.getInt("LteRsrq");
         mLteRssnr = m.getInt("LteRssnr");
         mLteCqi = m.getInt("LteCqi");
+        mTdScdmaRscp = m.getInt("TdScdma");
         isGsm = m.getBoolean("isGsm");
     }
 
@@ -957,6 +1045,7 @@
         m.putInt("LteRsrq", mLteRsrq);
         m.putInt("LteRssnr", mLteRssnr);
         m.putInt("LteCqi", mLteCqi);
+        m.putInt("TdScdma", mTdScdmaRscp);
         m.putBoolean("isGsm", Boolean.valueOf(isGsm));
     }
 
diff --git a/telephony/java/android/telephony/TelephonyManager.java b/telephony/java/android/telephony/TelephonyManager.java
index e104b38..f6e4bed 100644
--- a/telephony/java/android/telephony/TelephonyManager.java
+++ b/telephony/java/android/telephony/TelephonyManager.java
@@ -1099,11 +1099,21 @@
         case RILConstants.NETWORK_MODE_LTE_GSM_WCDMA:
         case RILConstants.NETWORK_MODE_LTE_WCDMA:
         case RILConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA:
+        case RILConstants.NETWORK_MODE_TDSCDMA_ONLY:
+        case RILConstants.NETWORK_MODE_TDSCDMA_WCDMA:
+        case RILConstants.NETWORK_MODE_LTE_TDSCDMA:
+        case RILConstants.NETWORK_MODE_TDSCDMA_GSM:
+        case RILConstants.NETWORK_MODE_LTE_TDSCDMA_GSM:
+        case RILConstants.NETWORK_MODE_TDSCDMA_GSM_WCDMA:
+        case RILConstants.NETWORK_MODE_LTE_TDSCDMA_WCDMA:
+        case RILConstants.NETWORK_MODE_LTE_TDSCDMA_GSM_WCDMA:
+        case RILConstants.NETWORK_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA:
             return PhoneConstants.PHONE_TYPE_GSM;
 
         // Use CDMA Phone for the global mode including CDMA
         case RILConstants.NETWORK_MODE_GLOBAL:
         case RILConstants.NETWORK_MODE_LTE_CDMA_EVDO:
+        case RILConstants.NETWORK_MODE_TDSCDMA_CDMA_EVDO_GSM_WCDMA:
             return PhoneConstants.PHONE_TYPE_CDMA;
 
         case RILConstants.NETWORK_MODE_LTE_ONLY:
diff --git a/telephony/java/com/android/ims/internal/IImsRegistrationListener.aidl b/telephony/java/com/android/ims/internal/IImsRegistrationListener.aidl
index a6a2658..23a69d1 100644
--- a/telephony/java/com/android/ims/internal/IImsRegistrationListener.aidl
+++ b/telephony/java/com/android/ims/internal/IImsRegistrationListener.aidl
@@ -71,7 +71,7 @@
      * @param disabledFeatures features disabled as defined in com.android.ims.ImsConfig#FeatureConstants.
      */
     void registrationFeatureCapabilityChanged(int serviceClass,
-            out int[] enabledFeatures, out int[] disabledFeatures);
+            in int[] enabledFeatures, in int[] disabledFeatures);
 
     /**
      * Updates the application with the waiting voice message count.
diff --git a/telephony/java/com/android/internal/telephony/RILConstants.java b/telephony/java/com/android/internal/telephony/RILConstants.java
index 8d48c86..7088be8 100644
--- a/telephony/java/com/android/internal/telephony/RILConstants.java
+++ b/telephony/java/com/android/internal/telephony/RILConstants.java
@@ -96,6 +96,16 @@
     int NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA = 10; /* LTE, CDMA, EvDo, GSM/WCDMA */
     int NETWORK_MODE_LTE_ONLY       = 11; /* LTE Only mode. */
     int NETWORK_MODE_LTE_WCDMA      = 12; /* LTE/WCDMA */
+    int NETWORK_MODE_TDSCDMA_ONLY            = 13; /* TD-SCDMA only */
+    int NETWORK_MODE_TDSCDMA_WCDMA           = 14; /* TD-SCDMA and WCDMA */
+    int NETWORK_MODE_LTE_TDSCDMA             = 15; /* TD-SCDMA and LTE */
+    int NETWORK_MODE_TDSCDMA_GSM             = 16; /* TD-SCDMA and GSM */
+    int NETWORK_MODE_LTE_TDSCDMA_GSM         = 17; /* TD-SCDMA,GSM and LTE */
+    int NETWORK_MODE_TDSCDMA_GSM_WCDMA       = 18; /* TD-SCDMA, GSM/WCDMA */
+    int NETWORK_MODE_LTE_TDSCDMA_WCDMA       = 19; /* TD-SCDMA, WCDMA and LTE */
+    int NETWORK_MODE_LTE_TDSCDMA_GSM_WCDMA   = 20; /* TD-SCDMA, GSM/WCDMA and LTE */
+    int NETWORK_MODE_TDSCDMA_CDMA_EVDO_GSM_WCDMA  = 21; /*TD-SCDMA,EvDo,CDMA,GSM/WCDMA*/
+    int NETWORK_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA = 22; /* TD-SCDMA/LTE/GSM/WCDMA, CDMA, and EvDo */
     int PREFERRED_NETWORK_MODE      = SystemProperties.getInt("ro.telephony.default_network",
             NETWORK_MODE_WCDMA_PREF);
 
diff --git a/tests/SurfaceComposition/Android.mk b/tests/SurfaceComposition/Android.mk
new file mode 100644
index 0000000..95f69f1
--- /dev/null
+++ b/tests/SurfaceComposition/Android.mk
@@ -0,0 +1,34 @@
+# 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.
+
+LOCAL_PATH:= $(call my-dir)
+
+include $(CLEAR_VARS)
+
+# Don't include this package in any target
+LOCAL_MODULE_TAGS := tests
+# When built, explicitly put it in the data partition.
+LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)
+
+LOCAL_DEX_PREOPT := false
+
+LOCAL_PROGUARD_ENABLED := disabled
+
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+LOCAL_PACKAGE_NAME := SurfaceComposition
+
+LOCAL_SDK_VERSION := current
+
+include $(BUILD_PACKAGE)
diff --git a/tests/SurfaceComposition/AndroidManifest.xml b/tests/SurfaceComposition/AndroidManifest.xml
new file mode 100644
index 0000000..4c0a9b6
--- /dev/null
+++ b/tests/SurfaceComposition/AndroidManifest.xml
@@ -0,0 +1,36 @@
+<?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.
+ -->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="android.surfacecomposition">
+    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
+    <application android:theme="@style/noeffects">
+        <uses-library android:name="android.test.runner" />
+        <activity android:name="android.surfacecomposition.SurfaceCompositionMeasuringActivity" >
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+        <uses-library android:name="android.test.runner" />
+    </application>
+
+    <!--  self-instrumenting test package. -->
+    <instrumentation android:name="android.test.InstrumentationTestRunner"
+                     android:targetPackage="android.surfacecomposition">
+    </instrumentation>
+</manifest>
diff --git a/tests/SurfaceComposition/res/values/themes.xml b/tests/SurfaceComposition/res/values/themes.xml
new file mode 100644
index 0000000..254d707
--- /dev/null
+++ b/tests/SurfaceComposition/res/values/themes.xml
@@ -0,0 +1,25 @@
+<?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.
+ -->
+<resources>
+    <style name="noeffects" parent="@android:style/Theme.Holo.NoActionBar.Fullscreen">
+        <item name="android:windowNoTitle">true</item>
+        <item name="android:windowFullscreen">true</item>
+        <item name="android:fadingEdge">none</item>
+        <item name="android:windowContentTransitions">false</item>
+        <item name="android:windowAnimationStyle">@null</item>
+    </style>
+</resources>
diff --git a/tests/SurfaceComposition/src/android/surfacecomposition/CustomLayout.java b/tests/SurfaceComposition/src/android/surfacecomposition/CustomLayout.java
new file mode 100644
index 0000000..d626f10
--- /dev/null
+++ b/tests/SurfaceComposition/src/android/surfacecomposition/CustomLayout.java
@@ -0,0 +1,48 @@
+/*
+ * 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 android.surfacecomposition;
+
+import android.content.Context;
+import android.view.View;
+import android.view.ViewGroup;
+
+public class CustomLayout extends ViewGroup {
+    public CustomLayout(Context context) {
+        super(context);
+    }
+
+    public static class LayoutParams extends ViewGroup.LayoutParams {
+        private int mLeft, mTop, mRight, mBottom;
+
+        public LayoutParams(int left, int top, int right, int bottom) {
+            super(0, 0);
+            mLeft = left;
+            mTop = top;
+            mRight = right;
+            mBottom = bottom;
+        }
+    }
+
+    @Override
+    protected void onLayout(boolean changed, int l, int t, int r, int b) {
+        final int count = getChildCount();
+        for (int i = 0; i < count; i++) {
+            View child = getChildAt(i);
+            CustomLayout.LayoutParams lp = (CustomLayout.LayoutParams) child.getLayoutParams();
+            child.layout(lp.mLeft, lp.mTop, lp.mRight, lp.mBottom);
+        }
+    }
+}
diff --git a/tests/SurfaceComposition/src/android/surfacecomposition/CustomSurfaceView.java b/tests/SurfaceComposition/src/android/surfacecomposition/CustomSurfaceView.java
new file mode 100644
index 0000000..0430662
--- /dev/null
+++ b/tests/SurfaceComposition/src/android/surfacecomposition/CustomSurfaceView.java
@@ -0,0 +1,243 @@
+/*
+ * 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 android.surfacecomposition;
+
+import java.util.Random;
+
+import android.content.Context;
+import android.graphics.Canvas;
+import android.graphics.Paint;
+import android.view.Surface;
+import android.view.SurfaceHolder;
+import android.view.SurfaceView;
+
+/**
+ * This provides functionality to measure Surface update frame rate. The idea is to
+ * constantly invalidates Surface in a separate thread. Lowest possible way is to
+ * use SurfaceView which works with Surface. This gives a very small overhead
+ * and very close to Android internals. Note, that lockCanvas is blocking
+ * methods and it returns once SurfaceFlinger consumes previous buffer. This
+ * gives the change to measure real performance of Surface compositor.
+ */
+public class CustomSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
+    private final static long DURATION_TO_WARMUP_MS = 50;
+    private final static long DURATION_TO_MEASURE_ROUGH_MS = 500;
+    private final static long DURATION_TO_MEASURE_PRECISE_MS = 3000;
+    private final static Random mRandom = new Random();
+
+    private final Object mSurfaceLock = new Object();
+    private Surface mSurface;
+    private boolean mDrawNameOnReady = true;
+    private boolean mSurfaceWasChanged = false;
+    private String mName;
+    private Canvas mCanvas;
+
+    class ValidateThread extends Thread {
+        private double mFPS = 0.0f;
+        // Used to support early exit and prevent long computation.
+        private double mBadFPS;
+        private double mPerfectFPS;
+
+        ValidateThread(double badFPS, double perfectFPS) {
+            mBadFPS = badFPS;
+            mPerfectFPS = perfectFPS;
+        }
+
+        public void run() {
+            long startTime = System.currentTimeMillis();
+            while (System.currentTimeMillis() - startTime < DURATION_TO_WARMUP_MS) {
+                invalidateSurface(false);
+            }
+
+            startTime = System.currentTimeMillis();
+            long endTime;
+            int frameCnt = 0;
+            while (true) {
+                invalidateSurface(false);
+                endTime = System.currentTimeMillis();
+                ++frameCnt;
+                mFPS = (double)frameCnt * 1000.0 / (endTime - startTime);
+                if ((endTime - startTime) >= DURATION_TO_MEASURE_ROUGH_MS) {
+                    // Test if result looks too bad or perfect and stop early.
+                    if (mFPS <= mBadFPS || mFPS >= mPerfectFPS) {
+                        break;
+                    }
+                }
+                if ((endTime - startTime) >= DURATION_TO_MEASURE_PRECISE_MS) {
+                    break;
+                }
+            }
+        }
+
+        public double getFPS() {
+            return mFPS;
+        }
+    }
+
+    public CustomSurfaceView(Context context, String name) {
+        super(context);
+        mName = name;
+        getHolder().addCallback(this);
+    }
+
+    public void setMode(int pixelFormat, boolean drawNameOnReady) {
+        mDrawNameOnReady = drawNameOnReady;
+        getHolder().setFormat(pixelFormat);
+    }
+
+    public void acquireCanvas() {
+        synchronized (mSurfaceLock) {
+            if (mCanvas != null) {
+                throw new RuntimeException("Surface canvas was already acquired.");
+            }
+            if (mSurface != null) {
+                mCanvas = mSurface.lockCanvas(null);
+            }
+        }
+    }
+
+    public void releaseCanvas() {
+        synchronized (mSurfaceLock) {
+            if (mCanvas != null) {
+                if (mSurface == null) {
+                    throw new RuntimeException(
+                            "Surface was destroyed but canvas was not released.");
+                }
+                mSurface.unlockCanvasAndPost(mCanvas);
+                mCanvas = null;
+            }
+        }
+    }
+
+    /**
+     * Invalidate surface.
+     */
+    private void invalidateSurface(boolean drawSurfaceId) {
+        synchronized (mSurfaceLock) {
+            if (mSurface != null) {
+                Canvas canvas = mSurface.lockCanvas(null);
+                // Draw surface name for debug purpose only. This does not affect the test
+                // because it is drawn only during allocation.
+                if (drawSurfaceId) {
+                    int textSize = canvas.getHeight() / 24;
+                    Paint paint = new Paint();
+                    paint.setTextSize(textSize);
+                    int textWidth = (int)(paint.measureText(mName) + 0.5f);
+                    int x = mRandom.nextInt(canvas.getWidth() - textWidth);
+                    int y = textSize + mRandom.nextInt(canvas.getHeight() - textSize);
+                    // Create effect of fog to visually control correctness of composition.
+                    paint.setColor(0xFFFF8040);
+                    canvas.drawARGB(32, 255, 255, 255);
+                    canvas.drawText(mName, x, y, paint);
+                }
+                mSurface.unlockCanvasAndPost(canvas);
+            }
+        }
+    }
+
+    /**
+     * Wait until surface is created and ready to use or return immediately if surface
+     * already exists.
+     */
+    public void waitForSurfaceReady() {
+        synchronized (mSurfaceLock) {
+            if (mSurface == null) {
+                try {
+                    mSurfaceLock.wait(5000);
+                } catch(InterruptedException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (mSurface == null)
+                throw new RuntimeException("Surface is not ready.");
+            mSurfaceWasChanged = false;
+        }
+    }
+
+    /**
+     * Wait until surface is destroyed or return immediately if surface does not exist.
+     */
+    public void waitForSurfaceDestroyed() {
+        synchronized (mSurfaceLock) {
+            if (mSurface != null) {
+                try {
+                    mSurfaceLock.wait(5000);
+                } catch(InterruptedException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (mSurface != null)
+                throw new RuntimeException("Surface still exists.");
+            mSurfaceWasChanged = false;
+        }
+    }
+
+    /**
+     * Validate that surface has not been changed since waitForSurfaceReady or
+     * waitForSurfaceDestroyed.
+     */
+    public void validateSurfaceNotChanged() {
+        synchronized (mSurfaceLock) {
+            if (mSurfaceWasChanged) {
+                throw new RuntimeException("Surface was changed during the test execution.");
+            }
+        }
+    }
+
+    public double measureFPS(double badFPS, double perfectFPS) {
+        try {
+            ValidateThread validateThread = new ValidateThread(badFPS, perfectFPS);
+            validateThread.start();
+            validateThread.join();
+            return validateThread.getFPS();
+        } catch (InterruptedException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    @Override
+    public void surfaceCreated(SurfaceHolder holder) {
+        synchronized (mSurfaceLock) {
+            mSurfaceWasChanged = true;
+        }
+    }
+
+    @Override
+    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
+        // This method is always called at least once, after surfaceCreated.
+        synchronized (mSurfaceLock) {
+            mSurface = holder.getSurface();
+            // We only need to invalidate the surface for the compositor performance test so that
+            // it gets included in the composition process. For allocation performance we
+            // don't need to invalidate surface and this allows us to remove non-necessary
+            // surface invalidation from the test.
+            if (mDrawNameOnReady) {
+                invalidateSurface(true);
+            }
+            mSurfaceWasChanged = true;
+            mSurfaceLock.notify();
+        }
+    }
+
+    @Override
+    public void surfaceDestroyed(SurfaceHolder holder) {
+        synchronized (mSurfaceLock) {
+            mSurface = null;
+            mSurfaceWasChanged = true;
+            mSurfaceLock.notify();
+        }
+    }
+}
diff --git a/tests/SurfaceComposition/src/android/surfacecomposition/MemoryAccessTask.java b/tests/SurfaceComposition/src/android/surfacecomposition/MemoryAccessTask.java
new file mode 100644
index 0000000..c716dae
--- /dev/null
+++ b/tests/SurfaceComposition/src/android/surfacecomposition/MemoryAccessTask.java
@@ -0,0 +1,71 @@
+/*
+ * 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 android.surfacecomposition;
+
+import android.util.Log;
+
+/**
+ * This task will simulate CPU activity by consuming memory bandwidth from the system.
+ * Note: On most system the CPU and GPU will share the same memory.
+ */
+public class MemoryAccessTask {
+    private final static String TAG = "MemoryAccessTask";
+    private final static int BUFFER_SIZE = 32 * 1024 * 1024;
+    private final static int BUFFER_STEP = 256;
+    private boolean mStopRequested;
+    private WorkThread mThread;
+    private final Object mLock = new Object();
+
+    public class WorkThread extends Thread {
+        public void run() {
+            byte[] memory = new byte[BUFFER_SIZE];
+            while (true) {
+                synchronized (mLock) {
+                    if (mStopRequested) {
+                        break;
+                    }
+                }
+                long result = 0;
+                for (int index = 0; index < BUFFER_SIZE; index += BUFFER_STEP) {
+                    result += ++memory[index];
+                }
+                Log.v(TAG, "Processing...:" + result);
+            }
+        }
+    }
+
+    public void start() {
+        if (mThread != null) {
+            throw new RuntimeException("Work thread is already started");
+        }
+        mStopRequested = false;
+        mThread = new WorkThread();
+        mThread.start();
+    }
+
+    public void stop() {
+        if (mThread != null) {
+            synchronized (mLock) {
+                mStopRequested = true;
+            }
+            try {
+                mThread.join();
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+}
diff --git a/tests/SurfaceComposition/src/android/surfacecomposition/SurfaceCompositionMeasuringActivity.java b/tests/SurfaceComposition/src/android/surfacecomposition/SurfaceCompositionMeasuringActivity.java
new file mode 100644
index 0000000..e3e1d34
--- /dev/null
+++ b/tests/SurfaceComposition/src/android/surfacecomposition/SurfaceCompositionMeasuringActivity.java
@@ -0,0 +1,602 @@
+/*
+ * 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 android.surfacecomposition;
+
+import java.text.DecimalFormat;
+import java.util.ArrayList;
+import java.util.List;
+
+import android.app.ActionBar;
+import android.app.Activity;
+import android.app.ActivityManager;
+import android.app.ActivityManager.MemoryInfo;
+import android.content.Context;
+import android.graphics.Color;
+import android.graphics.PixelFormat;
+import android.graphics.Rect;
+import android.graphics.drawable.ColorDrawable;
+import android.os.Bundle;
+import android.view.Display;
+import android.view.View;
+import android.view.View.OnClickListener;
+import android.view.ViewGroup;
+import android.view.Window;
+import android.view.WindowManager;
+import android.widget.ArrayAdapter;
+import android.widget.Button;
+import android.widget.LinearLayout;
+import android.widget.RelativeLayout;
+import android.widget.Spinner;
+import android.widget.TextView;
+
+/**
+ * This activity is designed to measure peformance scores of Android surfaces.
+ * It can work in two modes. In first mode functionality of this activity is
+ * invoked from Cts test (SurfaceCompositionTest). This activity can also be
+ * used in manual mode as a normal app. Different pixel formats are supported.
+ *
+ * measureCompositionScore(pixelFormat)
+ *   This test measures surface compositor performance which shows how many
+ *   surfaces of specific format surface compositor can combine without dropping
+ *   frames. We allow one dropped frame per half second.
+ *
+ * measureAllocationScore(pixelFormat)
+ *   This test measures surface allocation/deallocation performance. It shows
+ *   how many surface lifecycles (creation, destruction) can be done per second.
+ *
+ * In manual mode, which activated by pressing button 'Compositor speed' or
+ * 'Allocator speed', all possible pixel format are tested and combined result
+ * is displayed in text view. Additional system information such as memory
+ * status, display size and surface format is also displayed and regulary
+ * updated.
+ */
+public class SurfaceCompositionMeasuringActivity extends Activity implements OnClickListener {
+    private final static int MIN_NUMBER_OF_SURFACES = 15;
+    private final static int MAX_NUMBER_OF_SURFACES = 40;
+    private final static int WARM_UP_ALLOCATION_CYCLES = 2;
+    private final static int MEASURE_ALLOCATION_CYCLES = 5;
+    private final static int TEST_COMPOSITOR = 1;
+    private final static int TEST_ALLOCATION = 2;
+    private final static float MIN_REFRESH_RATE_SUPPORTED = 50.0f;
+
+    private final static DecimalFormat DOUBLE_FORMAT = new DecimalFormat("#.00");
+    // Possible selection in pixel format selector.
+    private final static int[] PIXEL_FORMATS = new int[] {
+            PixelFormat.TRANSLUCENT,
+            PixelFormat.TRANSPARENT,
+            PixelFormat.OPAQUE,
+            PixelFormat.RGBA_8888,
+            PixelFormat.RGBX_8888,
+            PixelFormat.RGB_888,
+            PixelFormat.RGB_565,
+    };
+
+
+    private List<CustomSurfaceView> mViews = new ArrayList<CustomSurfaceView>();
+    private Button mMeasureCompositionButton;
+    private Button mMeasureAllocationButton;
+    private Spinner mPixelFormatSelector;
+    private TextView mResultView;
+    private TextView mSystemInfoView;
+    private final Object mLockResumed = new Object();
+    private boolean mResumed;
+
+    // Drop one frame per half second.
+    // TODO(khmel)
+    // Add a feature flag and set the target FPS dependent on the target system as e.g.:
+    // 59FPS for MULTI_WINDOW and 54 otherwise (to satisfy the default lax Android requirements).
+    private double mRefreshRate;
+    private double mTargetFPS;
+
+    private int mWidth;
+    private int mHeight;
+
+    class CompositorScore {
+        double mSurfaces;
+        double mBitrate;
+
+        @Override
+        public String toString() {
+            return DOUBLE_FORMAT.format(mSurfaces) + " surfaces. " +
+                    "Bitrate: " + getReadableMemory((long)mBitrate) + "/s";
+        }
+    }
+
+    /**
+     * Measure performance score.
+     *
+     * @return biggest possible number of visible surfaces which surface
+     *         compositor can handle.
+     */
+    public CompositorScore measureCompositionScore(int pixelFormat) {
+        waitForActivityResumed();
+        //MemoryAccessTask memAccessTask = new MemoryAccessTask();
+        //memAccessTask.start();
+        // Destroy any active surface.
+        configureSurfacesAndWait(0, pixelFormat, false);
+        CompositorScore score = new CompositorScore();
+        score.mSurfaces = measureCompositionScore(new Measurement(0, 60.0),
+                new Measurement(mViews.size() + 1, 0.0f), pixelFormat);
+        // Assume 32 bits per pixel.
+        score.mBitrate = score.mSurfaces * mTargetFPS * mWidth * mHeight * 4.0;
+        //memAccessTask.stop();
+        return score;
+    }
+
+    static class AllocationScore {
+        double mMedian;
+        double mMin;
+        double mMax;
+
+        @Override
+        public String toString() {
+            return DOUBLE_FORMAT.format(mMedian) + " (min:" + DOUBLE_FORMAT.format(mMin) +
+                    ", max:" + DOUBLE_FORMAT.format(mMax) + ") surface allocations per second";
+        }
+    }
+
+    public AllocationScore measureAllocationScore(int pixelFormat) {
+        waitForActivityResumed();
+        AllocationScore score = new AllocationScore();
+        for (int i = 0; i < MEASURE_ALLOCATION_CYCLES + WARM_UP_ALLOCATION_CYCLES; ++i) {
+            long time1 = System.currentTimeMillis();
+            configureSurfacesAndWait(MIN_NUMBER_OF_SURFACES, pixelFormat, false);
+            acquireSurfacesCanvas();
+            long time2 = System.currentTimeMillis();
+            releaseSurfacesCanvas();
+            configureSurfacesAndWait(0, pixelFormat, false);
+            // Give SurfaceFlinger some time to rebuild the layer stack and release the buffers.
+            try {
+                Thread.sleep(500);
+            } catch(InterruptedException e) {
+                e.printStackTrace();
+            }
+            if (i < WARM_UP_ALLOCATION_CYCLES) {
+                // This is warm-up cycles, ignore result so far.
+                continue;
+            }
+            double speed = MIN_NUMBER_OF_SURFACES * 1000.0 / (time2 - time1);
+            score.mMedian += speed / MEASURE_ALLOCATION_CYCLES;
+            if (i == WARM_UP_ALLOCATION_CYCLES) {
+                score.mMin = speed;
+                score.mMax = speed;
+            } else {
+                score.mMin = Math.min(score.mMin, speed);
+                score.mMax = Math.max(score.mMax, speed);
+            }
+        }
+
+        return score;
+    }
+
+    @Override
+    public void onClick(View view) {
+        if (view == mMeasureCompositionButton) {
+            doTest(TEST_COMPOSITOR);
+        } else if (view == mMeasureAllocationButton) {
+            doTest(TEST_ALLOCATION);
+        }
+    }
+
+    private void doTest(final int test) {
+        enableControls(false);
+        final int pixelFormat = PIXEL_FORMATS[mPixelFormatSelector.getSelectedItemPosition()];
+        new Thread() {
+            public void run() {
+                final StringBuffer sb = new StringBuffer();
+                switch (test) {
+                    case TEST_COMPOSITOR: {
+                            sb.append("Compositor score:");
+                            CompositorScore score = measureCompositionScore(pixelFormat);
+                            sb.append("\n    " + getPixelFormatInfo(pixelFormat) + ":" +
+                                    score + ".");
+                        }
+                        break;
+                    case TEST_ALLOCATION: {
+                            sb.append("Allocation score:");
+                            AllocationScore score = measureAllocationScore(pixelFormat);
+                            sb.append("\n    " + getPixelFormatInfo(pixelFormat) + ":" +
+                                    score + ".");
+                        }
+                        break;
+                }
+                runOnUiThreadAndWait(new Runnable() {
+                    public void run() {
+                        mResultView.setText(sb.toString());
+                        enableControls(true);
+                        updateSystemInfo(pixelFormat);
+                    }
+                });
+            }
+        }.start();
+    }
+
+    /**
+     * Wait until activity is resumed.
+     */
+    public void waitForActivityResumed() {
+        synchronized (mLockResumed) {
+            if (!mResumed) {
+                try {
+                    mLockResumed.wait(10000);
+                } catch (InterruptedException e) {
+                }
+            }
+            if (!mResumed) {
+                throw new RuntimeException("Activity was not resumed");
+            }
+        }
+    }
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
+
+        detectRefreshRate();
+
+        // To layouts in parent. First contains list of Surfaces and second
+        // controls. Controls stay on top.
+        RelativeLayout rootLayout = new RelativeLayout(this);
+        rootLayout.setLayoutParams(new ViewGroup.LayoutParams(
+                ViewGroup.LayoutParams.MATCH_PARENT,
+                ViewGroup.LayoutParams.MATCH_PARENT));
+
+        CustomLayout layout = new CustomLayout(this);
+        layout.setLayoutParams(new ViewGroup.LayoutParams(
+                ViewGroup.LayoutParams.MATCH_PARENT,
+                ViewGroup.LayoutParams.MATCH_PARENT));
+
+        Rect rect = new Rect();
+        getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
+        mWidth = rect.right;
+        mHeight = rect.bottom;
+        long maxMemoryPerSurface = roundToNextPowerOf2(mWidth) * roundToNextPowerOf2(mHeight) * 4;
+        // Use 75% of available memory.
+        int surfaceCnt = (int)((getMemoryInfo().availMem * 3) / (4 * maxMemoryPerSurface));
+        if (surfaceCnt < MIN_NUMBER_OF_SURFACES) {
+            throw new RuntimeException("Not enough memory to allocate " +
+                    MIN_NUMBER_OF_SURFACES + " surfaces.");
+        }
+        if (surfaceCnt > MAX_NUMBER_OF_SURFACES) {
+            surfaceCnt = MAX_NUMBER_OF_SURFACES;
+        }
+
+        LinearLayout controlLayout = new LinearLayout(this);
+        controlLayout.setOrientation(LinearLayout.VERTICAL);
+        controlLayout.setLayoutParams(new ViewGroup.LayoutParams(
+                ViewGroup.LayoutParams.MATCH_PARENT,
+                ViewGroup.LayoutParams.MATCH_PARENT));
+
+        mMeasureCompositionButton = createButton("Compositor speed.", controlLayout);
+        mMeasureAllocationButton = createButton("Allocation speed", controlLayout);
+
+        String[] pixelFomats = new String[PIXEL_FORMATS.length];
+        for (int i = 0; i < pixelFomats.length; ++i) {
+            pixelFomats[i] = getPixelFormatInfo(PIXEL_FORMATS[i]);
+        }
+        mPixelFormatSelector = new Spinner(this);
+        ArrayAdapter<String> pixelFormatSelectorAdapter =
+                new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, pixelFomats);
+        pixelFormatSelectorAdapter.setDropDownViewResource(
+                android.R.layout.simple_spinner_dropdown_item);
+        mPixelFormatSelector.setAdapter(pixelFormatSelectorAdapter);
+        mPixelFormatSelector.setLayoutParams(new LinearLayout.LayoutParams(
+                ViewGroup.LayoutParams.WRAP_CONTENT,
+                ViewGroup.LayoutParams.WRAP_CONTENT));
+        controlLayout.addView(mPixelFormatSelector);
+
+        mResultView = new TextView(this);
+        mResultView.setBackgroundColor(0);
+        mResultView.setText("Press button to start test.");
+        mResultView.setLayoutParams(new LinearLayout.LayoutParams(
+                ViewGroup.LayoutParams.WRAP_CONTENT,
+                ViewGroup.LayoutParams.WRAP_CONTENT));
+        controlLayout.addView(mResultView);
+
+        mSystemInfoView = new TextView(this);
+        mSystemInfoView.setBackgroundColor(0);
+        mSystemInfoView.setLayoutParams(new LinearLayout.LayoutParams(
+                ViewGroup.LayoutParams.WRAP_CONTENT,
+                ViewGroup.LayoutParams.WRAP_CONTENT));
+        controlLayout.addView(mSystemInfoView);
+
+        for (int i = 0; i < surfaceCnt; ++i) {
+            CustomSurfaceView view = new CustomSurfaceView(this, "Surface:" + i);
+            // Create all surfaces overlapped in order to prevent SurfaceFlinger
+            // to filter out surfaces by optimization in case surface is opaque.
+            // In case surface is transparent it will be drawn anyway. Note that first
+            // surface covers whole screen and must stand below other surfaces. Z order of
+            // layers is not predictable and there is only one way to force first
+            // layer to be below others is to mark it as media and all other layers
+            // to mark as media overlay.
+            if (i == 0) {
+                view.setLayoutParams(new CustomLayout.LayoutParams(0, 0, mWidth, mHeight));
+                view.setZOrderMediaOverlay(false);
+            } else {
+                // Z order of other layers is not predefined so make offset on x and reverse
+                // offset on y to make sure that surface is visible in any layout.
+                int x = i;
+                int y = (surfaceCnt - i);
+                view.setLayoutParams(new CustomLayout.LayoutParams(x, y, x + mWidth, y + mHeight));
+                view.setZOrderMediaOverlay(true);
+            }
+            view.setVisibility(View.INVISIBLE);
+            layout.addView(view);
+            mViews.add(view);
+        }
+
+        rootLayout.addView(layout);
+        rootLayout.addView(controlLayout);
+
+        setContentView(rootLayout);
+    }
+
+    private Button createButton(String caption, LinearLayout layout) {
+        Button button = new Button(this);
+        button.setText(caption);
+        button.setLayoutParams(new LinearLayout.LayoutParams(
+                ViewGroup.LayoutParams.WRAP_CONTENT,
+                ViewGroup.LayoutParams.WRAP_CONTENT));
+        button.setOnClickListener(this);
+        layout.addView(button);
+        return button;
+    }
+
+    private void enableControls(boolean enabled) {
+        mMeasureCompositionButton.setEnabled(enabled);
+        mMeasureAllocationButton.setEnabled(enabled);
+        mPixelFormatSelector.setEnabled(enabled);
+    }
+
+    @Override
+    protected void onResume() {
+        super.onResume();
+
+        updateSystemInfo(PixelFormat.UNKNOWN);
+
+        synchronized (mLockResumed) {
+            mResumed = true;
+            mLockResumed.notifyAll();
+        }
+    }
+
+    @Override
+    protected void onPause() {
+        super.onPause();
+
+        synchronized (mLockResumed) {
+            mResumed = false;
+        }
+    }
+
+    class Measurement {
+        Measurement(int surfaceCnt, double fps) {
+            mSurfaceCnt = surfaceCnt;
+            mFPS = fps;
+        }
+
+        public final int mSurfaceCnt;
+        public final double mFPS;
+    }
+
+    private double measureCompositionScore(Measurement ok, Measurement fail, int pixelFormat) {
+        if (ok.mSurfaceCnt + 1 == fail.mSurfaceCnt) {
+            // Interpolate result.
+            double fraction = (mTargetFPS - fail.mFPS) / (ok.mFPS - fail.mFPS);
+            return ok.mSurfaceCnt + fraction;
+        }
+
+        int medianSurfaceCnt = (ok.mSurfaceCnt + fail.mSurfaceCnt) / 2;
+        Measurement median = new Measurement(medianSurfaceCnt,
+                measureFPS(medianSurfaceCnt, pixelFormat));
+
+        if (median.mFPS >= mTargetFPS) {
+            return measureCompositionScore(median, fail, pixelFormat);
+        } else {
+            return measureCompositionScore(ok, median, pixelFormat);
+        }
+    }
+
+    private double measureFPS(int surfaceCnt, int pixelFormat) {
+        configureSurfacesAndWait(surfaceCnt, pixelFormat, true);
+        // At least one view is visible and it is enough to update only
+        // one overlapped surface in order to force SurfaceFlinger to send
+        // all surfaces to compositor.
+        double fps = mViews.get(0).measureFPS(mRefreshRate * 0.8, mRefreshRate * 0.999);
+
+        // Make sure that surface configuration was not changed.
+        validateSurfacesNotChanged();
+
+        return fps;
+    }
+
+    private void waitForSurfacesConfigured(final int pixelFormat) {
+        for (int i = 0; i < mViews.size(); ++i) {
+            CustomSurfaceView view = mViews.get(i);
+            if (view.getVisibility() == View.VISIBLE) {
+                view.waitForSurfaceReady();
+            } else {
+                view.waitForSurfaceDestroyed();
+            }
+        }
+        runOnUiThreadAndWait(new Runnable() {
+            @Override
+            public void run() {
+                updateSystemInfo(pixelFormat);
+            }
+        });
+    }
+
+    private void validateSurfacesNotChanged() {
+        for (int i = 0; i < mViews.size(); ++i) {
+            CustomSurfaceView view = mViews.get(i);
+            view.validateSurfaceNotChanged();
+        }
+    }
+
+    private void configureSurfaces(int surfaceCnt, int pixelFormat, boolean invalidate) {
+        for (int i = 0; i < mViews.size(); ++i) {
+            CustomSurfaceView view = mViews.get(i);
+            if (i < surfaceCnt) {
+                view.setMode(pixelFormat, invalidate);
+                view.setVisibility(View.VISIBLE);
+            } else {
+                view.setVisibility(View.INVISIBLE);
+            }
+        }
+    }
+
+    private void configureSurfacesAndWait(final int surfaceCnt, final int pixelFormat,
+            final boolean invalidate) {
+        runOnUiThreadAndWait(new Runnable() {
+            @Override
+            public void run() {
+                configureSurfaces(surfaceCnt, pixelFormat, invalidate);
+            }
+        });
+        waitForSurfacesConfigured(pixelFormat);
+    }
+
+    private void acquireSurfacesCanvas() {
+        for (int i = 0; i < mViews.size(); ++i) {
+            CustomSurfaceView view = mViews.get(i);
+            view.acquireCanvas();
+        }
+    }
+
+    private void releaseSurfacesCanvas() {
+        for (int i = 0; i < mViews.size(); ++i) {
+            CustomSurfaceView view = mViews.get(i);
+            view.releaseCanvas();
+        }
+    }
+
+    private static String getReadableMemory(long bytes) {
+        long unit = 1024;
+        if (bytes < unit) {
+            return bytes + " B";
+        }
+        int exp = (int) (Math.log(bytes) / Math.log(unit));
+        return String.format("%.1f %sB", bytes / Math.pow(unit, exp),
+                "KMGTPE".charAt(exp-1));
+    }
+
+    private MemoryInfo getMemoryInfo() {
+        ActivityManager activityManager = (ActivityManager)
+                getSystemService(ACTIVITY_SERVICE);
+        MemoryInfo memInfo = new MemoryInfo();
+        activityManager.getMemoryInfo(memInfo);
+        return memInfo;
+    }
+
+    private void updateSystemInfo(int pixelFormat) {
+        int visibleCnt = 0;
+        for (int i = 0; i < mViews.size(); ++i) {
+            if (mViews.get(i).getVisibility() == View.VISIBLE) {
+                ++visibleCnt;
+            }
+        }
+
+        MemoryInfo memInfo = getMemoryInfo();
+        String info = "Available " +
+                getReadableMemory(memInfo.availMem) + " from " +
+                getReadableMemory(memInfo.totalMem) + ".\nVisible " +
+                visibleCnt + " from " + mViews.size() + " " +
+                getPixelFormatInfo(pixelFormat) + " surfaces.\n" +
+                "View size: " + mWidth + "x" + mHeight +
+                ". Refresh rate: " + DOUBLE_FORMAT.format(mRefreshRate) + ".";
+        mSystemInfoView.setText(info);
+    }
+
+    private void detectRefreshRate() {
+        WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
+        mRefreshRate = wm.getDefaultDisplay().getRefreshRate();
+        if (mRefreshRate < MIN_REFRESH_RATE_SUPPORTED)
+            throw new RuntimeException("Unsupported display refresh rate: " + mRefreshRate);
+        mTargetFPS = mRefreshRate - 2.0f;
+    }
+
+    private int roundToNextPowerOf2(int value) {
+        --value;
+        value |= value >> 1;
+        value |= value >> 2;
+        value |= value >> 4;
+        value |= value >> 8;
+        value |= value >> 16;
+        return value + 1;
+    }
+
+    public static String getPixelFormatInfo(int pixelFormat) {
+        switch (pixelFormat) {
+        case PixelFormat.TRANSLUCENT:
+            return "TRANSLUCENT";
+        case PixelFormat.TRANSPARENT:
+            return "TRANSPARENT";
+        case PixelFormat.OPAQUE:
+            return "OPAQUE";
+        case PixelFormat.RGBA_8888:
+            return "RGBA_8888";
+        case PixelFormat.RGBX_8888:
+            return "RGBX_8888";
+        case PixelFormat.RGB_888:
+            return "RGB_888";
+        case PixelFormat.RGB_565:
+            return "RGB_565";
+        default:
+            return "PIX.FORMAT:" + pixelFormat;
+        }
+    }
+
+    /**
+     * A helper that executes a task in the UI thread and waits for its completion.
+     *
+     * @param task - task to execute.
+     */
+    private void runOnUiThreadAndWait(Runnable task) {
+        new UIExecutor(task);
+    }
+
+    class UIExecutor implements Runnable {
+        private final Object mLock = new Object();
+        private Runnable mTask;
+        private boolean mDone = false;
+
+        UIExecutor(Runnable task) {
+            mTask = task;
+            mDone = false;
+            runOnUiThread(this);
+            synchronized (mLock) {
+                while (!mDone) {
+                    try {
+                        mLock.wait();
+                    } catch (InterruptedException e) {
+                        e.printStackTrace();
+                    }
+                }
+            }
+        }
+
+        public void run() {
+            mTask.run();
+            synchronized (mLock) {
+                mDone = true;
+                mLock.notify();
+            }
+        }
+    }
+}
diff --git a/tests/SurfaceComposition/src/android/surfacecomposition/SurfaceCompositionTest.java b/tests/SurfaceComposition/src/android/surfacecomposition/SurfaceCompositionTest.java
new file mode 100644
index 0000000..6e9e739
--- /dev/null
+++ b/tests/SurfaceComposition/src/android/surfacecomposition/SurfaceCompositionTest.java
@@ -0,0 +1,81 @@
+/*
+ * 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 android.surfacecomposition;
+
+import android.graphics.PixelFormat;
+import android.surfacecomposition.SurfaceCompositionMeasuringActivity.AllocationScore;
+import android.surfacecomposition.SurfaceCompositionMeasuringActivity.CompositorScore;
+import android.test.ActivityInstrumentationTestCase2;
+import android.test.suitebuilder.annotation.SmallTest;
+import android.util.Log;
+
+public class SurfaceCompositionTest extends
+        ActivityInstrumentationTestCase2<SurfaceCompositionMeasuringActivity> {
+    private final static String TAG = "SurfaceCompositionTest";
+
+    // Pass threshold for major pixel formats.
+    private final static int[] TEST_PIXEL_FORMATS = new int[] {
+        PixelFormat.TRANSLUCENT,
+        PixelFormat.OPAQUE,
+    };
+
+    // Based on Nexus 9 performance which is usually < 9.0.
+    private final static double[] MIN_ACCEPTED_COMPOSITION_SCORE = new double[] {
+        8.0,
+        8.0,
+    };
+
+    // Based on Nexus 6 performance which is usually < 28.0.
+    private final static double[] MIN_ACCEPTED_ALLOCATION_SCORE = new double[] {
+        20.0,
+        20.0,
+    };
+
+    public SurfaceCompositionTest() {
+        super(SurfaceCompositionMeasuringActivity.class);
+    }
+
+    private void testRestoreContexts() {
+    }
+
+    @SmallTest
+    public void testSurfaceCompositionPerformance() {
+        for (int i = 0; i < TEST_PIXEL_FORMATS.length; ++i) {
+            int pixelFormat = TEST_PIXEL_FORMATS[i];
+            String formatName = SurfaceCompositionMeasuringActivity.getPixelFormatInfo(pixelFormat);
+            CompositorScore score = getActivity().measureCompositionScore(pixelFormat);
+            Log.i(TAG, "testSurfaceCompositionPerformance(" + formatName + ") = " + score);
+            assertTrue("Device does not support surface(" + formatName + ") composition " +
+                    "performance score. " + score.mSurfaces + " < " +
+                    MIN_ACCEPTED_COMPOSITION_SCORE[i] + ".",
+                    score.mSurfaces >= MIN_ACCEPTED_COMPOSITION_SCORE[i]);
+        }
+    }
+
+    @SmallTest
+    public void testSurfaceAllocationPerformance() {
+        for (int i = 0; i < TEST_PIXEL_FORMATS.length; ++i) {
+            int pixelFormat = TEST_PIXEL_FORMATS[i];
+            String formatName = SurfaceCompositionMeasuringActivity.getPixelFormatInfo(pixelFormat);
+            AllocationScore score = getActivity().measureAllocationScore(pixelFormat);
+            Log.i(TAG, "testSurfaceAllocationPerformance(" + formatName + ") = " + score);
+            assertTrue("Device does not support surface(" + formatName + ") allocation " +
+                    "performance score. " + score.mMedian + " < " +
+                    MIN_ACCEPTED_ALLOCATION_SCORE[i] + ".",
+                    score.mMedian >= MIN_ACCEPTED_ALLOCATION_SCORE[i]);
+        }
+    }
+}
diff --git a/tests/UiBench/Android.mk b/tests/UiBench/Android.mk
index 1e5b117..24df85b 100644
--- a/tests/UiBench/Android.mk
+++ b/tests/UiBench/Android.mk
@@ -10,14 +10,18 @@
 # regressions are reflected in test data
 LOCAL_RESOURCE_DIR := \
     $(LOCAL_PATH)/res \
-    frameworks/support/v7/appcompat/res
+    frameworks/support/v7/appcompat/res \
+    frameworks/support/v7/cardview/res
 
 LOCAL_AAPT_FLAGS := \
-    --extra-packages android.support.v7.appcompat
+    --auto-add-overlay \
+    --extra-packages android.support.v7.appcompat \
+    --extra-packages android.support.v7.cardview
 
 LOCAL_STATIC_JAVA_LIBRARIES := \
     android-support-v4 \
-    android-support-v7-appcompat
+    android-support-v7-appcompat \
+    android-support-v7-cardview
 
 LOCAL_PACKAGE_NAME := UiBench
 
diff --git a/tests/UiBench/AndroidManifest.xml b/tests/UiBench/AndroidManifest.xml
index 6677e0d..9b3d186 100644
--- a/tests/UiBench/AndroidManifest.xml
+++ b/tests/UiBench/AndroidManifest.xml
@@ -14,11 +14,13 @@
   ~ limitations under the License
   -->
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
-        package="com.android.test.uibench">
+    xmlns:tools="http://schemas.android.com/tools"
+    package="com.android.test.uibench">
 
     <application
         android:allowBackup="false"
-        android:theme="@style/Theme.AppCompat.Light.DarkActionBar">
+        android:theme="@style/Theme.AppCompat.Light.DarkActionBar"
+        tools:ignore="MissingApplicationIcon">
         <uses-library android:name="android.test.runner" />
 
         <!-- Root navigation activity -->
@@ -32,10 +34,18 @@
             </intent-filter>
         </activity>
 
-        <!-- Tests -->
+        <!-- General -->
+        <activity
+            android:name=".DialogListActivity"
+            android:label="General/Dialog List" >
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="com.android.test.uibench.TEST" />
+            </intent-filter>
+        </activity>
         <activity
             android:name=".GlTextureViewActivity"
-            android:label="Microbenchmarks/GL TextureView" >
+            android:label="General/GL TextureView" >
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="com.android.test.uibench.TEST" />
@@ -43,7 +53,15 @@
         </activity>
         <activity
             android:name=".FullscreenOverdrawActivity"
-            android:label="Microbenchmarks/Fullscreen Overdraw" >
+            android:label="General/Fullscreen Overdraw" >
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="com.android.test.uibench.TEST" />
+            </intent-filter>
+        </activity>
+        <activity
+            android:name=".InvalidateActivity"
+            android:label="General/Invalidate" >
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="com.android.test.uibench.TEST" />
@@ -51,7 +69,7 @@
         </activity>
         <activity
             android:name=".TrivialAnimationActivity"
-            android:label="Microbenchmarks/Trivial Animation" >
+            android:label="General/Trivial Animation" >
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="com.android.test.uibench.TEST" />
@@ -59,7 +77,61 @@
         </activity>
         <activity
             android:name=".TrivialListActivity"
-            android:label="Microbenchmarks/Trivial ListView" >
+            android:label="General/Trivial ListView" >
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="com.android.test.uibench.TEST" />
+            </intent-filter>
+        </activity>
+
+        <!-- Rendering -->
+        <activity
+            android:name=".BitmapUploadActivity"
+            android:label="Rendering/Bitmap Upload" >
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="com.android.test.uibench.TEST" />
+            </intent-filter>
+        </activity>
+        <activity
+            android:name=".ShadowGridActivity"
+            android:label="Rendering/Shadow Grid" >
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="com.android.test.uibench.TEST" />
+            </intent-filter>
+        </activity>
+
+        <!-- Inflation -->
+        <activity
+            android:name=".InflatingListActivity"
+            android:label="Inflation/Inflating ListView" >
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="com.android.test.uibench.TEST" />
+            </intent-filter>
+        </activity>
+
+        <!-- Text -->
+        <activity
+            android:name=".EditTextTypeActivity"
+            android:label="Text/EditText Typing" >
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="com.android.test.uibench.TEST" />
+            </intent-filter>
+        </activity>
+        <activity
+            android:name=".TextCacheLowHitrateActivity"
+            android:label="Text/Layout Cache Low Hitrate" >
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="com.android.test.uibench.TEST" />
+            </intent-filter>
+        </activity>
+        <activity
+            android:name=".TextCacheHighHitrateActivity"
+            android:label="Text/Layout Cache High Hitrate" >
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="com.android.test.uibench.TEST" />
diff --git a/tests/UiBench/build.gradle b/tests/UiBench/build.gradle
index bf0ed11..deecbb6 100644
--- a/tests/UiBench/build.gradle
+++ b/tests/UiBench/build.gradle
@@ -34,4 +34,5 @@
     // Dependencies enumerated specifically for platform-independent / reproducible builds.
     compile 'com.android.support:support-v4:23.0.0'
     compile 'com.android.support:appcompat-v7:23.0.0'
+    compile 'com.android.support:cardview-v7:23.0.0'
 }
diff --git a/tests/UiBench/res/layout/activity_bitmap_upload.xml b/tests/UiBench/res/layout/activity_bitmap_upload.xml
new file mode 100644
index 0000000..70faa07
--- /dev/null
+++ b/tests/UiBench/res/layout/activity_bitmap_upload.xml
@@ -0,0 +1,52 @@
+<?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:id="@+id/upload_root"
+    android:orientation="vertical"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:padding="10dp"
+    android:clipToPadding="false">
+    <android.support.v7.widget.CardView
+        android:layout_width="match_parent"
+        android:layout_height="0dp"
+        android:layout_weight="1">
+        <view class="com.android.test.uibench.BitmapUploadActivity$UploadView"
+            android:id="@+id/upload_view"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"/>
+    </android.support.v7.widget.CardView>
+
+    <android.support.v4.widget.Space
+        android:layout_height="10dp"
+        android:layout_width="match_parent" />
+
+    <android.support.v7.widget.CardView
+        android:layout_width="match_parent"
+        android:layout_height="0dp"
+        android:layout_weight="1" />
+
+    <android.support.v4.widget.Space
+        android:layout_height="10dp"
+        android:layout_width="match_parent" />
+
+    <android.support.v7.widget.CardView
+        android:layout_width="match_parent"
+        android:layout_height="0dp"
+        android:layout_weight="1" />
+</LinearLayout>
\ No newline at end of file
diff --git a/tests/UiBench/res/layout/activity_invalidate.xml b/tests/UiBench/res/layout/activity_invalidate.xml
new file mode 100644
index 0000000..34bcca9
--- /dev/null
+++ b/tests/UiBench/res/layout/activity_invalidate.xml
@@ -0,0 +1,39 @@
+<?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:id="@+id/invalidate_root"
+    android:orientation="vertical"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent">
+    <include layout="@layout/invalidate_row"/>
+    <include layout="@layout/invalidate_row"/>
+    <include layout="@layout/invalidate_row"/>
+    <include layout="@layout/invalidate_row"/>
+    <include layout="@layout/invalidate_row"/>
+
+    <include layout="@layout/invalidate_row"/>
+    <include layout="@layout/invalidate_row"/>
+    <include layout="@layout/invalidate_row"/>
+    <include layout="@layout/invalidate_row"/>
+    <include layout="@layout/invalidate_row"/>
+
+    <include layout="@layout/invalidate_row"/>
+    <include layout="@layout/invalidate_row"/>
+    <include layout="@layout/invalidate_row"/>
+    <include layout="@layout/invalidate_row"/>
+    <include layout="@layout/invalidate_row"/>
+</LinearLayout>
\ No newline at end of file
diff --git a/tests/UiBench/res/layout/card_row.xml b/tests/UiBench/res/layout/card_row.xml
new file mode 100644
index 0000000..215f9df
--- /dev/null
+++ b/tests/UiBench/res/layout/card_row.xml
@@ -0,0 +1,45 @@
+<?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:orientation="horizontal"
+    android:layout_width="match_parent"
+    android:layout_height="100dp"
+    android:paddingStart="10dp"
+    android:paddingEnd="10dp"
+    android:paddingTop="5dp"
+    android:paddingBottom="5dp"
+    android:clipToPadding="false"
+    android:background="@null">
+    <android.support.v7.widget.CardView
+        android:layout_width="0dp"
+        android:layout_height="match_parent"
+        android:layout_weight="1">
+        <TextView
+            android:id="@+id/card_text"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"/>
+    </android.support.v7.widget.CardView>
+
+    <android.support.v4.widget.Space
+        android:layout_height="match_parent"
+        android:layout_width="10dp" />
+
+    <android.support.v7.widget.CardView
+        android:layout_width="0dp"
+        android:layout_height="match_parent"
+        android:layout_weight="1" />
+</LinearLayout>
\ No newline at end of file
diff --git a/tests/UiBench/res/layout/invalidate_row.xml b/tests/UiBench/res/layout/invalidate_row.xml
new file mode 100644
index 0000000..9feefde
--- /dev/null
+++ b/tests/UiBench/res/layout/invalidate_row.xml
@@ -0,0 +1,84 @@
+<?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:orientation="horizontal"
+    android:layout_width="match_parent"
+    android:layout_height="0dp"
+    android:layout_weight="1">
+    <view class="com.android.test.uibench.InvalidateActivity$ColorView"
+        android:layout_weight="1"
+        android:layout_width="0dp"
+        android:layout_height="match_parent"/>
+    <view class="com.android.test.uibench.InvalidateActivity$ColorView"
+        android:layout_weight="1"
+        android:layout_width="0dp"
+        android:layout_height="match_parent"/>
+    <view class="com.android.test.uibench.InvalidateActivity$ColorView"
+        android:layout_weight="1"
+        android:layout_width="0dp"
+        android:layout_height="match_parent"/>
+    <view class="com.android.test.uibench.InvalidateActivity$ColorView"
+        android:layout_weight="1"
+        android:layout_width="0dp"
+        android:layout_height="match_parent"/>
+    <view class="com.android.test.uibench.InvalidateActivity$ColorView"
+        android:layout_weight="1"
+        android:layout_width="0dp"
+        android:layout_height="match_parent"/>
+
+    <view class="com.android.test.uibench.InvalidateActivity$ColorView"
+        android:layout_weight="1"
+        android:layout_width="0dp"
+        android:layout_height="match_parent"/>
+    <view class="com.android.test.uibench.InvalidateActivity$ColorView"
+        android:layout_weight="1"
+        android:layout_width="0dp"
+        android:layout_height="match_parent"/>
+    <view class="com.android.test.uibench.InvalidateActivity$ColorView"
+        android:layout_weight="1"
+        android:layout_width="0dp"
+        android:layout_height="match_parent"/>
+    <view class="com.android.test.uibench.InvalidateActivity$ColorView"
+        android:layout_weight="1"
+        android:layout_width="0dp"
+        android:layout_height="match_parent"/>
+    <view class="com.android.test.uibench.InvalidateActivity$ColorView"
+        android:layout_weight="1"
+        android:layout_width="0dp"
+        android:layout_height="match_parent"/>
+
+    <view class="com.android.test.uibench.InvalidateActivity$ColorView"
+        android:layout_weight="1"
+        android:layout_width="0dp"
+        android:layout_height="match_parent"/>
+    <view class="com.android.test.uibench.InvalidateActivity$ColorView"
+        android:layout_weight="1"
+        android:layout_width="0dp"
+        android:layout_height="match_parent"/>
+    <view class="com.android.test.uibench.InvalidateActivity$ColorView"
+        android:layout_weight="1"
+        android:layout_width="0dp"
+        android:layout_height="match_parent"/>
+    <view class="com.android.test.uibench.InvalidateActivity$ColorView"
+        android:layout_weight="1"
+        android:layout_width="0dp"
+        android:layout_height="match_parent"/>
+    <view class="com.android.test.uibench.InvalidateActivity$ColorView"
+        android:layout_weight="1"
+        android:layout_width="0dp"
+        android:layout_height="match_parent"/>
+</LinearLayout>
\ No newline at end of file
diff --git a/tests/UiBench/src/com/android/test/uibench/BitmapUploadActivity.java b/tests/UiBench/src/com/android/test/uibench/BitmapUploadActivity.java
new file mode 100644
index 0000000..e2bf897
--- /dev/null
+++ b/tests/UiBench/src/com/android/test/uibench/BitmapUploadActivity.java
@@ -0,0 +1,96 @@
+/*
+ * 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.test.uibench;
+
+import android.animation.ObjectAnimator;
+import android.animation.ValueAnimator;
+import android.content.Context;
+import android.graphics.Bitmap;
+import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.Rect;
+import android.os.Bundle;
+import android.support.v7.app.AppCompatActivity;
+import android.util.AttributeSet;
+import android.util.DisplayMetrics;
+import android.view.View;
+
+public class BitmapUploadActivity extends AppCompatActivity {
+    public static class UploadView extends View {
+        private int mColorValue;
+        private Bitmap mBitmap;
+        private final DisplayMetrics mMetrics = new DisplayMetrics();
+        private final Rect mRect = new Rect();
+
+        public UploadView(Context context, AttributeSet attrs) {
+            super(context, attrs);
+        }
+
+        @SuppressWarnings("unused")
+        public void setColorValue(int colorValue) {
+            if (colorValue == mColorValue) return;
+
+            mColorValue = colorValue;
+
+            // modify the bitmap's color to ensure it's uploaded to the GPU
+            mBitmap.eraseColor(Color.rgb(mColorValue, 255 - mColorValue, 255));
+
+            invalidate();
+        }
+
+        @Override
+        protected void onAttachedToWindow() {
+            super.onAttachedToWindow();
+
+            getDisplay().getMetrics(mMetrics);
+            int minDisplayDimen = Math.min(mMetrics.widthPixels, mMetrics.heightPixels);
+            int bitmapSize = Math.min((int) (minDisplayDimen * 0.75), 720);
+            if (mBitmap == null
+                    || mBitmap.getWidth() != bitmapSize
+                    || mBitmap.getHeight() != bitmapSize) {
+                mBitmap = Bitmap.createBitmap(bitmapSize, bitmapSize, Bitmap.Config.ARGB_8888);
+            }
+        }
+
+        @Override
+        protected void onDraw(Canvas canvas) {
+            if (mBitmap != null) {
+                mRect.set(0, 0, getWidth(), getHeight());
+                canvas.drawBitmap(mBitmap, null, mRect, null);
+            }
+        }
+    }
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.activity_bitmap_upload);
+
+        // animate color to force bitmap uploads
+        UploadView uploadView = (UploadView) findViewById(R.id.upload_view);
+        ObjectAnimator colorValueAnimator = ObjectAnimator.ofInt(uploadView, "colorValue", 0, 255);
+        colorValueAnimator.setRepeatMode(ValueAnimator.REVERSE);
+        colorValueAnimator.setRepeatCount(ValueAnimator.INFINITE);
+        colorValueAnimator.start();
+
+        // animate scene root to guarantee there's a minimum amount of GPU rendering work
+        View uploadRoot = findViewById(R.id.upload_root);
+        ObjectAnimator yAnimator = ObjectAnimator.ofFloat(uploadRoot, "translationY", 0, 100);
+        yAnimator.setRepeatMode(ValueAnimator.REVERSE);
+        yAnimator.setRepeatCount(ValueAnimator.INFINITE);
+        yAnimator.start();
+    }
+}
diff --git a/tests/UiBench/src/com/android/test/uibench/CompatListActivity.java b/tests/UiBench/src/com/android/test/uibench/CompatListActivity.java
new file mode 100644
index 0000000..40ec453
--- /dev/null
+++ b/tests/UiBench/src/com/android/test/uibench/CompatListActivity.java
@@ -0,0 +1,39 @@
+/*
+ * 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.test.uibench;
+
+import android.os.Bundle;
+import android.support.v4.app.FragmentManager;
+import android.support.v4.app.ListFragment;
+import android.support.v7.app.AppCompatActivity;
+import android.widget.ListAdapter;
+
+public abstract class CompatListActivity extends AppCompatActivity {
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        FragmentManager fm = getSupportFragmentManager();
+        if (fm.findFragmentById(android.R.id.content) == null) {
+            ListFragment listFragment = new ListFragment();
+            listFragment.setListAdapter(createListAdapter());
+            fm.beginTransaction().add(android.R.id.content, listFragment).commit();
+        }
+    }
+
+    protected abstract ListAdapter createListAdapter();
+}
diff --git a/tests/UiBench/src/com/android/test/uibench/DialogListActivity.java b/tests/UiBench/src/com/android/test/uibench/DialogListActivity.java
new file mode 100644
index 0000000..fe712d5
--- /dev/null
+++ b/tests/UiBench/src/com/android/test/uibench/DialogListActivity.java
@@ -0,0 +1,38 @@
+/*
+ * 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.test.uibench;
+
+import android.os.Bundle;
+import android.support.v7.app.AlertDialog;
+import android.support.v7.app.AppCompatActivity;
+import android.widget.ArrayAdapter;
+import android.widget.ListView;
+
+public class DialogListActivity extends AppCompatActivity {
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        ListView listView = new ListView(this);
+        listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
+                TextUtils.buildSimpleStringList()));
+
+        AlertDialog.Builder builder = new AlertDialog.Builder(this);
+        builder.setTitle("Dialog");
+        builder.setView(listView);
+        builder.create().show();
+    }
+}
diff --git a/tests/UiBench/src/com/android/test/uibench/EditTextTypeActivity.java b/tests/UiBench/src/com/android/test/uibench/EditTextTypeActivity.java
new file mode 100644
index 0000000..08ab510
--- /dev/null
+++ b/tests/UiBench/src/com/android/test/uibench/EditTextTypeActivity.java
@@ -0,0 +1,110 @@
+/*
+ * 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.test.uibench;
+
+import android.app.Instrumentation;
+import android.os.Bundle;
+import android.os.Looper;
+import android.os.MessageQueue;
+import android.support.v7.app.AppCompatActivity;
+import android.view.KeyEvent;
+import android.widget.EditText;
+
+import java.util.concurrent.Semaphore;
+
+/**
+ * Note: currently incomplete, complexity of input continuously grows, instead of looping
+ * over a stable amount of work.
+ *
+ * Simulates typing continuously into an EditText.
+ */
+public class EditTextTypeActivity extends AppCompatActivity {
+    Thread mThread;
+
+    private static String sSeedText = "";
+    static {
+        final int count = 100;
+        final String string = "hello ";
+
+        StringBuilder builder = new StringBuilder(count * string.length());
+        for (int i = 0; i < count; i++) {
+            builder.append(string);
+        }
+        sSeedText = builder.toString();
+    }
+
+    final Object mLock = new Object();
+    boolean mShouldStop = false;
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        EditText editText = new EditText(this);
+        editText.setText(sSeedText);
+        setContentView(editText);
+
+        final Instrumentation instrumentation = new Instrumentation();
+        final Semaphore sem = new Semaphore(0);
+        MessageQueue.IdleHandler handler = new MessageQueue.IdleHandler() {
+            @Override
+            public boolean queueIdle() {
+                // TODO: consider other signaling approaches
+                sem.release();
+                return true;
+            }
+        };
+        Looper.myQueue().addIdleHandler(handler);
+        synchronized (mLock) {
+            mShouldStop = false;
+        }
+        mThread = new Thread(new Runnable() {
+            int codes[] = { KeyEvent.KEYCODE_H, KeyEvent.KEYCODE_E, KeyEvent.KEYCODE_L,
+                    KeyEvent.KEYCODE_L, KeyEvent.KEYCODE_O, KeyEvent.KEYCODE_SPACE };
+            int i = 0;
+            @Override
+            public void run() {
+                while (true) {
+                    try {
+                        sem.acquire();
+                    } catch (InterruptedException e) {
+                        // TODO, maybe
+                    }
+                    int code = codes[i % codes.length];
+                    if (i % 100 == 99) code = KeyEvent.KEYCODE_ENTER;
+
+                    synchronized (mLock) {
+                        if (mShouldStop) break;
+                    }
+
+                    // TODO: bit of a race here, since the event can arrive after pause/stop.
+                    // (Can't synchronize on key send, since it's synchronous.)
+                    instrumentation.sendKeyDownUpSync(code);
+                    i++;
+                }
+            }
+        });
+        mThread.start();
+    }
+
+    @Override
+    protected void onPause() {
+        synchronized (mLock) {
+            mShouldStop = true;
+        }
+        super.onPause();
+    }
+}
diff --git a/tests/UiBench/src/com/android/test/uibench/GlTextureViewActivity.java b/tests/UiBench/src/com/android/test/uibench/GlTextureViewActivity.java
index ce79259..a12742d 100644
--- a/tests/UiBench/src/com/android/test/uibench/GlTextureViewActivity.java
+++ b/tests/UiBench/src/com/android/test/uibench/GlTextureViewActivity.java
@@ -18,39 +18,19 @@
 
 import android.animation.ObjectAnimator;
 import android.animation.ValueAnimator;
-import android.content.res.Resources;
-import android.graphics.Bitmap;
-import android.graphics.BitmapFactory;
 import android.graphics.SurfaceTexture;
-import android.opengl.GLUtils;
 import android.os.Bundle;
-import android.os.Environment;
 import android.support.v7.app.AppCompatActivity;
 import android.util.Log;
 import android.view.Gravity;
 import android.view.TextureView;
-import android.view.View;
 import android.view.ViewGroup;
 import android.widget.FrameLayout;
 
-import javax.microedition.khronos.egl.EGL10;
-import javax.microedition.khronos.egl.EGLConfig;
-import javax.microedition.khronos.egl.EGLContext;
-import javax.microedition.khronos.egl.EGLDisplay;
-import javax.microedition.khronos.egl.EGLSurface;
-import java.io.BufferedOutputStream;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.nio.ByteBuffer;
-import java.nio.ByteOrder;
-import java.nio.FloatBuffer;
-
-import static android.opengl.GLES20.*;
+import com.android.test.uibench.opengl.ImageFlipRenderThread;
 
 public class GlTextureViewActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener {
-    private RenderThread mRenderThread;
+    private ImageFlipRenderThread mRenderThread;
     private TextureView mTextureView;
 
     @Override
@@ -66,7 +46,7 @@
 
     @Override
     public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
-        mRenderThread = new RenderThread(getResources(), surface);
+        mRenderThread = new ImageFlipRenderThread(getResources(), surface);
         mRenderThread.start();
 
         mTextureView.setCameraDistance(5000);
@@ -94,7 +74,7 @@
         try {
             mRenderThread.join();
         } catch (InterruptedException e) {
-            Log.e(RenderThread.LOG_TAG, "Could not wait for render thread");
+            Log.e(ImageFlipRenderThread.LOG_TAG, "Could not wait for render thread");
         }
         return true;
     }
@@ -103,311 +83,4 @@
     public void onSurfaceTextureUpdated(SurfaceTexture surface) {
     }
 
-    private static class RenderThread extends Thread {
-        private static final String LOG_TAG = "GLTextureView";
-
-        static final int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
-        static final int EGL_OPENGL_ES2_BIT = 4;
-
-        private volatile boolean mFinished;
-
-        private final Resources mResources;
-        private final SurfaceTexture mSurface;
-
-        private EGL10 mEgl;
-        private EGLDisplay mEglDisplay;
-        private EGLConfig mEglConfig;
-        private EGLContext mEglContext;
-        private EGLSurface mEglSurface;
-
-        RenderThread(Resources resources, SurfaceTexture surface) {
-            mResources = resources;
-            mSurface = surface;
-        }
-
-        private static final String sSimpleVS =
-                "attribute vec4 position;\n" +
-                "attribute vec2 texCoords;\n" +
-                "varying vec2 outTexCoords;\n" +
-                "\nvoid main(void) {\n" +
-                "    outTexCoords = texCoords;\n" +
-                "    gl_Position = position;\n" +
-                "}\n\n";
-        private static final String sSimpleFS =
-                "precision mediump float;\n\n" +
-                "varying vec2 outTexCoords;\n" +
-                "uniform sampler2D texture;\n" +
-                "\nvoid main(void) {\n" +
-                "    gl_FragColor = texture2D(texture, outTexCoords);\n" +
-                "}\n\n";
-
-        private static final int FLOAT_SIZE_BYTES = 4;
-        private static final int TRIANGLE_VERTICES_DATA_STRIDE_BYTES = 5 * FLOAT_SIZE_BYTES;
-        private static final int TRIANGLE_VERTICES_DATA_POS_OFFSET = 0;
-        private static final int TRIANGLE_VERTICES_DATA_UV_OFFSET = 3;
-        private final float[] mTriangleVerticesData = {
-                // X, Y, Z, U, V
-                -1.0f, -1.0f, 0.0f, 0.0f, 0.0f,
-                 1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
-                -1.0f,  1.0f, 0.0f, 0.0f, 1.0f,
-                 1.0f,  1.0f, 0.0f, 1.0f, 1.0f,
-        };
-
-        @Override
-        public void run() {
-            initGL();
-
-            FloatBuffer triangleVertices = ByteBuffer.allocateDirect(mTriangleVerticesData.length
-                    * FLOAT_SIZE_BYTES).order(ByteOrder.nativeOrder()).asFloatBuffer();
-            triangleVertices.put(mTriangleVerticesData).position(0);
-
-            int texture = loadTexture(R.drawable.large_photo);
-            int program = buildProgram(sSimpleVS, sSimpleFS);
-
-            int attribPosition = glGetAttribLocation(program, "position");
-            checkGlError();
-
-            int attribTexCoords = glGetAttribLocation(program, "texCoords");
-            checkGlError();
-
-            int uniformTexture = glGetUniformLocation(program, "texture");
-            checkGlError();
-
-            glBindTexture(GL_TEXTURE_2D, texture);
-            checkGlError();
-
-            glUseProgram(program);
-            checkGlError();
-
-            glEnableVertexAttribArray(attribPosition);
-            checkGlError();
-
-            glEnableVertexAttribArray(attribTexCoords);
-            checkGlError();
-
-            glUniform1i(uniformTexture, 0);
-            checkGlError();
-
-            // drawQuad
-            triangleVertices.position(TRIANGLE_VERTICES_DATA_POS_OFFSET);
-            glVertexAttribPointer(attribPosition, 3, GL_FLOAT, false,
-                    TRIANGLE_VERTICES_DATA_STRIDE_BYTES, triangleVertices);
-            checkGlError();
-
-            triangleVertices.position(TRIANGLE_VERTICES_DATA_UV_OFFSET);
-            glVertexAttribPointer(attribTexCoords, 3, GL_FLOAT, false,
-                    TRIANGLE_VERTICES_DATA_STRIDE_BYTES, triangleVertices);
-            checkGlError();
-
-            glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
-            checkGlError();
-
-            while (!mFinished) {
-                checkCurrent();
-
-                glClear(GL_COLOR_BUFFER_BIT);
-                checkGlError();
-
-                glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
-                checkGlError();
-
-                if (!mEgl.eglSwapBuffers(mEglDisplay, mEglSurface)) {
-                    throw new RuntimeException("Cannot swap buffers");
-                }
-                checkEglError();
-
-                try {
-                    Thread.sleep(2000);
-                } catch (InterruptedException e) {
-                    // Ignore
-                }
-            }
-
-            finishGL();
-        }
-
-        private int loadTexture(int resource) {
-            int[] textures = new int[1];
-
-            glActiveTexture(GL_TEXTURE0);
-            glGenTextures(1, textures, 0);
-            checkGlError();
-
-            int texture = textures[0];
-            glBindTexture(GL_TEXTURE_2D, texture);
-            checkGlError();
-
-            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
-            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-
-            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
-            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
-
-            Bitmap bitmap = BitmapFactory.decodeResource(mResources, resource);
-
-            GLUtils.texImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bitmap, GL_UNSIGNED_BYTE, 0);
-            checkGlError();
-
-            bitmap.recycle();
-
-            return texture;
-        }
-
-        private static int buildProgram(String vertex, String fragment) {
-            int vertexShader = buildShader(vertex, GL_VERTEX_SHADER);
-            if (vertexShader == 0) return 0;
-
-            int fragmentShader = buildShader(fragment, GL_FRAGMENT_SHADER);
-            if (fragmentShader == 0) return 0;
-
-            int program = glCreateProgram();
-            glAttachShader(program, vertexShader);
-            checkGlError();
-
-            glAttachShader(program, fragmentShader);
-            checkGlError();
-
-            glLinkProgram(program);
-            checkGlError();
-
-            int[] status = new int[1];
-            glGetProgramiv(program, GL_LINK_STATUS, status, 0);
-            if (status[0] != GL_TRUE) {
-                String error = glGetProgramInfoLog(program);
-                Log.d(LOG_TAG, "Error while linking program:\n" + error);
-                glDeleteShader(vertexShader);
-                glDeleteShader(fragmentShader);
-                glDeleteProgram(program);
-                return 0;
-            }
-
-            return program;
-        }
-
-        private static int buildShader(String source, int type) {
-            int shader = glCreateShader(type);
-
-            glShaderSource(shader, source);
-            checkGlError();
-
-            glCompileShader(shader);
-            checkGlError();
-
-            int[] status = new int[1];
-            glGetShaderiv(shader, GL_COMPILE_STATUS, status, 0);
-            if (status[0] != GL_TRUE) {
-                String error = glGetShaderInfoLog(shader);
-                Log.d(LOG_TAG, "Error while compiling shader:\n" + error);
-                glDeleteShader(shader);
-                return 0;
-            }
-
-            return shader;
-        }
-
-        private void checkEglError() {
-            int error = mEgl.eglGetError();
-            if (error != EGL10.EGL_SUCCESS) {
-                Log.w(LOG_TAG, "EGL error = 0x" + Integer.toHexString(error));
-            }
-        }
-
-        private static void checkGlError() {
-            int error = glGetError();
-            if (error != GL_NO_ERROR) {
-                Log.w(LOG_TAG, "GL error = 0x" + Integer.toHexString(error));
-            }
-        }
-
-        private void finishGL() {
-            mEgl.eglDestroyContext(mEglDisplay, mEglContext);
-            mEgl.eglDestroySurface(mEglDisplay, mEglSurface);
-        }
-
-        private void checkCurrent() {
-            if (!mEglContext.equals(mEgl.eglGetCurrentContext()) ||
-                    !mEglSurface.equals(mEgl.eglGetCurrentSurface(EGL10.EGL_DRAW))) {
-                if (!mEgl.eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext)) {
-                    throw new RuntimeException("eglMakeCurrent failed "
-                            + GLUtils.getEGLErrorString(mEgl.eglGetError()));
-                }
-            }
-        }
-
-        private void initGL() {
-            mEgl = (EGL10) EGLContext.getEGL();
-
-            mEglDisplay = mEgl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
-            if (mEglDisplay == EGL10.EGL_NO_DISPLAY) {
-                throw new RuntimeException("eglGetDisplay failed "
-                        + GLUtils.getEGLErrorString(mEgl.eglGetError()));
-            }
-
-            int[] version = new int[2];
-            if (!mEgl.eglInitialize(mEglDisplay, version)) {
-                throw new RuntimeException("eglInitialize failed " +
-                        GLUtils.getEGLErrorString(mEgl.eglGetError()));
-            }
-
-            mEglConfig = chooseEglConfig();
-            if (mEglConfig == null) {
-                throw new RuntimeException("eglConfig not initialized");
-            }
-
-            mEglContext = createContext(mEgl, mEglDisplay, mEglConfig);
-
-            mEglSurface = mEgl.eglCreateWindowSurface(mEglDisplay, mEglConfig, mSurface, null);
-
-            if (mEglSurface == null || mEglSurface == EGL10.EGL_NO_SURFACE) {
-                int error = mEgl.eglGetError();
-                if (error == EGL10.EGL_BAD_NATIVE_WINDOW) {
-                    Log.e(LOG_TAG, "createWindowSurface returned EGL_BAD_NATIVE_WINDOW.");
-                    return;
-                }
-                throw new RuntimeException("createWindowSurface failed "
-                        + GLUtils.getEGLErrorString(error));
-            }
-
-            if (!mEgl.eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext)) {
-                throw new RuntimeException("eglMakeCurrent failed "
-                        + GLUtils.getEGLErrorString(mEgl.eglGetError()));
-            }
-        }
-
-
-        EGLContext createContext(EGL10 egl, EGLDisplay eglDisplay, EGLConfig eglConfig) {
-            int[] attrib_list = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE };
-            return egl.eglCreateContext(eglDisplay, eglConfig, EGL10.EGL_NO_CONTEXT, attrib_list);
-        }
-
-        private EGLConfig chooseEglConfig() {
-            int[] configsCount = new int[1];
-            EGLConfig[] configs = new EGLConfig[1];
-            int[] configSpec = getConfig();
-            if (!mEgl.eglChooseConfig(mEglDisplay, configSpec, configs, 1, configsCount)) {
-                throw new IllegalArgumentException("eglChooseConfig failed " +
-                        GLUtils.getEGLErrorString(mEgl.eglGetError()));
-            } else if (configsCount[0] > 0) {
-                return configs[0];
-            }
-            return null;
-        }
-
-        private static int[] getConfig() {
-            return new int[] {
-                    EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
-                    EGL10.EGL_RED_SIZE, 8,
-                    EGL10.EGL_GREEN_SIZE, 8,
-                    EGL10.EGL_BLUE_SIZE, 8,
-                    EGL10.EGL_ALPHA_SIZE, 8,
-                    EGL10.EGL_DEPTH_SIZE, 0,
-                    EGL10.EGL_STENCIL_SIZE, 0,
-                    EGL10.EGL_NONE
-            };
-        }
-
-        void finish() {
-            mFinished = true;
-        }
-    }
 }
\ No newline at end of file
diff --git a/tests/UiBench/src/com/android/test/uibench/InflatingListActivity.java b/tests/UiBench/src/com/android/test/uibench/InflatingListActivity.java
new file mode 100644
index 0000000..84383ec
--- /dev/null
+++ b/tests/UiBench/src/com/android/test/uibench/InflatingListActivity.java
@@ -0,0 +1,35 @@
+/*
+ * 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.test.uibench;
+
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ArrayAdapter;
+import android.widget.ListAdapter;
+
+public class InflatingListActivity extends CompatListActivity {
+    @Override
+    protected ListAdapter createListAdapter() {
+        return new ArrayAdapter<String>(this,
+                android.R.layout.simple_list_item_1, TextUtils.buildSimpleStringList()) {
+            @Override
+            public View getView(int position, View convertView, ViewGroup parent) {
+                // pathological getView behavior: drop convertView on the floor to force inflation
+                return super.getView(position, null, parent);
+            }
+        };
+    }
+}
diff --git a/tests/UiBench/src/com/android/test/uibench/InvalidateActivity.java b/tests/UiBench/src/com/android/test/uibench/InvalidateActivity.java
new file mode 100644
index 0000000..93d67a6
--- /dev/null
+++ b/tests/UiBench/src/com/android/test/uibench/InvalidateActivity.java
@@ -0,0 +1,88 @@
+/*
+ * 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.test.uibench;
+
+import android.animation.ObjectAnimator;
+import android.animation.ValueAnimator;
+import android.content.Context;
+import android.graphics.Canvas;
+import android.graphics.Color;
+import android.os.Bundle;
+import android.support.annotation.ColorInt;
+import android.support.v7.app.AppCompatActivity;
+import android.util.AttributeSet;
+import android.view.View;
+import android.view.ViewGroup;
+
+/**
+ * Tests invalidation performance by invalidating a large number of easily rendered views,
+ */
+public class InvalidateActivity extends AppCompatActivity {
+    public static class ColorView extends View {
+        @ColorInt
+        public int mColor;
+
+        public ColorView(Context context, AttributeSet attrs) {
+            super(context, attrs);
+        }
+
+        public void setColor(@ColorInt int color) {
+            mColor = color;
+            invalidate();
+        }
+
+        @Override
+        protected void onDraw(Canvas canvas) {
+            canvas.drawColor(mColor);
+        }
+    }
+
+    ColorView[][] mColorViews;
+
+    @SuppressWarnings("unused")
+    public void setColorValue(int colorValue) {
+        @ColorInt int a = Color.rgb(colorValue, 255 - colorValue, 255);
+        @ColorInt int b = Color.rgb(255, colorValue, 255 - colorValue);
+        for (int y = 0; y < mColorViews.length; y++) {
+            for (int x = 0; x < mColorViews[y].length; x++) {
+                mColorViews[y][x].setColor((x + y) % 2 == 0 ? a : b);
+            }
+        }
+    }
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.activity_invalidate);
+
+        ViewGroup root = (ViewGroup) findViewById(R.id.invalidate_root);
+        for (int y = 0; y < root.getChildCount(); y++) {
+            ViewGroup row = (ViewGroup) root.getChildAt(y);
+            if (mColorViews == null) {
+                mColorViews = new ColorView[root.getChildCount()][row.getChildCount()];
+            }
+
+            for (int x = 0; x < row.getChildCount(); x++) {
+                mColorViews[y][x] = (ColorView) row.getChildAt(x);
+            }
+        }
+
+        ObjectAnimator animator = ObjectAnimator.ofInt(this, "colorValue", 0, 255);
+        animator.setRepeatMode(ValueAnimator.REVERSE);
+        animator.setRepeatCount(ValueAnimator.INFINITE);
+        animator.start();
+    }
+}
diff --git a/tests/UiBench/src/com/android/test/uibench/ShadowGridActivity.java b/tests/UiBench/src/com/android/test/uibench/ShadowGridActivity.java
new file mode 100644
index 0000000..d32f071
--- /dev/null
+++ b/tests/UiBench/src/com/android/test/uibench/ShadowGridActivity.java
@@ -0,0 +1,45 @@
+/*
+ * 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.test.uibench;
+
+import android.os.Bundle;
+import android.support.v4.app.FragmentManager;
+import android.support.v4.app.ListFragment;
+import android.support.v7.app.AppCompatActivity;
+import android.view.View;
+import android.widget.ArrayAdapter;
+
+public class ShadowGridActivity extends AppCompatActivity {
+    @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.setListAdapter(new ArrayAdapter<>(this,
+                    R.layout.card_row, R.id.card_text, TextUtils.buildSimpleStringList()));
+            fm.beginTransaction().add(android.R.id.content, listFragment).commit();
+        }
+    }
+}
diff --git a/tests/UiBench/src/com/android/test/uibench/TextCacheHighHitrateActivity.java b/tests/UiBench/src/com/android/test/uibench/TextCacheHighHitrateActivity.java
new file mode 100644
index 0000000..91d74ac
--- /dev/null
+++ b/tests/UiBench/src/com/android/test/uibench/TextCacheHighHitrateActivity.java
@@ -0,0 +1,27 @@
+/*
+ * 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.test.uibench;
+
+import android.widget.ArrayAdapter;
+import android.widget.ListAdapter;
+
+public class TextCacheHighHitrateActivity extends CompatListActivity {
+    @Override
+    protected ListAdapter createListAdapter() {
+        return new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
+                TextUtils.buildParagraphListWithHitPercentage(80));
+    }
+}
diff --git a/tests/UiBench/src/com/android/test/uibench/TextCacheLowHitrateActivity.java b/tests/UiBench/src/com/android/test/uibench/TextCacheLowHitrateActivity.java
new file mode 100644
index 0000000..b526cde
--- /dev/null
+++ b/tests/UiBench/src/com/android/test/uibench/TextCacheLowHitrateActivity.java
@@ -0,0 +1,27 @@
+/*
+ * 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.test.uibench;
+
+import android.widget.ArrayAdapter;
+import android.widget.ListAdapter;
+
+public class TextCacheLowHitrateActivity extends CompatListActivity {
+    @Override
+    protected ListAdapter createListAdapter() {
+        return new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
+                TextUtils.buildParagraphListWithHitPercentage(20));
+    }
+}
diff --git a/tests/UiBench/src/com/android/test/uibench/TextUtils.java b/tests/UiBench/src/com/android/test/uibench/TextUtils.java
new file mode 100644
index 0000000..d88ca1e
--- /dev/null
+++ b/tests/UiBench/src/com/android/test/uibench/TextUtils.java
@@ -0,0 +1,91 @@
+/*
+ * 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.test.uibench;
+
+import java.util.Random;
+
+public class TextUtils {
+    private static final int STRING_COUNT = 200;
+    private static final int SIMPLE_STRING_LENGTH = 10;
+
+    /**
+     * Create word of random assortment of lower/upper case letters
+     */
+    private static String randomWord(Random random, int length) {
+        String result = "";
+        for (int j = 0; j < length; j++) {
+            // add random letter
+            int base = random.nextInt(2) == 0 ? 'A' : 'a';
+            result += (char)(random.nextInt(26) + base);
+        }
+        return result;
+    }
+
+    public static String[] buildSimpleStringList() {
+        String[] strings = new String[STRING_COUNT];
+        Random random = new Random(0);
+        for (int i = 0; i < strings.length; i++) {
+            strings[i] = randomWord(random, SIMPLE_STRING_LENGTH);
+        }
+        return strings;
+    }
+
+    // a small number of strings reused frequently, expected to hit
+    // in the word-granularity text layout cache
+    static final String[] CACHE_HIT_STRINGS = new String[] {
+            "a",
+            "small",
+            "number",
+            "of",
+            "strings",
+            "reused",
+            "frequently"
+    };
+
+    private static final int WORDS_IN_PARAGRAPH = 150;
+
+    // misses are fairly long 'words' to ensure they miss
+    private static final int PARAGRAPH_MISS_MIN_LENGTH = 4;
+    private static final int PARAGRAPH_MISS_MAX_LENGTH = 9;
+
+    static String[] buildParagraphListWithHitPercentage(int hitPercentage) {
+        if (hitPercentage < 0 || hitPercentage > 100) throw new IllegalArgumentException();
+
+        String[] strings = new String[STRING_COUNT];
+        Random random = new Random(0);
+        for (int i = 0; i < strings.length; i++) {
+            String result = "";
+            for (int word = 0; word < WORDS_IN_PARAGRAPH; word++) {
+                if (word != 0) {
+                    result += " ";
+                }
+                if (random.nextInt(100) < hitPercentage) {
+                    // add a common word, which is very likely to hit in the cache
+                    result += CACHE_HIT_STRINGS[random.nextInt(CACHE_HIT_STRINGS.length)];
+                } else {
+                    // construct a random word, which will *most likely* miss
+                    int length = PARAGRAPH_MISS_MIN_LENGTH;
+                    length += random.nextInt(PARAGRAPH_MISS_MAX_LENGTH - PARAGRAPH_MISS_MIN_LENGTH);
+
+                    result += randomWord(random, length);
+                }
+            }
+            strings[i] = result;
+        }
+
+        return strings;
+    }
+}
diff --git a/tests/UiBench/src/com/android/test/uibench/TrivialListActivity.java b/tests/UiBench/src/com/android/test/uibench/TrivialListActivity.java
index 0af3471..339ac80 100644
--- a/tests/UiBench/src/com/android/test/uibench/TrivialListActivity.java
+++ b/tests/UiBench/src/com/android/test/uibench/TrivialListActivity.java
@@ -15,41 +15,13 @@
  */
 package com.android.test.uibench;
 
-import android.os.Bundle;
-import android.support.v4.app.FragmentManager;
-import android.support.v4.app.ListFragment;
-import android.support.v7.app.AppCompatActivity;
 import android.widget.ArrayAdapter;
+import android.widget.ListAdapter;
 
-import java.util.Random;
-
-public class TrivialListActivity extends AppCompatActivity {
-    static final int STRING_LENGTH = 10;
-
-    static String[] buildStringList() {
-        String[] strings = new String[200];
-        Random random = new Random(0);
-        for (int i = 0; i < strings.length; i++) {
-            String result = "";
-            for (int j = 0; j < STRING_LENGTH; j++) {
-                // add random letter
-                result += (char)(random.nextInt(26) + 65);
-            }
-            strings[i] = result;
-        }
-        return strings;
-    }
-
+public class TrivialListActivity extends CompatListActivity {
     @Override
-    protected void onCreate(Bundle savedInstanceState) {
-        super.onCreate(savedInstanceState);
-
-        FragmentManager fm = getSupportFragmentManager();
-        if (fm.findFragmentById(android.R.id.content) == null) {
-            ListFragment listFragment = new ListFragment();
-            listFragment.setListAdapter(new ArrayAdapter<>(TrivialListActivity.this,
-                    android.R.layout.simple_list_item_1, buildStringList()));
-            fm.beginTransaction().add(android.R.id.content, listFragment).commit();
-        }
+    protected ListAdapter createListAdapter() {
+        return new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
+                TextUtils.buildSimpleStringList());
     }
 }
diff --git a/tests/UiBench/src/com/android/test/uibench/opengl/ImageFlipRenderThread.java b/tests/UiBench/src/com/android/test/uibench/opengl/ImageFlipRenderThread.java
new file mode 100644
index 0000000..119ce52
--- /dev/null
+++ b/tests/UiBench/src/com/android/test/uibench/opengl/ImageFlipRenderThread.java
@@ -0,0 +1,389 @@
+/*
+ * 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.test.uibench.opengl;
+
+import android.content.res.Resources;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.graphics.SurfaceTexture;
+import android.opengl.GLUtils;
+import android.util.Log;
+
+import com.android.test.uibench.R;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.FloatBuffer;
+
+import javax.microedition.khronos.egl.EGL10;
+import javax.microedition.khronos.egl.EGLConfig;
+import javax.microedition.khronos.egl.EGLContext;
+import javax.microedition.khronos.egl.EGLDisplay;
+import javax.microedition.khronos.egl.EGLSurface;
+
+import static android.opengl.GLES20.GL_CLAMP_TO_EDGE;
+import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT;
+import static android.opengl.GLES20.GL_COMPILE_STATUS;
+import static android.opengl.GLES20.GL_FLOAT;
+import static android.opengl.GLES20.GL_FRAGMENT_SHADER;
+import static android.opengl.GLES20.GL_LINEAR;
+import static android.opengl.GLES20.GL_LINK_STATUS;
+import static android.opengl.GLES20.GL_NO_ERROR;
+import static android.opengl.GLES20.GL_RGBA;
+import static android.opengl.GLES20.GL_TEXTURE0;
+import static android.opengl.GLES20.GL_TEXTURE_2D;
+import static android.opengl.GLES20.GL_TEXTURE_MAG_FILTER;
+import static android.opengl.GLES20.GL_TEXTURE_MIN_FILTER;
+import static android.opengl.GLES20.GL_TEXTURE_WRAP_S;
+import static android.opengl.GLES20.GL_TEXTURE_WRAP_T;
+import static android.opengl.GLES20.GL_TRIANGLE_STRIP;
+import static android.opengl.GLES20.GL_TRUE;
+import static android.opengl.GLES20.GL_UNSIGNED_BYTE;
+import static android.opengl.GLES20.GL_VERTEX_SHADER;
+import static android.opengl.GLES20.glActiveTexture;
+import static android.opengl.GLES20.glAttachShader;
+import static android.opengl.GLES20.glBindTexture;
+import static android.opengl.GLES20.glClear;
+import static android.opengl.GLES20.glClearColor;
+import static android.opengl.GLES20.glCompileShader;
+import static android.opengl.GLES20.glCreateProgram;
+import static android.opengl.GLES20.glCreateShader;
+import static android.opengl.GLES20.glDeleteProgram;
+import static android.opengl.GLES20.glDeleteShader;
+import static android.opengl.GLES20.glDrawArrays;
+import static android.opengl.GLES20.glEnableVertexAttribArray;
+import static android.opengl.GLES20.glGenTextures;
+import static android.opengl.GLES20.glGetAttribLocation;
+import static android.opengl.GLES20.glGetError;
+import static android.opengl.GLES20.glGetProgramInfoLog;
+import static android.opengl.GLES20.glGetProgramiv;
+import static android.opengl.GLES20.glGetShaderInfoLog;
+import static android.opengl.GLES20.glGetShaderiv;
+import static android.opengl.GLES20.glGetUniformLocation;
+import static android.opengl.GLES20.glLinkProgram;
+import static android.opengl.GLES20.glShaderSource;
+import static android.opengl.GLES20.glTexParameteri;
+import static android.opengl.GLES20.glUniform1i;
+import static android.opengl.GLES20.glUseProgram;
+import static android.opengl.GLES20.glVertexAttribPointer;
+
+public class ImageFlipRenderThread extends Thread {
+    public static final String LOG_TAG = "GLTextureView";
+
+    static final int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
+    static final int EGL_OPENGL_ES2_BIT = 4;
+
+    private volatile boolean mFinished;
+
+    private final Resources mResources;
+    private final SurfaceTexture mSurface;
+
+    private EGL10 mEgl;
+    private EGLDisplay mEglDisplay;
+    private EGLConfig mEglConfig;
+    private EGLContext mEglContext;
+    private EGLSurface mEglSurface;
+
+    public ImageFlipRenderThread(Resources resources, SurfaceTexture surface) {
+        mResources = resources;
+        mSurface = surface;
+    }
+
+    private static final String sSimpleVS =
+            "attribute vec4 position;\n" +
+                    "attribute vec2 texCoords;\n" +
+                    "varying vec2 outTexCoords;\n" +
+                    "\nvoid main(void) {\n" +
+                    "    outTexCoords = texCoords;\n" +
+                    "    gl_Position = position;\n" +
+                    "}\n\n";
+    private static final String sSimpleFS =
+            "precision mediump float;\n\n" +
+                    "varying vec2 outTexCoords;\n" +
+                    "uniform sampler2D texture;\n" +
+                    "\nvoid main(void) {\n" +
+                    "    gl_FragColor = texture2D(texture, outTexCoords);\n" +
+                    "}\n\n";
+
+    private static final int FLOAT_SIZE_BYTES = 4;
+    private static final int TRIANGLE_VERTICES_DATA_STRIDE_BYTES = 5 * FLOAT_SIZE_BYTES;
+    private static final int TRIANGLE_VERTICES_DATA_POS_OFFSET = 0;
+    private static final int TRIANGLE_VERTICES_DATA_UV_OFFSET = 3;
+    private final float[] mTriangleVerticesData = {
+            // X, Y, Z, U, V
+            -1.0f, -1.0f, 0.0f, 0.0f, 0.0f,
+            1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
+            -1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
+            1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
+    };
+
+    @Override
+    public void run() {
+        initGL();
+
+        FloatBuffer triangleVertices = ByteBuffer.allocateDirect(mTriangleVerticesData.length
+                * FLOAT_SIZE_BYTES).order(ByteOrder.nativeOrder()).asFloatBuffer();
+        triangleVertices.put(mTriangleVerticesData).position(0);
+
+        int texture = loadTexture(R.drawable.large_photo);
+        int program = buildProgram(sSimpleVS, sSimpleFS);
+
+        int attribPosition = glGetAttribLocation(program, "position");
+        checkGlError();
+
+        int attribTexCoords = glGetAttribLocation(program, "texCoords");
+        checkGlError();
+
+        int uniformTexture = glGetUniformLocation(program, "texture");
+        checkGlError();
+
+        glBindTexture(GL_TEXTURE_2D, texture);
+        checkGlError();
+
+        glUseProgram(program);
+        checkGlError();
+
+        glEnableVertexAttribArray(attribPosition);
+        checkGlError();
+
+        glEnableVertexAttribArray(attribTexCoords);
+        checkGlError();
+
+        glUniform1i(uniformTexture, 0);
+        checkGlError();
+
+        // drawQuad
+        triangleVertices.position(TRIANGLE_VERTICES_DATA_POS_OFFSET);
+        glVertexAttribPointer(attribPosition, 3, GL_FLOAT, false,
+                TRIANGLE_VERTICES_DATA_STRIDE_BYTES, triangleVertices);
+        checkGlError();
+
+        triangleVertices.position(TRIANGLE_VERTICES_DATA_UV_OFFSET);
+        glVertexAttribPointer(attribTexCoords, 3, GL_FLOAT, false,
+                TRIANGLE_VERTICES_DATA_STRIDE_BYTES, triangleVertices);
+        checkGlError();
+
+        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+        checkGlError();
+
+        while (!mFinished) {
+            checkCurrent();
+
+            glClear(GL_COLOR_BUFFER_BIT);
+            checkGlError();
+
+            glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
+            checkGlError();
+
+            if (!mEgl.eglSwapBuffers(mEglDisplay, mEglSurface)) {
+                throw new RuntimeException("Cannot swap buffers");
+            }
+            checkEglError();
+
+            try {
+                Thread.sleep(2000);
+            } catch (InterruptedException e) {
+                // Ignore
+            }
+        }
+
+        finishGL();
+    }
+
+    private int loadTexture(int resource) {
+        int[] textures = new int[1];
+
+        glActiveTexture(GL_TEXTURE0);
+        glGenTextures(1, textures, 0);
+        checkGlError();
+
+        int texture = textures[0];
+        glBindTexture(GL_TEXTURE_2D, texture);
+        checkGlError();
+
+        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+
+        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+
+        Bitmap bitmap = BitmapFactory.decodeResource(mResources, resource);
+
+        GLUtils.texImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bitmap, GL_UNSIGNED_BYTE, 0);
+        checkGlError();
+
+        bitmap.recycle();
+
+        return texture;
+    }
+
+    private static int buildProgram(String vertex, String fragment) {
+        int vertexShader = buildShader(vertex, GL_VERTEX_SHADER);
+        if (vertexShader == 0) return 0;
+
+        int fragmentShader = buildShader(fragment, GL_FRAGMENT_SHADER);
+        if (fragmentShader == 0) return 0;
+
+        int program = glCreateProgram();
+        glAttachShader(program, vertexShader);
+        checkGlError();
+
+        glAttachShader(program, fragmentShader);
+        checkGlError();
+
+        glLinkProgram(program);
+        checkGlError();
+
+        int[] status = new int[1];
+        glGetProgramiv(program, GL_LINK_STATUS, status, 0);
+        if (status[0] != GL_TRUE) {
+            String error = glGetProgramInfoLog(program);
+            Log.d(LOG_TAG, "Error while linking program:\n" + error);
+            glDeleteShader(vertexShader);
+            glDeleteShader(fragmentShader);
+            glDeleteProgram(program);
+            return 0;
+        }
+
+        return program;
+    }
+
+    private static int buildShader(String source, int type) {
+        int shader = glCreateShader(type);
+
+        glShaderSource(shader, source);
+        checkGlError();
+
+        glCompileShader(shader);
+        checkGlError();
+
+        int[] status = new int[1];
+        glGetShaderiv(shader, GL_COMPILE_STATUS, status, 0);
+        if (status[0] != GL_TRUE) {
+            String error = glGetShaderInfoLog(shader);
+            Log.d(LOG_TAG, "Error while compiling shader:\n" + error);
+            glDeleteShader(shader);
+            return 0;
+        }
+
+        return shader;
+    }
+
+    private void checkEglError() {
+        int error = mEgl.eglGetError();
+        if (error != EGL10.EGL_SUCCESS) {
+            Log.w(LOG_TAG, "EGL error = 0x" + Integer.toHexString(error));
+        }
+    }
+
+    private static void checkGlError() {
+        int error = glGetError();
+        if (error != GL_NO_ERROR) {
+            Log.w(LOG_TAG, "GL error = 0x" + Integer.toHexString(error));
+        }
+    }
+
+    private void finishGL() {
+        mEgl.eglDestroyContext(mEglDisplay, mEglContext);
+        mEgl.eglDestroySurface(mEglDisplay, mEglSurface);
+    }
+
+    private void checkCurrent() {
+        if (!mEglContext.equals(mEgl.eglGetCurrentContext()) ||
+                !mEglSurface.equals(mEgl.eglGetCurrentSurface(EGL10.EGL_DRAW))) {
+            if (!mEgl.eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext)) {
+                throw new RuntimeException("eglMakeCurrent failed "
+                        + GLUtils.getEGLErrorString(mEgl.eglGetError()));
+            }
+        }
+    }
+
+    private void initGL() {
+        mEgl = (EGL10) EGLContext.getEGL();
+
+        mEglDisplay = mEgl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
+        if (mEglDisplay == EGL10.EGL_NO_DISPLAY) {
+            throw new RuntimeException("eglGetDisplay failed "
+                    + GLUtils.getEGLErrorString(mEgl.eglGetError()));
+        }
+
+        int[] version = new int[2];
+        if (!mEgl.eglInitialize(mEglDisplay, version)) {
+            throw new RuntimeException("eglInitialize failed " +
+                    GLUtils.getEGLErrorString(mEgl.eglGetError()));
+        }
+
+        mEglConfig = chooseEglConfig();
+        if (mEglConfig == null) {
+            throw new RuntimeException("eglConfig not initialized");
+        }
+
+        mEglContext = createContext(mEgl, mEglDisplay, mEglConfig);
+
+        mEglSurface = mEgl.eglCreateWindowSurface(mEglDisplay, mEglConfig, mSurface, null);
+
+        if (mEglSurface == null || mEglSurface == EGL10.EGL_NO_SURFACE) {
+            int error = mEgl.eglGetError();
+            if (error == EGL10.EGL_BAD_NATIVE_WINDOW) {
+                Log.e(LOG_TAG, "createWindowSurface returned EGL_BAD_NATIVE_WINDOW.");
+                return;
+            }
+            throw new RuntimeException("createWindowSurface failed "
+                    + GLUtils.getEGLErrorString(error));
+        }
+
+        if (!mEgl.eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext)) {
+            throw new RuntimeException("eglMakeCurrent failed "
+                    + GLUtils.getEGLErrorString(mEgl.eglGetError()));
+        }
+    }
+
+
+    EGLContext createContext(EGL10 egl, EGLDisplay eglDisplay, EGLConfig eglConfig) {
+        int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE};
+        return egl.eglCreateContext(eglDisplay, eglConfig, EGL10.EGL_NO_CONTEXT, attrib_list);
+    }
+
+    private EGLConfig chooseEglConfig() {
+        int[] configsCount = new int[1];
+        EGLConfig[] configs = new EGLConfig[1];
+        int[] configSpec = getConfig();
+        if (!mEgl.eglChooseConfig(mEglDisplay, configSpec, configs, 1, configsCount)) {
+            throw new IllegalArgumentException("eglChooseConfig failed " +
+                    GLUtils.getEGLErrorString(mEgl.eglGetError()));
+        } else if (configsCount[0] > 0) {
+            return configs[0];
+        }
+        return null;
+    }
+
+    private static int[] getConfig() {
+        return new int[]{
+                EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
+                EGL10.EGL_RED_SIZE, 8,
+                EGL10.EGL_GREEN_SIZE, 8,
+                EGL10.EGL_BLUE_SIZE, 8,
+                EGL10.EGL_ALPHA_SIZE, 8,
+                EGL10.EGL_DEPTH_SIZE, 0,
+                EGL10.EGL_STENCIL_SIZE, 0,
+                EGL10.EGL_NONE
+        };
+    }
+
+    public void finish() {
+        mFinished = true;
+    }
+}
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 6177784..95f676e 100644
--- a/tests/permission/src/com/android/framework/permission/tests/WindowManagerPermissionTests.java
+++ b/tests/permission/src/com/android/framework/permission/tests/WindowManagerPermissionTests.java
@@ -32,7 +32,7 @@
  */
 public class WindowManagerPermissionTests extends TestCase {
     IWindowManager mWm;
-    
+
     @Override
     protected void setUp() throws Exception {
         super.setUp();
@@ -51,7 +51,7 @@
         } catch (RemoteException e) {
             fail("Unexpected remote exception");
         }
-        
+
         try {
             mWm.resumeKeyDispatching(null);
             fail("IWindowManager.resumeKeyDispatching did not throw SecurityException as"
@@ -61,7 +61,7 @@
         } catch (RemoteException e) {
             fail("Unexpected remote exception");
         }
-        
+
         try {
             mWm.setEventDispatching(true);
             fail("IWindowManager.setEventDispatching did not throw SecurityException as"
@@ -71,7 +71,7 @@
         } catch (RemoteException e) {
             fail("Unexpected remote exception");
         }
-        
+
         try {
             mWm.addWindowToken(null, 0);
             fail("IWindowManager.addWindowToken did not throw SecurityException as"
@@ -81,7 +81,7 @@
         } catch (RemoteException e) {
             fail("Unexpected remote exception");
         }
-        
+
         try {
             mWm.removeWindowToken(null);
             fail("IWindowManager.removeWindowToken did not throw SecurityException as"
@@ -91,9 +91,10 @@
         } catch (RemoteException e) {
             fail("Unexpected remote exception");
         }
-        
+
         try {
-            mWm.addAppToken(0, null, 0, 0, 0, false, false, 0, 0, false, false, null);
+            mWm.addAppToken(0, null, 0, 0, 0, false, false, 0, 0, false, false, null,
+                    Configuration.EMPTY);
             fail("IWindowManager.addAppToken did not throw SecurityException as"
                     + " expected");
         } catch (SecurityException e) {
@@ -101,9 +102,9 @@
         } catch (RemoteException e) {
             fail("Unexpected remote exception");
         }
-        
+
         try {
-            mWm.setAppTask(null, 0, null);
+            mWm.setAppTask(null, 0, null, null);
             fail("IWindowManager.setAppGroupId did not throw SecurityException as"
                     + " expected");
         } catch (SecurityException e) {
@@ -111,7 +112,7 @@
         } catch (RemoteException e) {
             fail("Unexpected remote exception");
         }
-        
+
         try {
             mWm.updateOrientationFromAppTokens(new Configuration(), null);
             fail("IWindowManager.updateOrientationFromAppTokens did not throw SecurityException as"
@@ -121,7 +122,7 @@
         } catch (RemoteException e) {
             fail("Unexpected remote exception");
         }
-        
+
         try {
             mWm.setAppOrientation(null, 0);
             mWm.addWindowToken(null, 0);
@@ -132,7 +133,7 @@
         } catch (RemoteException e) {
             fail("Unexpected remote exception");
         }
-        
+
         try {
             mWm.setFocusedApp(null, false);
             fail("IWindowManager.setFocusedApp did not throw SecurityException as"
@@ -142,7 +143,7 @@
         } catch (RemoteException e) {
             fail("Unexpected remote exception");
         }
-        
+
         try {
             mWm.prepareAppTransition(0, false);
             fail("IWindowManager.prepareAppTransition did not throw SecurityException as"
@@ -152,7 +153,7 @@
         } catch (RemoteException e) {
             fail("Unexpected remote exception");
         }
-        
+
         try {
             mWm.executeAppTransition();
             fail("IWindowManager.executeAppTransition did not throw SecurityException as"
@@ -162,7 +163,7 @@
         } catch (RemoteException e) {
             fail("Unexpected remote exception");
         }
-        
+
         try {
             mWm.setAppStartingWindow(null, "foo", 0, null, null, 0, 0, 0, 0, null, false);
             fail("IWindowManager.setAppStartingWindow did not throw SecurityException as"
@@ -172,7 +173,7 @@
         } catch (RemoteException e) {
             fail("Unexpected remote exception");
         }
-        
+
         try {
             mWm.setAppWillBeHidden(null);
             fail("IWindowManager.setAppWillBeHidden did not throw SecurityException as"
@@ -182,7 +183,7 @@
         } catch (RemoteException e) {
             fail("Unexpected remote exception");
         }
-        
+
         try {
             mWm.setAppVisibility(null, false);
             fail("IWindowManager.setAppVisibility did not throw SecurityException as"
@@ -192,7 +193,7 @@
         } catch (RemoteException e) {
             fail("Unexpected remote exception");
         }
-        
+
         try {
             mWm.startAppFreezingScreen(null, 0);
             fail("IWindowManager.startAppFreezingScreen did not throw SecurityException as"
@@ -202,7 +203,7 @@
         } catch (RemoteException e) {
             fail("Unexpected remote exception");
         }
-        
+
         try {
             mWm.stopAppFreezingScreen(null, false);
             fail("IWindowManager.stopAppFreezingScreen did not throw SecurityException as"
@@ -212,7 +213,7 @@
         } catch (RemoteException e) {
             fail("Unexpected remote exception");
         }
-        
+
         try {
             mWm.removeAppToken(null);
             fail("IWindowManager.removeAppToken did not throw SecurityException as"
@@ -236,7 +237,7 @@
         } catch (RemoteException e) {
             fail("Unexpected remote exception");
         }
-        
+
         try {
             mWm.reenableKeyguard(token);
             fail("IWindowManager.reenableKeyguard did not throw SecurityException as"
@@ -246,7 +247,7 @@
         } catch (RemoteException e) {
             fail("Unexpected remote exception");
         }
-        
+
         try {
             mWm.exitKeyguardSecurely(null);
             fail("IWindowManager.exitKeyguardSecurely did not throw SecurityException as"
@@ -257,7 +258,7 @@
             fail("Unexpected remote exception");
         }
     }
-        
+
     @SmallTest
     public void testSET_ANIMATION_SCALE() {
         try {
@@ -269,7 +270,7 @@
         } catch (RemoteException e) {
             fail("Unexpected remote exception");
         }
-        
+
         try {
             mWm.setAnimationScales(new float[1]);
             fail("IWindowManager.setAnimationScales did not throw SecurityException as"
@@ -280,7 +281,7 @@
             fail("Unexpected remote exception");
         }
     }
-    
+
     @SmallTest
     public void testSET_ORIENTATION() {
         try {
diff --git a/tools/aapt/Android.mk b/tools/aapt/Android.mk
index 769e2a1..b701445 100644
--- a/tools/aapt/Android.mk
+++ b/tools/aapt/Android.mk
@@ -54,7 +54,6 @@
     tests/ResourceFilter_test.cpp \
     tests/ResourceTable_test.cpp
 
-aaptHostLdLibs :=
 aaptHostStaticLibs := \
     libandroidfw \
     libpng \
@@ -68,17 +67,13 @@
 aaptCFlags := -DAAPT_VERSION=\"$(BUILD_NUMBER_FROM_FILE)\"
 aaptCFlags += -Wall -Werror
 
-ifeq ($(HOST_OS),linux)
-    aaptHostLdLibs += -lrt -ldl -lpthread
-endif
+aaptHostLdLibs_linux := -lrt -ldl -lpthread
 
 # Statically link libz for MinGW (Win SDK under Linux),
 # and dynamically link for all others.
-ifneq ($(strip $(USE_MINGW)),)
-    aaptHostStaticLibs += libz
-else
-    aaptHostLdLibs += -lz
-endif
+aaptHostStaticLibs_windows := libz
+aaptHostLdLibs_linux += -lz
+aaptHostLdLibs_darwin := -lz
 
 
 # ==========================================================
@@ -87,13 +82,13 @@
 include $(CLEAR_VARS)
 
 LOCAL_MODULE := libaapt
-LOCAL_CFLAGS += -Wno-format-y2k -DSTATIC_ANDROIDFW_FOR_TOOLS $(aaptCFlags)
-LOCAL_CPPFLAGS += $(aaptCppFlags)
-ifeq (darwin,$(HOST_OS))
-LOCAL_CFLAGS += -D_DARWIN_UNLIMITED_STREAMS
-endif
+LOCAL_MODULE_HOST_OS := darwin linux windows
+LOCAL_CFLAGS := -Wno-format-y2k -DSTATIC_ANDROIDFW_FOR_TOOLS $(aaptCFlags)
+LOCAL_CPPFLAGS := $(aaptCppFlags)
+LOCAL_CFLAGS_darwin := -D_DARWIN_UNLIMITED_STREAMS
 LOCAL_SRC_FILES := $(aaptSources)
-LOCAL_STATIC_LIBRARIES += $(aaptHostStaticLibs)
+LOCAL_STATIC_LIBRARIES := $(aaptHostStaticLibs)
+LOCAL_STATIC_LIBRARIES_windows := $(aaptHostStaticLibs_windows)
 
 include $(BUILD_HOST_STATIC_LIBRARY)
 
@@ -103,11 +98,14 @@
 include $(CLEAR_VARS)
 
 LOCAL_MODULE := aapt
-LOCAL_CFLAGS += $(aaptCFlags)
-LOCAL_CPPFLAGS += $(aaptCppFlags)
-LOCAL_LDLIBS += $(aaptHostLdLibs)
+LOCAL_MODULE_HOST_OS := darwin linux windows
+LOCAL_CFLAGS := $(aaptCFlags)
+LOCAL_CPPFLAGS := $(aaptCppFlags)
+LOCAL_LDLIBS_darwin := $(aaptHostLdLibs_darwin)
+LOCAL_LDLIBS_linux := $(aaptHostLdLibs_linux)
 LOCAL_SRC_FILES := $(aaptMain)
-LOCAL_STATIC_LIBRARIES += libaapt $(aaptHostStaticLibs)
+LOCAL_STATIC_LIBRARIES := libaapt $(aaptHostStaticLibs)
+LOCAL_STATIC_LIBRARIES_windows := $(aaptHostStaticLibs_windows)
 
 include $(BUILD_HOST_EXECUTABLE)
 
@@ -116,15 +114,16 @@
 # Build the host tests: libaapt_tests
 # ==========================================================
 include $(CLEAR_VARS)
-LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
 
 LOCAL_MODULE := libaapt_tests
-LOCAL_CFLAGS += $(aaptCFlags)
-LOCAL_CPPFLAGS += $(aaptCppFlags)
-LOCAL_LDLIBS += $(aaptHostLdLibs)
-LOCAL_SRC_FILES += $(aaptTests)
-LOCAL_C_INCLUDES += $(LOCAL_PATH)
-LOCAL_STATIC_LIBRARIES += libaapt $(aaptHostStaticLibs)
+LOCAL_CFLAGS := $(aaptCFlags)
+LOCAL_CPPFLAGS := $(aaptCppFlags)
+LOCAL_LDLIBS_darwin := $(aaptHostLdLibs_darwin)
+LOCAL_LDLIBS_linux := $(aaptHostLdLibs_linux)
+LOCAL_SRC_FILES := $(aaptTests)
+LOCAL_C_INCLUDES := $(LOCAL_PATH)
+LOCAL_STATIC_LIBRARIES := libaapt $(aaptHostStaticLibs)
+LOCAL_STATIC_LIBRARIES_windows := $(aaptHostStaticLibs_windows)
 
 include $(BUILD_HOST_NATIVE_TEST)
 
diff --git a/tools/aidl/AST.h b/tools/aidl/AST.h
index ead5e7a..d7bfccb 100644
--- a/tools/aidl/AST.h
+++ b/tools/aidl/AST.h
@@ -1,5 +1,5 @@
-#ifndef AIDL_AST_H
-#define AIDL_AST_H
+#ifndef AIDL_AST_H_
+#define AIDL_AST_H_
 
 #include <string>
 #include <vector>
@@ -368,4 +368,4 @@
     virtual void Write(FILE* to);
 };
 
-#endif // AIDL_AST_H
+#endif // AIDL_AST_H_
diff --git a/tools/aidl/Android.mk b/tools/aidl/Android.mk
index efd60a2..8e6189f 100644
--- a/tools/aidl/Android.mk
+++ b/tools/aidl/Android.mk
@@ -6,24 +6,74 @@
 ifeq ($(TARGET_BUILD_APPS)$(filter true,$(TARGET_BUILD_PDK)),)
 
 LOCAL_PATH:= $(call my-dir)
+
+# Logic shared between aidl and its unittests
 include $(CLEAR_VARS)
+LOCAL_MODULE := libaidl-common
+
+LOCAL_CLANG_CFLAGS := -Wall -Werror
+# Tragically, the code is riddled with unused parameters.
+LOCAL_CLANG_CFLAGS += -Wno-unused-parameter
+# yacc dumps a lot of code *just in case*.
+LOCAL_CLANG_CFLAGS += -Wno-unused-function
+LOCAL_CLANG_CFLAGS += -Wno-unneeded-internal-declaration
+# yacc is a tool from a more civilized age.
+LOCAL_CLANG_CFLAGS += -Wno-deprecated-register
+# yacc also has a habit of using char* over const char*.
+LOCAL_CLANG_CFLAGS += -Wno-writable-strings
 
 LOCAL_SRC_FILES := \
-	aidl_language_l.l \
-	aidl_language_y.y \
-	aidl.cpp \
-	aidl_language.cpp \
-	options.cpp \
-	search_path.cpp \
-	AST.cpp \
-	Type.cpp \
-	generate_java.cpp \
-	generate_java_binder.cpp \
-	generate_java_rpc.cpp
+    AST.cpp \
+    Type.cpp \
+    aidl.cpp \
+    aidl_language.cpp \
+    aidl_language_l.l \
+    aidl_language_y.y \
+    generate_java.cpp \
+    generate_java_binder.cpp \
+    options.cpp \
+    search_path.cpp \
 
-LOCAL_CFLAGS := -g
+include $(BUILD_HOST_STATIC_LIBRARY)
+
+
+# aidl executable
+include $(CLEAR_VARS)
 LOCAL_MODULE := aidl
 
+LOCAL_MODULE_HOST_OS := darwin linux windows
+LOCAL_CFLAGS := -Wall -Werror
+LOCAL_SRC_FILES := main.cpp
+LOCAL_STATIC_LIBRARIES := libaidl-common
 include $(BUILD_HOST_EXECUTABLE)
 
+
+# TODO(wiley) Compile these for mac as well after b/22771504
+ifeq ($(HOST_OS),linux)
+# Unit tests
+include $(CLEAR_VARS)
+LOCAL_MODULE := aidl_unittests
+
+LOCAL_CFLAGS := -g -DUNIT_TEST -Wall -Werror
+# Tragically, the code is riddled with unused parameters.
+LOCAL_CLANG_CFLAGS := -Wno-unused-parameter
+LOCAL_SRC_FILES := \
+    options_unittest.cpp \
+    test_main.cpp \
+    tests/end_to_end_tests.cpp \
+    tests/example_interface_test_data.cpp \
+
+LOCAL_SHARED_LIBRARIES := \
+    libchrome-host \
+
+LOCAL_STATIC_LIBRARIES := \
+    libaidl-common \
+    libgmock_host \
+    libgtest_host \
+
+LOCAL_LDLIBS_linux := -lrt
+
+include $(BUILD_HOST_NATIVE_TEST)
+endif # HOST_OS == linux
+
 endif # No TARGET_BUILD_APPS or TARGET_BUILD_PDK
diff --git a/tools/aidl/Type.cpp b/tools/aidl/Type.cpp
index 2267750..d9b8b88 100644
--- a/tools/aidl/Type.cpp
+++ b/tools/aidl/Type.cpp
@@ -28,9 +28,6 @@
 Type* MAP_TYPE;
 Type* LIST_TYPE;
 Type* CLASSLOADER_TYPE;
-Type* RPC_DATA_TYPE;
-Type* RPC_ERROR_TYPE;
-Type* EVENT_FAKE_TYPE;
 
 Expression* NULL_VALUE;
 Expression* THIS_VALUE;
@@ -42,7 +39,6 @@
 register_base_types()
 {
     VOID_TYPE = new BasicType("void",
-            "XXX", "XXX", "XXX", "XXX", "XXX",
             "XXX", "XXX", "XXX", "XXX", "XXX");
     NAMES.Add(VOID_TYPE);
 
@@ -50,37 +46,37 @@
     NAMES.Add(BOOLEAN_TYPE);
 
     BYTE_TYPE = new BasicType("byte",
-            "writeByte", "readByte", "writeByteArray", "createByteArray", "readByteArray",
-            "putByte", "getByte", "putByteArray", "createByteArray", "getByteArray");
+            "writeByte", "readByte", "writeByteArray", "createByteArray",
+            "readByteArray");
     NAMES.Add(BYTE_TYPE);
 
     CHAR_TYPE = new CharType();
     NAMES.Add(CHAR_TYPE);
 
     INT_TYPE = new BasicType("int",
-            "writeInt", "readInt", "writeIntArray", "createIntArray", "readIntArray",
-            "putInteger", "getInteger", "putIntegerArray", "createIntegerArray", "getIntegerArray");
+            "writeInt", "readInt", "writeIntArray", "createIntArray",
+            "readIntArray");
     NAMES.Add(INT_TYPE);
 
     LONG_TYPE = new BasicType("long",
-            "writeLong", "readLong", "writeLongArray", "createLongArray", "readLongArray",
-            "putLong", "getLong", "putLongArray", "createLongArray", "getLongArray");
+            "writeLong", "readLong", "writeLongArray", "createLongArray",
+            "readLongArray");
     NAMES.Add(LONG_TYPE);
 
     FLOAT_TYPE = new BasicType("float",
-            "writeFloat", "readFloat", "writeFloatArray", "createFloatArray", "readFloatArray",
-            "putFloat", "getFloat", "putFloatArray", "createFloatArray", "getFloatArray");
+            "writeFloat", "readFloat", "writeFloatArray", "createFloatArray",
+            "readFloatArray");
     NAMES.Add(FLOAT_TYPE);
 
     DOUBLE_TYPE = new BasicType("double",
-            "writeDouble", "readDouble", "writeDoubleArray", "createDoubleArray", "readDoubleArray",
-            "putDouble", "getDouble", "putDoubleArray", "createDoubleArray", "getDoubleArray");
+            "writeDouble", "readDouble", "writeDoubleArray",
+            "createDoubleArray", "readDoubleArray");
     NAMES.Add(DOUBLE_TYPE);
 
     STRING_TYPE = new StringType();
     NAMES.Add(STRING_TYPE);
 
-    OBJECT_TYPE = new Type("java.lang", "Object", Type::BUILT_IN, false, false, false);
+    OBJECT_TYPE = new Type("java.lang", "Object", Type::BUILT_IN, false, false);
     NAMES.Add(OBJECT_TYPE);
 
     CHAR_SEQUENCE_TYPE = new CharSequenceType();
@@ -92,7 +88,7 @@
     LIST_TYPE = new ListType();
     NAMES.Add(LIST_TYPE);
 
-    TEXT_UTILS_TYPE = new Type("android.text", "TextUtils", Type::BUILT_IN, false, false, false);
+    TEXT_UTILS_TYPE = new Type("android.text", "TextUtils", Type::BUILT_IN, false, false);
     NAMES.Add(TEXT_UTILS_TYPE);
 
     REMOTE_EXCEPTION_TYPE = new RemoteExceptionType();
@@ -119,19 +115,9 @@
     PARCELABLE_INTERFACE_TYPE = new ParcelableInterfaceType();
     NAMES.Add(PARCELABLE_INTERFACE_TYPE);
 
-    CONTEXT_TYPE = new Type("android.content", "Context", Type::BUILT_IN, false, false, false);
+    CONTEXT_TYPE = new Type("android.content", "Context", Type::BUILT_IN, false, false);
     NAMES.Add(CONTEXT_TYPE);
 
-    RPC_DATA_TYPE = new RpcDataType();
-    NAMES.Add(RPC_DATA_TYPE);
-
-    RPC_ERROR_TYPE = new UserDataType("android.support.place.rpc", "RpcError",
-                                    true, __FILE__, __LINE__);
-    NAMES.Add(RPC_ERROR_TYPE);
-
-    EVENT_FAKE_TYPE = new Type("event", Type::BUILT_IN, false, false, false);
-    NAMES.Add(EVENT_FAKE_TYPE);
-
     CLASSLOADER_TYPE = new ClassLoaderType();
     NAMES.Add(CLASSLOADER_TYPE);
 
@@ -158,30 +144,27 @@
 
 // ================================================================
 
-Type::Type(const string& name, int kind, bool canWriteToParcel, bool canWriteToRpcData,
-        bool canBeOut)
+Type::Type(const string& name, int kind, bool canWriteToParcel, bool canBeOut)
     :m_package(),
      m_name(name),
      m_declFile(""),
      m_declLine(-1),
      m_kind(kind),
      m_canWriteToParcel(canWriteToParcel),
-     m_canWriteToRpcData(canWriteToRpcData),
      m_canBeOut(canBeOut)
 {
     m_qualifiedName = name;
 }
 
 Type::Type(const string& package, const string& name,
-            int kind, bool canWriteToParcel, bool canWriteToRpcData,
-            bool canBeOut, const string& declFile, int declLine)
+            int kind, bool canWriteToParcel, bool canBeOut,
+            const string& declFile, int declLine)
     :m_package(package),
      m_name(name),
      m_declFile(declFile),
      m_declLine(declLine),
      m_kind(kind),
      m_canWriteToParcel(canWriteToParcel),
-     m_canWriteToRpcData(canWriteToRpcData),
      m_canBeOut(canBeOut)
 {
     if (package.length() > 0) {
@@ -214,12 +197,6 @@
 }
 
 string
-Type::RpcCreatorName() const
-{
-    return "";
-}
-
-string
 Type::InstantiableName() const
 {
     return QualifiedName();
@@ -282,26 +259,6 @@
 }
 
 void
-Type::WriteToRpcData(StatementBlock* addTo, Expression* k, Variable* v,
-        Variable* data, int flags)
-{
-    fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n",
-            __FILE__, __LINE__, m_qualifiedName.c_str());
-    addTo->Add(new LiteralExpression("/* WriteToRpcData error "
-                + m_qualifiedName + " */"));
-}
-
-void
-Type::CreateFromRpcData(StatementBlock* addTo, Expression* k, Variable* v, Variable* data,
-        Variable** cl)
-{
-    fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n",
-            __FILE__, __LINE__, m_qualifiedName.c_str());
-    addTo->Add(new LiteralExpression("/* ReadFromRpcData error "
-                + m_qualifiedName + " */"));
-}
-
-void
 Type::SetQualifiedName(const string& qualified)
 {
     m_qualifiedName = qualified;
@@ -324,20 +281,13 @@
 
 BasicType::BasicType(const string& name, const string& marshallParcel,
           const string& unmarshallParcel, const string& writeArrayParcel,
-          const string& createArrayParcel, const string& readArrayParcel,
-          const string& marshallRpc, const string& unmarshallRpc,
-          const string& writeArrayRpc, const string& createArrayRpc, const string& readArrayRpc)
-    :Type(name, BUILT_IN, true, true, false),
+          const string& createArrayParcel, const string& readArrayParcel)
+    :Type(name, BUILT_IN, true, false),
      m_marshallParcel(marshallParcel),
      m_unmarshallParcel(unmarshallParcel),
      m_writeArrayParcel(writeArrayParcel),
      m_createArrayParcel(createArrayParcel),
-     m_readArrayParcel(readArrayParcel),
-     m_marshallRpc(marshallRpc),
-     m_unmarshallRpc(unmarshallRpc),
-     m_writeArrayRpc(writeArrayRpc),
-     m_createArrayRpc(createArrayRpc),
-     m_readArrayRpc(readArrayRpc)
+     m_readArrayParcel(readArrayParcel)
 {
 }
 
@@ -378,24 +328,10 @@
     addTo->Add(new MethodCall(parcel, m_readArrayParcel, 1, v));
 }
 
-void
-BasicType::WriteToRpcData(StatementBlock* addTo, Expression* k, Variable* v,
-        Variable* data, int flags)
-{
-    addTo->Add(new MethodCall(data, m_marshallRpc, 2, k, v));
-}
-
-void
-BasicType::CreateFromRpcData(StatementBlock* addTo, Expression* k, Variable* v, Variable* data,
-        Variable** cl)
-{
-    addTo->Add(new Assignment(v, new MethodCall(data, m_unmarshallRpc, 1, k)));
-}
-
 // ================================================================
 
 BooleanType::BooleanType()
-    :Type("boolean", BUILT_IN, true, true, false)
+    :Type("boolean", BUILT_IN, true, false)
 {
 }
 
@@ -439,24 +375,10 @@
     addTo->Add(new MethodCall(parcel, "readBooleanArray", 1, v));
 }
 
-void
-BooleanType::WriteToRpcData(StatementBlock* addTo, Expression* k, Variable* v,
-        Variable* data, int flags)
-{
-    addTo->Add(new MethodCall(data, "putBoolean", 2, k, v));
-}
-
-void
-BooleanType::CreateFromRpcData(StatementBlock* addTo, Expression* k, Variable* v, Variable* data,
-        Variable** cl)
-{
-    addTo->Add(new Assignment(v, new MethodCall(data, "getBoolean", 1, k)));
-}
-
 // ================================================================
 
 CharType::CharType()
-    :Type("char", BUILT_IN, true, true, false)
+    :Type("char", BUILT_IN, true, false)
 {
 }
 
@@ -498,24 +420,10 @@
     addTo->Add(new MethodCall(parcel, "readCharArray", 1, v));
 }
 
-void
-CharType::WriteToRpcData(StatementBlock* addTo, Expression* k, Variable* v,
-        Variable* data, int flags)
-{
-    addTo->Add(new MethodCall(data, "putChar", 2, k, v));
-}
-
-void
-CharType::CreateFromRpcData(StatementBlock* addTo, Expression* k, Variable* v, Variable* data,
-        Variable** cl)
-{
-    addTo->Add(new Assignment(v, new MethodCall(data, "getChar", 1, k)));
-}
-
 // ================================================================
 
 StringType::StringType()
-    :Type("java.lang", "String", BUILT_IN, true, true, false)
+    :Type("java.lang", "String", BUILT_IN, true, false)
 {
 }
 
@@ -562,24 +470,10 @@
     addTo->Add(new MethodCall(parcel, "readStringArray", 1, v));
 }
 
-void
-StringType::WriteToRpcData(StatementBlock* addTo, Expression* k, Variable* v,
-        Variable* data, int flags)
-{
-    addTo->Add(new MethodCall(data, "putString", 2, k, v));
-}
-
-void
-StringType::CreateFromRpcData(StatementBlock* addTo, Expression* k, Variable* v,
-        Variable* data, Variable**)
-{
-    addTo->Add(new Assignment(v, new MethodCall(data, "getString", 1, k)));
-}
-
 // ================================================================
 
 CharSequenceType::CharSequenceType()
-    :Type("java.lang", "CharSequence", BUILT_IN, true, true, false)
+    :Type("java.lang", "CharSequence", BUILT_IN, true, false)
 {
 }
 
@@ -639,7 +533,7 @@
 // ================================================================
 
 RemoteExceptionType::RemoteExceptionType()
-    :Type("android.os", "RemoteException", BUILT_IN, false, false, false)
+    :Type("android.os", "RemoteException", BUILT_IN, false, false)
 {
 }
 
@@ -658,7 +552,7 @@
 // ================================================================
 
 RuntimeExceptionType::RuntimeExceptionType()
-    :Type("java.lang", "RuntimeException", BUILT_IN, false, false, false)
+    :Type("java.lang", "RuntimeException", BUILT_IN, false, false)
 {
 }
 
@@ -678,7 +572,7 @@
 // ================================================================
 
 IBinderType::IBinderType()
-    :Type("android.os", "IBinder", BUILT_IN, true, false, false)
+    :Type("android.os", "IBinder", BUILT_IN, true, false)
 {
 }
 
@@ -717,7 +611,7 @@
 // ================================================================
 
 IInterfaceType::IInterfaceType()
-    :Type("android.os", "IInterface", BUILT_IN, false, false, false)
+    :Type("android.os", "IInterface", BUILT_IN, false, false)
 {
 }
 
@@ -737,7 +631,7 @@
 // ================================================================
 
 BinderType::BinderType()
-    :Type("android.os", "Binder", BUILT_IN, false, false, false)
+    :Type("android.os", "Binder", BUILT_IN, false, false)
 {
 }
 
@@ -758,7 +652,7 @@
 // ================================================================
 
 BinderProxyType::BinderProxyType()
-    :Type("android.os", "BinderProxy", BUILT_IN, false, false, false)
+    :Type("android.os", "BinderProxy", BUILT_IN, false, false)
 {
 }
 
@@ -779,7 +673,7 @@
 // ================================================================
 
 ParcelType::ParcelType()
-    :Type("android.os", "Parcel", BUILT_IN, false, false, false)
+    :Type("android.os", "Parcel", BUILT_IN, false, false)
 {
 }
 
@@ -798,7 +692,7 @@
 // ================================================================
 
 ParcelableInterfaceType::ParcelableInterfaceType()
-    :Type("android.os", "Parcelable", BUILT_IN, false, false, false)
+    :Type("android.os", "Parcelable", BUILT_IN, false, false)
 {
 }
 
@@ -817,7 +711,7 @@
 // ================================================================
 
 MapType::MapType()
-    :Type("java.util", "Map", BUILT_IN, true, false, true)
+    :Type("java.util", "Map", BUILT_IN, true, true)
 {
 }
 
@@ -857,7 +751,7 @@
 // ================================================================
 
 ListType::ListType()
-    :Type("java.util", "List", BUILT_IN, true, true, true)
+    :Type("java.util", "List", BUILT_IN, true, true)
 {
 }
 
@@ -888,26 +782,12 @@
     addTo->Add(new MethodCall(parcel, "readList", 2, v, *cl));
 }
 
-void
-ListType::WriteToRpcData(StatementBlock* addTo, Expression* k, Variable* v,
-        Variable* data, int flags)
-{
-    addTo->Add(new MethodCall(data, "putList", 2, k, v));
-}
-
-void
-ListType::CreateFromRpcData(StatementBlock* addTo, Expression* k, Variable* v, Variable* data,
-        Variable** cl)
-{
-    addTo->Add(new Assignment(v, new MethodCall(data, "getList", 1, k)));
-}
-
 // ================================================================
 
 UserDataType::UserDataType(const string& package, const string& name,
-                        bool builtIn, bool canWriteToParcel, bool canWriteToRpcData,
+                        bool builtIn, bool canWriteToParcel,
                         const string& declFile, int declLine)
-    :Type(package, name, builtIn ? BUILT_IN : USERDATA, canWriteToParcel, canWriteToRpcData,
+    :Type(package, name, builtIn ? BUILT_IN : USERDATA, canWriteToParcel,
             true, declFile, declLine)
 {
 }
@@ -918,12 +798,6 @@
     return QualifiedName() + ".CREATOR";
 }
 
-string
-UserDataType::RpcCreatorName() const
-{
-    return QualifiedName() + ".RPC_CREATOR";
-}
-
 void
 UserDataType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
 {
@@ -1014,29 +888,12 @@
                     v, new LiteralExpression(creator)));
 }
 
-void
-UserDataType::WriteToRpcData(StatementBlock* addTo, Expression* k, Variable* v,
-                                    Variable* data, int flags)
-{
-    // data.putFlattenable(k, v);
-    addTo->Add(new MethodCall(data, "putFlattenable", 2, k, v));
-}
-
-void
-UserDataType::CreateFromRpcData(StatementBlock* addTo, Expression* k, Variable* v,
-                                    Variable* data, Variable** cl)
-{
-    // data.getFlattenable(k, CLASS.RPC_CREATOR);
-    addTo->Add(new Assignment(v, new MethodCall(data, "getFlattenable", 2, k,
-                new FieldVariable(v->type, "RPC_CREATOR"))));
-}
-
 // ================================================================
 
 InterfaceType::InterfaceType(const string& package, const string& name,
                         bool builtIn, bool oneway,
                         const string& declFile, int declLine)
-    :Type(package, name, builtIn ? BUILT_IN : INTERFACE, true, false, false,
+    :Type(package, name, builtIn ? BUILT_IN : INTERFACE, true, false,
                         declFile, declLine)
     ,m_oneway(oneway)
 {
@@ -1075,7 +932,7 @@
 
 GenericType::GenericType(const string& package, const string& name,
                          const vector<Type*>& args)
-    :Type(package, name, BUILT_IN, true, true, true)
+    :Type(package, name, BUILT_IN, true, true)
 {
     m_args = args;
 
@@ -1200,65 +1057,11 @@
     }
 }
 
-void
-GenericListType::WriteToRpcData(StatementBlock* addTo, Expression* k, Variable* v,
-        Variable* data, int flags)
-{
-    Type* generic = GenericArgumentTypes()[0];
-    if (generic == RPC_DATA_TYPE) {
-        addTo->Add(new MethodCall(data, "putRpcDataList", 2, k, v));
-    } else if (generic->RpcCreatorName() != "") {
-        addTo->Add(new MethodCall(data, "putFlattenableList", 2, k, v));
-    } else {
-        addTo->Add(new MethodCall(data, "putList", 2, k, v));
-    }
-}
-
-void
-GenericListType::CreateFromRpcData(StatementBlock* addTo, Expression* k, Variable* v,
-        Variable* data, Variable** cl)
-{
-    Type* generic = GenericArgumentTypes()[0];
-    if (generic == RPC_DATA_TYPE) {
-        addTo->Add(new Assignment(v, new MethodCall(data, "getRpcDataList", 2, k)));
-    } else if (generic->RpcCreatorName() != "") {
-        addTo->Add(new Assignment(v, new MethodCall(data, "getFlattenableList", 2, k, 
-                        new LiteralExpression(generic->RpcCreatorName()))));
-    } else {
-        string classArg = GenericArgumentTypes()[0]->QualifiedName();
-        classArg += ".class";
-        addTo->Add(new Assignment(v, new MethodCall(data, "getList", 2, k,
-                        new LiteralExpression(classArg))));
-    }
-}
-
-
-// ================================================================
-
-RpcDataType::RpcDataType()
-    :UserDataType("android.support.place.rpc", "RpcData", true, true, true)
-{
-}
-
-void
-RpcDataType::WriteToRpcData(StatementBlock* addTo, Expression* k, Variable* v,
-        Variable* data, int flags)
-{
-    addTo->Add(new MethodCall(data, "putRpcData", 2, k, v));
-}
-
-void
-RpcDataType::CreateFromRpcData(StatementBlock* addTo, Expression* k, Variable* v, Variable* data,
-        Variable** cl)
-{
-    addTo->Add(new Assignment(v, new MethodCall(data, "getRpcData", 1, k)));
-}
-
 
 // ================================================================
 
 ClassLoaderType::ClassLoaderType()
-    :Type("java.lang", "ClassLoader", BUILT_IN, false, false, false)
+    :Type("java.lang", "ClassLoader", BUILT_IN, false, false)
 {
 }
 
diff --git a/tools/aidl/Type.h b/tools/aidl/Type.h
index ae12720..6ede79a 100644
--- a/tools/aidl/Type.h
+++ b/tools/aidl/Type.h
@@ -1,5 +1,5 @@
-#ifndef AIDL_TYPE_H
-#define AIDL_TYPE_H
+#ifndef AIDL_TYPE_H_
+#define AIDL_TYPE_H_
 
 #include "AST.h"
 #include <string>
@@ -24,9 +24,9 @@
     };
 
                     Type(const string& name, int kind, bool canWriteToParcel,
-                            bool canWriteToRpcData, bool canBeOut);
+                         bool canBeOut);
                     Type(const string& package, const string& name,
-                            int kind, bool canWriteToParcel, bool canWriteToRpcData, bool canBeOut,
+                            int kind, bool canWriteToParcel, bool canBeOut,
                             const string& declFile = "", int declLine = -1);
     virtual         ~Type();
 
@@ -37,12 +37,10 @@
     inline string   DeclFile() const            { return m_declFile; }
     inline int      DeclLine() const            { return m_declLine; }
     inline bool     CanWriteToParcel() const    { return m_canWriteToParcel; }
-    inline bool     CanWriteToRpcData() const   { return m_canWriteToRpcData; }
     inline bool     CanBeOutParameter() const   { return m_canBeOut; }
     
     virtual string  ImportType() const;
     virtual string  CreatorName() const;
-    virtual string  RpcCreatorName() const;
     virtual string  InstantiableName() const;
 
     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
@@ -61,11 +59,6 @@
     virtual void    ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
                                     Variable* parcel, Variable** cl);
 
-    virtual void    WriteToRpcData(StatementBlock* addTo, Expression* k, Variable* v,
-                                    Variable* data, int flags);
-    virtual void    CreateFromRpcData(StatementBlock* addTo, Expression* k, Variable* v,
-                                    Variable* data, Variable** cl);
-
 protected:
     void SetQualifiedName(const string& qualified);
     Expression* BuildWriteToParcelFlags(int flags);
@@ -81,7 +74,6 @@
     int m_declLine;
     int m_kind;
     bool m_canWriteToParcel;
-    bool m_canWriteToRpcData;
     bool m_canBeOut;
 };
 
@@ -93,12 +85,7 @@
                               const string& unmarshallParcel,
                               const string& writeArrayParcel,
                               const string& createArrayParcel,
-                              const string& readArrayParcel,
-                              const string& marshallRpc,
-                              const string& unmarshallRpc,
-                              const string& writeArrayRpc,
-                              const string& createArrayRpc,
-                              const string& readArrayRpc);
+                              const string& readArrayParcel);
 
     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
                                     Variable* parcel, int flags);
@@ -114,22 +101,12 @@
     virtual void    ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
                                     Variable* parcel, Variable** cl);
 
-    virtual void    WriteToRpcData(StatementBlock* addTo, Expression* k, Variable* v,
-                                    Variable* data, int flags);
-    virtual void    CreateFromRpcData(StatementBlock* addTo, Expression* k, Variable* v,
-                                    Variable* data, Variable** cl);
-
 private:
     string m_marshallParcel;
     string m_unmarshallParcel;
     string m_writeArrayParcel;
     string m_createArrayParcel;
     string m_readArrayParcel;
-    string m_marshallRpc;
-    string m_unmarshallRpc;
-    string m_writeArrayRpc;
-    string m_createArrayRpc;
-    string m_readArrayRpc;
 };
 
 class BooleanType : public Type
@@ -150,11 +127,6 @@
                                     Variable* parcel, Variable** cl);
     virtual void    ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
                                     Variable* parcel, Variable** cl);
-
-    virtual void    WriteToRpcData(StatementBlock* addTo, Expression* k, Variable* v,
-                                    Variable* data, int flags);
-    virtual void    CreateFromRpcData(StatementBlock* addTo, Expression* k, Variable* v,
-                                    Variable* data, Variable** cl);
 };
 
 class CharType : public Type
@@ -175,11 +147,6 @@
                                     Variable* parcel, Variable** cl);
     virtual void    ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
                                     Variable* parcel, Variable** cl);
-
-    virtual void    WriteToRpcData(StatementBlock* addTo, Expression* k, Variable* v,
-                                    Variable* data, int flags);
-    virtual void    CreateFromRpcData(StatementBlock* addTo, Expression* k, Variable* v,
-                                    Variable* data, Variable** cl);
 };
 
 
@@ -203,11 +170,6 @@
                                     Variable* parcel, Variable** cl);
     virtual void    ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
                                     Variable* parcel, Variable** cl);
-
-    virtual void    WriteToRpcData(StatementBlock* addTo, Expression* k, Variable* v,
-                                    Variable* data, int flags);
-    virtual void    CreateFromRpcData(StatementBlock* addTo, Expression* k, Variable* v,
-                                    Variable* data, Variable** cl);
 };
 
 class CharSequenceType : public Type
@@ -344,22 +306,16 @@
                                     Variable* parcel, Variable** cl);
     virtual void    ReadFromParcel(StatementBlock* addTo, Variable* v,
                                     Variable* parcel, Variable** cl);
-
-    virtual void    WriteToRpcData(StatementBlock* addTo, Expression* k, Variable* v,
-                                    Variable* data, int flags);
-    virtual void    CreateFromRpcData(StatementBlock* addTo, Expression* k, Variable* v,
-                                    Variable* data, Variable** cl);
 };
 
 class UserDataType : public Type
 {
 public:
                     UserDataType(const string& package, const string& name,
-                            bool builtIn, bool canWriteToParcel, bool canWriteToRpcData,
+                            bool builtIn, bool canWriteToParcel,
                             const string& declFile = "", int declLine = -1);
 
     virtual string  CreatorName() const;
-    virtual string  RpcCreatorName() const;
 
     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
                                     Variable* parcel, int flags);
@@ -376,11 +332,6 @@
                                     Variable* parcel, Variable** cl);
     virtual void    ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
                                     Variable* parcel, Variable** cl);
-
-    virtual void    WriteToRpcData(StatementBlock* addTo, Expression* k, Variable* v,
-                                    Variable* data, int flags);
-    virtual void    CreateFromRpcData(StatementBlock* addTo, Expression* k, Variable* v,
-                                    Variable* data, Variable** cl);
 };
 
 class InterfaceType : public Type
@@ -426,17 +377,6 @@
     vector<Type*> m_args;
 };
 
-class RpcDataType : public UserDataType
-{
-public:
-                    RpcDataType();
-
-    virtual void    WriteToRpcData(StatementBlock* addTo, Expression* k, Variable* v,
-                                    Variable* data, int flags);
-    virtual void    CreateFromRpcData(StatementBlock* addTo, Expression* k, Variable* v,
-                                    Variable* data, Variable** cl);
-};
-
 class ClassLoaderType : public Type
 {
 public:
@@ -459,11 +399,6 @@
     virtual void    ReadFromParcel(StatementBlock* addTo, Variable* v,
                                     Variable* parcel, Variable** cl);
 
-    virtual void    WriteToRpcData(StatementBlock* addTo, Expression* k, Variable* v,
-                                    Variable* data, int flags);
-    virtual void    CreateFromRpcData(StatementBlock* addTo, Expression* k, Variable* v,
-                                    Variable* data, Variable** cl);
-    
 private:
     string m_creator;
 };
@@ -526,11 +461,6 @@
 
 extern Type* CONTEXT_TYPE;
 
-extern Type* RPC_DATA_TYPE;
-extern Type* RPC_ERROR_TYPE;
-extern Type* RPC_CONTEXT_TYPE;
-extern Type* EVENT_FAKE_TYPE;
-
 extern Expression* NULL_VALUE;
 extern Expression* THIS_VALUE;
 extern Expression* SUPER_VALUE;
@@ -539,4 +469,4 @@
 
 void register_base_types();
 
-#endif // AIDL_TYPE_H
+#endif // AIDL_TYPE_H_
diff --git a/tools/aidl/aidl.cpp b/tools/aidl/aidl.cpp
index 438007f..832c51c 100644
--- a/tools/aidl/aidl.cpp
+++ b/tools/aidl/aidl.cpp
@@ -60,12 +60,9 @@
         }
         else if (d->item_type == USER_DATA_TYPE) {
             user_data_type* b = (user_data_type*)d;
-            if ((b->flattening_methods & PARCELABLE_DATA) != 0) {
+            if (b->parcelable) {
                 printf("parcelable %s %s;\n", b->package, b->name.data);
             }
-            if ((b->flattening_methods & RPC_DATA) != 0) {
-                printf("flattenable %s %s;\n", b->package, b->name.data);
-            }
         }
         else {
             printf("UNKNOWN d=0x%08lx d->item_type=%d\n", (long)d, d->item_type);
@@ -162,11 +159,6 @@
 {
 }
 
-static ParserCallbacks g_importCallbacks = {
-    &main_document_parsed,
-    &import_import_parsed
-};
-
 // ==========================================================
 static int
 check_filename(const char* filename, const char* package, buffer_type* name)
@@ -256,8 +248,7 @@
             user_data_type* p = (user_data_type*)items;
             err |= check_filename(filename, p->package, &p->name);
         }
-        else if (items->item_type == INTERFACE_TYPE_BINDER
-                || items->item_type == INTERFACE_TYPE_RPC) {
+        else if (items->item_type == INTERFACE_TYPE_BINDER) {
             interface_type* c = (interface_type*)items;
             err |= check_filename(filename, c->package, &c->name);
         }
@@ -308,11 +299,9 @@
         if (items->item_type == USER_DATA_TYPE) {
             user_data_type* p = (user_data_type*)items;
             type = new UserDataType(p->package ? p->package : "", p->name.data,
-                    false, ((p->flattening_methods & PARCELABLE_DATA) != 0),
-                    ((p->flattening_methods & RPC_DATA) != 0), filename, p->name.lineno);
+                    false, p->parcelable, filename, p->name.lineno);
         }
-        else if (items->item_type == INTERFACE_TYPE_BINDER
-                || items->item_type == INTERFACE_TYPE_RPC) {
+        else if (items->item_type == INTERFACE_TYPE_BINDER) {
             interface_type* c = (interface_type*)items;
             type = new InterfaceType(c->package ? c->package : "",
                             c->name.data, false, c->oneway,
@@ -336,30 +325,17 @@
                 string name = c->name.data;
                 name += ".Stub";
                 Type* stub = new Type(c->package ? c->package : "",
-                                        name, Type::GENERATED, false, false, false,
+                                        name, Type::GENERATED, false, false,
                                         filename, c->name.lineno);
                 NAMES.Add(stub);
 
                 name = c->name.data;
                 name += ".Stub.Proxy";
                 Type* proxy = new Type(c->package ? c->package : "",
-                                        name, Type::GENERATED, false, false, false,
+                                        name, Type::GENERATED, false, false,
                                         filename, c->name.lineno);
                 NAMES.Add(proxy);
             }
-            else if (items->item_type == INTERFACE_TYPE_RPC) {
-                // for interfaces, also add the service base type, we don't
-                // bother checking these for duplicates, because the parser
-                // won't let us do it.
-                interface_type* c = (interface_type*)items;
-
-                string name = c->name.data;
-                name += ".ServiceBase";
-                Type* base = new Type(c->package ? c->package : "",
-                                        name, Type::GENERATED, false, false, false,
-                                        filename, c->name.lineno);
-                NAMES.Add(base);
-            }
         } else {
             if (old->Kind() == Type::BUILT_IN) {
                 fprintf(stderr, "%s:%d attempt to redefine built in class %s\n",
@@ -411,7 +387,7 @@
 }
 
 static int
-check_method(const char* filename, int kind, method_type* m)
+check_method(const char* filename, method_type* m)
 {
     int err = 0;
 
@@ -424,19 +400,10 @@
         return err;
     }
 
-    if (returnType == EVENT_FAKE_TYPE) {
-        if (kind != INTERFACE_TYPE_RPC) {
-            fprintf(stderr, "%s:%d event methods only supported for rpc interfaces\n",
-                    filename, m->type.type.lineno);
-            err = 1;
-        }
-    } else {
-        if (!(kind == INTERFACE_TYPE_BINDER ? returnType->CanWriteToParcel()
-                    : returnType->CanWriteToRpcData())) {
-            fprintf(stderr, "%s:%d return type %s can't be marshalled.\n", filename,
+    if (!returnType->CanWriteToParcel()) {
+        fprintf(stderr, "%s:%d return type %s can't be marshalled.\n", filename,
                         m->type.type.lineno, m->type.type.data);
-            err = 1;
-        }
+        err = 1;
     }
 
     if (m->type.dimension > 0 && !returnType->CanBeArray()) {
@@ -469,30 +436,13 @@
             goto next;
         }
 
-        if (t == EVENT_FAKE_TYPE) {
-            fprintf(stderr, "%s:%d parameter %s (%d) event can not be used as a parameter %s\n",
-                    filename, m->type.type.lineno, arg->name.data, index,
-                    arg->type.type.data);
-            err = 1;
-            goto next;
-        }
-        
-        if (!(kind == INTERFACE_TYPE_BINDER ? t->CanWriteToParcel() : t->CanWriteToRpcData())) {
+        if (!t->CanWriteToParcel()) {
             fprintf(stderr, "%s:%d parameter %d: '%s %s' can't be marshalled.\n",
                         filename, m->type.type.lineno, index,
                         arg->type.type.data, arg->name.data);
             err = 1;
         }
 
-        if (returnType == EVENT_FAKE_TYPE
-                && convert_direction(arg->direction.data) != IN_PARAMETER) {
-            fprintf(stderr, "%s:%d parameter %d: '%s %s' All paremeters on events must be 'in'.\n",
-                    filename, m->type.type.lineno, index,
-                    arg->type.type.data, arg->name.data);
-            err = 1;
-            goto next;
-        }
-
         if (arg->direction.data == NULL
                 && (arg->type.dimension != 0 || t->CanBeOutParameter())) {
             fprintf(stderr, "%s:%d parameter %d: '%s %s' can be an out"
@@ -554,8 +504,7 @@
     int err = 0;
     while (items) {
         // (nothing to check for USER_DATA_TYPE)
-        if (items->item_type == INTERFACE_TYPE_BINDER
-                || items->item_type == INTERFACE_TYPE_RPC) {
+        if (items->item_type == INTERFACE_TYPE_BINDER) {
             map<string,method_type*> methodNames;
             interface_type* c = (interface_type*)items;
 
@@ -564,7 +513,7 @@
                 if (member->item_type == METHOD_TYPE) {
                     method_type* m = (method_type*)member;
 
-                    err |= check_method(filename, items->item_type, m);
+                    err |= check_method(filename, m);
 
                     // prevent duplicate methods
                     if (methodNames.find(m->name.data) == methodNames.end()) {
@@ -605,9 +554,6 @@
         if (next->item_type == INTERFACE_TYPE_BINDER) {
             lineno = ((interface_type*)next)->interface_token.lineno;
         }
-        else if (next->item_type == INTERFACE_TYPE_RPC) {
-            lineno = ((interface_type*)next)->interface_token.lineno;
-        }
         fprintf(stderr, "%s:%d aidl can only handle one interface per file\n",
                             filename, lineno);
         return 1;
@@ -617,9 +563,9 @@
         *onlyParcelable = true;
         if (options.failOnParcelable) {
             fprintf(stderr, "%s:%d aidl can only generate code for interfaces, not"
-                            " parcelables or flattenables,\n", filename,
+                            " parcelables,\n", filename,
                             ((user_data_type*)items)->keyword_token.lineno);
-            fprintf(stderr, "%s:%d .aidl files that only declare parcelables or flattenables"
+            fprintf(stderr, "%s:%d .aidl files that only declare parcelables"
                             "may not go in the Makefile.\n", filename,
                             ((user_data_type*)items)->keyword_token.lineno);
             return 1;
@@ -656,7 +602,7 @@
         slash = "";
     }
 
-    if (items->item_type == INTERFACE_TYPE_BINDER || items->item_type == INTERFACE_TYPE_RPC) {
+    if (items->item_type == INTERFACE_TYPE_BINDER) {
         fprintf(to, "%s: \\\n", options.outputFileName.c_str());
     } else {
         // parcelable: there's no output file.
@@ -730,7 +676,7 @@
 generate_outputFileName(const Options& options, const document_item_type* items)
 {
     // items has already been checked to have only one interface.
-    if (items->item_type == INTERFACE_TYPE_BINDER || items->item_type == INTERFACE_TYPE_RPC) {
+    if (items->item_type == INTERFACE_TYPE_BINDER) {
         interface_type* type = (interface_type*)items;
 
         return generate_outputFileName2(options, type->name, type->package);
@@ -817,22 +763,7 @@
             parcl->name.data = strdup(classname);
             parcl->semicolon_token.lineno = lineno;
             parcl->semicolon_token.data = strdup(";");
-            parcl->flattening_methods = PARCELABLE_DATA;
-            doc = (document_item_type*)parcl;
-        }
-        else if (0 == strcmp("flattenable", type)) {
-            user_data_type* parcl = (user_data_type*)malloc(
-                    sizeof(user_data_type));
-            memset(parcl, 0, sizeof(user_data_type));
-            parcl->document_item.item_type = USER_DATA_TYPE;
-            parcl->keyword_token.lineno = lineno;
-            parcl->keyword_token.data = strdup(type);
-            parcl->package = packagename ? strdup(packagename) : NULL;
-            parcl->name.lineno = lineno;
-            parcl->name.data = strdup(classname);
-            parcl->semicolon_token.lineno = lineno;
-            parcl->semicolon_token.data = strdup(";");
-            parcl->flattening_methods = RPC_DATA;
+            parcl->parcelable = true;
             doc = (document_item_type*)parcl;
         }
         else if (0 == strcmp("interface", type)) {
@@ -938,7 +869,7 @@
 }
 
 // ==========================================================
-static int
+int
 compile_aidl(Options& options)
 {
     int err = 0, N;
@@ -1068,7 +999,7 @@
     return err;
 }
 
-static int
+int
 preprocess_aidl(const Options& options)
 {
     vector<string> lines;
@@ -1086,12 +1017,9 @@
         string line;
         if (doc->item_type == USER_DATA_TYPE) {
             user_data_type* parcelable = (user_data_type*)doc;
-            if ((parcelable->flattening_methods & PARCELABLE_DATA) != 0) {
+            if (parcelable->parcelable) {
                 line = "parcelable ";
             }
-            if ((parcelable->flattening_methods & RPC_DATA) != 0) {
-                line = "flattenable ";
-            }
             if (parcelable->package) {
                 line += parcelable->package;
                 line += '.';
@@ -1140,24 +1068,3 @@
     close(fd);
     return 0;
 }
-
-// ==========================================================
-int
-main(int argc, const char **argv)
-{
-    Options options;
-    int result = parse_options(argc, argv, &options);
-    if (result) {
-        return result;
-    }
-
-    switch (options.task)
-    {
-        case COMPILE_AIDL:
-            return compile_aidl(options);
-        case PREPROCESS_AIDL:
-            return preprocess_aidl(options);
-    }
-    fprintf(stderr, "aidl: internal error\n");
-    return 1;
-}
diff --git a/tools/aidl/aidl.h b/tools/aidl/aidl.h
new file mode 100644
index 0000000..98b15f3
--- /dev/null
+++ b/tools/aidl/aidl.h
@@ -0,0 +1,9 @@
+#ifndef AIDL_AIDL_H_
+#define AIDL_AIDL_H_
+
+#include "options.h"
+
+int compile_aidl(Options& options);
+int preprocess_aidl(const Options& options);
+
+#endif  // AIDL_AIDL_H_
diff --git a/tools/aidl/aidl_language.h b/tools/aidl/aidl_language.h
index de1370c..a3b1efc 100644
--- a/tools/aidl/aidl_language.h
+++ b/tools/aidl/aidl_language.h
@@ -1,5 +1,5 @@
-#ifndef DEVICE_TOOLS_AIDL_AIDL_LANGUAGE_H
-#define DEVICE_TOOLS_AIDL_AIDL_LANGUAGE_H
+#ifndef AIDL_AIDL_LANGUAGE_H_
+#define AIDL_AIDL_LANGUAGE_H_
 
 
 typedef enum {
@@ -68,8 +68,7 @@
 
 enum {
     USER_DATA_TYPE = 12,
-    INTERFACE_TYPE_BINDER,
-    INTERFACE_TYPE_RPC
+    INTERFACE_TYPE_BINDER
 };
 
 typedef struct document_item_type {
@@ -78,19 +77,13 @@
 } document_item_type;
 
 
-// for user_data_type.flattening_methods
-enum {
-    PARCELABLE_DATA = 0x1,
-    RPC_DATA = 0x2
-};
-
 typedef struct user_data_type {
     document_item_type document_item;
     buffer_type keyword_token; // only the first one
     char* package;
     buffer_type name;
     buffer_type semicolon_token;
-    int flattening_methods;
+    bool parcelable;
 } user_data_type;
 
 typedef struct interface_type {
@@ -169,4 +162,4 @@
 #endif
 
 
-#endif // DEVICE_TOOLS_AIDL_AIDL_LANGUAGE_H
+#endif // AIDL_AIDL_LANGUAGE_H_
diff --git a/tools/aidl/aidl_language_l.l b/tools/aidl/aidl_language_l.l
index 3d33e7a..aa42f2e 100644
--- a/tools/aidl/aidl_language_l.l
+++ b/tools/aidl/aidl_language_l.l
@@ -83,8 +83,6 @@
     /* keywords */
 parcelable      { SET_BUFFER(PARCELABLE); return PARCELABLE; }
 interface       { SET_BUFFER(INTERFACE); return INTERFACE; }
-flattenable     { SET_BUFFER(FLATTENABLE); return FLATTENABLE; }
-rpc             { SET_BUFFER(INTERFACE); return RPC; }
 in              { SET_BUFFER(IN); return IN; }
 out             { SET_BUFFER(OUT); return OUT; }
 inout           { SET_BUFFER(INOUT); return INOUT; }
diff --git a/tools/aidl/aidl_language_y.y b/tools/aidl/aidl_language_y.y
index 9b40d28..9c5d10e 100644
--- a/tools/aidl/aidl_language_y.y
+++ b/tools/aidl/aidl_language_y.y
@@ -20,8 +20,6 @@
 %token ARRAY
 %token PARCELABLE
 %token INTERFACE
-%token FLATTENABLE
-%token RPC
 %token IN
 %token OUT
 %token INOUT
@@ -88,7 +86,7 @@
                                                         b->name = $2.buffer;
                                                         b->package = g_currentPackage ? strdup(g_currentPackage) : NULL;
                                                         b->semicolon_token = $3.buffer;
-                                                        b->flattening_methods = PARCELABLE_DATA;
+                                                        b->parcelable = true;
                                                         $$.user_data = b;
                                                     }
     |   PARCELABLE ';'                              {
@@ -101,28 +99,6 @@
                                                                      g_currentFilename, $2.buffer.lineno, $2.buffer.data);
                                                         $$.user_data = NULL;
                                                     }
-    |   FLATTENABLE IDENTIFIER ';'                  {
-                                                        user_data_type* b = (user_data_type*)malloc(sizeof(user_data_type));
-                                                        b->document_item.item_type = USER_DATA_TYPE;
-                                                        b->document_item.next = NULL;
-                                                        b->keyword_token = $1.buffer;
-                                                        b->name = $2.buffer;
-                                                        b->package = g_currentPackage ? strdup(g_currentPackage) : NULL;
-                                                        b->semicolon_token = $3.buffer;
-                                                        b->flattening_methods = PARCELABLE_DATA | RPC_DATA;
-                                                        $$.user_data = b;
-                                                    }
-    |   FLATTENABLE ';'                             {
-                                                        fprintf(stderr, "%s:%d syntax error in flattenable declaration. Expected type name.\n",
-                                                                     g_currentFilename, $1.buffer.lineno);
-                                                        $$.user_data = NULL;
-                                                    }
-    |   FLATTENABLE error ';'                       {
-                                                        fprintf(stderr, "%s:%d syntax error in flattenable declaration. Expected type name, saw \"%s\".\n",
-                                                                     g_currentFilename, $2.buffer.lineno, $2.buffer.data);
-                                                        $$.user_data = NULL;
-                                                    }
-
     ;
 
 interface_header:
@@ -146,21 +122,6 @@
                                                         c->comments_token = &c->oneway_token;
                                                         $$.interface_obj = c;
                                                    }
-    |   RPC                                        {
-                                                        interface_type* c = (interface_type*)malloc(sizeof(interface_type));
-                                                        c->document_item.item_type = INTERFACE_TYPE_RPC;
-                                                        c->document_item.next = NULL;
-                                                        c->interface_token = $1.buffer;
-                                                        c->oneway = false;
-                                                        memset(&c->oneway_token, 0, sizeof(buffer_type));
-                                                        c->comments_token = &c->interface_token;
-                                                        $$.interface_obj = c;
-                                                   }
-    ;
-
-interface_keywords:
-        INTERFACE
-    |   RPC
     ;
 
 interface_decl:
@@ -173,12 +134,12 @@
                                                         c->close_brace_token = $5.buffer;
                                                         $$.interface_obj = c;
                                                     }
-    |   interface_keywords error '{' interface_items '}'     {
+    |   INTERFACE error '{' interface_items '}'     {
                                                         fprintf(stderr, "%s:%d: syntax error in interface declaration.  Expected type name, saw \"%s\"\n",
                                                                     g_currentFilename, $2.buffer.lineno, $2.buffer.data);
                                                         $$.document_item = NULL;
                                                     }
-    |   interface_keywords error '}'                {
+    |   INTERFACE error '}'                {
                                                         fprintf(stderr, "%s:%d: syntax error in interface declaration.  Expected type name, saw \"%s\"\n",
                                                                     g_currentFilename, $2.buffer.lineno, $2.buffer.data);
                                                         $$.document_item = NULL;
diff --git a/tools/aidl/generate_java.cpp b/tools/aidl/generate_java.cpp
index 9e57407..1509340 100644
--- a/tools/aidl/generate_java.cpp
+++ b/tools/aidl/generate_java.cpp
@@ -66,9 +66,6 @@
     if (iface->document_item.item_type == INTERFACE_TYPE_BINDER) {
         cl = generate_binder_interface_class(iface);
     }
-    else if (iface->document_item.item_type == INTERFACE_TYPE_RPC) {
-        cl = generate_rpc_interface_class(iface);
-    }
 
     Document* document = new Document;
         document->comment = "";
diff --git a/tools/aidl/generate_java.h b/tools/aidl/generate_java.h
index 4bfcfeb..4e79743 100644
--- a/tools/aidl/generate_java.h
+++ b/tools/aidl/generate_java.h
@@ -1,5 +1,5 @@
-#ifndef GENERATE_JAVA_H
-#define GENERATE_JAVA_H
+#ifndef AIDL_GENERATE_JAVA_H_
+#define AIDL_GENERATE_JAVA_H_
 
 #include "aidl_language.h"
 #include "AST.h"
@@ -12,7 +12,6 @@
                 interface_type* iface);
 
 Class* generate_binder_interface_class(const interface_type* iface);
-Class* generate_rpc_interface_class(const interface_type* iface);
 
 string gather_comments(extra_text_type* extra);
 string append(const char* a, const char* b);
@@ -29,5 +28,5 @@
     int m_index;
 };
 
-#endif // GENERATE_JAVA_H
+#endif // AIDL_GENERATE_JAVA_H_
 
diff --git a/tools/aidl/generate_java_rpc.cpp b/tools/aidl/generate_java_rpc.cpp
deleted file mode 100644
index 5e4dacc..0000000
--- a/tools/aidl/generate_java_rpc.cpp
+++ /dev/null
@@ -1,1001 +0,0 @@
-#include "generate_java.h"
-#include "Type.h"
-#include <string.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-Type* SERVICE_CONTEXT_TYPE = new Type("android.content",
-        "Context", Type::BUILT_IN, false, false, false);
-Type* PRESENTER_BASE_TYPE = new Type("android.support.place.connector",
-        "EventListener", Type::BUILT_IN, false, false, false);
-Type* PRESENTER_LISTENER_BASE_TYPE = new Type("android.support.place.connector",
-        "EventListener.Listener", Type::BUILT_IN, false, false, false);
-Type* RPC_BROKER_TYPE = new Type("android.support.place.connector", "Broker",
-        Type::BUILT_IN, false, false, false);
-Type* RPC_CONTAINER_TYPE = new Type("com.android.athome.connector", "ConnectorContainer",
-        Type::BUILT_IN, false, false, false);
-Type* PLACE_INFO_TYPE = new Type("android.support.place.connector", "PlaceInfo",
-        Type::BUILT_IN, false, false, false);
-// TODO: Just use Endpoint, so this works for all endpoints.
-Type* RPC_CONNECTOR_TYPE = new Type("android.support.place.connector", "Connector",
-        Type::BUILT_IN, false, false, false);
-Type* RPC_ENDPOINT_INFO_TYPE = new UserDataType("android.support.place.rpc",
-        "EndpointInfo", true, __FILE__, __LINE__);
-Type* RPC_RESULT_HANDLER_TYPE = new UserDataType("android.support.place.rpc", "RpcResultHandler",
-        true, __FILE__, __LINE__);
-Type* RPC_ERROR_LISTENER_TYPE = new Type("android.support.place.rpc", "RpcErrorHandler",
-        Type::BUILT_IN, false, false, false);
-Type* RPC_CONTEXT_TYPE = new UserDataType("android.support.place.rpc", "RpcContext", true,
-        __FILE__, __LINE__);
-
-static void generate_create_from_data(Type* t, StatementBlock* addTo, const string& key,
-        Variable* v, Variable* data, Variable** cl);
-static void generate_new_array(Type* t, StatementBlock* addTo, Variable* v, Variable* from);
-static void generate_write_to_data(Type* t, StatementBlock* addTo, Expression* k, Variable* v,
-        Variable* data);
-
-static string
-format_int(int n)
-{
-    char str[20];
-    sprintf(str, "%d", n);
-    return string(str);
-}
-
-static string
-class_name_leaf(const string& str)
-{
-    string::size_type pos = str.rfind('.');
-    if (pos == string::npos) {
-        return str;
-    } else {
-        return string(str, pos+1);
-    }
-}
-
-static string
-results_class_name(const string& n)
-{
-    string str = n;
-    str[0] = toupper(str[0]);
-    str.insert(0, "On");
-    return str;
-}
-
-static string
-results_method_name(const string& n)
-{
-    string str = n;
-    str[0] = toupper(str[0]);
-    str.insert(0, "on");
-    return str;
-}
-
-static string
-push_method_name(const string& n)
-{
-    string str = n;
-    str[0] = toupper(str[0]);
-    str.insert(0, "push");
-    return str;
-}
-
-// =================================================
-class DispatcherClass : public Class
-{
-public:
-    DispatcherClass(const interface_type* iface, Expression* target);
-    virtual ~DispatcherClass();
-
-    void AddMethod(const method_type* method);
-    void DoneWithMethods();
-
-    Method* processMethod;
-    Variable* actionParam;
-    Variable* requestParam;
-    Variable* rpcContextParam;
-    Variable* errorParam;
-    Variable* requestData;
-    Variable* resultData;
-    IfStatement* dispatchIfStatement;
-    Expression* targetExpression;
-
-private:
-    void generate_process();
-};
-
-DispatcherClass::DispatcherClass(const interface_type* iface, Expression* target)
-    :Class(),
-     dispatchIfStatement(NULL),
-     targetExpression(target)
-{
-    generate_process();
-}
-
-DispatcherClass::~DispatcherClass()
-{
-}
-
-void
-DispatcherClass::generate_process()
-{
-    // byte[] process(String action, byte[] params, RpcContext context, RpcError status)
-    this->processMethod = new Method;
-        this->processMethod->modifiers = PUBLIC;
-        this->processMethod->returnType = BYTE_TYPE;
-        this->processMethod->returnTypeDimension = 1;
-        this->processMethod->name = "process";
-        this->processMethod->statements = new StatementBlock;
-
-    this->actionParam = new Variable(STRING_TYPE, "action");
-    this->processMethod->parameters.push_back(this->actionParam);
-
-    this->requestParam = new Variable(BYTE_TYPE, "requestParam", 1);
-    this->processMethod->parameters.push_back(this->requestParam);
-
-    this->rpcContextParam = new Variable(RPC_CONTEXT_TYPE, "context", 0);
-    this->processMethod->parameters.push_back(this->rpcContextParam);    
-
-    this->errorParam = new Variable(RPC_ERROR_TYPE, "errorParam", 0);
-    this->processMethod->parameters.push_back(this->errorParam);
-
-    this->requestData = new Variable(RPC_DATA_TYPE, "request");
-    this->processMethod->statements->Add(new VariableDeclaration(requestData,
-                new NewExpression(RPC_DATA_TYPE, 1, this->requestParam)));
-
-    this->resultData = new Variable(RPC_DATA_TYPE, "resultData");
-    this->processMethod->statements->Add(new VariableDeclaration(this->resultData,
-                NULL_VALUE));
-}
-
-void
-DispatcherClass::AddMethod(const method_type* method)
-{
-    arg_type* arg;
-
-    // The if/switch statement
-    IfStatement* ifs = new IfStatement();
-        ifs->expression = new MethodCall(new StringLiteralExpression(method->name.data), "equals",
-                1, this->actionParam);
-    StatementBlock* block = ifs->statements = new StatementBlock;
-    if (this->dispatchIfStatement == NULL) {
-        this->dispatchIfStatement = ifs;
-        this->processMethod->statements->Add(dispatchIfStatement);
-    } else {
-        this->dispatchIfStatement->elseif = ifs;
-        this->dispatchIfStatement = ifs;
-    }
-    
-    // The call to decl (from above)
-    MethodCall* realCall = new MethodCall(this->targetExpression, method->name.data);
-
-    // args
-    Variable* classLoader = NULL;
-    VariableFactory stubArgs("_arg");
-    arg = method->args;
-    while (arg != NULL) {
-        Type* t = NAMES.Search(arg->type.type.data);
-        Variable* v = stubArgs.Get(t);
-        v->dimension = arg->type.dimension;
-
-        // Unmarshall the parameter
-        block->Add(new VariableDeclaration(v));
-        if (convert_direction(arg->direction.data) & IN_PARAMETER) {
-            generate_create_from_data(t, block, arg->name.data, v,
-                    this->requestData, &classLoader);
-        } else {
-            if (arg->type.dimension == 0) {
-                block->Add(new Assignment(v, new NewExpression(v->type)));
-            }
-            else if (arg->type.dimension == 1) {
-                generate_new_array(v->type, block, v, this->requestData);
-            }
-            else {
-                fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__,
-                        __LINE__);
-            }
-        }
-
-        // Add that parameter to the method call
-        realCall->arguments.push_back(v);
-
-        arg = arg->next;
-    }
-
-    // Add a final parameter: RpcContext. Contains data about
-    // incoming request (e.g., certificate)
-    realCall->arguments.push_back(new Variable(RPC_CONTEXT_TYPE, "context", 0));
-
-    Type* returnType = NAMES.Search(method->type.type.data);
-    if (returnType == EVENT_FAKE_TYPE) {
-        returnType = VOID_TYPE;
-    }
-    
-    // the real call
-    bool first = true;
-    Variable* _result = NULL;
-    if (returnType == VOID_TYPE) {
-        block->Add(realCall);
-    } else {
-        _result = new Variable(returnType, "_result",
-                                method->type.dimension);
-        block->Add(new VariableDeclaration(_result, realCall));
-
-        // need the result RpcData
-        if (first) {
-            block->Add(new Assignment(this->resultData,
-                        new NewExpression(RPC_DATA_TYPE)));
-            first = false;
-        }
-
-        // marshall the return value
-        generate_write_to_data(returnType, block,
-                new StringLiteralExpression("_result"), _result, this->resultData);
-    }
-
-    // out parameters
-    int i = 0;
-    arg = method->args;
-    while (arg != NULL) {
-        Type* t = NAMES.Search(arg->type.type.data);
-        Variable* v = stubArgs.Get(i++);
-
-        if (convert_direction(arg->direction.data) & OUT_PARAMETER) {
-            // need the result RpcData
-            if (first) {
-                block->Add(new Assignment(this->resultData, new NewExpression(RPC_DATA_TYPE)));
-                first = false;
-            }
-
-            generate_write_to_data(t, block, new StringLiteralExpression(arg->name.data),
-                    v, this->resultData);
-        }
-
-        arg = arg->next;
-    }
-}
-
-void
-DispatcherClass::DoneWithMethods()
-{
-    if (this->dispatchIfStatement == NULL) {
-        return;
-    }
-
-    this->elements.push_back(this->processMethod);
-
-    IfStatement* fallthrough = new IfStatement();
-        fallthrough->statements = new StatementBlock;
-        fallthrough->statements->Add(new ReturnStatement(
-                    new MethodCall(SUPER_VALUE, "process", 4, 
-                    this->actionParam, this->requestParam, 
-                    this->rpcContextParam,
-                    this->errorParam)));
-    this->dispatchIfStatement->elseif = fallthrough;
-    IfStatement* s = new IfStatement;
-        s->statements = new StatementBlock;
-    this->processMethod->statements->Add(s);
-    s->expression = new Comparison(this->resultData, "!=", NULL_VALUE);
-    s->statements->Add(new ReturnStatement(new MethodCall(this->resultData, "serialize")));
-    s->elseif = new IfStatement;
-    s = s->elseif;
-    s->statements->Add(new ReturnStatement(NULL_VALUE));
-}
-
-// =================================================
-class RpcProxyClass : public Class
-{
-public:
-    RpcProxyClass(const interface_type* iface, InterfaceType* interfaceType);
-    virtual ~RpcProxyClass();
-
-    Variable* endpoint;
-    Variable* broker;
-
-private:
-    void generate_ctor();
-    void generate_get_endpoint_info();
-};
-
-RpcProxyClass::RpcProxyClass(const interface_type* iface, InterfaceType* interfaceType)
-    :Class()
-{
-    this->comment = gather_comments(iface->comments_token->extra);
-    this->modifiers = PUBLIC;
-    this->what = Class::CLASS;
-    this->type = interfaceType;
-
-    // broker
-    this->broker = new Variable(RPC_BROKER_TYPE, "_broker");
-    this->elements.push_back(new Field(PRIVATE, this->broker));
-    // endpoint
-    this->endpoint = new Variable(RPC_ENDPOINT_INFO_TYPE, "_endpoint");
-    this->elements.push_back(new Field(PRIVATE, this->endpoint));
-
-    // methods
-    generate_ctor();
-    generate_get_endpoint_info();
-}
-
-RpcProxyClass::~RpcProxyClass()
-{
-}
-
-void
-RpcProxyClass::generate_ctor()
-{
-    Variable* broker = new Variable(RPC_BROKER_TYPE, "broker");
-    Variable* endpoint = new Variable(RPC_ENDPOINT_INFO_TYPE, "endpoint");
-    Method* ctor = new Method;
-        ctor->modifiers = PUBLIC;
-        ctor->name = class_name_leaf(this->type->Name());
-        ctor->statements = new StatementBlock;
-        ctor->parameters.push_back(broker);
-        ctor->parameters.push_back(endpoint);
-    this->elements.push_back(ctor);
-
-    ctor->statements->Add(new Assignment(this->broker, broker));
-    ctor->statements->Add(new Assignment(this->endpoint, endpoint));
-}
-
-void
-RpcProxyClass::generate_get_endpoint_info()
-{
-    Method* get = new Method;
-    get->modifiers = PUBLIC;
-    get->returnType = RPC_ENDPOINT_INFO_TYPE;
-    get->name = "getEndpointInfo";
-    get->statements = new StatementBlock;
-    this->elements.push_back(get);
-
-    get->statements->Add(new ReturnStatement(this->endpoint));
-}
-
-// =================================================
-class EventListenerClass : public DispatcherClass
-{
-public:
-    EventListenerClass(const interface_type* iface, Type* listenerType);
-    virtual ~EventListenerClass();
-
-    Variable* _listener;
-
-private:
-    void generate_ctor();
-};
-
-Expression*
-generate_get_listener_expression(Type* cast)
-{
-    return new Cast(cast, new MethodCall(THIS_VALUE, "getView"));
-}
-
-EventListenerClass::EventListenerClass(const interface_type* iface, Type* listenerType)
-    :DispatcherClass(iface, new FieldVariable(THIS_VALUE, "_listener"))
-{
-    this->modifiers = PRIVATE;
-    this->what = Class::CLASS;
-    this->type = new Type(iface->package ? iface->package : "",
-                        append(iface->name.data, ".Presenter"),
-                        Type::GENERATED, false, false, false);
-    this->extends = PRESENTER_BASE_TYPE;
-
-    this->_listener = new Variable(listenerType, "_listener");
-    this->elements.push_back(new Field(PRIVATE, this->_listener));
-
-    // methods
-    generate_ctor();
-}
-
-EventListenerClass::~EventListenerClass()
-{
-}
-
-void
-EventListenerClass::generate_ctor()
-{
-    Variable* broker = new Variable(RPC_BROKER_TYPE, "broker");
-    Variable* listener = new Variable(this->_listener->type, "listener");
-    Method* ctor = new Method;
-        ctor->modifiers = PUBLIC;
-        ctor->name = class_name_leaf(this->type->Name());
-        ctor->statements = new StatementBlock;
-        ctor->parameters.push_back(broker);
-        ctor->parameters.push_back(listener);
-    this->elements.push_back(ctor);
-
-    ctor->statements->Add(new MethodCall("super", 2, broker, listener));
-    ctor->statements->Add(new Assignment(this->_listener, listener));
-}
-
-// =================================================
-class ListenerClass : public Class
-{
-public:
-    ListenerClass(const interface_type* iface);
-    virtual ~ListenerClass();
-
-    bool needed;
-
-private:
-    void generate_ctor();
-};
-
-ListenerClass::ListenerClass(const interface_type* iface)
-    :Class(),
-     needed(false)
-{
-    this->comment = "/** Extend this to listen to the events from this class. */";
-    this->modifiers = STATIC | PUBLIC ;
-    this->what = Class::CLASS;
-    this->type = new Type(iface->package ? iface->package : "",
-                        append(iface->name.data, ".Listener"),
-                        Type::GENERATED, false, false, false);
-    this->extends = PRESENTER_LISTENER_BASE_TYPE;
-}
-
-ListenerClass::~ListenerClass()
-{
-}
-
-// =================================================
-class EndpointBaseClass : public DispatcherClass
-{
-public:
-    EndpointBaseClass(const interface_type* iface);
-    virtual ~EndpointBaseClass();
-
-    bool needed;
-
-private:
-    void generate_ctor();
-};
-
-EndpointBaseClass::EndpointBaseClass(const interface_type* iface)
-    :DispatcherClass(iface, THIS_VALUE),
-     needed(false)
-{
-    this->comment = "/** Extend this to implement a link service. */";
-    this->modifiers = STATIC | PUBLIC | ABSTRACT;
-    this->what = Class::CLASS;
-    this->type = new Type(iface->package ? iface->package : "",
-                        append(iface->name.data, ".EndpointBase"),
-                        Type::GENERATED, false, false, false);
-    this->extends = RPC_CONNECTOR_TYPE;
-
-    // methods
-    generate_ctor();
-}
-
-EndpointBaseClass::~EndpointBaseClass()
-{
-}
-
-void
-EndpointBaseClass::generate_ctor()
-{
-    Variable* container = new Variable(RPC_CONTAINER_TYPE, "container");
-    Variable* broker = new Variable(RPC_BROKER_TYPE, "broker");
-	Variable* place = new Variable(PLACE_INFO_TYPE, "placeInfo");
-    Method* ctor = new Method;
-        ctor->modifiers = PUBLIC;
-        ctor->name = class_name_leaf(this->type->Name());
-        ctor->statements = new StatementBlock;
-        ctor->parameters.push_back(container);
-        ctor->parameters.push_back(broker);
-        ctor->parameters.push_back(place);
-    this->elements.push_back(ctor);
-
-    ctor->statements->Add(new MethodCall("super", 3, container, broker, place));
-}
-
-// =================================================
-class ResultDispatcherClass : public Class
-{
-public:
-    ResultDispatcherClass();
-    virtual ~ResultDispatcherClass();
-
-    void AddMethod(int index, const string& name, Method** method, Variable** param);
-
-    bool needed;
-    Variable* methodId;
-    Variable* callback;
-    Method* onResultMethod;
-    Variable* resultParam;
-    SwitchStatement* methodSwitch;
-
-private:
-    void generate_ctor();
-    void generate_onResult();
-};
-
-ResultDispatcherClass::ResultDispatcherClass()
-    :Class(),
-     needed(false)
-{
-    this->modifiers = PRIVATE | FINAL;
-    this->what = Class::CLASS;
-    this->type = new Type("_ResultDispatcher", Type::GENERATED, false, false, false);
-    this->interfaces.push_back(RPC_RESULT_HANDLER_TYPE);
-
-    // methodId
-    this->methodId = new Variable(INT_TYPE, "methodId");
-    this->elements.push_back(new Field(PRIVATE, this->methodId));
-    this->callback = new Variable(OBJECT_TYPE, "callback");
-    this->elements.push_back(new Field(PRIVATE, this->callback));
-
-    // methods
-    generate_ctor();
-    generate_onResult();
-}
-
-ResultDispatcherClass::~ResultDispatcherClass()
-{
-}
-
-void
-ResultDispatcherClass::generate_ctor()
-{
-    Variable* methodIdParam = new Variable(INT_TYPE, "methId");
-    Variable* callbackParam = new Variable(OBJECT_TYPE, "cbObj");
-    Method* ctor = new Method;
-        ctor->modifiers = PUBLIC;
-        ctor->name = class_name_leaf(this->type->Name());
-        ctor->statements = new StatementBlock;
-        ctor->parameters.push_back(methodIdParam);
-        ctor->parameters.push_back(callbackParam);
-    this->elements.push_back(ctor);
-
-    ctor->statements->Add(new Assignment(this->methodId, methodIdParam));
-    ctor->statements->Add(new Assignment(this->callback, callbackParam));
-}
-
-void
-ResultDispatcherClass::generate_onResult()
-{
-    this->onResultMethod = new Method;
-        this->onResultMethod->modifiers = PUBLIC;
-        this->onResultMethod->returnType = VOID_TYPE;
-        this->onResultMethod->returnTypeDimension = 0;
-        this->onResultMethod->name = "onResult";
-        this->onResultMethod->statements = new StatementBlock;
-    this->elements.push_back(this->onResultMethod);
-
-    this->resultParam = new Variable(BYTE_TYPE, "result", 1);
-    this->onResultMethod->parameters.push_back(this->resultParam);
-
-    this->methodSwitch = new SwitchStatement(this->methodId);
-    this->onResultMethod->statements->Add(this->methodSwitch);
-}
-
-void
-ResultDispatcherClass::AddMethod(int index, const string& name, Method** method, Variable** param)
-{
-    Method* m = new Method;
-        m->modifiers = PUBLIC;
-        m->returnType = VOID_TYPE;
-        m->returnTypeDimension = 0;
-        m->name = name;
-        m->statements = new StatementBlock;
-    *param = new Variable(BYTE_TYPE, "result", 1);
-    m->parameters.push_back(*param);
-    this->elements.push_back(m);
-    *method = m;
-
-    Case* c = new Case(format_int(index));
-    c->statements->Add(new MethodCall(new LiteralExpression("this"), name, 1, this->resultParam));
-    c->statements->Add(new Break());
-
-    this->methodSwitch->cases.push_back(c);
-}
-
-// =================================================
-static void
-generate_new_array(Type* t, StatementBlock* addTo, Variable* v, Variable* from)
-{
-    fprintf(stderr, "aidl: implement generate_new_array %s:%d\n", __FILE__, __LINE__);
-    exit(1);
-}
-
-static void
-generate_create_from_data(Type* t, StatementBlock* addTo, const string& key, Variable* v,
-                            Variable* data, Variable** cl)
-{
-    Expression* k = new StringLiteralExpression(key);
-    if (v->dimension == 0) {
-        t->CreateFromRpcData(addTo, k, v, data, cl);
-    }
-    if (v->dimension == 1) {
-        //t->ReadArrayFromRpcData(addTo, v, data, cl);
-        fprintf(stderr, "aidl: implement generate_create_from_data for arrays%s:%d\n",
-                __FILE__, __LINE__);
-    }
-}
-
-static void
-generate_write_to_data(Type* t, StatementBlock* addTo, Expression* k, Variable* v, Variable* data)
-{
-    if (v->dimension == 0) {
-        t->WriteToRpcData(addTo, k, v, data, 0);
-    }
-    if (v->dimension == 1) {
-        //t->WriteArrayToParcel(addTo, v, data);
-        fprintf(stderr, "aidl: implement generate_write_to_data for arrays%s:%d\n",
-                __FILE__, __LINE__);
-    }
-}
-
-// =================================================
-static Type*
-generate_results_method(const method_type* method, RpcProxyClass* proxyClass)
-{
-    arg_type* arg;
-
-    string resultsMethodName = results_method_name(method->name.data);
-    Type* resultsInterfaceType = new Type(results_class_name(method->name.data),
-            Type::GENERATED, false, false, false);
-
-    if (!method->oneway) {
-        Class* resultsClass = new Class;
-            resultsClass->modifiers = STATIC | PUBLIC;
-            resultsClass->what = Class::INTERFACE;
-            resultsClass->type = resultsInterfaceType;
-
-        Method* resultMethod = new Method;
-            resultMethod->comment = gather_comments(method->comments_token->extra);
-            resultMethod->modifiers = PUBLIC;
-            resultMethod->returnType = VOID_TYPE;
-            resultMethod->returnTypeDimension = 0;
-            resultMethod->name = resultsMethodName;
-        if (0 != strcmp("void", method->type.type.data)) {
-            resultMethod->parameters.push_back(new Variable(NAMES.Search(method->type.type.data),
-                        "_result", method->type.dimension));
-        }
-        arg = method->args;
-        while (arg != NULL) {
-            if (convert_direction(arg->direction.data) & OUT_PARAMETER) {
-                resultMethod->parameters.push_back(new Variable(
-                                    NAMES.Search(arg->type.type.data), arg->name.data,
-                                    arg->type.dimension));
-            }
-            arg = arg->next;
-        }
-        resultsClass->elements.push_back(resultMethod);
-
-        if (resultMethod->parameters.size() > 0) {
-            proxyClass->elements.push_back(resultsClass);
-            return resultsInterfaceType;
-        } 
-    }
-    //delete resultsInterfaceType;
-    return NULL;
-}
-
-static void
-generate_proxy_method(const method_type* method, RpcProxyClass* proxyClass,
-        ResultDispatcherClass* resultsDispatcherClass, Type* resultsInterfaceType, int index)
-{
-    arg_type* arg;
-    Method* proxyMethod = new Method;
-        proxyMethod->comment = gather_comments(method->comments_token->extra);
-        proxyMethod->modifiers = PUBLIC;
-        proxyMethod->returnType = VOID_TYPE;
-        proxyMethod->returnTypeDimension = 0;
-        proxyMethod->name = method->name.data;
-        proxyMethod->statements = new StatementBlock;
-    proxyClass->elements.push_back(proxyMethod);
-
-    // The local variables
-    Variable* _data = new Variable(RPC_DATA_TYPE, "_data");
-    proxyMethod->statements->Add(new VariableDeclaration(_data, new NewExpression(RPC_DATA_TYPE)));
-
-    // Add the arguments
-    arg = method->args;
-    while (arg != NULL) {
-        if (convert_direction(arg->direction.data) & IN_PARAMETER) {
-            // Function signature
-            Type* t = NAMES.Search(arg->type.type.data);
-            Variable* v = new Variable(t, arg->name.data, arg->type.dimension);
-            proxyMethod->parameters.push_back(v);
-
-            // Input parameter marshalling
-            generate_write_to_data(t, proxyMethod->statements,
-                    new StringLiteralExpression(arg->name.data), v, _data);
-        }
-        arg = arg->next;
-    }
-
-    // If there is a results interface for this class
-    Expression* resultParameter;
-    if (resultsInterfaceType != NULL) {
-        // Result interface parameter
-        Variable* resultListener = new Variable(resultsInterfaceType, "_result");
-        proxyMethod->parameters.push_back(resultListener);
-
-        // Add the results dispatcher callback
-        resultsDispatcherClass->needed = true;
-        resultParameter = new NewExpression(resultsDispatcherClass->type, 2,
-                new LiteralExpression(format_int(index)), resultListener);
-    } else {
-        resultParameter = NULL_VALUE;
-    }
-
-    // All proxy methods take an error parameter
-    Variable* errorListener = new Variable(RPC_ERROR_LISTENER_TYPE, "_errors");
-    proxyMethod->parameters.push_back(errorListener);
-
-    // Call the broker
-    proxyMethod->statements->Add(new MethodCall(new FieldVariable(THIS_VALUE, "_broker"),
-                "sendRpc", 5,
-                proxyClass->endpoint,
-                new StringLiteralExpression(method->name.data),
-                new MethodCall(_data, "serialize"),
-                resultParameter,
-                errorListener));
-}
-
-static void
-generate_result_dispatcher_method(const method_type* method,
-        ResultDispatcherClass* resultsDispatcherClass, Type* resultsInterfaceType, int index)
-{
-    arg_type* arg;
-    Method* dispatchMethod;
-    Variable* dispatchParam;
-    resultsDispatcherClass->AddMethod(index, method->name.data, &dispatchMethod, &dispatchParam);
-
-    Variable* classLoader = NULL;
-    Variable* resultData = new Variable(RPC_DATA_TYPE, "resultData");
-    dispatchMethod->statements->Add(new VariableDeclaration(resultData,
-                new NewExpression(RPC_DATA_TYPE, 1, dispatchParam)));
-
-    // The callback method itself
-    MethodCall* realCall = new MethodCall(
-            new Cast(resultsInterfaceType, new FieldVariable(THIS_VALUE, "callback")),
-            results_method_name(method->name.data));
-
-    // The return value
-    {
-        Type* t = NAMES.Search(method->type.type.data);
-        if (t != VOID_TYPE) {
-            Variable* rv = new Variable(t, "rv");
-            dispatchMethod->statements->Add(new VariableDeclaration(rv));
-            generate_create_from_data(t, dispatchMethod->statements, "_result", rv,
-                    resultData, &classLoader);
-            realCall->arguments.push_back(rv);
-        }
-    }
-
-    VariableFactory stubArgs("arg");
-    arg = method->args;
-    while (arg != NULL) {
-        if (convert_direction(arg->direction.data) & OUT_PARAMETER) {
-            // Unmarshall the results
-            Type* t = NAMES.Search(arg->type.type.data);
-            Variable* v = stubArgs.Get(t);
-            dispatchMethod->statements->Add(new VariableDeclaration(v));
-
-            generate_create_from_data(t, dispatchMethod->statements, arg->name.data, v,
-                    resultData, &classLoader);
-
-            // Add the argument to the callback
-            realCall->arguments.push_back(v);
-        }
-        arg = arg->next;
-    }
-
-    // Call the callback method
-    IfStatement* ifst = new IfStatement;
-        ifst->expression = new Comparison(new FieldVariable(THIS_VALUE, "callback"), "!=", NULL_VALUE);
-    dispatchMethod->statements->Add(ifst);
-    ifst->statements->Add(realCall);
-}
-
-static void
-generate_regular_method(const method_type* method, RpcProxyClass* proxyClass,
-        EndpointBaseClass* serviceBaseClass, ResultDispatcherClass* resultsDispatcherClass,
-        int index)
-{
-    arg_type* arg;
-
-    // == the callback interface for results ================================
-    // the service base class
-    Type* resultsInterfaceType = generate_results_method(method, proxyClass);
-    
-    // == the method in the proxy class =====================================
-    generate_proxy_method(method, proxyClass, resultsDispatcherClass, resultsInterfaceType, index);
-
-    // == the method in the result dispatcher class =========================
-    if (resultsInterfaceType != NULL) {
-        generate_result_dispatcher_method(method, resultsDispatcherClass, resultsInterfaceType,
-                index);
-    }
-
-    // == The abstract method that the service developers implement ==========
-    Method* decl = new Method;
-        decl->comment = gather_comments(method->comments_token->extra);
-        decl->modifiers = PUBLIC | ABSTRACT;
-        decl->returnType = NAMES.Search(method->type.type.data);
-        decl->returnTypeDimension = method->type.dimension;
-        decl->name = method->name.data;
-    arg = method->args;
-    while (arg != NULL) {
-        decl->parameters.push_back(new Variable(
-                            NAMES.Search(arg->type.type.data), arg->name.data,
-                            arg->type.dimension));
-        arg = arg->next;
-    }
-
-    // Add the default RpcContext param to all methods
-    decl->parameters.push_back(new Variable(RPC_CONTEXT_TYPE, "context", 0));
-	
-    serviceBaseClass->elements.push_back(decl);
-    
-
-    // == the dispatch method in the service base class ======================
-    serviceBaseClass->AddMethod(method);
-}
-
-static void
-generate_event_method(const method_type* method, RpcProxyClass* proxyClass,
-        EndpointBaseClass* serviceBaseClass, ListenerClass* listenerClass,
-        EventListenerClass* presenterClass, int index)
-{
-    arg_type* arg;
-    listenerClass->needed = true;
-
-    // == the push method in the service base class =========================
-    Method* push = new Method;
-        push->modifiers = PUBLIC;
-        push->name = push_method_name(method->name.data);
-        push->statements = new StatementBlock;
-        push->returnType = VOID_TYPE;
-    serviceBaseClass->elements.push_back(push);
-
-    // The local variables
-    Variable* _data = new Variable(RPC_DATA_TYPE, "_data");
-    push->statements->Add(new VariableDeclaration(_data, new NewExpression(RPC_DATA_TYPE)));
-
-    // Add the arguments
-    arg = method->args;
-    while (arg != NULL) {
-        // Function signature
-        Type* t = NAMES.Search(arg->type.type.data);
-        Variable* v = new Variable(t, arg->name.data, arg->type.dimension);
-        push->parameters.push_back(v);
-
-        // Input parameter marshalling
-        generate_write_to_data(t, push->statements,
-                new StringLiteralExpression(arg->name.data), v, _data);
-
-        arg = arg->next;
-    }
-
-    // Send the notifications
-    push->statements->Add(new MethodCall("pushEvent", 2,
-                new StringLiteralExpression(method->name.data),
-                new MethodCall(_data, "serialize")));
-
-    // == the event callback dispatcher method  ====================================
-    presenterClass->AddMethod(method);
-
-    // == the event method in the listener base class =====================
-    Method* event = new Method;
-        event->modifiers = PUBLIC;
-        event->name = method->name.data;
-        event->statements = new StatementBlock;
-        event->returnType = VOID_TYPE;
-    listenerClass->elements.push_back(event);
-    arg = method->args;
-    while (arg != NULL) {
-        event->parameters.push_back(new Variable(
-                            NAMES.Search(arg->type.type.data), arg->name.data,
-                            arg->type.dimension));
-        arg = arg->next;
-    }
-
-    // Add a final parameter: RpcContext. Contains data about
-    // incoming request (e.g., certificate)
-    event->parameters.push_back(new Variable(RPC_CONTEXT_TYPE, "context", 0));
-}
-
-static void
-generate_listener_methods(RpcProxyClass* proxyClass, Type* presenterType, Type* listenerType)
-{
-    // AndroidAtHomePresenter _presenter;
-    // void startListening(Listener listener) {
-    //     stopListening();
-    //     _presenter = new Presenter(_broker, listener);
-    //     _presenter.startListening(_endpoint);
-    // }
-    // void stopListening() {
-    //     if (_presenter != null) {
-    //         _presenter.stopListening();
-    //     }
-    // }
-
-    Variable* _presenter = new Variable(presenterType, "_presenter");
-    proxyClass->elements.push_back(new Field(PRIVATE, _presenter));
-
-    Variable* listener = new Variable(listenerType, "listener");
-
-    Method* startListeningMethod = new Method;
-        startListeningMethod->modifiers = PUBLIC;
-        startListeningMethod->returnType = VOID_TYPE;
-        startListeningMethod->name = "startListening";
-        startListeningMethod->statements = new StatementBlock;
-        startListeningMethod->parameters.push_back(listener);
-    proxyClass->elements.push_back(startListeningMethod);
-
-    startListeningMethod->statements->Add(new MethodCall(THIS_VALUE, "stopListening"));
-    startListeningMethod->statements->Add(new Assignment(_presenter,
-                new NewExpression(presenterType, 2, proxyClass->broker, listener)));
-    startListeningMethod->statements->Add(new MethodCall(_presenter,
-                "startListening", 1, proxyClass->endpoint));
-
-    Method* stopListeningMethod = new Method;
-        stopListeningMethod->modifiers = PUBLIC;
-        stopListeningMethod->returnType = VOID_TYPE;
-        stopListeningMethod->name = "stopListening";
-        stopListeningMethod->statements = new StatementBlock;
-    proxyClass->elements.push_back(stopListeningMethod);
-
-    IfStatement* ifst = new IfStatement;
-        ifst->expression = new Comparison(_presenter, "!=", NULL_VALUE);
-    stopListeningMethod->statements->Add(ifst);
-
-    ifst->statements->Add(new MethodCall(_presenter, "stopListening"));
-    ifst->statements->Add(new Assignment(_presenter, NULL_VALUE));
-}
-
-Class*
-generate_rpc_interface_class(const interface_type* iface)
-{
-    // the proxy class
-    InterfaceType* interfaceType = static_cast<InterfaceType*>(
-        NAMES.Find(iface->package, iface->name.data));
-    RpcProxyClass* proxy = new RpcProxyClass(iface, interfaceType);
-
-    // the listener class
-    ListenerClass* listener = new ListenerClass(iface);
-
-    // the presenter class
-    EventListenerClass* presenter = new EventListenerClass(iface, listener->type);
-
-    // the service base class
-    EndpointBaseClass* base = new EndpointBaseClass(iface);
-    proxy->elements.push_back(base);
-
-    // the result dispatcher
-    ResultDispatcherClass* results = new ResultDispatcherClass();
-
-    // all the declared methods of the proxy
-    int index = 0;
-    interface_item_type* item = iface->interface_items;
-    while (item != NULL) {
-        if (item->item_type == METHOD_TYPE) {
-            if (NAMES.Search(((method_type*)item)->type.type.data) == EVENT_FAKE_TYPE) {
-                generate_event_method((method_type*)item, proxy, base, listener, presenter, index);
-            } else {
-                generate_regular_method((method_type*)item, proxy, base, results, index);
-            }
-        }
-        item = item->next;
-        index++;
-    }
-    presenter->DoneWithMethods();
-    base->DoneWithMethods();
-
-    // only add this if there are methods with results / out parameters
-    if (results->needed) {
-        proxy->elements.push_back(results);
-    }
-    if (listener->needed) {
-        proxy->elements.push_back(listener);
-        proxy->elements.push_back(presenter);
-        generate_listener_methods(proxy, presenter->type, listener->type);
-    }
-
-    return proxy;
-}
diff --git a/tools/aidl/main.cpp b/tools/aidl/main.cpp
new file mode 100644
index 0000000..7cc2198
--- /dev/null
+++ b/tools/aidl/main.cpp
@@ -0,0 +1,23 @@
+#include "aidl.h"
+#include "options.h"
+
+#include <stdio.h>
+
+int
+main(int argc, const char **argv)
+{
+    Options options;
+    int result = parse_options(argc, argv, &options);
+    if (result) {
+        return result;
+    }
+
+    switch (options.task) {
+        case COMPILE_AIDL:
+            return compile_aidl(options);
+        case PREPROCESS_AIDL:
+            return preprocess_aidl(options);
+    }
+    fprintf(stderr, "aidl: internal error\n");
+    return 1;
+}
diff --git a/tools/aidl/options.cpp b/tools/aidl/options.cpp
index 7b2daeb..52b0972 100644
--- a/tools/aidl/options.cpp
+++ b/tools/aidl/options.cpp
@@ -48,10 +48,6 @@
         return 0;
     }
 
-    options->task = COMPILE_AIDL;
-    options->failOnParcelable = false;
-    options->autoDepFile = false;
-
     // OPTIONS
     while (i < argc) {
         const char* s = argv[i];
diff --git a/tools/aidl/options.h b/tools/aidl/options.h
index 387e37d..4e95e11 100644
--- a/tools/aidl/options.h
+++ b/tools/aidl/options.h
@@ -1,5 +1,5 @@
-#ifndef DEVICE_TOOLS_AIDL_H
-#define DEVICE_TOOLS_AIDL_H
+#ifndef AIDL_OPTIONS_H_
+#define AIDL_OPTIONS_H_
 
 #include <string.h>
 #include <string>
@@ -15,15 +15,15 @@
 // This struct is the parsed version of the command line options
 struct Options
 {
-    int task;
-    bool failOnParcelable;
+    int task{COMPILE_AIDL};
+    bool failOnParcelable{false};
     vector<string> importPaths;
     vector<string> preprocessedFiles;
     string inputFileName;
     string outputFileName;
     string outputBaseFolder;
     string depFileName;
-    bool autoDepFile;
+    bool autoDepFile{false};
 
     vector<string> filesToPreprocess;
 };
@@ -33,4 +33,4 @@
 // It also prints the usage statement on failure.
 int parse_options(int argc, const char* const* argv, Options *options);
 
-#endif // DEVICE_TOOLS_AIDL_H
+#endif // AIDL_OPTIONS_H_
diff --git a/tools/aidl/options_test.cpp b/tools/aidl/options_test.cpp
deleted file mode 100644
index bd106ce..0000000
--- a/tools/aidl/options_test.cpp
+++ /dev/null
@@ -1,291 +0,0 @@
-#include <iostream>
-#include "options.h"
-
-const bool VERBOSE = false;
-
-using namespace std;
-
-struct Answer {
-    const char* argv[8];
-    int result;
-    const char* systemSearchPath[8];
-    const char* localSearchPath[8];
-    const char* inputFileName;
-    language_t nativeLanguage;
-    const char* outputH;
-    const char* outputCPP;
-    const char* outputJava;
-};
-
-bool
-match_arrays(const char* const*expected, const vector<string> &got)
-{
-    int count = 0;
-    while (expected[count] != NULL) {
-        count++;
-    }
-    if (got.size() != count) {
-        return false;
-    }
-    for (int i=0; i<count; i++) {
-        if (got[i] != expected[i]) {
-            return false;
-        }
-    }
-    return true;
-}
-
-void
-print_array(const char* prefix, const char* const*expected)
-{
-    while (*expected) {
-        cout << prefix << *expected << endl;
-        expected++;
-    }
-}
-
-void
-print_array(const char* prefix, const vector<string> &got)
-{
-    size_t count = got.size();
-    for (size_t i=0; i<count; i++) {
-        cout << prefix << got[i] << endl;
-    }
-}
-
-static int
-test(const Answer& answer)
-{
-    int argc = 0;
-    while (answer.argv[argc]) {
-        argc++;
-    }
-
-    int err = 0;
-
-    Options options;
-    int result = parse_options(argc, answer.argv, &options);
-
-    // result
-    if (((bool)result) != ((bool)answer.result)) {
-        cout << "mismatch: result: got " << result << " expected " <<
-            answer.result << endl;
-        err = 1;
-    }
-
-    if (result != 0) {
-        // if it failed, everything is invalid
-        return err;
-    }
-
-    // systemSearchPath
-    if (!match_arrays(answer.systemSearchPath, options.systemSearchPath)) {
-        cout << "mismatch: systemSearchPath: got" << endl;
-        print_array("        ", options.systemSearchPath);
-        cout << "    expected" << endl;
-        print_array("        ", answer.systemSearchPath);
-        err = 1;
-    }
-
-    // localSearchPath
-    if (!match_arrays(answer.localSearchPath, options.localSearchPath)) {
-        cout << "mismatch: localSearchPath: got" << endl;
-        print_array("        ", options.localSearchPath);
-        cout << "    expected" << endl;
-        print_array("        ", answer.localSearchPath);
-        err = 1;
-    }
-
-    // inputFileName
-    if (answer.inputFileName != options.inputFileName) {
-        cout << "mismatch: inputFileName: got " << options.inputFileName
-            << " expected " << answer.inputFileName << endl;
-        err = 1;
-    }
-
-    // nativeLanguage
-    if (answer.nativeLanguage != options.nativeLanguage) {
-        cout << "mismatch: nativeLanguage: got " << options.nativeLanguage
-            << " expected " << answer.nativeLanguage << endl;
-        err = 1;
-    }
-
-    // outputH
-    if (answer.outputH != options.outputH) {
-        cout << "mismatch: outputH: got " << options.outputH
-            << " expected " << answer.outputH << endl;
-        err = 1;
-    }
-
-    // outputCPP
-    if (answer.outputCPP != options.outputCPP) {
-        cout << "mismatch: outputCPP: got " << options.outputCPP
-            << " expected " << answer.outputCPP << endl;
-        err = 1;
-    }
-
-    // outputJava
-    if (answer.outputJava != options.outputJava) {
-        cout << "mismatch: outputJava: got " << options.outputJava
-            << " expected " << answer.outputJava << endl;
-        err = 1;
-    }
-
-    return err;
-}
-
-const Answer g_tests[] = {
-
-    {
-        /* argv */              { "test", "-i/moof", "-I/blah", "-Ibleh", "-imoo", "inputFileName.aidl_cpp", NULL, NULL },
-        /* result */            0,
-        /* systemSearchPath */  { "/blah", "bleh", NULL, NULL, NULL, NULL, NULL, NULL },
-        /* localSearchPath */   { "/moof", "moo", NULL, NULL, NULL, NULL, NULL, NULL },
-        /* inputFileName */     "inputFileName.aidl_cpp",
-        /* nativeLanguage */    CPP,
-        /* outputH */           "",
-        /* outputCPP */         "",
-        /* outputJava */        ""
-    },
-
-    {
-        /* argv */              { "test", "inputFileName.aidl_cpp", "-oh", "outputH", NULL, NULL, NULL, NULL },
-        /* result */            0,
-        /* systemSearchPath */  { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
-        /* localSearchPath */   { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
-        /* inputFileName */     "inputFileName.aidl_cpp",
-        /* nativeLanguage */    CPP,
-        /* outputH */           "outputH",
-        /* outputCPP */         "",
-        /* outputJava */        ""
-    },
-
-    {
-        /* argv */              { "test", "inputFileName.aidl_cpp", "-ocpp", "outputCPP", NULL, NULL, NULL, NULL },
-        /* result */            0,
-        /* systemSearchPath */  { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
-        /* localSearchPath */   { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
-        /* inputFileName */     "inputFileName.aidl_cpp",
-        /* nativeLanguage */    CPP,
-        /* outputH */           "",
-        /* outputCPP */         "outputCPP",
-        /* outputJava */        ""
-    },
-
-    {
-        /* argv */              { "test", "inputFileName.aidl_cpp", "-ojava", "outputJava", NULL, NULL, NULL, NULL },
-        /* result */            0,
-        /* systemSearchPath */  { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
-        /* localSearchPath */   { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
-        /* inputFileName */     "inputFileName.aidl_cpp",
-        /* nativeLanguage */    CPP,
-        /* outputH */           "",
-        /* outputCPP */         "",
-        /* outputJava */        "outputJava"
-    },
-
-    {
-        /* argv */              { "test", "inputFileName.aidl_cpp", "-oh", "outputH", "-ocpp", "outputCPP", "-ojava", "outputJava" },
-        /* result */            0,
-        /* systemSearchPath */  { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
-        /* localSearchPath */   { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
-        /* inputFileName */     "inputFileName.aidl_cpp",
-        /* nativeLanguage */    CPP,
-        /* outputH */           "outputH",
-        /* outputCPP */         "outputCPP",
-        /* outputJava */        "outputJava"
-    },
-
-    {
-        /* argv */              { "test", "inputFileName.aidl_cpp", "-oh", "outputH", "-oh", "outputH1", NULL, NULL },
-        /* result */            1,
-        /* systemSearchPath */  { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
-        /* localSearchPath */   { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
-        /* inputFileName */     "",
-        /* nativeLanguage */    CPP,
-        /* outputH */           "",
-        /* outputCPP */         "",
-        /* outputJava */        ""
-    },
-
-    {
-        /* argv */              { "test", "inputFileName.aidl_cpp", "-ocpp", "outputCPP", "-ocpp", "outputCPP1", NULL, NULL },
-        /* result */            1,
-        /* systemSearchPath */  { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
-        /* localSearchPath */   { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
-        /* inputFileName */     "",
-        /* nativeLanguage */    CPP,
-        /* outputH */           "",
-        /* outputCPP */         "",
-        /* outputJava */        ""
-    },
-
-    {
-        /* argv */              { "test", "inputFileName.aidl_cpp", "-ojava", "outputJava", "-ojava", "outputJava1", NULL, NULL },
-        /* result */            1,
-        /* systemSearchPath */  { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
-        /* localSearchPath */   { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
-        /* inputFileName */     "",
-        /* nativeLanguage */    CPP,
-        /* outputH */           "",
-        /* outputCPP */         "",
-        /* outputJava */        ""
-    },
-
-};
-
-int
-main(int argc, const char** argv)
-{
-    const int count = sizeof(g_tests)/sizeof(g_tests[0]);
-    int matches[count];
-
-    int result = 0;
-    for (int i=0; i<count; i++) {
-        if (VERBOSE) {
-            cout << endl;
-            cout << "---------------------------------------------" << endl;
-            const char* const* p = g_tests[i].argv;
-            while (*p) {
-                cout << " " << *p;
-                p++;
-            }
-            cout << endl;
-            cout << "---------------------------------------------" << endl;
-        }
-        matches[i] = test(g_tests[i]);
-        if (VERBOSE) {
-            if (0 == matches[i]) {
-                cout << "passed" << endl;
-            } else {
-                cout << "failed" << endl;
-            }
-            result |= matches[i];
-        }
-    }
-
-    cout << endl;
-    cout << "=============================================" << endl;
-    cout << "options_test summary" << endl;
-    cout << "=============================================" << endl;
-
-    if (!result) {
-        cout << "passed" << endl;
-    } else {
-        cout << "failed the following tests:" << endl;
-        for (int i=0; i<count; i++) {
-            if (matches[i]) {
-                cout << "   ";
-                const char* const* p = g_tests[i].argv;
-                while (*p) {
-                    cout << " " << *p;
-                    p++;
-                }
-                cout << endl;
-            }
-        }
-    }
-
-    return result;
-}
-
diff --git a/tools/aidl/options_unittest.cpp b/tools/aidl/options_unittest.cpp
new file mode 100644
index 0000000..fec7f87
--- /dev/null
+++ b/tools/aidl/options_unittest.cpp
@@ -0,0 +1,54 @@
+/*
+ * 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.
+ */
+
+#include <string>
+#include <vector>
+
+#include <gtest/gtest.h>
+
+#include "options.h"
+
+using std::vector;
+using std::string;
+
+const char kPreprocessCommandOutputFile[] = "output_file_name";
+const char kPreprocessCommandInput1[] = "input1";
+const char kPreprocessCommandInput2[] = "input2";
+const char kPreprocessCommandInput3[] = "input3";
+const char* kPreprocessCommand[] = {
+    "aidl", "--preprocess",
+    kPreprocessCommandOutputFile,
+    kPreprocessCommandInput1,
+    kPreprocessCommandInput2,
+    kPreprocessCommandInput3,
+};
+
+TEST(OptionsTests, ParsesPreprocess) {
+  Options options;
+  const int argc = sizeof(kPreprocessCommand) / sizeof(*kPreprocessCommand);
+  EXPECT_EQ(parse_options(argc, kPreprocessCommand, &options), 0);
+  EXPECT_EQ(options.task, PREPROCESS_AIDL);
+  EXPECT_EQ(options.failOnParcelable, false);
+  EXPECT_EQ(options.importPaths.size(), 0u);
+  EXPECT_EQ(options.preprocessedFiles.size(), 0u);
+  EXPECT_EQ(options.inputFileName, string{""});
+  EXPECT_EQ(options.outputFileName, string{kPreprocessCommandOutputFile});
+  EXPECT_EQ(options.autoDepFile, false);
+  const vector<string> expected_input{kPreprocessCommandInput1,
+                                      kPreprocessCommandInput2,
+                                      kPreprocessCommandInput3};
+  EXPECT_EQ(options.filesToPreprocess, expected_input);
+}
diff --git a/tools/aidl/os.h b/tools/aidl/os.h
index 79d2c35..752ed47 100644
--- a/tools/aidl/os.h
+++ b/tools/aidl/os.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef _FRAMEWORKS_BASE_TOOLS_AIDL_OS_SEP_H_
-#define _FRAMEWORKS_BASE_TOOLS_AIDL_OS_SEP_H_
+#ifndef AIDL_OS_H_
+#define AIDL_OS_H_
 
 #if defined(_WIN32)
 #define OS_PATH_SEPARATOR '\\'
@@ -23,4 +23,4 @@
 #define OS_PATH_SEPARATOR '/'
 #endif
 
-#endif
+#endif  // AIDL_OS_H_
diff --git a/tools/aidl/search_path.h b/tools/aidl/search_path.h
index 2bf94b1..4fec257 100644
--- a/tools/aidl/search_path.h
+++ b/tools/aidl/search_path.h
@@ -1,5 +1,5 @@
-#ifndef DEVICE_TOOLS_AIDL_SEARCH_PATH_H
-#define DEVICE_TOOLS_AIDL_SEARCH_PATH_H
+#ifndef AIDL_SEARCH_PATH_H_
+#define AIDL_SEARCH_PATH_H_
 
 #include <stdio.h>
 
@@ -19,5 +19,4 @@
 void set_import_paths(const vector<string>& importPaths);
 #endif
 
-#endif // DEVICE_TOOLS_AIDL_SEARCH_PATH_H
-
+#endif // AIDL_SEARCH_PATH_H_
diff --git a/tools/aidl/test_main.cpp b/tools/aidl/test_main.cpp
new file mode 100644
index 0000000..4d820af
--- /dev/null
+++ b/tools/aidl/test_main.cpp
@@ -0,0 +1,6 @@
+#include <gtest/gtest.h>
+
+int main(int argc, char **argv) {
+  ::testing::InitGoogleTest(&argc, argv);
+  return RUN_ALL_TESTS();
+}
diff --git a/tools/aidl/tests/end_to_end_tests.cpp b/tools/aidl/tests/end_to_end_tests.cpp
new file mode 100644
index 0000000..54ca603
--- /dev/null
+++ b/tools/aidl/tests/end_to_end_tests.cpp
@@ -0,0 +1,165 @@
+/*
+ * 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.
+ */
+
+#include <memory>
+#include <string>
+#include <vector>
+
+#include <base/logging.h>
+#include <base/files/file_path.h>
+#include <base/files/file_util.h>
+#include <gtest/gtest.h>
+
+#include "aidl.h"
+#include "options.h"
+#include "tests/test_data.h"
+
+using base::FilePath;
+using std::string;
+using std::unique_ptr;
+using std::vector;
+
+using namespace aidl::test_data;
+
+namespace {
+
+const char kDiffTemplate[] = "diff %s %s";
+const char kStubInterfaceTemplate[] = "package %s;\ninterface %s { }";
+const char kStubParcelableTemplate[] = "package %s;\nparcelable %s;";
+
+FilePath GetPathForPackageClass(const char* package_class,
+                                const char* extension) {
+  string rel_path{package_class};
+  for (char& c : rel_path) {
+    if (c == '.') {
+      c = FilePath::kSeparators[0];
+    }
+  }
+  rel_path += extension;
+  return FilePath(rel_path);
+}
+
+void SplitPackageClass(const string& package_class,
+                       FilePath* rel_path,
+                       string* package,
+                       string* class_name) {
+  *package = string{package_class, 0, package_class.rfind('.')};
+  *class_name = string{package_class, package_class.rfind('.') + 1};
+  *rel_path = GetPathForPackageClass(package_class.c_str(), ".aidl");
+}
+
+}  // namespace
+
+class EndToEndTest : public ::testing::Test {
+ protected:
+  virtual void SetUp() {
+    ASSERT_TRUE(base::CreateNewTempDirectory(
+        string{"end_to_end_testsyyyy"}, &tmpDir_));
+    inputDir_ = tmpDir_.Append("input");
+    outputDir_ = tmpDir_.Append("output");
+    ASSERT_TRUE(base::CreateDirectory(inputDir_));
+    ASSERT_TRUE(base::CreateDirectory(outputDir_));
+  }
+
+  virtual void TearDown() {
+    ASSERT_TRUE(DeleteFile(tmpDir_, true))
+        << "Failed to remove temp directory: " << tmpDir_.value();
+  }
+
+  FilePath CreateInputFile(const FilePath& relative_path,
+                           const char contents[],
+                           int size) {
+    const FilePath created_file = inputDir_.Append(relative_path);
+    EXPECT_TRUE(base::CreateDirectory(created_file.DirName()));
+    EXPECT_TRUE(base::WriteFile(created_file, contents, size));
+    return created_file;
+  }
+
+  void CreateStubAidlFile(const string& package_class,
+                          const char* file_template) {
+    string package, class_name;
+    FilePath rel_path;
+    SplitPackageClass(package_class, &rel_path, &package, &class_name);
+    const size_t buf_len =
+        strlen(file_template) + package.length() + class_name.length() + 1;
+    unique_ptr<char[]> contents(new char[buf_len]);
+    const int written = snprintf(contents.get(), buf_len, file_template,
+                                 package.c_str(), class_name.c_str());
+    EXPECT_GT(written, 0);
+    CreateInputFile(rel_path, contents.get(), written);
+  }
+
+  void WriteStubAidls(const char** parcelables, const char** interfaces) {
+    while (*parcelables) {
+      CreateStubAidlFile(string{*parcelables}, kStubParcelableTemplate);
+      ++parcelables;
+    }
+    while (*interfaces) {
+      CreateStubAidlFile(string{*interfaces}, kStubInterfaceTemplate);
+      ++interfaces;
+    }
+  }
+
+  void CheckFileContents(const FilePath& rel_path,
+                         const string& expected_content) {
+    string actual_contents;
+    FilePath actual_path = outputDir_.Append(rel_path);
+    if (!ReadFileToString(actual_path, &actual_contents)) {
+      FAIL() << "Failed to read expected output file: " << rel_path.value();
+    }
+    // Generated .java files mention the "original" file as part of their
+    // comment header.  Thus we look for expected_content being a substring.
+    if (actual_contents.find(expected_content) == string::npos) {
+      // When the match fails, display a diff of what's wrong.  This greatly
+      // aids in debugging.
+      FilePath expected_path;
+      EXPECT_TRUE(CreateTemporaryFileInDir(tmpDir_, &expected_path));
+      base::WriteFile(expected_path, expected_content.c_str(),
+                      expected_content.length());
+      const size_t buf_len =
+          strlen(kDiffTemplate) + actual_path.value().length() +
+          expected_path.value().length() + 1;
+      unique_ptr<char[]> diff_cmd(new char[buf_len]);
+      EXPECT_GT(snprintf(diff_cmd.get(), buf_len, kDiffTemplate,
+                         expected_path.value().c_str(),
+                         actual_path.value().c_str()), 0);
+      system(diff_cmd.get());
+      FAIL() << "Actual contents of " << rel_path.value()
+             << " did not match expected content";
+    }
+  }
+
+  FilePath tmpDir_;
+  FilePath inputDir_;
+  FilePath outputDir_;
+};
+
+TEST_F(EndToEndTest, IExampleInterface) {
+  Options options;
+  options.failOnParcelable = true;
+  options.importPaths.push_back(inputDir_.value());
+  options.inputFileName =
+      CreateInputFile(GetPathForPackageClass(kIExampleInterfaceClass, ".aidl"),
+                      kIExampleInterfaceContents,
+                      strlen(kIExampleInterfaceContents)).value();
+  options.autoDepFile = true;
+  options.outputBaseFolder = outputDir_.value();
+  WriteStubAidls(kIExampleInterfaceParcelables, kIExampleInterfaceInterfaces);
+  EXPECT_EQ(compile_aidl(options), 0);
+  CheckFileContents(GetPathForPackageClass(kIExampleInterfaceClass, ".java"),
+                    kIExampleInterfaceJava);
+  // We'd like to check the depends file, but it mentions unique file paths.
+}
diff --git a/tools/aidl/tests/example_interface_test_data.cpp b/tools/aidl/tests/example_interface_test_data.cpp
new file mode 100644
index 0000000..b17a800
--- /dev/null
+++ b/tools/aidl/tests/example_interface_test_data.cpp
@@ -0,0 +1,404 @@
+/*
+ * 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.
+ */
+
+#include "tests/test_data.h"
+
+namespace aidl {
+namespace test_data {
+
+const char kIExampleInterfaceClass[] = "android.test.IExampleInterface";
+
+const char* kIExampleInterfaceParcelables[] = {
+  "android.foo.ExampleParcelable",
+  "android.test.ExampleParcelable2",
+  nullptr,
+};
+
+const char* kIExampleInterfaceInterfaces[] = {
+  "android.bar.IAuxInterface",
+  "android.test.IAuxInterface2",
+  nullptr,
+};
+
+const char kIExampleInterfaceContents[] = R"(
+package android.test;
+
+import android.foo.ExampleParcelable;
+import android.test.ExampleParcelable2;
+import android.bar.IAuxInterface;
+import android.test.IAuxInterface2;
+
+interface IExampleInterface {
+    boolean isEnabled();
+    int getState();
+    String getAddress();
+
+    ExampleParcelable[] getParcelables();
+
+    boolean setScanMode(int mode, int duration);
+
+    void registerBinder(IAuxInterface foo);
+    IExampleInterface getRecursiveBinder();
+
+    int takesAnInterface(in IAuxInterface2 arg);
+    int takesAParcelable(in ExampleParcelable2 arg);
+}
+)";
+
+const char kIExampleInterfaceDeps[] =
+R"(/tmp/.org.chromium.Chromium.Cdq7YZ/output/android/test/IExampleInterface.java: \
+  /tmp/.org.chromium.Chromium.Cdq7YZ/input/android/test/IExampleInterface.aidl \
+  /tmp/.org.chromium.Chromium.Cdq7YZ/input/android/foo/ExampleParcelable.aidl \
+  /tmp/.org.chromium.Chromium.Cdq7YZ/input/android/test/ExampleParcelable2.aidl \
+  /tmp/.org.chromium.Chromium.Cdq7YZ/input/android/bar/IAuxInterface.aidl \
+  /tmp/.org.chromium.Chromium.Cdq7YZ/input/android/test/IAuxInterface2.aidl 
+
+/tmp/.org.chromium.Chromium.Cdq7YZ/input/android/test/IExampleInterface.aidl :
+/tmp/.org.chromium.Chromium.Cdq7YZ/input/android/foo/ExampleParcelable.aidl :
+/tmp/.org.chromium.Chromium.Cdq7YZ/input/android/test/ExampleParcelable2.aidl :
+/tmp/.org.chromium.Chromium.Cdq7YZ/input/android/bar/IAuxInterface.aidl :
+/tmp/.org.chromium.Chromium.Cdq7YZ/input/android/test/IAuxInterface2.aidl :)";
+
+const char kIExampleInterfaceJava[] =
+R"(package android.test;
+public interface IExampleInterface extends android.os.IInterface
+{
+/** Local-side IPC implementation stub class. */
+public static abstract class Stub extends android.os.Binder implements android.test.IExampleInterface
+{
+private static final java.lang.String DESCRIPTOR = "android.test.IExampleInterface";
+/** Construct the stub at attach it to the interface. */
+public Stub()
+{
+this.attachInterface(this, DESCRIPTOR);
+}
+/**
+ * Cast an IBinder object into an android.test.IExampleInterface interface,
+ * generating a proxy if needed.
+ */
+public static android.test.IExampleInterface asInterface(android.os.IBinder obj)
+{
+if ((obj==null)) {
+return null;
+}
+android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
+if (((iin!=null)&&(iin instanceof android.test.IExampleInterface))) {
+return ((android.test.IExampleInterface)iin);
+}
+return new android.test.IExampleInterface.Stub.Proxy(obj);
+}
+@Override public android.os.IBinder asBinder()
+{
+return this;
+}
+@Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
+{
+switch (code)
+{
+case INTERFACE_TRANSACTION:
+{
+reply.writeString(DESCRIPTOR);
+return true;
+}
+case TRANSACTION_isEnabled:
+{
+data.enforceInterface(DESCRIPTOR);
+boolean _result = this.isEnabled();
+reply.writeNoException();
+reply.writeInt(((_result)?(1):(0)));
+return true;
+}
+case TRANSACTION_getState:
+{
+data.enforceInterface(DESCRIPTOR);
+int _result = this.getState();
+reply.writeNoException();
+reply.writeInt(_result);
+return true;
+}
+case TRANSACTION_getAddress:
+{
+data.enforceInterface(DESCRIPTOR);
+java.lang.String _result = this.getAddress();
+reply.writeNoException();
+reply.writeString(_result);
+return true;
+}
+case TRANSACTION_getParcelables:
+{
+data.enforceInterface(DESCRIPTOR);
+android.foo.ExampleParcelable[] _result = this.getParcelables();
+reply.writeNoException();
+reply.writeTypedArray(_result, android.os.Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
+return true;
+}
+case TRANSACTION_setScanMode:
+{
+data.enforceInterface(DESCRIPTOR);
+int _arg0;
+_arg0 = data.readInt();
+int _arg1;
+_arg1 = data.readInt();
+boolean _result = this.setScanMode(_arg0, _arg1);
+reply.writeNoException();
+reply.writeInt(((_result)?(1):(0)));
+return true;
+}
+case TRANSACTION_registerBinder:
+{
+data.enforceInterface(DESCRIPTOR);
+android.bar.IAuxInterface _arg0;
+_arg0 = android.bar.IAuxInterface.Stub.asInterface(data.readStrongBinder());
+this.registerBinder(_arg0);
+reply.writeNoException();
+return true;
+}
+case TRANSACTION_getRecursiveBinder:
+{
+data.enforceInterface(DESCRIPTOR);
+android.test.IExampleInterface _result = this.getRecursiveBinder();
+reply.writeNoException();
+reply.writeStrongBinder((((_result!=null))?(_result.asBinder()):(null)));
+return true;
+}
+case TRANSACTION_takesAnInterface:
+{
+data.enforceInterface(DESCRIPTOR);
+android.test.IAuxInterface2 _arg0;
+_arg0 = android.test.IAuxInterface2.Stub.asInterface(data.readStrongBinder());
+int _result = this.takesAnInterface(_arg0);
+reply.writeNoException();
+reply.writeInt(_result);
+return true;
+}
+case TRANSACTION_takesAParcelable:
+{
+data.enforceInterface(DESCRIPTOR);
+android.test.ExampleParcelable2 _arg0;
+if ((0!=data.readInt())) {
+_arg0 = android.test.ExampleParcelable2.CREATOR.createFromParcel(data);
+}
+else {
+_arg0 = null;
+}
+int _result = this.takesAParcelable(_arg0);
+reply.writeNoException();
+reply.writeInt(_result);
+return true;
+}
+}
+return super.onTransact(code, data, reply, flags);
+}
+private static class Proxy implements android.test.IExampleInterface
+{
+private android.os.IBinder mRemote;
+Proxy(android.os.IBinder remote)
+{
+mRemote = remote;
+}
+@Override public android.os.IBinder asBinder()
+{
+return mRemote;
+}
+public java.lang.String getInterfaceDescriptor()
+{
+return DESCRIPTOR;
+}
+@Override public boolean isEnabled() throws android.os.RemoteException
+{
+android.os.Parcel _data = android.os.Parcel.obtain();
+android.os.Parcel _reply = android.os.Parcel.obtain();
+boolean _result;
+try {
+_data.writeInterfaceToken(DESCRIPTOR);
+mRemote.transact(Stub.TRANSACTION_isEnabled, _data, _reply, 0);
+_reply.readException();
+_result = (0!=_reply.readInt());
+}
+finally {
+_reply.recycle();
+_data.recycle();
+}
+return _result;
+}
+@Override public int getState() throws android.os.RemoteException
+{
+android.os.Parcel _data = android.os.Parcel.obtain();
+android.os.Parcel _reply = android.os.Parcel.obtain();
+int _result;
+try {
+_data.writeInterfaceToken(DESCRIPTOR);
+mRemote.transact(Stub.TRANSACTION_getState, _data, _reply, 0);
+_reply.readException();
+_result = _reply.readInt();
+}
+finally {
+_reply.recycle();
+_data.recycle();
+}
+return _result;
+}
+@Override public java.lang.String getAddress() throws android.os.RemoteException
+{
+android.os.Parcel _data = android.os.Parcel.obtain();
+android.os.Parcel _reply = android.os.Parcel.obtain();
+java.lang.String _result;
+try {
+_data.writeInterfaceToken(DESCRIPTOR);
+mRemote.transact(Stub.TRANSACTION_getAddress, _data, _reply, 0);
+_reply.readException();
+_result = _reply.readString();
+}
+finally {
+_reply.recycle();
+_data.recycle();
+}
+return _result;
+}
+@Override public android.foo.ExampleParcelable[] getParcelables() throws android.os.RemoteException
+{
+android.os.Parcel _data = android.os.Parcel.obtain();
+android.os.Parcel _reply = android.os.Parcel.obtain();
+android.foo.ExampleParcelable[] _result;
+try {
+_data.writeInterfaceToken(DESCRIPTOR);
+mRemote.transact(Stub.TRANSACTION_getParcelables, _data, _reply, 0);
+_reply.readException();
+_result = _reply.createTypedArray(android.foo.ExampleParcelable.CREATOR);
+}
+finally {
+_reply.recycle();
+_data.recycle();
+}
+return _result;
+}
+@Override public boolean setScanMode(int mode, int duration) throws android.os.RemoteException
+{
+android.os.Parcel _data = android.os.Parcel.obtain();
+android.os.Parcel _reply = android.os.Parcel.obtain();
+boolean _result;
+try {
+_data.writeInterfaceToken(DESCRIPTOR);
+_data.writeInt(mode);
+_data.writeInt(duration);
+mRemote.transact(Stub.TRANSACTION_setScanMode, _data, _reply, 0);
+_reply.readException();
+_result = (0!=_reply.readInt());
+}
+finally {
+_reply.recycle();
+_data.recycle();
+}
+return _result;
+}
+@Override public void registerBinder(android.bar.IAuxInterface foo) throws android.os.RemoteException
+{
+android.os.Parcel _data = android.os.Parcel.obtain();
+android.os.Parcel _reply = android.os.Parcel.obtain();
+try {
+_data.writeInterfaceToken(DESCRIPTOR);
+_data.writeStrongBinder((((foo!=null))?(foo.asBinder()):(null)));
+mRemote.transact(Stub.TRANSACTION_registerBinder, _data, _reply, 0);
+_reply.readException();
+}
+finally {
+_reply.recycle();
+_data.recycle();
+}
+}
+@Override public android.test.IExampleInterface getRecursiveBinder() throws android.os.RemoteException
+{
+android.os.Parcel _data = android.os.Parcel.obtain();
+android.os.Parcel _reply = android.os.Parcel.obtain();
+android.test.IExampleInterface _result;
+try {
+_data.writeInterfaceToken(DESCRIPTOR);
+mRemote.transact(Stub.TRANSACTION_getRecursiveBinder, _data, _reply, 0);
+_reply.readException();
+_result = android.test.IExampleInterface.Stub.asInterface(_reply.readStrongBinder());
+}
+finally {
+_reply.recycle();
+_data.recycle();
+}
+return _result;
+}
+@Override public int takesAnInterface(android.test.IAuxInterface2 arg) throws android.os.RemoteException
+{
+android.os.Parcel _data = android.os.Parcel.obtain();
+android.os.Parcel _reply = android.os.Parcel.obtain();
+int _result;
+try {
+_data.writeInterfaceToken(DESCRIPTOR);
+_data.writeStrongBinder((((arg!=null))?(arg.asBinder()):(null)));
+mRemote.transact(Stub.TRANSACTION_takesAnInterface, _data, _reply, 0);
+_reply.readException();
+_result = _reply.readInt();
+}
+finally {
+_reply.recycle();
+_data.recycle();
+}
+return _result;
+}
+@Override public int takesAParcelable(android.test.ExampleParcelable2 arg) throws android.os.RemoteException
+{
+android.os.Parcel _data = android.os.Parcel.obtain();
+android.os.Parcel _reply = android.os.Parcel.obtain();
+int _result;
+try {
+_data.writeInterfaceToken(DESCRIPTOR);
+if ((arg!=null)) {
+_data.writeInt(1);
+arg.writeToParcel(_data, 0);
+}
+else {
+_data.writeInt(0);
+}
+mRemote.transact(Stub.TRANSACTION_takesAParcelable, _data, _reply, 0);
+_reply.readException();
+_result = _reply.readInt();
+}
+finally {
+_reply.recycle();
+_data.recycle();
+}
+return _result;
+}
+}
+static final int TRANSACTION_isEnabled = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
+static final int TRANSACTION_getState = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
+static final int TRANSACTION_getAddress = (android.os.IBinder.FIRST_CALL_TRANSACTION + 2);
+static final int TRANSACTION_getParcelables = (android.os.IBinder.FIRST_CALL_TRANSACTION + 3);
+static final int TRANSACTION_setScanMode = (android.os.IBinder.FIRST_CALL_TRANSACTION + 4);
+static final int TRANSACTION_registerBinder = (android.os.IBinder.FIRST_CALL_TRANSACTION + 5);
+static final int TRANSACTION_getRecursiveBinder = (android.os.IBinder.FIRST_CALL_TRANSACTION + 6);
+static final int TRANSACTION_takesAnInterface = (android.os.IBinder.FIRST_CALL_TRANSACTION + 7);
+static final int TRANSACTION_takesAParcelable = (android.os.IBinder.FIRST_CALL_TRANSACTION + 8);
+}
+public boolean isEnabled() throws android.os.RemoteException;
+public int getState() throws android.os.RemoteException;
+public java.lang.String getAddress() throws android.os.RemoteException;
+public android.foo.ExampleParcelable[] getParcelables() throws android.os.RemoteException;
+public boolean setScanMode(int mode, int duration) throws android.os.RemoteException;
+public void registerBinder(android.bar.IAuxInterface foo) throws android.os.RemoteException;
+public android.test.IExampleInterface getRecursiveBinder() throws android.os.RemoteException;
+public int takesAnInterface(android.test.IAuxInterface2 arg) throws android.os.RemoteException;
+public int takesAParcelable(android.test.ExampleParcelable2 arg) throws android.os.RemoteException;
+})";
+
+}  // namespace test_data
+}  // namespace aidl
diff --git a/tools/aidl/tests/test_data.h b/tools/aidl/tests/test_data.h
new file mode 100644
index 0000000..cd8887f
--- /dev/null
+++ b/tools/aidl/tests/test_data.h
@@ -0,0 +1,33 @@
+/*
+ * 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.
+ */
+
+#ifndef AIDL_TESTS_TEST_DATA_H_
+#define AIDL_TESTS_TEST_DATA_H_
+
+namespace aidl {
+namespace test_data {
+
+extern const char kIExampleInterfaceClass[];
+extern const char kIExampleInterfaceContents[];
+extern const char* kIExampleInterfaceParcelables[];
+extern const char* kIExampleInterfaceInterfaces[];
+
+extern const char kIExampleInterfaceDeps[];
+extern const char kIExampleInterfaceJava[];
+
+}  // namespace test_data
+}  // namespace aidl
+#endif // AIDL_TESTS_TEST_DATA_H_
diff --git a/tools/layoutlib/bridge/src/android/view/IWindowManagerImpl.java b/tools/layoutlib/bridge/src/android/view/IWindowManagerImpl.java
index a8fd91c..ed8b56e 100644
--- a/tools/layoutlib/bridge/src/android/view/IWindowManagerImpl.java
+++ b/tools/layoutlib/bridge/src/android/view/IWindowManagerImpl.java
@@ -74,11 +74,10 @@
     // ---- unused implementation of IWindowManager ----
 
     @Override
-    public Configuration addAppToken(int arg0, IApplicationToken arg1, int arg2, int arg3, int arg4,
+    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) throws RemoteException {
+            Rect arg11, Configuration arg12) throws RemoteException {
         // TODO Auto-generated method stub
-        return Configuration.EMPTY;
     }
 
     @Override
@@ -315,9 +314,9 @@
     }
 
     @Override
-    public Configuration setAppTask(IBinder arg0, int arg1, Rect arg2) throws RemoteException {
+    public void setAppTask(IBinder arg0, int arg1, Rect arg2, Configuration arg3)
+            throws RemoteException {
         // TODO Auto-generated method stub
-        return Configuration.EMPTY;
     }
 
     @Override
diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java
index 1f3802e..689e359 100644
--- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java
+++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java
@@ -36,8 +36,8 @@
 import org.xmlpull.v1.XmlPullParser;
 import org.xmlpull.v1.XmlPullParserException;
 
-import android.annotation.Nullable;
 import android.annotation.NonNull;
+import android.annotation.Nullable;
 import android.content.BroadcastReceiver;
 import android.content.ComponentName;
 import android.content.ContentResolver;
@@ -139,8 +139,9 @@
     private Map<StyleResourceValue, Integer> mStyleToDynamicIdMap;
     private int mDynamicIdGenerator = 0x02030000; // Base id for R.style in custom namespace
 
-    // cache for TypedArray generated from IStyleResourceValue object
-    private Map<int[], Map<Integer, BridgeTypedArray>> mTypedArrayCache;
+    // cache for TypedArray generated from StyleResourceValue object
+    private Map<int[], Map<List<StyleResourceValue>, Map<Integer, BridgeTypedArray>>>
+            mTypedArrayCache;
     private BridgeInflater mBridgeInflater;
 
     private BridgeContentResolver mContentResolver;
@@ -621,31 +622,38 @@
             }
         }
 
+        // The map is from
+        // attrs (int[]) -> context's current themes (List<StyleRV>) -> resid (int) -> typed array.
         if (mTypedArrayCache == null) {
-            mTypedArrayCache = new HashMap<int[], Map<Integer,BridgeTypedArray>>();
-
-            Map<Integer, BridgeTypedArray> map = new HashMap<Integer, BridgeTypedArray>();
-            mTypedArrayCache.put(attrs, map);
-
-            BridgeTypedArray ta = createStyleBasedTypedArray(style, attrs);
-            map.put(resid, ta);
-
-            return ta;
+            mTypedArrayCache = new IdentityHashMap<int[],
+                    Map<List<StyleResourceValue>, Map<Integer, BridgeTypedArray>>>();
         }
 
         // get the 2nd map
-        Map<Integer, BridgeTypedArray> map = mTypedArrayCache.get(attrs);
-        if (map == null) {
-            map = new HashMap<Integer, BridgeTypedArray>();
-            mTypedArrayCache.put(attrs, map);
+        Map<List<StyleResourceValue>, Map<Integer, BridgeTypedArray>> map2 =
+                mTypedArrayCache.get(attrs);
+        if (map2 == null) {
+            map2 = new HashMap<List<StyleResourceValue>, Map<Integer, BridgeTypedArray>>();
+            mTypedArrayCache.put(attrs, map2);
         }
 
-        // get the array from the 2nd map
-        BridgeTypedArray ta = map.get(resid);
+        // get the 3rd map
+        List<StyleResourceValue> currentThemes = mRenderResources.getAllThemes();
+        Map<Integer, BridgeTypedArray> map3 = map2.get(currentThemes);
+        if (map3 == null) {
+            map3 = new HashMap<Integer, BridgeTypedArray>();
+            // Create a copy of the list before adding it to the map. This allows reusing the
+            // existing list.
+            currentThemes = new ArrayList<StyleResourceValue>(currentThemes);
+            map2.put(currentThemes, map3);
+        }
+
+        // get the array from the 3rd map
+        BridgeTypedArray ta = map3.get(resid);
 
         if (ta == null) {
             ta = createStyleBasedTypedArray(style, attrs);
-            map.put(resid, ta);
+            map3.put(resid, ta);
         }
 
         return ta;
diff --git a/tools/split-select/Android.mk b/tools/split-select/Android.mk
index d9ddf08..239bed5 100644
--- a/tools/split-select/Android.mk
+++ b/tools/split-select/Android.mk
@@ -43,7 +43,6 @@
     external/zlib \
     frameworks/base/tools
 
-hostLdLibs :=
 hostStaticLibs := \
     libaapt \
     libandroidfw \
@@ -57,17 +56,13 @@
 
 cFlags := -Wall -Werror
 
-ifeq ($(HOST_OS),linux)
-    hostLdLibs += -lrt -ldl -lpthread
-endif
+hostLdLibs_linux := -lrt -ldl -lpthread
 
 # Statically link libz for MinGW (Win SDK under Linux),
 # and dynamically link for all others.
-ifneq ($(strip $(USE_MINGW)),)
-    hostStaticLibs += libz
-else
-    hostLdLibs += -lz
-endif
+hostStaticLibs_windows := libz
+hostLdLibs_darwin := -lz
+hostLdLibs_linux += -lz
 
 
 # ==========================================================
@@ -75,11 +70,12 @@
 # ==========================================================
 include $(CLEAR_VARS)
 LOCAL_MODULE := libsplit-select
+LOCAL_MODULE_HOST_OS := darwin linux windows
 
 LOCAL_SRC_FILES := $(sources)
 
-LOCAL_C_INCLUDES += $(cIncludes)
-LOCAL_CFLAGS += $(cFlags) -D_DARWIN_UNLIMITED_STREAMS
+LOCAL_C_INCLUDES := $(cIncludes)
+LOCAL_CFLAGS := $(cFlags) -D_DARWIN_UNLIMITED_STREAMS
 
 include $(BUILD_HOST_STATIC_LIBRARY)
 
@@ -93,10 +89,12 @@
 
 LOCAL_SRC_FILES := $(testSources)
 
-LOCAL_C_INCLUDES += $(cIncludes)
-LOCAL_STATIC_LIBRARIES += libsplit-select $(hostStaticLibs)
-LOCAL_LDLIBS += $(hostLdLibs)
-LOCAL_CFLAGS += $(cFlags)
+LOCAL_C_INCLUDES := $(cIncludes)
+LOCAL_STATIC_LIBRARIES := libsplit-select $(hostStaticLibs)
+LOCAL_STATIC_LIBRARIES_windows := $(hostStaticLibs_windows)
+LOCAL_LDLIBS_darwin := $(hostLdLibs_darwin)
+LOCAL_LDLIBS_linux := $(hostLdLibs_linux)
+LOCAL_CFLAGS := $(cFlags)
 
 include $(BUILD_HOST_NATIVE_TEST)
 
@@ -105,13 +103,16 @@
 # ==========================================================
 include $(CLEAR_VARS)
 LOCAL_MODULE := split-select
+LOCAL_MODULE_HOST_OS := darwin linux windows
 
 LOCAL_SRC_FILES := $(main)
 
-LOCAL_C_INCLUDES += $(cIncludes)
-LOCAL_STATIC_LIBRARIES += libsplit-select $(hostStaticLibs)
-LOCAL_LDLIBS += $(hostLdLibs)
-LOCAL_CFLAGS += $(cFlags)
+LOCAL_C_INCLUDES := $(cIncludes)
+LOCAL_STATIC_LIBRARIES := libsplit-select $(hostStaticLibs)
+LOCAL_STATIC_LIBRARIES_windows := $(hostStaticLibs_windows)
+LOCAL_LDLIBS_darwin := $(hostLdLibs_darwin)
+LOCAL_LDLIBS_linux := $(hostLdLibs_linux)
+LOCAL_CFLAGS := $(cFlags)
 
 include $(BUILD_HOST_EXECUTABLE)