androidkotlinandroid-intentandroid-settings

Highlighting bypass_dnd setting row in Android


I'm trying to highlight the Override Do not Distrub channel setting upon opening the intent.

I have written the code according to this answer:

private const val EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key"
private const val EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args"
private const val EXTRA_BYPASS_DND = "bypass_dnd"

// ...

val intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS).apply {
    putExtra(Settings.EXTRA_APP_PACKAGE, packageName)
    putExtra(Settings.EXTRA_CHANNEL_ID, channelId)
    putExtra(EXTRA_FRAGMENT_ARG_KEY, EXTRA_BYPASS_DND)
    putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS,
        bundleOf(EXTRA_FRAGMENT_ARG_KEY to EXTRA_BYPASS_DND))

}
context.startActivity(intent)
Toast.makeText(context,
    "Grant the Override Do not Disturb permission", Toast.LENGTH_SHORT).show()

Unfortunately, the intent opens, but the setting is not highlighted. The bypass_dnd key is taken from the source code.


Solution

  • On the one hand, the Settings app is known to have a lot of undocumented Intent extras that can control its behavior, such as your three private const val ones at the top of your code listing.

    However, undocumented extras are unreliable. That is especially true with the Settings app, where manufacturers mess with it all the time. Even documented actions and extras are unreliable, so much so that the documentation often calls this out. So while those extras show up in the AOSP source code, there is no guarantee that any given device will honor them. That includes Google's Pixel series — whereas the Nexus series tended to be very close to AOSP, the Pixels deviate, though less than with other major manufacturers.

    So, you're welcome to include those extras. If they work, they work. From a testing standpoint, an Android SDK emulator is going to be closer to AOSP than is a Pixel, so you might try that. But, in the end, they are not going to work for everyone, so your UX and documentation needs to be able to cope with that.