NekoX/TMessagesProj/src/main/java/org/telegram/ui/Cells/UserCell.java

295 lines
13 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.Cells;
import android.content.Context;
2017-03-31 01:58:05 +02:00
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
2018-07-30 04:07:02 +02:00
import android.graphics.Typeface;
2014-11-12 23:16:59 +01:00
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.ImageView;
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.AndroidUtilities;
import org.telegram.messenger.LocaleController;
import org.telegram.messenger.MessagesController;
import org.telegram.messenger.UserObject;
import org.telegram.messenger.R;
2015-09-24 22:52:02 +02:00
import org.telegram.tgnet.ConnectionsManager;
import org.telegram.tgnet.TLObject;
import org.telegram.tgnet.TLRPC;
import org.telegram.messenger.UserConfig;
2017-03-31 01:58:05 +02:00
import org.telegram.ui.ActionBar.Theme;
import org.telegram.ui.Components.AvatarDrawable;
import org.telegram.ui.Components.BackupImageView;
import org.telegram.ui.Components.CheckBox;
2015-11-26 22:04:02 +01:00
import org.telegram.ui.Components.CheckBoxSquare;
import org.telegram.ui.Components.LayoutHelper;
2016-04-22 15:49:00 +02:00
import org.telegram.ui.ActionBar.SimpleTextView;
2014-11-12 23:16:59 +01:00
public class UserCell extends FrameLayout {
private BackupImageView avatarImageView;
private SimpleTextView nameTextView;
private SimpleTextView statusTextView;
2014-11-12 23:16:59 +01:00
private ImageView imageView;
2014-11-13 21:10:14 +01:00
private CheckBox checkBox;
2015-11-26 22:04:02 +01:00
private CheckBoxSquare checkBoxBig;
2016-03-16 13:26:32 +01:00
private ImageView adminImage;
private AvatarDrawable avatarDrawable;
2016-05-25 23:49:47 +02:00
private TLObject currentObject;
2014-11-12 23:16:59 +01:00
private CharSequence currentName;
private CharSequence currrntStatus;
2018-07-30 04:07:02 +02:00
private int currentId;
2014-11-12 23:16:59 +01:00
private int currentDrawable;
2016-05-25 23:49:47 +02:00
private String lastName;
private int lastStatus;
private TLRPC.FileLocation lastAvatar;
2018-07-30 04:07:02 +02:00
private int currentAccount = UserConfig.selectedAccount;
2017-03-31 01:58:05 +02:00
private int statusColor;
private int statusOnlineColor;
2016-03-16 13:26:32 +01:00
public UserCell(Context context, int padding, int checkbox, boolean admin) {
super(context);
2017-03-31 01:58:05 +02:00
statusColor = Theme.getColor(Theme.key_windowBackgroundWhiteGrayText);
statusOnlineColor = Theme.getColor(Theme.key_windowBackgroundWhiteBlueText);
avatarDrawable = new AvatarDrawable();
2014-11-12 23:16:59 +01:00
avatarImageView = new BackupImageView(context);
2015-04-09 20:00:14 +02:00
avatarImageView.setRoundRadius(AndroidUtilities.dp(24));
addView(avatarImageView, LayoutHelper.createFrame(48, 48, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP, LocaleController.isRTL ? 0 : 7 + padding, 8, LocaleController.isRTL ? 7 + padding : 0, 0));
nameTextView = new SimpleTextView(context);
2017-03-31 01:58:05 +02:00
nameTextView.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlackText));
nameTextView.setTextSize(17);
nameTextView.setGravity((LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP);
2016-03-16 13:26:32 +01:00
addView(nameTextView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 20, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP, LocaleController.isRTL ? 28 + (checkbox == 2 ? 18 : 0) : (68 + padding), 11.5f, LocaleController.isRTL ? (68 + padding) : 28 + (checkbox == 2 ? 18 : 0), 0));
statusTextView = new SimpleTextView(context);
statusTextView.setTextSize(14);
statusTextView.setGravity((LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP);
addView(statusTextView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 20, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP, LocaleController.isRTL ? 28 : (68 + padding), 34.5f, LocaleController.isRTL ? (68 + padding) : 28, 0));
2014-11-12 23:16:59 +01:00
imageView = new ImageView(context);
imageView.setScaleType(ImageView.ScaleType.CENTER);
2018-07-30 04:07:02 +02:00
imageView.setColorFilter(new PorterDuffColorFilter(Theme.getColor(Theme.key_windowBackgroundWhiteGrayIcon), PorterDuff.Mode.MULTIPLY));
imageView.setVisibility(GONE);
addView(imageView, LayoutHelper.createFrame(LayoutParams.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.CENTER_VERTICAL, LocaleController.isRTL ? 0 : 16, 0, LocaleController.isRTL ? 16 : 0, 0));
2014-11-14 16:40:15 +01:00
2015-11-26 22:04:02 +01:00
if (checkbox == 2) {
2017-03-31 01:58:05 +02:00
checkBoxBig = new CheckBoxSquare(context, false);
2015-11-26 22:04:02 +01:00
addView(checkBoxBig, LayoutHelper.createFrame(18, 18, (LocaleController.isRTL ? Gravity.LEFT : Gravity.RIGHT) | Gravity.CENTER_VERTICAL, LocaleController.isRTL ? 19 : 0, 0, LocaleController.isRTL ? 0 : 19, 0));
} else if (checkbox == 1) {
checkBox = new CheckBox(context, R.drawable.round_check2);
checkBox.setVisibility(INVISIBLE);
2017-03-31 01:58:05 +02:00
checkBox.setColor(Theme.getColor(Theme.key_checkbox), Theme.getColor(Theme.key_checkboxCheck));
2015-11-26 22:04:02 +01:00
addView(checkBox, LayoutHelper.createFrame(22, 22, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP, LocaleController.isRTL ? 0 : 37 + padding, 38, LocaleController.isRTL ? 37 + padding : 0, 0));
}
2016-03-16 13:26:32 +01:00
if (admin) {
adminImage = new ImageView(context);
2017-03-31 01:58:05 +02:00
adminImage.setImageResource(R.drawable.admin_star);
2016-03-16 13:26:32 +01:00
addView(adminImage, LayoutHelper.createFrame(16, 16, (LocaleController.isRTL ? Gravity.LEFT : Gravity.RIGHT) | Gravity.TOP, LocaleController.isRTL ? 24 : 0, 13.5f, LocaleController.isRTL ? 0 : 24, 0));
}
}
2016-04-22 15:49:00 +02:00
public void setIsAdmin(int value) {
2016-03-16 13:26:32 +01:00
if (adminImage == null) {
return;
}
2016-04-22 15:49:00 +02:00
adminImage.setVisibility(value != 0 ? VISIBLE : GONE);
nameTextView.setPadding(LocaleController.isRTL && value != 0 ? AndroidUtilities.dp(16) : 0, 0, !LocaleController.isRTL && value != 0 ? AndroidUtilities.dp(16) : 0, 0);
if (value == 1) {
2017-03-31 01:58:05 +02:00
setTag(Theme.key_profile_creatorIcon);
adminImage.setColorFilter(new PorterDuffColorFilter(Theme.getColor(Theme.key_profile_creatorIcon), PorterDuff.Mode.MULTIPLY));
2016-04-22 15:49:00 +02:00
} else if (value == 2) {
2017-03-31 01:58:05 +02:00
setTag(Theme.key_profile_adminIcon);
adminImage.setColorFilter(new PorterDuffColorFilter(Theme.getColor(Theme.key_profile_adminIcon), PorterDuff.Mode.MULTIPLY));
2016-04-22 15:49:00 +02:00
}
}
2015-09-24 22:52:02 +02:00
public void setData(TLObject user, CharSequence name, CharSequence status, int resId) {
2018-07-30 04:07:02 +02:00
if (user == null && name == null && status == null) {
2014-11-12 23:16:59 +01:00
currrntStatus = null;
currentName = null;
2015-09-24 22:52:02 +02:00
currentObject = null;
2014-11-12 23:16:59 +01:00
nameTextView.setText("");
statusTextView.setText("");
avatarImageView.setImageDrawable(null);
return;
}
currrntStatus = status;
currentName = name;
2015-09-24 22:52:02 +02:00
currentObject = user;
2014-11-12 23:16:59 +01:00
currentDrawable = resId;
update(0);
}
2018-07-30 04:07:02 +02:00
public void setNameTypeface(Typeface typeface) {
nameTextView.setTypeface(typeface);
}
public void setCurrentId(int id) {
currentId = id;
}
2014-11-21 20:36:21 +01:00
public void setChecked(boolean checked, boolean animated) {
2015-11-26 22:04:02 +01:00
if (checkBox != null) {
if (checkBox.getVisibility() != VISIBLE) {
checkBox.setVisibility(VISIBLE);
}
checkBox.setChecked(checked, animated);
} else if (checkBoxBig != null) {
if (checkBoxBig.getVisibility() != VISIBLE) {
checkBoxBig.setVisibility(VISIBLE);
}
checkBoxBig.setChecked(checked, animated);
}
}
public void setCheckDisabled(boolean disabled) {
if (checkBoxBig != null) {
checkBoxBig.setDisabled(disabled);
2014-11-14 16:40:15 +01:00
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
2015-10-29 18:10:07 +01:00
super.onMeasure(MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(64), MeasureSpec.EXACTLY));
}
2014-11-12 23:16:59 +01:00
public void setStatusColors(int color, int onlineColor) {
statusColor = color;
statusOnlineColor = onlineColor;
}
2017-03-31 01:58:05 +02:00
@Override
public void invalidate() {
super.invalidate();
if (checkBoxBig != null) {
checkBoxBig.invalidate();
}
}
public void update(int mask) {
TLRPC.FileLocation photo = null;
String newName = null;
2015-09-24 22:52:02 +02:00
TLRPC.User currentUser = null;
TLRPC.Chat currentChat = null;
if (currentObject instanceof TLRPC.User) {
currentUser = (TLRPC.User) currentObject;
if (currentUser.photo != null) {
photo = currentUser.photo.photo_small;
}
2018-07-30 04:07:02 +02:00
} else if (currentObject instanceof TLRPC.Chat) {
2015-09-24 22:52:02 +02:00
currentChat = (TLRPC.Chat) currentObject;
if (currentChat.photo != null) {
photo = currentChat.photo.photo_small;
}
}
if (mask != 0) {
boolean continueUpdate = false;
2014-11-12 23:16:59 +01:00
if ((mask & MessagesController.UPDATE_MASK_AVATAR) != 0) {
if (lastAvatar != null && photo == null || lastAvatar == null && photo != null && lastAvatar != null && photo != null && (lastAvatar.volume_id != photo.volume_id || lastAvatar.local_id != photo.local_id)) {
continueUpdate = true;
}
}
2015-09-24 22:52:02 +02:00
if (currentUser != null && !continueUpdate && (mask & MessagesController.UPDATE_MASK_STATUS) != 0) {
int newStatus = 0;
2014-11-12 23:16:59 +01:00
if (currentUser.status != null) {
newStatus = currentUser.status.expires;
}
if (newStatus != lastStatus) {
continueUpdate = true;
}
}
if (!continueUpdate && currentName == null && lastName != null && (mask & MessagesController.UPDATE_MASK_NAME) != 0) {
2015-09-24 22:52:02 +02:00
if (currentUser != null) {
newName = UserObject.getUserName(currentUser);
} else {
newName = currentChat.title;
}
if (!newName.equals(lastName)) {
continueUpdate = true;
}
}
if (!continueUpdate) {
return;
}
}
2015-09-24 22:52:02 +02:00
if (currentUser != null) {
avatarDrawable.setInfo(currentUser);
if (currentUser.status != null) {
lastStatus = currentUser.status.expires;
} else {
lastStatus = 0;
}
2018-07-30 04:07:02 +02:00
} else if (currentChat != null) {
2015-09-24 22:52:02 +02:00
avatarDrawable.setInfo(currentChat);
2018-07-30 04:07:02 +02:00
} else if (currentName != null) {
avatarDrawable.setInfo(currentId, currentName.toString(), null, false);
} else {
avatarDrawable.setInfo(currentId, "#", null, false);
}
2014-11-12 23:16:59 +01:00
if (currentName != null) {
lastName = null;
2014-11-12 23:16:59 +01:00
nameTextView.setText(currentName);
} else {
2015-09-24 22:52:02 +02:00
if (currentUser != null) {
lastName = newName == null ? UserObject.getUserName(currentUser) : newName;
} else {
lastName = newName == null ? currentChat.title : newName;
}
nameTextView.setText(lastName);
}
2014-11-12 23:16:59 +01:00
if (currrntStatus != null) {
statusTextView.setTextColor(statusColor);
statusTextView.setText(currrntStatus);
2015-09-24 22:52:02 +02:00
} else if (currentUser != null) {
2015-11-26 22:04:02 +01:00
if (currentUser.bot) {
2014-11-12 23:16:59 +01:00
statusTextView.setTextColor(statusColor);
2017-03-31 01:58:05 +02:00
if (currentUser.bot_chat_history || adminImage != null && adminImage.getVisibility() == VISIBLE) {
2015-06-29 19:12:11 +02:00
statusTextView.setText(LocaleController.getString("BotStatusRead", R.string.BotStatusRead));
} else {
statusTextView.setText(LocaleController.getString("BotStatusCantRead", R.string.BotStatusCantRead));
}
} else {
2018-07-30 04:07:02 +02:00
if (currentUser.id == UserConfig.getInstance(currentAccount).getClientUserId() || currentUser.status != null && currentUser.status.expires > ConnectionsManager.getInstance(currentAccount).getCurrentTime() || MessagesController.getInstance(currentAccount).onlinePrivacy.containsKey(currentUser.id)) {
2015-06-29 19:12:11 +02:00
statusTextView.setTextColor(statusOnlineColor);
statusTextView.setText(LocaleController.getString("Online", R.string.Online));
} else {
statusTextView.setTextColor(statusColor);
2018-07-30 04:07:02 +02:00
statusTextView.setText(LocaleController.formatUserStatus(currentAccount, currentUser));
2015-06-29 19:12:11 +02:00
}
2014-10-21 22:35:16 +02:00
}
}
if (imageView.getVisibility() == VISIBLE && currentDrawable == 0 || imageView.getVisibility() == GONE && currentDrawable != 0) {
imageView.setVisibility(currentDrawable == 0 ? GONE : VISIBLE);
imageView.setImageResource(currentDrawable);
}
2014-11-12 23:16:59 +01:00
avatarImageView.setImage(photo, "50_50", avatarDrawable);
}
2015-10-29 18:10:07 +01:00
@Override
public boolean hasOverlappingRendering() {
return false;
}
}