Show profile avatar in toolbar.

This commit is contained in:
Greyson Parrelli 2018-09-24 23:41:29 -07:00
parent a3cba66450
commit f725dd5a7e
11 changed files with 62 additions and 22 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 475 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 332 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 575 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 979 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@ -23,7 +23,8 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView android:layout_width="36dp"
<ImageView android:id="@+id/toolbar_icon"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"

View File

@ -21,11 +21,13 @@ import android.annotation.SuppressLint;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.support.v7.widget.TooltipCompat;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
@ -33,11 +35,19 @@ import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import org.thoughtcrime.securesms.color.MaterialColor;
import org.thoughtcrime.securesms.components.RatingManager;
import org.thoughtcrime.securesms.components.SearchToolbar;
import org.thoughtcrime.securesms.contacts.avatars.ContactColors;
import org.thoughtcrime.securesms.contacts.avatars.GeneratedContactPhoto;
import org.thoughtcrime.securesms.contacts.avatars.ProfileContactPhoto;
import org.thoughtcrime.securesms.database.Address;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.MessagingDatabase.MarkedMessageInfo;
import org.thoughtcrime.securesms.lock.RegistrationLockDialog;
import org.thoughtcrime.securesms.mms.GlideApp;
import org.thoughtcrime.securesms.notifications.MarkReadReceiver;
import org.thoughtcrime.securesms.notifications.MessageNotifier;
import org.thoughtcrime.securesms.permissions.Permissions;
@ -48,6 +58,7 @@ import org.thoughtcrime.securesms.util.DynamicLanguage;
import org.thoughtcrime.securesms.util.DynamicNoActionBarTheme;
import org.thoughtcrime.securesms.util.DynamicTheme;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.whispersystems.libsignal.util.guava.Optional;
import java.util.List;
@ -97,6 +108,8 @@ public class ConversationListActivity extends PassphraseRequiredActionBarActivit
super.onResume();
dynamicTheme.onResume(this);
dynamicLanguage.onResume(this);
initializeProfileIcon();
}
@Override
@ -161,6 +174,23 @@ public class ConversationListActivity extends PassphraseRequiredActionBarActivit
});
}
private void initializeProfileIcon() {
ImageView icon = findViewById(R.id.toolbar_icon);
Address localAddress = Address.fromSerialized(TextSecurePreferences.getLocalNumber(this));
String profileName = Optional.fromNullable(TextSecurePreferences.getProfileName(this)).or("");
MaterialColor fallbackColor = TextUtils.isEmpty(profileName) ? MaterialColor.STEEL : ContactColors.generateFor(profileName);
Drawable fallback = new GeneratedContactPhoto(profileName, R.drawable.ic_profile_default).asDrawable(this, fallbackColor.toAvatarColor(this));
GlideApp.with(this)
.load(new ProfileContactPhoto(localAddress, String.valueOf(TextSecurePreferences.getProfileAvatarId(this))))
.error(fallback)
.circleCrop()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(icon);
icon.setOnClickListener(v -> handleDisplaySettings());
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);

View File

@ -15,6 +15,7 @@ import com.bumptech.glide.load.engine.DiskCacheStrategy;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.contacts.avatars.ContactColors;
import org.thoughtcrime.securesms.contacts.avatars.GeneratedContactPhoto;
import org.thoughtcrime.securesms.contacts.avatars.ResourceContactPhoto;
import org.thoughtcrime.securesms.mms.GlideRequests;
import org.thoughtcrime.securesms.recipients.Recipient;
@ -57,7 +58,7 @@ public class AvatarImageView extends AppCompatImageView {
.into(this);
setAvatarClickHandler(recipient, quickContactEnabled);
} else {
setImageDrawable(new GeneratedContactPhoto("#").asDrawable(getContext(), ContactColors.UNKNOWN_COLOR.toConversationColor(getContext()), inverted));
setImageDrawable(new ResourceContactPhoto(R.drawable.ic_profile_default).asDrawable(getContext(), ContactColors.UNKNOWN_COLOR.toConversationColor(getContext()), inverted));
super.setOnClickListener(listener);
}
}

View File

@ -6,6 +6,8 @@ import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.support.annotation.NonNull;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.v7.content.res.AppCompatResources;
import android.text.TextUtils;
@ -23,37 +25,43 @@ public class GeneratedContactPhoto implements FallbackContactPhoto {
private static final Typeface TYPEFACE = Typeface.create("sans-serif-medium", Typeface.NORMAL);
private final String name;
private final int fallbackResId;
public GeneratedContactPhoto(@NonNull String name) {
this.name = name;
public GeneratedContactPhoto(@NonNull String name, @DrawableRes int fallbackResId) {
this.name = name;
this.fallbackResId = fallbackResId;
}
@Override
public Drawable asDrawable(Context context, int color) {
return asDrawable(context, color, false);
return asDrawable(context, color,false);
}
@Override
public Drawable asDrawable(Context context, int color, boolean inverted) {
int targetSize = context.getResources().getDimensionPixelSize(R.dimen.contact_photo_target_size);
String character = getAbbreviation(name);
Drawable base = TextDrawable.builder()
.beginConfig()
.width(targetSize)
.height(targetSize)
.useFont(TYPEFACE)
.fontSize(ViewUtil.dpToPx(context, 24))
.textColor(inverted ? color : Color.WHITE)
.endConfig()
.buildRound(getAbbreviation(name), inverted ? Color.WHITE : color);
if (!TextUtils.isEmpty(character)) {
Drawable base = TextDrawable.builder()
.beginConfig()
.width(targetSize)
.height(targetSize)
.useFont(TYPEFACE)
.fontSize(ViewUtil.dpToPx(context, 24))
.textColor(inverted ? color : Color.WHITE)
.endConfig()
.buildRound(character, inverted ? Color.WHITE : color);
Drawable gradient = context.getResources().getDrawable(ThemeUtil.isDarkTheme(context) ? R.drawable.avatar_gradient_dark
: R.drawable.avatar_gradient_light);
Drawable gradient = context.getResources().getDrawable(ThemeUtil.isDarkTheme(context) ? R.drawable.avatar_gradient_dark
: R.drawable.avatar_gradient_light);
return new LayerDrawable(new Drawable[] { base, gradient });
}
return new LayerDrawable(new Drawable[] { base, gradient });
return new ResourceContactPhoto(fallbackResId).asDrawable(context, color, inverted);
}
private String getAbbreviation(String name) {
private @Nullable String getAbbreviation(String name) {
String[] parts = name.split(" ");
StringBuilder builder = new StringBuilder();
int count = 0;
@ -67,7 +75,7 @@ public class GeneratedContactPhoto implements FallbackContactPhoto {
}
if (builder.length() == 0) {
return "#";
return null;
} else {
return builder.toString();
}

View File

@ -92,7 +92,7 @@ public class SingleRecipientNotificationBuilder extends AbstractNotificationBuil
} else {
setContentTitle(context.getString(R.string.SingleRecipientNotificationBuilder_signal));
setLargeIcon(new GeneratedContactPhoto("Unknown").asDrawable(context, ContactColors.UNKNOWN_COLOR.toConversationColor(context)));
setLargeIcon(new GeneratedContactPhoto("Unknown", R.drawable.ic_profile_default).asDrawable(context, ContactColors.UNKNOWN_COLOR.toConversationColor(context)));
}
}

View File

@ -425,8 +425,8 @@ public class Recipient implements RecipientModifiedListener {
public synchronized @NonNull FallbackContactPhoto getFallbackContactPhoto() {
if (isResolving()) return new TransparentContactPhoto();
else if (isGroupRecipient()) return new ResourceContactPhoto(R.drawable.ic_group_white_24dp, R.drawable.ic_group_large);
else if (!TextUtils.isEmpty(name)) return new GeneratedContactPhoto(name);
else return new GeneratedContactPhoto("#");
else if (!TextUtils.isEmpty(name)) return new GeneratedContactPhoto(name, R.drawable.ic_profile_default);
else return new ResourceContactPhoto(R.drawable.ic_profile_default);
}
public synchronized @Nullable ContactPhoto getContactPhoto() {