Skip to content

Commit f18b779

Browse files
committed
Added "extract media from webpage preview" toggle.
1 parent 22a97a9 commit f18b779

5 files changed

Lines changed: 218 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: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@
187187
import org.telegram.messenger.browser.Browser;
188188
import org.telegram.messenger.camera.CameraView;
189189
import org.telegram.messenger.forkgram.ForkUtils;
190+
import org.telegram.messenger.forkgram.ExtractMediaFromPreview;
190191
import org.telegram.messenger.support.LongSparseIntArray;
191192
import org.telegram.messenger.utils.OnPostDrawView;
192193
import org.telegram.messenger.utils.PhotoUtilities;
@@ -805,6 +806,9 @@ public void reloadPinnedMessages() {
805806
private TLObject pinnedImageLocationObject;
806807
private int linkSearchRequestId;
807808
public TLRPC.WebPage foundWebPage;
809+
public boolean forkExtractMediaActive;
810+
public TLRPC.WebPage forkExtractMediaFrozenWebPage;
811+
private ImageView forkExtractMediaButton;
808812
private ArrayList<CharSequence> foundUrls;
809813
private String pendingLinkSearchString;
810814
private Runnable pendingWebPageTimeoutRunnable;
@@ -8382,6 +8386,8 @@ public void setVisibility(int visibility) {
83828386
} else if (fieldPanelShown == 3) {
83838387
openAnotherForward();
83848388
} else if (fieldPanelShown == 4) {
8389+
forkExtractMediaActive = false;
8390+
forkExtractMediaFrozenWebPage = null;
83858391
foundWebPage = null;
83868392
if (messagePreviewParams != null) {
83878393
messagePreviewParams.updateLink(currentAccount, null, null, replyingMessageObject == threadMessageObject ? null : replyingMessageObject, replyingQuote, editingMessageObject);
@@ -8400,6 +8406,16 @@ public void setVisibility(int visibility) {
84008406
}
84018407
});
84028408

8409+
forkExtractMediaButton = new ImageView(context);
8410+
forkExtractMediaButton.setImageResource(R.drawable.input_extract_media);
8411+
forkExtractMediaButton.setScaleType(ImageView.ScaleType.CENTER);
8412+
forkExtractMediaButton.setColorFilter(new PorterDuffColorFilter(getThemedColor(Theme.key_glass_defaultIcon), PorterDuff.Mode.MULTIPLY));
8413+
forkExtractMediaButton.setBackgroundDrawable(Theme.createSelectorDrawable(getThemedColor(Theme.key_listSelector), 1, AndroidUtilities.dp(19)));
8414+
forkExtractMediaButton.setContentDescription(LocaleController.getString(R.string.ExtractMediaFromPreview));
8415+
forkExtractMediaButton.setVisibility(View.GONE);
8416+
chatActivityEnterTopView.addView(forkExtractMediaButton, LayoutHelper.createFrame(52, 46, Gravity.RIGHT | Gravity.TOP, 0, 0.5f, 52, 0));
8417+
forkExtractMediaButton.setOnClickListener(v -> forkToggleExtractMedia());
8418+
84038419
replyNameTextView = new SimpleTextView(context);
84048420
replyNameTextView.setTextSize(14);
84058421
replyNameTextView.setTextColor(getThemedColor(Theme.key_chat_replyPanelName));
@@ -11029,6 +11045,59 @@ public void fallbackFieldPanel() {
1102911045
}
1103011046
}
1103111047

11048+
public TLRPC.WebPage forkExtractMediaEffectiveWebPage() {
11049+
return forkExtractMediaFrozenWebPage != null ? forkExtractMediaFrozenWebPage : foundWebPage;
11050+
}
11051+
11052+
public boolean forkExtractMediaAvailable() {
11053+
return fieldPanelShown == 4 && ExtractMediaFromPreview.hasMedia(forkExtractMediaEffectiveWebPage());
11054+
}
11055+
11056+
public boolean forkIsExtractMediaActive() {
11057+
return forkExtractMediaActive && forkExtractMediaFrozenWebPage != null;
11058+
}
11059+
11060+
private void forkToggleExtractMedia() {
11061+
if (forkExtractMediaActive) {
11062+
forkExtractMediaActive = false;
11063+
forkExtractMediaFrozenWebPage = null;
11064+
if (chatActivityEnterView != null) {
11065+
searchLinks(chatActivityEnterView.getFieldText(), true);
11066+
}
11067+
} else if (!forkExtractMediaAvailable()) {
11068+
forkExtractMediaActive = false;
11069+
forkExtractMediaFrozenWebPage = null;
11070+
} else {
11071+
forkExtractMediaActive = true;
11072+
forkExtractMediaFrozenWebPage = foundWebPage;
11073+
}
11074+
forkUpdateExtractMediaButton();
11075+
}
11076+
11077+
public void forkUpdateExtractMediaButton() {
11078+
if (forkExtractMediaButton == null) {
11079+
return;
11080+
}
11081+
boolean visible = forkExtractMediaAvailable() || forkIsExtractMediaActive();
11082+
forkExtractMediaButton.setVisibility(visible ? View.VISIBLE : View.GONE);
11083+
int color = getThemedColor(forkExtractMediaActive ? Theme.key_chat_messageLinkIn : Theme.key_glass_defaultIcon);
11084+
forkExtractMediaButton.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.MULTIPLY));
11085+
}
11086+
11087+
public void forkResetExtractMediaAfterSend() {
11088+
forkExtractMediaActive = false;
11089+
forkExtractMediaFrozenWebPage = null;
11090+
foundWebPage = null;
11091+
if (messagePreviewParams != null) {
11092+
messagePreviewParams.updateLink(currentAccount, null, "", null, null, null);
11093+
}
11094+
if (chatActivityEnterView != null) {
11095+
chatActivityEnterView.setWebPage(null, true);
11096+
}
11097+
fallbackFieldPanel();
11098+
forkUpdateExtractMediaButton();
11099+
}
11100+
1103211101
private boolean keyboardWasVisible;
1103311102
private void openForwardingPreview(int startTab) {
1103411103
if (messagePreviewParams == null || forwardingPreviewView != null) {
@@ -14146,6 +14215,9 @@ private void editResetMediaManual() {
1414614215
}
1414714216

1414814217
public void checkEditLinkRemoved(final CharSequence charSequence) {
14218+
if (forkExtractMediaFrozenWebPage != null) {
14219+
return;
14220+
}
1414914221
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;
1415014222
if (messagePreviewParams != null && editingMessageObject != null && (editingMessageObject.type == MessageObject.TYPE_TEXT || editingMessageObject.type == MessageObject.TYPE_EMOJIS) && foundWebPage != null) {
1415114223
if (messagePreviewParams.hasLink(charSequence, foundWebPage.url)) {
@@ -14166,6 +14238,9 @@ public void checkEditLinkRemoved(final CharSequence charSequence) {
1416614238
}
1416714239

1416814240
public void searchLinks(final CharSequence charSequence, final boolean force) {
14241+
if (forkExtractMediaFrozenWebPage != null) {
14242+
return;
14243+
}
1416914244
if (currentEncryptedChat != null && getMessagesController().secretWebpagePreview == 0 || editingMessageObject != null && (!editingMessageObject.isWebpage() || editingMessageObject.messageOwner.media.webpage instanceof TLRPC.TL_webPagePending)) {
1417014245
return;
1417114246
}
@@ -14314,6 +14389,9 @@ public void searchLinks(final CharSequence charSequence, final boolean force) {
1431414389
if (waitingForWebpageId != myId) {
1431514390
return;
1431614391
}
14392+
if (forkExtractMediaFrozenWebPage != null) {
14393+
return;
14394+
}
1431714395
if (success) {
1431814396
foundWebPage = webpage;
1431914397
foundWebPage.display_url = req.message;
@@ -15540,6 +15618,7 @@ public void showFieldPanel(boolean show, MessageObject messageObjectToReply, Mes
1554015618
if (chatActivityEnterView != null) {
1554115619
chatActivityEnterView.updateSendButtonPaid();
1554215620
}
15621+
forkUpdateExtractMediaButton();
1554315622
}
1554415623

1554515624
private void moveScrollToLastMessage(boolean skipSponsored) {
@@ -23378,7 +23457,7 @@ public void didReceivedNotification(int id, int account, final Object... args) {
2337823457
updateVisibleRows();
2337923458
}
2338023459
} else if (id == NotificationCenter.didReceivedWebpagesInUpdates) {
23381-
if (foundWebPage != null) {
23460+
if (foundWebPage != null && forkExtractMediaFrozenWebPage == null) {
2338223461
LongSparseArray<TLRPC.WebPage> hashMap = (LongSparseArray<TLRPC.WebPage>) args[0];
2338323462
for (int a = 0; a < hashMap.size(); a++) {
2338423463
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
@@ -134,6 +134,7 @@
134134
import org.telegram.messenger.LocaleController;
135135
import org.telegram.messenger.MediaController;
136136
import org.telegram.messenger.MediaDataController;
137+
import org.telegram.messenger.forkgram.ExtractMediaFromPreview;
137138
import org.telegram.messenger.MessageObject;
138139
import org.telegram.messenger.MessageSuggestionParams;
139140
import org.telegram.messenger.MessagesController;
@@ -7108,6 +7109,22 @@ protected boolean sendMessageInternal(boolean notify, int scheduleDate, int sche
71087109
if (checkPremiumAnimatedEmoji(currentAccount, dialog_id, parentFragment, null, message)) {
71097110
return;
71107111
}
7112+
if (parentFragment != null && parentFragment.forkIsExtractMediaActive()
7113+
&& ExtractMediaFromPreview.hasMedia(parentFragment.forkExtractMediaFrozenWebPage)) {
7114+
boolean forkSent = forkTrySendExtractedMedia(message, notify, scheduleDate, scheduleRepeatPeriod, payStars);
7115+
if (forkSent) {
7116+
if (messageEditText != null) {
7117+
messageEditText.setText("");
7118+
}
7119+
hideTopView(true);
7120+
if (delegate != null) {
7121+
delegate.onMessageSend(message, notify, scheduleDate, scheduleRepeatPeriod, payStars);
7122+
}
7123+
lastTypingTimeSend = 0;
7124+
}
7125+
updateSendButtonPaid();
7126+
return;
7127+
}
71117128
if (processSendingText(message, notify, scheduleDate, scheduleRepeatPeriod, payStars)) {
71127129
if (delegate.hasForwardingMessages() || (scheduleDate != 0 && !isInScheduleMode()) || isInScheduleMode()) {
71137130
if (messageEditText != null) {
@@ -7654,6 +7671,41 @@ public boolean processSendingText(CharSequence text, boolean notify, int schedul
76547671
return false;
76557672
}
76567673

7674+
private boolean forkTrySendExtractedMedia(CharSequence message, boolean notify, int scheduleDate, int scheduleRepeatPeriod, long payStars) {
7675+
if (parentFragment == null) {
7676+
return false;
7677+
}
7678+
TLRPC.WebPage webPage = parentFragment.forkExtractMediaFrozenWebPage;
7679+
if (!ExtractMediaFromPreview.hasMedia(webPage)) {
7680+
return false;
7681+
}
7682+
boolean isDocument = ExtractMediaFromPreview.isDocument(webPage);
7683+
TLRPC.Chat chat = parentFragment.getCurrentChat();
7684+
if (chat != null) {
7685+
if (isDocument && !ChatObject.canSendDocument(chat)) {
7686+
BulletinFactory.of(parentFragment).createErrorBulletin(ChatObject.getRestrictedErrorText(chat, ChatObject.ACTION_SEND_DOCUMENTS)).show();
7687+
return false;
7688+
}
7689+
if (!isDocument && !ChatObject.canSendPhoto(chat)) {
7690+
BulletinFactory.of(parentFragment).createErrorBulletin(ChatObject.getRestrictedErrorText(chat, ChatObject.ACTION_SEND_PHOTO)).show();
7691+
return false;
7692+
}
7693+
}
7694+
CharSequence trimmed = AndroidUtilities.getTrimmedString(message == null ? "" : message);
7695+
CharSequence[] msg = new CharSequence[]{ trimmed };
7696+
ArrayList<TLRPC.MessageEntity> entities = MediaDataController.getInstance(currentAccount).getEntities(msg, supportsSendingNewEntities());
7697+
String caption = msg[0].toString();
7698+
MessageObject replyToTopMsg = getThreadMessage();
7699+
if (replyToTopMsg == null && replyingTopMessage != null) {
7700+
replyToTopMsg = replyingTopMessage;
7701+
}
7702+
boolean sent = ExtractMediaFromPreview.send(currentAccount, dialog_id, webPage, caption, entities, replyingMessageObject, replyToTopMsg, notify, scheduleDate, scheduleRepeatPeriod, payStars);
7703+
if (sent) {
7704+
parentFragment.forkResetExtractMediaAfterSend();
7705+
}
7706+
return sent;
7707+
}
7708+
76577709
public long getSendMonoForumPeerId() {
76587710
return parentFragment != null ? parentFragment.getSendMonoForumPeerId() : 0;
76597711
}
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)