tile title = profile name
This commit is contained in:
1 file changed
+24
-12
@@ -1,24 +1,29 @@
|
||||
package com.github.kr328.clash
|
||||
|
||||
import android.content.*
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
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.service.data.ClashProfileEntity
|
||||
import com.github.kr328.clash.utils.ServiceUtils
|
||||
|
||||
class TileService : TileService() {
|
||||
override fun onClick() {
|
||||
val tile = qsTile
|
||||
|
||||
when ( tile.state ) {
|
||||
when (tile.state) {
|
||||
Tile.STATE_INACTIVE -> {
|
||||
ServiceUtils.startProxyService(this)
|
||||
}
|
||||
Tile.STATE_ACTIVE -> {
|
||||
val binder = clashStatusReceiver.peekService(this, Intent(this, ClashService::class.java))
|
||||
val binder =
|
||||
clashStatusReceiver.peekService(this, Intent(this, ClashService::class.java))
|
||||
|
||||
runCatching {
|
||||
val clash = IClashService.Stub.asInterface(binder)
|
||||
@@ -34,7 +39,9 @@ class TileService : TileService() {
|
||||
}
|
||||
|
||||
private fun refreshTileStatus() {
|
||||
when ( currentStatus ) {
|
||||
val current = getCurrentStatus()
|
||||
|
||||
when (current.first) {
|
||||
ProcessEvent.STARTED -> {
|
||||
qsTile.state = Tile.STATE_ACTIVE
|
||||
}
|
||||
@@ -43,10 +50,12 @@ class TileService : TileService() {
|
||||
}
|
||||
}
|
||||
|
||||
qsTile.label = current.second?.name
|
||||
|
||||
qsTile.updateTile()
|
||||
}
|
||||
|
||||
private val clashStatusReceiver = object: BroadcastReceiver() {
|
||||
private val clashStatusReceiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context?, intent: Intent?) {
|
||||
refreshTileStatus()
|
||||
}
|
||||
@@ -55,8 +64,10 @@ class TileService : TileService() {
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
|
||||
registerReceiver(clashStatusReceiver,
|
||||
IntentFilter(Constants.CLASH_PROCESS_BROADCAST_ACTION))
|
||||
registerReceiver(
|
||||
clashStatusReceiver,
|
||||
IntentFilter(Constants.CLASH_PROCESS_BROADCAST_ACTION)
|
||||
)
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
@@ -65,12 +76,13 @@ class TileService : TileService() {
|
||||
unregisterReceiver(clashStatusReceiver)
|
||||
}
|
||||
|
||||
private val currentStatus: ProcessEvent
|
||||
get() {
|
||||
val service = clashStatusReceiver.peekService(this, Intent(this, ClashService::class.java))
|
||||
private fun getCurrentStatus(): Pair<ProcessEvent, ClashProfileEntity?> {
|
||||
val service =
|
||||
IClashService.Stub.asInterface(clashStatusReceiver
|
||||
.peekService(this, Intent(this, ClashService::class.java)))
|
||||
|
||||
return runCatching {
|
||||
IClashService.Stub.asInterface(service)?.currentProcessStatus
|
||||
}.getOrNull() ?: ProcessEvent.STOPPED
|
||||
(service?.currentProcessStatus ?: ProcessEvent.STOPPED) to service?.profileService?.queryActiveProfile()
|
||||
}.getOrNull() ?: ProcessEvent.STOPPED to null
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user