Merge "MTP: Disable MTP when the keyguard is locked and secure"
diff --git a/api/current.xml b/api/current.xml
index 480ad93..c4fc0e4 100644
--- a/api/current.xml
+++ b/api/current.xml
@@ -9937,6 +9937,17 @@
  visibility="public"
 >
 </field>
+<field name="textCursorDrawable"
+ type="int"
+ transient="false"
+ volatile="false"
+ value="16843618"
+ static="true"
+ final="true"
+ deprecated="not deprecated"
+ visibility="public"
+>
+</field>
 <field name="textEditNoPasteWindowLayout"
  type="int"
  transient="false"
@@ -266626,7 +266637,7 @@
  deprecated="not deprecated"
  visibility="public"
 >
-<parameter name="arg0" type="T">
+<parameter name="t" type="T">
 </parameter>
 </method>
 </interface>
diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java
index 44fd1c4..c4d95b2 100644
--- a/core/java/android/widget/TextView.java
+++ b/core/java/android/widget/TextView.java
@@ -3997,7 +3997,7 @@
         }
 
         if (mBlink != null) {
-            mBlink.cancel();
+            mBlink.removeCallbacks(mBlink);
         }
 
         if (mInsertionPointCursorController != null) {
@@ -5421,18 +5421,13 @@
         invalidate();
         int curs = getSelectionStart();
 
-        if (curs >= 0 || (mGravity & Gravity.VERTICAL_GRAVITY_MASK) ==
-                             Gravity.BOTTOM) {
+        if (curs >= 0 || (mGravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) {
             registerForPreDraw();
         }
 
         if (curs >= 0) {
             mHighlightPathBogus = true;
-
-            if (isFocused()) {
-                mShowCursor = SystemClock.uptimeMillis();
-                makeBlink();
-            }
+            makeBlink();
         }
 
         checkForResize();
@@ -6589,13 +6584,15 @@
      */
     @android.view.RemotableViewMethod
     public void setCursorVisible(boolean visible) {
-        mCursorVisible = visible;
-        invalidate();
+        if (mCursorVisible != visible) {
+            mCursorVisible = visible;
+            invalidate();
 
-        makeBlink();
+            makeBlink();
 
-        // InsertionPointCursorController depends on mCursorVisible
-        prepareCursorControllers();
+            // InsertionPointCursorController depends on mCursorVisible
+            prepareCursorControllers();
+        }
     }
 
     private boolean isCursorVisible() {
@@ -6934,11 +6931,7 @@
             if (oldStart >= 0 || newStart >= 0) {
                 invalidateCursor(Selection.getSelectionStart(buf), oldStart, newStart);
                 registerForPreDraw();
-
-                if (isFocused()) {
-                    mShowCursor = SystemClock.uptimeMillis();
-                    makeBlink();
-                }
+                makeBlink();
             }
         }
 
@@ -7089,22 +7082,6 @@
         }
     }
 
-    private void makeBlink() {
-        if (!isCursorVisible()) {
-            if (mBlink != null) {
-                mBlink.removeCallbacks(mBlink);
-            }
-
-            return;
-        }
-
-        if (mBlink == null)
-            mBlink = new Blink(this);
-
-        mBlink.removeCallbacks(mBlink);
-        mBlink.postAtTime(mBlink, mShowCursor + BLINK);
-    }
-
     /**
      * @hide
      */
@@ -7271,11 +7248,7 @@
         if (hasWindowFocus) {
             if (mBlink != null) {
                 mBlink.uncancel();
-
-                if (isFocused()) {
-                    mShowCursor = SystemClock.uptimeMillis();
-                    makeBlink();
-                }
+                makeBlink();
             }
         } else {
             if (mBlink != null) {
@@ -7512,17 +7485,12 @@
 
             TextView tv = mView.get();
 
-            if (tv != null && tv.isFocused()) {
-                int st = tv.getSelectionStart();
-                int en = tv.getSelectionEnd();
-
-                if (st == en && st >= 0 && en >= 0) {
-                    if (tv.mLayout != null) {
-                        tv.invalidateCursorPath();
-                    }
-
-                    postAtTime(this, SystemClock.uptimeMillis() + BLINK);
+            if (tv != null && tv.shouldBlink()) {
+                if (tv.mLayout != null) {
+                    tv.invalidateCursorPath();
                 }
+
+                postAtTime(this, SystemClock.uptimeMillis() + BLINK);
             }
         }
 
@@ -7538,6 +7506,34 @@
         }
     }
 
+    /**
+     * @return True when the TextView isFocused and has a valid zero-length selection (cursor).
+     */
+    private boolean shouldBlink() {
+        if (!isFocused()) return false;
+
+        final int start = getSelectionStart();
+        if (start < 0) return false;
+
+        final int end = getSelectionEnd();
+        if (end < 0) return false;
+
+        return start == end;
+    }
+
+    private void makeBlink() {
+        if (isCursorVisible()) {
+            if (shouldBlink()) {
+                mShowCursor = SystemClock.uptimeMillis();
+                if (mBlink == null) mBlink = new Blink(this);
+                mBlink.removeCallbacks(mBlink);
+                mBlink.postAtTime(mBlink, mShowCursor + BLINK);
+            }
+        } else {
+            if (mBlink != null) mBlink.removeCallbacks(mBlink);
+        }
+    }
+
     @Override
     protected float getLeftFadingEdgeStrength() {
         if (mCurrentAlpha <= ViewConfiguration.ALPHA_THRESHOLD_INT) return 0.0f;
@@ -8682,7 +8678,8 @@
 
         // Touch-up filter: number of previous positions remembered
         private static final int HISTORY_SIZE = 5;
-        private static final int TOUCH_UP_FILTER_DELAY = 150;
+        private static final int TOUCH_UP_FILTER_DELAY_AFTER = 150;
+        private static final int TOUCH_UP_FILTER_DELAY_BEFORE = 350;
         private final long[] mPreviousOffsetsTimes = new long[HISTORY_SIZE];
         private final int[] mPreviousOffsets = new int[HISTORY_SIZE];
         private int mPreviousOffsetIndex = 0;
@@ -8709,15 +8706,17 @@
         public void filterOnTouchUp() {
             final long now = SystemClock.uptimeMillis();
             int i = 0;
-            int index = 0;
+            int index = mPreviousOffsetIndex;
             final int iMax = Math.min(mNumberPreviousOffsets, HISTORY_SIZE);
-            while (i < iMax) {
-                index = (mPreviousOffsetIndex - i + HISTORY_SIZE) % HISTORY_SIZE;
-                if ((now - mPreviousOffsetsTimes[index]) >= TOUCH_UP_FILTER_DELAY) break;
+            while (i < iMax && (now - mPreviousOffsetsTimes[index]) < TOUCH_UP_FILTER_DELAY_AFTER) {
                 i++;
+                index = (mPreviousOffsetIndex - i + HISTORY_SIZE) % HISTORY_SIZE;
             }
 
-            mController.updateOffset(this, mPreviousOffsets[index]);
+            if (i > 0 && i < iMax &&
+                    (now - mPreviousOffsetsTimes[index]) > TOUCH_UP_FILTER_DELAY_BEFORE) {
+                mController.updateOffset(this, mPreviousOffsets[index]);
+            }
         }
 
         public static final int LEFT = 0;
@@ -8739,40 +8738,40 @@
         public void setOrientation(int pos) {
             int handleWidth;
             switch (pos) {
-            case LEFT: {
-                if (mSelectHandleLeft == null) {
-                    mSelectHandleLeft = mContext.getResources().getDrawable(
-                            mTextSelectHandleLeftRes);
+                case LEFT: {
+                    if (mSelectHandleLeft == null) {
+                        mSelectHandleLeft = mContext.getResources().getDrawable(
+                                mTextSelectHandleLeftRes);
+                    }
+                    mDrawable = mSelectHandleLeft;
+                    handleWidth = mDrawable.getIntrinsicWidth();
+                    mHotspotX = handleWidth * 3.0f / 4.0f;
+                    break;
                 }
-                mDrawable = mSelectHandleLeft;
-                handleWidth = mDrawable.getIntrinsicWidth();
-                mHotspotX = handleWidth * 3.0f / 4.0f;
-                break;
-            }
 
-            case RIGHT: {
-                if (mSelectHandleRight == null) {
-                    mSelectHandleRight = mContext.getResources().getDrawable(
-                            mTextSelectHandleRightRes);
+                case RIGHT: {
+                    if (mSelectHandleRight == null) {
+                        mSelectHandleRight = mContext.getResources().getDrawable(
+                                mTextSelectHandleRightRes);
+                    }
+                    mDrawable = mSelectHandleRight;
+                    handleWidth = mDrawable.getIntrinsicWidth();
+                    mHotspotX = handleWidth / 4.0f;
+                    break;
                 }
-                mDrawable = mSelectHandleRight;
-                handleWidth = mDrawable.getIntrinsicWidth();
-                mHotspotX = handleWidth / 4.0f;
-                break;
-            }
 
-            case CENTER:
-            default: {
-                if (mSelectHandleCenter == null) {
-                    mSelectHandleCenter = mContext.getResources().getDrawable(
-                            mTextSelectHandleRes);
+                case CENTER:
+                default: {
+                    if (mSelectHandleCenter == null) {
+                        mSelectHandleCenter = mContext.getResources().getDrawable(
+                                mTextSelectHandleRes);
+                    }
+                    mDrawable = mSelectHandleCenter;
+                    handleWidth = mDrawable.getIntrinsicWidth();
+                    mHotspotX = handleWidth / 2.0f;
+                    mIsInsertionHandle = true;
+                    break;
                 }
-                mDrawable = mSelectHandleCenter;
-                handleWidth = mDrawable.getIntrinsicWidth();
-                mHotspotX = handleWidth / 2.0f;
-                mIsInsertionHandle = true;
-                break;
-            }
             }
 
             final int handleHeight = mDrawable.getIntrinsicHeight();
@@ -8952,11 +8951,16 @@
                     if (mIsInsertionHandle) {
                         long delay = SystemClock.uptimeMillis() - mTouchTimer;
                         if (delay < ViewConfiguration.getTapTimeout()) {
-                            if (mPastePopupWindow != null && mPastePopupWindow.isShowing()) {
-                                // Tapping on the handle dismisses the displayed paste view,
-                                mPastePopupWindow.hide();
-                            } else {
-                                ((InsertionPointCursorController) mController).show(0);
+                            final float deltaX = mDownPositionX - ev.getRawX();
+                            final float deltaY = mDownPositionY - ev.getRawY();
+                            final float distanceSquared = deltaX * deltaX + deltaY * deltaY;
+                            if (distanceSquared < mSquaredTouchSlopDistance) {
+                                if (mPastePopupWindow != null && mPastePopupWindow.isShowing()) {
+                                    // Tapping on the handle dismisses the displayed paste view,
+                                    mPastePopupWindow.hide();
+                                } else {
+                                    ((InsertionPointCursorController) mController).show(0);
+                                }
                             }
                         }
                     }
diff --git a/core/res/res/values/public.xml b/core/res/res/values/public.xml
index 4542575..c7f082b 100644
--- a/core/res/res/values/public.xml
+++ b/core/res/res/values/public.xml
@@ -1642,7 +1642,9 @@
   <!-- Default icon for applications that don't specify an icon. -->
   <public type="mipmap" name="sym_def_app_icon" id="0x010d0000" />
 
-  <!--  Theme attribute used to customize the text insertion cursor -->
-  <!--  Commented out for HC MR1 to prevent an API change -->
-  <!--  <public type="attr" name="textCursorDrawable" id="0x01010362" /> -->
+<!-- ===============================================================
+     Resources added in version 12 of the platform (Honeycomb / 3.1)
+     =============================================================== -->
+  <eat-comment />
+  <public type="attr" name="textCursorDrawable" id="0x01010362" />  
 </resources>