I'm trying to setup a small prototype to extract some information from the car using CarInfo API with the Polestar 2 Emulator.
I've been facing the problem of not being able to actually run the code. I'm following the tutorial on Using the Android for Cars App Library .
My current implementation looks like this:
HelloWorldService
class HelloWorldService: CarAppService() {
override fun createHostValidator(): HostValidator {
TODO("Not yet implemented")
}
override fun onCreateSession(): Session {
Log.d("TEST_LOG", "HelloWorldService is called")
return HelloWorldSession()
}
}
HelloWorldSession
class HelloWorldSession: Session() {
override fun onCreateScreen(intent: Intent): Screen {
Log.d("TEST_LOG", "HelloWorldSession is called")
CarToast.makeText(carContext, "Hello world", CarToast.LENGTH_LONG).show()
return HelloWorldScreen(carContext)
}
}
HelloWorldScreen
private const val TAG = "CAR_INFO"
class HelloWorldScreen(carContext: CarContext) : Screen(carContext) {
override fun onGetTemplate(): Template {
logCarInfo()
Log.d(TAG, "HelloWorldScreen is called")
val row = Row.Builder().setTitle("Hello automotive world!").build()
val pane = Pane.Builder().addRow(row).build()
return PaneTemplate.Builder(pane)
.setHeaderAction(Action.APP_ICON)
.build()
}
@SuppressLint("UnsafeOptInUsageError")
fun logCarInfo() {
val carInfo = carContext.getCarService(CarHardwareManager::class.java).carInfo
val energyLevel = OnCarDataAvailableListener<EnergyLevel> { data ->
Log.d(TAG, "EnergyLevel: ${data.energyIsLow}")
Log.d(TAG, "EnergyLevel: ${data.rangeRemainingMeters}")
}
val speedInfo = OnCarDataAvailableListener<Speed> { data ->
Log.d(TAG, "SpeedInfo: ${data.rawSpeedMetersPerSecond}")
Log.d(TAG, "SpeedInfo: ${data.displaySpeedMetersPerSecond}")
}
}
}
Android Manifest (automotive module)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-feature
android:name="android.hardware.type.automotive"
android:required="true" />
<uses-feature
android:name="android.software.car.templates_host"
android:required="true" />
<uses-feature
android:name="android.hardware.wifi"
android:required="false" />
<uses-feature
android:name="android.hardware.screen.portrait"
android:required="false" />
<uses-feature
android:name="android.hardware.screen.landscape"
android:required="false" />
<uses-permission android:name="android.car.permission.CAR_INFO" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="androidx.car.app.NAVIGATION_TEMPLATES" />
<uses-permission android:name="androidx.car.app.ACCESS_SURFACE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<application
android:allowBackup="true"
android:appCategory="video"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ContextInfo">
<meta-data
android:name="com.android.automotive"
android:resource="@xml/automotive_app_desc" />
<service
android:name=".HelloWorldService"
android:exported="true">
<intent-filter>
<action android:name="androidx.car.app.CarAppService" />
</intent-filter>
</service>
</application>
</manifest>
As I understood from the docs Android Automotive apps don't launch with an Activity and rather are services but I don't get how to actually run it? I'm trying to keep track with the Logs on what is being launched but with no success at all.
Would appreciate your support.