public DialogView(Context context) { super(context); checkResources(context); this.application = (TelegramApplication) context.getApplicationContext(); this.loader = application.getUiKernel().getAvatarLoader(); this.currentUserUid = application.getCurrentUid(); application.getTypingStates().registerListener(this); avatarBgPaint = new Paint(); avatarBgPaint.setStyle(Paint.Style.FILL); statePending = getResources().getDrawable(R.drawable.st_dialogs_clock); stateSent = getResources().getDrawable(R.drawable.st_dialogs_check); stateHalfCheck = getResources().getDrawable(R.drawable.st_dialogs_halfcheck); stateFailure = getResources().getDrawable(R.drawable.st_dialogs_warning); secureIcon = getResources().getDrawable(R.drawable.st_dialogs_lock); userPlaceHolder = ((BitmapDrawable) getResources().getDrawable(R.drawable.st_user_placeholder_dialog)) .getBitmap(); groupPlaceHolder = ((BitmapDrawable) getResources().getDrawable(R.drawable.st_group_placeholder)).getBitmap(); }
public MediaLoader(TelegramApplication application) { super("previews", 5, application); outMediaMask = (NinePatchDrawable) application.getResources().getDrawable(R.drawable.st_bubble_out_media_content); inMediaMask = (NinePatchDrawable) application.getResources().getDrawable(R.drawable.st_bubble_in_media_content); }
public void setDescription(DialogWireframe description, Object preparedLayouts) { this.preparedLayouts = (DialogLayout[]) preparedLayouts; this.description = description; this.state = description.getMessageState(); if (description.getPeerType() == PeerType.PEER_CHAT) { this.typingUids = application.getTypingStates().getChatTypes(description.getPeerId()); } else { this.userTypes = application.getTypingStates().isUserTyping(description.getPeerId()); } photo = description.getDialogAvatar(); if (description.getPeerType() == PeerType.PEER_USER_ENCRYPTED) { EncryptedChat encryptedChat = application.getEngine().getEncryptedChat(description.getPeerId()); avatarBgPaint.setColor(Placeholders.getBgColor(encryptedChat.getUserId())); placeholder = userPlaceHolder; } else if (description.getPeerType() == PeerType.PEER_USER) { avatarBgPaint.setColor(Placeholders.getBgColor(description.getPeerId())); placeholder = userPlaceHolder; } else if (description.getPeerType() == PeerType.PEER_CHAT) { avatarBgPaint.setColor(Placeholders.getBgColor(description.getPeerId())); placeholder = groupPlaceHolder; } else { throw new UnsupportedOperationException("Unknown peer type #" + description.getPeerType()); } if (photo instanceof TLLocalAvatarPhoto) { if (!loader.requestAvatar( ((TLLocalAvatarPhoto) photo).getPreviewLocation(), IS_LARGE ? AvatarLoader.TYPE_MEDIUM2 : AvatarLoader.TYPE_MEDIUM, this)) { releaseAvatar(); } } else { releaseAvatar(); loader.cancelRequest(this); } if (getMeasuredHeight() != 0 || getMeasuredWidth() != 0) { buildLayout(); invalidate(); } else { requestLayout(); } needNewUpdateTyping = true; }
private void updateTyping() { needNewUpdateTyping = false; if (description.getPeerType() == PeerType.PEER_USER || description.getPeerType() == PeerType.PEER_USER_ENCRYPTED) { if (userTypes) { typingLayout = new StaticLayout( getResources().getString(R.string.lang_common_typing), bodyHighlightPaint, layout.layoutMainWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false); } else { typingLayout = null; } } else { if (typingUids != null && typingUids.length != 0) { String[] names = new String[typingUids.length]; for (int i = 0; i < names.length; i++) { names[i] = application.getEngine().getUserRuntime(typingUids[i]).getFirstName(); } String typing = TextUtil.formatTyping(names); Spannable spannable = application .getEmojiProcessor() .processEmojiCutMutable(typing, EmojiProcessor.CONFIGURATION_DIALOGS); CharSequence sequence = TextUtils.ellipsize( spannable, bodyHighlightPaint, layout.layoutMainWidth, TextUtils.TruncateAt.END); typingLayout = new StaticLayout( sequence, bodyHighlightPaint, layout.layoutMainWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false); } else { typingLayout = null; } } }
public SyncStateEngine(TelegramApplication application) { syncPrefs = application.getSharedPreferences("org.telegram.android.Sync.prefs", Context.MODE_PRIVATE); }
public void build(DialogWireframe description, int w, int h, TelegramApplication application) { layoutH = h; layoutW = w; if (description.getPeerType() == PeerType.PEER_USER) { if (description.getPeerId() == 333000) { isHighlighted = false; } else { User user = description.getDialogUser(); isHighlighted = user.getLinkType() == LinkType.FOREIGN; } isGroup = false; isEncrypted = false; } else if (description.getPeerType() == PeerType.PEER_CHAT) { isHighlighted = false; isGroup = true; isEncrypted = false; } else if (description.getPeerType() == PeerType.PEER_USER_ENCRYPTED) { isHighlighted = false; isGroup = false; isEncrypted = true; } isBodyHighlighted = description.getContentType() != ContentType.MESSAGE_TEXT; if (description.getUnreadCount() != 0 && !description.isMine()) { isUnreadIn = true; } else { isUnreadIn = false; } time = org.telegram.android.ui.TextUtil.formatDate(description.getDate(), application); isRtl = application.isRTL(); if (IS_LARGE) { layoutAvatarWidth = px(64); layoutPadding = application.getResources().getDimensionPixelSize(R.dimen.dialogs_padding); layoutBodyPadding = layoutAvatarWidth + layoutPadding + px(12); layoutAvatarTop = px(8); layoutTitleTop = px(34); layoutMainTop = px(60); layoutTimeTop = px(34); layoutMarkTop = px(44); layoutMarkBottom = layoutMarkTop + px(22); layoutMarkTextTop = layoutMarkTop + px(18); } else { layoutAvatarWidth = px(54); layoutPadding = application.getResources().getDimensionPixelSize(R.dimen.dialogs_padding); layoutBodyPadding = layoutAvatarWidth + layoutPadding + px(12); layoutAvatarTop = px(8); layoutTitleTop = px(30); layoutMainTop = px(54); layoutTimeTop = px(30); layoutMarkTop = px(38); layoutMarkBottom = layoutMarkTop + px(22); layoutMarkTextTop = layoutMarkTop + px(18); } layoutMainContentTop = (int) (layoutMainTop + bodyPaint.getFontMetrics().ascent); layoutTitleLayoutTop = (int) (layoutTitleTop + titlePaint.getFontMetrics().ascent); layoutStateTop = layoutTimeTop - px(10); layoutClockTop = layoutTimeTop - px(12); layoutEncryptedTop = layoutTimeTop - px(14); if (isRtl) { layoutAvatarLeft = w - layoutPadding - layoutAvatarWidth; } else { layoutAvatarLeft = layoutPadding; } int timeWidth = (int) unreadClockPaint.measureText(time); if (isRtl) { layoutTimeLeft = layoutPadding; layoutStateLeftDouble = layoutPadding + timeWidth + px(2); layoutStateLeft = layoutStateLeftDouble + px(6); layoutClockLeft = layoutPadding + timeWidth + px(2); } else { layoutTimeLeft = w - layoutPadding - timeWidth; layoutClockLeft = w - layoutPadding - timeWidth - px(14); layoutStateLeft = w - layoutPadding - timeWidth - px(16); layoutStateLeftDouble = w - layoutPadding - timeWidth - px(6 + 16); } layoutMarkRadius = px(2); if (description.isErrorState() || (description.getMessageState() == MessageState.FAILURE && description.isMine())) { layoutMarkWidth = px(22); if (isRtl) { layoutMarkLeft = layoutPadding; // getMeasuredWidth() - layoutMarkWidth - getPx(80); } else { layoutMarkLeft = w - layoutMarkWidth - layoutPadding; } } else { if (description.getUnreadCount() > 0) { if (description.getUnreadCount() >= 1000) { unreadCountText = I18nUtil.getInstance().correctFormatNumber(description.getUnreadCount() / 1000) + "K"; } else { unreadCountText = I18nUtil.getInstance().correctFormatNumber(description.getUnreadCount()); } int width = (int) counterTitlePaint.measureText(unreadCountText); Rect r = new Rect(); counterTitlePaint.getTextBounds(unreadCountText, 0, unreadCountText.length(), r); layoutMarkTextTop = layoutMarkTop + (layoutMarkBottom - layoutMarkTop + r.top) / 2 - r.top; if (width < px(22 - 14)) { layoutMarkWidth = px(22); } else { layoutMarkWidth = px(14) + width; } layoutMarkTextLeft = (layoutMarkWidth - width) / 2; if (isRtl) { layoutMarkLeft = layoutPadding; // getMeasuredWidth() - layoutMarkWidth - getPx(80); } else { layoutMarkLeft = w - layoutMarkWidth - layoutPadding; } } else { layoutMarkLeft = 0; layoutMarkWidth = 0; } } layoutMarkRect.set( layoutMarkLeft, layoutMarkTop, layoutMarkLeft + layoutMarkWidth, layoutMarkBottom); if (description.getPeerType() == PeerType.PEER_USER_ENCRYPTED) { if (isRtl) { if (description.isMine()) { layoutTitleLeft = timeWidth + px(16) + px(16); } else { layoutTitleLeft = timeWidth + px(12); } layoutTitleWidth = w - layoutTitleLeft - layoutBodyPadding - px(14) - px(6); layoutEncryptedLeft = w - layoutBodyPadding - px(12); } else { layoutTitleLeft = layoutBodyPadding + px(16); if (description.isMine()) { layoutTitleWidth = w - layoutTitleLeft - timeWidth - px(24); } else { layoutTitleWidth = w - layoutTitleLeft - timeWidth - px(12); } layoutEncryptedLeft = layoutBodyPadding + px(2); } } else { if (isRtl) { if (description.isMine()) { layoutTitleLeft = timeWidth + px(16) + px(16); } else { layoutTitleLeft = timeWidth + px(12); } layoutTitleWidth = w - layoutTitleLeft - layoutBodyPadding; } else { layoutTitleLeft = layoutBodyPadding; if (description.isMine()) { layoutTitleWidth = w - layoutTitleLeft - timeWidth - px(24) - px(12); } else { layoutTitleWidth = w - layoutTitleLeft - timeWidth - px(12); } } } layoutMainWidth = w - layoutBodyPadding - layoutPadding; if (isRtl) { layoutMainLeft = w - layoutMainWidth - layoutBodyPadding; if (layoutMarkWidth != 0) { layoutMainLeft += layoutMarkWidth + px(8); layoutMainWidth -= layoutMarkWidth + px(8); } } else { layoutMainLeft = layoutBodyPadding; if (layoutMarkWidth != 0) { layoutMainWidth -= layoutMarkWidth + px(8); } } avatarRect.set( layoutAvatarLeft, layoutAvatarTop, layoutAvatarLeft + layoutAvatarWidth, layoutAvatarTop + layoutAvatarWidth); // Building text layouts { String message = description.getMessage(); if (message.length() > 150) { message = message.substring(0, 150) + "..."; } message = message.replace("\n", " "); TextPaint bodyTextPaint; if (isBodyHighlighted) { bodyTextPaint = bodyHighlightPaint; } else { if (HIGHLIGHT_UNDEAD) { if (isUnreadIn) { bodyTextPaint = bodyUnreadPaint; } else { bodyTextPaint = bodyPaint; } } else { bodyTextPaint = bodyPaint; } } int nameLength = 0; if (description.getContentType() != ContentType.MESSAGE_SYSTEM) { if (description.isMine()) { String name = application.getResources().getString(R.string.st_dialog_you); nameLength = BidiFormatter.getInstance().unicodeWrap(name).length(); message = BidiFormatter.getInstance().unicodeWrap(name) + ": " + BidiFormatter.getInstance().unicodeWrap(message); } else { if (isGroup) { User user = description.getSender(); nameLength = BidiFormatter.getInstance().unicodeWrap(user.getFirstName()).length(); message = BidiFormatter.getInstance().unicodeWrap(user.getFirstName().replace("\n", " ")) + ": " + BidiFormatter.getInstance().unicodeWrap(message); } } } String preSequence = TextUtils.ellipsize(message, bodyTextPaint, layoutMainWidth, TextUtils.TruncateAt.END) .toString(); // Spannable sequence = // application.getEmojiProcessor().processEmojiCutMutable(preSequence, // EmojiProcessor.CONFIGURATION_DIALOGS); // if (nameLength != 0) { // sequence.setSpan(new ForegroundColorSpan(HIGHLIGHT_COLOR), 0, // Math.min(nameLength, sequence.length()), Spanned.SPAN_EXCLUSIVE_INCLUSIVE); // } // CharSequence resSequence = TextUtils.ellipsize(sequence, bodyTextPaint, // layoutMainWidth, TextUtils.TruncateAt.END); // bodyLayout = new StaticLayout(resSequence, bodyTextPaint, layoutMainWidth, // Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false); // bodyString = null; Spannable sequence = application .getEmojiProcessor() .processEmojiCutMutable(preSequence, EmojiProcessor.CONFIGURATION_DIALOGS); if (nameLength != 0) { sequence.setSpan( new ForegroundColorSpan(HIGHLIGHT_COLOR), 0, Math.min(nameLength, sequence.length()), Spanned.SPAN_EXCLUSIVE_INCLUSIVE); } CharSequence resSequence = TextUtils.ellipsize(sequence, bodyTextPaint, layoutMainWidth, TextUtils.TruncateAt.END); bodyLayout = new StaticLayout( resSequence, bodyTextPaint, layoutMainWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false); bodyString = null; // if (EmojiProcessor.containsEmoji(message)) { // Spannable sequence = // application.getEmojiProcessor().processEmojiCutMutable(preSequence, // EmojiProcessor.CONFIGURATION_DIALOGS); // if (nameLength != 0) { // sequence.setSpan(new ForegroundColorSpan(HIGHLIGHT_COLOR), 0, // Math.min(nameLength, sequence.length()), Spanned.SPAN_EXCLUSIVE_INCLUSIVE); // } // // CharSequence resSequence = TextUtils.ellipsize(sequence, // bodyTextPaint, layoutMainWidth, TextUtils.TruncateAt.END); // bodyLayout = new StaticLayout(resSequence, bodyTextPaint, // layoutMainWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false); // bodyString = null; // } else { // bodyString = preSequence; // bodyLayout = null; // } } // Title { String title = description.getDialogTitle(); if (title.length() > 150) { title = title.substring(150) + "..."; } title = title.replace("\n", " "); TextPaint paint = isEncrypted ? titleEncryptedPaint : (isHighlighted ? titleHighlightPaint : titlePaint); // Spannable preSequence = // application.getEmojiProcessor().processEmojiCutMutable(title, // EmojiProcessor.CONFIGURATION_DIALOGS); // CharSequence sequence = TextUtils.ellipsize(preSequence, paint, // layoutTitleWidth, TextUtils.TruncateAt.END); // titleLayout = new StaticLayout(sequence, paint, layoutTitleWidth, // Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false); // titleString = null; if (EmojiProcessor.containsEmoji(title)) { Spannable preSequence = application .getEmojiProcessor() .processEmojiCutMutable(title, EmojiProcessor.CONFIGURATION_DIALOGS); CharSequence sequence = TextUtils.ellipsize(preSequence, paint, layoutTitleWidth, TextUtils.TruncateAt.END); titleLayout = new StaticLayout( sequence, paint, layoutTitleWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false); titleString = null; } else { titleString = TextUtils.ellipsize(title, paint, layoutTitleWidth, TextUtils.TruncateAt.END) .toString(); titleLayout = null; } } // Placeholder placeHolderName = description.getDialogName(); placeHolderColor = Placeholders.getBgColor(description.getPeerId()); if (placeHolderName.length() > 0) { usePlaceholder = true; placeholderLeft = layoutAvatarLeft + layoutAvatarWidth / 2; Rect rect = new Rect(); placeholderTextPaint.getTextBounds(placeHolderName, 0, placeHolderName.length(), rect); placeholderTop = layoutAvatarTop + (layoutAvatarWidth / 2 + ((rect.bottom - rect.top) / 2)); } else { usePlaceholder = false; } }
private static void checkResources(Context context) { if (!isLoaded) { TelegramApplication application = (TelegramApplication) context.getApplicationContext(); IS_LARGE = application.getUserSettings().getDialogItemSize() == UserSettings.DIALOG_SIZE_LARGE; avatarPaint = new Paint(); if (FontController.USE_SUBPIXEL) { titlePaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG); } else { titlePaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG); } titlePaint.setColor(0xff222222); if (IS_LARGE) { titlePaint.setTextSize(sp(20f)); titlePaint.setTypeface(FontController.loadTypeface(context, "medium")); } else { titlePaint.setTextSize(sp(18f)); titlePaint.setTypeface(FontController.loadTypeface(context, "medium")); } if (FontController.USE_SUBPIXEL) { titleHighlightPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG); } else { titleHighlightPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG); } titleHighlightPaint.setColor(0xff006FC8); if (IS_LARGE) { titleHighlightPaint.setTextSize(sp(20f)); titleHighlightPaint.setTypeface(FontController.loadTypeface(context, "medium")); } else { titleHighlightPaint.setTextSize(sp(18f)); titleHighlightPaint.setTypeface(FontController.loadTypeface(context, "medium")); } if (FontController.USE_SUBPIXEL) { titleEncryptedPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG); } else { titleEncryptedPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG); } titleEncryptedPaint.setColor(0xff368c3e); if (IS_LARGE) { titleEncryptedPaint.setTextSize(sp(20f)); titleEncryptedPaint.setTypeface(FontController.loadTypeface(context, "medium")); } else { titleEncryptedPaint.setTextSize(sp(18f)); titleEncryptedPaint.setTypeface(FontController.loadTypeface(context, "medium")); } if (FontController.USE_SUBPIXEL) { unreadClockPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG); } else { unreadClockPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG); } unreadClockPaint.setColor(UNREAD_TIME_COLOR); unreadClockPaint.setTextSize(sp(14)); unreadClockPaint.setTypeface(FontController.loadTypeface(context, "regular")); if (FontController.USE_SUBPIXEL) { readClockPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG); } else { readClockPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG); } readClockPaint.setColor(READ_TIME_COLOR); readClockPaint.setTextSize(sp(14)); readClockPaint.setTypeface(FontController.loadTypeface(context, "regular")); if (FontController.USE_SUBPIXEL) { bodyPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG); } else { bodyPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG); } bodyPaint.setColor(READ_COLOR); bodyPaint.setTextSize(sp(17f)); bodyPaint.setTypeface(FontController.loadTypeface(context, "regular")); if (FontController.USE_SUBPIXEL) { bodyUnreadPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG); } else { bodyUnreadPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG); } bodyUnreadPaint.setColor(UNREAD_COLOR); bodyUnreadPaint.setTextSize(sp(17f)); bodyUnreadPaint.setTypeface(FontController.loadTypeface(context, "regular")); if (FontController.USE_SUBPIXEL) { bodyHighlightPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG); } else { bodyHighlightPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG); } bodyHighlightPaint.setColor(HIGHLIGHT_COLOR); bodyHighlightPaint.setTextSize(sp(17f)); bodyHighlightPaint.setTypeface(FontController.loadTypeface(context, "regular")); if (FontController.USE_SUBPIXEL) { typingPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG); } else { typingPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG); } typingPaint.setColor(0xff006FC8); typingPaint.setTextSize(sp(16f)); typingPaint.setTypeface(FontController.loadTypeface(context, "regular")); if (FontController.USE_SUBPIXEL) { counterTitlePaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG); } else { counterTitlePaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG); } counterTitlePaint.setColor(0xffffffff); if (IS_LARGE) { counterTitlePaint.setTextSize(sp(15.5f)); } else { counterTitlePaint.setTextSize(sp(14f)); } counterTitlePaint.setTypeface(FontController.loadTypeface(context, "regular")); counterPaint = new Paint(); counterPaint.setColor(0xff8fc478); counterPaint.setAntiAlias(true); placeholderPaint = new Paint(); placeholderPaint.setAntiAlias(true); if (FontController.USE_SUBPIXEL) { placeholderTextPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG); } else { placeholderTextPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG); } placeholderTextPaint.setColor(0xffffffff); placeholderTextPaint.setTextSize(px(28f)); placeholderTextPaint.setTypeface(FontController.loadTypeface(context, "regular")); placeholderTextPaint.setTextAlign(Paint.Align.CENTER); isLoaded = true; } }