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

292 lines
9.8 KiB
Java
Raw Normal View History

/*
* This is the source code of Telegram for Android v. 1.7.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).
*
* Copyright Nikolai Kudashov, 2013-2014.
*/
package org.telegram.ui.Components;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
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;
import org.telegram.android.AndroidUtilities;
import org.telegram.messenger.FileLog;
2014-11-11 23:16:17 +01:00
import org.telegram.messenger.R;
import org.telegram.messenger.TLRPC;
2014-11-17 23:04:31 +01:00
import org.telegram.messenger.UserConfig;
import org.telegram.messenger.ApplicationLoader;
2014-11-17 23:04:31 +01:00
import java.util.Locale;
public class AvatarDrawable extends Drawable {
private static Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private static TextPaint namePaint;
private static TextPaint namePaintSmall;
private static int[] arrColors = {0xffe56555, 0xfff28c48, 0xffeec764, 0xff76c84d, 0xff5fbed5, 0xff549cdd, 0xff8e85ee, 0xfff2749a};
2014-11-18 13:37:11 +01:00
private static int[] arrColorsProfiles = {0xffd86f65, 0xfff69d61, 0xfffabb3c, 0xff67b35d, 0xff56a2bb, 0xff5c98cd, 0xff8c79d2, 0xfff37fa6};
2015-02-01 19:51:02 +01:00
private static int[] arrColorsProfilesBack = {0xffca6056, 0xfff18944, 0xff7d6ac4, 0xff56a14c, 0xff4492ac, 0xff4c84b6, 0xff7d6ac4, 0xff4c84b6};
private static int[] arrColorsProfilesText = {0xfff9cbc5, 0xfffdddc8, 0xffcdc4ed, 0xffc0edba, 0xffb8e2f0, 0xffb3d7f7, 0xffcdc4ed, 0xffb3d7f7};
private static int[] arrColorsNames = {0xffca5650, 0xffd87b29, 0xff4e92cc, 0xff50b232, 0xff42b1a8, 0xff4e92cc, 0xff4e92cc, 0xff4e92cc};
private static int[] arrColorsButtons = {R.drawable.bar_selector_red, R.drawable.bar_selector_orange, R.drawable.bar_selector_violet,
R.drawable.bar_selector_green, R.drawable.bar_selector_cyan, R.drawable.bar_selector_blue, R.drawable.bar_selector_violet, R.drawable.bar_selector_blue};
2014-11-12 23:16:59 +01:00
private static Drawable broadcastDrawable;
2014-11-14 16:40:15 +01:00
private static Drawable photoDrawable;
2014-11-12 23:16:59 +01:00
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;
2014-11-14 16:40:15 +01:00
private boolean drawPhoto;
private boolean smallStyle;
private StringBuilder stringBuilder = new StringBuilder(5);
public AvatarDrawable() {
super();
if (namePaint == null) {
namePaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
namePaint.setColor(0xffffffff);
namePaint.setTextSize(AndroidUtilities.dp(20));
2014-11-12 23:16:59 +01:00
namePaintSmall = new TextPaint(Paint.ANTI_ALIAS_FLAG);
namePaintSmall.setColor(0xffffffff);
namePaintSmall.setTextSize(AndroidUtilities.dp(14));
2014-11-12 23:16:59 +01:00
broadcastDrawable = ApplicationLoader.applicationContext.getResources().getDrawable(R.drawable.broadcast_w);
}
}
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) {
setInfo(user.id, user.first_name, user.last_name, false);
}
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) {
setInfo(chat.id, chat.title, null, chat.id < 0);
}
}
public void setSmallStyle(boolean value) {
smallStyle = value;
}
2014-11-17 23:04:31 +01:00
public static int getColorIndex(int id) {
2014-11-18 06:01:04 +01:00
if (id >= 0 && id < 8) {
return id;
}
2014-11-17 23:04:31 +01:00
try {
String str;
if (id >= 0) {
str = String.format(Locale.US, "%d%d", id, UserConfig.getClientUserId());
} else {
str = String.format(Locale.US, "%d", id);
}
if (str.length() > 15) {
str = str.substring(0, 15);
}
java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
byte[] digest = md.digest(str.getBytes());
int b = digest[Math.abs(id % 16)];
if (b < 0) {
b += 256;
}
return Math.abs(b) % arrColors.length;
} catch (Exception e) {
FileLog.e("tmessages", e);
}
return id % arrColors.length;
2014-11-10 12:05:22 +01:00
}
public static int getColorForId(int id) {
2014-11-17 23:04:31 +01:00
return arrColors[getColorIndex(id)];
}
2014-11-11 23:16:17 +01:00
public static int getButtonColorForId(int id) {
2014-11-17 23:04:31 +01:00
return arrColorsButtons[getColorIndex(id)];
2014-11-11 23:16:17 +01:00
}
2014-11-10 12:05:22 +01:00
public static int getProfileColorForId(int id) {
2014-11-17 23:04:31 +01:00
return arrColorsProfiles[getColorIndex(id)];
2014-11-10 12:05:22 +01:00
}
2014-11-11 23:16:17 +01:00
public static int getProfileTextColorForId(int id) {
2014-11-17 23:04:31 +01:00
return arrColorsProfilesText[getColorIndex(id)];
2014-11-11 23:16:17 +01:00
}
2014-11-10 12:05:22 +01:00
public static int getProfileBackColorForId(int id) {
2014-11-17 23:04:31 +01:00
return arrColorsProfilesBack[getColorIndex(id)];
2014-11-10 12:05:22 +01:00
}
public static int getNameColorForId(int id) {
return arrColorsNames[getColorIndex(id)];
}
public void setInfo(TLRPC.User user) {
if (user != null) {
setInfo(user.id, user.first_name, user.last_name, false);
}
}
public void setInfo(TLRPC.Chat chat) {
if (chat != null) {
setInfo(chat.id, chat.title, null, chat.id < 0);
}
}
public void setColor(int value) {
color = value;
}
public void setInfo(int id, String firstName, String lastName, boolean isBroadcast) {
2014-11-10 12:05:22 +01:00
if (isProfile) {
2014-11-17 23:04:31 +01:00
color = arrColorsProfiles[getColorIndex(id)];
2014-11-10 12:05:22 +01:00
} else {
2014-11-17 23:04:31 +01:00
color = arrColors[getColorIndex(id)];
2014-11-10 12:05:22 +01:00
}
2014-11-12 23:16:59 +01:00
drawBrodcast = isBroadcast;
if (firstName == null || firstName.length() == 0) {
firstName = lastName;
lastName = null;
}
stringBuilder.setLength(0);
if (firstName != null && firstName.length() > 0) {
stringBuilder.append(firstName.substring(0, 1));
}
if (lastName != null && lastName.length() > 0) {
2015-01-02 23:15:07 +01:00
String lastch = null;
for (int a = lastName.length() - 1; a >= 0; a--) {
if (lastch != null && lastName.charAt(a) == ' ') {
break;
}
lastch = lastName.substring(a, a + 1);
}
2015-02-27 20:57:58 +01:00
if (Build.VERSION.SDK_INT >= 16) {
stringBuilder.append("\u200C");
}
stringBuilder.append(lastch);
} 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) != ' ') {
2015-02-27 20:57:58 +01:00
if (Build.VERSION.SDK_INT >= 16) {
stringBuilder.append("\u200C");
}
stringBuilder.append(firstName.substring(a + 1, a + 2));
break;
}
}
}
}
if (stringBuilder.length() > 0) {
String text = stringBuilder.toString().toUpperCase();
try {
textLayout = new StaticLayout(text, (smallStyle ? namePaintSmall : 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) {
FileLog.e("tmessages", e);
}
} else {
textLayout = null;
}
}
2014-11-14 16:40:15 +01:00
public void setDrawPhoto(boolean value) {
if (value && photoDrawable == null) {
photoDrawable = ApplicationLoader.applicationContext.getResources().getDrawable(R.drawable.photo_w);
}
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();
paint.setColor(color);
canvas.save();
canvas.translate(bounds.left, bounds.top);
canvas.drawCircle(size / 2, size / 2, size / 2, paint);
2014-11-12 23:16:59 +01:00
if (drawBrodcast && broadcastDrawable != null) {
int x = (size - broadcastDrawable.getIntrinsicWidth()) / 2;
int y = (size - broadcastDrawable.getIntrinsicHeight()) / 2;
broadcastDrawable.setBounds(x, y, x + broadcastDrawable.getIntrinsicWidth(), y + broadcastDrawable.getIntrinsicHeight());
broadcastDrawable.draw(canvas);
} 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);
2014-11-14 16:40:15 +01:00
} else if (drawPhoto && photoDrawable != null) {
int x = (size - photoDrawable.getIntrinsicWidth()) / 2;
int y = (size - photoDrawable.getIntrinsicHeight()) / 2;
photoDrawable.setBounds(x, y, x + photoDrawable.getIntrinsicWidth(), y + photoDrawable.getIntrinsicHeight());
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() {
return 0;
}
@Override
public int getIntrinsicWidth() {
return 0;
}
@Override
public int getIntrinsicHeight() {
return 0;
}
}