mirror of
https://github.com/ev-map/EVMap.git
synced 2026-04-21 14:48:37 -04:00
make VehicleDataScreen request permissions and work correctly
This commit is contained in:
@@ -11,7 +11,6 @@ import net.vonforst.evmap.R
|
||||
*/
|
||||
class PermissionScreen(
|
||||
ctx: CarContext,
|
||||
val session: EVMapSession,
|
||||
@StringRes val message: Int,
|
||||
val permissions: List<String>
|
||||
) : Screen(ctx) {
|
||||
|
||||
@@ -1,43 +1,102 @@
|
||||
package net.vonforst.evmap.auto
|
||||
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
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.car.app.hardware.info.Model
|
||||
import androidx.car.app.model.*
|
||||
import androidx.core.content.ContextCompat
|
||||
import net.vonforst.evmap.R
|
||||
|
||||
class VehicleDataScreen(ctx: CarContext) : Screen(ctx), OnCarDataAvailableListener<EnergyLevel> {
|
||||
val hardwareMan = ctx.getCarService(CarContext.HARDWARE_SERVICE) as CarHardwareManager
|
||||
var energyLevel: EnergyLevel? = null
|
||||
class VehicleDataScreen(ctx: CarContext) : Screen(ctx) {
|
||||
private val hardwareMan = ctx.getCarService(CarContext.HARDWARE_SERVICE) as CarHardwareManager
|
||||
private var model: Model? = null
|
||||
private var energyLevel: EnergyLevel? = null
|
||||
|
||||
init {
|
||||
hardwareMan.carInfo.addEnergyLevelListener(ContextCompat.getMainExecutor(ctx), this)
|
||||
}
|
||||
private val permissions = listOf(
|
||||
"com.google.android.gms.permission.CAR_FUEL"
|
||||
)
|
||||
|
||||
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()
|
||||
if (permissionsGranted()) {
|
||||
setupListeners()
|
||||
} else {
|
||||
Handler(Looper.getMainLooper()).post {
|
||||
screenManager.pushForResult(
|
||||
PermissionScreen(
|
||||
carContext,
|
||||
R.string.auto_location_permission_needed,
|
||||
permissions
|
||||
)
|
||||
) {
|
||||
setupListeners()
|
||||
}
|
||||
}.build()
|
||||
).build()
|
||||
}
|
||||
}
|
||||
|
||||
val energyLevel = energyLevel
|
||||
val model = model
|
||||
|
||||
return GridTemplate.Builder().apply {
|
||||
setTitle(
|
||||
if (model != null && model.manufacturer.value != null && model.name.value != null) {
|
||||
"${model.manufacturer.value} ${model.name.value}"
|
||||
} else {
|
||||
carContext.getString(R.string.auto_vehicle_data)
|
||||
}
|
||||
)
|
||||
setHeaderAction(Action.BACK)
|
||||
if (!permissionsGranted()) {
|
||||
setLoading(true)
|
||||
} else {
|
||||
setSingleList(
|
||||
ItemList.Builder().apply {
|
||||
addItem(
|
||||
GridItem.Builder().apply {
|
||||
setTitle("Battery")
|
||||
energyLevel?.batteryPercent?.value?.let { percent ->
|
||||
setText("%.1f".format(percent))
|
||||
setImage(CarIcon.APP_ICON)
|
||||
} ?: setLoading(true)
|
||||
}.build()
|
||||
)
|
||||
addItem(
|
||||
GridItem.Builder().apply {
|
||||
setTitle("Fuel")
|
||||
energyLevel?.fuelPercent?.value?.let { percent ->
|
||||
setText("%.1f".format(percent))
|
||||
setImage(CarIcon.APP_ICON)
|
||||
} ?: setLoading(true)
|
||||
}.build()
|
||||
)
|
||||
}.build()
|
||||
)
|
||||
}
|
||||
}.build()
|
||||
}
|
||||
|
||||
override fun onCarDataAvailable(data: EnergyLevel) {
|
||||
this.energyLevel = energyLevel
|
||||
invalidate()
|
||||
private fun setupListeners() {
|
||||
val exec = ContextCompat.getMainExecutor(carContext)
|
||||
hardwareMan.carInfo.addEnergyLevelListener(exec) {
|
||||
this.energyLevel = it
|
||||
invalidate()
|
||||
}
|
||||
|
||||
hardwareMan.carInfo.fetchModel(exec) {
|
||||
this.model = it
|
||||
invalidate()
|
||||
}
|
||||
}
|
||||
|
||||
private fun permissionsGranted(): Boolean =
|
||||
permissions.all {
|
||||
ContextCompat.checkSelfPermission(
|
||||
carContext,
|
||||
it
|
||||
) == PackageManager.PERMISSION_GRANTED
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,6 @@ class WelcomeScreen(ctx: CarContext, val session: EVMapSession) : Screen(ctx), L
|
||||
screenManager.pushForResult(
|
||||
PermissionScreen(
|
||||
carContext,
|
||||
session,
|
||||
R.string.auto_location_permission_needed,
|
||||
listOf(Manifest.permission.ACCESS_FINE_LOCATION)
|
||||
)
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
<string name="open_in_app">In App öffnen</string>
|
||||
<string name="opened_on_phone">Auf dem Telefon geöffnet</string>
|
||||
<string name="auto_location_permission_needed">Um EVMap auf Android Auto zu nutzen, braucht die App Zugriff auf deinen Standort.</string>
|
||||
<string name="auto_vehicle_data_permission_needed">Für diese Funktion benötigt EVMap Zugriff auf Daten deines Fahrzeugs.</string>
|
||||
<string name="grant_on_phone">Auf Telefon zulassen</string>
|
||||
<string name="auto_chargers_closeby">In der Nähe</string>
|
||||
<string name="auto_favorites">Favoriten</string>
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
<string name="open_in_app">Open in app</string>
|
||||
<string name="opened_on_phone">Opened on phone</string>
|
||||
<string name="auto_location_permission_needed">To run EVMap on Android Auto, you need to grant access to your location.</string>
|
||||
<string name="auto_vehicle_data_permission_needed">For this feature, EVMap needs access to your vehicle data.</string>
|
||||
<string name="grant_on_phone">Grant on phone</string>
|
||||
<string name="auto_chargers_closeby">Nearby chargers</string>
|
||||
<string name="auto_favorites">Favorites</string>
|
||||
|
||||
Reference in New Issue
Block a user