@@ -49,28 +49,26 @@ class QuickTileService : TileService() {
49
49
}
50
50
51
51
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
+ }
67
69
}
68
70
}
69
71
}
70
- } else {
71
- val intent = Intent (this , MainActivity ::class .java)
72
- intent.addFlags(Intent .FLAG_ACTIVITY_NEW_TASK )
73
- startActivityAndCollapse(intent)
74
72
}
75
73
}
76
74
@@ -84,15 +82,13 @@ class QuickTileService : TileService() {
84
82
val icon = SlashDrawable (resources.getDrawable(R .drawable.ic_tile, Application .get().theme))
85
83
icon.setAnimationEnabled(false ) /* Unfortunately we can't have animations, since Icons are marshaled. */
86
84
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
89
86
var c = Canvas (b)
90
87
icon.setBounds(0 , 0 , c.width, c.height)
91
88
icon.draw(c)
92
89
iconOn = Icon .createWithBitmap(b)
93
90
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
96
92
c = Canvas (b)
97
93
icon.setBounds(0 , 0 , c.width, c.height)
98
94
icon.draw(c)
@@ -106,12 +102,12 @@ class QuickTileService : TileService() {
106
102
107
103
override fun onStartListening () {
108
104
Application .getTunnelManager().addOnPropertyChangedCallback(onTunnelChangedCallback)
109
- if ( tunnel != null ) tunnel !! .addOnPropertyChangedCallback(onStateChangedCallback)
105
+ tunnel? .addOnPropertyChangedCallback(onStateChangedCallback)
110
106
updateTile()
111
107
}
112
108
113
109
override fun onStopListening () {
114
- if ( tunnel != null ) tunnel !! .removeOnPropertyChangedCallback(onStateChangedCallback)
110
+ tunnel? .removeOnPropertyChangedCallback(onStateChangedCallback)
115
111
Application .getTunnelManager().removeOnPropertyChangedCallback(onTunnelChangedCallback)
116
112
}
117
113
@@ -127,26 +123,24 @@ class QuickTileService : TileService() {
127
123
// Update the tunnel.
128
124
val newTunnel = Application .getTunnelManager().lastUsedTunnel
129
125
if (newTunnel != tunnel) {
130
- if ( tunnel != null ) tunnel !! .removeOnPropertyChangedCallback(onStateChangedCallback)
126
+ tunnel? .removeOnPropertyChangedCallback(onStateChangedCallback)
131
127
tunnel = newTunnel
132
- if ( tunnel != null ) tunnel !! .addOnPropertyChangedCallback(onStateChangedCallback)
128
+ tunnel? .addOnPropertyChangedCallback(onStateChangedCallback)
133
129
}
134
130
// 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
+ }
150
144
}
151
145
tile.updateTile()
152
146
}
@@ -157,14 +151,16 @@ class QuickTileService : TileService() {
157
151
sender.removeOnPropertyChangedCallback(this )
158
152
return
159
153
}
160
- if (propertyId != 0 && propertyId != BR .state) return
154
+ if (propertyId != 0 && propertyId != BR .state)
155
+ return
161
156
updateTile()
162
157
}
163
158
}
164
159
165
160
private inner class OnTunnelChangedCallback : OnPropertyChangedCallback () {
166
161
override fun onPropertyChanged (sender : Observable , propertyId : Int ) {
167
- if (propertyId != 0 && propertyId != BR .lastUsedTunnel) return
162
+ if (propertyId != 0 && propertyId != BR .lastUsedTunnel)
163
+ return
168
164
updateTile()
169
165
}
170
166
}
0 commit comments