diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2023-05-11 02:52:50 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2023-05-11 02:52:50 +0200 |
commit | 675386241b35e1c6a02b3948ab5dacafd1537927 (patch) | |
tree | ea490b731ebf39c5c1edd2387c9ce2e1bc559e23 /ui/src/main/java/com/wireguard | |
parent | 8671acc1a629c6fed2eed6c313e06d5d50f65015 (diff) |
ui: cleanup quick tile and modernize
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'ui/src/main/java/com/wireguard')
-rw-r--r-- | ui/src/main/java/com/wireguard/android/QuickTileService.kt | 84 |
1 files changed, 40 insertions, 44 deletions
diff --git a/ui/src/main/java/com/wireguard/android/QuickTileService.kt b/ui/src/main/java/com/wireguard/android/QuickTileService.kt index d1bad2bd..7cd13e6e 100644 --- a/ui/src/main/java/com/wireguard/android/QuickTileService.kt +++ b/ui/src/main/java/com/wireguard/android/QuickTileService.kt @@ -49,28 +49,26 @@ class QuickTileService : TileService() { } override fun onClick() { - if (tunnel != null) { - unlockAndRun { - val tile = qsTile - if (tile != null) { - tile.icon = if (tile.icon == iconOn) iconOff else iconOn - tile.updateTile() - } - applicationScope.launch { - try { - tunnel!!.setStateAsync(Tunnel.State.TOGGLE) - updateTile() - } catch (_: Throwable) { - val toggleIntent = Intent(this@QuickTileService, TunnelToggleActivity::class.java) - toggleIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - startActivity(toggleIntent) + when (val tunnel = tunnel) { + null -> { + val intent = Intent(this, MainActivity::class.java) + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + startActivityAndCollapse(intent) + } + else -> { + unlockAndRun { + applicationScope.launch { + try { + tunnel.setStateAsync(Tunnel.State.TOGGLE) + updateTile() + } catch (_: Throwable) { + val toggleIntent = Intent(this@QuickTileService, TunnelToggleActivity::class.java) + toggleIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + startActivity(toggleIntent) + } } } } - } 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)) icon.setAnimationEnabled(false) /* Unfortunately we can't have animations, since Icons are marshaled. */ icon.setSlashed(false) - var b = Bitmap.createBitmap(icon.intrinsicWidth, icon.intrinsicHeight, Bitmap.Config.ARGB_8888) - ?: return + var b = Bitmap.createBitmap(icon.intrinsicWidth, icon.intrinsicHeight, Bitmap.Config.ARGB_8888) ?: return var c = Canvas(b) icon.setBounds(0, 0, c.width, c.height) icon.draw(c) iconOn = Icon.createWithBitmap(b) icon.setSlashed(true) - b = Bitmap.createBitmap(icon.intrinsicWidth, icon.intrinsicHeight, Bitmap.Config.ARGB_8888) - ?: return + b = Bitmap.createBitmap(icon.intrinsicWidth, icon.intrinsicHeight, Bitmap.Config.ARGB_8888) ?: return c = Canvas(b) icon.setBounds(0, 0, c.width, c.height) icon.draw(c) @@ -106,12 +102,12 @@ class QuickTileService : TileService() { override fun onStartListening() { Application.getTunnelManager().addOnPropertyChangedCallback(onTunnelChangedCallback) - if (tunnel != null) tunnel!!.addOnPropertyChangedCallback(onStateChangedCallback) + tunnel?.addOnPropertyChangedCallback(onStateChangedCallback) updateTile() } override fun onStopListening() { - if (tunnel != null) tunnel!!.removeOnPropertyChangedCallback(onStateChangedCallback) + tunnel?.removeOnPropertyChangedCallback(onStateChangedCallback) Application.getTunnelManager().removeOnPropertyChangedCallback(onTunnelChangedCallback) } @@ -127,26 +123,24 @@ class QuickTileService : TileService() { // Update the tunnel. val newTunnel = Application.getTunnelManager().lastUsedTunnel if (newTunnel != tunnel) { - if (tunnel != null) tunnel!!.removeOnPropertyChangedCallback(onStateChangedCallback) + tunnel?.removeOnPropertyChangedCallback(onStateChangedCallback) tunnel = newTunnel - if (tunnel != null) tunnel!!.addOnPropertyChangedCallback(onStateChangedCallback) + tunnel?.addOnPropertyChangedCallback(onStateChangedCallback) } // Update the tile contents. - val label: String - val state: Int - val tile = qsTile - if (tunnel != null) { - label = tunnel!!.name - state = if (tunnel!!.state == Tunnel.State.UP) Tile.STATE_ACTIVE else Tile.STATE_INACTIVE - } else { - label = getString(R.string.app_name) - state = Tile.STATE_INACTIVE - } - if (tile == null) return - tile.label = label - if (tile.state != state) { - tile.icon = if (state == Tile.STATE_ACTIVE) iconOn else iconOff - tile.state = state + val tile = qsTile ?: return + + when (val tunnel = tunnel) { + null -> { + tile.label = getString(R.string.app_name) + tile.state = Tile.STATE_INACTIVE + tile.icon = iconOff + } + 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 + } } tile.updateTile() } @@ -157,14 +151,16 @@ class QuickTileService : TileService() { sender.removeOnPropertyChangedCallback(this) return } - if (propertyId != 0 && propertyId != BR.state) return + if (propertyId != 0 && propertyId != BR.state) + return updateTile() } } private inner class OnTunnelChangedCallback : OnPropertyChangedCallback() { override fun onPropertyChanged(sender: Observable, propertyId: Int) { - if (propertyId != 0 && propertyId != BR.lastUsedTunnel) return + if (propertyId != 0 && propertyId != BR.lastUsedTunnel) + return updateTile() } } |