1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
| class NetWorkProxyKit : AbstractKit() {
override val icon: Int
get() = R.drawable.ic_acting
override val name: Int
get() = R.string.tools_proxy
override fun onAppInit(context: Context?) {
}
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
@SuppressLint("SetTextI18n")
override fun onClickWithReturn(activity: Activity): Boolean {
val view = LayoutInflater.from(activity).inflate(R.layout.layout_proxy_setting, null)
val hostEdit = view.proxy_host
val portEdit = view.proxy_port
val proxyHistoryGroup = view.proxy_history_group
val emptyView = view.proxy_history_empty
//设置历史代理设置监听
proxyHistoryGroup.setOnCheckedChangeListener { group, checkedId ->
val radioButton = group.findViewById<RadioButton>(checkedId)
val ip = radioButton.text.toString()
hostEdit.setText(ip.split(":")[0])
portEdit.setText(ip.split(":")[1])
}
//回显上次设置的代理
val host = ACache.get(activity).getAsString("proxy_host")
val port = ACache.get(activity).getAsString("proxy_port")
if (!TextUtils.isEmpty(host) && !TextUtils.isEmpty(port)) {
hostEdit.setText(host)
portEdit.setText(port)
}
//回显历史设置
val proxyList: JSONArray = ACache.get(activity).getAsJSONArray("proxy_list") ?: JSONArray()
if (proxyList.length() > 0) {
emptyView.visibility = View.GONE
proxyHistoryGroup.visibility = View.VISIBLE
var index = 0
val keys = arrayListOf<String>()
var selectId = -1
//动态构建选中项
for (i in (proxyList.length() - 1) downTo 0) {
//只展示最近5条
if (index == 5) break
val proxy: JSONObject = proxyList[i] as JSONObject
val proxyHost = proxy["proxy_host"]
val proxyPort = proxy["proxy_port"]
val ip = "${proxyHost}:${proxyPort}"
if (!keys.contains(ip)) {
val radioButton = RadioButton(activity)
radioButton.text = ip
radioButton.id = ThreadLocalRandom.current().nextInt()
if (proxyHost.equals(host)) {
selectId = radioButton.id
}
proxyHistoryGroup.addView(radioButton)
keys.add(ip)
index++
}
}
//设置选中项
if (selectId != -1) {
proxyHistoryGroup.check(selectId)
}
} else {
emptyView.visibility = View.VISIBLE
proxyHistoryGroup.visibility = View.GONE
}
val dialog = AlertDialog.Builder(activity)
.setTitle("设置网络代理")
.setView(view)
.setNegativeButton("清空代理") { _, _ ->
ACache.get(activity).put("proxy_host", "")
ACache.get(activity).put("proxy_port", "")
ToastUtil.shortToast(activity, "1s后将退出应用,再次重启生效!")
Handler(Looper.getMainLooper()).postDelayed({
exitProcess(0)
}, 1500)
}
.setPositiveButton("设置代理") { _, _ ->
ACache.get(activity).put("proxy_host", hostEdit.text.toString())
ACache.get(activity).put("proxy_port", portEdit.text.toString())
ToastUtil.shortToast(activity, "1s后将退出应用,再次重启生效!")
saveProxyList(activity, hostEdit.text.toString(), portEdit.text.toString())
Handler(Looper.getMainLooper()).postDelayed({
exitProcess(0)
}, 1500)
}.create()
dialog.show()
return super.onClickWithReturn(activity)
}
/**
* 注释:保存历史代理设置
* 时间:2021/12/21 0021 17:34
*/
private fun saveProxyList(context: Context?, host: String, port: String) {
val proxyList = ACache.get(context).getAsJSONArray("proxy_list") ?: JSONArray()
val params = mapOf(Pair("proxy_host", host), Pair("proxy_port", port))
proxyList.put(JSONObject(params))
ACache.get(context).put("proxy_list", proxyList)
}
}
|