diff options
Diffstat (limited to 'java/com/android/contacts/common/list/ContactListItemView.java')
-rw-r--r-- | java/com/android/contacts/common/list/ContactListItemView.java | 216 |
1 files changed, 123 insertions, 93 deletions
diff --git a/java/com/android/contacts/common/list/ContactListItemView.java b/java/com/android/contacts/common/list/ContactListItemView.java index 5a2749178..7a3194720 100644 --- a/java/com/android/contacts/common/list/ContactListItemView.java +++ b/java/com/android/contacts/common/list/ContactListItemView.java @@ -30,8 +30,8 @@ import android.provider.ContactsContract; import android.provider.ContactsContract.Contacts; import android.provider.ContactsContract.SearchSnippets; import android.support.annotation.IntDef; -import android.support.annotation.NonNull; import android.support.v4.content.ContextCompat; +import android.support.v4.graphics.drawable.DrawableCompat; import android.text.Spannable; import android.text.SpannableString; import android.text.TextUtils; @@ -55,6 +55,7 @@ import com.android.contacts.common.format.TextHighlighter; import com.android.contacts.common.list.PhoneNumberListAdapter.Listener; import com.android.contacts.common.util.ContactDisplayUtils; import com.android.contacts.common.util.SearchUtil; +import com.android.dialer.compat.CompatUtils; import com.android.dialer.util.ViewUtil; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -80,15 +81,13 @@ public class ContactListItemView extends ViewGroup implements SelectionBoundsAdj /** IntDef for indices of ViewPager tabs. */ @Retention(RetentionPolicy.SOURCE) - @IntDef({NONE, VIDEO, LIGHTBRINGER, CALL_AND_SHARE}) + @IntDef({NONE, VIDEO, CALL_AND_SHARE}) public @interface CallToAction {} public static final int NONE = 0; public static final int VIDEO = 1; - public static final int LIGHTBRINGER = 2; - public static final int CALL_AND_SHARE = 3; + public static final int CALL_AND_SHARE = 2; - private final PhotoPosition mPhotoPosition = getDefaultPhotoPosition(); private static final Pattern SPLIT_PATTERN = Pattern.compile("([\\w-\\.]+)@((?:[\\w]+\\.)+)([a-zA-Z]{2,4})|[\\w]+"); static final char SNIPPET_START_MATCH = '['; @@ -107,7 +106,7 @@ public class ContactListItemView extends ViewGroup implements SelectionBoundsAdj private int mNameTextViewTextSize; private int mHeaderWidth; private Drawable mActivatedBackgroundDrawable; - private int mCallToActionSize = 48; + private int mCallToActionSize = 32; private int mCallToActionMargin = 16; // Set in onLayout. Represent left and right position of the View on the screen. private int mLeftOffset; @@ -123,7 +122,10 @@ public class ContactListItemView extends ViewGroup implements SelectionBoundsAdj private String mHighlightedPrefix; /** Indicates whether the view should leave room for the "video call" icon. */ private boolean mSupportVideoCall; + /** Indicates whether the view should leave room for the "call and share" icon. */ + private boolean mSupportCallAndShare; + private PhotoPosition mPhotoPosition = getDefaultPhotoPosition(false /* normal/non opposite */); // Header layout data private TextView mHeaderTextView; private boolean mIsSectionHeaderEnabled; @@ -137,7 +139,7 @@ public class ContactListItemView extends ViewGroup implements SelectionBoundsAdj private TextView mSnippetView; private TextView mStatusView; private ImageView mPresenceIcon; - @NonNull private final ImageView mCallToActionView; + private ImageView mCallToAction; private ImageView mWorkProfileIcon; private ColorStateList mSecondaryTextColor; private int mDefaultPhotoViewSize = 0; @@ -179,14 +181,23 @@ public class ContactListItemView extends ViewGroup implements SelectionBoundsAdj private Rect mBoundsWithoutHeader = new Rect(); private CharSequence mUnknownNameText; - private String mPhoneNumber; - private int mPosition = -1; - private @CallToAction int mCallToAction = NONE; + public ContactListItemView(Context context) { + super(context); - public ContactListItemView(Context context, AttributeSet attrs, boolean supportVideoCallIcon) { + mTextHighlighter = new TextHighlighter(Typeface.BOLD); + mNameHighlightSequence = new ArrayList<>(); + mNumberHighlightSequence = new ArrayList<>(); + } + + public ContactListItemView( + Context context, + AttributeSet attrs, + boolean supportVideoCallIcon, + boolean supportCallAndShare) { this(context, attrs); mSupportVideoCall = supportVideoCallIcon; + mSupportCallAndShare = supportCallAndShare; } public ContactListItemView(Context context, AttributeSet attrs) { @@ -273,20 +284,19 @@ public class ContactListItemView extends ViewGroup implements SelectionBoundsAdj mNameHighlightSequence = new ArrayList<>(); mNumberHighlightSequence = new ArrayList<>(); - mCallToActionView = new ImageView(getContext()); - mCallToActionView.setId(R.id.call_to_action); - mCallToActionView.setLayoutParams(new LayoutParams(mCallToActionSize, mCallToActionSize)); - mCallToActionView.setScaleType(ScaleType.CENTER); - mCallToActionView.setImageTintList( - ContextCompat.getColorStateList(getContext(), R.color.search_video_call_icon_tint)); - addView(mCallToActionView); - setLayoutDirection(View.LAYOUT_DIRECTION_LOCALE); } - public static PhotoPosition getDefaultPhotoPosition() { - int layoutDirection = TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()); - return layoutDirection == View.LAYOUT_DIRECTION_RTL ? PhotoPosition.RIGHT : PhotoPosition.LEFT; + public static PhotoPosition getDefaultPhotoPosition(boolean opposite) { + final Locale locale = Locale.getDefault(); + final int layoutDirection = TextUtils.getLayoutDirectionFromLocale(locale); + switch (layoutDirection) { + case View.LAYOUT_DIRECTION_RTL: + return (opposite ? PhotoPosition.LEFT : PhotoPosition.RIGHT); + case View.LAYOUT_DIRECTION_LTR: + default: + return (opposite ? PhotoPosition.RIGHT : PhotoPosition.LEFT); + } } /** @@ -323,47 +333,35 @@ public class ContactListItemView extends ViewGroup implements SelectionBoundsAdj * @param position The position in the adapter of the call to action. */ public void setCallToAction(@CallToAction int action, Listener listener, int position) { - mCallToAction = action; - mPosition = position; - - Drawable drawable; + int drawable; int description; OnClickListener onClickListener; - if (action == CALL_AND_SHARE) { - drawable = getContext().getResources().getDrawable(R.drawable.ic_phone_attach); - drawable.setAutoMirrored(true); - description = R.string.description_search_call_and_share; + if (action == CALL_AND_SHARE && mSupportCallAndShare) { + drawable = R.drawable.ic_call_and_share; + description = R.string.description_search_video_call; onClickListener = v -> listener.onCallAndShareIconClicked(position); } else if (action == VIDEO && mSupportVideoCall) { - drawable = - getContext().getResources().getDrawable(R.drawable.quantum_ic_videocam_vd_theme_24); - drawable.setAutoMirrored(true); - description = R.string.description_search_video_call; + drawable = R.drawable.ic_search_video_call; + description = R.string.description_search_call_and_share; onClickListener = v -> listener.onVideoCallIconClicked(position); - } else if (action == LIGHTBRINGER) { - drawable = - getContext().getResources().getDrawable(R.drawable.quantum_ic_videocam_vd_theme_24); - drawable.setAutoMirrored(true); - description = R.string.description_search_video_call; - onClickListener = v -> listener.onLightbringerIconClicked(position); } else { - mCallToActionView.setVisibility(View.GONE); - mCallToActionView.setOnClickListener(null); + if (mCallToAction != null) { + mCallToAction.setVisibility(View.GONE); + mCallToAction.setOnClickListener(null); + } return; } - mCallToActionView.setContentDescription(getContext().getString(description)); - mCallToActionView.setOnClickListener(onClickListener); - mCallToActionView.setImageDrawable(drawable); - mCallToActionView.setVisibility(View.VISIBLE); - } - - public @CallToAction int getCallToAction() { - return mCallToAction; - } - - public int getPosition() { - return mPosition; + if (mCallToAction == null) { + mCallToAction = new ImageView(getContext()); + mCallToAction.setLayoutParams(new LayoutParams(mCallToActionSize, mCallToActionSize)); + mCallToAction.setScaleType(ScaleType.CENTER); + addView(mCallToAction); + } + mCallToAction.setContentDescription(getContext().getString(description)); + mCallToAction.setOnClickListener(onClickListener); + mCallToAction.setImageResource(drawable); + mCallToAction.setVisibility(View.VISIBLE); } /** @@ -378,6 +376,18 @@ public class ContactListItemView extends ViewGroup implements SelectionBoundsAdj mSupportVideoCall = supportVideoCall; } + /** + * Sets whether the view supports a call and share icon. This is independent of whether the view + * is actually showing an icon. Support for the icon ensures that the layout leaves space for it, + * should it be shown. + * + * @param supportCallAndShare {@code true} if the call and share icon is supported, {@code false} + * otherwise. + */ + public void setSupportCallAndShareIcon(boolean supportCallAndShare) { + mSupportCallAndShare = supportCallAndShare; + } + @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // We will match parent's width and wrap content vertically, but make sure @@ -413,7 +423,9 @@ public class ContactListItemView extends ViewGroup implements SelectionBoundsAdj effectiveWidth -= mHeaderWidth + mGapBetweenImageAndText; } - effectiveWidth -= (mCallToActionSize + mCallToActionMargin); + if (mSupportVideoCall || mSupportCallAndShare) { + effectiveWidth -= (mCallToActionSize + mCallToActionMargin); + } // Go over all visible text views and measure actual width of each of them. // Also calculate their heights to get the total height for this entire view. @@ -484,9 +496,11 @@ public class ContactListItemView extends ViewGroup implements SelectionBoundsAdj mStatusTextViewHeight = mPresenceIcon.getMeasuredHeight(); } - mCallToActionView.measure( - MeasureSpec.makeMeasureSpec(mCallToActionSize, MeasureSpec.EXACTLY), - MeasureSpec.makeMeasureSpec(mCallToActionSize, MeasureSpec.EXACTLY)); + if ((mSupportVideoCall || mSupportCallAndShare) && isVisible(mCallToAction)) { + mCallToAction.measure( + MeasureSpec.makeMeasureSpec(mCallToActionSize, MeasureSpec.EXACTLY), + MeasureSpec.makeMeasureSpec(mCallToActionSize, MeasureSpec.EXACTLY)); + } if (isVisible(mWorkProfileIcon)) { mWorkProfileIcon.measure( @@ -615,30 +629,34 @@ public class ContactListItemView extends ViewGroup implements SelectionBoundsAdj leftBound += mTextIndent; } - // Place the call to action at the end of the list (e.g. take into account RTL mode). - // Center the icon vertically - final int callToActionTop = topBound + (height - topBound - mCallToActionSize) / 2; + if (mSupportVideoCall || mSupportCallAndShare) { + // Place the call to action at the end of the list (e.g. take into account RTL mode). + if (isVisible(mCallToAction)) { + // Center the icon vertically + final int callToActionTop = topBound + (height - topBound - mCallToActionSize) / 2; - if (!isLayoutRtl) { - // When photo is on left, icon is placed on the right edge. - mCallToActionView.layout( - rightBound - mCallToActionSize, - callToActionTop, - rightBound, - callToActionTop + mCallToActionSize); - } else { - // When photo is on right, icon is placed on the left edge. - mCallToActionView.layout( - leftBound, - callToActionTop, - leftBound + mCallToActionSize, - callToActionTop + mCallToActionSize); - } + if (!isLayoutRtl) { + // When photo is on left, icon is placed on the right edge. + mCallToAction.layout( + rightBound - mCallToActionSize, + callToActionTop, + rightBound, + callToActionTop + mCallToActionSize); + } else { + // When photo is on right, icon is placed on the left edge. + mCallToAction.layout( + leftBound, + callToActionTop, + leftBound + mCallToActionSize, + callToActionTop + mCallToActionSize); + } + } - if (mPhotoPosition == PhotoPosition.LEFT) { - rightBound -= (mCallToActionSize + mCallToActionMargin); - } else { - leftBound += mCallToActionSize + mCallToActionMargin; + if (mPhotoPosition == PhotoPosition.LEFT) { + rightBound -= (mCallToActionSize + mCallToActionMargin); + } else { + leftBound += mCallToActionSize + mCallToActionMargin; + } } // Center text vertically, then apply the top offset. @@ -883,7 +901,9 @@ public class ContactListItemView extends ViewGroup implements SelectionBoundsAdj } if (mQuickContact == null) { mQuickContact = new QuickContactBadge(getContext()); - mQuickContact.setOverlay(null); + if (CompatUtils.isLollipopCompatible()) { + mQuickContact.setOverlay(null); + } mQuickContact.setLayoutParams(getDefaultPhotoLayoutParams()); if (mNameTextView != null) { mQuickContact.setContentDescription( @@ -988,7 +1008,9 @@ public class ContactListItemView extends ViewGroup implements SelectionBoundsAdj mNameTextView.setGravity(Gravity.CENTER_VERTICAL); mNameTextView.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START); mNameTextView.setId(R.id.cliv_name_textview); - mNameTextView.setElegantTextHeight(false); + if (CompatUtils.isLollipopCompatible()) { + mNameTextView.setElegantTextHeight(false); + } addView(mNameTextView); } return mNameTextView; @@ -1034,7 +1056,6 @@ public class ContactListItemView extends ViewGroup implements SelectionBoundsAdj * exists. */ public void setPhoneNumber(String text) { - mPhoneNumber = text; if (text == null) { if (mDataView != null) { mDataView.setVisibility(View.GONE); @@ -1065,10 +1086,6 @@ public class ContactListItemView extends ViewGroup implements SelectionBoundsAdj } } - public String getPhoneNumber() { - return mPhoneNumber; - } - private void setMarqueeText(TextView textView, CharSequence text) { if (getTextEllipsis() == TruncateAt.MARQUEE) { // To show MARQUEE correctly (with END effect during non-active state), we need @@ -1092,7 +1109,9 @@ public class ContactListItemView extends ViewGroup implements SelectionBoundsAdj mDataView.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START); mDataView.setActivated(isActivated()); mDataView.setId(R.id.cliv_data_view); - mDataView.setElegantTextHeight(false); + if (CompatUtils.isLollipopCompatible()) { + mDataView.setElegantTextHeight(false); + } addView(mDataView); } return mDataView; @@ -1450,17 +1469,28 @@ public class ContactListItemView extends ViewGroup implements SelectionBoundsAdj forceLayout(); } + public void setPhotoPosition(PhotoPosition photoPosition) { + mPhotoPosition = photoPosition; + } + /** * Set drawable resources directly for the drawable resource of the photo view. * - * @param drawable A drawable resource. + * @param drawableId Id of drawable resource. */ - public void setDrawable(Drawable drawable) { + public void setDrawableResource(int drawableId) { ImageView photo = getPhotoView(); photo.setScaleType(ImageView.ScaleType.CENTER); - int iconColor = ContextCompat.getColor(getContext(), R.color.search_shortcut_icon_color); - photo.setImageDrawable(drawable); - photo.setImageTintList(ColorStateList.valueOf(iconColor)); + final Drawable drawable = ContextCompat.getDrawable(getContext(), drawableId); + final int iconColor = ContextCompat.getColor(getContext(), R.color.search_shortcut_icon_color); + if (CompatUtils.isLollipopCompatible()) { + photo.setImageDrawable(drawable); + photo.setImageTintList(ColorStateList.valueOf(iconColor)); + } else { + final Drawable drawableWrapper = DrawableCompat.wrap(drawable).mutate(); + DrawableCompat.setTint(drawableWrapper, iconColor); + photo.setImageDrawable(drawableWrapper); + } } @Override |