chore: add back proxy settings action bar menu

This commit is contained in:
xtaodada 2024-02-25 23:45:31 +08:00
parent af7944c0f0
commit 7d9010c30e
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
3 changed files with 149 additions and 1 deletions

View File

@ -436,6 +436,18 @@ public class SharedConfig {
} 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<ProxyInfo> proxyList = new LinkedList<>();

View File

@ -67,7 +67,12 @@ import org.telegram.ui.Components.SlideChooseView;
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<SharedConfig.ProxyInfo> 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 class ProxyListActivity extends BaseFragment implements NotificationCente
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 class ProxyListActivity extends BaseFragment implements NotificationCente
}
});
// 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 @@ public class ProxyListActivity extends BaseFragment implements NotificationCente
}
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(() -> {

View File

@ -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<SharedConfig.ProxyInfo>()
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)
}
}
}