Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.
Elevated through the phone's own wireless debugging; Android 17's local network
Bee71f396a0bfb4
diff --git a/README.md b/README.mdindex 5b3b353..dd3738b 100644--- a/README.md+++ b/README.md@@ -93,7 +93,10 @@ place and tested; the user-facing features of Phase 1 come next.its own, at up to 1920 pixels and 60 frames a second, hardware encoded onthe phone; Android asks each time. Turn on **Control from your PC** inthe phone's Nectarlink settings to tap, swipe, scroll and type with the- PC's mouse and keyboard (right-click is Back).+ PC's mouse and keyboard (right-click is Back), or set up **Wireless+ debugging** there once for real touch (live dragging) and keys, with+ nothing else to install; Nectarlink turns Wireless debugging back on by+ itself after a reboot.- **Texts** on the PC: a Messages page with the phone's conversations(pictures included), to reply or start a new one; texts go out throughthe phone.diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.ktsindex d115a96..a953d8e 100644--- a/android/app/build.gradle.kts+++ b/android/app/build.gradle.kts@@ -183,6 +183,9 @@ dependencies {implementation(libs.camerax.lifecycle)implementation(libs.camerax.view)implementation(libs.zxing.core)+ // Elevated: the phone's own wireless debugging (pairing and shell).+ implementation(libs.libadb.android)+ implementation(libs.conscrypt.android)// UniFFI's Kotlin bindings call the core through JNA.implementation(libs.jna) { artifact { type = "aar" } }testImplementation(libs.junit)diff --git a/android/app/proguard-rules.pro b/android/app/proguard-rules.proindex ff7078e..21e83b9 100644--- a/android/app/proguard-rules.pro+++ b/android/app/proguard-rules.pro@@ -4,3 +4,12 @@-keep class * implements com.sun.jna.** { *; }-dontwarn java.awt.**-keep class app.nectarlink.core.** { *; }+# Elevated: the input helper is started by name through app_process.+-keep class app.nectarlink.android.elevated.InputServer { public static void main(java.lang.String[]); }+# Conscrypt (TLS for wireless debugging): its native code finds classes by+# name, and its adapters for Android 4.x name classes that later Android+# doesn't have.+-keep class org.conscrypt.** { *; }+-dontwarn com.android.org.conscrypt.SSLParametersImpl+-dontwarn org.apache.harmony.xnet.provider.jsse.SSLParametersImpl+-keep class io.github.muntashirakon.** { *; }diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xmlindex 20bc9d4..c6dcb71 100644--- a/android/app/src/main/AndroidManifest.xml+++ b/android/app/src/main/AndroidManifest.xml@@ -1,8 +1,11 @@<?xml version="1.0" encoding="utf-8"?><!-- SPDX-License-Identifier: GPL-3.0-or-later -->-<manifest xmlns:android="http://schemas.android.com/apk/res/android">+<manifest xmlns:android="http://schemas.android.com/apk/res/android"+ xmlns:tools="http://schemas.android.com/tools">- <!-- Talking to paired PCs on the local network. -->+ <!-- Talking to paired PCs on the local network. Android 17 asks the user+ for it (the Nearby devices permission group). -->+ <uses-permission android:name="android.permission.ACCESS_LOCAL_NETWORK" /><uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />@@ -13,6 +16,12 @@<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE" /><!-- Sharing the screen with a PC (each time, after Android's prompt). --><uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION" />+ <!-- Elevated: turning wireless debugging back on after a reboot or a new+ Wi-Fi network. Not grantable to apps; Nectarlink grants it to itself+ through the shell once the user has paired it with wireless debugging. -->+ <uses-permission+ android:name="android.permission.WRITE_SECURE_SETTINGS"+ tools:ignore="ProtectedPermissions" /><uses-permission android:name="android.permission.POST_NOTIFICATIONS" /><!-- Scanning the pairing QR code. --><uses-permission android:name="android.permission.CAMERA" />@@ -177,6 +186,11 @@android:resource="@xml/input_service" /></service>+ <!-- The wireless debugging pairing code, typed into a notification. -->+ <receiver+ android:name=".elevated.PairingNotification$Receiver"+ android:exported="false" />+<serviceandroid:name=".mirror.MirrorService"android:exported="false"diff --git a/android/app/src/main/kotlin/app/nectarlink/android/NectarlinkApplication.kt b/android/app/src/main/kotlin/app/nectarlink/android/NectarlinkApplication.ktindex b6d6082..01d45f0 100644--- a/android/app/src/main/kotlin/app/nectarlink/android/NectarlinkApplication.kt+++ b/android/app/src/main/kotlin/app/nectarlink/android/NectarlinkApplication.kt@@ -3,6 +3,7 @@ package app.nectarlink.androidimport android.app.Applicationimport app.nectarlink.android.core.Core+import app.nectarlink.android.elevated.Elevatedimport app.nectarlink.android.update.AppUpdaterimport kotlinx.coroutines.CoroutineScopeimport kotlinx.coroutines.Dispatchers@@ -19,6 +20,9 @@ class NectarlinkApplication : Application() {override fun onCreate() {super.onCreate()+ Elevated.init(this)+ // What the phone offers PCs follows Elevated starting and stopping.+ Elevated.onChange = { core.refreshNotificationAccess() }core.start()}}diff --git a/android/app/src/main/kotlin/app/nectarlink/android/core/BackgroundAccess.kt b/android/app/src/main/kotlin/app/nectarlink/android/core/BackgroundAccess.ktindex a714b90..796d2b7 100644--- a/android/app/src/main/kotlin/app/nectarlink/android/core/BackgroundAccess.kt+++ b/android/app/src/main/kotlin/app/nectarlink/android/core/BackgroundAccess.kt@@ -13,6 +13,20 @@ import androidx.core.net.toUri* Samsung's app freezer in particular) pause Nectarlink between events, so* a PC's requests (replies, dismissals, find my phone) time out.*/+/**+ * The local network: Android 17 lets an app reach devices on it (the PC)+ * only with the user's permission.+ */+object LocalNetwork {+ /** Android 17's permission (API 37). */+ const val PERMISSION = "android.permission.ACCESS_LOCAL_NETWORK"++ fun needed(): Boolean = android.os.Build.VERSION.SDK_INT >= 37++ fun granted(context: android.content.Context): Boolean =+ !needed() || context.checkSelfPermission(PERMISSION) == android.content.pm.PackageManager.PERMISSION_GRANTED+}+object BackgroundAccess {fun isUnrestricted(context: Context): Boolean =context.getSystemService(PowerManager::class.java)?.isIgnoringBatteryOptimizations(context.packageName) ?: truediff --git a/android/app/src/main/kotlin/app/nectarlink/android/core/Core.kt b/android/app/src/main/kotlin/app/nectarlink/android/core/Core.ktindex 5e15209..e343718 100644--- a/android/app/src/main/kotlin/app/nectarlink/android/core/Core.kt+++ b/android/app/src/main/kotlin/app/nectarlink/android/core/Core.kt@@ -17,6 +17,7 @@ import app.nectarlink.android.files.TransferNotificationsimport app.nectarlink.android.Rimport app.nectarlink.android.notifications.NotificationListenerimport app.nectarlink.android.calls.PhoneCalls+import app.nectarlink.android.elevated.Elevatedimport app.nectarlink.android.mirror.InputServiceimport app.nectarlink.android.mirror.MirrorRequestsimport app.nectarlink.android.photos.RecentPhotos@@ -112,6 +113,8 @@ class Core(context: Context, private val scope: CoroutineScope) : EventListenernetworkChange = scope.launch {delay(1_000)node?.networkChanged()+ // A new network turns wireless debugging off; Elevated turns it back on.+ if (!Elevated.running) Elevated.start()}}@@ -162,6 +165,8 @@ class Core(context: Context, private val scope: CoroutineScope) : EventListener)}started.updatePower(powerLevel(), capabilities(NotificationListener.hasAccess(context)))+ // Elevated, when set up and wireless debugging is on.+ scope.launch { Elevated.start() }scope.launch(Dispatchers.Main) {if (_state.value.photoAccess) photos.start()calls.start()@@ -211,6 +216,13 @@ class Core(context: Context, private val scope: CoroutineScope) : EventListenerval smsAccess = PhoneSms.hasAll(context)val inputAccess = InputService.runningval inputChanged = inputAccess != _state.value.inputAccess+ val elevated = Elevated.running+ val elevatedChanged = elevated != _state.value.elevated+ val localNetwork = LocalNetwork.granted(context)+ if (localNetwork && !_state.value.localNetwork) {+ // Just allowed: reach the PCs now rather than at the next retry.+ scope.launch { node?.networkChanged() }+ }val photosChanged = photoAccess != _state.value.photoAccessval callsChanged = callAccess != _state.value.callAccessval smsChanged = smsAccess != _state.value.smsAccess@@ -222,12 +234,14 @@ class Core(context: Context, private val scope: CoroutineScope) : EventListenercallAccess = callAccess,smsAccess = smsAccess,inputAccess = inputAccess,+ elevated = elevated,+ localNetwork = localNetwork,)}if (granted && NotificationListener.instance == null) {NotificationListenerService.requestRebind(NotificationListener.component(context))}- if ((photosChanged || callsChanged || smsChanged || inputChanged) && node != null) {+ if ((photosChanged || callsChanged || smsChanged || inputChanged || elevatedChanged) && node != null) {if (photoAccess) photos.start() else photos.stop()if (PhoneCalls.canFollow(context)) calls.start() else calls.stop()if (PhoneSms.canRead(context)) sms.start() else sms.stop()@@ -371,10 +385,17 @@ class Core(context: Context, private val scope: CoroutineScope) : EventListener(if (PhoneCalls.canControl(context)) listOf("call.control") else emptyList()) +(if (PhoneSms.canRead(context)) listOf("sms.read") else emptyList()) +(if (PhoneSms.canSend(context)) listOf("sms.send") else emptyList()) +- (if (InputService.running) listOf("mirror.input") else emptyList())+ (if (InputService.running || Elevated.running) listOf("mirror.input") else emptyList())- /** Assist when the user turned on control from the PC (docs/PLAN.md §4.6). */- private fun powerLevel(): PowerLevel = if (InputService.running) PowerLevel.ASSIST else PowerLevel.BASIC+ /**+ * Elevated while the wireless debugging helper runs, Assist when the+ * user turned on control from the PC (docs/PLAN.md §4.6).+ */+ private fun powerLevel(): PowerLevel = when {+ Elevated.running -> PowerLevel.ELEVATED+ InputService.running -> PowerLevel.ASSIST+ else -> PowerLevel.BASIC+ }// ---- Pairing ----diff --git a/android/app/src/main/kotlin/app/nectarlink/android/core/CoreState.kt b/android/app/src/main/kotlin/app/nectarlink/android/core/CoreState.ktindex bd2100e..2f2c34b 100644--- a/android/app/src/main/kotlin/app/nectarlink/android/core/CoreState.kt+++ b/android/app/src/main/kotlin/app/nectarlink/android/core/CoreState.kt@@ -62,6 +62,10 @@ data class CoreState(val ringingFrom: String? = null,/** Whether the user let Nectarlink read notifications (to mirror them). */val notificationAccess: Boolean = false,+ /** Whether Android lets Nectarlink reach the local network (Android 17). */+ val localNetwork: Boolean = true,+ /** Whether the Elevated helper runs (wireless debugging). */+ val elevated: Boolean = false,/** Whether the user turned on control from the PC (the accessibility service). */val inputAccess: Boolean = false,/** Whether the user let Nectarlink read and send texts (for PCs). */diff --git a/android/app/src/main/kotlin/app/nectarlink/android/core/PhonePlatform.kt b/android/app/src/main/kotlin/app/nectarlink/android/core/PhonePlatform.ktindex 6157ba1..1c5658d 100644--- a/android/app/src/main/kotlin/app/nectarlink/android/core/PhonePlatform.kt+++ b/android/app/src/main/kotlin/app/nectarlink/android/core/PhonePlatform.kt@@ -12,6 +12,7 @@ import app.nectarlink.android.clipboard.PhoneClipboardimport app.nectarlink.android.notifications.NotificationListenerimport app.nectarlink.android.media.PhoneMediaimport app.nectarlink.android.photos.RecentPhotos+import app.nectarlink.android.elevated.Elevatedimport app.nectarlink.android.mirror.InputServiceimport app.nectarlink.android.mirror.MirrorRequestimport app.nectarlink.android.mirror.MirrorRequests@@ -74,7 +75,10 @@ internal class PhonePlatform(override fun mirrorKeyframeRequested(pcId: String) = MirrorService.keyframe(pcId)- override fun mirrorInput(pcId: String, input: MirrorInputEvent) = InputService.handle(input)+ // Real events when Elevated runs; gestures through the accessibility service otherwise.+ override fun mirrorInput(pcId: String, input: MirrorInputEvent) {+ if (!Elevated.handle(input)) InputService.handle(input)+ }override fun smsThreads(limit: UInt): List<SmsThread> = sms().threads(limit.toInt())diff --git a/android/app/src/main/kotlin/app/nectarlink/android/elevated/AdbKey.kt b/android/app/src/main/kotlin/app/nectarlink/android/elevated/AdbKey.ktnew file mode 100644index 0000000..80a006f--- /dev/null+++ b/android/app/src/main/kotlin/app/nectarlink/android/elevated/AdbKey.kt@@ -0,0 +1,125 @@+// SPDX-License-Identifier: GPL-3.0-or-later+package app.nectarlink.android.elevated++import java.io.ByteArrayOutputStream+import java.io.File+import java.math.BigInteger+import java.security.KeyFactory+import java.security.KeyPair+import java.security.KeyPairGenerator+import java.security.PrivateKey+import java.security.SecureRandom+import java.security.Signature+import java.security.cert.Certificate+import java.security.cert.CertificateFactory+import java.security.spec.PKCS8EncodedKeySpec+import java.text.SimpleDateFormat+import java.util.Date+import java.util.Locale+import java.util.TimeZone++/**+ * This app's key for the phone's own wireless debugging: an RSA key and a+ * self-signed certificate (what adbd remembers after pairing). Kept in the+ * app's private storage; whoever holds it can open a shell on this phone,+ * so it never leaves it.+ */+internal class AdbKey private constructor(val privateKey: PrivateKey, val certificate: Certificate) {+ companion object {+ private const val KEY_FILE = "adb.key"+ private const val CERT_FILE = "adb.crt"++ /** The stored key, or a new one. */+ fun load(dir: File): AdbKey {+ val keyFile = File(dir, KEY_FILE)+ val certFile = File(dir, CERT_FILE)+ if (keyFile.exists() && certFile.exists()) {+ runCatching {+ val key = KeyFactory.getInstance("RSA").generatePrivate(PKCS8EncodedKeySpec(keyFile.readBytes()))+ val cert = CertificateFactory.getInstance("X.509").generateCertificate(certFile.inputStream())+ return AdbKey(key, cert)+ }+ }+ val pair = KeyPairGenerator.getInstance("RSA").apply { initialize(2048, SecureRandom()) }.generateKeyPair()+ val der = selfSignedCertificate(pair, "CN=Nectarlink")+ dir.mkdirs()+ certFile.writeBytes(der)+ keyFile.writeBytes(pair.private.encoded)+ val cert = CertificateFactory.getInstance("X.509").generateCertificate(der.inputStream())+ return AdbKey(pair.private, cert)+ }++ /** Forgets the key (unpaired: a new one is made next time). */+ fun delete(dir: File) {+ File(dir, KEY_FILE).delete()+ File(dir, CERT_FILE).delete()+ }++ /**+ * A minimal X.509 v3 certificate for `pair`, signed by its own key+ * (SHA-256 with RSA), valid for 30 years.+ */+ internal fun selfSignedCertificate(pair: KeyPair, name: String): ByteArray {+ val sha256WithRsa = sequence(oid(byteArrayOf(0x2a, 0x86.toByte(), 0x48, 0x86.toByte(), 0xf7.toByte(), 0x0d, 0x01, 0x01, 0x0b)), byteArrayOf(0x05, 0x00))+ val subject = name(name)+ val now = System.currentTimeMillis()+ val tbs = sequence(+ tlv(0xa0, integer(BigInteger.valueOf(2))), // v3+ integer(BigInteger(64, SecureRandom()).add(BigInteger.ONE)),+ sha256WithRsa,+ subject,+ sequence(time(Date(now - DAY)), time(Date(now + 30 * 365 * DAY))),+ subject,+ pair.public.encoded, // SubjectPublicKeyInfo, already DER+ )+ val signature = Signature.getInstance("SHA256withRSA").run {+ initSign(pair.private)+ update(tbs)+ sign()+ }+ return sequence(tbs, sha256WithRsa, tlv(0x03, byteArrayOf(0) + signature))+ }++ private const val DAY = 24L * 3600 * 1000++ // ---- DER ----++ private fun tlv(tag: Int, content: ByteArray): ByteArray {+ val out = ByteArrayOutputStream()+ out.write(tag)+ val n = content.size+ when {+ n < 0x80 -> out.write(n)+ n < 0x100 -> { out.write(0x81); out.write(n) }+ n < 0x10000 -> { out.write(0x82); out.write(n shr 8); out.write(n and 0xff) }+ else -> { out.write(0x83); out.write(n shr 16); out.write((n shr 8) and 0xff); out.write(n and 0xff) }+ }+ out.write(content)+ return out.toByteArray()+ }++ private fun sequence(vararg parts: ByteArray) = tlv(0x30, parts.fold(ByteArray(0)) { all, part -> all + part })++ private fun integer(value: BigInteger) = tlv(0x02, value.toByteArray())++ private fun oid(encoded: ByteArray) = tlv(0x06, encoded)++ /** `CN=...` as a Name with one UTF8String attribute. */+ private fun name(dn: String): ByteArray {+ val commonName = oid(byteArrayOf(0x55, 0x04, 0x03))+ val value = tlv(0x0c, dn.removePrefix("CN=").toByteArray(Charsets.UTF_8))+ return sequence(tlv(0x31, sequence(commonName, value)))+ }++ /** UTCTime (years before 2050) or GeneralizedTime. */+ private fun time(date: Date): ByteArray {+ val utc = TimeZone.getTimeZone("UTC")+ val year = java.util.Calendar.getInstance(utc).apply { time = date }.get(java.util.Calendar.YEAR)+ return if (year < 2050) {+ tlv(0x17, SimpleDateFormat("yyMMddHHmmss'Z'", Locale.ROOT).apply { timeZone = utc }.format(date).toByteArray())+ } else {+ tlv(0x18, SimpleDateFormat("yyyyMMddHHmmss'Z'", Locale.ROOT).apply { timeZone = utc }.format(date).toByteArray())+ }+ }+ }+}diff --git a/android/app/src/main/kotlin/app/nectarlink/android/elevated/Elevated.kt b/android/app/src/main/kotlin/app/nectarlink/android/elevated/Elevated.ktnew file mode 100644index 0000000..c8fe75c--- /dev/null+++ b/android/app/src/main/kotlin/app/nectarlink/android/elevated/Elevated.kt@@ -0,0 +1,328 @@+// SPDX-License-Identifier: GPL-3.0-or-later+package app.nectarlink.android.elevated++import android.Manifest+import android.content.Context+import android.content.pm.PackageManager+import android.os.Build+import android.provider.Settings+import android.util.Log+import android.view.KeyEvent+import android.view.WindowManager+import androidx.core.content.edit+import app.nectarlink.core.MirrorInputEvent+import app.nectarlink.core.TouchPhase+import io.github.muntashirakon.adb.AbsAdbConnectionManager+import io.github.muntashirakon.adb.AdbPairingRequiredException+import io.github.muntashirakon.adb.AdbStream+import io.github.muntashirakon.adb.android.AdbMdns+import kotlinx.coroutines.CompletableDeferred+import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.flow.MutableStateFlow+import kotlinx.coroutines.flow.StateFlow+import kotlinx.coroutines.flow.asStateFlow+import kotlinx.coroutines.sync.Mutex+import kotlinx.coroutines.sync.withLock+import kotlinx.coroutines.withContext+import kotlinx.coroutines.withTimeoutOrNull+import java.io.OutputStreamWriter+import java.io.Writer+import java.net.InetAddress+import java.security.SecureRandom++/**+ * The Elevated level (docs/PLAN.md §4.6) through the phone's own wireless+ * debugging, with nothing else to install: the user pairs Nectarlink once+ * with a pairing code, and from then on Nectarlink connects to the phone's+ * debugging service and runs [InputServer] with the shell's permissions,+ * for real touch and keys on the mirrored screen.+ *+ * Wireless debugging turns itself off on a reboot or a new Wi-Fi network;+ * after the first pairing Nectarlink may turn it back on itself+ * (WRITE_SECURE_SETTINGS, which it grants itself through the shell).+ */+object Elevated {+ sealed interface State {+ /** Never set up. */+ data object NotSetUp : State+ /** Set up, but wireless debugging is off (or the phone isn't on Wi-Fi). */+ data object Waiting : State+ data object Starting : State+ data object Running : State+ /** Pairing or starting failed. */+ data class Failed(val reason: Reason) : State+ }++ enum class Reason { WrongCode, NoPairingDialog, NotPaired, Other }++ private const val TAG = "Elevated"+ private const val PREFS = "elevated"+ private const val PAIRED = "paired"++ private val _state = MutableStateFlow<State>(State.NotSetUp)+ val state: StateFlow<State> = _state.asStateFlow()++ private lateinit var context: Context+ private val lock = Mutex()+ private var adb: Connection? = null+ private var shell: AdbStream? = null+ @Volatile private var input: Writer? = null+ private var socket: java.net.Socket? = null++ val running: Boolean get() = input != null++ fun init(context: Context) {+ this.context = context.applicationContext+ if (paired()) _state.value = State.Waiting+ }++ private fun paired() = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).getBoolean(PAIRED, false)++ /** The phone's own adbd, as Nectarlink. */+ private class Connection(context: Context) : AbsAdbConnectionManager() {+ private val key = AdbKey.load(context.noBackupFilesDir.resolve("adb"))++ init {+ setApi(Build.VERSION.SDK_INT)+ setHostAddress("127.0.0.1")+ }++ override fun getPrivateKey() = key.privateKey+ override fun getCertificate() = key.certificate+ override fun getDeviceName() = "Nectarlink"+ }++ // ---- Pairing ----++ /**+ * Pairs with the code wireless debugging shows ("Pair device with+ * pairing code"; the dialog must still be open), then starts.+ */+ suspend fun pair(code: String): Boolean = withContext(Dispatchers.IO) {+ lock.withLock {+ _state.value = State.Starting+ val port = discover(AdbMdns.SERVICE_TYPE_TLS_PAIRING)+ if (port == null) {+ _state.value = State.Failed(Reason.NoPairingDialog)+ return@withContext false+ }+ val paired = runCatching { connection().pair("127.0.0.1", port, code.trim()) }.getOrElse {+ Log.w(TAG, "pairing failed", it)+ false+ }+ if (!paired) {+ _state.value = State.Failed(Reason.WrongCode)+ return@withContext false+ }+ context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).edit { putBoolean(PAIRED, true) }+ }+ start()+ }++ /** The port a wireless debugging service is announced on, if it shows up soon. */+ private suspend fun discover(service: String): Int? {+ val found = CompletableDeferred<Int>()+ val mdns = AdbMdns(context, service) { _: InetAddress?, port: Int -> if (port > 0) found.complete(port) }+ mdns.start()+ return try {+ withTimeoutOrNull(DISCOVERY_MS) { found.await() }+ } finally {+ mdns.stop()+ }+ }++ private fun connection(): Connection = adb ?: Connection(context).also { adb = it }++ // ---- Starting ----++ /** Connects and starts the input helper (when set up; again after it stopped). */+ suspend fun start(): Boolean = withContext(Dispatchers.IO) {+ if (!paired()) return@withContext false+ lock.withLock {+ if (running) return@withContext true+ _state.value = State.Starting+ turnOnWirelessDebugging()+ val adb = connection()+ val connected = try {+ adb.isConnected || adb.connectTls(context, CONNECT_MS)+ } catch (e: AdbPairingRequiredException) {+ // adbd forgot us (the user revoked the authorizations).+ context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).edit { putBoolean(PAIRED, false) }+ _state.value = State.Failed(Reason.NotPaired)+ return@withContext false+ } catch (e: Exception) {+ Log.i(TAG, "wireless debugging isn't reachable", e)+ false+ }+ if (!connected) {+ _state.value = State.Waiting+ return@withContext false+ }+ runCatching { launch(adb) }.onFailure {+ Log.w(TAG, "the input helper didn't start", it)+ stopLocked()+ _state.value = State.Failed(Reason.Other)+ }.isSuccess+ }+ }++ /** Wireless debugging back on, when Nectarlink may (granted at the first start). */+ private fun turnOnWirelessDebugging() {+ if (context.checkSelfPermission(Manifest.permission.WRITE_SECURE_SETTINGS) != PackageManager.PERMISSION_GRANTED) return+ runCatching {+ if (Settings.Global.getInt(context.contentResolver, ADB_WIFI, 0) != 1) {+ Settings.Global.putInt(context.contentResolver, ADB_WIFI, 1)+ Thread.sleep(1500) // adbd announces itself.+ }+ }.onFailure { Log.i(TAG, "can't turn on wireless debugging", it) }+ }++ private fun launch(adb: Connection) {+ // Turning wireless debugging back on later is Nectarlink's to do.+ if (context.checkSelfPermission(Manifest.permission.WRITE_SECURE_SETTINGS) != PackageManager.PERMISSION_GRANTED) {+ run(adb, "pm grant ${context.packageName} ${Manifest.permission.WRITE_SECURE_SETTINGS}")+ }+ val token = ByteArray(24).also(SecureRandom()::nextBytes).joinToString("") { "%02x".format(it) }+ val apk = context.applicationInfo.sourceDir+ val command = "CLASSPATH=$apk app_process /system/bin ${InputServer::class.java.name} $token"+ val stream = adb.openStream("shell:$command")+ shell = stream+ // The helper says "ready <port>" once it listens on loopback.+ val output = stream.openInputStream().bufferedReader()+ val first = output.readLine().orEmpty()+ val port = first.removePrefix("ready ").toIntOrNull()+ check(first.startsWith("ready ") && port != null) { "the helper said ${first.ifEmpty { "nothing" }}" }+ val local = java.net.Socket(java.net.InetAddress.getLoopbackAddress(), port).apply { tcpNoDelay = true }+ socket = local+ val writer = OutputStreamWriter(local.outputStream, Charsets.UTF_8)+ writer.write(token + "\n")+ writer.flush()+ input = writer+ _state.value = State.Running+ // Watches the helper: when its shell ends, Elevated stops.+ Thread {+ runCatching { output.lineSequence().forEach { line -> Log.d(TAG, "helper: $line") } }+ input = null+ runCatching { socket?.close() }+ if (_state.value == State.Running) _state.value = State.Waiting+ onChange()+ }.start()+ onChange()+ }++ /** Runs a shell command to its end; returns what it printed. */+ private fun run(adb: Connection, command: String): String {+ val out = java.io.ByteArrayOutputStream()+ adb.openStream("shell:$command").use { stream ->+ val input = stream.openInputStream()+ val buffer = ByteArray(4096)+ // The library reports the end of a command's output as "Stream closed".+ try {+ while (true) {+ val n = input.read(buffer)+ if (n < 0) break+ out.write(buffer, 0, n)+ }+ } catch (_: java.io.IOException) {+ }+ }+ return out.toString(Charsets.UTF_8.name())+ }++ /** Stops the helper and disconnects. */+ suspend fun stop() = withContext(Dispatchers.IO) { lock.withLock { stopLocked() } }++ private fun stopLocked() {+ input = null+ runCatching { socket?.close() }+ runCatching { shell?.close() }+ runCatching { adb?.disconnect() }+ socket = null+ shell = null+ if (paired()) _state.value = State.Waiting+ onChange()+ }++ /** Forgets the pairing (the user turned Elevated off). */+ suspend fun forget() {+ stop()+ context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).edit { putBoolean(PAIRED, false) }+ runCatching { adb?.close() }+ adb = null+ AdbKey.delete(context.noBackupFilesDir.resolve("adb"))+ _state.value = State.NotSetUp+ onChange()+ }++ /** Told when Elevated starts or stops (what the phone offers changes). */+ @Volatile var onChange: () -> Unit = {}++ // ---- Input ----++ /** The PC's input as real events; false if Elevated isn't running. */+ fun handle(event: MirrorInputEvent): Boolean {+ val writer = input ?: return false+ val (w, h) = screen()+ val line = when (event) {+ is MirrorInputEvent.Touch -> {+ val action = when (event.action) {+ TouchPhase.DOWN -> "D"+ TouchPhase.MOVE -> "M"+ TouchPhase.UP -> "U"+ }+ "$action ${event.x * w} ${event.y * h}"+ }+ is MirrorInputEvent.Scroll -> "S ${event.x * w} ${event.y * h} ${event.dx} ${event.dy}"+ is MirrorInputEvent.Key -> keys[event.key]?.let { "K $it" }+ ?: if (event.key == "notifications") "C cmd statusbar expand-notifications" else return true+ // One line per piece: no line breaks inside.+ is MirrorInputEvent.Text -> return event.text.split('\n').withIndex().all { (i, piece) ->+ (i == 0 || send(writer, "K ${KeyEvent.KEYCODE_ENTER}")) && (piece.isEmpty() || send(writer, "T $piece"))+ }+ }+ return send(writer, line)+ }++ private fun send(writer: Writer, line: String): Boolean = try {+ synchronized(writer) {+ writer.write(line + "\n")+ writer.flush()+ }+ true+ } catch (e: Exception) {+ Log.i(TAG, "the input helper went away", e)+ input = null+ false+ }++ private val keys = mapOf(+ "back" to KeyEvent.KEYCODE_BACK,+ "home" to KeyEvent.KEYCODE_HOME,+ "recents" to KeyEvent.KEYCODE_APP_SWITCH,+ "enter" to KeyEvent.KEYCODE_ENTER,+ "backspace" to KeyEvent.KEYCODE_DEL,+ "delete" to KeyEvent.KEYCODE_FORWARD_DEL,+ "left" to KeyEvent.KEYCODE_DPAD_LEFT,+ "right" to KeyEvent.KEYCODE_DPAD_RIGHT,+ "up" to KeyEvent.KEYCODE_DPAD_UP,+ "down" to KeyEvent.KEYCODE_DPAD_DOWN,+ "tab" to KeyEvent.KEYCODE_TAB,+ )++ /** The screen's size in pixels, as it's turned now. */+ private fun screen(): Pair<Float, Float> {+ val windows = context.getSystemService(WindowManager::class.java)+ return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {+ windows.maximumWindowMetrics.bounds.let { it.width().toFloat() to it.height().toFloat() }+ } else {+ val metrics = android.util.DisplayMetrics()+ @Suppress("DEPRECATION")+ windows.defaultDisplay.getRealMetrics(metrics)+ metrics.widthPixels.toFloat() to metrics.heightPixels.toFloat()+ }+ }++ private const val ADB_WIFI = "adb_wifi_enabled"+ private const val DISCOVERY_MS = 10_000L+ private const val CONNECT_MS = 10_000L+}diff --git a/android/app/src/main/kotlin/app/nectarlink/android/elevated/InputServer.kt b/android/app/src/main/kotlin/app/nectarlink/android/elevated/InputServer.ktnew file mode 100644index 0000000..5956406--- /dev/null+++ b/android/app/src/main/kotlin/app/nectarlink/android/elevated/InputServer.kt@@ -0,0 +1,151 @@+// SPDX-License-Identifier: GPL-3.0-or-later+package app.nectarlink.android.elevated++import android.os.SystemClock+import android.view.InputDevice+import android.view.InputEvent+import android.view.KeyCharacterMap+import android.view.KeyEvent+import android.view.MotionEvent+import java.lang.reflect.Method+import java.net.InetAddress+import java.net.ServerSocket+import java.net.Socket+import java.security.MessageDigest++/**+ * Elevated input: runs as the shell user (started through the phone's own+ * wireless debugging with `app_process`, like scrcpy's server) and injects+ * real touch, scroll and key events for the app. It listens on loopback+ * (apps may not connect to the shell's Unix sockets) and serves only a+ * client that first sends the random token it was started with, which only+ * the app knows. It ends when the shell that started it goes (the app went+ * away).+ *+ * Lines from the app, positions in pixels:+ * D x y / M x y / U x y finger down, moved, up+ * S x y dx dy wheel, in notches+ * K keycode a key, pressed and released+ * T text typed text (what the keyboard map can type)+ * C command... a shell command of a fixed set (see `allowed`)+ */+object InputServer {+ @JvmStatic+ fun main(args: Array<String>) {+ val token = args.getOrNull(0) ?: return fail("no token")+ val injector = runCatching { Injector() }.getOrElse { return fail("can't inject input: $it") }+ val server = runCatching { ServerSocket(0, 1, InetAddress.getLoopbackAddress()) }.getOrElse { return fail("can't listen: $it") }+ println("ready ${server.localPort}")+ System.out.flush()+ while (true) {+ val client = runCatching { server.accept() }.getOrNull() ?: continue+ Thread { serve(client, token, injector) }.start()+ }+ }++ private fun fail(why: String) {+ System.err.println(why)+ }++ private fun serve(client: Socket, token: String, injector: Injector) {+ client.use {+ client.tcpNoDelay = true+ client.soTimeout = 0+ val reader = client.inputStream.bufferedReader()+ // Compared in constant time: the token is the only key.+ val given = runCatching { reader.readLine() }.getOrNull() ?: return+ if (!MessageDigest.isEqual(given.toByteArray(), token.toByteArray())) return+ while (true) {+ val line = reader.readLine() ?: return+ runCatching { injector.handle(line) }.onFailure { System.err.println("input failed: $it") }+ }+ }+ }++ private class Injector {+ private val manager: Any+ private val inject: Method+ private var downTime = 0L++ init {+ // Android 14 moved injection to InputManagerGlobal.+ val global = runCatching { Class.forName("android.hardware.input.InputManagerGlobal") }.getOrNull()+ val owner = global ?: Class.forName("android.hardware.input.InputManager")+ manager = owner.getDeclaredMethod("getInstance").invoke(null)!!+ inject = owner.getMethod("injectInputEvent", InputEvent::class.java, Int::class.javaPrimitiveType)+ }++ fun handle(line: String) {+ val parts = line.split(' ', limit = 2)+ val rest = parts.getOrElse(1) { "" }+ when (parts[0]) {+ "D", "M", "U" -> {+ val (x, y) = rest.split(' ').map { it.toFloat() }+ touch(parts[0], x, y)+ }+ "S" -> {+ val (x, y, dx, dy) = rest.split(' ').map { it.toFloat() }+ scroll(x, y, dx, dy)+ }+ "K" -> key(rest.toInt())+ "T" -> type(rest)+ "C" -> if (rest in allowed) Runtime.getRuntime().exec(rest.split(' ').toTypedArray()).waitFor()+ }+ }++ private fun send(event: InputEvent) {+ // 0: asynchronous; the app doesn't wait for the target app.+ inject.invoke(manager, event, 0)+ }++ private fun touch(action: String, x: Float, y: Float) {+ val now = SystemClock.uptimeMillis()+ val kind = when (action) {+ "D" -> { downTime = now; MotionEvent.ACTION_DOWN }+ "M" -> MotionEvent.ACTION_MOVE+ else -> MotionEvent.ACTION_UP+ }+ val event = MotionEvent.obtain(downTime, now, kind, x, y, 0)+ event.source = InputDevice.SOURCE_TOUCHSCREEN+ send(event)+ event.recycle()+ }++ private fun scroll(x: Float, y: Float, dx: Float, dy: Float) {+ val now = SystemClock.uptimeMillis()+ val properties = arrayOf(MotionEvent.PointerProperties().apply { id = 0; toolType = MotionEvent.TOOL_TYPE_MOUSE })+ val coords = arrayOf(+ MotionEvent.PointerCoords().apply {+ this.x = x+ this.y = y+ // Wheel down is a negative vertical scroll on Android.+ setAxisValue(MotionEvent.AXIS_VSCROLL, -dy)+ setAxisValue(MotionEvent.AXIS_HSCROLL, dx)+ },+ )+ val event = MotionEvent.obtain(+ now, now, MotionEvent.ACTION_SCROLL, 1, properties, coords, 0, 0, 1f, 1f, 0, 0, InputDevice.SOURCE_MOUSE, 0,+ )+ send(event)+ event.recycle()+ }++ private fun key(code: Int) {+ val now = SystemClock.uptimeMillis()+ for (action in intArrayOf(KeyEvent.ACTION_DOWN, KeyEvent.ACTION_UP)) {+ send(KeyEvent(now, now, action, code, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0, InputDevice.SOURCE_KEYBOARD))+ }+ }++ private fun type(text: String) {+ val map = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD)+ // What the keyboard map can't type (most non-Latin letters, emoji) is skipped.+ for (c in text) {+ map.getEvents(charArrayOf(c))?.forEach { send(it) }+ }+ }++ /** The only shell commands the app may run through this. */+ private val allowed = setOf("cmd statusbar expand-notifications")+ }+}diff --git a/android/app/src/main/kotlin/app/nectarlink/android/elevated/PairingNotification.kt b/android/app/src/main/kotlin/app/nectarlink/android/elevated/PairingNotification.ktnew file mode 100644index 0000000..a94b1e3--- /dev/null+++ b/android/app/src/main/kotlin/app/nectarlink/android/elevated/PairingNotification.kt@@ -0,0 +1,92 @@+// SPDX-License-Identifier: GPL-3.0-or-later+package app.nectarlink.android.elevated++import android.Manifest+import android.app.NotificationChannel+import android.app.NotificationManager+import android.app.PendingIntent+import android.content.BroadcastReceiver+import android.content.Context+import android.content.Intent+import android.content.pm.PackageManager+import androidx.core.app.NotificationCompat+import androidx.core.app.NotificationManagerCompat+import androidx.core.app.RemoteInput+import androidx.core.content.ContextCompat+import app.nectarlink.android.R+import kotlinx.coroutines.CoroutineScope+import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.SupervisorJob+import kotlinx.coroutines.launch++/**+ * Where the user types wireless debugging's pairing code: Android closes+ * its pairing dialog when another app comes to the front, so the code is+ * typed into this notification's reply field instead, with the dialog+ * still open behind it.+ */+object PairingNotification {+ private const val CHANNEL = "elevated"+ private const val ID = 7+ private const val KEY_CODE = "code"+ private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)++ /** Asks for the code; false if notifications are off for the app. */+ fun show(context: Context, text: String = context.getString(R.string.elevated_pair_text)): Boolean {+ if (ContextCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {+ return false+ }+ context.getSystemService(NotificationManager::class.java).createNotificationChannel(+ NotificationChannel(CHANNEL, context.getString(R.string.channel_elevated), NotificationManager.IMPORTANCE_HIGH),+ )+ val reply = PendingIntent.getBroadcast(+ context, ID, Intent(context, Receiver::class.java),+ PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,+ )+ val action = NotificationCompat.Action.Builder(0, context.getString(R.string.elevated_pair_action), reply)+ .addRemoteInput(RemoteInput.Builder(KEY_CODE).setLabel(context.getString(R.string.elevated_pair_hint)).build())+ .build()+ val notification = NotificationCompat.Builder(context, CHANNEL)+ .setSmallIcon(R.drawable.ic_notification)+ .setContentTitle(context.getString(R.string.elevated_pair_title))+ .setContentText(text)+ .setStyle(NotificationCompat.BigTextStyle().bigText(text))+ .addAction(action)+ .setOnlyAlertOnce(true)+ .setPriority(NotificationCompat.PRIORITY_HIGH)+ .build()+ return runCatching { NotificationManagerCompat.from(context).notify(ID, notification) }.isSuccess+ }++ fun dismiss(context: Context) = NotificationManagerCompat.from(context).cancel(ID)++ /** The code the user typed. */+ class Receiver : BroadcastReceiver() {+ override fun onReceive(context: Context, intent: Intent) {+ val code = RemoteInput.getResultsFromIntent(intent)?.getCharSequence(KEY_CODE)?.toString()?.filter(Char::isDigit)+ val app = context.applicationContext+ if (code.isNullOrEmpty()) {+ show(app)+ return+ }+ show(app, app.getString(R.string.elevated_pairing))+ val pending = goAsync()+ scope.launch {+ try {+ if (Elevated.pair(code)) {+ dismiss(app)+ } else {+ val why = when ((Elevated.state.value as? Elevated.State.Failed)?.reason) {+ Elevated.Reason.NoPairingDialog -> R.string.elevated_no_dialog+ Elevated.Reason.WrongCode -> R.string.elevated_wrong_code+ else -> R.string.elevated_failed+ }+ show(app, app.getString(why))+ }+ } finally {+ pending.finish()+ }+ }+ }+ }+}diff --git a/android/app/src/main/kotlin/app/nectarlink/android/ui/MainActivity.kt b/android/app/src/main/kotlin/app/nectarlink/android/ui/MainActivity.ktindex f2ec942..67a2e0d 100644--- a/android/app/src/main/kotlin/app/nectarlink/android/ui/MainActivity.kt+++ b/android/app/src/main/kotlin/app/nectarlink/android/ui/MainActivity.kt@@ -47,13 +47,17 @@ import app.nectarlink.android.core.PairingStateimport app.nectarlink.android.service.ConnectionServiceimport app.nectarlink.android.ui.home.HomeScreenimport app.nectarlink.android.ui.pairing.PairingActions+import app.nectarlink.android.core.LocalNetworkimport app.nectarlink.android.ui.pairing.PairingScreenimport app.nectarlink.android.ui.settings.SettingsScreenimport app.nectarlink.android.ui.theme.NectarlinkThemeclass MainActivity : ComponentActivity() {- private val notificationPermission =- registerForActivityResult(ActivityResultContracts.RequestPermission()) { }+ private val permissions =+ registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) {+ // The local network may have just been allowed: reconnect now.+ (application as NectarlinkApplication).core.refreshNotificationAccess()+ }override fun onCreate(savedInstanceState: Bundle?) {enableEdgeToEdge()@@ -61,9 +65,13 @@ class MainActivity : ComponentActivity() {val core = (application as NectarlinkApplication).coreval preferences = Preferences(this)- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {- notificationPermission.launch(Manifest.permission.POST_NOTIFICATIONS)- }+ // Notifications (transfers, calls, requests from PCs) and, on Android+ // 17, the local network, without which no PC can be reached.+ val wanted = buildList {+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) add(Manifest.permission.POST_NOTIFICATIONS)+ if (LocalNetwork.needed()) add(LocalNetwork.PERMISSION)+ }.filter { checkSelfPermission(it) != android.content.pm.PackageManager.PERMISSION_GRANTED }+ if (wanted.isNotEmpty()) permissions.launch(wanted.toTypedArray())if (savedInstanceState == null) handlePairingLink(intent)setContent {diff --git a/android/app/src/main/kotlin/app/nectarlink/android/ui/home/HomeScreen.kt b/android/app/src/main/kotlin/app/nectarlink/android/ui/home/HomeScreen.ktindex 43e6cac..49e5e9f 100644--- a/android/app/src/main/kotlin/app/nectarlink/android/ui/home/HomeScreen.kt+++ b/android/app/src/main/kotlin/app/nectarlink/android/ui/home/HomeScreen.kt@@ -50,6 +50,7 @@ import app.nectarlink.android.update.AppUpdaterimport app.nectarlink.android.update.UpdateCardimport app.nectarlink.android.clipboard.SendActivityimport app.nectarlink.android.core.BackgroundAccess+import app.nectarlink.android.core.LocalNetworkimport app.nectarlink.android.core.CoreStateimport app.nectarlink.android.core.Deviceimport app.nectarlink.android.files.transferTitle@@ -123,6 +124,15 @@ fun HomeScreen(item { RingingBanner(from, onStopRinging) }}item { UpdateCard(updater) }+ if (!state.localNetwork) {+ item {+ val ask = rememberLauncherForActivityResult(ActivityResultContracts.RequestPermission()) { onAccessChanged() }+ SetupCard(+ stringResource(R.string.local_network_title),+ stringResource(R.string.local_network_text),+ ) { ask.launch(LocalNetwork.PERMISSION) }+ }+ }if (state.devices.isNotEmpty() && !state.backgroundUnrestricted) {item {val context = LocalContext.currentdiff --git a/android/app/src/main/kotlin/app/nectarlink/android/ui/settings/SettingsScreen.kt b/android/app/src/main/kotlin/app/nectarlink/android/ui/settings/SettingsScreen.ktindex a26b4a0..66ebf2a 100644--- a/android/app/src/main/kotlin/app/nectarlink/android/ui/settings/SettingsScreen.kt+++ b/android/app/src/main/kotlin/app/nectarlink/android/ui/settings/SettingsScreen.kt@@ -30,6 +30,9 @@ import androidx.compose.material3.Switchimport androidx.compose.material3.Textimport androidx.compose.material3.TextButtonimport androidx.compose.runtime.Composable+import androidx.compose.runtime.collectAsState+import androidx.compose.runtime.rememberCoroutineScope+import kotlinx.coroutines.launchimport androidx.compose.runtime.getValueimport androidx.compose.runtime.mutableStateOfimport androidx.compose.runtime.remember@@ -43,6 +46,8 @@ import androidx.compose.ui.semantics.semanticsimport androidx.compose.ui.unit.dpimport app.nectarlink.android.BuildConfigimport app.nectarlink.android.R+import app.nectarlink.android.elevated.Elevated+import app.nectarlink.android.elevated.PairingNotificationimport app.nectarlink.android.mirror.InputServiceimport app.nectarlink.android.update.AppUpdaterimport app.nectarlink.android.update.CheckForUpdates@@ -82,6 +87,7 @@ fun SettingsScreen(Section(stringResource(R.string.settings_control)) {ControlAccess(state.inputAccess)+ ElevatedAccess()}Section(stringResource(R.string.settings_appearance)) {@@ -247,6 +253,66 @@ private fun ControlAccess(on: Boolean) {}}+/** Elevated through the phone's own wireless debugging: real touch and keys. */+@Composable+private fun ElevatedAccess() {+ val context = LocalContext.current+ val scope = rememberCoroutineScope()+ val state by Elevated.state.collectAsState()+ var steps by remember { mutableStateOf(false) }+ Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth()) {+ Column(Modifier.weight(1f).padding(end = 12.dp)) {+ Text(stringResource(R.string.elevated_title), style = MaterialTheme.typography.titleSmall)+ Text(+ stringResource(+ when (state) {+ Elevated.State.NotSetUp -> R.string.elevated_not_set_up+ Elevated.State.Waiting -> R.string.elevated_waiting+ Elevated.State.Starting -> R.string.elevated_starting+ Elevated.State.Running -> R.string.elevated_running+ is Elevated.State.Failed ->+ if ((state as Elevated.State.Failed).reason == Elevated.Reason.NotPaired) R.string.elevated_not_paired+ else R.string.elevated_failed+ },+ ),+ style = MaterialTheme.typography.bodySmall,+ color = MaterialTheme.colorScheme.onSurfaceVariant,+ )+ }+ when (state) {+ Elevated.State.NotSetUp, is Elevated.State.Failed -> Button(onClick = { steps = true }) {+ Text(stringResource(R.string.elevated_set_up))+ }+ Elevated.State.Waiting -> OutlinedButton(onClick = { scope.launch { Elevated.start() } }) {+ Text(stringResource(R.string.elevated_try_again))+ }+ Elevated.State.Running -> OutlinedButton(onClick = { scope.launch { Elevated.forget() } }) {+ Text(stringResource(R.string.elevated_turn_off))+ }+ Elevated.State.Starting -> {}+ }+ }+ if (steps) {+ AlertDialog(+ onDismissRequest = { steps = false },+ title = { Text(stringResource(R.string.elevated_steps_title)) },+ text = { Text(stringResource(R.string.elevated_steps)) },+ confirmButton = {+ TextButton(onClick = {+ steps = false+ PairingNotification.show(context)+ context.startActivity(+ Intent(Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK),+ )+ }) { Text(stringResource(R.string.elevated_open_developer)) }+ },+ dismissButton = {+ TextButton(onClick = { steps = false }) { Text(stringResource(R.string.action_cancel)) }+ },+ )+ }+}+@Composableprivate fun Section(title: String, content: @Composable () -> Unit) {Text(diff --git a/android/app/src/main/res/values/strings.xml b/android/app/src/main/res/values/strings.xmlindex d78435e..88a245a 100644--- a/android/app/src/main/res/values/strings.xml+++ b/android/app/src/main/res/values/strings.xml@@ -73,6 +73,29 @@<string name="control_title">Use your PC\'s mouse and keyboard</string><string name="control_on">On: while your screen is shown on your PC, you can tap, swipe and type from there.</string><string name="control_off">While your screen is shown on your PC, tap, swipe and type from there. Turn on Nectarlink in Accessibility.</string>+ <string name="local_network_title">Reach your PC on this network</string>+ <string name="local_network_text">Android asks before an app talks to devices on your network. Nectarlink needs it to reach your PC.</string>+ <string name="channel_elevated">Wireless debugging setup</string>+ <string name="elevated_title">Real touch and keys (Wireless debugging)</string>+ <string name="elevated_not_set_up">Drag in real time and use every key. Uses the phone\'s own Wireless debugging; nothing else to install.</string>+ <string name="elevated_waiting">Set up. Waiting for Wireless debugging: turn it on in Developer options (Nectarlink turns it on by itself after the first time).</string>+ <string name="elevated_starting">Starting…</string>+ <string name="elevated_running">On: your PC\'s mouse and keyboard work like a real touchscreen and keyboard.</string>+ <string name="elevated_failed">Couldn\'t start it. Check that Wireless debugging is on, then try again.</string>+ <string name="elevated_not_paired">The phone forgot Nectarlink\'s pairing. Set it up again.</string>+ <string name="elevated_set_up">Set up</string>+ <string name="elevated_turn_off">Turn off</string>+ <string name="elevated_try_again">Try again</string>+ <string name="elevated_steps_title">Set up Wireless debugging</string>+ <string name="elevated_steps">1. Turn on Developer options: in Settings › About phone, tap Build number 7 times.\n2. In Developer options, turn on Wireless debugging (on Wi-Fi), open it and tap Pair device with pairing code.\n3. Type the 6-digit code into Nectarlink\'s notification, without closing the code.</string>+ <string name="elevated_open_developer">Open Developer options</string>+ <string name="elevated_pair_title">Pair Nectarlink with Wireless debugging</string>+ <string name="elevated_pair_text">In Wireless debugging, tap Pair device with pairing code, then type the code here.</string>+ <string name="elevated_pair_action">Enter code</string>+ <string name="elevated_pair_hint">6-digit pairing code</string>+ <string name="elevated_pairing">Pairing…</string>+ <string name="elevated_no_dialog">The pairing code isn\'t showing. In Wireless debugging, tap Pair device with pairing code, then type it here.</string>+ <string name="elevated_wrong_code">That code didn\'t work. Check it and type it again.</string><string name="channel_mirroring">Screen sharing</string><string name="channel_mirror_requests">Screen sharing requests</string><string name="mirror_request_title">%1$s wants to see your screen</string>diff --git a/android/app/src/test/kotlin/app/nectarlink/android/elevated/AdbKeyTest.kt b/android/app/src/test/kotlin/app/nectarlink/android/elevated/AdbKeyTest.ktnew file mode 100644index 0000000..8fa3090--- /dev/null+++ b/android/app/src/test/kotlin/app/nectarlink/android/elevated/AdbKeyTest.kt@@ -0,0 +1,41 @@+// SPDX-License-Identifier: GPL-3.0-or-later+package app.nectarlink.android.elevated++import org.junit.Assert.assertArrayEquals+import org.junit.Assert.assertEquals+import org.junit.Rule+import org.junit.Test+import org.junit.rules.TemporaryFolder+import java.security.KeyPairGenerator+import java.security.cert.CertificateFactory+import java.security.cert.X509Certificate++class AdbKeyTest {+ @get:Rule val folder = TemporaryFolder()++ @Test+ fun theCertificateIsValidAndSelfSigned() {+ val pair = KeyPairGenerator.getInstance("RSA").apply { initialize(2048) }.generateKeyPair()+ val der = AdbKey.selfSignedCertificate(pair, "CN=Nectarlink")+ val cert = CertificateFactory.getInstance("X.509").generateCertificate(der.inputStream()) as X509Certificate+ assertEquals(3, cert.version)+ assertEquals("CN=Nectarlink", cert.subjectX500Principal.name)+ assertEquals(cert.subjectX500Principal, cert.issuerX500Principal)+ assertArrayEquals(pair.public.encoded, cert.publicKey.encoded)+ cert.checkValidity()+ cert.verify(pair.public) // Signed by its own key.+ assertEquals("SHA256withRSA", cert.sigAlgName)+ }++ @Test+ fun theKeyIsKept() {+ val dir = folder.newFolder()+ val first = AdbKey.load(dir)+ val again = AdbKey.load(dir)+ assertArrayEquals(first.privateKey.encoded, again.privateKey.encoded)+ assertArrayEquals(first.certificate.encoded, again.certificate.encoded)+ AdbKey.delete(dir)+ val fresh = AdbKey.load(dir)+ assert(!fresh.privateKey.encoded.contentEquals(first.privateKey.encoded))+ }+}diff --git a/android/gradle/libs.versions.toml b/android/gradle/libs.versions.tomlindex 7f05cd1..d9bb922 100644--- a/android/gradle/libs.versions.toml+++ b/android/gradle/libs.versions.toml@@ -13,6 +13,8 @@ jna = "5.19.1"junit = "4.13.2"[libraries]+libadb-android = { module = "com.github.MuntashirAkon:libadb-android", version = "3.1.1" }+conscrypt-android = { module = "org.conscrypt:conscrypt-android", version = "2.5.3" }androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "core-ktx" }androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "activity-compose" }androidx-lifecycle-runtime-compose = { module = "androidx.lifecycle:lifecycle-runtime-compose", version.ref = "lifecycle" }diff --git a/android/settings.gradle.kts b/android/settings.gradle.ktsindex dc12acb..02319c4 100644--- a/android/settings.gradle.kts+++ b/android/settings.gradle.kts@@ -12,6 +12,17 @@ dependencyResolutionManagement {repositories {google()mavenCentral()+ // libadb-android (wireless debugging, for Elevated; GPL-3.0-or-later+ // or Apache-2.0) is published only there; nothing else may come+ // from it.+ exclusiveContent {+ forRepository { maven("https://jitpack.io") }+ filter {+ includeModule("com.github.MuntashirAkon", "libadb-android")+ // Its SPAKE2 (the pairing code's key exchange), LGPL-3.0.+ includeGroup("com.github.MuntashirAkon.spake2-java")+ }+ }}}diff --git a/docs/protocol/mirror.md b/docs/protocol/mirror.mdindex b4f8bc8..6dd6c81 100644--- a/docs/protocol/mirror.md+++ b/docs/protocol/mirror.md@@ -77,14 +77,18 @@ only to phones that offer `mirror.input`. Positions are fractions of thescreen as the phone shows it now (0 at the left or top, 1 at the right orbottom), so they hold at any size and rotation.-- `touch`: one finger. At Assist, Android plays a gesture whole, so the- phone turns each down–up into a tap (barely moved), a long press (held- 450 ms or more) or a swipe along the way it went, when it comes up.+- `touch`: one finger. At Elevated, the phone injects each down, move and+ up as it comes, so dragging is live. At Assist, Android plays a gesture+ whole, so the phone turns each down–up into a tap (barely moved), a long+ press (held 450 ms or more) or a swipe along the way it went, when it+ comes up.- `scroll`: the mouse wheel at a point, in notches (positive: down orright); the phone swipes the other way.- `key`: `back`, `home`, `recents`, `notifications`, `enter`, `backspace`,`delete`, `left`, `right`, `up`, `down` or `tab`. Others are dropped.-- `text`: typed text (at most 4 KiB), into the focused text field.+- `text`: typed text (at most 4 KiB), into the focused text field. At+ Elevated it's typed as key events, so only what the keyboard map can type+ (most Latin text) arrives.The Windows app maps the left button to a finger, right-click to Back,middle-click to Home and Ctrl+V to typing the PC's clipboard.