more useful contact select headers

Closes #4608
// FREEBIE
This commit is contained in:
Jake McGinty 2015-11-16 16:57:51 -08:00 committed by Moxie Marlinspike
parent 4314a4b42b
commit 61386e9ca9
11 changed files with 34 additions and 72 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.2 KiB

View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<bitmap android:src="@drawable/ic_badge_24dp"
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="bottom|right"
android:tileMode="disabled" />

View File

@ -18,7 +18,6 @@
android:layout_height="40dp"
android:foreground="@drawable/contact_photo_background"
android:cropToPadding="true"
app:showBadge="true"
tools:src="@color/md_material_blue_600"
android:layout_marginRight="10dp"
android:contentDescription="@string/SingleContactSelectionActivity_contact_photo" />

View File

@ -132,7 +132,6 @@
<declare-styleable name="AvatarImageView">
<attr name="inverted" format="boolean" />
<attr name="showBadge" format="boolean" />
</declare-styleable>
<declare-styleable name="ThumbnailView">

View File

@ -3,13 +3,8 @@ package org.thoughtcrime.securesms.components;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.AsyncTask;
import android.provider.ContactsContract;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.util.Pair;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
@ -21,13 +16,10 @@ import org.thoughtcrime.securesms.contacts.avatars.ContactPhotoFactory;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.RecipientFactory;
import org.thoughtcrime.securesms.recipients.Recipients;
import org.thoughtcrime.securesms.util.DirectoryHelper;
import org.thoughtcrime.securesms.util.DirectoryHelper.UserCapabilities.Capability;
public class AvatarImageView extends ImageView {
private boolean inverted;
private boolean showBadge;
public AvatarImageView(Context context) {
super(context);
@ -41,7 +33,6 @@ public class AvatarImageView extends ImageView {
if (attrs != null) {
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.AvatarImageView, 0, 0);
inverted = typedArray.getBoolean(0, false);
showBadge = typedArray.getBoolean(1, false);
typedArray.recycle();
}
}
@ -51,12 +42,9 @@ public class AvatarImageView extends ImageView {
MaterialColor backgroundColor = recipients.getColor();
setImageDrawable(recipients.getContactPhoto().asDrawable(getContext(), backgroundColor.toConversationColor(getContext()), inverted));
setAvatarClickHandler(recipients, quickContactEnabled);
setTag(recipients);
if (showBadge) new BadgeResolutionTask(getContext()).execute(recipients);
} else {
setImageDrawable(ContactPhotoFactory.getDefaultContactPhoto(null).asDrawable(getContext(), ContactColors.UNKNOWN_COLOR.toConversationColor(getContext()), inverted));
setOnClickListener(null);
setTag(null);
}
}
@ -85,30 +73,4 @@ public class AvatarImageView extends ImageView {
setOnClickListener(null);
}
}
private class BadgeResolutionTask extends AsyncTask<Recipients,Void,Pair<Recipients, Boolean>> {
private final Context context;
public BadgeResolutionTask(Context context) {
this.context = context;
}
@Override
protected Pair<Recipients, Boolean> doInBackground(Recipients... recipients) {
Capability textCapability = DirectoryHelper.getUserCapabilities(context, recipients[0]).getTextCapability();
return new Pair<>(recipients[0], textCapability == Capability.SUPPORTED);
}
@Override
protected void onPostExecute(Pair<Recipients, Boolean> result) {
if (getTag() == result.first && result.second) {
final Drawable badged = new LayerDrawable(new Drawable[] {
getDrawable(),
ContextCompat.getDrawable(context, R.drawable.badge_drawable)
});
setImageDrawable(badged);
}
}
}
}

View File

@ -25,7 +25,6 @@ import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build.VERSION;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
@ -57,8 +56,8 @@ public class RecyclerViewFastScroller extends LinearLayout {
return;
final int verticalScrollOffset = recyclerView.computeVerticalScrollOffset();
final int verticalScrollRange = recyclerView.computeVerticalScrollRange();
float proportion = (float)verticalScrollOffset / ((float)verticalScrollRange - height);
setBubbleAndHandlePosition(height * proportion);
final float proportion = (float)verticalScrollOffset / verticalScrollRange;
setBubbleAndHandlePosition(proportion);
}
};
@ -106,7 +105,7 @@ public class RecyclerViewFastScroller extends LinearLayout {
handle.setSelected(true);
case MotionEvent.ACTION_MOVE:
final float y = event.getY();
setBubbleAndHandlePosition(y);
setBubbleAndHandlePosition(y / height);
setRecyclerViewPosition(y);
return true;
case MotionEvent.ACTION_UP:
@ -147,12 +146,14 @@ public class RecyclerViewFastScroller extends LinearLayout {
if (recyclerView != null) {
final int itemCount = recyclerView.getAdapter().getItemCount();
float proportion;
if (ViewUtil.getY(handle) == 0)
if (ViewUtil.getY(handle) == 0) {
proportion = 0f;
else if (ViewUtil.getY(handle) + handle.getHeight() >= height - TRACK_SNAP_RANGE)
} else if (ViewUtil.getY(handle) + handle.getHeight() >= height - TRACK_SNAP_RANGE) {
proportion = 1f;
else
proportion = y / (float) height;
} else {
proportion = y / (float)height;
}
final int targetPos = Util.clamp((int)(proportion * (float)itemCount), 0, itemCount - 1);
((LinearLayoutManager) recyclerView.getLayoutManager()).scrollToPositionWithOffset(targetPos, 0);
final CharSequence bubbleText = ((FastScrollAdapter) recyclerView.getAdapter()).getBubbleText(targetPos);
@ -164,8 +165,11 @@ public class RecyclerViewFastScroller extends LinearLayout {
private void setBubbleAndHandlePosition(float y) {
final int handleHeight = handle.getHeight();
final int bubbleHeight = bubble.getHeight();
ViewUtil.setY(handle, Util.clamp((int)(y - handleHeight / 2), 0, height - handleHeight));
ViewUtil.setY(bubble, Util.clamp((int)(y - bubbleHeight), 0, height - bubbleHeight - handleHeight / 2));
final int handleY = Util.clamp((int)((height - handleHeight) * y), 0, height - handleHeight);
ViewUtil.setY(handle, handleY);
ViewUtil.setY(bubble, Util.clamp(handleY - bubbleHeight - bubble.getPaddingBottom() + handleHeight,
0,
height - bubbleHeight));
}
@TargetApi(11)

View File

@ -20,14 +20,13 @@ import android.content.Context;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ImageSpan;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
@ -40,6 +39,7 @@ import org.thoughtcrime.securesms.ContactSelectionListFragment.StickyHeaderAdapt
import org.thoughtcrime.securesms.contacts.ContactSelectionListAdapter.HeaderViewHolder;
import org.thoughtcrime.securesms.contacts.ContactSelectionListAdapter.ViewHolder;
import org.thoughtcrime.securesms.database.CursorRecyclerViewAdapter;
import org.thoughtcrime.securesms.util.Util;
import java.util.HashMap;
import java.util.Map;
@ -103,7 +103,7 @@ public class ContactSelectionListAdapter extends CursorRecyclerViewAdapter<ViewH
@Override
public long getHeaderId(int i) {
return getHeaderString(i).hashCode();
return Util.hashCode(getHeaderString(i), isPush(i));
}
@Override
@ -137,36 +137,33 @@ public class ContactSelectionListAdapter extends CursorRecyclerViewAdapter<ViewH
@Override
public void onBindHeaderViewHolder(HeaderViewHolder viewHolder, int position) {
((TextView)viewHolder.itemView).setText(getSpannedHeaderString(position, R.drawable.ic_signal_grey_24dp));
((TextView)viewHolder.itemView).setText(getSpannedHeaderString(position));
}
@Override
public CharSequence getBubbleText(int position) {
return getSpannedHeaderString(position, R.drawable.ic_signal_white_48dp);
return getHeaderString(position);
}
public Map<Long, String> getSelectedContacts() {
return selectedContacts;
}
private CharSequence getSpannedHeaderString(int position, @DrawableRes int drawable) {
Cursor cursor = getCursorAtPositionOrThrow(position);
if (cursor.getInt(cursor.getColumnIndexOrThrow(ContactsDatabase.CONTACT_TYPE_COLUMN)) == ContactsDatabase.PUSH_TYPE) {
SpannableString spannable = new SpannableString(" ");
spannable.setSpan(new ImageSpan(getContext(), drawable, ImageSpan.ALIGN_BOTTOM), 0, spannable.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
private CharSequence getSpannedHeaderString(int position) {
final String headerString = getHeaderString(position);
if (isPush(position)) {
SpannableString spannable = new SpannableString(headerString);
spannable.setSpan(new ForegroundColorSpan(getContext().getResources().getColor(R.color.signal_primary)), 0, headerString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return spannable;
} else {
return getHeaderString(position);
return headerString;
}
}
private @NonNull String getHeaderString(int position) {
Cursor cursor = getCursorAtPositionOrThrow(position);
String letter = cursor.getString(cursor.getColumnIndexOrThrow(ContactsDatabase.NAME_COLUMN));
if (cursor.getInt(cursor.getColumnIndexOrThrow(ContactsDatabase.CONTACT_TYPE_COLUMN)) == ContactsDatabase.PUSH_TYPE) {
return getContext().getString(R.string.app_name);
} else if (!TextUtils.isEmpty(letter)) {
if (!TextUtils.isEmpty(letter)) {
String firstChar = letter.trim().substring(0, 1).toUpperCase();
if (Character.isLetterOrDigit(firstChar.codePointAt(0))) {
return firstChar;
@ -176,12 +173,19 @@ public class ContactSelectionListAdapter extends CursorRecyclerViewAdapter<ViewH
return "#";
}
private boolean isPush(int position) {
final Cursor cursor = getCursorAtPositionOrThrow(position);
return cursor.getInt(cursor.getColumnIndexOrThrow(ContactsDatabase.CONTACT_TYPE_COLUMN)) == ContactsDatabase.PUSH_TYPE;
}
private Cursor getCursorAtPositionOrThrow(int position) {
Cursor cursor = getCursor();
if (cursor == null) {
throw new IllegalStateException("Cursor should not be null here.");
}
if (!cursor.moveToPosition(position));
if (!cursor.moveToPosition(position)) {
throw new IllegalStateException("Cursor couldn't be moved to position.");
}
return cursor;
}