From 1069ba19918549b3ba8aee1f4fae0e4d0f380953 Mon Sep 17 00:00:00 2001
From: Kr328 <39107975+Kr328@users.noreply.github.com>
Date: Sat, 7 Dec 2019 15:48:45 +0800
Subject: [PATCH] use proxy order from GLOBAL & add tile
---
app/build.gradle | 4 +-
app/src/main/AndroidManifest.xml | 12 ++-
.../com/github/kr328/clash/ProxyActivity.kt | 4 +-
.../com/github/kr328/clash/TileService.kt | 76 +++++++++++++++++++
.../kr328/clash/service/ClashService.kt | 3 +-
.../github/kr328/clash/service/Constants.kt | 5 ++
6 files changed, 99 insertions(+), 5 deletions(-)
create mode 100644 app/src/main/java/com/github/kr328/clash/TileService.kt
create mode 100644 service/src/main/java/com/github/kr328/clash/service/Constants.kt
diff --git a/app/build.gradle b/app/build.gradle
index 8e081e9..e9cbb06 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -11,8 +11,8 @@ android {
applicationId "com.github.kr328.clash"
minSdkVersion 24
targetSdkVersion 29
- versionCode 10005
- versionName "1.0.5-alpha"
+ versionCode 10006
+ versionName "1.0.6-alpha"
}
buildTypes {
release {
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index d2131df..fa43fa6 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -10,7 +10,7 @@
android:name=".MainApplication"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
- android:label="@string/launch_name"
+ android:label="@string/application_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
@@ -86,6 +86,16 @@
+
+
+
+
+
+
val packet = clash.queryAllProxies()
val proxies = packet.proxies
+ val order = (proxies["GLOBAL".hashCode()] ?: error("GLOBAL not found")).all
+ .mapIndexed { index, i -> i to index }.toMap()
val listData = proxies
.asSequence()
@@ -101,7 +103,7 @@ class ProxyActivity : BaseActivity() {
{
it.value.type != ProxyPacket.Type.SELECT
}, {
- it.value.name
+ order[it.key] ?: 0
})
)
.flatMap {
diff --git a/app/src/main/java/com/github/kr328/clash/TileService.kt b/app/src/main/java/com/github/kr328/clash/TileService.kt
new file mode 100644
index 0000000..7248976
--- /dev/null
+++ b/app/src/main/java/com/github/kr328/clash/TileService.kt
@@ -0,0 +1,76 @@
+package com.github.kr328.clash
+
+import android.content.*
+import android.service.quicksettings.Tile
+import android.service.quicksettings.TileService
+import com.github.kr328.clash.core.event.ProcessEvent
+import com.github.kr328.clash.service.ClashService
+import com.github.kr328.clash.service.Constants
+import com.github.kr328.clash.service.IClashService
+import com.github.kr328.clash.utils.ServiceUtils
+
+class TileService : TileService() {
+ override fun onClick() {
+ val tile = qsTile
+
+ when ( tile.state ) {
+ Tile.STATE_INACTIVE -> {
+ ServiceUtils.startProxyService(this)
+ }
+ Tile.STATE_ACTIVE -> {
+ val binder = clashStatusReceiver.peekService(this, Intent(this, ClashService::class.java))
+
+ runCatching {
+ val clash = IClashService.Stub.asInterface(binder)
+
+ clash?.stop()
+ }
+ }
+ }
+ }
+
+ override fun onStartListening() {
+ refreshTileStatus()
+ }
+
+ private fun refreshTileStatus() {
+ when ( currentStatus ) {
+ ProcessEvent.STARTED -> {
+ qsTile.state = Tile.STATE_ACTIVE
+ }
+ ProcessEvent.STOPPED -> {
+ qsTile.state = Tile.STATE_INACTIVE
+ }
+ }
+
+ qsTile.updateTile()
+ }
+
+ private val clashStatusReceiver = object: BroadcastReceiver() {
+ override fun onReceive(context: Context?, intent: Intent?) {
+ refreshTileStatus()
+ }
+ }
+
+ override fun onCreate() {
+ super.onCreate()
+
+ registerReceiver(clashStatusReceiver,
+ IntentFilter(Constants.CLASH_PROCESS_BROADCAST_ACTION))
+ }
+
+ override fun onDestroy() {
+ super.onDestroy()
+
+ unregisterReceiver(clashStatusReceiver)
+ }
+
+ private val currentStatus: ProcessEvent
+ get() {
+ val service = clashStatusReceiver.peekService(this, Intent(this, ClashService::class.java))
+
+ return runCatching {
+ IClashService.Stub.asInterface(service)?.currentProcessStatus
+ }.getOrNull() ?: ProcessEvent.STOPPED
+ }
+}
\ No newline at end of file
diff --git a/service/src/main/java/com/github/kr328/clash/service/ClashService.kt b/service/src/main/java/com/github/kr328/clash/service/ClashService.kt
index 11cf0d0..d371f9e 100644
--- a/service/src/main/java/com/github/kr328/clash/service/ClashService.kt
+++ b/service/src/main/java/com/github/kr328/clash/service/ClashService.kt
@@ -8,7 +8,6 @@ import android.content.IntentFilter
import android.os.*
import com.github.kr328.clash.callback.IUrlTestCallback
import com.github.kr328.clash.core.Clash
-import com.github.kr328.clash.core.ClashProcess
import com.github.kr328.clash.core.event.*
import com.github.kr328.clash.core.model.GeneralPacket
import com.github.kr328.clash.core.model.ProxyPacket
@@ -253,6 +252,8 @@ class ClashService : Service(), IClashEventObserver, ClashEventService.Master,
stopSelf()
}
}
+
+ sendBroadcast(Intent(Constants.CLASH_PROCESS_BROADCAST_ACTION).setPackage(packageName))
}
private fun reloadProfile() {
diff --git a/service/src/main/java/com/github/kr328/clash/service/Constants.kt b/service/src/main/java/com/github/kr328/clash/service/Constants.kt
new file mode 100644
index 0000000..1416826
--- /dev/null
+++ b/service/src/main/java/com/github/kr328/clash/service/Constants.kt
@@ -0,0 +1,5 @@
+package com.github.kr328.clash.service
+
+object Constants {
+ const val CLASH_PROCESS_BROADCAST_ACTION = "com.github.kr328.clash.ClashService.ClashProcessEvent"
+}
\ No newline at end of file