-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Add command_screen_off to turn the display off while the device stays awake #7162
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
Draft
markfrancisonly
wants to merge
11
commits into
home-assistant:main
Choose a base branch
from
markfrancisonly:feature/add-screen-off-command
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+525
−11
Draft
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9fc056f
Add command_screen_off notification command
markfrancisonly 5d6fe8e
Open the device admin screen through an activity
markfrancisonly 327010a
Finish the admin request activity on recreation and reword the changelog
markfrancisonly 7d9ee3f
Merge branch 'main' into feature/add-screen-off-command
markfrancisonly 67811d2
Merge remote-tracking branch 'upstream/main' into feature/add-screen-…
markfrancisonly be39869
Move all wake lock handling into ScreenOffHelper and exclude Automotive
markfrancisonly cac0678
Merge branch 'main' into feature/add-screen-off-command
markfrancisonly 81e571d
Merge branch 'main' into feature/add-screen-off-command
markfrancisonly 4c8bb53
Keep the frontend running while the screen is commanded off
markfrancisonly a8a3644
Drop the wake lock and the frontend keep-running bypass
markfrancisonly bb67524
Never request the device admin behind a secure keyguard
markfrancisonly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
app/src/main/kotlin/io/homeassistant/companion/android/util/ScreenOffAdminReceiver.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package io.homeassistant.companion.android.util | ||
|
|
||
| import android.app.admin.DeviceAdminReceiver | ||
| import android.content.Context | ||
| import android.content.Intent | ||
| import dagger.hilt.android.AndroidEntryPoint | ||
| import javax.inject.Inject | ||
|
|
||
| /** Device admin holding the force lock policy so [ScreenOffHelper] can cut the display power. */ | ||
| @AndroidEntryPoint | ||
| class ScreenOffAdminReceiver : DeviceAdminReceiver() { | ||
|
|
||
| @Inject | ||
| lateinit var screenOffHelper: ScreenOffHelper | ||
|
|
||
| // Without this the wake lock held while the screen was off would never be released | ||
| override fun onDisabled(context: Context, intent: Intent) { | ||
| screenOffHelper.turnScreenOn() | ||
| } | ||
| } |
49 changes: 49 additions & 0 deletions
49
app/src/main/kotlin/io/homeassistant/companion/android/util/ScreenOffAdminRequestActivity.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package io.homeassistant.companion.android.util | ||
|
|
||
| import android.app.admin.DevicePolicyManager | ||
| import android.content.ActivityNotFoundException | ||
| import android.content.ComponentName | ||
| import android.content.Intent | ||
| import android.os.Bundle | ||
| import androidx.activity.ComponentActivity | ||
| import androidx.activity.result.contract.ActivityResultContracts | ||
| import io.homeassistant.companion.android.common.R as commonR | ||
| import timber.log.Timber | ||
|
|
||
| /** | ||
| * Opens the system screen to activate [ScreenOffAdminReceiver] as device admin. Routed through | ||
| * this transparent activity since some manufacturers, like Samsung, refuse to open that screen | ||
| * from a new task, which is the only way the notification handling can start an activity. | ||
| */ | ||
| class ScreenOffAdminRequestActivity : ComponentActivity() { | ||
|
|
||
| private val requestAdmin = | ||
| registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { finish() } | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| if (savedInstanceState != null) { | ||
| // A recreated instance cannot count on a pending result to finish it, so leave | ||
| // instead of lingering transparently over the task | ||
| finish() | ||
| return | ||
| } | ||
|
|
||
|
markfrancisonly marked this conversation as resolved.
|
||
| val intent = Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN) | ||
| .putExtra( | ||
| DevicePolicyManager.EXTRA_DEVICE_ADMIN, | ||
| ComponentName(this, ScreenOffAdminReceiver::class.java), | ||
| ) | ||
| .putExtra( | ||
| DevicePolicyManager.EXTRA_ADD_EXPLANATION, | ||
| getString(commonR.string.screen_off_admin_description), | ||
| ) | ||
| try { | ||
| requestAdmin.launch(intent) | ||
| } catch (e: ActivityNotFoundException) { | ||
| // Some devices, like Android Automotive, have no device admin settings | ||
|
markfrancisonly marked this conversation as resolved.
Outdated
|
||
| Timber.w(e, "Unable to open the device admin activation screen") | ||
| finish() | ||
| } | ||
| } | ||
| } | ||
78 changes: 78 additions & 0 deletions
78
app/src/main/kotlin/io/homeassistant/companion/android/util/ScreenOffHelper.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| package io.homeassistant.companion.android.util | ||
|
|
||
| import android.annotation.SuppressLint | ||
| import android.app.KeyguardManager | ||
| import android.app.admin.DevicePolicyManager | ||
| import android.content.ComponentName | ||
| import android.content.Context | ||
| import android.os.PowerManager | ||
| import androidx.annotation.VisibleForTesting | ||
| import androidx.core.content.getSystemService | ||
| import dagger.hilt.android.qualifiers.ApplicationContext | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
| import timber.log.Timber | ||
|
|
||
| private const val SCREEN_OFF_WAKE_LOCK_TAG = "HomeAssistant::NotificationScreenOffWakeLock" | ||
|
|
||
| /** | ||
| * Cuts the display power with [DevicePolicyManager.lockNow] while a partial wake lock keeps the | ||
| * device awake. Requires [ScreenOffAdminReceiver] active as device admin and no secure keyguard. | ||
| * All functions must be called from the main thread, the wake lock state is not synchronized. | ||
| */ | ||
| @Singleton | ||
| class ScreenOffHelper @Inject constructor(@ApplicationContext private val context: Context) { | ||
|
|
||
| private val devicePolicyManager by lazy { context.getSystemService<DevicePolicyManager>() } | ||
| private val keyguardManager by lazy { context.getSystemService<KeyguardManager>() } | ||
| private val powerManager by lazy { context.getSystemService<PowerManager>() } | ||
| private val adminComponent by lazy { ComponentName(context, ScreenOffAdminReceiver::class.java) } | ||
|
|
||
| private var screenOffWakeLock: PowerManager.WakeLock? = null | ||
|
|
||
| /** Whether the screen is currently turned off by [turnScreenOff]. */ | ||
| @VisibleForTesting | ||
| internal val isScreenOff: Boolean | ||
| get() = screenOffWakeLock != null | ||
|
|
||
| /** Whether [ScreenOffAdminReceiver] is active as device admin so [turnScreenOff] can be used. */ | ||
| fun canTurnScreenOff(): Boolean = devicePolicyManager?.isAdminActive(adminComponent) == true | ||
|
|
||
| /** | ||
| * @return `true` if the screen was turned off, `false` when the device admin is not active or | ||
| * a secure keyguard is set | ||
| */ | ||
| // The wake lock is held until the screen is turned back on, no timeout would be safe | ||
| @SuppressLint("WakelockTimeout") | ||
| fun turnScreenOff(): Boolean { | ||
| val devicePolicyManager = devicePolicyManager ?: return false | ||
| if (keyguardManager?.isDeviceSecure != false) { | ||
| Timber.w("Not turning the screen off, the secure keyguard would lock the device") | ||
| return false | ||
| } | ||
| return try { | ||
| if (screenOffWakeLock == null) { | ||
| // Keep the CPU and network awake so the device stays reachable by the server | ||
| screenOffWakeLock = powerManager | ||
| ?.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, SCREEN_OFF_WAKE_LOCK_TAG) | ||
| ?.apply { acquire() } | ||
| } | ||
| devicePolicyManager.lockNow() | ||
| true | ||
| } catch (e: SecurityException) { | ||
| Timber.e(e, "Device admin is not active, cannot turn the screen off") | ||
| turnScreenOn() | ||
| false | ||
| } | ||
| } | ||
|
|
||
| /** @return `true` if the screen was turned off before, `false` if there was nothing to do */ | ||
| fun turnScreenOn(): Boolean { | ||
| val wakeLock = screenOffWakeLock ?: return false | ||
| screenOffWakeLock = null | ||
| if (wakeLock.isHeld) { | ||
| wakeLock.release() | ||
| } | ||
| return true | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <device-admin xmlns:android="http://schemas.android.com/apk/res/android"> | ||
| <uses-policies> | ||
| <force-lock /> | ||
| </uses-policies> | ||
| </device-admin> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.