Merge "Accessibility focus traversal in virtual nodes." into jb-dev
diff --git a/core/java/android/view/GLES20Canvas.java b/core/java/android/view/GLES20Canvas.java
index 0bb5f9f..1631fb3 100644
--- a/core/java/android/view/GLES20Canvas.java
+++ b/core/java/android/view/GLES20Canvas.java
@@ -833,19 +833,40 @@
     @Override
     public void drawBitmap(int[] colors, int offset, int stride, float x, float y,
             int width, int height, boolean hasAlpha, Paint paint) {
+        if (width < 0) {
+            throw new IllegalArgumentException("width must be >= 0");
+        }
+
+        if (height < 0) {
+            throw new IllegalArgumentException("height must be >= 0");
+        }
+
+        if (Math.abs(stride) < width) {
+            throw new IllegalArgumentException("abs(stride) must be >= width");
+        }
+
+        int lastScanline = offset + (height - 1) * stride;
+        int length = colors.length;
+
+        if (offset < 0 || (offset + width > length) || lastScanline < 0 ||
+                (lastScanline + width > length)) {
+            throw new ArrayIndexOutOfBoundsException();
+        }
+
         // Shaders are ignored when drawing bitmaps
         int modifier = paint != null ? setupColorFilter(paint) : MODIFIER_NONE;
         try {
-            final Bitmap.Config config = hasAlpha ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
-            final Bitmap b = Bitmap.createBitmap(colors, offset, stride, width, height, config);
             final int nativePaint = paint == null ? 0 : paint.mNativePaint;
-            nDrawBitmap(mRenderer, b.mNativeBitmap, b.mBuffer, x, y, nativePaint);
-            b.recycle();
+            nDrawBitmap(mRenderer, colors, offset, stride, x, y,
+                    width, height, hasAlpha, nativePaint);
         } finally {
             if (modifier != MODIFIER_NONE) nResetModifiers(mRenderer, modifier);
         }
     }
 
+    private static native void nDrawBitmap(int renderer, int[] colors, int offset, int stride,
+            float x, float y, int width, int height, boolean hasAlpha, int nativePaint);
+
     @Override
     public void drawBitmap(int[] colors, int offset, int stride, int x, int y,
             int width, int height, boolean hasAlpha, Paint paint) {
diff --git a/core/java/android/widget/SpellChecker.java b/core/java/android/widget/SpellChecker.java
index 7ca02e1..74ea038 100644
--- a/core/java/android/widget/SpellChecker.java
+++ b/core/java/android/widget/SpellChecker.java
@@ -132,8 +132,6 @@
 
         // Restore SpellCheckSpans in pool
         for (int i = 0; i < mLength; i++) {
-            // Resets id and progress to invalidate spell check span
-            mSpellCheckSpans[i].setSpellCheckInProgress(false);
             mIds[i] = -1;
         }
         mLength = 0;
@@ -200,15 +198,16 @@
 
     private void addSpellCheckSpan(Editable editable, int start, int end) {
         final int index = nextSpellCheckSpanIndex();
-        editable.setSpan(mSpellCheckSpans[index], start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+        SpellCheckSpan spellCheckSpan = mSpellCheckSpans[index];
+        editable.setSpan(spellCheckSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+        spellCheckSpan.setSpellCheckInProgress(false);
         mIds[index] = mSpanSequenceCounter++;
     }
 
-    public void removeSpellCheckSpan(SpellCheckSpan spellCheckSpan) {
+    public void onSpellCheckSpanRemoved(SpellCheckSpan spellCheckSpan) {
+        // Recycle any removed SpellCheckSpan (from this code or during text edition)
         for (int i = 0; i < mLength; i++) {
             if (mSpellCheckSpans[i] == spellCheckSpan) {
-                // Resets id and progress to invalidate spell check span
-                mSpellCheckSpans[i].setSpellCheckInProgress(false);
                 mIds[i] = -1;
                 return;
             }
@@ -387,6 +386,7 @@
             final SpellCheckSpan spellCheckSpan =
                     onGetSuggestionsInternal(results[i], USE_SPAN_RANGE, USE_SPAN_RANGE);
             if (spellCheckSpan != null) {
+                // onSpellCheckSpanRemoved will recycle this span in the pool
                 editable.removeSpan(spellCheckSpan);
             }
         }
@@ -414,11 +414,12 @@
                         suggestionsInfo, offset, length);
                 if (spellCheckSpan == null && scs != null) {
                     // the spellCheckSpan is shared by all the "SuggestionsInfo"s in the same
-                    // SentenceSuggestionsInfo
+                    // SentenceSuggestionsInfo. Removal is deferred after this loop.
                     spellCheckSpan = scs;
                 }
             }
             if (spellCheckSpan != null) {
+                // onSpellCheckSpanRemoved will recycle this span in the pool
                 editable.removeSpan(spellCheckSpan);
             }
         }
@@ -633,7 +634,8 @@
                             }
                             break;
                         }
-                        removeSpellCheckSpan(spellCheckSpan);
+                        // This spellCheckSpan is replaced by the one we are creating
+                        editable.removeSpan(spellCheckSpan);
                         spellCheckStart = Math.min(spanStart, spellCheckStart);
                         spellCheckEnd = Math.max(spanEnd, spellCheckEnd);
                     }
diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java
index abf2eb2..fc56e11 100644
--- a/core/java/android/widget/TextView.java
+++ b/core/java/android/widget/TextView.java
@@ -7240,7 +7240,7 @@
 
         if (mEditor != null && mEditor.mSpellChecker != null && newStart < 0 &&
                 what instanceof SpellCheckSpan) {
-            mEditor.mSpellChecker.removeSpellCheckSpan((SpellCheckSpan) what);
+            mEditor.mSpellChecker.onSpellCheckSpanRemoved((SpellCheckSpan) what);
         }
     }
 
diff --git a/core/jni/android_view_GLES20Canvas.cpp b/core/jni/android_view_GLES20Canvas.cpp
index 60929ac..d8ec656 100644
--- a/core/jni/android_view_GLES20Canvas.cpp
+++ b/core/jni/android_view_GLES20Canvas.cpp
@@ -326,8 +326,8 @@
 // ----------------------------------------------------------------------------
 
 static void android_view_GLES20Canvas_drawBitmap(JNIEnv* env, jobject clazz,
-        OpenGLRenderer* renderer, SkBitmap* bitmap, jbyteArray buffer, float left,
-        float top, SkPaint* paint) {
+        OpenGLRenderer* renderer, SkBitmap* bitmap, jbyteArray buffer,
+        jfloat left, jfloat top, SkPaint* paint) {
     // This object allows the renderer to allocate a global JNI ref to the buffer object.
     JavaHeapBitmapRef bitmapRef(env, bitmap, buffer);
 
@@ -354,6 +354,24 @@
     renderer->drawBitmap(bitmap, matrix, paint);
 }
 
+static void android_view_GLES20Canvas_drawBitmapData(JNIEnv* env, jobject clazz,
+        OpenGLRenderer* renderer, jintArray colors, jint offset, jint stride,
+        jfloat left, jfloat top, jint width, jint height, jboolean hasAlpha, SkPaint* paint) {
+    SkBitmap bitmap;
+    SkBitmap::Config config = hasAlpha ? SkBitmap::kARGB_8888_Config : SkBitmap::kRGB_565_Config;
+    bitmap.setConfig(config, width, height);
+
+    if (!bitmap.allocPixels()) {
+        return;
+    }
+
+    if (!GraphicsJNI::SetPixels(env, colors, offset, stride, 0, 0, width, height, bitmap)) {
+        return;
+    }
+
+    renderer->drawBitmapData(&bitmap, left, top, paint);
+}
+
 static void android_view_GLES20Canvas_drawBitmapMesh(JNIEnv* env, jobject clazz,
         OpenGLRenderer* renderer, SkBitmap* bitmap, jbyteArray buffer,
         jint meshWidth, jint meshHeight, jfloatArray vertices, jint offset,
@@ -880,6 +898,7 @@
     { "nDrawBitmap",        "(II[BFFI)V",      (void*) android_view_GLES20Canvas_drawBitmap },
     { "nDrawBitmap",        "(II[BFFFFFFFFI)V",(void*) android_view_GLES20Canvas_drawBitmapRect },
     { "nDrawBitmap",        "(II[BII)V",       (void*) android_view_GLES20Canvas_drawBitmapMatrix },
+    { "nDrawBitmap",        "(I[IIIFFIIZI)V",  (void*) android_view_GLES20Canvas_drawBitmapData },
 
     { "nDrawBitmapMesh",    "(II[BII[FI[III)V",(void*) android_view_GLES20Canvas_drawBitmapMesh },
 
diff --git a/include/androidfw/Input.h b/include/androidfw/Input.h
index aa8b824..2c91fab 100644
--- a/include/androidfw/Input.h
+++ b/include/androidfw/Input.h
@@ -176,7 +176,6 @@
     status_t setAxisValue(int32_t axis, float value);
 
     void scale(float scale);
-    void lerp(const PointerCoords& a, const PointerCoords& b, float alpha);
 
     inline float getX() const {
         return getAxisValue(AMOTION_EVENT_AXIS_X);
diff --git a/include/androidfw/InputTransport.h b/include/androidfw/InputTransport.h
index 2924505..5706bce 100644
--- a/include/androidfw/InputTransport.h
+++ b/include/androidfw/InputTransport.h
@@ -92,6 +92,12 @@
                 PointerCoords coords;
             } pointers[MAX_POINTERS];
 
+            int32_t getActionId() const {
+                uint32_t index = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)
+                        >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
+                return pointers[index].properties.id;
+            }
+
             inline size_t size() const {
                 return sizeof(Motion) - sizeof(Pointer) * MAX_POINTERS
                         + sizeof(Pointer) * pointerCount;
@@ -322,6 +328,10 @@
     bool hasPendingBatch() const;
 
 private:
+    // True if touch resampling is enabled.
+    const bool mResampleTouch;
+
+    // The input channel.
     sp<InputChannel> mChannel;
 
     // The current input message.
@@ -341,6 +351,7 @@
     struct History {
         nsecs_t eventTime;
         BitSet32 idBits;
+        int32_t idToIndex[MAX_POINTER_ID + 1];
         PointerCoords pointers[MAX_POINTERS];
 
         void initializeFrom(const InputMessage* msg) {
@@ -349,10 +360,14 @@
             for (size_t i = 0; i < msg->body.motion.pointerCount; i++) {
                 uint32_t id = msg->body.motion.pointers[i].properties.id;
                 idBits.markBit(id);
-                size_t index = idBits.getIndexOfBit(id);
-                pointers[index].copyFrom(msg->body.motion.pointers[i].coords);
+                idToIndex[id] = i;
+                pointers[i].copyFrom(msg->body.motion.pointers[i].coords);
             }
         }
+
+        const PointerCoords& getPointerById(uint32_t id) const {
+            return pointers[idToIndex[id]];
+        }
     };
     struct TouchState {
         int32_t deviceId;
@@ -360,12 +375,15 @@
         size_t historyCurrent;
         size_t historySize;
         History history[2];
+        History lastResample;
 
         void initialize(int32_t deviceId, int32_t source) {
             this->deviceId = deviceId;
             this->source = source;
             historyCurrent = 0;
             historySize = 0;
+            lastResample.eventTime = 0;
+            lastResample.idBits.clear();
         }
 
         void addHistory(const InputMessage* msg) {
@@ -398,6 +416,7 @@
             Batch& batch, size_t count, uint32_t* outSeq, InputEvent** outEvent);
 
     void updateTouchState(InputMessage* msg);
+    void rewriteMessage(const TouchState& state, InputMessage* msg);
     void resampleTouchState(nsecs_t frameTime, MotionEvent* event,
             const InputMessage *next);
 
@@ -412,6 +431,8 @@
     static bool canAddSample(const Batch& batch, const InputMessage* msg);
     static ssize_t findSampleNoLaterThan(const Batch& batch, nsecs_t time);
     static bool shouldResampleTool(int32_t toolType);
+
+    static bool isTouchResamplingEnabled();
 };
 
 } // namespace android
diff --git a/include/androidfw/VelocityTracker.h b/include/androidfw/VelocityTracker.h
index 6964588..cbb07829 100644
--- a/include/androidfw/VelocityTracker.h
+++ b/include/androidfw/VelocityTracker.h
@@ -37,6 +37,9 @@
     struct Estimator {
         static const size_t MAX_DEGREE = 2;
 
+        // Estimator time base.
+        nsecs_t time;
+
         // Polynomial coefficients describing motion in X and Y.
         float xCoeff[MAX_DEGREE + 1], yCoeff[MAX_DEGREE + 1];
 
@@ -48,6 +51,7 @@
         float confidence;
 
         inline void clear() {
+            time = 0;
             degree = 0;
             confidence = 0;
             for (size_t i = 0; i <= MAX_DEGREE; i++) {
@@ -58,7 +62,6 @@
     };
 
     VelocityTracker();
-    VelocityTracker(VelocityTrackerStrategy* strategy);
     ~VelocityTracker();
 
     // Resets the velocity tracker state.
@@ -96,6 +99,7 @@
     inline BitSet32 getCurrentPointerIdBits() const { return mCurrentPointerIdBits; }
 
 private:
+    nsecs_t mLastEventTime;
     BitSet32 mCurrentPointerIdBits;
     int32_t mActivePointerId;
     VelocityTrackerStrategy* mStrategy;
diff --git a/libs/androidfw/Input.cpp b/libs/androidfw/Input.cpp
index 40a6c47..97b0ec1 100644
--- a/libs/androidfw/Input.cpp
+++ b/libs/androidfw/Input.cpp
@@ -211,26 +211,6 @@
     scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, scaleFactor);
 }
 
-void PointerCoords::lerp(const PointerCoords& a, const PointerCoords& b, float alpha) {
-    bits = 0;
-    for (uint64_t bitsRemaining = a.bits | b.bits; bitsRemaining; ) {
-        int32_t axis = __builtin_ctz(bitsRemaining);
-        uint64_t axisBit = 1LL << axis;
-        bitsRemaining &= ~axisBit;
-        if (a.bits & axisBit) {
-            if (b.bits & axisBit) {
-                float aval = a.getAxisValue(axis);
-                float bval = b.getAxisValue(axis);
-                setAxisValue(axis, aval + alpha * (bval - aval));
-            } else {
-                setAxisValue(axis, a.getAxisValue(axis));
-            }
-        } else {
-            setAxisValue(axis, b.getAxisValue(axis));
-        }
-    }
-}
-
 #ifdef HAVE_ANDROID_OS
 status_t PointerCoords::readFromParcel(Parcel* parcel) {
     bits = parcel->readInt64();
diff --git a/libs/androidfw/InputTransport.cpp b/libs/androidfw/InputTransport.cpp
index 9a4182c..351c666 100644
--- a/libs/androidfw/InputTransport.cpp
+++ b/libs/androidfw/InputTransport.cpp
@@ -21,6 +21,7 @@
 
 
 #include <cutils/log.h>
+#include <cutils/properties.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <androidfw/InputTransport.h>
@@ -43,15 +44,23 @@
 
 // Latency added during resampling.  A few milliseconds doesn't hurt much but
 // reduces the impact of mispredicted touch positions.
-static const nsecs_t RESAMPLE_LATENCY = 4 * NANOS_PER_MS;
+static const nsecs_t RESAMPLE_LATENCY = 5 * NANOS_PER_MS;
 
 // Minimum time difference between consecutive samples before attempting to resample.
-static const nsecs_t RESAMPLE_MIN_DELTA = 1 * NANOS_PER_MS;
+static const nsecs_t RESAMPLE_MIN_DELTA = 2 * NANOS_PER_MS;
 
-// Maximum linear interpolation scale value.  The larger this is, the more error may
-// potentially be introduced.
-static const float RESAMPLE_MAX_ALPHA = 2.0f;
+// Maximum time to predict forward from the last known state, to avoid predicting too
+// far into the future.  This time is further bounded by 50% of the last time delta.
+static const nsecs_t RESAMPLE_MAX_PREDICTION = 8 * NANOS_PER_MS;
 
+template<typename T>
+inline static T min(const T& a, const T& b) {
+    return a < b ? a : b;
+}
+
+inline static float lerp(float a, float b, float alpha) {
+    return a + alpha * (b - a);
+}
 
 // --- InputMessage ---
 
@@ -352,12 +361,28 @@
 // --- InputConsumer ---
 
 InputConsumer::InputConsumer(const sp<InputChannel>& channel) :
+        mResampleTouch(isTouchResamplingEnabled()),
         mChannel(channel), mMsgDeferred(false) {
 }
 
 InputConsumer::~InputConsumer() {
 }
 
+bool InputConsumer::isTouchResamplingEnabled() {
+    char value[PROPERTY_VALUE_MAX];
+    int length = property_get("debug.inputconsumer.resample", value, NULL);
+    if (length > 0) {
+        if (!strcmp("0", value)) {
+            return false;
+        }
+        if (strcmp("1", value)) {
+            ALOGD("Unrecognized property value for 'debug.inputconsumer.resample'.  "
+                    "Use '1' or '0'.");
+        }
+    }
+    return true;
+}
+
 status_t InputConsumer::consume(InputEventFactoryInterface* factory,
         bool consumeBatches, nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent) {
 #if DEBUG_TRANSPORT_ACTIONS
@@ -538,18 +563,19 @@
 }
 
 void InputConsumer::updateTouchState(InputMessage* msg) {
-    if (!(msg->body.motion.source & AINPUT_SOURCE_CLASS_POINTER)) {
+    if (!mResampleTouch ||
+            !(msg->body.motion.source & AINPUT_SOURCE_CLASS_POINTER)) {
         return;
     }
 
     int32_t deviceId = msg->body.motion.deviceId;
     int32_t source = msg->body.motion.source;
+    nsecs_t eventTime = msg->body.motion.eventTime;
 
-    // TODO: Filter the incoming touch event so that it aligns better
-    // with prior predictions.  Turning RESAMPLE_LATENCY offsets the need
-    // for filtering but it would be nice to reduce the latency further.
-
-    switch (msg->body.motion.action) {
+    // Update the touch state history to incorporate the new input message.
+    // If the message is in the past relative to the most recently produced resampled
+    // touch, then use the resampled time and coordinates instead.
+    switch (msg->body.motion.action & AMOTION_EVENT_ACTION_MASK) {
     case AMOTION_EVENT_ACTION_DOWN: {
         ssize_t index = findTouchState(deviceId, source);
         if (index < 0) {
@@ -567,6 +593,40 @@
         if (index >= 0) {
             TouchState& touchState = mTouchStates.editItemAt(index);
             touchState.addHistory(msg);
+            if (eventTime < touchState.lastResample.eventTime) {
+                rewriteMessage(touchState, msg);
+            } else {
+                touchState.lastResample.idBits.clear();
+            }
+        }
+        break;
+    }
+
+    case AMOTION_EVENT_ACTION_POINTER_DOWN: {
+        ssize_t index = findTouchState(deviceId, source);
+        if (index >= 0) {
+            TouchState& touchState = mTouchStates.editItemAt(index);
+            touchState.lastResample.idBits.clearBit(msg->body.motion.getActionId());
+            rewriteMessage(touchState, msg);
+        }
+        break;
+    }
+
+    case AMOTION_EVENT_ACTION_POINTER_UP: {
+        ssize_t index = findTouchState(deviceId, source);
+        if (index >= 0) {
+            TouchState& touchState = mTouchStates.editItemAt(index);
+            rewriteMessage(touchState, msg);
+            touchState.lastResample.idBits.clearBit(msg->body.motion.getActionId());
+        }
+        break;
+    }
+
+    case AMOTION_EVENT_ACTION_SCROLL: {
+        ssize_t index = findTouchState(deviceId, source);
+        if (index >= 0) {
+            const TouchState& touchState = mTouchStates.itemAt(index);
+            rewriteMessage(touchState, msg);
         }
         break;
     }
@@ -575,6 +635,8 @@
     case AMOTION_EVENT_ACTION_CANCEL: {
         ssize_t index = findTouchState(deviceId, source);
         if (index >= 0) {
+            const TouchState& touchState = mTouchStates.itemAt(index);
+            rewriteMessage(touchState, msg);
             mTouchStates.removeAt(index);
         }
         break;
@@ -582,13 +644,30 @@
     }
 }
 
+void InputConsumer::rewriteMessage(const TouchState& state, InputMessage* msg) {
+    for (size_t i = 0; i < msg->body.motion.pointerCount; i++) {
+        uint32_t id = msg->body.motion.pointers[i].properties.id;
+        if (state.lastResample.idBits.hasBit(id)) {
+            PointerCoords& msgCoords = msg->body.motion.pointers[i].coords;
+            const PointerCoords& resampleCoords = state.lastResample.getPointerById(id);
+#if DEBUG_RESAMPLING
+            ALOGD("[%d] - rewrite (%0.3f, %0.3f), old (%0.3f, %0.3f)", id,
+                    resampleCoords.getAxisValue(AMOTION_EVENT_AXIS_X),
+                    resampleCoords.getAxisValue(AMOTION_EVENT_AXIS_Y),
+                    msgCoords.getAxisValue(AMOTION_EVENT_AXIS_X),
+                    msgCoords.getAxisValue(AMOTION_EVENT_AXIS_Y));
+#endif
+            msgCoords.setAxisValue(AMOTION_EVENT_AXIS_X, resampleCoords.getX());
+            msgCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, resampleCoords.getY());
+        }
+    }
+}
+
 void InputConsumer::resampleTouchState(nsecs_t sampleTime, MotionEvent* event,
     const InputMessage* next) {
-    if (event->getAction() != AMOTION_EVENT_ACTION_MOVE
-            || !(event->getSource() & AINPUT_SOURCE_CLASS_POINTER)) {
-#if DEBUG_RESAMPLING
-        ALOGD("Not resampled, not a move.");
-#endif
+    if (!mResampleTouch
+            || !(event->getSource() & AINPUT_SOURCE_CLASS_POINTER)
+            || event->getAction() != AMOTION_EVENT_ACTION_MOVE) {
         return;
     }
 
@@ -608,39 +687,9 @@
         return;
     }
 
+    // Ensure that the current sample has all of the pointers that need to be reported.
     const History* current = touchState.getHistory(0);
-    const History* other;
-    History future;
-    if (next) {
-        future.initializeFrom(next);
-        other = &future;
-    } else if (touchState.historySize >= 2) {
-        other = touchState.getHistory(1);
-    } else {
-#if DEBUG_RESAMPLING
-        ALOGD("Not resampled, insufficient data.");
-#endif
-        return;
-    }
-
-    nsecs_t delta = current->eventTime - other->eventTime;
-    if (delta > -RESAMPLE_MIN_DELTA && delta < RESAMPLE_MIN_DELTA) {
-#if DEBUG_RESAMPLING
-        ALOGD("Not resampled, delta time is %lld", delta);
-#endif
-        return;
-    }
-
-    float alpha = float(current->eventTime - sampleTime) / delta;
-    if (fabs(alpha) > RESAMPLE_MAX_ALPHA) {
-#if DEBUG_RESAMPLING
-        ALOGD("Not resampled, alpha is %f", alpha);
-#endif
-        return;
-    }
-
     size_t pointerCount = event->getPointerCount();
-    PointerCoords resampledCoords[MAX_POINTERS];
     for (size_t i = 0; i < pointerCount; i++) {
         uint32_t id = event->getPointerId(i);
         if (!current->idBits.hasBit(id)) {
@@ -649,32 +698,89 @@
 #endif
             return;
         }
-        const PointerCoords& currentCoords =
-                current->pointers[current->idBits.getIndexOfBit(id)];
+    }
+
+    // Find the data to use for resampling.
+    const History* other;
+    History future;
+    float alpha;
+    if (next) {
+        // Interpolate between current sample and future sample.
+        // So current->eventTime <= sampleTime <= future.eventTime.
+        future.initializeFrom(next);
+        other = &future;
+        nsecs_t delta = future.eventTime - current->eventTime;
+        if (delta < RESAMPLE_MIN_DELTA) {
+#if DEBUG_RESAMPLING
+            ALOGD("Not resampled, delta time is %lld ns.", delta);
+#endif
+            return;
+        }
+        alpha = float(sampleTime - current->eventTime) / delta;
+    } else if (touchState.historySize >= 2) {
+        // Extrapolate future sample using current sample and past sample.
+        // So other->eventTime <= current->eventTime <= sampleTime.
+        other = touchState.getHistory(1);
+        nsecs_t delta = current->eventTime - other->eventTime;
+        if (delta < RESAMPLE_MIN_DELTA) {
+#if DEBUG_RESAMPLING
+            ALOGD("Not resampled, delta time is %lld ns.", delta);
+#endif
+            return;
+        }
+        nsecs_t maxPredict = current->eventTime + min(delta / 2, RESAMPLE_MAX_PREDICTION);
+        if (sampleTime > maxPredict) {
+#if DEBUG_RESAMPLING
+            ALOGD("Sample time is too far in the future, adjusting prediction "
+                    "from %lld to %lld ns.",
+                    sampleTime - current->eventTime, maxPredict - current->eventTime);
+#endif
+            sampleTime = maxPredict;
+        }
+        alpha = float(current->eventTime - sampleTime) / delta;
+    } else {
+#if DEBUG_RESAMPLING
+        ALOGD("Not resampled, insufficient data.");
+#endif
+        return;
+    }
+
+    // Resample touch coordinates.
+    touchState.lastResample.eventTime = sampleTime;
+    touchState.lastResample.idBits.clear();
+    for (size_t i = 0; i < pointerCount; i++) {
+        uint32_t id = event->getPointerId(i);
+        touchState.lastResample.idToIndex[id] = i;
+        touchState.lastResample.idBits.markBit(id);
+        PointerCoords& resampledCoords = touchState.lastResample.pointers[i];
+        const PointerCoords& currentCoords = current->getPointerById(id);
         if (other->idBits.hasBit(id)
                 && shouldResampleTool(event->getToolType(i))) {
-            const PointerCoords& otherCoords =
-                    other->pointers[other->idBits.getIndexOfBit(id)];
-            resampledCoords[i].lerp(currentCoords, otherCoords, alpha);
+            const PointerCoords& otherCoords = other->getPointerById(id);
+            resampledCoords.copyFrom(currentCoords);
+            resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_X,
+                    lerp(currentCoords.getX(), otherCoords.getX(), alpha));
+            resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_Y,
+                    lerp(currentCoords.getY(), otherCoords.getY(), alpha));
 #if DEBUG_RESAMPLING
             ALOGD("[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f), "
                     "other (%0.3f, %0.3f), alpha %0.3f",
-                    i, resampledCoords[i].getX(), resampledCoords[i].getY(),
+                    id, resampledCoords.getX(), resampledCoords.getY(),
                     currentCoords.getX(), currentCoords.getY(),
                     otherCoords.getX(), otherCoords.getY(),
                     alpha);
 #endif
         } else {
-            resampledCoords[i].copyFrom(currentCoords);
+            resampledCoords.copyFrom(currentCoords);
 #if DEBUG_RESAMPLING
             ALOGD("[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f)",
-                    i, resampledCoords[i].getX(), resampledCoords[i].getY(),
+                    id, resampledCoords.getX(), resampledCoords.getY(),
                     currentCoords.getX(), currentCoords.getY());
 #endif
         }
     }
 
-    event->addSample(sampleTime, resampledCoords);
+    event->addSample(sampleTime, touchState.lastResample.pointers);
 }
 
 bool InputConsumer::shouldResampleTool(int32_t toolType) {
diff --git a/libs/androidfw/VelocityTracker.cpp b/libs/androidfw/VelocityTracker.cpp
index de214f8f..5dbafd8 100644
--- a/libs/androidfw/VelocityTracker.cpp
+++ b/libs/androidfw/VelocityTracker.cpp
@@ -33,6 +33,16 @@
 
 namespace android {
 
+// Nanoseconds per milliseconds.
+static const nsecs_t NANOS_PER_MS = 1000000;
+
+// Threshold for determining that a pointer has stopped moving.
+// Some input devices do not send ACTION_MOVE events in the case where a pointer has
+// stopped.  We need to detect this case so that we can accurately predict the
+// velocity after the pointer starts moving again.
+static const nsecs_t ASSUME_POINTER_STOPPED_TIME = 40 * NANOS_PER_MS;
+
+
 static float vectorDot(const float* a, const float* b, uint32_t m) {
     float r = 0;
     while (m--) {
@@ -89,15 +99,10 @@
 // --- VelocityTracker ---
 
 VelocityTracker::VelocityTracker() :
-        mCurrentPointerIdBits(0), mActivePointerId(-1),
+        mLastEventTime(0), mCurrentPointerIdBits(0), mActivePointerId(-1),
         mStrategy(new LeastSquaresVelocityTrackerStrategy()) {
 }
 
-VelocityTracker::VelocityTracker(VelocityTrackerStrategy* strategy) :
-        mCurrentPointerIdBits(0), mActivePointerId(-1),
-        mStrategy(strategy) {
-}
-
 VelocityTracker::~VelocityTracker() {
     delete mStrategy;
 }
@@ -125,6 +130,18 @@
         idBits.clearLastMarkedBit();
     }
 
+    if ((mCurrentPointerIdBits.value & idBits.value)
+            && eventTime >= mLastEventTime + ASSUME_POINTER_STOPPED_TIME) {
+#if DEBUG_VELOCITY
+        ALOGD("VelocityTracker: stopped for %0.3f ms, clearing state.",
+                (eventTime - mLastEventTime) * 0.000001f);
+#endif
+        // We have not received any movements for too long.  Assume that all pointers
+        // have stopped.
+        mStrategy->clear();
+    }
+    mLastEventTime = eventTime;
+
     mCurrentPointerIdBits = idBits;
     if (mActivePointerId < 0 || !idBits.hasBit(mActivePointerId)) {
         mActivePointerId = idBits.isEmpty() ? -1 : idBits.firstMarkedBit();
@@ -145,8 +162,8 @@
                 "estimator (degree=%d, xCoeff=%s, yCoeff=%s, confidence=%f)",
                 id, positions[index].x, positions[index].y,
                 int(estimator.degree),
-                vectorToString(estimator.xCoeff, estimator.degree).string(),
-                vectorToString(estimator.yCoeff, estimator.degree).string(),
+                vectorToString(estimator.xCoeff, estimator.degree + 1).string(),
+                vectorToString(estimator.yCoeff, estimator.degree + 1).string(),
                 estimator.confidence);
     }
 #endif
@@ -195,6 +212,11 @@
         idBits.markBit(event->getPointerId(i));
     }
 
+    uint32_t pointerIndex[MAX_POINTERS];
+    for (size_t i = 0; i < pointerCount; i++) {
+        pointerIndex[i] = idBits.getIndexOfBit(event->getPointerId(i));
+    }
+
     nsecs_t eventTime;
     Position positions[pointerCount];
 
@@ -202,16 +224,18 @@
     for (size_t h = 0; h < historySize; h++) {
         eventTime = event->getHistoricalEventTime(h);
         for (size_t i = 0; i < pointerCount; i++) {
-            positions[i].x = event->getHistoricalX(i, h);
-            positions[i].y = event->getHistoricalY(i, h);
+            uint32_t index = pointerIndex[i];
+            positions[index].x = event->getHistoricalX(i, h);
+            positions[index].y = event->getHistoricalY(i, h);
         }
         addMovement(eventTime, idBits, positions);
     }
 
     eventTime = event->getEventTime();
     for (size_t i = 0; i < pointerCount; i++) {
-        positions[i].x = event->getX(i);
-        positions[i].y = event->getY(i);
+        uint32_t index = pointerIndex[i];
+        positions[index].x = event->getX(i);
+        positions[index].y = event->getY(i);
     }
     addMovement(eventTime, idBits, positions);
 }
@@ -460,6 +484,7 @@
         uint32_t n = degree + 1;
         if (solveLeastSquares(time, x, m, n, outEstimator->xCoeff, &xdet)
                 && solveLeastSquares(time, y, m, n, outEstimator->yCoeff, &ydet)) {
+            outEstimator->time = newestMovement.eventTime;
             outEstimator->degree = degree;
             outEstimator->confidence = xdet * ydet;
 #if DEBUG_LEAST_SQUARES
@@ -476,6 +501,7 @@
     // No velocity data available for this pointer, but we do have its current position.
     outEstimator->xCoeff[0] = x[0];
     outEstimator->yCoeff[0] = y[0];
+    outEstimator->time = newestMovement.eventTime;
     outEstimator->degree = 0;
     outEstimator->confidence = 1;
     return true;
diff --git a/libs/hwui/DisplayListRenderer.cpp b/libs/hwui/DisplayListRenderer.cpp
index f6ca77c..d2a6a7a 100644
--- a/libs/hwui/DisplayListRenderer.cpp
+++ b/libs/hwui/DisplayListRenderer.cpp
@@ -49,6 +49,7 @@
     "DrawBitmap",
     "DrawBitmapMatrix",
     "DrawBitmapRect",
+    "DrawBitmapData",
     "DrawBitmapMesh",
     "DrawPatch",
     "DrawColor",
@@ -434,6 +435,14 @@
                         (char*) indent, OP_NAMES[op], bitmap, f1, f2, f3, f4, f5, f6, f7, f8, paint);
             }
             break;
+            case DrawBitmapData: {
+                SkBitmap* bitmap = getBitmapData();
+                float x = getFloat();
+                float y = getFloat();
+                SkPaint* paint = getPaint(renderer);
+                ALOGD("%s%s %.2f, %.2f, %p", (char*) indent, OP_NAMES[op], x, y, paint);
+            }
+            break;
             case DrawBitmapMesh: {
                 int verticesCount = 0;
                 uint32_t colorsCount = 0;
@@ -1020,6 +1029,19 @@
                 renderer.drawBitmap(bitmap, f1, f2, f3, f4, f5, f6, f7, f8, paint);
             }
             break;
+            case DrawBitmapData: {
+                SkBitmap* bitmap = getBitmapData();
+                float x = getFloat();
+                float y = getFloat();
+                SkPaint* paint = getPaint(renderer);
+                DISPLAY_LIST_LOGD("%s%s %p, %.2f, %.2f, %p", (char*) indent, OP_NAMES[op],
+                        bitmap, x, y, paint);
+                if (bitmap) {
+                    renderer.drawBitmap(bitmap, x, y, paint);
+                    delete bitmap;
+                }
+            }
+            break;
             case DrawBitmapMesh: {
                 int32_t verticesCount = 0;
                 uint32_t colorsCount = 0;
@@ -1487,6 +1509,15 @@
     addSkip(location);
 }
 
+void DisplayListRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
+    const bool reject = quickReject(left, top, left + bitmap->width(), bitmap->height());
+    uint32_t* location = addOp(DisplayList::DrawBitmapData, reject);
+    addBitmapData(bitmap);
+    addPoint(left, top);
+    addPaint(paint);
+    addSkip(location);
+}
+
 void DisplayListRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
         float* vertices, int* colors, SkPaint* paint) {
     addOp(DisplayList::DrawBitmapMesh);
diff --git a/libs/hwui/DisplayListRenderer.h b/libs/hwui/DisplayListRenderer.h
index 5ce770d..2f74f5b 100644
--- a/libs/hwui/DisplayListRenderer.h
+++ b/libs/hwui/DisplayListRenderer.h
@@ -91,6 +91,7 @@
         DrawBitmap,
         DrawBitmapMatrix,
         DrawBitmapRect,
+        DrawBitmapData,
         DrawBitmapMesh,
         DrawPatch,
         DrawColor,
@@ -422,6 +423,19 @@
         return (SkBitmap*) getInt();
     }
 
+    SkBitmap* getBitmapData() {
+        SkBitmap* bitmap = new SkBitmap;
+        bitmap->setConfig((SkBitmap::Config) getInt(), getInt(), getInt());
+        if (!bitmap->allocPixels()) {
+            delete bitmap;
+            return NULL;
+        }
+
+        bitmap->setPixels((void*) mReader.skip(bitmap->height() * bitmap->rowBytes()));
+
+        return bitmap;
+    }
+
     SkiaShader* getShader() {
         return (SkiaShader*) getInt();
     }
@@ -574,6 +588,7 @@
     virtual void drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop,
             float srcRight, float srcBottom, float dstLeft, float dstTop,
             float dstRight, float dstBottom, SkPaint* paint);
+    virtual void drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint);
     virtual void drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
             float* vertices, int* colors, SkPaint* paint);
     virtual void drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
@@ -701,16 +716,23 @@
 
     void addInts(const int32_t* values, uint32_t count) {
         mWriter.writeInt(count);
-        for (uint32_t i = 0; i < count; i++) {
-            mWriter.writeInt(values[i]);
-        }
+        mWriter.write(values, count * sizeof(int32_t));
+    }
+
+    void addBitmapData(SkBitmap* bitmap) {
+        mWriter.writeInt(bitmap->config());
+        mWriter.writeInt(bitmap->width());
+        mWriter.writeInt(bitmap->height());
+
+        SkAutoLockPixels alp(*bitmap);
+        void* src = bitmap->getPixels();
+
+        mWriter.write(src, bitmap->rowBytes() * bitmap->height());
     }
 
     void addUInts(const uint32_t* values, int8_t count) {
         mWriter.writeInt(count);
-        for (int8_t i = 0; i < count; i++) {
-            mWriter.writeInt(values[i]);
-        }
+        mWriter.write(values, count * sizeof(uint32_t));
     }
 
     inline void addFloat(float value) {
@@ -719,9 +741,7 @@
 
     void addFloats(const float* values, int32_t count) {
         mWriter.writeInt(count);
-        for (int32_t i = 0; i < count; i++) {
-            mWriter.writeScalar(values[i]);
-        }
+        mWriter.write(values, count * sizeof(float));
     }
 
     inline void addPoint(float x, float y) {
diff --git a/libs/hwui/OpenGLRenderer.cpp b/libs/hwui/OpenGLRenderer.cpp
index da2192f..7f242c3 100644
--- a/libs/hwui/OpenGLRenderer.cpp
+++ b/libs/hwui/OpenGLRenderer.cpp
@@ -1502,6 +1502,21 @@
     restore();
 }
 
+void OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
+    const float right = left + bitmap->width();
+    const float bottom = top + bitmap->height();
+
+    if (quickReject(left, top, right, bottom)) {
+        return;
+    }
+
+    mCaches.activeTexture(0);
+    Texture* texture = mCaches.textureCache.getTransient(bitmap);
+    const AutoTexture autoCleanup(texture);
+
+    drawTextureRect(left, top, right, bottom, texture, paint);
+}
+
 void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
         float* vertices, int* colors, SkPaint* paint) {
     // TODO: Do a quickReject
diff --git a/libs/hwui/OpenGLRenderer.h b/libs/hwui/OpenGLRenderer.h
index 18a6923..0ea0db7 100644
--- a/libs/hwui/OpenGLRenderer.h
+++ b/libs/hwui/OpenGLRenderer.h
@@ -115,6 +115,7 @@
     virtual void drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop,
             float srcRight, float srcBottom, float dstLeft, float dstTop,
             float dstRight, float dstBottom, SkPaint* paint);
+    virtual void drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint);
     virtual void drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
             float* vertices, int* colors, SkPaint* paint);
     virtual void drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
diff --git a/libs/hwui/TextureCache.cpp b/libs/hwui/TextureCache.cpp
index cc09aae..9fb61e4 100644
--- a/libs/hwui/TextureCache.cpp
+++ b/libs/hwui/TextureCache.cpp
@@ -160,6 +160,16 @@
     return texture;
 }
 
+Texture* TextureCache::getTransient(SkBitmap* bitmap) {
+    Texture* texture = new Texture;
+    texture->bitmapSize = bitmap->rowBytes() * bitmap->height();
+    texture->cleanup = true;
+
+    generateTexture(bitmap, texture, false);
+
+    return texture;
+}
+
 void TextureCache::remove(SkBitmap* bitmap) {
     mCache.remove(bitmap);
 }
diff --git a/libs/hwui/TextureCache.h b/libs/hwui/TextureCache.h
index 10a05e0..31a2e3d 100644
--- a/libs/hwui/TextureCache.h
+++ b/libs/hwui/TextureCache.h
@@ -67,6 +67,11 @@
      */
     Texture* get(SkBitmap* bitmap);
     /**
+     * Returns the texture associated with the specified bitmap. The generated
+     * texture is not kept in the cache. The caller must destroy the texture.
+     */
+    Texture* getTransient(SkBitmap* bitmap);
+    /**
      * Removes the texture associated with the specified bitmap.
      * Upon remove the texture is freed.
      */
diff --git a/packages/SystemUI/res/layout/status_bar_expanded.xml b/packages/SystemUI/res/layout/status_bar_expanded.xml
index bc144bb..4cff67b 100644
--- a/packages/SystemUI/res/layout/status_bar_expanded.xml
+++ b/packages/SystemUI/res/layout/status_bar_expanded.xml
@@ -48,7 +48,7 @@
             android:id="@+id/latestItems"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
-            systemui:rowHeight="@dimen/notification_height"
+            systemui:rowHeight="@dimen/notification_row_min_height"
             />
     </ScrollView>
 
@@ -68,4 +68,4 @@
 
     </com.android.systemui.statusbar.phone.CloseDragHandle>
 
-</FrameLayout><!-- end of sliding panel -->
\ No newline at end of file
+</FrameLayout><!-- end of sliding panel -->
diff --git a/packages/SystemUI/res/layout/system_bar_notification_panel.xml b/packages/SystemUI/res/layout/system_bar_notification_panel.xml
index 42af147..5579505 100644
--- a/packages/SystemUI/res/layout/system_bar_notification_panel.xml
+++ b/packages/SystemUI/res/layout/system_bar_notification_panel.xml
@@ -77,7 +77,7 @@
                     android:clickable="true"
                     android:focusable="true"
                     android:descendantFocusability="afterDescendants"
-                    systemui:rowHeight="@dimen/notification_height"
+                    systemui:rowHeight="@dimen/notification_row_min_height"
                     />
             </ScrollView>
         </LinearLayout>
diff --git a/packages/SystemUI/res/values-xhdpi/dimens.xml b/packages/SystemUI/res/values-xhdpi/dimens.xml
deleted file mode 100644
index 303841a..0000000
--- a/packages/SystemUI/res/values-xhdpi/dimens.xml
+++ /dev/null
@@ -1,27 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
- * 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.
-*/
--->
-<resources>
-    <!-- thickness (height) of each notification row, including any separators or padding -->
-    <!-- note: this is the same value as in values/dimens.xml; the value is overridden in
-         values-hdpi/dimens.xml and so we need to re-assert the general value here -->
-    <dimen name="notification_height">68dp</dimen>
-
-    <!-- thickness (height) of dividers between each notification row -->
-    <!-- same as in values/dimens.xml; see note at notification_height -->
-    <dimen name="notification_divider_height">2dp</dimen>
-</resources>
diff --git a/packages/SystemUI/res/values/dimens.xml b/packages/SystemUI/res/values/dimens.xml
index f548166..9042045 100644
--- a/packages/SystemUI/res/values/dimens.xml
+++ b/packages/SystemUI/res/values/dimens.xml
@@ -49,18 +49,21 @@
          reducing false presses on navbar buttons; approx 2mm -->
     <dimen name="navigation_bar_deadzone_size">12dp</dimen>
 
-    <!-- thickness (height) of each 1U notification row plus glow, padding, etc -->
-    <dimen name="notification_height">72dp</dimen>
-
     <!-- Height of notification icons in the status bar -->
     <dimen name="status_bar_icon_size">@*android:dimen/status_bar_icon_size</dimen>
 
-    <!-- Height of a small notification in the status bar plus glow, padding, etc -->
-    <dimen name="notification_min_height">72dp</dimen>
+    <!-- Height of a small notification in the status bar -->
+    <dimen name="notification_min_height">64dp</dimen>
 
     <!-- Height of a large notification in the status bar -->
     <dimen name="notification_max_height">256dp</dimen>
 
+    <!-- Height of a small notification in the status bar plus glow, padding, etc -->
+    <dimen name="notification_row_min_height">72dp</dimen>
+
+    <!-- Height of a large notification in the status bar plus glow, padding, etc -->
+    <dimen name="notification_row_max_height">260dp</dimen>
+
     <!-- size at which Notification icons will be drawn in the status bar -->
     <dimen name="status_bar_icon_drawing_size">18dip</dimen>
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/BaseStatusBar.java b/packages/SystemUI/src/com/android/systemui/statusbar/BaseStatusBar.java
index 464d4fb..4b223dd 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/BaseStatusBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/BaseStatusBar.java
@@ -478,7 +478,7 @@
 
     protected  boolean inflateViews(NotificationData.Entry entry, ViewGroup parent) {
         int rowHeight =
-                mContext.getResources().getDimensionPixelSize(R.dimen.notification_height);
+                mContext.getResources().getDimensionPixelSize(R.dimen.notification_row_min_height);
         int minHeight =
                 mContext.getResources().getDimensionPixelSize(R.dimen.notification_min_height);
         int maxHeight =
@@ -498,7 +498,6 @@
         // for blaming (see SwipeHelper.setLongPressListener)
         row.setTag(sbn.pkg);
 
-        ViewGroup.LayoutParams lp = row.getLayoutParams();
         workAroundBadLayerDrawableOpacity(row);
         View vetoButton = updateNotificationVetoButton(row, sbn);
         vetoButton.setContentDescription(mContext.getString(
@@ -510,13 +509,6 @@
         ViewGroup content = (ViewGroup)row.findViewById(R.id.content);
         ViewGroup adaptive = (ViewGroup)row.findViewById(R.id.adaptive);
 
-        // Ensure that R.id.content is properly set to 64dp high if 1U
-        lp = content.getLayoutParams();
-        if (large == null) {
-            lp.height = minHeight;
-        }
-        content.setLayoutParams(lp);
-
         content.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
 
         PendingIntent contentIntent = sbn.notification.contentIntent;
@@ -528,6 +520,7 @@
             content.setOnClickListener(null);
         }
 
+        // TODO(cwren) normalize variable names with those in updateNotification
         View expandedOneU = null;
         View expandedLarge = null;
         Exception exception = null;
@@ -556,7 +549,7 @@
                 SizeAdaptiveLayout.LayoutParams params =
                         new SizeAdaptiveLayout.LayoutParams(expandedLarge.getLayoutParams());
                 params.minHeight = minHeight+1;
-                params.maxHeight = SizeAdaptiveLayout.LayoutParams.UNBOUNDED;
+                params.maxHeight = maxHeight;
                 adaptive.addView(expandedLarge, params);
             }
             row.setDrawingCacheEnabled(true);
@@ -723,20 +716,18 @@
     }
 
     protected boolean expandView(NotificationData.Entry entry, boolean expand) {
-        if (entry.expandable()) {
-            int rowHeight =
-                    mContext.getResources().getDimensionPixelSize(R.dimen.notification_height);
-            ViewGroup.LayoutParams lp = entry.row.getLayoutParams();
-            if (expand) {
-                lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
-            } else {
-                lp.height = rowHeight;
-            }
-            entry.row.setLayoutParams(lp);
-            return expand;
+        int rowHeight =
+                mContext.getResources().getDimensionPixelSize(R.dimen.notification_row_min_height);
+        ViewGroup.LayoutParams lp = entry.row.getLayoutParams();
+        if (entry.expandable() && expand) {
+            if (DEBUG) Slog.d(TAG, "setting expanded row height to WRAP_CONTENT");
+            lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
         } else {
-            return false;
+            if (DEBUG) Slog.d(TAG, "setting collapsed row height to " + rowHeight);
+            lp.height = rowHeight;
         }
+        entry.row.setLayoutParams(lp);
+        return expand;
     }
 
     protected void updateExpansionStates() {
@@ -763,7 +754,7 @@
     protected abstract void tick(IBinder key, StatusBarNotification n, boolean firstTime);
     protected abstract void updateExpandedViewPos(int expandedPosition);
     protected abstract int getExpandedViewMaxHeight();
-    protected abstract boolean isStatusBarExpanded();
+    protected abstract boolean shouldDisableNavbarGestures();
 
     protected boolean isTopNotification(ViewGroup parent, NotificationData.Entry entry) {
         return parent.indexOfChild(entry.row) == 0;
@@ -781,32 +772,41 @@
         final StatusBarNotification oldNotification = oldEntry.notification;
 
         // XXX: modify when we do something more intelligent with the two content views
-        final RemoteViews oldContentView = (oldNotification.notification.bigContentView != null)
-                ? oldNotification.notification.bigContentView
-                : oldNotification.notification.contentView;
-        final RemoteViews contentView = (notification.notification.bigContentView != null)
-                ? notification.notification.bigContentView
-                : notification.notification.contentView;
+        final RemoteViews oldContentView = oldNotification.notification.contentView;
+        final RemoteViews contentView = notification.notification.contentView;
+        final RemoteViews oldBigContentView = oldNotification.notification.bigContentView;
+        final RemoteViews bigContentView = notification.notification.bigContentView;
 
         if (DEBUG) {
             Slog.d(TAG, "old notification: when=" + oldNotification.notification.when
                     + " ongoing=" + oldNotification.isOngoing()
                     + " expanded=" + oldEntry.expanded
                     + " contentView=" + oldContentView
+                    + " bigContentView=" + oldBigContentView
                     + " rowParent=" + oldEntry.row.getParent());
             Slog.d(TAG, "new notification: when=" + notification.notification.when
                     + " ongoing=" + oldNotification.isOngoing()
-                    + " contentView=" + contentView);
+                    + " contentView=" + contentView
+                    + " bigContentView=" + bigContentView);
         }
 
         // Can we just reapply the RemoteViews in place?  If when didn't change, the order
         // didn't change.
+
+        // 1U is never null
         boolean contentsUnchanged = oldEntry.expanded != null
-                && contentView != null && oldContentView != null
                 && contentView.getPackage() != null
                 && oldContentView.getPackage() != null
                 && oldContentView.getPackage().equals(contentView.getPackage())
                 && oldContentView.getLayoutId() == contentView.getLayoutId();
+        // large view may be null
+        boolean bigContentsUnchanged =
+                (oldEntry.getLargeView() == null && bigContentView == null)
+                || ((oldEntry.getLargeView() != null && bigContentView != null)
+                    && bigContentView.getPackage() != null
+                    && oldBigContentView.getPackage() != null
+                    && oldBigContentView.getPackage().equals(bigContentView.getPackage())
+                    && oldBigContentView.getLayoutId() == bigContentView.getLayoutId());
         ViewGroup rowParent = (ViewGroup) oldEntry.row.getParent();
         boolean orderUnchanged = notification.notification.when==oldNotification.notification.when
                 && notification.score == oldNotification.score;
@@ -816,12 +816,15 @@
                 && !TextUtils.equals(notification.notification.tickerText,
                         oldEntry.notification.notification.tickerText);
         boolean isTopAnyway = isTopNotification(rowParent, oldEntry);
-        if (contentsUnchanged && (orderUnchanged || isTopAnyway)) {
+        if (contentsUnchanged && bigContentsUnchanged && (orderUnchanged || isTopAnyway)) {
             if (DEBUG) Slog.d(TAG, "reusing notification for key: " + key);
             oldEntry.notification = notification;
             try {
                 // Reapply the RemoteViews
-                contentView.reapply(mContext, oldEntry.content);
+                contentView.reapply(mContext, oldEntry.expanded);
+                if (bigContentView != null && oldEntry.getLargeView() != null) {
+                    bigContentView.reapply(mContext, oldEntry.getLargeView());
+                }
                 // update the contentIntent
                 final PendingIntent contentIntent = notification.notification.contentIntent;
                 if (contentIntent != null) {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/DelegateViewHelper.java b/packages/SystemUI/src/com/android/systemui/statusbar/DelegateViewHelper.java
index 912a165..0e6dfd5 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/DelegateViewHelper.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/DelegateViewHelper.java
@@ -53,7 +53,7 @@
     }
 
     public boolean onInterceptTouchEvent(MotionEvent event) {
-        if (mBar.isStatusBarExpanded()) {
+        if (mBar.shouldDisableNavbarGestures()) {
             return false;
         }
         switch (event.getAction()) {
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 a0d3eb4..287c2922 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
@@ -2111,8 +2111,8 @@
     }
 
     @Override
-    protected boolean isStatusBarExpanded() {
-        return mExpanded;
+    protected boolean shouldDisableNavbarGestures() {
+        return mExpanded || (mDisabled & StatusBarManager.DISABLE_HOME) != 0;
     }
 }
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/NotificationRowLayout.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/NotificationRowLayout.java
index 0fe7a0a..f41d99c 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/NotificationRowLayout.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/NotificationRowLayout.java
@@ -101,8 +101,8 @@
         float densityScale = getResources().getDisplayMetrics().density;
         float pagingTouchSlop = ViewConfiguration.get(mContext).getScaledPagingTouchSlop();
         mSwipeHelper = new SwipeHelper(SwipeHelper.X, this, densityScale, pagingTouchSlop);
-        int minHeight = getResources().getDimensionPixelSize(R.dimen.notification_min_height);
-        int maxHeight = getResources().getDimensionPixelSize(R.dimen.notification_max_height);
+        int minHeight = getResources().getDimensionPixelSize(R.dimen.notification_row_min_height);
+        int maxHeight = getResources().getDimensionPixelSize(R.dimen.notification_row_max_height);
         mExpandHelper = new ExpandHelper(mContext, this, minHeight, maxHeight);
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/tablet/TabletStatusBar.java b/packages/SystemUI/src/com/android/systemui/statusbar/tablet/TabletStatusBar.java
index 5ab9919..dba1606 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/tablet/TabletStatusBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/tablet/TabletStatusBar.java
@@ -1620,8 +1620,9 @@
     }
 
     @Override
-    protected boolean isStatusBarExpanded() {
-        return mNotificationPanel.getVisibility() == View.VISIBLE;
+    protected boolean shouldDisableNavbarGestures() {
+        return mNotificationPanel.getVisibility() == View.VISIBLE
+                || (mDisabled & StatusBarManager.DISABLE_HOME) != 0;
     }
 }
 
diff --git a/tests/HwAccelerationTest/src/com/android/test/hwui/BitmapMutateActivity.java b/tests/HwAccelerationTest/src/com/android/test/hwui/BitmapMutateActivity.java
index db9017c..0d825d7 100644
--- a/tests/HwAccelerationTest/src/com/android/test/hwui/BitmapMutateActivity.java
+++ b/tests/HwAccelerationTest/src/com/android/test/hwui/BitmapMutateActivity.java
@@ -87,7 +87,7 @@
             int width = mBitmap1.getWidth();
             int height = mBitmap1.getHeight();
 
-            canvas.translate((getWidth() - width) / 2, (getHeight() - height) / 2);
+            canvas.translate((getWidth() - width) / 2, 0);
 
             for (int x = 0; x < width; x++) {
                 int color = 0xff000000;
@@ -101,6 +101,11 @@
 
             mBitmap1.setPixels(mPixels, 0, width, 0, 0, width, height);
             canvas.drawBitmap(mBitmap1, 0.0f, 0.0f, mBitmapPaint);
+
+            canvas.save();
+            canvas.translate(0.0f, height + 32);
+            canvas.drawBitmap(mPixels, 0, width, 0.0f, 0.0f, width, height, false, mBitmapPaint);
+            canvas.restore();
         }
     }
 }