Skip to content

Commit

Permalink
chore: add back proxy settings action bar menu
Browse files Browse the repository at this point in the history
  • Loading branch information
omg-xtao committed Feb 25, 2024
1 parent b303179 commit c629ebc
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProxyInfo> proxyList = new LinkedList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand All @@ -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);
Expand Down Expand Up @@ -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(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)

}

}
}

0 comments on commit c629ebc

Please sign in to comment.