diff --git a/README.md b/README.md index 9fd314c..f287124 100644 --- a/README.md +++ b/README.md @@ -111,8 +111,14 @@ The app recognises a Quest by the `oculus.hardware.standalone_vr` system feature - USB debugging and wireless debugging work. Turning on wireless debugging opens the Quest's own dialog asking to allow the network, and the app shows the address for `adb connect`. -- Stay awake and Keep screen on are hidden. The proximity sensor puts the headset to sleep about 20 seconds after - it is taken off, and neither setting prevents that. +- The proximity sensor puts the headset to sleep about 20 seconds after it is taken off. Neither Stay awake nor a + screen wake lock prevents that, so Stay awake is hidden. +- Keep screen on becomes "Keep headset awake". Besides the wake lock, it sends the + `com.oculus.vrpowermanager.prox_close` broadcast, which makes the headset act as if it is worn. This is the + same override that the Proximity Sensor switch in Meta Quest Developer Hub uses, and it needs no permission. The + app sends it again every minute, and sends `com.oculus.vrpowermanager.automation_disable` when the time runs out + or you stop it. The headset uses more battery while the sensor is overridden. If Android kills the app first, the + override stays on until it lapses or the headset restarts. - The warning that Developer options are off is hidden. The Quest does not set `development_settings_enabled`; developer mode is turned on in the Meta Horizon app instead. - Quick Settings tiles are not available, because Horizon OS has no Quick Settings panel for them. diff --git a/app/src/main/java/com/rangelreale/devsettings/data/DevicePlatform.kt b/app/src/main/java/com/rangelreale/devsettings/data/DevicePlatform.kt index d815bc5..d5a4e1c 100644 --- a/app/src/main/java/com/rangelreale/devsettings/data/DevicePlatform.kt +++ b/app/src/main/java/com/rangelreale/devsettings/data/DevicePlatform.kt @@ -6,16 +6,19 @@ package com.rangelreale.devsettings.data * Tested on a Quest 3 with Horizon OS on Android 14: * - `development_settings_enabled` is not set, although USB and wireless debugging work. * - The proximity sensor puts the headset to sleep about 20 seconds after it is taken off. - * Neither `stay_on_while_plugged_in` nor a screen wake lock prevents this. + * Neither `stay_on_while_plugged_in` nor a screen wake lock prevents this, but the + * `prox_close` broadcast in [ProximityOverride] does. */ enum class DevicePlatform( /** True when `development_settings_enabled` reflects whether the debugging settings take effect. */ val usesDeveloperOptionsFlag: Boolean, - /** True when "Stay awake while charging" and "Keep screen on" keep the device awake. */ - val supportsKeepAwake: Boolean, + /** True when "Stay awake while charging" keeps the device awake. */ + val supportsStayAwake: Boolean, + /** True when keeping the device awake also needs [ProximityOverride]. */ + val needsProximityOverride: Boolean, ) { - PHONE(usesDeveloperOptionsFlag = true, supportsKeepAwake = true), - META_QUEST(usesDeveloperOptionsFlag = false, supportsKeepAwake = false), + PHONE(usesDeveloperOptionsFlag = true, supportsStayAwake = true, needsProximityOverride = false), + META_QUEST(usesDeveloperOptionsFlag = false, supportsStayAwake = false, needsProximityOverride = true), ; companion object { @@ -28,6 +31,26 @@ enum class DevicePlatform( } } +/** + * Broadcasts that override the Quest's proximity sensor. They come from Meta's testing tools and + * need no permission. Tested on a Quest 3 from an ordinary app. + */ +object ProximityOverride { + /** Makes the headset act as if it is worn, so it does not sleep when taken off. */ + const val ACTION_KEEP_AWAKE = "com.oculus.vrpowermanager.prox_close" + + /** Hands control back to the proximity sensor. */ + const val ACTION_RELEASE = "com.oculus.vrpowermanager.automation_disable" + + /** + * How often to send [ACTION_KEEP_AWAKE] again while it is wanted. Meta Quest Developer Hub's + * override lapses about 10 minutes after it stops renewing it, so renew well within that. + */ + const val RENEW_INTERVAL_MILLIS = 60_000L + + fun action(keepAwake: Boolean): String = if (keepAwake) ACTION_KEEP_AWAKE else ACTION_RELEASE +} + /** True when the app should warn that Developer options are off. */ fun DevSettingsRepository.shouldWarnDeveloperOptionsOff(platform: DevicePlatform): Boolean = platform.usesDeveloperOptionsFlag && !developerOptionsEnabled() diff --git a/app/src/main/java/com/rangelreale/devsettings/keepon/KeepScreenOnService.kt b/app/src/main/java/com/rangelreale/devsettings/keepon/KeepScreenOnService.kt index f7d8eb4..8024879 100644 --- a/app/src/main/java/com/rangelreale/devsettings/keepon/KeepScreenOnService.kt +++ b/app/src/main/java/com/rangelreale/devsettings/keepon/KeepScreenOnService.kt @@ -20,17 +20,32 @@ import androidx.core.app.ServiceCompat import androidx.core.content.ContextCompat import com.rangelreale.devsettings.MainActivity import com.rangelreale.devsettings.R +import com.rangelreale.devsettings.data.DevicePlatform +import com.rangelreale.devsettings.data.ProximityOverride import com.rangelreale.devsettings.tiles.KeepScreenOnTileService /** * Holds a screen wake lock for a chosen duration, with an ongoing notification * that counts down and offers a Stop action. + * + * On a Meta Quest the wake lock is not enough, because the proximity sensor puts the headset to + * sleep when it is taken off. There the service also overrides the sensor with [ProximityOverride] + * and hands control back when it stops. If the process is killed without [onDestroy], the override + * lasts until it lapses or the headset restarts. */ class KeepScreenOnService : Service() { private val handler = Handler(Looper.getMainLooper()) private var wakeLock: PowerManager.WakeLock? = null private val stopRunnable = Runnable { stopSelf() } + private val platform by lazy { DevicePlatform.detect(packageManager::hasSystemFeature) } + private var proximityOverridden = false + private val renewProximityRunnable = object : Runnable { + override fun run() { + sendProximityOverride(keepAwake = true) + handler.postDelayed(this, ProximityOverride.RENEW_INTERVAL_MILLIS) + } + } override fun onBind(intent: Intent?): IBinder? = null @@ -60,6 +75,11 @@ class KeepScreenOnService : Service() { acquireWakeLock(duration.millis) handler.removeCallbacks(stopRunnable) duration.millis?.let { handler.postDelayed(stopRunnable, it) } + if (platform.needsProximityOverride) { + handler.removeCallbacks(renewProximityRunnable) + renewProximityRunnable.run() + proximityOverridden = true + } KeepScreenOnState.set(status) refreshTile() @@ -68,6 +88,11 @@ class KeepScreenOnService : Service() { override fun onDestroy() { handler.removeCallbacks(stopRunnable) + handler.removeCallbacks(renewProximityRunnable) + if (proximityOverridden) { + sendProximityOverride(keepAwake = false) + proximityOverridden = false + } releaseWakeLock() KeepScreenOnState.set(null) refreshTile() @@ -96,6 +121,11 @@ class KeepScreenOnService : Service() { wakeLock = null } + // The receivers are registered at run time inside the system, so the broadcast stays implicit. + private fun sendProximityOverride(keepAwake: Boolean) { + sendBroadcast(Intent(ProximityOverride.action(keepAwake))) + } + private fun buildNotification(status: KeepScreenOnStatus): Notification { val manager = getSystemService(NotificationManager::class.java) manager.createNotificationChannel( @@ -121,7 +151,11 @@ class KeepScreenOnService : Service() { val builder = Notification.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.ic_screen) - .setContentTitle(getString(R.string.notification_title)) + .setContentTitle( + getString( + if (platform.needsProximityOverride) R.string.notification_title_quest else R.string.notification_title, + ), + ) .setOngoing(true) .setOnlyAlertOnce(true) .setContentIntent(openApp) diff --git a/app/src/main/java/com/rangelreale/devsettings/ui/MainScreen.kt b/app/src/main/java/com/rangelreale/devsettings/ui/MainScreen.kt index f6d85a9..99c6510 100644 --- a/app/src/main/java/com/rangelreale/devsettings/ui/MainScreen.kt +++ b/app/src/main/java/com/rangelreale/devsettings/ui/MainScreen.kt @@ -219,7 +219,8 @@ fun MainScreen(repository: DevSettingsRepository) { onCopy = ::copy, onOpenPairing = { SystemScreens.openWirelessDebugging(context) }, ) - if (platform.supportsKeepAwake) { + // On a Quest the proximity sensor overrides this setting, so the row is left out. + if (platform.supportsStayAwake) { HorizontalDivider() val stayAwake = snapshot.enabled.getValue(DevSetting.STAY_AWAKE) SettingRow( @@ -229,20 +230,13 @@ fun MainScreen(repository: DevSettingsRepository) { onCheckedChange = { onSwitch(DevSetting.STAY_AWAKE, it) }, stateText = if (stayAwake) stringResource(R.string.stay_awake_on_detail) else null, ) - HorizontalDivider() - KeepScreenOnSection( - status = keepOnStatus, - enabled = snapshot.hasPermission, - onStartFailed = { showMessage(context.getString(R.string.keep_screen_on_failed)) }, - ) } - } - - if (!platform.supportsKeepAwake) { - Text( - stringResource(R.string.quest_keep_awake_unsupported), - style = MaterialTheme.typography.bodySmall, - modifier = Modifier.padding(horizontal = 16.dp), + HorizontalDivider() + KeepScreenOnSection( + status = keepOnStatus, + platform = platform, + enabled = snapshot.hasPermission, + onStartFailed = { showMessage(context.getString(R.string.keep_screen_on_failed)) }, ) } @@ -427,9 +421,11 @@ private fun WirelessDetails( @Composable private fun KeepScreenOnSection( status: KeepScreenOnStatus?, + platform: DevicePlatform, enabled: Boolean, onStartFailed: () -> Unit, ) { + val quest = platform.needsProximityOverride val context = LocalContext.current var selected by rememberSaveable { mutableStateOf(KeepScreenOnDuration.MIN_15) } val notificationPermission = rememberLauncherForActivityResult(ActivityResultContracts.RequestPermission()) { } @@ -461,14 +457,17 @@ private fun KeepScreenOnSection( RowItem( icon = R.drawable.ic_screen, - title = stringResource(R.string.keep_screen_on), + title = stringResource(if (quest) R.string.keep_headset_awake else R.string.keep_screen_on), state = stateText, checked = status != null, enabled = enabled, onCheckedChange = { on -> if (on) start(selected) else KeepScreenOnService.stop(context) }, ) Column(Modifier.padding(start = 56.dp, end = 16.dp, bottom = 12.dp)) { - Text(stringResource(R.string.keep_screen_on_detail), style = MaterialTheme.typography.bodySmall) + Text( + stringResource(if (quest) R.string.keep_headset_awake_detail else R.string.keep_screen_on_detail), + style = MaterialTheme.typography.bodySmall, + ) FlowRow(horizontalArrangement = Arrangement.spacedBy(8.dp)) { KeepScreenOnDuration.entries.forEach { duration -> val isSelected = (status?.duration ?: selected) == duration diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a62c2a0..2f2c808 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -56,6 +56,8 @@ Keeping the screen on Until you stop it Stop - Stay awake and Keep screen on are hidden on Meta Quest. The headset sleeps when you take it off, whatever these settings say. + Keep headset awake + Keeps the headset awake when you take it off, by overriding the proximity sensor. Uses more battery. + Keeping the headset awake Holds the screen on, charging or not. diff --git a/app/src/test/java/com/rangelreale/devsettings/data/DevicePlatformTest.kt b/app/src/test/java/com/rangelreale/devsettings/data/DevicePlatformTest.kt index 7e54ed2..5aa214d 100644 --- a/app/src/test/java/com/rangelreale/devsettings/data/DevicePlatformTest.kt +++ b/app/src/test/java/com/rangelreale/devsettings/data/DevicePlatformTest.kt @@ -22,9 +22,26 @@ class DevicePlatformTest { } @Test - fun onlyPhoneSupportsKeepAwake() { - assertTrue(DevicePlatform.PHONE.supportsKeepAwake) - assertFalse(DevicePlatform.META_QUEST.supportsKeepAwake) + fun onlyPhoneSupportsStayAwake() { + assertTrue(DevicePlatform.PHONE.supportsStayAwake) + assertFalse(DevicePlatform.META_QUEST.supportsStayAwake) + } + + @Test + fun onlyQuestNeedsProximityOverride() { + assertFalse(DevicePlatform.PHONE.needsProximityOverride) + assertTrue(DevicePlatform.META_QUEST.needsProximityOverride) + } + + @Test + fun proximityOverrideActions() { + assertEquals("com.oculus.vrpowermanager.prox_close", ProximityOverride.action(keepAwake = true)) + assertEquals("com.oculus.vrpowermanager.automation_disable", ProximityOverride.action(keepAwake = false)) + } + + @Test + fun proximityOverrideRenewsWellBeforeItLapses() { + assertTrue(ProximityOverride.RENEW_INTERVAL_MILLIS in 1..5 * 60_000L) } @Test