Fix adaptive shortcut icon shapes.

This commit is contained in:
Alex Hart 2021-06-30 13:05:13 -03:00
parent b85c5eb54a
commit e20d6b63cf
3 changed files with 39 additions and 11 deletions

View file

@ -1,12 +1,16 @@
package org.thoughtcrime.securesms.contacts.avatars;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.LayerDrawable;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import org.thoughtcrime.securesms.R;
@ -41,7 +45,16 @@ public final class FallbackPhoto80dp implements FallbackContactPhoto {
@Override
public Drawable asCallCard(Context context) {
throw new UnsupportedOperationException();
Drawable background = new ColorDrawable(backgroundColor);
Drawable foreground = AppCompatResources.getDrawable(context, drawable80dp);
int transparent20 = ContextCompat.getColor(context, R.color.signal_transparent_20);
Drawable gradient = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[]{ Color.TRANSPARENT, transparent20 });
LayerDrawable drawable = new LayerDrawable(new Drawable[]{background, foreground, gradient});
int foregroundInset = ViewUtil.dpToPx(24);
drawable.setLayerInset(1, foregroundInset, foregroundInset, foregroundInset, foregroundInset);
return drawable;
}
private @NonNull Drawable buildDrawable(@NonNull Context context) {

View file

@ -36,6 +36,13 @@ import java.util.concurrent.ExecutionException;
public final class ConversationShortcutPhoto implements Key {
/**
* Version integer to update whenever business logic changes in this class (such as
* design tweaks or bug fixes). This way, the old photos will be considered invalid
* in the cache.
*/
private static final long VERSION = 1L;
private final Recipient recipient;
private final String avatarObject;
@ -43,7 +50,6 @@ public final class ConversationShortcutPhoto implements Key {
public ConversationShortcutPhoto(@NonNull Recipient recipient) {
this.recipient = recipient.resolve();
this.avatarObject = Util.firstNonNull(recipient.getProfileAvatar(), "");
}
@Override
@ -52,6 +58,7 @@ public final class ConversationShortcutPhoto implements Key {
messageDigest.update(avatarObject.getBytes());
messageDigest.update(isSystemContactPhoto() ? (byte) 1 : (byte) 0);
messageDigest.update(ByteUtil.longToByteArray(getFileLastModified()));
messageDigest.update(ByteUtil.longToByteArray(VERSION));
}
@Override
@ -161,7 +168,10 @@ public final class ConversationShortcutPhoto implements Key {
}
private @NonNull Bitmap getShortcutInfoBitmap(@NonNull Context context) throws ExecutionException, InterruptedException {
return DrawableUtil.wrapBitmapForShortcutInfo(request(GlideApp.with(context).asBitmap(), context, false).circleCrop().submit().get());
return DrawableUtil.wrapBitmapForShortcutInfo(AvatarUtil.loadIconBitmapSquareNoCache(context,
photo.recipient,
DrawableUtil.SHORTCUT_INFO_WRAPPED_SIZE,
DrawableUtil.SHORTCUT_INFO_WRAPPED_SIZE));
}
private @NonNull Bitmap getFallbackForShortcut(@NonNull Context context) {
@ -177,28 +187,33 @@ public final class ConversationShortcutPhoto implements Key {
}
FallbackContactPhoto photo = recipient.isSelf() || recipient.isGroup() ? new FallbackPhoto80dp(photoSource, recipient.getAvatarColor().colorInt())
: new ShortcutGeneratedContactPhoto(recipient.getDisplayName(context), photoSource, ViewUtil.dpToPx(80), ViewUtil.dpToPx(28));
Bitmap toWrap = DrawableUtil.toBitmap(photo.asDrawable(context, recipient.getAvatarColor().colorInt()), ViewUtil.dpToPx(80), ViewUtil.dpToPx(80));
: new ShortcutGeneratedContactPhoto(recipient.getDisplayName(context), photoSource, ViewUtil.dpToPx(80), ViewUtil.dpToPx(28), recipient.getAvatarColor().colorInt());
Bitmap toWrap = DrawableUtil.toBitmap(photo.asCallCard(context), ViewUtil.dpToPx(80), ViewUtil.dpToPx(80));
Bitmap wrapped = DrawableUtil.wrapBitmapForShortcutInfo(toWrap);
toWrap.recycle();
return wrapped;
}
private <T> GlideRequest<T> request(@NonNull GlideRequest<T> glideRequest, @NonNull Context context, boolean loadSelf) {
return glideRequest.load(photo.recipient.getContactPhoto()).diskCacheStrategy(DiskCacheStrategy.ALL);
}
}
private static final class ShortcutGeneratedContactPhoto extends GeneratedContactPhoto {
public ShortcutGeneratedContactPhoto(@NonNull String name, int fallbackResId, int targetSize, int fontSize) {
private final int color;
public ShortcutGeneratedContactPhoto(@NonNull String name, int fallbackResId, int targetSize, int fontSize, int color) {
super(name, fallbackResId, targetSize, fontSize);
this.color = color;
}
@Override
protected Drawable newFallbackDrawable(@NonNull Context context, int color, boolean inverted) {
return new FallbackPhoto80dp(getFallbackResId(), color).asDrawable(context, -1);
}
@Override public Drawable asCallCard(Context context) {
return new FallbackPhoto80dp(getFallbackResId(), color).asCallCard(context);
}
}
}

View file

@ -11,7 +11,7 @@ import androidx.core.graphics.drawable.DrawableCompat;
public final class DrawableUtil {
private static final int SHORTCUT_INFO_BITMAP_SIZE = ViewUtil.dpToPx(108);
private static final int SHORTCUT_INFO_WRAPPED_SIZE = ViewUtil.dpToPx(72);
public static final int SHORTCUT_INFO_WRAPPED_SIZE = ViewUtil.dpToPx(72);
private static final int SHORTCUT_INFO_PADDING = (SHORTCUT_INFO_BITMAP_SIZE - SHORTCUT_INFO_WRAPPED_SIZE) / 2;
private DrawableUtil() {}