feat(agent): GitHub sign-in floats its device code over the browser (#2570)

* feat(agent): GitHub sign-in floats its device code over the browser

* fix(agent): the device code falls back to the browser when nothing could open

* fix(agent): dd-gh-login waits for DroidDeck to report whether the page opened
This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-11-13 13:15:24 -05:00
1 parent 58af2e136b
commit 13b05656aa
6 files changed
+290 -12

No files matched your search

+11
View File
@@ -118,6 +118,17 @@
</intent-filter>
</activity>
<activity
android:name=".DeviceCodeActivity"
android:exported="false"
android:launchMode="singleTask"
android:taskAffinity="${applicationId}.devicecode"
android:excludeFromRecents="true"
android:supportsPictureInPicture="true"
android:resizeableActivity="true"
android:configChanges="orientation|screenSize|screenLayout|smallestScreenSize|density|keyboard|keyboardHidden|navigation"
android:theme="@style/AppTheme" />
<activity
android:name=".PairingGuideActivity"
android:exported="false"
+38 -3
View File
@@ -1,17 +1,52 @@
#!/bin/bash
# Starts GitHub's device login and opens its approval page in Android.
# Starts GitHub's device login, floats its code over Android's browser and opens the approval page.
url=https://github.com/login/device
inbox=${DD_AGENT_INBOX:-}
# DroidDeck takes $inbox/device-code within a few seconds when it can float the code.
float_code() {
[ -n "$inbox" ] && [ -d "$inbox" ] || return 1
rm -f "$inbox/device-code.result"
printf '%s %s\n' "$1" "$url" > "$inbox/device-code.part" && mv -f "$inbox/device-code.part" "$inbox/device-code" || return 1
i=0
while [ $i -lt 40 ] && [ -e "$inbox/device-code" ]; do
sleep 0.2
i=$((i + 1))
done
if [ -e "$inbox/device-code" ]; then
rm -f "$inbox/device-code"
return 1
fi
# DroidDeck writes "ok" or "failed" once it has tried to open the page; no answer counts as failed.
i=0
while [ $i -lt 50 ] && [ ! -e "$inbox/device-code.result" ]; do
sleep 0.2
i=$((i + 1))
done
result=$(cat "$inbox/device-code.result" 2>/dev/null)
rm -f "$inbox/device-code.result"
[ "$result" = ok ]
}
coproc GH { BROWSER=true gh auth login --hostname github.com --git-protocol https --web 2>&1; }
gh_pid=$GH_PID
exec {ghout}<&"${GH[0]}" {ghin}>&"${GH[1]}"
shown=
while IFS= read -r line <&"$ghout"; do
printf '%s\n' "$line"
code=$(grep -oE '[A-Z0-9]{4}-[A-Z0-9]{4}' <<<"$line" | head -1)
# gh's own prompts would push the code out of the one line the setup card shows.
case "$line" in
*"one-time code"* | *"Press Enter"* | *"Open this URL"*) ;;
*) printf '%s\n' "$line" ;;
esac
if [ -n "$code" ] && [ -z "$shown" ]; then
shown=1
echo >&"$ghin"
printf 'GitHub device code: %s · github.com/login/device\n' "$code"
android-open https://github.com/login/device
float_code "$code" || android-open "$url"
fi
done
wait "$gh_pid"
status=$?
[ -n "$shown" ] && [ -n "$inbox" ] && [ -d "$inbox" ] && : > "$inbox/device-code"
exit $status
@@ -0,0 +1,167 @@
package com.droiddeck.launcher
import android.app.ActivityOptions
import android.app.PictureInPictureParams
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.content.res.Configuration
import android.net.Uri
import android.os.Bundle
import android.util.Rational
import androidx.activity.ComponentActivity
import androidx.activity.compose.BackHandler
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.widthIn
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.droiddeck.launcher.core.AppLanguage
import com.droiddeck.launcher.ui.DroidDeckTheme
import com.droiddeck.launcher.ui.FitText
import com.droiddeck.launcher.ui.LocalPalette
import com.droiddeck.launcher.ui.PrimaryButton
import com.droiddeck.launcher.ui.SecondaryButton
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 url = ""
private var display: Int? = null
private var inPip by mutableStateOf(false)
private var openPending = false
override fun attachBaseContext(newBase: Context) = super.attachBaseContext(AppLanguage.wrap(newBase))
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
current = WeakReference(this)
read(intent)
openPending = savedInstanceState == null
setContent { DroidDeckTheme { Float() } }
}
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
setIntent(intent)
read(intent)
openPending = true
}
override fun onDestroy() {
if (current?.get() === this) current = null
super.onDestroy()
}
override fun onResume() {
super.onResume()
if (!openPending) return
if (isInPictureInPictureMode) {
openPage()
return
}
enterPip()
if (!inPip) {
openPage()
finish()
}
}
override fun onPictureInPictureModeChanged(isInPictureInPictureMode: Boolean, newConfig: Configuration) {
super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig)
inPip = isInPictureInPictureMode
if (isInPictureInPictureMode && openPending) openPage()
}
private fun read(intent: Intent) {
code = intent.getStringExtra(EXTRA_CODE).orEmpty()
url = intent.getStringExtra(EXTRA_URL).orEmpty()
display = intent.getIntExtra(EXTRA_DISPLAY, -1).takeIf { it > 0 }
copy()
}
private fun copy() {
runCatching { getSystemService(ClipboardManager::class.java)?.setPrimaryClip(ClipData.newPlainText("GitHub", code)) }
}
private fun enterPip() {
inPip = runCatching {
enterPictureInPictureMode(PictureInPictureParams.Builder().setAspectRatio(Rational(16, 9)).build())
}.getOrDefault(false)
}
private fun openPage() {
openPending = false
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url)).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
val at = display
val opened = runCatching {
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)
}
@androidx.compose.runtime.Composable
private fun Float() {
val colors = MaterialTheme.colorScheme
val accent = LocalPalette.current.signal
BackHandler(enabled = !inPip) { enterPip() }
Box(Modifier.fillMaxSize().background(colors.background), contentAlignment = Alignment.Center) {
Column(
Modifier.widthIn(max = 520.dp).fillMaxWidth().padding(if (inPip) 8.dp else 24.dp),
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)
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)
if (!inPip) Row(horizontalArrangement = Arrangement.spacedBy(12.dp)) {
PrimaryButton(stringResource(R.string.fm_copy), onClick = ::copy)
SecondaryButton(stringResource(R.string.common_close), onClick = ::finish)
}
}
}
}
companion object {
private const val EXTRA_CODE = "code"
private const val EXTRA_URL = "url"
private const val EXTRA_DISPLAY = "display"
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 {
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)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
return runCatching {
if (display > 0) context.startActivity(intent, ActivityOptions.makeBasic().setLaunchDisplayId(display).toBundle())
else context.startActivity(intent)
}.isSuccess
}
fun close() {
current?.get()?.finish()
}
}
}
@@ -508,6 +508,7 @@ object AgentHost {
appContext?.let { app ->
claimLabRequest(app)
claimOpenUrl(app)
claimDeviceCode(app)
// proot comes from an add-on pack fetched after the app starts, so the runtime can become usable later.
if (!available && state != State.WORKING) available = LinuxRuntime.hasRootfs(app)
lab = LabProgress.read(LinuxRuntime.rootDir(app))
@@ -529,15 +530,43 @@ object AgentHost {
val url = runCatching { claimed.readText().trim() }.getOrNull()
claimed.delete()
if (url == null || !(url.startsWith("https://") || url.startsWith("http://"))) return
if (!openUrl(app, url)) runCatching { File(app.filesDir, "$OPEN_URL.failed").writeText("") }
}
/** dd-gh-login leaves "<code> <url>" here to float the code over the page, and an empty file when it ends. */
private fun claimDeviceCode(app: Context) {
val request = File(app.filesDir, DEVICE_CODE)
if (!request.isFile) return
val claimed = File(app.filesDir, "$DEVICE_CODE.claimed")
if (!request.renameTo(claimed)) return
val parts = runCatching { claimed.readText().trim() }.getOrDefault("").split(' ', limit = 2)
claimed.delete()
val code = parts[0]
val url = parts.getOrNull(1)?.takeIf { it.startsWith("https://") }
if (code.isEmpty() || url == null) {
com.droiddeck.launcher.DeviceCodeActivity.close()
return
}
if (!com.droiddeck.launcher.DeviceCodeActivity.show(app, code, url, AgentAdb.browserDisplay)) deviceCodeOpened(app, openUrl(app, url))
}
/** Tells dd-gh-login whether the device page opened, so it can fall back to android-open. */
fun deviceCodeOpened(app: Context, opened: Boolean) {
runCatching {
val part = File(app.filesDir, "$DEVICE_CODE.result.part")
part.writeText(if (opened) "ok" else "failed")
part.renameTo(File(app.filesDir, "$DEVICE_CODE.result"))
}
}
private fun openUrl(app: Context, url: String): Boolean {
val intent = android.content.Intent(android.content.Intent.ACTION_VIEW, android.net.Uri.parse(url))
.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK)
val options = android.app.ActivityOptions.makeBasic().setLaunchDisplayId(AgentAdb.browserDisplay).toBundle()
runCatching { app.startActivity(intent, options) }
return runCatching { app.startActivity(intent, options) }
.recoverCatching { app.startActivity(intent) }
.onFailure {
Log.w(TAG, "could not open a sign-in link: ${it.javaClass.simpleName}")
runCatching { File(app.filesDir, "$OPEN_URL.failed").writeText("") }
}
.onFailure { Log.w(TAG, "could not open a sign-in link: ${it.javaClass.simpleName}") }
.isSuccess
}
/**
@@ -593,6 +622,7 @@ object AgentHost {
private const val RESUME = "agent/resume"
private const val LAB_REQUEST = "agent/lab-request"
private const val OPEN_URL = "agent/open-url"
private const val DEVICE_CODE = "agent/device-code"
private const val LAB_CHAT_KEY = "lab_chat"
/** The raw output of the latest turn, for working out what a CLI did when the chat shows nothing. */
private const val TURN_LOG = "agent/turn.log"
+38 -3
View File
@@ -1,17 +1,52 @@
#!/bin/bash
# Starts GitHub's device login and opens its approval page in Android.
# Starts GitHub's device login, floats its code over Android's browser and opens the approval page.
url=https://github.com/login/device
inbox=${DD_AGENT_INBOX:-}
# DroidDeck takes $inbox/device-code within a few seconds when it can float the code.
float_code() {
[ -n "$inbox" ] && [ -d "$inbox" ] || return 1
rm -f "$inbox/device-code.result"
printf '%s %s\n' "$1" "$url" > "$inbox/device-code.part" && mv -f "$inbox/device-code.part" "$inbox/device-code" || return 1
i=0
while [ $i -lt 40 ] && [ -e "$inbox/device-code" ]; do
sleep 0.2
i=$((i + 1))
done
if [ -e "$inbox/device-code" ]; then
rm -f "$inbox/device-code"
return 1
fi
# DroidDeck writes "ok" or "failed" once it has tried to open the page; no answer counts as failed.
i=0
while [ $i -lt 50 ] && [ ! -e "$inbox/device-code.result" ]; do
sleep 0.2
i=$((i + 1))
done
result=$(cat "$inbox/device-code.result" 2>/dev/null)
rm -f "$inbox/device-code.result"
[ "$result" = ok ]
}
coproc GH { BROWSER=true gh auth login --hostname github.com --git-protocol https --web 2>&1; }
gh_pid=$GH_PID
exec {ghout}<&"${GH[0]}" {ghin}>&"${GH[1]}"
shown=
while IFS= read -r line <&"$ghout"; do
printf '%s\n' "$line"
code=$(grep -oE '[A-Z0-9]{4}-[A-Z0-9]{4}' <<<"$line" | head -1)
# gh's own prompts would push the code out of the one line the setup card shows.
case "$line" in
*"one-time code"* | *"Press Enter"* | *"Open this URL"*) ;;
*) printf '%s\n' "$line" ;;
esac
if [ -n "$code" ] && [ -z "$shown" ]; then
shown=1
echo >&"$ghin"
printf 'GitHub device code: %s · github.com/login/device\n' "$code"
android-open https://github.com/login/device
float_code "$code" || android-open "$url"
fi
done
wait "$gh_pid"
status=$?
[ -n "$shown" ] && [ -n "$inbox" ] && [ -d "$inbox" ] && : > "$inbox/device-code"
exit $status
+1 -1
View File
@@ -27,7 +27,7 @@ if [ "$pkg" = "$P" ]; then
else
python3 "$here/axml_rename.py" "$in" "$work/renamed.apk" \
"$P=$pkg" "$P.androidx-startup=$pkg.androidx-startup" "$P.logs=$pkg.logs" \
"$P.pairingguide=$pkg.pairingguide" "$P.pin=$pkg.pin" \
"$P.pairingguide=$pkg.pairingguide" "$P.devicecode=$pkg.devicecode" "$P.pin=$pkg.pin" \
"$P.documents=$pkg.documents" "$P.home=$pkg.home" "$P.agent=$pkg.agent" \
"$P.vr_recovery=$pkg.vr_recovery" "$P.dualscreen=$pkg.dualscreen" "$P.typing=$pkg.typing" \
"$P.DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION=$pkg.DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION"