Skip to content

Commit 01cc0a0

Browse files
committed
Add Clear cache message menu option
1 parent fecf883 commit 01cc0a0

6 files changed

Lines changed: 150 additions & 2 deletions

File tree

TMessagesProj/src/main/java/org/telegram/messenger/DownloadController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ public int canDownloadMediaType(MessageObject messageObject, long overrideSize)
686686
}
687687

688688
private int canDownloadMediaInternal(MessageObject message) {
689-
if (message == null || message.messageOwner == null) return 0;
689+
if (message == null || message.messageOwner == null || message.suppressAutoDownload) return 0;
690690
if (message.messageOwner.media instanceof TLRPC.TL_messageMediaStory) {
691691
return canPreloadStories() ? 2 : 0;
692692
}
@@ -776,7 +776,7 @@ private int canDownloadMediaInternal(MessageObject message) {
776776
}
777777

778778
private int canDownloadMediaInternal(MessageObject message, long overrideSize) {
779-
if (message == null || message.messageOwner == null) return 0;
779+
if (message == null || message.messageOwner == null || message.suppressAutoDownload) return 0;
780780
if (message.messageOwner.media instanceof TLRPC.TL_messageMediaStory) {
781781
return canPreloadStories() ? 2 : 0;
782782
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ public class MessageObject {
212212
public boolean isOauthPush;
213213
public boolean putInDownloadsStore;
214214
public boolean isDownloadingFile;
215+
public boolean suppressAutoDownload;
215216
public boolean forcePlayEffect;
216217
private int isRoundVideoCached;
217218
public long eventId;

TMessagesProj/src/main/java/org/telegram/messenger/MessagesStorage.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4388,6 +4388,30 @@ private boolean addFilesToDelete(TLRPC.Message message, ArrayList<File> filesToD
43884388
return false;
43894389
}
43904390

4391+
public void clearMessageMediaCache(ArrayList<TLRPC.Message> messages) {
4392+
if (messages == null || messages.isEmpty()) {
4393+
return;
4394+
}
4395+
storageQueue.postRunnable(() -> {
4396+
ArrayList<File> filesToDelete = new ArrayList<>();
4397+
ArrayList<String> namesToDelete = new ArrayList<>();
4398+
ArrayList<Pair<Long, Integer>> idsToDelete = new ArrayList<>();
4399+
try {
4400+
for (int a = 0, N = messages.size(); a < N; a++) {
4401+
TLRPC.Message message = messages.get(a);
4402+
if (message != null) {
4403+
addFilesToDelete(message, filesToDelete, idsToDelete, namesToDelete, false);
4404+
}
4405+
}
4406+
} catch (Exception e) {
4407+
FileLog.e(e);
4408+
}
4409+
deleteFromDownloadQueue(idsToDelete, true);
4410+
AndroidUtilities.runOnUIThread(() -> getFileLoader().cancelLoadFiles(namesToDelete));
4411+
getFileLoader().deleteFiles(filesToDelete, 0);
4412+
});
4413+
}
4414+
43914415
public void deleteDialog(long did, int messagesOnly) {
43924416
storageQueue.postRunnable(() -> {
43934417
SQLiteCursor cursor = null;

TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,6 +1238,7 @@ public void run() {
12381238
public final static int OPTION_SUGGESTION_ADD_OFFER = 114;
12391239

12401240
public final static int OPTION_VIEW_STATISTICS = 115;
1241+
public final static int OPTION_CLEAR_CACHE = 116;
12411242

12421243
private final static int[] allowedNotificationsDuringChatListAnimations = new int[]{
12431244
NotificationCenter.messagesRead,
@@ -33094,6 +33095,69 @@ private void saveMessageToGallery(MessageObject messageObject) {
3309433095
MediaController.saveFile(path, getParentActivity(), messageObject.isVideo() ? 1 : 0, null, null);
3309533096
}
3309633097

33098+
private static int clearCacheInvalidationCounter = 0;
33099+
33100+
private void invalidateMessageMediaInMemory(ArrayList<MessageObject> messageObjects) {
33101+
if (messageObjects == null || messageObjects.isEmpty()) {
33102+
return;
33103+
}
33104+
ImageLoader imageLoader = ImageLoader.getInstance();
33105+
for (int i = 0, N = messageObjects.size(); i < N; i++) {
33106+
MessageObject messageObject = messageObjects.get(i);
33107+
if (messageObject == null || messageObject.messageOwner == null) {
33108+
continue;
33109+
}
33110+
TLRPC.MessageMedia media = messageObject.messageOwner.media;
33111+
if (media != null) {
33112+
invalidateMediaMemCache(imageLoader, media);
33113+
}
33114+
messageObject.mediaExists = false;
33115+
messageObject.attachPathExists = false;
33116+
messageObject.suppressAutoDownload = true;
33117+
messageObject.forceUpdate = true;
33118+
chatAdapter.updateRowWithMessageObject(messageObject, true, false);
33119+
messageObject.forceUpdate = false;
33120+
}
33121+
}
33122+
33123+
private void invalidateMediaMemCache(ImageLoader imageLoader, TLRPC.MessageMedia media) {
33124+
if (media.photo != null) {
33125+
for (int s = 0, NS = media.photo.sizes.size(); s < NS; s++) {
33126+
TLRPC.PhotoSize size = media.photo.sizes.get(s);
33127+
if (size instanceof TLRPC.TL_photoStrippedSize || size instanceof TLRPC.TL_photoPathSize) {
33128+
continue;
33129+
}
33130+
ImageLocation location = ImageLocation.getForPhoto(size, media.photo);
33131+
invalidateMediaMemCacheKey(imageLoader, location);
33132+
}
33133+
}
33134+
if (media.document != null) {
33135+
ImageLocation location = ImageLocation.getForDocument(media.document);
33136+
invalidateMediaMemCacheKey(imageLoader, location);
33137+
for (int s = 0, NS = media.document.thumbs != null ? media.document.thumbs.size() : 0; s < NS; s++) {
33138+
TLRPC.PhotoSize size = media.document.thumbs.get(s);
33139+
if (size instanceof TLRPC.TL_photoStrippedSize || size instanceof TLRPC.TL_photoPathSize) {
33140+
continue;
33141+
}
33142+
invalidateMediaMemCacheKey(imageLoader, ImageLocation.getForDocument(size, media.document));
33143+
}
33144+
for (int s = 0, NS = media.document.video_thumbs != null ? media.document.video_thumbs.size() : 0; s < NS; s++) {
33145+
invalidateMediaMemCacheKey(imageLoader, ImageLocation.getForDocument(media.document.video_thumbs.get(s), media.document));
33146+
}
33147+
}
33148+
}
33149+
33150+
private void invalidateMediaMemCacheKey(ImageLoader imageLoader, ImageLocation location) {
33151+
if (location == null) {
33152+
return;
33153+
}
33154+
String baseKey = location.getKey(null, null, false);
33155+
if (baseKey == null) {
33156+
return;
33157+
}
33158+
imageLoader.replaceImageInCache(baseKey, baseKey + "_ccinv_" + (clearCacheInvalidationCounter++), null, false);
33159+
}
33160+
3309733161
private void processSelectedOption(int option) {
3309833162
if (selectedObject == null || getParentActivity() == null) {
3309933163
return;
@@ -33136,6 +33200,31 @@ private void processSelectedOption(int option) {
3313633200
createDeleteMessagesAlert(selectedObject, selectedObjectGroup, true);
3313733201
break;
3313833202
}
33203+
case OPTION_CLEAR_CACHE: {
33204+
MessageObject obj = selectedObject;
33205+
MessageObject.GroupedMessages group = selectedObjectGroup;
33206+
ArrayList<TLRPC.Message> rawMessages = new ArrayList<>();
33207+
ArrayList<MessageObject> messageObjects = new ArrayList<>();
33208+
if (group != null) {
33209+
for (int a = 0, N = group.messages.size(); a < N; a++) {
33210+
MessageObject m = group.messages.get(a);
33211+
if (m != null && m.messageOwner != null) {
33212+
rawMessages.add(m.messageOwner);
33213+
messageObjects.add(m);
33214+
}
33215+
}
33216+
}
33217+
if (obj != null && obj.messageOwner != null) {
33218+
rawMessages.add(obj.messageOwner);
33219+
boolean alreadyAdded = group != null && group.messages.contains(obj);
33220+
if (!alreadyAdded) {
33221+
messageObjects.add(obj);
33222+
}
33223+
}
33224+
getMessagesStorage().clearMessageMediaCache(rawMessages);
33225+
invalidateMessageMediaInMemory(messageObjects);
33226+
break;
33227+
}
3313933228
case OPTION_FORWARD: {
3314033229
if (getMessagesController().isFrozen()) {
3314133230
AccountFrozenAlert.show(currentAccount);
@@ -45419,6 +45508,10 @@ public void fillMessageMenu(
4541945508
deleteIconRes = R.drawable.msg_delete;
4542045509
}
4542145510

45511+
final boolean hasCacheableMedia = (MessageObject.getDocument(selectedObject.messageOwner) != null || MessageObject.getPhoto(selectedObject.messageOwner) != null)
45512+
&& selectedObject.getId() > 0 && !selectedObject.isOut() && !selectedObject.isSponsored() && !selectedObject.isExpiredStory() && !selectedObject.isSticker() && !selectedObject.isAnimatedSticker()
45513+
&& ApplicationLoader.applicationContext.getSharedPreferences("mainconfig", Activity.MODE_PRIVATE).getBoolean("displayAdditionalMessageOptions", false);
45514+
4542245515
if (type == -1) {
4542345516
if ((selectedObject.type == MessageObject.TYPE_TEXT || selectedObject.type == MessageObject.TYPE_ARTICLE || selectedObject.isAnimatedEmoji() || selectedObject.isAnimatedEmojiStickers() || getMessageCaption(selectedObject, selectedObjectGroup) != null) && (!noforwardsOrPaidMedia || isEphemeral) && !message.isExpiredStory()) {
4542445517
items.add(LocaleController.getString(R.string.Copy));
@@ -45504,6 +45597,11 @@ public void fillMessageMenu(
4550445597
options.add(OPTION_DELETE);
4550545598
icons.add(deleteIconRes);
4550645599
}
45600+
if (hasCacheableMedia) {
45601+
items.add(LocaleController.getString(R.string.ClearCache));
45602+
options.add(OPTION_CLEAR_CACHE);
45603+
icons.add(R.drawable.msg_clearcache);
45604+
}
4550745605
} else if (type == 20) {
4550845606
items.add(LocaleController.getString(R.string.Retry));
4550945607
options.add(OPTION_RETRY);
@@ -45828,6 +45926,11 @@ public void fillMessageMenu(
4582845926
options.add(OPTION_DELETE);
4582945927
icons.add(deleteIconRes);
4583045928
}
45929+
if (hasCacheableMedia) {
45930+
items.add(LocaleController.getString(R.string.ClearCache));
45931+
options.add(OPTION_CLEAR_CACHE);
45932+
icons.add(R.drawable.msg_clearcache);
45933+
}
4583145934
} else {
4583245935
if ((allowChatActions || isEphemeralFromBot) && !isInsideContainer) {
4583345936
items.add(LocaleController.getString(R.string.Reply));
@@ -45917,6 +46020,11 @@ public void fillMessageMenu(
4591746020
items.add(LocaleController.getString(chatMode == MODE_SAVED && threadMessageId != getUserConfig().getClientUserId() ? R.string.Remove : R.string.Delete));
4591846021
options.add(OPTION_DELETE);
4591946022
icons.add(deleteIconRes);
46023+
if (hasCacheableMedia) {
46024+
items.add(LocaleController.getString(R.string.ClearCache));
46025+
options.add(OPTION_CLEAR_CACHE);
46026+
icons.add(R.drawable.msg_clearcache);
46027+
}
4592046028
}
4592146029
}
4592246030
}

TMessagesProj/src/main/java/org/telegram/ui/GeneralSettingsActivity.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ private void fillItems(ArrayList<UItem> items, UniversalAdapter adapter) {
6868
mainConfig.getBoolean("hideTopChatBars", false));
6969
hideTopChatBars.subtext = getString(R.string.HideTopChatBarsInfo);
7070
items.add(hideTopChatBars);
71+
72+
UItem displayAdditionalMessageOptions = UItem.asCheck(3, getString(R.string.DisplayAdditionalMessageOptions)).setChecked(
73+
mainConfig.getBoolean("displayAdditionalMessageOptions", false));
74+
displayAdditionalMessageOptions.subtext = getString(R.string.DisplayAdditionalMessageOptionsInfo);
75+
items.add(displayAdditionalMessageOptions);
7176
}
7277

7378
private void onClick(UItem item, View view, int position, float x, float y) {
@@ -98,6 +103,14 @@ private void onClick(UItem item, View view, int position, float x, float y) {
98103
((TextCheckCell) view).setChecked(newValue);
99104
}
100105
listView.adapter.update(false);
106+
} else if (item.id == 3) {
107+
SharedPreferences prefs = ApplicationLoader.applicationContext.getSharedPreferences("mainconfig", Activity.MODE_PRIVATE);
108+
boolean newValue = !prefs.getBoolean("displayAdditionalMessageOptions", false);
109+
prefs.edit().putBoolean("displayAdditionalMessageOptions", newValue).apply();
110+
if (view instanceof TextCheckCell) {
111+
((TextCheckCell) view).setChecked(newValue);
112+
}
113+
listView.adapter.update(false);
101114
}
102115
}
103116

TMessagesProj/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
<string name="DisplayExtraProfileInformationInfo">Show additional information about the user or chat on the profile page.</string>
1919
<string name="HideTopChatBars">Hide top chat bars</string>
2020
<string name="HideTopChatBarsInfo">Automatically hide the pinned messages and live stream bars at the top of chats.</string>
21+
<string name="DisplayAdditionalMessageOptions">Display additional message options</string>
22+
<string name="DisplayAdditionalMessageOptionsInfo">Show additional options in the message context menu.</string>
2123
<!--signin view-->
2224
<string name="YourPhone">Your Phone</string>
2325
<string name="StartText">Please confirm your country code and enter your phone number.</string>

0 commit comments

Comments
 (0)