Improve quick settings tiles reliability

Android apparently can decide to kill TileService when there are too many. Therefore, let's check if our service is connected before doing anything.

Source: https://android.googlesource.com/platform/frameworks/base/+/e1d13c9/packages/SystemUI/src/com/android/systemui/qs/external/TileServices.java#52
This commit is contained in:
Mygod
2018-12-25 12:17:43 +08:00
parent 426b93226d
commit 36b0284573
5 changed files with 50 additions and 23 deletions

View File

@@ -3,7 +3,6 @@ package be.mygod.vpnhotspot.manage
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.graphics.drawable.Icon
import android.os.IBinder
import android.service.quicksettings.Tile
@@ -22,8 +21,7 @@ import java.io.IOException
import java.lang.reflect.InvocationTargetException
@RequiresApi(24)
sealed class TetheringTileService : TetherListeningTileService(), ServiceConnection,
TetheringManager.OnStartTetheringCallback {
sealed class TetheringTileService : TetherListeningTileService(), TetheringManager.OnStartTetheringCallback {
protected val tileOff by lazy { Icon.createWithResource(application, icon) }
protected val tileOn by lazy { Icon.createWithResource(application, R.drawable.ic_quick_settings_tile_on) }
@@ -49,6 +47,7 @@ sealed class TetheringTileService : TetherListeningTileService(), ServiceConnect
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
binder = service as TetheringService.Binder
service.routingsChanged[this] = { updateTile() }
super.onServiceConnected(name, service)
}
override fun onServiceDisconnected(name: ComponentName?) {
@@ -89,10 +88,13 @@ sealed class TetheringTileService : TetherListeningTileService(), ServiceConnect
override fun onClick() {
val interested = interested
if (interested.isEmpty()) safeInvoker { start() } else {
val inactive = interested.filter { binder?.isActive(it) != true }
if (inactive.isEmpty()) safeInvoker { stop() }
else ContextCompat.startForegroundService(this, Intent(this, TetheringService::class.java)
.putExtra(TetheringService.EXTRA_ADD_INTERFACES, inactive.toTypedArray()))
val binder = binder
if (binder == null) tapPending = true else {
val inactive = interested.filter { !binder.isActive(it) }
if (inactive.isEmpty()) safeInvoker { stop() }
else ContextCompat.startForegroundService(this, Intent(this, TetheringService::class.java)
.putExtra(TetheringService.EXTRA_ADD_INTERFACES, inactive.toTypedArray()))
}
}
}
@@ -154,10 +156,13 @@ sealed class TetheringTileService : TetherListeningTileService(), ServiceConnect
}
override fun onClick() = if (tethering?.active == true) {
val inactive = interested.filter { binder?.isActive(it) != true }
if (inactive.isEmpty()) safeInvoker { stop() }
else ContextCompat.startForegroundService(this, Intent(this, TetheringService::class.java)
.putExtra(TetheringService.EXTRA_ADD_INTERFACES, inactive.toTypedArray()))
val binder = binder
if (binder == null) tapPending = true else {
val inactive = interested.filter { !binder.isActive(it) }
if (inactive.isEmpty()) safeInvoker { stop() }
else ContextCompat.startForegroundService(this, Intent(this, TetheringService::class.java)
.putExtra(TetheringService.EXTRA_ADD_INTERFACES, inactive.toTypedArray()))
}
} else safeInvoker { start() }
}