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
37 changes: 36 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ An Android app that toggles the developer settings I use most, without opening D
- Stay awake while charging (AC, USB and wireless)
- Keep screen on for a chosen time, charging or not

The app is for my own phones. It is not published on the Play Store. It needs Android 11 (API 30) or later.
The app is for my own phones and Meta Quest headset. It is not published on the Play Store. It needs Android 11 (API 30) or later.

## Build

Expand Down Expand Up @@ -82,6 +82,41 @@ A foreground service holds a screen wake lock. Its ongoing notification counts d
action. On Android 13 and later, the app asks for notification permission the first time; without it, the screen
still stays on but the notification is hidden. The power button still turns the screen off.

## Meta Quest

The app also runs on Meta Quest headsets. It was tested on a Quest 3 with Horizon OS on Android 14.

### Set up a Quest

1. Turn on developer mode. This needs a Meta developer account. In the Meta Horizon app on your phone, open the
headset's settings and turn on Developer mode. Restart the headset.
2. Connect the headset to the computer with a USB cable. Put the headset on and allow USB debugging for this
computer.
3. Install the app and grant the permission:

```bash
adb install -r app/build/outputs/apk/debug/app-debug.apk
adb shell pm grant com.rangelreale.devsettings android.permission.WRITE_SECURE_SETTINGS
```

4. In the headset, open the Library, filter it by Unknown sources, and open Dev Settings. It opens as a flat panel.

To use adb without the cable, run `adb tcpip 5555` while the cable is connected. Then disconnect it and run
`adb connect <headset IP>:5555`. You can also turn on wireless debugging from the app: it shows the address for
`adb connect`. That address only accepts a computer that is already paired.

### Differences on a Quest

The app recognises a Quest by the `oculus.hardware.standalone_vr` system feature and changes as follows:

- 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 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.

## Releases

CI runs on every pull request and every push to `master`: unit tests, lint and a debug build
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.rangelreale.devsettings.data

/**
* The kind of device the app runs on. Some features behave differently on a Meta Quest headset.
*
* 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.
*/
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,
) {
PHONE(usesDeveloperOptionsFlag = true, supportsKeepAwake = true),
META_QUEST(usesDeveloperOptionsFlag = false, supportsKeepAwake = false),
;

companion object {
/** System feature that every Meta Quest headset reports. */
const val QUEST_FEATURE = "oculus.hardware.standalone_vr"

/** [hasSystemFeature] is usually `PackageManager::hasSystemFeature`. */
fun detect(hasSystemFeature: (String) -> Boolean): DevicePlatform =
if (hasSystemFeature(QUEST_FEATURE)) META_QUEST else PHONE
}
}

/** True when the app should warn that Developer options are off. */
fun DevSettingsRepository.shouldWarnDeveloperOptionsOff(platform: DevicePlatform): Boolean =
platform.usesDeveloperOptionsFlag && !developerOptionsEnabled()
63 changes: 40 additions & 23 deletions app/src/main/java/com/rangelreale/devsettings/ui/MainScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.rangelreale.devsettings.R
import com.rangelreale.devsettings.SystemScreens
import com.rangelreale.devsettings.data.DevSetting
import com.rangelreale.devsettings.data.DevicePlatform
import com.rangelreale.devsettings.data.DevSettingsRepository
import com.rangelreale.devsettings.data.GlobalKeys
import com.rangelreale.devsettings.data.WirelessDebugInfo
import com.rangelreale.devsettings.data.WriteResult
import com.rangelreale.devsettings.data.grantCommand
import com.rangelreale.devsettings.data.shouldWarnDeveloperOptionsOff
import com.rangelreale.devsettings.keepon.KeepScreenOnDuration
import com.rangelreale.devsettings.keepon.KeepScreenOnService
import com.rangelreale.devsettings.keepon.KeepScreenOnState
Expand All @@ -83,13 +85,13 @@ import kotlinx.coroutines.launch
/** Values read from the system on each refresh. */
private data class Snapshot(
val hasPermission: Boolean,
val developerOptions: Boolean,
val warnDeveloperOptionsOff: Boolean,
val enabled: Map<DevSetting, Boolean>,
)

private fun DevSettingsRepository.snapshot() = Snapshot(
private fun DevSettingsRepository.snapshot(platform: DevicePlatform) = Snapshot(
hasPermission = hasPermission(),
developerOptions = developerOptionsEnabled(),
warnDeveloperOptionsOff = shouldWarnDeveloperOptionsOff(platform),
enabled = DevSetting.entries.associateWith { isEnabled(it) },
)

Expand All @@ -114,10 +116,11 @@ fun MainScreen(repository: DevSettingsRepository) {
val scope = rememberCoroutineScope()
val snackbar = remember { SnackbarHostState() }

var snapshot by remember { mutableStateOf(repository.snapshot()) }
val platform = remember { DevicePlatform.detect(context.packageManager::hasSystemFeature) }
var snapshot by remember { mutableStateOf(repository.snapshot(platform)) }
var resumeCount by remember { mutableIntStateOf(0) }
fun refresh() {
snapshot = repository.snapshot()
snapshot = repository.snapshot(platform)
}

// Re-read on every resume: values may have changed while the app was in the background.
Expand Down Expand Up @@ -190,9 +193,9 @@ fun MainScreen(repository: DevSettingsRepository) {
verticalArrangement = Arrangement.spacedBy(12.dp),
) {
if (!snapshot.hasPermission) {
SetupCard(command = grantCommand(context.packageName), onCopy = ::copy)
SetupCard(command = grantCommand(context.packageName), platform = platform, onCopy = ::copy)
}
if (!snapshot.developerOptions) {
if (snapshot.warnDeveloperOptionsOff) {
DeveloperOptionsCard(onOpenAboutPhone = { SystemScreens.openAboutPhone(context) })
}

Expand All @@ -216,20 +219,30 @@ fun MainScreen(repository: DevSettingsRepository) {
onCopy = ::copy,
onOpenPairing = { SystemScreens.openWirelessDebugging(context) },
)
HorizontalDivider()
val stayAwake = snapshot.enabled.getValue(DevSetting.STAY_AWAKE)
SettingRow(
setting = DevSetting.STAY_AWAKE,
checked = stayAwake,
enabled = snapshot.hasPermission,
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) {
HorizontalDivider()
val stayAwake = snapshot.enabled.getValue(DevSetting.STAY_AWAKE)
SettingRow(
setting = DevSetting.STAY_AWAKE,
checked = stayAwake,
enabled = snapshot.hasPermission,
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),
)
}

Expand Down Expand Up @@ -263,15 +276,19 @@ fun MainScreen(repository: DevSettingsRepository) {
}

@Composable
private fun SetupCard(command: String, onCopy: (String) -> Unit) {
private fun SetupCard(command: String, platform: DevicePlatform, onCopy: (String) -> Unit) {
Card(
modifier = Modifier.fillMaxWidth(),
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.errorContainer),
) {
Column(Modifier.padding(16.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) {
Text(stringResource(R.string.setup_title), style = MaterialTheme.typography.titleMedium)
Text(stringResource(R.string.setup_body))
Text(stringResource(R.string.setup_step_1))
Text(
stringResource(
if (platform == DevicePlatform.META_QUEST) R.string.setup_step_1_quest else R.string.setup_step_1,
),
)
Text(stringResource(R.string.setup_step_2))
Surface(
color = MaterialTheme.colorScheme.surface,
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<string name="setup_title">One-time setup</string>
<string name="setup_body">This app needs the WRITE_SECURE_SETTINGS permission. Only a computer can grant it.</string>
<string name="setup_step_1">1. Turn on USB debugging in Developer options, then connect the phone to a computer.</string>
<string name="setup_step_1_quest">1. Turn on developer mode for the headset in the Meta Horizon app, then connect the headset to a computer.</string>
<string name="setup_step_2">2. On the computer, run this command:</string>
<string name="setup_step_3">3. Come back here. The switches turn on within a few seconds.</string>
<string name="copy_command">Copy command</string>
Expand Down Expand Up @@ -55,5 +56,6 @@
<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_screen_on_detail">Holds the screen on, charging or not.</string>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.rangelreale.devsettings.data

import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test

class DevicePlatformTest {
private val store = FakeSettingsStore()
private val repo = DevSettingsRepository(store)

@Test
fun questFeatureMeansMetaQuest() {
val platform = DevicePlatform.detect { it == "oculus.hardware.standalone_vr" }
assertEquals(DevicePlatform.META_QUEST, platform)
}

@Test
fun otherFeaturesMeanPhone() {
assertEquals(DevicePlatform.PHONE, DevicePlatform.detect { false })
assertEquals(DevicePlatform.PHONE, DevicePlatform.detect { it == "android.hardware.telephony" })
}

@Test
fun onlyPhoneSupportsKeepAwake() {
assertTrue(DevicePlatform.PHONE.supportsKeepAwake)
assertFalse(DevicePlatform.META_QUEST.supportsKeepAwake)
}

@Test
fun phoneWarnsWhenDeveloperOptionsAreOff() {
assertTrue(repo.shouldWarnDeveloperOptionsOff(DevicePlatform.PHONE))
store.values["development_settings_enabled"] = 1
assertFalse(repo.shouldWarnDeveloperOptionsOff(DevicePlatform.PHONE))
}

@Test
fun questNeverWarnsAboutDeveloperOptions() {
assertFalse(repo.shouldWarnDeveloperOptionsOff(DevicePlatform.META_QUEST))
}
}
Loading