Skip to content

Commit fa989fe

Browse files
committed
QuickTileService: handle onClick in applicationScope
This helps with the case where the TunnelManager is still loading. Signed-off-by: Jason A. Donenfeld <[email protected]>
1 parent 1310a51 commit fa989fe

File tree

1 file changed

+38
-25
lines changed

1 file changed

+38
-25
lines changed

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

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -52,33 +52,46 @@ class QuickTileService : TileService() {
5252
}
5353

5454
override fun onClick() {
55-
when (val tunnel = tunnel) {
56-
null -> {
57-
val intent = Intent(this, MainActivity::class.java)
58-
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
59-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
60-
startActivityAndCollapse(PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE))
61-
} else {
62-
@Suppress("DEPRECATION")
63-
startActivityAndCollapse(intent)
55+
applicationScope.launch {
56+
when (val tunnel = tunnel) {
57+
null -> {
58+
Log.d(TAG, "No tunnel set, so launching main activity")
59+
val intent = Intent(this@QuickTileService, MainActivity::class.java)
60+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
61+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
62+
startActivityAndCollapse(PendingIntent.getActivity(this@QuickTileService, 0, intent, PendingIntent.FLAG_IMMUTABLE))
63+
} else {
64+
@Suppress("DEPRECATION")
65+
startActivityAndCollapse(intent)
66+
}
6467
}
65-
}
66-
else -> {
67-
unlockAndRun {
68-
applicationScope.launch {
69-
try {
70-
tunnel.setStateAsync(Tunnel.State.TOGGLE)
71-
updateTile()
72-
} catch (_: Throwable) {
73-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE && !Settings.canDrawOverlays(this@QuickTileService)) {
74-
val permissionIntent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:$packageName"))
75-
permissionIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
76-
startActivityAndCollapse(PendingIntent.getActivity(this@QuickTileService, 0, permissionIntent, PendingIntent.FLAG_IMMUTABLE))
77-
return@launch
68+
69+
else -> {
70+
unlockAndRun {
71+
applicationScope.launch {
72+
try {
73+
tunnel.setStateAsync(Tunnel.State.TOGGLE)
74+
updateTile()
75+
} catch (e: Throwable) {
76+
Log.d(TAG, "Failed to set state, so falling back", e)
77+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE && !Settings.canDrawOverlays(this@QuickTileService)) {
78+
Log.d(TAG, "Need overlay permissions")
79+
val permissionIntent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:$packageName"))
80+
permissionIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
81+
startActivityAndCollapse(
82+
PendingIntent.getActivity(
83+
this@QuickTileService,
84+
0,
85+
permissionIntent,
86+
PendingIntent.FLAG_IMMUTABLE
87+
)
88+
)
89+
return@launch
90+
}
91+
val toggleIntent = Intent(this@QuickTileService, TunnelToggleActivity::class.java)
92+
toggleIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
93+
startActivity(toggleIntent)
7894
}
79-
val toggleIntent = Intent(this@QuickTileService, TunnelToggleActivity::class.java)
80-
toggleIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
81-
startActivity(toggleIntent)
8295
}
8396
}
8497
}

0 commit comments

Comments
 (0)