Commit 608e9032 authored by sheteng's avatar sheteng

加速修改

parent 984a0e8f
......@@ -57,7 +57,6 @@ class BottomNavigationFragment : XPageFragment(), ViewPager.OnPageChangeListener
binding.viewPager.adapter = adapter
binding.viewPager.setNoScroll(true)
AcceleratorUtils.initConnect(fragments[0] as AcceleratorFragment)
}
override fun initListeners() {
......
......@@ -29,6 +29,7 @@ import android.hardware.SensorEventListener
import android.os.Handler
import android.util.Log
import com.ccwangluo.accelerator.ui.dialog.PermissionDialog
import com.ccwangluo.accelerator.ui.home.AcceleratorFragment
import com.ccwangluo.accelerator.utils.datareport.DataRePortUtils
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer
import com.tencent.android.tpush.XGIOperateCallback
......@@ -88,6 +89,8 @@ class MainActivity() : XPageActivity(), TencentLocationListener {
mSensorEventListener = JCVideoPlayer.JCAutoFullscreenListener()
DataRePortUtils.report("st_app_open")
AcceleratorUtils.initConnect(this)
}
private fun getPermission() {
......
......@@ -22,6 +22,7 @@ import com.ccwangluo.accelerator.utils.ShareUtils
import com.ccwangluo.accelerator.utils.SysUtils
import com.ccwangluo.accelerator.utils.datareport.DataRePortUtils
import com.ccwangluo.cc_quickly.utils.SettingSPUtils
import com.github.shadowsocks.bg.AuthManager
import com.github.shadowsocks.bg.BaseService
import com.github.shadowsocks.http.HttpConfig
import com.hjq.toast.ToastUtils
......@@ -108,11 +109,11 @@ class AcceleratorFragment : XPageFragment() {
binding.activeNetwork.setText(SysUtils.getActiveNet())
})
acceleratorViewModel.accState.observe(this, {
acceleratorViewModel.accState.observe(this) {
binding.accStateTx.setText(SysUtils.getAccTimeString(accTimer))
state = it
freshData(it)
if (SettingSPUtils.isSmartAccOpen) {
if (AuthManager.isSmartAccOpen) {
binding.smartAccOpen.setText("已开启")
binding.smartAccOpen.setTextColor(resources.getColor(R.color.open_acc_enable))
binding.smartAccOpenArea.visibility = View.GONE
......@@ -121,15 +122,15 @@ class AcceleratorFragment : XPageFragment() {
binding.smartAccOpen.setTextColor(resources.getColor(R.color.white))
binding.smartAccOpenArea.visibility = View.VISIBLE
}
})
}
acceleratorViewModel.userProfile.observe(this, {
acceleratorViewModel.userProfile.observe(this) {
if (it.hasUnreadNotice) {
binding.iconNoticeBadge.visibility = View.VISIBLE
} else {
binding.iconNoticeBadge.visibility = View.GONE
}
})
}
binding.progressViewCircleSmall.setProgressViewUpdateListener(object :
CircleProgressView.CircleProgressUpdateListener {
......@@ -172,13 +173,13 @@ class AcceleratorFragment : XPageFragment() {
}
binding.smartAccOpen.setOnClickListener {
if (!SettingSPUtils.isSmartAccOpen) {
if (!AuthManager.isSmartAccOpen) {
openToWebview(HttpConfig.UI_SETTING_URL)
}
}
binding.smartAccOpenBtn.setOnClickListener {
if (!SettingSPUtils.isSmartAccOpen) {
if (!AuthManager.isSmartAccOpen) {
openToWebview(HttpConfig.UI_SETTING_URL)
}
}
......@@ -246,20 +247,20 @@ class AcceleratorFragment : XPageFragment() {
AcceleratorUtils.getGameList(this) {
if (it) {
//
AcceleratorUtils.game?.let {
if (!SysUtils.isPackageInstall(requireContext(), it.packageName)) {
binding.quickTopToast.visibility = View.VISIBLE
binding.quickTopToastTx.setText("请先安装游戏后加速")
binding.quickTopToastTx.postDelayed({
binding.quickTopToast.visibility = View.GONE
}, 2000)
DataRePortUtils.report(
"st_speed_result",
mapOf("result" to 0, "cause" to "1")
)
return@getGameList
}
}
// AcceleratorUtils.game?.let {
// if (!SysUtils.isPackageInstall(requireContext(), it.packageName)) {
// binding.quickTopToast.visibility = View.VISIBLE
// binding.quickTopToastTx.setText("请先安装游戏后加速")
// binding.quickTopToastTx.postDelayed({
// binding.quickTopToast.visibility = View.GONE
// }, 2000)
// DataRePortUtils.report(
// "st_speed_result",
// mapOf("result" to 0, "cause" to "1")
// )
// return@getGameList
// }
// }
//用户是否有使用时长
LoginUtils.getUserHasTimer(this@AcceleratorFragment) {
if (it) {
......@@ -405,18 +406,18 @@ class AcceleratorFragment : XPageFragment() {
override fun onStart() {
super.onStart()
// AcceleratorUtils.registerStateListener { state, l, net ->
// net?.let {
// acceleratorViewModel.netState.postValue(net)
// }
// accTimer = l
// acceleratorViewModel.accState.postValue(state)
// }
AcceleratorUtils.registerStateListener { state, l, net ->
net?.let {
acceleratorViewModel.netState.postValue(net)
}
accTimer = l
acceleratorViewModel.accState.postValue(state)
}
}
override fun onStop() {
super.onStop()
// AcceleratorUtils.unRegisterStateListener()
AcceleratorUtils.unRegisterStateListener()
}
override fun onResume() {
......
......@@ -13,6 +13,7 @@ import android.content.Intent
import android.net.Uri
import android.provider.Settings
import com.ccwangluo.accelerator.utils.SysUtils
import com.github.shadowsocks.bg.AuthManager
import com.xuexiang.xpage.base.XPageActivity
......@@ -109,7 +110,7 @@ class AndroidActivityInterface(val xPageActivity: XPageActivity, val agentWeb: A
*/
@JavascriptInterface
fun setSmartAccOpen(isopen: Boolean) {
SettingSPUtils.isSmartAccOpen = isopen
AuthManager.isSmartAccOpen = isopen
}
/**
......@@ -118,7 +119,7 @@ class AndroidActivityInterface(val xPageActivity: XPageActivity, val agentWeb: A
@JavascriptInterface
fun getSmartAccStatus(callback: String) {
agentWeb.getJsAccessEntrace()
.quickCallJs(callback, SettingSPUtils.isSmartAccOpen.toString())
.quickCallJs(callback, AuthManager.isSmartAccOpen.toString())
}
/**
......
......@@ -18,6 +18,7 @@ import com.ccwangluo.accelerator.utils.AcceleratorUtils
import com.ccwangluo.accelerator.utils.ShareUtils
import com.ccwangluo.accelerator.utils.SysUtils
import com.ccwangluo.accelerator.utils.datareport.DataRePortUtils
import com.github.shadowsocks.bg.AuthManager
import com.github.shadowsocks.bg.BaseService
import com.tencent.bugly.beta.Beta
......@@ -184,7 +185,7 @@ class AndroidInterface(val xPageFragment: XPageFragment, val agentWeb: AgentWeb)
*/
@JavascriptInterface
fun setSmartAccOpen(isopen: Boolean) {
SettingSPUtils.isSmartAccOpen = isopen
AuthManager.isSmartAccOpen = isopen
}
......@@ -194,7 +195,7 @@ class AndroidInterface(val xPageFragment: XPageFragment, val agentWeb: AgentWeb)
@JavascriptInterface
fun getSmartAccStatus(callback: String) {
agentWeb.getJsAccessEntrace()
.quickCallJs(callback, SettingSPUtils.isSmartAccOpen.toString())
.quickCallJs(callback, AuthManager.isSmartAccOpen.toString())
}
/**
......
......@@ -33,15 +33,6 @@ object SettingSPUtils {
MMKV.defaultMMKV().putBoolean(IS_AGREE_PRIVACY_KEY, isAgreePrivacy)
}
/**
* 智能加速是否启动
*/
var isSmartAccOpen: Boolean
get() = MMKV.defaultMMKV().decodeBool(IS_SMART_ACC_OPEN, true)
set(isSmartAccOpen) {
MMKV.defaultMMKV().putBoolean(IS_SMART_ACC_OPEN, isSmartAccOpen)
}
var lastAPPOpenReportTime: Long
get() = MMKV.defaultMMKV().decodeLong(LAST_APP_SRART_TIME, 0L)
set(lastAPPOpenReportTime) {
......
package com.github.shadowsocks.bg
import android.net.Network
import android.os.Build
import com.ccnet.acc.AccVpn
import com.ccnet.acc.NetworkAdapter
import com.github.shadowsocks.Core
import com.github.shadowsocks.database.Profile
import com.github.shadowsocks.database.ProfileManager
......@@ -11,8 +13,7 @@ import com.google.gson.Gson
import com.google.gson.JsonStreamParser
import com.google.gson.reflect.TypeToken
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.coroutines.*
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.OkHttpClient
import okhttp3.Request
......@@ -21,6 +22,8 @@ import okhttp3.RequestBody.Companion.toRequestBody
import timber.log.Timber
import java.io.File
import java.io.IOException
import java.net.InetSocketAddress
import java.net.Socket
import java.security.SecureRandom
import java.security.cert.X509Certificate
import java.util.concurrent.Executors
......@@ -84,12 +87,22 @@ object AuthManager {
mmkv.putLong("LAST_ACC_TIME", last_acc_time)
}
/**
* 智能加速是否启动
*/
var isSmartAccOpen: Boolean
get() = mmkv.decodeBool("IS_SMART_ACC_OPEN", true)
set(isSmartAccOpen) {
mmkv.putBoolean("IS_SMART_ACC_OPEN", isSmartAccOpen)
}
class AuthException(msg: String) : IOException(msg), BaseService.ExpectedException
suspend fun initAuthProfile(profile: Profile) {
withContext(Dispatchers.IO) {
val gameId: Int = gameId?.toInt() ?: throw AuthException("游戏不存在")
val lineConfigResult = postSync<LineConfig>("/api/acc/line",authToken, LineConfigReq(gameId, area))
val lineConfigResult =
postSync<LineConfig>("/api/acc/line", authToken, LineConfigReq(gameId, area))
if (lineConfigResult.isSuccessful()) {
val lineConfig = lineConfigResult.unwrapData()
profile.host = lineConfig.host
......@@ -113,7 +126,7 @@ object AuthManager {
return
}
scheduleAtFixedRate?.cancel(false)
var failedCount:Int=0
var failedCount: Int = 0
scheduleAtFixedRate = executor.scheduleAtFixedRate({
val context =
if (Build.VERSION.SDK_INT < 24 || Core.user.isUserUnlocked) Core.app else Core.deviceStorage
......@@ -124,12 +137,12 @@ object AuthManager {
if (data.state == BaseService.State.Connected) {
data.proxy?.let { proxyInstance ->
val refreshTokenResult = postSync<RefreshTokenRes>(
"/api/acc/line/refresh",authToken,
"/api/acc/line/refresh", authToken,
RefreshTokenReq(proxyInstance.profile.token ?: "")
)
Timber.d("refreshTokenResult: $refreshTokenResult")
if (refreshTokenResult.isSuccessful()) {
failedCount=0
failedCount = 0
proxyInstance.profile.token = refreshTokenResult.unwrapData().token
configJson.addProperty("token", proxyInstance.profile.token)
configFile.writeText(configJson.toString())
......@@ -138,23 +151,80 @@ object AuthManager {
}
return@let
} else if (refreshTokenResult.code == 1001) {
failedCount=0
failedCount = 0
throw AuthException(refreshTokenResult.error!!)
} else {
failedCount++;
//连续失败超过3次抛异常
if(failedCount>=3){
failedCount=0
if (failedCount >= 3) {
failedCount = 0
throw AuthException("网络异常")
}
}
}
GlobalScope.launch {
testLatency()
}
}
}, 1, 1, TimeUnit.MINUTES)
}
var loginAuthToken:String = "1"
private suspend fun testLatency() {
val networkAutoSwitch =
AuthManager.isSmartAccOpen // 根据用户设置测试延迟 ,过高时切换 connection.bindNetwork(NetworkMode.CELLULAR.code)
if (networkAutoSwitch) {
val wifiLatency: Int = doTestLatency(
DataStore.pingAccHost,
DataStore.pingAccPort,
NetworkAdapter.wifiNetwork
)
val cellularLatency: Int = doTestLatency(
DataStore.pingAccHost,
DataStore.pingAccPort,
NetworkAdapter.cellularNetwork
)
Timber.d("wifi latency: $wifiLatency, cellular latency: $cellularLatency")
} else {
val currentNetworkLatency =
doTestLatency(DataStore.pingAccHost, DataStore.pingAccPort, null, true)
Timber.d("network latency: $currentNetworkLatency")
}
}
private suspend fun doTestLatency(
pingHost: String,
pingPort: Int,
network: Network?,
systemNetwork: Boolean = false
): Int {
return withTimeoutOrNull(DataStore.pingTimeoutMillis.toLong()) {
try {
val socketAddress: Socket = when {
network != null -> {
network.socketFactory.createSocket()
}
systemNetwork -> {
Socket()
}
else -> {
return@withTimeoutOrNull -1
}
}
val start = System.currentTimeMillis()
socketAddress.connect(
InetSocketAddress(pingHost, pingPort),
DataStore.pingTimeoutMillis
)
(System.currentTimeMillis() - start).toInt()
} catch (_: Exception) {
-1
}
} ?: -1
}
var loginAuthToken: String = "1"
val jsonType get() = "application/json; charset=utf-8".toMediaTypeOrNull()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment