NekoX/TMessagesProj/src/main/java/org/telegram/ui/Components/AvatarDrawable.java

326 lines
11 KiB
Java
Raw Normal View History

/*
2019-01-23 18:03:33 +01:00
* This is the source code of Telegram for Android v. 5.x.x.
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
2019-01-23 18:03:33 +01:00
* Copyright Nikolai Kudashov, 2013-2018.
*/
package org.telegram.ui.Components;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
2015-10-29 18:10:07 +01:00
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.AndroidUtilities;
import org.telegram.messenger.FileLog;
2019-07-18 15:01:39 +02:00
import org.telegram.messenger.UserObject;
2015-09-24 22:52:02 +02:00
import org.telegram.tgnet.TLRPC;
2016-04-22 15:49:00 +02:00
import org.telegram.ui.ActionBar.Theme;
public class AvatarDrawable extends Drawable {
2017-03-31 01:58:05 +02:00
private TextPaint namePaint;
private int color;
2019-09-10 12:56:11 +02:00
private boolean needApplyColorAccent;
private StaticLayout textLayout;
private float textWidth;
private float textHeight;
2014-11-21 01:14:44 +01:00
private float textLeft;
2014-11-10 12:05:22 +01:00
private boolean isProfile;
2019-07-18 15:01:39 +02:00
private boolean drawDeleted;
2019-05-14 14:08:05 +02:00
private int avatarType;
private float archivedAvatarProgress;
private StringBuilder stringBuilder = new StringBuilder(5);
2019-05-14 14:08:05 +02:00
public static final int AVATAR_TYPE_NORMAL = 0;
public static final int AVATAR_TYPE_SAVED = 1;
public static final int AVATAR_TYPE_SAVED_SMALL = 2;
public static final int AVATAR_TYPE_ARCHIVED = 3;
public AvatarDrawable() {
super();
2017-03-31 01:58:05 +02:00
namePaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
namePaint.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
namePaint.setTextSize(AndroidUtilities.dp(18));
}
public AvatarDrawable(TLRPC.User user) {
2014-11-17 23:04:31 +01:00
this(user, false);
}
public AvatarDrawable(TLRPC.Chat chat) {
2014-11-17 23:04:31 +01:00
this(chat, false);
}
2014-11-10 12:05:22 +01:00
public AvatarDrawable(TLRPC.User user, boolean profile) {
2014-11-19 16:17:24 +01:00
this();
2014-11-10 12:05:22 +01:00
isProfile = profile;
2014-11-17 23:04:31 +01:00
if (user != null) {
2019-09-10 12:56:11 +02:00
setInfo(user.id, user.first_name, user.last_name, null);
2019-07-18 15:01:39 +02:00
drawDeleted = UserObject.isDeleted(user);
2014-11-17 23:04:31 +01:00
}
2014-11-10 12:05:22 +01:00
}
public AvatarDrawable(TLRPC.Chat chat, boolean profile) {
2014-11-19 16:17:24 +01:00
this();
2014-11-10 12:05:22 +01:00
isProfile = profile;
2014-11-17 23:04:31 +01:00
if (chat != null) {
2019-09-10 12:56:11 +02:00
setInfo(chat.id, chat.title, null, null);
2014-11-17 23:04:31 +01:00
}
}
2015-11-26 22:04:02 +01:00
public void setProfile(boolean value) {
isProfile = value;
}
2019-09-10 12:56:11 +02:00
private static int getColorIndex(int id) {
2017-12-08 18:35:59 +01:00
if (id >= 0 && id < 7) {
2014-11-18 06:01:04 +01:00
return id;
}
2017-03-31 01:58:05 +02:00
return Math.abs(id % Theme.keys_avatar_background.length);
2014-11-10 12:05:22 +01:00
}
public static int getColorForId(int id) {
2017-03-31 01:58:05 +02:00
return Theme.getColor(Theme.keys_avatar_background[getColorIndex(id)]);
}
2014-11-11 23:16:17 +01:00
public static int getButtonColorForId(int id) {
2019-01-23 18:03:33 +01:00
return Theme.getColor(Theme.key_avatar_actionBarSelectorBlue);
2017-03-31 01:58:05 +02:00
}
public static int getIconColorForId(int id) {
2019-01-23 18:03:33 +01:00
return Theme.getColor(Theme.key_avatar_actionBarIconBlue);
2014-11-11 23:16:17 +01:00
}
2014-11-10 12:05:22 +01:00
public static int getProfileColorForId(int id) {
2019-01-23 18:03:33 +01:00
return Theme.getColor(Theme.keys_avatar_background[getColorIndex(id)]);
2014-11-10 12:05:22 +01:00
}
2014-11-11 23:16:17 +01:00
public static int getProfileTextColorForId(int id) {
2019-01-23 18:03:33 +01:00
return Theme.getColor(Theme.key_avatar_subtitleInProfileBlue);
2014-11-11 23:16:17 +01:00
}
2014-11-10 12:05:22 +01:00
public static int getProfileBackColorForId(int id) {
2019-01-23 18:03:33 +01:00
return Theme.getColor(Theme.key_avatar_backgroundActionBarBlue);
2014-11-10 12:05:22 +01:00
}
public static int getNameColorForId(int id) {
2017-03-31 01:58:05 +02:00
return Theme.getColor(Theme.keys_avatar_nameInMessage[getColorIndex(id)]);
}
public void setInfo(TLRPC.User user) {
if (user != null) {
2019-09-10 12:56:11 +02:00
setInfo(user.id, user.first_name, user.last_name, null);
2019-07-18 15:01:39 +02:00
drawDeleted = UserObject.isDeleted(user);
}
}
2019-05-14 14:08:05 +02:00
public void setAvatarType(int value) {
avatarType = value;
if (avatarType == AVATAR_TYPE_ARCHIVED) {
color = Theme.getColor(Theme.key_avatar_backgroundArchivedHidden);
} else {
color = Theme.getColor(Theme.key_avatar_backgroundSaved);
}
2019-09-10 12:56:11 +02:00
needApplyColorAccent = false;
2019-05-14 14:08:05 +02:00
}
public void setArchivedAvatarHiddenProgress(float progress) {
archivedAvatarProgress = progress;
}
public int getAvatarType() {
return avatarType;
2017-12-08 18:35:59 +01:00
}
public void setInfo(TLRPC.Chat chat) {
if (chat != null) {
2019-09-10 12:56:11 +02:00
setInfo(chat.id, chat.title, null, null);
}
}
public void setColor(int value) {
color = value;
2019-09-10 12:56:11 +02:00
needApplyColorAccent = false;
}
2017-03-31 01:58:05 +02:00
public void setTextSize(int size) {
namePaint.setTextSize(size);
}
2019-09-10 12:56:11 +02:00
public void setInfo(int id, String firstName, String lastName) {
setInfo(id, firstName, lastName, null);
2016-10-11 13:57:01 +02:00
}
2017-03-31 01:58:05 +02:00
public int getColor() {
2019-09-10 12:56:11 +02:00
return needApplyColorAccent ? Theme.changeColorAccent(color) : color;
2017-03-31 01:58:05 +02:00
}
2019-09-10 12:56:11 +02:00
public void setInfo(int id, String firstName, String lastName, String custom) {
2014-11-10 12:05:22 +01:00
if (isProfile) {
2017-03-31 01:58:05 +02:00
color = getProfileColorForId(id);
2014-11-10 12:05:22 +01:00
} else {
2017-03-31 01:58:05 +02:00
color = getColorForId(id);
2014-11-10 12:05:22 +01:00
}
2019-09-10 12:56:11 +02:00
needApplyColorAccent = id == 5; // Tinting manually set blue color
2019-05-14 14:08:05 +02:00
avatarType = AVATAR_TYPE_NORMAL;
2019-07-18 15:01:39 +02:00
drawDeleted = false;
2014-11-12 23:16:59 +01:00
if (firstName == null || firstName.length() == 0) {
firstName = lastName;
lastName = null;
}
stringBuilder.setLength(0);
2016-10-11 13:57:01 +02:00
if (custom != null) {
stringBuilder.append(custom);
} else {
if (firstName != null && firstName.length() > 0) {
2017-03-31 01:58:05 +02:00
stringBuilder.appendCodePoint(firstName.codePointAt(0));
}
2016-10-11 13:57:01 +02:00
if (lastName != null && lastName.length() > 0) {
2017-03-31 01:58:05 +02:00
Integer lastch = null;
2016-10-11 13:57:01 +02:00
for (int a = lastName.length() - 1; a >= 0; a--) {
if (lastch != null && lastName.charAt(a) == ' ') {
break;
}
2017-03-31 01:58:05 +02:00
lastch = lastName.codePointAt(a);
2016-10-11 13:57:01 +02:00
}
2018-07-30 04:07:02 +02:00
if (Build.VERSION.SDK_INT > 17) {
2017-12-08 18:35:59 +01:00
stringBuilder.append("\u200C");
}
2017-03-31 01:58:05 +02:00
stringBuilder.appendCodePoint(lastch);
2016-10-11 13:57:01 +02:00
} else if (firstName != null && firstName.length() > 0) {
for (int a = firstName.length() - 1; a >= 0; a--) {
if (firstName.charAt(a) == ' ') {
if (a != firstName.length() - 1 && firstName.charAt(a + 1) != ' ') {
2018-07-30 04:07:02 +02:00
if (Build.VERSION.SDK_INT > 17) {
2017-12-08 18:35:59 +01:00
stringBuilder.append("\u200C");
}
2017-03-31 01:58:05 +02:00
stringBuilder.appendCodePoint(firstName.codePointAt(a + 1));
2016-10-11 13:57:01 +02:00
break;
}
}
}
}
}
if (stringBuilder.length() > 0) {
String text = stringBuilder.toString().toUpperCase();
try {
2017-03-31 01:58:05 +02:00
textLayout = new StaticLayout(text, namePaint, AndroidUtilities.dp(100), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
if (textLayout.getLineCount() > 0) {
2014-11-21 01:14:44 +01:00
textLeft = textLayout.getLineLeft(0);
textWidth = textLayout.getLineWidth(0);
textHeight = textLayout.getLineBottom(0);
}
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
}
} else {
textLayout = null;
}
}
@Override
public void draw(Canvas canvas) {
Rect bounds = getBounds();
if (bounds == null) {
return;
}
2014-11-12 23:16:59 +01:00
int size = bounds.width();
2017-03-31 01:58:05 +02:00
namePaint.setColor(Theme.getColor(Theme.key_avatar_text));
2019-09-10 12:56:11 +02:00
Theme.avatar_backgroundPaint.setColor(getColor());
canvas.save();
canvas.translate(bounds.left, bounds.top);
2017-12-08 18:35:59 +01:00
canvas.drawCircle(size / 2.0f, size / 2.0f, size / 2.0f, Theme.avatar_backgroundPaint);
2019-05-14 14:08:05 +02:00
if (avatarType == AVATAR_TYPE_ARCHIVED) {
if (archivedAvatarProgress != 0) {
Theme.avatar_backgroundPaint.setColor(Theme.getColor(Theme.key_avatar_backgroundArchived));
canvas.drawCircle(size / 2.0f, size / 2.0f, size / 2.0f * archivedAvatarProgress, Theme.avatar_backgroundPaint);
if (Theme.dialogs_archiveAvatarDrawableRecolored) {
2019-07-18 15:01:39 +02:00
Theme.dialogs_archiveAvatarDrawable.beginApplyLayerColors();
2020-01-05 12:50:11 +01:00
Theme.dialogs_archiveAvatarDrawable.setLayerColor("Arrow1.**", Theme.getNonAnimatedColor(Theme.key_avatar_backgroundArchived));
Theme.dialogs_archiveAvatarDrawable.setLayerColor("Arrow2.**", Theme.getNonAnimatedColor(Theme.key_avatar_backgroundArchived));
2019-07-18 15:01:39 +02:00
Theme.dialogs_archiveAvatarDrawable.commitApplyLayerColors();
2019-05-14 14:08:05 +02:00
Theme.dialogs_archiveAvatarDrawableRecolored = false;
}
} else {
if (!Theme.dialogs_archiveAvatarDrawableRecolored) {
2019-07-18 15:01:39 +02:00
Theme.dialogs_archiveAvatarDrawable.beginApplyLayerColors();
2019-12-31 14:08:08 +01:00
Theme.dialogs_archiveAvatarDrawable.setLayerColor("Arrow1.**", color);
Theme.dialogs_archiveAvatarDrawable.setLayerColor("Arrow2.**", color);
2019-07-18 15:01:39 +02:00
Theme.dialogs_archiveAvatarDrawable.commitApplyLayerColors();
2019-05-14 14:08:05 +02:00
Theme.dialogs_archiveAvatarDrawableRecolored = true;
}
}
int w = Theme.dialogs_archiveAvatarDrawable.getIntrinsicWidth();
int h = Theme.dialogs_archiveAvatarDrawable.getIntrinsicHeight();
int x = (size - w) / 2;
int y = (size - h) / 2;
canvas.save();
Theme.dialogs_archiveAvatarDrawable.setBounds(x, y, x + w, y + h);
Theme.dialogs_archiveAvatarDrawable.draw(canvas);
canvas.restore();
} else if (avatarType != 0 && Theme.avatar_savedDrawable != null) {
2017-12-08 18:35:59 +01:00
int w = Theme.avatar_savedDrawable.getIntrinsicWidth();
int h = Theme.avatar_savedDrawable.getIntrinsicHeight();
2019-05-14 14:08:05 +02:00
if (avatarType == AVATAR_TYPE_SAVED_SMALL) {
2017-12-08 18:35:59 +01:00
w *= 0.8f;
h *= 0.8f;
}
int x = (size - w) / 2;
int y = (size - h) / 2;
Theme.avatar_savedDrawable.setBounds(x, y, x + w, y + h);
Theme.avatar_savedDrawable.draw(canvas);
2019-07-18 15:01:39 +02:00
} else if (drawDeleted && Theme.avatar_ghostDrawable != null) {
int x = (size - Theme.avatar_ghostDrawable.getIntrinsicWidth()) / 2;
int y = (size - Theme.avatar_ghostDrawable.getIntrinsicHeight()) / 2;
Theme.avatar_ghostDrawable.setBounds(x, y, x + Theme.avatar_ghostDrawable.getIntrinsicWidth(), y + Theme.avatar_ghostDrawable.getIntrinsicHeight());
Theme.avatar_ghostDrawable.draw(canvas);
2014-11-12 23:16:59 +01:00
} else {
if (textLayout != null) {
2014-11-21 01:14:44 +01:00
canvas.translate((size - textWidth) / 2 - textLeft, (size - textHeight) / 2);
2014-11-12 23:16:59 +01:00
textLayout.draw(canvas);
}
}
canvas.restore();
}
@Override
public void setAlpha(int alpha) {
}
@Override
public void setColorFilter(ColorFilter cf) {
}
@Override
public int getOpacity() {
2015-10-29 18:10:07 +01:00
return PixelFormat.TRANSPARENT;
}
@Override
public int getIntrinsicWidth() {
return 0;
}
@Override
public int getIntrinsicHeight() {
return 0;
}
}