From 6c58f552b860579894ca76ca2f1e5e5093e03cb8 Mon Sep 17 00:00:00 2001 From: Kr328 <39107975+Kr328@users.noreply.github.com> Date: Tue, 10 Dec 2019 11:23:47 +0800 Subject: [PATCH] tile title = profile name --- .../com/github/kr328/clash/TileService.kt | 36 ++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/github/kr328/clash/TileService.kt b/app/src/main/java/com/github/kr328/clash/TileService.kt index 7248976..bc967ed 100644 --- a/app/src/main/java/com/github/kr328/clash/TileService.kt +++ b/app/src/main/java/com/github/kr328/clash/TileService.kt @@ -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 { + 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 } } \ No newline at end of file