• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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  *      http://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 package com.android.customization.picker.clock.data.repository
17 
18 import android.app.NotificationManager
19 import android.content.ComponentName
20 import android.content.Context
21 import android.view.LayoutInflater
22 import com.android.systemui.plugins.Plugin
23 import com.android.systemui.plugins.PluginManager
24 import com.android.systemui.shared.clocks.ClockRegistry
25 import com.android.systemui.shared.clocks.DefaultClockProvider
26 import com.android.systemui.shared.plugins.PluginActionManager
27 import com.android.systemui.shared.plugins.PluginEnabler
28 import com.android.systemui.shared.plugins.PluginInstance
29 import com.android.systemui.shared.plugins.PluginManagerImpl
30 import com.android.systemui.shared.plugins.PluginPrefs
31 import com.android.systemui.shared.system.UncaughtExceptionPreHandlerManager_Factory
32 import com.android.wallpaper.config.BaseFlags
33 import java.util.concurrent.Executors
34 import kotlinx.coroutines.CoroutineDispatcher
35 import kotlinx.coroutines.CoroutineScope
36 
37 /**
38  * Provide the [ClockRegistry] singleton. Note that we need to make sure that the [PluginManager]
39  * needs to be connected before [ClockRegistry] is ready to use.
40  */
41 class ClockRegistryProvider(
42     private val context: Context,
43     private val coroutineScope: CoroutineScope,
44     private val mainDispatcher: CoroutineDispatcher,
45     private val backgroundDispatcher: CoroutineDispatcher,
46 ) {
<lambda>null47     private val clockRegistry: ClockRegistry by lazy {
48         val flags = BaseFlags.get()
49         ClockRegistry(
50             context,
51             createPluginManager(context),
52             coroutineScope,
53             mainDispatcher,
54             backgroundDispatcher,
55             isEnabled = flags.isCustomClocksEnabled(context),
56             handleAllUsers = false,
57             DefaultClockProvider(
58                 ctx = context,
59                 layoutInflater = LayoutInflater.from(context),
60                 resources = context.resources,
61                 isClockReactiveVariantsEnabled = flags.isClockReactiveVariantsEnabled(),
62                 vibrator = null,
63             ),
64             keepAllLoaded = true,
65             subTag = "Picker",
66         )
67     }
68 
69     init {
70         // Listeners in ClockRegistry get cleaned up when app ended
71         clockRegistry.registerListeners()
72     }
73 
getnull74     fun get() = clockRegistry
75 
76     private fun createPluginManager(context: Context): PluginManager {
77         val privilegedPlugins = listOf<String>()
78         val isDebugDevice = true
79 
80         val instanceFactory =
81             PluginInstance.Factory(
82                 this::class.java.classLoader,
83                 PluginInstance.InstanceFactory<Plugin>(),
84                 PluginInstance.VersionCheckerImpl(),
85                 privilegedPlugins,
86                 isDebugDevice,
87             )
88 
89         /*
90          * let SystemUI handle plugin, in this class assume plugins are enabled
91          */
92         val pluginEnabler =
93             object : PluginEnabler {
94                 override fun setEnabled(component: ComponentName) = Unit
95 
96                 override fun setDisabled(
97                     component: ComponentName,
98                     @PluginEnabler.DisableReason reason: Int,
99                 ) = Unit
100 
101                 override fun isEnabled(component: ComponentName): Boolean {
102                     return true
103                 }
104 
105                 @PluginEnabler.DisableReason
106                 override fun getDisableReason(componentName: ComponentName): Int {
107                     return PluginEnabler.ENABLED
108                 }
109             }
110 
111         val pluginActionManager =
112             PluginActionManager.Factory(
113                 context,
114                 context.packageManager,
115                 context.mainExecutor,
116                 Executors.newSingleThreadExecutor(),
117                 context.getSystemService(NotificationManager::class.java),
118                 pluginEnabler,
119                 privilegedPlugins,
120                 instanceFactory,
121             )
122         return PluginManagerImpl(
123             context,
124             pluginActionManager,
125             isDebugDevice,
126             UncaughtExceptionPreHandlerManager_Factory.create().get(),
127             pluginEnabler,
128             PluginPrefs(context),
129             listOf(),
130         )
131     }
132 }
133