Skip to content

Commit 22a0c2a

Browse files
author
DrKLO
committed
Fixes (unstable, don't upload to markets)
1 parent c6b1a34 commit 22a0c2a

38 files changed

+753
-146
lines changed

TMessagesProj/jni/image.c

Lines changed: 99 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ static inline uint64_t get_colors (const uint8_t *p) {
99
return p[0] + (p[1] << 16) + ((uint64_t)p[2] << 32);
1010
}
1111

12-
static void fastBlur(int imageWidth, int imageHeight, int imageStride, void *pixels) {
12+
static void fastBlurMore(int imageWidth, int imageHeight, int imageStride, void *pixels, int radius) {
1313
uint8_t *pix = (uint8_t *)pixels;
1414
const int w = imageWidth;
1515
const int h = imageHeight;
1616
const int stride = imageStride;
17-
const int radius = 3;
1817
const int r1 = radius + 1;
1918
const int div = radius * 2 + 1;
2019

@@ -23,6 +22,98 @@ static void fastBlur(int imageWidth, int imageHeight, int imageStride, void *pix
2322
}
2423

2524
uint64_t *rgb = malloc(imageWidth * imageHeight * sizeof(uint64_t));
25+
if (rgb == NULL) {
26+
return;
27+
}
28+
29+
int x, y, i;
30+
31+
int yw = 0;
32+
const int we = w - r1;
33+
for (y = 0; y < h; y++) {
34+
uint64_t cur = get_colors (&pix[yw]);
35+
uint64_t rgballsum = -radius * cur;
36+
uint64_t rgbsum = cur * ((r1 * (r1 + 1)) >> 1);
37+
38+
for (i = 1; i <= radius; i++) {
39+
uint64_t cur = get_colors (&pix[yw + i * 4]);
40+
rgbsum += cur * (r1 - i);
41+
rgballsum += cur;
42+
}
43+
44+
x = 0;
45+
46+
#define update(start, middle, end) \
47+
rgb[y * w + x] = (rgbsum >> 6) & 0x00FF00FF00FF00FF; \
48+
rgballsum += get_colors (&pix[yw + (start) * 4]) - 2 * get_colors (&pix[yw + (middle) * 4]) + get_colors (&pix[yw + (end) * 4]); \
49+
rgbsum += rgballsum; \
50+
x++; \
51+
52+
while (x < r1) {
53+
update (0, x, x + r1);
54+
}
55+
while (x < we) {
56+
update (x - r1, x, x + r1);
57+
}
58+
while (x < w) {
59+
update (x - r1, x, w - 1);
60+
}
61+
#undef update
62+
63+
yw += stride;
64+
}
65+
66+
const int he = h - r1;
67+
for (x = 0; x < w; x++) {
68+
uint64_t rgballsum = -radius * rgb[x];
69+
uint64_t rgbsum = rgb[x] * ((r1 * (r1 + 1)) >> 1);
70+
for (i = 1; i <= radius; i++) {
71+
rgbsum += rgb[i * w + x] * (r1 - i);
72+
rgballsum += rgb[i * w + x];
73+
}
74+
75+
y = 0;
76+
int yi = x * 4;
77+
78+
#define update(start, middle, end) \
79+
int64_t res = rgbsum >> 6; \
80+
pix[yi] = res; \
81+
pix[yi + 1] = res >> 16; \
82+
pix[yi + 2] = res >> 32; \
83+
rgballsum += rgb[x + (start) * w] - 2 * rgb[x + (middle) * w] + rgb[x + (end) * w]; \
84+
rgbsum += rgballsum; \
85+
y++; \
86+
yi += stride;
87+
88+
while (y < r1) {
89+
update (0, y, y + r1);
90+
}
91+
while (y < he) {
92+
update (y - r1, y, y + r1);
93+
}
94+
while (y < h) {
95+
update (y - r1, y, h - 1);
96+
}
97+
#undef update
98+
}
99+
}
100+
101+
static void fastBlur(int imageWidth, int imageHeight, int imageStride, void *pixels, int radius) {
102+
uint8_t *pix = (uint8_t *)pixels;
103+
const int w = imageWidth;
104+
const int h = imageHeight;
105+
const int stride = imageStride;
106+
const int r1 = radius + 1;
107+
const int div = radius * 2 + 1;
108+
109+
if (radius > 15 || div >= w || div >= h || w * h > 90 * 90 || imageStride > imageWidth * 4) {
110+
return;
111+
}
112+
113+
uint64_t *rgb = malloc(imageWidth * imageHeight * sizeof(uint64_t));
114+
if (rgb == NULL) {
115+
return;
116+
}
26117

27118
int x, y, i;
28119

@@ -111,7 +202,7 @@ METHODDEF(void) my_error_exit(j_common_ptr cinfo) {
111202
longjmp(myerr->setjmp_buffer, 1);
112203
}
113204

114-
JNIEXPORT void Java_org_telegram_messenger_Utilities_blurBitmap(JNIEnv *env, jclass class, jobject bitmap) {
205+
JNIEXPORT void Java_org_telegram_messenger_Utilities_blurBitmap(JNIEnv *env, jclass class, jobject bitmap, int radius) {
115206
if (!bitmap) {
116207
return;
117208
}
@@ -130,7 +221,11 @@ JNIEXPORT void Java_org_telegram_messenger_Utilities_blurBitmap(JNIEnv *env, jcl
130221
if (AndroidBitmap_lockPixels(env, bitmap, &pixels) < 0) {
131222
return;
132223
}
133-
fastBlur(info.width, info.height, info.stride, pixels);
224+
if (radius <= 3) {
225+
fastBlur(info.width, info.height, info.stride, pixels, radius);
226+
} else {
227+
fastBlurMore(info.width, info.height, info.stride, pixels, radius);
228+
}
134229
AndroidBitmap_unlockPixels(env, bitmap);
135230
}
136231

Binary file not shown.
4 KB
Binary file not shown.
4 KB
Binary file not shown.

TMessagesProj/src/main/java/org/telegram/android/AndroidUtilities.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,10 @@ public static void RunOnUIThread(Runnable runnable, long delay) {
274274
}
275275
}
276276

277+
public static void CancelRunOnUIThread(Runnable runnable) {
278+
ApplicationLoader.applicationHandler.removeCallbacks(runnable);
279+
}
280+
277281
public static boolean isTablet() {
278282
if (isTablet == null) {
279283
isTablet = ApplicationLoader.applicationContext.getResources().getBoolean(R.bool.isTablet);

TMessagesProj/src/main/java/org/telegram/android/ImageLoader.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.telegram.messenger.DispatchQueue;
2929
import org.telegram.messenger.FileLoader;
3030
import org.telegram.messenger.FileLog;
31-
import org.telegram.messenger.R;
3231
import org.telegram.messenger.TLRPC;
3332
import org.telegram.messenger.UserConfig;
3433
import org.telegram.messenger.Utilities;
@@ -293,7 +292,7 @@ public void run() {
293292
}
294293
}
295294
if (image != null && blur && bitmapH < 100 && bitmapW < 100) {
296-
Utilities.blurBitmap(image);
295+
Utilities.blurBitmap(image, 3);
297296
}
298297
}
299298
if (runtimeHack != null) {

TMessagesProj/src/main/java/org/telegram/android/ImageReceiver.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class ImageReceiver {
3535
private boolean isVisible = true;
3636
private boolean isAspectFit = false;
3737
private boolean lastCacheOnly = false;
38+
private boolean forcePreview = false;
3839

3940
public ImageReceiver() {
4041

@@ -195,7 +196,7 @@ private void recycleBitmap(BitmapDrawable newBitmap) {
195196
public boolean draw(Canvas canvas, int x, int y, int w, int h) {
196197
try {
197198
Drawable bitmapDrawable = currentImage;
198-
if (bitmapDrawable == null && last_placeholder != null && last_placeholder instanceof BitmapDrawable) {
199+
if (forcePreview || bitmapDrawable == null && last_placeholder != null && last_placeholder instanceof BitmapDrawable) {
199200
bitmapDrawable = last_placeholder;
200201
}
201202
if (bitmapDrawable != null) {
@@ -371,4 +372,8 @@ public String getFilter() {
371372
public String getKey() {
372373
return currentPath;
373374
}
375+
376+
public void setForcePreview(boolean value) {
377+
forcePreview = value;
378+
}
374379
}

TMessagesProj/src/main/java/org/telegram/android/MessageObject.java

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import android.text.TextPaint;
1717
import android.text.util.Linkify;
1818

19+
import org.telegram.messenger.ConnectionsManager;
1920
import org.telegram.messenger.FileLoader;
2021
import org.telegram.messenger.FileLog;
2122
import org.telegram.messenger.TLRPC;
@@ -340,7 +341,7 @@ public void generateThumbs(boolean update, int preview) {
340341
if (!update) {
341342
photoThumbs = new ArrayList<PhotoObject>();
342343
for (TLRPC.PhotoSize size : messageOwner.action.photo.sizes) {
343-
photoThumbs.add(new PhotoObject(size, preview));
344+
photoThumbs.add(new PhotoObject(size, preview, isSecretMedia()));
344345
}
345346
} else if (photoThumbs != null && !photoThumbs.isEmpty()) {
346347
for (PhotoObject photoObject : photoThumbs) {
@@ -361,7 +362,7 @@ public void generateThumbs(boolean update, int preview) {
361362
if (!update) {
362363
photoThumbs = new ArrayList<PhotoObject>();
363364
for (TLRPC.PhotoSize size : messageOwner.media.photo.sizes) {
364-
PhotoObject obj = new PhotoObject(size, preview);
365+
PhotoObject obj = new PhotoObject(size, preview, isSecretMedia());
365366
photoThumbs.add(obj);
366367
if (imagePreview == null && obj.image != null) {
367368
imagePreview = obj.image;
@@ -383,7 +384,7 @@ public void generateThumbs(boolean update, int preview) {
383384
} else if (messageOwner.media instanceof TLRPC.TL_messageMediaVideo) {
384385
if (!update) {
385386
photoThumbs = new ArrayList<PhotoObject>();
386-
PhotoObject obj = new PhotoObject(messageOwner.media.video.thumb, preview);
387+
PhotoObject obj = new PhotoObject(messageOwner.media.video.thumb, preview, isSecretMedia());
387388
photoThumbs.add(obj);
388389
if (imagePreview == null && obj.image != null) {
389390
imagePreview = obj.image;
@@ -396,7 +397,7 @@ public void generateThumbs(boolean update, int preview) {
396397
if (!(messageOwner.media.document.thumb instanceof TLRPC.TL_photoSizeEmpty)) {
397398
if (!update) {
398399
photoThumbs = new ArrayList<PhotoObject>();
399-
PhotoObject obj = new PhotoObject(messageOwner.media.document.thumb, preview);
400+
PhotoObject obj = new PhotoObject(messageOwner.media.document.thumb, preview, isSecretMedia());
400401
photoThumbs.add(obj);
401402
} else if (photoThumbs != null && !photoThumbs.isEmpty() && messageOwner.media.document.thumb != null) {
402403
PhotoObject photoObject = photoThumbs.get(0);
@@ -599,15 +600,39 @@ private void generateLayout() {
599600
}
600601

601602
public boolean isOut() {
602-
return messageOwner.out;
603+
return (messageOwner.flags & TLRPC.MESSAGE_FLAG_OUT) != 0;
603604
}
604605

605606
public boolean isFromMe() {
606607
return messageOwner.from_id == UserConfig.getClientUserId();
607608
}
608609

609-
public boolean isUnread () {
610-
return messageOwner.unread;
610+
public boolean isUnread() {
611+
return (messageOwner.flags & TLRPC.MESSAGE_FLAG_UNREAD) != 0;
612+
}
613+
614+
public void setIsRead() {
615+
messageOwner.flags &=~ TLRPC.MESSAGE_FLAG_UNREAD;
616+
}
617+
618+
public boolean isSecretMedia() {
619+
return messageOwner.media instanceof TLRPC.TL_messageMediaPhoto && messageOwner.ttl != 0;
620+
}
621+
622+
public static void setIsUnread(TLRPC.Message message, boolean unread) {
623+
if (unread) {
624+
message.flags |= TLRPC.MESSAGE_FLAG_UNREAD;
625+
} else {
626+
message.flags &=~ TLRPC.MESSAGE_FLAG_UNREAD;
627+
}
628+
}
629+
630+
public static boolean isUnread(TLRPC.Message message) {
631+
return (message.flags & TLRPC.MESSAGE_FLAG_UNREAD) != 0;
632+
}
633+
634+
public static boolean isOut(TLRPC.Message message) {
635+
return (message.flags & TLRPC.MESSAGE_FLAG_OUT) != 0;
611636
}
612637

613638
public long getDialogId() {
@@ -635,4 +660,25 @@ public boolean isSendError() {
635660
public boolean isSent() {
636661
return messageOwner.send_state == MESSAGE_SEND_STATE_SENT;
637662
}
663+
664+
public String getSecretTimeString() {
665+
if (!isSecretMedia()) {
666+
return null;
667+
}
668+
int secondsLeft = messageOwner.ttl;
669+
if (messageOwner.destroyTime != 0) {
670+
secondsLeft = Math.max(0, messageOwner.destroyTime - ConnectionsManager.getInstance().getCurrentTime());
671+
}
672+
String str;
673+
if (secondsLeft < 60) {
674+
str = secondsLeft + "s";
675+
} else if (secondsLeft < 60 * 60) {
676+
str = secondsLeft / 60 + "m";
677+
} else if (secondsLeft < 60 * 60 * 24) {
678+
str = secondsLeft / 60 / 60 + "h";
679+
} else {
680+
str = secondsLeft / 60 / 60 / 24 + "d";
681+
}
682+
return str;
683+
}
638684
}

0 commit comments

Comments
 (0)