From c629ebc2594147513b390cb667be3ad70e6115d4 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Sun, 25 Feb 2024 23:45:31 +0800 Subject: [PATCH] chore: add back proxy settings action bar menu --- .../org/telegram/messenger/SharedConfig.java | 12 +++ .../org/telegram/ui/ProxyListActivity.java | 48 +++++++++- .../tw/nekomimi/nekogram/utils/ProxyUtil.kt | 90 +++++++++++++++++++ 3 files changed, 149 insertions(+), 1 deletion(-) diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/SharedConfig.java b/TMessagesProj/src/main/java/org/telegram/messenger/SharedConfig.java index 4ecb4b121f..388fbb00db 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/SharedConfig.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/SharedConfig.java @@ -436,6 +436,18 @@ public String getLink() { } catch (UnsupportedEncodingException ignored) {} return url.toString(); } + + public static ProxyInfo fromUrl(String url) { + Uri lnk = Uri.parse(url); + if (lnk == null) throw new IllegalArgumentException(url); + return new ProxyInfo( + lnk.getQueryParameter("server"), + Utilities.parseInt(lnk.getQueryParameter("port")), + lnk.getQueryParameter("user"), + lnk.getQueryParameter("pass"), + lnk.getQueryParameter("secret") + ); + } } public static LinkedList proxyList = new LinkedList<>(); diff --git a/TMessagesProj/src/main/java/org/telegram/ui/ProxyListActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/ProxyListActivity.java index f240d1703a..57e4b84493 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/ProxyListActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/ProxyListActivity.java @@ -67,7 +67,12 @@ import java.util.ArrayList; import java.util.Collections; +import java.util.LinkedList; import java.util.List; +import java.util.TreeSet; + +import tw.nekomimi.nekogram.utils.AlertUtil; +import tw.nekomimi.nekogram.utils.ProxyUtil; public class ProxyListActivity extends BaseFragment implements NotificationCenter.NotificationCenterDelegate { private final static boolean IS_PROXY_ROTATION_AVAILABLE = true; @@ -108,6 +113,9 @@ public class ProxyListActivity extends BaseFragment implements NotificationCente private List proxyList = new ArrayList<>(); private boolean wasCheckedAllList; + // na: action bar menu + private ActionBarMenuItem otherItem; + public class TextDetailProxyCell extends FrameLayout { private TextView textView; @@ -353,6 +361,12 @@ public void onFragmentDestroy() { NotificationCenter.getInstance(currentAccount).removeObserver(this, NotificationCenter.didUpdateConnectionState); } + private final static int na_menu_other = 1001; + private final static int na_menu_add_input_telegram = 1002; + private final static int na_menu_add_import_from_clipboard = 1003; + private final static int na_menu_retest_ping = 1004; + private final static int na_menu_delete_all = 1005; + @Override public View createView(Context context) { actionBar.setBackButtonDrawable(new BackDrawable(false)); @@ -371,6 +385,32 @@ public void onItemClick(int id) { } }); + // na: action bar menu + ActionBarMenu menu = actionBar.createMenu(); + otherItem = menu.addItem(na_menu_other, R.drawable.ic_ab_other); + otherItem.setContentDescription(LocaleController.getString("AccDescrMoreOptions", R.string.AccDescrMoreOptions)); + otherItem.addSubItem(na_menu_add_input_telegram, LocaleController.getString("AddProxyTelegram", R.string.AddProxyTelegram)).setOnClickListener((v) -> presentFragment(new ProxySettingsActivity())); + otherItem.addSubItem(na_menu_add_import_from_clipboard, LocaleController.getString("ImportProxyFromClipboard", R.string.ImportProxyFromClipboard)).setOnClickListener((v) -> ProxyUtil.importFromClipboard(getParentActivity())); + otherItem.addSubItem(na_menu_retest_ping, LocaleController.getString("RetestPing", R.string.RetestPing)).setOnClickListener((v) -> { + checkProxyList(true); + for (int a = proxyStartRow; a < proxyEndRow; a++) { + RecyclerListView.Holder holder = (RecyclerListView.Holder) listView.findViewHolderForAdapterPosition(a); + if (holder != null) { + TextDetailProxyCell cell = (TextDetailProxyCell) holder.itemView; + cell.updateStatus(); + } + } + }); + otherItem.addSubItem(na_menu_delete_all, LocaleController.getString("DeleteAllServer", R.string.DeleteAllServer)).setOnClickListener((v) -> { + AlertUtil.showConfirm(getParentActivity(), + LocaleController.getString("DeleteAllServer", R.string.DeleteAllServer), + R.drawable.baseline_delete_24, LocaleController.getString("Delete", R.string.Delete), + true, () -> { + SharedConfig.deleteAllProxy(); + updateRows(true); + }); + }); + listAdapter = new ListAdapter(context); fragmentView = new FrameLayout(context); @@ -716,10 +756,16 @@ private void updateRows(boolean notify) { } private void checkProxyList() { + checkProxyList(false); + } + + private void checkProxyList(boolean force) { for (int a = 0, count = proxyList.size(); a < count; a++) { final SharedConfig.ProxyInfo proxyInfo = proxyList.get(a); if (proxyInfo.checking || SystemClock.elapsedRealtime() - proxyInfo.availableCheckTime < 2 * 60 * 1000) { - continue; + if (!force) { + continue; + } } proxyInfo.checking = true; proxyInfo.proxyCheckPingId = ConnectionsManager.getInstance(currentAccount).checkProxy(proxyInfo.address, proxyInfo.port, proxyInfo.username, proxyInfo.password, proxyInfo.secret, time -> AndroidUtilities.runOnUIThread(() -> { diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/utils/ProxyUtil.kt b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/utils/ProxyUtil.kt index f2e095cdff..bfde14d6da 100644 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/utils/ProxyUtil.kt +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/utils/ProxyUtil.kt @@ -5,6 +5,7 @@ package tw.nekomimi.nekogram.utils import android.Manifest import android.app.Activity import android.app.AlertDialog +import android.content.ClipboardManager import android.content.Context import android.content.ContextWrapper import android.content.pm.PackageManager @@ -17,6 +18,7 @@ import android.net.NetworkCapabilities import android.net.NetworkRequest import android.os.Build import android.os.Environment +import android.util.Base64 import android.view.Gravity import android.view.View import android.widget.ImageView @@ -304,4 +306,92 @@ object ProxyUtil { } + @JvmStatic + fun importFromClipboard(ctx: Activity) { + + val text = (ApplicationLoader.applicationContext.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager).primaryClip?.getItemAt(0)?.text?.toString() + + val proxies = mutableListOf() + + var error = false + + text?.trim()?.split('\n')?.map { it.split(" ") }?.forEach { + + it.forEach { line -> + + if (line.startsWith("tg://proxy") || + line.startsWith("tg://socks") || + line.startsWith("https://t.me/proxy") || + line.startsWith("https://t.me/socks")) { + + runCatching { proxies.add(SharedConfig.ProxyInfo.fromUrl(line)) }.onFailure { + + error = true + + showToast(LocaleController.getString("BrokenLink", R.string.BrokenLink) + ": ${it.message ?: it.javaClass.simpleName}") + + } + + } + + } + + } + + runCatching { + + if (proxies.isNullOrEmpty() && !error) { + + String(Base64.decode(text, Base64.NO_PADDING)).trim().split('\n').map { it.split(" ") }.forEach { str -> + + str.forEach { line -> + + if (line.startsWith("tg://proxy") || + line.startsWith("tg://socks") || + line.startsWith("https://t.me/proxy") || + line.startsWith("https://t.me/socks")) { + + runCatching { proxies.add(SharedConfig.ProxyInfo.fromUrl(line)) }.onFailure { + + error = true + + showToast(LocaleController.getString("BrokenLink", R.string.BrokenLink) + ": ${it.message ?: it.javaClass.simpleName}") + + } + + } + + } + + } + + } + + } + + if (proxies.isNullOrEmpty()) { + + if (!error) showToast(LocaleController.getString("BrokenLink", R.string.BrokenLink)) + + return + + } else if (!error) { + + AlertUtil.showSimpleAlert(ctx, LocaleController.getString("ImportedProxies", R.string.ImportedProxies) + "\n\n" + proxies.joinToString("\n") { it.address }) + + } + + proxies.forEach { + + SharedConfig.addProxy(it) + + } + + UIUtil.runOnUIThread { + + NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.proxySettingsChanged) + + } + + } }