Adapt mesh views to phone rotation and toggle the exit from Quick Settings
Use the available width for the Android device table, keeping individual peers expandable and their full details reachable in both orientations. Show network selection and settings side by side in landscape and reuse the VIP editor with a compact toolbar and width-aware backend columns. Retain the selected network, expanded peer, traffic unit and VIP draft across rotation. Show data-channel readiness consistently in the tray and on Android. Distinguish an establishing channel, a disabled data plane, an offline peer and a transport failure. Use an animated arc for visible waiting peers. Explain Phosphor controls on hover, focus and long press, and provide a masked-by-default reveal button in the shared secret field. Store the last confirmed exit network and endpoint in Android's private preferences. A Quick Settings tap disconnects that selection or restores the exact peer through the existing agent. First use opens the chooser. On a cold start, wait for an authenticated exit offer outside the command loop. Check the current selection again before changing the route. Only changed exit offers wake roster subscribers. Wire formats, software version and mesh identity storage remain compatible. On macOS arm64, cargo fmt and strict workspace Clippy passed. The locked workspace suite passed 598 tests with four opt-in tests ignored. Built the tray, the ARM64 debug APK and its test APK. All three instrumentation tests passed on an Android API 37 emulator. Opened the chooser from the actual Quick Settings tile, rotated the activity and inspected rendered device lists with ready, waiting, failed and offline peers. Physical phones and native Linux and Windows builds were not exercised.
This commit is contained in:
1 parent
50c0ecea45
commit
e3e6089d0b
29 files changed
+1688
-324
No files matched your search
@@ -0,0 +1,33 @@
|
||||
package cy.hexor.tsunagi
|
||||
|
||||
import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import androidx.test.platform.app.InstrumentationRegistry
|
||||
import org.junit.Test
|
||||
|
||||
class ExitTileTest {
|
||||
@Test fun tileKeepsTheExactLastSelectionOnlyAfterTheAgentConfirmsIt() {
|
||||
val context = InstrumentationRegistry.getInstrumentation().targetContext
|
||||
val info = context.packageManager.getServiceInfo(ComponentName(context, ExitTile::class.java), 0)
|
||||
check(info.permission == "android.permission.BIND_QUICK_SETTINGS_TILE")
|
||||
check(!context.packageManager.getActivityInfo(ExitTile.entry(context), 0).exported)
|
||||
check(Native.tileExit(0, "network", "peer", true, false) == "Tsunagi Agent stopped")
|
||||
val isolated = context.createDeviceProtectedStorageContext()
|
||||
val settings = isolated.getSharedPreferences("exit_tile", Context.MODE_PRIVATE)
|
||||
settings.edit().clear().commit()
|
||||
val first = ExitTile.Target("network-a", "peer", "Gateway")
|
||||
val last = ExitTile.Target("network-b", "peer", "Gateway")
|
||||
try {
|
||||
check(ExitTile.last(isolated) == null)
|
||||
ExitTile.publish(isolated, 1L, first, true)
|
||||
ExitTile.publish(isolated, 1L, last, true)
|
||||
ExitTile.publish(isolated, 1L, null, false)
|
||||
check(ExitTile.last(isolated) == last)
|
||||
ExitTile.reset(isolated)
|
||||
check(ExitTile.last(isolated) == last)
|
||||
} finally {
|
||||
ExitTile.reset(isolated)
|
||||
settings.edit().clear().commit()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,10 +16,18 @@
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<activity-alias android:name=".OpenExit" android:targetActivity=".MainActivity" android:exported="false" />
|
||||
<service android:name=".MeshVpn" android:permission="android.permission.BIND_VPN_SERVICE"
|
||||
android:foregroundServiceType="systemExempted" android:exported="false">
|
||||
<intent-filter><action android:name="android.net.VpnService" /></intent-filter>
|
||||
<meta-data android:name="android.net.VpnService.SUPPORTS_ALWAYS_ON" android:value="false" />
|
||||
</service>
|
||||
<service android:name=".ExitTile" android:exported="true"
|
||||
android:label="Tsunagi Exit" android:icon="@drawable/exit_tile"
|
||||
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
|
||||
<intent-filter><action android:name="android.service.quicksettings.action.QS_TILE" /></intent-filter>
|
||||
<meta-data android:name="android.service.quicksettings.ACTIVE_TILE" android:value="true" />
|
||||
<meta-data android:name="android.service.quicksettings.TOGGLEABLE_TILE" android:value="true" />
|
||||
</service>
|
||||
</application>
|
||||
</manifest>
|
||||
@@ -0,0 +1,113 @@
|
||||
package cy.hexor.tsunagi
|
||||
|
||||
import android.app.PendingIntent
|
||||
import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.service.quicksettings.Tile
|
||||
import android.service.quicksettings.TileService
|
||||
import android.widget.Toast
|
||||
|
||||
/** Quick Settings changes the internet exit of the existing mesh agent. */
|
||||
class ExitTile : TileService() {
|
||||
internal data class Target(val network: String, val peer: String, val name: String)
|
||||
private data class Snapshot(val session: Long, val active: Target?, val online: Boolean)
|
||||
|
||||
companion object {
|
||||
internal fun entry(context: Context) = ComponentName(context, "${context.packageName}.OpenExit")
|
||||
private var live = Snapshot(0, null, false)
|
||||
private var pending = false
|
||||
private val main = Handler(Looper.getMainLooper())
|
||||
|
||||
internal fun last(context: Context): Target? {
|
||||
val prefs = context.getSharedPreferences("exit_tile", Context.MODE_PRIVATE)
|
||||
val network = prefs.getString("network", null) ?: return null
|
||||
val peer = prefs.getString("peer", null) ?: return null
|
||||
return Target(network, peer, prefs.getString("name", null) ?: peer.take(12))
|
||||
}
|
||||
private fun refresh(context: Context) {
|
||||
requestListeningState(context, ComponentName(context, ExitTile::class.java))
|
||||
}
|
||||
internal fun publish(context: Context, session: Long, active: Target?, online: Boolean) {
|
||||
// Only confirmed agent status updates the last selection. Clearing an exit or
|
||||
// stopping the process leaves the exact network and peer available for reuse.
|
||||
if (active != null && last(context) != active) {
|
||||
context.getSharedPreferences("exit_tile", Context.MODE_PRIVATE).edit()
|
||||
.putString("network", active.network).putString("peer", active.peer)
|
||||
.putString("name", active.name).apply()
|
||||
}
|
||||
live = Snapshot(session, active, online)
|
||||
refresh(context)
|
||||
}
|
||||
internal fun reset(context: Context) {
|
||||
live = Snapshot(0, null, false)
|
||||
refresh(context)
|
||||
}
|
||||
internal fun restoreIfReady(context: Context, target: Target): Boolean {
|
||||
if (live.session == 0L) return false
|
||||
if (live.active == null) change(context, live.session, target, true, waitForDiscovery = true)
|
||||
return true
|
||||
}
|
||||
private fun change(context: Context, session: Long, target: Target, connect: Boolean, waitForDiscovery: Boolean = false) {
|
||||
if (pending) return
|
||||
pending = true
|
||||
refresh(context)
|
||||
val app = context.applicationContext
|
||||
Thread({
|
||||
val error = Native.tileExit(session, target.network, target.peer, connect, waitForDiscovery)
|
||||
main.post {
|
||||
pending = false
|
||||
if (error != null) Toast.makeText(app, error, Toast.LENGTH_LONG).show()
|
||||
refresh(app)
|
||||
}
|
||||
}, "tsunagi-exit-tile").start()
|
||||
}
|
||||
}
|
||||
|
||||
private var displayed = live
|
||||
private var remembered: Target? = null
|
||||
|
||||
override fun onStartListening() {
|
||||
super.onStartListening()
|
||||
displayed = live
|
||||
remembered = last(this)
|
||||
val tile = qsTile ?: return
|
||||
val active = displayed.active
|
||||
val peer = active ?: remembered
|
||||
val subtitle = when {
|
||||
pending -> "Updating…"
|
||||
active != null && !displayed.online -> "${active.name} · offline"
|
||||
peer != null -> peer.name
|
||||
else -> "Choose exit node"
|
||||
}
|
||||
tile.state = when {
|
||||
pending -> Tile.STATE_UNAVAILABLE
|
||||
active != null -> Tile.STATE_ACTIVE
|
||||
else -> Tile.STATE_INACTIVE
|
||||
}
|
||||
tile.label = if (Build.VERSION.SDK_INT < 29 && peer != null) "Exit · ${peer.name}" else "Tsunagi Exit"
|
||||
if (Build.VERSION.SDK_INT >= 29) tile.subtitle = subtitle
|
||||
tile.contentDescription = "Tsunagi exit node, $subtitle"
|
||||
tile.updateTile()
|
||||
}
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
override fun onClick() {
|
||||
super.onClick()
|
||||
if (pending || displayed != live) { onStartListening(); return }
|
||||
val target = displayed.active ?: remembered
|
||||
if (displayed.session != 0L && target != null) {
|
||||
change(this, displayed.session, target, displayed.active == null)
|
||||
return
|
||||
}
|
||||
// Consent, a stopped service, or the first selection needs an activity.
|
||||
val open = Intent().setComponent(entry(this))
|
||||
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP)
|
||||
if (Build.VERSION.SDK_INT >= 34) {
|
||||
startActivityAndCollapse(PendingIntent.getActivity(this, 2, open, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT))
|
||||
} else startActivityAndCollapse(open)
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ class MainActivity : Activity() {
|
||||
private val settings by lazy { getSharedPreferences("agent", MODE_PRIVATE) }
|
||||
private var checkedAutoStart = false
|
||||
private var consentPending = false
|
||||
private var tileRequested = false
|
||||
private var askedForNotifications = false
|
||||
internal fun requestExitNotifications() {
|
||||
if (Build.VERSION.SDK_INT >= 33 && !askedForNotifications &&
|
||||
@@ -24,6 +25,7 @@ class MainActivity : Activity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
consentPending = savedInstanceState?.getBoolean("consent_pending") == true
|
||||
tileRequested = savedInstanceState?.getBoolean("exit_tile") ?: (intent.component == ExitTile.entry(this))
|
||||
surface = MeshSurface(this)
|
||||
val root = FrameLayout(this)
|
||||
if (Build.VERSION.SDK_INT >= 30) {
|
||||
@@ -39,6 +41,11 @@ class MainActivity : Activity() {
|
||||
}
|
||||
override fun onPostResume() {
|
||||
super.onPostResume()
|
||||
if (tileRequested && !consentPending) {
|
||||
checkedAutoStart = true
|
||||
requestAgent()
|
||||
return
|
||||
}
|
||||
if (checkedAutoStart) return
|
||||
checkedAutoStart = true
|
||||
if (!MeshVpn.started && settings.getBoolean("auto_start", true)) requestAgent()
|
||||
@@ -46,6 +53,11 @@ class MainActivity : Activity() {
|
||||
override fun onSaveInstanceState(outState: Bundle) {
|
||||
super.onSaveInstanceState(outState)
|
||||
outState.putBoolean("consent_pending", consentPending)
|
||||
outState.putBoolean("exit_tile", tileRequested)
|
||||
}
|
||||
override fun onNewIntent(intent: Intent) {
|
||||
super.onNewIntent(intent)
|
||||
if (intent.component == ExitTile.entry(this)) tileRequested = true
|
||||
}
|
||||
internal fun requestAgent() {
|
||||
if (consentPending) return
|
||||
@@ -58,6 +70,11 @@ class MainActivity : Activity() {
|
||||
private fun startAgent() {
|
||||
settings.edit().putBoolean("auto_start", true).apply()
|
||||
val service = Intent(this, MeshVpn::class.java)
|
||||
if (tileRequested) {
|
||||
tileRequested = false
|
||||
if (ExitTile.last(this) != null) service.action = MeshVpn.RESTORE_EXIT
|
||||
else surface.openExitPicker()
|
||||
}
|
||||
// A retry of an existing service may already have left foreground mode.
|
||||
if (MeshVpn.started) startService(service) else startForegroundService(service)
|
||||
}
|
||||
@@ -71,6 +88,7 @@ class MainActivity : Activity() {
|
||||
if (request == 1) {
|
||||
consentPending = false
|
||||
if (result == RESULT_OK) startAgent() else {
|
||||
tileRequested = false
|
||||
settings.edit().putBoolean("auto_start", false).apply()
|
||||
surface.input(5, "Android VPN permission was declined")
|
||||
}
|
||||
|
||||
@@ -23,6 +23,8 @@ class MeshSurface(private val activity: MainActivity) : GLSurfaceView(activity),
|
||||
private var widthPx = 0
|
||||
private var heightPx = 0
|
||||
private val repaint = Runnable { requestRender() }
|
||||
private val pickExit = java.util.concurrent.atomic.AtomicBoolean()
|
||||
fun openExitPicker() { pickExit.set(true); requestRender() }
|
||||
init {
|
||||
setEGLContextClientVersion(3)
|
||||
preserveEGLContextOnPause = true
|
||||
@@ -34,7 +36,9 @@ class MeshSurface(private val activity: MainActivity) : GLSurfaceView(activity),
|
||||
override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) = Native.surface()
|
||||
override fun onSurfaceChanged(gl: GL10?, w: Int, h: Int) { widthPx = w; heightPx = h }
|
||||
override fun onDrawFrame(gl: GL10?) {
|
||||
val delay = Native.draw(this, widthPx, heightPx, density, MeshVpn.started)
|
||||
if (pickExit.getAndSet(false)) Native.input(6, "")
|
||||
val landscape = resources.configuration.orientation == android.content.res.Configuration.ORIENTATION_LANDSCAPE
|
||||
val delay = Native.draw(this, widthPx, heightPx, density, MeshVpn.started, landscape)
|
||||
removeCallbacks(repaint)
|
||||
postDelayed(repaint, delay.coerceAtLeast(16))
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import java.util.concurrent.TimeUnit
|
||||
class MeshVpn : VpnService() {
|
||||
companion object {
|
||||
const val STOP_VPN = "cy.hexor.tsunagi.STOP_VPN"
|
||||
const val RESTORE_EXIT = "cy.hexor.tsunagi.RESTORE_EXIT"
|
||||
private const val DISCONNECT_EXIT = "cy.hexor.tsunagi.DISCONNECT_EXIT"
|
||||
private const val NOTIFICATION_ID = 1
|
||||
private const val EXIT_CHANNEL = "exit-node"
|
||||
@@ -34,6 +35,7 @@ class MeshVpn : VpnService() {
|
||||
private val notifications by lazy { getSystemService(NotificationManager::class.java) }
|
||||
@Volatile private var instance = 0L
|
||||
private var closed = false
|
||||
private var restore: ExitTile.Target? = null
|
||||
private val connectivity by lazy { getSystemService(ConnectivityManager::class.java) }
|
||||
@Volatile private var upstream: Network? = null
|
||||
private val changes = object : ConnectivityManager.NetworkCallback() {
|
||||
@@ -76,6 +78,10 @@ class MeshVpn : VpnService() {
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
when (intent?.action) {
|
||||
STOP_VPN -> { shutdown(); stopSelf(startId); return START_NOT_STICKY }
|
||||
RESTORE_EXIT -> {
|
||||
restore = ExitTile.last(this)
|
||||
restore?.let { if (ExitTile.restoreIfReady(this, it)) restore = null }
|
||||
}
|
||||
DISCONNECT_EXIT -> {
|
||||
val ids = intent.data?.pathSegments.orEmpty()
|
||||
if (started && ids.size == 2 && intent.getLongExtra("instance", 0) == instance) {
|
||||
@@ -118,9 +124,12 @@ class MeshVpn : VpnService() {
|
||||
// Rust publishes a change even when the activity is closed. Android holds its
|
||||
// own service binding for an established TUN, so mesh-only mode can leave
|
||||
// foreground promotion and remove the app's notification.
|
||||
fun updateExitNotification(session: Long, network: String, peer: String, title: String, text: String) {
|
||||
fun updateExitNotification(session: Long, network: String, peer: String, title: String, text: String, name: String, online: Boolean) {
|
||||
main.post {
|
||||
if (closed || session != instance) return@post
|
||||
val selected = if (peer.isEmpty()) null else ExitTile.Target(network, peer, name)
|
||||
ExitTile.publish(this, session, selected, online)
|
||||
restore?.let { if (ExitTile.restoreIfReady(this, it)) restore = null }
|
||||
if (peer.isEmpty()) {
|
||||
stopForeground(STOP_FOREGROUND_REMOVE)
|
||||
return@post
|
||||
@@ -191,6 +200,8 @@ class MeshVpn : VpnService() {
|
||||
@Synchronized private fun shutdown() {
|
||||
if (closed) return
|
||||
closed = true
|
||||
restore = null
|
||||
ExitTile.reset(this)
|
||||
if (started) connectivity.unregisterNetworkCallback(changes)
|
||||
started = false
|
||||
if (instance != 0L) Native.stop(instance)
|
||||
|
||||
@@ -10,10 +10,11 @@ internal object Native {
|
||||
external fun failed(instance: Long): Boolean
|
||||
external fun stop(instance: Long)
|
||||
external fun disconnectExit(instance: Long, network: String, peer: String): Boolean
|
||||
external fun tileExit(instance: Long, network: String, peer: String, connect: Boolean, waitForDiscovery: Boolean): String?
|
||||
external fun networkChanged(instance: Long, addresses: String)
|
||||
|
||||
external fun surface()
|
||||
external fun draw(host: MeshSurface, width: Int, height: Int, density: Float, serviceStarted: Boolean): Long
|
||||
external fun draw(host: MeshSurface, width: Int, height: Int, density: Float, serviceStarted: Boolean, landscape: Boolean): Long
|
||||
external fun touch(action: Int, id: Int, x: Float, y: Float)
|
||||
external fun input(kind: Int, value: String)
|
||||
external fun key(key: Int)
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp" android:height="24dp" android:viewportWidth="24" android:viewportHeight="24">
|
||||
<path android:strokeColor="#ffffff" android:strokeWidth="2"
|
||||
android:strokeLineCap="round" android:strokeLineJoin="round"
|
||||
android:pathData="M12,3H4v18h8 M9,12h12 M17,8l4,4l-4,4" />
|
||||
</vector>
|
||||
Reference in new issue
Block a user