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 
17 package com.android.systemui;
18 
19 import android.content.Context;
20 import android.content.res.Resources;
21 import android.os.Handler;
22 import android.os.HandlerThread;
23 import android.util.Log;
24 
25 import com.android.systemui.dagger.GlobalRootComponent;
26 import com.android.systemui.dagger.SysUIComponent;
27 import com.android.systemui.dagger.WMComponent;
28 import com.android.systemui.util.InitializationChecker;
29 import com.android.wm.shell.dagger.WMShellConcurrencyModule;
30 import com.android.wm.shell.keyguard.KeyguardTransitions;
31 import com.android.wm.shell.sysui.ShellInterface;
32 import com.android.wm.shell.transition.ShellTransitions;
33 
34 import java.util.Optional;
35 import java.util.concurrent.ExecutionException;
36 
37 /**
38  * Initializer that stands up SystemUI.
39  *
40  * Implementations should override {@link #getGlobalRootComponentBuilder()} to fill in their own
41  * Dagger root component.
42  */
43 public abstract class SystemUIInitializer {
44     private static final String TAG = "SystemUIFactory";
45 
46     private final Context mContext;
47 
48     private GlobalRootComponent mRootComponent;
49     private WMComponent mWMComponent;
50     private SysUIComponent mSysUIComponent;
51     private InitializationChecker mInitializationChecker;
52 
SystemUIInitializer(Context context)53     public SystemUIInitializer(Context context) {
54         mContext = context;
55     }
56 
getGlobalRootComponentBuilder()57     protected abstract GlobalRootComponent.Builder getGlobalRootComponentBuilder();
58 
59     /**
60      * Prepares the SysUIComponent builder before it is built.
61      * @param sysUIBuilder the builder provided by the root component's getSysUIComponent() method
62      * @param wm the built WMComponent from the root component's getWMComponent() method
63      */
prepareSysUIComponentBuilder( SysUIComponent.Builder sysUIBuilder, WMComponent wm)64     protected SysUIComponent.Builder prepareSysUIComponentBuilder(
65             SysUIComponent.Builder sysUIBuilder, WMComponent wm) {
66         return sysUIBuilder;
67     }
68 
69     /**
70      * Starts the initialization process. This stands up the Dagger graph.
71      */
init(boolean fromTest)72     public void init(boolean fromTest) throws ExecutionException, InterruptedException {
73         mRootComponent = getGlobalRootComponentBuilder()
74                 .context(mContext)
75                 .instrumentationTest(fromTest)
76                 .build();
77 
78         mInitializationChecker = mRootComponent.getInitializationChecker();
79         boolean initializeComponents = mInitializationChecker.initializeComponents();
80 
81         // Stand up WMComponent
82         setupWmComponent(mContext);
83 
84         // And finally, retrieve whatever SysUI needs from WMShell and build SysUI.
85         SysUIComponent.Builder builder = mRootComponent.getSysUIComponent();
86         if (initializeComponents) {
87             // Only initialize when not starting from tests since this currently initializes some
88             // components that shouldn't be run in the test environment
89             builder = prepareSysUIComponentBuilder(builder, mWMComponent)
90                     .setShell(mWMComponent.getShell())
91                     .setPip(mWMComponent.getPip())
92                     .setSplitScreen(mWMComponent.getSplitScreen())
93                     .setOneHanded(mWMComponent.getOneHanded())
94                     .setBubbles(mWMComponent.getBubbles())
95                     .setTaskViewFactory(mWMComponent.getTaskViewFactory())
96                     .setTransitions(mWMComponent.getTransitions())
97                     .setKeyguardTransitions(mWMComponent.getKeyguardTransitions())
98                     .setStartingSurface(mWMComponent.getStartingSurface())
99                     .setDisplayAreaHelper(mWMComponent.getDisplayAreaHelper())
100                     .setRecentTasks(mWMComponent.getRecentTasks())
101                     .setBackAnimation(mWMComponent.getBackAnimation())
102                     .setDesktopMode(mWMComponent.getDesktopMode());
103 
104             // Only initialize when not starting from tests since this currently initializes some
105             // components that shouldn't be run in the test environment
106             mWMComponent.init();
107         } else {
108             // TODO: Call on prepareSysUIComponentBuilder but not with real components. Other option
109             // is separating this logic into newly creating SystemUITestsFactory.
110             builder = prepareSysUIComponentBuilder(builder, mWMComponent)
111                     .setShell(new ShellInterface() {})
112                     .setPip(Optional.ofNullable(null))
113                     .setSplitScreen(Optional.ofNullable(null))
114                     .setOneHanded(Optional.ofNullable(null))
115                     .setBubbles(Optional.ofNullable(null))
116                     .setTaskViewFactory(Optional.ofNullable(null))
117                     .setTransitions(new ShellTransitions() {})
118                     .setKeyguardTransitions(new KeyguardTransitions() {})
119                     .setDisplayAreaHelper(Optional.ofNullable(null))
120                     .setStartingSurface(Optional.ofNullable(null))
121                     .setRecentTasks(Optional.ofNullable(null))
122                     .setBackAnimation(Optional.ofNullable(null))
123                     .setDesktopMode(Optional.ofNullable(null));
124         }
125         mSysUIComponent = builder.build();
126         if (initializeComponents) {
127             mSysUIComponent.init();
128         }
129 
130         // Every other part of our codebase currently relies on Dependency, so we
131         // really need to ensure the Dependency gets initialized early on.
132         Dependency dependency = mSysUIComponent.createDependency();
133         dependency.start();
134     }
135 
136     /**
137      * Sets up {@link #mWMComponent}. On devices where the Shell runs on its own main thread,
138      * this will pre-create the thread to ensure that the components are constructed on the
139      * same thread, to reduce the likelihood of side effects from running the constructors on
140      * a different thread than the rest of the class logic.
141      */
setupWmComponent(Context context)142     private void setupWmComponent(Context context) {
143         WMComponent.Builder wmBuilder = mRootComponent.getWMComponentBuilder();
144         if (!mInitializationChecker.initializeComponents()
145                 || !WMShellConcurrencyModule.enableShellMainThread(context)) {
146             // If running under tests or shell thread is not enabled, we don't need anything special
147             mWMComponent = wmBuilder.build();
148             return;
149         }
150 
151         // If the shell main thread is enabled, initialize the component on that thread
152         HandlerThread shellThread = WMShellConcurrencyModule.createShellMainThread();
153         shellThread.start();
154 
155         // Use an async handler since we don't care about synchronization
156         Handler shellHandler = Handler.createAsync(shellThread.getLooper());
157         boolean built = shellHandler.runWithScissors(() -> {
158             wmBuilder.setShellMainThread(shellThread);
159             mWMComponent = wmBuilder.build();
160         }, 5000);
161         if (!built) {
162             Log.w(TAG, "Failed to initialize WMComponent");
163             throw new RuntimeException();
164         }
165     }
166 
getRootComponent()167     public GlobalRootComponent getRootComponent() {
168         return mRootComponent;
169     }
170 
getWMComponent()171     public WMComponent getWMComponent() {
172         return mWMComponent;
173     }
174 
getSysUIComponent()175     public SysUIComponent getSysUIComponent() {
176         return mSysUIComponent;
177     }
178 
179     /**
180      * Returns the list of additional system UI components that should be started.
181      */
getVendorComponent(Resources resources)182     public String getVendorComponent(Resources resources) {
183         return resources.getString(R.string.config_systemUIVendorServiceComponent);
184     }
185 }
186