Add Wear OS companion app and remote controls
Add a Wearable Data Layer bridge for syncing dashboard state and commands. Introduce a standalone watch app with assistant, app launcher, touchpad remote, settings, and connectivity UI.
No files matched your search
@@ -0,0 +1,35 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<uses-feature android:name="android.hardware.type.watch" />
|
||||
|
||||
<uses-permission android:name="android.permission.VIBRATE" />
|
||||
|
||||
<!-- Not standalone: the watch app is a remote for Faceclaw on the phone
|
||||
and does nothing useful without it. -->
|
||||
<application
|
||||
android:allowBackup="false"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:theme="@style/Theme.Faceclaw">
|
||||
|
||||
<uses-library
|
||||
android:name="com.google.android.wearable"
|
||||
android:required="true" />
|
||||
|
||||
<meta-data
|
||||
android:name="com.google.android.wearable.standalone"
|
||||
android:value="false" />
|
||||
|
||||
<activity
|
||||
android:name=".MainActivity"
|
||||
android:exported="true"
|
||||
android:launchMode="singleTask"
|
||||
android:taskAffinity="">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
</manifest>
|
||||
@@ -0,0 +1,179 @@
|
||||
package com.faceclaw.wear
|
||||
|
||||
import android.content.Context
|
||||
import android.hardware.Sensor
|
||||
import android.hardware.SensorEvent
|
||||
import android.hardware.SensorEventListener
|
||||
import android.hardware.SensorManager
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.os.SystemClock
|
||||
import android.util.Log
|
||||
import kotlin.math.max
|
||||
import kotlin.math.sqrt
|
||||
|
||||
/** How hard a fingertip tap has to hit before it counts. */
|
||||
enum class TapSensitivity(val label: String, val threshold: Float, val maxActivity: Float) {
|
||||
/** Only firm taps; most resistant to false positives. */
|
||||
LOW("Low", 4.0f, 1.0f),
|
||||
NORMAL("Normal", 2.6f, 1.4f),
|
||||
/** Light taps; expect the odd stray click when moving about. */
|
||||
HIGH("High", 1.7f, 1.9f);
|
||||
|
||||
fun next(): TapSensitivity = entries[(ordinal + 1) % entries.size]
|
||||
}
|
||||
|
||||
/**
|
||||
* Fingertip ("tip tap") gestures: tapping the index finger against the
|
||||
* thumb sends a short, sharp acceleration impulse down the wrist. Wear OS
|
||||
* keeps its own pinch gesture to itself, so this reads the linear
|
||||
* acceleration sensor directly while the pad is showing and looks for brief
|
||||
* spikes above a quiet baseline: an impulse that rises past the threshold
|
||||
* and falls back within MAX_SPIKE_MS is a tap; anything longer is the arm
|
||||
* moving. Taps are suppressed while the baseline activity is high (walking,
|
||||
* gesturing), and a second tap inside the double-tap window makes a double.
|
||||
*
|
||||
* A heuristic, not a classifier: good enough for a deliberate tap with the
|
||||
* wrist held still, tunable through TapSensitivity, and off in a toggle.
|
||||
*/
|
||||
class FingerTapDetector(
|
||||
context: Context,
|
||||
private val sensitivity: () -> TapSensitivity,
|
||||
/** elapsedRealtime before which samples are ignored (the watch's own haptics). */
|
||||
private val quietUntil: () -> Long,
|
||||
/** False while the watch is off the wrist; taps then never fire. */
|
||||
private val onBody: () -> Boolean,
|
||||
private val onTap: () -> Unit,
|
||||
private val onDoubleTap: () -> Unit,
|
||||
) : SensorEventListener {
|
||||
private val sensorManager: SensorManager? = context.getSystemService(SensorManager::class.java)
|
||||
private val sensor: Sensor? = sensorManager?.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION)
|
||||
?: sensorManager?.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
|
||||
private val subtractGravity = sensor?.type == Sensor.TYPE_ACCELEROMETER
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
|
||||
private val gravity = floatArrayOf(0f, 0f, SensorManager.GRAVITY_EARTH)
|
||||
/** Slow average of the magnitude outside spikes: how much the arm is moving. */
|
||||
private var activity = 0f
|
||||
private var inSpike = false
|
||||
/** Last sample that counted as the arm moving; a tap needs stillness before it. */
|
||||
private var lastMotionNs = 0L
|
||||
private var spikeStartNs = 0L
|
||||
private var refractoryUntilNs = 0L
|
||||
private var lastTapNs = 0L
|
||||
private var pendingSingle: Runnable? = null
|
||||
private var running = false
|
||||
|
||||
val available: Boolean get() = sensor != null
|
||||
|
||||
/** Start listening; false when the watch has no usable accelerometer. */
|
||||
fun start(): Boolean {
|
||||
val manager = sensorManager ?: return false
|
||||
val target = sensor ?: return false
|
||||
if (running) return true
|
||||
running = manager.registerListener(this, target, SAMPLING_PERIOD_US, handler)
|
||||
Log.i(TAG, "tip-tap detector ${if (running) "started" else "failed to start"} on ${target.name}")
|
||||
return running
|
||||
}
|
||||
|
||||
fun stop() {
|
||||
if (!running) return
|
||||
running = false
|
||||
sensorManager?.unregisterListener(this)
|
||||
pendingSingle?.let { handler.removeCallbacks(it) }
|
||||
pendingSingle = null
|
||||
inSpike = false
|
||||
}
|
||||
|
||||
override fun onSensorChanged(event: SensorEvent) {
|
||||
var x = event.values[0]
|
||||
var y = event.values[1]
|
||||
var z = event.values[2]
|
||||
if (subtractGravity) {
|
||||
// Raw accelerometer fallback: strip a slowly tracked gravity vector.
|
||||
gravity[0] += (x - gravity[0]) * GRAVITY_ALPHA
|
||||
gravity[1] += (y - gravity[1]) * GRAVITY_ALPHA
|
||||
gravity[2] += (z - gravity[2]) * GRAVITY_ALPHA
|
||||
x -= gravity[0]
|
||||
y -= gravity[1]
|
||||
z -= gravity[2]
|
||||
}
|
||||
val magnitude = sqrt(x * x + y * y + z * z)
|
||||
val now = event.timestamp
|
||||
val level = sensitivity()
|
||||
|
||||
if (SystemClock.elapsedRealtime() < quietUntil() || !onBody()) {
|
||||
// Our own vibration motor (or a desk being bumped): not a finger.
|
||||
// Also forget any spike in progress so its tail can't complete it.
|
||||
inSpike = false
|
||||
refractoryUntilNs = now + REFRACTORY_NS
|
||||
return
|
||||
}
|
||||
|
||||
if (!inSpike) {
|
||||
val still = activity < level.maxActivity && now - lastMotionNs > STILLNESS_BEFORE_TAP_NS
|
||||
if (magnitude > level.threshold && still && now > refractoryUntilNs) {
|
||||
inSpike = true
|
||||
spikeStartNs = now
|
||||
} else {
|
||||
activity += (magnitude - activity) * ACTIVITY_ALPHA
|
||||
if (magnitude > level.maxActivity) lastMotionNs = now
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
val durationNs = now - spikeStartNs
|
||||
if (magnitude < level.threshold * SPIKE_END_FRACTION) {
|
||||
inSpike = false
|
||||
refractoryUntilNs = now + REFRACTORY_NS
|
||||
if (durationNs <= MAX_SPIKE_NS) {
|
||||
registerTap(now)
|
||||
} else {
|
||||
activity = max(activity, magnitude)
|
||||
lastMotionNs = now
|
||||
}
|
||||
} else if (durationNs > MAX_SPIKE_NS) {
|
||||
// Sustained acceleration is the arm moving, not a tap.
|
||||
inSpike = false
|
||||
refractoryUntilNs = now + REFRACTORY_NS
|
||||
activity += (magnitude - activity) * MOVEMENT_ALPHA
|
||||
lastMotionNs = now
|
||||
}
|
||||
}
|
||||
|
||||
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) = Unit
|
||||
|
||||
private fun registerTap(nowNs: Long) {
|
||||
val pending = pendingSingle
|
||||
if (pending != null && nowNs - lastTapNs <= DOUBLE_TAP_WINDOW_NS) {
|
||||
handler.removeCallbacks(pending)
|
||||
pendingSingle = null
|
||||
lastTapNs = 0L
|
||||
onDoubleTap()
|
||||
return
|
||||
}
|
||||
lastTapNs = nowNs
|
||||
val fire = Runnable {
|
||||
pendingSingle = null
|
||||
onTap()
|
||||
}
|
||||
pendingSingle = fire
|
||||
handler.postDelayed(fire, DOUBLE_TAP_WINDOW_MS)
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val TAG = "FaceclawWear"
|
||||
/** 100 Hz: under the 200 Hz line that needs HIGH_SAMPLING_RATE_SENSORS. */
|
||||
const val SAMPLING_PERIOD_US = 10_000
|
||||
const val MAX_SPIKE_NS = 90_000_000L
|
||||
/** The hand has to be still this long before an impulse can be a tap. */
|
||||
const val STILLNESS_BEFORE_TAP_NS = 250_000_000L
|
||||
const val REFRACTORY_NS = 140_000_000L
|
||||
const val DOUBLE_TAP_WINDOW_MS = 380L
|
||||
const val DOUBLE_TAP_WINDOW_NS = DOUBLE_TAP_WINDOW_MS * 1_000_000L
|
||||
const val SPIKE_END_FRACTION = 0.5f
|
||||
const val ACTIVITY_ALPHA = 0.04f
|
||||
const val MOVEMENT_ALPHA = 0.3f
|
||||
const val GRAVITY_ALPHA = 0.05f
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.faceclaw.wear
|
||||
|
||||
import android.content.Context
|
||||
import android.os.SystemClock
|
||||
import android.os.VibrationEffect
|
||||
import android.os.Vibrator
|
||||
|
||||
/** Short confirmations for sent gestures; each call is a no-op without a vibrator. */
|
||||
class Haptics(context: Context, private val enabled: () -> Boolean) {
|
||||
private val vibrator: Vibrator? = context.getSystemService(Vibrator::class.java)
|
||||
@Volatile private var lastVibrationAt = 0L
|
||||
|
||||
/**
|
||||
* elapsedRealtime until which the motion sensors will still be feeling
|
||||
* our own buzz. The tip-tap detector ignores that window, or a click's
|
||||
* confirmation would read as another fingertip tap.
|
||||
*/
|
||||
fun quietUntil(): Long = lastVibrationAt + VIBRATION_SETTLE_MS
|
||||
|
||||
/** A sent scroll step. */
|
||||
fun tick() = play(VibrationEffect.EFFECT_TICK)
|
||||
|
||||
/** A sent click / command. */
|
||||
fun click() = play(VibrationEffect.EFFECT_CLICK)
|
||||
|
||||
/** A sent double-click. */
|
||||
fun doubleClick() = play(VibrationEffect.EFFECT_DOUBLE_CLICK)
|
||||
|
||||
/** The start of a hold. */
|
||||
fun heavy() = play(VibrationEffect.EFFECT_HEAVY_CLICK)
|
||||
|
||||
/** The phone refused or did not answer; always plays. */
|
||||
fun error() {
|
||||
val v = vibrator ?: return
|
||||
if (!v.hasVibrator()) return
|
||||
lastVibrationAt = SystemClock.elapsedRealtime()
|
||||
v.vibrate(VibrationEffect.createWaveform(longArrayOf(0, 40, 60, 40), -1))
|
||||
}
|
||||
|
||||
private fun play(effect: Int) {
|
||||
if (!enabled()) return
|
||||
val v = vibrator ?: return
|
||||
if (!v.hasVibrator()) return
|
||||
lastVibrationAt = SystemClock.elapsedRealtime()
|
||||
v.vibrate(VibrationEffect.createPredefined(effect))
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val VIBRATION_SETTLE_MS = 400L
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package com.faceclaw.wear
|
||||
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.view.KeyEvent
|
||||
import android.view.WindowManager
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import kotlinx.coroutines.launch
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import com.faceclaw.wear.ui.FaceclawWearApp
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
private lateinit var link: PhoneLink
|
||||
private lateinit var prefs: WatchPrefs
|
||||
private lateinit var haptics: Haptics
|
||||
|
||||
// Side (stem) buttons: button 1 is click / hold, 2 double-click, 3 "Hey Even".
|
||||
private val stemHandler = Handler(Looper.getMainLooper())
|
||||
private var stemHoldRunnable: Runnable? = null
|
||||
private var stemHoldSent = false
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
link = PhoneLink(this)
|
||||
prefs = WatchPrefs(this)
|
||||
haptics = Haptics(this) { prefs.prefs.value.haptics }
|
||||
setContent {
|
||||
FaceclawWearApp(link = link, prefsStore = prefs, haptics = haptics)
|
||||
}
|
||||
// A remote that dozes mid-gesture is no remote: hold the display while
|
||||
// the app is in front (every screen, not just the pad), unless the
|
||||
// user turned "Keep screen on" off.
|
||||
lifecycleScope.launch {
|
||||
prefs.prefs.collect { current ->
|
||||
if (current.keepScreenOn) {
|
||||
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
|
||||
} else {
|
||||
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
link.start()
|
||||
}
|
||||
|
||||
override fun onStop() {
|
||||
cancelStemHold()
|
||||
link.stop()
|
||||
super.onStop()
|
||||
}
|
||||
|
||||
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
|
||||
when (keyCode) {
|
||||
KeyEvent.KEYCODE_STEM_1 -> {
|
||||
if (event.repeatCount == 0) {
|
||||
cancelStemHold()
|
||||
stemHoldSent = false
|
||||
val runnable = Runnable {
|
||||
stemHoldSent = true
|
||||
haptics.heavy()
|
||||
link.sendGesture(Gesture.LONG_PRESS_START)
|
||||
}
|
||||
stemHoldRunnable = runnable
|
||||
stemHandler.postDelayed(runnable, STEM_HOLD_MS)
|
||||
}
|
||||
return true
|
||||
}
|
||||
KeyEvent.KEYCODE_STEM_2 -> {
|
||||
if (event.repeatCount == 0) {
|
||||
haptics.doubleClick()
|
||||
link.sendGesture(Gesture.DOUBLE_CLICK)
|
||||
}
|
||||
return true
|
||||
}
|
||||
KeyEvent.KEYCODE_STEM_3 -> {
|
||||
if (event.repeatCount == 0) {
|
||||
haptics.click()
|
||||
link.sendGesture(Gesture.WAKEWORD)
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
return super.onKeyDown(keyCode, event)
|
||||
}
|
||||
|
||||
override fun onKeyUp(keyCode: Int, event: KeyEvent): Boolean {
|
||||
if (keyCode == KeyEvent.KEYCODE_STEM_1) {
|
||||
cancelStemHold()
|
||||
if (stemHoldSent) {
|
||||
stemHoldSent = false
|
||||
haptics.click()
|
||||
link.sendGesture(Gesture.LONG_PRESS_RELEASE)
|
||||
} else {
|
||||
haptics.click()
|
||||
link.sendGesture(Gesture.CLICK)
|
||||
}
|
||||
return true
|
||||
}
|
||||
if (keyCode == KeyEvent.KEYCODE_STEM_2 || keyCode == KeyEvent.KEYCODE_STEM_3) return true
|
||||
return super.onKeyUp(keyCode, event)
|
||||
}
|
||||
|
||||
private fun cancelStemHold() {
|
||||
stemHoldRunnable?.let { stemHandler.removeCallbacks(it) }
|
||||
stemHoldRunnable = null
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val STEM_HOLD_MS = 450L
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.faceclaw.wear
|
||||
|
||||
import android.content.Context
|
||||
import android.hardware.Sensor
|
||||
import android.hardware.SensorEvent
|
||||
import android.hardware.SensorEventListener
|
||||
import android.hardware.SensorManager
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.util.Log
|
||||
|
||||
/**
|
||||
* Whether the watch is on a wrist, from the low-latency off-body sensor.
|
||||
* The motion-sensor gestures (tip taps, wrist twists) only make sense on a
|
||||
* wrist: on a desk, a buzzing phone next to the watch reads as a fingertip
|
||||
* tap. Watches without the sensor are assumed worn.
|
||||
*/
|
||||
class OnBodyMonitor(context: Context) : SensorEventListener {
|
||||
private val sensorManager: SensorManager? = context.getSystemService(SensorManager::class.java)
|
||||
private val sensor: Sensor? = sensorManager?.getDefaultSensor(Sensor.TYPE_LOW_LATENCY_OFFBODY_DETECT)
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
private var running = false
|
||||
|
||||
@Volatile
|
||||
var onBody: Boolean = true
|
||||
private set
|
||||
|
||||
fun start() {
|
||||
val manager = sensorManager ?: return
|
||||
val target = sensor ?: return
|
||||
if (running) return
|
||||
running = manager.registerListener(this, target, SensorManager.SENSOR_DELAY_NORMAL, handler)
|
||||
}
|
||||
|
||||
fun stop() {
|
||||
if (!running) return
|
||||
running = false
|
||||
sensorManager?.unregisterListener(this)
|
||||
onBody = true
|
||||
}
|
||||
|
||||
override fun onSensorChanged(event: SensorEvent) {
|
||||
val worn = event.values.firstOrNull()?.let { it >= 0.5f } ?: true
|
||||
if (worn != onBody) {
|
||||
onBody = worn
|
||||
Log.i(TAG, if (worn) "watch on wrist: motion gestures enabled" else "watch off wrist: motion gestures paused")
|
||||
}
|
||||
}
|
||||
|
||||
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) = Unit
|
||||
|
||||
private companion object {
|
||||
const val TAG = "FaceclawWear"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,335 @@
|
||||
package com.faceclaw.wear
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.util.Log
|
||||
import com.google.android.gms.common.api.ApiException
|
||||
import com.google.android.gms.wearable.CapabilityClient
|
||||
import com.google.android.gms.wearable.CapabilityInfo
|
||||
import com.google.android.gms.wearable.DataClient
|
||||
import com.google.android.gms.wearable.DataEvent
|
||||
import com.google.android.gms.wearable.DataEventBuffer
|
||||
import com.google.android.gms.wearable.DataItem
|
||||
import com.google.android.gms.wearable.DataMapItem
|
||||
import com.google.android.gms.wearable.MessageClient
|
||||
import com.google.android.gms.wearable.MessageEvent
|
||||
import com.google.android.gms.wearable.Node
|
||||
import com.google.android.gms.wearable.Wearable
|
||||
import com.google.android.gms.wearable.WearableStatusCodes
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharedFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asSharedFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.tasks.await
|
||||
import org.json.JSONObject
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import java.util.concurrent.atomic.AtomicLong
|
||||
|
||||
/** What we know about the phone and the Faceclaw app on it. */
|
||||
data class LinkStatus(
|
||||
/** A phone is connected to the watch at all (any node). */
|
||||
val phoneReachable: Boolean = false,
|
||||
/** That phone has Faceclaw installed (advertises the capability). */
|
||||
val phoneAppInstalled: Boolean = false,
|
||||
val phoneNodeId: String? = null,
|
||||
val phoneName: String = "",
|
||||
/**
|
||||
* Whether the phone app's dashboard (the JS side) answered our last
|
||||
* message; null until we have heard anything. False means Faceclaw is
|
||||
* installed but not running, so the user has to open it.
|
||||
*/
|
||||
val appRunning: Boolean? = null,
|
||||
val lastError: String? = null,
|
||||
)
|
||||
|
||||
/** The assistant overlay as mirrored from the glasses. */
|
||||
data class AssistantMirror(
|
||||
/** idle | thinking | streaming | done | error */
|
||||
val phase: String = "idle",
|
||||
val text: String = "",
|
||||
val updatedAt: Long = 0,
|
||||
)
|
||||
|
||||
/** A transient message for the UI (ack failures, confirmations). */
|
||||
data class Notice(val text: String, val isError: Boolean, val id: Long)
|
||||
|
||||
/**
|
||||
* The watch's end of the Wearable Data Layer conversation with the phone:
|
||||
* sends gestures/commands/text as messages, receives acks and events, and
|
||||
* mirrors the phone's state item. Lives for the activity's lifetime
|
||||
* (start()/stop() from onStart/onStop).
|
||||
*/
|
||||
class PhoneLink(context: Context) :
|
||||
MessageClient.OnMessageReceivedListener,
|
||||
DataClient.OnDataChangedListener,
|
||||
CapabilityClient.OnCapabilityChangedListener {
|
||||
|
||||
private val appContext = context.applicationContext
|
||||
private val messageClient = Wearable.getMessageClient(appContext)
|
||||
private val dataClient = Wearable.getDataClient(appContext)
|
||||
private val capabilityClient = Wearable.getCapabilityClient(appContext)
|
||||
private val nodeClient = Wearable.getNodeClient(appContext)
|
||||
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
|
||||
private val nextSeq = AtomicLong(1)
|
||||
private val pendingAcks = ConcurrentHashMap<Long, Job>()
|
||||
private var noticeJob: Job? = null
|
||||
private var started = false
|
||||
|
||||
private val _state = MutableStateFlow<PhoneState?>(null)
|
||||
val state: StateFlow<PhoneState?> = _state.asStateFlow()
|
||||
|
||||
private val _link = MutableStateFlow(LinkStatus())
|
||||
val link: StateFlow<LinkStatus> = _link.asStateFlow()
|
||||
|
||||
private val _assistant = MutableStateFlow(AssistantMirror())
|
||||
val assistant: StateFlow<AssistantMirror> = _assistant.asStateFlow()
|
||||
|
||||
private val _events = MutableSharedFlow<PhoneEvent>(extraBufferCapacity = 32)
|
||||
val events: SharedFlow<PhoneEvent> = _events.asSharedFlow()
|
||||
|
||||
private val _notice = MutableStateFlow<Notice?>(null)
|
||||
val notice: StateFlow<Notice?> = _notice.asStateFlow()
|
||||
|
||||
fun start() {
|
||||
if (started) return
|
||||
started = true
|
||||
messageClient.addListener(this)
|
||||
dataClient.addListener(this)
|
||||
capabilityClient.addListener(this, Protocol.CAPABILITY_PHONE)
|
||||
refresh()
|
||||
}
|
||||
|
||||
fun stop() {
|
||||
if (!started) return
|
||||
started = false
|
||||
messageClient.removeListener(this)
|
||||
dataClient.removeListener(this)
|
||||
capabilityClient.removeListener(this, Protocol.CAPABILITY_PHONE)
|
||||
}
|
||||
|
||||
/** Re-discover the phone, reload the cached state item, and ask for a fresh one. */
|
||||
fun refresh() {
|
||||
scope.launch {
|
||||
findPhone()
|
||||
loadStateItem()
|
||||
if (_link.value.phoneAppInstalled) requestState()
|
||||
}
|
||||
}
|
||||
|
||||
// ---- outbound -----------------------------------------------------
|
||||
|
||||
fun sendGesture(gesture: Gesture, steps: Int = 1) {
|
||||
send(Protocol.PATH_INPUT, JSONObject().put("gesture", gesture.wire).put("steps", steps))
|
||||
}
|
||||
|
||||
fun sendCommand(command: String, vararg extras: Pair<String, String>) {
|
||||
val body = JSONObject().put("command", command)
|
||||
for ((key, value) in extras) body.put(key, value)
|
||||
send(Protocol.PATH_COMMAND, body)
|
||||
}
|
||||
|
||||
fun sendAssistantQuery(text: String) {
|
||||
_assistant.value = AssistantMirror(phase = "thinking", text = "", updatedAt = System.currentTimeMillis())
|
||||
send(Protocol.PATH_ASSISTANT, JSONObject().put("text", text))
|
||||
}
|
||||
|
||||
fun sendText(text: String) {
|
||||
send(Protocol.PATH_TEXT, JSONObject().put("text", text))
|
||||
}
|
||||
|
||||
fun requestState() {
|
||||
send(Protocol.PATH_STATE_REQUEST, JSONObject(), quiet = true)
|
||||
}
|
||||
|
||||
fun clearAssistant() {
|
||||
_assistant.value = AssistantMirror()
|
||||
}
|
||||
|
||||
private fun send(path: String, body: JSONObject, quiet: Boolean = false) {
|
||||
val seq = nextSeq.getAndIncrement()
|
||||
body.put("seq", seq)
|
||||
Log.d(TAG, "send $path $body")
|
||||
val payload = body.toString().toByteArray(Charsets.UTF_8)
|
||||
scope.launch {
|
||||
var nodeId = _link.value.phoneNodeId
|
||||
if (nodeId == null) {
|
||||
findPhone()
|
||||
nodeId = _link.value.phoneNodeId
|
||||
}
|
||||
if (nodeId == null) {
|
||||
if (!quiet) showNotice("No phone connected.", isError = true)
|
||||
return@launch
|
||||
}
|
||||
if (!_link.value.phoneAppInstalled) {
|
||||
if (!quiet) showNotice("Faceclaw isn't installed on the phone.", isError = true)
|
||||
return@launch
|
||||
}
|
||||
try {
|
||||
val requestId = messageClient.sendMessage(nodeId, path, payload).await()
|
||||
Log.d(TAG, "sent seq=$seq requestId=$requestId")
|
||||
// The phone acks every message; silence means the app
|
||||
// process never received it (or Play services dropped it).
|
||||
val timeout = scope.launch {
|
||||
delay(ACK_TIMEOUT_MS)
|
||||
if (pendingAcks.remove(seq) != null) {
|
||||
_link.update { it.copy(appRunning = it.appRunning ?: false) }
|
||||
if (!quiet) showNotice("No reply from the phone.", isError = true)
|
||||
}
|
||||
}
|
||||
pendingAcks[seq] = timeout
|
||||
} catch (error: ApiException) {
|
||||
Log.w(TAG, "send $path failed: ${error.statusCode}", error)
|
||||
val message = when (error.statusCode) {
|
||||
WearableStatusCodes.TARGET_NODE_NOT_CONNECTED -> "The phone is out of range."
|
||||
else -> "Couldn't reach the phone (${error.statusCode})."
|
||||
}
|
||||
_link.update { it.copy(lastError = message) }
|
||||
if (!quiet) showNotice(message, isError = true)
|
||||
scope.launch { findPhone() }
|
||||
} catch (error: Exception) {
|
||||
Log.w(TAG, "send $path failed", error)
|
||||
if (!quiet) showNotice("Couldn't reach the phone.", isError = true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---- inbound ------------------------------------------------------
|
||||
|
||||
override fun onMessageReceived(event: MessageEvent) {
|
||||
val json = String(event.data, Charsets.UTF_8)
|
||||
Log.d(TAG, "recv ${event.path} $json")
|
||||
when (event.path) {
|
||||
Protocol.PATH_ACK -> Ack.parse(json)?.let { handleAck(it) }
|
||||
Protocol.PATH_EVENT -> PhoneEvent.parse(json)?.let { handleEvent(it) }
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleAck(ack: Ack) {
|
||||
pendingAcks.remove(ack.seq)?.cancel()
|
||||
_link.update { it.copy(appRunning = ack.jsReady, lastError = null) }
|
||||
if (!ack.ok) {
|
||||
showNotice(ack.message.ifBlank { "The phone refused that." }, isError = true)
|
||||
} else if (ack.message.isNotBlank()) {
|
||||
showNotice(ack.message, isError = false)
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleEvent(event: PhoneEvent) {
|
||||
when (event) {
|
||||
is PhoneEvent.Assistant -> {
|
||||
_assistant.update { current ->
|
||||
when (event.phase) {
|
||||
"thinking" -> AssistantMirror("thinking", "", System.currentTimeMillis())
|
||||
"streaming" -> AssistantMirror("streaming", event.text, System.currentTimeMillis())
|
||||
"done" -> AssistantMirror("done", event.text.ifBlank { current.text }, System.currentTimeMillis())
|
||||
"error" -> AssistantMirror("error", event.text, System.currentTimeMillis())
|
||||
"closed" -> AssistantMirror("idle", current.text, System.currentTimeMillis())
|
||||
else -> current
|
||||
}
|
||||
}
|
||||
}
|
||||
is PhoneEvent.Alert -> showNotice(event.text, isError = false)
|
||||
}
|
||||
_events.tryEmit(event)
|
||||
}
|
||||
|
||||
override fun onDataChanged(buffer: DataEventBuffer) {
|
||||
for (event in buffer) {
|
||||
if (event.type != DataEvent.TYPE_CHANGED) continue
|
||||
val item = event.dataItem
|
||||
if (item.uri.path == Protocol.PATH_STATE) applyStateItem(item)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCapabilityChanged(info: CapabilityInfo) {
|
||||
scope.launch { findPhone() }
|
||||
}
|
||||
|
||||
private fun applyStateItem(item: DataItem) {
|
||||
val map = DataMapItem.fromDataItem(item).dataMap
|
||||
val json = map.getString(Protocol.STATE_KEY_JSON) ?: return
|
||||
val updatedAt = map.getLong(Protocol.STATE_KEY_UPDATED_AT, 0)
|
||||
PhoneState.parse(json, updatedAt)?.let { parsed ->
|
||||
_state.value = parsed
|
||||
// A state item is only ever written by the running dashboard.
|
||||
_link.update { it.copy(appRunning = true) }
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun loadStateItem() {
|
||||
try {
|
||||
val uri = Uri.Builder().scheme("wear").path(Protocol.PATH_STATE).build()
|
||||
val items = dataClient.getDataItems(uri).await()
|
||||
try {
|
||||
for (item in items) applyStateItem(item)
|
||||
} finally {
|
||||
items.release()
|
||||
}
|
||||
} catch (error: Exception) {
|
||||
Log.w(TAG, "state item load failed", error)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun findPhone() {
|
||||
try {
|
||||
val info = capabilityClient
|
||||
.getCapability(Protocol.CAPABILITY_PHONE, CapabilityClient.FILTER_REACHABLE)
|
||||
.await()
|
||||
val phone = pickNode(info.nodes)
|
||||
if (phone != null) {
|
||||
_link.update {
|
||||
it.copy(
|
||||
phoneReachable = true,
|
||||
phoneAppInstalled = true,
|
||||
phoneNodeId = phone.id,
|
||||
phoneName = phone.displayName,
|
||||
lastError = null,
|
||||
)
|
||||
}
|
||||
return
|
||||
}
|
||||
val connected = nodeClient.connectedNodes.await()
|
||||
val any = pickNode(connected.toSet())
|
||||
_link.update {
|
||||
it.copy(
|
||||
phoneReachable = any != null,
|
||||
phoneAppInstalled = false,
|
||||
phoneNodeId = null,
|
||||
phoneName = any?.displayName ?: "",
|
||||
appRunning = null,
|
||||
)
|
||||
}
|
||||
} catch (error: Exception) {
|
||||
Log.w(TAG, "phone lookup failed", error)
|
||||
_link.update { it.copy(lastError = error.message) }
|
||||
}
|
||||
}
|
||||
|
||||
private fun pickNode(nodes: Set<Node>): Node? =
|
||||
nodes.firstOrNull { it.isNearby } ?: nodes.firstOrNull()
|
||||
|
||||
private fun showNotice(text: String, isError: Boolean) {
|
||||
noticeJob?.cancel()
|
||||
_notice.value = Notice(text, isError, System.nanoTime())
|
||||
noticeJob = scope.launch {
|
||||
delay(if (isError) NOTICE_ERROR_MS else NOTICE_INFO_MS)
|
||||
_notice.value = null
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val TAG = "FaceclawWear"
|
||||
private const val ACK_TIMEOUT_MS = 3000L
|
||||
private const val NOTICE_INFO_MS = 1800L
|
||||
private const val NOTICE_ERROR_MS = 3200L
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
package com.faceclaw.wear
|
||||
|
||||
import org.json.JSONObject
|
||||
|
||||
/**
|
||||
* Wire format shared with the phone app. Mirror of app/native/wear-bridge.ts
|
||||
* (paths) and app/g2/wear-remote.ts (payloads); see ../../PROTOCOL.md.
|
||||
*/
|
||||
object Protocol {
|
||||
const val PROTOCOL_VERSION = 1
|
||||
|
||||
/** Advertised by the phone app so the watch can find it. */
|
||||
const val CAPABILITY_PHONE = "faceclaw_phone"
|
||||
|
||||
const val PATH_INPUT = "/faceclaw/input"
|
||||
const val PATH_COMMAND = "/faceclaw/command"
|
||||
const val PATH_ASSISTANT = "/faceclaw/assistant"
|
||||
const val PATH_TEXT = "/faceclaw/text"
|
||||
const val PATH_STATE_REQUEST = "/faceclaw/state/request"
|
||||
const val PATH_ACK = "/faceclaw/ack"
|
||||
const val PATH_EVENT = "/faceclaw/event"
|
||||
const val PATH_STATE = "/faceclaw/state"
|
||||
|
||||
const val STATE_KEY_JSON = "json"
|
||||
const val STATE_KEY_UPDATED_AT = "updatedAt"
|
||||
}
|
||||
|
||||
/** Ring-style gestures, as the phone's synthetic ring input understands them. */
|
||||
enum class Gesture(val wire: String) {
|
||||
CLICK("click"),
|
||||
DOUBLE_CLICK("double-click"),
|
||||
SCROLL_UP("scroll-up"),
|
||||
SCROLL_DOWN("scroll-down"),
|
||||
/** A complete short hold (press + release). */
|
||||
LONG_PRESS("long-press"),
|
||||
/** Finger down: the hold lasts until LONG_PRESS_RELEASE. */
|
||||
LONG_PRESS_START("long-press-start"),
|
||||
LONG_PRESS_RELEASE("long-press-release"),
|
||||
/** The spoken "Hey Even": opens the glasses' own voice dialog. */
|
||||
WAKEWORD("wakeword"),
|
||||
/**
|
||||
* Spatial (four-way) input the ring cannot produce. The glasses UI gives
|
||||
* it a spatial meaning where one exists (grid rows and columns, panel
|
||||
* columns, tracks) and otherwise treats up/down as scroll, right as select
|
||||
* and left as back.
|
||||
*/
|
||||
SWIPE_UP("swipe-up"),
|
||||
SWIPE_DOWN("swipe-down"),
|
||||
SWIPE_LEFT("swipe-left"),
|
||||
SWIPE_RIGHT("swipe-right"),
|
||||
}
|
||||
|
||||
/** Shell commands; things the ring cannot express. */
|
||||
object Command {
|
||||
const val LAUNCH_APP = "launch-app"
|
||||
const val FOCUS_WINDOW = "focus-window"
|
||||
const val CLOSE_WINDOW = "close-window"
|
||||
const val SIDEBAR = "sidebar"
|
||||
const val WAKE = "wake"
|
||||
const val SLEEP = "sleep"
|
||||
const val LOCK = "lock"
|
||||
const val UNLOCK = "unlock"
|
||||
const val CONNECT = "connect"
|
||||
const val DISCONNECT = "disconnect"
|
||||
const val CLOSE_ASSISTANT = "close-assistant"
|
||||
const val DISPLAY_MODE = "display-mode"
|
||||
}
|
||||
|
||||
/** The glasses' display modes, mirroring the phone's Display > Display mode. */
|
||||
enum class DisplayMode(val wire: String, val label: String) {
|
||||
BAND("576x288", "Band · 576×288"),
|
||||
TALL("576x480", "Tall · 576×480"),
|
||||
FULL("640x480", "Full panel · 640×480");
|
||||
|
||||
fun next(): DisplayMode = entries[(ordinal + 1) % entries.size]
|
||||
|
||||
companion object {
|
||||
fun fromWire(value: String): DisplayMode? = entries.firstOrNull { it.wire == value }
|
||||
}
|
||||
}
|
||||
|
||||
data class AppEntry(val appId: String, val title: String)
|
||||
|
||||
data class WindowEntry(
|
||||
val windowId: String,
|
||||
val title: String,
|
||||
val appId: String,
|
||||
val focused: Boolean,
|
||||
val closeable: Boolean,
|
||||
val acceptsText: Boolean,
|
||||
)
|
||||
|
||||
/** The phone's mirrored dashboard state (Data Layer item /faceclaw/state). */
|
||||
data class PhoneState(
|
||||
val protocol: Int,
|
||||
val version: String,
|
||||
val phase: String,
|
||||
val status: String,
|
||||
val connected: Boolean,
|
||||
val screenOn: Boolean,
|
||||
val locked: Boolean,
|
||||
val worn: Boolean?,
|
||||
val listening: Boolean,
|
||||
/** Display mode wire value ("576x288" | "576x480" | "640x480"). */
|
||||
val displayMode: String,
|
||||
val battery: Int?,
|
||||
val charging: Boolean,
|
||||
val foreground: AppEntry?,
|
||||
val windows: List<WindowEntry>,
|
||||
val apps: List<AppEntry>,
|
||||
val remoteEnabled: Boolean,
|
||||
val canUnlock: Boolean,
|
||||
val mirrorAssistant: Boolean,
|
||||
val assistantAvailable: Boolean,
|
||||
val updatedAt: Long,
|
||||
) {
|
||||
companion object {
|
||||
fun parse(json: String, updatedAt: Long): PhoneState? {
|
||||
return try {
|
||||
val o = JSONObject(json)
|
||||
val foreground = o.optJSONObject("foreground")
|
||||
PhoneState(
|
||||
protocol = o.optInt("protocol", 1),
|
||||
version = o.optString("version", ""),
|
||||
phase = o.optString("phase", "disconnected"),
|
||||
status = o.optString("status", ""),
|
||||
connected = o.optBoolean("connected", false),
|
||||
screenOn = o.optBoolean("screenOn", false),
|
||||
locked = o.optBoolean("locked", false),
|
||||
worn = if (o.isNull("worn")) null else o.optBoolean("worn"),
|
||||
listening = o.optBoolean("listening", false),
|
||||
displayMode = o.optString("displayMode", "576x288"),
|
||||
battery = if (o.isNull("battery")) null else o.optInt("battery").takeIf { it >= 0 },
|
||||
charging = o.optBoolean("charging", false),
|
||||
foreground = foreground?.let {
|
||||
AppEntry(it.optString("appId", ""), it.optString("title", ""))
|
||||
},
|
||||
windows = o.optJSONArray("windows")?.let { array ->
|
||||
(0 until array.length()).mapNotNull { index ->
|
||||
val w = array.optJSONObject(index) ?: return@mapNotNull null
|
||||
WindowEntry(
|
||||
windowId = w.optString("windowId", ""),
|
||||
title = w.optString("title", ""),
|
||||
appId = w.optString("appId", ""),
|
||||
focused = w.optBoolean("focused", false),
|
||||
closeable = w.optBoolean("closeable", true),
|
||||
acceptsText = w.optBoolean("acceptsText", false),
|
||||
)
|
||||
}
|
||||
} ?: emptyList(),
|
||||
apps = o.optJSONArray("apps")?.let { array ->
|
||||
(0 until array.length()).mapNotNull { index ->
|
||||
val a = array.optJSONObject(index) ?: return@mapNotNull null
|
||||
AppEntry(a.optString("appId", ""), a.optString("title", ""))
|
||||
}
|
||||
} ?: emptyList(),
|
||||
remoteEnabled = o.optBoolean("remoteEnabled", true),
|
||||
canUnlock = o.optBoolean("canUnlock", true),
|
||||
mirrorAssistant = o.optBoolean("mirrorAssistant", true),
|
||||
assistantAvailable = o.optBoolean("assistantAvailable", false),
|
||||
updatedAt = updatedAt,
|
||||
)
|
||||
} catch (error: Exception) {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** The phone's reply to one of our messages. */
|
||||
data class Ack(val seq: Long, val ok: Boolean, val jsReady: Boolean, val message: String) {
|
||||
companion object {
|
||||
fun parse(json: String): Ack? = try {
|
||||
val o = JSONObject(json)
|
||||
Ack(
|
||||
seq = o.optLong("seq", 0),
|
||||
ok = o.optBoolean("ok", false),
|
||||
jsReady = o.optBoolean("jsReady", true),
|
||||
message = o.optString("message", ""),
|
||||
)
|
||||
} catch (error: Exception) {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Phone -> watch events (/faceclaw/event). */
|
||||
sealed class PhoneEvent {
|
||||
/** phase: thinking | streaming | done | error | closed; text is the reply so far. */
|
||||
data class Assistant(val phase: String, val text: String) : PhoneEvent()
|
||||
data class Alert(val text: String) : PhoneEvent()
|
||||
|
||||
companion object {
|
||||
fun parse(json: String): PhoneEvent? = try {
|
||||
val o = JSONObject(json)
|
||||
when (o.optString("type")) {
|
||||
"assistant" -> Assistant(o.optString("phase", ""), o.optString("text", ""))
|
||||
"alert" -> Alert(o.optString("text", ""))
|
||||
else -> null
|
||||
}
|
||||
} catch (error: Exception) {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
package com.faceclaw.wear
|
||||
|
||||
import android.content.Context
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
|
||||
enum class CrownSensitivity(val label: String, val pixelsPerStep: Float) {
|
||||
LOW("Low", 110f),
|
||||
NORMAL("Normal", 60f),
|
||||
HIGH("High", 30f);
|
||||
|
||||
fun next(): CrownSensitivity = entries[(ordinal + 1) % entries.size]
|
||||
}
|
||||
|
||||
/** What a horizontal swipe on the pad does. */
|
||||
enum class SwipeAction(val label: String) {
|
||||
/** Sent as directional input; the glasses UI decides (see Gesture.SWIPE_LEFT). */
|
||||
NAVIGATE("Navigate"),
|
||||
SIDEBAR("Sidebar"),
|
||||
DOUBLE_CLICK("Double-click"),
|
||||
CLICK("Click"),
|
||||
LONG_PRESS("Long press"),
|
||||
WAKEWORD("“Hey Even”"),
|
||||
NONE("Nothing");
|
||||
|
||||
fun next(): SwipeAction = entries[(ordinal + 1) % entries.size]
|
||||
}
|
||||
|
||||
/** Watch-local preferences (nothing here is synced to the phone). */
|
||||
data class Prefs(
|
||||
/** Buzz on every gesture sent. */
|
||||
val haptics: Boolean = true,
|
||||
/**
|
||||
* Keep the display awake while the pad is showing. Wear otherwise dozes
|
||||
* within seconds of the wrist dropping and freezes the app, which is
|
||||
* exactly when a remote wants to stay ready.
|
||||
*/
|
||||
val keepScreenOn: Boolean = true,
|
||||
/** Swipe/crown direction follows the content (touch-scroll style) instead of the ring. */
|
||||
val naturalScroll: Boolean = false,
|
||||
/** Tap the top/bottom of the pad to scroll instead of swiping. */
|
||||
val tapZones: Boolean = false,
|
||||
val crownSensitivity: CrownSensitivity = CrownSensitivity.NORMAL,
|
||||
val swipeLeft: SwipeAction = SwipeAction.NAVIGATE,
|
||||
val swipeRight: SwipeAction = SwipeAction.NAVIGATE,
|
||||
/**
|
||||
* Fingertip (pinch) taps from the motion sensors; see FingerTapDetector.
|
||||
* Off by default: on a wrist, ordinary hand movement produced phantom
|
||||
* clicks in testing, and a phantom click opens things.
|
||||
*/
|
||||
val fingerTaps: Boolean = false,
|
||||
val tapSensitivity: TapSensitivity = TapSensitivity.NORMAL,
|
||||
/** Two quick wrist twists = back (double-click); see WristTwistDetector. */
|
||||
val wristTwist: Boolean = true,
|
||||
val twistSensitivity: TwistSensitivity = TwistSensitivity.NORMAL,
|
||||
)
|
||||
|
||||
class WatchPrefs(context: Context) {
|
||||
private val store = context.applicationContext.getSharedPreferences("faceclaw_wear", Context.MODE_PRIVATE)
|
||||
private val _prefs = MutableStateFlow(load())
|
||||
val prefs: StateFlow<Prefs> = _prefs.asStateFlow()
|
||||
|
||||
private fun load(): Prefs = Prefs(
|
||||
haptics = store.getBoolean(KEY_HAPTICS, true),
|
||||
keepScreenOn = store.getBoolean(KEY_KEEP_SCREEN_ON, true),
|
||||
naturalScroll = store.getBoolean(KEY_NATURAL_SCROLL, false),
|
||||
tapZones = store.getBoolean(KEY_TAP_ZONES, false),
|
||||
crownSensitivity = store.getString(KEY_CROWN, null)
|
||||
?.let { stored -> CrownSensitivity.entries.firstOrNull { it.name == stored } }
|
||||
?: CrownSensitivity.NORMAL,
|
||||
swipeLeft = store.getString(KEY_SWIPE_LEFT, null)
|
||||
?.let { stored -> SwipeAction.entries.firstOrNull { it.name == stored } }
|
||||
?: SwipeAction.NAVIGATE,
|
||||
swipeRight = store.getString(KEY_SWIPE_RIGHT, null)
|
||||
?.let { stored -> SwipeAction.entries.firstOrNull { it.name == stored } }
|
||||
?: SwipeAction.NAVIGATE,
|
||||
fingerTaps = store.getBoolean(KEY_FINGER_TAPS, false),
|
||||
tapSensitivity = store.getString(KEY_TAP_SENSITIVITY, null)
|
||||
?.let { stored -> TapSensitivity.entries.firstOrNull { it.name == stored } }
|
||||
?: TapSensitivity.NORMAL,
|
||||
wristTwist = store.getBoolean(KEY_WRIST_TWIST, true),
|
||||
twistSensitivity = store.getString(KEY_TWIST_SENSITIVITY, null)
|
||||
?.let { stored -> TwistSensitivity.entries.firstOrNull { it.name == stored } }
|
||||
?: TwistSensitivity.NORMAL,
|
||||
)
|
||||
|
||||
fun update(transform: (Prefs) -> Prefs) {
|
||||
val next = transform(_prefs.value)
|
||||
_prefs.value = next
|
||||
store.edit()
|
||||
.putBoolean(KEY_HAPTICS, next.haptics)
|
||||
.putBoolean(KEY_KEEP_SCREEN_ON, next.keepScreenOn)
|
||||
.putBoolean(KEY_NATURAL_SCROLL, next.naturalScroll)
|
||||
.putBoolean(KEY_TAP_ZONES, next.tapZones)
|
||||
.putString(KEY_CROWN, next.crownSensitivity.name)
|
||||
.putString(KEY_SWIPE_LEFT, next.swipeLeft.name)
|
||||
.putString(KEY_SWIPE_RIGHT, next.swipeRight.name)
|
||||
.putBoolean(KEY_FINGER_TAPS, next.fingerTaps)
|
||||
.putString(KEY_TAP_SENSITIVITY, next.tapSensitivity.name)
|
||||
.putBoolean(KEY_WRIST_TWIST, next.wristTwist)
|
||||
.putString(KEY_TWIST_SENSITIVITY, next.twistSensitivity.name)
|
||||
.apply()
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val KEY_HAPTICS = "haptics"
|
||||
const val KEY_KEEP_SCREEN_ON = "keepScreenOn"
|
||||
const val KEY_NATURAL_SCROLL = "naturalScroll"
|
||||
const val KEY_TAP_ZONES = "tapZones"
|
||||
const val KEY_CROWN = "crownSensitivity"
|
||||
const val KEY_SWIPE_LEFT = "swipeLeft"
|
||||
const val KEY_SWIPE_RIGHT = "swipeRight"
|
||||
const val KEY_FINGER_TAPS = "fingerTaps"
|
||||
const val KEY_TAP_SENSITIVITY = "tapSensitivity"
|
||||
const val KEY_WRIST_TWIST = "wristTwist"
|
||||
const val KEY_TWIST_SENSITIVITY = "twistSensitivity"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
package com.faceclaw.wear
|
||||
|
||||
import android.content.Context
|
||||
import android.hardware.Sensor
|
||||
import android.hardware.SensorEvent
|
||||
import android.hardware.SensorEventListener
|
||||
import android.hardware.SensorManager
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.os.SystemClock
|
||||
import android.util.Log
|
||||
import kotlin.math.abs
|
||||
|
||||
/** How fast the wrist has to turn before a twist counts. */
|
||||
enum class TwistSensitivity(val label: String, val threshold: Float) {
|
||||
/** Only a brisk flick; rad/s about the forearm. */
|
||||
LOW("Low", 5.0f),
|
||||
NORMAL("Normal", 3.6f),
|
||||
HIGH("High", 2.6f);
|
||||
|
||||
fun next(): TwistSensitivity = entries[(ordinal + 1) % entries.size]
|
||||
}
|
||||
|
||||
/**
|
||||
* "Twist the wrist twice" = back. The gyroscope's x axis runs along the
|
||||
* forearm on a watch, so a wrist twist (pronation / supination) is a burst of
|
||||
* angular velocity about x. Two brisk twists in the same direction, each
|
||||
* short and separated by 120–900 ms, fire the gesture; the wrist coming back
|
||||
* between them spins the other way and is ignored. The direction is not
|
||||
* fixed (away or toward), so it works on either wrist, and the twists must be
|
||||
* quick: a slow arm rotation never crosses the rate threshold.
|
||||
*
|
||||
* Same status as FingerTapDetector: a heuristic with a toggle, not a
|
||||
* classifier.
|
||||
*/
|
||||
class WristTwistDetector(
|
||||
context: Context,
|
||||
private val sensitivity: () -> TwistSensitivity,
|
||||
/** elapsedRealtime before which samples are ignored (the watch's own haptics). */
|
||||
private val quietUntil: () -> Long,
|
||||
/** False while the watch is off the wrist; twists then never fire. */
|
||||
private val onBody: () -> Boolean,
|
||||
private val onDoubleTwist: () -> Unit,
|
||||
) : SensorEventListener {
|
||||
private val sensorManager: SensorManager? = context.getSystemService(SensorManager::class.java)
|
||||
private val sensor: Sensor? = sensorManager?.getDefaultSensor(Sensor.TYPE_GYROSCOPE)
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
|
||||
private var inBurst = false
|
||||
/** Slow average of |rate| outside bursts: how much the arm is rotating anyway. */
|
||||
private var activity = 0f
|
||||
private var lastMotionNs = 0L
|
||||
private var burstStartNs = 0L
|
||||
private var burstSign = 0
|
||||
private var firstTwistNs = 0L
|
||||
private var firstTwistSign = 0
|
||||
private var running = false
|
||||
|
||||
val available: Boolean get() = sensor != null
|
||||
|
||||
fun start(): Boolean {
|
||||
val manager = sensorManager ?: return false
|
||||
val target = sensor ?: return false
|
||||
if (running) return true
|
||||
running = manager.registerListener(this, target, SAMPLING_PERIOD_US, handler)
|
||||
Log.i(TAG, "wrist-twist detector ${if (running) "started" else "failed to start"}")
|
||||
return running
|
||||
}
|
||||
|
||||
fun stop() {
|
||||
if (!running) return
|
||||
running = false
|
||||
sensorManager?.unregisterListener(this)
|
||||
inBurst = false
|
||||
firstTwistNs = 0L
|
||||
}
|
||||
|
||||
override fun onSensorChanged(event: SensorEvent) {
|
||||
val rate = event.values[0]
|
||||
val now = event.timestamp
|
||||
if (SystemClock.elapsedRealtime() < quietUntil() || !onBody()) {
|
||||
inBurst = false
|
||||
firstTwistNs = 0L
|
||||
return
|
||||
}
|
||||
val threshold = sensitivity().threshold
|
||||
|
||||
if (!inBurst) {
|
||||
val magnitude = abs(rate)
|
||||
// A twist pair only starts from a still-ish wrist: general arm
|
||||
// rotation (reaching, gesturing) produced same-sign burst pairs
|
||||
// that read as the gesture.
|
||||
val still = activity < threshold * ACTIVITY_FRACTION && now - lastMotionNs > STILLNESS_BEFORE_TWIST_NS
|
||||
if (magnitude > threshold && (still || firstTwistNs != 0L)) {
|
||||
inBurst = true
|
||||
burstStartNs = now
|
||||
burstSign = if (rate > 0) 1 else -1
|
||||
} else {
|
||||
activity += (magnitude - activity) * ACTIVITY_ALPHA
|
||||
if (magnitude > threshold * ACTIVITY_FRACTION) lastMotionNs = now
|
||||
}
|
||||
return
|
||||
}
|
||||
// In a burst: wait for the rate to drop back under the threshold.
|
||||
if (abs(rate) > threshold * BURST_END_FRACTION && (if (rate > 0) 1 else -1) == burstSign) return
|
||||
inBurst = false
|
||||
val duration = now - burstStartNs
|
||||
if (duration > MAX_BURST_NS) return // a slow sweep, not a flick
|
||||
|
||||
val sinceFirst = now - firstTwistNs
|
||||
if (firstTwistNs != 0L && burstSign == firstTwistSign && sinceFirst in MIN_GAP_NS..MAX_GAP_NS) {
|
||||
firstTwistNs = 0L
|
||||
onDoubleTwist()
|
||||
} else if (firstTwistNs == 0L || sinceFirst > MAX_GAP_NS || burstSign != firstTwistSign) {
|
||||
// The return twist (opposite sign) does not start a new pair; a
|
||||
// fresh same-direction twist does.
|
||||
if (firstTwistNs == 0L || sinceFirst > MAX_GAP_NS) {
|
||||
firstTwistNs = now
|
||||
firstTwistSign = burstSign
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) = Unit
|
||||
|
||||
private companion object {
|
||||
const val TAG = "FaceclawWear"
|
||||
const val SAMPLING_PERIOD_US = 10_000
|
||||
const val MAX_BURST_NS = 350_000_000L
|
||||
const val MIN_GAP_NS = 120_000_000L
|
||||
const val MAX_GAP_NS = 900_000_000L
|
||||
const val BURST_END_FRACTION = 0.5f
|
||||
const val ACTIVITY_ALPHA = 0.04f
|
||||
const val ACTIVITY_FRACTION = 0.35f
|
||||
const val STILLNESS_BEFORE_TWIST_NS = 300_000_000L
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package com.faceclaw.wear.ui
|
||||
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import androidx.wear.compose.foundation.lazy.ScalingLazyColumn
|
||||
import androidx.wear.compose.foundation.lazy.items
|
||||
import androidx.wear.compose.foundation.lazy.rememberScalingLazyListState
|
||||
import androidx.wear.compose.material.Button
|
||||
import androidx.wear.compose.material.ButtonDefaults
|
||||
import androidx.wear.compose.material.Chip
|
||||
import androidx.wear.compose.material.ChipDefaults
|
||||
import androidx.wear.compose.material.Icon
|
||||
import androidx.wear.compose.material.ListHeader
|
||||
import androidx.wear.compose.material.PositionIndicator
|
||||
import androidx.wear.compose.material.Scaffold
|
||||
import androidx.wear.compose.material.Text
|
||||
import androidx.wear.compose.material.TimeText
|
||||
import com.faceclaw.wear.Command
|
||||
import com.faceclaw.wear.Haptics
|
||||
import com.faceclaw.wear.PhoneLink
|
||||
import com.faceclaw.wear.R
|
||||
import com.faceclaw.wear.WindowEntry
|
||||
|
||||
/**
|
||||
* The glasses' sidebar and launcher, on the wrist: open windows first (tap to
|
||||
* bring one to the front, × to close it), then every launchable app.
|
||||
*/
|
||||
@Composable
|
||||
fun AppsScreen(link: PhoneLink, haptics: Haptics, onDone: () -> Unit) {
|
||||
val state by link.state.collectAsStateWithLifecycle()
|
||||
val notice by link.notice.collectAsStateWithLifecycle()
|
||||
val listState = rememberScalingLazyListState()
|
||||
val windows = state?.windows ?: emptyList()
|
||||
val apps = state?.apps ?: emptyList()
|
||||
|
||||
Scaffold(
|
||||
timeText = { TimeText() },
|
||||
positionIndicator = { PositionIndicator(scalingLazyListState = listState) },
|
||||
) {
|
||||
ScalingLazyColumn(
|
||||
state = listState,
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
) {
|
||||
val onScreen = windows.filter { it.focused }
|
||||
val others = windows.filter { !it.focused }
|
||||
if (onScreen.isNotEmpty()) {
|
||||
item { ListHeader { Text("On screen") } }
|
||||
items(onScreen, key = { "w:" + it.windowId }) { window -> WindowRow(window, link, haptics, onDone) }
|
||||
}
|
||||
if (others.isNotEmpty()) {
|
||||
item { ListHeader { Text("Also open") } }
|
||||
items(others, key = { "w:" + it.windowId }) { window -> WindowRow(window, link, haptics, onDone) }
|
||||
}
|
||||
item { ListHeader { Text(if (apps.isEmpty()) "No app list yet" else "Launch") } }
|
||||
items(apps, key = { "a:" + it.appId }) { app ->
|
||||
Chip(
|
||||
onClick = {
|
||||
haptics.click()
|
||||
link.sendCommand(Command.LAUNCH_APP, "appId" to app.appId)
|
||||
onDone()
|
||||
},
|
||||
label = { Text(app.title, maxLines = 1, overflow = TextOverflow.Ellipsis) },
|
||||
colors = ChipDefaults.secondaryChipColors(),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
}
|
||||
if (apps.isEmpty()) {
|
||||
item {
|
||||
Text(
|
||||
"Open Faceclaw on the phone and connect the glasses.",
|
||||
textAlign = androidx.compose.ui.text.style.TextAlign.Center,
|
||||
style = androidx.wear.compose.material.MaterialTheme.typography.caption2,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
NoticeOverlay(notice)
|
||||
}
|
||||
}
|
||||
|
||||
/** One open window: tap to bring it to the front, × to close it. */
|
||||
@Composable
|
||||
private fun WindowRow(window: WindowEntry, link: PhoneLink, haptics: Haptics, onDone: () -> Unit) {
|
||||
Row(modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
|
||||
Chip(
|
||||
onClick = {
|
||||
haptics.click()
|
||||
link.sendCommand(Command.FOCUS_WINDOW, "windowId" to window.windowId)
|
||||
onDone()
|
||||
},
|
||||
// The launcher's window is titled "Apps" on the glasses; here that
|
||||
// would read as this very screen.
|
||||
label = { Text(if (window.appId == "launcher") "Launcher" else window.title, maxLines = 1, overflow = TextOverflow.Ellipsis) },
|
||||
secondaryLabel = if (window.acceptsText) ({ Text("takes typed text") }) else null,
|
||||
colors = if (window.focused) ChipDefaults.primaryChipColors() else ChipDefaults.secondaryChipColors(),
|
||||
modifier = Modifier.weight(1f),
|
||||
)
|
||||
if (window.closeable) {
|
||||
Spacer(modifier = Modifier.width(4.dp))
|
||||
Button(
|
||||
onClick = {
|
||||
haptics.click()
|
||||
link.sendCommand(Command.CLOSE_WINDOW, "windowId" to window.windowId)
|
||||
},
|
||||
modifier = Modifier.size(32.dp),
|
||||
colors = ButtonDefaults.secondaryButtonColors(),
|
||||
) {
|
||||
Icon(
|
||||
painter = painterResource(R.drawable.ic_close),
|
||||
contentDescription = "Close ${window.title}",
|
||||
modifier = Modifier.size(16.dp),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
package com.faceclaw.wear.ui
|
||||
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import androidx.wear.compose.foundation.lazy.ScalingLazyColumn
|
||||
import androidx.wear.compose.foundation.lazy.rememberScalingLazyListState
|
||||
import androidx.wear.compose.material.Button
|
||||
import androidx.wear.compose.material.ButtonDefaults
|
||||
import androidx.wear.compose.material.Chip
|
||||
import androidx.wear.compose.material.ChipDefaults
|
||||
import androidx.wear.compose.material.Icon
|
||||
import androidx.wear.compose.material.MaterialTheme
|
||||
import androidx.wear.compose.material.PositionIndicator
|
||||
import androidx.wear.compose.material.Scaffold
|
||||
import androidx.wear.compose.material.Text
|
||||
import androidx.wear.compose.material.TimeText
|
||||
import com.faceclaw.wear.Command
|
||||
import com.faceclaw.wear.Gesture
|
||||
import com.faceclaw.wear.Haptics
|
||||
import com.faceclaw.wear.PhoneLink
|
||||
import com.faceclaw.wear.R
|
||||
|
||||
/**
|
||||
* Ask the assistant from the wrist (watch speech recognition or keyboard) and
|
||||
* read its reply here as it streams, mirrored from the glasses overlay. "Hey
|
||||
* Even" instead uses the glasses' own microphone and voice dialog.
|
||||
*/
|
||||
@Composable
|
||||
fun AssistantScreen(link: PhoneLink, haptics: Haptics) {
|
||||
val state by link.state.collectAsStateWithLifecycle()
|
||||
val mirror by link.assistant.collectAsStateWithLifecycle()
|
||||
val notice by link.notice.collectAsStateWithLifecycle()
|
||||
val listState = rememberScalingLazyListState()
|
||||
val available = state?.assistantAvailable != false
|
||||
|
||||
val ask = rememberSpeechLauncher(
|
||||
prompt = stringResource(R.string.speech_prompt_assistant),
|
||||
onResult = { text -> haptics.click(); link.sendAssistantQuery(text) },
|
||||
onUnavailable = { haptics.error() },
|
||||
)
|
||||
val type = rememberKeyboardLauncher(
|
||||
label = stringResource(R.string.keyboard_label_assistant),
|
||||
onResult = { text -> haptics.click(); link.sendAssistantQuery(text) },
|
||||
onUnavailable = { haptics.error() },
|
||||
)
|
||||
|
||||
// A finished reply deserves a nudge: the wrist may be down while it streams.
|
||||
LaunchedEffect(mirror.phase, mirror.updatedAt) {
|
||||
if (mirror.phase == "done" || mirror.phase == "error") haptics.doubleClick()
|
||||
}
|
||||
|
||||
Scaffold(
|
||||
timeText = { TimeText() },
|
||||
positionIndicator = { PositionIndicator(scalingLazyListState = listState) },
|
||||
) {
|
||||
ScalingLazyColumn(state = listState, modifier = Modifier.fillMaxSize()) {
|
||||
item {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth().padding(top = 6.dp),
|
||||
horizontalArrangement = Arrangement.Center,
|
||||
) {
|
||||
Button(onClick = ask, modifier = Modifier.size(44.dp), enabled = available) {
|
||||
Icon(painterResource(R.drawable.ic_mic), contentDescription = "Ask by voice", modifier = Modifier.size(22.dp))
|
||||
}
|
||||
Spacer(modifier = Modifier.size(10.dp))
|
||||
Button(
|
||||
onClick = type,
|
||||
modifier = Modifier.size(44.dp),
|
||||
colors = ButtonDefaults.secondaryButtonColors(),
|
||||
enabled = available,
|
||||
) {
|
||||
Icon(painterResource(R.drawable.ic_keyboard), contentDescription = "Ask by keyboard", modifier = Modifier.size(22.dp))
|
||||
}
|
||||
}
|
||||
}
|
||||
item {
|
||||
val label = when (mirror.phase) {
|
||||
"thinking" -> "Thinking…"
|
||||
"streaming" -> "Replying…"
|
||||
"error" -> "Error"
|
||||
"done" -> "Reply"
|
||||
else -> if (available) "Ask something" else "Set up the assistant in Faceclaw's Settings"
|
||||
}
|
||||
Text(
|
||||
label,
|
||||
style = MaterialTheme.typography.caption1,
|
||||
color = MaterialTheme.colors.onSurfaceVariant,
|
||||
textAlign = TextAlign.Center,
|
||||
modifier = Modifier.fillMaxWidth().padding(top = 4.dp),
|
||||
)
|
||||
}
|
||||
if (mirror.text.isNotBlank()) {
|
||||
item {
|
||||
Text(
|
||||
mirror.text,
|
||||
style = MaterialTheme.typography.body2,
|
||||
modifier = Modifier.fillMaxWidth().padding(horizontal = 6.dp, vertical = 4.dp),
|
||||
)
|
||||
}
|
||||
}
|
||||
item {
|
||||
Chip(
|
||||
onClick = {
|
||||
haptics.click()
|
||||
link.sendGesture(Gesture.WAKEWORD)
|
||||
},
|
||||
label = { Text("“Hey Even” on glasses") },
|
||||
secondaryLabel = { Text("uses the glasses mic") },
|
||||
colors = ChipDefaults.secondaryChipColors(),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
}
|
||||
if (mirror.phase != "idle") {
|
||||
item {
|
||||
Chip(
|
||||
onClick = {
|
||||
haptics.click()
|
||||
link.sendCommand(Command.CLOSE_ASSISTANT)
|
||||
link.clearAssistant()
|
||||
},
|
||||
label = { Text(if (mirror.phase == "thinking" || mirror.phase == "streaming") "Cancel" else "Dismiss") },
|
||||
colors = ChipDefaults.secondaryChipColors(),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
NoticeOverlay(notice)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package com.faceclaw.wear.ui
|
||||
|
||||
import androidx.compose.animation.AnimatedVisibility
|
||||
import androidx.compose.animation.fadeIn
|
||||
import androidx.compose.animation.fadeOut
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.wear.compose.material.MaterialTheme
|
||||
import androidx.wear.compose.material.Text
|
||||
import com.faceclaw.wear.LinkStatus
|
||||
import com.faceclaw.wear.Notice
|
||||
import com.faceclaw.wear.PhoneState
|
||||
|
||||
/** A transient pill at the bottom of the screen: ack failures and confirmations. */
|
||||
@Composable
|
||||
fun NoticeOverlay(notice: Notice?) {
|
||||
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.BottomCenter) {
|
||||
AnimatedVisibility(visible = notice != null, enter = fadeIn(), exit = fadeOut()) {
|
||||
val current = notice ?: return@AnimatedVisibility
|
||||
Text(
|
||||
text = current.text,
|
||||
style = MaterialTheme.typography.caption2,
|
||||
color = if (current.isError) Color(0xFFFFB4A8) else MaterialTheme.colors.onSurface,
|
||||
textAlign = TextAlign.Center,
|
||||
maxLines = 2,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
modifier = Modifier
|
||||
.padding(horizontal = 28.dp, vertical = 18.dp)
|
||||
.background(
|
||||
if (current.isError) Color(0xFF4A1F1A) else Color(0xFF2A2A2A),
|
||||
RoundedCornerShape(14.dp),
|
||||
)
|
||||
.padding(horizontal = 10.dp, vertical = 5.dp),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** The pad's top line: how the glasses and phone are doing, in a few words. */
|
||||
data class StatusLine(val text: String, val tone: StatusTone)
|
||||
|
||||
enum class StatusTone { GOOD, BUSY, OFF, PROBLEM }
|
||||
|
||||
fun statusLine(state: PhoneState?, link: LinkStatus): StatusLine = when {
|
||||
!link.phoneReachable -> StatusLine("No phone", StatusTone.PROBLEM)
|
||||
!link.phoneAppInstalled -> StatusLine("Install Faceclaw on phone", StatusTone.PROBLEM)
|
||||
link.appRunning == false -> StatusLine("Open Faceclaw on phone", StatusTone.PROBLEM)
|
||||
state == null -> StatusLine("Waiting for phone", StatusTone.BUSY)
|
||||
state.remoteEnabled == false -> StatusLine("Watch control off", StatusTone.PROBLEM)
|
||||
state.connected -> {
|
||||
val battery = state.battery?.let { " · $it%" + if (state.charging) "⚡" else "" } ?: ""
|
||||
StatusLine("Connected$battery", StatusTone.GOOD)
|
||||
}
|
||||
state.phase == "connecting" -> StatusLine("Connecting…", StatusTone.BUSY)
|
||||
state.phase == "disconnecting" -> StatusLine("Disconnecting…", StatusTone.BUSY)
|
||||
else -> StatusLine("Glasses disconnected", StatusTone.OFF)
|
||||
}
|
||||
|
||||
/** The pad's centre line: what the input will land on. */
|
||||
fun focusLine(state: PhoneState?): String = when {
|
||||
state == null || !state.connected -> ""
|
||||
state.locked -> "Locked"
|
||||
!state.screenOn -> "Display off"
|
||||
state.listening -> "Listening…"
|
||||
else -> state.foreground?.title ?: "Launcher"
|
||||
}
|
||||
|
||||
fun StatusTone.color(): Color = when (this) {
|
||||
StatusTone.GOOD -> Color(0xFF7BD88F)
|
||||
StatusTone.BUSY -> Color(0xFFF2C14E)
|
||||
StatusTone.OFF -> Color(0xFF8A8A8A)
|
||||
StatusTone.PROBLEM -> Color(0xFFFF8A80)
|
||||
}
|
||||
|
||||
/** Status dot + text, e.g. "● Connected · 100%". */
|
||||
@Composable
|
||||
fun StatusRow(line: StatusLine, modifier: Modifier = Modifier) {
|
||||
Row(modifier = modifier, verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.Center) {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.size(7.dp)
|
||||
.background(line.tone.color(), CircleShape),
|
||||
)
|
||||
Spacer(modifier = Modifier.width(6.dp))
|
||||
Text(
|
||||
text = line.text,
|
||||
style = MaterialTheme.typography.caption2,
|
||||
color = MaterialTheme.colors.onSurfaceVariant,
|
||||
maxLines = 1,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.faceclaw.wear.ui
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import androidx.wear.compose.material.Colors
|
||||
import androidx.wear.compose.material.MaterialTheme
|
||||
import androidx.wear.compose.navigation.SwipeDismissableNavHost
|
||||
import androidx.wear.compose.navigation.composable
|
||||
import androidx.wear.compose.navigation.rememberSwipeDismissableNavController
|
||||
import com.faceclaw.wear.Haptics
|
||||
import com.faceclaw.wear.PhoneLink
|
||||
import com.faceclaw.wear.WatchPrefs
|
||||
|
||||
object Routes {
|
||||
const val REMOTE = "remote"
|
||||
const val APPS = "apps"
|
||||
const val ASSISTANT = "assistant"
|
||||
const val STATUS = "status"
|
||||
const val SETTINGS = "settings"
|
||||
}
|
||||
|
||||
private val FaceclawColors = Colors(
|
||||
primary = Color(0xFFB8D8FF),
|
||||
primaryVariant = Color(0xFF7FB0F0),
|
||||
secondary = Color(0xFFC9C9C9),
|
||||
secondaryVariant = Color(0xFF8A8A8A),
|
||||
onPrimary = Color(0xFF0B1A2E),
|
||||
onSecondary = Color(0xFF101010),
|
||||
)
|
||||
|
||||
/** Root: the remote pad, with every other screen a swipe-back away. */
|
||||
@Composable
|
||||
fun FaceclawWearApp(link: PhoneLink, prefsStore: WatchPrefs, haptics: Haptics) {
|
||||
val prefs by prefsStore.prefs.collectAsStateWithLifecycle()
|
||||
val navController = rememberSwipeDismissableNavController()
|
||||
|
||||
MaterialTheme(colors = FaceclawColors) {
|
||||
SwipeDismissableNavHost(navController = navController, startDestination = Routes.REMOTE) {
|
||||
composable(Routes.REMOTE) {
|
||||
RemoteScreen(
|
||||
link = link,
|
||||
prefs = prefs,
|
||||
haptics = haptics,
|
||||
onOpenApps = { navController.navigate(Routes.APPS) },
|
||||
onOpenAssistant = { navController.navigate(Routes.ASSISTANT) },
|
||||
onOpenStatus = { navController.navigate(Routes.STATUS) },
|
||||
)
|
||||
}
|
||||
composable(Routes.APPS) {
|
||||
AppsScreen(link = link, haptics = haptics, onDone = { navController.popBackStack() })
|
||||
}
|
||||
composable(Routes.ASSISTANT) {
|
||||
AssistantScreen(link = link, haptics = haptics)
|
||||
}
|
||||
composable(Routes.STATUS) {
|
||||
StatusScreen(link = link, haptics = haptics, onOpenSettings = { navController.navigate(Routes.SETTINGS) })
|
||||
}
|
||||
composable(Routes.SETTINGS) {
|
||||
SettingsScreen(prefs = prefs, store = prefsStore)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,384 @@
|
||||
package com.faceclaw.wear.ui
|
||||
|
||||
import androidx.compose.foundation.Canvas
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.BoxWithConstraints
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.focusable
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.DisposableEffect
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberUpdatedState
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.focus.FocusRequester
|
||||
import androidx.compose.ui.focus.focusRequester
|
||||
import androidx.compose.ui.geometry.Offset
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.drawscope.Stroke
|
||||
import androidx.compose.ui.input.pointer.pointerInput
|
||||
import androidx.compose.ui.input.rotary.onRotaryScrollEvent
|
||||
import androidx.compose.ui.layout.onSizeChanged
|
||||
import androidx.compose.ui.platform.LocalView
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import androidx.wear.compose.material.Button
|
||||
import androidx.wear.compose.material.ButtonDefaults
|
||||
import androidx.wear.compose.material.Icon
|
||||
import androidx.wear.compose.material.MaterialTheme
|
||||
import androidx.wear.compose.material.Text
|
||||
import androidx.wear.compose.material.TimeText
|
||||
import com.faceclaw.wear.Command
|
||||
import com.faceclaw.wear.FingerTapDetector
|
||||
import com.faceclaw.wear.Gesture
|
||||
import com.faceclaw.wear.Haptics
|
||||
import com.faceclaw.wear.OnBodyMonitor
|
||||
import com.faceclaw.wear.PhoneLink
|
||||
import com.faceclaw.wear.Prefs
|
||||
import com.faceclaw.wear.R
|
||||
import com.faceclaw.wear.SwipeAction
|
||||
import com.faceclaw.wear.WristTwistDetector
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlin.math.abs
|
||||
|
||||
private const val MAX_SWIPE_STEPS = 4
|
||||
/** A two-finger swipe scrolls this many steps: a "page". */
|
||||
private const val TWO_FINGER_SWIPE_STEPS = 3
|
||||
private const val FLASH_MS = 160L
|
||||
private const val ARROW_LIT_MS = 220L
|
||||
private const val RING_RADIUS_FRACTION = 0.33f
|
||||
private const val RING_CENTER_Y_FRACTION = 0.47f
|
||||
private val ARROW_INSET = 6.dp
|
||||
private val ARROW_SIZE = 18.dp
|
||||
|
||||
/** The d-pad directions, for lighting the arrow that was just swiped. */
|
||||
enum class Arrow { UP, DOWN, LEFT, RIGHT }
|
||||
|
||||
/**
|
||||
* The touchpad. The whole screen is the pad: tap = select, double-tap =
|
||||
* back, hold = long press (released when the finger lifts), two-finger tap =
|
||||
* the hold menu ("right click"), swipes in four directions = spatial
|
||||
* navigation (crown and two-finger vertical swipes = plain scrolling, a page
|
||||
* at a time for the latter). Fingertip pinch-taps from the motion sensors
|
||||
* select / go back, and two quick wrist twists go back. The small buttons at
|
||||
* the bottom reach the rest.
|
||||
*/
|
||||
@Composable
|
||||
fun RemoteScreen(
|
||||
link: PhoneLink,
|
||||
prefs: Prefs,
|
||||
haptics: Haptics,
|
||||
onOpenApps: () -> Unit,
|
||||
onOpenAssistant: () -> Unit,
|
||||
onOpenStatus: () -> Unit,
|
||||
) {
|
||||
val state by link.state.collectAsStateWithLifecycle()
|
||||
val linkStatus by link.link.collectAsStateWithLifecycle()
|
||||
val notice by link.notice.collectAsStateWithLifecycle()
|
||||
val currentPrefs by rememberUpdatedState(prefs)
|
||||
val view = LocalView.current
|
||||
var holding by remember { mutableStateOf(false) }
|
||||
// Brief ring highlight acknowledging a sensor (tip-tap) gesture.
|
||||
var flashUntil by remember { mutableStateOf(0L) }
|
||||
var flashing by remember { mutableStateOf(false) }
|
||||
// Which d-pad arrow lights up for the swipe just sent.
|
||||
var litArrow by remember { mutableStateOf<Arrow?>(null) }
|
||||
var litTick by remember { mutableStateOf(0L) }
|
||||
val focusRequester = remember { FocusRequester() }
|
||||
var crownAccumulator by remember { mutableStateOf(0f) }
|
||||
// Pad height in px, for the tap zones; written by onSizeChanged, read by
|
||||
// the gesture callbacks (plain holder: the callbacks are remembered once).
|
||||
val padHeight = remember { floatArrayOf(1f) }
|
||||
|
||||
val keyboardToApp = rememberKeyboardLauncher(
|
||||
label = stringResource(R.string.keyboard_label_app),
|
||||
onResult = { text -> haptics.click(); link.sendText(text) },
|
||||
onUnavailable = { haptics.error() },
|
||||
)
|
||||
|
||||
fun scroll(gesture: Gesture, steps: Int) {
|
||||
haptics.tick()
|
||||
link.sendGesture(gesture, steps)
|
||||
}
|
||||
|
||||
fun light(arrow: Arrow) {
|
||||
litArrow = arrow
|
||||
litTick = System.currentTimeMillis()
|
||||
}
|
||||
|
||||
fun click() {
|
||||
haptics.click()
|
||||
link.sendGesture(Gesture.CLICK)
|
||||
}
|
||||
|
||||
fun doubleClick() {
|
||||
haptics.doubleClick()
|
||||
link.sendGesture(Gesture.DOUBLE_CLICK)
|
||||
}
|
||||
|
||||
/** Run a horizontal-swipe action; `left` says which way the finger went. */
|
||||
fun perform(action: SwipeAction, left: Boolean) {
|
||||
when (action) {
|
||||
SwipeAction.NAVIGATE -> { haptics.click(); link.sendGesture(if (left) Gesture.SWIPE_LEFT else Gesture.SWIPE_RIGHT) }
|
||||
SwipeAction.SIDEBAR -> { haptics.click(); link.sendCommand(Command.SIDEBAR) }
|
||||
SwipeAction.DOUBLE_CLICK -> doubleClick()
|
||||
SwipeAction.CLICK -> click()
|
||||
SwipeAction.LONG_PRESS -> { haptics.heavy(); link.sendGesture(Gesture.LONG_PRESS) }
|
||||
SwipeAction.WAKEWORD -> { haptics.click(); link.sendGesture(Gesture.WAKEWORD) }
|
||||
SwipeAction.NONE -> Unit
|
||||
}
|
||||
}
|
||||
|
||||
/** Whether a vertical swipe means "up", honouring the natural-scroll preference. */
|
||||
fun swipeIsUp(dy: Float): Boolean {
|
||||
// Ring convention: swiping up means up. "Natural" flips it so the
|
||||
// content follows the finger like a touch screen.
|
||||
return if (currentPrefs.naturalScroll) dy > 0 else dy < 0
|
||||
}
|
||||
|
||||
val callbacks = remember(link, haptics) {
|
||||
TouchpadCallbacks(
|
||||
onTap = { position ->
|
||||
if (currentPrefs.tapZones) {
|
||||
// Top third scrolls up, bottom third down, the middle clicks.
|
||||
val zone = position.y / padHeight[0].coerceAtLeast(1f)
|
||||
when {
|
||||
zone < 1f / 3f -> scroll(Gesture.SCROLL_UP, 1)
|
||||
zone > 2f / 3f -> scroll(Gesture.SCROLL_DOWN, 1)
|
||||
else -> click()
|
||||
}
|
||||
} else {
|
||||
click()
|
||||
}
|
||||
},
|
||||
onDoubleTap = { _ -> doubleClick() },
|
||||
// Two fingers = back as well: easier than timing a double-tap.
|
||||
onTwoFingerTap = { doubleClick() },
|
||||
onTwoFingerSwipe = { dx, dy ->
|
||||
if (abs(dy) >= abs(dx)) {
|
||||
scroll(if (swipeIsUp(dy)) Gesture.SCROLL_UP else Gesture.SCROLL_DOWN, TWO_FINGER_SWIPE_STEPS)
|
||||
} else {
|
||||
perform(if (dx < 0) currentPrefs.swipeLeft else currentPrefs.swipeRight, left = dx < 0)
|
||||
}
|
||||
},
|
||||
onLongPressStart = {
|
||||
holding = true
|
||||
haptics.heavy()
|
||||
link.sendGesture(Gesture.LONG_PRESS_START)
|
||||
},
|
||||
onLongPressEnd = {
|
||||
holding = false
|
||||
haptics.click()
|
||||
link.sendGesture(Gesture.LONG_PRESS_RELEASE)
|
||||
},
|
||||
onSwipe = { dx, dy, _ ->
|
||||
if (abs(dy) >= abs(dx)) {
|
||||
// One step per swipe, regardless of length: the glasses
|
||||
// repaint per step, and multi-step swipes overshot lists
|
||||
// whenever the display lagged the finger. Fast repeated
|
||||
// swiping, the crown, or a two-finger page swipe cover
|
||||
// long distances instead.
|
||||
val up = swipeIsUp(dy)
|
||||
light(if (up) Arrow.UP else Arrow.DOWN)
|
||||
scroll(if (up) Gesture.SWIPE_UP else Gesture.SWIPE_DOWN, 1)
|
||||
} else {
|
||||
light(if (dx < 0) Arrow.LEFT else Arrow.RIGHT)
|
||||
perform(if (dx < 0) currentPrefs.swipeLeft else currentPrefs.swipeRight, left = dx < 0)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// Motion-sensor gestures only count on a wrist (see OnBodyMonitor).
|
||||
val onBody = remember { OnBodyMonitor(view.context) }
|
||||
DisposableEffect(onBody) {
|
||||
onBody.start()
|
||||
onDispose { onBody.stop() }
|
||||
}
|
||||
|
||||
// Fingertip (pinch) taps from the motion sensors, only while the pad is up.
|
||||
val fingerTapsEnabled = prefs.fingerTaps
|
||||
DisposableEffect(fingerTapsEnabled, link, haptics) {
|
||||
if (!fingerTapsEnabled) return@DisposableEffect onDispose { }
|
||||
val detector = FingerTapDetector(
|
||||
context = view.context,
|
||||
sensitivity = { currentPrefs.tapSensitivity },
|
||||
quietUntil = { haptics.quietUntil() },
|
||||
onBody = { onBody.onBody },
|
||||
onTap = { flashUntil = System.currentTimeMillis() + FLASH_MS; click() },
|
||||
onDoubleTap = { flashUntil = System.currentTimeMillis() + FLASH_MS; doubleClick() },
|
||||
)
|
||||
detector.start()
|
||||
onDispose { detector.stop() }
|
||||
}
|
||||
// Two quick wrist twists = back, from the gyroscope, only while the pad is up.
|
||||
val wristTwistEnabled = prefs.wristTwist
|
||||
DisposableEffect(wristTwistEnabled, link, haptics) {
|
||||
if (!wristTwistEnabled) return@DisposableEffect onDispose { }
|
||||
val detector = WristTwistDetector(
|
||||
context = view.context,
|
||||
sensitivity = { currentPrefs.twistSensitivity },
|
||||
quietUntil = { haptics.quietUntil() },
|
||||
onBody = { onBody.onBody },
|
||||
onDoubleTwist = { flashUntil = System.currentTimeMillis() + FLASH_MS; doubleClick() },
|
||||
)
|
||||
detector.start()
|
||||
onDispose { detector.stop() }
|
||||
}
|
||||
LaunchedEffect(litTick) {
|
||||
if (litTick == 0L) return@LaunchedEffect
|
||||
delay(ARROW_LIT_MS)
|
||||
litArrow = null
|
||||
}
|
||||
LaunchedEffect(flashUntil) {
|
||||
if (flashUntil == 0L) return@LaunchedEffect
|
||||
flashing = true
|
||||
delay((flashUntil - System.currentTimeMillis()).coerceAtLeast(1L))
|
||||
flashing = false
|
||||
}
|
||||
|
||||
LaunchedEffect(Unit) { focusRequester.requestFocus() }
|
||||
|
||||
// A refused or unanswered gesture gets a distinct buzz, so the wrist knows
|
||||
// without looking (the notice pill says why).
|
||||
LaunchedEffect(notice?.id) {
|
||||
if (notice?.isError == true) haptics.error()
|
||||
}
|
||||
|
||||
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.onRotaryScrollEvent { event ->
|
||||
// Clockwise (positive) scrolls down, as in every Wear list.
|
||||
crownAccumulator += event.verticalScrollPixels
|
||||
val threshold = currentPrefs.crownSensitivity.pixelsPerStep
|
||||
var steps = 0
|
||||
while (crownAccumulator >= threshold) { crownAccumulator -= threshold; steps++ }
|
||||
while (crownAccumulator <= -threshold) { crownAccumulator += threshold; steps-- }
|
||||
// Capped low: a fast twirl otherwise outruns the display.
|
||||
if (steps > 0) { light(Arrow.DOWN); scroll(Gesture.SCROLL_DOWN, steps.coerceAtMost(2)) }
|
||||
if (steps < 0) { light(Arrow.UP); scroll(Gesture.SCROLL_UP, (-steps).coerceAtMost(2)) }
|
||||
true
|
||||
}
|
||||
.focusRequester(focusRequester)
|
||||
.focusable()
|
||||
.onSizeChanged { padHeight[0] = it.height.toFloat() }
|
||||
.pointerInput(callbacks) { detectTouchpadGestures(callbacks) },
|
||||
) {
|
||||
// The pad: a ring with a d-pad arrow at each compass point (the one
|
||||
// you swiped lights up), the current glasses app in the middle, and
|
||||
// the tap legend under it. Brightens while a hold is in progress.
|
||||
BoxWithConstraints(modifier = Modifier.fillMaxSize()) {
|
||||
val ringRadius = minOf(maxWidth, maxHeight) * RING_RADIUS_FRACTION
|
||||
val ringCenterY = maxHeight * RING_CENTER_Y_FRACTION
|
||||
val ringColor = if (holding || flashing) MaterialTheme.colors.primary else Color(0xFF2E2E2E)
|
||||
Canvas(modifier = Modifier.fillMaxSize()) {
|
||||
drawCircle(
|
||||
color = ringColor,
|
||||
radius = ringRadius.toPx(),
|
||||
center = Offset(size.width / 2f, ringCenterY.toPx()),
|
||||
style = Stroke(width = if (holding || flashing) 4f else 2f),
|
||||
)
|
||||
}
|
||||
|
||||
TimeText()
|
||||
StatusRow(
|
||||
statusLine(state, linkStatus),
|
||||
modifier = Modifier.align(Alignment.TopCenter).padding(top = 28.dp, start = 36.dp, end = 36.dp),
|
||||
)
|
||||
|
||||
ArrowGlyph("▲", litArrow == Arrow.UP, Modifier.align(Alignment.TopCenter).padding(top = ringCenterY - ringRadius + ARROW_INSET))
|
||||
ArrowGlyph("▼", litArrow == Arrow.DOWN, Modifier.align(Alignment.TopCenter).padding(top = ringCenterY + ringRadius - ARROW_INSET - ARROW_SIZE))
|
||||
ArrowGlyph("◀", litArrow == Arrow.LEFT, Modifier.align(Alignment.TopStart).padding(start = maxWidth / 2 - ringRadius + ARROW_INSET, top = ringCenterY - ARROW_SIZE / 2))
|
||||
ArrowGlyph("▶", litArrow == Arrow.RIGHT, Modifier.align(Alignment.TopEnd).padding(end = maxWidth / 2 - ringRadius + ARROW_INSET, top = ringCenterY - ARROW_SIZE / 2))
|
||||
|
||||
// What the next gesture lands on (the app on the glasses, or the
|
||||
// state in the way of it) above the centre line; the tap legend
|
||||
// below it, clear of the side arrows.
|
||||
val focus = if (holding) "holding" else focusLine(state).ifEmpty { "—" }
|
||||
Text(
|
||||
text = focus,
|
||||
style = MaterialTheme.typography.title3,
|
||||
color = if (holding) MaterialTheme.colors.primary else MaterialTheme.colors.onSurface,
|
||||
textAlign = TextAlign.Center,
|
||||
maxLines = 1,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
modifier = Modifier
|
||||
.align(Alignment.TopCenter)
|
||||
.padding(top = ringCenterY - 44.dp, start = 60.dp, end = 60.dp),
|
||||
)
|
||||
Text(
|
||||
text = if (prefs.tapZones) "▲▼ tap zones · ● select" else "● select ●● back ‒ menu",
|
||||
style = MaterialTheme.typography.caption3,
|
||||
color = MaterialTheme.colors.onSurfaceVariant,
|
||||
textAlign = TextAlign.Center,
|
||||
maxLines = 1,
|
||||
modifier = Modifier
|
||||
.align(Alignment.TopCenter)
|
||||
.padding(top = ringCenterY + 18.dp, start = 56.dp, end = 56.dp),
|
||||
)
|
||||
|
||||
Row(
|
||||
// Four 36dp buttons need 144dp; at this height the round
|
||||
// display's chord is ~186dp wide, so 36dp margins keep every
|
||||
// button inside the glass.
|
||||
modifier = Modifier
|
||||
.align(Alignment.BottomCenter)
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 36.dp, vertical = 26.dp),
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
PadButton(icon = R.drawable.ic_assistant, description = "Assistant", onClick = onOpenAssistant)
|
||||
PadButton(icon = R.drawable.ic_keyboard, description = "Type into app", onClick = keyboardToApp)
|
||||
PadButton(icon = R.drawable.ic_apps, description = "Apps", onClick = onOpenApps)
|
||||
PadButton(icon = R.drawable.ic_more, description = "Status and settings", onClick = onOpenStatus)
|
||||
}
|
||||
}
|
||||
|
||||
NoticeOverlay(notice)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun ArrowGlyph(glyph: String, lit: Boolean, modifier: Modifier) {
|
||||
Text(
|
||||
text = glyph,
|
||||
style = MaterialTheme.typography.caption1,
|
||||
color = if (lit) MaterialTheme.colors.primary else Color(0xFF6E6E6E),
|
||||
textAlign = TextAlign.Center,
|
||||
modifier = modifier.size(ARROW_SIZE),
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun PadButton(icon: Int, description: String, onClick: () -> Unit) {
|
||||
Button(
|
||||
onClick = onClick,
|
||||
modifier = Modifier.size(36.dp),
|
||||
colors = ButtonDefaults.secondaryButtonColors(),
|
||||
) {
|
||||
Icon(
|
||||
painter = painterResource(icon),
|
||||
contentDescription = description,
|
||||
modifier = Modifier.size(20.dp),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
package com.faceclaw.wear.ui
|
||||
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.wear.compose.foundation.lazy.ScalingLazyColumn
|
||||
import androidx.wear.compose.foundation.lazy.rememberScalingLazyListState
|
||||
import androidx.wear.compose.material.Chip
|
||||
import androidx.wear.compose.material.ChipDefaults
|
||||
import androidx.wear.compose.material.ListHeader
|
||||
import androidx.wear.compose.material.MaterialTheme
|
||||
import androidx.wear.compose.material.PositionIndicator
|
||||
import androidx.wear.compose.material.Scaffold
|
||||
import androidx.wear.compose.material.Switch
|
||||
import androidx.wear.compose.material.Text
|
||||
import androidx.wear.compose.material.TimeText
|
||||
import androidx.wear.compose.material.ToggleChip
|
||||
import com.faceclaw.wear.BuildConfig
|
||||
import com.faceclaw.wear.Prefs
|
||||
import com.faceclaw.wear.WatchPrefs
|
||||
|
||||
/** Watch-local preferences: feel of the pad and crown. */
|
||||
@Composable
|
||||
fun SettingsScreen(prefs: Prefs, store: WatchPrefs) {
|
||||
val listState = rememberScalingLazyListState()
|
||||
Scaffold(
|
||||
timeText = { TimeText() },
|
||||
positionIndicator = { PositionIndicator(scalingLazyListState = listState) },
|
||||
) {
|
||||
ScalingLazyColumn(state = listState, modifier = Modifier.fillMaxSize()) {
|
||||
item { ListHeader { Text("Touchpad") } }
|
||||
item {
|
||||
ToggleChip(
|
||||
checked = prefs.haptics,
|
||||
onCheckedChange = { on -> store.update { it.copy(haptics = on) } },
|
||||
label = { Text("Vibrate on input") },
|
||||
toggleControl = { Switch(checked = prefs.haptics) },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
}
|
||||
item {
|
||||
ToggleChip(
|
||||
checked = prefs.keepScreenOn,
|
||||
onCheckedChange = { on -> store.update { it.copy(keepScreenOn = on) } },
|
||||
label = { Text("Keep screen on") },
|
||||
secondaryLabel = { Text("while Faceclaw is in front") },
|
||||
toggleControl = { Switch(checked = prefs.keepScreenOn) },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
}
|
||||
item {
|
||||
ToggleChip(
|
||||
checked = prefs.naturalScroll,
|
||||
onCheckedChange = { on -> store.update { it.copy(naturalScroll = on) } },
|
||||
label = { Text("Natural swipe") },
|
||||
secondaryLabel = { Text(if (prefs.naturalScroll) "content follows finger" else "swipe up = scroll up (ring)") },
|
||||
toggleControl = { Switch(checked = prefs.naturalScroll) },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
}
|
||||
item {
|
||||
ToggleChip(
|
||||
checked = prefs.tapZones,
|
||||
onCheckedChange = { on -> store.update { it.copy(tapZones = on) } },
|
||||
label = { Text("Tap zones") },
|
||||
secondaryLabel = { Text("tap top/bottom to scroll") },
|
||||
toggleControl = { Switch(checked = prefs.tapZones) },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
}
|
||||
item {
|
||||
Chip(
|
||||
onClick = { store.update { it.copy(swipeLeft = it.swipeLeft.next()) } },
|
||||
label = { Text("Swipe left") },
|
||||
secondaryLabel = { Text(prefs.swipeLeft.label) },
|
||||
colors = ChipDefaults.secondaryChipColors(),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
}
|
||||
item {
|
||||
Chip(
|
||||
onClick = { store.update { it.copy(swipeRight = it.swipeRight.next()) } },
|
||||
label = { Text("Swipe right") },
|
||||
secondaryLabel = { Text(prefs.swipeRight.label) },
|
||||
colors = ChipDefaults.secondaryChipColors(),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
}
|
||||
item { ListHeader { Text("Tip taps") } }
|
||||
item {
|
||||
ToggleChip(
|
||||
checked = prefs.fingerTaps,
|
||||
onCheckedChange = { on -> store.update { it.copy(fingerTaps = on) } },
|
||||
label = { Text("Fingertip taps (experimental)") },
|
||||
secondaryLabel = { Text("finger to thumb: select / double = back") },
|
||||
toggleControl = { Switch(checked = prefs.fingerTaps) },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
}
|
||||
item {
|
||||
Chip(
|
||||
onClick = { store.update { it.copy(tapSensitivity = it.tapSensitivity.next()) } },
|
||||
label = { Text("Tap sensitivity") },
|
||||
secondaryLabel = { Text(prefs.tapSensitivity.label) },
|
||||
colors = ChipDefaults.secondaryChipColors(),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
enabled = prefs.fingerTaps,
|
||||
)
|
||||
}
|
||||
item {
|
||||
Text(
|
||||
"Experimental: taps are read from the motion sensors while the pad is open, and ignored while the arm is moving.",
|
||||
style = MaterialTheme.typography.caption3,
|
||||
color = MaterialTheme.colors.onSurfaceVariant,
|
||||
textAlign = TextAlign.Center,
|
||||
)
|
||||
}
|
||||
item { ListHeader { Text("Wrist twist") } }
|
||||
item {
|
||||
ToggleChip(
|
||||
checked = prefs.wristTwist,
|
||||
onCheckedChange = { on -> store.update { it.copy(wristTwist = on) } },
|
||||
label = { Text("Twist twice = back") },
|
||||
secondaryLabel = { Text("two quick wrist flicks") },
|
||||
toggleControl = { Switch(checked = prefs.wristTwist) },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
}
|
||||
item {
|
||||
Chip(
|
||||
onClick = { store.update { it.copy(twistSensitivity = it.twistSensitivity.next()) } },
|
||||
label = { Text("Twist sensitivity") },
|
||||
secondaryLabel = { Text(prefs.twistSensitivity.label) },
|
||||
colors = ChipDefaults.secondaryChipColors(),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
enabled = prefs.wristTwist,
|
||||
)
|
||||
}
|
||||
item { ListHeader { Text("Crown") } }
|
||||
item {
|
||||
Chip(
|
||||
onClick = { store.update { it.copy(crownSensitivity = it.crownSensitivity.next()) } },
|
||||
label = { Text("Sensitivity") },
|
||||
secondaryLabel = { Text(prefs.crownSensitivity.label) },
|
||||
colors = ChipDefaults.secondaryChipColors(),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
}
|
||||
item { ListHeader { Text("Buttons") } }
|
||||
item {
|
||||
Text(
|
||||
"Side button 1: tap = select, hold = long press.\nButton 2: back.\nButton 3: “Hey Even”.",
|
||||
style = MaterialTheme.typography.caption2,
|
||||
color = MaterialTheme.colors.onSurfaceVariant,
|
||||
textAlign = TextAlign.Center,
|
||||
)
|
||||
}
|
||||
item {
|
||||
Text(
|
||||
"Faceclaw watch v${BuildConfig.VERSION_NAME}",
|
||||
style = MaterialTheme.typography.caption3,
|
||||
color = MaterialTheme.colors.onSurfaceVariant,
|
||||
textAlign = TextAlign.Center,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
package com.faceclaw.wear.ui
|
||||
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import androidx.wear.compose.foundation.lazy.ScalingLazyColumn
|
||||
import androidx.wear.compose.foundation.lazy.rememberScalingLazyListState
|
||||
import androidx.wear.compose.material.Chip
|
||||
import androidx.wear.compose.material.ChipDefaults
|
||||
import androidx.wear.compose.material.Icon
|
||||
import androidx.wear.compose.material.ListHeader
|
||||
import androidx.wear.compose.material.PositionIndicator
|
||||
import androidx.wear.compose.material.Scaffold
|
||||
import androidx.wear.compose.material.Text
|
||||
import androidx.wear.compose.material.TimeText
|
||||
import com.faceclaw.wear.Command
|
||||
import com.faceclaw.wear.DisplayMode
|
||||
import com.faceclaw.wear.Haptics
|
||||
import com.faceclaw.wear.PhoneLink
|
||||
import com.faceclaw.wear.R
|
||||
|
||||
/** Glasses and phone status, with the connection / display / lock switches. */
|
||||
@Composable
|
||||
fun StatusScreen(link: PhoneLink, haptics: Haptics, onOpenSettings: () -> Unit) {
|
||||
val state by link.state.collectAsStateWithLifecycle()
|
||||
val linkStatus by link.link.collectAsStateWithLifecycle()
|
||||
val notice by link.notice.collectAsStateWithLifecycle()
|
||||
val listState = rememberScalingLazyListState()
|
||||
val connected = state?.connected == true
|
||||
|
||||
Scaffold(
|
||||
timeText = { TimeText() },
|
||||
positionIndicator = { PositionIndicator(scalingLazyListState = listState) },
|
||||
) {
|
||||
ScalingLazyColumn(state = listState, modifier = Modifier.fillMaxSize()) {
|
||||
item { ListHeader { Text("Glasses") } }
|
||||
item {
|
||||
val phaseLabel = when {
|
||||
state == null -> "No status yet"
|
||||
connected -> "Connected" + (state?.battery?.let { " · $it%" } ?: "") + if (state?.charging == true) " charging" else ""
|
||||
state?.phase == "connecting" -> "Connecting…"
|
||||
state?.phase == "disconnecting" -> "Disconnecting…"
|
||||
else -> "Disconnected"
|
||||
}
|
||||
Chip(
|
||||
onClick = {
|
||||
haptics.click()
|
||||
link.sendCommand(if (connected) Command.DISCONNECT else Command.CONNECT)
|
||||
},
|
||||
label = { Text(phaseLabel, maxLines = 1, overflow = TextOverflow.Ellipsis) },
|
||||
secondaryLabel = { Text(if (connected) "tap to disconnect" else "tap to connect") },
|
||||
colors = if (connected) ChipDefaults.primaryChipColors() else ChipDefaults.secondaryChipColors(),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
enabled = linkStatus.appRunning != false,
|
||||
)
|
||||
}
|
||||
item {
|
||||
val on = state?.screenOn == true
|
||||
Chip(
|
||||
onClick = {
|
||||
haptics.click()
|
||||
link.sendCommand(if (on) Command.SLEEP else Command.WAKE)
|
||||
},
|
||||
label = { Text(if (on) "Display on" else "Display off") },
|
||||
secondaryLabel = { Text(if (on) "tap to turn off" else "tap to wake") },
|
||||
colors = ChipDefaults.secondaryChipColors(),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
enabled = connected && state?.locked != true,
|
||||
)
|
||||
}
|
||||
item {
|
||||
val locked = state?.locked == true
|
||||
Chip(
|
||||
onClick = {
|
||||
haptics.click()
|
||||
link.sendCommand(if (locked) Command.UNLOCK else Command.LOCK)
|
||||
},
|
||||
label = { Text(if (locked) "Locked" else "Unlocked") },
|
||||
secondaryLabel = {
|
||||
Text(
|
||||
when {
|
||||
locked && state?.canUnlock == false -> "unlock from the phone"
|
||||
locked -> "tap to unlock"
|
||||
else -> "tap to lock"
|
||||
},
|
||||
)
|
||||
},
|
||||
colors = ChipDefaults.secondaryChipColors(),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
enabled = connected && !(locked && state?.canUnlock == false),
|
||||
)
|
||||
}
|
||||
item {
|
||||
val mode = DisplayMode.fromWire(state?.displayMode ?: "") ?: DisplayMode.BAND
|
||||
Chip(
|
||||
onClick = {
|
||||
haptics.click()
|
||||
link.sendCommand(Command.DISPLAY_MODE, "value" to mode.next().wire)
|
||||
},
|
||||
label = { Text("Display mode") },
|
||||
secondaryLabel = { Text(mode.label) },
|
||||
colors = ChipDefaults.secondaryChipColors(),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
enabled = connected,
|
||||
)
|
||||
}
|
||||
state?.foreground?.let { foreground ->
|
||||
item {
|
||||
Chip(
|
||||
onClick = { haptics.click(); link.sendCommand(Command.SIDEBAR) },
|
||||
label = { Text("On screen: ${foreground.title}", maxLines = 1, overflow = TextOverflow.Ellipsis) },
|
||||
secondaryLabel = { Text("tap to open the sidebar") },
|
||||
colors = ChipDefaults.secondaryChipColors(),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
enabled = connected,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
item { ListHeader { Text("Phone") } }
|
||||
item {
|
||||
val phoneLabel = when {
|
||||
!linkStatus.phoneReachable -> "No phone connected"
|
||||
!linkStatus.phoneAppInstalled -> "Faceclaw not installed"
|
||||
linkStatus.appRunning == false -> "Faceclaw not running"
|
||||
else -> linkStatus.phoneName.ifBlank { "Phone" }
|
||||
}
|
||||
val detail = when {
|
||||
!linkStatus.phoneReachable -> "check Bluetooth"
|
||||
!linkStatus.phoneAppInstalled -> "install it on the phone"
|
||||
linkStatus.appRunning == false -> "open it on the phone"
|
||||
state?.version?.isNotBlank() == true -> "Faceclaw v${state?.version}"
|
||||
else -> "tap to refresh"
|
||||
}
|
||||
Chip(
|
||||
onClick = { haptics.click(); link.refresh() },
|
||||
label = { Text(phoneLabel, maxLines = 1, overflow = TextOverflow.Ellipsis) },
|
||||
secondaryLabel = { Text(detail) },
|
||||
icon = { Icon(painterResource(R.drawable.ic_refresh), contentDescription = null, modifier = Modifier.size(20.dp)) },
|
||||
colors = ChipDefaults.secondaryChipColors(),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
}
|
||||
if (state?.remoteEnabled == false) {
|
||||
item {
|
||||
Text(
|
||||
"Watch control is turned off in Faceclaw's Settings > Watch.",
|
||||
style = androidx.wear.compose.material.MaterialTheme.typography.caption2,
|
||||
)
|
||||
}
|
||||
}
|
||||
item {
|
||||
Chip(
|
||||
onClick = onOpenSettings,
|
||||
label = { Text("Watch settings") },
|
||||
icon = { Icon(painterResource(R.drawable.ic_settings), contentDescription = null, modifier = Modifier.size(20.dp)) },
|
||||
colors = ChipDefaults.secondaryChipColors(),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
}
|
||||
}
|
||||
NoticeOverlay(notice)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.faceclaw.wear.ui
|
||||
|
||||
import android.app.Activity
|
||||
import android.app.RemoteInput
|
||||
import android.content.ActivityNotFoundException
|
||||
import android.content.Intent
|
||||
import android.speech.RecognizerIntent
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.rememberUpdatedState
|
||||
import androidx.wear.input.RemoteInputIntentHelper
|
||||
|
||||
private const val REMOTE_INPUT_KEY = "faceclaw_text"
|
||||
|
||||
/**
|
||||
* The watch's own speech recognizer (the system's, so it works with the
|
||||
* wrist raised and no glasses mic involved). Returns a launcher; the
|
||||
* recognized text arrives in onResult.
|
||||
*/
|
||||
@Composable
|
||||
fun rememberSpeechLauncher(
|
||||
prompt: String,
|
||||
onResult: (String) -> Unit,
|
||||
onUnavailable: () -> Unit,
|
||||
): () -> Unit {
|
||||
val currentOnResult = rememberUpdatedState(onResult)
|
||||
val launcher = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
|
||||
if (result.resultCode != Activity.RESULT_OK) return@rememberLauncherForActivityResult
|
||||
val text = result.data
|
||||
?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
|
||||
?.firstOrNull()
|
||||
?.trim()
|
||||
if (!text.isNullOrEmpty()) currentOnResult.value(text)
|
||||
}
|
||||
return {
|
||||
val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
|
||||
.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
|
||||
.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt)
|
||||
try {
|
||||
launcher.launch(intent)
|
||||
} catch (error: ActivityNotFoundException) {
|
||||
onUnavailable()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The Wear OS text entry sheet (keyboard / voice / handwriting, whatever the
|
||||
* watch offers). Returns a launcher; the entered text arrives in onResult.
|
||||
*/
|
||||
@Composable
|
||||
fun rememberKeyboardLauncher(
|
||||
label: String,
|
||||
onResult: (String) -> Unit,
|
||||
onUnavailable: () -> Unit,
|
||||
): () -> Unit {
|
||||
val currentOnResult = rememberUpdatedState(onResult)
|
||||
val launcher = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
|
||||
if (result.resultCode != Activity.RESULT_OK) return@rememberLauncherForActivityResult
|
||||
val data = result.data ?: return@rememberLauncherForActivityResult
|
||||
val text = RemoteInput.getResultsFromIntent(data)
|
||||
?.getCharSequence(REMOTE_INPUT_KEY)
|
||||
?.toString()
|
||||
?.trim()
|
||||
if (!text.isNullOrEmpty()) currentOnResult.value(text)
|
||||
}
|
||||
return {
|
||||
val remoteInput = RemoteInput.Builder(REMOTE_INPUT_KEY)
|
||||
.setLabel(label)
|
||||
.setAllowFreeFormInput(true)
|
||||
.build()
|
||||
val intent = RemoteInputIntentHelper.createActionRemoteInputIntent()
|
||||
RemoteInputIntentHelper.putRemoteInputsExtra(intent, listOf(remoteInput))
|
||||
RemoteInputIntentHelper.putTitleExtra(intent, label)
|
||||
try {
|
||||
launcher.launch(intent)
|
||||
} catch (error: ActivityNotFoundException) {
|
||||
onUnavailable()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package com.faceclaw.wear.ui
|
||||
|
||||
import androidx.compose.foundation.gestures.awaitEachGesture
|
||||
import androidx.compose.foundation.gestures.awaitFirstDown
|
||||
import androidx.compose.foundation.gestures.waitForUpOrCancellation
|
||||
import androidx.compose.ui.geometry.Offset
|
||||
import androidx.compose.ui.input.pointer.PointerInputScope
|
||||
import androidx.compose.ui.input.pointer.positionChanged
|
||||
|
||||
/** What the remote's touch surface can report. */
|
||||
class TouchpadCallbacks(
|
||||
val onTap: (position: Offset) -> Unit,
|
||||
val onDoubleTap: (position: Offset) -> Unit,
|
||||
val onTwoFingerTap: () -> Unit,
|
||||
/** Two fingers travelled past touch slop; dx/dy are the primary finger's displacement. */
|
||||
val onTwoFingerSwipe: (dx: Float, dy: Float) -> Unit,
|
||||
val onLongPressStart: () -> Unit,
|
||||
val onLongPressEnd: () -> Unit,
|
||||
/** Finger travelled past touch slop; dx/dy are the total displacement. */
|
||||
val onSwipe: (dx: Float, dy: Float, durationMs: Long) -> Unit,
|
||||
)
|
||||
|
||||
/**
|
||||
* One detector for everything the pad does, so a gesture is reported exactly
|
||||
* once: tap, double-tap (a second tap inside the double-tap timeout), a hold
|
||||
* (reported at the long-press timeout and again on release), a swipe, and a
|
||||
* two-finger tap or swipe. Written as a single awaitEachGesture loop because the
|
||||
* stock detectors would each claim the same finger.
|
||||
*/
|
||||
suspend fun PointerInputScope.detectTouchpadGestures(callbacks: TouchpadCallbacks) {
|
||||
val longPressTimeout = viewConfiguration.longPressTimeoutMillis
|
||||
val doubleTapTimeout = viewConfiguration.doubleTapTimeoutMillis
|
||||
val slop = viewConfiguration.touchSlop
|
||||
|
||||
awaitEachGesture {
|
||||
val down = awaitFirstDown()
|
||||
down.consume()
|
||||
val downTime = down.uptimeMillis
|
||||
var maxPointers = 1
|
||||
var moved = false
|
||||
var longPressed = false
|
||||
var displacement = Offset.Zero
|
||||
var releaseTime = downTime
|
||||
|
||||
while (true) {
|
||||
val remaining = if (longPressed || moved || maxPointers > 1) {
|
||||
HOLD_POLL_MS
|
||||
} else {
|
||||
longPressTimeout - (System.currentTimeMillis() - wallClockAt(downTime))
|
||||
}
|
||||
val event = withTimeoutOrNull(remaining.coerceAtLeast(1L)) { awaitPointerEvent() }
|
||||
if (event == null) {
|
||||
if (!longPressed && !moved && maxPointers == 1) {
|
||||
longPressed = true
|
||||
callbacks.onLongPressStart()
|
||||
}
|
||||
continue
|
||||
}
|
||||
// Something above us (the system, a parent) took the gesture:
|
||||
// Compose reports that as already-consumed changes. Drop it rather
|
||||
// than mistaking the cancellation for a tap.
|
||||
if (event.changes.any { it.isConsumed }) break
|
||||
val pressedCount = event.changes.count { it.pressed }
|
||||
if (pressedCount > maxPointers) maxPointers = pressedCount
|
||||
val primary = event.changes.firstOrNull { it.id == down.id } ?: event.changes.first()
|
||||
if (primary.positionChanged()) {
|
||||
displacement = primary.position - down.position
|
||||
if (!moved && !longPressed && displacement.getDistance() > slop) moved = true
|
||||
}
|
||||
for (change in event.changes) change.consume()
|
||||
if (pressedCount > 0) continue
|
||||
|
||||
releaseTime = event.changes.maxOf { it.uptimeMillis }
|
||||
val duration = releaseTime - downTime
|
||||
when {
|
||||
longPressed -> callbacks.onLongPressEnd()
|
||||
maxPointers >= 2 -> if (moved) callbacks.onTwoFingerSwipe(displacement.x, displacement.y) else callbacks.onTwoFingerTap()
|
||||
moved -> callbacks.onSwipe(displacement.x, displacement.y, duration)
|
||||
else -> {
|
||||
// A plain tap; give a second tap a chance to make it a double.
|
||||
val second = withTimeoutOrNull(doubleTapTimeout) { awaitFirstDown() }
|
||||
if (second == null) {
|
||||
callbacks.onTap(down.position)
|
||||
} else {
|
||||
second.consume()
|
||||
val up = waitForUpOrCancellation()
|
||||
if (up != null) up.consume()
|
||||
callbacks.onDoubleTap(down.position)
|
||||
}
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Poll interval while a hold or drag is in progress (no timeout semantics needed). */
|
||||
private const val HOLD_POLL_MS = 60_000L
|
||||
|
||||
/**
|
||||
* Pointer timestamps are SystemClock.uptimeMillis; map one to wall-clock so the
|
||||
* remaining long-press time can be computed without a second clock.
|
||||
*/
|
||||
private fun wallClockAt(uptimeMillis: Long): Long =
|
||||
System.currentTimeMillis() - (android.os.SystemClock.uptimeMillis() - uptimeMillis)
|
||||
@@ -0,0 +1,9 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24">
|
||||
<path
|
||||
android:fillColor="@android:color/white"
|
||||
android:pathData="M4,8h4L8,4L4,4v4zM10,20h4v-4h-4v4zM4,20h4v-4L4,16v4zM4,14h4v-4L4,10v4zM10,14h4v-4h-4v4zM16,4v4h4L20,4h-4zM10,8h4L14,4h-4v4zM16,14h4v-4h-4v4zM16,20h4v-4h-4v4z" />
|
||||
</vector>
|
||||
@@ -0,0 +1,9 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24">
|
||||
<path
|
||||
android:fillColor="@android:color/white"
|
||||
android:pathData="M19,9l1.25,-2.75L23,5l-2.75,-1.25L19,1l-1.25,2.75L15,5l2.75,1.25L19,9zM11.5,9.5L9,4 6.5,9.5 1,12l5.5,2.5L9,20l2.5,-5.5L17,12l-5.5,-2.5zM19,15l-1.25,2.75L15,19l2.75,1.25L19,23l1.25,-2.75L23,19l-2.75,-1.25L19,15z" />
|
||||
</vector>
|
||||
@@ -0,0 +1,9 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24">
|
||||
<path
|
||||
android:fillColor="@android:color/white"
|
||||
android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z" />
|
||||
</vector>
|
||||
@@ -0,0 +1,9 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24">
|
||||
<path
|
||||
android:fillColor="@android:color/white"
|
||||
android:pathData="M20,5L4,5c-1.1,0 -1.99,0.9 -1.99,2L2,17c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,7c0,-1.1 -0.9,-2 -2,-2zM11,8h2v2h-2L11,8zM11,11h2v2h-2v-2zM8,8h2v2L8,10L8,8zM8,11h2v2L8,13v-2zM7,13L5,13v-2h2v2zM7,10L5,10L5,8h2v2zM16,17L8,17v-2h8v2zM16,13h-2v-2h2v2zM16,10h-2L14,8h2v2zM19,13h-2v-2h2v2zM19,10h-2L17,8h2v2z" />
|
||||
</vector>
|
||||
@@ -0,0 +1,15 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="108dp"
|
||||
android:height="108dp"
|
||||
android:viewportWidth="108"
|
||||
android:viewportHeight="108">
|
||||
<group android:scaleX="0.07382812"
|
||||
android:scaleY="0.07382812"
|
||||
android:translateX="16.2"
|
||||
android:translateY="16.2">
|
||||
<path
|
||||
android:pathData="M843,511.86L843,512.96C843,513.22 842.8,513.43 842.55,513.46C809.18,516.58 783.07,543.69 783.07,577.86L783.07,714.8C783.07,724.37 781.06,733.6 777.43,742.03C767.08,766.1 743.59,783.6 716.27,783.6L647.07,783.6L646.59,783L377.41,445.68L377.41,783.6L307.73,783.6C270.84,783.6 240.93,753.7 240.93,716.8L240.93,577.86C240.93,543.69 214.82,516.58 181.45,513.46C181.2,513.43 181,513.22 181,512.96L181,511.86C181,511.61 181.19,511.4 181.45,511.37C214.69,508.14 240.67,480.35 240.67,446.26L240.67,307.2C240.67,297.91 242.57,289.05 246,281.01C256.18,257.14 279.87,240.39 307.47,240.39L377.13,240.39L377.41,240.74L646.59,578.07L646.59,240.39L716.53,240.39C753.42,240.39 783.33,270.3 783.33,307.2L783.33,446.26C783.33,480.35 809.31,508.14 842.55,511.37C842.81,511.4 843,511.61 843,511.86"
|
||||
android:fillColor="#65ADF1"
|
||||
android:fillType="nonZero"/>
|
||||
</group>
|
||||
</vector>
|
||||
@@ -0,0 +1,9 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24">
|
||||
<path
|
||||
android:fillColor="@android:color/white"
|
||||
android:pathData="M12,14c1.66,0 2.99,-1.34 2.99,-3L15,5c0,-1.66 -1.34,-3 -3,-3S9,3.34 9,5v6c0,1.66 1.34,3 3,3zM17.3,11c0,3 -2.54,5.1 -5.3,5.1S6.7,14 6.7,11L5,11c0,3.41 2.72,6.23 6,6.72L11,21h2v-3.28c3.28,-0.48 6,-3.3 6,-6.72h-1.7z" />
|
||||
</vector>
|
||||
@@ -0,0 +1,9 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24">
|
||||
<path
|
||||
android:fillColor="@android:color/white"
|
||||
android:pathData="M6,10c-1.1,0 -2,0.9 -2,2s0.9,2 2,2 2,-0.9 2,-2 -0.9,-2 -2,-2zM18,10c-1.1,0 -2,0.9 -2,2s0.9,2 2,2 2,-0.9 2,-2 -0.9,-2 -2,-2zM12,10c-1.1,0 -2,0.9 -2,2s0.9,2 2,2 2,-0.9 2,-2 -0.9,-2 -2,-2z" />
|
||||
</vector>
|
||||
@@ -0,0 +1,9 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24">
|
||||
<path
|
||||
android:fillColor="@android:color/white"
|
||||
android:pathData="M17.65,6.35C16.2,4.9 14.21,4 12,4c-4.42,0 -7.99,3.58 -7.99,8s3.57,8 7.99,8c3.73,0 6.84,-2.55 7.73,-6h-2.08c-0.82,2.33 -3.04,4 -5.65,4 -3.31,0 -6,-2.69 -6,-6s2.69,-6 6,-6c1.66,0 3.14,0.69 4.22,1.78L13,11h7V4l-2.35,2.35z" />
|
||||
</vector>
|
||||
@@ -0,0 +1,9 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24">
|
||||
<path
|
||||
android:fillColor="@android:color/white"
|
||||
android:pathData="M19.14,12.94c0.04,-0.3 0.06,-0.61 0.06,-0.94 0,-0.32 -0.02,-0.64 -0.07,-0.94l2.03,-1.58c0.18,-0.14 0.23,-0.41 0.12,-0.61l-1.92,-3.32c-0.12,-0.22 -0.37,-0.29 -0.59,-0.22l-2.39,0.96c-0.5,-0.38 -1.03,-0.7 -1.62,-0.94L14.4,2.81c-0.04,-0.24 -0.24,-0.41 -0.48,-0.41h-3.84c-0.24,0 -0.43,0.17 -0.47,0.41L9.25,5.35C8.66,5.59 8.12,5.92 7.63,6.29L5.24,5.33c-0.22,-0.08 -0.47,0 -0.59,0.22L2.74,8.87C2.62,9.08 2.66,9.34 2.86,9.48l2.03,1.58C4.84,11.36 4.8,11.69 4.8,12s0.02,0.64 0.07,0.94l-2.03,1.58c-0.18,0.14 -0.23,0.41 -0.12,0.61l1.92,3.32c0.12,0.22 0.37,0.29 0.59,0.22l2.39,-0.96c0.5,0.38 1.03,0.7 1.62,0.94l0.36,2.54c0.05,0.24 0.24,0.41 0.48,0.41h3.84c0.24,0 0.44,-0.17 0.47,-0.41l0.36,-2.54c0.59,-0.24 1.13,-0.56 1.62,-0.94l2.39,0.96c0.22,0.08 0.47,0 0.59,-0.22l1.92,-3.32c0.12,-0.22 0.07,-0.47 -0.12,-0.61L19.14,12.94zM12,15.6c-1.98,0 -3.6,-1.62 -3.6,-3.6s1.62,-3.6 3.6,-3.6 3.6,1.62 3.6,3.6S13.98,15.6 12,15.6z" />
|
||||
</vector>
|
||||
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<background android:drawable="@color/ic_launcher_background"/>
|
||||
<foreground android:drawable="@mipmap/ic_launcher_foreground"/>
|
||||
</adaptive-icon>
|
||||
|
After Width: | Height: | Size: 9.6 KiB |
|
After Width: | Height: | Size: 21 KiB |
|
After Width: | Height: | Size: 4.8 KiB |
|
After Width: | Height: | Size: 11 KiB |
|
After Width: | Height: | Size: 16 KiB |
|
After Width: | Height: | Size: 34 KiB |
|
After Width: | Height: | Size: 32 KiB |
|
After Width: | Height: | Size: 65 KiB |
|
After Width: | Height: | Size: 53 KiB |
|
After Width: | Height: | Size: 106 KiB |
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<color name="ic_launcher_background">#00000000</color>
|
||||
</resources>
|
||||
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="app_name">Faceclaw</string>
|
||||
<string name="speech_prompt_assistant">Ask the assistant</string>
|
||||
<string name="speech_prompt_app">Say what to type</string>
|
||||
<string name="keyboard_label_assistant">Ask the assistant</string>
|
||||
<string name="keyboard_label_app">Type into the app</string>
|
||||
</resources>
|
||||
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<!-- The pad uses rightward swipes as input, so the system's whole-window
|
||||
swipe-to-dismiss is off; the crown/home button leaves the app, and
|
||||
inner screens still swipe back through SwipeDismissableNavHost. -->
|
||||
<style name="Theme.Faceclaw" parent="@android:style/Theme.DeviceDefault">
|
||||
<item name="android:windowSwipeToDismiss">false</item>
|
||||
</style>
|
||||
</resources>
|
||||
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- Advertised over the Wearable Data Layer; the phone app targets its
|
||||
messages at nodes with this capability (FaceclawWearBridge). -->
|
||||
<resources>
|
||||
<string-array name="android_wear_capabilities">
|
||||
<item>faceclaw_watch</item>
|
||||
</string-array>
|
||||
</resources>
|
||||