feat: extend DeviceInfo with battery, manufacturer, heartbeat
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -13,9 +13,12 @@ data class AuthMessage(
|
||||
@Serializable
|
||||
data class DeviceInfoMsg(
|
||||
val model: String,
|
||||
val manufacturer: String,
|
||||
val androidVersion: String,
|
||||
val screenWidth: Int,
|
||||
val screenHeight: Int
|
||||
val screenHeight: Int,
|
||||
val batteryLevel: Int,
|
||||
val isCharging: Boolean
|
||||
)
|
||||
|
||||
@Serializable
|
||||
@@ -48,6 +51,13 @@ data class PongMessage(
|
||||
val type: String = "pong"
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class HeartbeatMessage(
|
||||
val type: String = "heartbeat",
|
||||
val batteryLevel: Int,
|
||||
val isCharging: Boolean
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class ServerMessage(
|
||||
val type: String,
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package com.thisux.droidclaw.util
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.os.BatteryManager
|
||||
import android.util.DisplayMetrics
|
||||
import android.view.WindowManager
|
||||
import com.thisux.droidclaw.model.DeviceInfoMsg
|
||||
@@ -11,11 +14,30 @@ object DeviceInfoHelper {
|
||||
val metrics = DisplayMetrics()
|
||||
@Suppress("DEPRECATION")
|
||||
wm.defaultDisplay.getRealMetrics(metrics)
|
||||
|
||||
val batteryIntent = context.registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
|
||||
val level = batteryIntent?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) ?: -1
|
||||
val scale = batteryIntent?.getIntExtra(BatteryManager.EXTRA_SCALE, -1) ?: -1
|
||||
val batteryPct = if (level >= 0 && scale > 0) (level * 100 / scale) else -1
|
||||
val plugged = batteryIntent?.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) ?: 0
|
||||
|
||||
return DeviceInfoMsg(
|
||||
model = android.os.Build.MODEL,
|
||||
manufacturer = android.os.Build.MANUFACTURER.replaceFirstChar { it.uppercase() },
|
||||
androidVersion = android.os.Build.VERSION.RELEASE,
|
||||
screenWidth = metrics.widthPixels,
|
||||
screenHeight = metrics.heightPixels
|
||||
screenHeight = metrics.heightPixels,
|
||||
batteryLevel = batteryPct,
|
||||
isCharging = plugged != 0
|
||||
)
|
||||
}
|
||||
|
||||
fun getBattery(context: Context): Pair<Int, Boolean> {
|
||||
val batteryIntent = context.registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
|
||||
val level = batteryIntent?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) ?: -1
|
||||
val scale = batteryIntent?.getIntExtra(BatteryManager.EXTRA_SCALE, -1) ?: -1
|
||||
val batteryPct = if (level >= 0 && scale > 0) (level * 100 / scale) else -1
|
||||
val plugged = batteryIntent?.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) ?: 0
|
||||
return Pair(batteryPct, plugged != 0)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user