commit  loacal
更新代码1
This commit is contained in:
level 2025-07-04 15:38:30 +08:00
parent 7c270b68a5
commit 1e3265785a
2 changed files with 63 additions and 276 deletions

View File

@ -123,8 +123,6 @@ class NewMainActivity : BaseActivity() {
val clashRunning: Boolean
get() = Remote.broadcasts.clashRunning
// lateinit var tv_rechar: Button
lateinit var tv_username: TextView
lateinit var tv_expire_date: TextView
private lateinit var drawer: DrawerLayout
@ -138,28 +136,77 @@ class NewMainActivity : BaseActivity() {
var payResultLoading: RxDialogShapeLoading? = null
private var timerJob: Job? = null
private var loadingTimeoutNum = 3
private var timerJobNodes: Job? = null
var GetNodeTimeOutNum = 5;
var dialogDoMainChoose:DialogDoMainChoose? =null
private var broadcastReceiver: BroadcastReceiver? = null
private val mPageInfo = PageInfo()
companion object {
//记录心跳失败次数
var HEART_BEAT_FAILURE_FREQUENCY = 0
const val REQUEST_CODE_PAY_WEB_VIEW_ACTIVITY = 1000
var isSubbed = false
var isSubed = false
var newUuid: String? = null
var subUrl: String? = null
// private var profile: ProfileManager? = null
// private var clashManager: ClashManager? = null
var clashBinder: IClashManager? = null
var profileBinder: IProfileManager? = null
var newMainActGlo: Boolean = false
var isUpload: Boolean = false
var hostsMem: ArrayList<String> = ArrayList()
suspend fun setSub2(url: String) {
profileBinder?.let { pm ->
pm.queryAll().forEach { itemPro ->
if (itemPro.name != Conf.getClashConfName(MainApplication.getApp()?.applicationContext)) {
pm.delete(itemPro.uuid)
}
}
var pro = pm.queryAll().find {
it.name == Conf.getClashConfName(MainApplication.getApp()?.applicationContext)
}
pro?.let {
pm.setActive(it)
delay(500)
//判断配置文件source
if (!TextUtils.isEmpty(pro.source)) {
Log.d("setSub2 pro.source==>${pro.source}, url==>${url}")
//订阅链接不同就更换
if (pro.source != url) {
pm.patch(
pro.uuid, pro.name,
url, 1800000L
)
pm.commit(pro.uuid) { status ->
android.util.Log.d(
Conf.TAG,
"new updateStatus: " + Gson().toJson(status.args) + ", pro==>" + status.progress
)
}
}
}
pm.update(it.uuid)
newUuid = pro.uuid.toString()
isSubbed = true
} ?: run {
val uuid: UUID = pm.create(
Profile.Type.Url,
Conf.getClashConfName(MainApplication.getApp()?.applicationContext)
)
delay(500)
pm.queryByUUID(uuid)?.let { qPro ->
pm.setActive(qPro)
pm.patch(
qPro.uuid, qPro.name,
url, 1800000L
)
pm.commit(qPro.uuid) { status ->
android.util.Log.d(
Conf.TAG,
"new updateStatus: " + Gson().toJson(status.args) + ", pro==>" + status.progress
)
}
newUuid = qPro.uuid.toString()
isSubbed = true
}
}
}
}
}
val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
@ -280,186 +327,10 @@ class NewMainActivity : BaseActivity() {
requestPermissionLauncher.launch(android.Manifest.permission.POST_NOTIFICATIONS)
}
}
//在用户未到期的情况下检测节点数据是否可用
val isExpire = checkExpire()
if(!isExpire){
nodeIsChange()
}
//用于检测是否进行了百度上传
mainAtyVM.getExpensesRecord(mPageInfo)
}
private fun nodeIsChange() {
val intentFilter = IntentFilter(Intents.ACTION_NODE_LIST)
broadcastReceiver = NodeListBR()
if (Build.VERSION.SDK_INT >= 34 && applicationInfo.targetSdkVersion >= 34) {
registerReceiver(
broadcastReceiver,
intentFilter,
Context.RECEIVER_EXPORTED
)
} else {
registerReceiver(
broadcastReceiver,
intentFilter
)
}
startTimer()
}
private fun startTimer() {
GetNodeTimeOutNum = 10
timerJobNodes = launch {
repeat(11) {
Log.d("GetNodeTimeOutNum==>$GetNodeTimeOutNum")
if (GetNodeTimeOutNum == 0) {
Log.d("stopTimer...")
RxToast.error(resources.getString(R.string.switchManually))
showDomainList()
timerJobNodes?.cancel()
}
GetNodeTimeOutNum--
delay(1000)
}
}
}
inner class NodeListBR : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
when (intent?.action) {
Intents.ACTION_NODE_LIST -> {
Log.d("new main act ACTION_NODE_LIST ACTION_NODE_LIST")
timerJobNodes?.cancel()
dialogDoMainChoose?.dismiss()
val json = serStore.nodes
if (TextUtils.isEmpty(json)) {
return
}
//缓存订阅地址
MMKV.defaultMMKV().encode(BZYConstants.PROFILE_SUB_URL, subUrl)
//缓存节点
MMKV.defaultMMKV().encode(BZYConstants.NODE_All_ITEM_NAME_KEY, json)
}
else -> {
}
}
}
}
private fun showDomainList() {
try {
SplashHelper(mainAtyVM).getHostWithFallback { success ->
MainScope().launch {
if (success) {
if (dialogDoMainChoose==null) {
dialogDoMainChoose = DialogDoMainChoose(this@NewMainActivity)
}
if (dialogDoMainChoose?.isShowing==false) {
dialogDoMainChoose?.setOnTryButtonListener(object :
DialogDoMainChoose.OnTryButtonListener {
override fun tryButton() {
val currentHost = MMKV.defaultMMKV()
.decodeString(BZYConstants.MMKV_KEY_CURRENT_HOST, "")
val oldUrl = subUrl
currentHost?.let { changeSubUrl(oldUrl!!, it) }
updateProfile(false)
}
override fun cancel() {
com.cncat.vpn.common.log.Log.d("tryButton CHECK_LOGIN cancel")
dialogDoMainChoose?.dismiss()
}
})
dialogDoMainChoose?.setOnListItemListener(object :
DialogDoMainChoose.OnListItemListener {
override fun listItemClick() {
RxToast.info(getString(R.string.StorageSuccessful))
MainApplication.restartApp(this@NewMainActivity,object:MainApplication.OnRestartButtonListener{
override fun enterButton() {
}
override fun cancelButton() {
dialogDoMainChoose?.dismiss()
}
})
}
})
dialogDoMainChoose?.show()
}
}
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
private fun changeSubUrl(originalUrl: String, newUrl: String) {
subUrl = originalUrl.replace(Regex("https://[^/]+/"), "$newUrl/")
}
private fun updateProfile(isDelete: Boolean) {
try {
launch(Dispatchers.IO) {
profileBinder?.let { profileManager ->
var mProfile = profileManager.queryAll().find {
it.name == Conf.getClashConfName(this@NewMainActivity.applicationContext)
}
if (mProfile == null) {
//为空的情况下选择第一个
profileManager.queryAll().forEach { profile ->
mProfile = profile
return@forEach
}
}
mProfile?.let { profile ->
var uuid = profile.uuid
Log.d("ChangeNode22 uuid==>${profile.uuid}")
if (isDelete) {
Log.d("gogogogogo isDelete")
profileManager.delete(profile.uuid)
MMKV.defaultMMKV()
.removeValueForKey(BZYConstants.NODE_All_ITEM_NAME_KEY)
subUrl?.let { setSub2(it) }
} else {
Log.d("gogogogogo update")
profileManager.setActive(profile)
delay(500)
if (!TextUtils.isEmpty(profile.source)) {
// Log.d("updateProfile pro.source==>${profile.source}, url==>${nowSubUrl}")
if (profile.source != subUrl) {
Log.d("updateProfile patch")
subUrl?.let {
profileManager.patch(
profile.uuid,
profile.name,
it,
1800000L
)
}
Log.d("updateProfile patch 1")
profileManager.commit(profile.uuid) { status ->
Log.d("updateProfile updateStatus: " + Gson().toJson(status.args) + ", pro==>" + status.progress)
}
Log.d("updateProfile patch 2")
}
}
Log.d("updateProfile go update")
profileManager.update(profile.uuid)
}
Log.d("gogogogogo end")
}
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
/*
*活动弹窗
* */
@ -723,13 +594,13 @@ class NewMainActivity : BaseActivity() {
// var url2="https://api.ymvpnpro11.cc:8700/7air2vr7ikiqzgvxz/api/v1/client/subscribe?token=5af82930d222840cbc2dec572b21d6c6"
if (url.isNotEmpty()) {
//todo 判断订阅地址是否修改过,未修改就用接口返回的地址反之使用本地修改的地址
var pro_sub_url =
val proSubUrl =
MMKV.defaultMMKV().decodeString(BZYConstants.PROFILE_SUB_URL, "")
if (!TextUtils.isEmpty(pro_sub_url)){
if (pro_sub_url == url) {
if (!TextUtils.isEmpty(proSubUrl)){
if (proSubUrl == url) {
subUrl = url
} else {
subUrl = pro_sub_url
subUrl = proSubUrl
}
}else{
subUrl = url
@ -737,7 +608,6 @@ class NewMainActivity : BaseActivity() {
lifecycleScope.launch(Dispatchers.IO) {
// setSub(url)
subUrl?.let { setSub2(it) }
}
}
@ -1438,91 +1308,6 @@ class NewMainActivity : BaseActivity() {
}
startActivity(intent)
}
private suspend fun setSub2(url: String) {
profileBinder?.let { pm ->
pm.queryAll().forEach { itemPro ->
if (itemPro.name != Conf.getClashConfName(this@NewMainActivity.applicationContext)) {
pm.delete(itemPro.uuid)
}
}
val pro = pm.queryActive()
pro?.let {
pm.setActive(it)
delay(500)
//判断配置文件source
if (!TextUtils.isEmpty(pro.source)) {
Log.d("setSub2 pro.source==>${pro.source}, url==>${url}")
//订阅链接不同就更换
if (pro.source != url) {
pm.patch(
pro.uuid, pro.name,
url, 1800000L
)
pm.commit(pro.uuid) { status ->
android.util.Log.d(
Conf.TAG,
"new updateStatus: " + Gson().toJson(status.args) + ", pro==>" + status.progress
)
}
}
}
pm.update(it.uuid)
newUuid = pro.uuid.toString()
isSubbed = true
} ?: run {
val uuid: UUID = pm.create(
Profile.Type.Url, Conf.getClashConfName(this@NewMainActivity.applicationContext)
)
delay(500)
pm.queryByUUID(uuid)?.let { qPro ->
pm.setActive(qPro)
pm.patch(
qPro.uuid, qPro.name,
url, 1800000L
)
pm.commit(qPro.uuid) { status ->
android.util.Log.d(
Conf.TAG,
"new updateStatus: " + Gson().toJson(status.args) + ", pro==>" + status.progress
)
}
newUuid = qPro.uuid.toString()
isSubbed = true
}
}
}
}
suspend fun createMyProfile(pm: IProfileManager, url: String) {
var uuid: UUID = pm.create(
Profile.Type.Url, Conf.getClashConfName(this@NewMainActivity.applicationContext)
)
delay(500)
pm.queryByUUID(uuid)?.let { qPro ->
pm.setActive(qPro)
pm.patch(
qPro.uuid, qPro.name,
// "https://mojie0201.xn--8stx8olrwkucjq3b.com/api/v1/client/subscribe?token=169110d2674c49604b7e5b5cea341579",
// "https://www.otcopusapp.cc/lx3af288h5i8pz380/api/v1/client/subscribe?token=d6bc98c7ea53099fa5bd1a3ea415ebb6",
// "https://reg.bazhuayujiasu.cc/api/v1/client/subscribe?token=7f0f025ab341bcec4822d2c90aadf8b0",
url, 1800000L
)
// coroutineScope {
pm.commit(qPro.uuid) { status ->
android.util.Log.d(
Conf.TAG,
"new updateStatus: " + Gson().toJson(status.args) + ", pro==>" + status.progress
)
}
newUuid = qPro.uuid.toString();
isSubed = true
// }
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_CODE_PAY_WEB_VIEW_ACTIVITY) {

View File

@ -222,6 +222,8 @@ class SplashActivity : BaseActivity() {
val routerSet =
MMKV.defaultMMKV().decodeStringSet(BZYConstants.MMKV_KEY_ROUTER_HOST)
if (routerSet != null && routerSet.size > 0) {
SplashHelper(splashAtyVM).getHostWithFallback { success ->
}
isNeedRegister(isNeedRegister)
} else {
if (BuildConfig.is_back_cn == true) { //没有备用域名所以无需调用getHost