rjr-apps
rjr-apps

Reputation: 318

Detecting ambient mode in Wear OS 5

I have a ComplicationDataSourceService that needs to be notified when the watch goes into ambient mode or the screen turns off. The idea is that this is for a sort of "flashlight" complication, where a completely white image is rendered when turned on (via tap), and nothing is rendered when turned off. If the flashlight is turned on when the screen turns off or goes into ambient mode, I want the flashlight to turn off, and stay off after the watch wakes up again.

Prior to Wear OS 5, I was using DisplayListener.onDisplayChanged to accomplish this. However, Wear OS 5 doesn't seem to be calling this method at all, regardless of whether my targetSdkVersion is 33 or 34.

Is there another way I can get a callback for ambient mode / the screen turning off, or a different way to implement DisplayListener.onDisplayChanged?

displayListener = object : DisplayManager.DisplayListener {
    override fun onDisplayChanged(displayId: Int) {
        val isStateAmbient = displayManager.getDisplay(displayId).state != Display.STATE_ON
        // ...
    }
}.also {
    displayManager.registerDisplayListener(it, Handler(Looper.getMainLooper()))
}

Upvotes: 0

Views: 96

Answers (1)

ianhanniballake
ianhanniballake

Reputation: 199825

Your requirement was:

If the flashlight is turned on when the screen turns off or goes into ambient mode, I want the flashlight to turn off.

This is precisely the use case for burn in safe images:

On screens susceptible to burn-in, solid blocks of color should be avoided in ambient mode. If your icons or images include solid blocks of color, also provide a burn-in safe version.

Since you are making a solid white color for your flashlight being 'on', you'll want to include a burn-in safe image (e.g., your empty icon) which will automatically be used when in ambient mode. This way you don't need to listen for when you enter/exit ambient mode at all, but the system takes care of this for you. This would allow you to remove your DisplayListener on every API level and remove your need to use it in Wear 5.

When you provide an icon using ComplicationData.Builder#setIcon, include a burn-in safe version using ComplicationData.Builder#setBurnInProtectionIcon.

When you provide an image using ComplicationData.Builder#setSmallImage, include a burn-in safe version using ComplicationData.Builder#setBurnInProtectionSmallImage.

Upvotes: 2

Related Questions