Set powerbar to zero if no sensor available is available

This commit is contained in:
Tim Kluge 2024-12-08 16:25:45 +01:00
parent 6b260a9f71
commit 7eed81a109
3 changed files with 40 additions and 41 deletions

View File

@ -57,8 +57,11 @@ class CustomProgressBar @JvmOverloads constructor(
val corners = 2f val corners = 2f
canvas.drawRoundRect(0f, 2f + 4f, canvas.width.toFloat(), canvas.height.toFloat(), 2f, 2f, background) canvas.drawRoundRect(0f, 2f + 4f, canvas.width.toFloat(), canvas.height.toFloat(), 2f, 2f, background)
canvas.drawRoundRect(rect, corners, corners, blurPaint)
canvas.drawRoundRect(rect, corners, corners, linePaint) if (progress > 0.0) {
canvas.drawRoundRect(rect, corners, corners, blurPaint)
canvas.drawRoundRect(rect, corners, corners, linePaint)
}
canvas.drawRoundRect(rect.right-4, rect.top, rect.right+4, rect.bottom, 2f, 2f, blurPaintHighlight) canvas.drawRoundRect(rect.right-4, rect.top, rect.right+4, rect.bottom, 2f, 2f, blurPaintHighlight)
} }

View File

@ -46,7 +46,7 @@ fun Context.streamSettings(): Flow<PowerbarSettings> {
settingsJson[settingsKey] ?: PowerbarSettings.defaultSettings settingsJson[settingsKey] ?: PowerbarSettings.defaultSettings
) )
} catch(e: Throwable){ } catch(e: Throwable){
Log.e("karoo-powerbar", "Failed to read preferences", e) Log.e(KarooPowerbarExtension.TAG, "Failed to read preferences", e)
jsonWithUnknownKeys.decodeFromString<PowerbarSettings>(PowerbarSettings.defaultSettings) jsonWithUnknownKeys.decodeFromString<PowerbarSettings>(PowerbarSettings.defaultSettings)
} }
}.distinctUntilChanged() }.distinctUntilChanged()

View File

@ -34,15 +34,14 @@ fun remap(value: Double, fromMin: Double, fromMax: Double, toMin: Double, toMax:
class Window( class Window(
private val context: Context private val context: Context
) { ) {
// private var mProgressBar: ProgressBar private val rootView: View
private val mView: View private var layoutParams: WindowManager.LayoutParams? = null
private var mParams: WindowManager.LayoutParams? = null private val windowManager: WindowManager
private val mWindowManager: WindowManager
private val layoutInflater: LayoutInflater private val layoutInflater: LayoutInflater
private val mProgressBar: CustomProgressBar private val powerbar: CustomProgressBar
init { init {
mParams = WindowManager.LayoutParams( layoutParams = WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
@ -51,29 +50,27 @@ class Window(
) )
layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
mView = layoutInflater.inflate(R.layout.popup_window, null) rootView = layoutInflater.inflate(R.layout.popup_window, null)
mProgressBar = mView.findViewById(R.id.progressBar) powerbar = rootView.findViewById(R.id.progressBar)
mProgressBar.progress = 0.0 powerbar.progress = 0.0
mWindowManager = context.getSystemService(WINDOW_SERVICE) as WindowManager windowManager = context.getSystemService(WINDOW_SERVICE) as WindowManager
val displayMetrics = DisplayMetrics() val displayMetrics = DisplayMetrics()
if (Build.VERSION.SDK_INT >= 30) { if (Build.VERSION.SDK_INT >= 30) {
val windowMetrics = mWindowManager.currentWindowMetrics val windowMetrics = windowManager.currentWindowMetrics
val insets = windowMetrics.windowInsets.getInsetsIgnoringVisibility(WindowInsets.Type.systemBars()) val insets = windowMetrics.windowInsets.getInsetsIgnoringVisibility(WindowInsets.Type.systemBars())
val bounds = windowMetrics.bounds val bounds = windowMetrics.bounds
displayMetrics.widthPixels = bounds.width() - insets.left - insets.right displayMetrics.widthPixels = bounds.width() - insets.left - insets.right
displayMetrics.heightPixels = bounds.height() - insets.top - insets.bottom displayMetrics.heightPixels = bounds.height() - insets.top - insets.bottom
} else { } else {
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
mWindowManager.defaultDisplay.getMetrics(displayMetrics) windowManager.defaultDisplay.getMetrics(displayMetrics)
} }
// Define the position of the layoutParams?.gravity = Gravity.BOTTOM
// window within the screen layoutParams?.width = displayMetrics.widthPixels
mParams?.gravity = Gravity.BOTTOM layoutParams?.alpha = 1.0f
mParams?.width = displayMetrics.widthPixels
mParams?.alpha = 1.0f
} }
private val karooSystem: KarooSystemService = KarooSystemService(context) private val karooSystem: KarooSystemService = KarooSystemService(context)
@ -86,33 +83,32 @@ class Window(
serviceJob = CoroutineScope(Dispatchers.IO).launch { serviceJob = CoroutineScope(Dispatchers.IO).launch {
karooSystem.connect { connected -> karooSystem.connect { connected ->
if (connected) { if (connected) {
Log.i("karoo-powerbar", "Connected") Log.i(KarooPowerbarExtension.TAG, "Connected")
} }
} }
context.streamSettings().distinctUntilChanged().collectLatest { settings -> context.streamSettings().distinctUntilChanged().collectLatest { settings ->
mProgressBar.progressColor = context.resources.getColor(R.color.zoneAerobic) powerbar.progressColor = context.resources.getColor(R.color.zoneAerobic)
mProgressBar.progress = 0.5 powerbar.progress = 0.0
mProgressBar.invalidate() powerbar.invalidate()
Log.i("karoo-powerbar", "Streaming ${settings.source}") Log.i(KarooPowerbarExtension.TAG, "Streaming ${settings.source}")
when (settings.source){ when (settings.source){
SelectedSource.POWER -> streamPower(PowerStreamSmoothing.RAW) SelectedSource.POWER -> streamPower(PowerStreamSmoothing.RAW)
SelectedSource.POWER_3S -> streamPower(PowerStreamSmoothing.SMOOTHED_3S) SelectedSource.POWER_3S -> streamPower(PowerStreamSmoothing.SMOOTHED_3S)
SelectedSource.POWER_10S -> streamPower(PowerStreamSmoothing.SMOOTHED_10S) SelectedSource.POWER_10S -> streamPower(PowerStreamSmoothing.SMOOTHED_10S)
// SelectedSource.POWER_30S -> streamPower(PowerStreamSmoothing.SMOOTHED_30S)
SelectedSource.HEART_RATE -> streamHeartrate() SelectedSource.HEART_RATE -> streamHeartrate()
} }
} }
} }
try { try {
if (mView.windowToken == null && mView.parent == null) { if (rootView.windowToken == null && rootView.parent == null) {
mWindowManager.addView(mView, mParams) windowManager.addView(rootView, layoutParams)
} }
} catch (e: Exception) { } catch (e: Exception) {
Log.d("karoo-powerbar", e.toString()) Log.d(KarooPowerbarExtension.TAG, e.toString())
} }
} }
@ -136,11 +132,11 @@ class Window(
val progress = val progress =
remap(streamData.value, minHr.toDouble(), maxHr.toDouble(), 0.0, 1.0) remap(streamData.value, minHr.toDouble(), maxHr.toDouble(), 0.0, 1.0)
mProgressBar.progressColor = color powerbar.progressColor = color
mProgressBar.progress = progress powerbar.progress = progress
mProgressBar.invalidate() powerbar.invalidate()
Log.i("karoo-powerbar", "Hr: ${streamData.value} min: $minHr max: $maxHr") Log.d(KarooPowerbarExtension.TAG, "Hr: ${streamData.value} min: $minHr max: $maxHr")
} }
} }
@ -170,22 +166,22 @@ class Window(
val progress = val progress =
remap(streamData.value, minPower.toDouble(), maxPower.toDouble(), 0.0, 1.0) remap(streamData.value, minPower.toDouble(), maxPower.toDouble(), 0.0, 1.0)
mProgressBar.progressColor = color powerbar.progressColor = color
mProgressBar.progress = progress powerbar.progress = progress
mProgressBar.invalidate() powerbar.invalidate()
Log.i("karoo-powerbar", "Power: ${streamData.value} min: $minPower max: $maxPower") Log.d(KarooPowerbarExtension.TAG, "Power: ${streamData.value} min: $minPower max: $maxPower")
} }
} }
fun close() { fun close() {
try { try {
serviceJob?.cancel() serviceJob?.cancel()
(context.getSystemService(WINDOW_SERVICE) as WindowManager).removeView(mView) (context.getSystemService(WINDOW_SERVICE) as WindowManager).removeView(rootView)
mView.invalidate() rootView.invalidate()
(mView.parent as ViewGroup).removeAllViews() (rootView.parent as ViewGroup).removeAllViews()
} catch (e: Exception) { } catch (e: Exception) {
Log.d("karoo-powerbar", e.toString()) Log.d(KarooPowerbarExtension.TAG, e.toString())
} }
} }
} }