summaryrefslogtreecommitdiff
path: root/src/com/android/customization/picker/clock/ui/view/ClockViewFactory.kt
blob: 67c70027456b5950066e1817f8288d3ab2449ab5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * Copyright (C) 2023 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 com.android.customization.picker.clock.ui.view

import android.app.Activity
import android.util.TypedValue
import android.view.View
import androidx.annotation.ColorInt
import androidx.lifecycle.LifecycleOwner
import com.android.systemui.plugins.ClockController
import com.android.systemui.shared.clocks.ClockRegistry
import com.android.wallpaper.R
import com.android.wallpaper.util.ScreenSizeCalculator
import com.android.wallpaper.util.TimeUtils.TimeTicker
import java.util.concurrent.ConcurrentHashMap

class ClockViewFactory(
    private val activity: Activity,
    private val registry: ClockRegistry,
) {
    private val timeTickListeners: ConcurrentHashMap<Int, TimeTicker> = ConcurrentHashMap()
    private val clockControllers: HashMap<String, ClockController> = HashMap()
    private var ticker: TimeTicker? = null
    fun getRatio(): Float {
        val screenSizeCalculator = ScreenSizeCalculator.getInstance()
        val screenSize = screenSizeCalculator.getScreenSize(activity.windowManager.defaultDisplay)
        return activity.resources.getDimensionPixelSize(R.dimen.screen_preview_height).toFloat() /
            screenSize.y.toFloat()
    }

    fun getController(clockId: String): ClockController {
        return clockControllers[clockId] ?: initClockController(clockId)
    }

    fun getView(clockId: String): View {
        return getController(clockId).largeClock.view
    }

    fun updateColorForAllClocks(@ColorInt seedColor: Int?) {
        clockControllers.values.forEach { it.events.onSeedColorChanged(seedColor = seedColor) }
    }

    fun updateColor(clockId: String, @ColorInt seedColor: Int?) {
        return (clockControllers[clockId] ?: initClockController(clockId))
            .events
            .onSeedColorChanged(seedColor)
    }

    fun registerTimeTicker(owner: LifecycleOwner) {
        val hashCode = owner.hashCode()
        if (timeTickListeners.keys.contains(hashCode)) {
            return
        }
        timeTickListeners[hashCode] =
            TimeTicker.registerNewReceiver(activity.applicationContext) {
                clockControllers.values.forEach { it.largeClock.events.onTimeTick() }
            }
    }

    fun unregisterTimeTicker(owner: LifecycleOwner) {
        val hashCode = owner.hashCode()
        timeTickListeners[hashCode]?.let {
            activity.applicationContext.unregisterReceiver(it)
            timeTickListeners.remove(hashCode)
        }
    }

    private fun initClockController(clockId: String): ClockController {
        val controller =
            registry.createExampleClock(clockId).also { it?.initialize(activity.resources, 0f, 0f) }
        checkNotNull(controller)

        // Configure light/dark theme
        val isLightTheme = TypedValue()
        activity.theme.resolveAttribute(android.R.attr.isLightTheme, isLightTheme, true)
        val isRegionDark = isLightTheme.data == 0
        controller.largeClock.events.onRegionDarknessChanged(isRegionDark)

        // Configure font size
        controller.largeClock.events.onFontSettingChanged(
            activity.resources.getDimensionPixelSize(R.dimen.large_clock_text_size).toFloat() *
                getRatio()
        )
        clockControllers[clockId] = controller
        return controller
    }
}