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.res.R; 28 import com.android.systemui.util.InitializationChecker; 29 import com.android.wm.shell.dagger.WMComponent; 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 .setAppZoomOut(mWMComponent.getAppZoomOut()); 105 106 // Only initialize when not starting from tests since this currently initializes some 107 // components that shouldn't be run in the test environment 108 mWMComponent.init(); 109 } else { 110 // TODO: Call on prepareSysUIComponentBuilder but not with real components. Other option 111 // is separating this logic into newly creating SystemUITestsFactory. 112 builder = prepareSysUIComponentBuilder(builder, mWMComponent) 113 .setShell(new ShellInterface() {}) 114 .setPip(Optional.ofNullable(null)) 115 .setSplitScreen(Optional.ofNullable(null)) 116 .setOneHanded(Optional.ofNullable(null)) 117 .setBubbles(Optional.ofNullable(null)) 118 .setTaskViewFactory(Optional.ofNullable(null)) 119 .setShellTransitions(new ShellTransitions() {}) 120 .setKeyguardTransitions(new KeyguardTransitions() {}) 121 .setDisplayAreaHelper(Optional.ofNullable(null)) 122 .setStartingSurface(Optional.ofNullable(null)) 123 .setRecentTasks(Optional.ofNullable(null)) 124 .setBackAnimation(Optional.ofNullable(null)) 125 .setDesktopMode(Optional.ofNullable(null)) 126 .setAppZoomOut(Optional.ofNullable(null)); 127 } 128 mSysUIComponent = builder.build(); 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