yijianlianUI/app/src/main/java/com/yingmao/clash/act/ProxyModeActivity.kt

253 lines
10 KiB
Kotlin

package com.yingmao.clash.act
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.ViewGroup.*
import android.widget.LinearLayout
import android.widget.RadioButton
import android.widget.TextView
import androidx.lifecycle.ViewModelProvider
import com.tencent.mmkv.MMKV
import com.yingmao.clash.R
import com.yingmao.clash.base.BaseActivity
import com.yingmao.clash.common.constants.Intents
import com.yingmao.clash.common.log.Log
import com.yingmao.clash.conf.BZYConstants
import com.yingmao.clash.fragment.NewAccFragment
import com.yingmao.clash.service.model.AccessControlMode
import com.yingmao.clash.view.RxToast
import com.yingmao.clash.view.popwindow.CommonPopupWindow
import okio.ByteString.Companion.encode
class ProxyModeActivity : BaseActivity() {
var proxyModeAtyVM: ProxyModeAtyVM? = null
private lateinit var llApplicationMode: LinearLayout
private lateinit var TVApplicationModeTxt: TextView
private lateinit var llControlApplication: LinearLayout
private lateinit var radioApplicationMode: RadioButton
private var popupWindow: CommonPopupWindow? = null
private var layoutGravity: CommonPopupWindow.LayoutGravity? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_proxymodel)
proxyModeAtyVM = ViewModelProvider(this).get(ProxyModeAtyVM::class.java)
showTitleBar(resources.getString(R.string.RoutingMode))
initView()
}
lateinit var radio_but_pac: RadioButton
lateinit var radio_but_global: RadioButton
lateinit var pm: LinearLayout
lateinit var gm: LinearLayout
private fun initView() {
// var ivPacMode = findViewById<ImageView>(R.id.iv_pac_model)
// var ivGlobal = findViewById<ImageView>(R.id.iv_global_mode)
radio_but_pac = findViewById<RadioButton>(R.id.radio_but_pac)
radio_but_global = findViewById<RadioButton>(R.id.radio_but_global)
pm = findViewById<LinearLayout>(R.id.ll_pac_mode)
gm = findViewById<LinearLayout>(R.id.ll_global_model)
llApplicationMode = findViewById(R.id.llApplicationMode)
TVApplicationModeTxt = findViewById(R.id.TVApplicationModeTxt)
llControlApplication = findViewById(R.id.llControlApplication)
radioApplicationMode = findViewById(R.id.radioApplicationMode)
val isGlobal = MMKV.defaultMMKV().decodeBool(BZYConstants.MMKV_KEY_GLOBAL_MODEL, false)
Log.d("initView isGlobal==>${isGlobal}")
// if (isGlobal) {
// ivGlobal.visibility = View.VISIBLE
//
// } else {
// ivPacMode.visibility = View.VISIBLE
// }
var px = ProxyMode.PAC
if (isGlobal) {
px = ProxyMode.GLOBAL
} else {
px = ProxyMode.PAC
}
setBackground(px)
pm.setOnClickListener {
// ivPacMode.visibility = View.VISIBLE
// ivGlobal.visibility = View.INVISIBLE
setBackground(ProxyMode.PAC)
MMKV.defaultMMKV().encode(BZYConstants.MMKV_KEY_GLOBAL_MODEL, false)
goFinish()
}
gm.setOnClickListener {
// ivPacMode.visibility = View.INVISIBLE
// ivGlobal.visibility = View.VISIBLE
MMKV.defaultMMKV().encode(BZYConstants.MMKV_KEY_GLOBAL_MODEL, true)
setBackground(ProxyMode.GLOBAL)
goFinish()
}
radioApplicationMode.setOnClickListener {
// setBackground(ProxyMode.PACKAGE)
}
radio_but_pac.setOnClickListener {
MMKV.defaultMMKV().encode(BZYConstants.MMKV_KEY_GLOBAL_MODEL, false)
setBackground(ProxyMode.PAC)
goFinish()
}
radio_but_global.setOnClickListener {
MMKV.defaultMMKV().encode(BZYConstants.MMKV_KEY_GLOBAL_MODEL, true)
setBackground(ProxyMode.GLOBAL)
goFinish()
}
// getNodes()
val amStr = MMKV.defaultMMKV().decodeString(BZYConstants.applicationMode, "")
if (!amStr.isNullOrEmpty()) {
when (amStr) {
"" -> {
TVApplicationModeTxt.setText(R.string.allow_all_apps)
}
"AllApp" -> {
TVApplicationModeTxt.setText(R.string.allow_all_apps)
}
"ChooseApp" -> {
TVApplicationModeTxt.setText(R.string.allow_selected_apps)
}
"noChooseApp" -> {
TVApplicationModeTxt.setText(R.string.deny_selected_apps)
}
}
}
llApplicationMode.setOnClickListener {
if (NewAccFragment.clashRunning) {
RxToast.info("请关闭加速后再选择.");
return@setOnClickListener
}
popupWindow = object : CommonPopupWindow(
this,
R.layout.applicationmode_popwindow_item,
500,
LayoutParams.WRAP_CONTENT
) {
override fun initView() {
val view = getContentView()
val allApp = view.findViewById<LinearLayout>(R.id.LLAll)
val tvAllApp = view.findViewById<TextView>(R.id.TVAllApp)
val chooseApp = view.findViewById<LinearLayout>(R.id.LLChoose)
val tvChooseApp = view.findViewById<TextView>(R.id.TVChoose)
val noChooseApp = view.findViewById<LinearLayout>(R.id.LLNoChoose)
val tvNoChooseApp = view.findViewById<TextView>(R.id.TVNoChoose)
val applicationModeStr =
MMKV.defaultMMKV().decodeString(BZYConstants.applicationMode)
if (applicationModeStr.isNullOrEmpty()) {
allApp.setBackgroundResource(R.color.color_clash)
tvAllApp.setTextColor(resources.getColor(R.color.white))
} else if (applicationModeStr == "AllApp") {
allApp.setBackgroundResource(R.color.color_clash)
tvAllApp.setTextColor(resources.getColor(R.color.white))
} else if (applicationModeStr == "ChooseApp") {
chooseApp.setBackgroundResource(R.color.color_clash)
tvChooseApp.setTextColor(resources.getColor(R.color.white))
} else if (applicationModeStr == "noChooseApp") {
noChooseApp.setBackgroundResource(R.color.color_clash)
tvNoChooseApp.setTextColor(resources.getColor(R.color.white))
}
allApp.setOnClickListener {
serStore.accessControlMode = AccessControlMode.AcceptAll
MMKV.defaultMMKV().encode(BZYConstants.applicationMode, "AllApp")
TVApplicationModeTxt.setText(R.string.allow_all_apps)
popupWindow.dismiss()
}
chooseApp.setOnClickListener {
serStore.accessControlMode = AccessControlMode.AcceptSelected
MMKV.defaultMMKV().encode(BZYConstants.applicationMode, "ChooseApp")
TVApplicationModeTxt.setText(R.string.allow_selected_apps)
popupWindow.dismiss()
}
noChooseApp.setOnClickListener {
serStore.accessControlMode = AccessControlMode.DenySelected
MMKV.defaultMMKV().encode(BZYConstants.applicationMode, "noChooseApp")
TVApplicationModeTxt.setText(R.string.deny_selected_apps)
popupWindow.dismiss()
}
}
override fun initEvent() {}
}
layoutGravity =
CommonPopupWindow.LayoutGravity(CommonPopupWindow.LayoutGravity.ALIGN_LEFT or CommonPopupWindow.LayoutGravity.TO_BOTTOM)
(popupWindow as CommonPopupWindow).showBashOfAnchor(
TVApplicationModeTxt,
layoutGravity,
0,
0
)
}
llControlApplication.setOnClickListener {
startActivity(Intent(this, InstallApplicationActivity::class.java))
}
}
private fun goFinish() {
val intent = Intent()
setResult(Activity.RESULT_OK, intent)
finish()
}
private fun setBackground(proxyMode: ProxyMode) {
when (proxyMode) {
ProxyMode.PAC -> {
NewAccFragment.isGlo = false
radio_but_global.isChecked = false
radio_but_pac.isChecked = true
radioApplicationMode.isChecked = false
pm.setBackgroundResource(R.drawable.setting_bg_shape_selected)
gm.setBackgroundResource(R.drawable.setting_bg_shape)
llApplicationMode.setBackgroundResource(R.drawable.setting_bg_shape)
sendMode(false)
}
ProxyMode.GLOBAL -> {
NewAccFragment.isGlo = true
radio_but_global.isChecked = true
radio_but_pac.isChecked = false
radioApplicationMode.isChecked = false
pm.setBackgroundResource(R.drawable.setting_bg_shape)
gm.setBackgroundResource(R.drawable.setting_bg_shape_selected)
llApplicationMode.setBackgroundResource(R.drawable.setting_bg_shape)
sendMode(true)
}
ProxyMode.PACKAGE -> {
// pm.setBackgroundResource(R.drawable.setting_bg_shape)
// gm.setBackgroundResource(R.drawable.setting_bg_shape)
// llApplicationMode.setBackgroundResource(R.drawable.setting_bg_shape_selected)
// radio_but_global.isChecked = false
// radio_but_pac.isChecked = false
// radioApplicationMode.isChecked = true
}
}
}
fun sendMode(type: Boolean) {
val intent = Intent(Intents.ACTION_Glo_Mode)
intent.putExtra("mode", type)
sendBroadcast(
intent.setPackage(this.packageName)
)
}
enum class ProxyMode {
PAC, GLOBAL, PACKAGE
}
}