• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.systemui
17 
18 import android.annotation.SuppressLint
19 import android.content.Context
20 import android.util.Log
21 import com.android.internal.annotations.VisibleForTesting
22 import com.android.systemui.util.Assert
23 
24 /**
25  * Factory to reflectively lookup a [SystemUIInitializer] to start SystemUI with.
26  */
27 @Deprecated("Provide your own {@link SystemUIAppComponentFactoryBase} that doesn't need this.")
28 object SystemUIInitializerFactory {
29     private const val TAG = "SysUIInitializerFactory"
30     @SuppressLint("StaticFieldLeak")
31     private var initializer: SystemUIInitializer? = null
32 
33     /**
34      * Instantiate a [SystemUIInitializer] reflectively.
35      */
36     @JvmStatic
createWithContextnull37     fun createWithContext(context: Context): SystemUIInitializer {
38         return createFromConfig(context)
39     }
40 
41     /**
42      * Instantiate a [SystemUIInitializer] reflectively.
43      */
44     @JvmStatic
createFromConfignull45     private fun createFromConfig(context: Context): SystemUIInitializer {
46         Assert.isMainThread()
47 
48         return createFromConfigNoAssert(context)
49     }
50 
51     @JvmStatic
52     @VisibleForTesting
createFromConfigNoAssertnull53     fun createFromConfigNoAssert(context: Context): SystemUIInitializer {
54 
55         return initializer ?: run {
56             val className = context.getString(R.string.config_systemUIFactoryComponent)
57             if (className.isEmpty()) {
58                 throw RuntimeException("No SystemUIFactory component configured")
59             }
60             try {
61                 val cls = context.classLoader.loadClass(className)
62                 val constructor = cls.getConstructor(Context::class.java)
63                 (constructor.newInstance(context) as SystemUIInitializer).apply {
64                     initializer = this
65                 }
66             } catch (t: Throwable) {
67                 Log.w(TAG, "Error creating SystemUIInitializer component: $className", t)
68                 throw t
69             }
70         }
71     }
72 }
73