fix(epic): float the Epic code over Chrome so DroidDeck returns once the sign-in is confirmed

This commit is contained in:
Kurt Himebauch committed 2026-11-16 09:55:31 -05:00
1 parent cc27e51d4d
commit 1c2857ddb2
2 files changed
+45 -9

No files matched your search

@@ -44,6 +44,8 @@ import java.lang.ref.WeakReference
/** Floats a sign-in code over the browser page that asks for it. */
class DeviceCodeActivity : ComponentActivity() {
private var code by mutableStateOf("")
private var label by mutableStateOf("")
private var agent = true
private var url = ""
private var display: Int? = null
private var inPip by mutableStateOf(false)
@@ -94,12 +96,14 @@ class DeviceCodeActivity : ComponentActivity() {
private fun read(intent: Intent) {
code = intent.getStringExtra(EXTRA_CODE).orEmpty()
url = intent.getStringExtra(EXTRA_URL).orEmpty()
label = intent.getStringExtra(EXTRA_LABEL) ?: getString(R.string.agent_step_github)
agent = intent.getBooleanExtra(EXTRA_AGENT, true)
display = intent.getIntExtra(EXTRA_DISPLAY, -1).takeIf { it > 0 }
copy()
if (agent) copy()
}
private fun copy() {
runCatching { getSystemService(ClipboardManager::class.java)?.setPrimaryClip(ClipData.newPlainText("GitHub", code)) }
runCatching { getSystemService(ClipboardManager::class.java)?.setPrimaryClip(ClipData.newPlainText(label, code)) }
}
private fun enterPip() {
@@ -116,7 +120,8 @@ class DeviceCodeActivity : ComponentActivity() {
if (at == null) startActivity(intent)
else startActivity(intent, ActivityOptions.makeBasic().setLaunchDisplayId(at).toBundle())
}.recoverCatching { startActivity(intent) }.isSuccess
com.droiddeck.launcher.agent.AgentHost.deviceCodeOpened(applicationContext, opened)
if (agent) com.droiddeck.launcher.agent.AgentHost.deviceCodeOpened(applicationContext, opened)
else if (!opened) finish()
}
@androidx.compose.runtime.Composable
@@ -130,7 +135,7 @@ class DeviceCodeActivity : ComponentActivity() {
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(if (inPip) 2.dp else 16.dp),
) {
Text(stringResource(R.string.agent_step_github), style = MaterialTheme.typography.labelLarge, color = colors.onSurfaceVariant)
Text(label, style = MaterialTheme.typography.labelLarge, color = colors.onSurfaceVariant)
FitText(code, maxSize = if (inPip) 40.sp else 56.sp, fontWeight = FontWeight.Bold, color = accent,
modifier = Modifier.fillMaxWidth())
Text(Uri.parse(url).let { it.host + it.path }, style = MaterialTheme.typography.labelMedium, color = colors.onSurfaceVariant, maxLines = 1)
@@ -146,13 +151,16 @@ class DeviceCodeActivity : ComponentActivity() {
private const val EXTRA_CODE = "code"
private const val EXTRA_URL = "url"
private const val EXTRA_DISPLAY = "display"
private const val EXTRA_LABEL = "label"
private const val EXTRA_AGENT = "agent"
private var current: WeakReference<DeviceCodeActivity>? = null
/** False when this device cannot float a window, so the caller opens the page itself. */
fun show(context: Context, code: String, url: String, display: Int): Boolean {
fun show(context: Context, code: String, url: String, display: Int, label: String? = null): Boolean {
if (!PairingGuideActivity.canFloat(context)) return false
val intent = Intent(context, DeviceCodeActivity::class.java)
.putExtra(EXTRA_CODE, code).putExtra(EXTRA_URL, url).putExtra(EXTRA_DISPLAY, display)
.putExtra(EXTRA_AGENT, label == null).also { if (label != null) it.putExtra(EXTRA_LABEL, label) }
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
return runCatching {
if (display > 0) context.startActivity(intent, ActivityOptions.makeBasic().setLaunchDisplayId(display).toBundle())
@@ -163,5 +171,16 @@ class DeviceCodeActivity : ComponentActivity() {
fun close() {
current?.get()?.finish()
}
/** While the float is on screen, Android lets it bring DroidDeck back in front of the browser. */
fun returnToApp(): Boolean {
val float = current?.get()?.takeIf { !it.isFinishing && !it.isDestroyed } ?: return false
val opened = runCatching {
float.startActivity(Intent(float, FrontEndActivity::class.java)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_REORDER_TO_FRONT))
}.isSuccess
float.finish()
return opened
}
}
}
@@ -13,6 +13,7 @@ import android.provider.Browser
import android.provider.Settings
import android.util.Log
import android.widget.Toast
import com.droiddeck.launcher.DeviceCodeActivity
import com.droiddeck.launcher.FrontEndActivity
import com.droiddeck.launcher.R
import com.droiddeck.launcher.stores.Store
@@ -40,11 +41,13 @@ internal object EpicDeviceLogin {
val start = EpicNative.call("auth.deviceStart")
val url = start?.optString("url").orEmpty()
val deviceCode = start?.optString("deviceCode").orEmpty()
val userCode = start?.optString("userCode").orEmpty()
if (url.isEmpty() || deviceCode.isEmpty()) {
Log.w(TAG, "device sign-in did not start: ${start?.optString("error").orEmpty().ifEmpty { "no answer" }}")
return fail(app, mine)
}
if (!openBrowser(app, url)) return fail(app, mine)
val floated = main.await { DeviceCodeActivity.show(app, userCode, url, 0, Store.EPIC.label) }
if (!floated && !openBrowser(app, url)) return fail(app, mine)
val intervalMs = start!!.optLong("interval", 10).coerceAtLeast(1) * 1000
val deadline = System.currentTimeMillis() + start.optLong("expiresIn", 600) * 1000
while (attempt.get() == mine && System.currentTimeMillis() < deadline) {
@@ -55,6 +58,7 @@ internal object EpicDeviceLogin {
val tokens = EpicCredentialStore.TokenResult.from(answer)
if (tokens == null) {
Log.w(TAG, "device sign-in failed: ${answer.optString("error").ifEmpty { "no token" }}")
main.post { DeviceCodeActivity.close() }
return fail(app, mine)
}
try {
@@ -69,7 +73,10 @@ internal object EpicDeviceLogin {
}
return
}
if (attempt.get() == mine) Log.w(TAG, "device sign-in expired before it was confirmed")
if (attempt.get() == mine) {
Log.w(TAG, "device sign-in expired before it was confirmed")
main.post { DeviceCodeActivity.close() }
}
}
private fun openBrowser(app: Context, url: String): Boolean {
@@ -85,9 +92,11 @@ internal object EpicDeviceLogin {
return false
}
// Android lets an app in the background bring itself forward only while it may draw over
// other apps; without that, a tap on DroidDeck's own notification is what brings it back.
// Android lets an app in the background bring itself forward only while one of its windows is
// visible (the floating code) or it may draw over other apps; otherwise a tap on DroidDeck's
// own notification is what brings it back.
private fun bringBack(app: Context, name: String?) {
if (DeviceCodeActivity.returnToApp()) return
val front = Intent(app, FrontEndActivity::class.java)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_REORDER_TO_FRONT or Intent.FLAG_ACTIVITY_SINGLE_TOP)
if (Settings.canDrawOverlays(app) && runCatching { app.startActivity(front) }.isSuccess) return
@@ -109,6 +118,14 @@ internal object EpicDeviceLogin {
}.onFailure { Log.w(TAG, "could not post the return notice: ${it.javaClass.simpleName}") }
}
private fun Handler.await(block: () -> Boolean): Boolean {
val done = java.util.concurrent.CountDownLatch(1)
var result = false
post { result = runCatching(block).getOrDefault(false); done.countDown() }
done.await()
return result
}
private fun fail(app: Context, mine: Int) {
main.post {
if (attempt.get() == mine) Toast.makeText(app, R.string.stores_login_error_generic, Toast.LENGTH_LONG).show()