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

283 lines
9.1 KiB
Java
Raw Normal View History

/*
2015-10-29 18:10:07 +01:00
* This is the source code of Telegram for Android v. 3.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).
*
2017-03-31 01:58:05 +02:00
* Copyright Nikolai Kudashov, 2013-2017.
*/
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;
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;
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;
2014-11-12 23:16:59 +01:00
private boolean drawBrodcast;
2017-12-08 18:35:59 +01:00
private int savedMessages;
2014-11-14 16:40:15 +01:00
private boolean drawPhoto;
private StringBuilder stringBuilder = new StringBuilder(5);
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) {
2016-10-11 13:57:01 +02:00
setInfo(user.id, user.first_name, user.last_name, false, null);
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) {
2016-10-11 13:57:01 +02:00
setInfo(chat.id, chat.title, null, chat.id < 0, null);
2014-11-17 23:04:31 +01:00
}
}
2015-11-26 22:04:02 +01:00
public void setProfile(boolean value) {
isProfile = value;
}
2014-11-17 23:04:31 +01:00
public 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) {
2017-03-31 01:58:05 +02:00
return Theme.getColor(Theme.keys_avatar_actionBarSelector[getColorIndex(id)]);
}
public static int getIconColorForId(int id) {
return Theme.getColor(Theme.keys_avatar_actionBarIcon[getColorIndex(id)]);
2014-11-11 23:16:17 +01:00
}
2014-11-10 12:05:22 +01:00
public static int getProfileColorForId(int id) {
2017-03-31 01:58:05 +02:00
return Theme.getColor(Theme.keys_avatar_backgroundInProfile[getColorIndex(id)]);
2014-11-10 12:05:22 +01:00
}
2014-11-11 23:16:17 +01:00
public static int getProfileTextColorForId(int id) {
2017-03-31 01:58:05 +02:00
return Theme.getColor(Theme.keys_avatar_subtitleInProfile[getColorIndex(id)]);
2014-11-11 23:16:17 +01:00
}
2014-11-10 12:05:22 +01:00
public static int getProfileBackColorForId(int id) {
2017-03-31 01:58:05 +02:00
return Theme.getColor(Theme.keys_avatar_backgroundActionBar[getColorIndex(id)]);
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) {
2016-10-11 13:57:01 +02:00
setInfo(user.id, user.first_name, user.last_name, false, null);
}
}
2017-12-08 18:35:59 +01:00
public void setSavedMessages(int value) {
savedMessages = value;
color = Theme.getColor(Theme.key_avatar_backgroundSaved);
}
public void setInfo(TLRPC.Chat chat) {
if (chat != null) {
2016-10-11 13:57:01 +02:00
setInfo(chat.id, chat.title, null, chat.id < 0, null);
}
}
public void setColor(int value) {
color = value;
}
2017-03-31 01:58:05 +02:00
public void setTextSize(int size) {
namePaint.setTextSize(size);
}
public void setInfo(int id, String firstName, String lastName, boolean isBroadcast) {
2016-10-11 13:57:01 +02:00
setInfo(id, firstName, lastName, isBroadcast, null);
}
2017-03-31 01:58:05 +02:00
public int getColor() {
return color;
}
2016-10-11 13:57:01 +02:00
public void setInfo(int id, String firstName, String lastName, boolean isBroadcast, 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
}
2014-11-12 23:16:59 +01:00
drawBrodcast = isBroadcast;
2017-12-08 18:35:59 +01:00
savedMessages = 0;
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
}
2017-12-08 18:35:59 +01:00
if (Build.VERSION.SDK_INT >= 17) {
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) != ' ') {
2017-12-08 18:35:59 +01:00
if (Build.VERSION.SDK_INT >= 17) {
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;
}
}
2014-11-14 16:40:15 +01:00
public void setDrawPhoto(boolean value) {
drawPhoto = value;
}
@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));
Theme.avatar_backgroundPaint.setColor(color);
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);
if (savedMessages != 0 && Theme.avatar_savedDrawable != null) {
int w = Theme.avatar_savedDrawable.getIntrinsicWidth();
int h = Theme.avatar_savedDrawable.getIntrinsicHeight();
if (savedMessages == 2) {
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);
} else if (drawBrodcast && Theme.avatar_broadcastDrawable != null) {
2017-03-31 01:58:05 +02:00
int x = (size - Theme.avatar_broadcastDrawable.getIntrinsicWidth()) / 2;
int y = (size - Theme.avatar_broadcastDrawable.getIntrinsicHeight()) / 2;
Theme.avatar_broadcastDrawable.setBounds(x, y, x + Theme.avatar_broadcastDrawable.getIntrinsicWidth(), y + Theme.avatar_broadcastDrawable.getIntrinsicHeight());
Theme.avatar_broadcastDrawable.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);
2017-03-31 01:58:05 +02:00
} else if (drawPhoto && Theme.avatar_photoDrawable != null) {
int x = (size - Theme.avatar_photoDrawable.getIntrinsicWidth()) / 2;
int y = (size - Theme.avatar_photoDrawable.getIntrinsicHeight()) / 2;
Theme.avatar_photoDrawable.setBounds(x, y, x + Theme.avatar_photoDrawable.getIntrinsicWidth(), y + Theme.avatar_photoDrawable.getIntrinsicHeight());
Theme.avatar_photoDrawable.draw(canvas);
2014-11-12 23:16:59 +01:00
}
}
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;
}
}