From 73e4e0830257fcf7ad937bbee714195e43620a25 Mon Sep 17 00:00:00 2001 From: Dave Mankoff Date: Mon, 13 Jun 2022 21:51:52 +0000 Subject: [PATCH] Remove SystemUIFactory#shouldInitializeComponents Replaced it with an injectable InitializationChecker class. This is primarily to remove SystemUIGoogle's reliance on overriding SystemUIFactory#init. Bug: 235624311 Test: m SystemUI Change-Id: I56537764f0f6ea3519211a9403da2bef7c49bc7e Merged-In: I56537764f0f6ea3519211a9403da2bef7c49bc7e --- .../com/android/systemui/SystemUIFactory.java | 35 ++++++++--------- .../systemui/dagger/GlobalRootComponent.java | 11 +++++- .../qualifiers/InstrumentationTest.java | 34 +++++++++++++++++ .../systemui/tv/TvSystemUIFactory.java | 8 +--- .../systemui/util/InitializationChecker.kt | 38 +++++++++++++++++++ 5 files changed, 100 insertions(+), 26 deletions(-) create mode 100644 packages/SystemUI/src/com/android/systemui/dagger/qualifiers/InstrumentationTest.java create mode 100644 packages/SystemUI/src/com/android/systemui/util/InitializationChecker.kt diff --git a/packages/SystemUI/src/com/android/systemui/SystemUIFactory.java b/packages/SystemUI/src/com/android/systemui/SystemUIFactory.java index 78112e24e0eb7..ca94b8cd05b74 100644 --- a/packages/SystemUI/src/com/android/systemui/SystemUIFactory.java +++ b/packages/SystemUI/src/com/android/systemui/SystemUIFactory.java @@ -16,7 +16,6 @@ package com.android.systemui; -import android.app.ActivityThread; import android.content.Context; import android.content.res.Resources; import android.os.Handler; @@ -28,6 +27,7 @@ import com.android.systemui.dagger.DaggerGlobalRootComponent; import com.android.systemui.dagger.GlobalRootComponent; import com.android.systemui.dagger.SysUIComponent; import com.android.systemui.dagger.WMComponent; +import com.android.systemui.util.InitializationChecker; import com.android.wm.shell.dagger.WMShellConcurrencyModule; import com.android.wm.shell.transition.ShellTransitions; @@ -47,7 +47,7 @@ public class SystemUIFactory { private GlobalRootComponent mRootComponent; private WMComponent mWMComponent; private SysUIComponent mSysUIComponent; - private boolean mInitializeComponents; + private InitializationChecker mInitializationChecker; public static T getInstance() { return (T) mFactory; @@ -89,15 +89,17 @@ public class SystemUIFactory { @VisibleForTesting public void init(Context context, boolean fromTest) throws ExecutionException, InterruptedException { - // Only initialize components for the main system ui process running as the primary user - mInitializeComponents = !fromTest - && android.os.Process.myUserHandle().isSystem() - && ActivityThread.currentProcessName().equals(ActivityThread.currentPackageName()); - mRootComponent = buildGlobalRootComponent(context); + mRootComponent = getGlobalRootComponentBuilder() + .context(context) + .instrumentationTest(fromTest) + .build(); + + mInitializationChecker = mRootComponent.getInitializationChecker(); + boolean initializeComponents = mInitializationChecker.initializeComponents(); // Stand up WMComponent setupWmComponent(context); - if (mInitializeComponents) { + if (initializeComponents) { // Only initialize when not starting from tests since this currently initializes some // components that shouldn't be run in the test environment mWMComponent.init(); @@ -105,7 +107,7 @@ public class SystemUIFactory { // And finally, retrieve whatever SysUI needs from WMShell and build SysUI. SysUIComponent.Builder builder = mRootComponent.getSysUIComponent(); - if (mInitializeComponents) { + if (initializeComponents) { // Only initialize when not starting from tests since this currently initializes some // components that shouldn't be run in the test environment builder = prepareSysUIComponentBuilder(builder, mWMComponent) @@ -145,7 +147,7 @@ public class SystemUIFactory { .setBackAnimation(Optional.ofNullable(null)); } mSysUIComponent = builder.build(); - if (mInitializeComponents) { + if (initializeComponents) { mSysUIComponent.init(); } @@ -163,7 +165,8 @@ public class SystemUIFactory { */ private void setupWmComponent(Context context) { WMComponent.Builder wmBuilder = mRootComponent.getWMComponentBuilder(); - if (!mInitializeComponents || !WMShellConcurrencyModule.enableShellMainThread(context)) { + if (!mInitializationChecker.initializeComponents() + || !WMShellConcurrencyModule.enableShellMainThread(context)) { // If running under tests or shell thread is not enabled, we don't need anything special mWMComponent = wmBuilder.build(); return; @@ -195,14 +198,8 @@ public class SystemUIFactory { return sysUIBuilder; } - protected GlobalRootComponent buildGlobalRootComponent(Context context) { - return DaggerGlobalRootComponent.builder() - .context(context) - .build(); - } - - protected boolean shouldInitializeComponents() { - return mInitializeComponents; + protected GlobalRootComponent.Builder getGlobalRootComponentBuilder() { + return DaggerGlobalRootComponent.builder(); } public GlobalRootComponent getRootComponent() { diff --git a/packages/SystemUI/src/com/android/systemui/dagger/GlobalRootComponent.java b/packages/SystemUI/src/com/android/systemui/dagger/GlobalRootComponent.java index 4f55ba496b6ba..9e33ee1faab3b 100644 --- a/packages/SystemUI/src/com/android/systemui/dagger/GlobalRootComponent.java +++ b/packages/SystemUI/src/com/android/systemui/dagger/GlobalRootComponent.java @@ -18,6 +18,9 @@ package com.android.systemui.dagger; import android.content.Context; +import com.android.systemui.dagger.qualifiers.InstrumentationTest; +import com.android.systemui.util.InitializationChecker; + import javax.inject.Singleton; import dagger.BindsInstance; @@ -37,7 +40,8 @@ public interface GlobalRootComponent { interface Builder { @BindsInstance Builder context(Context context); - + @BindsInstance + Builder instrumentationTest(@InstrumentationTest boolean test); GlobalRootComponent build(); } @@ -50,4 +54,9 @@ public interface GlobalRootComponent { * Builder for a {@link SysUIComponent}, which makes it a subcomponent of this class. */ SysUIComponent.Builder getSysUIComponent(); + + /** + * Returns an {@link InitializationChecker}. + */ + InitializationChecker getInitializationChecker(); } diff --git a/packages/SystemUI/src/com/android/systemui/dagger/qualifiers/InstrumentationTest.java b/packages/SystemUI/src/com/android/systemui/dagger/qualifiers/InstrumentationTest.java new file mode 100644 index 0000000000000..a803a39f114f0 --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/dagger/qualifiers/InstrumentationTest.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.systemui.dagger.qualifiers; + +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; + +import javax.inject.Qualifier; + + +/** + * An annotation for injecting whether or not we are running in a test environment. + */ +@Qualifier +@Documented +@Retention(RUNTIME) +public @interface InstrumentationTest { +} diff --git a/packages/SystemUI/src/com/android/systemui/tv/TvSystemUIFactory.java b/packages/SystemUI/src/com/android/systemui/tv/TvSystemUIFactory.java index c99ad23ab23d5..0b5594b14d0ec 100644 --- a/packages/SystemUI/src/com/android/systemui/tv/TvSystemUIFactory.java +++ b/packages/SystemUI/src/com/android/systemui/tv/TvSystemUIFactory.java @@ -16,8 +16,6 @@ package com.android.systemui.tv; -import android.content.Context; - import com.android.systemui.SystemUIFactory; import com.android.systemui.dagger.GlobalRootComponent; @@ -27,9 +25,7 @@ import com.android.systemui.dagger.GlobalRootComponent; */ public class TvSystemUIFactory extends SystemUIFactory { @Override - protected GlobalRootComponent buildGlobalRootComponent(Context context) { - return DaggerTvGlobalRootComponent.builder() - .context(context) - .build(); + protected GlobalRootComponent.Builder getGlobalRootComponentBuilder() { + return DaggerTvGlobalRootComponent.builder(); } } diff --git a/packages/SystemUI/src/com/android/systemui/util/InitializationChecker.kt b/packages/SystemUI/src/com/android/systemui/util/InitializationChecker.kt new file mode 100644 index 0000000000000..f53b6824d45d3 --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/util/InitializationChecker.kt @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.systemui.util + +import android.app.ActivityThread +import android.os.Process +import com.android.systemui.dagger.qualifiers.InstrumentationTest +import javax.inject.Inject + +/** + * Used to check whether SystemUI should be fully initialized. + */ +class InitializationChecker @Inject constructor( + @InstrumentationTest private val instrumentationTest: Boolean +) { + + /** + * Only initialize components for the main system ui process running as the primary user + */ + fun initializeComponents(): Boolean = + !instrumentationTest && + Process.myUserHandle().isSystem && + ActivityThread.currentProcessName() == ActivityThread.currentPackageName() +}