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