From bc742af5bb568ed39dc37764587120928d068330 Mon Sep 17 00:00:00 2001 From: Scott Kennedy Date: Fri, 2 Aug 2013 21:56:47 -0700 Subject: Make Chips work on API 11+ By using the support library, and copying a few methods from TextView into RecipientEditTextView, we can support chips back to API 11. Bug: 8744878 Change-Id: I2d9bb43e71556504e929dcdccff9dae347020f28 --- chips/Android.mk | 3 +- chips/AndroidManifest.xml | 7 ++- .../com/android/ex/chips/BaseRecipientAdapter.java | 2 +- .../android/ex/chips/RecipientEditTextView.java | 51 +++++++++++++++++++++- 4 files changed, 58 insertions(+), 5 deletions(-) diff --git a/chips/Android.mk b/chips/Android.mk index 6aa8a01..ef4b8ab 100644 --- a/chips/Android.mk +++ b/chips/Android.mk @@ -16,6 +16,7 @@ LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := android-common-chips +LOCAL_STATIC_JAVA_LIBRARIES += android-support-v4 LOCAL_SDK_VERSION := 14 LOCAL_SRC_FILES := \ $(call all-java-files-under, src) \ @@ -26,4 +27,4 @@ include $(BUILD_STATIC_JAVA_LIBRARY) ################################################## # Build all sub-directories -include $(call all-makefiles-under,$(LOCAL_PATH)) \ No newline at end of file +include $(call all-makefiles-under,$(LOCAL_PATH)) diff --git a/chips/AndroidManifest.xml b/chips/AndroidManifest.xml index fd7775d..7487c18 100644 --- a/chips/AndroidManifest.xml +++ b/chips/AndroidManifest.xml @@ -16,4 +16,9 @@ - + + + + \ No newline at end of file diff --git a/chips/src/com/android/ex/chips/BaseRecipientAdapter.java b/chips/src/com/android/ex/chips/BaseRecipientAdapter.java index a9aee46..0230c86 100644 --- a/chips/src/com/android/ex/chips/BaseRecipientAdapter.java +++ b/chips/src/com/android/ex/chips/BaseRecipientAdapter.java @@ -32,10 +32,10 @@ import android.os.Message; import android.provider.ContactsContract; import android.provider.ContactsContract.CommonDataKinds.Photo; import android.provider.ContactsContract.Directory; +import android.support.v4.util.LruCache; import android.text.TextUtils; import android.text.util.Rfc822Token; import android.util.Log; -import android.util.LruCache; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; diff --git a/chips/src/com/android/ex/chips/RecipientEditTextView.java b/chips/src/com/android/ex/chips/RecipientEditTextView.java index ff1e720..fefd273 100644 --- a/chips/src/com/android/ex/chips/RecipientEditTextView.java +++ b/chips/src/com/android/ex/chips/RecipientEditTextView.java @@ -48,6 +48,7 @@ import android.graphics.RectF; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.AsyncTask; +import android.os.Build; import android.os.Handler; import android.os.Looper; import android.os.Message; @@ -1419,7 +1420,7 @@ public class RecipientEditTextView extends MultiAutoCompleteTextView implements if (mCopyAddress == null && action == MotionEvent.ACTION_UP) { float x = event.getX(); float y = event.getY(); - int offset = putOffsetInRange(getOffsetForPosition(x, y)); + int offset = putOffsetInRange(x, y); DrawableRecipientChip currentChip = findChip(offset); if (currentChip != null) { if (action == MotionEvent.ACTION_UP) { @@ -1513,6 +1514,18 @@ public class RecipientEditTextView extends MultiAutoCompleteTextView implements mCheckedItem = position; } + private int putOffsetInRange(final float x, final float y) { + final int offset; + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { + offset = getOffsetForPosition(x, y); + } else { + offset = supportGetOffsetForPosition(x, y); + } + + return putOffsetInRange(offset); + } + // TODO: This algorithm will need a lot of tweaking after more people have used // the chips ui. This attempts to be "forgiving" to fat finger touches by favoring // what comes before the finger. @@ -2691,7 +2704,7 @@ public class RecipientEditTextView extends MultiAutoCompleteTextView implements } float x = event.getX(); float y = event.getY(); - int offset = putOffsetInRange(getOffsetForPosition(x, y)); + final int offset = putOffsetInRange(x, y); DrawableRecipientChip currentChip = findChip(offset); if (currentChip != null) { if (mDragEnabled) { @@ -2704,6 +2717,40 @@ public class RecipientEditTextView extends MultiAutoCompleteTextView implements } } + // The following methods are used to provide some functionality on older versions of Android + // These methods were copied out of JB MR2's TextView + ///////////////////////////////////////////////// + private int supportGetOffsetForPosition(float x, float y) { + if (getLayout() == null) return -1; + final int line = supportGetLineAtCoordinate(y); + final int offset = supportGetOffsetAtCoordinate(line, x); + return offset; + } + + private float supportConvertToLocalHorizontalCoordinate(float x) { + x -= getTotalPaddingLeft(); + // Clamp the position to inside of the view. + x = Math.max(0.0f, x); + x = Math.min(getWidth() - getTotalPaddingRight() - 1, x); + x += getScrollX(); + return x; + } + + private int supportGetLineAtCoordinate(float y) { + y -= getTotalPaddingLeft(); + // Clamp the position to inside of the view. + y = Math.max(0.0f, y); + y = Math.min(getHeight() - getTotalPaddingBottom() - 1, y); + y += getScrollY(); + return getLayout().getLineForVertical((int) y); + } + + private int supportGetOffsetAtCoordinate(int line, float x) { + x = supportConvertToLocalHorizontalCoordinate(x); + return getLayout().getOffsetForHorizontal(line, x); + } + ///////////////////////////////////////////////// + /** * Enables drag-and-drop for chips. */ -- cgit v1.2.3