QuickTileService: handle onClick in applicationScope

This helps with the case where the TunnelManager is still loading.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld
2025-05-28 03:49:37 +02:00
parent 1310a513d8
commit fa989fe58e
@@ -52,33 +52,46 @@ class QuickTileService : TileService() {
} }
override fun onClick() { override fun onClick() {
when (val tunnel = tunnel) { applicationScope.launch {
null -> { when (val tunnel = tunnel) {
val intent = Intent(this, MainActivity::class.java) null -> {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) Log.d(TAG, "No tunnel set, so launching main activity")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { val intent = Intent(this@QuickTileService, MainActivity::class.java)
startActivityAndCollapse(PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
} else { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
@Suppress("DEPRECATION") startActivityAndCollapse(PendingIntent.getActivity(this@QuickTileService, 0, intent, PendingIntent.FLAG_IMMUTABLE))
startActivityAndCollapse(intent) } else {
@Suppress("DEPRECATION")
startActivityAndCollapse(intent)
}
} }
}
else -> { else -> {
unlockAndRun { unlockAndRun {
applicationScope.launch { applicationScope.launch {
try { try {
tunnel.setStateAsync(Tunnel.State.TOGGLE) tunnel.setStateAsync(Tunnel.State.TOGGLE)
updateTile() updateTile()
} catch (_: Throwable) { } catch (e: Throwable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE && !Settings.canDrawOverlays(this@QuickTileService)) { Log.d(TAG, "Failed to set state, so falling back", e)
val permissionIntent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:$packageName")) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE && !Settings.canDrawOverlays(this@QuickTileService)) {
permissionIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) Log.d(TAG, "Need overlay permissions")
startActivityAndCollapse(PendingIntent.getActivity(this@QuickTileService, 0, permissionIntent, PendingIntent.FLAG_IMMUTABLE)) val permissionIntent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:$packageName"))
return@launch permissionIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivityAndCollapse(
PendingIntent.getActivity(
this@QuickTileService,
0,
permissionIntent,
PendingIntent.FLAG_IMMUTABLE
)
)
return@launch
}
val toggleIntent = Intent(this@QuickTileService, TunnelToggleActivity::class.java)
toggleIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(toggleIntent)
} }
val toggleIntent = Intent(this@QuickTileService, TunnelToggleActivity::class.java)
toggleIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(toggleIntent)
} }
} }
} }