Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 150.67 KB | None | 0 0
  1. private void setMessageContent(MessageObject messageObject, MessageObject.GroupedMessages groupedMessages, boolean bottomNear, boolean topNear) {
  2. if (messageObject.checkLayout() || currentPosition != null && lastHeight != AndroidUtilities.displaySize.y) {
  3. currentMessageObject = null;
  4. }
  5. lastHeight = AndroidUtilities.displaySize.y;
  6. boolean messageIdChanged = currentMessageObject == null || currentMessageObject.getId() != messageObject.getId();
  7. boolean messageChanged = currentMessageObject != messageObject || messageObject.forceUpdate;
  8. boolean dataChanged = currentMessageObject != null && currentMessageObject.getId() == messageObject.getId() && lastSendState == MessageObject.MESSAGE_SEND_STATE_EDITING && messageObject.isSent()
  9. || currentMessageObject == messageObject && (isUserDataChanged() || photoNotSet);
  10. boolean groupChanged = groupedMessages != currentMessagesGroup;
  11. boolean pollChanged = false;
  12. if (!messageChanged && messageObject.type == MessageObject.TYPE_POLL) {
  13. ArrayList<TLRPC.TL_pollAnswerVoters> newResults = null;
  14. TLRPC.TL_poll newPoll = null;
  15. int newVoters = 0;
  16. if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaPoll) {
  17. TLRPC.TL_messageMediaPoll mediaPoll = (TLRPC.TL_messageMediaPoll) messageObject.messageOwner.media;
  18. newResults = mediaPoll.results.results;
  19. newPoll = mediaPoll.poll;
  20. newVoters = mediaPoll.results.total_voters;
  21. }
  22. if (newResults != null && lastPollResults != null && newVoters != lastPollResultsVoters) {
  23. pollChanged = true;
  24. }
  25. if (!pollChanged && newResults != lastPollResults) {
  26. pollChanged = true;
  27. }
  28. if (!pollChanged && lastPoll != newPoll && lastPoll.closed != newPoll.closed) {
  29. pollChanged = true;
  30. }
  31. if (pollChanged && attachedToWindow) {
  32. pollAnimationProgressTime = 0.0f;
  33. if (pollVoted && !messageObject.isVoted()) {
  34. pollUnvoteInProgress = true;
  35. }
  36. }
  37. }
  38. if (!groupChanged && groupedMessages != null) {
  39. MessageObject.GroupedMessagePosition newPosition;
  40. if (groupedMessages.messages.size() > 1) {
  41. newPosition = currentMessagesGroup.positions.get(currentMessageObject);
  42. } else {
  43. newPosition = null;
  44. }
  45. groupChanged = newPosition != currentPosition;
  46. }
  47. if (messageChanged || dataChanged || groupChanged || pollChanged || isPhotoDataChanged(messageObject) || pinnedBottom != bottomNear || pinnedTop != topNear) {
  48. pinnedBottom = bottomNear;
  49. pinnedTop = topNear;
  50. currentMessageObject = messageObject;
  51. currentMessagesGroup = groupedMessages;
  52. lastTime = -2;
  53. isHighlightedAnimated = false;
  54. widthBeforeNewTimeLine = -1;
  55. if (currentMessagesGroup != null && currentMessagesGroup.posArray.size() > 1) {
  56. currentPosition = currentMessagesGroup.positions.get(currentMessageObject);
  57. if (currentPosition == null) {
  58. currentMessagesGroup = null;
  59. }
  60. } else {
  61. currentMessagesGroup = null;
  62. currentPosition = null;
  63. }
  64. drawPinnedTop = pinnedTop && (currentPosition == null || (currentPosition.flags & MessageObject.POSITION_FLAG_TOP) != 0);
  65. drawPinnedBottom = pinnedBottom && (currentPosition == null || (currentPosition.flags & MessageObject.POSITION_FLAG_BOTTOM) != 0);
  66. photoImage.setCrossfadeWithOldImage(false);
  67. lastSendState = messageObject.messageOwner.send_state;
  68. lastDeleteDate = messageObject.messageOwner.destroyTime;
  69. lastViewsCount = messageObject.messageOwner.views;
  70. isPressed = false;
  71. gamePreviewPressed = false;
  72. sharePressed = false;
  73. isCheckPressed = true;
  74. hasNewLineForTime = false;
  75. isAvatarVisible = isChat && !messageObject.isOutOwner() && messageObject.needDrawAvatar() && (currentPosition == null || currentPosition.edge);
  76. wasLayout = false;
  77. drwaShareGoIcon = false;
  78. groupPhotoInvisible = false;
  79. animatingDrawVideoImageButton = 0;
  80. drawVideoSize = false;
  81. canStreamVideo = false;
  82. animatingNoSound = 0;
  83. drawShareButton = checkNeedDrawShareButton(messageObject);
  84. replyNameLayout = null;
  85. adminLayout = null;
  86. checkOnlyButtonPressed = false;
  87. replyTextLayout = null;
  88. hasEmbed = false;
  89. autoPlayingMedia = false;
  90. replyNameWidth = 0;
  91. replyTextWidth = 0;
  92. viaWidth = 0;
  93. viaNameWidth = 0;
  94. addedCaptionHeight = 0;
  95. currentReplyPhoto = null;
  96. currentUser = null;
  97. currentChat = null;
  98. currentViaBotUser = null;
  99. instantViewLayout = null;
  100. drawNameLayout = false;
  101. if (scheduledInvalidate) {
  102. AndroidUtilities.cancelRunOnUIThread(invalidateRunnable);
  103. scheduledInvalidate = false;
  104. }
  105.  
  106. resetPressedLink(-1);
  107. messageObject.forceUpdate = false;
  108. drawPhotoImage = false;
  109. drawPhotoCheckBox = false;
  110. hasLinkPreview = false;
  111. hasOldCaptionPreview = false;
  112. hasGamePreview = false;
  113. hasInvoicePreview = false;
  114. instantPressed = instantButtonPressed = false;
  115. if (!pollChanged && Build.VERSION.SDK_INT >= 21 && selectorDrawable != null) {
  116. selectorDrawable.setVisible(false, false);
  117. selectorDrawable.setState(StateSet.NOTHING);
  118. }
  119. linkPreviewPressed = false;
  120. buttonPressed = 0;
  121. miniButtonPressed = 0;
  122. pressedBotButton = -1;
  123. pressedVoteButton = -1;
  124. linkPreviewHeight = 0;
  125. mediaOffsetY = 0;
  126. documentAttachType = DOCUMENT_ATTACH_TYPE_NONE;
  127. documentAttach = null;
  128. descriptionLayout = null;
  129. titleLayout = null;
  130. videoInfoLayout = null;
  131. photosCountLayout = null;
  132. siteNameLayout = null;
  133. authorLayout = null;
  134. captionLayout = null;
  135. captionOffsetX = 0;
  136. currentCaption = null;
  137. docTitleLayout = null;
  138. drawImageButton = false;
  139. drawVideoImageButton = false;
  140. currentPhotoObject = null;
  141. photoParentObject = null;
  142. currentPhotoObjectThumb = null;
  143. currentPhotoFilter = null;
  144. infoLayout = null;
  145. cancelLoading = false;
  146. buttonState = -1;
  147. miniButtonState = -1;
  148. hasMiniProgress = 0;
  149. if (addedForTest && currentUrl != null && currentWebFile != null) {
  150. ImageLoader.getInstance().removeTestWebFile(currentUrl);
  151. }
  152. addedForTest = false;
  153. currentUrl = null;
  154. currentWebFile = null;
  155. photoNotSet = false;
  156. drawBackground = true;
  157. drawName = false;
  158. useSeekBarWaweform = false;
  159. drawInstantView = false;
  160. drawInstantViewType = 0;
  161. drawForwardedName = false;
  162. photoImage.setSideClip(0);
  163. imageBackgroundColor = 0;
  164. imageBackgroundSideColor = 0;
  165. mediaBackground = false;
  166. photoImage.setAlpha(1.0f);
  167. if (messageChanged || dataChanged) {
  168. pollButtons.clear();
  169. }
  170. int captionNewLine = 0;
  171. availableTimeWidth = 0;
  172. photoImage.setForceLoading(false);
  173. photoImage.setNeedsQualityThumb(false);
  174. photoImage.setShouldGenerateQualityThumb(false);
  175. photoImage.setAllowDecodeSingleFrame(false);
  176. photoImage.setRoundRadius(AndroidUtilities.dp(4));
  177. photoImage.setColorFilter(null);
  178.  
  179. if (messageChanged) {
  180. firstVisibleBlockNum = 0;
  181. lastVisibleBlockNum = 0;
  182. needNewVisiblePart = true;
  183. }
  184.  
  185. if (messageObject.type == 0) {
  186. drawForwardedName = true;
  187.  
  188. int maxWidth;
  189. if (AndroidUtilities.isTablet()) {
  190. if (isChat && !messageObject.isOutOwner() && messageObject.needDrawAvatar()) {
  191. maxWidth = AndroidUtilities.getMinTabletSide() - AndroidUtilities.dp(122);
  192. drawName = true;
  193. } else {
  194. drawName = messageObject.messageOwner.to_id.channel_id != 0 && !messageObject.isOutOwner();
  195. maxWidth = AndroidUtilities.getMinTabletSide() - AndroidUtilities.dp(80);
  196. }
  197. } else {
  198. if (isChat && !messageObject.isOutOwner() && messageObject.needDrawAvatar()) {
  199. maxWidth = Math.min(AndroidUtilities.displaySize.x, AndroidUtilities.displaySize.y) - AndroidUtilities.dp(122);
  200. drawName = true;
  201. } else {
  202. maxWidth = Math.min(AndroidUtilities.displaySize.x, AndroidUtilities.displaySize.y) - AndroidUtilities.dp(80);
  203. drawName = messageObject.messageOwner.to_id.channel_id != 0 && !messageObject.isOutOwner();
  204. }
  205. }
  206. availableTimeWidth = maxWidth;
  207. if (messageObject.isRoundVideo()) {
  208. availableTimeWidth -= Math.ceil(Theme.chat_audioTimePaint.measureText("00:00")) + (messageObject.isOutOwner() ? 0 : AndroidUtilities.dp(64));
  209. }
  210. measureTime(messageObject);
  211. int timeMore = timeWidth + AndroidUtilities.dp(6);
  212. if (messageObject.isOutOwner()) {
  213. timeMore += AndroidUtilities.dp(20.5f);
  214. }
  215.  
  216. hasGamePreview = messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaGame && messageObject.messageOwner.media.game instanceof TLRPC.TL_game;
  217. hasInvoicePreview = messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaInvoice;
  218. hasLinkPreview = messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaWebPage && messageObject.messageOwner.media.webpage instanceof TLRPC.TL_webPage;
  219. drawInstantView = hasLinkPreview && messageObject.messageOwner.media.webpage.cached_page != null;
  220. hasEmbed = hasLinkPreview && !TextUtils.isEmpty(messageObject.messageOwner.media.webpage.embed_url) && !messageObject.isGif();
  221. boolean slideshow = false;
  222. String siteName = hasLinkPreview ? messageObject.messageOwner.media.webpage.site_name : null;
  223. String webpageType = hasLinkPreview ? messageObject.messageOwner.media.webpage.type : null;
  224. if (!drawInstantView) {
  225. if ("telegram_channel".equals(webpageType)) {
  226. drawInstantView = true;
  227. drawInstantViewType = 1;
  228. } else if ("telegram_megagroup".equals(webpageType)) {
  229. drawInstantView = true;
  230. drawInstantViewType = 2;
  231. } else if ("telegram_message".equals(webpageType)) {
  232. drawInstantView = true;
  233. drawInstantViewType = 3;
  234. } else if ("telegram_background".equals(webpageType)) {
  235. drawInstantView = true;
  236. drawInstantViewType = 6;
  237. try {
  238. Uri url = Uri.parse(messageObject.messageOwner.media.webpage.url);
  239. int intensity = Utilities.parseInt(url.getQueryParameter("intensity"));
  240. String bgColor = url.getQueryParameter("bg_color");
  241. if (TextUtils.isEmpty(bgColor)) {
  242. TLRPC.Document document = messageObject.getDocument();
  243. if (document != null && "image/png".equals(document.mime_type)) {
  244. bgColor = "ffffff";
  245. }
  246. if (intensity == 0) {
  247. intensity = 50;
  248. }
  249. }
  250. if (bgColor != null) {
  251. imageBackgroundColor = Integer.parseInt(bgColor, 16) | 0xff000000;
  252. imageBackgroundSideColor = AndroidUtilities.getPatternSideColor(imageBackgroundColor);
  253. photoImage.setColorFilter(new PorterDuffColorFilter(AndroidUtilities.getPatternColor(imageBackgroundColor), PorterDuff.Mode.SRC_IN));
  254. photoImage.setAlpha(intensity / 100.0f);
  255. } else {
  256. String color = url.getLastPathSegment();
  257. if (color != null && color.length() == 6) {
  258. imageBackgroundColor = Integer.parseInt(color, 16) | 0xff000000;
  259. currentPhotoObject = new TLRPC.TL_photoSizeEmpty();
  260. currentPhotoObject.type = "s";
  261. currentPhotoObject.w = AndroidUtilities.dp(180);
  262. currentPhotoObject.h = AndroidUtilities.dp(150);
  263. currentPhotoObject.location = new TLRPC.TL_fileLocationUnavailable();
  264. }
  265. }
  266. } catch (Exception ignore) {
  267.  
  268. }
  269. } /*else if ("telegram_proxy".equals(webpageType)) {
  270. drawInstantView = true;
  271. drawInstantViewType = 4;
  272. }*/
  273. } else if (siteName != null) {
  274. siteName = siteName.toLowerCase();
  275. if ((siteName.equals("instagram") || siteName.equals("twitter") || "telegram_album".equals(webpageType)) && messageObject.messageOwner.media.webpage.cached_page instanceof TLRPC.TL_page &&
  276. (messageObject.messageOwner.media.webpage.photo instanceof TLRPC.TL_photo || MessageObject.isVideoDocument(messageObject.messageOwner.media.webpage.document))) {
  277. drawInstantView = false;
  278. slideshow = true;
  279. ArrayList<TLRPC.PageBlock> blocks = messageObject.messageOwner.media.webpage.cached_page.blocks;
  280. int count = 1;
  281. for (int a = 0; a < blocks.size(); a++) {
  282. TLRPC.PageBlock block = blocks.get(a);
  283. if (block instanceof TLRPC.TL_pageBlockSlideshow) {
  284. TLRPC.TL_pageBlockSlideshow b = (TLRPC.TL_pageBlockSlideshow) block;
  285. count = b.items.size();
  286. } else if (block instanceof TLRPC.TL_pageBlockCollage) {
  287. TLRPC.TL_pageBlockCollage b = (TLRPC.TL_pageBlockCollage) block;
  288. count = b.items.size();
  289. }
  290. }
  291. String str = LocaleController.formatString("Of", R.string.Of, 1, count);
  292. photosCountWidth = (int) Math.ceil(Theme.chat_durationPaint.measureText(str));
  293. photosCountLayout = new StaticLayout(str, Theme.chat_durationPaint, photosCountWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
  294. }
  295. }
  296. backgroundWidth = maxWidth;
  297. if (hasLinkPreview || hasGamePreview || hasInvoicePreview || maxWidth - messageObject.lastLineWidth < timeMore) {
  298. backgroundWidth = Math.max(backgroundWidth, messageObject.lastLineWidth) + AndroidUtilities.dp(31);
  299. backgroundWidth = Math.max(backgroundWidth, timeWidth + AndroidUtilities.dp(31));
  300. } else {
  301. int diff = backgroundWidth - messageObject.lastLineWidth;
  302. if (diff >= 0 && diff <= timeMore) {
  303. backgroundWidth = backgroundWidth + timeMore - diff + AndroidUtilities.dp(31);
  304. } else {
  305. backgroundWidth = Math.max(backgroundWidth, messageObject.lastLineWidth + timeMore) + AndroidUtilities.dp(31);
  306. }
  307. }
  308. availableTimeWidth = backgroundWidth - AndroidUtilities.dp(31);
  309. if (messageObject.isRoundVideo()) {
  310. availableTimeWidth -= Math.ceil(Theme.chat_audioTimePaint.measureText("00:00")) + (messageObject.isOutOwner() ? 0 : AndroidUtilities.dp(64));
  311. }
  312.  
  313. setMessageObjectInternal(messageObject);
  314.  
  315. backgroundWidth = messageObject.textWidth + ((hasGamePreview || hasInvoicePreview) ? AndroidUtilities.dp(10) : 0);
  316. totalHeight = messageObject.textHeight + AndroidUtilities.dp(19.5f) + namesOffset;
  317. if (drawPinnedTop) {
  318. namesOffset -= AndroidUtilities.dp(1);
  319. }
  320.  
  321. int maxChildWidth = Math.max(backgroundWidth, nameWidth);
  322. maxChildWidth = Math.max(maxChildWidth, forwardedNameWidth);
  323. maxChildWidth = Math.max(maxChildWidth, replyNameWidth);
  324. maxChildWidth = Math.max(maxChildWidth, replyTextWidth);
  325. int maxWebWidth = 0;
  326.  
  327. if (hasLinkPreview || hasGamePreview || hasInvoicePreview) {
  328. int linkPreviewMaxWidth;
  329. if (AndroidUtilities.isTablet()) {
  330. if (isChat && messageObject.needDrawAvatar() && !currentMessageObject.isOutOwner()) {
  331. linkPreviewMaxWidth = AndroidUtilities.getMinTabletSide() - AndroidUtilities.dp(132);
  332. } else {
  333. linkPreviewMaxWidth = AndroidUtilities.getMinTabletSide() - AndroidUtilities.dp(80);
  334. }
  335. } else {
  336. if (isChat && messageObject.needDrawAvatar() && !currentMessageObject.isOutOwner()) {
  337. linkPreviewMaxWidth = AndroidUtilities.displaySize.x - AndroidUtilities.dp(132);
  338. } else {
  339. linkPreviewMaxWidth = AndroidUtilities.displaySize.x - AndroidUtilities.dp(80);
  340. }
  341. }
  342. if (drawShareButton) {
  343. linkPreviewMaxWidth -= AndroidUtilities.dp(20);
  344. }
  345. String site_name;
  346. String title;
  347. String author;
  348. String description;
  349. TLRPC.Photo photo;
  350. TLRPC.Document document;
  351. WebFile webDocument;
  352. int duration;
  353. boolean smallImage;
  354. String type;
  355. if (hasLinkPreview) {
  356. TLRPC.TL_webPage webPage = (TLRPC.TL_webPage) messageObject.messageOwner.media.webpage;
  357. site_name = webPage.site_name;
  358. title = drawInstantViewType != 6 ? webPage.title : null;
  359. author = drawInstantViewType != 6 ? webPage.author : null;
  360. description = drawInstantViewType != 6 ? webPage.description : null;
  361. photo = webPage.photo;
  362. webDocument = null;
  363. document = webPage.document;
  364. type = webPage.type;
  365. duration = webPage.duration;
  366. if (site_name != null && photo != null && site_name.toLowerCase().equals("instagram")) {
  367. linkPreviewMaxWidth = Math.max(AndroidUtilities.displaySize.y / 3, currentMessageObject.textWidth);
  368. }
  369. boolean isSmallImageType = "app".equals(type) || "profile".equals(type) || "article".equals(type);
  370. smallImage = !slideshow && !drawInstantView && document == null && isSmallImageType;
  371. isSmallImage = !slideshow && !drawInstantView && document == null && description != null && type != null && isSmallImageType && currentMessageObject.photoThumbs != null;
  372. } else if (hasInvoicePreview) {
  373. TLRPC.TL_messageMediaInvoice invoice = (TLRPC.TL_messageMediaInvoice) messageObject.messageOwner.media;
  374. site_name = messageObject.messageOwner.media.title;
  375. title = null;
  376. description = null;
  377. photo = null;
  378. author = null;
  379. document = null;
  380. if (invoice.photo instanceof TLRPC.TL_webDocument) {
  381. webDocument = WebFile.createWithWebDocument(invoice.photo);
  382. } else {
  383. webDocument = null;
  384. }
  385. duration = 0;
  386. type = "invoice";
  387. isSmallImage = false;
  388. smallImage = false;
  389. } else {
  390. TLRPC.TL_game game = messageObject.messageOwner.media.game;
  391. site_name = game.title;
  392. title = null;
  393. webDocument = null;
  394. description = TextUtils.isEmpty(messageObject.messageText) ? game.description : null;
  395. photo = game.photo;
  396. author = null;
  397. document = game.document;
  398. duration = 0;
  399. type = "game";
  400. isSmallImage = false;
  401. smallImage = false;
  402. }
  403. if (drawInstantViewType == 6) {
  404. site_name = LocaleController.getString("ChatBackground", R.string.ChatBackground);
  405. }
  406.  
  407. int additinalWidth = hasInvoicePreview ? 0 : AndroidUtilities.dp(10);
  408. int restLinesCount = 3;
  409. int additionalHeight = 0;
  410. linkPreviewMaxWidth -= additinalWidth;
  411.  
  412. if (currentMessageObject.photoThumbs == null && photo != null) {
  413. currentMessageObject.generateThumbs(true);
  414. }
  415.  
  416. if (site_name != null) {
  417. try {
  418. int width = (int) Math.ceil(Theme.chat_replyNamePaint.measureText(site_name) + 1);
  419. siteNameLayout = new StaticLayout(site_name, Theme.chat_replyNamePaint, Math.min(width, linkPreviewMaxWidth), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
  420. siteNameRtl = siteNameLayout.getLineLeft(0) != 0;
  421. int height = siteNameLayout.getLineBottom(siteNameLayout.getLineCount() - 1);
  422. linkPreviewHeight += height;
  423. totalHeight += height;
  424. additionalHeight += height;
  425. siteNameWidth = width = siteNameLayout.getWidth();
  426. maxChildWidth = Math.max(maxChildWidth, width + additinalWidth);
  427. maxWebWidth = Math.max(maxWebWidth, width + additinalWidth);
  428. } catch (Exception e) {
  429. FileLog.e(e);
  430. }
  431. }
  432.  
  433. boolean titleIsRTL = false;
  434. if (title != null) {
  435. try {
  436. titleX = Integer.MAX_VALUE;
  437. if (linkPreviewHeight != 0) {
  438. linkPreviewHeight += AndroidUtilities.dp(2);
  439. totalHeight += AndroidUtilities.dp(2);
  440. }
  441. int restLines = 0;
  442. if (!isSmallImage || description == null) {
  443. titleLayout = StaticLayoutEx.createStaticLayout(title, Theme.chat_replyNamePaint, linkPreviewMaxWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, AndroidUtilities.dp(1), false, TextUtils.TruncateAt.END, linkPreviewMaxWidth, 4);
  444. } else {
  445. restLines = restLinesCount;
  446. titleLayout = generateStaticLayout(title, Theme.chat_replyNamePaint, linkPreviewMaxWidth, linkPreviewMaxWidth - AndroidUtilities.dp(48 + 4), restLinesCount, 4);
  447. restLinesCount -= titleLayout.getLineCount();
  448. }
  449. int height = titleLayout.getLineBottom(titleLayout.getLineCount() - 1);
  450. linkPreviewHeight += height;
  451. totalHeight += height;
  452. boolean checkForRtl = true;
  453. for (int a = 0; a < titleLayout.getLineCount(); a++) {
  454. int lineLeft = (int) titleLayout.getLineLeft(a);
  455. if (lineLeft != 0) {
  456. titleIsRTL = true;
  457. }
  458. if (titleX == Integer.MAX_VALUE) {
  459. titleX = -lineLeft;
  460. } else {
  461. titleX = Math.max(titleX, -lineLeft);
  462. }
  463. int width;
  464. if (lineLeft != 0) {
  465. width = titleLayout.getWidth() - lineLeft;
  466. } else {
  467. width = (int) Math.ceil(titleLayout.getLineWidth(a));
  468. }
  469. if (a < restLines || lineLeft != 0 && isSmallImage) {
  470. width += AndroidUtilities.dp(48 + 4);
  471. }
  472. maxChildWidth = Math.max(maxChildWidth, width + additinalWidth);
  473. maxWebWidth = Math.max(maxWebWidth, width + additinalWidth);
  474. }
  475. } catch (Exception e) {
  476. FileLog.e(e);
  477. }
  478. if (titleIsRTL && isSmallImage) {
  479. linkPreviewMaxWidth -= AndroidUtilities.dp(48);
  480. }
  481. }
  482.  
  483. boolean authorIsRTL = false;
  484. if (author != null && title == null) {
  485. try {
  486. if (linkPreviewHeight != 0) {
  487. linkPreviewHeight += AndroidUtilities.dp(2);
  488. totalHeight += AndroidUtilities.dp(2);
  489. }
  490. if (restLinesCount == 3 && (!isSmallImage || description == null)) {
  491. authorLayout = new StaticLayout(author, Theme.chat_replyNamePaint, linkPreviewMaxWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
  492. } else {
  493. authorLayout = generateStaticLayout(author, Theme.chat_replyNamePaint, linkPreviewMaxWidth, linkPreviewMaxWidth - AndroidUtilities.dp(48 + 4), restLinesCount, 1);
  494. restLinesCount -= authorLayout.getLineCount();
  495. }
  496. int height = authorLayout.getLineBottom(authorLayout.getLineCount() - 1);
  497. linkPreviewHeight += height;
  498. totalHeight += height;
  499. int lineLeft = (int) authorLayout.getLineLeft(0);
  500. authorX = -lineLeft;
  501. int width;
  502. if (lineLeft != 0) {
  503. width = authorLayout.getWidth() - lineLeft;
  504. authorIsRTL = true;
  505. } else {
  506. width = (int) Math.ceil(authorLayout.getLineWidth(0));
  507. }
  508. maxChildWidth = Math.max(maxChildWidth, width + additinalWidth);
  509. maxWebWidth = Math.max(maxWebWidth, width + additinalWidth);
  510. } catch (Exception e) {
  511. FileLog.e(e);
  512. }
  513. }
  514.  
  515. if (description != null) {
  516. try {
  517. descriptionX = 0;
  518. currentMessageObject.generateLinkDescription();
  519. if (linkPreviewHeight != 0) {
  520. linkPreviewHeight += AndroidUtilities.dp(2);
  521. totalHeight += AndroidUtilities.dp(2);
  522. }
  523. int restLines = 0;
  524. boolean allowAllLines = site_name != null && site_name.toLowerCase().equals("twitter");
  525. if (restLinesCount == 3 && !isSmallImage) {
  526. descriptionLayout = StaticLayoutEx.createStaticLayout(messageObject.linkDescription, Theme.chat_replyTextPaint, linkPreviewMaxWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, AndroidUtilities.dp(1), false, TextUtils.TruncateAt.END, linkPreviewMaxWidth, allowAllLines ? 100 : 6);
  527. } else {
  528. restLines = restLinesCount;
  529. descriptionLayout = generateStaticLayout(messageObject.linkDescription, Theme.chat_replyTextPaint, linkPreviewMaxWidth, linkPreviewMaxWidth - AndroidUtilities.dp(48 + 4), restLinesCount, allowAllLines ? 100 : 6);
  530. }
  531. int height = descriptionLayout.getLineBottom(descriptionLayout.getLineCount() - 1);
  532. linkPreviewHeight += height;
  533. totalHeight += height;
  534.  
  535. boolean hasRTL = false;
  536. for (int a = 0; a < descriptionLayout.getLineCount(); a++) {
  537. int lineLeft = (int) Math.ceil(descriptionLayout.getLineLeft(a));
  538. if (lineLeft != 0) {
  539. hasRTL = true;
  540. if (descriptionX == 0) {
  541. descriptionX = -lineLeft;
  542. } else {
  543. descriptionX = Math.max(descriptionX, -lineLeft);
  544. }
  545. }
  546. }
  547.  
  548. int textWidth = descriptionLayout.getWidth();
  549. for (int a = 0; a < descriptionLayout.getLineCount(); a++) {
  550. int lineLeft = (int) Math.ceil(descriptionLayout.getLineLeft(a));
  551. if (lineLeft == 0 && descriptionX != 0) {
  552. descriptionX = 0;
  553. }
  554.  
  555. int width;
  556. if (lineLeft != 0) {
  557. width = textWidth - lineLeft;
  558. } else {
  559. if (hasRTL) {
  560. width = textWidth;
  561. } else {
  562. width = Math.min((int) Math.ceil(descriptionLayout.getLineWidth(a)), textWidth);
  563. }
  564. }
  565. if (a < restLines || restLines != 0 && lineLeft != 0 && isSmallImage) {
  566. width += AndroidUtilities.dp(48 + 4);
  567. }
  568. if (maxWebWidth < width + additinalWidth) {
  569. if (titleIsRTL) {
  570. titleX += (width + additinalWidth - maxWebWidth);
  571. }
  572. if (authorIsRTL) {
  573. authorX += (width + additinalWidth - maxWebWidth);
  574. }
  575. maxWebWidth = width + additinalWidth;
  576. }
  577. maxChildWidth = Math.max(maxChildWidth, width + additinalWidth);
  578. }
  579. } catch (Exception e) {
  580. FileLog.e(e);
  581. }
  582. }
  583.  
  584. if (smallImage && (descriptionLayout == null || descriptionLayout != null && descriptionLayout.getLineCount() == 1)) {
  585. smallImage = false;
  586. isSmallImage = false;
  587. }
  588. int maxPhotoWidth = smallImage ? AndroidUtilities.dp(48) : linkPreviewMaxWidth;
  589.  
  590. if (document != null) {
  591. if (MessageObject.isRoundVideoDocument(document)) {
  592. currentPhotoObject = FileLoader.getClosestPhotoSizeWithSize(document.thumbs, 90);
  593. photoParentObject = document;
  594. documentAttach = document;
  595. documentAttachType = DOCUMENT_ATTACH_TYPE_ROUND;
  596. } else if (MessageObject.isGifDocument(document)) {
  597. if (!messageObject.isGame() && !SharedConfig.autoplayGifs) {
  598. messageObject.gifState = 1;
  599. }
  600. photoImage.setAllowStartAnimation(messageObject.gifState != 1);
  601. currentPhotoObject = FileLoader.getClosestPhotoSizeWithSize(document.thumbs, 90);
  602. photoParentObject = document;
  603. if (currentPhotoObject != null && (currentPhotoObject.w == 0 || currentPhotoObject.h == 0)) {
  604. for (int a = 0; a < document.attributes.size(); a++) {
  605. TLRPC.DocumentAttribute attribute = document.attributes.get(a);
  606. if (attribute instanceof TLRPC.TL_documentAttributeImageSize || attribute instanceof TLRPC.TL_documentAttributeVideo) {
  607. currentPhotoObject.w = attribute.w;
  608. currentPhotoObject.h = attribute.h;
  609. break;
  610. }
  611. }
  612. if (currentPhotoObject.w == 0 || currentPhotoObject.h == 0) {
  613. currentPhotoObject.w = currentPhotoObject.h = AndroidUtilities.dp(150);
  614. }
  615. }
  616. documentAttach = document;
  617. documentAttachType = DOCUMENT_ATTACH_TYPE_GIF;
  618. } else if (MessageObject.isVideoDocument(document)) {
  619. if (photo != null) {
  620. currentPhotoObject = FileLoader.getClosestPhotoSizeWithSize(photo.sizes, AndroidUtilities.getPhotoSize(), true);
  621. currentPhotoObjectThumb = FileLoader.getClosestPhotoSizeWithSize(photo.sizes, 40);
  622. photoParentObject = photo;
  623. }
  624. if (currentPhotoObject == null) {
  625. currentPhotoObject = FileLoader.getClosestPhotoSizeWithSize(document.thumbs, 320);
  626. currentPhotoObjectThumb = FileLoader.getClosestPhotoSizeWithSize(document.thumbs, 40);
  627. photoParentObject = document;
  628. }
  629. if (currentPhotoObject == currentPhotoObjectThumb) {
  630. currentPhotoObjectThumb = null;
  631. }
  632. if (currentPhotoObject == null) {
  633. currentPhotoObject = new TLRPC.TL_photoSize();
  634. currentPhotoObject.type = "s";
  635. currentPhotoObject.location = new TLRPC.TL_fileLocationUnavailable();
  636. }
  637. if (currentPhotoObject != null && (currentPhotoObject.w == 0 || currentPhotoObject.h == 0 || currentPhotoObject instanceof TLRPC.TL_photoStrippedSize)) {
  638. for (int a = 0; a < document.attributes.size(); a++) {
  639. TLRPC.DocumentAttribute attribute = document.attributes.get(a);
  640. if (attribute instanceof TLRPC.TL_documentAttributeVideo) {
  641. if (currentPhotoObject instanceof TLRPC.TL_photoStrippedSize) {
  642. float scale = Math.max(attribute.w, attribute.w) / 50.0f;
  643. currentPhotoObject.w = (int) (attribute.w / scale);
  644. currentPhotoObject.h = (int) (attribute.h / scale);
  645. } else {
  646. currentPhotoObject.w = attribute.w;
  647. currentPhotoObject.h = attribute.h;
  648. }
  649. break;
  650. }
  651. }
  652. if (currentPhotoObject.w == 0 || currentPhotoObject.h == 0) {
  653. currentPhotoObject.w = currentPhotoObject.h = AndroidUtilities.dp(150);
  654. }
  655. }
  656. createDocumentLayout(0, messageObject);
  657. } else if (MessageObject.isStickerDocument(document)) {
  658. currentPhotoObject = FileLoader.getClosestPhotoSizeWithSize(document.thumbs, 90);
  659. photoParentObject = document;
  660. if (currentPhotoObject != null && (currentPhotoObject.w == 0 || currentPhotoObject.h == 0)) {
  661. for (int a = 0; a < document.attributes.size(); a++) {
  662. TLRPC.DocumentAttribute attribute = document.attributes.get(a);
  663. if (attribute instanceof TLRPC.TL_documentAttributeImageSize) {
  664. currentPhotoObject.w = attribute.w;
  665. currentPhotoObject.h = attribute.h;
  666. break;
  667. }
  668. }
  669. if (currentPhotoObject.w == 0 || currentPhotoObject.h == 0) {
  670. currentPhotoObject.w = currentPhotoObject.h = AndroidUtilities.dp(150);
  671. }
  672. }
  673. documentAttach = document;
  674. documentAttachType = DOCUMENT_ATTACH_TYPE_STICKER;
  675. } else if (drawInstantViewType == 6) {
  676. currentPhotoObject = FileLoader.getClosestPhotoSizeWithSize(document.thumbs, 320);
  677. photoParentObject = document;
  678. if (currentPhotoObject != null && (currentPhotoObject.w == 0 || currentPhotoObject.h == 0)) {
  679. for (int a = 0; a < document.attributes.size(); a++) {
  680. TLRPC.DocumentAttribute attribute = document.attributes.get(a);
  681. if (attribute instanceof TLRPC.TL_documentAttributeImageSize) {
  682. currentPhotoObject.w = attribute.w;
  683. currentPhotoObject.h = attribute.h;
  684. break;
  685. }
  686. }
  687. if (currentPhotoObject.w == 0 || currentPhotoObject.h == 0) {
  688. currentPhotoObject.w = currentPhotoObject.h = AndroidUtilities.dp(150);
  689. }
  690. }
  691. documentAttach = document;
  692. documentAttachType = DOCUMENT_ATTACH_TYPE_WALLPAPER;
  693. String str = AndroidUtilities.formatFileSize(documentAttach.size);
  694. durationWidth = (int) Math.ceil(Theme.chat_durationPaint.measureText(str));
  695. videoInfoLayout = new StaticLayout(str, Theme.chat_durationPaint, durationWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
  696. } else {
  697. calcBackgroundWidth(maxWidth, timeMore, maxChildWidth);
  698. if (backgroundWidth < maxWidth + AndroidUtilities.dp(20)) {
  699. backgroundWidth = maxWidth + AndroidUtilities.dp(20);
  700. }
  701. if (MessageObject.isVoiceDocument(document)) {
  702. createDocumentLayout(backgroundWidth - AndroidUtilities.dp(10), messageObject);
  703. mediaOffsetY = currentMessageObject.textHeight + AndroidUtilities.dp(8) + linkPreviewHeight;
  704. totalHeight += AndroidUtilities.dp(30 + 14);
  705. linkPreviewHeight += AndroidUtilities.dp(44);
  706.  
  707. maxWidth = maxWidth - AndroidUtilities.dp(86);
  708. if (AndroidUtilities.isTablet()) {
  709. maxChildWidth = Math.max(maxChildWidth, Math.min(AndroidUtilities.getMinTabletSide() - AndroidUtilities.dp(isChat && messageObject.needDrawAvatar() && !messageObject.isOutOwner() ? 52 : 0), AndroidUtilities.dp(220)) - AndroidUtilities.dp(30) + additinalWidth);
  710. } else {
  711. maxChildWidth = Math.max(maxChildWidth, Math.min(AndroidUtilities.displaySize.x - AndroidUtilities.dp(isChat && messageObject.needDrawAvatar() && !messageObject.isOutOwner() ? 52 : 0), AndroidUtilities.dp(220)) - AndroidUtilities.dp(30) + additinalWidth);
  712. }
  713. calcBackgroundWidth(maxWidth, timeMore, maxChildWidth);
  714. } else if (MessageObject.isMusicDocument(document)) {
  715. int durationWidth = createDocumentLayout(backgroundWidth - AndroidUtilities.dp(10), messageObject);
  716. mediaOffsetY = currentMessageObject.textHeight + AndroidUtilities.dp(8) + linkPreviewHeight;
  717. totalHeight += AndroidUtilities.dp(42 + 14);
  718. linkPreviewHeight += AndroidUtilities.dp(56);
  719.  
  720. maxWidth = maxWidth - AndroidUtilities.dp(86);
  721. maxChildWidth = Math.max(maxChildWidth, durationWidth + additinalWidth + AndroidUtilities.dp(86 + 8));
  722. if (songLayout != null && songLayout.getLineCount() > 0) {
  723. maxChildWidth = (int) Math.max(maxChildWidth, songLayout.getLineWidth(0) + additinalWidth + AndroidUtilities.dp(86));
  724. }
  725. if (performerLayout != null && performerLayout.getLineCount() > 0) {
  726. maxChildWidth = (int) Math.max(maxChildWidth, performerLayout.getLineWidth(0) + additinalWidth + AndroidUtilities.dp(86));
  727. }
  728.  
  729. calcBackgroundWidth(maxWidth, timeMore, maxChildWidth);
  730. } else {
  731. createDocumentLayout(backgroundWidth - AndroidUtilities.dp(86 + 24 + 58), messageObject);
  732. drawImageButton = true;
  733. if (drawPhotoImage) {
  734. totalHeight += AndroidUtilities.dp(86 + 14);
  735. linkPreviewHeight += AndroidUtilities.dp(86);
  736. photoImage.setImageCoords(0, totalHeight + namesOffset, AndroidUtilities.dp(86), AndroidUtilities.dp(86));
  737. } else {
  738. mediaOffsetY = currentMessageObject.textHeight + AndroidUtilities.dp(8) + linkPreviewHeight;
  739. photoImage.setImageCoords(0, totalHeight + namesOffset - AndroidUtilities.dp(14), AndroidUtilities.dp(56), AndroidUtilities.dp(56));
  740. totalHeight += AndroidUtilities.dp(50 + 14);
  741. linkPreviewHeight += AndroidUtilities.dp(50);
  742. if (docTitleLayout != null && docTitleLayout.getLineCount() > 1) {
  743. int h = (docTitleLayout.getLineCount() - 1) * AndroidUtilities.dp(16);
  744. totalHeight += h;
  745. linkPreviewHeight += h;
  746. }
  747. }
  748. }
  749. }
  750. } else if (photo != null) {
  751. boolean isPhoto = type != null && type.equals("photo");
  752. currentPhotoObject = FileLoader.getClosestPhotoSizeWithSize(messageObject.photoThumbs, isPhoto || !smallImage ? AndroidUtilities.getPhotoSize() : maxPhotoWidth, !isPhoto);
  753. photoParentObject = messageObject.photoThumbsObject;
  754. checkOnlyButtonPressed = !isPhoto;
  755. currentPhotoObjectThumb = FileLoader.getClosestPhotoSizeWithSize(messageObject.photoThumbs, 40);
  756. if (currentPhotoObjectThumb == currentPhotoObject) {
  757. currentPhotoObjectThumb = null;
  758. }
  759. } else if (webDocument != null) {
  760. if (!webDocument.mime_type.startsWith("image/")) {
  761. webDocument = null;
  762. }
  763. drawImageButton = false;
  764. }
  765.  
  766. if (documentAttachType != DOCUMENT_ATTACH_TYPE_MUSIC && documentAttachType != DOCUMENT_ATTACH_TYPE_AUDIO && documentAttachType != DOCUMENT_ATTACH_TYPE_DOCUMENT) {
  767. if (currentPhotoObject != null || webDocument != null) {
  768. drawImageButton = photo != null && !smallImage || type != null && (type.equals("photo") || type.equals("document") && documentAttachType != DOCUMENT_ATTACH_TYPE_STICKER || type.equals("gif") || documentAttachType == DOCUMENT_ATTACH_TYPE_VIDEO || documentAttachType == DOCUMENT_ATTACH_TYPE_WALLPAPER);
  769. if (linkPreviewHeight != 0) {
  770. linkPreviewHeight += AndroidUtilities.dp(2);
  771. totalHeight += AndroidUtilities.dp(2);
  772. }
  773.  
  774. if (imageBackgroundSideColor != 0) {
  775. maxPhotoWidth = AndroidUtilities.dp(208);
  776. } else if (currentPhotoObject instanceof TLRPC.TL_photoSizeEmpty && currentPhotoObject.w != 0) {
  777. maxPhotoWidth = currentPhotoObject.w;
  778. } else if (documentAttachType == DOCUMENT_ATTACH_TYPE_STICKER || documentAttachType == DOCUMENT_ATTACH_TYPE_WALLPAPER) {
  779. if (AndroidUtilities.isTablet()) {
  780. maxPhotoWidth = (int) (AndroidUtilities.getMinTabletSide() * 0.5f);
  781. } else {
  782. maxPhotoWidth = (int) (AndroidUtilities.displaySize.x * 0.5f);
  783. }
  784. } else if (documentAttachType == DOCUMENT_ATTACH_TYPE_ROUND) {
  785. maxPhotoWidth = AndroidUtilities.roundMessageSize;
  786. photoImage.setAllowDecodeSingleFrame(true);
  787. }
  788.  
  789. maxChildWidth = Math.max(maxChildWidth, maxPhotoWidth - (hasInvoicePreview ? AndroidUtilities.dp(12) : 0) + additinalWidth);
  790. if (currentPhotoObject != null) {
  791. currentPhotoObject.size = -1;
  792. if (currentPhotoObjectThumb != null) {
  793. currentPhotoObjectThumb.size = -1;
  794. }
  795. } else {
  796. webDocument.size = -1;
  797. }
  798. if (imageBackgroundSideColor != 0) {
  799. imageBackgroundSideWidth = maxChildWidth - AndroidUtilities.dp(13);
  800. }
  801.  
  802. int width;
  803. int height;
  804. if (smallImage || documentAttachType == DOCUMENT_ATTACH_TYPE_ROUND) {
  805. width = height = maxPhotoWidth;
  806. } else {
  807. if (hasGamePreview || hasInvoicePreview) {
  808. width = 640;
  809. height = 360;
  810. float scale = width / (float) (maxPhotoWidth - AndroidUtilities.dp(2));
  811. width /= scale;
  812. height /= scale;
  813. } else {
  814. width = currentPhotoObject.w;
  815. height = currentPhotoObject.h;
  816. float scale = width / (float) (maxPhotoWidth - AndroidUtilities.dp(2));
  817. width /= scale;
  818. height /= scale;
  819. if (site_name == null || site_name != null && !site_name.toLowerCase().equals("instagram") && documentAttachType == 0) {
  820. if (height > AndroidUtilities.displaySize.y / 3) {
  821. height = AndroidUtilities.displaySize.y / 3;
  822. }
  823. } else {
  824. if (height > AndroidUtilities.displaySize.y / 2) {
  825. height = AndroidUtilities.displaySize.y / 2;
  826. }
  827. }
  828. if (imageBackgroundSideColor != 0) {
  829. scale = height / (float) AndroidUtilities.dp(160);
  830. width /= scale;
  831. height /= scale;
  832. }
  833. if (height < AndroidUtilities.dp(60)) {
  834. height = AndroidUtilities.dp(60);
  835. }
  836. }
  837. }
  838. if (isSmallImage) {
  839. if (AndroidUtilities.dp(50) + additionalHeight > linkPreviewHeight) {
  840. totalHeight += AndroidUtilities.dp(50) + additionalHeight - linkPreviewHeight + AndroidUtilities.dp(8);
  841. linkPreviewHeight = AndroidUtilities.dp(50) + additionalHeight;
  842. }
  843. linkPreviewHeight -= AndroidUtilities.dp(8);
  844. } else {
  845. totalHeight += height + AndroidUtilities.dp(12);
  846. linkPreviewHeight += height;
  847. }
  848.  
  849. if (documentAttachType == DOCUMENT_ATTACH_TYPE_WALLPAPER && imageBackgroundSideColor == 0) {
  850. photoImage.setImageCoords(0, 0, Math.max(maxChildWidth - AndroidUtilities.dp(13), width), height);
  851. } else {
  852. photoImage.setImageCoords(0, 0, width, height);
  853. }
  854.  
  855. currentPhotoFilter = String.format(Locale.US, "%d_%d", width, height);
  856. currentPhotoFilterThumb = String.format(Locale.US, "%d_%d_b", width, height);
  857.  
  858. if (webDocument != null) {
  859. photoImage.setImage(ImageLocation.getForWebFile(webDocument), currentPhotoFilter, null, null, webDocument.size, null, messageObject, 1);
  860. } else {
  861. if (documentAttachType == DOCUMENT_ATTACH_TYPE_WALLPAPER) {
  862. if (messageObject.mediaExists) {
  863. photoImage.setImage(ImageLocation.getForDocument(documentAttach), currentPhotoFilter, ImageLocation.getForDocument(currentPhotoObject, document), "b1", 0, "jpg", messageObject, 1);
  864. } else {
  865. photoImage.setImage(null, null, ImageLocation.getForDocument(currentPhotoObject, document), "b1", 0, "jpg", messageObject, 1);
  866. }
  867. } else if (documentAttachType == DOCUMENT_ATTACH_TYPE_STICKER) {
  868. photoImage.setImage(ImageLocation.getForDocument(documentAttach), currentPhotoFilter, ImageLocation.getForDocument(currentPhotoObject, documentAttach), "b1", documentAttach.size, "webp", messageObject, 1);
  869. } else if (documentAttachType == DOCUMENT_ATTACH_TYPE_VIDEO) {
  870. photoImage.setNeedsQualityThumb(true);
  871. photoImage.setShouldGenerateQualityThumb(true);
  872. if (SharedConfig.autoplayVideo && (
  873. currentMessageObject.mediaExists ||
  874. messageObject.canStreamVideo() && DownloadController.getInstance(currentAccount).canDownloadMedia(currentMessageObject)
  875. )) {
  876. photoImage.setAllowDecodeSingleFrame(true);
  877. photoImage.setAllowStartAnimation(true);
  878. photoImage.startAnimation();
  879. photoImage.setImage(ImageLocation.getForDocument(documentAttach), ImageLoader.AUTOPLAY_FILTER, ImageLocation.getForDocument(currentPhotoObject, documentAttach), currentPhotoFilter, ImageLocation.getForDocument(currentPhotoObjectThumb, documentAttach), currentPhotoFilterThumb, null, documentAttach.size, null, messageObject, 0);
  880. autoPlayingMedia = true;
  881. } else {
  882. if (currentPhotoObjectThumb != null) {
  883. photoImage.setImage(ImageLocation.getForDocument(currentPhotoObject, documentAttach), currentPhotoFilter, ImageLocation.getForDocument(currentPhotoObjectThumb, documentAttach), currentPhotoFilterThumb, 0, null, messageObject, 0);
  884. } else {
  885. photoImage.setImage(null, null, ImageLocation.getForDocument(currentPhotoObject, documentAttach), currentPhotoObject instanceof TLRPC.TL_photoStrippedSize || "s".equals(currentPhotoObject.type) ? currentPhotoFilterThumb : currentPhotoFilter, 0, null, messageObject, 0);
  886. }
  887. }
  888. } else if (documentAttachType == DOCUMENT_ATTACH_TYPE_GIF || documentAttachType == DOCUMENT_ATTACH_TYPE_ROUND) {
  889. photoImage.setAllowDecodeSingleFrame(true);
  890. String fileName = FileLoader.getAttachFileName(document);
  891. boolean autoDownload = false;
  892. if (MessageObject.isRoundVideoDocument(document)) {
  893. photoImage.setRoundRadius(AndroidUtilities.roundMessageSize / 2);
  894. autoDownload = DownloadController.getInstance(currentAccount).canDownloadMedia(currentMessageObject);
  895. } else if (MessageObject.isGifDocument(document)) {
  896. autoDownload = DownloadController.getInstance(currentAccount).canDownloadMedia(currentMessageObject);
  897. }
  898. String filter = currentPhotoObject instanceof TLRPC.TL_photoStrippedSize || "s".equals(currentPhotoObject.type) ? currentPhotoFilterThumb : currentPhotoFilter;
  899. if (messageObject.mediaExists || autoDownload) {
  900. autoPlayingMedia = true;
  901. photoImage.setImage(ImageLocation.getForDocument(document), document.size < 1024 * 32 ? null : ImageLoader.AUTOPLAY_FILTER, ImageLocation.getForDocument(currentPhotoObject, documentAttach), filter, ImageLocation.getForDocument(currentPhotoObjectThumb, documentAttach), currentPhotoFilterThumb, null, document.size, null, messageObject, 0);
  902. } else {
  903. photoImage.setImage(null, null, ImageLocation.getForDocument(currentPhotoObject, documentAttach), filter, 0, null, currentMessageObject, 0);
  904. }
  905. } else {
  906. boolean photoExist = messageObject.mediaExists;
  907. String fileName = FileLoader.getAttachFileName(currentPhotoObject);
  908. if (hasGamePreview || photoExist || DownloadController.getInstance(currentAccount).canDownloadMedia(currentMessageObject) || FileLoader.getInstance(currentAccount).isLoadingFile(fileName)) {
  909. photoNotSet = false;
  910. photoImage.setImage(ImageLocation.getForObject(currentPhotoObject, photoParentObject), currentPhotoFilter, ImageLocation.getForObject(currentPhotoObjectThumb, photoParentObject), currentPhotoFilterThumb, 0, null, messageObject, 0);
  911. } else {
  912. photoNotSet = true;
  913. if (currentPhotoObjectThumb != null) {
  914. photoImage.setImage(null, null, ImageLocation.getForObject(currentPhotoObjectThumb, photoParentObject), String.format(Locale.US, "%d_%d_b", width, height), 0, null, messageObject, 0);
  915. } else {
  916. photoImage.setImageBitmap((Drawable) null);
  917. }
  918. }
  919. }
  920. }
  921. drawPhotoImage = true;
  922.  
  923. if (type != null && type.equals("video") && duration != 0) {
  924. int minutes = duration / 60;
  925. int seconds = duration - minutes * 60;
  926. String str = String.format("%d:%02d", minutes, seconds);
  927. durationWidth = (int) Math.ceil(Theme.chat_durationPaint.measureText(str));
  928. videoInfoLayout = new StaticLayout(str, Theme.chat_durationPaint, durationWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
  929. } else if (hasGamePreview) {
  930. String str = LocaleController.getString("AttachGame", R.string.AttachGame).toUpperCase();
  931. durationWidth = (int) Math.ceil(Theme.chat_gamePaint.measureText(str));
  932. videoInfoLayout = new StaticLayout(str, Theme.chat_gamePaint, durationWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
  933. }
  934. } else {
  935. photoImage.setImageBitmap((Drawable) null);
  936. linkPreviewHeight -= AndroidUtilities.dp(6);
  937. totalHeight += AndroidUtilities.dp(4);
  938. }
  939. if (hasInvoicePreview) {
  940. CharSequence str;
  941. if ((messageObject.messageOwner.media.flags & 4) != 0) {
  942. str = LocaleController.getString("PaymentReceipt", R.string.PaymentReceipt).toUpperCase();
  943. } else {
  944. if (messageObject.messageOwner.media.test) {
  945. str = LocaleController.getString("PaymentTestInvoice", R.string.PaymentTestInvoice).toUpperCase();
  946. } else {
  947. str = LocaleController.getString("PaymentInvoice", R.string.PaymentInvoice).toUpperCase();
  948. }
  949. }
  950. String price = LocaleController.getInstance().formatCurrencyString(messageObject.messageOwner.media.total_amount, messageObject.messageOwner.media.currency);
  951. SpannableStringBuilder stringBuilder = new SpannableStringBuilder(price + " " + str);
  952. stringBuilder.setSpan(new TypefaceSpan(AndroidUtilities.getTypeface("fonts/rmedium.ttf")), 0, price.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  953. durationWidth = (int) Math.ceil(Theme.chat_shipmentPaint.measureText(stringBuilder, 0, stringBuilder.length()));
  954. videoInfoLayout = new StaticLayout(stringBuilder, Theme.chat_shipmentPaint, durationWidth + AndroidUtilities.dp(10), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
  955. if (!drawPhotoImage) {
  956. totalHeight += AndroidUtilities.dp(6);
  957. int timeWidthTotal = timeWidth + AndroidUtilities.dp(14 + (messageObject.isOutOwner() ? 20 : 0));
  958. if (durationWidth + timeWidthTotal > maxWidth) {
  959. maxChildWidth = Math.max(durationWidth, maxChildWidth);
  960. totalHeight += AndroidUtilities.dp(12);
  961. } else {
  962. maxChildWidth = Math.max(durationWidth + timeWidthTotal, maxChildWidth);
  963. }
  964. }
  965. }
  966. if (hasGamePreview && messageObject.textHeight != 0) {
  967. linkPreviewHeight += messageObject.textHeight + AndroidUtilities.dp(6);
  968. totalHeight += AndroidUtilities.dp(4);
  969. }
  970. calcBackgroundWidth(maxWidth, timeMore, maxChildWidth);
  971. }
  972. createInstantViewButton();
  973. } else {
  974. photoImage.setImageBitmap((Drawable) null);
  975. calcBackgroundWidth(maxWidth, timeMore, maxChildWidth);
  976. }
  977. } else if (messageObject.type == 16) {
  978. drawName = false;
  979. drawForwardedName = false;
  980. drawPhotoImage = false;
  981. if (AndroidUtilities.isTablet()) {
  982. backgroundWidth = Math.min(AndroidUtilities.getMinTabletSide() - AndroidUtilities.dp(isChat && messageObject.needDrawAvatar() && !messageObject.isOutOwner() ? 102 : 50), AndroidUtilities.dp(270));
  983. } else {
  984. backgroundWidth = Math.min(AndroidUtilities.displaySize.x - AndroidUtilities.dp(isChat && messageObject.needDrawAvatar() && !messageObject.isOutOwner() ? 102 : 50), AndroidUtilities.dp(270));
  985. }
  986. availableTimeWidth = backgroundWidth - AndroidUtilities.dp(31);
  987.  
  988. int maxWidth = getMaxNameWidth() - AndroidUtilities.dp(50);
  989. if (maxWidth < 0) {
  990. maxWidth = AndroidUtilities.dp(10);
  991. }
  992.  
  993. String text;
  994. String time = LocaleController.getInstance().formatterDay.format((long) (messageObject.messageOwner.date) * 1000);
  995. TLRPC.TL_messageActionPhoneCall call = (TLRPC.TL_messageActionPhoneCall) messageObject.messageOwner.action;
  996. boolean isMissed = call.reason instanceof TLRPC.TL_phoneCallDiscardReasonMissed;
  997. if (messageObject.isOutOwner()) {
  998. if (isMissed) {
  999. text = LocaleController.getString("CallMessageOutgoingMissed", R.string.CallMessageOutgoingMissed);
  1000. } else {
  1001. text = LocaleController.getString("CallMessageOutgoing", R.string.CallMessageOutgoing);
  1002. }
  1003. } else {
  1004. if (isMissed) {
  1005. text = LocaleController.getString("CallMessageIncomingMissed", R.string.CallMessageIncomingMissed);
  1006. } else if (call.reason instanceof TLRPC.TL_phoneCallDiscardReasonBusy) {
  1007. text = LocaleController.getString("CallMessageIncomingDeclined", R.string.CallMessageIncomingDeclined);
  1008. } else {
  1009. text = LocaleController.getString("CallMessageIncoming", R.string.CallMessageIncoming);
  1010. }
  1011. }
  1012. if (call.duration > 0) {
  1013. time += ", " + LocaleController.formatCallDuration(call.duration);
  1014. }
  1015.  
  1016. titleLayout = new StaticLayout(TextUtils.ellipsize(text, Theme.chat_audioTitlePaint, maxWidth, TextUtils.TruncateAt.END), Theme.chat_audioTitlePaint, maxWidth + AndroidUtilities.dp(2), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
  1017. docTitleLayout = new StaticLayout(TextUtils.ellipsize(time, Theme.chat_contactPhonePaint, maxWidth, TextUtils.TruncateAt.END), Theme.chat_contactPhonePaint, maxWidth + AndroidUtilities.dp(2), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
  1018.  
  1019. setMessageObjectInternal(messageObject);
  1020.  
  1021. totalHeight = AndroidUtilities.dp(65) + namesOffset;
  1022. if (drawPinnedTop) {
  1023. namesOffset -= AndroidUtilities.dp(1);
  1024. }
  1025. } else if (messageObject.type == 12) {
  1026. drawName = false;
  1027. drawForwardedName = true;
  1028. drawPhotoImage = true;
  1029. photoImage.setRoundRadius(AndroidUtilities.dp(22));
  1030. if (AndroidUtilities.isTablet()) {
  1031. backgroundWidth = Math.min(AndroidUtilities.getMinTabletSide() - AndroidUtilities.dp(isChat && messageObject.needDrawAvatar() && !messageObject.isOutOwner() ? 102 : 50), AndroidUtilities.dp(270));
  1032. } else {
  1033. backgroundWidth = Math.min(AndroidUtilities.displaySize.x - AndroidUtilities.dp(isChat && messageObject.needDrawAvatar() && !messageObject.isOutOwner() ? 102 : 50), AndroidUtilities.dp(270));
  1034. }
  1035. availableTimeWidth = backgroundWidth - AndroidUtilities.dp(31);
  1036.  
  1037. int uid = messageObject.messageOwner.media.user_id;
  1038. TLRPC.User user = MessagesController.getInstance(currentAccount).getUser(uid);
  1039.  
  1040. int maxWidth = getMaxNameWidth() - AndroidUtilities.dp(80);
  1041. if (maxWidth < 0) {
  1042. maxWidth = AndroidUtilities.dp(10);
  1043. }
  1044. if (user != null) {
  1045. contactAvatarDrawable.setInfo(user);
  1046. }
  1047. photoImage.setImage(ImageLocation.getForUser(user, false), "50_50", user != null ? contactAvatarDrawable : Theme.chat_contactDrawable[messageObject.isOutOwner() ? 1 : 0], null, messageObject, 0);
  1048.  
  1049. CharSequence phone;
  1050. if (!TextUtils.isEmpty(messageObject.vCardData)) {
  1051. phone = messageObject.vCardData;
  1052. drawInstantView = true;
  1053. drawInstantViewType = 5;
  1054. } else {
  1055. if (user != null && !TextUtils.isEmpty(user.phone)) {
  1056. phone = PhoneFormat.getInstance().format("+" + user.phone);
  1057. } else {
  1058. phone = messageObject.messageOwner.media.phone_number;
  1059. if (!TextUtils.isEmpty(phone)) {
  1060. phone = PhoneFormat.getInstance().format((String) phone);
  1061. } else {
  1062. phone = LocaleController.getString("NumberUnknown", R.string.NumberUnknown);
  1063. }
  1064. }
  1065. }
  1066.  
  1067. CharSequence currentNameString = ContactsController.formatName(messageObject.messageOwner.media.first_name, messageObject.messageOwner.media.last_name).replace('\n', ' ');
  1068. if (currentNameString.length() == 0) {
  1069. currentNameString = messageObject.messageOwner.media.phone_number;
  1070. if (currentNameString == null) {
  1071. currentNameString = "";
  1072. }
  1073. }
  1074. titleLayout = new StaticLayout(TextUtils.ellipsize(currentNameString, Theme.chat_contactNamePaint, maxWidth, TextUtils.TruncateAt.END), Theme.chat_contactNamePaint, maxWidth + AndroidUtilities.dp(4), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
  1075. docTitleLayout = new StaticLayout(phone, Theme.chat_contactPhonePaint, maxWidth + AndroidUtilities.dp(2), Layout.Alignment.ALIGN_NORMAL, 1.0f, AndroidUtilities.dp(1), false);
  1076.  
  1077. setMessageObjectInternal(messageObject);
  1078.  
  1079. if (drawForwardedName && messageObject.needDrawForwarded() && (currentPosition == null || currentPosition.minY == 0)) {
  1080. namesOffset += AndroidUtilities.dp(5);
  1081. } else if (drawNameLayout && messageObject.messageOwner.reply_to_msg_id == 0) {
  1082. namesOffset += AndroidUtilities.dp(7);
  1083. }
  1084.  
  1085. totalHeight = AndroidUtilities.dp(70 - 15) + namesOffset + docTitleLayout.getHeight();
  1086. if (drawPinnedTop) {
  1087. namesOffset -= AndroidUtilities.dp(1);
  1088. }
  1089. if (drawInstantView) {
  1090. createInstantViewButton();
  1091. } else {
  1092. if (docTitleLayout.getLineCount() > 0) {
  1093. int timeLeft = backgroundWidth - AndroidUtilities.dp(40 + 18 + 44 + 8) - (int) Math.ceil(docTitleLayout.getLineWidth(docTitleLayout.getLineCount() - 1));
  1094. if (timeLeft < timeWidth) {
  1095. totalHeight += AndroidUtilities.dp(8);
  1096. }
  1097. }
  1098. }
  1099. } else if (messageObject.type == 2) {
  1100. drawForwardedName = true;
  1101. if (AndroidUtilities.isTablet()) {
  1102. backgroundWidth = Math.min(AndroidUtilities.getMinTabletSide() - AndroidUtilities.dp(isChat && messageObject.needDrawAvatar() && !messageObject.isOutOwner() ? 102 : 50), AndroidUtilities.dp(270));
  1103. } else {
  1104. backgroundWidth = Math.min(AndroidUtilities.displaySize.x - AndroidUtilities.dp(isChat && messageObject.needDrawAvatar() && !messageObject.isOutOwner() ? 102 : 50), AndroidUtilities.dp(270));
  1105. }
  1106. createDocumentLayout(backgroundWidth, messageObject);
  1107.  
  1108. setMessageObjectInternal(messageObject);
  1109.  
  1110. totalHeight = AndroidUtilities.dp(70) + namesOffset;
  1111. if (drawPinnedTop) {
  1112. namesOffset -= AndroidUtilities.dp(1);
  1113. }
  1114. } else if (messageObject.type == 14) {
  1115. if (AndroidUtilities.isTablet()) {
  1116. backgroundWidth = Math.min(AndroidUtilities.getMinTabletSide() - AndroidUtilities.dp(isChat && messageObject.needDrawAvatar() && !messageObject.isOutOwner() ? 102 : 50), AndroidUtilities.dp(270));
  1117. } else {
  1118. backgroundWidth = Math.min(AndroidUtilities.displaySize.x - AndroidUtilities.dp(isChat && messageObject.needDrawAvatar() && !messageObject.isOutOwner() ? 102 : 50), AndroidUtilities.dp(270));
  1119. }
  1120.  
  1121. createDocumentLayout(backgroundWidth, messageObject);
  1122.  
  1123. setMessageObjectInternal(messageObject);
  1124.  
  1125. totalHeight = AndroidUtilities.dp(82) + namesOffset;
  1126. if (drawPinnedTop) {
  1127. namesOffset -= AndroidUtilities.dp(1);
  1128. }
  1129. } else if (messageObject.type == MessageObject.TYPE_POLL) {
  1130. createSelectorDrawable();
  1131. drawName = true;
  1132. drawForwardedName = true;
  1133. drawPhotoImage = false;
  1134. int maxWidth = availableTimeWidth = Math.min(AndroidUtilities.dp(500), messageObject.getMaxMessageTextWidth());
  1135. backgroundWidth = maxWidth + AndroidUtilities.dp(31);
  1136. availableTimeWidth = AndroidUtilities.dp(120);
  1137. measureTime(messageObject);
  1138.  
  1139. TLRPC.TL_messageMediaPoll media = (TLRPC.TL_messageMediaPoll) messageObject.messageOwner.media;
  1140.  
  1141. pollClosed = media.poll.closed;
  1142. pollVoted = messageObject.isVoted();
  1143. titleLayout = new StaticLayout(Emoji.replaceEmoji(media.poll.question, Theme.chat_audioTitlePaint.getFontMetricsInt(), AndroidUtilities.dp(16), false), Theme.chat_audioTitlePaint, maxWidth + AndroidUtilities.dp(2), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
  1144. boolean titleRtl = false;
  1145. if (titleLayout != null) {
  1146. for (int a = 0, N = titleLayout.getLineCount(); a < N; a++) {
  1147. if (titleLayout.getLineLeft(a) != 0) {
  1148. titleRtl = true;
  1149. break;
  1150. }
  1151. }
  1152. }
  1153. docTitleLayout = new StaticLayout(TextUtils.ellipsize(media.poll.closed ? LocaleController.getString("FinalResults", R.string.FinalResults) : LocaleController.getString("AnonymousPoll", R.string.AnonymousPoll), Theme.chat_timePaint, maxWidth, TextUtils.TruncateAt.END), Theme.chat_timePaint, maxWidth + AndroidUtilities.dp(2), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
  1154. if (docTitleLayout != null && docTitleLayout.getLineCount() > 0) {
  1155. if (titleRtl && !LocaleController.isRTL) {
  1156. docTitleOffsetX = (int) Math.ceil(maxWidth - docTitleLayout.getLineWidth(0));
  1157. } else if (!titleRtl && LocaleController.isRTL) {
  1158. docTitleOffsetX = -(int) Math.ceil(docTitleLayout.getLineLeft(0));
  1159. }
  1160. }
  1161. int w = maxWidth - timeWidth - AndroidUtilities.dp(messageObject.isOutOwner() ? 28 : 8);
  1162. infoLayout = new StaticLayout(TextUtils.ellipsize(media.results.total_voters == 0 ? LocaleController.getString("NoVotes", R.string.NoVotes) : LocaleController.formatPluralString("Vote", media.results.total_voters), Theme.chat_livePaint, w, TextUtils.TruncateAt.END), Theme.chat_livePaint, w, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
  1163. infoX = (int) Math.ceil(infoLayout != null && infoLayout.getLineCount() > 0 ? -infoLayout.getLineLeft(0) : 0);
  1164.  
  1165. lastPoll = media.poll;
  1166. lastPollResults = media.results.results;
  1167. lastPollResultsVoters = media.results.total_voters;
  1168.  
  1169. int maxVote = 0;
  1170. if (!animatePollAnswer && pollVoteInProgress) {
  1171. performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
  1172. }
  1173. animatePollAnswerAlpha = animatePollAnswer = attachedToWindow && (pollVoteInProgress || pollUnvoteInProgress);
  1174. ArrayList<PollButton> previousPollButtons = null;
  1175. ArrayList<PollButton> sortedPollButtons = new ArrayList<>();
  1176. if (!pollButtons.isEmpty()) {
  1177. previousPollButtons = new ArrayList<>(pollButtons);
  1178. pollButtons.clear();
  1179. if (!animatePollAnswer) {
  1180. animatePollAnswer = attachedToWindow && (pollVoted || pollClosed);
  1181. }
  1182. if (pollAnimationProgress > 0 && pollAnimationProgress < 1.0f) {
  1183. for (int b = 0, N2 = previousPollButtons.size(); b < N2; b++) {
  1184. PollButton button = previousPollButtons.get(b);
  1185. button.percent = (int) Math.ceil(button.prevPercent + (button.percent - button.prevPercent) * pollAnimationProgress);
  1186. button.percentProgress = button.prevPercentProgress + (button.percentProgress - button.prevPercentProgress) * pollAnimationProgress;
  1187. }
  1188. }
  1189. }
  1190.  
  1191. pollAnimationProgress = animatePollAnswer ? 0.0f : 1.0f;
  1192. byte[] votingFor;
  1193. if (!animatePollAnswerAlpha) {
  1194. pollVoteInProgress = false;
  1195. pollVoteInProgressNum = -1;
  1196. votingFor = SendMessagesHelper.getInstance(currentAccount).isSendingVote(currentMessageObject);
  1197. } else {
  1198. votingFor = null;
  1199. }
  1200.  
  1201. int height = titleLayout != null ? titleLayout.getHeight() : 0;
  1202. int restPercent = 100;
  1203. boolean hasDifferent = false;
  1204. int previousPercent = 0;
  1205. for (int a = 0, N = media.poll.answers.size(); a < N; a++) {
  1206. PollButton button = new PollButton();
  1207. button.answer = media.poll.answers.get(a);
  1208. button.title = new StaticLayout(Emoji.replaceEmoji(button.answer.text, Theme.chat_audioPerformerPaint.getFontMetricsInt(), AndroidUtilities.dp(15), false), Theme.chat_audioPerformerPaint, maxWidth - AndroidUtilities.dp(33), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
  1209. button.y = height + AndroidUtilities.dp(52);
  1210. button.height = button.title.getHeight();
  1211. pollButtons.add(button);
  1212. sortedPollButtons.add(button);
  1213. height += button.height + AndroidUtilities.dp(26);
  1214. if (!media.results.results.isEmpty()) {
  1215. for (int b = 0, N2 = media.results.results.size(); b < N2; b++) {
  1216. TLRPC.TL_pollAnswerVoters answer = media.results.results.get(b);
  1217. if (Arrays.equals(button.answer.option, answer.option)) {
  1218. if ((pollVoted || pollClosed) && media.results.total_voters > 0) {
  1219. button.decimal = 100 * (answer.voters / (float) media.results.total_voters);
  1220. button.percent = (int) button.decimal;
  1221. button.decimal -= button.percent;
  1222. } else {
  1223. button.percent = 0;
  1224. button.decimal = 0;
  1225. }
  1226. if (previousPercent == 0) {
  1227. previousPercent = button.percent;
  1228. } else if (button.percent != 0 && previousPercent != button.percent) {
  1229. hasDifferent = true;
  1230. }
  1231. restPercent -= button.percent;
  1232. maxVote = Math.max(button.percent, maxVote);
  1233. break;
  1234. }
  1235. }
  1236. }
  1237. if (previousPollButtons != null) {
  1238. for (int b = 0, N2 = previousPollButtons.size(); b < N2; b++) {
  1239. PollButton prevButton = previousPollButtons.get(b);
  1240. if (Arrays.equals(button.answer.option, prevButton.answer.option)) {
  1241. button.prevPercent = prevButton.percent;
  1242. button.prevPercentProgress = prevButton.percentProgress;
  1243. break;
  1244. }
  1245. }
  1246. }
  1247. if (votingFor != null && Arrays.equals(button.answer.option, votingFor)) {
  1248. pollVoteInProgressNum = a;
  1249. pollVoteInProgress = true;
  1250. votingFor = null;
  1251. }
  1252. }
  1253. if (hasDifferent && restPercent != 0) {
  1254. Collections.sort(sortedPollButtons, (o1, o2) -> {
  1255. if (o1.decimal > o2.decimal) {
  1256. return -1;
  1257. } else if (o1.decimal < o2.decimal) {
  1258. return 1;
  1259. }
  1260. return 0;
  1261. });
  1262. for (int a = 0, N = Math.min(restPercent, sortedPollButtons.size()); a < N; a++) {
  1263. sortedPollButtons.get(a).percent += 1;
  1264. }
  1265. }
  1266. int width = backgroundWidth - AndroidUtilities.dp(76);
  1267. for (int b = 0, N2 = pollButtons.size(); b < N2; b++) {
  1268. PollButton button = pollButtons.get(b);
  1269. button.percentProgress = Math.max(AndroidUtilities.dp(5) / (float) width, maxVote != 0 ? button.percent / (float) maxVote : 0);
  1270. }
  1271.  
  1272. setMessageObjectInternal(messageObject);
  1273.  
  1274. totalHeight = AndroidUtilities.dp(46 + 27) + namesOffset + height;
  1275. if (drawPinnedTop) {
  1276. namesOffset -= AndroidUtilities.dp(1);
  1277. }
  1278. } else {
  1279. drawForwardedName = messageObject.messageOwner.fwd_from != null && !messageObject.isAnyKindOfSticker();
  1280. mediaBackground = messageObject.type != 9;
  1281. drawImageButton = true;
  1282. drawPhotoImage = true;
  1283.  
  1284. int photoWidth = 0;
  1285. int photoHeight = 0;
  1286. int additionHeight = 0;
  1287.  
  1288. if (messageObject.gifState != 2 && !SharedConfig.autoplayGifs && (messageObject.type == 8 || messageObject.type == MessageObject.TYPE_ROUND_VIDEO)) {
  1289. messageObject.gifState = 1;
  1290. }
  1291.  
  1292. photoImage.setAllowDecodeSingleFrame(true);
  1293. if (messageObject.isVideo()) {
  1294. photoImage.setAllowStartAnimation(true);
  1295. } else if (messageObject.isRoundVideo()) {
  1296. MessageObject playingMessage = MediaController.getInstance().getPlayingMessageObject();
  1297. photoImage.setAllowStartAnimation(playingMessage == null || !playingMessage.isRoundVideo());
  1298. } else {
  1299. photoImage.setAllowStartAnimation(messageObject.gifState == 0);
  1300. }
  1301.  
  1302. photoImage.setForcePreview(messageObject.needDrawBluredPreview());
  1303. if (messageObject.type == 9) {
  1304. if (AndroidUtilities.isTablet()) {
  1305. backgroundWidth = Math.min(AndroidUtilities.getMinTabletSide() - AndroidUtilities.dp(isChat && messageObject.needDrawAvatar() && !messageObject.isOutOwner() ? 102 : 50), AndroidUtilities.dp(300));
  1306. } else {
  1307. backgroundWidth = Math.min(AndroidUtilities.displaySize.x - AndroidUtilities.dp(isChat && messageObject.needDrawAvatar() && !messageObject.isOutOwner() ? 102 : 50), AndroidUtilities.dp(300));
  1308. }
  1309. if (checkNeedDrawShareButton(messageObject)) {
  1310. backgroundWidth -= AndroidUtilities.dp(20);
  1311. }
  1312. int maxTextWidth = 0;
  1313. int maxWidth = backgroundWidth - AndroidUtilities.dp(86 + 52);
  1314. int widthForCaption = 0;
  1315. createDocumentLayout(maxWidth, messageObject);
  1316. if (!TextUtils.isEmpty(messageObject.caption)) {
  1317. try {
  1318. currentCaption = messageObject.caption;
  1319. int width = backgroundWidth - AndroidUtilities.dp(31);
  1320. widthForCaption = width - AndroidUtilities.dp(10);
  1321. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  1322. captionLayout = StaticLayout.Builder.obtain(messageObject.caption, 0, messageObject.caption.length(), Theme.chat_msgTextPaint, widthForCaption)
  1323. .setBreakStrategy(StaticLayout.BREAK_STRATEGY_HIGH_QUALITY)
  1324. .setHyphenationFrequency(StaticLayout.HYPHENATION_FREQUENCY_NONE)
  1325. .setAlignment(Layout.Alignment.ALIGN_NORMAL)
  1326. .build();
  1327. } else {
  1328. captionLayout = new StaticLayout(messageObject.caption, Theme.chat_msgTextPaint, widthForCaption, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
  1329. }
  1330. } catch (Exception e) {
  1331. FileLog.e(e);
  1332. }
  1333. }
  1334.  
  1335. if (docTitleLayout != null) {
  1336. for (int a = 0, N = docTitleLayout.getLineCount(); a < N; a++) {
  1337. maxTextWidth = Math.max(maxTextWidth, (int) Math.ceil(docTitleLayout.getLineWidth(a) + docTitleLayout.getLineLeft(a)) + AndroidUtilities.dp(86 + (drawPhotoImage ? 52 : 22)));
  1338. }
  1339. }
  1340. if (infoLayout != null) {
  1341. for (int a = 0, N = infoLayout.getLineCount(); a < N; a++) {
  1342. maxTextWidth = Math.max(maxTextWidth, (int) Math.ceil(infoLayout.getLineWidth(a)) + AndroidUtilities.dp(86 + (drawPhotoImage ? 52 : 22)));
  1343. }
  1344. }
  1345. if (captionLayout != null) {
  1346. for (int a = 0, N = captionLayout.getLineCount(); a < N; a++) {
  1347. int w = (int) Math.ceil(Math.min(widthForCaption, captionLayout.getLineWidth(a) + captionLayout.getLineLeft(a))) + AndroidUtilities.dp(31);
  1348. if (w > maxTextWidth) {
  1349. maxTextWidth = w;
  1350. }
  1351. }
  1352. }
  1353.  
  1354. if (maxTextWidth > 0) {
  1355. backgroundWidth = maxTextWidth;
  1356. maxWidth = maxTextWidth - AndroidUtilities.dp(31);
  1357. }
  1358. if (drawPhotoImage) {
  1359. photoWidth = AndroidUtilities.dp(86);
  1360. photoHeight = AndroidUtilities.dp(86);
  1361. } else {
  1362. photoWidth = AndroidUtilities.dp(56);
  1363. photoHeight = AndroidUtilities.dp(56);
  1364. if (docTitleLayout != null && docTitleLayout.getLineCount() > 1) {
  1365. photoHeight += (docTitleLayout.getLineCount() - 1) * AndroidUtilities.dp(16);
  1366. }
  1367. }
  1368. availableTimeWidth = maxWidth;
  1369. if (!drawPhotoImage && TextUtils.isEmpty(messageObject.caption) && infoLayout != null) {
  1370. int lineCount = infoLayout.getLineCount();
  1371. measureTime(messageObject);
  1372. int timeLeft = backgroundWidth - AndroidUtilities.dp(40 + 18 + 56 + 8) - (int) Math.ceil(infoLayout.getLineWidth(0));
  1373. if (timeLeft < timeWidth) {
  1374. photoHeight += AndroidUtilities.dp(12);
  1375. } else if (lineCount == 1) {
  1376. photoHeight += AndroidUtilities.dp(4);
  1377. }
  1378. }
  1379. } else if (messageObject.type == 4) { //geo
  1380. TLRPC.GeoPoint point = messageObject.messageOwner.media.geo;
  1381. double lat = point.lat;
  1382. double lon = point._long;
  1383.  
  1384. if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaGeoLive) {
  1385. if (AndroidUtilities.isTablet()) {
  1386. backgroundWidth = Math.min(AndroidUtilities.getMinTabletSide() - AndroidUtilities.dp(isChat && messageObject.needDrawAvatar() && !messageObject.isOutOwner() ? 102 : 50), AndroidUtilities.dp(252 + 37));
  1387. } else {
  1388. backgroundWidth = Math.min(AndroidUtilities.displaySize.x - AndroidUtilities.dp(isChat && messageObject.needDrawAvatar() && !messageObject.isOutOwner() ? 102 : 50), AndroidUtilities.dp(252 + 37));
  1389. }
  1390. backgroundWidth -= AndroidUtilities.dp(4);
  1391. if (checkNeedDrawShareButton(messageObject)) {
  1392. backgroundWidth -= AndroidUtilities.dp(20);
  1393. }
  1394. int maxWidth = backgroundWidth - AndroidUtilities.dp(37);
  1395. availableTimeWidth = maxWidth;
  1396. maxWidth -= AndroidUtilities.dp(54);
  1397.  
  1398. photoWidth = backgroundWidth - AndroidUtilities.dp(17);
  1399. photoHeight = AndroidUtilities.dp(195);
  1400.  
  1401. int offset = 268435456;
  1402. double rad = offset / Math.PI;
  1403. double y = Math.round(offset - rad * Math.log((1 + Math.sin(lat * Math.PI / 180.0)) / (1 - Math.sin(lat * Math.PI / 180.0))) / 2) - (AndroidUtilities.dp(10.3f) << (21 - 15));
  1404. lat = (Math.PI / 2.0 - 2 * Math.atan(Math.exp((y - offset) / rad))) * 180.0 / Math.PI;
  1405. currentUrl = AndroidUtilities.formapMapUrl(currentAccount, lat, lon, (int) (photoWidth / AndroidUtilities.density), (int) (photoHeight / AndroidUtilities.density), false, 15);
  1406. currentWebFile = WebFile.createWithGeoPoint(lat, lon, point.access_hash, (int) (photoWidth / AndroidUtilities.density), (int) (photoHeight / AndroidUtilities.density), 15, Math.min(2, (int) Math.ceil(AndroidUtilities.density)));
  1407.  
  1408. if (!(locationExpired = isCurrentLocationTimeExpired(messageObject))) {
  1409. photoImage.setCrossfadeWithOldImage(true);
  1410. mediaBackground = false;
  1411. additionHeight = AndroidUtilities.dp(56);
  1412. AndroidUtilities.runOnUIThread(invalidateRunnable, 1000);
  1413. scheduledInvalidate = true;
  1414. } else {
  1415. backgroundWidth -= AndroidUtilities.dp(9);
  1416. }
  1417. docTitleLayout = new StaticLayout(TextUtils.ellipsize(LocaleController.getString("AttachLiveLocation", R.string.AttachLiveLocation), Theme.chat_locationTitlePaint, maxWidth, TextUtils.TruncateAt.END), Theme.chat_locationTitlePaint, maxWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
  1418.  
  1419. updateCurrentUserAndChat();
  1420. if (currentUser != null) {
  1421. contactAvatarDrawable.setInfo(currentUser);
  1422. locationImageReceiver.setImage(ImageLocation.getForUser(currentUser, false), "50_50", contactAvatarDrawable, null, currentUser, 0);
  1423. } else if (currentChat != null) {
  1424. if (currentChat.photo != null) {
  1425. currentPhoto = currentChat.photo.photo_small;
  1426. }
  1427. contactAvatarDrawable.setInfo(currentChat);
  1428. locationImageReceiver.setImage(ImageLocation.getForChat(currentChat, false), "50_50", contactAvatarDrawable, null, currentChat, 0);
  1429. } else {
  1430. locationImageReceiver.setImage(null, null, contactAvatarDrawable, null, null, 0);
  1431. }
  1432. infoLayout = new StaticLayout(LocaleController.formatLocationUpdateDate(messageObject.messageOwner.edit_date != 0 ? messageObject.messageOwner.edit_date : messageObject.messageOwner.date), Theme.chat_locationAddressPaint, maxWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
  1433. } else if (!TextUtils.isEmpty(messageObject.messageOwner.media.title)) {
  1434. if (AndroidUtilities.isTablet()) {
  1435. backgroundWidth = Math.min(AndroidUtilities.getMinTabletSide() - AndroidUtilities.dp(isChat && messageObject.needDrawAvatar() && !messageObject.isOutOwner() ? 102 : 50), AndroidUtilities.dp(252 + 37));
  1436. } else {
  1437. backgroundWidth = Math.min(AndroidUtilities.displaySize.x - AndroidUtilities.dp(isChat && messageObject.needDrawAvatar() && !messageObject.isOutOwner() ? 102 : 50), AndroidUtilities.dp(252 + 37));
  1438. }
  1439. backgroundWidth -= AndroidUtilities.dp(4);
  1440. if (checkNeedDrawShareButton(messageObject)) {
  1441. backgroundWidth -= AndroidUtilities.dp(20);
  1442. }
  1443. int maxWidth = backgroundWidth - AndroidUtilities.dp(34);
  1444. availableTimeWidth = maxWidth;
  1445.  
  1446. photoWidth = backgroundWidth - AndroidUtilities.dp(17);
  1447. photoHeight = AndroidUtilities.dp(195);
  1448.  
  1449. mediaBackground = false;
  1450. currentUrl = AndroidUtilities.formapMapUrl(currentAccount, lat, lon, (int) (photoWidth / AndroidUtilities.density), (int) (photoHeight / AndroidUtilities.density), true, 15);
  1451. currentWebFile = WebFile.createWithGeoPoint(point, (int) (photoWidth / AndroidUtilities.density), (int) (photoHeight / AndroidUtilities.density), 15, Math.min(2, (int) Math.ceil(AndroidUtilities.density)));
  1452.  
  1453. docTitleLayout = StaticLayoutEx.createStaticLayout(messageObject.messageOwner.media.title, Theme.chat_locationTitlePaint, maxWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false, TextUtils.TruncateAt.END, maxWidth, 1);
  1454. additionHeight += AndroidUtilities.dp(50);
  1455. int lineCount = docTitleLayout.getLineCount();
  1456. if (!TextUtils.isEmpty(messageObject.messageOwner.media.address)) {
  1457. infoLayout = StaticLayoutEx.createStaticLayout(messageObject.messageOwner.media.address, Theme.chat_locationAddressPaint, maxWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false, TextUtils.TruncateAt.END, maxWidth, 1);
  1458. measureTime(messageObject);
  1459. int timeLeft = backgroundWidth - (int) Math.ceil(infoLayout.getLineWidth(0)) - AndroidUtilities.dp(24);
  1460. if (timeLeft < timeWidth + AndroidUtilities.dp(20 + (messageObject.isOutOwner() ? 20 : 0))) {
  1461. additionHeight += AndroidUtilities.dp(8);
  1462. }
  1463. } else {
  1464. infoLayout = null;
  1465. }
  1466. } else {
  1467. if (AndroidUtilities.isTablet()) {
  1468. backgroundWidth = Math.min(AndroidUtilities.getMinTabletSide() - AndroidUtilities.dp(isChat && messageObject.needDrawAvatar() && !messageObject.isOutOwner() ? 102 : 50), AndroidUtilities.dp(252 + 37));
  1469. } else {
  1470. backgroundWidth = Math.min(AndroidUtilities.displaySize.x - AndroidUtilities.dp(isChat && messageObject.needDrawAvatar() && !messageObject.isOutOwner() ? 102 : 50), AndroidUtilities.dp(252 + 37));
  1471. }
  1472. backgroundWidth -= AndroidUtilities.dp(4);
  1473. if (checkNeedDrawShareButton(messageObject)) {
  1474. backgroundWidth -= AndroidUtilities.dp(20);
  1475. }
  1476. availableTimeWidth = backgroundWidth - AndroidUtilities.dp(34);
  1477.  
  1478. photoWidth = backgroundWidth - AndroidUtilities.dp(8);
  1479. photoHeight = AndroidUtilities.dp(195);
  1480.  
  1481. currentUrl = AndroidUtilities.formapMapUrl(currentAccount, lat, lon, (int) (photoWidth / AndroidUtilities.density), (int) (photoHeight / AndroidUtilities.density), true, 15);
  1482. currentWebFile = WebFile.createWithGeoPoint(point, (int) (photoWidth / AndroidUtilities.density), (int) (photoHeight / AndroidUtilities.density), 15, Math.min(2, (int) Math.ceil(AndroidUtilities.density)));
  1483. }
  1484. if ((int) messageObject.getDialogId() == 0) {
  1485. if (SharedConfig.mapPreviewType == 0) {
  1486. currentMapProvider = 2;
  1487. } else if (SharedConfig.mapPreviewType == 1) {
  1488. currentMapProvider = 1;
  1489. } else {
  1490. currentMapProvider = -1;
  1491. }
  1492. } else {
  1493. currentMapProvider = MessagesController.getInstance(messageObject.currentAccount).mapProvider;
  1494. }
  1495. if (currentMapProvider == -1) {
  1496. photoImage.setImage(null, null, Theme.chat_locationDrawable[messageObject.isOutOwner() ? 1 : 0], null, messageObject, 0);
  1497. } else if (currentMapProvider == 2) {
  1498. if (currentWebFile != null) {
  1499. photoImage.setImage(ImageLocation.getForWebFile(currentWebFile), null, Theme.chat_locationDrawable[messageObject.isOutOwner() ? 1 : 0], null, messageObject, 0);
  1500. }
  1501. } else {
  1502. if (currentMapProvider == 3 || currentMapProvider == 4) {
  1503. ImageLoader.getInstance().addTestWebFile(currentUrl, currentWebFile);
  1504. addedForTest = true;
  1505. }
  1506. if (currentUrl != null) {
  1507. photoImage.setImage(currentUrl, null, Theme.chat_locationDrawable[messageObject.isOutOwner() ? 1 : 0], null, 0);
  1508. }
  1509. }
  1510. } else if (messageObject.isAnyKindOfSticker()) { //sticker
  1511. drawBackground = false;
  1512. boolean isWebpSticker = messageObject.type == MessageObject.TYPE_STICKER;
  1513. for (int a = 0; a < messageObject.messageOwner.media.document.attributes.size(); a++) {
  1514. TLRPC.DocumentAttribute attribute = messageObject.messageOwner.media.document.attributes.get(a);
  1515. if (attribute instanceof TLRPC.TL_documentAttributeImageSize) {
  1516. photoWidth = attribute.w;
  1517. photoHeight = attribute.h;
  1518. }
  1519. }
  1520. if (photoWidth == 0 && photoHeight == 0 && messageObject.isAnimatedSticker()) {
  1521. photoWidth = photoHeight = 512;
  1522. }
  1523. float maxHeight;
  1524. float maxWidth;
  1525. if (AndroidUtilities.isTablet()) {
  1526. maxHeight = maxWidth = AndroidUtilities.getMinTabletSide() * 0.4f;
  1527. } else {
  1528. maxHeight = maxWidth = Math.min(AndroidUtilities.displaySize.x, AndroidUtilities.displaySize.y) * 0.5f;
  1529. }
  1530. if (photoWidth == 0) {
  1531. photoHeight = (int) maxHeight;
  1532. photoWidth = photoHeight + AndroidUtilities.dp(100);
  1533. }
  1534. photoHeight *= maxWidth / photoWidth;
  1535. photoWidth = (int) maxWidth;
  1536. if (photoHeight > maxHeight) {
  1537. photoWidth *= maxHeight / photoHeight;
  1538. photoHeight = (int) maxHeight;
  1539. }
  1540. documentAttachType = DOCUMENT_ATTACH_TYPE_STICKER;
  1541. availableTimeWidth = photoWidth - AndroidUtilities.dp(14);
  1542. backgroundWidth = photoWidth + AndroidUtilities.dp(12);
  1543.  
  1544. currentPhotoObjectThumb = FileLoader.getClosestPhotoSizeWithSize(messageObject.photoThumbs, 40);
  1545. photoParentObject = messageObject.photoThumbsObject;
  1546. if (messageObject.attachPathExists) {
  1547. photoImage.setImage(ImageLocation.getForPath(messageObject.messageOwner.attachPath), String.format(Locale.US, "%d_%d", photoWidth, photoHeight),
  1548. ImageLocation.getForObject(currentPhotoObjectThumb, photoParentObject), "b1",
  1549. messageObject.messageOwner.media.document.size, isWebpSticker ? "webp" : null, messageObject, 1);
  1550. } else if (messageObject.messageOwner.media.document.id != 0) {
  1551. photoImage.setImage(ImageLocation.getForDocument(messageObject.messageOwner.media.document), String.format(Locale.US, "%d_%d", photoWidth, photoHeight),
  1552. ImageLocation.getForObject(currentPhotoObjectThumb, photoParentObject), "b1",
  1553. messageObject.messageOwner.media.document.size, isWebpSticker ? "webp" : null, messageObject, 1);
  1554. }
  1555. } else {
  1556. currentPhotoObject = FileLoader.getClosestPhotoSizeWithSize(messageObject.photoThumbs, AndroidUtilities.getPhotoSize());
  1557. photoParentObject = messageObject.photoThumbsObject;
  1558. int maxPhotoWidth;
  1559. boolean useFullWidth = false;
  1560. if (messageObject.type == MessageObject.TYPE_ROUND_VIDEO) {
  1561. maxPhotoWidth = photoWidth = AndroidUtilities.roundMessageSize;
  1562. documentAttach = messageObject.getDocument();
  1563. documentAttachType = DOCUMENT_ATTACH_TYPE_ROUND;
  1564. } else {
  1565. if (AndroidUtilities.isTablet()) {
  1566. maxPhotoWidth = photoWidth = (int) (AndroidUtilities.getMinTabletSide() * 0.7f);
  1567. } else {
  1568. if (currentPhotoObject != null && (messageObject.type == 1 || messageObject.type == 3 || messageObject.type == 8) && currentPhotoObject.w >= currentPhotoObject.h) {
  1569. maxPhotoWidth = photoWidth = Math.min(AndroidUtilities.displaySize.x, AndroidUtilities.displaySize.y) - AndroidUtilities.dp(64);
  1570. useFullWidth = true;
  1571. } else {
  1572. maxPhotoWidth = photoWidth = (int) (Math.min(AndroidUtilities.displaySize.x, AndroidUtilities.displaySize.y) * 0.7f);
  1573. }
  1574. }
  1575. }
  1576. photoHeight = photoWidth + AndroidUtilities.dp(100);
  1577. if (!useFullWidth) {
  1578. if (messageObject.type != 5 && checkNeedDrawShareButton(messageObject)) {
  1579. maxPhotoWidth -= AndroidUtilities.dp(20);
  1580. photoWidth -= AndroidUtilities.dp(20);
  1581. }
  1582. if (photoWidth > AndroidUtilities.getPhotoSize()) {
  1583. photoWidth = AndroidUtilities.getPhotoSize();
  1584. }
  1585. if (photoHeight > AndroidUtilities.getPhotoSize()) {
  1586. photoHeight = AndroidUtilities.getPhotoSize();
  1587. }
  1588. } else if (isChat && messageObject.needDrawAvatar() && !messageObject.isOutOwner()) {
  1589. photoWidth -= AndroidUtilities.dp(52);
  1590. }
  1591.  
  1592. boolean needQualityPreview = false;
  1593.  
  1594. if (messageObject.type == 1) { //photo
  1595. updateSecretTimeText(messageObject);
  1596. currentPhotoObjectThumb = FileLoader.getClosestPhotoSizeWithSize(messageObject.photoThumbs, 40);
  1597. } else if (messageObject.type == 3 || messageObject.type == 8) { //video, gif
  1598. createDocumentLayout(0, messageObject);
  1599. currentPhotoObjectThumb = FileLoader.getClosestPhotoSizeWithSize(messageObject.photoThumbs, 40);
  1600. updateSecretTimeText(messageObject);
  1601. needQualityPreview = true;
  1602. } else if (messageObject.type == MessageObject.TYPE_ROUND_VIDEO) {
  1603. currentPhotoObjectThumb = FileLoader.getClosestPhotoSizeWithSize(messageObject.photoThumbs, 40);
  1604. needQualityPreview = true;
  1605. }
  1606. int w;
  1607. int h;
  1608. if (messageObject.type == MessageObject.TYPE_ROUND_VIDEO) {
  1609. w = h = AndroidUtilities.roundMessageSize;
  1610. } else {
  1611. TLRPC.PhotoSize size = currentPhotoObject != null ? currentPhotoObject : currentPhotoObjectThumb;
  1612. int imageW = 0;
  1613. int imageH = 0;
  1614. if (size != null) {
  1615. imageW = size.w;
  1616. imageH = size.h;
  1617. } else if (documentAttach != null) {
  1618. for (int a = 0, N = documentAttach.attributes.size(); a < N; a++) {
  1619. TLRPC.DocumentAttribute attribute = documentAttach.attributes.get(a);
  1620. if (attribute instanceof TLRPC.TL_documentAttributeVideo) {
  1621. imageW = attribute.w;
  1622. imageH = attribute.h;
  1623. }
  1624. }
  1625. }
  1626. float scale = (float) imageW / (float) photoWidth;
  1627. w = (int) (imageW / scale);
  1628. h = (int) (imageH / scale);
  1629. if (w == 0) {
  1630. w = AndroidUtilities.dp(150);
  1631. }
  1632. if (h == 0) {
  1633. h = AndroidUtilities.dp(150);
  1634. }
  1635. if (h > photoHeight) {
  1636. float scale2 = h;
  1637. h = photoHeight;
  1638. scale2 /= h;
  1639. w = (int) (w / scale2);
  1640. } else if (h < AndroidUtilities.dp(120)) {
  1641. h = AndroidUtilities.dp(120);
  1642. float hScale = (float) imageH / h;
  1643. if (imageW / hScale < photoWidth) {
  1644. w = (int) (imageW / hScale);
  1645. }
  1646. }
  1647. }
  1648. if (currentPhotoObject != null && "s".equals(currentPhotoObject.type)) {
  1649. currentPhotoObject = null;
  1650. }
  1651.  
  1652. if (currentPhotoObject != null && currentPhotoObject == currentPhotoObjectThumb) {
  1653. if (messageObject.type == 1) {
  1654. currentPhotoObjectThumb = null;
  1655. } else {
  1656. currentPhotoObject = null;
  1657. }
  1658. }
  1659.  
  1660. if (needQualityPreview) {
  1661. /*if ((DownloadController.getInstance(currentAccount).getAutodownloadMask() & DownloadController.AUTODOWNLOAD_TYPE_PHOTO) == 0) {
  1662. currentPhotoObject = null;
  1663. }*/
  1664. if (!messageObject.needDrawBluredPreview() && (currentPhotoObject == null || currentPhotoObject == currentPhotoObjectThumb) && (currentPhotoObjectThumb == null || !"m".equals(currentPhotoObjectThumb.type))) {
  1665. photoImage.setNeedsQualityThumb(true);
  1666. photoImage.setShouldGenerateQualityThumb(true);
  1667. }
  1668. }
  1669.  
  1670. if (currentMessagesGroup == null && messageObject.caption != null) {
  1671. mediaBackground = false;
  1672. }
  1673.  
  1674. if ((w == 0 || h == 0) && messageObject.type == 8) {
  1675. for (int a = 0; a < messageObject.messageOwner.media.document.attributes.size(); a++) {
  1676. TLRPC.DocumentAttribute attribute = messageObject.messageOwner.media.document.attributes.get(a);
  1677. if (attribute instanceof TLRPC.TL_documentAttributeImageSize || attribute instanceof TLRPC.TL_documentAttributeVideo) {
  1678. float scale = (float) attribute.w / (float) photoWidth;
  1679. w = (int) (attribute.w / scale);
  1680. h = (int) (attribute.h / scale);
  1681. if (h > photoHeight) {
  1682. float scale2 = h;
  1683. h = photoHeight;
  1684. scale2 /= h;
  1685. w = (int) (w / scale2);
  1686. } else if (h < AndroidUtilities.dp(120)) {
  1687. h = AndroidUtilities.dp(120);
  1688. float hScale = (float) attribute.h / h;
  1689. if (attribute.w / hScale < photoWidth) {
  1690. w = (int) (attribute.w / hScale);
  1691. }
  1692. }
  1693. break;
  1694. }
  1695. }
  1696. }
  1697.  
  1698. if (w == 0 || h == 0) {
  1699. w = h = AndroidUtilities.dp(150);
  1700. }
  1701. if (messageObject.type == 3) {
  1702. if (w < infoWidth + AndroidUtilities.dp(16 + 24)) {
  1703. w = infoWidth + AndroidUtilities.dp(16 + 24);
  1704. }
  1705. }
  1706.  
  1707. if (currentMessagesGroup != null) {
  1708. int firstLineWidth = 0;
  1709. int dWidth = getGroupPhotosWidth();
  1710. for (int a = 0; a < currentMessagesGroup.posArray.size(); a++) {
  1711. MessageObject.GroupedMessagePosition position = currentMessagesGroup.posArray.get(a);
  1712. if (position.minY == 0) {
  1713. firstLineWidth += Math.ceil((position.pw + position.leftSpanOffset) / 1000.0f * dWidth);
  1714. } else {
  1715. break;
  1716. }
  1717. }
  1718. availableTimeWidth = firstLineWidth - AndroidUtilities.dp(35);
  1719. } else {
  1720. availableTimeWidth = maxPhotoWidth - AndroidUtilities.dp(14);
  1721. }
  1722. if (messageObject.type == MessageObject.TYPE_ROUND_VIDEO) {
  1723. availableTimeWidth -= Math.ceil(Theme.chat_audioTimePaint.measureText("00:00")) + AndroidUtilities.dp(26);
  1724. }
  1725. measureTime(messageObject);
  1726. int timeWidthTotal = timeWidth + AndroidUtilities.dp(14 + (messageObject.isOutOwner() ? 20 : 0));
  1727. if (w < timeWidthTotal) {
  1728. w = timeWidthTotal;
  1729. }
  1730.  
  1731. if (messageObject.isRoundVideo()) {
  1732. w = h = Math.min(w, h);
  1733. drawBackground = false;
  1734. photoImage.setRoundRadius(w / 2);
  1735. } else if (messageObject.needDrawBluredPreview()) {
  1736. if (AndroidUtilities.isTablet()) {
  1737. w = h = (int) (AndroidUtilities.getMinTabletSide() * 0.5f);
  1738. } else {
  1739. w = h = (int) (Math.min(AndroidUtilities.displaySize.x, AndroidUtilities.displaySize.y) * 0.5f);
  1740. }
  1741. }
  1742.  
  1743. int widthForCaption = 0;
  1744. boolean fixPhotoWidth = false;
  1745. if (currentMessagesGroup != null) {
  1746. float maxHeight = Math.max(AndroidUtilities.displaySize.x, AndroidUtilities.displaySize.y) * 0.5f;
  1747. int dWidth = getGroupPhotosWidth();
  1748. w = (int) Math.ceil(currentPosition.pw / 1000.0f * dWidth);
  1749. if (currentPosition.minY != 0 && (messageObject.isOutOwner() && (currentPosition.flags & MessageObject.POSITION_FLAG_LEFT) != 0 || !messageObject.isOutOwner() && (currentPosition.flags & MessageObject.POSITION_FLAG_RIGHT) != 0)) {
  1750. int firstLineWidth = 0;
  1751. int currentLineWidth = 0;
  1752. for (int a = 0; a < currentMessagesGroup.posArray.size(); a++) {
  1753. MessageObject.GroupedMessagePosition position = currentMessagesGroup.posArray.get(a);
  1754. if (position.minY == 0) {
  1755. firstLineWidth += Math.ceil(position.pw / 1000.0f * dWidth) + (position.leftSpanOffset != 0 ? Math.ceil(position.leftSpanOffset / 1000.0f * dWidth) : 0);
  1756. } else if (position.minY == currentPosition.minY) {
  1757. currentLineWidth += Math.ceil((position.pw) / 1000.0f * dWidth) + (position.leftSpanOffset != 0 ? Math.ceil(position.leftSpanOffset / 1000.0f * dWidth) : 0);
  1758. } else if (position.minY > currentPosition.minY) {
  1759. break;
  1760. }
  1761. }
  1762. w += firstLineWidth - currentLineWidth;
  1763. }
  1764. w -= AndroidUtilities.dp(9);
  1765. if (isAvatarVisible) {
  1766. w -= AndroidUtilities.dp(48);
  1767. }
  1768. if (currentPosition.siblingHeights != null) {
  1769. h = 0;
  1770. for (int a = 0; a < currentPosition.siblingHeights.length; a++) {
  1771. h += (int) Math.ceil(maxHeight * currentPosition.siblingHeights[a]);
  1772. }
  1773. h += (currentPosition.maxY - currentPosition.minY) * Math.round(7 * AndroidUtilities.density); //TODO fix
  1774. } else {
  1775. h = (int) Math.ceil(maxHeight * currentPosition.ph);
  1776. }
  1777. backgroundWidth = w;
  1778. if ((currentPosition.flags & MessageObject.POSITION_FLAG_RIGHT) != 0 && (currentPosition.flags & MessageObject.POSITION_FLAG_LEFT) != 0) {
  1779. w -= AndroidUtilities.dp(8);
  1780. } else if ((currentPosition.flags & MessageObject.POSITION_FLAG_RIGHT) == 0 && (currentPosition.flags & MessageObject.POSITION_FLAG_LEFT) == 0) {
  1781. w -= AndroidUtilities.dp(11);
  1782. } else if ((currentPosition.flags & MessageObject.POSITION_FLAG_RIGHT) != 0) {
  1783. w -= AndroidUtilities.dp(10);
  1784. } else {
  1785. w -= AndroidUtilities.dp(9);
  1786. }
  1787. photoWidth = w;
  1788. if (!currentPosition.edge) {
  1789. photoWidth += AndroidUtilities.dp(10);
  1790. }
  1791. photoHeight = h;
  1792. widthForCaption += photoWidth - AndroidUtilities.dp(10);
  1793. if ((currentPosition.flags & MessageObject.POSITION_FLAG_BOTTOM) != 0 || currentMessagesGroup.hasSibling && (currentPosition.flags & MessageObject.POSITION_FLAG_TOP) == 0) {
  1794. widthForCaption += getAdditionalWidthForPosition(currentPosition);
  1795. int count = currentMessagesGroup.messages.size();
  1796. for (int i = 0; i < count; i++) {
  1797. MessageObject m = currentMessagesGroup.messages.get(i);
  1798. MessageObject.GroupedMessagePosition rowPosition = currentMessagesGroup.posArray.get(i);
  1799. if (rowPosition != currentPosition && (rowPosition.flags & MessageObject.POSITION_FLAG_BOTTOM) != 0) {
  1800. w = (int) Math.ceil(rowPosition.pw / 1000.0f * dWidth);
  1801. if (rowPosition.minY != 0 && (messageObject.isOutOwner() && (rowPosition.flags & MessageObject.POSITION_FLAG_LEFT) != 0 || !messageObject.isOutOwner() && (rowPosition.flags & MessageObject.POSITION_FLAG_RIGHT) != 0)) {
  1802. int firstLineWidth = 0;
  1803. int currentLineWidth = 0;
  1804. for (int a = 0; a < currentMessagesGroup.posArray.size(); a++) {
  1805. MessageObject.GroupedMessagePosition position = currentMessagesGroup.posArray.get(a);
  1806. if (position.minY == 0) {
  1807. firstLineWidth += Math.ceil(position.pw / 1000.0f * dWidth) + (position.leftSpanOffset != 0 ? Math.ceil(position.leftSpanOffset / 1000.0f * dWidth) : 0);
  1808. } else if (position.minY == rowPosition.minY) {
  1809. currentLineWidth += Math.ceil((position.pw) / 1000.0f * dWidth) + (position.leftSpanOffset != 0 ? Math.ceil(position.leftSpanOffset / 1000.0f * dWidth) : 0);
  1810. } else if (position.minY > rowPosition.minY) {
  1811. break;
  1812. }
  1813. }
  1814. w += firstLineWidth - currentLineWidth;
  1815. }
  1816. w -= AndroidUtilities.dp(9);
  1817. if ((rowPosition.flags & MessageObject.POSITION_FLAG_RIGHT) != 0 && (rowPosition.flags & MessageObject.POSITION_FLAG_LEFT) != 0) {
  1818. w -= AndroidUtilities.dp(8);
  1819. } else if ((rowPosition.flags & MessageObject.POSITION_FLAG_RIGHT) == 0 && (rowPosition.flags & MessageObject.POSITION_FLAG_LEFT) == 0) {
  1820. w -= AndroidUtilities.dp(11);
  1821. } else if ((rowPosition.flags & MessageObject.POSITION_FLAG_RIGHT) != 0) {
  1822. w -= AndroidUtilities.dp(10);
  1823. } else {
  1824. w -= AndroidUtilities.dp(9);
  1825. }
  1826. if (isChat && !m.isOutOwner() && m.needDrawAvatar() && (rowPosition == null || rowPosition.edge)) {
  1827. w -= AndroidUtilities.dp(48);
  1828. }
  1829. w += getAdditionalWidthForPosition(rowPosition);
  1830. if (!rowPosition.edge) {
  1831. w += AndroidUtilities.dp(10);
  1832. }
  1833. widthForCaption += w;
  1834. if (rowPosition.minX < currentPosition.minX || currentMessagesGroup.hasSibling && rowPosition.minY != rowPosition.maxY) {
  1835. captionOffsetX -= w;
  1836. }
  1837. }
  1838. if (m.caption != null) {
  1839. if (currentCaption != null) {
  1840. currentCaption = null;
  1841. break;
  1842. } else {
  1843. currentCaption = m.caption;
  1844. }
  1845. }
  1846. }
  1847. }
  1848. } else {
  1849. photoHeight = h;
  1850. photoWidth = w;
  1851. currentCaption = messageObject.caption;
  1852.  
  1853. int minCaptionWidth;
  1854. if (AndroidUtilities.isTablet()) {
  1855. minCaptionWidth = (int) (AndroidUtilities.getMinTabletSide() * 0.65f);
  1856. } else {
  1857. minCaptionWidth = (int) (Math.min(AndroidUtilities.displaySize.x, AndroidUtilities.displaySize.y) * 0.65f);
  1858. }
  1859. if (!messageObject.needDrawBluredPreview() && currentCaption != null && photoWidth < minCaptionWidth) {
  1860. widthForCaption = minCaptionWidth;
  1861. fixPhotoWidth = true;
  1862. } else {
  1863. widthForCaption = photoWidth - AndroidUtilities.dp(10);
  1864. }
  1865.  
  1866. backgroundWidth = photoWidth + AndroidUtilities.dp(8);
  1867. if (!mediaBackground) {
  1868. backgroundWidth += AndroidUtilities.dp(9);
  1869. }
  1870. }
  1871.  
  1872. if (currentCaption != null) {
  1873. try {
  1874. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  1875. captionLayout = StaticLayout.Builder.obtain(currentCaption, 0, currentCaption.length(), Theme.chat_msgTextPaint, widthForCaption)
  1876. .setBreakStrategy(StaticLayout.BREAK_STRATEGY_HIGH_QUALITY)
  1877. .setHyphenationFrequency(StaticLayout.HYPHENATION_FREQUENCY_NONE)
  1878. .setAlignment(Layout.Alignment.ALIGN_NORMAL)
  1879. .build();
  1880. } else {
  1881. captionLayout = new StaticLayout(currentCaption, Theme.chat_msgTextPaint, widthForCaption, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
  1882. }
  1883. int lineCount = captionLayout.getLineCount();
  1884. if (lineCount > 0) {
  1885. if (fixPhotoWidth) {
  1886. captionWidth = 0;
  1887. for (int a = 0; a < lineCount; a++) {
  1888. captionWidth = (int) Math.max(captionWidth, Math.ceil(captionLayout.getLineWidth(a)));
  1889. if (captionLayout.getLineLeft(a) != 0) {
  1890. captionWidth = widthForCaption;
  1891. break;
  1892. }
  1893. }
  1894. if (captionWidth > widthForCaption) {
  1895. captionWidth = widthForCaption;
  1896. }
  1897. } else {
  1898. captionWidth = widthForCaption;
  1899. }
  1900. captionHeight = captionLayout.getHeight();
  1901. addedCaptionHeight = captionHeight + AndroidUtilities.dp(9);
  1902. if (currentPosition == null || (currentPosition.flags & MessageObject.POSITION_FLAG_BOTTOM) != 0) {
  1903. additionHeight += addedCaptionHeight;
  1904. int widthToCheck = Math.max(captionWidth, photoWidth - AndroidUtilities.dp(10));
  1905. float lastLineWidth = captionLayout.getLineWidth(captionLayout.getLineCount() - 1) + captionLayout.getLineLeft(captionLayout.getLineCount() - 1);
  1906. if (widthToCheck + AndroidUtilities.dp(2) - lastLineWidth < timeWidthTotal) {
  1907. additionHeight += AndroidUtilities.dp(14);
  1908. addedCaptionHeight += AndroidUtilities.dp(14);
  1909. captionNewLine = 1;
  1910. }
  1911. } else {
  1912. captionLayout = null;
  1913. }
  1914. }
  1915. } catch (Exception e) {
  1916. FileLog.e(e);
  1917. }
  1918. }
  1919.  
  1920. if (fixPhotoWidth && photoWidth < captionWidth + AndroidUtilities.dp(10)) {
  1921. photoWidth = captionWidth + AndroidUtilities.dp(10);
  1922. backgroundWidth = photoWidth + AndroidUtilities.dp(8);
  1923. if (!mediaBackground) {
  1924. backgroundWidth += AndroidUtilities.dp(9);
  1925. }
  1926. }
  1927.  
  1928. currentPhotoFilter = currentPhotoFilterThumb = String.format(Locale.US, "%d_%d", (int) (w / AndroidUtilities.density), (int) (h / AndroidUtilities.density));
  1929. if (messageObject.photoThumbs != null && messageObject.photoThumbs.size() > 1 || messageObject.type == 3 || messageObject.type == 8 || messageObject.type == MessageObject.TYPE_ROUND_VIDEO) {
  1930. if (messageObject.needDrawBluredPreview()) {
  1931. currentPhotoFilter += "_b2";
  1932. currentPhotoFilterThumb += "_b2";
  1933. } else {
  1934. currentPhotoFilterThumb += "_b";
  1935. }
  1936. }
  1937.  
  1938. boolean noSize = false;
  1939. if (messageObject.type == 3 || messageObject.type == 8 || messageObject.type == MessageObject.TYPE_ROUND_VIDEO) {
  1940. noSize = true;
  1941. }
  1942. if (currentPhotoObject != null && !noSize && currentPhotoObject.size == 0) {
  1943. currentPhotoObject.size = -1;
  1944. }
  1945. if (currentPhotoObjectThumb != null && !noSize && currentPhotoObjectThumb.size == 0) {
  1946. currentPhotoObjectThumb.size = -1;
  1947. }
  1948.  
  1949. if (SharedConfig.autoplayVideo && messageObject.type == 3 && !messageObject.needDrawBluredPreview() && (
  1950. currentMessageObject.mediaExists ||
  1951. messageObject.canStreamVideo() && DownloadController.getInstance(currentAccount).canDownloadMedia(currentMessageObject)
  1952. )) {
  1953. if (currentPosition != null) {
  1954. autoPlayingMedia = (currentPosition.flags & MessageObject.POSITION_FLAG_LEFT) != 0 && (currentPosition.flags & MessageObject.POSITION_FLAG_RIGHT) != 0;
  1955. } else {
  1956. autoPlayingMedia = true;
  1957. }
  1958. }
  1959.  
  1960. if (autoPlayingMedia) {
  1961. photoImage.setAllowStartAnimation(true);
  1962. photoImage.startAnimation();
  1963. TLRPC.Document document = messageObject.messageOwner.media.document;
  1964. photoImage.setImage(ImageLocation.getForDocument(document), ImageLoader.AUTOPLAY_FILTER, ImageLocation.getForObject(currentPhotoObject, photoParentObject), currentPhotoFilter, ImageLocation.getForDocument(currentPhotoObjectThumb, document), currentPhotoFilterThumb, null, messageObject.messageOwner.media.document.size, null, messageObject, 0);
  1965. } else if (messageObject.type == 1) {
  1966. if (messageObject.useCustomPhoto) {
  1967. photoImage.setImageBitmap(getResources().getDrawable(R.drawable.theme_preview_image));
  1968. } else {
  1969. if (currentPhotoObject != null) {
  1970. boolean photoExist = true;
  1971. String fileName = FileLoader.getAttachFileName(currentPhotoObject);
  1972. if (messageObject.mediaExists) {
  1973. DownloadController.getInstance(currentAccount).removeLoadingFileObserver(this);
  1974. } else {
  1975. photoExist = false;
  1976. }
  1977. if (photoExist || DownloadController.getInstance(currentAccount).canDownloadMedia(currentMessageObject) || FileLoader.getInstance(currentAccount).isLoadingFile(fileName)) {
  1978. photoImage.setImage(ImageLocation.getForObject(currentPhotoObject, photoParentObject), currentPhotoFilter, ImageLocation.getForObject(currentPhotoObjectThumb, photoParentObject), currentPhotoFilterThumb, noSize ? 0 : currentPhotoObject.size, null, currentMessageObject, currentMessageObject.shouldEncryptPhotoOrVideo() ? 2 : 0);
  1979. } else {
  1980. photoNotSet = true;
  1981. if (currentPhotoObjectThumb != null) {
  1982. photoImage.setImage(null, null, ImageLocation.getForObject(currentPhotoObjectThumb, photoParentObject), currentPhotoFilterThumb, 0, null, currentMessageObject, currentMessageObject.shouldEncryptPhotoOrVideo() ? 2 : 0);
  1983. } else {
  1984. photoImage.setImageBitmap((Drawable) null);
  1985. }
  1986. }
  1987. } else {
  1988. photoImage.setImageBitmap((Drawable) null);
  1989. }
  1990. }
  1991. } else if (messageObject.type == 8 || messageObject.type == MessageObject.TYPE_ROUND_VIDEO) {
  1992. String fileName = FileLoader.getAttachFileName(messageObject.messageOwner.media.document);
  1993. int localFile = 0;
  1994. if (messageObject.attachPathExists) {
  1995. DownloadController.getInstance(currentAccount).removeLoadingFileObserver(this);
  1996. localFile = 1;
  1997. } else if (messageObject.mediaExists) {
  1998. localFile = 2;
  1999. }
  2000. boolean autoDownload = false;
  2001. if (MessageObject.isGifDocument(messageObject.messageOwner.media.document) || messageObject.type == MessageObject.TYPE_ROUND_VIDEO) {
  2002. autoDownload = DownloadController.getInstance(currentAccount).canDownloadMedia(currentMessageObject);
  2003. }
  2004. if (!messageObject.isSending() && !messageObject.isEditing() && (localFile != 0 || FileLoader.getInstance(currentAccount).isLoadingFile(fileName) || autoDownload)) {
  2005. if (localFile != 1 && !messageObject.needDrawBluredPreview() && (localFile != 0 || messageObject.canStreamVideo() && autoDownload)) {
  2006. autoPlayingMedia = true;
  2007. //TODO here they setting up gif with thumb
  2008. photoImage.setImage(ImageLocation.getForDocument(messageObject.messageOwner.media.document), ImageLoader.AUTOPLAY_FILTER, ImageLocation.getForObject(currentPhotoObject, photoParentObject), currentPhotoFilter, ImageLocation.getForObject(currentPhotoObjectThumb, photoParentObject), currentPhotoFilterThumb, null, messageObject.messageOwner.media.document.size, null, messageObject, 0);
  2009. } else if (localFile == 1) {
  2010. photoImage.setImage(ImageLocation.getForPath(messageObject.isSendError() ? null : messageObject.messageOwner.attachPath), null, ImageLocation.getForObject(currentPhotoObjectThumb, photoParentObject), currentPhotoFilterThumb, 0, null, messageObject, 0);
  2011. } else {
  2012. photoImage.setImage(ImageLocation.getForDocument(messageObject.messageOwner.media.document), null, ImageLocation.getForObject(currentPhotoObject, photoParentObject), currentPhotoFilter, ImageLocation.getForObject(currentPhotoObjectThumb, photoParentObject), currentPhotoFilterThumb, null, messageObject.messageOwner.media.document.size, null, messageObject, 0);
  2013. }
  2014. } else {
  2015. photoImage.setImage(ImageLocation.getForObject(currentPhotoObject, photoParentObject), currentPhotoFilter, ImageLocation.getForObject(currentPhotoObjectThumb, photoParentObject), currentPhotoFilterThumb, 0, null, messageObject, 0);
  2016. }
  2017. } else {
  2018. photoImage.setImage(ImageLocation.getForObject(currentPhotoObject, photoParentObject), currentPhotoFilter, ImageLocation.getForObject(currentPhotoObjectThumb, photoParentObject), currentPhotoFilterThumb, 0, null, messageObject, currentMessageObject.shouldEncryptPhotoOrVideo() ? 2 : 0);
  2019. }
  2020. }
  2021. setMessageObjectInternal(messageObject);
  2022.  
  2023. if (drawForwardedName && messageObject.needDrawForwarded() && (currentPosition == null || currentPosition.minY == 0)) {
  2024. if (messageObject.type != 5) {
  2025. namesOffset += AndroidUtilities.dp(5);
  2026. }
  2027. } else if (drawNameLayout && messageObject.messageOwner.reply_to_msg_id == 0) {
  2028. namesOffset += AndroidUtilities.dp(7);
  2029. }
  2030. totalHeight = photoHeight + AndroidUtilities.dp(14) + namesOffset + additionHeight;
  2031. if (currentPosition != null && (currentPosition.flags & MessageObject.POSITION_FLAG_BOTTOM) == 0) {
  2032. totalHeight -= AndroidUtilities.dp(3);
  2033. }
  2034.  
  2035. int additionalTop = 0;
  2036. if (currentPosition != null) {
  2037. photoWidth += getAdditionalWidthForPosition(currentPosition);
  2038. if ((currentPosition.flags & MessageObject.POSITION_FLAG_TOP) == 0) {
  2039. photoHeight += AndroidUtilities.dp(4);
  2040. additionalTop -= AndroidUtilities.dp(4);
  2041. }
  2042. if ((currentPosition.flags & MessageObject.POSITION_FLAG_BOTTOM) == 0) {
  2043. photoHeight += AndroidUtilities.dp(1);
  2044. }
  2045. }
  2046.  
  2047. if (drawPinnedTop) {
  2048. namesOffset -= AndroidUtilities.dp(1);
  2049. }
  2050.  
  2051. int y;
  2052. if (currentPosition != null) {
  2053. if (namesOffset > 0) {
  2054. y = AndroidUtilities.dp(7);
  2055. totalHeight -= AndroidUtilities.dp(2);
  2056. } else {
  2057. y = AndroidUtilities.dp(5);
  2058. totalHeight -= AndroidUtilities.dp(4);
  2059. }
  2060. } else {
  2061. if (namesOffset > 0) {
  2062. y = AndroidUtilities.dp(7);
  2063. totalHeight -= AndroidUtilities.dp(2);
  2064. } else {
  2065. y = AndroidUtilities.dp(5);
  2066. totalHeight -= AndroidUtilities.dp(4);
  2067. }
  2068. }
  2069. photoImage.setImageCoords(0, y + namesOffset + additionalTop, photoWidth, photoHeight);
  2070. invalidate();
  2071. }
  2072.  
  2073. if (currentPosition == null && !messageObject.isAnyKindOfSticker() && addedCaptionHeight == 0) {
  2074. if (captionLayout == null && messageObject.caption != null) {
  2075. try {
  2076. currentCaption = messageObject.caption;
  2077. int width = backgroundWidth - AndroidUtilities.dp(31);
  2078. int widthForCaption = width - AndroidUtilities.dp(10);
  2079. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  2080. captionLayout = StaticLayout.Builder.obtain(messageObject.caption, 0, messageObject.caption.length(), Theme.chat_msgTextPaint, widthForCaption)
  2081. .setBreakStrategy(StaticLayout.BREAK_STRATEGY_HIGH_QUALITY)
  2082. .setHyphenationFrequency(StaticLayout.HYPHENATION_FREQUENCY_NONE)
  2083. .setAlignment(Layout.Alignment.ALIGN_NORMAL)
  2084. .build();
  2085. } else {
  2086. captionLayout = new StaticLayout(messageObject.caption, Theme.chat_msgTextPaint, widthForCaption, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
  2087. }
  2088. } catch (Exception e) {
  2089. FileLog.e(e);
  2090. }
  2091. }
  2092. if (captionLayout != null) {
  2093. try {
  2094. int width = backgroundWidth - AndroidUtilities.dp(31);
  2095. int widthForCaption = width - AndroidUtilities.dp(10);
  2096. if (captionLayout != null && captionLayout.getLineCount() > 0) {
  2097. captionWidth = widthForCaption;
  2098. int timeWidthTotal = timeWidth + (messageObject.isOutOwner() ? AndroidUtilities.dp(20) : 0);
  2099. captionHeight = captionLayout.getHeight();
  2100. totalHeight += captionHeight + AndroidUtilities.dp(9);
  2101. float lastLineWidth = captionLayout.getLineWidth(captionLayout.getLineCount() - 1) + captionLayout.getLineLeft(captionLayout.getLineCount() - 1);
  2102. if (width - AndroidUtilities.dp(8) - lastLineWidth < timeWidthTotal) {
  2103. totalHeight += AndroidUtilities.dp(14);
  2104. captionHeight += AndroidUtilities.dp(14);
  2105. captionNewLine = 2;
  2106. }
  2107. }
  2108. } catch (Exception e) {
  2109. FileLog.e(e);
  2110. }
  2111. }
  2112. }
  2113. if (captionLayout == null && widthBeforeNewTimeLine != -1 && availableTimeWidth - widthBeforeNewTimeLine < timeWidth) {
  2114. totalHeight += AndroidUtilities.dp(14);
  2115. }
  2116.  
  2117. if (currentMessageObject.eventId != 0 && !currentMessageObject.isMediaEmpty() && currentMessageObject.messageOwner.media.webpage != null) {
  2118. int linkPreviewMaxWidth = backgroundWidth - AndroidUtilities.dp(41);
  2119. hasOldCaptionPreview = true;
  2120. linkPreviewHeight = 0;
  2121. TLRPC.WebPage webPage = currentMessageObject.messageOwner.media.webpage;
  2122. try {
  2123. int width = siteNameWidth = (int) Math.ceil(Theme.chat_replyNamePaint.measureText(webPage.site_name) + 1);
  2124. siteNameLayout = new StaticLayout(webPage.site_name, Theme.chat_replyNamePaint, Math.min(width, linkPreviewMaxWidth), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
  2125. siteNameRtl = siteNameLayout.getLineLeft(0) != 0;
  2126. int height = siteNameLayout.getLineBottom(siteNameLayout.getLineCount() - 1);
  2127. linkPreviewHeight += height;
  2128. totalHeight += height;
  2129. } catch (Exception e) {
  2130. FileLog.e(e);
  2131. }
  2132.  
  2133. try {
  2134. descriptionX = 0;
  2135. if (linkPreviewHeight != 0) {
  2136. totalHeight += AndroidUtilities.dp(2);
  2137. }
  2138.  
  2139. descriptionLayout = StaticLayoutEx.createStaticLayout(webPage.description, Theme.chat_replyTextPaint, linkPreviewMaxWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, AndroidUtilities.dp(1), false, TextUtils.TruncateAt.END, linkPreviewMaxWidth, 6);
  2140.  
  2141. int height = descriptionLayout.getLineBottom(descriptionLayout.getLineCount() - 1);
  2142. linkPreviewHeight += height;
  2143. totalHeight += height;
  2144.  
  2145. for (int a = 0; a < descriptionLayout.getLineCount(); a++) {
  2146. int lineLeft = (int) Math.ceil(descriptionLayout.getLineLeft(a));
  2147. if (lineLeft != 0) {
  2148. if (descriptionX == 0) {
  2149. descriptionX = -lineLeft;
  2150. } else {
  2151. descriptionX = Math.max(descriptionX, -lineLeft);
  2152. }
  2153. }
  2154. }
  2155. } catch (Exception e) {
  2156. FileLog.e(e);
  2157. }
  2158.  
  2159. totalHeight += AndroidUtilities.dp(17);
  2160. if (captionNewLine != 0) {
  2161. totalHeight -= AndroidUtilities.dp(14);
  2162. if (captionNewLine == 2) {
  2163. captionHeight -= AndroidUtilities.dp(14);
  2164. }
  2165. }
  2166. }
  2167.  
  2168. botButtons.clear();
  2169. if (messageIdChanged) {
  2170. botButtonsByData.clear();
  2171. botButtonsByPosition.clear();
  2172. botButtonsLayout = null;
  2173. }
  2174. if (currentPosition == null && messageObject.messageOwner.reply_markup instanceof TLRPC.TL_replyInlineMarkup) {
  2175. int rows = messageObject.messageOwner.reply_markup.rows.size();
  2176. substractBackgroundHeight = keyboardHeight = AndroidUtilities.dp(44 + 4) * rows + AndroidUtilities.dp(1);
  2177.  
  2178. widthForButtons = backgroundWidth - AndroidUtilities.dp(mediaBackground ? 0 : 9);
  2179. boolean fullWidth = false;
  2180. if (messageObject.wantedBotKeyboardWidth > widthForButtons) {
  2181. int maxButtonWidth = -AndroidUtilities.dp(isChat && messageObject.needDrawAvatar() && !messageObject.isOutOwner() ? 62 : 10);
  2182. if (AndroidUtilities.isTablet()) {
  2183. maxButtonWidth += AndroidUtilities.getMinTabletSide();
  2184. } else {
  2185. maxButtonWidth += Math.min(AndroidUtilities.displaySize.x, AndroidUtilities.displaySize.y) - AndroidUtilities.dp(5);
  2186. }
  2187. widthForButtons = Math.max(backgroundWidth, Math.min(messageObject.wantedBotKeyboardWidth, maxButtonWidth));
  2188. }
  2189.  
  2190. int maxButtonsWidth = 0;
  2191. HashMap<String, BotButton> oldByData = new HashMap<>(botButtonsByData);
  2192. HashMap<String, BotButton> oldByPosition;
  2193. if (messageObject.botButtonsLayout != null && botButtonsLayout != null && botButtonsLayout.equals(messageObject.botButtonsLayout.toString())) {
  2194. oldByPosition = new HashMap<>(botButtonsByPosition);
  2195. } else {
  2196. if (messageObject.botButtonsLayout != null) {
  2197. botButtonsLayout = messageObject.botButtonsLayout.toString();
  2198. }
  2199. oldByPosition = null;
  2200. }
  2201. botButtonsByData.clear();
  2202. for (int a = 0; a < rows; a++) {
  2203. TLRPC.TL_keyboardButtonRow row = messageObject.messageOwner.reply_markup.rows.get(a);
  2204. int buttonsCount = row.buttons.size();
  2205. if (buttonsCount == 0) {
  2206. continue;
  2207. }
  2208. int buttonWidth = (widthForButtons - AndroidUtilities.dp(5) * (buttonsCount - 1) - AndroidUtilities.dp(2)) / buttonsCount;
  2209. for (int b = 0; b < row.buttons.size(); b++) {
  2210. BotButton botButton = new BotButton();
  2211. botButton.button = row.buttons.get(b);
  2212. String key = Utilities.bytesToHex(botButton.button.data);
  2213. String position = a + "" + b;
  2214. BotButton oldButton;
  2215. if (oldByPosition != null) {
  2216. oldButton = oldByPosition.get(position);
  2217. } else {
  2218. oldButton = oldByData.get(key);
  2219. }
  2220. if (oldButton != null) {
  2221. botButton.progressAlpha = oldButton.progressAlpha;
  2222. botButton.angle = oldButton.angle;
  2223. botButton.lastUpdateTime = oldButton.lastUpdateTime;
  2224. } else {
  2225. botButton.lastUpdateTime = System.currentTimeMillis();
  2226. }
  2227. botButtonsByData.put(key, botButton);
  2228. botButtonsByPosition.put(position, botButton);
  2229. botButton.x = b * (buttonWidth + AndroidUtilities.dp(5));
  2230. botButton.y = a * AndroidUtilities.dp(44 + 4) + AndroidUtilities.dp(5);
  2231. botButton.width = buttonWidth;
  2232. botButton.height = AndroidUtilities.dp(44);
  2233. CharSequence buttonText;
  2234. if (botButton.button instanceof TLRPC.TL_keyboardButtonBuy && (messageObject.messageOwner.media.flags & 4) != 0) {
  2235. buttonText = LocaleController.getString("PaymentReceipt", R.string.PaymentReceipt);
  2236. } else {
  2237. buttonText = Emoji.replaceEmoji(botButton.button.text, Theme.chat_botButtonPaint.getFontMetricsInt(), AndroidUtilities.dp(15), false);
  2238. buttonText = TextUtils.ellipsize(buttonText, Theme.chat_botButtonPaint, buttonWidth - AndroidUtilities.dp(10), TextUtils.TruncateAt.END);
  2239. }
  2240. botButton.title = new StaticLayout(buttonText, Theme.chat_botButtonPaint, buttonWidth - AndroidUtilities.dp(10), Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
  2241. botButtons.add(botButton);
  2242. if (b == row.buttons.size() - 1) {
  2243. maxButtonsWidth = Math.max(maxButtonsWidth, botButton.x + botButton.width);
  2244. }
  2245. }
  2246. }
  2247. widthForButtons = maxButtonsWidth;
  2248. } else {
  2249. substractBackgroundHeight = 0;
  2250. keyboardHeight = 0;
  2251. }
  2252. if (drawPinnedBottom && drawPinnedTop) {
  2253. totalHeight -= AndroidUtilities.dp(2);
  2254. } else if (drawPinnedBottom) {
  2255. totalHeight -= AndroidUtilities.dp(1);
  2256. } else if (drawPinnedTop && pinnedBottom && currentPosition != null && currentPosition.siblingHeights == null) {
  2257. totalHeight -= AndroidUtilities.dp(1);
  2258. }
  2259. if (messageObject.isAnyKindOfSticker() && totalHeight < AndroidUtilities.dp(70)) {
  2260. totalHeight = AndroidUtilities.dp(70);
  2261. }
  2262. if (!drawPhotoImage) {
  2263. photoImage.setImageBitmap((Drawable) null);
  2264. }
  2265. if (documentAttachType == DOCUMENT_ATTACH_TYPE_MUSIC) {
  2266. if (MessageObject.isDocumentHasThumb(documentAttach)) {
  2267. TLRPC.PhotoSize thumb = FileLoader.getClosestPhotoSizeWithSize(documentAttach.thumbs, 90);
  2268. radialProgress.setImageOverlay(thumb, documentAttach, messageObject);
  2269. } else {
  2270. String artworkUrl = messageObject.getArtworkUrl(true);
  2271. if (!TextUtils.isEmpty(artworkUrl)) {
  2272. radialProgress.setImageOverlay(artworkUrl);
  2273. } else {
  2274. radialProgress.setImageOverlay(null, null, null);
  2275. }
  2276. }
  2277. } else {
  2278. radialProgress.setImageOverlay(null, null, null);
  2279. }
  2280. }
  2281. updateWaveform();
  2282. updateButtonState(false, dataChanged && !messageObject.cancelEditing, true);
  2283.  
  2284. if (buttonState == 2 && documentAttachType == DOCUMENT_ATTACH_TYPE_AUDIO && DownloadController.getInstance(currentAccount).canDownloadMedia(messageObject)) {
  2285. FileLoader.getInstance(currentAccount).loadFile(documentAttach, currentMessageObject, 1, 0);
  2286. buttonState = 4;
  2287. radialProgress.setIcon(getIconForCurrentState(), false, false);
  2288. }
  2289.  
  2290. accessibilityVirtualViewBounds.clear();
  2291. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement