DefaultTimeSource.kt
/*
* Copyright 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.wear.compose.material
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.text.format.DateFormat
import androidx.annotation.VisibleForTesting
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.State
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import androidx.compose.ui.platform.LocalContext
import java.util.Calendar
internal actual class DefaultTimeSource actual constructor(timeFormat: String) : TimeSource {
private val _timeFormat = timeFormat
override val currentTime: String
@Composable
get() = currentTime({ currentTimeMillis() }, _timeFormat).value
}
@Composable
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
internal fun currentTime(
time: () -> Long,
timeFormat: String
): State<String> {
var calendar by remember { mutableStateOf(Calendar.getInstance()) }
var currentTime by remember { mutableStateOf(time()) }
val timeText = remember {
derivedStateOf { formatTime(calendar, currentTime, timeFormat) }
}
val context = LocalContext.current
val updatedTimeLambda by rememberUpdatedState(time)
DisposableEffect(context, updatedTimeLambda) {
val receiver = TimeBroadcastReceiver(
onTimeChanged = { currentTime = updatedTimeLambda() },
onTimeZoneChanged = { calendar = Calendar.getInstance() }
)
receiver.register(context)
onDispose {
receiver.unregister(context)
}
}
return timeText
}
/**
* A [BroadcastReceiver] to receive time tick, time change, and time zone change events.
*/
private class TimeBroadcastReceiver(
val onTimeChanged: () -> Unit,
val onTimeZoneChanged: () -> Unit
) : BroadcastReceiver() {
private var registered = false
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == Intent.ACTION_TIMEZONE_CHANGED) {
onTimeZoneChanged()
} else {
onTimeChanged()
}
}
fun register(context: Context) {
if (!registered) {
val filter = IntentFilter()
filter.addAction(Intent.ACTION_TIME_TICK)
filter.addAction(Intent.ACTION_TIME_CHANGED)
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED)
context.registerReceiver(this, filter)
registered = true
}
}
fun unregister(context: Context) {
if (registered) {
context.unregisterReceiver(this)
registered = false
}
}
}
private fun formatTime(
calendar: Calendar,
currentTime: Long,
timeFormat: String
): String {
calendar.timeInMillis = currentTime
return DateFormat.format(timeFormat, calendar).toString()
}