1 /*
2  * Copyright (C) 2024 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 
17 package com.android.systemui.car.systembar.element;
18 
19 import android.text.TextUtils;
20 import android.util.Log;
21 import android.view.View;
22 import android.view.ViewGroup;
23 
24 import com.android.systemui.dagger.SysUISingleton;
25 
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Map;
29 
30 import javax.inject.Inject;
31 import javax.inject.Provider;
32 
33 /** Helper class for retrieving and initializing CarSystemBarElements and their controllers. */
34 @SysUISingleton
35 public class CarSystemBarElementInitializer {
36     private static final String TAG = CarSystemBarElementInitializer.class.getSimpleName();
37 
38     private final Map<Class<?>, Provider<CarSystemBarElementController.Factory>>
39             mElementControllerFactories;
40 
41     /** Convert a class string to a class instance of CarSystemBarElementController */
getElementControllerClassFromString(String str)42     public static Class<?> getElementControllerClassFromString(String str) {
43         if (!TextUtils.isEmpty(str)) {
44             try {
45                 Class<?> clazz = Class.forName(str);
46                 if (clazz != null && CarSystemBarElementController.class.isAssignableFrom(clazz)) {
47                     return clazz;
48                 }
49             } catch (ClassNotFoundException e) {
50                 Log.w(TAG, "cannot find class for string " + str, e);
51             }
52         }
53         return null;
54     }
55 
56     @Inject
CarSystemBarElementInitializer( Map<Class<?>, Provider<CarSystemBarElementController.Factory>> factories)57     public CarSystemBarElementInitializer(
58             Map<Class<?>, Provider<CarSystemBarElementController.Factory>> factories) {
59         mElementControllerFactories = factories;
60     }
61 
62     /** Instantiate all CarSystemBarElements found within the provided rootView */
initializeCarSystemBarElements( ViewGroup rootView)63     public List<CarSystemBarElementController> initializeCarSystemBarElements(
64             ViewGroup rootView) {
65         List<ElementViewControllerData> elementData = findSystemBarElements(rootView);
66         List<CarSystemBarElementController> controllers = new ArrayList<>();
67         for (ElementViewControllerData element : elementData) {
68             if (element.getControllerClass() != null) {
69                 Provider<CarSystemBarElementController.Factory> factoryProvider =
70                         mElementControllerFactories.get(element.getControllerClass());
71                 if (factoryProvider == null) {
72                     Log.d(TAG, "cannot find factory provider for class "
73                             + element.getControllerClass());
74                     continue;
75                 }
76                 CarSystemBarElementController.Factory factory = factoryProvider.get();
77                 if (factory == null) {
78                     Log.d(TAG, "cannot find factory for class " + element.getControllerClass());
79                     continue;
80                 }
81                 CarSystemBarElementController controller = factory.create(element.getView());
82                 controller.init();
83                 controllers.add(controller);
84             }
85         }
86         return controllers;
87     }
88 
89     // Returns information as a pair of (View, ElementControllerClass)
findSystemBarElements(ViewGroup rootView)90     private static List<ElementViewControllerData> findSystemBarElements(ViewGroup rootView) {
91         List<ElementViewControllerData> info = new ArrayList<>();
92         for (int i = 0; i < rootView.getChildCount(); i++) {
93             View v = rootView.getChildAt(i);
94             if (v instanceof CarSystemBarElement) {
95                 info.add(new ElementViewControllerData(v,
96                         ((CarSystemBarElement) v).getElementControllerClass()));
97             }
98             if (v instanceof ViewGroup) {
99                 info.addAll(findSystemBarElements((ViewGroup) v));
100             }
101         }
102         return info;
103     }
104 
105     private static final class ElementViewControllerData {
106         private final View mView;
107         private final Class<?> mControllerClass;
108 
ElementViewControllerData(View view, Class<?> clazz)109         ElementViewControllerData(View view, Class<?> clazz) {
110             mView = view;
111             mControllerClass = clazz;
112         }
113 
getView()114         View getView() {
115             return mView;
116         }
117 
getControllerClass()118         Class<?> getControllerClass() {
119             return mControllerClass;
120         }
121     }
122 }
123