Skip to content

Commit 6753862

Browse files
committed
ui: cleanup quick tile and modernize
Signed-off-by: Jason A. Donenfeld <[email protected]>
1 parent 8671acc commit 6753862

File tree

2 files changed

+44
-44
lines changed

2 files changed

+44
-44
lines changed

ui/src/main/AndroidManifest.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@
143143
<meta-data
144144
android:name="android.service.quicksettings.ACTIVE_TILE"
145145
android:value="false" />
146+
147+
<meta-data
148+
android:name="android.service.quicksettings.TOGGLEABLE_TILE"
149+
android:value="true" />
146150
</service>
147151

148152
<meta-data

ui/src/main/java/com/wireguard/android/QuickTileService.kt

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -49,28 +49,26 @@ class QuickTileService : TileService() {
4949
}
5050

5151
override fun onClick() {
52-
if (tunnel != null) {
53-
unlockAndRun {
54-
val tile = qsTile
55-
if (tile != null) {
56-
tile.icon = if (tile.icon == iconOn) iconOff else iconOn
57-
tile.updateTile()
58-
}
59-
applicationScope.launch {
60-
try {
61-
tunnel!!.setStateAsync(Tunnel.State.TOGGLE)
62-
updateTile()
63-
} catch (_: Throwable) {
64-
val toggleIntent = Intent(this@QuickTileService, TunnelToggleActivity::class.java)
65-
toggleIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
66-
startActivity(toggleIntent)
52+
when (val tunnel = tunnel) {
53+
null -> {
54+
val intent = Intent(this, MainActivity::class.java)
55+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
56+
startActivityAndCollapse(intent)
57+
}
58+
else -> {
59+
unlockAndRun {
60+
applicationScope.launch {
61+
try {
62+
tunnel.setStateAsync(Tunnel.State.TOGGLE)
63+
updateTile()
64+
} catch (_: Throwable) {
65+
val toggleIntent = Intent(this@QuickTileService, TunnelToggleActivity::class.java)
66+
toggleIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
67+
startActivity(toggleIntent)
68+
}
6769
}
6870
}
6971
}
70-
} else {
71-
val intent = Intent(this, MainActivity::class.java)
72-
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
73-
startActivityAndCollapse(intent)
7472
}
7573
}
7674

@@ -84,15 +82,13 @@ class QuickTileService : TileService() {
8482
val icon = SlashDrawable(resources.getDrawable(R.drawable.ic_tile, Application.get().theme))
8583
icon.setAnimationEnabled(false) /* Unfortunately we can't have animations, since Icons are marshaled. */
8684
icon.setSlashed(false)
87-
var b = Bitmap.createBitmap(icon.intrinsicWidth, icon.intrinsicHeight, Bitmap.Config.ARGB_8888)
88-
?: return
85+
var b = Bitmap.createBitmap(icon.intrinsicWidth, icon.intrinsicHeight, Bitmap.Config.ARGB_8888) ?: return
8986
var c = Canvas(b)
9087
icon.setBounds(0, 0, c.width, c.height)
9188
icon.draw(c)
9289
iconOn = Icon.createWithBitmap(b)
9390
icon.setSlashed(true)
94-
b = Bitmap.createBitmap(icon.intrinsicWidth, icon.intrinsicHeight, Bitmap.Config.ARGB_8888)
95-
?: return
91+
b = Bitmap.createBitmap(icon.intrinsicWidth, icon.intrinsicHeight, Bitmap.Config.ARGB_8888) ?: return
9692
c = Canvas(b)
9793
icon.setBounds(0, 0, c.width, c.height)
9894
icon.draw(c)
@@ -106,12 +102,12 @@ class QuickTileService : TileService() {
106102

107103
override fun onStartListening() {
108104
Application.getTunnelManager().addOnPropertyChangedCallback(onTunnelChangedCallback)
109-
if (tunnel != null) tunnel!!.addOnPropertyChangedCallback(onStateChangedCallback)
105+
tunnel?.addOnPropertyChangedCallback(onStateChangedCallback)
110106
updateTile()
111107
}
112108

113109
override fun onStopListening() {
114-
if (tunnel != null) tunnel!!.removeOnPropertyChangedCallback(onStateChangedCallback)
110+
tunnel?.removeOnPropertyChangedCallback(onStateChangedCallback)
115111
Application.getTunnelManager().removeOnPropertyChangedCallback(onTunnelChangedCallback)
116112
}
117113

@@ -127,26 +123,24 @@ class QuickTileService : TileService() {
127123
// Update the tunnel.
128124
val newTunnel = Application.getTunnelManager().lastUsedTunnel
129125
if (newTunnel != tunnel) {
130-
if (tunnel != null) tunnel!!.removeOnPropertyChangedCallback(onStateChangedCallback)
126+
tunnel?.removeOnPropertyChangedCallback(onStateChangedCallback)
131127
tunnel = newTunnel
132-
if (tunnel != null) tunnel!!.addOnPropertyChangedCallback(onStateChangedCallback)
128+
tunnel?.addOnPropertyChangedCallback(onStateChangedCallback)
133129
}
134130
// Update the tile contents.
135-
val label: String
136-
val state: Int
137-
val tile = qsTile
138-
if (tunnel != null) {
139-
label = tunnel!!.name
140-
state = if (tunnel!!.state == Tunnel.State.UP) Tile.STATE_ACTIVE else Tile.STATE_INACTIVE
141-
} else {
142-
label = getString(R.string.app_name)
143-
state = Tile.STATE_INACTIVE
144-
}
145-
if (tile == null) return
146-
tile.label = label
147-
if (tile.state != state) {
148-
tile.icon = if (state == Tile.STATE_ACTIVE) iconOn else iconOff
149-
tile.state = state
131+
val tile = qsTile ?: return
132+
133+
when (val tunnel = tunnel) {
134+
null -> {
135+
tile.label = getString(R.string.app_name)
136+
tile.state = Tile.STATE_INACTIVE
137+
tile.icon = iconOff
138+
}
139+
else -> {
140+
tile.label = tunnel.name
141+
tile.state = if (tunnel.state == Tunnel.State.UP) Tile.STATE_ACTIVE else Tile.STATE_INACTIVE
142+
tile.icon = if (tunnel.state == Tunnel.State.UP) iconOn else iconOff
143+
}
150144
}
151145
tile.updateTile()
152146
}
@@ -157,14 +151,16 @@ class QuickTileService : TileService() {
157151
sender.removeOnPropertyChangedCallback(this)
158152
return
159153
}
160-
if (propertyId != 0 && propertyId != BR.state) return
154+
if (propertyId != 0 && propertyId != BR.state)
155+
return
161156
updateTile()
162157
}
163158
}
164159

165160
private inner class OnTunnelChangedCallback : OnPropertyChangedCallback() {
166161
override fun onPropertyChanged(sender: Observable, propertyId: Int) {
167-
if (propertyId != 0 && propertyId != BR.lastUsedTunnel) return
162+
if (propertyId != 0 && propertyId != BR.lastUsedTunnel)
163+
return
168164
updateTile()
169165
}
170166
}

0 commit comments

Comments
 (0)