Search code examples
timerwear-os

How can I access the remaining time on the google countdown timer from a watchface?


I'm buliding a watch face and want to display the remaining time on the timer.

I've been looking for ways to access (presumably) "com.google.android.deskclock" for the timer data, but have not found anything on the net.

Thank you for your help.


Solution

  • There's no official API for this, but because the system Clock app exposes this value via a complication, you can access it that way.

    Start by specifying the timer complication as a default:

    setDefaultComplicationProvider(myComplicationId, 
        new ComponentName("com.google.android.deskclock", 
                "com.google.android.deskclock.complications.TimerProviderService"), 
        ComplicationData.TYPE_SHORT_TEXT);
    

    If your watch face already supports complications, you could just feed this provider into that logic. Otherwise - if you want to do something else with the timer value - you'll need to extract it from the ComplicationData. So, in your WatchFaceService.Engine subclass:

    @Override
    public void onComplicationDataUpdate(int complicationId, ComplicationData complicationData) {
        super.onComplicationDataUpdate(watchFaceComplicationId, data);
    
        if (complicationId == myComplicationId) {
            // This is the timer complication
            CharSequence timerValue = complicationData.getShortText()
                    .getText(getBaseContext(), System.currentTimeMillis());
        }
    }
    

    This gives you the current timerValue to do whatever you'd like with.

    A couple of caveats to this approach:

    • Because this isn't one of the published system providers, there's always a chance that it won't work on some watch somewhere - or that an update may break it.
    • The user will need to have granted complication permission to your app. If your face is already showing other complications, this may be a non-issue, but otherwise it's your call if this is an acceptable UX.