Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand Down
31 changes: 15 additions & 16 deletions app/src/main/java/com/rangelreale/devsettings/ui/MainScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)) },
)
}

Expand Down Expand Up @@ -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()) { }
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
<string name="notification_title">Keeping the screen on</string>
<string name="notification_indefinite">Until you stop it</string>
<string name="stop">Stop</string>
<string name="quest_keep_awake_unsupported">Stay awake and Keep screen on are hidden on Meta Quest. The headset sleeps when you take it off, whatever these settings say.</string>
<string name="keep_headset_awake">Keep headset awake</string>
<string name="keep_headset_awake_detail">Keeps the headset awake when you take it off, by overriding the proximity sensor. Uses more battery.</string>
<string name="notification_title_quest">Keeping the headset awake</string>
<string name="keep_screen_on_detail">Holds the screen on, charging or not.</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading