home-assistant / android

:iphone: Home Assistant Companion for Android
https://companion.home-assistant.io/
Apache License 2.0
2.35k stars 656 forks source link

Catch automotive exception and allow sensors to be enabled via server side #4652

Closed dshokouhi closed 1 month ago

dshokouhi commented 2 months ago

Summary

Allows automotive sensors to be enabled in the play store build by enabling via the server side

User will get a notification to enable and will then be prompted to enable and grant permission upon selecting the notification.

Also catches the below crash I experienced using a new automotive emulator

2024-09-18 16:30:23.491 12966-12966 AndroidRuntime          io....stant.companion.android.debug  E  FATAL EXCEPTION: main (Ask Gemini)
                                                                                                    Process: io.homeassistant.companion.android.debug, PID: 12966
                                                                                                    java.lang.RuntimeException: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.car.drivingstate.CarUxRestrictions.isRequiresDistractionOptimization()' on a null object reference
                                                                                                        at androidx.car.app.utils.RemoteUtils.lambda$dispatchCallFromHost$0(RemoteUtils.java:153)
                                                                                                        at androidx.car.app.utils.RemoteUtils$$ExternalSyntheticLambda3.run(D8$$SyntheticClass:0)
                                                                                                        at androidx.car.app.utils.ThreadUtils.runOnMain(ThreadUtils.java:38)
                                                                                                        at androidx.car.app.utils.RemoteUtils.dispatchCallFromHost(RemoteUtils.java:145)
                                                                                                        at androidx.car.app.utils.RemoteUtils.lambda$dispatchCallFromHost$1(RemoteUtils.java:184)
                                                                                                        at androidx.car.app.utils.RemoteUtils$$ExternalSyntheticLambda4.run(D8$$SyntheticClass:0)
                                                                                                        at android.os.Handler.handleCallback(Handler.java:942)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                                        at android.os.Looper.loopOnce(Looper.java:201)
                                                                                                        at android.os.Looper.loop(Looper.java:288)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:7924)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
                                                                                                    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.car.drivingstate.CarUxRestrictions.isRequiresDistractionOptimization()' on a null object reference
                                                                                                        at io.homeassistant.companion.android.vehicle.BaseVehicleScreen.isDrivingOptimized(BaseVehicleScreen.kt:25)
                                                                                                        at io.homeassistant.companion.android.vehicle.MainVehicleScreen.onGetTemplate(MainVehicleScreen.kt:181)
                                                                                                        at androidx.car.app.Screen.getTemplateWrapper(Screen.java:359)
                                                                                                        at androidx.car.app.ScreenManager.getTopTemplate(ScreenManager.java:280)
                                                                                                        at androidx.car.app.AppManager$1$$ExternalSyntheticLambda1.dispatch(D8$$SyntheticClass:0)
                                                                                                        at androidx.car.app.utils.RemoteUtils.lambda$dispatchCallFromHost$0(RemoteUtils.java:148)
                                                                                                        at androidx.car.app.utils.RemoteUtils$$ExternalSyntheticLambda3.run(D8$$SyntheticClass:0) 
                                                                                                        at androidx.car.app.utils.ThreadUtils.runOnMain(ThreadUtils.java:38) 
                                                                                                        at androidx.car.app.utils.RemoteUtils.dispatchCallFromHost(RemoteUtils.java:145) 
                                                                                                        at androidx.car.app.utils.RemoteUtils.lambda$dispatchCallFromHost$1(RemoteUtils.java:184) 
                                                                                                        at androidx.car.app.utils.RemoteUtils$$ExternalSyntheticLambda4.run(D8$$SyntheticClass:0) 
                                                                                                        at android.os.Handler.handleCallback(Handler.java:942) 
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                                                        at android.os.Looper.loopOnce(Looper.java:201) 
                                                                                                        at android.os.Looper.loop(Looper.java:288) 
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:7924) 
                                                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) 
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936) 
2024-09-18 16:30:23.509  2466-2466  CarApp.H.Tem            com...pps.automotive.templates.host  E  Error: [type: null, cause: null, debug msg: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.car.drivingstate.CarUxRestrictions.isRequiresDistractionOptimization()' on a null object reference (Ask Gemini)
                                                                                                        at io.homeassistant.companion.android.vehicle.BaseVehicleScreen.isDrivingOptimized(BaseVehicleScreen.kt:25)
                                                                                                        at io.homeassistant.companion.android.vehicle.MainVehicleScreen.onGetTemplate(MainVehicleScreen.kt:181)
                                                                                                        at androidx.car.app.Screen.getTemplateWrapper(Screen.java:359)
                                                                                                        at androidx.car.app.ScreenManager.getTopTemplate(ScreenManager.java:280)
                                                                                                        at androidx.car.app.AppManager$1$$ExternalSyntheticLambda1.dispatch(D8$$SyntheticClass:0)
                                                                                                        at androidx.car.app.utils.RemoteUtils.lambda$dispatchCallFromHost$0(RemoteUtils.java:148)
                                                                                                        at androidx.car.app.utils.RemoteUtils$$ExternalSyntheticLambda3.run(D8$$SyntheticClass:0)
                                                                                                        at androidx.car.app.utils.ThreadUtils.runOnMain(ThreadUtils.java:38)
                                                                                                        at androidx.car.app.utils.RemoteUtils.dispatchCallFromHost(RemoteUtils.java:145)
                                                                                                        at androidx.car.app.utils.RemoteUtils.lambda$dispatchCallFromHost$1(RemoteUtils.java:184)
                                                                                                        at androidx.car.app.utils.RemoteUtils$$ExternalSyntheticLambda4.run(D8$$SyntheticClass:0)
                                                                                                        at android.os.Handler.handleCallback(Handler.java:942)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                                        at android.os.Looper.loopOnce(Looper.java:201)
                                                                                                        at android.os.Looper.loop(Looper.java:288)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:7924)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
                                                                                                    ]

Screenshots

image

image

image

image

Link to pull request in Documentation repository

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

Any other notes

The polestar emulator no longer seems to work so I have switched to an Android 13 API emulator that works pretty well and no longer crashes or requires a weird play store update. Process is pretty smooth as well.

dshokouhi commented 2 months ago

Leaving as a draft as we should only merge this after our current build receives approval

domeng83 commented 2 months ago

@dshokouhi General Motors makes their Google Built In emulator available via their developer portal - https://developer.gm.com/in-vehicle-apps this has all sensors enabled on it and would be viable for testing.

chrismelba commented 1 month ago

Love to see this being merged. Anyone have any idea how long it takes to become available on the play store?

dshokouhi commented 1 month ago

Love to see this being merged. Anyone have any idea how long it takes to become available on the play store?

this weeks beta push so 2024.10.2, every saturday evening all recently merged commits get pushed to the weekly beta

domeng83 commented 1 month ago

@dshokouhi just installed the update and have my sensors enabled. Very happy about this new feature.

I noticed from the commit that CarSensorManager class doesn't read value for range_remaining property. This is exposed via androidx in the [getRangeRemainingMeters](https://developer.android.com/reference/androidx/car/app/hardware/info/EnergyLevel#getRangeRemainingMeters()) call. Is there any plan to add this too?

Regardless, very happy about this new content.

image

dshokouhi commented 1 month ago

Is there any plan to add this too?

Merged PRs are not a place for feature requests to be made.