Skip to content

Commit 5c171c8

Browse files
committed
Added "extract media from webpage preview" toggle.
1 parent ed8c963 commit 5c171c8

5 files changed

Lines changed: 217 additions & 1 deletion

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.telegram.messenger.forkgram
2+
3+
import org.telegram.messenger.MessageObject
4+
import org.telegram.messenger.SendMessagesHelper
5+
import org.telegram.tgnet.TLRPC
6+
7+
object ExtractMediaFromPreview {
8+
9+
@JvmStatic
10+
fun hasMedia(webPage: TLRPC.WebPage?): Boolean {
11+
return webPage != null && (webPage.photo is TLRPC.TL_photo || webPage.document is TLRPC.TL_document)
12+
}
13+
14+
@JvmStatic
15+
fun isDocument(webPage: TLRPC.WebPage?): Boolean {
16+
return webPage != null && webPage.document is TLRPC.TL_document
17+
}
18+
19+
@JvmStatic
20+
fun send(
21+
currentAccount: Int,
22+
peer: Long,
23+
webPage: TLRPC.WebPage,
24+
caption: String?,
25+
entities: ArrayList<TLRPC.MessageEntity>?,
26+
replyToMsg: MessageObject?,
27+
replyToTopMsg: MessageObject?,
28+
notify: Boolean,
29+
scheduleDate: Int,
30+
scheduleRepeatPeriod: Int,
31+
payStars: Long
32+
): Boolean {
33+
val params: SendMessagesHelper.SendMessageParams = when {
34+
webPage.document is TLRPC.TL_document -> SendMessagesHelper.SendMessageParams.of(
35+
webPage.document as TLRPC.TL_document, null, null, peer,
36+
replyToMsg, replyToTopMsg, caption, entities, null, null,
37+
notify, scheduleDate, scheduleRepeatPeriod, 0, webPage, null, false
38+
)
39+
webPage.photo is TLRPC.TL_photo -> SendMessagesHelper.SendMessageParams.of(
40+
webPage.photo as TLRPC.TL_photo, null, peer,
41+
replyToMsg, replyToTopMsg, caption, entities, null, null,
42+
notify, scheduleDate, scheduleRepeatPeriod, 0, webPage, false
43+
)
44+
else -> return false
45+
}
46+
params.payStars = payStars
47+
SendMessagesHelper.getInstance(currentAccount).sendMessage(params)
48+
return true
49+
}
50+
}

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

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@
186186
import org.telegram.messenger.browser.Browser;
187187
import org.telegram.messenger.camera.CameraView;
188188
import org.telegram.messenger.forkgram.ForkUtils;
189+
import org.telegram.messenger.forkgram.ExtractMediaFromPreview;
189190
import org.telegram.messenger.support.LongSparseIntArray;
190191
import org.telegram.messenger.utils.OnPostDrawView;
191192
import org.telegram.messenger.utils.PhotoUtilities;
@@ -802,6 +803,9 @@ public void reloadPinnedMessages() {
802803
private TLObject pinnedImageLocationObject;
803804
private int linkSearchRequestId;
804805
public TLRPC.WebPage foundWebPage;
806+
public boolean forkExtractMediaActive;
807+
public TLRPC.WebPage forkExtractMediaFrozenWebPage;
808+
private ImageView forkExtractMediaButton;
805809
private ArrayList<CharSequence> foundUrls;
806810
private String pendingLinkSearchString;
807811
private Runnable pendingWebPageTimeoutRunnable;
@@ -8311,6 +8315,8 @@ public void setVisibility(int visibility) {
83118315
} else if (fieldPanelShown == 3) {
83128316
openAnotherForward();
83138317
} else if (fieldPanelShown == 4) {
8318+
forkExtractMediaActive = false;
8319+
forkExtractMediaFrozenWebPage = null;
83148320
foundWebPage = null;
83158321
if (messagePreviewParams != null) {
83168322
messagePreviewParams.updateLink(currentAccount, null, null, replyingMessageObject == threadMessageObject ? null : replyingMessageObject, replyingQuote, editingMessageObject);
@@ -8329,6 +8335,15 @@ public void setVisibility(int visibility) {
83298335
}
83308336
});
83318337

8338+
forkExtractMediaButton = new ImageView(context);
8339+
forkExtractMediaButton.setImageResource(R.drawable.input_extract_media);
8340+
forkExtractMediaButton.setScaleType(ImageView.ScaleType.CENTER);
8341+
forkExtractMediaButton.setColorFilter(new PorterDuffColorFilter(getThemedColor(Theme.key_glass_defaultIcon), PorterDuff.Mode.MULTIPLY));
8342+
forkExtractMediaButton.setBackgroundDrawable(Theme.createSelectorDrawable(getThemedColor(Theme.key_listSelector), 1, AndroidUtilities.dp(19)));
8343+
forkExtractMediaButton.setContentDescription(LocaleController.getString(R.string.ExtractMediaFromPreview));
8344+
forkExtractMediaButton.setVisibility(View.GONE);
8345+
chatActivityEnterTopView.addView(forkExtractMediaButton, LayoutHelper.createFrame(52, 46, Gravity.RIGHT | Gravity.TOP, 0, 0.5f, 52, 0));
8346+
forkExtractMediaButton.setOnClickListener(v -> forkToggleExtractMedia());
83328347
contentView.addView(
83338348
suggestEmojiPanel = new SuggestEmojiView(context, currentAccount, chatActivityEnterView, themeDelegate),
83348349
LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 160, Gravity.LEFT | Gravity.BOTTOM, 7, 0, 7, 0)
@@ -10924,6 +10939,59 @@ public void fallbackFieldPanel() {
1092410939
}
1092510940
}
1092610941

10942+
public TLRPC.WebPage forkExtractMediaEffectiveWebPage() {
10943+
return forkExtractMediaFrozenWebPage != null ? forkExtractMediaFrozenWebPage : foundWebPage;
10944+
}
10945+
10946+
public boolean forkExtractMediaAvailable() {
10947+
return fieldPanelShown == 4 && ExtractMediaFromPreview.hasMedia(forkExtractMediaEffectiveWebPage());
10948+
}
10949+
10950+
public boolean forkIsExtractMediaActive() {
10951+
return forkExtractMediaActive && forkExtractMediaFrozenWebPage != null;
10952+
}
10953+
10954+
private void forkToggleExtractMedia() {
10955+
if (forkExtractMediaActive) {
10956+
forkExtractMediaActive = false;
10957+
forkExtractMediaFrozenWebPage = null;
10958+
if (chatActivityEnterView != null) {
10959+
searchLinks(chatActivityEnterView.getFieldText(), true);
10960+
}
10961+
} else if (!forkExtractMediaAvailable()) {
10962+
forkExtractMediaActive = false;
10963+
forkExtractMediaFrozenWebPage = null;
10964+
} else {
10965+
forkExtractMediaActive = true;
10966+
forkExtractMediaFrozenWebPage = foundWebPage;
10967+
}
10968+
forkUpdateExtractMediaButton();
10969+
}
10970+
10971+
public void forkUpdateExtractMediaButton() {
10972+
if (forkExtractMediaButton == null) {
10973+
return;
10974+
}
10975+
boolean visible = forkExtractMediaAvailable() || forkIsExtractMediaActive();
10976+
forkExtractMediaButton.setVisibility(visible ? View.VISIBLE : View.GONE);
10977+
int color = getThemedColor(forkExtractMediaActive ? Theme.key_chat_messageLinkIn : Theme.key_glass_defaultIcon);
10978+
forkExtractMediaButton.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.MULTIPLY));
10979+
}
10980+
10981+
public void forkResetExtractMediaAfterSend() {
10982+
forkExtractMediaActive = false;
10983+
forkExtractMediaFrozenWebPage = null;
10984+
foundWebPage = null;
10985+
if (messagePreviewParams != null) {
10986+
messagePreviewParams.updateLink(currentAccount, null, "", null, null, null);
10987+
}
10988+
if (chatActivityEnterView != null) {
10989+
chatActivityEnterView.setWebPage(null, true);
10990+
}
10991+
fallbackFieldPanel();
10992+
forkUpdateExtractMediaButton();
10993+
}
10994+
1092710995
private boolean keyboardWasVisible;
1092810996
private void openForwardingPreview(int startTab) {
1092910997
if (messagePreviewParams == null || forwardingPreviewView != null) {
@@ -14051,6 +14119,9 @@ private void editResetMediaManual() {
1405114119
}
1405214120

1405314121
public void checkEditLinkRemoved(final CharSequence charSequence) {
14122+
if (forkExtractMediaFrozenWebPage != null) {
14123+
return;
14124+
}
1405414125
final boolean manual = editingMessageObject != null && editingMessageObject.messageOwner != null && editingMessageObject.messageOwner.media != null && editingMessageObject.messageOwner.media.webpage != null && !(editingMessageObject.messageOwner.media.webpage instanceof TLRPC.TL_webPageEmpty) && editingMessageObject.messageOwner.media.manual;
1405514126
if (messagePreviewParams != null && editingMessageObject != null && (editingMessageObject.type == MessageObject.TYPE_TEXT || editingMessageObject.type == MessageObject.TYPE_EMOJIS) && foundWebPage != null) {
1405614127
if (messagePreviewParams.hasLink(charSequence, foundWebPage.url)) {
@@ -14071,6 +14142,9 @@ public void checkEditLinkRemoved(final CharSequence charSequence) {
1407114142
}
1407214143

1407314144
public void searchLinks(final CharSequence charSequence, final boolean force) {
14145+
if (forkExtractMediaFrozenWebPage != null) {
14146+
return;
14147+
}
1407414148
if (currentEncryptedChat != null && getMessagesController().secretWebpagePreview == 0 || editingMessageObject != null && (!editingMessageObject.isWebpage() || editingMessageObject.messageOwner.media.webpage instanceof TLRPC.TL_webPagePending)) {
1407514149
return;
1407614150
}
@@ -14219,6 +14293,9 @@ public void searchLinks(final CharSequence charSequence, final boolean force) {
1421914293
if (waitingForWebpageId != myId) {
1422014294
return;
1422114295
}
14296+
if (forkExtractMediaFrozenWebPage != null) {
14297+
return;
14298+
}
1422214299
if (success) {
1422314300
foundWebPage = webpage;
1422414301
foundWebPage.display_url = req.message;
@@ -15503,6 +15580,7 @@ public void showFieldPanel(boolean show, MessageObject messageObjectToReply, Mes
1550315580
if (chatActivityEnterView != null) {
1550415581
chatActivityEnterView.updateSendButtonPaid();
1550515582
}
15583+
forkUpdateExtractMediaButton();
1550615584
}
1550715585

1550815586
private void moveScrollToLastMessage(boolean skipSponsored) {
@@ -23354,7 +23432,7 @@ public void didReceivedNotification(int id, int account, final Object... args) {
2335423432
updateVisibleRows();
2335523433
}
2335623434
} else if (id == NotificationCenter.didReceivedWebpagesInUpdates) {
23357-
if (foundWebPage != null) {
23435+
if (foundWebPage != null && forkExtractMediaFrozenWebPage == null) {
2335823436
LongSparseArray<TLRPC.WebPage> hashMap = (LongSparseArray<TLRPC.WebPage>) args[0];
2335923437
for (int a = 0; a < hashMap.size(); a++) {
2336023438
TLRPC.WebPage webPage = hashMap.valueAt(a);

TMessagesProj/src/main/java/org/telegram/ui/Components/ChatActivityEnterView.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
import org.telegram.messenger.LocaleController;
134134
import org.telegram.messenger.MediaController;
135135
import org.telegram.messenger.MediaDataController;
136+
import org.telegram.messenger.forkgram.ExtractMediaFromPreview;
136137
import org.telegram.messenger.MessageObject;
137138
import org.telegram.messenger.MessageSuggestionParams;
138139
import org.telegram.messenger.MessagesController;
@@ -7123,6 +7124,22 @@ protected boolean sendMessageInternal(boolean notify, int scheduleDate, int sche
71237124
if (checkPremiumAnimatedEmoji(currentAccount, dialog_id, parentFragment, null, message)) {
71247125
return;
71257126
}
7127+
if (parentFragment != null && parentFragment.forkIsExtractMediaActive()
7128+
&& ExtractMediaFromPreview.hasMedia(parentFragment.forkExtractMediaFrozenWebPage)) {
7129+
boolean forkSent = forkTrySendExtractedMedia(message, notify, scheduleDate, scheduleRepeatPeriod, payStars);
7130+
if (forkSent) {
7131+
if (messageEditText != null) {
7132+
messageEditText.setText("");
7133+
}
7134+
hideTopView(true);
7135+
if (delegate != null) {
7136+
delegate.onMessageSend(message, notify, scheduleDate, scheduleRepeatPeriod, payStars);
7137+
}
7138+
lastTypingTimeSend = 0;
7139+
}
7140+
updateSendButtonPaid();
7141+
return;
7142+
}
71267143
if (processSendingText(message, notify, scheduleDate, scheduleRepeatPeriod, payStars)) {
71277144
if (delegate.hasForwardingMessages() || (scheduleDate != 0 && !isInScheduleMode()) || isInScheduleMode()) {
71287145
if (messageEditText != null) {
@@ -7669,6 +7686,41 @@ public boolean processSendingText(CharSequence text, boolean notify, int schedul
76697686
return false;
76707687
}
76717688

7689+
private boolean forkTrySendExtractedMedia(CharSequence message, boolean notify, int scheduleDate, int scheduleRepeatPeriod, long payStars) {
7690+
if (parentFragment == null) {
7691+
return false;
7692+
}
7693+
TLRPC.WebPage webPage = parentFragment.forkExtractMediaFrozenWebPage;
7694+
if (!ExtractMediaFromPreview.hasMedia(webPage)) {
7695+
return false;
7696+
}
7697+
boolean isDocument = ExtractMediaFromPreview.isDocument(webPage);
7698+
TLRPC.Chat chat = parentFragment.getCurrentChat();
7699+
if (chat != null) {
7700+
if (isDocument && !ChatObject.canSendDocument(chat)) {
7701+
BulletinFactory.of(parentFragment).createErrorBulletin(ChatObject.getRestrictedErrorText(chat, ChatObject.ACTION_SEND_DOCUMENTS)).show();
7702+
return false;
7703+
}
7704+
if (!isDocument && !ChatObject.canSendPhoto(chat)) {
7705+
BulletinFactory.of(parentFragment).createErrorBulletin(ChatObject.getRestrictedErrorText(chat, ChatObject.ACTION_SEND_PHOTO)).show();
7706+
return false;
7707+
}
7708+
}
7709+
CharSequence trimmed = AndroidUtilities.getTrimmedString(message == null ? "" : message);
7710+
CharSequence[] msg = new CharSequence[]{ trimmed };
7711+
ArrayList<TLRPC.MessageEntity> entities = MediaDataController.getInstance(currentAccount).getEntities(msg, supportsSendingNewEntities());
7712+
String caption = msg[0].toString();
7713+
MessageObject replyToTopMsg = getThreadMessage();
7714+
if (replyToTopMsg == null && replyingTopMessage != null) {
7715+
replyToTopMsg = replyingTopMessage;
7716+
}
7717+
boolean sent = ExtractMediaFromPreview.send(currentAccount, dialog_id, webPage, caption, entities, replyingMessageObject, replyToTopMsg, notify, scheduleDate, scheduleRepeatPeriod, payStars);
7718+
if (sent) {
7719+
parentFragment.forkResetExtractMediaAfterSend();
7720+
}
7721+
return sent;
7722+
}
7723+
76727724
public long getSendMonoForumPeerId() {
76737725
return parentFragment != null ? parentFragment.getSendMonoForumPeerId() : 0;
76747726
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:width="24dp"
4+
android:height="24dp"
5+
android:viewportWidth="24"
6+
android:viewportHeight="24">
7+
8+
<path
9+
android:strokeColor="#FFFFFF"
10+
android:strokeWidth="2"
11+
android:strokeLineJoin="round"
12+
android:strokeLineCap="round"
13+
android:pathData="M5.1,3 H12.9 A2.6,2.6 0 0 1 15.5,5.6 V11.4 A2.6,2.6 0 0 1 12.9,14 H5.1 A2.6,2.6 0 0 1 2.5,11.4 V5.6 A2.6,2.6 0 0 1 5.1,3 Z" />
14+
<path
15+
android:fillColor="#FFFFFF"
16+
android:pathData="M5,6.8 a1.2,1.2 0 1 0 2.4,0 a1.2,1.2 0 1 0 -2.4,0 Z" />
17+
<path
18+
android:strokeColor="#FFFFFF"
19+
android:strokeWidth="2"
20+
android:strokeLineJoin="round"
21+
android:strokeLineCap="round"
22+
android:pathData="M3,12.6 l3.1,-2.9 a1.5,1.5 0 0 1 2,0 l3.4,3.2" />
23+
<path
24+
android:strokeColor="#FFFFFF"
25+
android:strokeWidth="2"
26+
android:strokeLineJoin="round"
27+
android:strokeLineCap="round"
28+
android:pathData="M18.7,12.8 V20.5" />
29+
<path
30+
android:strokeColor="#FFFFFF"
31+
android:strokeWidth="2"
32+
android:strokeLineJoin="round"
33+
android:strokeLineCap="round"
34+
android:pathData="M15.6,17.4 l3.1,3.1 l3.1,-3.1" />
35+
</vector>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
<string name="ThirdParty">Third-party</string>
7474
<string name="EnableLastSeenDots">Enable colored last seen dots</string>
7575
<string name="HideStoriesInArchive">Hide Stories in Archive</string>
76+
<string name="ExtractMediaFromPreview">Send media from link preview as attachment</string>
7677
<string name="AppName">Fork Client</string>
7778
<string name="AppNameBeta">Fork Client</string>
7879
<string name="AppNameFdroid">Forkgram</string>

0 commit comments

Comments
 (0)