forked from Blendman974/faceclaw
Add battery indicator for Android Wear watch
This commit is contained in:
1 parent
5bbb04abce
commit
937c73d81b
15 files changed
+279
-18
No files matched your search
@@ -33,5 +33,16 @@
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<!-- Answers the phone's battery polls (/faceclaw/battery/request) so
|
||||
the glasses can show the watch battery while this app is closed. -->
|
||||
<service
|
||||
android:name=".PhoneListenerService"
|
||||
android:exported="true">
|
||||
<intent-filter>
|
||||
<action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
|
||||
<data android:scheme="wear" android:host="*" android:pathPrefix="/faceclaw/battery" />
|
||||
</intent-filter>
|
||||
</service>
|
||||
</application>
|
||||
</manifest>
|
||||
@@ -1,8 +1,12 @@
|
||||
package com.faceclaw.wear
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.net.Uri
|
||||
import android.util.Log
|
||||
import androidx.core.content.ContextCompat
|
||||
import com.google.android.gms.common.api.ApiException
|
||||
import com.google.android.gms.wearable.CapabilityClient
|
||||
import com.google.android.gms.wearable.CapabilityInfo
|
||||
@@ -83,6 +87,15 @@ class PhoneLink(context: Context) :
|
||||
private val pendingAcks = ConcurrentHashMap<Long, Job>()
|
||||
private var noticeJob: Job? = null
|
||||
private var started = false
|
||||
/** The battery state last reported to the phone (pushes only on change). */
|
||||
private var lastBatteryReported: WatchBatteryState? = null
|
||||
// While the app is open, charge and charging changes reach the phone at
|
||||
// once; closed, the phone polls PhoneListenerService instead.
|
||||
private val batteryReceiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
if (intent.action == Intent.ACTION_BATTERY_CHANGED) reportBattery(force = false)
|
||||
}
|
||||
}
|
||||
|
||||
private val _state = MutableStateFlow<PhoneState?>(null)
|
||||
val state: StateFlow<PhoneState?> = _state.asStateFlow()
|
||||
@@ -105,12 +118,28 @@ class PhoneLink(context: Context) :
|
||||
messageClient.addListener(this)
|
||||
dataClient.addListener(this)
|
||||
capabilityClient.addListener(this, Protocol.CAPABILITY_PHONE)
|
||||
try {
|
||||
ContextCompat.registerReceiver(
|
||||
appContext,
|
||||
batteryReceiver,
|
||||
IntentFilter(Intent.ACTION_BATTERY_CHANGED),
|
||||
ContextCompat.RECEIVER_NOT_EXPORTED,
|
||||
)
|
||||
} catch (error: Exception) {
|
||||
Log.w(TAG, "battery receiver registration failed", error)
|
||||
}
|
||||
refresh()
|
||||
}
|
||||
|
||||
fun stop() {
|
||||
if (!started) return
|
||||
started = false
|
||||
try {
|
||||
appContext.unregisterReceiver(batteryReceiver)
|
||||
} catch (error: Exception) {
|
||||
Log.w(TAG, "battery receiver unregistration failed", error)
|
||||
}
|
||||
lastBatteryReported = null
|
||||
messageClient.removeListener(this)
|
||||
dataClient.removeListener(this)
|
||||
capabilityClient.removeListener(this, Protocol.CAPABILITY_PHONE)
|
||||
@@ -121,10 +150,26 @@ class PhoneLink(context: Context) :
|
||||
scope.launch {
|
||||
findPhone()
|
||||
loadStateItem()
|
||||
if (_link.value.phoneAppInstalled) requestState()
|
||||
if (_link.value.phoneAppInstalled) {
|
||||
requestState()
|
||||
reportBattery(force = true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send our battery to the phone (for the glasses' Watch indicator).
|
||||
* Skipped when unchanged since the last report unless `force`.
|
||||
*/
|
||||
private fun reportBattery(force: Boolean) {
|
||||
val nodeId = _link.value.phoneNodeId ?: return
|
||||
if (!_link.value.phoneAppInstalled) return
|
||||
val state = WatchBattery.read(appContext)
|
||||
if (!force && state == lastBatteryReported) return
|
||||
lastBatteryReported = state
|
||||
WatchBattery.report(appContext, nodeId, state)
|
||||
}
|
||||
|
||||
// ---- outbound -----------------------------------------------------
|
||||
|
||||
fun sendGesture(gesture: Gesture, steps: Int = 1) {
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.faceclaw.wear
|
||||
|
||||
import android.util.Log
|
||||
import com.google.android.gms.wearable.MessageEvent
|
||||
import com.google.android.gms.wearable.WearableListenerService
|
||||
|
||||
/**
|
||||
* Play services starts this (in the watch app's process) for messages under
|
||||
* /faceclaw/battery whether or not the app is open, so the phone can poll
|
||||
* the watch battery for the glasses' indicator while the watch sits idle.
|
||||
* Everything else the phone sends (acks, events, the state item) is only
|
||||
* meaningful to a running PhoneLink and is not routed here.
|
||||
*/
|
||||
class PhoneListenerService : WearableListenerService() {
|
||||
override fun onMessageReceived(event: MessageEvent) {
|
||||
if (event.path != Protocol.PATH_BATTERY_REQUEST) return
|
||||
Log.d(TAG, "battery request from ${event.sourceNodeId}")
|
||||
WatchBattery.report(this, event.sourceNodeId)
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val TAG = "FaceclawWear"
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,10 @@ object Protocol {
|
||||
const val PATH_ASSISTANT = "/faceclaw/assistant"
|
||||
const val PATH_TEXT = "/faceclaw/text"
|
||||
const val PATH_STATE_REQUEST = "/faceclaw/state/request"
|
||||
/** Watch -> phone: our own battery ({battery, charging}); no seq, never acked. */
|
||||
const val PATH_BATTERY = "/faceclaw/battery"
|
||||
/** Phone -> watch: please send PATH_BATTERY now (answered by PhoneListenerService). */
|
||||
const val PATH_BATTERY_REQUEST = "/faceclaw/battery/request"
|
||||
const val PATH_ACK = "/faceclaw/ack"
|
||||
const val PATH_EVENT = "/faceclaw/event"
|
||||
const val PATH_STATE = "/faceclaw/state"
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.faceclaw.wear
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.os.BatteryManager
|
||||
import android.util.Log
|
||||
import com.google.android.gms.wearable.Wearable
|
||||
import org.json.JSONObject
|
||||
|
||||
/** The watch's own charge level and charging state, for the glasses' battery indicators. */
|
||||
data class WatchBatteryState(val battery: Int?, val charging: Boolean) {
|
||||
fun toJson(): JSONObject = JSONObject()
|
||||
.put("battery", battery ?: JSONObject.NULL)
|
||||
.put("charging", charging)
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the watch battery from the sticky ACTION_BATTERY_CHANGED broadcast
|
||||
* (no receiver needed) and reports it to the phone on /faceclaw/battery. Used
|
||||
* both by PhoneListenerService, which answers the phone's polls while the app
|
||||
* is closed, and by PhoneLink, which pushes changes while the app is open.
|
||||
*/
|
||||
object WatchBattery {
|
||||
private const val TAG = "FaceclawWear"
|
||||
|
||||
fun read(context: Context): WatchBatteryState {
|
||||
val intent: Intent? = try {
|
||||
context.registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
|
||||
} catch (error: Exception) {
|
||||
Log.w(TAG, "battery read failed", error)
|
||||
null
|
||||
}
|
||||
if (intent == null) return WatchBatteryState(null, false)
|
||||
val level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
|
||||
val scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
|
||||
val status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1)
|
||||
val percent = if (level >= 0 && scale > 0) (level * 100 + scale / 2) / scale else null
|
||||
val charging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL
|
||||
return WatchBatteryState(percent?.coerceIn(0, 100), charging)
|
||||
}
|
||||
|
||||
/** Fire-and-forget: the phone never acks battery reports. */
|
||||
fun report(context: Context, nodeId: String, state: WatchBatteryState = read(context)) {
|
||||
val payload = state.toJson().toString()
|
||||
Log.d(TAG, "send ${Protocol.PATH_BATTERY} $payload")
|
||||
try {
|
||||
Wearable.getMessageClient(context.applicationContext)
|
||||
.sendMessage(nodeId, Protocol.PATH_BATTERY, payload.toByteArray(Charsets.UTF_8))
|
||||
.addOnFailureListener { error -> Log.w(TAG, "battery report failed", error) }
|
||||
} catch (error: Exception) {
|
||||
Log.w(TAG, "battery report failed", error)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user