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

210 lines
8.1 KiB
Java
Raw Normal View History

2014-03-10 10:27:49 +01:00
/*
* This is the source code of Telegram for Android v. 1.3.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.
2014-03-10 10:27:49 +01:00
*/
package org.telegram.ui.Components;
2014-03-10 10:27:49 +01:00
import android.content.Context;
import android.graphics.Canvas;
2014-11-17 03:44:57 +01:00
import android.graphics.ColorFilter;
2014-03-10 10:27:49 +01:00
import android.graphics.Paint;
2020-07-26 10:03:38 +02:00
import android.graphics.PixelFormat;
2022-04-16 16:43:17 +02:00
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
2014-03-10 10:27:49 +01:00
import android.graphics.drawable.Drawable;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
2022-04-16 16:43:17 +02:00
import androidx.core.content.ContextCompat;
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.AndroidUtilities;
2022-04-16 16:43:17 +02:00
import org.telegram.messenger.ApplicationLoader;
2014-03-10 10:27:49 +01:00
import org.telegram.messenger.FileLog;
2020-07-26 10:03:38 +02:00
import org.telegram.messenger.LocaleController;
import org.telegram.messenger.R;
2017-03-31 01:58:05 +02:00
import org.telegram.ui.ActionBar.Theme;
2014-03-10 10:27:49 +01:00
2014-11-17 03:44:57 +01:00
public class TimerDrawable extends Drawable {
2014-03-10 10:27:49 +01:00
2017-03-31 01:58:05 +02:00
private TextPaint timePaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Paint linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
2014-03-10 10:27:49 +01:00
private StaticLayout timeLayout;
private float timeWidth = 0;
private int timeHeight = 0;
2022-04-16 16:43:17 +02:00
private int time = -1;
private Drawable currentTtlIcon;
private int iconColor;
private int currentTtlIconId;
Context context;
Theme.ResourcesProvider resourcesProvider;
private boolean overrideColor;
private boolean isStaticIcon;
public TimerDrawable(Context context, Theme.ResourcesProvider resourcesProvider) {
this.context = context;
this.resourcesProvider = resourcesProvider;
timePaint.setTypeface(AndroidUtilities.getTypeface("fonts/rcondensedbold.ttf"));
2017-03-31 01:58:05 +02:00
linePaint.setStrokeWidth(AndroidUtilities.dp(1));
linePaint.setStyle(Paint.Style.STROKE);
2014-03-10 10:27:49 +01:00
}
public void setTime(int value) {
2022-04-16 16:43:17 +02:00
if (time != value) {
time = value;
currentTtlIcon = ContextCompat.getDrawable(context, time == 0 ? R.drawable.msg_mini_autodelete : R.drawable.msg_mini_autodelete_empty).mutate();
currentTtlIcon.setColorFilter(currentColorFilter);
invalidateSelf();
String timeString;
if (time >= 1 && time < 60) {
timeString = "" + value;
if (timeString.length() < 2) {
timeString += LocaleController.getString("SecretChatTimerSeconds", R.string.SecretChatTimerSeconds);
}
} else if (time >= 60 && time < 60 * 60) {
timeString = "" + value / 60;
if (timeString.length() < 2) {
timeString += LocaleController.getString("SecretChatTimerMinutes", R.string.SecretChatTimerMinutes);
}
} else if (time >= 60 * 60 && time < 60 * 60 * 24) {
timeString = "" + value / 60 / 60;
if (timeString.length() < 2) {
timeString += LocaleController.getString("SecretChatTimerHours", R.string.SecretChatTimerHours);
}
} else if (time >= 60 * 60 * 24 && time < 60 * 60 * 24 * 7) {
timeString = "" + value / 60 / 60 / 24;
if (timeString.length() < 2) {
timeString += LocaleController.getString("SecretChatTimerDays", R.string.SecretChatTimerDays);
}
} else if (time < 60 * 60 * 24 * 31) {
timeString = "" + value / 60 / 60 / 24 / 7;
if (timeString.length() < 2) {
timeString += LocaleController.getString("SecretChatTimerWeeks", R.string.SecretChatTimerWeeks);
} else if (timeString.length() > 2) {
timeString = "c";
}
2022-06-21 04:51:00 +02:00
} else if (time < 60 * 60 * 24 * 364){
2022-04-16 16:43:17 +02:00
timeString = "" + value / 60 / 60 / 24 / 30;
if (timeString.length() < 2) {
timeString += LocaleController.getString("SecretChatTimerMonths", R.string.SecretChatTimerMonths);
}
2022-06-21 04:51:00 +02:00
} else {
timeString = "" + value / 60 / 60 / 24 / 364;
if (timeString.length() < 2) {
timeString += LocaleController.getString("SecretChatTimerYears", R.string.SecretChatTimerYears);
}
2014-10-14 22:36:15 +02:00
}
2022-04-16 16:43:17 +02:00
timePaint.setTextSize(AndroidUtilities.dp(11));
timeWidth = timePaint.measureText(timeString);
if (timeWidth > AndroidUtilities.dp(13)) {
timePaint.setTextSize(AndroidUtilities.dp(9));
timeWidth = timePaint.measureText(timeString);
2014-10-14 22:36:15 +02:00
}
2022-04-16 16:43:17 +02:00
if (timeWidth > AndroidUtilities.dp(13)) {
timePaint.setTextSize(AndroidUtilities.dp(6));
timeWidth = timePaint.measureText(timeString);
2021-07-30 16:49:55 +02:00
}
2022-04-16 16:43:17 +02:00
try {
timeLayout = new StaticLayout(timeString, timePaint, (int) Math.ceil(timeWidth), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
timeHeight = timeLayout.getHeight();
} catch (Exception e) {
timeLayout = null;
FileLog.e(e);
2014-10-14 22:36:15 +02:00
}
2014-03-10 10:27:49 +01:00
2022-04-16 16:43:17 +02:00
invalidateSelf();
2014-03-10 10:27:49 +01:00
}
2022-04-16 16:43:17 +02:00
}
2014-03-10 10:27:49 +01:00
2022-04-16 16:43:17 +02:00
public static TimerDrawable getTtlIcon(int ttl) {
TimerDrawable timerDrawable = new TimerDrawable(ApplicationLoader.applicationContext, null);
timerDrawable.setTime(ttl);
timerDrawable.isStaticIcon = true;
return timerDrawable;
2014-03-10 10:27:49 +01:00
}
@Override
2014-11-17 03:44:57 +01:00
public void draw(Canvas canvas) {
2017-03-31 01:58:05 +02:00
int width = getIntrinsicWidth();
int height = getIntrinsicHeight();
2022-04-16 16:43:17 +02:00
if (!isStaticIcon) {
if (!overrideColor) {
paint.setColor(Theme.getColor(Theme.key_actionBarDefault, resourcesProvider));
}
timePaint.setColor(Theme.getColor(Theme.key_actionBarDefaultTitle, resourcesProvider));
2014-03-10 10:27:49 +01:00
} else {
2022-04-16 16:43:17 +02:00
timePaint.setColor(Theme.getColor(Theme.key_actionBarDefaultSubmenuItemIcon, resourcesProvider));
2014-03-10 10:27:49 +01:00
}
2022-04-16 16:43:17 +02:00
if (currentTtlIcon != null) {
if (!isStaticIcon) {
canvas.drawCircle(getBounds().centerX(), getBounds().centerY(), getBounds().width() / 2f, paint);
int iconColor = Theme.getColor(Theme.key_actionBarDefaultTitle, resourcesProvider);
if (this.iconColor != iconColor) {
this.iconColor = iconColor;
currentTtlIcon.setColorFilter(new PorterDuffColorFilter(iconColor, PorterDuff.Mode.MULTIPLY));
}
}
2022-04-21 20:03:20 +02:00
AndroidUtilities.rectTmp2.set((int) (getBounds().centerX() - AndroidUtilities.dp(10.5f)), (int) (getBounds().centerY() - AndroidUtilities.dp(10.5f)),
(int) (getBounds().centerX() - AndroidUtilities.dp(10.5f)) + currentTtlIcon.getIntrinsicWidth(),
(int) (getBounds().centerY() - AndroidUtilities.dp(10.5f)) + currentTtlIcon.getIntrinsicHeight());
2022-04-16 16:43:17 +02:00
currentTtlIcon.setBounds(AndroidUtilities.rectTmp2);
currentTtlIcon.draw(canvas);
}
if (time != 0) {
if (timeLayout != null) {
int xOffxet = 0;
if (AndroidUtilities.density == 3) {
xOffxet = -1;
}
canvas.translate((int) (width / 2 - Math.ceil(timeWidth / 2)) + xOffxet, (height - timeHeight) / 2);
timeLayout.draw(canvas);
2014-11-19 16:17:24 +01:00
}
2014-03-10 10:27:49 +01:00
}
}
2014-11-17 03:44:57 +01:00
@Override
public void setAlpha(int alpha) {
}
2022-04-16 16:43:17 +02:00
ColorFilter currentColorFilter;
2014-11-17 03:44:57 +01:00
@Override
public void setColorFilter(ColorFilter cf) {
2022-04-16 16:43:17 +02:00
currentColorFilter = cf;
if (isStaticIcon) {
currentTtlIcon.setColorFilter(cf);
}
2014-11-17 03:44:57 +01:00
}
@Override
public int getOpacity() {
2020-07-26 10:03:38 +02:00
return PixelFormat.TRANSPARENT;
2014-11-17 03:44:57 +01:00
}
@Override
public int getIntrinsicWidth() {
2022-04-16 16:43:17 +02:00
return AndroidUtilities.dp(23);
2014-11-17 03:44:57 +01:00
}
@Override
public int getIntrinsicHeight() {
2022-04-16 16:43:17 +02:00
return AndroidUtilities.dp(23);
}
public void setBackgroundColor(int currentActionBarColor) {
overrideColor = true;
paint.setColor(currentActionBarColor);
2014-11-17 03:44:57 +01:00
}
2014-03-10 10:27:49 +01:00
}