Android Auto: create a VehicleDataScreen showing state of charge

This commit is contained in:
Johan von Forstner
2021-07-22 14:32:00 +02:00
committed by johan12345
parent 630178bfcf
commit 65189cd798
6 changed files with 73 additions and 1 deletions

View File

@@ -5,8 +5,9 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="androidx.car.app.MAP_TEMPLATES" />
<uses-permission android:name="com.google.android.gms.permission.CAR_FUEL" />
<uses-sdk tools:overrideLibrary="androidx.car.app" />
<uses-sdk tools:overrideLibrary="androidx.car.app,androidx.car.app.projected" />
<application>
<meta-data

View File

@@ -0,0 +1,43 @@
package net.vonforst.evmap.auto
import androidx.car.app.CarContext
import androidx.car.app.Screen
import androidx.car.app.hardware.CarHardwareManager
import androidx.car.app.hardware.common.OnCarDataAvailableListener
import androidx.car.app.hardware.info.EnergyLevel
import androidx.car.app.model.GridItem
import androidx.car.app.model.GridTemplate
import androidx.car.app.model.ItemList
import androidx.car.app.model.Template
import androidx.core.content.ContextCompat
class VehicleDataScreen(ctx: CarContext) : Screen(ctx), OnCarDataAvailableListener<EnergyLevel> {
val hardwareMan = ctx.getCarService(CarContext.HARDWARE_SERVICE) as CarHardwareManager
var energyLevel: EnergyLevel? = null
init {
hardwareMan.carInfo.addEnergyLevelListener(ContextCompat.getMainExecutor(ctx), this)
}
override fun onGetTemplate(): Template {
val energy = energyLevel ?: return GridTemplate.Builder().setLoading(true).build()
return GridTemplate.Builder().setSingleList(
ItemList.Builder().apply {
energy.batteryPercent.value?.let { percent ->
addItem(
GridItem.Builder()
.setTitle("Battery")
.setText("%.1f".format(percent))
.build()
)
}
}.build()
).build()
}
override fun onCarDataAvailable(data: EnergyLevel) {
this.energyLevel = energyLevel
invalidate()
}
}

View File

@@ -6,6 +6,7 @@ import androidx.car.app.Screen
import androidx.car.app.model.*
import androidx.core.graphics.drawable.IconCompat
import net.vonforst.evmap.R
import net.vonforst.evmap.auto.screens.VehicleDataScreen
/**
* Welcome screen with selection between favorites and nearby chargers
@@ -55,6 +56,21 @@ class WelcomeScreen(ctx: CarContext, val session: EVMapSession) : Screen(ctx), L
screenManager.push(MapScreen(carContext, session, favorites = true))
}
.build())
addItem(
Row.Builder()
.setTitle(carContext.getString(R.string.auto_vehicle_data))
.setImage(
CarIcon.Builder(
IconCompat.createWithResource(carContext, R.drawable.ic_car)
).setTint(CarColor.DEFAULT).build()
)
.setBrowsable(true)
.setOnClickListener {
session.mapScreen = null
screenManager.push(VehicleDataScreen(carContext))
}
.build()
)
}.build())
setCurrentLocationEnabled(true)
setHeaderAction(Action.APP_ICON)

View File

@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M18.92,6.01C18.72,5.42 18.16,5 17.5,5h-11c-0.66,0 -1.21,0.42 -1.42,1.01L3,12v8c0,0.55 0.45,1 1,1h1c0.55,0 1,-0.45 1,-1v-1h12v1c0,0.55 0.45,1 1,1h1c0.55,0 1,-0.45 1,-1v-8l-2.08,-5.99zM6.5,16c-0.83,0 -1.5,-0.67 -1.5,-1.5S5.67,13 6.5,13s1.5,0.67 1.5,1.5S7.33,16 6.5,16zM17.5,16c-0.83,0 -1.5,-0.67 -1.5,-1.5s0.67,-1.5 1.5,-1.5 1.5,0.67 1.5,1.5 -0.67,1.5 -1.5,1.5zM5,11l1.5,-4.5h11L19,11L5,11z" />
</vector>

View File

@@ -20,6 +20,7 @@
<string name="auto_favorites">Favoriten</string>
<string name="auto_fault_report_date">⚠️ Störungsmeldung (%s)</string>
<string name="auto_no_refresh_possible">Weitere Aktualisierung nicht möglich. Bitte zurück gehen und neu starten.</string>
<string name="auto_vehicle_data">Fahrzeugdaten</string>
<string name="welcome_android_auto">Android Auto-Unterstützung</string>
<string name="welcome_android_auto_detail">Auf unterstützen Autos kannst du EVMap auch mit Android Auto nutzen. Öffne dazu einfach die EVMap-App aus dem Menü von Android Auto.</string>
<string name="sounds_cool">klingt cool</string>

View File

@@ -30,6 +30,7 @@
<string name="auto_favorites">Favorites</string>
<string name="auto_fault_report_date">⚠️ Fault report (%s)</string>
<string name="auto_no_refresh_possible">Further updates not possible. Please go back and restart.</string>
<string name="auto_vehicle_data">Vehicle data</string>
<string name="welcome_android_auto">Android Auto support</string>
<string name="welcome_android_auto_detail">You can also use EVMap from within Android Auto on supported cars. Simply select the EVMap app in the Android Auto menu.</string>
<string name="sounds_cool">sounds cool</string>