ui: cleanup quick tile and modernize

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2023-05-11 02:52:50 +02:00
parent 8671acc1a6
commit 675386241b
2 changed files with 44 additions and 44 deletions

View File

@ -143,6 +143,10 @@
<meta-data <meta-data
android:name="android.service.quicksettings.ACTIVE_TILE" android:name="android.service.quicksettings.ACTIVE_TILE"
android:value="false" /> android:value="false" />
<meta-data
android:name="android.service.quicksettings.TOGGLEABLE_TILE"
android:value="true" />
</service> </service>
<meta-data <meta-data

View File

@ -49,16 +49,17 @@ class QuickTileService : TileService() {
} }
override fun onClick() { override fun onClick() {
if (tunnel != null) { when (val tunnel = tunnel) {
unlockAndRun { null -> {
val tile = qsTile val intent = Intent(this, MainActivity::class.java)
if (tile != null) { intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
tile.icon = if (tile.icon == iconOn) iconOff else iconOn startActivityAndCollapse(intent)
tile.updateTile()
} }
else -> {
unlockAndRun {
applicationScope.launch { applicationScope.launch {
try { try {
tunnel!!.setStateAsync(Tunnel.State.TOGGLE) tunnel.setStateAsync(Tunnel.State.TOGGLE)
updateTile() updateTile()
} catch (_: Throwable) { } catch (_: Throwable) {
val toggleIntent = Intent(this@QuickTileService, TunnelToggleActivity::class.java) val toggleIntent = Intent(this@QuickTileService, TunnelToggleActivity::class.java)
@ -67,10 +68,7 @@ class QuickTileService : TileService() {
} }
} }
} }
} else { }
val intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivityAndCollapse(intent)
} }
} }
@ -84,15 +82,13 @@ class QuickTileService : TileService() {
val icon = SlashDrawable(resources.getDrawable(R.drawable.ic_tile, Application.get().theme)) val icon = SlashDrawable(resources.getDrawable(R.drawable.ic_tile, Application.get().theme))
icon.setAnimationEnabled(false) /* Unfortunately we can't have animations, since Icons are marshaled. */ icon.setAnimationEnabled(false) /* Unfortunately we can't have animations, since Icons are marshaled. */
icon.setSlashed(false) icon.setSlashed(false)
var b = Bitmap.createBitmap(icon.intrinsicWidth, icon.intrinsicHeight, Bitmap.Config.ARGB_8888) var b = Bitmap.createBitmap(icon.intrinsicWidth, icon.intrinsicHeight, Bitmap.Config.ARGB_8888) ?: return
?: return
var c = Canvas(b) var c = Canvas(b)
icon.setBounds(0, 0, c.width, c.height) icon.setBounds(0, 0, c.width, c.height)
icon.draw(c) icon.draw(c)
iconOn = Icon.createWithBitmap(b) iconOn = Icon.createWithBitmap(b)
icon.setSlashed(true) icon.setSlashed(true)
b = Bitmap.createBitmap(icon.intrinsicWidth, icon.intrinsicHeight, Bitmap.Config.ARGB_8888) b = Bitmap.createBitmap(icon.intrinsicWidth, icon.intrinsicHeight, Bitmap.Config.ARGB_8888) ?: return
?: return
c = Canvas(b) c = Canvas(b)
icon.setBounds(0, 0, c.width, c.height) icon.setBounds(0, 0, c.width, c.height)
icon.draw(c) icon.draw(c)
@ -106,12 +102,12 @@ class QuickTileService : TileService() {
override fun onStartListening() { override fun onStartListening() {
Application.getTunnelManager().addOnPropertyChangedCallback(onTunnelChangedCallback) Application.getTunnelManager().addOnPropertyChangedCallback(onTunnelChangedCallback)
if (tunnel != null) tunnel!!.addOnPropertyChangedCallback(onStateChangedCallback) tunnel?.addOnPropertyChangedCallback(onStateChangedCallback)
updateTile() updateTile()
} }
override fun onStopListening() { override fun onStopListening() {
if (tunnel != null) tunnel!!.removeOnPropertyChangedCallback(onStateChangedCallback) tunnel?.removeOnPropertyChangedCallback(onStateChangedCallback)
Application.getTunnelManager().removeOnPropertyChangedCallback(onTunnelChangedCallback) Application.getTunnelManager().removeOnPropertyChangedCallback(onTunnelChangedCallback)
} }
@ -127,26 +123,24 @@ class QuickTileService : TileService() {
// Update the tunnel. // Update the tunnel.
val newTunnel = Application.getTunnelManager().lastUsedTunnel val newTunnel = Application.getTunnelManager().lastUsedTunnel
if (newTunnel != tunnel) { if (newTunnel != tunnel) {
if (tunnel != null) tunnel!!.removeOnPropertyChangedCallback(onStateChangedCallback) tunnel?.removeOnPropertyChangedCallback(onStateChangedCallback)
tunnel = newTunnel tunnel = newTunnel
if (tunnel != null) tunnel!!.addOnPropertyChangedCallback(onStateChangedCallback) tunnel?.addOnPropertyChangedCallback(onStateChangedCallback)
} }
// Update the tile contents. // Update the tile contents.
val label: String val tile = qsTile ?: return
val state: Int
val tile = qsTile when (val tunnel = tunnel) {
if (tunnel != null) { null -> {
label = tunnel!!.name tile.label = getString(R.string.app_name)
state = if (tunnel!!.state == Tunnel.State.UP) Tile.STATE_ACTIVE else Tile.STATE_INACTIVE tile.state = Tile.STATE_INACTIVE
} else { tile.icon = iconOff
label = getString(R.string.app_name) }
state = Tile.STATE_INACTIVE else -> {
tile.label = tunnel.name
tile.state = if (tunnel.state == Tunnel.State.UP) Tile.STATE_ACTIVE else Tile.STATE_INACTIVE
tile.icon = if (tunnel.state == Tunnel.State.UP) iconOn else iconOff
} }
if (tile == null) return
tile.label = label
if (tile.state != state) {
tile.icon = if (state == Tile.STATE_ACTIVE) iconOn else iconOff
tile.state = state
} }
tile.updateTile() tile.updateTile()
} }
@ -157,14 +151,16 @@ class QuickTileService : TileService() {
sender.removeOnPropertyChangedCallback(this) sender.removeOnPropertyChangedCallback(this)
return return
} }
if (propertyId != 0 && propertyId != BR.state) return if (propertyId != 0 && propertyId != BR.state)
return
updateTile() updateTile()
} }
} }
private inner class OnTunnelChangedCallback : OnPropertyChangedCallback() { private inner class OnTunnelChangedCallback : OnPropertyChangedCallback() {
override fun onPropertyChanged(sender: Observable, propertyId: Int) { override fun onPropertyChanged(sender: Observable, propertyId: Int) {
if (propertyId != 0 && propertyId != BR.lastUsedTunnel) return if (propertyId != 0 && propertyId != BR.lastUsedTunnel)
return
updateTile() updateTile()
} }
} }