This PR implements the command_screen_off notification command, all… - #6973
This PR implements the command_screen_off notification command, all…#6973rvroon wants to merge 4 commits into
command_screen_off notification command, all…#6973Conversation
…owing users to remotely lock their Android device screen via Home Assistant notifications.
|
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍 |
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds support for remotely locking the device screen via a new command_screen_off notification command, powered by Android Device Administrator.
Changes:
- Introduces a
DeviceAdminReceiverand device admin policy XML to enableDevicePolicyManager.lockNow(). - Adds
command_screen_offhandling, permission request flow, and user-facing explanation text. - Registers the device admin receiver in the app manifest.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| common/src/main/res/values/strings.xml | Adds user-facing explanation shown during device-admin activation. |
| app/src/main/res/xml/device_admin.xml | Declares the device admin policy (force-lock). |
| app/src/main/kotlin/io/homeassistant/companion/android/notifications/ScreenOffAdminReceiver.kt | Adds the required DeviceAdminReceiver implementation. |
| app/src/main/kotlin/io/homeassistant/companion/android/notifications/MessagingManager.kt | Adds new command constant, dispatch, lockNow execution, and permission request intent. |
| app/src/main/AndroidManifest.xml | Registers the admin receiver and links it to the policy XML. |
| COMMAND_SCREEN_OFF -> { | ||
| val devicePolicyManager = context.getSystemService<DevicePolicyManager>() | ||
| val adminComponent = ComponentName(context, ScreenOffAdminReceiver::class.java) | ||
|
|
||
| if (devicePolicyManager?.isAdminActive(adminComponent) == true) { | ||
| Timber.d("Locking device screen") | ||
| devicePolicyManager.lockNow() | ||
| } else { | ||
| notifyMissingPermission(message, serverId) | ||
| } | ||
| } |
| <receiver | ||
| android:name=".notifications.ScreenOffAdminReceiver" | ||
| android:permission="android.permission.BIND_DEVICE_ADMIN" | ||
| android:exported="true"> | ||
| <meta-data | ||
| android:name="android.app.device_admin" | ||
| android:resource="@xml/device_admin" /> | ||
| <intent-filter> | ||
| <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" /> | ||
| </intent-filter> | ||
| </receiver> |
| <string name="delete_shortcut">Delete shortcut</string> | ||
| <string name="description">Description</string> | ||
| <string name="details">Details</string> | ||
| <string name="device_admin_explanation">Home Assistant requires device administrator permission to lock your screen remotely using the command_screen_off notification command.</string> |
| private fun requestDeviceAdminPermission() { | ||
| val intent = Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN).apply { | ||
| putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, ComponentName(context, ScreenOffAdminReceiver::class.java)) | ||
| putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, context.getString(commonR.string.device_admin_explanation)) | ||
| flags = Intent.FLAG_ACTIVITY_NEW_TASK | ||
| } | ||
| context.startActivity(intent) | ||
| } |
There was a problem hiding this comment.
Valid concern, but already a pre-existing pattern in this file — not a new risk introduced by this code.
Looking at the code around lines 1801–1834, there are already 5 other functions using the exact same pattern of context.startActivity(intent) with FLAG_ACTIVITY_NEW_TASK:
requestDNDPermission()(line 1801)requestSystemAlertPermission()(line 1808)requestNotificationPermission()(line 1817)navigateAppDetails()(line 1823)requestWriteSystemPermission()(line 1829)
| COMMAND_SCREEN_OFF -> { | ||
| handleDeviceCommands(jsonData) | ||
| } |
There was a problem hiding this comment.
Each command may diverge in behavior in the future (e.g., adding validation like COMMAND_MEDIA has)
|
We previously rejected this due to requiring the device admin role, as it is quite 'heavy': #2520. I believe Google Play is also quite strict in when apps are allowed to use this, with multiple reports online (example). My opinion hasn't really changed on this subject but I'm interested to know what @TimoPtr thinks. In case you were not aware, as a workaround of sorts, you can adjust the screen off timeout to a very low setting to quickly turn off the device. |
@jpelgrom Thanks for your suggestion. I tried this, also following this thread: https://community.home-assistant.io/t/turn-off-screen-of-android-device/338013 |
|
I'm sharing the same feeling than @jpelgrom here. I'm not feeling confident for the app to get the ADMIN device permission. Also I would like to push back a little on adding new command until we come up with a refactor on the mobile_app integration to make the usage of these commands user friendly. This feature could be quite invasive for secondary users for instance. Lastly I would not call With both of us @jpelgrom not being confident at the moment I'm closing the PR until we have a plan how to deliver this properly. |
…owing users to remotely lock their Android device screen via Home Assistant notifications.
Summary
This PR implements the
command_screen_offnotification command, allowing users to remotely lock their Android device screen via Home Assistant notifications.Checklist
Link to pull request in documentation repositories
User Documentation: home-assistant/companion.home-assistant#
Any other notes