Merge "Fix 5021968: Add input method switcher button to lock screen."
diff --git a/api/current.txt b/api/current.txt
index 4332833..eb58ae4 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -22315,6 +22315,7 @@
     field public static final int AXIS_RZ = 14; // 0xe
     field public static final int AXIS_SIZE = 3; // 0x3
     field public static final int AXIS_THROTTLE = 19; // 0x13
+    field public static final int AXIS_TILT = 25; // 0x19
     field public static final int AXIS_TOOL_MAJOR = 6; // 0x6
     field public static final int AXIS_TOOL_MINOR = 7; // 0x7
     field public static final int AXIS_TOUCH_MAJOR = 4; // 0x4
diff --git a/cmds/dumpstate/dumpstate.c b/cmds/dumpstate/dumpstate.c
index c2beb74..836cf15 100644
--- a/cmds/dumpstate/dumpstate.c
+++ b/cmds/dumpstate/dumpstate.c
@@ -79,6 +79,7 @@
     dump_file("VMALLOC INFO", "/proc/vmallocinfo");
     dump_file("SLAB INFO", "/proc/slabinfo");
     dump_file("ZONEINFO", "/proc/zoneinfo");
+    dump_file("PAGETYPEINFO", "/proc/pagetypeinfo");
 
     if (screenshot_path[0]) {
         LOGI("taking screenshot\n");
diff --git a/core/java/android/content/pm/ResolveInfo.java b/core/java/android/content/pm/ResolveInfo.java
index 74e756b..bcd599b 100644
--- a/core/java/android/content/pm/ResolveInfo.java
+++ b/core/java/android/content/pm/ResolveInfo.java
@@ -113,7 +113,12 @@
      * containing the resolved component.
      */
     public String resolvePackageName;
-    
+
+    /**
+     * @hide Target comes from system process?
+     */
+    public boolean system;
+
     /**
      * Retrieve the current textual label associated with this resolution.  This
      * will call back on the given PackageManager to load the label from
@@ -261,6 +266,7 @@
         TextUtils.writeToParcel(nonLocalizedLabel, dest, parcelableFlags);
         dest.writeInt(icon);
         dest.writeString(resolvePackageName);
+        dest.writeInt(system ? 1 : 0);
     }
 
     public static final Creator<ResolveInfo> CREATOR
@@ -300,6 +306,7 @@
                 = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
         icon = source.readInt();
         resolvePackageName = source.readString();
+        system = source.readInt() != 0;
     }
     
     public static class DisplayNameComparator
diff --git a/core/java/android/hardware/Camera.java b/core/java/android/hardware/Camera.java
index d3dbb8d..879c441 100644
--- a/core/java/android/hardware/Camera.java
+++ b/core/java/android/hardware/Camera.java
@@ -2406,13 +2406,16 @@
         }
 
         /**
-         * Sets the white balance.
+         * Sets the white balance. Changing the setting will release the
+         * auto-white balance lock.
          *
          * @param value new white balance.
          * @see #getWhiteBalance()
+         * @see #setAutoWhiteBalanceLock()
          */
         public void setWhiteBalance(String value) {
             set(KEY_WHITE_BALANCE, value);
+            set(KEY_AUTO_WHITEBALANCE_LOCK, FALSE);
         }
 
         /**
@@ -2823,6 +2826,9 @@
          * set the lock to false. However, the lock can be re-enabled before
          * preview is re-started to keep the same white balance parameters.</p>
          *
+         * <p> Changing the white balance mode with {@link #setWhiteBalance}
+         * will release the auto-white balance lock if it is set.</p>
+         *
          * <p>Exposure compensation, in conjunction with re-enabling the AE and
          * AWB locks after each still capture, can be used to capture an
          * exposure-bracketed burst of images, for example. Auto-white balance
@@ -2845,6 +2851,7 @@
          *        auto-white balance routine is free to run normally.
          *
          * @see #getAutoWhiteBalanceLock()
+         * @see #setWhiteBalance(String)
          */
         public void setAutoWhiteBalanceLock(boolean toggle) {
             set(KEY_AUTO_WHITEBALANCE_LOCK, toggle ? TRUE : FALSE);
diff --git a/core/java/android/view/MotionEvent.java b/core/java/android/view/MotionEvent.java
index da5c7b2..8e0ab1a 100644
--- a/core/java/android/view/MotionEvent.java
+++ b/core/java/android/view/MotionEvent.java
@@ -619,6 +619,11 @@
      * indicates that the major axis of contact is oriented to the left.
      * The full range is from -PI/2 radians (finger pointing fully left) to PI/2 radians
      * (finger pointing fully right).
+     * <li>For a stylus, the orientation indicates the direction in which the stylus
+     * is pointing in relation to the vertical axis of the current orientation of the screen.
+     * The range is from -PI radians to PI radians, where 0 is pointing up,
+     * -PI/2 radians is pointing left, -PI or PI radians is pointing down, and PI/2 radians
+     * is pointing right.  See also {@link #AXIS_TILT}.
      * </ul>
      * </p>
      *
@@ -883,8 +888,8 @@
      * <p>
      * <ul>
      * <li>For a stylus, reports the distance of the stylus from the screen.
-     * The value is nominally measured in millimeters where 0.0 indicates direct contact
-     * and larger values indicate increasing distance from the surface.
+     * A value of 0.0 indicates direct contact and larger values indicate increasing
+     * distance from the surface.
      * </ul>
      * </p>
      *
@@ -896,6 +901,24 @@
     public static final int AXIS_DISTANCE = 24;
 
     /**
+     * Axis constant: Tilt axis of a motion event.
+     * <p>
+     * <ul>
+     * <li>For a stylus, reports the tilt angle of the stylus in radians where
+     * 0 radians indicates that the stylus is being held perpendicular to the
+     * surface, and PI/2 radians indicates that the stylus is being held flat
+     * against the surface.
+     * </ul>
+     * </p>
+     *
+     * @see #getAxisValue(int, int)
+     * @see #getHistoricalAxisValue(int, int, int)
+     * @see MotionEvent.PointerCoords#getAxisValue(int, int)
+     * @see InputDevice#getMotionRange
+     */
+    public static final int AXIS_TILT = 25;
+
+    /**
      * Axis constant: Generic 1 axis of a motion event.
      * The interpretation of a generic axis is device-specific.
      *
@@ -1104,6 +1127,7 @@
         names.append(AXIS_GAS, "AXIS_GAS");
         names.append(AXIS_BRAKE, "AXIS_BRAKE");
         names.append(AXIS_DISTANCE, "AXIS_DISTANCE");
+        names.append(AXIS_TILT, "AXIS_TILT");
         names.append(AXIS_GENERIC_1, "AXIS_GENERIC_1");
         names.append(AXIS_GENERIC_2, "AXIS_GENERIC_2");
         names.append(AXIS_GENERIC_3, "AXIS_GENERIC_3");
diff --git a/core/java/android/view/VolumePanel.java b/core/java/android/view/VolumePanel.java
index cb85e5f..e1aa9a4 100644
--- a/core/java/android/view/VolumePanel.java
+++ b/core/java/android/view/VolumePanel.java
@@ -101,6 +101,8 @@
     /** Dialog's content view */
     private final View mView;
 
+    /** The visible portion of the volume overlay */
+    private final ViewGroup mPanel;
     /** Contains the sliders and their touchable icons */
     private final ViewGroup mSliderGroup;
     /** The button that expands the dialog to show all sliders */
@@ -173,10 +175,23 @@
         View view = mView = inflater.inflate(R.layout.volume_adjust, null);
         mView.setOnTouchListener(new View.OnTouchListener() {
             public boolean onTouch(View v, MotionEvent event) {
+                // Dismiss the dialog if the user touches outside the visible area. This is not
+                // handled by the usual dialog dismissing code because there is a region above
+                // the panel (marginTop) that is still within the dialog.
+                if (event.getAction() == MotionEvent.ACTION_DOWN) {
+                    int x = (int) event.getX();
+                    int y = (int) event.getY();
+                    if (x < mPanel.getLeft() || x > mPanel.getRight() || y < mPanel.getTop()
+                            || y > mPanel.getBottom()) {
+                        forceTimeout();
+                        return true;
+                    }
+                }
                 resetTimeout();
                 return true;
             }
         });
+        mPanel = (ViewGroup) mView.findViewById(R.id.visible_panel);
         mSliderGroup = (ViewGroup) mView.findViewById(R.id.slider_group);
         mMoreButton = (ImageView) mView.findViewById(R.id.expand_button);
         mDivider = (ImageView) mView.findViewById(R.id.expand_button_divider);
@@ -639,6 +654,11 @@
         sendMessageDelayed(obtainMessage(MSG_TIMEOUT), TIMEOUT_DELAY);
     }
 
+    private void forceTimeout() {
+        removeMessages(MSG_TIMEOUT);
+        sendMessage(obtainMessage(MSG_TIMEOUT));
+    }
+
     public void onProgressChanged(SeekBar seekBar, int progress,
             boolean fromUser) {
         final Object tag = seekBar.getTag();
diff --git a/core/java/android/widget/SearchView.java b/core/java/android/widget/SearchView.java
index 99f2765..f230031 100644
--- a/core/java/android/widget/SearchView.java
+++ b/core/java/android/widget/SearchView.java
@@ -745,7 +745,7 @@
     }
 
     @Override
-    public void onDetachedFromWindow() {
+    protected void onDetachedFromWindow() {
         removeCallbacks(mUpdateDrawableStateRunnable);
         super.onDetachedFromWindow();
     }
diff --git a/core/java/com/android/internal/app/ActionBarImpl.java b/core/java/com/android/internal/app/ActionBarImpl.java
index 31360e1..cfecca5 100644
--- a/core/java/com/android/internal/app/ActionBarImpl.java
+++ b/core/java/com/android/internal/app/ActionBarImpl.java
@@ -111,6 +111,9 @@
                 mContentView.setTranslationY(0);
                 mContainerView.setTranslationY(0);
             }
+            if (mSplitView != null && mContextDisplayMode == CONTEXT_DISPLAY_SPLIT) {
+                mSplitView.setVisibility(View.GONE);
+            }
             mContainerView.setVisibility(View.GONE);
             mContainerView.setTransitioning(false);
             mCurrentShowAnim = null;
@@ -540,6 +543,7 @@
             }
             if (mSplitView != null && mContextDisplayMode == CONTEXT_DISPLAY_SPLIT) {
                 mSplitView.setAlpha(0);
+                mSplitView.setVisibility(View.VISIBLE);
                 b.with(ObjectAnimator.ofFloat(mSplitView, "alpha", 1));
             }
             anim.addListener(mShowListener);
diff --git a/core/java/com/android/internal/view/menu/ActionMenuPresenter.java b/core/java/com/android/internal/view/menu/ActionMenuPresenter.java
index dd75925..df4243a 100644
--- a/core/java/com/android/internal/view/menu/ActionMenuPresenter.java
+++ b/core/java/com/android/internal/view/menu/ActionMenuPresenter.java
@@ -19,6 +19,7 @@
 import com.android.internal.view.menu.ActionMenuView.ActionMenuChildView;
 
 import android.content.Context;
+import android.content.res.Configuration;
 import android.content.res.Resources;
 import android.os.Parcel;
 import android.os.Parcelable;
@@ -109,6 +110,16 @@
         mScrapActionButtonView = null;
     }
 
+    public void onConfigurationChanged(Configuration newConfig) {
+        if (!mMaxItemsSet) {
+            mMaxItems = mContext.getResources().getInteger(
+                    com.android.internal.R.integer.max_action_buttons);
+            if (mMenu != null) {
+                mMenu.onItemsChanged(true);
+            }
+        }
+    }
+
     public void setWidthLimit(int width, boolean strict) {
         mWidthLimit = width;
         mStrictWidthLimit = strict;
diff --git a/core/java/com/android/internal/widget/ActionBarContextView.java b/core/java/com/android/internal/widget/ActionBarContextView.java
index 5645a6f..45d38ae 100644
--- a/core/java/com/android/internal/widget/ActionBarContextView.java
+++ b/core/java/com/android/internal/widget/ActionBarContextView.java
@@ -25,6 +25,7 @@
 import android.animation.AnimatorSet;
 import android.animation.ObjectAnimator;
 import android.content.Context;
+import android.content.res.Configuration;
 import android.content.res.TypedArray;
 import android.graphics.drawable.Drawable;
 import android.text.TextUtils;
@@ -92,6 +93,14 @@
         a.recycle();
     }
 
+    @Override
+    public void onConfigurationChanged(Configuration newConfig) {
+        super.onConfigurationChanged(newConfig);
+        if (mActionMenuPresenter != null) {
+            mActionMenuPresenter.onConfigurationChanged(newConfig);
+        }
+    }
+
     public void setHeight(int height) {
         mContentHeight = height;
     }
diff --git a/core/java/com/android/internal/widget/ActionBarView.java b/core/java/com/android/internal/widget/ActionBarView.java
index 792766b..28181ba 100644
--- a/core/java/com/android/internal/widget/ActionBarView.java
+++ b/core/java/com/android/internal/widget/ActionBarView.java
@@ -34,14 +34,12 @@
 import android.content.pm.PackageManager;
 import android.content.pm.PackageManager.NameNotFoundException;
 import android.content.res.Configuration;
-import android.content.res.Resources;
 import android.content.res.TypedArray;
 import android.graphics.drawable.Drawable;
 import android.os.Parcel;
 import android.os.Parcelable;
 import android.text.TextUtils;
 import android.util.AttributeSet;
-import android.util.DisplayMetrics;
 import android.util.Log;
 import android.view.CollapsibleActionView;
 import android.view.Gravity;
@@ -52,7 +50,6 @@
 import android.view.ViewGroup;
 import android.view.ViewParent;
 import android.view.Window;
-import android.view.ViewGroup.LayoutParams;
 import android.widget.AdapterView;
 import android.widget.FrameLayout;
 import android.widget.ImageView;
@@ -88,7 +85,7 @@
     private int mContentHeight;
 
     private int mNavigationMode;
-    private int mDisplayOptions = ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP;
+    private int mDisplayOptions = -1;
     private CharSequence mTitle;
     private CharSequence mSubtitle;
     private Drawable mIcon;
@@ -262,6 +259,9 @@
                 com.android.internal.R.attr.actionBarStyle, 0);
         setContentHeight(a.getLayoutDimension(R.styleable.ActionBar_height, 0));
         a.recycle();
+        if (mActionMenuPresenter != null) {
+            mActionMenuPresenter.onConfigurationChanged(newConfig);
+        }
     }
 
     @Override
@@ -514,7 +514,7 @@
     }
 
     public void setDisplayOptions(int options) {
-        final int flagsChanged = options ^ mDisplayOptions;
+        final int flagsChanged = mDisplayOptions == -1 ? -1 : options ^ mDisplayOptions;
         mDisplayOptions = options;
 
         if ((flagsChanged & DISPLAY_RELAYOUT_MASK) != 0) {
@@ -737,8 +737,9 @@
         }
 
         addView(mTitleLayout);
-        if (mExpandedActionView != null) {
-            // Don't show while in expanded mode
+        if (mExpandedActionView != null ||
+                (TextUtils.isEmpty(mTitle) && TextUtils.isEmpty(mSubtitle))) {
+            // Don't show while in expanded mode or with empty text
             mTitleLayout.setVisibility(GONE);
         }
     }
diff --git a/core/java/com/android/internal/widget/PointerLocationView.java b/core/java/com/android/internal/widget/PointerLocationView.java
index bf1c637..158291b 100644
--- a/core/java/com/android/internal/widget/PointerLocationView.java
+++ b/core/java/com/android/internal/widget/PointerLocationView.java
@@ -46,6 +46,7 @@
         
         // Most recent coordinates.
         private PointerCoords mCoords = new PointerCoords();
+        private int mToolType;
         
         // Most recent velocity.
         private float mXVelocity;
@@ -88,7 +89,7 @@
     private int mMaxNumPointers;
     private int mActivePointerId;
     private final ArrayList<PointerState> mPointers = new ArrayList<PointerState>();
-    private final PointerCoords mHoverCoords = new PointerCoords();
+    private final PointerCoords mTempCoords = new PointerCoords();
     
     private final VelocityTracker mVelocity;
     
@@ -306,22 +307,66 @@
                             ps.mCoords.toolMinor, ps.mCoords.orientation, mPaint);
 
                     // Draw the orientation arrow.
+                    float arrowSize = ps.mCoords.toolMajor * 0.7f;
+                    if (arrowSize < 20) {
+                        arrowSize = 20;
+                    }
                     mPaint.setARGB(255, pressureLevel, 255, 0);
-                    float orientationVectorX = (float) (Math.sin(-ps.mCoords.orientation)
-                            * ps.mCoords.toolMajor * 0.7);
-                    float orientationVectorY = (float) (Math.cos(-ps.mCoords.orientation)
-                            * ps.mCoords.toolMajor * 0.7);
-                    canvas.drawLine(
-                            ps.mCoords.x - orientationVectorX, ps.mCoords.y - orientationVectorY,
-                            ps.mCoords.x + orientationVectorX, ps.mCoords.y + orientationVectorY,
-                            mPaint);
+                    float orientationVectorX = (float) (Math.sin(ps.mCoords.orientation)
+                            * arrowSize);
+                    float orientationVectorY = (float) (-Math.cos(ps.mCoords.orientation)
+                            * arrowSize);
+                    if (ps.mToolType == MotionEvent.TOOL_TYPE_STYLUS
+                            || ps.mToolType == MotionEvent.TOOL_TYPE_ERASER) {
+                        // Show full circle orientation.
+                        canvas.drawLine(ps.mCoords.x, ps.mCoords.y,
+                                ps.mCoords.x + orientationVectorX,
+                                ps.mCoords.y + orientationVectorY,
+                                mPaint);
+                    } else {
+                        // Show half circle orientation.
+                        canvas.drawLine(
+                                ps.mCoords.x - orientationVectorX,
+                                ps.mCoords.y - orientationVectorY,
+                                ps.mCoords.x + orientationVectorX,
+                                ps.mCoords.y + orientationVectorY,
+                                mPaint);
+                    }
+
+                    // Draw the tilt point along the orientation arrow.
+                    float tiltScale = (float) Math.sin(
+                            ps.mCoords.getAxisValue(MotionEvent.AXIS_TILT));
+                    canvas.drawCircle(
+                            ps.mCoords.x + orientationVectorX * tiltScale,
+                            ps.mCoords.y + orientationVectorY * tiltScale,
+                            3.0f, mPaint);
                 }
             }
         }
     }
-    
-    private void logPointerCoords(int action, int index, MotionEvent.PointerCoords coords, int id,
-            int toolType, int buttonState) {
+
+    private void logMotionEvent(String type, MotionEvent event) {
+        final int action = event.getAction();
+        final int N = event.getHistorySize();
+        final int NI = event.getPointerCount();
+        for (int historyPos = 0; historyPos < N; historyPos++) {
+            for (int i = 0; i < NI; i++) {
+                final int id = event.getPointerId(i);
+                event.getHistoricalPointerCoords(i, historyPos, mTempCoords);
+                logCoords(type, action, i, mTempCoords, id,
+                        event.getToolType(i), event.getButtonState());
+            }
+        }
+        for (int i = 0; i < NI; i++) {
+            final int id = event.getPointerId(i);
+            event.getPointerCoords(i, mTempCoords);
+            logCoords(type, action, i, mTempCoords, id,
+                    event.getToolType(i), event.getButtonState());
+        }
+    }
+
+    private void logCoords(String type, int action, int index,
+            MotionEvent.PointerCoords coords, int id, int toolType, int buttonState) {
         final String prefix;
         switch (action & MotionEvent.ACTION_MASK) {
             case MotionEvent.ACTION_DOWN:
@@ -373,7 +418,7 @@
         }
 
         Log.i(TAG, mText.clear()
-                .append("Pointer ").append(id + 1)
+                .append(type).append(" id ").append(id + 1)
                 .append(": ")
                 .append(prefix)
                 .append(" (").append(coords.x, 3).append(", ").append(coords.y, 3)
@@ -385,6 +430,9 @@
                 .append(" ToolMinor=").append(coords.toolMinor, 3)
                 .append(" Orientation=").append((float)(coords.orientation * 180 / Math.PI), 1)
                 .append("deg")
+                .append(" Tilt=").append((float)(
+                        coords.getAxisValue(MotionEvent.AXIS_TILT) * 180 / Math.PI), 1)
+                .append("deg")
                 .append(" Distance=").append(coords.getAxisValue(MotionEvent.AXIS_DISTANCE), 1)
                 .append(" VScroll=").append(coords.getAxisValue(MotionEvent.AXIS_VSCROLL), 1)
                 .append(" HScroll=").append(coords.getAxisValue(MotionEvent.AXIS_HSCROLL), 1)
@@ -445,10 +493,10 @@
                 for (int i = 0; i < NI; i++) {
                     final int id = event.getPointerId(i);
                     final PointerState ps = mCurDown ? mPointers.get(id) : null;
-                    final PointerCoords coords = ps != null ? ps.mCoords : mHoverCoords;
+                    final PointerCoords coords = ps != null ? ps.mCoords : mTempCoords;
                     event.getHistoricalPointerCoords(i, historyPos, coords);
                     if (mPrintCoords) {
-                        logPointerCoords(action, i, coords, id,
+                        logCoords("Pointer", action, i, coords, id,
                                 event.getToolType(i), event.getButtonState());
                     }
                     if (ps != null) {
@@ -459,16 +507,17 @@
             for (int i = 0; i < NI; i++) {
                 final int id = event.getPointerId(i);
                 final PointerState ps = mCurDown ? mPointers.get(id) : null;
-                final PointerCoords coords = ps != null ? ps.mCoords : mHoverCoords;
+                final PointerCoords coords = ps != null ? ps.mCoords : mTempCoords;
                 event.getPointerCoords(i, coords);
                 if (mPrintCoords) {
-                    logPointerCoords(action, i, coords, id,
+                    logCoords("Pointer", action, i, coords, id,
                             event.getToolType(i), event.getButtonState());
                 }
                 if (ps != null) {
                     ps.addTrace(coords.x, coords.y);
                     ps.mXVelocity = mVelocity.getXVelocity(id);
                     ps.mYVelocity = mVelocity.getYVelocity(id);
+                    ps.mToolType = event.getToolType(i);
                 }
             }
 
@@ -515,11 +564,11 @@
         if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
             addPointerEvent(event);
         } else if ((source & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
-            Log.i(TAG, "Joystick: " + event);
+            logMotionEvent("Joystick", event);
         } else if ((source & InputDevice.SOURCE_CLASS_POSITION) != 0) {
-            Log.i(TAG, "Position: " + event);
+            logMotionEvent("Position", event);
         } else {
-            Log.i(TAG, "Generic: " + event);
+            logMotionEvent("Generic", event);
         }
         return true;
     }
@@ -563,7 +612,7 @@
 
     @Override
     public boolean onTrackballEvent(MotionEvent event) {
-        Log.i(TAG, "Trackball: " + event);
+        logMotionEvent("Trackball", event);
         return true;
     }
     
diff --git a/core/jni/android/graphics/Canvas.cpp b/core/jni/android/graphics/Canvas.cpp
index f8be136..b01dcd8 100644
--- a/core/jni/android/graphics/Canvas.cpp
+++ b/core/jni/android/graphics/Canvas.cpp
@@ -770,11 +770,7 @@
         value->computeValues(paint, textArray, start, count, end, flags);
 #endif
 
-#if DEBUG_GLYPHS
-        logGlyphs(value);
-#endif
-
-    doDrawGlyphs(canvas, value->getGlyphs(), 0, value->getGlyphsCount(),
+        doDrawGlyphs(canvas, value->getGlyphs(), 0, value->getGlyphsCount(),
             x, y, flags, paint);
     }
 
@@ -794,10 +790,6 @@
         value->computeValues(paint, textArray, start, count, contextCount, flags);
 #endif
 
-#if DEBUG_GLYPHS
-        logGlyphs(value);
-#endif
-
         doDrawGlyphs(canvas, value->getGlyphs(), 0, value->getGlyphsCount(),
                 x, y, flags, paint);
     }
diff --git a/core/jni/android/graphics/TextLayout.h b/core/jni/android/graphics/TextLayout.h
index d197d04..9bb1b92 100644
--- a/core/jni/android/graphics/TextLayout.h
+++ b/core/jni/android/graphics/TextLayout.h
@@ -64,14 +64,6 @@
     kDirection_Mask = 0x1
 };
 
-static void logGlyphs(sp<TextLayoutCacheValue> value) {
-    if (value == NULL) return;
-    LOGD("Got glyphs - count=%d", value->getGlyphsCount());
-    for (size_t i = 0; i < value->getGlyphsCount(); i++) {
-        LOGD("      glyphs[%d]=%d", i, value->getGlyphs()[i]);
-    }
-}
-
 class TextLayout {
 public:
 
diff --git a/core/jni/android/graphics/TextLayoutCache.cpp b/core/jni/android/graphics/TextLayoutCache.cpp
index 89440c9..f04c5eb5 100644
--- a/core/jni/android/graphics/TextLayoutCache.cpp
+++ b/core/jni/android/graphics/TextLayoutCache.cpp
@@ -554,6 +554,15 @@
 #endif
 }
 
+static void logGlyphs(HB_ShaperItem shaperItem) {
+    LOGD("Got glyphs - count=%d", shaperItem.num_glyphs);
+    for (size_t i = 0; i < shaperItem.num_glyphs; i++) {
+        LOGD("      glyphs[%d]=%d - offset.x=%f offset.y=%f", i, shaperItem.glyphs[i],
+                HBFixedToFloat(shaperItem.offsets[i].x),
+                HBFixedToFloat(shaperItem.offsets[i].y));
+    }
+}
+
 void TextLayoutCacheValue::computeRunValuesWithHarfbuzz(SkPaint* paint, const UChar* chars,
         size_t start, size_t count, size_t contextCount, bool isRTL,
         jfloat* outAdvances, jfloat* outTotalAdvance,
@@ -570,6 +579,8 @@
             shaperItem.kerning_applied);
     LOGD("         -- string= '%s'", String8(chars + start, count).string());
     LOGD("         -- isDevKernText=%d", paint->isDevKernText());
+
+    logGlyphs(shaperItem);
 #endif
 
     if (shaperItem.advances == NULL || shaperItem.num_glyphs == 0) {
diff --git a/core/jni/android_view_GLES20Canvas.cpp b/core/jni/android_view_GLES20Canvas.cpp
index 80c79fd..fb5e5fe 100644
--- a/core/jni/android_view_GLES20Canvas.cpp
+++ b/core/jni/android_view_GLES20Canvas.cpp
@@ -472,9 +472,6 @@
     value = new TextLayoutCacheValue();
     value->computeValues(paint, text, 0, count, count, flags);
 #endif
-#if DEBUG_GLYPHS
-    logGlyphs(value);
-#endif
     const jchar* glyphArray = value->getGlyphs();
     int glyphCount = value->getGlyphsCount();
     int bytesCount = glyphCount * sizeof(jchar);
@@ -505,9 +502,6 @@
     value = new TextLayoutCacheValue();
     value->computeValues(paint, text, start, count, contextCount, flags);
 #endif
-#if DEBUG_GLYPHS
-    logGlyphs(value);
-#endif
     const jchar* glyphArray = value->getGlyphs();
     int glyphCount = value->getGlyphsCount();
     int bytesCount = glyphCount * sizeof(jchar);
diff --git a/core/res/res/drawable-hdpi/overscroll_glow.png b/core/res/res/drawable-hdpi/overscroll_glow.png
index 5963c00..8f0c2cb 100644
--- a/core/res/res/drawable-hdpi/overscroll_glow.png
+++ b/core/res/res/drawable-hdpi/overscroll_glow.png
Binary files differ
diff --git a/core/res/res/drawable-mdpi/overscroll_glow.png b/core/res/res/drawable-mdpi/overscroll_glow.png
index e63e465..8389ef4 100644
--- a/core/res/res/drawable-mdpi/overscroll_glow.png
+++ b/core/res/res/drawable-mdpi/overscroll_glow.png
Binary files differ
diff --git a/core/res/res/drawable-xhdpi/overscroll_glow.png b/core/res/res/drawable-xhdpi/overscroll_glow.png
index 227565e..75c3eb4 100644
--- a/core/res/res/drawable-xhdpi/overscroll_glow.png
+++ b/core/res/res/drawable-xhdpi/overscroll_glow.png
Binary files differ
diff --git a/core/res/res/layout/volume_adjust.xml b/core/res/res/layout/volume_adjust.xml
index 7303003..ea4e1f9 100644
--- a/core/res/res/layout/volume_adjust.xml
+++ b/core/res/res/layout/volume_adjust.xml
@@ -18,6 +18,7 @@
     android:layout_width="480dp"
     android:layout_height="wrap_content">
     <LinearLayout
+        android:id="@+id/visible_panel"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_marginTop="80dp"
diff --git a/core/res/res/values-port/dimens.xml b/core/res/res/values-w360dp/dimens.xml
similarity index 94%
copy from core/res/res/values-port/dimens.xml
copy to core/res/res/values-w360dp/dimens.xml
index bf0a342..0f5d656 100644
--- a/core/res/res/values-port/dimens.xml
+++ b/core/res/res/values-w360dp/dimens.xml
@@ -20,5 +20,5 @@
     <!-- The maximum number of action buttons that should be permitted within
          an action bar/action mode. This will be used to determine how many
          showAsAction="ifRoom" items can fit. "always" items can override this. -->
-    <integer name="max_action_buttons">2</integer>
+    <integer name="max_action_buttons">3</integer>
 </resources>
diff --git a/core/res/res/values-port/dimens.xml b/core/res/res/values-w500dp/dimens.xml
similarity index 94%
rename from core/res/res/values-port/dimens.xml
rename to core/res/res/values-w500dp/dimens.xml
index bf0a342..68841ca 100644
--- a/core/res/res/values-port/dimens.xml
+++ b/core/res/res/values-w500dp/dimens.xml
@@ -20,5 +20,5 @@
     <!-- The maximum number of action buttons that should be permitted within
          an action bar/action mode. This will be used to determine how many
          showAsAction="ifRoom" items can fit. "always" items can override this. -->
-    <integer name="max_action_buttons">2</integer>
+    <integer name="max_action_buttons">4</integer>
 </resources>
diff --git a/core/res/res/values-port/dimens.xml b/core/res/res/values-w600dp/dimens.xml
similarity index 94%
copy from core/res/res/values-port/dimens.xml
copy to core/res/res/values-w600dp/dimens.xml
index bf0a342..83c45b5 100644
--- a/core/res/res/values-port/dimens.xml
+++ b/core/res/res/values-w600dp/dimens.xml
@@ -20,5 +20,5 @@
     <!-- The maximum number of action buttons that should be permitted within
          an action bar/action mode. This will be used to determine how many
          showAsAction="ifRoom" items can fit. "always" items can override this. -->
-    <integer name="max_action_buttons">2</integer>
+    <integer name="max_action_buttons">5</integer>
 </resources>
diff --git a/core/res/res/values/arrays.xml b/core/res/res/values/arrays.xml
index fe5ace8..04e510b 100644
--- a/core/res/res/values/arrays.xml
+++ b/core/res/res/values/arrays.xml
@@ -236,11 +236,8 @@
        <item>@drawable/spinner_dropdown_background_up</item>
        <item>@drawable/spinner_focused_holo_dark</item>
        <item>@drawable/spinner_focused_holo_light</item>
-       <item>@drawable/spinner_normal</item>
-       <item>@drawable/spinner_press</item>
        <item>@drawable/spinner_pressed_holo_dark</item>
        <item>@drawable/spinner_pressed_holo_light</item>
-       <item>@drawable/spinner_select</item>
        <item>@drawable/cab_background_bottom_holo_dark</item>
        <item>@drawable/cab_background_top_holo_light</item>
        <item>@drawable/cab_background_bottom_holo_light</item>
diff --git a/core/res/res/values/dimens.xml b/core/res/res/values/dimens.xml
index 83e86939..c522c1e 100644
--- a/core/res/res/values/dimens.xml
+++ b/core/res/res/values/dimens.xml
@@ -28,7 +28,7 @@
     <!-- The maximum number of action buttons that should be permitted within
          an action bar/action mode. This will be used to determine how many
          showAsAction="ifRoom" items can fit. "always" items can override this. -->
-    <integer name="max_action_buttons">3</integer>
+    <integer name="max_action_buttons">2</integer>
     <dimen name="toast_y_offset">64dip</dimen>
     <!-- Height of the status bar -->
     <dimen name="status_bar_height">25dip</dimen>
diff --git a/docs/html/guide/topics/graphics/opengl.jd b/docs/html/guide/topics/graphics/opengl.jd
index 2edc63d..b750858 100644
--- a/docs/html/guide/topics/graphics/opengl.jd
+++ b/docs/html/guide/topics/graphics/opengl.jd
@@ -14,7 +14,7 @@
         </ol>
       <li><a href="#manifest">Declaring OpenGL Requirements</a></li>
       </li>
-      <li><a href="#coordinate-mapping">Coordinate Mapping for Drawn Objects</a>  
+      <li><a href="#coordinate-mapping">Mapping Coordinates for Drawn Objects</a>  
         <ol>
           <li><a href="#proj-es1">Projection and camera in ES 1.0</a></li>
           <li><a href="#proj-es1">Projection and camera in ES 2.0</a></li>
@@ -197,9 +197,9 @@
   installed on devices that do not support OpenGL ES 2.0.</p>
   </li>
   <li><strong>Texture compression requirements</strong> - If your application uses texture
-compression formats that are not supported by all devices, you must declare them in your manifest
-file using <a href="{@docRoot}guide/topics/manifest/supports-gl-texture-element.html">
-{@code &lt;supports-gl-texture&gt;}</a>. For more information about available texture compression
+compression formats, you must declare the formats your application supports in your manifest file
+using <a href="{@docRoot}guide/topics/manifest/supports-gl-texture-element.html">{@code
+&lt;supports-gl-texture&gt;}</a>. For more information about available texture compression
 formats, see <a href="#textures">Texture compression support</a>. 
 
 <p>Declaring texture compression requirements in your manifest hides your application from users
@@ -212,7 +212,7 @@
 </ul>
 
 
-<h2 id="coordinate-mapping">Coordinate Mapping for Drawn Objects</h2>
+<h2 id="coordinate-mapping">Mapping Coordinates for Drawn Objects</h2>
 
 <p>One of the basic problems in displaying graphics on Android devices is that their screens can
 vary in size and shape. OpenGL assumes a square, uniform coordinate system and, by default, happily
@@ -241,9 +241,11 @@
 <ol>
 <li><strong>Projection matrix</strong> - Create a projection matrix using the geometry of the
 device screen in order to recalculate object coordinates so they are drawn with correct proportions.
-The following example code demonstrates how to modify the {@code onSurfaceChanged()} method of a
-{@link android.opengl.GLSurfaceView.Renderer} implementation to create a projection matrix based on
-the screen's aspect ratio and apply it to the OpenGL rendering environment.
+The following example code demonstrates how to modify the {@link
+android.opengl.GLSurfaceView.Renderer#onSurfaceChanged(javax.microedition.khronos.opengles.GL10,
+int, int) onSurfaceChanged()} method of a {@link android.opengl.GLSurfaceView.Renderer}
+implementation to create a projection matrix based on the screen's aspect ratio and apply it to the
+OpenGL rendering environment.
 
 <pre>
   public void onSurfaceChanged(GL10 gl, int width, int height) {
@@ -260,11 +262,13 @@
 
 <li><strong>Camera transformation matrix</strong> - Once you have adjusted the coordinate system
 using a projection matrix, you must also apply a camera view. The following example code shows how
-to modify the {@code onDrawFrame()} method of a {@link android.opengl.GLSurfaceView.Renderer}
-implementation to apply a model view and use the {@link
-android.opengl.GLU#gluLookAt(javax.microedition.khronos.opengles.GL10, float, float, float, float,
-float, float, float, float, float) GLU.gluLookAt()} utility to create a viewing tranformation which
-simulates a camera position.
+to modify the {@link        
+android.opengl.GLSurfaceView.Renderer#onDrawFrame(javax.microedition.khronos.opengles.GL10)
+onDrawFrame()} method of a {@link android.opengl.GLSurfaceView.Renderer}
+implementation to apply a model view and use the
+{@link android.opengl.GLU#gluLookAt(javax.microedition.khronos.opengles.GL10, float, float, float,
+float, float, float, float, float, float) GLU.gluLookAt()} utility to create a viewing tranformation
+which simulates a camera position.
 
 <pre>
     public void onDrawFrame(GL10 gl) {
@@ -320,8 +324,10 @@
 </li>
 <li><strong>Access the shader matrix</strong> - After creating a hook in your vertex shaders to
 apply projection and camera view, you can then access that variable to apply projection and
-camera viewing matrices. The following code shows how to modify the {@code onSurfaceCreated()}
-method of a {@link android.opengl.GLSurfaceView.Renderer} implementation to access the matrix
+camera viewing matrices. The following code shows how to modify the {@link
+android.opengl.GLSurfaceView.Renderer#onSurfaceCreated(javax.microedition.khronos.opengles.GL10,
+javax.microedition.khronos.egl.EGLConfig) onSurfaceCreated()} method of a {@link
+android.opengl.GLSurfaceView.Renderer} implementation to access the matrix
 variable defined in the vertex shader above.
 
 <pre>
@@ -334,9 +340,13 @@
 </li>
 <li><strong>Create projection and camera viewing matrices</strong> - Generate the projection and
 viewing matrices to be applied the graphic objects. The following example code shows how to modify
-the {@code onSurfaceCreated()} and {@code onSurfaceChanged()} methods of a {@link
-android.opengl.GLSurfaceView.Renderer} implementation to create camera view matrix and a projection
-matrix based on the screen aspect ratio of the device.
+the {@link    
+android.opengl.GLSurfaceView.Renderer#onSurfaceCreated(javax.microedition.khronos.opengles.GL10,
+javax.microedition.khronos.egl.EGLConfig) onSurfaceCreated()} and {@link
+android.opengl.GLSurfaceView.Renderer#onSurfaceChanged(javax.microedition.khronos.opengles.GL10,
+int, int) onSurfaceChanged()} methods of a {@link android.opengl.GLSurfaceView.Renderer}
+implementation to create camera view matrix and a projection matrix based on the screen aspect ratio
+of the device.
 
 <pre>
     public void onSurfaceCreated(GL10 unused, EGLConfig config) {
@@ -358,9 +368,11 @@
 
 <li><strong>Apply projection and camera viewing matrices</strong> - To apply the projection and
 camera view transformations, multiply the matrices together and then set them into the vertex
-shader. The following example code shows how modify the {@code onDrawFrame()} method of a {@link
-android.opengl.GLSurfaceView.Renderer} implementation to combine the projection matrix and camera
-view created in the code above and then apply it to the graphic objects to be rendered by OpenGL.
+shader. The following example code shows how modify the {@link
+android.opengl.GLSurfaceView.Renderer#onDrawFrame(javax.microedition.khronos.opengles.GL10)
+onDrawFrame()} method of a {@link android.opengl.GLSurfaceView.Renderer} implementation to combine
+the projection matrix and camera view created in the code above and then apply it to the graphic
+objects to be rendered by OpenGL.
   
 <pre>
     public void onDrawFrame(GL10 unused) {
diff --git a/docs/html/resources/tutorials/opengl/opengl-es10.jd b/docs/html/resources/tutorials/opengl/opengl-es10.jd
index 40304fd..3570766 100644
--- a/docs/html/resources/tutorials/opengl/opengl-es10.jd
+++ b/docs/html/resources/tutorials/opengl/opengl-es10.jd
@@ -342,8 +342,8 @@
 system onto your typically non-square screen. To solve this problem, you can apply an OpenGL
 projection mode and camera view (eye point) to transform the coordinates of your graphic objects
 so they have the correct proportions on any display. For more information about OpenGL coordinate
-mapping, see <a href="{@docRoot}guide/topics/graphics/opengl.html#coordinate-mapping">Coordinate
-Mapping for Drawn Objects</a>.</p>
+mapping, see <a href="{@docRoot}guide/topics/graphics/opengl.html#coordinate-mapping">Mapping
+Coordinates for Drawn Objects</a>.</p>
 
 <p>To apply projection and camera view transformations to your triangle:
 </p>
diff --git a/docs/html/resources/tutorials/opengl/opengl-es20.jd b/docs/html/resources/tutorials/opengl/opengl-es20.jd
index 439f7d5..889dd50 100644
--- a/docs/html/resources/tutorials/opengl/opengl-es20.jd
+++ b/docs/html/resources/tutorials/opengl/opengl-es20.jd
@@ -422,8 +422,8 @@
 system onto your typically non-square screen. To solve this problem, you can apply an OpenGL
 projection mode and camera view (eye point) to transform the coordinates of your graphic objects
 so they have the correct proportions on any display. For more information about OpenGL coordinate
-mapping, see <a href="{@docRoot}guide/topics/graphics/opengl.html#coordinate-mapping">Coordinate
-Mapping for Drawn Objects</a>.</p>
+mapping, see <a href="{@docRoot}guide/topics/graphics/opengl.html#coordinate-mapping">Mapping
+Coordinates for Drawn Objects</a>.</p>
 
 <p>To apply projection and camera view transformations to your triangle:
 </p>
diff --git a/include/media/MediaProfiles.h b/include/media/MediaProfiles.h
index 69d5001..4b023d1 100644
--- a/include/media/MediaProfiles.h
+++ b/include/media/MediaProfiles.h
@@ -140,6 +140,19 @@
     int getVideoEditorCapParamByName(const char *name) const;
 
     /**
+     * Returns the value for the given param name for the video editor export codec format
+     * param or -1 if error.
+     * Supported param name are:
+     * videoeditor.export.profile - export video profile
+     * videoeditor.export.level - export video level
+     * Supported param codec are:
+     * 1 for h263
+     * 2 for h264
+     * 3 for mpeg4
+     */
+    int getVideoEditorExportParamByName(const char *name, int codec) const;
+
+    /**
      * Returns the audio encoders supported.
      */
     Vector<audio_encoder> getAudioEncoders() const;
@@ -332,7 +345,14 @@
         int mCameraId;
         Vector<int> mLevels;
     };
-
+    struct ExportVideoProfile {
+        ExportVideoProfile(int codec, int profile, int level)
+            :mCodec(codec),mProfile(profile),mLevel(level) {}
+        ~ExportVideoProfile() {}
+        int mCodec;
+        int mProfile;
+        int mLevel;
+    };
     struct VideoEditorCap {
         VideoEditorCap(int inFrameWidth, int inFrameHeight,
             int outFrameWidth, int outFrameHeight)
@@ -374,6 +394,7 @@
     static AudioEncoderCap* createAudioEncoderCap(const char **atts);
     static VideoEditorCap* createVideoEditorCap(
                 const char **atts, MediaProfiles *profiles);
+    static ExportVideoProfile* createExportVideoProfile(const char **atts);
 
     static CamcorderProfile* createCamcorderProfile(
                 int cameraId, const char **atts, Vector<int>& cameraIds);
@@ -418,6 +439,8 @@
     static void createDefaultImageEncodingQualityLevels(MediaProfiles *profiles);
     static void createDefaultImageDecodingMaxMemory(MediaProfiles *profiles);
     static void createDefaultVideoEditorCap(MediaProfiles *profiles);
+    static void createDefaultExportVideoProfiles(MediaProfiles *profiles);
+
     static VideoEncoderCap* createDefaultH263VideoEncoderCap();
     static VideoEncoderCap* createDefaultM4vVideoEncoderCap();
     static AudioEncoderCap* createDefaultAmrNBEncoderCap();
@@ -475,6 +498,7 @@
     RequiredProfiles *mRequiredProfileRefs;
     Vector<int>              mCameraIds;
     VideoEditorCap* mVideoEditorCap;
+    Vector<ExportVideoProfile*> mVideoEditorExportProfiles;
 };
 
 }; // namespace android
diff --git a/include/ui/KeycodeLabels.h b/include/ui/KeycodeLabels.h
index 8383957..2efe8ca 100755
--- a/include/ui/KeycodeLabels.h
+++ b/include/ui/KeycodeLabels.h
@@ -278,6 +278,8 @@
     { "WHEEL", 21 },
     { "GAS", 22 },
     { "BRAKE", 23 },
+    { "DISTANCE", 24 },
+    { "TILT", 25 },
     { "GENERIC_1", 32 },
     { "GENERIC_2", 33 },
     { "GENERIC_3", 34 },
diff --git a/libs/rs/driver/rsdBcc.cpp b/libs/rs/driver/rsdBcc.cpp
index 176dd18..44ea79c 100644
--- a/libs/rs/driver/rsdBcc.cpp
+++ b/libs/rs/driver/rsdBcc.cpp
@@ -45,6 +45,7 @@
     InvokeFunc_t *mInvokeFunctions;
     void ** mFieldAddress;
     bool * mFieldIsObject;
+    const uint32_t *mExportForEachSignatureList;
 
     const uint8_t * mScriptText;
     uint32_t mScriptTextLength;
@@ -74,6 +75,7 @@
     size_t exportFuncCount = 0;
     size_t exportVarCount = 0;
     size_t objectSlotCount = 0;
+    size_t exportForEachSignatureCount = 0;
 
     DrvScript *drv = (DrvScript *)calloc(1, sizeof(DrvScript));
     if (drv == NULL) {
@@ -153,6 +155,10 @@
         }
     }
 
+    exportForEachSignatureCount = drv->ME->getExportForEachSignatureCount();
+    rsAssert(exportForEachSignatureCount <= 1);
+    drv->mExportForEachSignatureList = drv->ME->getExportForEachSignatureList();
+
     // Copy info over to runtime
     script->mHal.info.exportedFunctionCount = drv->ME->getExportFuncCount();
     script->mHal.info.exportedVariableCount = drv->ME->getExportVarCount();
@@ -179,6 +185,7 @@
 typedef struct {
     Context *rsc;
     Script *script;
+    uint32_t sig;
     const Allocation * ain;
     Allocation * aout;
     const void * usr;
@@ -206,7 +213,7 @@
     uint32_t dimZ;
     uint32_t dimArray;
 } MTLaunchStruct;
-typedef int (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
+typedef void (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
 
 static void wc_xy(void *usr, uint32_t idx) {
     MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
@@ -214,6 +221,8 @@
     memset(&p, 0, sizeof(p));
     p.usr = mtls->usr;
     p.usr_len = mtls->usrLen;
+    RsdHal * dc = (RsdHal *)mtls->rsc->mHal.drv;
+    uint32_t sig = mtls->sig;
 
     while (1) {
         uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
@@ -234,7 +243,7 @@
             for (p.x = mtls->xStart; p.x < mtls->xEnd; p.x++) {
                 p.in = xPtrIn;
                 p.out = xPtrOut;
-                ((rs_t)mtls->script->mHal.info.root) (p.in, p.out, p.usr, p.x, p.y, 0, 0);
+                dc->mForEachLaunch[sig](&mtls->script->mHal.info.root, &p);
                 xPtrIn += mtls->eStrideIn;
                 xPtrOut += mtls->eStrideOut;
             }
@@ -248,6 +257,8 @@
     memset(&p, 0, sizeof(p));
     p.usr = mtls->usr;
     p.usr_len = mtls->usrLen;
+    RsdHal * dc = (RsdHal *)mtls->rsc->mHal.drv;
+    uint32_t sig = mtls->sig;
 
     while (1) {
         uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
@@ -265,7 +276,7 @@
         for (p.x = xStart; p.x < xEnd; p.x++) {
             p.in = xPtrIn;
             p.out = xPtrOut;
-            ((rs_t)mtls->script->mHal.info.root) (p.in, p.out, p.usr, p.x, 0, 0, 0);
+            dc->mForEachLaunch[sig](&mtls->script->mHal.info.root, &p);
             xPtrIn += mtls->eStrideIn;
             xPtrOut += mtls->eStrideOut;
         }
@@ -286,6 +297,10 @@
     MTLaunchStruct mtls;
     memset(&mtls, 0, sizeof(mtls));
 
+    DrvScript *drv = (DrvScript *)s->mHal.drv;
+    // We only support slot 0 (root) at this point in time.
+    rsAssert(slot == 0);
+    mtls.sig = drv->mExportForEachSignatureList[slot];
     if (ain) {
         mtls.dimX = ain->getType()->getDimX();
         mtls.dimY = ain->getType()->getDimY();
@@ -369,6 +384,7 @@
         memset(&p, 0, sizeof(p));
         p.usr = mtls.usr;
         p.usr_len = mtls.usrLen;
+        uint32_t sig = mtls.sig;
 
         //LOGE("launch 3");
         for (p.ar[0] = mtls.arrayStart; p.ar[0] < mtls.arrayEnd; p.ar[0]++) {
@@ -383,7 +399,7 @@
                     for (p.x = mtls.xStart; p.x < mtls.xEnd; p.x++) {
                         p.in = xPtrIn;
                         p.out = xPtrOut;
-                        ((rs_t)s->mHal.info.root) (p.in, p.out, p.usr, p.x, p.y, p.z, p.ar[0]);
+                        dc->mForEachLaunch[sig](&s->mHal.info.root, &p);
                         xPtrIn += mtls.eStrideIn;
                         xPtrOut += mtls.eStrideOut;
                     }
diff --git a/libs/rs/driver/rsdCore.cpp b/libs/rs/driver/rsdCore.cpp
index 38f6895..171d045 100644
--- a/libs/rs/driver/rsdCore.cpp
+++ b/libs/rs/driver/rsdCore.cpp
@@ -43,6 +43,7 @@
 
 static void Shutdown(Context *rsc);
 static void SetPriority(const Context *rsc, int32_t priority);
+static void initForEach(outer_foreach_t* forEachLaunch);
 
 static RsdHalFunctions FunctionTable = {
     rsdGLInit,
@@ -206,6 +207,8 @@
     rsdgThreadTLSKeyCount++;
     pthread_mutex_unlock(&rsdgInitMutex);
 
+    initForEach(dc->mForEachLaunch);
+
     dc->mTlsStruct.mContext = rsc;
     dc->mTlsStruct.mScript = NULL;
     int status = pthread_setspecific(rsdgThreadTLSKey, &dc->mTlsStruct);
@@ -287,4 +290,112 @@
 
 }
 
+static void rsdForEach17(const void *vRoot,
+        const android::renderscript::RsForEachStubParamStruct *p) {
+    typedef void (*fe)(const void *, uint32_t);
+    (*(fe*)vRoot)(p->in, p->y);
+}
+
+static void rsdForEach18(const void *vRoot,
+        const android::renderscript::RsForEachStubParamStruct *p) {
+    typedef void (*fe)(void *, uint32_t);
+    (*(fe*)vRoot)(p->out, p->y);
+}
+
+static void rsdForEach19(const void *vRoot,
+        const android::renderscript::RsForEachStubParamStruct *p) {
+    typedef void (*fe)(const void *, void *, uint32_t);
+    (*(fe*)vRoot)(p->in, p->out, p->y);
+}
+
+static void rsdForEach21(const void *vRoot,
+        const android::renderscript::RsForEachStubParamStruct *p) {
+    typedef void (*fe)(const void *, const void *, uint32_t);
+    (*(fe*)vRoot)(p->in, p->usr, p->y);
+}
+
+static void rsdForEach22(const void *vRoot,
+        const android::renderscript::RsForEachStubParamStruct *p) {
+    typedef void (*fe)(void *, const void *, uint32_t);
+    (*(fe*)vRoot)(p->out, p->usr, p->y);
+}
+
+static void rsdForEach23(const void *vRoot,
+        const android::renderscript::RsForEachStubParamStruct *p) {
+    typedef void (*fe)(const void *, void *, const void *, uint32_t);
+    (*(fe*)vRoot)(p->in, p->out, p->usr, p->y);
+}
+
+static void rsdForEach25(const void *vRoot,
+        const android::renderscript::RsForEachStubParamStruct *p) {
+    typedef void (*fe)(const void *, uint32_t, uint32_t);
+    (*(fe*)vRoot)(p->in, p->x, p->y);
+}
+
+static void rsdForEach26(const void *vRoot,
+        const android::renderscript::RsForEachStubParamStruct *p) {
+    typedef void (*fe)(void *, uint32_t, uint32_t);
+    (*(fe*)vRoot)(p->out, p->x, p->y);
+}
+
+static void rsdForEach27(const void *vRoot,
+        const android::renderscript::RsForEachStubParamStruct *p) {
+    typedef void (*fe)(const void *, void *, uint32_t, uint32_t);
+    (*(fe*)vRoot)(p->in, p->out, p->x, p->y);
+}
+
+static void rsdForEach29(const void *vRoot,
+        const android::renderscript::RsForEachStubParamStruct *p) {
+    typedef void (*fe)(const void *, const void *, uint32_t, uint32_t);
+    (*(fe*)vRoot)(p->in, p->usr, p->x, p->y);
+}
+
+static void rsdForEach30(const void *vRoot,
+        const android::renderscript::RsForEachStubParamStruct *p) {
+    typedef void (*fe)(void *, const void *, uint32_t, uint32_t);
+    (*(fe*)vRoot)(p->out, p->usr, p->x, p->y);
+}
+
+static void rsdForEach31(const void *vRoot,
+        const android::renderscript::RsForEachStubParamStruct *p) {
+    typedef void (*fe)(const void *, void *, const void *, uint32_t, uint32_t);
+    (*(fe*)vRoot)(p->in, p->out, p->usr, p->x, p->y);
+}
+
+
+static void initForEach(outer_foreach_t* forEachLaunch) {
+    rsAssert(forEachLaunch);
+    forEachLaunch[0x00] = NULL;
+    forEachLaunch[0x01] = rsdForEach31; // in
+    forEachLaunch[0x02] = rsdForEach30; //     out
+    forEachLaunch[0x03] = rsdForEach31; // in, out
+    forEachLaunch[0x04] = NULL;
+    forEachLaunch[0x05] = rsdForEach29;  // in,      usr
+    forEachLaunch[0x06] = rsdForEach30; //     out, usr
+    forEachLaunch[0x07] = rsdForEach31; // in, out, usr
+    forEachLaunch[0x08] = NULL;
+    forEachLaunch[0x09] = rsdForEach25; // in,           x
+    forEachLaunch[0x0a] = rsdForEach26; //     out,      x
+    forEachLaunch[0x0b] = rsdForEach27; // in, out,      x
+    forEachLaunch[0x0c] = NULL;
+    forEachLaunch[0x0d] = rsdForEach29; // in,      usr, x
+    forEachLaunch[0x0e] = rsdForEach30; //     out, usr, x
+    forEachLaunch[0x0f] = rsdForEach31; // in, out, usr, x
+    forEachLaunch[0x10] = NULL;
+    forEachLaunch[0x11] = rsdForEach17; // in               y
+    forEachLaunch[0x12] = rsdForEach18; //     out,         y
+    forEachLaunch[0x13] = rsdForEach19; // in, out,         y
+    forEachLaunch[0x14] = NULL;
+    forEachLaunch[0x15] = rsdForEach21; // in,      usr,    y
+    forEachLaunch[0x16] = rsdForEach22; //     out, usr,    y
+    forEachLaunch[0x17] = rsdForEach23; // in, out, usr,    y
+    forEachLaunch[0x18] = NULL;
+    forEachLaunch[0x19] = rsdForEach25; // in,           x, y
+    forEachLaunch[0x1a] = rsdForEach26; //     out,      x, y
+    forEachLaunch[0x1b] = rsdForEach27; // in, out,      x, y
+    forEachLaunch[0x1c] = NULL;
+    forEachLaunch[0x1d] = rsdForEach29; // in,      usr, x, y
+    forEachLaunch[0x1e] = rsdForEach30; //     out, usr, x, y
+    forEachLaunch[0x1f] = rsdForEach31; // in, out, usr, x, y
+}
 
diff --git a/libs/rs/driver/rsdCore.h b/libs/rs/driver/rsdCore.h
index f393b60..159b72a 100644
--- a/libs/rs/driver/rsdCore.h
+++ b/libs/rs/driver/rsdCore.h
@@ -27,6 +27,9 @@
 typedef void (* InvokeFunc_t)(void);
 typedef void (*WorkerCallback_t)(void *usr, uint32_t idx);
 
+typedef void (*outer_foreach_t)(const void *,
+    const android::renderscript::RsForEachStubParamStruct *);
+
 typedef struct RsdSymbolTableRec {
     const char * mName;
     void * mPtr;
@@ -57,6 +60,8 @@
     Workers mWorkers;
     bool mExit;
 
+    outer_foreach_t mForEachLaunch[32];
+
     ScriptTLSStruct mTlsStruct;
 
     RsdGL gl;
diff --git a/libs/rs/rsAllocation.cpp b/libs/rs/rsAllocation.cpp
index a366d49..4359d95 100644
--- a/libs/rs/rsAllocation.cpp
+++ b/libs/rs/rsAllocation.cpp
@@ -58,19 +58,7 @@
 }
 
 Allocation::~Allocation() {
-    if (mHal.state.hasReferences &&
-        (mHal.state.hasFaces || mHal.state.hasMipmaps)) {
-        LOGE("Cube/mip allocation with references unsupported, memory not cleaned up!");
-    }
-
-    uint32_t elemCount = mHal.state.dimensionX;
-    if (mHal.state.dimensionY > 1) {
-        elemCount *= mHal.state.dimensionY;
-    }
-    if (mHal.state.dimensionZ > 1) {
-        elemCount *= mHal.state.dimensionZ;
-    }
-    decRefs(getPtr(), elemCount, 0);
+    freeChildrenUnlocked();
     mRSC->mHal.funcs.allocation.destroy(mRSC, this);
 }
 
@@ -299,6 +287,19 @@
     }
 }
 
+void Allocation::freeChildrenUnlocked () {
+    decRefs(getPtr(), mHal.state.type->getSizeBytes() / mHal.state.type->getElementSizeBytes(), 0);
+}
+
+bool Allocation::freeChildren() {
+    if (mHal.state.hasReferences) {
+        incSysRef();
+        freeChildrenUnlocked();
+        return decSysRef();
+    }
+    return false;
+}
+
 void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
 }
 
diff --git a/libs/rs/rsAllocation.h b/libs/rs/rsAllocation.h
index f2589c0..67fc3b5 100644
--- a/libs/rs/rsAllocation.h
+++ b/libs/rs/rsAllocation.h
@@ -116,6 +116,7 @@
 
     void incRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
     void decRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
+    virtual bool freeChildren();
 
     void sendDirty(const Context *rsc) const;
     bool getHasGraphicsMipmaps() const {
@@ -127,6 +128,7 @@
     Vector<const Program *> mToDirtyList;
 
 private:
+    void freeChildrenUnlocked();
     Allocation(Context *rsc, const Type *, uint32_t usages, RsAllocationMipmapControl mc);
 };
 
diff --git a/libs/rs/rsContext.cpp b/libs/rs/rsContext.cpp
index 8996ad1..e3a9cf8 100644
--- a/libs/rs/rsContext.cpp
+++ b/libs/rs/rsContext.cpp
@@ -312,6 +312,7 @@
          mStateSampler.deinit(this);
          mFBOCache.deinit(this);
     }
+    ObjectBase::freeAllChildren(this);
     //LOGV("destroyWorkerThreadResources 2");
     mExit = true;
 }
diff --git a/libs/rs/rsObjectBase.cpp b/libs/rs/rsObjectBase.cpp
index f428f94..f5ced26 100644
--- a/libs/rs/rsObjectBase.cpp
+++ b/libs/rs/rsObjectBase.cpp
@@ -81,6 +81,10 @@
 void ObjectBase::preDestroy() const {
 }
 
+bool ObjectBase::freeChildren() {
+    return false;
+}
+
 bool ObjectBase::checkDelete(const ObjectBase *ref) {
     if (!ref) {
         return false;
@@ -217,6 +221,28 @@
     }
 }
 
+void ObjectBase::freeAllChildren(Context *rsc) {
+    if (rsc->props.mLogObjects) {
+        LOGV("Forcing release of all child objects.");
+    }
+
+    // This operation can be slow, only to be called during context cleanup.
+    ObjectBase * o = (ObjectBase *)rsc->mObjHead;
+    while (o) {
+        if (o->freeChildren()) {
+            // deleted ref to self and possibly others, restart from head.
+            o = (ObjectBase *)rsc->mObjHead;
+        } else {
+            o = (ObjectBase *)o->mNext;
+        }
+    }
+
+    if (rsc->props.mLogObjects) {
+        LOGV("Objects remaining.");
+        dumpAll(rsc);
+    }
+}
+
 void ObjectBase::dumpAll(Context *rsc) {
     asyncLock();
 
diff --git a/libs/rs/rsObjectBase.h b/libs/rs/rsObjectBase.h
index c7cfb0e..d9f5f3b 100644
--- a/libs/rs/rsObjectBase.h
+++ b/libs/rs/rsObjectBase.h
@@ -50,8 +50,10 @@
     void setName(const char *, uint32_t len);
 
     Context * getContext() const {return mRSC;}
+    virtual bool freeChildren();
 
     static void zeroAllUserRef(Context *rsc);
+    static void freeAllChildren(Context *rsc);
     static void dumpAll(Context *rsc);
 
     virtual void dumpLOGV(const char *prefix) const;
diff --git a/libs/rs/rsProgram.cpp b/libs/rs/rsProgram.cpp
index 33eb422e..4178aa5 100644
--- a/libs/rs/rsProgram.cpp
+++ b/libs/rs/rsProgram.cpp
@@ -70,15 +70,8 @@
 }
 
 Program::~Program() {
+    freeChildren();
 
-    for (uint32_t ct=0; ct < mHal.state.constantsCount; ct++) {
-        bindAllocation(NULL, NULL, ct);
-    }
-
-    for (uint32_t ct=0; ct < mHal.state.texturesCount; ct++) {
-        bindTexture(NULL, ct, NULL);
-        bindSampler(NULL, ct, NULL);
-    }
     delete[] mHal.state.textures;
     delete[] mHal.state.samplers;
     delete[] mHal.state.textureTargets;
@@ -90,6 +83,18 @@
     mHal.state.texturesCount = 0;
 }
 
+bool Program::freeChildren() {
+    for (uint32_t ct=0; ct < mHal.state.constantsCount; ct++) {
+        bindAllocation(NULL, NULL, ct);
+    }
+
+    for (uint32_t ct=0; ct < mHal.state.texturesCount; ct++) {
+        bindTexture(NULL, ct, NULL);
+        bindSampler(NULL, ct, NULL);
+    }
+    return false;
+}
+
 void Program::initMemberVars() {
     mDirty = true;
 
diff --git a/libs/rs/rsProgram.h b/libs/rs/rsProgram.h
index 948ba3e..3237a72 100644
--- a/libs/rs/rsProgram.h
+++ b/libs/rs/rsProgram.h
@@ -34,6 +34,7 @@
     Program(Context *, const char * shaderText, uint32_t shaderLength,
                        const uint32_t * params, uint32_t paramLength);
     virtual ~Program();
+    virtual bool freeChildren();
 
     void bindAllocation(Context *, Allocation *, uint32_t slot);
 
diff --git a/media/java/android/media/videoeditor/MediaArtistNativeHelper.java b/media/java/android/media/videoeditor/MediaArtistNativeHelper.java
index 8caa04c..b7d129d 100644
--- a/media/java/android/media/videoeditor/MediaArtistNativeHelper.java
+++ b/media/java/android/media/videoeditor/MediaArtistNativeHelper.java
@@ -79,7 +79,8 @@
     private boolean mRegenerateAudio = true;
 
     private String mExportFilename = null;
-
+    private int mExportVideoCodec = 0;
+    private int mExportAudioCodec = 0;
     private int mProgressToApp;
 
     private String mRenderPreviewOverlayFile;
@@ -765,17 +766,14 @@
         /** No video present in output clip. Used to generate audio only clip */
         public static final int NO_VIDEO = 0;
 
-        /** H263 baseline format. */
+        /** H263 video format. */
         public static final int H263 = 1;
 
-        /** MPEG4 video Simple Profile format. */
-        public static final int MPEG4 = 2;
-
-        /** MPEG4 video Simple Profile format with support for EMP. */
-        public static final int MPEG4_EMP = 3;
-
         /** H264 video */
-        public static final int H264 = 4;
+        public static final int H264 = 2;
+
+        /** MPEG4 video format. */
+        public static final int MPEG4 = 3;
 
         /** No transcoding. Output video format is same as input video format */
         public static final int NULL_VIDEO = 254;
@@ -784,96 +782,6 @@
         public static final int UNSUPPORTED = 255;
     }
 
-    /** Defines video profiles and levels. */
-    public final class VideoProfile {
-        /** H263, Profile 0, Level 10. */
-        public static final int H263_PROFILE_0_LEVEL_10 = MediaProperties.H263_PROFILE_0_LEVEL_10;
-
-        /** H263, Profile 0, Level 20. */
-        public static final int H263_PROFILE_0_LEVEL_20 = MediaProperties.H263_PROFILE_0_LEVEL_20;
-
-        /** H263, Profile 0, Level 30. */
-        public static final int H263_PROFILE_0_LEVEL_30 = MediaProperties.H263_PROFILE_0_LEVEL_30;
-
-        /** H263, Profile 0, Level 40. */
-        public static final int H263_PROFILE_0_LEVEL_40 = MediaProperties.H263_PROFILE_0_LEVEL_40;
-
-        /** H263, Profile 0, Level 45. */
-        public static final int H263_PROFILE_0_LEVEL_45 = MediaProperties.H263_PROFILE_0_LEVEL_45;
-
-        /** MPEG4, Simple Profile, Level 0. */
-        public static final int MPEG4_SP_LEVEL_0 = MediaProperties.MPEG4_SP_LEVEL_0;
-
-        /** MPEG4, Simple Profile, Level 0B. */
-        public static final int MPEG4_SP_LEVEL_0B = MediaProperties.MPEG4_SP_LEVEL_0B;
-
-        /** MPEG4, Simple Profile, Level 1. */
-        public static final int MPEG4_SP_LEVEL_1 = MediaProperties.MPEG4_SP_LEVEL_1;
-
-        /** MPEG4, Simple Profile, Level 2. */
-        public static final int MPEG4_SP_LEVEL_2 = MediaProperties.MPEG4_SP_LEVEL_2;
-
-        /** MPEG4, Simple Profile, Level 3. */
-        public static final int MPEG4_SP_LEVEL_3 = MediaProperties.MPEG4_SP_LEVEL_3;
-
-        /** MPEG4, Simple Profile, Level 4A. */
-        public static final int MPEG4_SP_LEVEL_4A = MediaProperties.MPEG4_SP_LEVEL_4A;
-
-        /** MPEG4, Simple Profile, Level 0. */
-        public static final int MPEG4_SP_LEVEL_5 = MediaProperties.MPEG4_SP_LEVEL_5;
-
-        /** H264, Profile 0, Level 1. */
-        public static final int H264_PROFILE_0_LEVEL_1 = MediaProperties.H264_PROFILE_0_LEVEL_1;
-
-        /** H264, Profile 0, Level 1b. */
-        public static final int H264_PROFILE_0_LEVEL_1b = MediaProperties.H264_PROFILE_0_LEVEL_1B;
-
-        /** H264, Profile 0, Level 1.1 */
-        public static final int H264_PROFILE_0_LEVEL_1_1 = MediaProperties.H264_PROFILE_0_LEVEL_1_1;
-
-        /** H264, Profile 0, Level 1.2 */
-        public static final int H264_PROFILE_0_LEVEL_1_2 = MediaProperties.H264_PROFILE_0_LEVEL_1_2;
-
-        /** H264, Profile 0, Level 1.3 */
-        public static final int H264_PROFILE_0_LEVEL_1_3 = MediaProperties.H264_PROFILE_0_LEVEL_1_3;
-
-        /** H264, Profile 0, Level 2. */
-        public static final int H264_PROFILE_0_LEVEL_2 = MediaProperties.H264_PROFILE_0_LEVEL_2;
-
-        /** H264, Profile 0, Level 2.1 */
-        public static final int H264_PROFILE_0_LEVEL_2_1 = MediaProperties.H264_PROFILE_0_LEVEL_2_1;
-
-        /** H264, Profile 0, Level 2.2 */
-        public static final int H264_PROFILE_0_LEVEL_2_2 = MediaProperties.H264_PROFILE_0_LEVEL_2_2;
-
-        /** H264, Profile 0, Level 3. */
-        public static final int H264_PROFILE_0_LEVEL_3 = MediaProperties.H264_PROFILE_0_LEVEL_3;
-
-        /** H264, Profile 0, Level 3.1 */
-        public static final int H264_PROFILE_0_LEVEL_3_1 = MediaProperties.H264_PROFILE_0_LEVEL_3_1;
-
-        /** H264, Profile 0, Level 3.2 */
-        public static final int H264_PROFILE_0_LEVEL_3_2 = MediaProperties.H264_PROFILE_0_LEVEL_3_2;
-
-        /** H264, Profile 0, Level 4. */
-        public static final int H264_PROFILE_0_LEVEL_4 = MediaProperties.H264_PROFILE_0_LEVEL_4;
-
-        /** H264, Profile 0, Level 4.1 */
-        public static final int H264_PROFILE_0_LEVEL_4_1 = MediaProperties.H264_PROFILE_0_LEVEL_4_1;
-
-        /** H264, Profile 0, Level 4.2 */
-        public static final int H264_PROFILE_0_LEVEL_4_2 = MediaProperties.H264_PROFILE_0_LEVEL_4_2;
-
-        /** H264, Profile 0, Level 5. */
-        public static final int H264_PROFILE_0_LEVEL_5 = MediaProperties.H264_PROFILE_0_LEVEL_5;
-
-        /** H264, Profile 0, Level 5.1 */
-        public static final int H264_PROFILE_0_LEVEL_5_1 = MediaProperties.H264_PROFILE_0_LEVEL_5_1;
-
-        /** Profile out of range. */
-        public static final int OUT_OF_RANGE = MediaProperties.UNSUPPORTED_PROFILE_LEVEL;
-    }
-
     /** Defines video frame sizes. */
     public final class VideoFrameSize {
 
@@ -1620,6 +1528,16 @@
         public int videoFormat;
 
         /**
+         * Profile of the video stream in the output clip.
+         */
+        public int videoProfile;
+
+        /**
+         * Level of the video stream in the output clip.
+         */
+        public int videoLevel;
+
+        /**
          * Format of the audio stream in the output clip. See
          * {@link AudioFormat AudioFormat} for valid values.
          */
@@ -1723,10 +1641,28 @@
         public float averageFrameRate;
 
         /**
-         * Profile and level of the video in the media.
+         * Profile of the video in the media.
          */
 
-        public int profileAndLevel;
+        public int profile;
+
+        /**
+         * Level of the video in the media.
+         */
+
+        public int level;
+
+        /**
+         * Is Video Profile supported.
+         */
+
+        public boolean profileSupported;
+
+        /**
+         * Is Video Level supported.
+         */
+
+        public boolean levelSupported;
 
         /**
          * Audio format.
@@ -2112,6 +2048,25 @@
     }
 
     /**
+     * Sets the export audio codec
+     *
+     * @param export audio codec
+     *
+     */
+    void setAudioCodec(int codec) {
+        mExportAudioCodec = codec;
+    }
+    /**
+     * Sets the export video codec
+     *
+     * @param export video codec
+     *
+     */
+    void setVideoCodec(int codec) {
+        mExportVideoCodec = codec;
+    }
+
+    /**
      * Sets the audio regenerate flag
      *
      * @param flag The boolean to set the audio regenerate flag
@@ -2320,7 +2275,8 @@
         int err = 0;
         EditSettings editSettings = null;
         String EffectClipPath = null;
-
+        int outVideoProfile = 0;
+        int outVideoLevel = 0;
         editSettings = new EditSettings();
 
         editSettings.clipSettingsArray = new ClipSettings[1];
@@ -2338,6 +2294,11 @@
             tmpFile.delete();
         }
 
+        outVideoProfile = VideoEditorProfile.getExportProfile(VideoFormat.H264);
+        outVideoLevel = VideoEditorProfile.getExportLevel(VideoFormat.H264);
+        editSettings.videoProfile = outVideoProfile;
+        editSettings.videoLevel= outVideoLevel;
+
         if (lMediaItem instanceof MediaVideoItem) {
             MediaVideoItem m = (MediaVideoItem)lMediaItem;
 
@@ -2399,6 +2360,8 @@
     String generateKenBurnsClip(EditSettings e, MediaImageItem m) {
         String output = null;
         int err = 0;
+        int outVideoProfile = 0;
+        int outVideoLevel = 0;
 
         e.backgroundMusicSettings = null;
         e.transitionSettingsArray = null;
@@ -2410,6 +2373,11 @@
             tmpFile.delete();
         }
 
+        outVideoProfile = VideoEditorProfile.getExportProfile(VideoFormat.H264);
+        outVideoLevel = VideoEditorProfile.getExportLevel(VideoFormat.H264);
+        e.videoProfile = outVideoProfile;
+        e.videoLevel = outVideoLevel;
+
         e.outputFile = output;
         e.audioBitrate = Bitrate.BR_64_KBPS;
         e.audioChannels = 2;
@@ -2496,8 +2464,15 @@
             MediaItem m1, MediaItem m2,Transition t) {
         String outputFilename = null;
         int err = 0;
-
+        int outVideoProfile = 0;
+        int outVideoLevel = 0;
         outputFilename = String.format(mProjectPath + "/" + uniqueId + ".3gp");
+
+        outVideoProfile = VideoEditorProfile.getExportProfile(VideoFormat.H264);
+        outVideoLevel = VideoEditorProfile.getExportLevel(VideoFormat.H264);
+        e.videoProfile = outVideoProfile;
+        e.videoLevel = outVideoLevel;
+
         e.outputFile = outputFilename;
         e.audioBitrate = Bitrate.BR_64_KBPS;
         e.audioChannels = 2;
@@ -3275,7 +3250,7 @@
                 retValue = MediaProperties.VCODEC_H263;
                 break;
             case VideoFormat.H264:
-                retValue = MediaProperties.VCODEC_H264BP;
+                retValue = MediaProperties.VCODEC_H264;
                 break;
             case VideoFormat.MPEG4:
                 retValue = MediaProperties.VCODEC_MPEG4;
@@ -3602,6 +3577,24 @@
         mExportFilename = filePath;
         previewStoryBoard(mediaItemsList, mediaTransitionList, mediaBGMList,null);
         mExportProgressListener = listener;
+        int outVideoProfile = 0;
+        int outVideoLevel = 0;
+
+        /** Check the platform specific maximum export resolution */
+        VideoEditorProfile veProfile = VideoEditorProfile.get();
+        if (veProfile == null) {
+            throw new RuntimeException("Can't get the video editor profile");
+        }
+        final int maxOutputHeight = veProfile.maxOutputVideoFrameHeight;
+        final int maxOutputWidth = veProfile.maxOutputVideoFrameWidth;
+        if (height > maxOutputHeight) {
+            throw new IllegalArgumentException(
+                    "Unsupported export resolution. Supported maximum width:" +
+                    maxOutputWidth + " height:" + maxOutputHeight +
+                    " current height:" + height);
+        }
+        outVideoProfile = VideoEditorProfile.getExportProfile(mExportVideoCodec);
+        outVideoLevel = VideoEditorProfile.getExportLevel(mExportVideoCodec);
 
         mProgressToApp = 0;
 
@@ -3655,8 +3648,10 @@
 
         int aspectRatio = mVideoEditor.getAspectRatio();
         mPreviewEditSettings.videoFrameSize = findVideoResolution(aspectRatio, height);
-        mPreviewEditSettings.videoFormat = VideoFormat.H264;
-        mPreviewEditSettings.audioFormat = AudioFormat.AAC;
+        mPreviewEditSettings.videoFormat = mExportVideoCodec;
+        mPreviewEditSettings.audioFormat = mExportAudioCodec;
+        mPreviewEditSettings.videoProfile = outVideoProfile;
+        mPreviewEditSettings.videoLevel = outVideoLevel;
         mPreviewEditSettings.audioSamplingFreq = AudioSamplingFrequency.FREQ_32000;
         mPreviewEditSettings.maxFileSize = 0;
         mPreviewEditSettings.audioChannels = 2;
diff --git a/media/java/android/media/videoeditor/MediaProperties.java b/media/java/android/media/videoeditor/MediaProperties.java
index ff13e5d..fd034ab 100755
--- a/media/java/android/media/videoeditor/MediaProperties.java
+++ b/media/java/android/media/videoeditor/MediaProperties.java
@@ -135,89 +135,118 @@
      *  Video codec types
      */
     public static final int VCODEC_H263 = 1;
-    public static final int VCODEC_MPEG4 = 2;
-    // 3 Value is used for MPEG4_EMP
-    public static final int VCODEC_H264BP = 4;
-    public static final int VCODEC_H264MP = 5;  // Unsupported
+    public static final int VCODEC_H264 = 2;
+    public static final int VCODEC_MPEG4 = 3;
 
     /**
      *  The array of supported video codecs
      */
     private static final int[] SUPPORTED_VCODECS = new int[] {
-        VCODEC_H264BP,
+        VCODEC_H264,
         VCODEC_H263,
         VCODEC_MPEG4,
     };
 
-    /* H.263 Profiles and levels */
-    public static final int     H263_PROFILE_0_LEVEL_10   = 0;
-    public static final int     H263_PROFILE_0_LEVEL_20   = 1;
-    public static final int     H263_PROFILE_0_LEVEL_30   = 2;
-    public static final int     H263_PROFILE_0_LEVEL_40   = 3;
-    public static final int     H263_PROFILE_0_LEVEL_45   = 4;
-    /* MPEG-4 Profiles and levels */
-    public static final int     MPEG4_SP_LEVEL_0          = 50;
-    public static final int     MPEG4_SP_LEVEL_0B         = 51;
-    public static final int     MPEG4_SP_LEVEL_1          = 52;
-    public static final int     MPEG4_SP_LEVEL_2          = 53;
-    public static final int     MPEG4_SP_LEVEL_3          = 54;
-    public static final int     MPEG4_SP_LEVEL_4A         = 55;
-    public static final int     MPEG4_SP_LEVEL_5          = 56;
-    /* AVC Profiles and levels */
-    public static final int     H264_PROFILE_0_LEVEL_1    = 150;
-    public static final int     H264_PROFILE_0_LEVEL_1B   = 151;
-    public static final int     H264_PROFILE_0_LEVEL_1_1  = 152;
-    public static final int     H264_PROFILE_0_LEVEL_1_2  = 153;
-    public static final int     H264_PROFILE_0_LEVEL_1_3  = 154;
-    public static final int     H264_PROFILE_0_LEVEL_2    = 155;
-    public static final int     H264_PROFILE_0_LEVEL_2_1  = 156;
-    public static final int     H264_PROFILE_0_LEVEL_2_2  = 157;
-    public static final int     H264_PROFILE_0_LEVEL_3    = 158;
-    public static final int     H264_PROFILE_0_LEVEL_3_1  = 159;
-    public static final int     H264_PROFILE_0_LEVEL_3_2  = 160;
-    public static final int     H264_PROFILE_0_LEVEL_4    = 161;
-    public static final int     H264_PROFILE_0_LEVEL_4_1  = 162;
-    public static final int     H264_PROFILE_0_LEVEL_4_2  = 163;
-    public static final int     H264_PROFILE_0_LEVEL_5    = 164;
-    public static final int     H264_PROFILE_0_LEVEL_5_1  = 165;
-    /* Unsupported profile and level */
-    public static final int     UNSUPPORTED_PROFILE_LEVEL = 255;
-
     /**
-     *  The array of supported video codec Profile and Levels
+     *  The H264 profile, the values are same as the one in OMX_Video.h
      */
-    private static final int[] SUPPORTED_VCODEC_PROFILE_LEVELS = new int[] {
-        H263_PROFILE_0_LEVEL_10,
-        H263_PROFILE_0_LEVEL_20,
-        H263_PROFILE_0_LEVEL_30,
-        H263_PROFILE_0_LEVEL_40,
-        H263_PROFILE_0_LEVEL_45,
-        MPEG4_SP_LEVEL_0,
-        MPEG4_SP_LEVEL_0B,
-        MPEG4_SP_LEVEL_1,
-        MPEG4_SP_LEVEL_2,
-        MPEG4_SP_LEVEL_3,
-        MPEG4_SP_LEVEL_4A,
-        MPEG4_SP_LEVEL_5,
-        H264_PROFILE_0_LEVEL_1,
-        H264_PROFILE_0_LEVEL_1B,
-        H264_PROFILE_0_LEVEL_1_1,
-        H264_PROFILE_0_LEVEL_1_2,
-        H264_PROFILE_0_LEVEL_1_3,
-        H264_PROFILE_0_LEVEL_2,
-        H264_PROFILE_0_LEVEL_2_1,
-        H264_PROFILE_0_LEVEL_2_2,
-        H264_PROFILE_0_LEVEL_3,
-        H264_PROFILE_0_LEVEL_3_1,
-        H264_PROFILE_0_LEVEL_3_2,
-        H264_PROFILE_0_LEVEL_4,
-        H264_PROFILE_0_LEVEL_4_1,
-        H264_PROFILE_0_LEVEL_4_2,
-        H264_PROFILE_0_LEVEL_5,
-        H264_PROFILE_0_LEVEL_5_1,
-        UNSUPPORTED_PROFILE_LEVEL
-    };
-
+    public final class H264Profile {
+        public static final int H264ProfileBaseline = 0x01; /**< Baseline profile */
+        public static final int H264ProfileMain     = 0x02; /**< Main profile */
+        public static final int H264ProfileExtended = 0x04; /**< Extended profile */
+        public static final int H264ProfileHigh     = 0x08; /**< High profile */
+        public static final int H264ProfileHigh10   = 0x10; /**< High 10 profile */
+        public static final int H264ProfileHigh422  = 0x20; /**< High 4:2:2 profile */
+        public static final int H264ProfileHigh444  = 0x40; /**< High 4:4:4 profile */
+        public static final int H264ProfileUnknown  = 0x7FFFFFFF;
+   }
+    /**
+     *  The H264 level, the values are same as the one in OMX_Video.h
+     */
+    public final class H264Level {
+        public static final int H264Level1   = 0x01; /**< Level 1 */
+        public static final int H264Level1b  = 0x02; /**< Level 1b */
+        public static final int H264Level11  = 0x04; /**< Level 1.1 */
+        public static final int H264Level12  = 0x08; /**< Level 1.2 */
+        public static final int H264Level13  = 0x10; /**< Level 1.3 */
+        public static final int H264Level2   = 0x20; /**< Level 2 */
+        public static final int H264Level21  = 0x40; /**< Level 2.1 */
+        public static final int H264Level22  = 0x80; /**< Level 2.2 */
+        public static final int H264Level3   = 0x100; /**< Level 3 */
+        public static final int H264Level31  = 0x200; /**< Level 3.1 */
+        public static final int H264Level32  = 0x400; /**< Level 3.2 */
+        public static final int H264Level4   = 0x800; /**< Level 4 */
+        public static final int H264Level41  = 0x1000; /**< Level 4.1 */
+        public static final int H264Level42  = 0x2000; /**< Level 4.2 */
+        public static final int H264Level5   = 0x4000; /**< Level 5 */
+        public static final int H264Level51  = 0x8000; /**< Level 5.1 */
+        public static final int H264LevelUnknown = 0x7FFFFFFF;
+    }
+    /**
+     *  The H263 profile, the values are same as the one in OMX_Video.h
+     */
+    public final class H263Profile {
+        public static final int H263ProfileBaseline            = 0x01;
+        public static final int H263ProfileH320Coding          = 0x02;
+        public static final int H263ProfileBackwardCompatible  = 0x04;
+        public static final int H263ProfileISWV2               = 0x08;
+        public static final int H263ProfileISWV3               = 0x10;
+        public static final int H263ProfileHighCompression     = 0x20;
+        public static final int H263ProfileInternet            = 0x40;
+        public static final int H263ProfileInterlace           = 0x80;
+        public static final int H263ProfileHighLatency       = 0x100;
+        public static final int H263ProfileUnknown          = 0x7FFFFFFF;
+    }
+    /**
+     *  The H263 level, the values are same as the one in OMX_Video.h
+     */
+    public final class H263Level {
+        public static final int H263Level10  = 0x01;
+        public static final int H263Level20  = 0x02;
+        public static final int H263Level30  = 0x04;
+        public static final int H263Level40  = 0x08;
+        public static final int H263Level45  = 0x10;
+        public static final int H263Level50  = 0x20;
+        public static final int H263Level60  = 0x40;
+        public static final int H263Level70  = 0x80;
+        public static final int H263LevelUnknown = 0x7FFFFFFF;
+    }
+    /**
+     *  The mpeg4 profile, the values are same as the one in OMX_Video.h
+     */
+    public final class MPEG4Profile {
+        public static final int MPEG4ProfileSimple           = 0x01;
+        public static final int MPEG4ProfileSimpleScalable   = 0x02;
+        public static final int MPEG4ProfileCore             = 0x04;
+        public static final int MPEG4ProfileMain             = 0x08;
+        public static final int MPEG4ProfileNbit             = 0x10;
+        public static final int MPEG4ProfileScalableTexture  = 0x20;
+        public static final int MPEG4ProfileSimpleFace       = 0x40;
+        public static final int MPEG4ProfileSimpleFBA        = 0x80;
+        public static final int MPEG4ProfileBasicAnimated    = 0x100;
+        public static final int MPEG4ProfileHybrid           = 0x200;
+        public static final int MPEG4ProfileAdvancedRealTime = 0x400;
+        public static final int MPEG4ProfileCoreScalable     = 0x800;
+        public static final int MPEG4ProfileAdvancedCoding   = 0x1000;
+        public static final int MPEG4ProfileAdvancedCore     = 0x2000;
+        public static final int MPEG4ProfileAdvancedScalable = 0x4000;
+        public static final int MPEG4ProfileAdvancedSimple   = 0x8000;
+        public static final int MPEG4ProfileUnknown          = 0x7FFFFFFF;
+    }
+    /**
+     *  The mpeg4 level, the values are same as the one in OMX_Video.h
+     */
+    public final class MPEG4Level {
+        public static final int MPEG4Level0  = 0x01; /**< Level 0 */
+        public static final int MPEG4Level0b = 0x02; /**< Level 0b */
+        public static final int MPEG4Level1  = 0x04; /**< Level 1 */
+        public static final int MPEG4Level2  = 0x08; /**< Level 2 */
+        public static final int MPEG4Level3  = 0x10; /**< Level 3 */
+        public static final int MPEG4Level4  = 0x20; /**< Level 4 */
+        public static final int MPEG4Level4a = 0x40; /**< Level 4a */
+        public static final int MPEG4Level5  = 0x80; /**< Level 5 */
+        public static final int MPEG4LevelUnknown = 0x7FFFFFFF;
+    }
     /**
      *  Audio codec types
      */
@@ -241,7 +270,6 @@
         ACODEC_AMRWB
     };
 
-
     /**
      *  Samples per frame for each audio codec
      */
diff --git a/media/java/android/media/videoeditor/MediaVideoItem.java b/media/java/android/media/videoeditor/MediaVideoItem.java
index 0ac354b..fea751b 100755
--- a/media/java/android/media/videoeditor/MediaVideoItem.java
+++ b/media/java/android/media/videoeditor/MediaVideoItem.java
@@ -42,6 +42,7 @@
     private final int mFileType;
     private final int mVideoType;
     private final int mVideoProfile;
+    private final int mVideoLevel;
     private final int mVideoBitrate;
     private final long mDurationMs;
     private final int mAudioBitrate;
@@ -134,6 +135,15 @@
                 ", current width:" + properties.width +
                 " height:" + properties.height);
         }
+        /** Check the platform specific maximum video profile and level */
+        if (!properties.profileSupported) {
+            throw new IllegalArgumentException(
+                "Unsupported video profile " + properties.profile);
+        }
+        if (!properties.levelSupported) {
+            throw new IllegalArgumentException(
+                "Unsupported video level " + properties.level);
+        }
         switch (mMANativeHelper.getFileType(properties.fileType)) {
             case MediaProperties.FILE_3GP:
             case MediaProperties.FILE_MP4:
@@ -146,8 +156,7 @@
 
         switch (mMANativeHelper.getVideoCodecType(properties.videoFormat)) {
             case MediaProperties.VCODEC_H263:
-            case MediaProperties.VCODEC_H264BP:
-            case MediaProperties.VCODEC_H264MP:
+            case MediaProperties.VCODEC_H264:
             case MediaProperties.VCODEC_MPEG4:
                 break;
 
@@ -155,18 +164,14 @@
                 throw new IllegalArgumentException("Unsupported Video Codec Format in Input File");
         }
 
-        /* Check if the profile is unsupported. */
-        if (properties.profileAndLevel == MediaProperties.UNDEFINED_VIDEO_PROFILE) {
-            throw new IllegalArgumentException("Unsupported Video Codec Profile in Input File");
-        }
-
         mWidth = properties.width;
         mHeight = properties.height;
         mAspectRatio = mMANativeHelper.getAspectRatio(properties.width,
                 properties.height);
         mFileType = mMANativeHelper.getFileType(properties.fileType);
         mVideoType = mMANativeHelper.getVideoCodecType(properties.videoFormat);
-        mVideoProfile = properties.profileAndLevel;
+        mVideoProfile = properties.profile;
+        mVideoLevel = properties.level;
         mDurationMs = properties.videoDuration;
         mVideoBitrate = properties.videoBitrate;
         mAudioBitrate = properties.audioBitrate;
@@ -661,6 +666,13 @@
     }
 
     /**
+     * @return The video profile
+     */
+    public int getVideoLevel() {
+        return mVideoLevel;
+    }
+
+    /**
      * @return The video bitrate
      */
     public int getVideoBitrate() {
diff --git a/media/java/android/media/videoeditor/VideoEditor.java b/media/java/android/media/videoeditor/VideoEditor.java
index 720e8022..424e436 100755
--- a/media/java/android/media/videoeditor/VideoEditor.java
+++ b/media/java/android/media/videoeditor/VideoEditor.java
@@ -396,7 +396,7 @@
      *        supported for export purposes.
      * @param videoCodec The video codec to be used for the export. The video
      *        codec values are defined in the MediaProperties class (e.g.
-     *        VCODEC_H264BP). Note that not all video codec types are
+     *        VCODEC_H264). Note that not all video codec types are
      *        supported for export purposes.
      * @param listener The listener for progress notifications. Use null if
      *        export progress notifications are not needed.
diff --git a/media/java/android/media/videoeditor/VideoEditorImpl.java b/media/java/android/media/videoeditor/VideoEditorImpl.java
index ea7fe63..f18dd88 100755
--- a/media/java/android/media/videoeditor/VideoEditorImpl.java
+++ b/media/java/android/media/videoeditor/VideoEditorImpl.java
@@ -339,42 +339,8 @@
                        int audioCodec, int videoCodec,
                        ExportProgressListener listener)
                        throws IOException {
-
-        switch (audioCodec) {
-            case MediaProperties.ACODEC_AAC_LC:
-                break;
-            case MediaProperties.ACODEC_AMRNB:
-                break;
-
-            default: {
-                String message = "Unsupported audio codec type " + audioCodec;
-                throw new IllegalArgumentException(message);
-            }
-        }
-
-        switch (videoCodec) {
-            case MediaProperties.VCODEC_H263:
-                break;
-            case MediaProperties.VCODEC_H264BP:
-                break;
-            case MediaProperties.VCODEC_MPEG4:
-                break;
-
-            default: {
-                String message = "Unsupported video codec type " + videoCodec;
-                throw new IllegalArgumentException(message);
-            }
-        }
-
-        export(filename, height, bitrate, listener);
-    }
-
-    /*
-     * {@inheritDoc}
-     */
-    public void export(String filename, int height, int bitrate,
-                       ExportProgressListener listener)
-                       throws IOException {
+        int audcodec = 0;
+        int vidcodec = 0;
         if (filename == null) {
             throw new IllegalArgumentException("export: filename is null");
         }
@@ -388,20 +354,6 @@
             throw new IllegalStateException("No MediaItems added");
         }
 
-        /** Check the platform specific maximum export resolution */
-        VideoEditorProfile veProfile = VideoEditorProfile.get();
-        if (veProfile == null) {
-            throw new RuntimeException("Can't get the video editor profile");
-        }
-        final int maxOutputHeight = veProfile.maxOutputVideoFrameHeight;
-        final int maxOutputWidth = veProfile.maxOutputVideoFrameWidth;
-        if (height > maxOutputHeight) {
-            throw new IllegalArgumentException(
-                    "Unsupported export resolution. Supported maximum width:" +
-                    maxOutputWidth + " height:" + maxOutputHeight +
-                    " current height:" + height);
-        }
-
         switch (height) {
             case MediaProperties.HEIGHT_144:
                 break;
@@ -461,6 +413,36 @@
         if (MAX_SUPPORTED_FILE_SIZE <= fileSize) {
             throw new IllegalStateException("Export Size is more than 2GB");
         }
+        switch (audioCodec) {
+            case MediaProperties.ACODEC_AAC_LC:
+                audcodec = MediaArtistNativeHelper.AudioFormat.AAC;
+                break;
+            case MediaProperties.ACODEC_AMRNB:
+                audcodec = MediaArtistNativeHelper.AudioFormat.AMR_NB;
+                break;
+
+            default: {
+                String message = "Unsupported audio codec type " + audioCodec;
+                throw new IllegalArgumentException(message);
+            }
+        }
+
+        switch (videoCodec) {
+            case MediaProperties.VCODEC_H263:
+                vidcodec = MediaArtistNativeHelper.VideoFormat.H263;
+                break;
+            case MediaProperties.VCODEC_H264:
+                vidcodec = MediaArtistNativeHelper.VideoFormat.H264;
+                break;
+            case MediaProperties.VCODEC_MPEG4:
+                vidcodec = MediaArtistNativeHelper.VideoFormat.MPEG4;
+                break;
+
+            default: {
+                String message = "Unsupported video codec type " + videoCodec;
+                throw new IllegalArgumentException(message);
+            }
+        }
 
         boolean semAcquireDone = false;
         try {
@@ -470,7 +452,8 @@
             if (mMANativeHelper == null) {
                 throw new IllegalStateException("The video editor is not initialized");
             }
-
+            mMANativeHelper.setAudioCodec(audcodec);
+            mMANativeHelper.setVideoCodec(vidcodec);
             mMANativeHelper.export(filename, mProjectPath, height,bitrate,
                                mMediaItems, mTransitions, mAudioTracks, listener);
         } catch (InterruptedException  ex) {
@@ -485,6 +468,19 @@
     /*
      * {@inheritDoc}
      */
+    public void export(String filename, int height, int bitrate,
+                       ExportProgressListener listener)
+                       throws IOException {
+        int defaultAudiocodec = MediaArtistNativeHelper.AudioFormat.AAC;
+        int defaultVideocodec = MediaArtistNativeHelper.VideoFormat.H264;
+
+        export(filename, height, bitrate, defaultAudiocodec,
+                defaultVideocodec, listener);
+    }
+
+    /*
+     * {@inheritDoc}
+     */
     public void generatePreview(MediaProcessingProgressListener listener) {
         boolean semAcquireDone = false;
         try {
diff --git a/media/java/android/media/videoeditor/VideoEditorProfile.java b/media/java/android/media/videoeditor/VideoEditorProfile.java
index 7d9fc8f..ecdcdfb 100755
--- a/media/java/android/media/videoeditor/VideoEditorProfile.java
+++ b/media/java/android/media/videoeditor/VideoEditorProfile.java
@@ -31,6 +31,10 @@
  */
 public class VideoEditorProfile
 {
+    static {
+        System.loadLibrary("media_jni");
+        native_init();
+    }
     /**
      * The max input video frame width
      */
@@ -58,9 +62,42 @@
         return native_get_videoeditor_profile();
     }
 
-    static {
-        System.loadLibrary("media_jni");
-        native_init();
+    /**
+     * Returns the supported profile by given video codec
+     */
+    public static int getExportProfile(int vidCodec) {
+        int profile = -1;
+
+        switch (vidCodec) {
+            case MediaProperties.VCODEC_H263:
+            case MediaProperties.VCODEC_H264:
+            case MediaProperties.VCODEC_MPEG4:
+                 profile = native_get_videoeditor_export_profile(vidCodec);
+                 break;
+            default :
+               throw new IllegalArgumentException("Unsupported video codec" + vidCodec);
+        }
+
+        return profile;
+    }
+
+    /**
+     * Returns the supported level by given video codec
+     */
+    public static int getExportLevel(int vidCodec) {
+        int level = -1;
+
+        switch (vidCodec) {
+            case MediaProperties.VCODEC_H263:
+            case MediaProperties.VCODEC_H264:
+            case MediaProperties.VCODEC_MPEG4:
+                 level = native_get_videoeditor_export_profile(vidCodec);
+                 break;
+            default :
+               throw new IllegalArgumentException("Unsupported video codec" + vidCodec);
+        }
+
+        return level;
     }
 
     // Private constructor called by JNI
@@ -78,5 +115,8 @@
     // Methods implemented by JNI
     private static native final void native_init();
     private static native final VideoEditorProfile
-	    native_get_videoeditor_profile();
+        native_get_videoeditor_profile();
+    private static native final int native_get_videoeditor_export_profile(int codec);
+    private static native final int native_get_videoeditor_export_level(int level);
+
 }
diff --git a/media/jni/android_media_MediaProfiles.cpp b/media/jni/android_media_MediaProfiles.cpp
index 2b8dfe4..e5e688c 100644
--- a/media/jni/android_media_MediaProfiles.cpp
+++ b/media/jni/android_media_MediaProfiles.cpp
@@ -324,7 +324,37 @@
                           maxOutputFrameWidth,
                           maxOutputFrameHeight);
 }
+static jint
+android_media_MediaProfiles_native_get_videoeditor_export_profile(
+    JNIEnv *env, jobject thiz, jint codec)
+{
+    LOGV("android_media_MediaProfiles_native_get_export_profile index ");
+    int profile =0;
+    profile = sProfiles->getVideoEditorExportParamByName("videoeditor.export.profile", codec);
+    // Check the values retrieved
+    if (profile == -1) {
+        jniThrowException(env, "java/lang/RuntimeException",\
+            "Error retrieving videoeditor export profile params");
+        return -1;
+    }
+    return static_cast<jint>(profile);
+}
 
+static jint
+android_media_MediaProfiles_native_get_videoeditor_export_level(
+    JNIEnv *env, jobject thiz, jint codec)
+{
+    LOGV("android_media_MediaProfiles_native_get_export_level");
+    int level =0;
+    level = sProfiles->getVideoEditorExportParamByName("videoeditor.export.level", codec);
+    // Check the values retrieved
+    if (level == -1) {
+        jniThrowException(env, "java/lang/RuntimeException",\
+            "Error retrieving videoeditor export level params");
+        return -1;
+    }
+    return static_cast<jint>(level);
+}
 static JNINativeMethod gMethodsForEncoderCapabilitiesClass[] = {
     {"native_init",                            "()V",                    (void *)android_media_MediaProfiles_native_init},
     {"native_get_num_file_formats",            "()I",                    (void *)android_media_MediaProfiles_native_get_num_file_formats},
@@ -363,16 +393,18 @@
 };
 
 static JNINativeMethod gMethodsForVideoEditorProfileClass[] = {
-    {"native_init",                            "()V",                    (void *)android_media_MediaProfiles_native_init},
-    {"native_get_videoeditor_profile",           "()Landroid/media/videoeditor/VideoEditorProfile;",
-                                                                         (void *)android_media_MediaProfiles_native_get_videoeditor_profile},
+    {"native_init",                            "()V",         (void *)android_media_MediaProfiles_native_init},
+    {"native_get_videoeditor_profile", "()Landroid/media/videoeditor/VideoEditorProfile;", (void *)android_media_MediaProfiles_native_get_videoeditor_profile},
+    {"native_get_videoeditor_export_profile", "(I)I", (void *)android_media_MediaProfiles_native_get_videoeditor_export_profile},
+    {"native_get_videoeditor_export_level", "(I)I", (void *)android_media_MediaProfiles_native_get_videoeditor_export_level},
 };
 
 static const char* const kEncoderCapabilitiesClassPathName = "android/media/EncoderCapabilities";
 static const char* const kDecoderCapabilitiesClassPathName = "android/media/DecoderCapabilities";
 static const char* const kCamcorderProfileClassPathName = "android/media/CamcorderProfile";
 static const char* const kCameraProfileClassPathName = "android/media/CameraProfile";
-static const char* const kVideoEditorProfileClassPathName = "android/media/videoeditor/VideoEditorProfile";
+static const char* const kVideoEditorProfileClassPathName =
+    "android/media/videoeditor/VideoEditorProfile";
 
 // This function only registers the native methods, and is called from
 // JNI_OnLoad in android_media_MediaPlayer.cpp
diff --git a/media/jni/mediaeditor/VideoEditorClasses.cpp b/media/jni/mediaeditor/VideoEditorClasses.cpp
index 4c0e731..69735ca 100755
--- a/media/jni/mediaeditor/VideoEditorClasses.cpp
+++ b/media/jni/mediaeditor/VideoEditorClasses.cpp
@@ -402,7 +402,6 @@
     VIDEOEDIT_JAVA_CONSTANT_INIT("NO_VIDEO",    M4VIDEOEDITING_kNoneVideo),
     VIDEOEDIT_JAVA_CONSTANT_INIT("H263",        M4VIDEOEDITING_kH263),
     VIDEOEDIT_JAVA_CONSTANT_INIT("MPEG4",       M4VIDEOEDITING_kMPEG4),
-    VIDEOEDIT_JAVA_CONSTANT_INIT("MPEG4_EMP",   M4VIDEOEDITING_kMPEG4_EMP),
     VIDEOEDIT_JAVA_CONSTANT_INIT("H264",        M4VIDEOEDITING_kH264),
     VIDEOEDIT_JAVA_CONSTANT_INIT("NULL_VIDEO",  M4VIDEOEDITING_kNullVideo),
     VIDEOEDIT_JAVA_CONSTANT_INIT("UNSUPPORTED", M4VIDEOEDITING_kUnsupportedVideo),
@@ -448,73 +447,6 @@
 VIDEOEDIT_JAVA_DEFINE_CONSTANT_CLASS(VideoFrameSize, VIDEO_FRAME_SIZE_CLASS_NAME,
  M4OSA_NULL, M4OSA_NULL)
 
-
-VIDEOEDIT_JAVA_DEFINE_CONSTANTS(VideoProfile)
-{
-    VIDEOEDIT_JAVA_CONSTANT_INIT("MPEG4_SP_LEVEL_0",       \
-        M4VIDEOEDITING_kMPEG4_SP_Level_0),
-    VIDEOEDIT_JAVA_CONSTANT_INIT("MPEG4_SP_LEVEL_0B",      \
-        M4VIDEOEDITING_kMPEG4_SP_Level_0b),
-    VIDEOEDIT_JAVA_CONSTANT_INIT("MPEG4_SP_LEVEL_1",       \
-        M4VIDEOEDITING_kMPEG4_SP_Level_1),
-    VIDEOEDIT_JAVA_CONSTANT_INIT("MPEG4_SP_LEVEL_2",       \
-        M4VIDEOEDITING_kMPEG4_SP_Level_2),
-    VIDEOEDIT_JAVA_CONSTANT_INIT("MPEG4_SP_LEVEL_3",       \
-        M4VIDEOEDITING_kMPEG4_SP_Level_3),
-    VIDEOEDIT_JAVA_CONSTANT_INIT("MPEG4_SP_LEVEL_4A",      \
-        M4VIDEOEDITING_kMPEG4_SP_Level_4a),
-    VIDEOEDIT_JAVA_CONSTANT_INIT("MPEG4_SP_LEVEL_5",       \
-        M4VIDEOEDITING_kMPEG4_SP_Level_5),
-    VIDEOEDIT_JAVA_CONSTANT_INIT("H263_PROFILE_0_LEVEL_10",\
-        M4VIDEOEDITING_kH263_Profile_0_Level_10),
-    VIDEOEDIT_JAVA_CONSTANT_INIT("H263_PROFILE_0_LEVEL_20",\
-        M4VIDEOEDITING_kH263_Profile_0_Level_20),
-    VIDEOEDIT_JAVA_CONSTANT_INIT("H263_PROFILE_0_LEVEL_30",\
-        M4VIDEOEDITING_kH263_Profile_0_Level_30),
-    VIDEOEDIT_JAVA_CONSTANT_INIT("H263_PROFILE_0_LEVEL_40",\
-        M4VIDEOEDITING_kH263_Profile_0_Level_40),
-    VIDEOEDIT_JAVA_CONSTANT_INIT("H263_PROFILE_0_LEVEL_45",\
-        M4VIDEOEDITING_kH263_Profile_0_Level_45),
-    VIDEOEDIT_JAVA_CONSTANT_INIT("H264_PROFILE_0_LEVEL_1", \
-        M4VIDEOEDITING_kH264_Profile_0_Level_1),
-    VIDEOEDIT_JAVA_CONSTANT_INIT("H264_PROFILE_0_LEVEL_1b",\
-        M4VIDEOEDITING_kH264_Profile_0_Level_1b),
-    VIDEOEDIT_JAVA_CONSTANT_INIT("H264_PROFILE_0_LEVEL_1_1",\
-        M4VIDEOEDITING_kH264_Profile_0_Level_1_1),
-    VIDEOEDIT_JAVA_CONSTANT_INIT("H264_PROFILE_0_LEVEL_1_2",\
-        M4VIDEOEDITING_kH264_Profile_0_Level_1_2),
-    VIDEOEDIT_JAVA_CONSTANT_INIT("H264_PROFILE_0_LEVEL_1_3",\
-        M4VIDEOEDITING_kH264_Profile_0_Level_1_3),
-    VIDEOEDIT_JAVA_CONSTANT_INIT("H264_PROFILE_0_LEVEL_2",  \
-        M4VIDEOEDITING_kH264_Profile_0_Level_2),
-    VIDEOEDIT_JAVA_CONSTANT_INIT("H264_PROFILE_0_LEVEL_2_1",\
-        M4VIDEOEDITING_kH264_Profile_0_Level_2_1),
-    VIDEOEDIT_JAVA_CONSTANT_INIT("H264_PROFILE_0_LEVEL_2_2",\
-        M4VIDEOEDITING_kH264_Profile_0_Level_2_2),
-    VIDEOEDIT_JAVA_CONSTANT_INIT("H264_PROFILE_0_LEVEL_3",  \
-        M4VIDEOEDITING_kH264_Profile_0_Level_3),
-    VIDEOEDIT_JAVA_CONSTANT_INIT("H264_PROFILE_0_LEVEL_3_1",\
-        M4VIDEOEDITING_kH264_Profile_0_Level_3_1),
-    VIDEOEDIT_JAVA_CONSTANT_INIT("H264_PROFILE_0_LEVEL_3_2",\
-        M4VIDEOEDITING_kH264_Profile_0_Level_3_2),
-    VIDEOEDIT_JAVA_CONSTANT_INIT("H264_PROFILE_0_LEVEL_4",  \
-        M4VIDEOEDITING_kH264_Profile_0_Level_4),
-    VIDEOEDIT_JAVA_CONSTANT_INIT("H264_PROFILE_0_LEVEL_4_1",\
-        M4VIDEOEDITING_kH264_Profile_0_Level_4_1),
-    VIDEOEDIT_JAVA_CONSTANT_INIT("H264_PROFILE_0_LEVEL_4_2",\
-        M4VIDEOEDITING_kH264_Profile_0_Level_4_2),
-    VIDEOEDIT_JAVA_CONSTANT_INIT("H264_PROFILE_0_LEVEL_5",  \
-        M4VIDEOEDITING_kH264_Profile_0_Level_5),
-    VIDEOEDIT_JAVA_CONSTANT_INIT("H264_PROFILE_0_LEVEL_5_1",\
-        M4VIDEOEDITING_kH264_Profile_0_Level_5_1),
-    VIDEOEDIT_JAVA_CONSTANT_INIT("OUT_OF_RANGE",            \
-        M4VIDEOEDITING_kProfile_and_Level_Out_Of_Range)
-};
-
-VIDEOEDIT_JAVA_DEFINE_CONSTANT_CLASS(VideoProfile, VIDEO_PROFILE_CLASS_NAME, M4OSA_NULL,
-                                     M4OSA_NULL)
-
-
 VIDEOEDIT_JAVA_DEFINE_CONSTANTS(VideoTransition)
 {
     VIDEOEDIT_JAVA_CONSTANT_INIT("NONE",             M4VSS3GPP_kVideoTransitionType_None),
@@ -550,7 +482,10 @@
     VIDEOEDIT_JAVA_FIELD_INIT("width",                  "I"),
     VIDEOEDIT_JAVA_FIELD_INIT("height",                 "I"),
     VIDEOEDIT_JAVA_FIELD_INIT("averageFrameRate",       "F"),
-    VIDEOEDIT_JAVA_FIELD_INIT("profileAndLevel",        "I"),
+    VIDEOEDIT_JAVA_FIELD_INIT("profile",                "I"),
+    VIDEOEDIT_JAVA_FIELD_INIT("level",                  "I"),
+    VIDEOEDIT_JAVA_FIELD_INIT("profileSupported",       "Z"),
+    VIDEOEDIT_JAVA_FIELD_INIT("levelSupported",         "Z"),
     VIDEOEDIT_JAVA_FIELD_INIT("audioFormat",            "I"),
     VIDEOEDIT_JAVA_FIELD_INIT("audioDuration",          "I"),
     VIDEOEDIT_JAVA_FIELD_INIT("audioBitrate",           "I"),
@@ -620,6 +555,8 @@
     VIDEOEDIT_JAVA_FIELD_INIT("outputFile",              "Ljava/lang/String;"                    ),
     VIDEOEDIT_JAVA_FIELD_INIT("videoFrameSize",          "I"                                     ),
     VIDEOEDIT_JAVA_FIELD_INIT("videoFormat",             "I"                                     ),
+    VIDEOEDIT_JAVA_FIELD_INIT("videoProfile",            "I"                                     ),
+    VIDEOEDIT_JAVA_FIELD_INIT("videoLevel",              "I"                                     ),
     VIDEOEDIT_JAVA_FIELD_INIT("audioFormat",             "I"                                     ),
     VIDEOEDIT_JAVA_FIELD_INIT("audioSamplingFreq",       "I"                                     ),
     VIDEOEDIT_JAVA_FIELD_INIT("maxFileSize",             "I"                                     ),
@@ -846,7 +783,6 @@
         videoEditJava_initVideoFormatConstants(pResult, pEnv);
         videoEditJava_initVideoFrameRateConstants(pResult, pEnv);
         videoEditJava_initVideoFrameSizeConstants(pResult, pEnv);
-        videoEditJava_initVideoProfileConstants(pResult, pEnv);
         videoEditJava_initVideoTransitionConstants(pResult, pEnv);
 
         // Initialize the fields.
@@ -881,7 +817,6 @@
         videoEditJava_initErrorConstants(pResult, pEnv);
         videoEditJava_initFileTypeConstants(pResult, pEnv);
         videoEditJava_initVideoFormatConstants(pResult, pEnv);
-        videoEditJava_initVideoProfileConstants(pResult, pEnv);
 
         // Initialize the fields.
         videoEditJava_initPropertiesFields(pResult, pEnv);
@@ -1251,15 +1186,6 @@
             "%*c fAverageFrameRate:                %.3f",     indentation, ' ',
             pProperties->fAverageFrameRate);
         VIDEOEDIT_LOG_SETTING(ANDROID_LOG_INFO, "VIDEO_EDITOR_CLASSES",
-            "%*c ProfileAndLevel:                  %s",       indentation, ' ',
-            videoEditJava_getVideoProfileString(pProperties->ProfileAndLevel));
-        VIDEOEDIT_LOG_SETTING(ANDROID_LOG_INFO, "VIDEO_EDITOR_CLASSES",
-            "%*c uiH263level:                      %d",       indentation, ' ',
-            pProperties->uiH263level);
-        VIDEOEDIT_LOG_SETTING(ANDROID_LOG_INFO, "VIDEO_EDITOR_CLASSES",
-            "%*c uiVideoProfile:                   %d",       indentation, ' ',
-            pProperties->uiVideoProfile);
-        VIDEOEDIT_LOG_SETTING(ANDROID_LOG_INFO, "VIDEO_EDITOR_CLASSES",
             "%*c bMPEG4dataPartition:              %s",       indentation, ' ',
             pProperties->bMPEG4dataPartition ? "true" : "false");
         VIDEOEDIT_LOG_SETTING(ANDROID_LOG_INFO, "VIDEO_EDITOR_CLASSES",
@@ -1650,9 +1576,21 @@
             // Set the averageFrameRate field.
             pEnv->SetFloatField(object, fieldIds.averageFrameRate, pProperties->fAverageFrameRate);
 
-            // Set the profileAndLevel field.
-            pEnv->SetIntField(object, fieldIds.profileAndLevel,
-                videoEditJava_getVideoProfileCToJava(pProperties->ProfileAndLevel));
+            // Set the profile field.
+            pEnv->SetIntField(object, fieldIds.profile,
+                pProperties->uiVideoProfile);
+
+            // Set the level field.
+            pEnv->SetIntField(object, fieldIds.level,
+                pProperties->uiVideoLevel);
+
+            // Set whether profile supported
+            pEnv->SetBooleanField(object, fieldIds.profileSupported,
+                pProperties->bProfileSupported);
+
+            // Set whether level supported
+            pEnv->SetBooleanField(object, fieldIds.levelSupported,
+                pProperties->bLevelSupported);
 
             // Set the audioFormat field.
             pEnv->SetIntField(object, fieldIds.audioFormat,
@@ -2062,6 +2000,14 @@
                     pSettings->xVSS.outputVideoBitrate = (M4OSA_UInt32)pEnv->GetIntField(object,
                             fieldIds.videoBitrate);
 
+                    // Set the output video profile.
+                    pSettings->xVSS.outputVideoProfile = (M4OSA_UInt32)pEnv->GetIntField(object,
+                            fieldIds.videoProfile);
+
+                    // Set the output video level.
+                    pSettings->xVSS.outputVideoLevel = (M4OSA_UInt32)pEnv->GetIntField(object,
+                            fieldIds.videoLevel);
+
                     // Set the output audio bitrate.
                     pSettings->xVSS.outputAudioBitrate = (M4OSA_UInt32)pEnv->GetIntField(object,
                             fieldIds.audioBitrate);
@@ -2256,6 +2202,12 @@
             "%*c outputVideoFormat:    %s", indentation, ' ',
             videoEditJava_getVideoFormatString(pSettings->xVSS.outputVideoFormat));
         VIDEOEDIT_LOG_SETTING(ANDROID_LOG_INFO, "VIDEO_EDITOR_CLASSES",
+            "%*c outputVideoProfile:    %u", indentation, ' ',
+            videoEditJava_getVideoFormatString(pSettings->xVSS.outputVideoProfile));
+        VIDEOEDIT_LOG_SETTING(ANDROID_LOG_INFO, "VIDEO_EDITOR_CLASSES",
+            "%*c outputVideoLevel:    %u", indentation, ' ',
+            videoEditJava_getVideoFormatString(pSettings->xVSS.outputVideoLevel));
+        VIDEOEDIT_LOG_SETTING(ANDROID_LOG_INFO, "VIDEO_EDITOR_CLASSES",
             "%*c outputAudioFormat:    %s", indentation, ' ',
             videoEditJava_getAudioFormatString(pSettings->xVSS.outputAudioFormat));
         VIDEOEDIT_LOG_SETTING(ANDROID_LOG_INFO, "VIDEO_EDITOR_CLASSES",
@@ -2984,10 +2936,6 @@
             pProperties->fAverageFrameRate);
 
         VIDEOEDIT_LOG_SETTING(ANDROID_LOG_INFO, "VIDEO_EDITOR_PROP_CLASSES",
-            "%*c ProfileAndLevel:                  %s",       indentation, ' ',
-            videoEditJava_getVideoProfileString(pProperties->ProfileAndLevel));
-
-        VIDEOEDIT_LOG_SETTING(ANDROID_LOG_INFO, "VIDEO_EDITOR_PROP_CLASSES",
             "%*c AudioStreamType:                  %s",       indentation, ' ',
             videoEditJava_getAudioFormatString(pProperties->AudioStreamType));
 
diff --git a/media/jni/mediaeditor/VideoEditorClasses.h b/media/jni/mediaeditor/VideoEditorClasses.h
index 045f229..3c10b1d 100755
--- a/media/jni/mediaeditor/VideoEditorClasses.h
+++ b/media/jni/mediaeditor/VideoEditorClasses.h
@@ -128,20 +128,23 @@
  * Structure to hold media properties from native layer
  */
 typedef struct {
-    M4OSA_UInt32                        uiClipDuration;
-    VideoEditClasses_FileType         FileType;
-    M4VIDEOEDITING_VideoFormat          VideoStreamType;
-    M4OSA_UInt32                        uiClipVideoDuration;
-    M4OSA_UInt32                        uiVideoBitrate;
-    M4OSA_UInt32                        uiVideoWidth;
-    M4OSA_UInt32                        uiVideoHeight;
-    M4OSA_Float                         fAverageFrameRate;
-    M4VIDEOEDITING_VideoProfileAndLevel ProfileAndLevel;
-    M4VIDEOEDITING_AudioFormat          AudioStreamType;
-    M4OSA_UInt32                        uiClipAudioDuration;
-    M4OSA_UInt32                        uiAudioBitrate;
-    M4OSA_UInt32                        uiNbChannels;
-    M4OSA_UInt32                        uiSamplingFrequency;
+    M4OSA_UInt32 uiClipDuration;
+    VideoEditClasses_FileType  FileType;
+    M4VIDEOEDITING_VideoFormat VideoStreamType;
+    M4OSA_UInt32 uiClipVideoDuration;
+    M4OSA_UInt32 uiVideoBitrate;
+    M4OSA_UInt32 uiVideoWidth;
+    M4OSA_UInt32 uiVideoHeight;
+    M4OSA_Float  fAverageFrameRate;
+    M4OSA_UInt32 uiVideoProfile; /**< H263 or MPEG-4 or H264 profile(from core decoder) */
+    M4OSA_UInt32 uiVideoLevel;   /**< H263 or MPEG-4 or H264 level*/
+    M4OSA_Bool bProfileSupported;
+    M4OSA_Bool bLevelSupported;
+    M4VIDEOEDITING_AudioFormat AudioStreamType;
+    M4OSA_UInt32 uiClipAudioDuration;
+    M4OSA_UInt32 uiAudioBitrate;
+    M4OSA_UInt32 uiNbChannels;
+    M4OSA_UInt32 uiSamplingFrequency;
 } VideoEditPropClass_Properties;
 
 typedef struct
@@ -154,7 +157,10 @@
     jfieldID width;
     jfieldID height;
     jfieldID averageFrameRate;
-    jfieldID profileAndLevel;
+    jfieldID profile;
+    jfieldID level;
+    jfieldID profileSupported;
+    jfieldID levelSupported;
     jfieldID audioFormat;
     jfieldID audioDuration;
     jfieldID audioBitrate;
@@ -192,6 +198,8 @@
     jfieldID outputFile;
     jfieldID videoFrameSize;
     jfieldID videoFormat;
+    jfieldID videoProfile;
+    jfieldID videoLevel;
     jfieldID audioFormat;
     jfieldID audioSamplingFreq;
     jfieldID maxFileSize;
diff --git a/media/jni/mediaeditor/VideoEditorMain.cpp b/media/jni/mediaeditor/VideoEditorMain.cpp
index 5dda8d3..b737e5d 100755
--- a/media/jni/mediaeditor/VideoEditorMain.cpp
+++ b/media/jni/mediaeditor/VideoEditorMain.cpp
@@ -13,7 +13,8 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
+#define LOG_NDEBUG 1
+#define LOG_TAG "VideoEditorMain"
 #include <dlfcn.h>
 #include <stdio.h>
 #include <unistd.h>
@@ -103,6 +104,7 @@
     bool                            mIsUpdateOverlay;
     char                            *mOverlayFileName;
     int                             mOverlayRenderingMode;
+    M4DECODER_VideoDecoders* decoders;
 } ManualEditContext;
 
 extern "C" M4OSA_ERR M4MCS_open_normalMode(
@@ -484,6 +486,82 @@
     pContext->pVM->DetachCurrentThread();
 
 }
+static M4OSA_ERR checkClipVideoProfileAndLevel(M4DECODER_VideoDecoders *pDecoders,
+    M4OSA_Int32 format, M4OSA_UInt32 profile, M4OSA_UInt32 level){
+
+    M4OSA_Int32 codec = 0;
+    M4OSA_Bool foundCodec = M4OSA_FALSE;
+    M4OSA_ERR  result = M4VSS3GPP_ERR_EDITING_UNSUPPORTED_VIDEO_PROFILE;
+    M4OSA_Bool foundProfile = M4OSA_FALSE;
+    LOGV("checkClipVideoProfileAndLevel format %d profile;%d level:0x%x",
+       format, profile, level);
+
+    switch (format) {
+        case M4VIDEOEDITING_kH263:
+            codec = M4DA_StreamTypeVideoH263;
+            break;
+        case M4VIDEOEDITING_kH264:
+             codec = M4DA_StreamTypeVideoMpeg4Avc;
+            break;
+        case M4VIDEOEDITING_kMPEG4:
+             codec = M4DA_StreamTypeVideoMpeg4;
+            break;
+        case M4VIDEOEDITING_kNoneVideo:
+        case M4VIDEOEDITING_kNullVideo:
+        case M4VIDEOEDITING_kUnsupportedVideo:
+             // For these case we do not check the profile and level
+             return M4NO_ERROR;
+        default :
+            LOGE("checkClipVideoProfileAndLevel unsupport Video format %ld", format);
+            break;
+    }
+
+    if (pDecoders != M4OSA_NULL && pDecoders->decoderNumber > 0) {
+        VideoDecoder *pVideoDecoder = pDecoders->decoder;
+        for(size_t k =0; k < pDecoders->decoderNumber; k++) {
+            if (pVideoDecoder != M4OSA_NULL) {
+                if (pVideoDecoder->codec == codec) {
+                    foundCodec = M4OSA_TRUE;
+                    break;
+                }
+            }
+            pVideoDecoder++;
+        }
+
+        if (foundCodec) {
+            VideoComponentCapabilities* pComponent = pVideoDecoder->component;
+            for (size_t i = 0; i < pVideoDecoder->componentNumber; i++) {
+                if (pComponent != M4OSA_NULL) {
+                    VideoProfileLevel *pProfileLevel = pComponent->profileLevel;
+                    for (size_t j =0; j < pComponent->profileNumber; j++) {
+                        // Check the profile and level
+                        if (pProfileLevel != M4OSA_NULL) {
+                            if (profile == pProfileLevel->mProfile) {
+                                foundProfile = M4OSA_TRUE;
+
+                                if (level <= pProfileLevel->mLevel) {
+                                    return M4NO_ERROR;
+                                }
+                            } else {
+                                foundProfile = M4OSA_FALSE;
+                            }
+                        }
+                        pProfileLevel++;
+                    }
+                }
+                pComponent++;
+            }
+        }
+    }
+
+    if (foundProfile) {
+        result = M4VSS3GPP_ERR_EDITING_UNSUPPORTED_VIDEO_LEVEL;
+    } else {
+        result = M4VSS3GPP_ERR_EDITING_UNSUPPORTED_VIDEO_PROFILE;
+    }
+
+    return result;
+}
 static int videoEditor_stopPreview(JNIEnv*  pEnv,
                               jobject  thiz)
 {
@@ -1244,6 +1322,8 @@
     // Set the video format.
     pOutputParams->OutputVideoFormat =
         (M4VIDEOEDITING_VideoFormat)M4VIDEOEDITING_kNoneVideo;//M4VIDEOEDITING_kNoneVideo;
+    pOutputParams->outputVideoProfile = 1;
+    pOutputParams->outputVideoLevel = 1;
     // Set the frame size.
     pOutputParams->OutputVideoFrameSize
         = (M4VIDEOEDITING_VideoFrameSize)M4VIDEOEDITING_kQCIF;
@@ -1797,7 +1877,8 @@
         }
 
         fid = pEnv->GetFieldID(audioSettingClazz,"bRemoveOriginal","Z");
-        pContext->mAudioSettings->bRemoveOriginal = pEnv->GetIntField(audioSettingObject,fid);
+        pContext->mAudioSettings->bRemoveOriginal =
+            pEnv->GetBooleanField(audioSettingObject,fid);
         M4OSA_TRACE1_1("bRemoveOriginal = %d",pContext->mAudioSettings->bRemoveOriginal);
 
         fid = pEnv->GetFieldID(audioSettingClazz,"channels","I");
@@ -1816,7 +1897,7 @@
 
         fid = pEnv->GetFieldID(audioSettingClazz,"startMs","J");
         pContext->mAudioSettings->uiAddCts
-            = pEnv->GetIntField(audioSettingObject,fid);
+            = pEnv->GetLongField(audioSettingObject,fid);
         M4OSA_TRACE1_1("uiAddCts = %d",pContext->mAudioSettings->uiAddCts);
 
         fid = pEnv->GetFieldID(audioSettingClazz,"volume","I");
@@ -1826,17 +1907,17 @@
 
         fid = pEnv->GetFieldID(audioSettingClazz,"loop","Z");
         pContext->mAudioSettings->bLoop
-            = pEnv->GetIntField(audioSettingObject,fid);
+            = pEnv->GetBooleanField(audioSettingObject,fid);
         M4OSA_TRACE1_1("bLoop = %d",pContext->mAudioSettings->bLoop);
 
         fid = pEnv->GetFieldID(audioSettingClazz,"beginCutTime","J");
         pContext->mAudioSettings->beginCutMs
-            = pEnv->GetIntField(audioSettingObject,fid);
+            = pEnv->GetLongField(audioSettingObject,fid);
         M4OSA_TRACE1_1("begin cut time = %d",pContext->mAudioSettings->beginCutMs);
 
         fid = pEnv->GetFieldID(audioSettingClazz,"endCutTime","J");
         pContext->mAudioSettings->endCutMs
-            = pEnv->GetIntField(audioSettingObject,fid);
+            = pEnv->GetLongField(audioSettingObject,fid);
         M4OSA_TRACE1_1("end cut time = %d",pContext->mAudioSettings->endCutMs);
 
         fid = pEnv->GetFieldID(audioSettingClazz,"fileType","I");
@@ -2090,8 +2171,44 @@
                 jstring                             file)
 {
     jobject object = M4OSA_NULL;
+    jclass clazz = pEnv->FindClass(PROPERTIES_CLASS_NAME);
+    jfieldID fid;
+    bool needToBeLoaded = true;
+    ManualEditContext* pContext = M4OSA_NULL;
+    M4OSA_ERR          result   = M4NO_ERROR;
+    int profile = 0;
+    int level = 0;
+    int videoFormat = 0;
+
+    // Get the context.
+    pContext = (ManualEditContext*)videoEditClasses_getContext(&needToBeLoaded, pEnv, thiz);
+
+    videoEditJava_checkAndThrowIllegalStateException(&needToBeLoaded, pEnv,
+                                             (M4OSA_NULL == clazz),
+                                             "not initialized");
+
     object = videoEditProp_getProperties(pEnv,thiz,file);
 
+    if (object != M4OSA_NULL) {
+        fid = pEnv->GetFieldID(clazz,"profile","I");
+        profile = pEnv->GetIntField(object,fid);
+        fid = pEnv->GetFieldID(clazz,"level","I");
+        level = pEnv->GetIntField(object,fid);
+        fid = pEnv->GetFieldID(clazz,"videoFormat","I");
+        videoFormat = pEnv->GetIntField(object,fid);
+
+        result = checkClipVideoProfileAndLevel(pContext->decoders, videoFormat, profile, level);
+
+        fid = pEnv->GetFieldID(clazz,"profileSupported","Z");
+        if (M4VSS3GPP_ERR_EDITING_UNSUPPORTED_VIDEO_PROFILE == result) {
+            pEnv->SetBooleanField(object,fid,false);
+        }
+
+        fid = pEnv->GetFieldID(clazz,"levelSupported","Z");
+        if (M4VSS3GPP_ERR_EDITING_UNSUPPORTED_VIDEO_LEVEL == result) {
+            pEnv->SetBooleanField(object,fid,false);
+        }
+    }
     return object;
 
 }
@@ -2505,6 +2622,7 @@
             free(tmpString);
             pContext->mIsUpdateOverlay = false;
             pContext->mOverlayFileName = NULL;
+            pContext->decoders = NULL;
         }
 
         // Check if the initialization succeeded
@@ -2550,6 +2668,12 @@
             // Check if the library could be initialized.
             videoEditJava_checkAndThrowRuntimeException(&initialized, pEnv,
              (M4NO_ERROR != result), result);
+
+            // Get platform video decoder capablities.
+            result = M4xVSS_getVideoDecoderCapabilities(&pContext->decoders);
+
+            videoEditJava_checkAndThrowRuntimeException(&initialized, pEnv,
+             (M4NO_ERROR != result), result);
         }
 
         if(initialized)
@@ -3097,6 +3221,47 @@
             free(pContext->mAudioSettings);
             pContext->mAudioSettings = M4OSA_NULL;
         }
+        // Free video Decoders capabilities
+        if (pContext->decoders != M4OSA_NULL) {
+            VideoDecoder *pDecoder = NULL;
+            VideoComponentCapabilities *pComponents = NULL;
+            int32_t decoderNumber = pContext->decoders->decoderNumber;
+            if (pContext->decoders->decoder != NULL &&
+                decoderNumber > 0) {
+                pDecoder = pContext->decoders->decoder;
+                for (int32_t k = 0; k < decoderNumber; k++) {
+                    // free each component
+                    LOGV("decoder index :%d",k);
+                    if (pDecoder != NULL &&
+                        pDecoder->component != NULL &&
+                        pDecoder->componentNumber > 0) {
+                        LOGV("component number %d",pDecoder->componentNumber);
+                        int32_t componentNumber =
+                           pDecoder->componentNumber;
+
+                        pComponents = pDecoder->component;
+                        for (int32_t i = 0; i< componentNumber; i++) {
+                            LOGV("component index :%d",i);
+                            if (pComponents != NULL &&
+                                pComponents->profileLevel != NULL) {
+                                free(pComponents->profileLevel);
+                                pComponents->profileLevel = NULL;
+                            }
+                            pComponents++;
+                        }
+                        free(pDecoder->component);
+                        pDecoder->component = NULL;
+                    }
+
+                    pDecoder++;
+                }
+                free(pContext->decoders->decoder);
+                pContext->decoders->decoder = NULL;
+            }
+            free(pContext->decoders);
+            pContext->decoders = NULL;
+        }
+
         videoEditor_freeContext(pEnv, &pContext);
     }
 }
@@ -3421,6 +3586,7 @@
     M4OSA_ERR result = M4NO_ERROR;
     ManualEditContext* pContext = M4OSA_NULL;
     bool needToBeLoaded = true;
+    const char *pPCMFilePath, *pStringOutAudioGraphFile;
 
     VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR",
         "videoEditor_generateAudioWaveFormSync() ");
@@ -3435,20 +3601,20 @@
     VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR",
         "videoEditor_generateAudioWaveFormSync Retrieving pStringOutAudioGraphFile");
 
-    const char *pPCMFilePath = pEnv->GetStringUTFChars(pcmfilePath, NULL);
+    pPCMFilePath = pEnv->GetStringUTFChars(pcmfilePath, NULL);
     if (pPCMFilePath == M4OSA_NULL) {
-        if (pEnv != NULL) {
-            jniThrowException(pEnv, "java/lang/RuntimeException",
-                "Input string PCMFilePath is null");
-        }
+        jniThrowException(pEnv, "java/lang/RuntimeException",
+            "Input string PCMFilePath is null");
+        result = M4ERR_PARAMETER;
+        goto out;
     }
 
-    const char *pStringOutAudioGraphFile = pEnv->GetStringUTFChars(outGraphfilePath, NULL);
+    pStringOutAudioGraphFile = pEnv->GetStringUTFChars(outGraphfilePath, NULL);
     if (pStringOutAudioGraphFile == M4OSA_NULL) {
-        if (pEnv != NULL) {
-            jniThrowException(pEnv, "java/lang/RuntimeException",
-                "Input string outGraphfilePath is null");
-        }
+        jniThrowException(pEnv, "java/lang/RuntimeException",
+            "Input string outGraphfilePath is null");
+        result = M4ERR_PARAMETER;
+        goto out2;
     }
 
     VIDEOEDIT_LOG_API(ANDROID_LOG_INFO, "VIDEO_EDITOR",
@@ -3463,14 +3629,14 @@
         (M4OSA_UInt32)frameDuration,
         pContext);
 
-    if (pStringOutAudioGraphFile != NULL) {
-        pEnv->ReleaseStringUTFChars(outGraphfilePath, pStringOutAudioGraphFile);
-    }
+    pEnv->ReleaseStringUTFChars(outGraphfilePath, pStringOutAudioGraphFile);
 
+out2:
     if (pPCMFilePath != NULL) {
         pEnv->ReleaseStringUTFChars(pcmfilePath, pPCMFilePath);
     }
 
+out:
     VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR",
         "videoEditor_generateAudioWaveFormSync pContext->bSkipState ");
 
diff --git a/media/jni/mediaeditor/VideoEditorPropertiesMain.cpp b/media/jni/mediaeditor/VideoEditorPropertiesMain.cpp
index 93fe702..2ca3a08 100755
--- a/media/jni/mediaeditor/VideoEditorPropertiesMain.cpp
+++ b/media/jni/mediaeditor/VideoEditorPropertiesMain.cpp
@@ -248,7 +248,11 @@
                 pProperties->uiVideoWidth        = pClipProperties->uiVideoWidth;
                 pProperties->uiVideoHeight       = pClipProperties->uiVideoHeight;
                 pProperties->fAverageFrameRate   = pClipProperties->fAverageFrameRate;
-                pProperties->ProfileAndLevel     = pClipProperties->ProfileAndLevel;
+                pProperties->uiVideoProfile      = pClipProperties->uiVideoProfile;
+                pProperties->uiVideoLevel        = pClipProperties->uiVideoLevel;
+                // Set profile and level support to TRUE, pending check
+                pProperties->bProfileSupported   = M4OSA_TRUE;
+                pProperties->bLevelSupported     = M4OSA_TRUE;
                 pProperties->AudioStreamType     = pClipProperties->AudioStreamType;
                 pProperties->uiClipAudioDuration = pClipProperties->uiClipAudioDuration;
                 pProperties->uiAudioBitrate      = pClipProperties->uiAudioBitrate;
@@ -272,7 +276,8 @@
             pProperties->uiVideoWidth        = width;
             pProperties->uiVideoHeight       = height;
             pProperties->fAverageFrameRate   = 0.0f;
-            pProperties->ProfileAndLevel     = M4VIDEOEDITING_kProfile_and_Level_Out_Of_Range;
+            pProperties->uiVideoProfile = M4VIDEOEDITING_VIDEO_UNKNOWN_PROFILE;
+            pProperties->uiVideoLevel = M4VIDEOEDITING_VIDEO_UNKNOWN_LEVEL;
             pProperties->AudioStreamType     = M4VIDEOEDITING_kNoneAudio;
             pProperties->uiClipAudioDuration = 0;
             pProperties->uiAudioBitrate      = 0;
@@ -291,11 +296,13 @@
         }
     }
 
-    // Create a properties object.
-    videoEditPropClass_createProperties(&gotten, pEnv, pProperties, &properties);
+    if (M4NO_ERROR == result) {
+        // Create a properties object.
+        videoEditPropClass_createProperties(&gotten, pEnv, pProperties, &properties);
 
-    // Log the properties.
-    VIDEOEDIT_PROP_LOG_PROPERTIES(pProperties);
+        // Log the properties.
+        VIDEOEDIT_PROP_LOG_PROPERTIES(pProperties);
+    }
 
     // Free the properties.
     videoEditOsal_free(pProperties);
diff --git a/media/libmedia/MediaProfiles.cpp b/media/libmedia/MediaProfiles.cpp
index f0f07a2..5a8bc60 100644
--- a/media/libmedia/MediaProfiles.cpp
+++ b/media/libmedia/MediaProfiles.cpp
@@ -26,6 +26,7 @@
 #include <expat.h>
 #include <media/MediaProfiles.h>
 #include <media/stagefright/MediaDebug.h>
+#include <media/stagefright/openmax/OMX_Video.h>
 
 namespace android {
 
@@ -377,7 +378,24 @@
     LOGV("%s: cameraId=%d, offset=%d ms", __func__, cameraId, offsetTimeMs);
     mStartTimeOffsets.replaceValueFor(cameraId, offsetTimeMs);
 }
+/*static*/ MediaProfiles::ExportVideoProfile*
+MediaProfiles::createExportVideoProfile(const char **atts)
+{
+    CHECK(!strcmp("name", atts[0]) &&
+          !strcmp("profile", atts[2]) &&
+          !strcmp("level", atts[4]));
 
+    const size_t nMappings =
+        sizeof(sVideoEncoderNameMap)/sizeof(sVideoEncoderNameMap[0]);
+    const int codec = findTagForName(sVideoEncoderNameMap, nMappings, atts[1]);
+    CHECK(codec != -1);
+
+    MediaProfiles::ExportVideoProfile *profile =
+        new MediaProfiles::ExportVideoProfile(
+            codec, atoi(atts[3]), atoi(atts[5]));
+
+    return profile;
+}
 /*static*/ MediaProfiles::VideoEditorCap*
 MediaProfiles::createVideoEditorCap(const char **atts, MediaProfiles *profiles)
 {
@@ -428,6 +446,8 @@
         profiles->addImageEncodingQualityLevel(profiles->mCurrentCameraId, atts);
     } else if (strcmp("VideoEditorCap", name) == 0) {
         createVideoEditorCap(atts, profiles);
+    } else if (strcmp("ExportVideoProfile", name) == 0) {
+        profiles->mVideoEditorExportProfiles.add(createExportVideoProfile(atts));
     }
 }
 
@@ -830,6 +850,20 @@
                 VIDEOEDITOR_DEFAULT_MAX_OUTPUT_FRAME_WIDTH,
                 VIDEOEDITOR_DEFUALT_MAX_OUTPUT_FRAME_HEIGHT);
 }
+/*static*/ void
+MediaProfiles::createDefaultExportVideoProfiles(MediaProfiles *profiles)
+{
+    // Create default video export profiles
+    profiles->mVideoEditorExportProfiles.add(
+        new ExportVideoProfile(VIDEO_ENCODER_H263,
+            OMX_VIDEO_H263ProfileBaseline, OMX_VIDEO_H263Level10));
+    profiles->mVideoEditorExportProfiles.add(
+        new ExportVideoProfile(VIDEO_ENCODER_MPEG_4_SP,
+            OMX_VIDEO_MPEG4ProfileSimple, OMX_VIDEO_MPEG4Level1));
+    profiles->mVideoEditorExportProfiles.add(
+        new ExportVideoProfile(VIDEO_ENCODER_H264,
+            OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel13));
+}
 
 /*static*/ MediaProfiles*
 MediaProfiles::createDefaultInstance()
@@ -843,6 +877,7 @@
     createDefaultEncoderOutputFileFormats(profiles);
     createDefaultImageEncodingQualityLevels(profiles);
     createDefaultVideoEditorCap(profiles);
+    createDefaultExportVideoProfiles(profiles);
     return profiles;
 }
 
@@ -940,7 +975,31 @@
     LOGE("The given video encoder param name %s is not found", name);
     return -1;
 }
+int MediaProfiles::getVideoEditorExportParamByName(
+    const char *name, int codec) const
+{
+    LOGV("getVideoEditorExportParamByName: name %s codec %d", name, codec);
+    ExportVideoProfile *exportProfile = NULL;
+    int index = -1;
+    for (size_t i =0; i < mVideoEditorExportProfiles.size(); i++) {
+        exportProfile = mVideoEditorExportProfiles[i];
+        if (exportProfile->mCodec == codec) {
+            index = i;
+            break;
+        }
+    }
+    if (index == -1) {
+        LOGE("The given video decoder %d is not found", codec);
+        return -1;
+    }
+    if (!strcmp("videoeditor.export.profile", name))
+        return exportProfile->mProfile;
+    if (!strcmp("videoeditor.export.level", name))
+        return exportProfile->mLevel;
 
+    LOGE("The given video editor export param name %s is not found", name);
+    return -1;
+}
 int MediaProfiles::getVideoEditorCapParamByName(const char *name) const
 {
     LOGV("getVideoEditorCapParamByName: %s", name);
diff --git a/media/libstagefright/codecs/on2/dec/SoftVPX.cpp b/media/libstagefright/codecs/on2/dec/SoftVPX.cpp
index 7e83163..61a02ac 100644
--- a/media/libstagefright/codecs/on2/dec/SoftVPX.cpp
+++ b/media/libstagefright/codecs/on2/dec/SoftVPX.cpp
@@ -66,7 +66,7 @@
     def.eDir = OMX_DirInput;
     def.nBufferCountMin = kNumBuffers;
     def.nBufferCountActual = def.nBufferCountMin;
-    def.nBufferSize = 8192;
+    def.nBufferSize = 256 * 1024;
     def.bEnabled = OMX_TRUE;
     def.bPopulated = OMX_FALSE;
     def.eDomain = OMX_PortDomainVideo;
diff --git a/media/libstagefright/omx/OMXNodeInstance.cpp b/media/libstagefright/omx/OMXNodeInstance.cpp
index 12ab941..e615d93 100644
--- a/media/libstagefright/omx/OMXNodeInstance.cpp
+++ b/media/libstagefright/omx/OMXNodeInstance.cpp
@@ -419,7 +419,7 @@
     def.nVersion.s.nStep = 0;
     def.nPortIndex = portIndex;
     OMX_ERRORTYPE err = OMX_GetParameter(mHandle, OMX_IndexParamPortDefinition, &def);
-    if (err != OK)
+    if (err != OMX_ErrorNone)
     {
         LOGE("%s::%d:Error getting OMX_IndexParamPortDefinition", __FUNCTION__, __LINE__);
         return err;
diff --git a/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/functional/MediaPropertiesTest.java b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/functional/MediaPropertiesTest.java
index 4d517db..0ad6760 100755
--- a/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/functional/MediaPropertiesTest.java
+++ b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/functional/MediaPropertiesTest.java
@@ -71,7 +71,7 @@
 
     protected void validateVideoProperties(int aspectRatio, int fileType,
         int videoCodecType, int duration, int videoBitrate, int fps,
-        int videoProfile, int width, int height, int audioCodecType,
+        int videoProfile, int videoLevel, int width, int height, int audioCodecType,
         int audioSamplingFrequency, int audioChannel, int audioBitrate,
         MediaVideoItem mvi) throws Exception {
         assertEquals("Aspect Ratio Mismatch", aspectRatio, mvi.getAspectRatio());
@@ -82,6 +82,8 @@
             duration, mvi.getDuration(), 10));
         assertEquals("Video Profile " + mvi.getVideoProfile(), videoProfile,
             mvi.getVideoProfile());
+        assertEquals("Video Level " + mvi.getVideoLevel(), videoLevel,
+            mvi.getVideoLevel());
         assertEquals("Video height " + mvi.getHeight(), height, mvi.getHeight());
         assertEquals("Video width " + mvi.getWidth(), width, mvi.getWidth());
         /** Check FPS with 10% range */
@@ -143,7 +145,8 @@
         final int audioCodecType = MediaProperties.ACODEC_AAC_LC;
         final int audioSamplingFrequency = 16000;
         final int audioChannel = 2;
-        final int videoProfile = MediaProperties.MPEG4_SP_LEVEL_1;
+        final int videoProfile = MediaProperties.MPEG4Profile.MPEG4ProfileSimple;
+        final int videoLevel = MediaProperties.MPEG4Level.MPEG4Level1;
         final int width = 854;
         final int height = MediaProperties.HEIGHT_480;
 
@@ -152,7 +155,7 @@
             MediaItem.RENDERING_MODE_BLACK_BORDER);
 
         validateVideoProperties(aspectRatio, fileType, videoCodecType, duration,
-            videoBitrate, fps, videoProfile, width, height, audioCodecType,
+            videoBitrate, fps, videoProfile, videoLevel, width, height, audioCodecType,
             audioSamplingFrequency, audioChannel, audioBitrate, mvi);
     }
 
@@ -175,7 +178,8 @@
         final int audioCodecType = MediaProperties.ACODEC_AMRNB;
         final int audioSamplingFrequency = 8000;
         final int audioChannel = 1;
-        final int videoProfile = MediaProperties.MPEG4_SP_LEVEL_1;
+        final int videoProfile = MediaProperties.MPEG4Profile.MPEG4ProfileSimple;
+        final int videoLevel = MediaProperties.MPEG4Level.MPEG4Level1;
         final int width = 800;
         final int height = MediaProperties.HEIGHT_480;
         final int renderingMode = MediaItem.RENDERING_MODE_BLACK_BORDER;
@@ -184,7 +188,7 @@
             (mVideoEditor, "m1", videoItemFilename, renderingMode);
 
         validateVideoProperties(aspectRatio, fileType, videoCodecType, duration,
-            videoBitrate, fps, videoProfile, width, height, audioCodecType,
+            videoBitrate, fps, videoProfile, videoLevel, width, height, audioCodecType,
             audioSamplingFrequency, audioChannel, audioBitrate, mvi);
     }
 
@@ -206,7 +210,8 @@
         final int audioCodecType = MediaProperties.ACODEC_AAC_LC;
         final int audioSamplingFrequency = 48000;
         final int audioChannel = 2;
-        final int videoProfile = MediaProperties.MPEG4_SP_LEVEL_1;
+        final int videoProfile = MediaProperties.MPEG4Profile.MPEG4ProfileSimple;
+        final int videoLevel = MediaProperties.MPEG4Level.MPEG4Level1;
         final int width = 720;
         final int height = MediaProperties.HEIGHT_480;
         final int renderingMode = MediaItem.RENDERING_MODE_BLACK_BORDER;
@@ -215,7 +220,7 @@
             (mVideoEditor, "m1", videoItemFilename, renderingMode);
 
         validateVideoProperties(aspectRatio, fileType, videoCodecType, duration,
-            videoBitrate, fps, videoProfile, width, height, audioCodecType,
+            videoBitrate, fps, videoProfile, videoLevel, width, height, audioCodecType,
             audioSamplingFrequency, audioChannel, audioBitrate, mvi);
     }
 
@@ -237,7 +242,8 @@
         final int audioCodecType = MediaProperties.ACODEC_AAC_LC;
         final int audioSamplingFrequency = 48000;
         final int audioChannel = 2;
-        final int videoProfile = MediaProperties.MPEG4_SP_LEVEL_1;
+        final int videoProfile = MediaProperties.MPEG4Profile.MPEG4ProfileSimple;
+        final int videoLevel = MediaProperties.MPEG4Level.MPEG4Level1;
         final int width = 640;
         final int height = MediaProperties.HEIGHT_480;
         final int renderingMode = MediaItem.RENDERING_MODE_BLACK_BORDER;
@@ -246,7 +252,7 @@
             (mVideoEditor, "m1", videoItemFilename, renderingMode);
 
         validateVideoProperties(aspectRatio, fileType, videoCodecType, duration,
-            videoBitrate, fps, videoProfile, width, height, audioCodecType,
+            videoBitrate, fps, videoProfile, videoLevel, width, height, audioCodecType,
             audioSamplingFrequency, audioChannel, audioBitrate, mvi);
     }
 
@@ -268,7 +274,8 @@
         final int audioCodecType = MediaProperties.ACODEC_AMRNB;
         final int audioSamplingFrequency = 8000;
         final int audioChannel = 1;
-        final int videoProfile = MediaProperties.MPEG4_SP_LEVEL_1;
+        final int videoProfile = MediaProperties.MPEG4Profile.MPEG4ProfileSimple;
+        final int videoLevel = MediaProperties.MPEG4Level.MPEG4Level1;
         final int width = 176;
         final int height = MediaProperties.HEIGHT_144;
         final int renderingMode = MediaItem.RENDERING_MODE_BLACK_BORDER;
@@ -277,7 +284,7 @@
             (mVideoEditor, "m1", videoItemFilename, renderingMode);
 
         validateVideoProperties(aspectRatio, fileType, videoCodecType, duration,
-            videoBitrate, fps, videoProfile, width, height, audioCodecType,
+            videoBitrate, fps, videoProfile, videoLevel, width, height, audioCodecType,
             audioSamplingFrequency, audioChannel, audioBitrate, mvi);
     }
 
@@ -299,7 +306,8 @@
         final int audioCodecType = MediaProperties.ACODEC_AAC_LC;
         final int audioSamplingFrequency = 16000;
         final int audioChannel = 1;
-        final int videoProfile = MediaProperties.H263_PROFILE_0_LEVEL_10;
+        final int videoProfile = MediaProperties.H263Profile.H263ProfileBaseline;
+        final int videoLevel = MediaProperties.H263Level.H263Level10;
         final int width = 176;
         final int height = MediaProperties.HEIGHT_144;
         final int renderingMode = MediaItem.RENDERING_MODE_BLACK_BORDER;
@@ -307,7 +315,7 @@
             (mVideoEditor, "m1", videoItemFilename, renderingMode);
 
         validateVideoProperties(aspectRatio, fileType, videoCodecType, duration,
-            videoBitrate, fps, videoProfile, width, height, audioCodecType,
+            videoBitrate, fps, videoProfile, videoLevel, width, height, audioCodecType,
             audioSamplingFrequency, audioChannel, audioBitrate, mvi);
     }
 
@@ -321,7 +329,7 @@
             + "H264_BP_640x480_15fps_1200Kbps_AACLC_48KHz_64kps_m_0_27.3gp";
         final int aspectRatio = MediaProperties.ASPECT_RATIO_4_3;
         final int fileType = MediaProperties.FILE_3GP;
-        final int videoCodecType = MediaProperties.VCODEC_H264BP;
+        final int videoCodecType = MediaProperties.VCODEC_H264;
         final int duration = 77600;
         final int videoBitrate = 745000;
         final int audioBitrate = 64000;
@@ -329,7 +337,8 @@
         final int audioCodecType = MediaProperties.ACODEC_AAC_LC;
         final int audioSamplingFrequency = 48000;
         final int audioChannel = 2;
-        final int videoProfile = MediaProperties.H264_PROFILE_0_LEVEL_1_3;
+        final int videoProfile = MediaProperties.H264Profile.H264ProfileBaseline;
+        final int videoLevel = MediaProperties.H264Level.H264Level13;
         final int width = 640;
         final int height = MediaProperties.HEIGHT_480;
         final int renderingMode = MediaItem.RENDERING_MODE_BLACK_BORDER;
@@ -337,7 +346,7 @@
             (mVideoEditor, "m1", videoItemFilename, renderingMode);
 
         validateVideoProperties(aspectRatio, fileType, videoCodecType, duration,
-            videoBitrate, fps, videoProfile, width, height, audioCodecType,
+            videoBitrate, fps, videoProfile, videoLevel, width, height, audioCodecType,
             audioSamplingFrequency, audioChannel, audioBitrate, mvi);
     }
 
@@ -351,7 +360,7 @@
             + "H264_BP_720x480_25fps_256kbps_AMRNB_8khz_12.2kbps_m_0_26.mp4";
         final int aspectRatio = MediaProperties.ASPECT_RATIO_3_2;
         final int fileType = MediaProperties.FILE_MP4;
-        final int videoCodecType = MediaProperties.VCODEC_H264BP;
+        final int videoCodecType = MediaProperties.VCODEC_H264;
         final int duration = 26880;
         final int videoBitrate = 244000;
         final int audioBitrate = 12200;
@@ -359,7 +368,8 @@
         final int audioCodecType = MediaProperties.ACODEC_AMRNB;
         final int audioSamplingFrequency = 8000;
         final int audioChannel = 1;
-        final int videoProfile = MediaProperties.H264_PROFILE_0_LEVEL_1_3;
+        final int videoProfile = MediaProperties.H264Profile.H264ProfileBaseline;
+        final int videoLevel = MediaProperties.H264Level.H264Level13;
         final int width = 720;
         final int height = MediaProperties.HEIGHT_480;
         final int renderingMode = MediaItem.RENDERING_MODE_BLACK_BORDER;
@@ -368,7 +378,7 @@
             (mVideoEditor, "m1", videoItemFilename, renderingMode);
 
         validateVideoProperties(aspectRatio, fileType, videoCodecType, duration,
-            videoBitrate, fps, videoProfile, width, height, audioCodecType,
+            videoBitrate, fps, videoProfile, videoLevel, width, height, audioCodecType,
             audioSamplingFrequency, audioChannel, audioBitrate, mvi);
     }
 
@@ -382,7 +392,7 @@
               "H264_BP_800x480_15fps_512kbps_AACLC_24KHz_38Kbps_s_1_17.mp4";
         final int aspectRatio = MediaProperties.ASPECT_RATIO_5_3;
         final int fileType = MediaProperties.FILE_MP4;
-        final int videoCodecType = MediaProperties.VCODEC_H264BP;
+        final int videoCodecType = MediaProperties.VCODEC_H264;
         final int duration = 77466;
         final int videoBitrate = 528000;
         final int audioBitrate = 38000;
@@ -390,7 +400,8 @@
         final int audioCodecType = MediaProperties.ACODEC_AAC_LC;
         final int audioSamplingFrequency = 24000;
         final int audioChannel = 2;
-        final int videoProfile = MediaProperties.H264_PROFILE_0_LEVEL_1_3;
+        final int videoProfile = MediaProperties.H264Profile.H264ProfileBaseline;
+        final int videoLevel = MediaProperties.H264Level.H264Level13;
         final int width = 800;
         final int height = MediaProperties.HEIGHT_480;
         final int renderingMode = MediaItem.RENDERING_MODE_BLACK_BORDER;
@@ -399,7 +410,7 @@
             (mVideoEditor, "m1", videoItemFilename, renderingMode);
 
         validateVideoProperties(aspectRatio, fileType, videoCodecType, duration,
-            videoBitrate, fps, videoProfile, width, height, audioCodecType,
+            videoBitrate, fps, videoProfile, videoLevel, width, height, audioCodecType,
             audioSamplingFrequency, audioChannel, audioBitrate, mvi);
     }
 
@@ -413,7 +424,7 @@
             + "H264_BP_1280x720_15fps_512kbps_AACLC_16khz_48kbps_s_1_17.mp4";
         final int aspectRatio = MediaProperties.ASPECT_RATIO_16_9;
         final int fileType = MediaProperties.FILE_MP4;
-        final int videoCodecType = MediaProperties.VCODEC_H264BP;
+        final int videoCodecType = MediaProperties.VCODEC_H264;
         final int duration = 77600;
         final int videoBitrate = 606000;
         final int audioBitrate = 48000;
@@ -421,7 +432,8 @@
         final int audioCodecType = MediaProperties.ACODEC_AAC_LC;
         final int audioSamplingFrequency = 16000;
         final int audioChannel = 2;
-        final int videoProfile = MediaProperties.H264_PROFILE_0_LEVEL_1_3;
+        final int videoProfile = MediaProperties.H264Profile.H264ProfileBaseline;
+        final int videoLevel = MediaProperties.H264Level.H264Level13;
         final int width = 1280;
         final int height = MediaProperties.HEIGHT_720;
         final int renderingMode = MediaItem.RENDERING_MODE_BLACK_BORDER;
@@ -430,7 +442,7 @@
             (mVideoEditor, "m1", videoItemFilename, renderingMode);
 
         validateVideoProperties(aspectRatio, fileType, videoCodecType, duration,
-            videoBitrate, fps, videoProfile, width, height, audioCodecType,
+            videoBitrate, fps, videoProfile, videoLevel, width, height, audioCodecType,
             audioSamplingFrequency, audioChannel, audioBitrate, mvi);
     }
 
@@ -444,7 +456,7 @@
             + "H264_BP_1080x720_30fps_12Mbps_AACLC_44.1khz_64kbps_s_1_17.mp4";
         final int aspectRatio = MediaProperties.ASPECT_RATIO_3_2;
         final int fileType = MediaProperties.FILE_MP4;
-        final int videoCodecType = MediaProperties.VCODEC_H264BP;
+        final int videoCodecType = MediaProperties.VCODEC_H264;
         final int duration = 77500;
         final int videoBitrate = 1190000;
         final int audioBitrate = 64000;
@@ -452,7 +464,8 @@
         final int audioCodecType = MediaProperties.ACODEC_AAC_LC;
         final int audioSamplingFrequency = 44100;
         final int audioChannel = 2;
-        final int videoProfile = MediaProperties.H264_PROFILE_0_LEVEL_1_3;
+        final int videoProfile = MediaProperties.H264Profile.H264ProfileBaseline;
+        final int videoLevel = MediaProperties.H264Level.H264Level13;
         final int width = 1080;
         final int height = MediaProperties.HEIGHT_720;
         final int renderingMode = MediaItem.RENDERING_MODE_BLACK_BORDER;
@@ -461,7 +474,7 @@
             (mVideoEditor, "m1", videoItemFilename, renderingMode);
 
         validateVideoProperties(aspectRatio, fileType, videoCodecType, duration,
-            videoBitrate, fps, videoProfile, width, height, audioCodecType,
+            videoBitrate, fps, videoProfile, videoLevel, width, height, audioCodecType,
             audioSamplingFrequency, audioChannel, audioBitrate, mvi);
     }
 
@@ -491,7 +504,7 @@
     }
 
     /**
-     *To test media properties for H.264 Main/Advanced profile. (unsupported profile input)
+     *To test media properties for H.264 Main/Advanced profile.
      */
     // TODO : Remove TC_MP_013
     @LargeTest
@@ -499,19 +512,28 @@
         final String videoItemFilename = INPUT_FILE_PATH
             + "H264_MP_960x720_25fps_800kbps_AACLC_48Khz_192Kbps_s_1_17.mp4";
         final int aspectRatio = MediaProperties.ASPECT_RATIO_4_3;
-        //final int videoCodecType = MediaProperties.VCODEC_H264BP;
-        final int videoCodecType = MediaProperties.VCODEC_H264MP;
+        final int videoCodecType = MediaProperties.VCODEC_H264;
+        final int fileType = MediaProperties.FILE_MP4;
+        final int duration = 77500;
+        final int videoBitrate = 800000;
+        final int audioBitrate = 192000;
+        final int fps = 25;
+        final int audioCodecType = MediaProperties.ACODEC_AAC_LC;
+        final int audioSamplingFrequency = 48000;
+        final int audioChannel = 2;
+        final int videoProfile = MediaProperties.H264Profile.H264ProfileMain;
+        final int videoLevel = MediaProperties.H264Level.H264Level31;
+        final int width = 960;
+        final int height = MediaProperties.HEIGHT_720;
         final int renderingMode = MediaItem.RENDERING_MODE_BLACK_BORDER;
-        boolean flagForException = false;
 
-        try {
         final MediaVideoItem mvi = mVideoEditorHelper.createMediaItem
             (mVideoEditor, "m1", videoItemFilename, renderingMode);
-            assertEquals("VideoCodec Mismatch", videoCodecType, mvi.getVideoType());
-        }catch (IllegalArgumentException e){
-            flagForException = true;
-        }
-            assertTrue("Unsupported Main Profile", flagForException);
+
+        validateVideoProperties(aspectRatio, fileType, videoCodecType, duration,
+            videoBitrate, fps, videoProfile, videoLevel, width, height, audioCodecType,
+            audioSamplingFrequency, audioChannel, audioBitrate, mvi);
+
     }
 
     /**
@@ -544,7 +566,7 @@
             "H264_BP_1080x720_30fps_800kbps_1_17.mp4";
         final int aspectRatio = MediaProperties.ASPECT_RATIO_3_2;
         final int fileType = MediaProperties.FILE_MP4;
-        final int videoCodecType = MediaProperties.VCODEC_H264BP;
+        final int videoCodecType = MediaProperties.VCODEC_H264;
         final int duration = 77366;
         final int videoBitrate = 859000;
         final int audioBitrate = 0;
@@ -552,7 +574,8 @@
         final int audioCodecType = -1;
         final int audioSamplingFrequency = 0;
         final int audioChannel = 0;
-        final int videoProfile = MediaProperties.H264_PROFILE_0_LEVEL_1_3;
+        final int videoProfile = MediaProperties.H264Profile.H264ProfileBaseline;
+        final int videoLevel = MediaProperties.H264Level.H264Level13;
         final int width = 1080;
         final int height = MediaProperties.HEIGHT_720;
         final int renderingMode = MediaItem.RENDERING_MODE_BLACK_BORDER;
@@ -561,7 +584,7 @@
             (mVideoEditor, "m1", videoItemFilename, renderingMode);
 
         validateVideoProperties(aspectRatio, fileType, videoCodecType, duration,
-            videoBitrate, fps, videoProfile, width, height, audioCodecType,
+            videoBitrate, fps, videoProfile, videoLevel, width, height, audioCodecType,
             audioSamplingFrequency, audioChannel, audioBitrate, mvi);
     }
 
diff --git a/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/functional/VideoEditorExportTest.java b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/functional/VideoEditorExportTest.java
index 74d4766..e1b337d 100755
--- a/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/functional/VideoEditorExportTest.java
+++ b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/functional/VideoEditorExportTest.java
@@ -166,7 +166,7 @@
             + mediaImageItem6.getDuration();
         mVideoEditorHelper.validateExport(mVideoEditor, outFilename,
             MediaProperties.HEIGHT_720, 0, storyBoardDuration,
-            MediaProperties.VCODEC_H264BP, MediaProperties.ACODEC_AAC_LC);
+            MediaProperties.VCODEC_H264, MediaProperties.ACODEC_AAC_LC);
         mVideoEditorHelper.checkDeleteExistingFile(outFilename);
     }
 
@@ -294,7 +294,7 @@
             + mediaImageItem6.getDuration();
         mVideoEditorHelper.validateExport(mVideoEditor, outFilename,
             MediaProperties.HEIGHT_720, 0, storyBoardDuration,
-            MediaProperties.VCODEC_H264BP, MediaProperties.ACODEC_AAC_LC);
+            MediaProperties.VCODEC_H264, MediaProperties.ACODEC_AAC_LC);
         mVideoEditorHelper.checkDeleteExistingFile(outFilename);
     }
 
@@ -352,7 +352,7 @@
         }
         mVideoEditorHelper.validateExport(mVideoEditor, outFilename,
             MediaProperties.HEIGHT_720, 0, mediaImageItem.getDuration(),
-            MediaProperties.VCODEC_H264BP, MediaProperties.ACODEC_AAC_LC);
+            MediaProperties.VCODEC_H264, MediaProperties.ACODEC_AAC_LC);
         mVideoEditorHelper.checkDeleteExistingFile(outFilename);
     }
 
@@ -412,7 +412,7 @@
         mVideoEditorHelper.validateExport(mVideoEditor, outFilename,
             MediaProperties.HEIGHT_720, 0, (mediaVideoItem.getTimelineDuration() +
             mediaImageItem.getDuration()),
-            MediaProperties.VCODEC_H264BP, MediaProperties.ACODEC_AAC_LC);
+            MediaProperties.VCODEC_H264, MediaProperties.ACODEC_AAC_LC);
 
         mVideoEditorHelper.checkDeleteExistingFile(outFilename);
     }
@@ -531,7 +531,7 @@
             + mediaItem6.getTimelineDuration() - transition5And6.getDuration();
         mVideoEditorHelper.validateExport(mVideoEditor, outFilename,
             MediaProperties.HEIGHT_720, 0, storyBoardDuration,
-            MediaProperties.VCODEC_H264BP, MediaProperties.ACODEC_AAC_LC);
+            MediaProperties.VCODEC_H264, MediaProperties.ACODEC_AAC_LC);
         mVideoEditorHelper.checkDeleteExistingFile(outFilename);
     }
 
@@ -693,7 +693,7 @@
 
         mVideoEditorHelper.validateExport(mVideoEditor, outFilename,
             MediaProperties.HEIGHT_144, 0, storyBoardDuration,
-            MediaProperties.VCODEC_H264BP, MediaProperties.ACODEC_AAC_LC);
+            MediaProperties.VCODEC_H264, MediaProperties.ACODEC_AAC_LC);
          mVideoEditorHelper.checkDeleteExistingFile(outFilename);
     }
 
@@ -730,7 +730,7 @@
         }
         mVideoEditorHelper.validateExport(mVideoEditor, outFilename,
             MediaProperties.HEIGHT_720, 0, mediaItem1.getDuration(),
-            MediaProperties.VCODEC_H264BP, MediaProperties.ACODEC_AAC_LC);
+            MediaProperties.VCODEC_H264, MediaProperties.ACODEC_AAC_LC);
         mVideoEditorHelper.checkDeleteExistingFile(outFilename);
     }
 
@@ -814,7 +814,7 @@
         mVideoEditorHelper.validateExport(mVideoEditor, outFilename,
             MediaProperties.HEIGHT_720, 0,
             (mediaVideoItem.getDuration()+ mediaVideoItem1.getDuration()),
-            MediaProperties.VCODEC_H264BP, MediaProperties.ACODEC_AAC_LC);
+            MediaProperties.VCODEC_H264, MediaProperties.ACODEC_AAC_LC);
         mVideoEditorHelper.checkDeleteExistingFile(outFilename);
     }
 }
diff --git a/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/performance/VideoEditorPerformance.java b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/performance/VideoEditorPerformance.java
index 7eb6d22..3d0be4f 100644
--- a/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/performance/VideoEditorPerformance.java
+++ b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/performance/VideoEditorPerformance.java
@@ -366,7 +366,7 @@
         final int renderingMode = MediaItem.RENDERING_MODE_BLACK_BORDER;
         final int outHeight = MediaProperties.HEIGHT_480;
         final int outBitrate = MediaProperties.BITRATE_256K;
-        final int outVcodec = MediaProperties.VCODEC_H264BP;
+        final int outVcodec = MediaProperties.VCODEC_H264;
         final String[] loggingInfo = new String[1];
         final String outFilename = mVideoEditorHelper
             .createRandomFile(mVideoEditor.getPath() + "/") + ".3gp";
@@ -639,11 +639,12 @@
         final int renderingMode = MediaItem.RENDERING_MODE_BLACK_BORDER;
         final int aspectRatio = MediaProperties.ASPECT_RATIO_3_2;
         final int fileType = MediaProperties.FILE_MP4;
-        final int videoCodecType = MediaProperties.VCODEC_H264BP;
+        final int videoCodecType = MediaProperties.VCODEC_H264;
         final int duration = 77366;
         final int videoBitrate = 3169971;
         final int fps = 30;
-        final int videoProfile = MediaProperties.H264_PROFILE_0_LEVEL_1_3;
+        final int videoProfile = MediaProperties.H264Profile.H264ProfileBaseline;
+        final int videoLevel = MediaProperties.H264Level.H264Level13;
         final int width = 1080;
         final int height = MediaProperties.HEIGHT_720;
         int timeTaken = 0;
@@ -665,6 +666,8 @@
                     duration, mediaVideoItem.getDuration());
                 assertEquals("Video Profile ",
                     videoProfile, mediaVideoItem.getVideoProfile());
+                assertEquals("Video Level ",
+                    videoLevel, mediaVideoItem.getVideoLevel());
                 assertEquals("Video height ",
                     height, mediaVideoItem.getHeight());
                 assertEquals("Video width ",
diff --git a/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/stress/VideoEditorStressTest.java b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/stress/VideoEditorStressTest.java
index 94ffb8e..4d30784 100755
--- a/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/stress/VideoEditorStressTest.java
+++ b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/stress/VideoEditorStressTest.java
@@ -665,9 +665,10 @@
         int i = 0;
         final int videoAspectRatio = MediaProperties.ASPECT_RATIO_3_2;
         final int videoFileType = MediaProperties.FILE_MP4;
-        final int videoCodecType = MediaProperties.VCODEC_H264BP;
+        final int videoCodecType = MediaProperties.VCODEC_H264;
         final int videoDuration = 77366;
-        final int videoProfile = MediaProperties.H264_PROFILE_0_LEVEL_1_3;
+        final int videoProfile = MediaProperties.H264Profile.H264ProfileBaseline;
+        final int videoLevel = MediaProperties.H264Level.H264Level13;
         final int videoHeight = MediaProperties.HEIGHT_720;
         final int videoWidth = 1080;
 
@@ -698,6 +699,8 @@
                     videoDuration, mediaItem1.getDuration());
                 assertEquals("Video Profile ",
                     videoProfile, mediaItem1.getVideoProfile());
+                assertEquals("Video Level ",
+                    videoLevel, mediaItem1.getVideoLevel());
                 assertEquals("Video height ",
                     videoHeight, mediaItem1.getHeight());
                 assertEquals("Video width ",
@@ -939,9 +942,9 @@
 
         for ( i = 0; i < 50; i++) {
             if(i%4 ==0){
-                final int aspectRatio = MediaProperties.ASPECT_RATIO_4_3;
+                final int aspectRatio = MediaProperties.ASPECT_RATIO_11_9;
                 mVideoEditor.setAspectRatio(aspectRatio);
-                mVideoEditor.export(outFilename, MediaProperties.HEIGHT_480,
+                mVideoEditor.export(outFilename, MediaProperties.HEIGHT_288,
                     MediaProperties.BITRATE_256K,MediaProperties.ACODEC_AAC_LC,
                         MediaProperties.VCODEC_H263,
                         new ExportProgressListener() {
@@ -967,7 +970,7 @@
                 mVideoEditor.setAspectRatio(aspectRatio);
                 mVideoEditor.export(outFilename, MediaProperties.HEIGHT_144,
                     MediaProperties.BITRATE_512K,MediaProperties.ACODEC_AAC_LC,
-                        MediaProperties.VCODEC_H264BP,
+                        MediaProperties.VCODEC_H264,
                         new ExportProgressListener() {
                         public void onProgress(VideoEditor ve, String outFileName,
                             int progress) {
@@ -979,7 +982,7 @@
                 mVideoEditor.setAspectRatio(aspectRatio);
                 mVideoEditor.export(outFilename, MediaProperties.HEIGHT_480,
                     MediaProperties.BITRATE_800K,MediaProperties.ACODEC_AAC_LC,
-                        MediaProperties.VCODEC_H264BP,
+                        MediaProperties.VCODEC_H264,
                         new ExportProgressListener() {
                         public void onProgress(VideoEditor ve, String outFileName,
                             int progress) {
diff --git a/native/include/android/input.h b/native/include/android/input.h
index 7a0dcd3..f2befa9e 100644
--- a/native/include/android/input.h
+++ b/native/include/android/input.h
@@ -373,6 +373,7 @@
     AMOTION_EVENT_AXIS_GAS = 22,
     AMOTION_EVENT_AXIS_BRAKE = 23,
     AMOTION_EVENT_AXIS_DISTANCE = 24,
+    AMOTION_EVENT_AXIS_TILT = 25,
     AMOTION_EVENT_AXIS_GENERIC_1 = 32,
     AMOTION_EVENT_AXIS_GENERIC_2 = 33,
     AMOTION_EVENT_AXIS_GENERIC_3 = 34,
diff --git a/opengl/libagl/state.cpp b/opengl/libagl/state.cpp
index 8b4136a..90e9612 100644
--- a/opengl/libagl/state.cpp
+++ b/opengl/libagl/state.cpp
@@ -191,6 +191,9 @@
         // these need to fall through into the rasterizer
         c->rasterizer.procs.enableDisable(c, cap, enabled);
         break;
+    case GL_TEXTURE_EXTERNAL_OES:
+        c->rasterizer.procs.enableDisable(c, GL_TEXTURE_2D, enabled);
+        break;
 
     case GL_MULTISAMPLE:
     case GL_SAMPLE_ALPHA_TO_COVERAGE:
diff --git a/opengl/libagl/texture.cpp b/opengl/libagl/texture.cpp
index 8eb17c4..88e8651 100644
--- a/opengl/libagl/texture.cpp
+++ b/opengl/libagl/texture.cpp
@@ -633,7 +633,7 @@
 static void texParameterx(
         GLenum target, GLenum pname, GLfixed param, ogles_context_t* c)
 {
-    if (target != GL_TEXTURE_2D) {
+    if (target != GL_TEXTURE_2D && target != GL_TEXTURE_EXTERNAL_OES) {
         ogles_error(c, GL_INVALID_ENUM);
         return;
     }
@@ -866,7 +866,7 @@
 void glBindTexture(GLenum target, GLuint texture)
 {
     ogles_context_t* c = ogles_context_t::get();
-    if (target != GL_TEXTURE_2D) {
+    if (target != GL_TEXTURE_2D && target != GL_TEXTURE_EXTERNAL_OES) {
         ogles_error(c, GL_INVALID_ENUM);
         return;
     }
@@ -1012,7 +1012,7 @@
         GLenum target, GLenum pname, const GLint* params)
 {
     ogles_context_t* c = ogles_context_t::get();
-    if (target != GGL_TEXTURE_2D) {
+    if (target != GL_TEXTURE_2D && target != GL_TEXTURE_EXTERNAL_OES) {
         ogles_error(c, GL_INVALID_ENUM);
         return;
     }
@@ -1605,7 +1605,7 @@
 void glEGLImageTargetTexture2DOES(GLenum target, GLeglImageOES image)
 {
     ogles_context_t* c = ogles_context_t::get();
-    if (target != GL_TEXTURE_2D) {
+    if (target != GL_TEXTURE_2D && target != GL_TEXTURE_EXTERNAL_OES) {
         ogles_error(c, GL_INVALID_ENUM);
         return;
     }
diff --git a/packages/SystemUI/res/drawable-hdpi/compat_mode_help_top_divider.9.png b/packages/SystemUI/res/drawable-hdpi/compat_mode_help_top_divider.9.png
deleted file mode 100644
index 03a5639..0000000
--- a/packages/SystemUI/res/drawable-hdpi/compat_mode_help_top_divider.9.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/ic_notification_open.png b/packages/SystemUI/res/drawable-hdpi/ic_notification_open.png
index 165fa80..d697c2f 100644
--- a/packages/SystemUI/res/drawable-hdpi/ic_notification_open.png
+++ b/packages/SystemUI/res/drawable-hdpi/ic_notification_open.png
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_airplane_off.png b/packages/SystemUI/res/drawable-hdpi/ic_sysbar_airplane_off.png
deleted file mode 100644
index a557164..0000000
--- a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_airplane_off.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_apps_default.png b/packages/SystemUI/res/drawable-hdpi/ic_sysbar_apps_default.png
deleted file mode 100644
index 48cab60..0000000
--- a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_apps_default.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_apps_default_land.png b/packages/SystemUI/res/drawable-hdpi/ic_sysbar_apps_default_land.png
deleted file mode 100644
index 000af5e..0000000
--- a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_apps_default_land.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_back_ime_default_land.png b/packages/SystemUI/res/drawable-hdpi/ic_sysbar_back_ime_default_land.png
deleted file mode 100644
index e77dfafb..0000000
--- a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_back_ime_default_land.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_brightness_low.png b/packages/SystemUI/res/drawable-hdpi/ic_sysbar_brightness_low.png
deleted file mode 100644
index b91be07..0000000
--- a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_brightness_low.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_gps_on.png b/packages/SystemUI/res/drawable-hdpi/ic_sysbar_gps_on.png
deleted file mode 100644
index 31e747e..0000000
--- a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_gps_on.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_ime_default_land.png b/packages/SystemUI/res/drawable-hdpi/ic_sysbar_ime_default_land.png
deleted file mode 100644
index 8883601..0000000
--- a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_ime_default_land.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_notification_dnd.png b/packages/SystemUI/res/drawable-hdpi/ic_sysbar_notification_dnd.png
deleted file mode 100644
index 13b969c..0000000
--- a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_notification_dnd.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_notification_dnd_off.png b/packages/SystemUI/res/drawable-hdpi/ic_sysbar_notification_dnd_off.png
deleted file mode 100644
index 25e7e6f..0000000
--- a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_notification_dnd_off.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_rotate_off_lanscape.png b/packages/SystemUI/res/drawable-hdpi/ic_sysbar_rotate_off_lanscape.png
deleted file mode 100644
index da512e4..0000000
--- a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_rotate_off_lanscape.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_rotate_off_portrait.png b/packages/SystemUI/res/drawable-hdpi/ic_sysbar_rotate_off_portrait.png
deleted file mode 100644
index f2a7c6d..0000000
--- a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_rotate_off_portrait.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_settings.png b/packages/SystemUI/res/drawable-hdpi/ic_sysbar_settings.png
deleted file mode 100644
index 18fe5be..0000000
--- a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_settings.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_shadow.9.png b/packages/SystemUI/res/drawable-hdpi/ic_sysbar_shadow.9.png
deleted file mode 100644
index ab581a1..0000000
--- a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_shadow.9.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_sound_off.png b/packages/SystemUI/res/drawable-hdpi/ic_sysbar_sound_off.png
deleted file mode 100644
index 0246cd4..0000000
--- a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_sound_off.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_sound_on.png b/packages/SystemUI/res/drawable-hdpi/ic_sysbar_sound_on.png
deleted file mode 100644
index bec75e5..0000000
--- a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_sound_on.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/recent_overlay.png b/packages/SystemUI/res/drawable-hdpi/recent_overlay.png
deleted file mode 100644
index ce48e0b..0000000
--- a/packages/SystemUI/res/drawable-hdpi/recent_overlay.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/recent_rez_border.png b/packages/SystemUI/res/drawable-hdpi/recent_rez_border.png
deleted file mode 100644
index f10a487..0000000
--- a/packages/SystemUI/res/drawable-hdpi/recent_rez_border.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/shade_handlebar.9.png b/packages/SystemUI/res/drawable-hdpi/shade_handlebar.9.png
deleted file mode 100644
index 0c12103..0000000
--- a/packages/SystemUI/res/drawable-hdpi/shade_handlebar.9.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/shade_header_background.9.png b/packages/SystemUI/res/drawable-hdpi/shade_header_background.9.png
deleted file mode 100644
index 8fbe78e..0000000
--- a/packages/SystemUI/res/drawable-hdpi/shade_header_background.9.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/stat_sys_battery_unknown.png b/packages/SystemUI/res/drawable-hdpi/stat_sys_battery_unknown.png
deleted file mode 100644
index e6c9e805..0000000
--- a/packages/SystemUI/res/drawable-hdpi/stat_sys_battery_unknown.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_in_1x.png b/packages/SystemUI/res/drawable-hdpi/stat_sys_data_in_1x.png
deleted file mode 100644
index b2e725c..0000000
--- a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_in_1x.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_in_3g.png b/packages/SystemUI/res/drawable-hdpi/stat_sys_data_in_3g.png
deleted file mode 100644
index b2e725c..0000000
--- a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_in_3g.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_in_4g.png b/packages/SystemUI/res/drawable-hdpi/stat_sys_data_in_4g.png
deleted file mode 100644
index b2e725c..0000000
--- a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_in_4g.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_in_e.png b/packages/SystemUI/res/drawable-hdpi/stat_sys_data_in_e.png
deleted file mode 100644
index b2e725c..0000000
--- a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_in_e.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_in_g.png b/packages/SystemUI/res/drawable-hdpi/stat_sys_data_in_g.png
deleted file mode 100644
index b2e725c..0000000
--- a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_in_g.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_in_h.png b/packages/SystemUI/res/drawable-hdpi/stat_sys_data_in_h.png
deleted file mode 100644
index b2e725c..0000000
--- a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_in_h.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_inandout_1x.png b/packages/SystemUI/res/drawable-hdpi/stat_sys_data_inandout_1x.png
deleted file mode 100644
index b2e725c..0000000
--- a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_inandout_1x.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_inandout_3g.png b/packages/SystemUI/res/drawable-hdpi/stat_sys_data_inandout_3g.png
deleted file mode 100644
index b2e725c..0000000
--- a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_inandout_3g.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_inandout_4g.png b/packages/SystemUI/res/drawable-hdpi/stat_sys_data_inandout_4g.png
deleted file mode 100644
index b2e725c..0000000
--- a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_inandout_4g.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_inandout_e.png b/packages/SystemUI/res/drawable-hdpi/stat_sys_data_inandout_e.png
deleted file mode 100644
index b2e725c..0000000
--- a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_inandout_e.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_inandout_g.png b/packages/SystemUI/res/drawable-hdpi/stat_sys_data_inandout_g.png
deleted file mode 100644
index b2e725c..0000000
--- a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_inandout_g.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_inandout_h.png b/packages/SystemUI/res/drawable-hdpi/stat_sys_data_inandout_h.png
deleted file mode 100644
index b2e725c..0000000
--- a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_inandout_h.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_out_1x.png b/packages/SystemUI/res/drawable-hdpi/stat_sys_data_out_1x.png
deleted file mode 100644
index b2e725c..0000000
--- a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_out_1x.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_out_3g.png b/packages/SystemUI/res/drawable-hdpi/stat_sys_data_out_3g.png
deleted file mode 100644
index b2e725c..0000000
--- a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_out_3g.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_out_4g.png b/packages/SystemUI/res/drawable-hdpi/stat_sys_data_out_4g.png
deleted file mode 100644
index b2e725c..0000000
--- a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_out_4g.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_out_e.png b/packages/SystemUI/res/drawable-hdpi/stat_sys_data_out_e.png
deleted file mode 100644
index b2e725c..0000000
--- a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_out_e.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_out_g.png b/packages/SystemUI/res/drawable-hdpi/stat_sys_data_out_g.png
deleted file mode 100644
index b2e725c..0000000
--- a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_out_g.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_out_h.png b/packages/SystemUI/res/drawable-hdpi/stat_sys_data_out_h.png
deleted file mode 100644
index b2e725c..0000000
--- a/packages/SystemUI/res/drawable-hdpi/stat_sys_data_out_h.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/statusbar_background.9.png b/packages/SystemUI/res/drawable-hdpi/statusbar_background.9.png
deleted file mode 100644
index 6eaf6c9..0000000
--- a/packages/SystemUI/res/drawable-hdpi/statusbar_background.9.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/title_bar_portrait.9.png b/packages/SystemUI/res/drawable-hdpi/title_bar_portrait.9.png
deleted file mode 100644
index da1b637..0000000
--- a/packages/SystemUI/res/drawable-hdpi/title_bar_portrait.9.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/ic_notification_open.png b/packages/SystemUI/res/drawable-mdpi/ic_notification_open.png
index ba0ca70..839c134 100644
--- a/packages/SystemUI/res/drawable-mdpi/ic_notification_open.png
+++ b/packages/SystemUI/res/drawable-mdpi/ic_notification_open.png
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_airplane_off.png b/packages/SystemUI/res/drawable-mdpi/ic_sysbar_airplane_off.png
deleted file mode 100644
index 6d8b923..0000000
--- a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_airplane_off.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_apps_default.png b/packages/SystemUI/res/drawable-mdpi/ic_sysbar_apps_default.png
deleted file mode 100644
index 2ed5df4..0000000
--- a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_apps_default.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_apps_default_land.png b/packages/SystemUI/res/drawable-mdpi/ic_sysbar_apps_default_land.png
deleted file mode 100644
index c877f72..0000000
--- a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_apps_default_land.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_back_ime_default_land.png b/packages/SystemUI/res/drawable-mdpi/ic_sysbar_back_ime_default_land.png
deleted file mode 100644
index 14d437e..0000000
--- a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_back_ime_default_land.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_brightness_low.png b/packages/SystemUI/res/drawable-mdpi/ic_sysbar_brightness_low.png
deleted file mode 100644
index 91e1429..0000000
--- a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_brightness_low.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_gps_on.png b/packages/SystemUI/res/drawable-mdpi/ic_sysbar_gps_on.png
deleted file mode 100644
index 08d60d1..0000000
--- a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_gps_on.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_ime_default_land.png b/packages/SystemUI/res/drawable-mdpi/ic_sysbar_ime_default_land.png
deleted file mode 100644
index e5711eb..0000000
--- a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_ime_default_land.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_notification_dnd.png b/packages/SystemUI/res/drawable-mdpi/ic_sysbar_notification_dnd.png
deleted file mode 100644
index c046f58..0000000
--- a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_notification_dnd.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_notification_dnd_off.png b/packages/SystemUI/res/drawable-mdpi/ic_sysbar_notification_dnd_off.png
deleted file mode 100644
index 6b72be2..0000000
--- a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_notification_dnd_off.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_rotate_off_lanscape.png b/packages/SystemUI/res/drawable-mdpi/ic_sysbar_rotate_off_lanscape.png
deleted file mode 100644
index 540af65..0000000
--- a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_rotate_off_lanscape.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_rotate_off_portrait.png b/packages/SystemUI/res/drawable-mdpi/ic_sysbar_rotate_off_portrait.png
deleted file mode 100644
index 268a9bf..0000000
--- a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_rotate_off_portrait.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_settings.png b/packages/SystemUI/res/drawable-mdpi/ic_sysbar_settings.png
deleted file mode 100644
index 06e09e7..0000000
--- a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_settings.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_shadow.9.png b/packages/SystemUI/res/drawable-mdpi/ic_sysbar_shadow.9.png
deleted file mode 100644
index 897d216..0000000
--- a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_shadow.9.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_sound_off.png b/packages/SystemUI/res/drawable-mdpi/ic_sysbar_sound_off.png
deleted file mode 100644
index a11dabb..0000000
--- a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_sound_off.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_sound_on.png b/packages/SystemUI/res/drawable-mdpi/ic_sysbar_sound_on.png
deleted file mode 100644
index 16215bd..0000000
--- a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_sound_on.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/recent_overlay.png b/packages/SystemUI/res/drawable-mdpi/recent_overlay.png
deleted file mode 100644
index 33eabb2..0000000
--- a/packages/SystemUI/res/drawable-mdpi/recent_overlay.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/recent_rez_border.png b/packages/SystemUI/res/drawable-mdpi/recent_rez_border.png
deleted file mode 100644
index 5da42a3..0000000
--- a/packages/SystemUI/res/drawable-mdpi/recent_rez_border.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/shade_handlebar.9.png b/packages/SystemUI/res/drawable-mdpi/shade_handlebar.9.png
deleted file mode 100644
index d9598ae..0000000
--- a/packages/SystemUI/res/drawable-mdpi/shade_handlebar.9.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/shade_header_background.9.png b/packages/SystemUI/res/drawable-mdpi/shade_header_background.9.png
deleted file mode 100644
index 761ef05..0000000
--- a/packages/SystemUI/res/drawable-mdpi/shade_header_background.9.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/stat_sys_battery_unknown.png b/packages/SystemUI/res/drawable-mdpi/stat_sys_battery_unknown.png
deleted file mode 100644
index d4dca3e..0000000
--- a/packages/SystemUI/res/drawable-mdpi/stat_sys_battery_unknown.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_in_1x.png b/packages/SystemUI/res/drawable-mdpi/stat_sys_data_in_1x.png
deleted file mode 100644
index eecdefb..0000000
--- a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_in_1x.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_in_3g.png b/packages/SystemUI/res/drawable-mdpi/stat_sys_data_in_3g.png
deleted file mode 100644
index eecdefb..0000000
--- a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_in_3g.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_in_4g.png b/packages/SystemUI/res/drawable-mdpi/stat_sys_data_in_4g.png
deleted file mode 100644
index eecdefb..0000000
--- a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_in_4g.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_in_e.png b/packages/SystemUI/res/drawable-mdpi/stat_sys_data_in_e.png
deleted file mode 100644
index eecdefb..0000000
--- a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_in_e.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_in_g.png b/packages/SystemUI/res/drawable-mdpi/stat_sys_data_in_g.png
deleted file mode 100644
index eecdefb..0000000
--- a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_in_g.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_in_h.png b/packages/SystemUI/res/drawable-mdpi/stat_sys_data_in_h.png
deleted file mode 100644
index eecdefb..0000000
--- a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_in_h.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_inandout_1x.png b/packages/SystemUI/res/drawable-mdpi/stat_sys_data_inandout_1x.png
deleted file mode 100644
index eecdefb..0000000
--- a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_inandout_1x.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_inandout_3g.png b/packages/SystemUI/res/drawable-mdpi/stat_sys_data_inandout_3g.png
deleted file mode 100644
index eecdefb..0000000
--- a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_inandout_3g.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_inandout_4g.png b/packages/SystemUI/res/drawable-mdpi/stat_sys_data_inandout_4g.png
deleted file mode 100644
index eecdefb..0000000
--- a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_inandout_4g.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_inandout_e.png b/packages/SystemUI/res/drawable-mdpi/stat_sys_data_inandout_e.png
deleted file mode 100644
index eecdefb..0000000
--- a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_inandout_e.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_inandout_g.png b/packages/SystemUI/res/drawable-mdpi/stat_sys_data_inandout_g.png
deleted file mode 100644
index eecdefb..0000000
--- a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_inandout_g.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_inandout_h.png b/packages/SystemUI/res/drawable-mdpi/stat_sys_data_inandout_h.png
deleted file mode 100644
index eecdefb..0000000
--- a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_inandout_h.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_out_1x.png b/packages/SystemUI/res/drawable-mdpi/stat_sys_data_out_1x.png
deleted file mode 100644
index eecdefb..0000000
--- a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_out_1x.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_out_3g.png b/packages/SystemUI/res/drawable-mdpi/stat_sys_data_out_3g.png
deleted file mode 100644
index eecdefb..0000000
--- a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_out_3g.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_out_4g.png b/packages/SystemUI/res/drawable-mdpi/stat_sys_data_out_4g.png
deleted file mode 100644
index eecdefb..0000000
--- a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_out_4g.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_out_e.png b/packages/SystemUI/res/drawable-mdpi/stat_sys_data_out_e.png
deleted file mode 100644
index eecdefb..0000000
--- a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_out_e.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_out_g.png b/packages/SystemUI/res/drawable-mdpi/stat_sys_data_out_g.png
deleted file mode 100644
index eecdefb..0000000
--- a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_out_g.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_out_h.png b/packages/SystemUI/res/drawable-mdpi/stat_sys_data_out_h.png
deleted file mode 100644
index eecdefb..0000000
--- a/packages/SystemUI/res/drawable-mdpi/stat_sys_data_out_h.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/statusbar_background.9.png b/packages/SystemUI/res/drawable-mdpi/statusbar_background.9.png
deleted file mode 100644
index 6f19bf4..0000000
--- a/packages/SystemUI/res/drawable-mdpi/statusbar_background.9.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/title_bar_portrait.9.png b/packages/SystemUI/res/drawable-mdpi/title_bar_portrait.9.png
deleted file mode 100644
index 9de8324..0000000
--- a/packages/SystemUI/res/drawable-mdpi/title_bar_portrait.9.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-nodpi/bg_scrim_notification.png b/packages/SystemUI/res/drawable-nodpi/bg_scrim_notification.png
deleted file mode 100644
index 4d5135e..0000000
--- a/packages/SystemUI/res/drawable-nodpi/bg_scrim_notification.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-nodpi/compat_mode_help_bg.png b/packages/SystemUI/res/drawable-nodpi/compat_mode_help_bg.png
index 59a70ff..87d8c41 100644
--- a/packages/SystemUI/res/drawable-nodpi/compat_mode_help_bg.png
+++ b/packages/SystemUI/res/drawable-nodpi/compat_mode_help_bg.png
Binary files differ
diff --git a/packages/SystemUI/res/drawable-nodpi/notify_glow_back.png b/packages/SystemUI/res/drawable-nodpi/notify_glow_back.png
deleted file mode 100644
index 269049e..0000000
--- a/packages/SystemUI/res/drawable-nodpi/notify_glow_back.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-nodpi/notify_item_glow_left.png b/packages/SystemUI/res/drawable-nodpi/notify_item_glow_left.png
deleted file mode 100644
index 0e8c25c..0000000
--- a/packages/SystemUI/res/drawable-nodpi/notify_item_glow_left.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-nodpi/notify_item_glow_top.png b/packages/SystemUI/res/drawable-nodpi/notify_item_glow_top.png
deleted file mode 100644
index 536357a..0000000
--- a/packages/SystemUI/res/drawable-nodpi/notify_item_glow_top.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-nodpi/panel_notification.png b/packages/SystemUI/res/drawable-nodpi/panel_notification.png
deleted file mode 100644
index 275e492..0000000
--- a/packages/SystemUI/res/drawable-nodpi/panel_notification.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-sw600dp-hdpi/stat_sys_battery_unknown.png b/packages/SystemUI/res/drawable-sw600dp-hdpi/stat_sys_battery_unknown.png
deleted file mode 100644
index 004aee3..0000000
--- a/packages/SystemUI/res/drawable-sw600dp-hdpi/stat_sys_battery_unknown.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-sw600dp-mdpi/stat_sys_battery_unknown.png b/packages/SystemUI/res/drawable-sw600dp-mdpi/stat_sys_battery_unknown.png
deleted file mode 100644
index d71738d..0000000
--- a/packages/SystemUI/res/drawable-sw600dp-mdpi/stat_sys_battery_unknown.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-sw600dp-xhdpi/stat_sys_battery_unknown.png b/packages/SystemUI/res/drawable-sw600dp-xhdpi/stat_sys_battery_unknown.png
deleted file mode 100644
index 4394f92..0000000
--- a/packages/SystemUI/res/drawable-sw600dp-xhdpi/stat_sys_battery_unknown.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/ic_notification_open.png b/packages/SystemUI/res/drawable-xhdpi/ic_notification_open.png
new file mode 100644
index 0000000..4f8c987
--- /dev/null
+++ b/packages/SystemUI/res/drawable-xhdpi/ic_notification_open.png
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_airplane_off.png b/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_airplane_off.png
deleted file mode 100644
index ed968c8..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_airplane_off.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_apps_default.png b/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_apps_default.png
deleted file mode 100644
index 5641b2b..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_apps_default.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_apps_default_land.png b/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_apps_default_land.png
deleted file mode 100644
index 1ea83fc..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_apps_default_land.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_back_ime_default_land.png b/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_back_ime_default_land.png
deleted file mode 100644
index dc08550..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_back_ime_default_land.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_brightness_low.png b/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_brightness_low.png
deleted file mode 100644
index 03885fa..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_brightness_low.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_gps_on.png b/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_gps_on.png
deleted file mode 100644
index 92e86c6..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_gps_on.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_ime_default_land.png b/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_ime_default_land.png
deleted file mode 100644
index 94acf9a..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_ime_default_land.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_notification_dnd.png b/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_notification_dnd.png
deleted file mode 100644
index 4fc936b..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_notification_dnd.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_notification_dnd_off.png b/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_notification_dnd_off.png
deleted file mode 100644
index 27357ea..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_notification_dnd_off.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_rotate_off_lanscape.png b/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_rotate_off_lanscape.png
deleted file mode 100644
index 53c7094..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_rotate_off_lanscape.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_rotate_off_portrait.png b/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_rotate_off_portrait.png
deleted file mode 100644
index a882b94..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_rotate_off_portrait.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_settings.png b/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_settings.png
deleted file mode 100644
index fdcf409..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_settings.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_shadow.9.png b/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_shadow.9.png
deleted file mode 100644
index 792bac4..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_shadow.9.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_sound_off.png b/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_sound_off.png
deleted file mode 100644
index 31b4663..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_sound_off.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_sound_on.png b/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_sound_on.png
deleted file mode 100644
index 45b5bf3..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_sound_on.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/stat_sys_battery_unknown.png b/packages/SystemUI/res/drawable-xhdpi/stat_sys_battery_unknown.png
deleted file mode 100644
index ffac512..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/stat_sys_battery_unknown.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_in_1x.png b/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_in_1x.png
deleted file mode 100644
index 48038f0..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_in_1x.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_in_3g.png b/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_in_3g.png
deleted file mode 100644
index 48038f0..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_in_3g.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_in_4g.png b/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_in_4g.png
deleted file mode 100644
index 48038f0..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_in_4g.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_in_e.png b/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_in_e.png
deleted file mode 100644
index 48038f0..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_in_e.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_in_g.png b/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_in_g.png
deleted file mode 100644
index 48038f0..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_in_g.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_in_h.png b/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_in_h.png
deleted file mode 100644
index 48038f0..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_in_h.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_inandout_1x.png b/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_inandout_1x.png
deleted file mode 100644
index 48038f0..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_inandout_1x.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_inandout_3g.png b/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_inandout_3g.png
deleted file mode 100644
index 48038f0..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_inandout_3g.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_inandout_4g.png b/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_inandout_4g.png
deleted file mode 100644
index 48038f0..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_inandout_4g.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_inandout_e.png b/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_inandout_e.png
deleted file mode 100644
index 48038f0..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_inandout_e.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_inandout_g.png b/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_inandout_g.png
deleted file mode 100644
index 48038f0..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_inandout_g.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_inandout_h.png b/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_inandout_h.png
deleted file mode 100644
index 48038f0..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_inandout_h.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_out_1x.png b/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_out_1x.png
deleted file mode 100644
index 48038f0..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_out_1x.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_out_3g.png b/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_out_3g.png
deleted file mode 100644
index 48038f0..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_out_3g.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_out_4g.png b/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_out_4g.png
deleted file mode 100644
index 48038f0..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_out_4g.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_out_e.png b/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_out_e.png
deleted file mode 100644
index 48038f0..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_out_e.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_out_g.png b/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_out_g.png
deleted file mode 100644
index 48038f0..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_out_g.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_out_h.png b/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_out_h.png
deleted file mode 100644
index 48038f0..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/stat_sys_data_out_h.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable/compat_mode_help_bg.png b/packages/SystemUI/res/drawable/compat_mode_help_bg.png
deleted file mode 100644
index 87d8c41..0000000
--- a/packages/SystemUI/res/drawable/compat_mode_help_bg.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/layout/navigation_bar.xml b/packages/SystemUI/res/layout/navigation_bar.xml
index 6c4c9c1..fbca299 100644
--- a/packages/SystemUI/res/layout/navigation_bar.xml
+++ b/packages/SystemUI/res/layout/navigation_bar.xml
@@ -23,12 +23,12 @@
     xmlns:systemui="http://schemas.android.com/apk/res/com.android.systemui"
     android:layout_height="match_parent"
     android:layout_width="match_parent"
+    android:background="#FF000000"
     >
 
     <FrameLayout android:id="@+id/rot0"
         android:layout_height="match_parent"
         android:layout_width="match_parent"
-        android:background="#FF000000"
         >
 
         <LinearLayout
@@ -98,6 +98,51 @@
                 />
         </LinearLayout>
 
+        <!-- lights out layout to match exactly -->
+        <LinearLayout
+            android:layout_height="match_parent"
+            android:layout_width="match_parent"
+            android:orientation="horizontal"
+            android:id="@+id/lights_out"
+            android:visibility="gone"
+            >
+            <ImageView
+                android:layout_width="80dp"
+                android:layout_height="match_parent"
+                android:layout_marginLeft="40dp"
+                android:src="@drawable/ic_sysbar_lights_out_dot_small"
+                android:scaleType="center"
+                android:layout_weight="0"
+                />
+            <View 
+                android:layout_width="match_parent"
+                android:layout_height="match_parent"
+                android:layout_weight="1"
+                android:visibility="invisible"
+                />
+            <ImageView
+                android:layout_width="80dp"
+                android:layout_height="match_parent"
+                android:src="@drawable/ic_sysbar_lights_out_dot_large"
+                android:scaleType="center"
+                android:layout_weight="0"
+                />
+            <View 
+                android:layout_width="match_parent"
+                android:layout_height="match_parent"
+                android:layout_weight="1"
+                android:visibility="invisible"
+                />
+            <ImageView
+                android:layout_width="80dp"
+                android:layout_marginRight="40dp"
+                android:layout_height="match_parent"
+                android:src="@drawable/ic_sysbar_lights_out_dot_small"
+                android:scaleType="center"
+                android:layout_weight="0"
+                />
+        </LinearLayout>
+
         <View android:id="@+id/deadzone"
             android:layout_height="@dimen/navigation_bar_deadzone_size"
             android:layout_width="match_parent"
@@ -109,7 +154,6 @@
     <FrameLayout android:id="@+id/rot90"
         android:layout_height="match_parent"
         android:layout_width="match_parent"
-        android:background="#FF000000"
         android:visibility="gone"
         android:paddingTop="24dp"
         >
@@ -131,6 +175,8 @@
                 systemui:keyCode="82"
                 android:layout_weight="0"
                 android:visibility="invisible"
+                android:contentDescription="@string/accessibility_menu"
+                systemui:glowBackground="@drawable/ic_sysbar_highlight_land"
                 />
             <com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/recent_apps"
                 android:layout_height="80dp"
@@ -171,15 +217,56 @@
                 android:contentDescription="@string/accessibility_back"
                 systemui:glowBackground="@drawable/ic_sysbar_highlight_land"
                 />
-            <com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/menu"
+            <View
                 android:layout_height="40dp"
                 android:layout_width="match_parent"
-                android:src="@drawable/ic_sysbar_menu_land"
-                systemui:keyCode="82"
                 android:layout_weight="0"
                 android:visibility="invisible"
-                android:contentDescription="@string/accessibility_menu"
-                systemui:glowBackground="@drawable/ic_sysbar_highlight_land"
+                />
+        </LinearLayout>
+
+        <!-- lights out layout to match exactly -->
+        <LinearLayout 
+            android:layout_height="match_parent"
+            android:layout_width="match_parent"
+            android:orientation="vertical"
+            android:id="@+id/lights_out"
+            android:visibility="gone"
+            >
+            <ImageView
+                android:layout_height="80dp"
+                android:layout_marginTop="40dp"
+                android:layout_width="match_parent"
+                android:src="@drawable/ic_sysbar_lights_out_dot_small"
+                android:scaleType="center"
+                android:layout_weight="0"
+                />
+            <View 
+                android:layout_height="match_parent"
+                android:layout_width="match_parent"
+                android:layout_weight="1"
+                android:visibility="invisible"
+                />
+            <ImageView
+                android:layout_height="80dp"
+                android:layout_width="match_parent"
+                android:src="@drawable/ic_sysbar_lights_out_dot_large"
+                android:scaleType="center"
+                android:layout_weight="0"
+                />
+            <View 
+                android:layout_height="match_parent"
+                android:layout_width="match_parent"
+                android:layout_weight="1"
+                android:visibility="invisible"
+                />
+            <ImageView
+                android:layout_height="80dp"
+                android:layout_marginBottom="40dp"
+                android:layout_width="match_parent"
+                android:src="@drawable/ic_sysbar_lights_out_dot_small"
+                android:scaleType="center"
+                android:layout_weight="0"
                 />
         </LinearLayout>
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java
index 5f1ae58..e6c0b96 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java
@@ -25,6 +25,7 @@
 import android.os.ServiceManager;
 import android.util.AttributeSet;
 import android.util.Slog;
+import android.view.animation.AccelerateInterpolator;
 import android.view.Display;
 import android.view.KeyEvent;
 import android.view.MotionEvent;
@@ -47,6 +48,8 @@
 
     final static boolean NAVBAR_ALWAYS_AT_RIGHT = true;
 
+    final static boolean ANIMATE_HIDE_TRANSITION = false; // turned off because it introduces unsightly delay when videos goes to full screen
+
     protected IStatusBarService mBarService;
     final Display mDisplay;
     View mCurrentView = null;
@@ -56,7 +59,7 @@
     int mBarSize;
     boolean mVertical;
 
-    boolean mHidden;
+    boolean mHidden, mLowProfile;
     boolean mEnabled = true;
 
     public View getRecentsButton() {
@@ -87,6 +90,65 @@
         mCurrentView.setVisibility(enable ? View.VISIBLE : View.INVISIBLE);
     }
 
+    View.OnTouchListener mLightsOutListener = new View.OnTouchListener() {
+        @Override
+        public boolean onTouch(View v, MotionEvent ev) {
+            if (ev.getAction() == MotionEvent.ACTION_DOWN) {
+                // even though setting the systemUI visibility below will turn these views
+                // on, we need them to come up faster so that they can catch this motion
+                // event
+                setLowProfile(false, false);
+
+                try {
+                    mBarService.setSystemUiVisibility(0);
+                } catch (android.os.RemoteException ex) {
+                }
+            }
+            return false;
+        }
+    };
+
+    public void setLowProfile(final boolean lightsOut) {
+        setLowProfile(lightsOut, true);
+    }
+
+    public void setLowProfile(final boolean lightsOut, final boolean animate) {
+        if (lightsOut == mLowProfile) return;
+
+        mLowProfile = lightsOut;
+
+        if (DEBUG) Slog.d(TAG, "setting lights " + (lightsOut?"out":"on"));
+
+        final View navButtons = mCurrentView.findViewById(R.id.nav_buttons);
+        final View lowLights = mCurrentView.findViewById(R.id.lights_out);
+
+        if (!animate) {
+            lowLights.setVisibility(View.GONE);
+            navButtons.setAlpha(1f);
+        } else {
+            navButtons.animate()
+                .alpha(lightsOut ? 0f : 1f)
+                .setDuration(lightsOut ? 600 : 200)
+                .start();
+
+            lowLights.setOnTouchListener(mLightsOutListener);
+            lowLights.setAlpha(0f);
+            lowLights.setVisibility(View.VISIBLE);
+            lowLights.animate()
+                .alpha(lightsOut ? 1f : 0f)
+                .setStartDelay(lightsOut ? 500 : 0)
+                .setDuration(lightsOut ? 1000 : 300)
+                .setInterpolator(new AccelerateInterpolator(2.0f))
+                .setListener(lightsOut ? null : new AnimatorListenerAdapter() {
+                    @Override
+                    public void onAnimationEnd(Animator _a) {
+                        lowLights.setVisibility(View.GONE);
+                    }
+                })
+                .start();
+        }
+    }
+
     public void setHidden(final boolean hide) {
         if (hide == mHidden) return;
 
@@ -94,6 +156,14 @@
         Slog.d(TAG,
             (hide ? "HIDING" : "SHOWING") + " navigation bar");
 
+        // bring up the lights no matter what
+        setLowProfile(false);
+
+        if (!ANIMATE_HIDE_TRANSITION) {
+            setVisibility(hide ? View.GONE : View.VISIBLE);
+            return;
+        }
+
         float oldAlpha = mCurrentView.getAlpha();
         if (DEBUG) {
             Slog.d(TAG, "animating alpha: " + oldAlpha + " -> "
@@ -147,8 +217,10 @@
 
     @Override
     public boolean onTouchEvent(MotionEvent ev) {
-        // immediately bring up the lights
-        setHidden(false);
+        try {
+            mBarService.setSystemUiVisibility(0);
+        } catch (android.os.RemoteException ex) {
+        }
         return false; // pass it on
     }
 
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 68f6dcf..7a563c7 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
@@ -284,6 +284,8 @@
                 mNavigationBarView = 
                     (NavigationBarView) View.inflate(context, R.layout.navigation_bar, null);
 
+                mNavigationBarView.setEnabled((mDisabled & StatusBarManager.DISABLE_NAVIGATION) == 0);
+
                 sb.setOnSystemUiVisibilityChangeListener(
                     new View.OnSystemUiVisibilityChangeListener() {
                         @Override
@@ -1072,7 +1074,9 @@
                 mHandler.sendEmptyMessage(MSG_CLOSE_RECENTS_PANEL);
             }
 
-            mNavigationBarView.setEnabled((state & StatusBarManager.DISABLE_NAVIGATION) == 0);
+            if (mNavigationBarView != null) {
+                mNavigationBarView.setEnabled((state & StatusBarManager.DISABLE_NAVIGATION) == 0);
+            }
         }
 
         if ((diff & StatusBarManager.DISABLE_NOTIFICATION_ICONS) != 0) {
@@ -1490,11 +1494,18 @@
 
     @Override // CommandQueue
     public void setSystemUiVisibility(int vis) {
-        if (vis != mSystemUiVisibility) {
+        final int old = mSystemUiVisibility;
+        final int diff = vis ^ old;
+
+        if (diff != 0) {
             mSystemUiVisibility = vis;
 
-            if (0 != (vis & View.SYSTEM_UI_FLAG_LOW_PROFILE)) {
-                animateCollapse();
+            if (0 != (diff & View.SYSTEM_UI_FLAG_LOW_PROFILE)) {
+                final boolean lightsOut = (0 != (vis & View.SYSTEM_UI_FLAG_LOW_PROFILE));
+                if (lightsOut) {
+                    animateCollapse();
+                }
+                mNavigationBarView.setLowProfile(lightsOut);
             }
 
             notifyUiVisibilityChanged();
diff --git a/policy/src/com/android/internal/policy/impl/PhoneWindow.java b/policy/src/com/android/internal/policy/impl/PhoneWindow.java
index c580f08..c929bbc 100644
--- a/policy/src/com/android/internal/policy/impl/PhoneWindow.java
+++ b/policy/src/com/android/internal/policy/impl/PhoneWindow.java
@@ -587,6 +587,13 @@
             }
         } else if (!st.isInListMode()) {
             width = MATCH_PARENT;
+        } else if (st.createdPanelView != null) {
+            // If we already had a panel view, carry width=MATCH_PARENT through
+            // as we did above when it was created.
+            ViewGroup.LayoutParams lp = st.createdPanelView.getLayoutParams();
+            if (lp != null && lp.width == ViewGroup.LayoutParams.MATCH_PARENT) {
+                width = MATCH_PARENT;
+            }
         }
 
         st.isOpen = true;
diff --git a/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java b/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
index 7764e35..ebadb5e 100755
--- a/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
+++ b/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
@@ -2398,10 +2398,11 @@
         }
     }
 
+    final Object mScreenshotLock = new Object();
     ServiceConnection mScreenshotConnection = null;
     Runnable mScreenshotTimeout = null;
 
-    void finishScreenshot(ServiceConnection conn) {
+    void finishScreenshotLSS(ServiceConnection conn) {
         if (mScreenshotConnection == conn) {
             mContext.unbindService(conn);
             mScreenshotConnection = null;
@@ -2416,48 +2417,56 @@
         mHandler.post(new Runnable() {
             @Override
             public void run() {
-                if (mScreenshotConnection != null) {
-                    return;
-                }
-                ComponentName cn = new ComponentName("com.android.systemui",
-                        "com.android.systemui.screenshot.TakeScreenshotService");
-                Intent intent = new Intent();
-                intent.setComponent(cn);
-                ServiceConnection conn = new ServiceConnection() {
-                    @Override
-                    public void onServiceConnected(ComponentName name, IBinder service) {
-                        if (mScreenshotConnection != this) {
-                            return;
-                        }
-                        Messenger messenger = new Messenger(service);
-                        Message msg = Message.obtain(null, 1);
-                        final ServiceConnection myConn = this;
-                        Handler h = new Handler(mHandler.getLooper()) {
-                            @Override
-                            public void handleMessage(Message msg) {
-                                finishScreenshot(myConn);
-                            }
-                        };
-                        msg.replyTo = new Messenger(h);
-                        try {
-                            messenger.send(msg);
-                        } catch (RemoteException e) {
-                        }
+                synchronized (mScreenshotLock) {
+                    if (mScreenshotConnection != null) {
+                        return;
                     }
-                    @Override
-                    public void onServiceDisconnected(ComponentName name) {}
-                };
-                if (mContext.bindService(intent, conn, Context.BIND_AUTO_CREATE)) {
-                    mScreenshotConnection = conn;
-                    mScreenshotTimeout = new Runnable() {
-                        @Override public void run() {
-                            if (mScreenshotConnection != null) {
-                                finishScreenshot(mScreenshotConnection);
+                    ComponentName cn = new ComponentName("com.android.systemui",
+                            "com.android.systemui.screenshot.TakeScreenshotService");
+                    Intent intent = new Intent();
+                    intent.setComponent(cn);
+                    ServiceConnection conn = new ServiceConnection() {
+                        @Override
+                        public void onServiceConnected(ComponentName name, IBinder service) {
+                            synchronized (mScreenshotLock) {
+                                if (mScreenshotConnection != this) {
+                                    return;
+                                }
+                                Messenger messenger = new Messenger(service);
+                                Message msg = Message.obtain(null, 1);
+                                final ServiceConnection myConn = this;
+                                Handler h = new Handler(mHandler.getLooper()) {
+                                    @Override
+                                    public void handleMessage(Message msg) {
+                                        synchronized (mScreenshotLock) {
+                                            finishScreenshotLSS(myConn);
+                                        }
+                                    }
+                                };
+                                msg.replyTo = new Messenger(h);
+                                try {
+                                    messenger.send(msg);
+                                } catch (RemoteException e) {
+                                }
                             }
                         }
-
+                        @Override
+                        public void onServiceDisconnected(ComponentName name) {}
                     };
-                    mHandler.postDelayed(mScreenshotTimeout, 10000);
+                    if (mContext.bindService(intent, conn, Context.BIND_AUTO_CREATE)) {
+                        mScreenshotConnection = conn;
+                        mScreenshotTimeout = new Runnable() {
+                            @Override public void run() {
+                                synchronized (mScreenshotLock) {
+                                    if (mScreenshotConnection != null) {
+                                        finishScreenshotLSS(mScreenshotConnection);
+                                    }
+                                }
+                            }
+    
+                        };
+                        mHandler.postDelayed(mScreenshotTimeout, 10000);
+                    }
                 }
             }
         });
diff --git a/services/input/InputDispatcher.cpp b/services/input/InputDispatcher.cpp
index 3cd3ac3..f6ce44c 100644
--- a/services/input/InputDispatcher.cpp
+++ b/services/input/InputDispatcher.cpp
@@ -401,6 +401,14 @@
         break;
     }
 
+    case EventEntry::TYPE_DEVICE_RESET: {
+        DeviceResetEntry* typedEntry =
+                static_cast<DeviceResetEntry*>(mPendingEvent);
+        done = dispatchDeviceResetLocked(currentTime, typedEntry);
+        dropReason = DROP_REASON_NOT_DROPPED; // device resets are never dropped
+        break;
+    }
+
     case EventEntry::TYPE_KEY: {
         KeyEntry* typedEntry = static_cast<KeyEntry*>(mPendingEvent);
         if (isAppSwitchDue) {
@@ -727,6 +735,19 @@
     return true;
 }
 
+bool InputDispatcher::dispatchDeviceResetLocked(
+        nsecs_t currentTime, DeviceResetEntry* entry) {
+#if DEBUG_OUTBOUND_EVENT_DETAILS
+    LOGD("dispatchDeviceReset - eventTime=%lld, deviceId=%d", entry->eventTime, entry->deviceId);
+#endif
+
+    CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS,
+            "device was reset");
+    options.deviceId = entry->deviceId;
+    synthesizeCancelationEventsForAllConnectionsLocked(options);
+    return true;
+}
+
 bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, KeyEntry* entry,
         DropReason* dropReason, nsecs_t* nextWakeupTime) {
     // Preprocessing.
@@ -2921,6 +2942,25 @@
             args->switchCode, args->switchValue, policyFlags);
 }
 
+void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) {
+#if DEBUG_INBOUND_EVENT_DETAILS
+    LOGD("notifyDeviceReset - eventTime=%lld, deviceId=%d",
+            args->eventTime, args->deviceId);
+#endif
+
+    bool needWake;
+    { // acquire lock
+        AutoMutex _l(mLock);
+
+        DeviceResetEntry* newEntry = new DeviceResetEntry(args->eventTime, args->deviceId);
+        needWake = enqueueInboundEventLocked(newEntry);
+    } // release lock
+
+    if (needWake) {
+        mLooper->wake();
+    }
+}
+
 int32_t InputDispatcher::injectInputEvent(const InputEvent* event,
         int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
         uint32_t policyFlags) {
@@ -4016,6 +4056,17 @@
 }
 
 
+// --- InputDispatcher::DeviceResetEntry ---
+
+InputDispatcher::DeviceResetEntry::DeviceResetEntry(nsecs_t eventTime, int32_t deviceId) :
+        EventEntry(TYPE_DEVICE_RESET, eventTime, 0),
+        deviceId(deviceId) {
+}
+
+InputDispatcher::DeviceResetEntry::~DeviceResetEntry() {
+}
+
+
 // --- InputDispatcher::KeyEntry ---
 
 InputDispatcher::KeyEntry::KeyEntry(nsecs_t eventTime,
@@ -4407,6 +4458,10 @@
         return false;
     }
 
+    if (options.deviceId != -1 && memento.deviceId != options.deviceId) {
+        return false;
+    }
+
     switch (options.mode) {
     case CancelationOptions::CANCEL_ALL_EVENTS:
     case CancelationOptions::CANCEL_NON_POINTER_EVENTS:
@@ -4420,6 +4475,10 @@
 
 bool InputDispatcher::InputState::shouldCancelMotion(const MotionMemento& memento,
         const CancelationOptions& options) {
+    if (options.deviceId != -1 && memento.deviceId != options.deviceId) {
+        return false;
+    }
+
     switch (options.mode) {
     case CancelationOptions::CANCEL_ALL_EVENTS:
         return true;
diff --git a/services/input/InputDispatcher.h b/services/input/InputDispatcher.h
index cae1610..3c83691 100644
--- a/services/input/InputDispatcher.h
+++ b/services/input/InputDispatcher.h
@@ -381,6 +381,7 @@
     virtual void notifyKey(const NotifyKeyArgs* args);
     virtual void notifyMotion(const NotifyMotionArgs* args);
     virtual void notifySwitch(const NotifySwitchArgs* args);
+    virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args);
 
     virtual int32_t injectInputEvent(const InputEvent* event,
             int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
@@ -424,6 +425,7 @@
     struct EventEntry : Link<EventEntry> {
         enum {
             TYPE_CONFIGURATION_CHANGED,
+            TYPE_DEVICE_RESET,
             TYPE_KEY,
             TYPE_MOTION
         };
@@ -453,6 +455,15 @@
         virtual ~ConfigurationChangedEntry();
     };
 
+    struct DeviceResetEntry : EventEntry {
+        int32_t deviceId;
+
+        DeviceResetEntry(nsecs_t eventTime, int32_t deviceId);
+
+    protected:
+        virtual ~DeviceResetEntry();
+    };
+
     struct KeyEntry : EventEntry {
         int32_t deviceId;
         uint32_t source;
@@ -688,8 +699,11 @@
         // The specific keycode of the key event to cancel, or -1 to cancel any key event.
         int32_t keyCode;
 
+        // The specific device id of events to cancel, or -1 to cancel events from any device.
+        int32_t deviceId;
+
         CancelationOptions(Mode mode, const char* reason) :
-                mode(mode), reason(reason), keyCode(-1) { }
+                mode(mode), reason(reason), keyCode(-1), deviceId(-1) { }
     };
 
     /* Tracks dispatched key and motion event state so that cancelation events can be
@@ -982,6 +996,8 @@
     // Dispatch inbound events.
     bool dispatchConfigurationChangedLocked(
             nsecs_t currentTime, ConfigurationChangedEntry* entry);
+    bool dispatchDeviceResetLocked(
+            nsecs_t currentTime, DeviceResetEntry* entry);
     bool dispatchKeyLocked(
             nsecs_t currentTime, KeyEntry* entry,
             DropReason* dropReason, nsecs_t* nextWakeupTime);
diff --git a/services/input/InputListener.cpp b/services/input/InputListener.cpp
index 4f9fe90..657a6b9 100644
--- a/services/input/InputListener.cpp
+++ b/services/input/InputListener.cpp
@@ -118,6 +118,21 @@
 }
 
 
+// --- NotifyDeviceResetArgs ---
+
+NotifyDeviceResetArgs::NotifyDeviceResetArgs(nsecs_t eventTime, int32_t deviceId) :
+        eventTime(eventTime), deviceId(deviceId) {
+}
+
+NotifyDeviceResetArgs::NotifyDeviceResetArgs(const NotifyDeviceResetArgs& other) :
+        eventTime(other.eventTime), deviceId(other.deviceId) {
+}
+
+void NotifyDeviceResetArgs::notify(const sp<InputListenerInterface>& listener) const {
+    listener->notifyDeviceReset(this);
+}
+
+
 // --- QueuedInputListener ---
 
 QueuedInputListener::QueuedInputListener(const sp<InputListenerInterface>& innerListener) :
@@ -148,6 +163,10 @@
     mArgsQueue.push(new NotifySwitchArgs(*args));
 }
 
+void QueuedInputListener::notifyDeviceReset(const NotifyDeviceResetArgs* args) {
+    mArgsQueue.push(new NotifyDeviceResetArgs(*args));
+}
+
 void QueuedInputListener::flush() {
     size_t count = mArgsQueue.size();
     for (size_t i = 0; i < count; i++) {
diff --git a/services/input/InputListener.h b/services/input/InputListener.h
index 3fef132..f920cd1 100644
--- a/services/input/InputListener.h
+++ b/services/input/InputListener.h
@@ -131,6 +131,24 @@
 };
 
 
+/* Describes a device reset event, such as when a device is added,
+ * reconfigured, or removed. */
+struct NotifyDeviceResetArgs : public NotifyArgs {
+    nsecs_t eventTime;
+    int32_t deviceId;
+
+    inline NotifyDeviceResetArgs() { }
+
+    NotifyDeviceResetArgs(nsecs_t eventTime, int32_t deviceId);
+
+    NotifyDeviceResetArgs(const NotifyDeviceResetArgs& other);
+
+    virtual ~NotifyDeviceResetArgs() { }
+
+    virtual void notify(const sp<InputListenerInterface>& listener) const;
+};
+
+
 /*
  * The interface used by the InputReader to notify the InputListener about input events.
  */
@@ -144,6 +162,7 @@
     virtual void notifyKey(const NotifyKeyArgs* args) = 0;
     virtual void notifyMotion(const NotifyMotionArgs* args) = 0;
     virtual void notifySwitch(const NotifySwitchArgs* args) = 0;
+    virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args) = 0;
 };
 
 
@@ -162,6 +181,7 @@
     virtual void notifyKey(const NotifyKeyArgs* args);
     virtual void notifyMotion(const NotifyMotionArgs* args);
     virtual void notifySwitch(const NotifySwitchArgs* args);
+    virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args);
 
     void flush();
 
diff --git a/services/input/InputReader.cpp b/services/input/InputReader.cpp
index 2035a4b..88378ef 100644
--- a/services/input/InputReader.cpp
+++ b/services/input/InputReader.cpp
@@ -198,6 +198,39 @@
 }
 
 
+// --- InputReaderConfiguration ---
+
+bool InputReaderConfiguration::getDisplayInfo(int32_t displayId, bool external,
+        int32_t* width, int32_t* height, int32_t* orientation) const {
+    if (displayId == 0) {
+        const DisplayInfo& info = external ? mExternalDisplay : mInternalDisplay;
+        if (info.width > 0 && info.height > 0) {
+            if (width) {
+                *width = info.width;
+            }
+            if (height) {
+                *height = info.height;
+            }
+            if (orientation) {
+                *orientation = info.orientation;
+            }
+            return true;
+        }
+    }
+    return false;
+}
+
+void InputReaderConfiguration::setDisplayInfo(int32_t displayId, bool external,
+        int32_t width, int32_t height, int32_t orientation) {
+    if (displayId == 0) {
+        DisplayInfo& info = external ? mExternalDisplay : mInternalDisplay;
+        info.width = width;
+        info.height = height;
+        info.orientation = orientation;
+    }
+}
+
+
 // --- InputReader ---
 
 InputReader::InputReader(const sp<EventHubInterface>& eventHub,
@@ -289,10 +322,10 @@
         } else {
             switch (rawEvent->type) {
             case EventHubInterface::DEVICE_ADDED:
-                addDeviceLocked(rawEvent->deviceId);
+                addDeviceLocked(rawEvent->when, rawEvent->deviceId);
                 break;
             case EventHubInterface::DEVICE_REMOVED:
-                removeDeviceLocked(rawEvent->deviceId);
+                removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
                 break;
             case EventHubInterface::FINISHED_DEVICE_SCAN:
                 handleConfigurationChangedLocked(rawEvent->when);
@@ -307,12 +340,13 @@
     }
 }
 
-void InputReader::addDeviceLocked(int32_t deviceId) {
+void InputReader::addDeviceLocked(nsecs_t when, int32_t deviceId) {
     String8 name = mEventHub->getDeviceName(deviceId);
     uint32_t classes = mEventHub->getDeviceClasses(deviceId);
 
     InputDevice* device = createDeviceLocked(deviceId, name, classes);
-    device->configure(&mConfig, 0);
+    device->configure(when, &mConfig, 0);
+    device->reset(when);
 
     if (device->isIgnored()) {
         LOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId, name.string());
@@ -331,7 +365,7 @@
     }
 }
 
-void InputReader::removeDeviceLocked(int32_t deviceId) {
+void InputReader::removeDeviceLocked(nsecs_t when, int32_t deviceId) {
     InputDevice* device = NULL;
     ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
     if (deviceIndex >= 0) {
@@ -350,8 +384,7 @@
                 device->getId(), device->getName().string(), device->getSources());
     }
 
-    device->reset();
-
+    device->reset(when);
     delete device;
 }
 
@@ -453,13 +486,14 @@
 
     if (changes) {
         LOGI("Reconfiguring input devices.  changes=0x%08x", changes);
+        nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
 
         if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
             mEventHub->requestReopenDevices();
         } else {
             for (size_t i = 0; i < mDevices.size(); i++) {
                 InputDevice* device = mDevices.valueAt(i);
-                device->configure(&mConfig, changes);
+                device->configure(now, &mConfig, changes);
             }
         }
     }
@@ -861,7 +895,7 @@
     mMappers.add(mapper);
 }
 
-void InputDevice::configure(const InputReaderConfiguration* config, uint32_t changes) {
+void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes) {
     mSources = 0;
 
     if (!isIgnored()) {
@@ -872,18 +906,22 @@
         size_t numMappers = mMappers.size();
         for (size_t i = 0; i < numMappers; i++) {
             InputMapper* mapper = mMappers[i];
-            mapper->configure(config, changes);
+            mapper->configure(when, config, changes);
             mSources |= mapper->getSources();
         }
     }
 }
 
-void InputDevice::reset() {
+void InputDevice::reset(nsecs_t when) {
     size_t numMappers = mMappers.size();
     for (size_t i = 0; i < numMappers; i++) {
         InputMapper* mapper = mMappers[i];
-        mapper->reset();
+        mapper->reset(when);
     }
+
+    mContext->updateGlobalMetaState();
+
+    notifyReset(when);
 }
 
 void InputDevice::process(const RawEvent* rawEvents, size_t count) {
@@ -915,7 +953,7 @@
         } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_DROPPED) {
             LOGI("Detected input event buffer overrun for device %s.", mName.string());
             mDropUntilNextSync = true;
-            reset();
+            reset(rawEvent->when);
         } else {
             for (size_t i = 0; i < numMappers; i++) {
                 InputMapper* mapper = mMappers[i];
@@ -1001,6 +1039,11 @@
     }
 }
 
+void InputDevice::notifyReset(nsecs_t when) {
+    NotifyDeviceResetArgs args(when, mId);
+    mContext->getListener()->notifyDeviceReset(&args);
+}
+
 
 // --- CursorButtonAccumulator ---
 
@@ -1008,6 +1051,17 @@
     clearButtons();
 }
 
+void CursorButtonAccumulator::reset(InputDevice* device) {
+    mBtnLeft = device->isKeyPressed(BTN_LEFT);
+    mBtnRight = device->isKeyPressed(BTN_RIGHT);
+    mBtnMiddle = device->isKeyPressed(BTN_MIDDLE);
+    mBtnBack = device->isKeyPressed(BTN_BACK);
+    mBtnSide = device->isKeyPressed(BTN_SIDE);
+    mBtnForward = device->isKeyPressed(BTN_FORWARD);
+    mBtnExtra = device->isKeyPressed(BTN_EXTRA);
+    mBtnTask = device->isKeyPressed(BTN_TASK);
+}
+
 void CursorButtonAccumulator::clearButtons() {
     mBtnLeft = 0;
     mBtnRight = 0;
@@ -1073,21 +1127,17 @@
 
 // --- CursorMotionAccumulator ---
 
-CursorMotionAccumulator::CursorMotionAccumulator() :
-        mHaveRelWheel(false), mHaveRelHWheel(false) {
+CursorMotionAccumulator::CursorMotionAccumulator() {
     clearRelativeAxes();
 }
 
-void CursorMotionAccumulator::configure(InputDevice* device) {
-    mHaveRelWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_WHEEL);
-    mHaveRelHWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_HWHEEL);
+void CursorMotionAccumulator::reset(InputDevice* device) {
+    clearRelativeAxes();
 }
 
 void CursorMotionAccumulator::clearRelativeAxes() {
     mRelX = 0;
     mRelY = 0;
-    mRelWheel = 0;
-    mRelHWheel = 0;
 }
 
 void CursorMotionAccumulator::process(const RawEvent* rawEvent) {
@@ -1099,6 +1149,39 @@
         case REL_Y:
             mRelY = rawEvent->value;
             break;
+        }
+    }
+}
+
+void CursorMotionAccumulator::finishSync() {
+    clearRelativeAxes();
+}
+
+
+// --- CursorScrollAccumulator ---
+
+CursorScrollAccumulator::CursorScrollAccumulator() :
+        mHaveRelWheel(false), mHaveRelHWheel(false) {
+    clearRelativeAxes();
+}
+
+void CursorScrollAccumulator::configure(InputDevice* device) {
+    mHaveRelWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_WHEEL);
+    mHaveRelHWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_HWHEEL);
+}
+
+void CursorScrollAccumulator::reset(InputDevice* device) {
+    clearRelativeAxes();
+}
+
+void CursorScrollAccumulator::clearRelativeAxes() {
+    mRelWheel = 0;
+    mRelHWheel = 0;
+}
+
+void CursorScrollAccumulator::process(const RawEvent* rawEvent) {
+    if (rawEvent->type == EV_REL) {
+        switch (rawEvent->scanCode) {
         case REL_WHEEL:
             mRelWheel = rawEvent->value;
             break;
@@ -1109,6 +1192,10 @@
     }
 }
 
+void CursorScrollAccumulator::finishSync() {
+    clearRelativeAxes();
+}
+
 
 // --- TouchButtonAccumulator ---
 
@@ -1118,7 +1205,21 @@
 }
 
 void TouchButtonAccumulator::configure(InputDevice* device) {
-    mHaveBtnTouch = device->getEventHub()->hasScanCode(device->getId(), BTN_TOUCH);
+    mHaveBtnTouch = device->hasKey(BTN_TOUCH);
+}
+
+void TouchButtonAccumulator::reset(InputDevice* device) {
+    mBtnTouch = device->isKeyPressed(BTN_TOUCH);
+    mBtnStylus = device->isKeyPressed(BTN_STYLUS);
+    mBtnStylus2 = device->isKeyPressed(BTN_STYLUS);
+    mBtnToolFinger = device->isKeyPressed(BTN_TOOL_FINGER);
+    mBtnToolPen = device->isKeyPressed(BTN_TOOL_PEN);
+    mBtnToolRubber = device->isKeyPressed(BTN_TOOL_RUBBER);
+    mBtnToolBrush = device->isKeyPressed(BTN_TOOL_BRUSH);
+    mBtnToolPencil = device->isKeyPressed(BTN_TOOL_PENCIL);
+    mBtnToolAirbrush = device->isKeyPressed(BTN_TOOL_AIRBRUSH);
+    mBtnToolMouse = device->isKeyPressed(BTN_TOOL_MOUSE);
+    mBtnToolLens = device->isKeyPressed(BTN_TOOL_LENS);
 }
 
 void TouchButtonAccumulator::clearButtons() {
@@ -1128,6 +1229,11 @@
     mBtnToolFinger = 0;
     mBtnToolPen = 0;
     mBtnToolRubber = 0;
+    mBtnToolBrush = 0;
+    mBtnToolPencil = 0;
+    mBtnToolAirbrush = 0;
+    mBtnToolMouse = 0;
+    mBtnToolLens = 0;
 }
 
 void TouchButtonAccumulator::process(const RawEvent* rawEvent) {
@@ -1151,6 +1257,21 @@
         case BTN_TOOL_RUBBER:
             mBtnToolRubber = rawEvent->value;
             break;
+        case BTN_TOOL_BRUSH:
+            mBtnToolBrush = rawEvent->value;
+            break;
+        case BTN_TOOL_PENCIL:
+            mBtnToolPencil = rawEvent->value;
+            break;
+        case BTN_TOOL_AIRBRUSH:
+            mBtnToolAirbrush = rawEvent->value;
+            break;
+        case BTN_TOOL_MOUSE:
+            mBtnToolMouse = rawEvent->value;
+            break;
+        case BTN_TOOL_LENS:
+            mBtnToolLens = rawEvent->value;
+            break;
         }
     }
 }
@@ -1167,10 +1288,13 @@
 }
 
 int32_t TouchButtonAccumulator::getToolType() const {
+    if (mBtnToolMouse || mBtnToolLens) {
+        return AMOTION_EVENT_TOOL_TYPE_MOUSE;
+    }
     if (mBtnToolRubber) {
         return AMOTION_EVENT_TOOL_TYPE_ERASER;
     }
-    if (mBtnToolPen) {
+    if (mBtnToolPen || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush) {
         return AMOTION_EVENT_TOOL_TYPE_STYLUS;
     }
     if (mBtnToolFinger) {
@@ -1180,7 +1304,9 @@
 }
 
 bool TouchButtonAccumulator::isToolActive() const {
-    return mBtnTouch || mBtnToolFinger || mBtnToolPen || mBtnToolRubber;
+    return mBtnTouch || mBtnToolFinger || mBtnToolPen || mBtnToolRubber
+            || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush
+            || mBtnToolMouse || mBtnToolLens;
 }
 
 bool TouchButtonAccumulator::isHovering() const {
@@ -1204,6 +1330,8 @@
     toolMinor.clear();
     orientation.clear();
     distance.clear();
+    tiltX.clear();
+    tiltY.clear();
     trackingId.clear();
     slot.clear();
 }
@@ -1284,12 +1412,24 @@
     clearAbsoluteAxes();
 }
 
+void SingleTouchMotionAccumulator::reset(InputDevice* device) {
+    mAbsX = device->getAbsoluteAxisValue(ABS_X);
+    mAbsY = device->getAbsoluteAxisValue(ABS_Y);
+    mAbsPressure = device->getAbsoluteAxisValue(ABS_PRESSURE);
+    mAbsToolWidth = device->getAbsoluteAxisValue(ABS_TOOL_WIDTH);
+    mAbsDistance = device->getAbsoluteAxisValue(ABS_DISTANCE);
+    mAbsTiltX = device->getAbsoluteAxisValue(ABS_TILT_X);
+    mAbsTiltY = device->getAbsoluteAxisValue(ABS_TILT_Y);
+}
+
 void SingleTouchMotionAccumulator::clearAbsoluteAxes() {
     mAbsX = 0;
     mAbsY = 0;
     mAbsPressure = 0;
     mAbsToolWidth = 0;
     mAbsDistance = 0;
+    mAbsTiltX = 0;
+    mAbsTiltY = 0;
 }
 
 void SingleTouchMotionAccumulator::process(const RawEvent* rawEvent) {
@@ -1310,6 +1450,12 @@
         case ABS_DISTANCE:
             mAbsDistance = rawEvent->value;
             break;
+        case ABS_TILT_X:
+            mAbsTiltX = rawEvent->value;
+            break;
+        case ABS_TILT_Y:
+            mAbsTiltY = rawEvent->value;
+            break;
         }
     }
 }
@@ -1333,9 +1479,37 @@
     mSlots = new Slot[slotCount];
 }
 
+void MultiTouchMotionAccumulator::reset(InputDevice* device) {
+    // Unfortunately there is no way to read the initial contents of the slots.
+    // So when we reset the accumulator, we must assume they are all zeroes.
+    if (mUsingSlotsProtocol) {
+        // Query the driver for the current slot index and use it as the initial slot
+        // before we start reading events from the device.  It is possible that the
+        // current slot index will not be the same as it was when the first event was
+        // written into the evdev buffer, which means the input mapper could start
+        // out of sync with the initial state of the events in the evdev buffer.
+        // In the extremely unlikely case that this happens, the data from
+        // two slots will be confused until the next ABS_MT_SLOT event is received.
+        // This can cause the touch point to "jump", but at least there will be
+        // no stuck touches.
+        int32_t initialSlot;
+        status_t status = device->getEventHub()->getAbsoluteAxisValue(device->getId(),
+                ABS_MT_SLOT, &initialSlot);
+        if (status) {
+            LOGD("Could not retrieve current multitouch slot index.  status=%d", status);
+            initialSlot = -1;
+        }
+        clearSlots(initialSlot);
+    } else {
+        clearSlots(-1);
+    }
+}
+
 void MultiTouchMotionAccumulator::clearSlots(int32_t initialSlot) {
-    for (size_t i = 0; i < mSlotCount; i++) {
-        mSlots[i].clear();
+    if (mSlots) {
+        for (size_t i = 0; i < mSlotCount; i++) {
+            mSlots[i].clear();
+        }
     }
     mCurrentSlot = initialSlot;
 }
@@ -1425,6 +1599,12 @@
     }
 }
 
+void MultiTouchMotionAccumulator::finishSync() {
+    if (!mUsingSlotsProtocol) {
+        clearSlots(-1);
+    }
+}
+
 
 // --- MultiTouchMotionAccumulator::Slot ---
 
@@ -1479,10 +1659,11 @@
 void InputMapper::dump(String8& dump) {
 }
 
-void InputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
+void InputMapper::configure(nsecs_t when,
+        const InputReaderConfiguration* config, uint32_t changes) {
 }
 
-void InputMapper::reset() {
+void InputMapper::reset(nsecs_t when) {
 }
 
 void InputMapper::timeoutExpired(nsecs_t when) {
@@ -1564,17 +1745,11 @@
         uint32_t source, int32_t keyboardType) :
         InputMapper(device), mSource(source),
         mKeyboardType(keyboardType) {
-    initialize();
 }
 
 KeyboardInputMapper::~KeyboardInputMapper() {
 }
 
-void KeyboardInputMapper::initialize() {
-    mMetaState = AMETA_NONE;
-    mDownTime = 0;
-}
-
 uint32_t KeyboardInputMapper::getSources() {
     return mSource;
 }
@@ -1589,21 +1764,31 @@
     dump.append(INDENT2 "Keyboard Input Mapper:\n");
     dumpParameters(dump);
     dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
+    dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
     dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mKeyDowns.size());
     dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mMetaState);
     dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
 }
 
 
-void KeyboardInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
-    InputMapper::configure(config, changes);
+void KeyboardInputMapper::configure(nsecs_t when,
+        const InputReaderConfiguration* config, uint32_t changes) {
+    InputMapper::configure(when, config, changes);
 
     if (!changes) { // first time only
         // Configure basic parameters.
         configureParameters();
+    }
 
-        // Reset LEDs.
-        resetLedState();
+    if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
+        if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
+            if (!config->getDisplayInfo(mParameters.associatedDisplayId,
+                        false /*external*/, NULL, NULL, &mOrientation)) {
+                mOrientation = DISPLAY_ORIENTATION_0;
+            }
+        } else {
+            mOrientation = DISPLAY_ORIENTATION_0;
+        }
     }
 }
 
@@ -1626,19 +1811,14 @@
             toString(mParameters.orientationAware));
 }
 
-void KeyboardInputMapper::reset() {
-    // Synthesize key up event on reset if keys are currently down.
-    while (!mKeyDowns.isEmpty()) {
-        const KeyDown& keyDown = mKeyDowns.top();
-        nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
-        processKey(when, false, keyDown.keyCode, keyDown.scanCode, 0);
-    }
+void KeyboardInputMapper::reset(nsecs_t when) {
+    mMetaState = AMETA_NONE;
+    mDownTime = 0;
+    mKeyDowns.clear();
 
-    initialize();
     resetLedState();
 
-    InputMapper::reset();
-    getContext()->updateGlobalMetaState();
+    InputMapper::reset(when);
 }
 
 void KeyboardInputMapper::process(const RawEvent* rawEvent) {
@@ -1666,15 +1846,8 @@
 
     if (down) {
         // Rotate key codes according to orientation if needed.
-        // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
         if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
-            int32_t orientation;
-            if (!getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
-                    false /*external*/, NULL, NULL, & orientation)) {
-                orientation = DISPLAY_ORIENTATION_0;
-            }
-
-            keyCode = rotateKeyCode(keyCode, orientation);
+            keyCode = rotateKeyCode(keyCode, mOrientation);
         }
 
         // Add key down.
@@ -1813,7 +1986,6 @@
 
 CursorInputMapper::CursorInputMapper(InputDevice* device) :
         InputMapper(device) {
-    initialize();
 }
 
 CursorInputMapper::~CursorInputMapper() {
@@ -1838,10 +2010,10 @@
     }
     info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f);
 
-    if (mCursorMotionAccumulator.haveRelativeVWheel()) {
+    if (mCursorScrollAccumulator.haveRelativeVWheel()) {
         info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
     }
-    if (mCursorMotionAccumulator.haveRelativeHWheel()) {
+    if (mCursorScrollAccumulator.haveRelativeHWheel()) {
         info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
     }
 }
@@ -1854,21 +2026,23 @@
     dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
     dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
     dump.appendFormat(INDENT3 "HaveVWheel: %s\n",
-            toString(mCursorMotionAccumulator.haveRelativeVWheel()));
+            toString(mCursorScrollAccumulator.haveRelativeVWheel()));
     dump.appendFormat(INDENT3 "HaveHWheel: %s\n",
-            toString(mCursorMotionAccumulator.haveRelativeHWheel()));
+            toString(mCursorScrollAccumulator.haveRelativeHWheel()));
     dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
     dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
+    dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
     dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mButtonState);
     dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState)));
     dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
 }
 
-void CursorInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
-    InputMapper::configure(config, changes);
+void CursorInputMapper::configure(nsecs_t when,
+        const InputReaderConfiguration* config, uint32_t changes) {
+    InputMapper::configure(when, config, changes);
 
     if (!changes) { // first time only
-        mCursorMotionAccumulator.configure(getDevice());
+        mCursorScrollAccumulator.configure(getDevice());
 
         // Configure basic parameters.
         configureParameters();
@@ -1901,6 +2075,17 @@
         mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters);
         mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters);
     }
+
+    if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
+        if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
+            if (!config->getDisplayInfo(mParameters.associatedDisplayId,
+                        false /*external*/, NULL, NULL, &mOrientation)) {
+                mOrientation = DISPLAY_ORIENTATION_0;
+            }
+        } else {
+            mOrientation = DISPLAY_ORIENTATION_0;
+        }
+    }
 }
 
 void CursorInputMapper::configureParameters() {
@@ -1944,34 +2129,25 @@
             toString(mParameters.orientationAware));
 }
 
-void CursorInputMapper::initialize() {
-    mCursorButtonAccumulator.clearButtons();
-    mCursorMotionAccumulator.clearRelativeAxes();
-
+void CursorInputMapper::reset(nsecs_t when) {
     mButtonState = 0;
     mDownTime = 0;
-}
 
-void CursorInputMapper::reset() {
-    // Reset velocity.
     mPointerVelocityControl.reset();
     mWheelXVelocityControl.reset();
     mWheelYVelocityControl.reset();
 
-    // Synthesize button up event on reset.
-    nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
-    mCursorButtonAccumulator.clearButtons();
-    mCursorMotionAccumulator.clearRelativeAxes();
-    sync(when);
+    mCursorButtonAccumulator.reset(getDevice());
+    mCursorMotionAccumulator.reset(getDevice());
+    mCursorScrollAccumulator.reset(getDevice());
 
-    initialize();
-
-    InputMapper::reset();
+    InputMapper::reset(when);
 }
 
 void CursorInputMapper::process(const RawEvent* rawEvent) {
     mCursorButtonAccumulator.process(rawEvent);
     mCursorMotionAccumulator.process(rawEvent);
+    mCursorScrollAccumulator.process(rawEvent);
 
     if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
         sync(rawEvent->when);
@@ -2001,19 +2177,13 @@
     float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale;
     bool moved = deltaX != 0 || deltaY != 0;
 
+    // Rotate delta according to orientation if needed.
     if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
             && (deltaX != 0.0f || deltaY != 0.0f)) {
-        // Rotate motion based on display orientation if needed.
-        // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
-        int32_t orientation;
-        if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
-                false /*external*/, NULL, NULL, & orientation)) {
-            orientation = DISPLAY_ORIENTATION_0;
-        }
-
-        rotateDelta(orientation, &deltaX, &deltaY);
+        rotateDelta(mOrientation, &deltaX, &deltaY);
     }
 
+    // Move the pointer.
     PointerProperties pointerProperties;
     pointerProperties.clear();
     pointerProperties.id = 0;
@@ -2022,8 +2192,8 @@
     PointerCoords pointerCoords;
     pointerCoords.clear();
 
-    float vscroll = mCursorMotionAccumulator.getRelativeVWheel();
-    float hscroll = mCursorMotionAccumulator.getRelativeHWheel();
+    float vscroll = mCursorScrollAccumulator.getRelativeVWheel();
+    float hscroll = mCursorScrollAccumulator.getRelativeHWheel();
     bool scrolled = vscroll != 0 || hscroll != 0;
 
     mWheelYVelocityControl.move(when, NULL, &vscroll);
@@ -2115,7 +2285,8 @@
     synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
             policyFlags, lastButtonState, currentButtonState);
 
-    mCursorMotionAccumulator.clearRelativeAxes();
+    mCursorMotionAccumulator.finishSync();
+    mCursorScrollAccumulator.finishSync();
 }
 
 int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
@@ -2137,65 +2308,57 @@
 
 TouchInputMapper::TouchInputMapper(InputDevice* device) :
         InputMapper(device),
+        mSource(0), mDeviceMode(DEVICE_MODE_DISABLED),
         mSurfaceOrientation(-1), mSurfaceWidth(-1), mSurfaceHeight(-1) {
-    initialize();
 }
 
 TouchInputMapper::~TouchInputMapper() {
 }
 
 uint32_t TouchInputMapper::getSources() {
-    return mTouchSource | mPointerSource;
+    return mSource;
 }
 
 void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
     InputMapper::populateDeviceInfo(info);
 
-    // Ensure surface information is up to date so that orientation changes are
-    // noticed immediately.
-    if (!configureSurface()) {
-        return;
-    }
-
-    info->addMotionRange(mOrientedRanges.x);
-    info->addMotionRange(mOrientedRanges.y);
-
-    if (mOrientedRanges.havePressure) {
+    if (mDeviceMode != DEVICE_MODE_DISABLED) {
+        info->addMotionRange(mOrientedRanges.x);
+        info->addMotionRange(mOrientedRanges.y);
         info->addMotionRange(mOrientedRanges.pressure);
-    }
 
-    if (mOrientedRanges.haveSize) {
-        info->addMotionRange(mOrientedRanges.size);
-    }
-
-    if (mOrientedRanges.haveTouchSize) {
-        info->addMotionRange(mOrientedRanges.touchMajor);
-        info->addMotionRange(mOrientedRanges.touchMinor);
-    }
-
-    if (mOrientedRanges.haveToolSize) {
-        info->addMotionRange(mOrientedRanges.toolMajor);
-        info->addMotionRange(mOrientedRanges.toolMinor);
-    }
-
-    if (mOrientedRanges.haveOrientation) {
-        info->addMotionRange(mOrientedRanges.orientation);
-    }
-
-    if (mOrientedRanges.haveDistance) {
-        info->addMotionRange(mOrientedRanges.distance);
-    }
-
-    if (mPointerController != NULL) {
-        float minX, minY, maxX, maxY;
-        if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
-            info->addMotionRange(AMOTION_EVENT_AXIS_X, mPointerSource,
-                    minX, maxX, 0.0f, 0.0f);
-            info->addMotionRange(AMOTION_EVENT_AXIS_Y, mPointerSource,
-                    minY, maxY, 0.0f, 0.0f);
+        if (mOrientedRanges.haveSize) {
+            info->addMotionRange(mOrientedRanges.size);
         }
-        info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mPointerSource,
-                0.0f, 1.0f, 0.0f, 0.0f);
+
+        if (mOrientedRanges.haveTouchSize) {
+            info->addMotionRange(mOrientedRanges.touchMajor);
+            info->addMotionRange(mOrientedRanges.touchMinor);
+        }
+
+        if (mOrientedRanges.haveToolSize) {
+            info->addMotionRange(mOrientedRanges.toolMajor);
+            info->addMotionRange(mOrientedRanges.toolMinor);
+        }
+
+        if (mOrientedRanges.haveOrientation) {
+            info->addMotionRange(mOrientedRanges.orientation);
+        }
+
+        if (mOrientedRanges.haveDistance) {
+            info->addMotionRange(mOrientedRanges.distance);
+        }
+
+        if (mOrientedRanges.haveTilt) {
+            info->addMotionRange(mOrientedRanges.tilt);
+        }
+
+        if (mCursorScrollAccumulator.haveRelativeVWheel()) {
+            info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
+        }
+        if (mCursorScrollAccumulator.haveRelativeHWheel()) {
+            info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
+        }
     }
 }
 
@@ -2215,8 +2378,14 @@
     dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
     dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
     dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
+    dump.appendFormat(INDENT4 "OrientationCenter: %0.3f\n", mOrientationCenter);
     dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
     dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
+    dump.appendFormat(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt));
+    dump.appendFormat(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter);
+    dump.appendFormat(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale);
+    dump.appendFormat(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter);
+    dump.appendFormat(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale);
 
     dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState);
 
@@ -2226,11 +2395,12 @@
         const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i];
         dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
                 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
-                "orientation=%d, distance=%d, toolType=%d, isHovering=%s\n", i,
+                "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, "
+                "toolType=%d, isHovering=%s\n", i,
                 pointer.id, pointer.x, pointer.y, pointer.pressure,
                 pointer.touchMajor, pointer.touchMinor,
                 pointer.toolMajor, pointer.toolMinor,
-                pointer.orientation, pointer.distance,
+                pointer.orientation, pointer.tiltX, pointer.tiltY, pointer.distance,
                 pointer.toolType, toString(pointer.isHovering));
     }
 
@@ -2241,7 +2411,8 @@
         const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i];
         dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, "
                 "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, "
-                "orientation=%0.3f, distance=%0.3f, toolType=%d, isHovering=%s\n", i,
+                "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, "
+                "toolType=%d, isHovering=%s\n", i,
                 pointerProperties.id,
                 pointerCoords.getX(),
                 pointerCoords.getY(),
@@ -2251,50 +2422,30 @@
                 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
                 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
                 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
+                pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT),
                 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
                 pointerProperties.toolType,
                 toString(mLastCookedPointerData.isHovering(i)));
     }
 
-    if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
+    if (mDeviceMode == DEVICE_MODE_POINTER) {
         dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
         dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
-                mPointerGestureXMovementScale);
+                mPointerXMovementScale);
         dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
-                mPointerGestureYMovementScale);
+                mPointerYMovementScale);
         dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
-                mPointerGestureXZoomScale);
+                mPointerXZoomScale);
         dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
-                mPointerGestureYZoomScale);
+                mPointerYZoomScale);
         dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
                 mPointerGestureMaxSwipeWidth);
     }
 }
 
-void TouchInputMapper::initialize() {
-    mCurrentRawPointerData.clear();
-    mLastRawPointerData.clear();
-    mCurrentCookedPointerData.clear();
-    mLastCookedPointerData.clear();
-    mCurrentButtonState = 0;
-    mLastButtonState = 0;
-    mSentHoverEnter = false;
-    mDownTime = 0;
-
-    mCurrentVirtualKey.down = false;
-
-    mOrientedRanges.havePressure = false;
-    mOrientedRanges.haveSize = false;
-    mOrientedRanges.haveTouchSize = false;
-    mOrientedRanges.haveToolSize = false;
-    mOrientedRanges.haveOrientation = false;
-    mOrientedRanges.haveDistance = false;
-
-    mPointerGesture.reset();
-}
-
-void TouchInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
-    InputMapper::configure(config, changes);
+void TouchInputMapper::configure(nsecs_t when,
+        const InputReaderConfiguration* config, uint32_t changes) {
+    InputMapper::configure(when, config, changes);
 
     mConfig = *config;
 
@@ -2302,23 +2453,9 @@
         // Configure basic parameters.
         configureParameters();
 
-        // Configure sources.
-        switch (mParameters.deviceType) {
-        case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
-            mTouchSource = AINPUT_SOURCE_TOUCHSCREEN;
-            mPointerSource = 0;
-            break;
-        case Parameters::DEVICE_TYPE_TOUCH_PAD:
-            mTouchSource = AINPUT_SOURCE_TOUCHPAD;
-            mPointerSource = 0;
-            break;
-        case Parameters::DEVICE_TYPE_POINTER:
-            mTouchSource = AINPUT_SOURCE_TOUCHPAD;
-            mPointerSource = AINPUT_SOURCE_MOUSE;
-            break;
-        default:
-            LOG_ASSERT(false);
-        }
+        // Configure common accumulators.
+        mCursorScrollAccumulator.configure(getDevice());
+        mTouchButtonAccumulator.configure(getDevice());
 
         // Configure absolute axis information.
         configureRawPointerAxes();
@@ -2326,19 +2463,27 @@
         // Prepare input device calibration.
         parseCalibration();
         resolveCalibration();
-
-         // Configure surface dimensions and orientation.
-        configureSurface();
     }
 
     if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
-        mPointerGesture.pointerVelocityControl.setParameters(
-                mConfig.pointerVelocityControlParameters);
+        // Update pointer speed.
+        mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters);
+        mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
+        mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
     }
 
-    if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT)) {
-        // Reset the touch screen when pointer gesture enablement changes.
-        reset();
+    bool resetNeeded = false;
+    if (!changes || (changes & (InputReaderConfiguration::CHANGE_DISPLAY_INFO
+            | InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT))) {
+        // Configure device sources, surface dimensions, orientation and
+        // scaling factors.
+        configureSurface(when, &resetNeeded);
+    }
+
+    if (changes && resetNeeded) {
+        // Send reset, unless this is the first time the device has been configured,
+        // in which case the reader will call reset itself after all mappers are ready.
+        getDevice()->notifyReset(when);
     }
 }
 
@@ -2435,8 +2580,8 @@
         LOG_ASSERT(false);
     }
 
-    dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
-            mParameters.associatedDisplayId);
+    dump.appendFormat(INDENT4 "AssociatedDisplay: id=%d, isExternal=%s\n",
+            mParameters.associatedDisplayId, toString(mParameters.associatedDisplayIsExternal));
     dump.appendFormat(INDENT4 "OrientationAware: %s\n",
             toString(mParameters.orientationAware));
 }
@@ -2456,47 +2601,79 @@
     dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
     dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
     dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
+    dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX");
+    dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY");
     dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
     dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
 }
 
-bool TouchInputMapper::configureSurface() {
+void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) {
+    int32_t oldDeviceMode = mDeviceMode;
+
+    // Determine device mode.
+    if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
+            && mConfig.pointerGesturesEnabled) {
+        mSource = AINPUT_SOURCE_MOUSE;
+        mDeviceMode = DEVICE_MODE_POINTER;
+    } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
+            && mParameters.associatedDisplayId >= 0) {
+        mSource = AINPUT_SOURCE_TOUCHSCREEN;
+        mDeviceMode = DEVICE_MODE_DIRECT;
+    } else {
+        mSource = AINPUT_SOURCE_TOUCHPAD;
+        mDeviceMode = DEVICE_MODE_UNSCALED;
+    }
+
     // Ensure we have valid X and Y axes.
     if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
         LOGW(INDENT "Touch device '%s' did not report support for X or Y axis!  "
                 "The device will be inoperable.", getDeviceName().string());
-        return false;
+        mDeviceMode = DEVICE_MODE_DISABLED;
+        return;
     }
 
-    // Update orientation and dimensions if needed.
-    int32_t orientation = DISPLAY_ORIENTATION_0;
-    int32_t width = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
-    int32_t height = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
-
+    // Get associated display dimensions.
     if (mParameters.associatedDisplayId >= 0) {
-        // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
-        if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
+        if (!mConfig.getDisplayInfo(mParameters.associatedDisplayId,
                 mParameters.associatedDisplayIsExternal,
                 &mAssociatedDisplayWidth, &mAssociatedDisplayHeight,
                 &mAssociatedDisplayOrientation)) {
-            return false;
-        }
-
-        // A touch screen inherits the dimensions of the display.
-        if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
-            width = mAssociatedDisplayWidth;
-            height = mAssociatedDisplayHeight;
-        }
-
-        // The device inherits the orientation of the display if it is orientation aware.
-        if (mParameters.orientationAware) {
-            orientation = mAssociatedDisplayOrientation;
+            LOGI(INDENT "Touch device '%s' could not query the properties of its associated "
+                    "display %d.  The device will be inoperable until the display size "
+                    "becomes available.",
+                    getDeviceName().string(), mParameters.associatedDisplayId);
+            mDeviceMode = DEVICE_MODE_DISABLED;
+            return;
         }
     }
 
-    if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
-            && mPointerController == NULL) {
-        mPointerController = getPolicy()->obtainPointerController(getDeviceId());
+    // Configure dimensions.
+    int32_t width, height, orientation;
+    if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) {
+        width = mAssociatedDisplayWidth;
+        height = mAssociatedDisplayHeight;
+        orientation = mParameters.orientationAware ?
+                mAssociatedDisplayOrientation : DISPLAY_ORIENTATION_0;
+    } else {
+        width = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
+        height = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
+        orientation = DISPLAY_ORIENTATION_0;
+    }
+
+    // If moving between pointer modes, need to reset some state.
+    bool deviceModeChanged;
+    if (mDeviceMode != oldDeviceMode) {
+        deviceModeChanged = true;
+
+        if (mDeviceMode == DEVICE_MODE_POINTER) {
+            if (mPointerController == NULL) {
+                mPointerController = getPolicy()->obtainPointerController(getDeviceId());
+            }
+        } else {
+            mPointerController.clear();
+        }
+
+        mOrientedRanges.clear();
     }
 
     bool orientationChanged = mSurfaceOrientation != orientation;
@@ -2505,9 +2682,9 @@
     }
 
     bool sizeChanged = mSurfaceWidth != width || mSurfaceHeight != height;
-    if (sizeChanged) {
-        LOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d",
-                getDeviceId(), getDeviceName().string(), width, height);
+    if (sizeChanged || deviceModeChanged) {
+        LOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d, mode is %d",
+                getDeviceId(), getDeviceName().string(), width, height, mDeviceMode);
 
         mSurfaceWidth = width;
         mSurfaceHeight = height;
@@ -2519,9 +2696,9 @@
         mYPrecision = 1.0f / mYScale;
 
         mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
-        mOrientedRanges.x.source = mTouchSource;
+        mOrientedRanges.x.source = mSource;
         mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
-        mOrientedRanges.y.source = mTouchSource;
+        mOrientedRanges.y.source = mSource;
 
         configureVirtualKeys();
 
@@ -2550,7 +2727,7 @@
             mOrientedRanges.haveSize = true;
 
             mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
-            mOrientedRanges.touchMajor.source = mTouchSource;
+            mOrientedRanges.touchMajor.source = mSource;
             mOrientedRanges.touchMajor.min = 0;
             mOrientedRanges.touchMajor.max = diagonalSize;
             mOrientedRanges.touchMajor.flat = 0;
@@ -2560,7 +2737,7 @@
             mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
 
             mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
-            mOrientedRanges.toolMajor.source = mTouchSource;
+            mOrientedRanges.toolMajor.source = mSource;
             mOrientedRanges.toolMajor.min = 0;
             mOrientedRanges.toolMajor.max = diagonalSize;
             mOrientedRanges.toolMajor.flat = 0;
@@ -2570,7 +2747,7 @@
             mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
 
             mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
-            mOrientedRanges.size.source = mTouchSource;
+            mOrientedRanges.size.source = mSource;
             mOrientedRanges.size.min = 0;
             mOrientedRanges.size.max = 1.0;
             mOrientedRanges.size.flat = 0;
@@ -2581,44 +2758,77 @@
 
         // Pressure factors.
         mPressureScale = 0;
-        if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE) {
-            if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
-                    || mCalibration.pressureCalibration
-                            == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
-                if (mCalibration.havePressureScale) {
-                    mPressureScale = mCalibration.pressureScale;
-                } else if (mRawPointerAxes.pressure.valid
-                        && mRawPointerAxes.pressure.maxValue != 0) {
-                    mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
-                }
+        if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
+                || mCalibration.pressureCalibration
+                        == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
+            if (mCalibration.havePressureScale) {
+                mPressureScale = mCalibration.pressureScale;
+            } else if (mRawPointerAxes.pressure.valid
+                    && mRawPointerAxes.pressure.maxValue != 0) {
+                mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
             }
+        }
 
-            mOrientedRanges.havePressure = true;
+        mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
+        mOrientedRanges.pressure.source = mSource;
+        mOrientedRanges.pressure.min = 0;
+        mOrientedRanges.pressure.max = 1.0;
+        mOrientedRanges.pressure.flat = 0;
+        mOrientedRanges.pressure.fuzz = 0;
 
-            mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
-            mOrientedRanges.pressure.source = mTouchSource;
-            mOrientedRanges.pressure.min = 0;
-            mOrientedRanges.pressure.max = 1.0;
-            mOrientedRanges.pressure.flat = 0;
-            mOrientedRanges.pressure.fuzz = 0;
+        // Tilt
+        mTiltXCenter = 0;
+        mTiltXScale = 0;
+        mTiltYCenter = 0;
+        mTiltYScale = 0;
+        mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
+        if (mHaveTilt) {
+            mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue,
+                    mRawPointerAxes.tiltX.maxValue);
+            mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue,
+                    mRawPointerAxes.tiltY.maxValue);
+            mTiltXScale = M_PI / 180;
+            mTiltYScale = M_PI / 180;
+
+            mOrientedRanges.haveTilt = true;
+
+            mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT;
+            mOrientedRanges.tilt.source = mSource;
+            mOrientedRanges.tilt.min = 0;
+            mOrientedRanges.tilt.max = M_PI_2;
+            mOrientedRanges.tilt.flat = 0;
+            mOrientedRanges.tilt.fuzz = 0;
         }
 
         // Orientation
+        mOrientationCenter = 0;
         mOrientationScale = 0;
-        if (mCalibration.orientationCalibration != Calibration::ORIENTATION_CALIBRATION_NONE) {
+        if (mHaveTilt) {
+            mOrientedRanges.haveOrientation = true;
+
+            mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
+            mOrientedRanges.orientation.source = mSource;
+            mOrientedRanges.orientation.min = -M_PI;
+            mOrientedRanges.orientation.max = M_PI;
+            mOrientedRanges.orientation.flat = 0;
+            mOrientedRanges.orientation.fuzz = 0;
+        } else if (mCalibration.orientationCalibration !=
+                Calibration::ORIENTATION_CALIBRATION_NONE) {
             if (mCalibration.orientationCalibration
                     == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
-                if (mRawPointerAxes.orientation.valid
-                        && mRawPointerAxes.orientation.maxValue != 0) {
-                    mOrientationScale = float(M_PI_2) / mRawPointerAxes.orientation.maxValue;
+                if (mRawPointerAxes.orientation.valid) {
+                    mOrientationCenter = avg(mRawPointerAxes.orientation.minValue,
+                            mRawPointerAxes.orientation.maxValue);
+                    mOrientationScale = M_PI / (mRawPointerAxes.orientation.maxValue -
+                            mRawPointerAxes.orientation.minValue);
                 }
             }
 
             mOrientedRanges.haveOrientation = true;
 
             mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
-            mOrientedRanges.orientation.source = mTouchSource;
-            mOrientedRanges.orientation.min = - M_PI_2;
+            mOrientedRanges.orientation.source = mSource;
+            mOrientedRanges.orientation.min = -M_PI_2;
             mOrientedRanges.orientation.max = M_PI_2;
             mOrientedRanges.orientation.flat = 0;
             mOrientedRanges.orientation.fuzz = 0;
@@ -2639,7 +2849,7 @@
             mOrientedRanges.haveDistance = true;
 
             mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
-            mOrientedRanges.distance.source = mTouchSource;
+            mOrientedRanges.distance.source = mSource;
             mOrientedRanges.distance.min =
                     mRawPointerAxes.distance.minValue * mDistanceScale;
             mOrientedRanges.distance.max =
@@ -2650,7 +2860,7 @@
         }
     }
 
-    if (orientationChanged || sizeChanged) {
+    if (orientationChanged || sizeChanged || deviceModeChanged) {
         // Compute oriented surface dimensions, precision, scales and ranges.
         // Note that the maximum value reported is an inclusive maximum value so it is one
         // unit less than the total width or height of surface.
@@ -2698,7 +2908,7 @@
         }
 
         // Compute pointer gesture detection parameters.
-        if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
+        if (mDeviceMode == DEVICE_MODE_POINTER) {
             int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
             int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
             float rawDiagonal = hypotf(rawWidth, rawHeight);
@@ -2710,34 +2920,30 @@
             // is applied.
             // Assume that the touch pad has a square aspect ratio such that movements in
             // X and Y of the same number of raw units cover the same physical distance.
-            mPointerGestureXMovementScale = mConfig.pointerGestureMovementSpeedRatio
+            mPointerXMovementScale = mConfig.pointerGestureMovementSpeedRatio
                     * displayDiagonal / rawDiagonal;
-            mPointerGestureYMovementScale = mPointerGestureXMovementScale;
+            mPointerYMovementScale = mPointerXMovementScale;
 
             // Scale zooms to cover a smaller range of the display than movements do.
             // This value determines the area around the pointer that is affected by freeform
             // pointer gestures.
-            mPointerGestureXZoomScale = mConfig.pointerGestureZoomSpeedRatio
+            mPointerXZoomScale = mConfig.pointerGestureZoomSpeedRatio
                     * displayDiagonal / rawDiagonal;
-            mPointerGestureYZoomScale = mPointerGestureXZoomScale;
+            mPointerYZoomScale = mPointerXZoomScale;
 
             // Max width between pointers to detect a swipe gesture is more than some fraction
             // of the diagonal axis of the touch pad.  Touches that are wider than this are
             // translated into freeform gestures.
             mPointerGestureMaxSwipeWidth =
                     mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
-
-            // Reset the current pointer gesture.
-            mPointerGesture.reset();
-
-            // Remove any current spots.
-            if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
-                mPointerController->clearSpots();
-            }
         }
-    }
 
-    return true;
+        // Abort current pointer usages because the state has changed.
+        abortPointerUsage(when, 0 /*policyFlags*/);
+
+        // Inform the dispatcher about the changes.
+        *outResetNeeded = true;
+    }
 }
 
 void TouchInputMapper::dumpSurface(String8& dump) {
@@ -3023,26 +3229,71 @@
     }
 }
 
-void TouchInputMapper::reset() {
-    // Synthesize touch up event.
-    // This will also take care of finishing virtual key processing if needed.
-    nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
+void TouchInputMapper::reset(nsecs_t when) {
+    mCursorButtonAccumulator.reset(getDevice());
+    mCursorScrollAccumulator.reset(getDevice());
+    mTouchButtonAccumulator.reset(getDevice());
+
+    mPointerVelocityControl.reset();
+    mWheelXVelocityControl.reset();
+    mWheelYVelocityControl.reset();
+
     mCurrentRawPointerData.clear();
+    mLastRawPointerData.clear();
+    mCurrentCookedPointerData.clear();
+    mLastCookedPointerData.clear();
     mCurrentButtonState = 0;
-    syncTouch(when, true);
+    mLastButtonState = 0;
+    mCurrentRawVScroll = 0;
+    mCurrentRawHScroll = 0;
+    mCurrentFingerIdBits.clear();
+    mLastFingerIdBits.clear();
+    mCurrentStylusIdBits.clear();
+    mLastStylusIdBits.clear();
+    mCurrentMouseIdBits.clear();
+    mLastMouseIdBits.clear();
+    mPointerUsage = POINTER_USAGE_NONE;
+    mSentHoverEnter = false;
+    mDownTime = 0;
 
-    initialize();
+    mCurrentVirtualKey.down = false;
 
-    if (mPointerController != NULL
-            && mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
+    mPointerGesture.reset();
+    mPointerSimple.reset();
+
+    if (mPointerController != NULL) {
         mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
         mPointerController->clearSpots();
     }
 
-    InputMapper::reset();
+    InputMapper::reset(when);
 }
 
-void TouchInputMapper::syncTouch(nsecs_t when, bool havePointerIds) {
+void TouchInputMapper::process(const RawEvent* rawEvent) {
+    mCursorButtonAccumulator.process(rawEvent);
+    mCursorScrollAccumulator.process(rawEvent);
+    mTouchButtonAccumulator.process(rawEvent);
+
+    if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
+        sync(rawEvent->when);
+    }
+}
+
+void TouchInputMapper::sync(nsecs_t when) {
+    // Sync button state.
+    mCurrentButtonState = mTouchButtonAccumulator.getButtonState()
+            | mCursorButtonAccumulator.getButtonState();
+
+    // Sync scroll state.
+    mCurrentRawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
+    mCurrentRawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
+    mCursorScrollAccumulator.finishSync();
+
+    // Sync touch state.
+    bool havePointerIds = true;
+    mCurrentRawPointerData.clear();
+    syncTouch(when, &havePointerIds);
+
 #if DEBUG_RAW_EVENTS
     if (!havePointerIds) {
         LOGD("syncTouch: pointerCount %d -> %d, no pointer ids",
@@ -3060,66 +3311,123 @@
     }
 #endif
 
-    // Configure the surface now, if possible.
-    if (!configureSurface()) {
-        mLastRawPointerData.clear();
-        mLastCookedPointerData.clear();
-        mLastButtonState = 0;
-        return;
-    }
+    // Reset state that we will compute below.
+    mCurrentFingerIdBits.clear();
+    mCurrentStylusIdBits.clear();
+    mCurrentMouseIdBits.clear();
+    mCurrentCookedPointerData.clear();
 
-    // Preprocess pointer data.
-    if (!havePointerIds) {
-        assignPointerIds();
-    }
-
-    // Handle policy on initial down or hover events.
-    uint32_t policyFlags = 0;
-    if (mLastRawPointerData.pointerCount == 0 && mCurrentRawPointerData.pointerCount != 0) {
-        if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
-            // If this is a touch screen, hide the pointer on an initial down.
-            getContext()->fadePointer();
-        }
-
-        // Initial downs on external touch devices should wake the device.
-        // We don't do this for internal touch screens to prevent them from waking
-        // up in your pocket.
-        // TODO: Use the input device configuration to control this behavior more finely.
-        if (getDevice()->isExternal()) {
-            policyFlags |= POLICY_FLAG_WAKE_DROPPED;
-        }
-    }
-
-    // Synthesize key down from raw buttons if needed.
-    synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mTouchSource,
-            policyFlags, mLastButtonState, mCurrentButtonState);
-
-    if (consumeRawTouches(when, policyFlags)) {
+    if (mDeviceMode == DEVICE_MODE_DISABLED) {
+        // Drop all input if the device is disabled.
         mCurrentRawPointerData.clear();
+        mCurrentButtonState = 0;
+    } else {
+        // Preprocess pointer data.
+        if (!havePointerIds) {
+            assignPointerIds();
+        }
+
+        // Handle policy on initial down or hover events.
+        uint32_t policyFlags = 0;
+        if (mLastRawPointerData.pointerCount == 0 && mCurrentRawPointerData.pointerCount != 0) {
+            if (mDeviceMode == DEVICE_MODE_DIRECT) {
+                // If this is a touch screen, hide the pointer on an initial down.
+                getContext()->fadePointer();
+            }
+
+            // Initial downs on external touch devices should wake the device.
+            // We don't do this for internal touch screens to prevent them from waking
+            // up in your pocket.
+            // TODO: Use the input device configuration to control this behavior more finely.
+            if (getDevice()->isExternal()) {
+                policyFlags |= POLICY_FLAG_WAKE_DROPPED;
+            }
+        }
+
+        // Synthesize key down from raw buttons if needed.
+        synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
+                policyFlags, mLastButtonState, mCurrentButtonState);
+
+        // Consume raw off-screen touches before cooking pointer data.
+        // If touches are consumed, subsequent code will not receive any pointer data.
+        if (consumeRawTouches(when, policyFlags)) {
+            mCurrentRawPointerData.clear();
+        }
+
+        // Cook pointer data.  This call populates the mCurrentCookedPointerData structure
+        // with cooked pointer data that has the same ids and indices as the raw data.
+        // The following code can use either the raw or cooked data, as needed.
+        cookPointerData();
+
+        // Dispatch the touches either directly or by translation through a pointer on screen.
+        if (mPointerController != NULL) {
+            for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) {
+                uint32_t id = idBits.clearFirstMarkedBit();
+                const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
+                if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
+                        || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
+                    mCurrentStylusIdBits.markBit(id);
+                } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
+                        || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
+                    mCurrentFingerIdBits.markBit(id);
+                } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
+                    mCurrentMouseIdBits.markBit(id);
+                }
+            }
+            for (BitSet32 idBits(mCurrentRawPointerData.hoveringIdBits); !idBits.isEmpty(); ) {
+                uint32_t id = idBits.clearFirstMarkedBit();
+                const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
+                if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
+                        || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
+                    mCurrentStylusIdBits.markBit(id);
+                }
+            }
+
+            // Stylus takes precedence over all tools, then mouse, then finger.
+            PointerUsage pointerUsage = mPointerUsage;
+            if (!mCurrentStylusIdBits.isEmpty()) {
+                mCurrentMouseIdBits.clear();
+                mCurrentFingerIdBits.clear();
+                pointerUsage = POINTER_USAGE_STYLUS;
+            } else if (!mCurrentMouseIdBits.isEmpty()) {
+                mCurrentFingerIdBits.clear();
+                pointerUsage = POINTER_USAGE_MOUSE;
+            } else if (!mCurrentFingerIdBits.isEmpty() || isPointerDown(mCurrentButtonState)) {
+                pointerUsage = POINTER_USAGE_GESTURES;
+            } else {
+                pointerUsage = POINTER_USAGE_NONE;
+            }
+
+            dispatchPointerUsage(when, policyFlags, pointerUsage);
+        } else {
+            dispatchHoverExit(when, policyFlags);
+            dispatchTouches(when, policyFlags);
+            dispatchHoverEnterAndMove(when, policyFlags);
+        }
+
+        // Synthesize key up from raw buttons if needed.
+        synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
+                policyFlags, mLastButtonState, mCurrentButtonState);
     }
 
-    if (mPointerController != NULL && mConfig.pointerGesturesEnabled) {
-        dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
-    }
-
-    cookPointerData();
-    dispatchHoverExit(when, policyFlags);
-    dispatchTouches(when, policyFlags);
-    dispatchHoverEnterAndMove(when, policyFlags);
-
-    // Synthesize key up from raw buttons if needed.
-    synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mTouchSource,
-            policyFlags, mLastButtonState, mCurrentButtonState);
-
     // Copy current touch to last touch in preparation for the next cycle.
     mLastRawPointerData.copyFrom(mCurrentRawPointerData);
     mLastCookedPointerData.copyFrom(mCurrentCookedPointerData);
     mLastButtonState = mCurrentButtonState;
+    mLastFingerIdBits = mCurrentFingerIdBits;
+    mLastStylusIdBits = mCurrentStylusIdBits;
+    mLastMouseIdBits = mCurrentMouseIdBits;
+
+    // Clear some transient state.
+    mCurrentRawVScroll = 0;
+    mCurrentRawHScroll = 0;
 }
 
 void TouchInputMapper::timeoutExpired(nsecs_t when) {
     if (mPointerController != NULL) {
-        dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
+        if (mPointerUsage == POINTER_USAGE_GESTURES) {
+            dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
+        }
     }
 }
 
@@ -3245,7 +3553,7 @@
         if (!currentIdBits.isEmpty()) {
             // No pointer id changes so this is a move event.
             // The listener takes care of batching moves so we don't have to deal with that here.
-            dispatchMotion(when, policyFlags, mTouchSource,
+            dispatchMotion(when, policyFlags, mSource,
                     AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
                     AMOTION_EVENT_EDGE_FLAG_NONE,
                     mCurrentCookedPointerData.pointerProperties,
@@ -3280,7 +3588,7 @@
         while (!upIdBits.isEmpty()) {
             uint32_t upId = upIdBits.clearFirstMarkedBit();
 
-            dispatchMotion(when, policyFlags, mTouchSource,
+            dispatchMotion(when, policyFlags, mSource,
                     AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
                     mLastCookedPointerData.pointerProperties,
                     mLastCookedPointerData.pointerCoords,
@@ -3295,7 +3603,7 @@
         // events, they do not generally handle them except when presented in a move event.
         if (moveNeeded) {
             LOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
-            dispatchMotion(when, policyFlags, mTouchSource,
+            dispatchMotion(when, policyFlags, mSource,
                     AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
                     mCurrentCookedPointerData.pointerProperties,
                     mCurrentCookedPointerData.pointerCoords,
@@ -3314,7 +3622,7 @@
                 mDownTime = when;
             }
 
-            dispatchMotion(when, policyFlags, mTouchSource,
+            dispatchMotion(when, policyFlags, mSource,
                     AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
                     mCurrentCookedPointerData.pointerProperties,
                     mCurrentCookedPointerData.pointerCoords,
@@ -3330,7 +3638,7 @@
             (mCurrentCookedPointerData.hoveringIdBits.isEmpty()
                     || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) {
         int32_t metaState = getContext()->getGlobalMetaState();
-        dispatchMotion(when, policyFlags, mTouchSource,
+        dispatchMotion(when, policyFlags, mSource,
                 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
                 mLastCookedPointerData.pointerProperties,
                 mLastCookedPointerData.pointerCoords,
@@ -3346,7 +3654,7 @@
             && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) {
         int32_t metaState = getContext()->getGlobalMetaState();
         if (!mSentHoverEnter) {
-            dispatchMotion(when, policyFlags, mTouchSource,
+            dispatchMotion(when, policyFlags, mSource,
                     AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
                     mCurrentCookedPointerData.pointerProperties,
                     mCurrentCookedPointerData.pointerCoords,
@@ -3356,7 +3664,7 @@
             mSentHoverEnter = true;
         }
 
-        dispatchMotion(when, policyFlags, mTouchSource,
+        dispatchMotion(when, policyFlags, mSource,
                 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
                 mCurrentCookedPointerData.pointerProperties,
                 mCurrentCookedPointerData.pointerCoords,
@@ -3467,30 +3775,40 @@
             break;
         }
 
-        // Orientation
+        // Tilt and Orientation
+        float tilt;
         float orientation;
-        switch (mCalibration.orientationCalibration) {
-        case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
-            orientation = in.orientation * mOrientationScale;
-            break;
-        case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
-            int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
-            int32_t c2 = signExtendNybble(in.orientation & 0x0f);
-            if (c1 != 0 || c2 != 0) {
-                orientation = atan2f(c1, c2) * 0.5f;
-                float confidence = hypotf(c1, c2);
-                float scale = 1.0f + confidence / 16.0f;
-                touchMajor *= scale;
-                touchMinor /= scale;
-                toolMajor *= scale;
-                toolMinor /= scale;
-            } else {
+        if (mHaveTilt) {
+            float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
+            float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
+            orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
+            tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
+        } else {
+            tilt = 0;
+
+            switch (mCalibration.orientationCalibration) {
+            case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
+                orientation = (in.orientation - mOrientationCenter) * mOrientationScale;
+                break;
+            case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
+                int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
+                int32_t c2 = signExtendNybble(in.orientation & 0x0f);
+                if (c1 != 0 || c2 != 0) {
+                    orientation = atan2f(c1, c2) * 0.5f;
+                    float confidence = hypotf(c1, c2);
+                    float scale = 1.0f + confidence / 16.0f;
+                    touchMajor *= scale;
+                    touchMinor /= scale;
+                    toolMajor *= scale;
+                    toolMinor /= scale;
+                } else {
+                    orientation = 0;
+                }
+                break;
+            }
+            default:
                 orientation = 0;
             }
-            break;
-        }
-        default:
-            orientation = 0;
         }
 
         // Distance
@@ -3545,6 +3863,7 @@
         out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
         out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
         out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
+        out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
         out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
 
         // Write output properties.
@@ -3559,6 +3878,46 @@
     }
 }
 
+void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags,
+        PointerUsage pointerUsage) {
+    if (pointerUsage != mPointerUsage) {
+        abortPointerUsage(when, policyFlags);
+        mPointerUsage = pointerUsage;
+    }
+
+    switch (mPointerUsage) {
+    case POINTER_USAGE_GESTURES:
+        dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
+        break;
+    case POINTER_USAGE_STYLUS:
+        dispatchPointerStylus(when, policyFlags);
+        break;
+    case POINTER_USAGE_MOUSE:
+        dispatchPointerMouse(when, policyFlags);
+        break;
+    default:
+        break;
+    }
+}
+
+void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) {
+    switch (mPointerUsage) {
+    case POINTER_USAGE_GESTURES:
+        abortPointerGestures(when, policyFlags);
+        break;
+    case POINTER_USAGE_STYLUS:
+        abortPointerStylus(when, policyFlags);
+        break;
+    case POINTER_USAGE_MOUSE:
+        abortPointerMouse(when, policyFlags);
+        break;
+    default:
+        break;
+    }
+
+    mPointerUsage = POINTER_USAGE_NONE;
+}
+
 void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
         bool isTimeout) {
     // Update current gesture coordinates.
@@ -3649,7 +4008,7 @@
     BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
     if (!dispatchedGestureIdBits.isEmpty()) {
         if (cancelPreviousGesture) {
-            dispatchMotion(when, policyFlags, mPointerSource,
+            dispatchMotion(when, policyFlags, mSource,
                     AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
                     AMOTION_EVENT_EDGE_FLAG_NONE,
                     mPointerGesture.lastGestureProperties,
@@ -3669,7 +4028,7 @@
             while (!upGestureIdBits.isEmpty()) {
                 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
 
-                dispatchMotion(when, policyFlags, mPointerSource,
+                dispatchMotion(when, policyFlags, mSource,
                         AMOTION_EVENT_ACTION_POINTER_UP, 0,
                         metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
                         mPointerGesture.lastGestureProperties,
@@ -3684,7 +4043,7 @@
 
     // Send motion events for all pointers that moved.
     if (moveNeeded) {
-        dispatchMotion(when, policyFlags, mPointerSource,
+        dispatchMotion(when, policyFlags, mSource,
                 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
                 mPointerGesture.currentGestureProperties,
                 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
@@ -3704,7 +4063,7 @@
                 mPointerGesture.downTime = when;
             }
 
-            dispatchMotion(when, policyFlags, mPointerSource,
+            dispatchMotion(when, policyFlags, mSource,
                     AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
                     mPointerGesture.currentGestureProperties,
                     mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
@@ -3715,7 +4074,7 @@
 
     // Send motion events for hover.
     if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
-        dispatchMotion(when, policyFlags, mPointerSource,
+        dispatchMotion(when, policyFlags, mSource,
                 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
                 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
                 mPointerGesture.currentGestureProperties,
@@ -3741,7 +4100,7 @@
         pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
         pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
 
-        NotifyMotionArgs args(when, getDeviceId(), mPointerSource, policyFlags,
+        NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
                 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
                 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
                 1, &pointerProperties, &pointerCoords, 0, 0, mPointerGesture.downTime);
@@ -3766,6 +4125,31 @@
     }
 }
 
+void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) {
+    // Cancel previously dispatches pointers.
+    if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
+        int32_t metaState = getContext()->getGlobalMetaState();
+        int32_t buttonState = mCurrentButtonState;
+        dispatchMotion(when, policyFlags, mSource,
+                AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
+                AMOTION_EVENT_EDGE_FLAG_NONE,
+                mPointerGesture.lastGestureProperties,
+                mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
+                mPointerGesture.lastGestureIdBits, -1,
+                0, 0, mPointerGesture.downTime);
+    }
+
+    // Reset the current pointer gesture.
+    mPointerGesture.reset();
+    mPointerVelocityControl.reset();
+
+    // Remove any current spots.
+    if (mPointerController != NULL) {
+        mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
+        mPointerController->clearSpots();
+    }
+}
+
 bool TouchInputMapper::preparePointerGestures(nsecs_t when,
         bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
     *outCancelPreviousGesture = false;
@@ -3793,7 +4177,7 @@
                 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
                 mPointerGesture.currentGestureIdBits.clear();
 
-                mPointerGesture.pointerVelocityControl.reset();
+                mPointerVelocityControl.reset();
                 return true;
             }
         }
@@ -3802,18 +4186,21 @@
         return false;
     }
 
+    const uint32_t currentFingerCount = mCurrentFingerIdBits.count();
+    const uint32_t lastFingerCount = mLastFingerIdBits.count();
+
     // Update the velocity tracker.
     {
         VelocityTracker::Position positions[MAX_POINTERS];
         uint32_t count = 0;
-        for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); count++) {
+        for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); count++) {
             uint32_t id = idBits.clearFirstMarkedBit();
             const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
-            positions[count].x = pointer.x * mPointerGestureXMovementScale;
-            positions[count].y = pointer.y * mPointerGestureYMovementScale;
+            positions[count].x = pointer.x * mPointerXMovementScale;
+            positions[count].y = pointer.y * mPointerYMovementScale;
         }
         mPointerGesture.velocityTracker.addMovement(when,
-                mCurrentRawPointerData.touchingIdBits, positions);
+                mCurrentFingerIdBits, positions);
     }
 
     // Pick a new active touch id if needed.
@@ -3825,25 +4212,22 @@
     int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
     int32_t activeTouchId = lastActiveTouchId;
     if (activeTouchId < 0) {
-        if (!mCurrentRawPointerData.touchingIdBits.isEmpty()) {
+        if (!mCurrentFingerIdBits.isEmpty()) {
             activeTouchChanged = true;
             activeTouchId = mPointerGesture.activeTouchId =
-                    mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
+                    mCurrentFingerIdBits.firstMarkedBit();
             mPointerGesture.firstTouchTime = when;
         }
-    } else if (!mCurrentRawPointerData.touchingIdBits.hasBit(activeTouchId)) {
+    } else if (!mCurrentFingerIdBits.hasBit(activeTouchId)) {
         activeTouchChanged = true;
-        if (!mCurrentRawPointerData.touchingIdBits.isEmpty()) {
+        if (!mCurrentFingerIdBits.isEmpty()) {
             activeTouchId = mPointerGesture.activeTouchId =
-                    mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
+                    mCurrentFingerIdBits.firstMarkedBit();
         } else {
             activeTouchId = mPointerGesture.activeTouchId = -1;
         }
     }
 
-    uint32_t currentTouchingPointerCount = mCurrentRawPointerData.touchingIdBits.count();
-    uint32_t lastTouchingPointerCount = mLastRawPointerData.touchingIdBits.count();
-
     // Determine whether we are in quiet time.
     bool isQuietTime = false;
     if (activeTouchId < 0) {
@@ -3854,13 +4238,13 @@
             if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
                     || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
                     || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
-                    && currentTouchingPointerCount < 2) {
+                    && currentFingerCount < 2) {
                 // Enter quiet time when exiting swipe or freeform state.
                 // This is to prevent accidentally entering the hover state and flinging the
                 // pointer when finishing a swipe and there is still one pointer left onscreen.
                 isQuietTime = true;
             } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
-                    && currentTouchingPointerCount >= 2
+                    && currentFingerCount >= 2
                     && !isPointerDown(mCurrentButtonState)) {
                 // Enter quiet time when releasing the button and there are still two or more
                 // fingers down.  This may indicate that one finger was used to press the button
@@ -3888,7 +4272,7 @@
         mPointerGesture.currentGestureMode = PointerGesture::QUIET;
         mPointerGesture.currentGestureIdBits.clear();
 
-        mPointerGesture.pointerVelocityControl.reset();
+        mPointerVelocityControl.reset();
     } else if (isPointerDown(mCurrentButtonState)) {
         // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
         // The pointer follows the active touch point.
@@ -3905,7 +4289,7 @@
         // being dragged.
 #if DEBUG_GESTURES
         LOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
-                "currentTouchingPointerCount=%d", activeTouchId, currentTouchingPointerCount);
+                "currentFingerCount=%d", activeTouchId, currentFingerCount);
 #endif
         // Reset state when just starting.
         if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
@@ -3915,10 +4299,10 @@
 
         // Switch pointers if needed.
         // Find the fastest pointer and follow it.
-        if (activeTouchId >= 0 && currentTouchingPointerCount > 1) {
+        if (activeTouchId >= 0 && currentFingerCount > 1) {
             int32_t bestId = -1;
             float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
-            for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) {
+            for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); ) {
                 uint32_t id = idBits.clearFirstMarkedBit();
                 float vx, vy;
                 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
@@ -3939,23 +4323,23 @@
             }
         }
 
-        if (activeTouchId >= 0 && mLastRawPointerData.touchingIdBits.hasBit(activeTouchId)) {
+        if (activeTouchId >= 0 && mLastFingerIdBits.hasBit(activeTouchId)) {
             const RawPointerData::Pointer& currentPointer =
                     mCurrentRawPointerData.pointerForId(activeTouchId);
             const RawPointerData::Pointer& lastPointer =
                     mLastRawPointerData.pointerForId(activeTouchId);
-            float deltaX = (currentPointer.x - lastPointer.x) * mPointerGestureXMovementScale;
-            float deltaY = (currentPointer.y - lastPointer.y) * mPointerGestureYMovementScale;
+            float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
+            float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
 
             rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
-            mPointerGesture.pointerVelocityControl.move(when, &deltaX, &deltaY);
+            mPointerVelocityControl.move(when, &deltaX, &deltaY);
 
             // Move the pointer using a relative motion.
             // When using spots, the click will occur at the position of the anchor
             // spot and all other spots will move there.
             mPointerController->move(deltaX, deltaY);
         } else {
-            mPointerGesture.pointerVelocityControl.reset();
+            mPointerVelocityControl.reset();
         }
 
         float x, y;
@@ -3972,7 +4356,7 @@
         mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
         mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
         mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
-    } else if (currentTouchingPointerCount == 0) {
+    } else if (currentFingerCount == 0) {
         // Case 3. No fingers down and button is not pressed. (NEUTRAL)
         if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) {
             *outFinishPreviousGesture = true;
@@ -3983,7 +4367,7 @@
         bool tapped = false;
         if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
                 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
-                && lastTouchingPointerCount == 1) {
+                && lastFingerCount == 1) {
             if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
                 float x, y;
                 mPointerController->getPosition(&x, &y);
@@ -4033,7 +4417,7 @@
             }
         }
 
-        mPointerGesture.pointerVelocityControl.reset();
+        mPointerVelocityControl.reset();
 
         if (!tapped) {
 #if DEBUG_GESTURES
@@ -4043,7 +4427,7 @@
             mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
             mPointerGesture.currentGestureIdBits.clear();
         }
-    } else if (currentTouchingPointerCount == 1) {
+    } else if (currentFingerCount == 1) {
         // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
         // The pointer follows the active touch point.
         // When in HOVER, emit HOVER_MOVE events at the pointer location.
@@ -4075,24 +4459,24 @@
             mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
         }
 
-        if (mLastRawPointerData.touchingIdBits.hasBit(activeTouchId)) {
+        if (mLastFingerIdBits.hasBit(activeTouchId)) {
             const RawPointerData::Pointer& currentPointer =
                     mCurrentRawPointerData.pointerForId(activeTouchId);
             const RawPointerData::Pointer& lastPointer =
                     mLastRawPointerData.pointerForId(activeTouchId);
             float deltaX = (currentPointer.x - lastPointer.x)
-                    * mPointerGestureXMovementScale;
+                    * mPointerXMovementScale;
             float deltaY = (currentPointer.y - lastPointer.y)
-                    * mPointerGestureYMovementScale;
+                    * mPointerYMovementScale;
 
             rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
-            mPointerGesture.pointerVelocityControl.move(when, &deltaX, &deltaY);
+            mPointerVelocityControl.move(when, &deltaX, &deltaY);
 
             // Move the pointer using a relative motion.
             // When using spots, the hover or drag will occur at the position of the anchor spot.
             mPointerController->move(deltaX, deltaY);
         } else {
-            mPointerGesture.pointerVelocityControl.reset();
+            mPointerVelocityControl.reset();
         }
 
         bool down;
@@ -4128,7 +4512,7 @@
         mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
                 down ? 1.0f : 0.0f);
 
-        if (lastTouchingPointerCount == 0 && currentTouchingPointerCount != 0) {
+        if (lastFingerCount == 0 && currentFingerCount != 0) {
             mPointerGesture.resetTap();
             mPointerGesture.tapDownTime = when;
             mPointerGesture.tapX = x;
@@ -4156,7 +4540,7 @@
                 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
                 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
             *outFinishPreviousGesture = true;
-        } else if (!settled && currentTouchingPointerCount > lastTouchingPointerCount) {
+        } else if (!settled && currentFingerCount > lastFingerCount) {
             // Additional pointers have gone down but not yet settled.
             // Reset the gesture.
 #if DEBUG_GESTURES
@@ -4175,7 +4559,7 @@
             mPointerGesture.currentGestureMode = PointerGesture::PRESS;
             mPointerGesture.activeGestureId = 0;
             mPointerGesture.referenceIdBits.clear();
-            mPointerGesture.pointerVelocityControl.reset();
+            mPointerVelocityControl.reset();
 
             // Use the centroid and pointer location as the reference points for the gesture.
 #if DEBUG_GESTURES
@@ -4192,18 +4576,18 @@
         }
 
         // Clear the reference deltas for fingers not yet included in the reference calculation.
-        for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits.value
+        for (BitSet32 idBits(mCurrentFingerIdBits.value
                 & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) {
             uint32_t id = idBits.clearFirstMarkedBit();
             mPointerGesture.referenceDeltas[id].dx = 0;
             mPointerGesture.referenceDeltas[id].dy = 0;
         }
-        mPointerGesture.referenceIdBits = mCurrentRawPointerData.touchingIdBits;
+        mPointerGesture.referenceIdBits = mCurrentFingerIdBits;
 
         // Add delta for all fingers and calculate a common movement delta.
         float commonDeltaX = 0, commonDeltaY = 0;
-        BitSet32 commonIdBits(mLastRawPointerData.touchingIdBits.value
-                & mCurrentRawPointerData.touchingIdBits.value);
+        BitSet32 commonIdBits(mLastFingerIdBits.value
+                & mCurrentFingerIdBits.value);
         for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
             bool first = (idBits == commonIdBits);
             uint32_t id = idBits.clearFirstMarkedBit();
@@ -4229,8 +4613,8 @@
             for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
                 uint32_t id = idBits.clearFirstMarkedBit();
                 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
-                dist[id] = hypotf(delta.dx * mPointerGestureXZoomScale,
-                        delta.dy * mPointerGestureYZoomScale);
+                dist[id] = hypotf(delta.dx * mPointerXZoomScale,
+                        delta.dy * mPointerYZoomScale);
                 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
                     distOverThreshold += 1;
                 }
@@ -4239,17 +4623,17 @@
             // Only transition when at least two pointers have moved further than
             // the minimum distance threshold.
             if (distOverThreshold >= 2) {
-                if (currentTouchingPointerCount > 2) {
+                if (currentFingerCount > 2) {
                     // There are more than two pointers, switch to FREEFORM.
 #if DEBUG_GESTURES
                     LOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
-                            currentTouchingPointerCount);
+                            currentFingerCount);
 #endif
                     *outCancelPreviousGesture = true;
                     mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
                 } else {
                     // There are exactly two pointers.
-                    BitSet32 idBits(mCurrentRawPointerData.touchingIdBits);
+                    BitSet32 idBits(mCurrentFingerIdBits);
                     uint32_t id1 = idBits.clearFirstMarkedBit();
                     uint32_t id2 = idBits.firstMarkedBit();
                     const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1);
@@ -4277,10 +4661,10 @@
                             // approches 1.0.  Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
                             PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
                             PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
-                            float dx1 = delta1.dx * mPointerGestureXZoomScale;
-                            float dy1 = delta1.dy * mPointerGestureYZoomScale;
-                            float dx2 = delta2.dx * mPointerGestureXZoomScale;
-                            float dy2 = delta2.dy * mPointerGestureYZoomScale;
+                            float dx1 = delta1.dx * mPointerXZoomScale;
+                            float dy1 = delta1.dy * mPointerYZoomScale;
+                            float dx2 = delta2.dx * mPointerXZoomScale;
+                            float dy2 = delta2.dy * mPointerYZoomScale;
                             float dot = dx1 * dx2 + dy1 * dy2;
                             float cosine = dot / (dist1 * dist2); // denominator always > 0
                             if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
@@ -4314,10 +4698,10 @@
         } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
             // Switch from SWIPE to FREEFORM if additional pointers go down.
             // Cancel previous gesture.
-            if (currentTouchingPointerCount > 2) {
+            if (currentFingerCount > 2) {
 #if DEBUG_GESTURES
                 LOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
-                        currentTouchingPointerCount);
+                        currentFingerCount);
 #endif
                 *outCancelPreviousGesture = true;
                 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
@@ -4338,11 +4722,11 @@
             mPointerGesture.referenceTouchX += commonDeltaX;
             mPointerGesture.referenceTouchY += commonDeltaY;
 
-            commonDeltaX *= mPointerGestureXMovementScale;
-            commonDeltaY *= mPointerGestureYMovementScale;
+            commonDeltaX *= mPointerXMovementScale;
+            commonDeltaY *= mPointerYMovementScale;
 
             rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY);
-            mPointerGesture.pointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
+            mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
 
             mPointerGesture.referenceGestureX += commonDeltaX;
             mPointerGesture.referenceGestureY += commonDeltaY;
@@ -4355,7 +4739,7 @@
 #if DEBUG_GESTURES
             LOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
                     "activeGestureId=%d, currentTouchPointerCount=%d",
-                    activeTouchId, mPointerGesture.activeGestureId, currentTouchingPointerCount);
+                    activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
 #endif
             LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
 
@@ -4377,7 +4761,7 @@
 #if DEBUG_GESTURES
             LOGD("Gestures: FREEFORM activeTouchId=%d,"
                     "activeGestureId=%d, currentTouchPointerCount=%d",
-                    activeTouchId, mPointerGesture.activeGestureId, currentTouchingPointerCount);
+                    activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
 #endif
             LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
 
@@ -4399,14 +4783,14 @@
             } else {
                 // Otherwise, assume we mapped all touches from the previous frame.
                 // Reuse all mappings that are still applicable.
-                mappedTouchIdBits.value = mLastRawPointerData.touchingIdBits.value
-                        & mCurrentRawPointerData.touchingIdBits.value;
+                mappedTouchIdBits.value = mLastFingerIdBits.value
+                        & mCurrentFingerIdBits.value;
                 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
 
                 // Check whether we need to choose a new active gesture id because the
                 // current went went up.
-                for (BitSet32 upTouchIdBits(mLastRawPointerData.touchingIdBits.value
-                        & ~mCurrentRawPointerData.touchingIdBits.value);
+                for (BitSet32 upTouchIdBits(mLastFingerIdBits.value
+                        & ~mCurrentFingerIdBits.value);
                         !upTouchIdBits.isEmpty(); ) {
                     uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
                     uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
@@ -4425,8 +4809,8 @@
                     mPointerGesture.activeGestureId);
 #endif
 
-            BitSet32 idBits(mCurrentRawPointerData.touchingIdBits);
-            for (uint32_t i = 0; i < currentTouchingPointerCount; i++) {
+            BitSet32 idBits(mCurrentFingerIdBits);
+            for (uint32_t i = 0; i < currentFingerCount; i++) {
                 uint32_t touchId = idBits.clearFirstMarkedBit();
                 uint32_t gestureId;
                 if (!mappedTouchIdBits.hasBit(touchId)) {
@@ -4451,9 +4835,9 @@
                 const RawPointerData::Pointer& pointer =
                         mCurrentRawPointerData.pointerForId(touchId);
                 float deltaX = (pointer.x - mPointerGesture.referenceTouchX)
-                        * mPointerGestureXZoomScale;
+                        * mPointerXZoomScale;
                 float deltaY = (pointer.y - mPointerGesture.referenceTouchY)
-                        * mPointerGestureYZoomScale;
+                        * mPointerYZoomScale;
                 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
 
                 mPointerGesture.currentGestureProperties[i].clear();
@@ -4517,6 +4901,215 @@
     return true;
 }
 
+void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) {
+    mPointerSimple.currentCoords.clear();
+    mPointerSimple.currentProperties.clear();
+
+    bool down, hovering;
+    if (!mCurrentStylusIdBits.isEmpty()) {
+        uint32_t id = mCurrentStylusIdBits.firstMarkedBit();
+        uint32_t index = mCurrentCookedPointerData.idToIndex[id];
+        float x = mCurrentCookedPointerData.pointerCoords[index].getX();
+        float y = mCurrentCookedPointerData.pointerCoords[index].getY();
+        mPointerController->setPosition(x, y);
+
+        hovering = mCurrentCookedPointerData.hoveringIdBits.hasBit(id);
+        down = !hovering;
+
+        mPointerController->getPosition(&x, &y);
+        mPointerSimple.currentCoords.copyFrom(mCurrentCookedPointerData.pointerCoords[index]);
+        mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
+        mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
+        mPointerSimple.currentProperties.id = 0;
+        mPointerSimple.currentProperties.toolType =
+                mCurrentCookedPointerData.pointerProperties[index].toolType;
+    } else {
+        down = false;
+        hovering = false;
+    }
+
+    dispatchPointerSimple(when, policyFlags, down, hovering);
+}
+
+void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) {
+    abortPointerSimple(when, policyFlags);
+}
+
+void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) {
+    mPointerSimple.currentCoords.clear();
+    mPointerSimple.currentProperties.clear();
+
+    bool down, hovering;
+    if (!mCurrentMouseIdBits.isEmpty()) {
+        uint32_t id = mCurrentMouseIdBits.firstMarkedBit();
+        uint32_t currentIndex = mCurrentRawPointerData.idToIndex[id];
+        if (mLastMouseIdBits.hasBit(id)) {
+            uint32_t lastIndex = mCurrentRawPointerData.idToIndex[id];
+            float deltaX = (mCurrentRawPointerData.pointers[currentIndex].x
+                    - mLastRawPointerData.pointers[lastIndex].x)
+                    * mPointerXMovementScale;
+            float deltaY = (mCurrentRawPointerData.pointers[currentIndex].y
+                    - mLastRawPointerData.pointers[lastIndex].y)
+                    * mPointerYMovementScale;
+
+            rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
+            mPointerVelocityControl.move(when, &deltaX, &deltaY);
+
+            mPointerController->move(deltaX, deltaY);
+        } else {
+            mPointerVelocityControl.reset();
+        }
+
+        down = isPointerDown(mCurrentButtonState);
+        hovering = !down;
+
+        float x, y;
+        mPointerController->getPosition(&x, &y);
+        mPointerSimple.currentCoords.copyFrom(
+                mCurrentCookedPointerData.pointerCoords[currentIndex]);
+        mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
+        mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
+        mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
+                hovering ? 0.0f : 1.0f);
+        mPointerSimple.currentProperties.id = 0;
+        mPointerSimple.currentProperties.toolType =
+                mCurrentCookedPointerData.pointerProperties[currentIndex].toolType;
+    } else {
+        mPointerVelocityControl.reset();
+
+        down = false;
+        hovering = false;
+    }
+
+    dispatchPointerSimple(when, policyFlags, down, hovering);
+}
+
+void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) {
+    abortPointerSimple(when, policyFlags);
+
+    mPointerVelocityControl.reset();
+}
+
+void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
+        bool down, bool hovering) {
+    int32_t metaState = getContext()->getGlobalMetaState();
+
+    if (mPointerController != NULL) {
+        if (down || hovering) {
+            mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
+            mPointerController->clearSpots();
+            mPointerController->setButtonState(mCurrentButtonState);
+            mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
+        } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
+            mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
+        }
+    }
+
+    if (mPointerSimple.down && !down) {
+        mPointerSimple.down = false;
+
+        // Send up.
+        NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
+                 AMOTION_EVENT_ACTION_UP, 0, metaState, mLastButtonState, 0,
+                 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
+                 mOrientedXPrecision, mOrientedYPrecision,
+                 mPointerSimple.downTime);
+        getListener()->notifyMotion(&args);
+    }
+
+    if (mPointerSimple.hovering && !hovering) {
+        mPointerSimple.hovering = false;
+
+        // Send hover exit.
+        NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
+                AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
+                1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
+                mOrientedXPrecision, mOrientedYPrecision,
+                mPointerSimple.downTime);
+        getListener()->notifyMotion(&args);
+    }
+
+    if (down) {
+        if (!mPointerSimple.down) {
+            mPointerSimple.down = true;
+            mPointerSimple.downTime = when;
+
+            // Send down.
+            NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
+                    AMOTION_EVENT_ACTION_DOWN, 0, metaState, mCurrentButtonState, 0,
+                    1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
+                    mOrientedXPrecision, mOrientedYPrecision,
+                    mPointerSimple.downTime);
+            getListener()->notifyMotion(&args);
+        }
+
+        // Send move.
+        NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
+                AMOTION_EVENT_ACTION_MOVE, 0, metaState, mCurrentButtonState, 0,
+                1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
+                mOrientedXPrecision, mOrientedYPrecision,
+                mPointerSimple.downTime);
+        getListener()->notifyMotion(&args);
+    }
+
+    if (hovering) {
+        if (!mPointerSimple.hovering) {
+            mPointerSimple.hovering = true;
+
+            // Send hover enter.
+            NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
+                    AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
+                    1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
+                    mOrientedXPrecision, mOrientedYPrecision,
+                    mPointerSimple.downTime);
+            getListener()->notifyMotion(&args);
+        }
+
+        // Send hover move.
+        NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
+                AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
+                1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
+                mOrientedXPrecision, mOrientedYPrecision,
+                mPointerSimple.downTime);
+        getListener()->notifyMotion(&args);
+    }
+
+    if (mCurrentRawVScroll || mCurrentRawHScroll) {
+        float vscroll = mCurrentRawVScroll;
+        float hscroll = mCurrentRawHScroll;
+        mWheelYVelocityControl.move(when, NULL, &vscroll);
+        mWheelXVelocityControl.move(when, &hscroll, NULL);
+
+        // Send scroll.
+        PointerCoords pointerCoords;
+        pointerCoords.copyFrom(mPointerSimple.currentCoords);
+        pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
+        pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
+
+        NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
+                AMOTION_EVENT_ACTION_SCROLL, 0, metaState, mCurrentButtonState, 0,
+                1, &mPointerSimple.currentProperties, &pointerCoords,
+                mOrientedXPrecision, mOrientedYPrecision,
+                mPointerSimple.downTime);
+        getListener()->notifyMotion(&args);
+    }
+
+    // Save state.
+    if (down || hovering) {
+        mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
+        mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
+    } else {
+        mPointerSimple.reset();
+    }
+}
+
+void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) {
+    mPointerSimple.currentCoords.clear();
+    mPointerSimple.currentProperties.clear();
+
+    dispatchPointerSimple(when, policyFlags, false, false);
+}
+
 void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
         int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
         const PointerProperties* properties, const PointerCoords* coords,
@@ -4862,44 +5455,32 @@
 
 SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
         TouchInputMapper(device) {
-    clearState();
 }
 
 SingleTouchInputMapper::~SingleTouchInputMapper() {
 }
 
-void SingleTouchInputMapper::clearState() {
-    mCursorButtonAccumulator.clearButtons();
-    mTouchButtonAccumulator.clearButtons();
-    mSingleTouchMotionAccumulator.clearAbsoluteAxes();
+void SingleTouchInputMapper::reset(nsecs_t when) {
+    mSingleTouchMotionAccumulator.reset(getDevice());
+
+    TouchInputMapper::reset(when);
 }
 
-void SingleTouchInputMapper::reset() {
-    TouchInputMapper::reset();
-
-    clearState();
- }
-
 void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
-    mCursorButtonAccumulator.process(rawEvent);
-    mTouchButtonAccumulator.process(rawEvent);
-    mSingleTouchMotionAccumulator.process(rawEvent);
+    TouchInputMapper::process(rawEvent);
 
-    if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
-        sync(rawEvent->when);
-    }
+    mSingleTouchMotionAccumulator.process(rawEvent);
 }
 
-void SingleTouchInputMapper::sync(nsecs_t when) {
-    mCurrentRawPointerData.clear();
-    mCurrentButtonState = 0;
-
+void SingleTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
     if (mTouchButtonAccumulator.isToolActive()) {
         mCurrentRawPointerData.pointerCount = 1;
         mCurrentRawPointerData.idToIndex[0] = 0;
 
-        bool isHovering = mTouchButtonAccumulator.isHovering()
-                || mSingleTouchMotionAccumulator.getAbsoluteDistance() > 0;
+        bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
+                && (mTouchButtonAccumulator.isHovering()
+                        || (mRawPointerAxes.pressure.valid
+                                && mSingleTouchMotionAccumulator.getAbsolutePressure() <= 0));
         mCurrentRawPointerData.markIdBit(0, isHovering);
 
         RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0];
@@ -4913,29 +5494,26 @@
         outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
         outPointer.orientation = 0;
         outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance();
+        outPointer.tiltX = mSingleTouchMotionAccumulator.getAbsoluteTiltX();
+        outPointer.tiltY = mSingleTouchMotionAccumulator.getAbsoluteTiltY();
         outPointer.toolType = mTouchButtonAccumulator.getToolType();
         if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
             outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
         }
         outPointer.isHovering = isHovering;
     }
-
-    mCurrentButtonState = mTouchButtonAccumulator.getButtonState()
-            | mCursorButtonAccumulator.getButtonState();
-
-    syncTouch(when, true);
 }
 
 void SingleTouchInputMapper::configureRawPointerAxes() {
     TouchInputMapper::configureRawPointerAxes();
 
-    mTouchButtonAccumulator.configure(getDevice());
-
     getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x);
     getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y);
     getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure);
     getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor);
     getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance);
+    getAbsoluteAxisInfo(ABS_TILT_X, &mRawPointerAxes.tiltX);
+    getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY);
 }
 
 
@@ -4948,58 +5526,25 @@
 MultiTouchInputMapper::~MultiTouchInputMapper() {
 }
 
-void MultiTouchInputMapper::clearState() {
-    mCursorButtonAccumulator.clearButtons();
-    mTouchButtonAccumulator.clearButtons();
+void MultiTouchInputMapper::reset(nsecs_t when) {
+    mMultiTouchMotionAccumulator.reset(getDevice());
+
     mPointerIdBits.clear();
 
-    if (mMultiTouchMotionAccumulator.isUsingSlotsProtocol()) {
-        // Query the driver for the current slot index and use it as the initial slot
-        // before we start reading events from the device.  It is possible that the
-        // current slot index will not be the same as it was when the first event was
-        // written into the evdev buffer, which means the input mapper could start
-        // out of sync with the initial state of the events in the evdev buffer.
-        // In the extremely unlikely case that this happens, the data from
-        // two slots will be confused until the next ABS_MT_SLOT event is received.
-        // This can cause the touch point to "jump", but at least there will be
-        // no stuck touches.
-        int32_t initialSlot;
-        status_t status = getEventHub()->getAbsoluteAxisValue(getDeviceId(), ABS_MT_SLOT,
-                &initialSlot);
-        if (status) {
-            LOGW("Could not retrieve current multitouch slot index.  status=%d", status);
-            initialSlot = -1;
-        }
-        mMultiTouchMotionAccumulator.clearSlots(initialSlot);
-    } else {
-        mMultiTouchMotionAccumulator.clearSlots(-1);
-    }
-}
-
-void MultiTouchInputMapper::reset() {
-    TouchInputMapper::reset();
-
-    clearState();
+    TouchInputMapper::reset(when);
 }
 
 void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
-    mCursorButtonAccumulator.process(rawEvent);
-    mTouchButtonAccumulator.process(rawEvent);
-    mMultiTouchMotionAccumulator.process(rawEvent);
+    TouchInputMapper::process(rawEvent);
 
-    if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
-        sync(rawEvent->when);
-    }
+    mMultiTouchMotionAccumulator.process(rawEvent);
 }
 
-void MultiTouchInputMapper::sync(nsecs_t when) {
+void MultiTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
     size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
     size_t outCount = 0;
-    bool havePointerIds = true;
     BitSet32 newPointerIdBits;
 
-    mCurrentRawPointerData.clear();
-
     for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
         const MultiTouchMotionAccumulator::Slot* inSlot =
                 mMultiTouchMotionAccumulator.getSlot(inIndex);
@@ -5026,6 +5571,8 @@
         outPointer.toolMinor = inSlot->getToolMinor();
         outPointer.orientation = inSlot->getOrientation();
         outPointer.distance = inSlot->getDistance();
+        outPointer.tiltX = 0;
+        outPointer.tiltY = 0;
 
         outPointer.toolType = inSlot->getToolType();
         if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
@@ -5035,12 +5582,13 @@
             }
         }
 
-        bool isHovering = mTouchButtonAccumulator.isHovering()
-                || inSlot->getDistance() > 0;
+        bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
+                && (mTouchButtonAccumulator.isHovering()
+                        || (mRawPointerAxes.pressure.valid && inSlot->getPressure() <= 0));
         outPointer.isHovering = isHovering;
 
         // Assign pointer id using tracking id if available.
-        if (havePointerIds) {
+        if (*outHavePointerIds) {
             int32_t trackingId = inSlot->getTrackingId();
             int32_t id = -1;
             if (trackingId >= 0) {
@@ -5057,7 +5605,7 @@
                 }
             }
             if (id < 0) {
-                havePointerIds = false;
+                *outHavePointerIds = false;
                 mCurrentRawPointerData.clearIdBits();
                 newPointerIdBits.clear();
             } else {
@@ -5072,23 +5620,14 @@
     }
 
     mCurrentRawPointerData.pointerCount = outCount;
-    mCurrentButtonState = mTouchButtonAccumulator.getButtonState()
-            | mCursorButtonAccumulator.getButtonState();
-
     mPointerIdBits = newPointerIdBits;
 
-    syncTouch(when, havePointerIds);
-
-    if (!mMultiTouchMotionAccumulator.isUsingSlotsProtocol()) {
-        mMultiTouchMotionAccumulator.clearSlots(-1);
-    }
+    mMultiTouchMotionAccumulator.finishSync();
 }
 
 void MultiTouchInputMapper::configureRawPointerAxes() {
     TouchInputMapper::configureRawPointerAxes();
 
-    mTouchButtonAccumulator.configure(getDevice());
-
     getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x);
     getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y);
     getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor);
@@ -5115,8 +5654,6 @@
     } else {
         mMultiTouchMotionAccumulator.configure(MAX_POINTERS, false /*usingSlotsProtocol*/);
     }
-
-    clearState();
 }
 
 
@@ -5184,8 +5721,9 @@
     }
 }
 
-void JoystickInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
-    InputMapper::configure(config, changes);
+void JoystickInputMapper::configure(nsecs_t when,
+        const InputReaderConfiguration* config, uint32_t changes) {
+    InputMapper::configure(when, config, changes);
 
     if (!changes) { // first time only
         // Collect all axes.
@@ -5314,19 +5852,15 @@
     }
 }
 
-void JoystickInputMapper::reset() {
+void JoystickInputMapper::reset(nsecs_t when) {
     // Recenter all axes.
-    nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
-
     size_t numAxes = mAxes.size();
     for (size_t i = 0; i < numAxes; i++) {
         Axis& axis = mAxes.editValueAt(i);
         axis.resetValue();
     }
 
-    sync(when, true /*force*/);
-
-    InputMapper::reset();
+    InputMapper::reset(when);
 }
 
 void JoystickInputMapper::process(const RawEvent* rawEvent) {
diff --git a/services/input/InputReader.h b/services/input/InputReader.h
index 72802fc..76d77f1 100644
--- a/services/input/InputReader.h
+++ b/services/input/InputReader.h
@@ -53,6 +53,9 @@
         // The pointer gesture control changed.
         CHANGE_POINTER_GESTURE_ENABLEMENT = 1 << 1,
 
+        // The display size or orientation changed.
+        CHANGE_DISPLAY_INFO = 1 << 2,
+
         // All devices must be reopened.
         CHANGE_MUST_REOPEN = 1 << 31,
     };
@@ -153,6 +156,26 @@
             pointerGestureSwipeMaxWidthRatio(0.25f),
             pointerGestureMovementSpeedRatio(0.8f),
             pointerGestureZoomSpeedRatio(0.3f) { }
+
+    bool getDisplayInfo(int32_t displayId, bool external,
+            int32_t* width, int32_t* height, int32_t* orientation) const;
+
+    void setDisplayInfo(int32_t displayId, bool external,
+            int32_t width, int32_t height, int32_t orientation);
+
+private:
+    struct DisplayInfo {
+        int32_t width;
+        int32_t height;
+        int32_t orientation;
+
+        DisplayInfo() :
+            width(-1), height(-1), orientation(DISPLAY_ORIENTATION_0) {
+        }
+    };
+
+    DisplayInfo mInternalDisplay;
+    DisplayInfo mExternalDisplay;
 };
 
 
@@ -174,22 +197,6 @@
     virtual ~InputReaderPolicyInterface() { }
 
 public:
-    /* Display orientations. */
-    enum {
-        ROTATION_0 = 0,
-        ROTATION_90 = 1,
-        ROTATION_180 = 2,
-        ROTATION_270 = 3
-    };
-
-    /* Gets information about the display with the specified id.
-     * If external is true, returns the size of the external mirrored
-     * counterpart of the specified display.
-     * Returns true if the display info is available, false otherwise.
-     */
-    virtual bool getDisplayInfo(int32_t displayId, bool external,
-            int32_t* width, int32_t* height, int32_t* orientation) = 0;
-
     /* Gets the input reader configuration. */
     virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) = 0;
 
@@ -364,8 +371,8 @@
     // low-level input event decoding and device management
     void processEventsLocked(const RawEvent* rawEvents, size_t count);
 
-    void addDeviceLocked(int32_t deviceId);
-    void removeDeviceLocked(int32_t deviceId);
+    void addDeviceLocked(nsecs_t when, int32_t deviceId);
+    void removeDeviceLocked(nsecs_t when, int32_t deviceId);
     void processEventsForDeviceLocked(int32_t deviceId, const RawEvent* rawEvents, size_t count);
     void timeoutExpiredLocked(nsecs_t when);
 
@@ -431,8 +438,8 @@
 
     void dump(String8& dump);
     void addMapper(InputMapper* mapper);
-    void configure(const InputReaderConfiguration* config, uint32_t changes);
-    void reset();
+    void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
+    void reset(nsecs_t when);
     void process(const RawEvent* rawEvents, size_t count);
     void timeoutExpired(nsecs_t when);
 
@@ -447,9 +454,25 @@
 
     void fadePointer();
 
+    void notifyReset(nsecs_t when);
+
     inline const PropertyMap& getConfiguration() { return mConfiguration; }
     inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
 
+    bool hasKey(int32_t code) {
+        return getEventHub()->hasScanCode(mId, code);
+    }
+
+    bool isKeyPressed(int32_t code) {
+        return getEventHub()->getScanCodeState(mId, code) == AKEY_STATE_DOWN;
+    }
+
+    int32_t getAbsoluteAxisValue(int32_t code) {
+        int32_t value;
+        getEventHub()->getAbsoluteAxisValue(mId, code, &value);
+        return value;
+    }
+
 private:
     InputReaderContext* mContext;
     int32_t mId;
@@ -472,8 +495,8 @@
 class CursorButtonAccumulator {
 public:
     CursorButtonAccumulator();
+    void reset(InputDevice* device);
 
-    void clearButtons();
     void process(const RawEvent* rawEvent);
 
     uint32_t getButtonState() const;
@@ -487,6 +510,8 @@
     bool mBtnForward;
     bool mBtnExtra;
     bool mBtnTask;
+
+    void clearButtons();
 };
 
 
@@ -495,10 +520,32 @@
 class CursorMotionAccumulator {
 public:
     CursorMotionAccumulator();
-    void configure(InputDevice* device);
+    void reset(InputDevice* device);
+
+    void process(const RawEvent* rawEvent);
+    void finishSync();
+
+    inline int32_t getRelativeX() const { return mRelX; }
+    inline int32_t getRelativeY() const { return mRelY; }
+
+private:
+    int32_t mRelX;
+    int32_t mRelY;
 
     void clearRelativeAxes();
+};
+
+
+/* Keeps track of cursor scrolling motions. */
+
+class CursorScrollAccumulator {
+public:
+    CursorScrollAccumulator();
+    void configure(InputDevice* device);
+    void reset(InputDevice* device);
+
     void process(const RawEvent* rawEvent);
+    void finishSync();
 
     inline bool haveRelativeVWheel() const { return mHaveRelWheel; }
     inline bool haveRelativeHWheel() const { return mHaveRelHWheel; }
@@ -516,6 +563,8 @@
     int32_t mRelY;
     int32_t mRelWheel;
     int32_t mRelHWheel;
+
+    void clearRelativeAxes();
 };
 
 
@@ -524,8 +573,8 @@
 public:
     TouchButtonAccumulator();
     void configure(InputDevice* device);
+    void reset(InputDevice* device);
 
-    void clearButtons();
     void process(const RawEvent* rawEvent);
 
     uint32_t getButtonState() const;
@@ -542,6 +591,13 @@
     bool mBtnToolFinger;
     bool mBtnToolPen;
     bool mBtnToolRubber;
+    bool mBtnToolBrush;
+    bool mBtnToolPencil;
+    bool mBtnToolAirbrush;
+    bool mBtnToolMouse;
+    bool mBtnToolLens;
+
+    void clearButtons();
 };
 
 
@@ -556,6 +612,8 @@
     RawAbsoluteAxisInfo toolMinor;
     RawAbsoluteAxisInfo orientation;
     RawAbsoluteAxisInfo distance;
+    RawAbsoluteAxisInfo tiltX;
+    RawAbsoluteAxisInfo tiltY;
     RawAbsoluteAxisInfo trackingId;
     RawAbsoluteAxisInfo slot;
 
@@ -577,6 +635,8 @@
         int32_t toolMinor;
         int32_t orientation;
         int32_t distance;
+        int32_t tiltX;
+        int32_t tiltY;
         int32_t toolType; // a fully decoded AMOTION_EVENT_TOOL_TYPE constant
         bool isHovering;
     };
@@ -637,14 +697,16 @@
 public:
     SingleTouchMotionAccumulator();
 
-    void clearAbsoluteAxes();
     void process(const RawEvent* rawEvent);
+    void reset(InputDevice* device);
 
     inline int32_t getAbsoluteX() const { return mAbsX; }
     inline int32_t getAbsoluteY() const { return mAbsY; }
     inline int32_t getAbsolutePressure() const { return mAbsPressure; }
     inline int32_t getAbsoluteToolWidth() const { return mAbsToolWidth; }
     inline int32_t getAbsoluteDistance() const { return mAbsDistance; }
+    inline int32_t getAbsoluteTiltX() const { return mAbsTiltX; }
+    inline int32_t getAbsoluteTiltY() const { return mAbsTiltY; }
 
 private:
     int32_t mAbsX;
@@ -652,6 +714,10 @@
     int32_t mAbsPressure;
     int32_t mAbsToolWidth;
     int32_t mAbsDistance;
+    int32_t mAbsTiltX;
+    int32_t mAbsTiltY;
+
+    void clearAbsoluteAxes();
 };
 
 
@@ -703,11 +769,10 @@
     ~MultiTouchMotionAccumulator();
 
     void configure(size_t slotCount, bool usingSlotsProtocol);
+    void reset(InputDevice* device);
     void process(const RawEvent* rawEvent);
+    void finishSync();
 
-    void clearSlots(int32_t initialSlot);
-
-    inline bool isUsingSlotsProtocol() const { return mUsingSlotsProtocol; }
     inline size_t getSlotCount() const { return mSlotCount; }
     inline const Slot* getSlot(size_t index) const { return &mSlots[index]; }
 
@@ -716,12 +781,22 @@
     Slot* mSlots;
     size_t mSlotCount;
     bool mUsingSlotsProtocol;
+
+    void clearSlots(int32_t initialSlot);
 };
 
 
 /* An input mapper transforms raw input events into cooked event data.
  * A single input device can have multiple associated input mappers in order to interpret
  * different classes of events.
+ *
+ * InputMapper lifecycle:
+ * - create
+ * - configure with 0 changes
+ * - reset
+ * - process, process, process (may occasionally reconfigure with non-zero changes or reset)
+ * - reset
+ * - destroy
  */
 class InputMapper {
 public:
@@ -739,8 +814,8 @@
     virtual uint32_t getSources() = 0;
     virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
     virtual void dump(String8& dump);
-    virtual void configure(const InputReaderConfiguration* config, uint32_t changes);
-    virtual void reset();
+    virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
+    virtual void reset(nsecs_t when);
     virtual void process(const RawEvent* rawEvent) = 0;
     virtual void timeoutExpired(nsecs_t when);
 
@@ -788,8 +863,8 @@
     virtual uint32_t getSources();
     virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
     virtual void dump(String8& dump);
-    virtual void configure(const InputReaderConfiguration* config, uint32_t changes);
-    virtual void reset();
+    virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
+    virtual void reset(nsecs_t when);
     virtual void process(const RawEvent* rawEvent);
 
     virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
@@ -800,8 +875,6 @@
     virtual int32_t getMetaState();
 
 private:
-    Mutex mLock;
-
     struct KeyDown {
         int32_t keyCode;
         int32_t scanCode;
@@ -810,6 +883,8 @@
     uint32_t mSource;
     int32_t mKeyboardType;
 
+    int32_t mOrientation; // orientation for dpad keys
+
     Vector<KeyDown> mKeyDowns; // keys that are down
     int32_t mMetaState;
     nsecs_t mDownTime; // time of most recent key down
@@ -828,8 +903,6 @@
         bool orientationAware;
     } mParameters;
 
-    void initialize();
-
     void configureParameters();
     void dumpParameters(String8& dump);
 
@@ -856,8 +929,8 @@
     virtual uint32_t getSources();
     virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
     virtual void dump(String8& dump);
-    virtual void configure(const InputReaderConfiguration* config, uint32_t changes);
-    virtual void reset();
+    virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
+    virtual void reset(nsecs_t when);
     virtual void process(const RawEvent* rawEvent);
 
     virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
@@ -882,6 +955,7 @@
 
     CursorButtonAccumulator mCursorButtonAccumulator;
     CursorMotionAccumulator mCursorMotionAccumulator;
+    CursorScrollAccumulator mCursorScrollAccumulator;
 
     int32_t mSource;
     float mXScale;
@@ -898,13 +972,13 @@
     VelocityControl mWheelXVelocityControl;
     VelocityControl mWheelYVelocityControl;
 
+    int32_t mOrientation;
+
     sp<PointerControllerInterface> mPointerController;
 
     int32_t mButtonState;
     nsecs_t mDownTime;
 
-    void initialize();
-
     void configureParameters();
     void dumpParameters(String8& dump);
 
@@ -920,8 +994,9 @@
     virtual uint32_t getSources();
     virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
     virtual void dump(String8& dump);
-    virtual void configure(const InputReaderConfiguration* config, uint32_t changes);
-    virtual void reset();
+    virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
+    virtual void reset(nsecs_t when);
+    virtual void process(const RawEvent* rawEvent);
 
     virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
     virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
@@ -932,6 +1007,10 @@
     virtual void timeoutExpired(nsecs_t when);
 
 protected:
+    CursorButtonAccumulator mCursorButtonAccumulator;
+    CursorScrollAccumulator mCursorScrollAccumulator;
+    TouchButtonAccumulator mTouchButtonAccumulator;
+
     struct VirtualKey {
         int32_t keyCode;
         int32_t scanCode;
@@ -948,9 +1027,16 @@
         }
     };
 
-    // Input sources supported by the device.
-    uint32_t mTouchSource; // sources when reporting touch data
-    uint32_t mPointerSource; // sources when reporting pointer gestures
+    // Input sources and device mode.
+    uint32_t mSource;
+
+    enum DeviceMode {
+        DEVICE_MODE_DISABLED, // input is disabled
+        DEVICE_MODE_DIRECT, // direct mapping (touchscreen)
+        DEVICE_MODE_UNSCALED, // unscaled mapping (touchpad)
+        DEVICE_MODE_POINTER, // pointer mapping (pointer)
+    };
+    DeviceMode mDeviceMode;
 
     // The reader's configuration.
     InputReaderConfiguration mConfig;
@@ -1053,6 +1139,18 @@
     int32_t mCurrentButtonState;
     int32_t mLastButtonState;
 
+    // Scroll state.
+    int32_t mCurrentRawVScroll;
+    int32_t mCurrentRawHScroll;
+
+    // Id bits used to differentiate fingers, stylus and mouse tools.
+    BitSet32 mCurrentFingerIdBits; // finger or unknown
+    BitSet32 mLastFingerIdBits;
+    BitSet32 mCurrentStylusIdBits; // stylus or eraser
+    BitSet32 mLastStylusIdBits;
+    BitSet32 mCurrentMouseIdBits; // mouse or lens
+    BitSet32 mLastMouseIdBits;
+
     // True if we sent a HOVER_ENTER event.
     bool mSentHoverEnter;
 
@@ -1068,7 +1166,7 @@
     virtual void dumpParameters(String8& dump);
     virtual void configureRawPointerAxes();
     virtual void dumpRawPointerAxes(String8& dump);
-    virtual bool configureSurface();
+    virtual void configureSurface(nsecs_t when, bool* outResetNeeded);
     virtual void dumpSurface(String8& dump);
     virtual void configureVirtualKeys();
     virtual void dumpVirtualKeys(String8& dump);
@@ -1076,7 +1174,7 @@
     virtual void resolveCalibration();
     virtual void dumpCalibration(String8& dump);
 
-    void syncTouch(nsecs_t when, bool havePointerIds);
+    virtual void syncTouch(nsecs_t when, bool* outHavePointerIds) = 0;
 
 private:
     // The surface orientation and width and height set by configureSurface().
@@ -1102,16 +1200,21 @@
 
     float mSizeScale;
 
+    float mOrientationCenter;
     float mOrientationScale;
 
     float mDistanceScale;
 
+    bool mHaveTilt;
+    float mTiltXCenter;
+    float mTiltXScale;
+    float mTiltYCenter;
+    float mTiltYScale;
+
     // Oriented motion ranges for input device info.
     struct OrientedRanges {
         InputDeviceInfo::MotionRange x;
         InputDeviceInfo::MotionRange y;
-
-        bool havePressure;
         InputDeviceInfo::MotionRange pressure;
 
         bool haveSize;
@@ -1130,6 +1233,22 @@
 
         bool haveDistance;
         InputDeviceInfo::MotionRange distance;
+
+        bool haveTilt;
+        InputDeviceInfo::MotionRange tilt;
+
+        OrientedRanges() {
+            clear();
+        }
+
+        void clear() {
+            haveSize = false;
+            haveTouchSize = false;
+            haveToolSize = false;
+            haveOrientation = false;
+            haveDistance = false;
+            haveTilt = false;
+        }
     } mOrientedRanges;
 
     // Oriented dimensions and precision.
@@ -1146,13 +1265,13 @@
         int32_t scanCode;
     } mCurrentVirtualKey;
 
-    // Scale factor for gesture based pointer movements.
-    float mPointerGestureXMovementScale;
-    float mPointerGestureYMovementScale;
+    // Scale factor for gesture or mouse based pointer movements.
+    float mPointerXMovementScale;
+    float mPointerYMovementScale;
 
     // Scale factor for gesture based zooming and other freeform motions.
-    float mPointerGestureXZoomScale;
-    float mPointerGestureYZoomScale;
+    float mPointerXZoomScale;
+    float mPointerYZoomScale;
 
     // The maximum swipe width.
     float mPointerGestureMaxSwipeWidth;
@@ -1163,6 +1282,14 @@
         uint64_t distance : 48; // squared distance
     };
 
+    enum PointerUsage {
+        POINTER_USAGE_NONE,
+        POINTER_USAGE_GESTURES,
+        POINTER_USAGE_STYLUS,
+        POINTER_USAGE_MOUSE,
+    };
+    PointerUsage mPointerUsage;
+
     struct PointerGesture {
         enum Mode {
             // No fingers, button is not pressed.
@@ -1273,9 +1400,6 @@
         // A velocity tracker for determining whether to switch active pointers during drags.
         VelocityTracker velocityTracker;
 
-        // Velocity control for pointer movements.
-        VelocityControl pointerVelocityControl;
-
         void reset() {
             firstTouchTime = LLONG_MIN;
             activeTouchId = -1;
@@ -1288,7 +1412,6 @@
             velocityTracker.clear();
             resetTap();
             resetQuietTime();
-            pointerVelocityControl.reset();
         }
 
         void resetTap() {
@@ -1301,7 +1424,38 @@
         }
     } mPointerGesture;
 
-    void initialize();
+    struct PointerSimple {
+        PointerCoords currentCoords;
+        PointerProperties currentProperties;
+        PointerCoords lastCoords;
+        PointerProperties lastProperties;
+
+        // True if the pointer is down.
+        bool down;
+
+        // True if the pointer is hovering.
+        bool hovering;
+
+        // Time the pointer last went down.
+        nsecs_t downTime;
+
+        void reset() {
+            currentCoords.clear();
+            currentProperties.clear();
+            lastCoords.clear();
+            lastProperties.clear();
+            down = false;
+            hovering = false;
+            downTime = 0;
+        }
+    } mPointerSimple;
+
+    // The pointer and scroll velocity controls.
+    VelocityControl mPointerVelocityControl;
+    VelocityControl mWheelXVelocityControl;
+    VelocityControl mWheelYVelocityControl;
+
+    void sync(nsecs_t when);
 
     bool consumeRawTouches(nsecs_t when, uint32_t policyFlags);
     void dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
@@ -1312,9 +1466,24 @@
     void dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags);
     void cookPointerData();
 
+    void dispatchPointerUsage(nsecs_t when, uint32_t policyFlags, PointerUsage pointerUsage);
+    void abortPointerUsage(nsecs_t when, uint32_t policyFlags);
+
     void dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, bool isTimeout);
+    void abortPointerGestures(nsecs_t when, uint32_t policyFlags);
     bool preparePointerGestures(nsecs_t when,
-            bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout);
+            bool* outCancelPreviousGesture, bool* outFinishPreviousGesture,
+            bool isTimeout);
+
+    void dispatchPointerStylus(nsecs_t when, uint32_t policyFlags);
+    void abortPointerStylus(nsecs_t when, uint32_t policyFlags);
+
+    void dispatchPointerMouse(nsecs_t when, uint32_t policyFlags);
+    void abortPointerMouse(nsecs_t when, uint32_t policyFlags);
+
+    void dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
+            bool down, bool hovering);
+    void abortPointerSimple(nsecs_t when, uint32_t policyFlags);
 
     // Dispatches a motion event.
     // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
@@ -1346,20 +1515,15 @@
     SingleTouchInputMapper(InputDevice* device);
     virtual ~SingleTouchInputMapper();
 
-    virtual void reset();
+    virtual void reset(nsecs_t when);
     virtual void process(const RawEvent* rawEvent);
 
 protected:
+    virtual void syncTouch(nsecs_t when, bool* outHavePointerIds);
     virtual void configureRawPointerAxes();
 
 private:
-    CursorButtonAccumulator mCursorButtonAccumulator;
-    TouchButtonAccumulator mTouchButtonAccumulator;
     SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
-
-    void clearState();
-
-    void sync(nsecs_t when);
 };
 
 
@@ -1368,24 +1532,19 @@
     MultiTouchInputMapper(InputDevice* device);
     virtual ~MultiTouchInputMapper();
 
-    virtual void reset();
+    virtual void reset(nsecs_t when);
     virtual void process(const RawEvent* rawEvent);
 
 protected:
+    virtual void syncTouch(nsecs_t when, bool* outHavePointerIds);
     virtual void configureRawPointerAxes();
 
 private:
-    CursorButtonAccumulator mCursorButtonAccumulator;
-    TouchButtonAccumulator mTouchButtonAccumulator;
     MultiTouchMotionAccumulator mMultiTouchMotionAccumulator;
 
     // Specifies the pointer id bits that are in use, and their associated tracking id.
     BitSet32 mPointerIdBits;
     int32_t mPointerTrackingIdMap[MAX_POINTER_ID + 1];
-
-    void clearState();
-
-    void sync(nsecs_t when);
 };
 
 
@@ -1397,8 +1556,8 @@
     virtual uint32_t getSources();
     virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
     virtual void dump(String8& dump);
-    virtual void configure(const InputReaderConfiguration* config, uint32_t changes);
-    virtual void reset();
+    virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
+    virtual void reset(nsecs_t when);
     virtual void process(const RawEvent* rawEvent);
 
 private:
diff --git a/services/input/tests/InputReader_test.cpp b/services/input/tests/InputReader_test.cpp
index ebf66aa..4796958 100644
--- a/services/input/tests/InputReader_test.cpp
+++ b/services/input/tests/InputReader_test.cpp
@@ -125,13 +125,6 @@
 // --- FakeInputReaderPolicy ---
 
 class FakeInputReaderPolicy : public InputReaderPolicyInterface {
-    struct DisplayInfo {
-        int32_t width;
-        int32_t height;
-        int32_t orientation;
-    };
-
-    KeyedVector<int32_t, DisplayInfo> mDisplayInfos;
     InputReaderConfiguration mConfig;
     KeyedVector<int32_t, sp<FakePointerController> > mPointerControllers;
 
@@ -142,18 +135,10 @@
     FakeInputReaderPolicy() {
     }
 
-    void removeDisplayInfo(int32_t displayId) {
-        mDisplayInfos.removeItem(displayId);
-    }
-
     void setDisplayInfo(int32_t displayId, int32_t width, int32_t height, int32_t orientation) {
-        removeDisplayInfo(displayId);
-
-        DisplayInfo info;
-        info.width = width;
-        info.height = height;
-        info.orientation = orientation;
-        mDisplayInfos.add(displayId, info);
+        // Set the size of both the internal and external display at the same time.
+        mConfig.setDisplayInfo(displayId, false /*external*/, width, height, orientation);
+        mConfig.setDisplayInfo(displayId, true /*external*/, width, height, orientation);
     }
 
     virtual nsecs_t getVirtualKeyQuietTime() {
@@ -168,26 +153,11 @@
         mPointerControllers.add(deviceId, controller);
     }
 
-private:
-    virtual bool getDisplayInfo(int32_t displayId, bool external /*currently ignored*/,
-            int32_t* width, int32_t* height, int32_t* orientation) {
-        ssize_t index = mDisplayInfos.indexOfKey(displayId);
-        if (index >= 0) {
-            const DisplayInfo& info = mDisplayInfos.valueAt(index);
-            if (width) {
-                *width = info.width;
-            }
-            if (height) {
-                *height = info.height;
-            }
-            if (orientation) {
-                *orientation = info.orientation;
-            }
-            return true;
-        }
-        return false;
+    const InputReaderConfiguration* getReaderConfiguration() const {
+        return &mConfig;
     }
 
+private:
     virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) {
         *outConfig = mConfig;
     }
@@ -203,6 +173,7 @@
 class FakeInputListener : public InputListenerInterface {
 private:
     List<NotifyConfigurationChangedArgs> mNotifyConfigurationChangedArgsQueue;
+    List<NotifyDeviceResetArgs> mNotifyDeviceResetArgsQueue;
     List<NotifyKeyArgs> mNotifyKeyArgsQueue;
     List<NotifyMotionArgs> mNotifyMotionArgsQueue;
     List<NotifySwitchArgs> mNotifySwitchArgsQueue;
@@ -224,6 +195,16 @@
         mNotifyConfigurationChangedArgsQueue.erase(mNotifyConfigurationChangedArgsQueue.begin());
     }
 
+    void assertNotifyDeviceResetWasCalled(
+            NotifyDeviceResetArgs* outEventArgs = NULL) {
+        ASSERT_FALSE(mNotifyDeviceResetArgsQueue.empty())
+                << "Expected notifyDeviceReset() to have been called.";
+        if (outEventArgs) {
+            *outEventArgs = *mNotifyDeviceResetArgsQueue.begin();
+        }
+        mNotifyDeviceResetArgsQueue.erase(mNotifyDeviceResetArgsQueue.begin());
+    }
+
     void assertNotifyKeyWasCalled(NotifyKeyArgs* outEventArgs = NULL) {
         ASSERT_FALSE(mNotifyKeyArgsQueue.empty())
                 << "Expected notifyKey() to have been called.";
@@ -266,6 +247,10 @@
         mNotifyConfigurationChangedArgsQueue.push_back(*args);
     }
 
+    virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args) {
+        mNotifyDeviceResetArgsQueue.push_back(*args);
+    }
+
     virtual void notifyKey(const NotifyKeyArgs* args) {
         mNotifyKeyArgsQueue.push_back(*args);
     }
@@ -792,11 +777,12 @@
         }
     }
 
-    virtual void configure(const InputReaderConfiguration* config, uint32_t changes) {
+    virtual void configure(nsecs_t when,
+            const InputReaderConfiguration* config, uint32_t changes) {
         mConfigureWasCalled = true;
     }
 
-    virtual void reset() {
+    virtual void reset(nsecs_t when) {
         mResetWasCalled = true;
     }
 
@@ -913,6 +899,7 @@
     void addDevice(int32_t deviceId, const String8& name, uint32_t classes,
             const PropertyMap* configuration) {
         mFakeEventHub->addDevice(deviceId, name, classes);
+
         if (configuration) {
             mFakeEventHub->addConfigurationMap(deviceId, configuration);
         }
@@ -1263,7 +1250,15 @@
 TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
     // Configuration.
     InputReaderConfiguration config;
-    mDevice->configure(&config, 0);
+    mDevice->configure(ARBITRARY_TIME, &config, 0);
+
+    // Reset.
+    mDevice->reset(ARBITRARY_TIME);
+
+    NotifyDeviceResetArgs resetArgs;
+    ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
+    ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
+    ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
 
     // Metadata.
     ASSERT_TRUE(mDevice->isIgnored());
@@ -1292,9 +1287,6 @@
             << "Ignored device should never mark any key codes.";
     ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
     ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
-
-    // Reset.
-    mDevice->reset();
 }
 
 TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
@@ -1318,7 +1310,7 @@
     mDevice->addMapper(mapper2);
 
     InputReaderConfiguration config;
-    mDevice->configure(&config, 0);
+    mDevice->configure(ARBITRARY_TIME, &config, 0);
 
     String8 propertyValue;
     ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
@@ -1328,6 +1320,16 @@
     ASSERT_NO_FATAL_FAILURE(mapper1->assertConfigureWasCalled());
     ASSERT_NO_FATAL_FAILURE(mapper2->assertConfigureWasCalled());
 
+    // Reset
+    mDevice->reset(ARBITRARY_TIME);
+    ASSERT_NO_FATAL_FAILURE(mapper1->assertResetWasCalled());
+    ASSERT_NO_FATAL_FAILURE(mapper2->assertResetWasCalled());
+
+    NotifyDeviceResetArgs resetArgs;
+    ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
+    ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
+    ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
+
     // Metadata.
     ASSERT_FALSE(mDevice->isIgnored());
     ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
@@ -1379,12 +1381,6 @@
 
     ASSERT_NO_FATAL_FAILURE(mapper1->assertProcessWasCalled());
     ASSERT_NO_FATAL_FAILURE(mapper2->assertProcessWasCalled());
-
-    // Reset.
-    mDevice->reset();
-
-    ASSERT_NO_FATAL_FAILURE(mapper1->assertResetWasCalled());
-    ASSERT_NO_FATAL_FAILURE(mapper2->assertResetWasCalled());
 }
 
 
@@ -1424,10 +1420,16 @@
     }
 
     void addMapperAndConfigure(InputMapper* mapper) {
-        InputReaderConfiguration config;
-
         mDevice->addMapper(mapper);
-        mDevice->configure(&config, 0);
+        mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
+        mDevice->reset(ARBITRARY_TIME);
+    }
+
+    void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
+            int32_t orientation) {
+        mFakePolicy->setDisplayInfo(displayId, width, height, orientation);
+        mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
+                InputReaderConfiguration::CHANGE_DISPLAY_INFO);
     }
 
     static void process(InputMapper* mapper, nsecs_t when, int32_t deviceId, int32_t type,
@@ -1593,71 +1595,6 @@
     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
 }
 
-TEST_F(KeyboardInputMapperTest, Reset_WhenKeysAreNotDown_DoesNotSynthesizeKeyUp) {
-    KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
-            AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
-    addMapperAndConfigure(mapper);
-
-    // Key down.
-    process(mapper, ARBITRARY_TIME, DEVICE_ID,
-            EV_KEY, KEY_HOME, AKEYCODE_HOME, 1, POLICY_FLAG_WAKE);
-    ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
-
-    // Key up.
-    process(mapper, ARBITRARY_TIME, DEVICE_ID,
-            EV_KEY, KEY_HOME, AKEYCODE_HOME, 0, POLICY_FLAG_WAKE);
-    ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
-
-    // Reset.  Since no keys still down, should not synthesize any key ups.
-    mapper->reset();
-    ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
-}
-
-TEST_F(KeyboardInputMapperTest, Reset_WhenKeysAreDown_SynthesizesKeyUps) {
-    KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
-            AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
-    addMapperAndConfigure(mapper);
-
-    // Metakey down.
-    process(mapper, ARBITRARY_TIME, DEVICE_ID,
-            EV_KEY, KEY_LEFTSHIFT, AKEYCODE_SHIFT_LEFT, 1, 0);
-    ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
-
-    // Key down.
-    process(mapper, ARBITRARY_TIME + 1, DEVICE_ID,
-            EV_KEY, KEY_A, AKEYCODE_A, 1, 0);
-    ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
-
-    // Reset.  Since two keys are still down, should synthesize two key ups in reverse order.
-    mapper->reset();
-
-    NotifyKeyArgs args;
-    ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
-    ASSERT_EQ(DEVICE_ID, args.deviceId);
-    ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
-    ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
-    ASSERT_EQ(AKEYCODE_A, args.keyCode);
-    ASSERT_EQ(KEY_A, args.scanCode);
-    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
-    ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
-    ASSERT_EQ(uint32_t(0), args.policyFlags);
-    ASSERT_EQ(ARBITRARY_TIME + 1, args.downTime);
-
-    ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
-    ASSERT_EQ(DEVICE_ID, args.deviceId);
-    ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
-    ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
-    ASSERT_EQ(AKEYCODE_SHIFT_LEFT, args.keyCode);
-    ASSERT_EQ(KEY_LEFTSHIFT, args.scanCode);
-    ASSERT_EQ(AMETA_NONE, args.metaState);
-    ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
-    ASSERT_EQ(uint32_t(0), args.policyFlags);
-    ASSERT_EQ(ARBITRARY_TIME + 1, args.downTime);
-
-    // And that's it.
-    ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
-}
-
 TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
     KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
             AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
@@ -1703,7 +1640,7 @@
             AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
     addMapperAndConfigure(mapper);
 
-    mFakePolicy->setDisplayInfo(DISPLAY_ID,
+    setDisplayInfoAndReconfigure(DISPLAY_ID,
             DISPLAY_WIDTH, DISPLAY_HEIGHT,
             DISPLAY_ORIENTATION_90);
     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
@@ -1722,7 +1659,7 @@
     addConfigurationProperty("keyboard.orientationAware", "1");
     addMapperAndConfigure(mapper);
 
-    mFakePolicy->setDisplayInfo(DISPLAY_ID,
+    setDisplayInfoAndReconfigure(DISPLAY_ID,
             DISPLAY_WIDTH, DISPLAY_HEIGHT,
             DISPLAY_ORIENTATION_0);
     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
@@ -1734,7 +1671,7 @@
     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
             KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
 
-    mFakePolicy->setDisplayInfo(DISPLAY_ID,
+    setDisplayInfoAndReconfigure(DISPLAY_ID,
             DISPLAY_WIDTH, DISPLAY_HEIGHT,
             DISPLAY_ORIENTATION_90);
     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
@@ -1746,7 +1683,7 @@
     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
             KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN));
 
-    mFakePolicy->setDisplayInfo(DISPLAY_ID,
+    setDisplayInfoAndReconfigure(DISPLAY_ID,
             DISPLAY_WIDTH, DISPLAY_HEIGHT,
             DISPLAY_ORIENTATION_180);
     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
@@ -1758,7 +1695,7 @@
     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
             KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_RIGHT));
 
-    mFakePolicy->setDisplayInfo(DISPLAY_ID,
+    setDisplayInfoAndReconfigure(DISPLAY_ID,
             DISPLAY_WIDTH, DISPLAY_HEIGHT,
             DISPLAY_ORIENTATION_270);
     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
@@ -1774,7 +1711,7 @@
     // in the key up as we did in the key down.
     NotifyKeyArgs args;
 
-    mFakePolicy->setDisplayInfo(DISPLAY_ID,
+    setDisplayInfoAndReconfigure(DISPLAY_ID,
             DISPLAY_WIDTH, DISPLAY_HEIGHT,
             DISPLAY_ORIENTATION_270);
     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, KEY_UP, AKEYCODE_DPAD_UP, 1, 0);
@@ -1783,7 +1720,7 @@
     ASSERT_EQ(KEY_UP, args.scanCode);
     ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
 
-    mFakePolicy->setDisplayInfo(DISPLAY_ID,
+    setDisplayInfoAndReconfigure(DISPLAY_ID,
             DISPLAY_WIDTH, DISPLAY_HEIGHT,
             DISPLAY_ORIENTATION_180);
     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, KEY_UP, AKEYCODE_DPAD_UP, 0, 0);
@@ -2149,58 +2086,12 @@
             0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
 }
 
-TEST_F(CursorInputMapperTest, Reset_WhenButtonIsNotDown_ShouldNotSynthesizeButtonUp) {
-    CursorInputMapper* mapper = new CursorInputMapper(mDevice);
-    addConfigurationProperty("cursor.mode", "navigation");
-    addMapperAndConfigure(mapper);
-
-    NotifyMotionArgs args;
-
-    // Button press.
-    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0, 1, 0);
-    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0, 0, 0);
-    ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
-    ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
-
-    // Button release.
-    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0, 0, 0);
-    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0, 0, 0);
-    ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
-    ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
-
-    // Reset.  Should not synthesize button up since button is not pressed.
-    mapper->reset();
-
-    ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
-}
-
-TEST_F(CursorInputMapperTest, Reset_WhenButtonIsDown_ShouldSynthesizeButtonUp) {
-    CursorInputMapper* mapper = new CursorInputMapper(mDevice);
-    addConfigurationProperty("cursor.mode", "navigation");
-    addMapperAndConfigure(mapper);
-
-    NotifyMotionArgs args;
-
-    // Button press.
-    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0, 1, 0);
-    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0, 0, 0);
-    ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
-
-    // Reset.  Should synthesize button up.
-    mapper->reset();
-
-    ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
-    ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
-    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
-            0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
-}
-
 TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
     CursorInputMapper* mapper = new CursorInputMapper(mDevice);
     addConfigurationProperty("cursor.mode", "navigation");
     addMapperAndConfigure(mapper);
 
-    mFakePolicy->setDisplayInfo(DISPLAY_ID,
+    setDisplayInfoAndReconfigure(DISPLAY_ID,
             DISPLAY_WIDTH, DISPLAY_HEIGHT,
             DISPLAY_ORIENTATION_90);
     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0,  1,  0,  1));
@@ -2219,7 +2110,7 @@
     addConfigurationProperty("cursor.orientationAware", "1");
     addMapperAndConfigure(mapper);
 
-    mFakePolicy->setDisplayInfo(DISPLAY_ID,
+    setDisplayInfoAndReconfigure(DISPLAY_ID,
             DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0);
     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0,  1,  0,  1));
     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  1,  1,  1));
@@ -2230,7 +2121,7 @@
     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  0, -1,  0));
     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  1, -1,  1));
 
-    mFakePolicy->setDisplayInfo(DISPLAY_ID,
+    setDisplayInfoAndReconfigure(DISPLAY_ID,
             DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_90);
     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0,  1,  1,  0));
     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  1,  1, -1));
@@ -2241,7 +2132,7 @@
     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  0,  0,  1));
     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  1,  1,  1));
 
-    mFakePolicy->setDisplayInfo(DISPLAY_ID,
+    setDisplayInfoAndReconfigure(DISPLAY_ID,
             DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_180);
     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0,  1,  0, -1));
     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  1, -1, -1));
@@ -2252,7 +2143,7 @@
     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  0,  1,  0));
     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  1,  1, -1));
 
-    mFakePolicy->setDisplayInfo(DISPLAY_ID,
+    setDisplayInfoAndReconfigure(DISPLAY_ID,
             DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_270);
     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0,  1, -1,  0));
     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  1, -1,  1));
@@ -2480,6 +2371,8 @@
     static const int32_t RAW_ORIENTATION_MAX;
     static const int32_t RAW_DISTANCE_MIN;
     static const int32_t RAW_DISTANCE_MAX;
+    static const int32_t RAW_TILT_MIN;
+    static const int32_t RAW_TILT_MAX;
     static const int32_t RAW_ID_MIN;
     static const int32_t RAW_ID_MAX;
     static const int32_t RAW_SLOT_MIN;
@@ -2500,8 +2393,9 @@
         MINOR = 1 << 5,
         ID = 1 << 6,
         DISTANCE = 1 << 7,
-        SLOT = 1 << 8,
-        TOOL_TYPE = 1 << 9,
+        TILT = 1 << 8,
+        SLOT = 1 << 9,
+        TOOL_TYPE = 1 << 10,
     };
 
     void prepareDisplay(int32_t orientation);
@@ -2526,6 +2420,8 @@
 const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
 const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
 const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
+const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
+const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
 const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
 const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
 const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
@@ -2543,7 +2439,7 @@
 };
 
 void TouchInputMapperTest::prepareDisplay(int32_t orientation) {
-    mFakePolicy->setDisplayInfo(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation);
+    setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation);
 }
 
 void TouchInputMapperTest::prepareVirtualKeys() {
@@ -2583,6 +2479,7 @@
     void processPressure(SingleTouchInputMapper* mapper, int32_t pressure);
     void processToolMajor(SingleTouchInputMapper* mapper, int32_t toolMajor);
     void processDistance(SingleTouchInputMapper* mapper, int32_t distance);
+    void processTilt(SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY);
     void processKey(SingleTouchInputMapper* mapper, int32_t code, int32_t value);
     void processSync(SingleTouchInputMapper* mapper);
 };
@@ -2610,6 +2507,12 @@
         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_DISTANCE,
                 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
     }
+    if (axes & TILT) {
+        mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_X,
+                RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
+        mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_Y,
+                RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
+    }
 }
 
 void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
@@ -2642,6 +2545,12 @@
     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_DISTANCE, 0, distance, 0);
 }
 
+void SingleTouchInputMapperTest::processTilt(
+        SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY) {
+    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_TILT_X, 0, tiltX, 0);
+    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_TILT_Y, 0, tiltY, 0);
+}
+
 void SingleTouchInputMapperTest::processKey(
         SingleTouchInputMapper* mapper, int32_t code, int32_t value) {
     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, code, 0, value, 0);
@@ -2658,7 +2567,7 @@
     prepareAxes(POSITION);
     addMapperAndConfigure(mapper);
 
-    ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
+    ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
 }
 
 TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
@@ -2766,71 +2675,6 @@
     ASSERT_FALSE(flags[1]);
 }
 
-TEST_F(SingleTouchInputMapperTest, Reset_WhenVirtualKeysAreDown_SendsUp) {
-    // Note: Ideally we should send cancels but the implementation is more straightforward
-    // with up and this will only happen if a device is forcibly removed.
-    SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
-    addConfigurationProperty("touch.deviceType", "touchScreen");
-    prepareDisplay(DISPLAY_ORIENTATION_0);
-    prepareButtons();
-    prepareAxes(POSITION);
-    prepareVirtualKeys();
-    addMapperAndConfigure(mapper);
-
-    mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
-
-    // Press virtual key.
-    int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
-    int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
-    processDown(mapper, x, y);
-    processSync(mapper);
-    ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
-
-    // Reset.  Since key is down, synthesize key up.
-    mapper->reset();
-
-    NotifyKeyArgs args;
-    ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
-    //ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
-    ASSERT_EQ(DEVICE_ID, args.deviceId);
-    ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
-    ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
-    ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
-    ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
-    ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
-    ASSERT_EQ(KEY_HOME, args.scanCode);
-    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
-    ASSERT_EQ(ARBITRARY_TIME, args.downTime);
-}
-
-TEST_F(SingleTouchInputMapperTest, Reset_WhenNothingIsPressed_NothingMuchHappens) {
-    SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
-    addConfigurationProperty("touch.deviceType", "touchScreen");
-    prepareDisplay(DISPLAY_ORIENTATION_0);
-    prepareButtons();
-    prepareAxes(POSITION);
-    prepareVirtualKeys();
-    addMapperAndConfigure(mapper);
-
-    // Press virtual key.
-    int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
-    int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
-    processDown(mapper, x, y);
-    processSync(mapper);
-    ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
-
-    // Release virtual key.
-    processUp(mapper);
-    processSync(mapper);
-    ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
-
-    // Reset.  Since no key is down, nothing happens.
-    mapper->reset();
-
-    ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
-    ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
-}
-
 TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
     addConfigurationProperty("touch.deviceType", "touchScreen");
@@ -3260,7 +3104,7 @@
     addConfigurationProperty("touch.deviceType", "touchScreen");
     prepareDisplay(DISPLAY_ORIENTATION_0);
     prepareButtons();
-    prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE);
+    prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
     addMapperAndConfigure(mapper);
 
     // These calculations are based on the input device calibration documentation.
@@ -3268,7 +3112,9 @@
     int32_t rawY = 200;
     int32_t rawPressure = 10;
     int32_t rawToolMajor = 12;
-    int32_t rawDistance = 0;
+    int32_t rawDistance = 2;
+    int32_t rawTiltX = 30;
+    int32_t rawTiltY = 110;
 
     float x = toDisplayX(rawX);
     float y = toDisplayY(rawY);
@@ -3277,16 +3123,25 @@
     float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
     float distance = float(rawDistance);
 
+    float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
+    float tiltScale = M_PI / 180;
+    float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
+    float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
+    float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
+    float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
+
     processDown(mapper, rawX, rawY);
     processPressure(mapper, rawPressure);
     processToolMajor(mapper, rawToolMajor);
     processDistance(mapper, rawDistance);
+    processTilt(mapper, rawTiltX, rawTiltY);
     processSync(mapper);
 
     NotifyMotionArgs args;
     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
-            x, y, pressure, size, tool, tool, tool, tool, 0, distance));
+            x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
+    ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
 }
 
 TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
@@ -3482,8 +3337,48 @@
     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
 
-    // finger
+    // brush
     processKey(mapper, BTN_TOOL_PEN, 0);
+    processKey(mapper, BTN_TOOL_BRUSH, 1);
+    processSync(mapper);
+    ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
+    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
+    ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
+
+    // pencil
+    processKey(mapper, BTN_TOOL_BRUSH, 0);
+    processKey(mapper, BTN_TOOL_PENCIL, 1);
+    processSync(mapper);
+    ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
+    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
+    ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
+
+    // airbrush
+    processKey(mapper, BTN_TOOL_PENCIL, 0);
+    processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
+    processSync(mapper);
+    ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
+    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
+    ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
+
+    // mouse
+    processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
+    processKey(mapper, BTN_TOOL_MOUSE, 1);
+    processSync(mapper);
+    ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
+    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
+    ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
+
+    // lens
+    processKey(mapper, BTN_TOOL_MOUSE, 0);
+    processKey(mapper, BTN_TOOL_LENS, 1);
+    processSync(mapper);
+    ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
+    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
+    ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
+
+    // finger
+    processKey(mapper, BTN_TOOL_LENS, 0);
     processKey(mapper, BTN_TOOL_FINGER, 1);
     processSync(mapper);
     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
@@ -3504,7 +3399,15 @@
     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
 
+    // mouse trumps eraser
+    processKey(mapper, BTN_TOOL_MOUSE, 1);
+    processSync(mapper);
+    ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
+    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
+    ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
+
     // back to default tool type
+    processKey(mapper, BTN_TOOL_MOUSE, 0);
     processKey(mapper, BTN_TOOL_RUBBER, 0);
     processKey(mapper, BTN_TOOL_PEN, 0);
     processKey(mapper, BTN_TOOL_FINGER, 0);
@@ -3587,29 +3490,29 @@
             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
 }
 
-TEST_F(SingleTouchInputMapperTest, Process_WhenAbsDistanceIsPresent_HoversIfItsValueIsGreaterThanZero) {
+TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
     addConfigurationProperty("touch.deviceType", "touchScreen");
     prepareDisplay(DISPLAY_ORIENTATION_0);
     prepareButtons();
-    prepareAxes(POSITION | DISTANCE);
+    prepareAxes(POSITION | PRESSURE);
     addMapperAndConfigure(mapper);
 
     NotifyMotionArgs motionArgs;
 
-    // initially hovering because distance is 1, pressure defaults to 0
+    // initially hovering because pressure is 0
     processDown(mapper, 100, 200);
-    processDistance(mapper, 1);
+    processPressure(mapper, 0);
     processSync(mapper);
     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
-            toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 1));
+            toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
 
     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
-            toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 1));
+            toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
 
     // move a little
     processMove(mapper, 150, 250);
@@ -3617,23 +3520,23 @@
     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
-            toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 1));
+            toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
 
-    // down when distance goes to 0, pressure defaults to 1
-    processDistance(mapper, 0);
+    // down when pressure is non-zero
+    processPressure(mapper, RAW_PRESSURE_MAX);
     processSync(mapper);
     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
-            toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 1));
+            toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
 
     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
             toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
 
-    // up when distance goes to 1, hover restored
-    processDistance(mapper, 1);
+    // up when pressure becomes 0, hover restored
+    processPressure(mapper, 0);
     processSync(mapper);
     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
@@ -3643,12 +3546,12 @@
     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
-            toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 1));
+            toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
 
     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
-            toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 1));
+            toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
 
     // exit hover when pointer goes away
     processUp(mapper);
@@ -3656,7 +3559,7 @@
     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
-            toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 1));
+            toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
 }
 
 
@@ -4823,8 +4726,48 @@
     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
 
-    // finger
+    // brush
     processKey(mapper, BTN_TOOL_PEN, 0);
+    processKey(mapper, BTN_TOOL_BRUSH, 1);
+    processSync(mapper);
+    ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
+    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
+    ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
+
+    // pencil
+    processKey(mapper, BTN_TOOL_BRUSH, 0);
+    processKey(mapper, BTN_TOOL_PENCIL, 1);
+    processSync(mapper);
+    ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
+    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
+    ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
+
+    // airbrush
+    processKey(mapper, BTN_TOOL_PENCIL, 0);
+    processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
+    processSync(mapper);
+    ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
+    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
+    ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
+
+    // mouse
+    processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
+    processKey(mapper, BTN_TOOL_MOUSE, 1);
+    processSync(mapper);
+    ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
+    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
+    ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
+
+    // lens
+    processKey(mapper, BTN_TOOL_MOUSE, 0);
+    processKey(mapper, BTN_TOOL_LENS, 1);
+    processSync(mapper);
+    ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
+    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
+    ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
+
+    // finger
+    processKey(mapper, BTN_TOOL_LENS, 0);
     processKey(mapper, BTN_TOOL_FINGER, 1);
     processSync(mapper);
     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
@@ -4845,6 +4788,13 @@
     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
 
+    // mouse trumps eraser
+    processKey(mapper, BTN_TOOL_MOUSE, 1);
+    processSync(mapper);
+    ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
+    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
+    ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
+
     // MT tool type trumps BTN tool types: MT_TOOL_FINGER
     processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
     processSync(mapper);
@@ -4861,6 +4811,7 @@
 
     // back to default tool type
     processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
+    processKey(mapper, BTN_TOOL_MOUSE, 0);
     processKey(mapper, BTN_TOOL_RUBBER, 0);
     processKey(mapper, BTN_TOOL_PEN, 0);
     processKey(mapper, BTN_TOOL_FINGER, 0);
@@ -4942,29 +4893,29 @@
             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
 }
 
-TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTDistanceIsPresent_HoversIfItsValueIsGreaterThanZero) {
+TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
     MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
     addConfigurationProperty("touch.deviceType", "touchScreen");
     prepareDisplay(DISPLAY_ORIENTATION_0);
-    prepareAxes(POSITION | ID | SLOT | DISTANCE);
+    prepareAxes(POSITION | ID | SLOT | PRESSURE);
     addMapperAndConfigure(mapper);
 
     NotifyMotionArgs motionArgs;
 
-    // initially hovering because distance is 1, pressure defaults to 0
+    // initially hovering because pressure is 0
     processId(mapper, 1);
     processPosition(mapper, 100, 200);
-    processDistance(mapper, 1);
+    processPressure(mapper, 0);
     processSync(mapper);
     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
-            toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 1));
+            toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
 
     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
-            toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 1));
+            toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
 
     // move a little
     processPosition(mapper, 150, 250);
@@ -4972,23 +4923,23 @@
     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
-            toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 1));
+            toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
 
-    // down when distance goes to 0, pressure defaults to 1
-    processDistance(mapper, 0);
+    // down when pressure becomes non-zero
+    processPressure(mapper, RAW_PRESSURE_MAX);
     processSync(mapper);
     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
-            toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 1));
+            toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
 
     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
             toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
 
-    // up when distance goes to 1, hover restored
-    processDistance(mapper, 1);
+    // up when pressure becomes 0, hover restored
+    processPressure(mapper, 0);
     processSync(mapper);
     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
@@ -4998,12 +4949,12 @@
     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
-            toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 1));
+            toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
 
     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
-            toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 1));
+            toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
 
     // exit hover when pointer goes away
     processId(mapper, -1);
@@ -5011,7 +4962,7 @@
     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
-            toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 1));
+            toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
 }
 
 
diff --git a/services/java/com/android/server/am/ActivityManagerService.java b/services/java/com/android/server/am/ActivityManagerService.java
index cba04df..800c4fc 100644
--- a/services/java/com/android/server/am/ActivityManagerService.java
+++ b/services/java/com/android/server/am/ActivityManagerService.java
@@ -572,8 +572,8 @@
      * string containing the provider's implementation class and values are a
      * ContentProviderRecord object containing the data about it.
      */
-    final HashMap<String, ContentProviderRecord> mProvidersByClass
-            = new HashMap<String, ContentProviderRecord>();
+    final HashMap<ComponentName, ContentProviderRecord> mProvidersByClass
+            = new HashMap<ComponentName, ContentProviderRecord>();
 
     /**
      * List of content providers who have clients waiting for them.  The
@@ -1074,7 +1074,7 @@
                     int uid = msg.arg1;
                     boolean restart = (msg.arg2 == 1);
                     String pkg = (String) msg.obj;
-                    forceStopPackageLocked(pkg, uid, restart, false, true);
+                    forceStopPackageLocked(pkg, uid, restart, false, true, false);
                 }
             } break;
             case FINALIZE_PENDING_INTENT_MSG: {
@@ -3086,7 +3086,7 @@
                     return;
                 }
                 killPackageProcessesLocked(packageName, pkgUid,
-                        ProcessList.SECONDARY_SERVER_ADJ, false, true, true);
+                        ProcessList.SECONDARY_SERVER_ADJ, false, true, true, false);
             }
         } finally {
             Binder.restoreCallingIdentity(callingId);
@@ -3244,7 +3244,7 @@
     }
 
     private void forceStopPackageLocked(final String packageName, int uid) {
-        forceStopPackageLocked(packageName, uid, false, false, true);
+        forceStopPackageLocked(packageName, uid, false, false, true, false);
         Intent intent = new Intent(Intent.ACTION_PACKAGE_RESTARTED,
                 Uri.fromParts("package", packageName, null));
         if (!mProcessesReady) {
@@ -3257,7 +3257,8 @@
     }
     
     private final boolean killPackageProcessesLocked(String packageName, int uid,
-            int minOomAdj, boolean callerWillRestart, boolean allowRestart, boolean doit) {
+            int minOomAdj, boolean callerWillRestart, boolean allowRestart, boolean doit,
+            boolean evenPersistent) {
         ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>();
 
         // Remove all processes this package may have touched: all with the
@@ -3268,7 +3269,7 @@
             final int NA = apps.size();
             for (int ia=0; ia<NA; ia++) {
                 ProcessRecord app = apps.valueAt(ia);
-                if (app.persistent) {
+                if (app.persistent && !evenPersistent) {
                     // we don't kill persistent processes
                     continue;
                 }
@@ -3298,7 +3299,8 @@
     }
 
     private final boolean forceStopPackageLocked(String name, int uid,
-            boolean callerWillRestart, boolean purgeCache, boolean doit) {
+            boolean callerWillRestart, boolean purgeCache, boolean doit,
+            boolean evenPersistent) {
         int i;
         int N;
 
@@ -3322,12 +3324,12 @@
         }
         
         boolean didSomething = killPackageProcessesLocked(name, uid, -100,
-                callerWillRestart, false, doit);
+                callerWillRestart, false, doit, evenPersistent);
         
         for (i=mMainStack.mHistory.size()-1; i>=0; i--) {
             ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
             if (r.packageName.equals(name)
-                    && (r.app == null || !r.app.persistent)) {
+                    && (r.app == null || evenPersistent || !r.app.persistent)) {
                 if (!doit) {
                     return true;
                 }
@@ -3344,7 +3346,7 @@
         ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>();
         for (ServiceRecord service : mServices.values()) {
             if (service.packageName.equals(name)
-                    && (service.app == null || !service.app.persistent)) {
+                    && (service.app == null || evenPersistent || !service.app.persistent)) {
                 if (!doit) {
                     return true;
                 }
@@ -3757,7 +3759,7 @@
                 if (pkgs != null) {
                     for (String pkg : pkgs) {
                         synchronized (ActivityManagerService.this) {
-                          if (forceStopPackageLocked(pkg, -1, false, false, false)) {
+                          if (forceStopPackageLocked(pkg, -1, false, false, false, false)) {
                               setResultCode(Activity.RESULT_OK);
                               return;
                           }
@@ -5438,10 +5440,11 @@
             for (int i=0; i<N; i++) {
                 ProviderInfo cpi =
                     (ProviderInfo)providers.get(i);
-                ContentProviderRecord cpr = mProvidersByClass.get(cpi.name);
+                ComponentName comp = new ComponentName(cpi.packageName, cpi.name);
+                ContentProviderRecord cpr = mProvidersByClass.get(comp);
                 if (cpr == null) {
                     cpr = new ContentProviderRecord(cpi, app.info);
-                    mProvidersByClass.put(cpi.name, cpr);
+                    mProvidersByClass.put(comp, cpr);
                 }
                 app.pubProviders.put(cpi.name, cpr);
                 app.addPackage(cpi.applicationInfo.packageName);
@@ -5606,8 +5609,9 @@
                     throw new IllegalArgumentException(
                             "Attempt to launch content provider before system ready");
                 }
-                
-                cpr = mProvidersByClass.get(cpi.name);
+
+                ComponentName comp = new ComponentName(cpi.packageName, cpi.name);
+                cpr = mProvidersByClass.get(comp);
                 final boolean firstClass = cpr == null;
                 if (firstClass) {
                     try {
@@ -5689,7 +5693,7 @@
                 // Make sure the provider is published (the same provider class
                 // may be published under multiple names).
                 if (firstClass) {
-                    mProvidersByClass.put(cpi.name, cpr);
+                    mProvidersByClass.put(comp, cpr);
                 }
                 mProvidersByName.put(name, cpr);
 
@@ -5769,7 +5773,8 @@
                         " when removing content provider " + name);
             }
             //update content provider record entry info
-            ContentProviderRecord localCpr = mProvidersByClass.get(cpr.info.name);
+            ComponentName comp = new ComponentName(cpr.info.packageName, cpr.info.name);
+            ContentProviderRecord localCpr = mProvidersByClass.get(comp);
             if (DEBUG_PROVIDER) Slog.v(TAG, "Removing provider requested by "
                     + r.info.processName + " from process "
                     + localCpr.appInfo.processName);
@@ -5801,7 +5806,8 @@
             }
 
             //update content provider record entry info
-            ContentProviderRecord localCpr = mProvidersByClass.get(cpr.info.name);
+            ComponentName comp = new ComponentName(cpr.info.packageName, cpr.info.name);
+            ContentProviderRecord localCpr = mProvidersByClass.get(comp);
             localCpr.externals--;
             if (localCpr.externals < 0) {
                 Slog.e(TAG, "Externals < 0 for content provider " + localCpr);
@@ -5835,7 +5841,8 @@
                 }
                 ContentProviderRecord dst = r.pubProviders.get(src.info.name);
                 if (dst != null) {
-                    mProvidersByClass.put(dst.info.name, dst);
+                    ComponentName comp = new ComponentName(dst.info.packageName, dst.info.name);
+                    mProvidersByClass.put(comp, dst);
                     String names[] = dst.info.authority.split(";");
                     for (int j = 0; j < names.length; j++) {
                         mProvidersByName.put(names[j], dst);
@@ -6178,7 +6185,7 @@
             mDebugTransient = !persistent;
             if (packageName != null) {
                 final long origId = Binder.clearCallingIdentity();
-                forceStopPackageLocked(packageName, -1, false, false, true);
+                forceStopPackageLocked(packageName, -1, false, false, true, true);
                 Binder.restoreCallingIdentity(origId);
             }
         }
@@ -7607,15 +7614,15 @@
                 pw.println("    i[ntents]: pending intent state");
                 pw.println("    p[rocesses]: process state");
                 pw.println("    o[om]: out of memory management");
-                pw.println("    prov[iders]: content provider state");
-                pw.println("    s[ervices]: service state");
+                pw.println("    prov[iders] [COMP_SPEC ...]: content provider state");
+                pw.println("    s[ervices] [COMP_SPEC ...]: service state");
                 pw.println("    service [COMP_SPEC]: service client-side state");
+                pw.println("    all: dump all activities");
+                pw.println("    top: dump the top activity");
                 pw.println("  cmd may also be a COMP_SPEC to dump activities.");
-                pw.println("  COMP_SPEC may also be a component name (com.foo/.myApp),");
-                pw.println("    a partial substring in a component name, an");
-                pw.println("    ActivityRecord hex object identifier, or");
-                pw.println("    \"all\" for all objects, or");
-                pw.println("    \"top\" for the top activity.");
+                pw.println("  COMP_SPEC may be a component name (com.foo/.myApp),");
+                pw.println("    a partial substring in a component name, a");
+                pw.println("    hex object identifier.");
                 pw.println("  -a: include all available server state.");
                 pw.println("  -c: include client state.");
                 return;
@@ -8166,9 +8173,89 @@
         }
     }
 
+    static class ItemMatcher {
+        ArrayList<ComponentName> components;
+        ArrayList<String> strings;
+        ArrayList<Integer> objects;
+        boolean all;
+        
+        ItemMatcher() {
+            all = true;
+        }
+
+        void build(String name) {
+            ComponentName componentName = ComponentName.unflattenFromString(name);
+            if (componentName != null) {
+                if (components == null) {
+                    components = new ArrayList<ComponentName>();
+                }
+                components.add(componentName);
+                all = false;
+            } else {
+                int objectId = 0;
+                // Not a '/' separated full component name; maybe an object ID?
+                try {
+                    objectId = Integer.parseInt(name, 16);
+                    if (objects == null) {
+                        objects = new ArrayList<Integer>();
+                    }
+                    objects.add(objectId);
+                    all = false;
+                } catch (RuntimeException e) {
+                    // Not an integer; just do string match.
+                    if (strings == null) {
+                        strings = new ArrayList<String>();
+                    }
+                    strings.add(name);
+                    all = false;
+                }
+            }
+        }
+
+        int build(String[] args, int opti) {
+            for (; opti<args.length; opti++) {
+                String name = args[opti];
+                if ("--".equals(name)) {
+                    return opti+1;
+                }
+                build(name);
+            }
+            return opti;
+        }
+
+        boolean match(Object object, ComponentName comp) {
+            if (all) {
+                return true;
+            }
+            if (components != null) {
+                for (int i=0; i<components.size(); i++) {
+                    if (components.get(i).equals(comp)) {
+                        return true;
+                    }
+                }
+            }
+            if (objects != null) {
+                for (int i=0; i<objects.size(); i++) {
+                    if (System.identityHashCode(object) == objects.get(i)) {
+                        return true;
+                    }
+                }
+            }
+            if (strings != null) {
+                String flat = comp.flattenToString();
+                for (int i=0; i<strings.size(); i++) {
+                    if (flat.contains(strings.get(i))) {
+                        return true;
+                    }
+                }
+            }
+            return false;
+        }
+    }
+
     /**
      * There are three things that cmd can be:
-     *  - a flattened component name that matched an existing activity
+     *  - a flattened component name that matches an existing activity
      *  - the cmd arg isn't the flattened component name of an existing activity:
      *    dump all activity whose component contains the cmd as a substring
      *  - A hex number of the ActivityRecord object instance.
@@ -8191,29 +8278,12 @@
                 }
             }
         } else {
-            ComponentName componentName = ComponentName.unflattenFromString(name);
-            int objectId = 0;
-            if (componentName == null) {
-                // Not a '/' separated full component name; maybe an object ID?
-                try {
-                    objectId = Integer.parseInt(name, 16);
-                    name = null;
-                    componentName = null;
-                } catch (RuntimeException e) {
-                }
-            }
+            ItemMatcher matcher = new ItemMatcher();
+            matcher.build(name);
 
             synchronized (this) {
                 for (ActivityRecord r1 : (ArrayList<ActivityRecord>)mMainStack.mHistory) {
-                    if (componentName != null) {
-                        if (r1.intent.getComponent().equals(componentName)) {
-                            activities.add(r1);
-                        }
-                    } else if (name != null) {
-                        if (r1.intent.getComponent().flattenToString().contains(name)) {
-                            activities.add(r1);
-                        }
-                    } else if (System.identityHashCode(r1) == objectId) {
+                    if (matcher.match(r1, r1.intent.getComponent())) {
                         activities.add(r1);
                     }
                 }
@@ -8404,6 +8474,9 @@
             int opti, boolean dumpAll, boolean dumpClient) {
         boolean needSep = false;
 
+        ItemMatcher matcher = new ItemMatcher();
+        matcher.build(args, opti);
+
         pw.println("ACTIVITY MANAGER SERVICES (dumpsys activity services)");
         if (mServices.size() > 0) {
             pw.println("  Active services:");
@@ -8412,6 +8485,9 @@
             needSep = false;
             while (it.hasNext()) {
                 ServiceRecord r = it.next();
+                if (!matcher.match(r, r.name)) {
+                    continue;
+                }
                 if (needSep) {
                     pw.println();
                 }
@@ -8457,6 +8533,9 @@
             pw.println("  Pending services:");
             for (int i=0; i<mPendingServices.size(); i++) {
                 ServiceRecord r = mPendingServices.get(i);
+                if (!matcher.match(r, r.name)) {
+                    continue;
+                }
                 pw.print("  * Pending "); pw.println(r);
                 r.dump(pw, "    ");
             }
@@ -8468,6 +8547,9 @@
             pw.println("  Restarting services:");
             for (int i=0; i<mRestartingServices.size(); i++) {
                 ServiceRecord r = mRestartingServices.get(i);
+                if (!matcher.match(r, r.name)) {
+                    continue;
+                }
                 pw.print("  * Restarting "); pw.println(r);
                 r.dump(pw, "    ");
             }
@@ -8479,6 +8561,9 @@
             pw.println("  Stopping services:");
             for (int i=0; i<mStoppingServices.size(); i++) {
                 ServiceRecord r = mStoppingServices.get(i);
+                if (!matcher.match(r, r.name)) {
+                    continue;
+                }
                 pw.print("  * Stopping "); pw.println(r);
                 r.dump(pw, "    ");
             }
@@ -8494,8 +8579,12 @@
                 while (it.hasNext()) {
                     ArrayList<ConnectionRecord> r = it.next();
                     for (int i=0; i<r.size(); i++) {
-                        pw.print("  * "); pw.println(r.get(i));
-                        r.get(i).dump(pw, "    ");
+                        ConnectionRecord cr = r.get(i);
+                        if (!matcher.match(cr.binding.service, cr.binding.service.name)) {
+                            continue;
+                        }
+                        pw.print("  * "); pw.println(cr);
+                        cr.dump(pw, "    ");
                     }
                 }
                 needSep = true;
@@ -8509,20 +8598,34 @@
             int opti, boolean dumpAll) {
         boolean needSep = false;
 
+        ItemMatcher matcher = new ItemMatcher();
+        matcher.build(args, opti);
+
         pw.println("ACTIVITY MANAGER CONTENT PROVIDERS (dumpsys activity providers)");
         if (mProvidersByClass.size() > 0) {
             if (needSep) pw.println(" ");
             pw.println("  Published content providers (by class):");
-            Iterator<Map.Entry<String, ContentProviderRecord>> it
+            Iterator<Map.Entry<ComponentName, ContentProviderRecord>> it
                     = mProvidersByClass.entrySet().iterator();
             while (it.hasNext()) {
-                Map.Entry<String, ContentProviderRecord> e = it.next();
+                Map.Entry<ComponentName, ContentProviderRecord> e = it.next();
                 ContentProviderRecord r = e.getValue();
+                ComponentName comp = e.getKey();
+                String cls = comp.getClassName();
+                int end = cls.lastIndexOf('.');
+                if (end > 0 && end < (cls.length()-2)) {
+                    cls = cls.substring(end+1);
+                }
+                if (!matcher.match(r, comp)) {
+                    continue;
+                }
+                pw.print("  * "); pw.print(cls); pw.print(" (");
+                        pw.print(comp.flattenToShortString()); pw.print(")");
                 if (dumpAll) {
-                    pw.print("  * "); pw.println(r);
-                    r.dump(pw, "    ");
+                    pw.println();
+                    r.dump(pw, "      ");
                 } else {
-                    pw.print("  * "); pw.print(r.name.toShortString());
+                    pw.print("  * "); pw.print(e.getKey().flattenToShortString());
                     if (r.app != null) {
                         pw.println(":");
                         pw.print("      "); pw.println(r.app);
@@ -8543,6 +8646,9 @@
                 while (it.hasNext()) {
                     Map.Entry<String, ContentProviderRecord> e = it.next();
                     ContentProviderRecord r = e.getValue();
+                    if (!matcher.match(r, r.name)) {
+                        continue;
+                    }
                     pw.print("  "); pw.print(e.getKey()); pw.print(": ");
                             pw.println(r);
                 }
@@ -11256,7 +11362,7 @@
                         String list[] = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
                         if (list != null && (list.length > 0)) {
                             for (String pkg : list) {
-                                forceStopPackageLocked(pkg, -1, false, true, true);
+                                forceStopPackageLocked(pkg, -1, false, true, true, false);
                             }
                             sendPackageBroadcastLocked(
                                     IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE, list);
@@ -11267,7 +11373,7 @@
                         if (data != null && (ssp=data.getSchemeSpecificPart()) != null) {
                             if (!intent.getBooleanExtra(Intent.EXTRA_DONT_KILL_APP, false)) {
                                 forceStopPackageLocked(ssp,
-                                        intent.getIntExtra(Intent.EXTRA_UID, -1), false, true, true);
+                                        intent.getIntExtra(Intent.EXTRA_UID, -1), false, true, true, false);
                             }
                             if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) {
                                 sendPackageBroadcastLocked(IApplicationThread.PACKAGE_REMOVED,
@@ -12018,6 +12124,7 @@
             while (mParallelBroadcasts.size() > 0) {
                 r = mParallelBroadcasts.remove(0);
                 r.dispatchTime = SystemClock.uptimeMillis();
+                r.dispatchClockTime = System.currentTimeMillis();
                 final int N = r.receivers.size();
                 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing parallel broadcast "
                         + r);
@@ -12156,7 +12263,7 @@
             r.receiverTime = SystemClock.uptimeMillis();
             if (recIdx == 0) {
                 r.dispatchTime = r.receiverTime;
-
+                r.dispatchClockTime = System.currentTimeMillis();
                 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing ordered broadcast "
                         + r);
             }
@@ -12217,7 +12324,7 @@
                 }
                 skip = true;
             }
-            if (r.callingUid != Process.SYSTEM_UID &&
+            if (info.activityInfo.applicationInfo.uid != Process.SYSTEM_UID &&
                 r.requiredPermission != null) {
                 try {
                     perm = AppGlobals.getPackageManager().
@@ -12364,7 +12471,8 @@
             }
 
             final long origId = Binder.clearCallingIdentity();
-            forceStopPackageLocked(ii.targetPackage, -1, true, false, true);
+            // Instrumentation can kill and relaunch even persistent processes
+            forceStopPackageLocked(ii.targetPackage, -1, true, false, true, true);
             ProcessRecord app = addAppLocked(ai);
             app.instrumentationClass = className;
             app.instrumentationInfo = ai;
@@ -12419,7 +12527,7 @@
         app.instrumentationProfileFile = null;
         app.instrumentationArguments = null;
 
-        forceStopPackageLocked(app.processName, -1, false, false, true);
+        forceStopPackageLocked(app.processName, -1, false, false, true, true);
     }
 
     public void finishInstrumentation(IApplicationThread target,
diff --git a/services/java/com/android/server/am/BroadcastRecord.java b/services/java/com/android/server/am/BroadcastRecord.java
index c95053e..bcb0134 100644
--- a/services/java/com/android/server/am/BroadcastRecord.java
+++ b/services/java/com/android/server/am/BroadcastRecord.java
@@ -29,6 +29,7 @@
 import android.util.TimeUtils;
 
 import java.io.PrintWriter;
+import java.util.Date;
 import java.util.List;
 
 /**
@@ -47,6 +48,7 @@
     final List receivers;   // contains BroadcastFilter and ResolveInfo
     IIntentReceiver resultTo; // who receives final result if non-null
     long dispatchTime;      // when dispatch started on this set of receivers
+    long dispatchClockTime; // the clock time the dispatch started
     long receiverTime;      // when current receiver started for timeouts.
     long finishTime;        // when we finished the broadcast.
     int resultCode;         // current result code value.
@@ -91,6 +93,8 @@
         if (requiredPermission != null) {
             pw.print(prefix); pw.print("requiredPermission="); pw.println(requiredPermission);
         }
+        pw.print(prefix); pw.print("dispatchClockTime=");
+                pw.println(new Date(dispatchClockTime));
         pw.print(prefix); pw.print("dispatchTime=");
                 TimeUtils.formatDuration(dispatchTime, now, pw);
         if (finishTime != 0) {
diff --git a/services/java/com/android/server/pm/PackageManagerService.java b/services/java/com/android/server/pm/PackageManagerService.java
index abb62de..dbb8164 100644
--- a/services/java/com/android/server/pm/PackageManagerService.java
+++ b/services/java/com/android/server/pm/PackageManagerService.java
@@ -4340,6 +4340,7 @@
             res.labelRes = info.labelRes;
             res.nonLocalizedLabel = info.nonLocalizedLabel;
             res.icon = info.icon;
+            res.system = isSystemApp(res.activityInfo.applicationInfo);
             return res;
         }
 
@@ -4512,6 +4513,7 @@
             res.labelRes = info.labelRes;
             res.nonLocalizedLabel = info.nonLocalizedLabel;
             res.icon = info.icon;
+            res.system = isSystemApp(res.serviceInfo.applicationInfo);
             return res;
         }
 
@@ -4569,7 +4571,13 @@
             v1 = r1.match;
             v2 = r2.match;
             //System.out.println("Comparing: m1=" + m1 + " m2=" + m2);
-            return (v1 > v2) ? -1 : ((v1 < v2) ? 1 : 0);
+            if (v1 != v2) {
+                return (v1 > v2) ? -1 : 1;
+            }
+            if (r1.system != r2.system) {
+                return r1.system ? -1 : 1;
+            }
+            return 0;
         }
     };
 
diff --git a/services/jni/com_android_server_InputManager.cpp b/services/jni/com_android_server_InputManager.cpp
index 7c84e43..f2a0a71 100644
--- a/services/jni/com_android_server_InputManager.cpp
+++ b/services/jni/com_android_server_InputManager.cpp
@@ -182,8 +182,6 @@
 
     /* --- InputReaderPolicyInterface implementation --- */
 
-    virtual bool getDisplayInfo(int32_t displayId, bool external,
-            int32_t* width, int32_t* height, int32_t* orientation);
     virtual void getReaderConfiguration(InputReaderConfiguration* outConfig);
     virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId);
 
@@ -273,7 +271,7 @@
         mLocked.displayHeight = -1;
         mLocked.displayExternalWidth = -1;
         mLocked.displayExternalHeight = -1;
-        mLocked.displayOrientation = ROTATION_0;
+        mLocked.displayOrientation = DISPLAY_ORIENTATION_0;
 
         mLocked.systemUiVisibility = ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE;
         mLocked.pointerSpeed = 0;
@@ -311,31 +309,42 @@
 
 void NativeInputManager::setDisplaySize(int32_t displayId, int32_t width, int32_t height,
         int32_t externalWidth, int32_t externalHeight) {
+    bool changed = false;
     if (displayId == 0) {
-        { // acquire lock
-            AutoMutex _l(mLock);
+        AutoMutex _l(mLock);
 
-            if (mLocked.displayWidth != width || mLocked.displayHeight != height) {
-                mLocked.displayWidth = width;
-                mLocked.displayHeight = height;
+        if (mLocked.displayWidth != width || mLocked.displayHeight != height) {
+            changed = true;
+            mLocked.displayWidth = width;
+            mLocked.displayHeight = height;
 
-                sp<PointerController> controller = mLocked.pointerController.promote();
-                if (controller != NULL) {
-                    controller->setDisplaySize(width, height);
-                }
+            sp<PointerController> controller = mLocked.pointerController.promote();
+            if (controller != NULL) {
+                controller->setDisplaySize(width, height);
             }
+        }
 
+        if (mLocked.displayExternalWidth != externalWidth
+                || mLocked.displayExternalHeight != externalHeight) {
+            changed = true;
             mLocked.displayExternalWidth = externalWidth;
             mLocked.displayExternalHeight = externalHeight;
-        } // release lock
+        }
+    }
+
+    if (changed) {
+        mInputManager->getReader()->requestRefreshConfiguration(
+                InputReaderConfiguration::CHANGE_DISPLAY_INFO);
     }
 }
 
 void NativeInputManager::setDisplayOrientation(int32_t displayId, int32_t orientation) {
+    bool changed = false;
     if (displayId == 0) {
         AutoMutex _l(mLock);
 
         if (mLocked.displayOrientation != orientation) {
+            changed = true;
             mLocked.displayOrientation = orientation;
 
             sp<PointerController> controller = mLocked.pointerController.promote();
@@ -344,6 +353,11 @@
             }
         }
     }
+
+    if (changed) {
+        mInputManager->getReader()->requestRefreshConfiguration(
+                InputReaderConfiguration::CHANGE_DISPLAY_INFO);
+    }
 }
 
 status_t NativeInputManager::registerInputChannel(JNIEnv* env,
@@ -358,28 +372,6 @@
     return mInputManager->getDispatcher()->unregisterInputChannel(inputChannel);
 }
 
-bool NativeInputManager::getDisplayInfo(int32_t displayId, bool external,
-        int32_t* width, int32_t* height, int32_t* orientation) {
-    bool result = false;
-    if (displayId == 0) {
-        AutoMutex _l(mLock);
-
-        if (mLocked.displayWidth > 0 && mLocked.displayHeight > 0) {
-            if (width) {
-                *width = external ? mLocked.displayExternalWidth : mLocked.displayWidth;
-            }
-            if (height) {
-                *height = external ? mLocked.displayExternalHeight : mLocked.displayHeight;
-            }
-            if (orientation) {
-                *orientation = mLocked.displayOrientation;
-            }
-            result = true;
-        }
-    }
-    return result;
-}
-
 void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {
     JNIEnv* env = jniEnv();
 
@@ -438,6 +430,12 @@
         outConfig->pointerVelocityControlParameters.scale = exp2f(mLocked.pointerSpeed
                 * POINTER_SPEED_EXPONENT);
         outConfig->pointerGesturesEnabled = mLocked.pointerGesturesEnabled;
+
+        outConfig->setDisplayInfo(0, false /*external*/,
+                mLocked.displayWidth, mLocked.displayHeight, mLocked.displayOrientation);
+        outConfig->setDisplayInfo(0, true /*external*/,
+                mLocked.displayExternalWidth, mLocked.displayExternalHeight,
+                mLocked.displayOrientation);
     } // release lock
 }
 
diff --git a/services/sensorservice/Fusion.cpp b/services/sensorservice/Fusion.cpp
index d706af5..ff4786b 100644
--- a/services/sensorservice/Fusion.cpp
+++ b/services/sensorservice/Fusion.cpp
@@ -48,6 +48,7 @@
 static const float magSTDEV  = 0.5f;    // uT    (measured 0.7  / CDD 0.5)
 
 static const float FREE_FALL_THRESHOLD = 0.981f;
+static const float SYMMETRY_TOLERANCE = 1e-10f;
 
 // -----------------------------------------------------------------------
 
@@ -134,10 +135,13 @@
 
 void Fusion::init() {
     mInitState = 0;
+
     mGyroRate = 0;
+
     mCount[0] = 0;
     mCount[1] = 0;
     mCount[2] = 0;
+
     mData = 0;
 }
 
@@ -267,19 +271,16 @@
     return NO_ERROR;
 }
 
-bool Fusion::checkState(const vec3_t& v) {
-    if (isnanf(length(v))) {
-        LOGW("9-axis fusion diverged. reseting state.");
+void Fusion::checkState() {
+    // P needs to stay positive semidefinite or the fusion diverges. When we
+    // detect divergence, we reset the fusion.
+    // TODO(braun): Instead, find the reason for the divergence and fix it.
+
+    if (!isPositiveSemidefinite(P[0][0], SYMMETRY_TOLERANCE) ||
+        !isPositiveSemidefinite(P[1][1], SYMMETRY_TOLERANCE)) {
+        LOGW("Sensor fusion diverged; resetting state.");
         P = 0;
-        x1 = 0;
-        mInitState = 0;
-        mCount[0] = 0;
-        mCount[1] = 0;
-        mCount[2] = 0;
-        mData = 0;
-        return false;
     }
-    return true;
 }
 
 vec4_t Fusion::getAttitude() const {
@@ -327,6 +328,8 @@
     Phi[1][0] = wx*k0 - I33dT - wx2*(ilwe*ilwe*ilwe)*(lwedT-k1);
 
     P = Phi*P*transpose(Phi) + GQGt;
+
+    checkState();
 }
 
 void Fusion::update(const vec3_t& z, const vec3_t& Bi, float sigma) {
@@ -365,6 +368,8 @@
     q += getF(q)*(0.5f*dq);
     x0 = normalize_quat(q);
     x1 += db;
+
+    checkState();
 }
 
 // -----------------------------------------------------------------------
diff --git a/services/sensorservice/Fusion.h b/services/sensorservice/Fusion.h
index 556944b..7062999 100644
--- a/services/sensorservice/Fusion.h
+++ b/services/sensorservice/Fusion.h
@@ -37,7 +37,7 @@
     vec3_t  x1;
 
     /*
-     * the predicated covariance matrix is made of 4 3x3 sub-matrices and it
+     * the predicated covariance matrix is made of 4 3x3 sub-matrices and it is
      * semi-definite positive.
      *
      * P = | P00  P10 | = | P00  P10 |
@@ -74,7 +74,7 @@
     enum { ACC=0x1, MAG=0x2, GYRO=0x4 };
     bool checkInitComplete(int, const vec3_t& w, float d = 0);
     void initFusion(const vec4_t& q0, float dT);
-    bool checkState(const vec3_t& v);
+    void checkState();
     void predict(const vec3_t& w, float dT);
     void update(const vec3_t& z, const vec3_t& Bi, float sigma);
     static mat34_t getF(const vec4_t& p);
diff --git a/services/sensorservice/mat.h b/services/sensorservice/mat.h
index 1302ca3..a76fc91 100644
--- a/services/sensorservice/mat.h
+++ b/services/sensorservice/mat.h
@@ -295,6 +295,29 @@
     return r;
 }
 
+// Calculate the trace of a matrix
+template <typename TYPE, size_t C> static TYPE trace(const mat<TYPE, C, C>& m) {
+    TYPE t;
+    for (size_t i=0 ; i<C ; i++)
+        t += m[i][i];
+    return t;
+}
+
+// Test positive-semidefiniteness of a matrix
+template <typename TYPE, size_t C>
+static bool isPositiveSemidefinite(const mat<TYPE, C, C>& m, TYPE tolerance) {
+    for (size_t i=0 ; i<C ; i++)
+        if (m[i][i] < 0)
+            return false;
+
+    for (size_t i=0 ; i<C ; i++)
+      for (size_t j=i+1 ; j<C ; j++)
+          if (fabs(m[i][j] - m[j][i]) > tolerance)
+              return false;
+
+    return true;
+}
+
 // Transpose a vector
 template <
     template<typename T, size_t S> class VEC,
diff --git a/services/surfaceflinger/Layer.cpp b/services/surfaceflinger/Layer.cpp
index 64eaecc..19c7ddd 100644
--- a/services/surfaceflinger/Layer.cpp
+++ b/services/surfaceflinger/Layer.cpp
@@ -196,13 +196,12 @@
      * 1) buffer orientation/flip/mirror
      * 2) state transformation (window manager)
      * 3) layer orientation (screen orientation)
-     * mOrientation is already the composition of (2) and (3)
+     * mTransform is already the composition of (2) and (3)
      * (NOTE: the matrices are multiplied in reverse order)
      */
 
     const Transform bufferOrientation(mCurrentTransform);
-    const Transform layerOrientation(mOrientation);
-    const Transform tr(layerOrientation * bufferOrientation);
+    const Transform tr(mTransform * bufferOrientation);
 
     // this gives us only the "orientation" component of the transform
     const uint32_t finalTransform = tr.getOrientation();
diff --git a/services/surfaceflinger/LayerBase.cpp b/services/surfaceflinger/LayerBase.cpp
index 7bf73d9..4cc245a 100644
--- a/services/surfaceflinger/LayerBase.cpp
+++ b/services/surfaceflinger/LayerBase.cpp
@@ -257,6 +257,7 @@
 
     // cache a few things...
     mOrientation = tr.getOrientation();
+    mTransform = tr;
     mTransformedBounds = tr.makeBounds(w, h);
     mLeft = tr.tx();
     mTop  = tr.ty();
diff --git a/services/surfaceflinger/LayerBase.h b/services/surfaceflinger/LayerBase.h
index a3d3644..2cd3a94 100644
--- a/services/surfaceflinger/LayerBase.h
+++ b/services/surfaceflinger/LayerBase.h
@@ -247,6 +247,7 @@
 protected:
                 // cached during validateVisibility()
                 int32_t         mOrientation;
+                Transform       mTransform;
                 GLfloat         mVertices[4][2];
                 Rect            mTransformedBounds;
                 int             mLeft;
diff --git a/telephony/java/com/android/internal/telephony/IccCard.java b/telephony/java/com/android/internal/telephony/IccCard.java
index 02617c8..bce9991 100644
--- a/telephony/java/com/android/internal/telephony/IccCard.java
+++ b/telephony/java/com/android/internal/telephony/IccCard.java
@@ -268,7 +268,6 @@
     }
 
     public void supplyNetworkDepersonalization (String pin, Message onComplete) {
-        if(mDbg) log("Network Despersonalization: " + pin);
         mPhone.mCM.supplyNetworkDepersonalization(pin,
                 mHandler.obtainMessage(EVENT_PINPUK_DONE, onComplete));
     }
@@ -359,7 +358,6 @@
       */
      public void changeIccLockPassword(String oldPassword, String newPassword,
              Message onComplete) {
-         if(mDbg) log("Change Pin1 old: " + oldPassword + " new: " + newPassword);
          mPhone.mCM.changeIccPin(oldPassword, newPassword,
                  mHandler.obtainMessage(EVENT_CHANGE_ICC_PASSWORD_DONE, onComplete));
 
@@ -378,7 +376,6 @@
       */
      public void changeIccFdnPassword(String oldPassword, String newPassword,
              Message onComplete) {
-         if(mDbg) log("Change Pin2 old: " + oldPassword + " new: " + newPassword);
          mPhone.mCM.changeIccPin2(oldPassword, newPassword,
                  mHandler.obtainMessage(EVENT_CHANGE_ICC_PASSWORD_DONE, onComplete));
 
diff --git a/tests/DumpRenderTree/src/com/android/dumprendertree/FsUtils.java b/tests/DumpRenderTree/src/com/android/dumprendertree/FsUtils.java
index b7d2c26..5e2a9fd 100644
--- a/tests/DumpRenderTree/src/com/android/dumprendertree/FsUtils.java
+++ b/tests/DumpRenderTree/src/com/android/dumprendertree/FsUtils.java
@@ -141,7 +141,7 @@
             } else if (!path.startsWith(HTTP_LOCAL_TESTS_PREFIX)
                     && !path.startsWith(HTTP_MEDIA_TESTS_PREFIX)
                     && !path.startsWith(HTTP_WML_TESTS_PREFIX)) {
-                url = "http://127.0.0.1:8000/" + path.substring(HTTP_TESTS_PREFIX.length());
+                url = "http://127.0.0.1:18000/" + path.substring(HTTP_TESTS_PREFIX.length());
             } else {
                 url = "file://" + path;
             }
diff --git a/tests/DumpRenderTree/src/com/android/dumprendertree/forwarder/ForwardService.java b/tests/DumpRenderTree/src/com/android/dumprendertree/forwarder/ForwardService.java
index 25dd04fd..7a277d7 100644
--- a/tests/DumpRenderTree/src/com/android/dumprendertree/forwarder/ForwardService.java
+++ b/tests/DumpRenderTree/src/com/android/dumprendertree/forwarder/ForwardService.java
@@ -26,7 +26,7 @@
 
 public class ForwardService {
 
-    private ForwardServer fs8000, fs8080, fs8443;
+    private ForwardServer fs18000, fs8080, fs8443;
 
     private static ForwardService inst;
 
@@ -40,7 +40,7 @@
     private ForwardService() {
         int addr = getForwardHostAddr();
         if (addr != -1) {
-            fs8000 = new ForwardServer(8000, addr, 8000);
+            fs18000 = new ForwardServer(18000, addr, 8000);
             fs8080 = new ForwardServer(8080, addr, 8080);
             fs8443 = new ForwardServer(8443, addr, 8443);
         }
@@ -55,8 +55,8 @@
 
     public void startForwardService() {
         try {
-            if (fs8000 != null)
-                fs8000.start();
+            if (fs18000 != null)
+                fs18000.start();
             if (fs8080 != null)
                 fs8080.start();
             if (fs8443 != null)
@@ -68,9 +68,9 @@
     }
 
     public void stopForwardService() {
-        if (fs8000 != null) {
-            fs8000.stop();
-            fs8000 = null;
+        if (fs18000 != null) {
+            fs18000.stop();
+            fs18000 = null;
         }
         if (fs8080 != null) {
             fs8080.stop();
diff --git a/tests/RenderScriptTests/tests/src/com/android/rs/test/RSTestCore.java b/tests/RenderScriptTests/tests/src/com/android/rs/test/RSTestCore.java
index be012ee..e37e9b5 100644
--- a/tests/RenderScriptTests/tests/src/com/android/rs/test/RSTestCore.java
+++ b/tests/RenderScriptTests/tests/src/com/android/rs/test/RSTestCore.java
@@ -70,6 +70,7 @@
         unitTests.add(new UT_rstime(this, mRes, mCtx));
         unitTests.add(new UT_rstypes(this, mRes, mCtx));
         unitTests.add(new UT_alloc(this, mRes, mCtx));
+        unitTests.add(new UT_foreach(this, mRes, mCtx));
         unitTests.add(new UT_math(this, mRes, mCtx));
         unitTests.add(new UT_fp_mad(this, mRes, mCtx));
         /*
diff --git a/tests/RenderScriptTests/tests/src/com/android/rs/test/UT_foreach.java b/tests/RenderScriptTests/tests/src/com/android/rs/test/UT_foreach.java
new file mode 100644
index 0000000..1d2555e
--- /dev/null
+++ b/tests/RenderScriptTests/tests/src/com/android/rs/test/UT_foreach.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT 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.rs.test;
+
+import android.content.Context;
+import android.content.res.Resources;
+import android.renderscript.*;
+
+public class UT_foreach extends UnitTest {
+    private Resources mRes;
+    private Allocation A;
+
+    protected UT_foreach(RSTestCore rstc, Resources res, Context ctx) {
+        super(rstc, "ForEach", ctx);
+        mRes = res;
+    }
+
+    private void initializeGlobals(RenderScript RS, ScriptC_foreach s) {
+        Type.Builder typeBuilder = new Type.Builder(RS, Element.I32(RS));
+        int X = 5;
+        int Y = 7;
+        s.set_dimX(X);
+        s.set_dimY(Y);
+        typeBuilder.setX(X).setY(Y);
+        A = Allocation.createTyped(RS, typeBuilder.create());
+        s.bind_a(A);
+
+        return;
+    }
+
+    public void run() {
+        RenderScript pRS = RenderScript.create(mCtx);
+        ScriptC_foreach s = new ScriptC_foreach(pRS, mRes, R.raw.foreach);
+        pRS.setMessageHandler(mRsMessage);
+        initializeGlobals(pRS, s);
+        s.forEach_root(A);
+        s.invoke_foreach_test();
+        pRS.finish();
+        waitForMessage();
+        pRS.destroy();
+    }
+}
diff --git a/tests/RenderScriptTests/tests/src/com/android/rs/test/foreach.rs b/tests/RenderScriptTests/tests/src/com/android/rs/test/foreach.rs
new file mode 100644
index 0000000..3ba3eef
--- /dev/null
+++ b/tests/RenderScriptTests/tests/src/com/android/rs/test/foreach.rs
@@ -0,0 +1,42 @@
+#include "shared.rsh"
+
+int *a;
+int dimX;
+int dimY;
+
+void root(int *out, uint32_t x, uint32_t y) {
+    *out = x + y * dimX;
+}
+
+static bool test_foreach_output() {
+    bool failed = false;
+    int i, j;
+
+    for (j = 0; j < dimY; j++) {
+        for (i = 0; i < dimX; i++) {
+            _RS_ASSERT(a[i + j * dimX] == (i + j * dimX));
+        }
+    }
+
+    if (failed) {
+        rsDebug("test_foreach_output FAILED", 0);
+    }
+    else {
+        rsDebug("test_foreach_output PASSED", 0);
+    }
+
+    return failed;
+}
+
+void foreach_test() {
+    bool failed = false;
+    failed |= test_foreach_output();
+
+    if (failed) {
+        rsSendToClientBlocking(RS_MSG_TEST_FAILED);
+    }
+    else {
+        rsSendToClientBlocking(RS_MSG_TEST_PASSED);
+    }
+}
+
diff --git a/wifi/java/android/net/wifi/WifiNative.java b/wifi/java/android/net/wifi/WifiNative.java
index bfc4d5e..6c6f149 100644
--- a/wifi/java/android/net/wifi/WifiNative.java
+++ b/wifi/java/android/net/wifi/WifiNative.java
@@ -304,4 +304,28 @@
         return doBooleanCommand("P2P_INVITE group=" + group.getInterface()
                 + " peer=" + deviceAddress + " go_dev_addr=" + group.getOwner().deviceAddress);
     }
+
+    public static String p2pGetInterfaceAddress(String deviceAddress) {
+        if (deviceAddress == null) return null;
+
+        //  "p2p_peer deviceAddress" returns a multi-line result containing
+        //      intended_addr=fa:7b:7a:42:82:13
+        String peerInfo = p2pPeer(deviceAddress);
+        if (peerInfo == null) return null;
+        String[] tokens= peerInfo.split("\n");
+
+        for (String token : tokens) {
+            //TODO: update from interface_addr when wpa_supplicant implementation is fixed
+            if (token.startsWith("intended_addr=")) {
+                String[] nameValue = token.split("=");
+                if (nameValue.length != 2) break;
+                return nameValue[1];
+            }
+        }
+        return null;
+    }
+
+    public static String p2pPeer(String deviceAddress) {
+        return doStringCommand("P2P_PEER " + deviceAddress);
+    }
 }
diff --git a/wifi/java/android/net/wifi/p2p/WifiP2pDevice.java b/wifi/java/android/net/wifi/p2p/WifiP2pDevice.java
index 83dc285..7908726 100644
--- a/wifi/java/android/net/wifi/p2p/WifiP2pDevice.java
+++ b/wifi/java/android/net/wifi/p2p/WifiP2pDevice.java
@@ -158,9 +158,6 @@
             return;
         }
 
-        Pattern p = Pattern.compile("(?:[0-9a-f]{2}:){5}[0-9a-f]{2}", Pattern.CASE_INSENSITIVE);
-        if (p.matcher(tokens[1]).matches()) interfaceAddress = tokens[1];
-
         for (String token : tokens) {
             String[] nameValue = token.split("=");
             if (nameValue.length != 2) continue;
@@ -177,6 +174,7 @@
 
             if (nameValue[0].equals("name")) {
                 deviceName = trimQuotes(nameValue[1]);
+                continue;
             }
 
             if (nameValue[0].equals("config_methods")) {
@@ -213,9 +211,7 @@
         if (other == null || other.deviceAddress == null) {
             return (deviceAddress == null);
         }
-        //STOPSHIP: fix later
-        //return other.deviceAddress.equals(deviceAddress);
-        return other.deviceAddress.startsWith(deviceAddress.substring(0,8));
+        return other.deviceAddress.equals(deviceAddress);
     }
 
     public String toString() {
diff --git a/wifi/java/android/net/wifi/p2p/WifiP2pService.java b/wifi/java/android/net/wifi/p2p/WifiP2pService.java
index 49ce124..4447971 100644
--- a/wifi/java/android/net/wifi/p2p/WifiP2pService.java
+++ b/wifi/java/android/net/wifi/p2p/WifiP2pService.java
@@ -663,17 +663,20 @@
             if (DBG) Slog.d(TAG, getName() + message.toString());
             switch (message.what) {
                 case WifiMonitor.AP_STA_CONNECTED_EVENT:
-                    String address = (String) message.obj;
-                    mGroup.addClient(address);
-                    updateDeviceStatus(address, Status.CONNECTED);
+                    //After a GO setup, STA connected event comes with interface address
+                    String interfaceAddress = (String) message.obj;
+                    String deviceAddress = getDeviceAddress(interfaceAddress);
+                    mGroup.addClient(deviceAddress);
+                    updateDeviceStatus(deviceAddress, Status.CONNECTED);
                     if (DBG) Slog.d(TAG, getName() + " ap sta connected");
                     sendP2pPeersChangedBroadcast();
                     break;
                 case WifiMonitor.AP_STA_DISCONNECTED_EVENT:
-                    address = (String) message.obj;
-                    updateDeviceStatus(address, Status.AVAILABLE);
-                    if (mGroup.removeClient(address)) {
-                        if (DBG) Slog.d(TAG, "Removed client " + address);
+                    interfaceAddress = (String) message.obj;
+                    deviceAddress = getDeviceAddress(interfaceAddress);
+                    updateDeviceStatus(deviceAddress, Status.AVAILABLE);
+                    if (mGroup.removeClient(deviceAddress)) {
+                        if (DBG) Slog.d(TAG, "Removed client " + deviceAddress);
                         if (mGroup.isClientListEmpty()) {
                             Slog.d(TAG, "Client list empty, killing p2p connection");
                             sendMessage(WifiP2pManager.REMOVE_GROUP);
@@ -682,7 +685,7 @@
                             sendP2pPeersChangedBroadcast();
                         }
                     } else {
-                        if (DBG) Slog.d(TAG, "Failed to remove client " + address);
+                        if (DBG) Slog.d(TAG, "Failed to remove client " + deviceAddress);
                         for (WifiP2pDevice c : mGroup.getClientList()) {
                             if (DBG) Slog.d(TAG,"client " + c.deviceAddress);
                         }
@@ -1005,12 +1008,20 @@
 
     private void updateDeviceStatus(String deviceAddress, Status status) {
         for (WifiP2pDevice d : mPeers.getDeviceList()) {
-           // TODO: fix later
-           // if (d.deviceAddress.equals(deviceAddress)) {
-            if (d.deviceAddress.startsWith(deviceAddress.substring(0, 8))) {
+            if (d.deviceAddress.equals(deviceAddress)) {
                 d.status = status;
             }
         }
     }
+
+    private String getDeviceAddress(String interfaceAddress) {
+        for (WifiP2pDevice d : mPeers.getDeviceList()) {
+            if (interfaceAddress.equals(WifiNative.p2pGetInterfaceAddress(d.deviceAddress))) {
+                return d.deviceAddress;
+            }
+        }
+        return null;
+    }
+
     }
 }