Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate car sensors with automotive, and added permissions #4122

Merged

Conversation

ivancea
Copy link
Contributor

@ivancea ivancea commented Jan 6, 2024

Summary

Integrate car sensors with Android Automotive:

  • Added required permissions to manifest
  • Updated the car sensors manager logic with the automotive permissions, separated from the auto ones

To better separate Auto from Automotive, I decided to add the core differences (Sensor availability and permissions) to the sensors themselves. This way, we have all the sensor logic and requirements in the same location, which is interesting when we start adding more sensors.

For that I made a CarSensor wrapper class of BasicSensor (I considered inheritance, but data classes are final, and I'm not sure changing them would have been worth it). Everything else is quite the same, but using the sensor objects instead.

Screenshots

Example entities on an Automotive emulator:
imagen

The permission request on enabling some sensors:
imagen

Link to pull request in Documentation repository

Documentation: home-assistant/companion.home-assistant#

We could change the "Currently unavailabe for AAOS users" to "Currently unavailabe for AAOS users from Google Play" (Or similar). But I'd say it's not worth it yet, at least until the full flow is ready (?)

Any other notes

Only available in Automotive Full flavor (Not in the Google Play version yet). Sensors must be manually enabled, which can't be done in the minimal flavor. Enabling the sensors from the server won't ask for permissions from what I tested, so it may only work for some.

override val name: Int
get() = R.string.sensor_name_car

override suspend fun getAvailableSensors(context: Context): List<SensorManager.BasicSensor> {
this.context = context.applicationContext
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those lateinit context set are a bit sketchy. I had to set it from more methods, as it wasn't set in some calls. I added it to every method accepting a context, just in case it happens again in the future.

I'm considering 2 other options: Adding it on construction (Not sure if possible), or simply drilling it through method calls, so we're sure we actually have a context when calling something that needs it.

I'd like to see more opinions about this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we do actually use a latestContext method of storing the latest context received in the app for some sensors, very similar to what you are doing here.

https://github.com/home-assistant/android/blob/master/common/src/main/java/io/homeassistant/companion/android/common/sensors/LightSensorManager.kt#L57

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Renaming to latestContext, to keep the same naming as the other sensors

@ivancea ivancea marked this pull request as ready for review January 6, 2024 23:08
@ivancea ivancea requested a review from jpelgrom January 7, 2024 21:31

private fun allDisabled(): Boolean = sensorsList.none { isEnabled(context, it) }
if (isAutomotive && !carSensor.automotiveEnabled || !isAutomotive && !carSensor.autoEnabled) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to add some parenthesis here so the conditions are properly evaluated?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding them to improve readability

@chrismelba
Copy link

Looks really good, will be following with interest

Copy link
Member

@dshokouhi dshokouhi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran a local test with the changes and don't see any issues. Thank you for taking this on!

@JBassett JBassett merged commit b0f1aa2 into home-assistant:master Jan 24, 2024
4 checks passed
@ivancea ivancea deleted the feat/automotive-enable-sensors branch January 24, 2024 16:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants