Skip to content

Commit a20f1e6

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

4 files changed

Lines changed: 238 additions & 0 deletions

File tree

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

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.telegram.tgnet.tl.TL_account;
4545
import org.telegram.tgnet.tl.TL_bots;
4646
import org.telegram.tgnet.tl.TL_communities;
47+
import org.telegram.tgnet.tl.TL_iv;
4748
import org.telegram.tgnet.tl.TL_stories;
4849
import org.telegram.tgnet.tl.TL_update;
4950
import org.telegram.ui.ActionBar.Theme;
@@ -4388,6 +4389,79 @@ private boolean addFilesToDelete(TLRPC.Message message, ArrayList<File> filesToD
43884389
return false;
43894390
}
43904391

4392+
public void clearMessageMediaCache(ArrayList<TLRPC.Message> messages) {
4393+
if (messages == null || messages.isEmpty()) {
4394+
return;
4395+
}
4396+
storageQueue.postRunnable(() -> {
4397+
ArrayList<File> filesToDelete = new ArrayList<>();
4398+
ArrayList<String> namesToDelete = new ArrayList<>();
4399+
ArrayList<Pair<Long, Integer>> idsToDelete = new ArrayList<>();
4400+
try {
4401+
for (int a = 0, N = messages.size(); a < N; a++) {
4402+
TLRPC.Message message = messages.get(a);
4403+
if (message != null) {
4404+
addFilesToDelete(message, filesToDelete, idsToDelete, namesToDelete, false);
4405+
if (message.rich_message != null) {
4406+
addRichMessageFilesToDelete(message.rich_message, filesToDelete, idsToDelete, namesToDelete, false);
4407+
}
4408+
}
4409+
}
4410+
} catch (Exception e) {
4411+
FileLog.e(e);
4412+
}
4413+
deleteFromDownloadQueue(idsToDelete, true);
4414+
AndroidUtilities.runOnUIThread(() -> getFileLoader().cancelLoadFiles(namesToDelete));
4415+
getFileLoader().deleteFiles(filesToDelete, 0);
4416+
});
4417+
}
4418+
4419+
private void addRichMessageFilesToDelete(TL_iv.RichMessage richMessage, ArrayList<File> filesToDelete, ArrayList<Pair<Long, Integer>> ids, ArrayList<String> namesToDelete, boolean forceCache) {
4420+
if (richMessage == null) {
4421+
return;
4422+
}
4423+
for (int p = 0, NP = richMessage.photos != null ? richMessage.photos.size() : 0; p < NP; p++) {
4424+
TLRPC.Photo photo = richMessage.photos.get(p);
4425+
if (photo == null) {
4426+
continue;
4427+
}
4428+
ids.add(new Pair<>(photo.id, DownloadController.AUTODOWNLOAD_TYPE_PHOTO));
4429+
for (int s = 0, NS = photo.sizes.size(); s < NS; s++) {
4430+
TLRPC.PhotoSize photoSize = photo.sizes.get(s);
4431+
String name = FileLoader.getAttachFileName(photoSize);
4432+
if (!TextUtils.isEmpty(name)) {
4433+
namesToDelete.add(name);
4434+
}
4435+
File file = getFileLoader().getPathToAttach(photoSize, forceCache);
4436+
if (file.toString().length() > 0) {
4437+
filesToDelete.add(file);
4438+
}
4439+
}
4440+
}
4441+
for (int d = 0, ND = richMessage.documents != null ? richMessage.documents.size() : 0; d < ND; d++) {
4442+
TLRPC.Document document = richMessage.documents.get(d);
4443+
if (document == null) {
4444+
continue;
4445+
}
4446+
ids.add(new Pair<>(document.id, DownloadController.AUTODOWNLOAD_TYPE_DOCUMENT));
4447+
String name = FileLoader.getAttachFileName(document);
4448+
if (!TextUtils.isEmpty(name)) {
4449+
namesToDelete.add(name);
4450+
}
4451+
File file = getFileLoader().getPathToAttach(document, forceCache);
4452+
if (file.toString().length() > 0) {
4453+
filesToDelete.add(file);
4454+
}
4455+
for (int s = 0, NS = document.thumbs != null ? document.thumbs.size() : 0; s < NS; s++) {
4456+
TLRPC.PhotoSize photoSize = document.thumbs.get(s);
4457+
File thumbFile = getFileLoader().getPathToAttach(photoSize);
4458+
if (thumbFile.toString().length() > 0) {
4459+
filesToDelete.add(thumbFile);
4460+
}
4461+
}
4462+
}
4463+
}
4464+
43914465
public void deleteDialog(long did, int messagesOnly) {
43924466
storageQueue.postRunnable(() -> {
43934467
SQLiteCursor cursor = null;

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

Lines changed: 149 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,108 @@ 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+
TL_iv.RichMessage richMessage = messageObject.messageOwner.rich_message;
33115+
if (richMessage != null) {
33116+
invalidateRichMessageMemCache(imageLoader, richMessage);
33117+
}
33118+
messageObject.mediaExists = false;
33119+
messageObject.attachPathExists = false;
33120+
messageObject.forceUpdate = true;
33121+
chatAdapter.updateRowWithMessageObject(messageObject, true, false);
33122+
messageObject.forceUpdate = false;
33123+
}
33124+
}
33125+
33126+
private void invalidateRichMessageMemCache(ImageLoader imageLoader, TL_iv.RichMessage richMessage) {
33127+
if (richMessage == null) {
33128+
return;
33129+
}
33130+
for (int p = 0, NP = richMessage.photos != null ? richMessage.photos.size() : 0; p < NP; p++) {
33131+
TLRPC.Photo photo = richMessage.photos.get(p);
33132+
if (photo == null) {
33133+
continue;
33134+
}
33135+
for (int s = 0, NS = photo.sizes.size(); s < NS; s++) {
33136+
TLRPC.PhotoSize size = photo.sizes.get(s);
33137+
if (size instanceof TLRPC.TL_photoStrippedSize || size instanceof TLRPC.TL_photoPathSize) {
33138+
continue;
33139+
}
33140+
invalidateMediaMemCacheKey(imageLoader, ImageLocation.getForPhoto(size, photo));
33141+
}
33142+
}
33143+
for (int d = 0, ND = richMessage.documents != null ? richMessage.documents.size() : 0; d < ND; d++) {
33144+
TLRPC.Document document = richMessage.documents.get(d);
33145+
if (document == null) {
33146+
continue;
33147+
}
33148+
invalidateMediaMemCacheKey(imageLoader, ImageLocation.getForDocument(document));
33149+
for (int s = 0, NS = document.thumbs != null ? document.thumbs.size() : 0; s < NS; s++) {
33150+
TLRPC.PhotoSize size = document.thumbs.get(s);
33151+
if (size instanceof TLRPC.TL_photoStrippedSize || size instanceof TLRPC.TL_photoPathSize) {
33152+
continue;
33153+
}
33154+
invalidateMediaMemCacheKey(imageLoader, ImageLocation.getForDocument(size, document));
33155+
}
33156+
for (int s = 0, NS = document.video_thumbs != null ? document.video_thumbs.size() : 0; s < NS; s++) {
33157+
invalidateMediaMemCacheKey(imageLoader, ImageLocation.getForDocument(document.video_thumbs.get(s), document));
33158+
}
33159+
}
33160+
}
33161+
33162+
private void invalidateMediaMemCache(ImageLoader imageLoader, TLRPC.MessageMedia media) {
33163+
if (media.photo != null) {
33164+
for (int s = 0, NS = media.photo.sizes.size(); s < NS; s++) {
33165+
TLRPC.PhotoSize size = media.photo.sizes.get(s);
33166+
if (size instanceof TLRPC.TL_photoStrippedSize || size instanceof TLRPC.TL_photoPathSize) {
33167+
continue;
33168+
}
33169+
ImageLocation location = ImageLocation.getForPhoto(size, media.photo);
33170+
invalidateMediaMemCacheKey(imageLoader, location);
33171+
}
33172+
}
33173+
if (media.document != null) {
33174+
ImageLocation location = ImageLocation.getForDocument(media.document);
33175+
invalidateMediaMemCacheKey(imageLoader, location);
33176+
for (int s = 0, NS = media.document.thumbs != null ? media.document.thumbs.size() : 0; s < NS; s++) {
33177+
TLRPC.PhotoSize size = media.document.thumbs.get(s);
33178+
if (size instanceof TLRPC.TL_photoStrippedSize || size instanceof TLRPC.TL_photoPathSize) {
33179+
continue;
33180+
}
33181+
invalidateMediaMemCacheKey(imageLoader, ImageLocation.getForDocument(size, media.document));
33182+
}
33183+
for (int s = 0, NS = media.document.video_thumbs != null ? media.document.video_thumbs.size() : 0; s < NS; s++) {
33184+
invalidateMediaMemCacheKey(imageLoader, ImageLocation.getForDocument(media.document.video_thumbs.get(s), media.document));
33185+
}
33186+
}
33187+
}
33188+
33189+
private void invalidateMediaMemCacheKey(ImageLoader imageLoader, ImageLocation location) {
33190+
if (location == null) {
33191+
return;
33192+
}
33193+
String baseKey = location.getKey(null, null, false);
33194+
if (baseKey == null) {
33195+
return;
33196+
}
33197+
imageLoader.replaceImageInCache(baseKey, baseKey + "_ccinv_" + (clearCacheInvalidationCounter++), null, false);
33198+
}
33199+
3309733200
private void processSelectedOption(int option) {
3309833201
if (selectedObject == null || getParentActivity() == null) {
3309933202
return;
@@ -33136,6 +33239,31 @@ private void processSelectedOption(int option) {
3313633239
createDeleteMessagesAlert(selectedObject, selectedObjectGroup, true);
3313733240
break;
3313833241
}
33242+
case OPTION_CLEAR_CACHE: {
33243+
MessageObject obj = selectedObject;
33244+
MessageObject.GroupedMessages group = selectedObjectGroup;
33245+
ArrayList<TLRPC.Message> rawMessages = new ArrayList<>();
33246+
ArrayList<MessageObject> messageObjects = new ArrayList<>();
33247+
if (group != null) {
33248+
for (int a = 0, N = group.messages.size(); a < N; a++) {
33249+
MessageObject m = group.messages.get(a);
33250+
if (m != null && m.messageOwner != null) {
33251+
rawMessages.add(m.messageOwner);
33252+
messageObjects.add(m);
33253+
}
33254+
}
33255+
}
33256+
if (obj != null && obj.messageOwner != null) {
33257+
rawMessages.add(obj.messageOwner);
33258+
boolean alreadyAdded = group != null && group.messages.contains(obj);
33259+
if (!alreadyAdded) {
33260+
messageObjects.add(obj);
33261+
}
33262+
}
33263+
getMessagesStorage().clearMessageMediaCache(rawMessages);
33264+
invalidateMessageMediaInMemory(messageObjects);
33265+
break;
33266+
}
3313933267
case OPTION_FORWARD: {
3314033268
if (getMessagesController().isFrozen()) {
3314133269
AccountFrozenAlert.show(currentAccount);
@@ -45419,6 +45547,12 @@ public void fillMessageMenu(
4541945547
deleteIconRes = R.drawable.msg_delete;
4542045548
}
4542145549

45550+
final TL_iv.RichMessage richMessage = selectedObject.messageOwner != null ? selectedObject.messageOwner.rich_message : null;
45551+
final boolean hasCacheableMedia = (MessageObject.getDocument(selectedObject.messageOwner) != null || MessageObject.getPhoto(selectedObject.messageOwner) != null
45552+
|| (richMessage != null && (!richMessage.documents.isEmpty() || !richMessage.photos.isEmpty())))
45553+
&& selectedObject.getId() > 0 && !selectedObject.isSponsored() && !selectedObject.isExpiredStory() && !selectedObject.isSticker() && !selectedObject.isAnimatedSticker()
45554+
&& ApplicationLoader.applicationContext.getSharedPreferences("mainconfig", Activity.MODE_PRIVATE).getBoolean("displayAdditionalMessageOptions", false);
45555+
4542245556
if (type == -1) {
4542345557
if ((selectedObject.type == MessageObject.TYPE_TEXT || selectedObject.type == MessageObject.TYPE_ARTICLE || selectedObject.isAnimatedEmoji() || selectedObject.isAnimatedEmojiStickers() || getMessageCaption(selectedObject, selectedObjectGroup) != null) && (!noforwardsOrPaidMedia || isEphemeral) && !message.isExpiredStory()) {
4542445558
items.add(LocaleController.getString(R.string.Copy));
@@ -45504,6 +45638,11 @@ public void fillMessageMenu(
4550445638
options.add(OPTION_DELETE);
4550545639
icons.add(deleteIconRes);
4550645640
}
45641+
if (hasCacheableMedia) {
45642+
items.add(LocaleController.getString(R.string.ClearCache));
45643+
options.add(OPTION_CLEAR_CACHE);
45644+
icons.add(R.drawable.msg_clearcache);
45645+
}
4550745646
} else if (type == 20) {
4550845647
items.add(LocaleController.getString(R.string.Retry));
4550945648
options.add(OPTION_RETRY);
@@ -45828,6 +45967,11 @@ public void fillMessageMenu(
4582845967
options.add(OPTION_DELETE);
4582945968
icons.add(deleteIconRes);
4583045969
}
45970+
if (hasCacheableMedia) {
45971+
items.add(LocaleController.getString(R.string.ClearCache));
45972+
options.add(OPTION_CLEAR_CACHE);
45973+
icons.add(R.drawable.msg_clearcache);
45974+
}
4583145975
} else {
4583245976
if ((allowChatActions || isEphemeralFromBot) && !isInsideContainer) {
4583345977
items.add(LocaleController.getString(R.string.Reply));
@@ -45917,6 +46061,11 @@ public void fillMessageMenu(
4591746061
items.add(LocaleController.getString(chatMode == MODE_SAVED && threadMessageId != getUserConfig().getClientUserId() ? R.string.Remove : R.string.Delete));
4591846062
options.add(OPTION_DELETE);
4591946063
icons.add(deleteIconRes);
46064+
if (hasCacheableMedia) {
46065+
items.add(LocaleController.getString(R.string.ClearCache));
46066+
options.add(OPTION_CLEAR_CACHE);
46067+
icons.add(R.drawable.msg_clearcache);
46068+
}
4592046069
}
4592146070
}
4592246071
}

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)