register app for opening ss:// and socks5:// proxy URLs

also linkify proxy URLs in text messages
This commit is contained in:
adbenitez
2024-10-05 20:15:40 +02:00
parent 6f3d8a4414
commit 8487c286e6
4 changed files with 66 additions and 2 deletions
@@ -213,6 +213,7 @@ public class EmojiTextView extends AppCompatTextView {
// tools for linkify
private static final Pattern CMD_PATTERN = Pattern.compile("(?<=^|\\s)/[a-zA-Z][a-zA-Z@\\d_/.-]{0,254}");
private static final Pattern PROXY_PATTERN = Pattern.compile("(?<=^|\\s)(SOCKS5|socks5|ss|SS):[^ \\n]+");
private static void replaceURLSpan(SpannableString messageBody) {
URLSpan[] urlSpans = messageBody.getSpans(0, messageBody.length(), URLSpan.class);
@@ -231,6 +232,10 @@ public class EmojiTextView extends AppCompatTextView {
EmojiTextView.replaceURLSpan(messageBody); // replace URLSpan so that it is not removed on the next addLinks() call
}
if (Linkify.addLinks(messageBody, PROXY_PATTERN, null, null, null)) {
EmojiTextView.replaceURLSpan(messageBody); // replace URLSpan so that it is not removed on the next addLinks() call
}
// linkyfiy urls etc., this removes all existing URLSpan
if (Linkify.addLinks(messageBody, Linkify.EMAIL_ADDRESSES|Linkify.WEB_URLS|Linkify.PHONE_NUMBERS)) {
EmojiTextView.replaceURLSpan(messageBody);
@@ -4,6 +4,7 @@ import static org.thoughtcrime.securesms.connect.DcHelper.CONFIG_PROXY_ENABLED;
import static org.thoughtcrime.securesms.connect.DcHelper.CONFIG_PROXY_URL;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
@@ -70,6 +71,14 @@ public class ProxySettingsActivity extends BaseActionBarActivity
proxyList.addFooterView(footer);
adapter.changeData(DcHelper.get(this, CONFIG_PROXY_URL));
DcHelper.getEventCenter(this).addObserver(DcContext.DC_EVENT_CONNECTIVITY_CHANGED, this);
handleOpenProxyUrl();
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleOpenProxyUrl();
}
@Override
@@ -173,6 +182,34 @@ public class ProxySettingsActivity extends BaseActionBarActivity
.show();
}
private void handleOpenProxyUrl() {
if (getIntent() != null && Intent.ACTION_VIEW.equals(getIntent().getAction())) {
Uri uri = getIntent().getData();
if (uri == null) {
return;
}
DcContext dcContext = DcHelper.getContext(this);
final DcLot qrParsed = dcContext.checkQr(uri.toString());
if (qrParsed.getState() == DcContext.DC_QR_PROXY) {
new AlertDialog.Builder(this)
.setTitle(R.string.proxy_use_proxy)
.setMessage(getString(R.string.proxy_use_proxy_confirm, qrParsed.getText1()))
.setPositiveButton(R.string.proxy_use_proxy, (dlg, btn) -> {
dcContext.setConfigFromQr(uri.toString());
dcContext.restartIo();
adapter.changeData(DcHelper.get(this, CONFIG_PROXY_URL));
proxySwitch.setChecked(DcHelper.getInt(this, CONFIG_PROXY_ENABLED) == 1);
})
.setNegativeButton(R.string.cancel, null)
.setCancelable(false)
.show();
} else {
Toast.makeText(this, R.string.proxy_invalid, Toast.LENGTH_LONG).show();
}
}
}
@Override
public void handleEvent(@NonNull DcEvent event) {
if (event.getId() == DcContext.DC_EVENT_CONNECTIVITY_CHANGED) {
@@ -81,7 +81,14 @@ public class LongClickCopySpan extends ClickableSpan {
QrCodeHandler qrCodeHandler = new QrCodeHandler((Activity) widget.getContext());
qrCodeHandler.handleQrData(url);
} else {
IntentUtils.showBrowserIntent(widget.getContext(), url);
Activity activity = (Activity) widget.getContext();
DcContext dcContext = DcHelper.getContext(activity);
if (dcContext.checkQr(url).getState() == DcContext.DC_QR_PROXY) {
QrCodeHandler qrCodeHandler = new QrCodeHandler(activity);
qrCodeHandler.handleQrData(url);
} else {
IntentUtils.showBrowserIntent(widget.getContext(), url);
}
}
}