|
| 1 | +/* |
| 2 | + * Copyright 2025 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.wear.snippets.complication |
| 18 | + |
| 19 | +import androidx.wear.watchface.complications.data.ComplicationData |
| 20 | +import androidx.wear.watchface.complications.data.ComplicationType |
| 21 | +import androidx.wear.watchface.complications.data.NoDataComplicationData |
| 22 | +import androidx.wear.watchface.complications.data.PlainComplicationText |
| 23 | +import androidx.wear.watchface.complications.data.ShortTextComplicationData |
| 24 | +import androidx.wear.watchface.complications.datasource.ComplicationDataTimeline |
| 25 | +import androidx.wear.watchface.complications.datasource.ComplicationRequest |
| 26 | +import androidx.wear.watchface.complications.datasource.SuspendingTimelineComplicationDataSourceService |
| 27 | +import androidx.wear.watchface.complications.datasource.TimeInterval |
| 28 | +import androidx.wear.watchface.complications.datasource.TimelineEntry |
| 29 | +import java.time.Instant |
| 30 | +import java.time.temporal.ChronoUnit |
| 31 | + |
| 32 | +data class CalendarEntry( |
| 33 | + val start: Instant, |
| 34 | + val end: Instant, |
| 35 | + val name: String |
| 36 | +) |
| 37 | + |
| 38 | +// [START android_wear_timeline_complication] |
| 39 | +class MyTimelineComplicationDataSourceService : SuspendingTimelineComplicationDataSourceService() { |
| 40 | + override suspend fun onComplicationRequest(request: ComplicationRequest): ComplicationDataTimeline? { |
| 41 | + if (request.complicationType != ComplicationType.SHORT_TEXT) { |
| 42 | + return ComplicationDataTimeline( |
| 43 | + defaultComplicationData = NoDataComplicationData(), |
| 44 | + timelineEntries = emptyList() |
| 45 | + ) |
| 46 | + } |
| 47 | + // Retrieve list of events from your own datasource / database. |
| 48 | + val events = getCalendarEvents() |
| 49 | + return ComplicationDataTimeline( |
| 50 | + defaultComplicationData = shortTextComplicationData("No event"), |
| 51 | + timelineEntries = events.map { |
| 52 | + TimelineEntry( |
| 53 | + validity = TimeInterval(it.start, it.end), |
| 54 | + complicationData = shortTextComplicationData(it.name) |
| 55 | + ) |
| 56 | + } |
| 57 | + ) |
| 58 | + } |
| 59 | + |
| 60 | + override fun getPreviewData(type: ComplicationType): ComplicationData? { |
| 61 | + return shortTextComplicationData("Event 1") |
| 62 | + } |
| 63 | + |
| 64 | + private fun shortTextComplicationData(text: String) = |
| 65 | + ShortTextComplicationData.Builder( |
| 66 | + text = PlainComplicationText.Builder(text).build(), |
| 67 | + contentDescription = PlainComplicationText.Builder(text).build() |
| 68 | + ) |
| 69 | + // Add further optional details here such as icon, tap action, title etc |
| 70 | + .build() |
| 71 | + |
| 72 | + // [START_EXCLUDE] |
| 73 | + private fun getCalendarEvents(): List<CalendarEntry> { |
| 74 | + val now = Instant.now() |
| 75 | + return listOf( |
| 76 | + CalendarEntry(now, now.plus(1, ChronoUnit.HOURS), "Event 1"), |
| 77 | + CalendarEntry(now.plus(2, ChronoUnit.HOURS), now.plus(3, ChronoUnit.HOURS), "Event 2"), |
| 78 | + CalendarEntry(now.plus(4, ChronoUnit.HOURS), now.plus(5, ChronoUnit.HOURS), "Event 3"), |
| 79 | + ) |
| 80 | + } |
| 81 | + // [END_EXCLUDE] |
| 82 | +} |
| 83 | +// [END android_wear_timeline_complication] |
0 commit comments