Optimize and improve PerAppProxyActivity

This commit is contained in:
2dust 2025-03-20 14:37:38 +08:00
parent a60f45ce31
commit 14ff9eb527
3 changed files with 23 additions and 18 deletions

View file

@ -5,11 +5,8 @@ import android.text.TextUtils
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.view.View
import androidx.appcompat.widget.SearchView
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.v2ray.ang.AppConfig
import com.v2ray.ang.AppConfig.ANG_PACKAGE
import com.v2ray.ang.R
@ -18,6 +15,7 @@ import com.v2ray.ang.dto.AppInfo
import com.v2ray.ang.extension.toast
import com.v2ray.ang.extension.v2RayApplication
import com.v2ray.ang.handler.MmkvManager
import com.v2ray.ang.handler.SettingsManager
import com.v2ray.ang.util.AppManagerUtil
import com.v2ray.ang.util.HttpUtil
import com.v2ray.ang.util.Utils
@ -48,7 +46,7 @@ class PerAppProxyActivity : BaseActivity() {
val blacklist = MmkvManager.decodeSettingsStringSet(AppConfig.PREF_PER_APP_PROXY_SET)
val apps = withContext(Dispatchers.IO) {
val appsList = AppManagerUtil.loadNetworkAppList(this@PerAppProxyActivity)
if (blacklist != null) {
appsList.forEach { app ->
app.isSelected = if (blacklist.contains(app.packageName)) 1 else 0
@ -152,13 +150,20 @@ class PerAppProxyActivity : BaseActivity() {
private fun selectProxyApp() {
toast(R.string.msg_downloading_content)
binding.pbWaiting.show()
val url = AppConfig.androidpackagenamelistUrl
lifecycleScope.launch(Dispatchers.IO) {
val content = HttpUtil.getUrlContent(url, 5000)
var content = HttpUtil.getUrlContent(url, 5000)
if (content.isNullOrEmpty()) {
val httpPort = SettingsManager.getHttpPort()
content = HttpUtil.getUrlContent(url, 5000, httpPort) ?: ""
}
launch(Dispatchers.Main) {
Log.d(ANG_PACKAGE, content)
selectProxyApp(content, true)
toast(R.string.toast_success)
binding.pbWaiting.hide()
}
}
}

View file

@ -26,18 +26,18 @@ object HttpUtil {
*
* @param url The URL to fetch content from.
* @param timeout The timeout value in milliseconds.
* @param httpPort The HTTP port to use.
* @return The content of the URL as a string.
*/
fun getUrlContent(url: String, timeout: Int): String {
var result: String = ""
val conn = createProxyConnection(url, 0, timeout, timeout) ?: return result
fun getUrlContent(url: String, timeout: Int, httpPort: Int = 0): String? {
val conn = createProxyConnection(url, httpPort, timeout, timeout) ?: return null
try {
result = conn.inputStream.bufferedReader().readText()
return conn.inputStream.bufferedReader().readText()
} catch (_: Exception) {
} finally {
conn.disconnect()
}
return result
return null
}
/**

View file

@ -7,6 +7,14 @@
android:fitsSystemWindows="true"
android:orientation="vertical">
<com.google.android.material.progressindicator.LinearProgressIndicator
android:id="@+id/pb_waiting"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
android:visibility="invisible"
app:indicatorColor="@color/color_fab_active" />
<LinearLayout
android:id="@+id/header_view"
android:layout_width="match_parent"
@ -71,14 +79,6 @@
</LinearLayout>
</LinearLayout>
<com.google.android.material.progressindicator.LinearProgressIndicator
android:id="@+id/pb_waiting"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
android:visibility="invisible"
app:indicatorColor="@color/color_fab_active" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"