Skip to content

Commit

Permalink
Show two gauges in PaneTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
Ixam97 committed May 5, 2024
1 parent 1bd751b commit bd10617
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 33 deletions.
1 change: 1 addition & 0 deletions automotive/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@
<string name="car_app_trip_data">Tripdaten</string>
<string name="car_app_strip_selection">Tripauswahl</string>
<string name="car_app_dashboard">Dashboard</string>
<string name="car_app_legacy_dashboard">Klassisches Dashboard</string>
<string name="car_app_menu">Verschiedenes</string>
<string name="car_app_toast_reset_confirmation">Manuellert Trip wurde zurückgesetzt</string>
<string name="car_app_toast_reset_hint">Erneut tippen, um manuellen Trip zurückzusetzen.</string>
Expand Down
1 change: 1 addition & 0 deletions automotive/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@
<string name="car_app_trip_data">Trip Data</string>
<string name="car_app_strip_selection">Trip selection</string>
<string name="car_app_dashboard">Dashboard</string>
<string name="car_app_legacy_dashboard">Legacy Dashboard</string>
<string name="car_app_menu">Miscellaneous</string>
<string name="car_app_status">Car Status</string>
<string name="car_app_toast_reset_confirmation">Manual Trip has been reset.</string>
Expand Down
87 changes: 63 additions & 24 deletions automotive/src/play/java/com/ixam97/carStatsViewer/carApp/Gauge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.graphics.RectF
import androidx.car.app.CarContext
import com.ixam97.carStatsViewer.CarStatsViewer
import com.ixam97.carStatsViewer.R
import kotlin.math.max
import kotlin.math.roundToInt

class Gauge(
Expand Down Expand Up @@ -70,65 +71,103 @@ class Gauge(
valueString: String? = null,
unitString: String? = null,
selected: Boolean = false
): Bitmap = draw(size, size, value, min, max, valueString, unitString, selected)

fun draw(
width: Int,
height: Int,
value: Float,
min: Float = 0f,
max: Float = 100f,
valueString: String? = null,
unitString: String? = null,
selected: Boolean = false
): Bitmap {

valuePaint.textSize = size / 4f
unitPaint.textSize = size / 8f
valuePaint.textSize = max(width, height) / 4f
unitPaint.textSize = max(width, height) / 8f

val bottomLine: Float = if (valueString != null) {
size - valuePaint.textSize
height - valuePaint.textSize
} else {
height.toFloat()
}

val topLine: Float = if (valueString != null) {
valuePaint.textSize
} else {
size.toFloat()
0f
}

val bottomOffset = size / 50f
val textOffsetY = valuePaint.textSize / 10f

if (min >= max) throw Exception("Min can't be larger than Max!")

val bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888)
val bitmap = Bitmap.createBitmap(max(width, height), max(width, height), Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
val floatSize = size.toFloat()
val floatHeight = height.toFloat()
val floatWidth = width.toFloat()

val zeroLineY: Float = bottomLine * (max / (max - min))
val zeroLineX: Float = floatWidth - (floatWidth * (max / (max - min)))
var valueLineY: Float = (zeroLineY - value * (bottomLine / (max - min)))
var valueLineX: Float = (zeroLineX + value) * (floatWidth / (max - min))

if (valueLineY > bottomLine) valueLineY = bottomLine
else if (valueLineY <= 0) valueLineY = 0f

if (valueLineX > floatWidth) valueLineX = floatWidth
else if (valueLineX <= 0) valueLineX = 0f

if (value > 0 || value < 0) {
gageBarRect.apply {
left = 0f
right = floatSize
bottom = zeroLineY
top = valueLineY
if (width < height) {
left = 0f
right = floatWidth
bottom = zeroLineY
top = valueLineY
} else {
left = zeroLineX
right = valueLineX
top = topLine
bottom = floatHeight
}
}
canvas.drawRect(gageBarRect, if (value > 0) posPaint else negPaint)
}

if (min < 0) {
gageZeroLine.apply {
reset()
moveTo(0f, zeroLineY)
lineTo(floatSize, zeroLineY)
if (width < height) {
reset()
moveTo(0f, zeroLineY)
lineTo(floatWidth, zeroLineY)
close()
} else {
reset()
moveTo(zeroLineX, topLine)
lineTo(zeroLineX, floatHeight)
close()
}
}
canvas.drawPath(gageZeroLine, zeroLinePaint)
}

gageBorder.apply {
reset()
moveTo(0f + borderPaint.strokeWidth/2, 0f + borderPaint.strokeWidth/2)
lineTo(0f + borderPaint.strokeWidth/2, bottomLine - borderPaint.strokeWidth/2)
lineTo(floatSize - borderPaint.strokeWidth/2, bottomLine - borderPaint.strokeWidth/2)
lineTo(floatSize - borderPaint.strokeWidth/2, 0f + borderPaint.strokeWidth/2)
moveTo(0f + borderPaint.strokeWidth/2, topLine + borderPaint.strokeWidth/2)
lineTo(0f + borderPaint.strokeWidth/2, floatHeight - borderPaint.strokeWidth/2)
lineTo(floatWidth - borderPaint.strokeWidth/2, floatHeight - borderPaint.strokeWidth/2)
lineTo(floatWidth - borderPaint.strokeWidth/2, topLine + borderPaint.strokeWidth/2)
close()
}
canvas.drawPath(gageBorder, borderPaint)

valueString?.let { it ->
canvas.drawText(it, 0f, floatSize - bottomOffset, valuePaint)
canvas.drawText(it, 0f, topLine - textOffsetY, valuePaint)
if (unitString != null) {
val xOffset = valuePaint.measureText(valueString) + 10f
canvas.drawText(unitString, xOffset, floatSize - bottomOffset, unitPaint)
canvas.drawText(unitString, xOffset, topLine - textOffsetY, unitPaint)
}
}

Expand All @@ -142,12 +181,12 @@ class Gauge(
return bitmap
}

fun drawPowerGauge(size: Int, value: Float): Bitmap {
fun drawPowerGauge(width: Int, height: Int, value: Float): Bitmap {
val valueString = "${((value/1_000_000) * 10).toInt() / 10f }"
return draw(size, value/1_000_000, min = -150f, max = 300f, valueString, "kW")
return draw(width, height, value/1_000_000, min = -150f, max = 300f, valueString, "kW")
}

fun drawConsumptionGauge(size: Int, valueInstCons: Float?, valueSpeed: Float?): Bitmap {
fun drawConsumptionGauge(width: Int, height: Int, valueInstCons: Float?, valueSpeed: Float?): Bitmap {
val instConsVal: Number? = if (valueInstCons != null && (valueSpeed?:0f) * 3.6 > 3) {
if (appPreferences.consumptionUnit) {
appPreferences.distanceUnit.asUnit(valueInstCons).roundToInt()
Expand All @@ -168,6 +207,6 @@ class Gauge(

val instCons = if ((valueSpeed?:0f) * 3.6 > 3) valueInstCons else null

return draw(size, instCons?:0f, -300f, 600f, valueString, consUnit)
return draw(width, height, instCons?:0f, -300f, 600f, valueString, consUnit)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,24 @@ internal fun CarStatsViewerScreen.MenuList() = ListTemplate.Builder().apply {

setSingleList(ItemList.Builder().apply {
addItem(Row.Builder().apply{
setTitle(carContext.getString(R.string.car_app_dashboard))
setImage(CarIcon.Builder(IconCompat.createWithResource(carContext, R.drawable.ic_diagram)).build())
setTitle(carContext.getString(R.string.car_app_legacy_dashboard))
setImage(CarIcon.Builder(IconCompat.createWithResource(carContext, R.drawable.ic_car_app_dashboard)).build())
setBrowsable(true)
setOnClickListener {
carContext.startActivity(mainActivityIntent)
}
}.build())
addItem(Row.Builder().apply{
setTitle(carContext.getString(R.string.history_title))
setImage(CarIcon.Builder(IconCompat.createWithResource(carContext, R.drawable.ic_history)).build())
setImage(CarIcon.Builder(IconCompat.createWithResource(carContext, R.drawable.ic_car_app_history)).build())
setBrowsable(true)
setOnClickListener {
carContext.startActivity(historyActivityIntent)
}
}.build())
addItem(Row.Builder().apply{
setTitle(carContext.getString(R.string.settings_title))
setImage(CarIcon.Builder(IconCompat.createWithResource(carContext, R.drawable.ic_settings)).build())
setImage(CarIcon.Builder(IconCompat.createWithResource(carContext, R.drawable.ic_car_app_settings)).build())
setBrowsable(true)
setOnClickListener {
carContext.startActivity(settingsActivityIntent)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.ixam97.carStatsViewer.carApp

import android.graphics.Bitmap
import android.graphics.Canvas
import androidx.annotation.OptIn
import androidx.car.app.CarContext
import androidx.car.app.ScreenManager
Expand Down Expand Up @@ -82,17 +84,27 @@ class TripDataTemplate(val carContext: CarContext) {
))
}
addAction(configAction(showTitle = true))
if (session.session_type == 1 && carContext.carAppApiLevel >= 7) {
if (session.session_type == 1) {
addAction(resetAction(showTitle = true))
}
val selectedGaugeIndex = CarStatsViewer.appPreferences.carAppSelectedRealTimeData

if (realTimeData != null) when (selectedGaugeIndex) {
1 -> setImage(gauge.drawPowerGauge(480, realTimeData.power?:0f).asCarIcon())
2 -> setImage(gauge.drawConsumptionGauge(480, realTimeData.instConsumption, realTimeData.speed).asCarIcon())
else -> { /* Don't show an image */ }
if (realTimeData != null) {

val gaugeBitmap = Bitmap.createBitmap(480, 480, Bitmap.Config.ARGB_8888)
val canvas = Canvas(gaugeBitmap)
canvas.drawBitmap(gauge.drawPowerGauge(480, 230, realTimeData.power?:0f), 0f, 0f ,null)
canvas.drawBitmap(gauge.drawConsumptionGauge(480, 230, realTimeData.instConsumption, realTimeData.speed), 0f, 250f, null)

setImage(gaugeBitmap.asCarIcon())
}

// if (realTimeData != null) when (selectedGaugeIndex) {
// 1 -> setImage(.asCarIcon())
// 2 -> setImage(.asCarIcon())
// else -> { /* Don't show an image */ }
// }

}
}.build()).build()

Expand Down
10 changes: 10 additions & 0 deletions automotive/src/play/res/drawable/ic_car_app_history.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="48dp"
android:height="48dp"
android:viewportWidth="960"
android:viewportHeight="960"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M477,840Q328,840 224,734.5Q120,629 120,479L180,479Q180,604 266,692Q352,780 477,780Q604,780 692,691Q780,602 780,475Q780,351 691,265.5Q602,180 477,180Q409,180 349.5,211Q290,242 246,293L351,293L351,353L142,353L142,145L202,145L202,251Q254,190 325.5,155Q397,120 477,120Q552,120 618,148Q684,176 733.5,224.5Q783,273 811.5,338Q840,403 840,478Q840,553 811.5,619Q783,685 733.5,734Q684,783 618,811.5Q552,840 477,840ZM605,643L451,491L451,277L511,277L511,466L648,600L605,643Z"/>
</vector>

0 comments on commit bd10617

Please sign in to comment.