diff --git a/dagger/Android.bp b/dagger/Android.bp index 4dbe868445..b37e1d60d6 100644 --- a/dagger/Android.bp +++ b/dagger/Android.bp @@ -20,7 +20,7 @@ package { java_library { name: "launcher-dagger-qualifiers", srcs: [ - "src/com/android/launcher3/dagger/ActivityContextScope.java", + "src/com/android/launcher3/dagger/ActivityContextSingleton.java", "src/com/android/launcher3/dagger/ApplicationContext.java", "src/com/android/launcher3/dagger/LauncherAppSingleton.java", ], diff --git a/dagger/src/com/android/launcher3/dagger/ActivityContextScope.java b/dagger/src/com/android/launcher3/dagger/ActivityContextSingleton.java similarity index 95% rename from dagger/src/com/android/launcher3/dagger/ActivityContextScope.java rename to dagger/src/com/android/launcher3/dagger/ActivityContextSingleton.java index 887f15cacf..dd7510cea9 100644 --- a/dagger/src/com/android/launcher3/dagger/ActivityContextScope.java +++ b/dagger/src/com/android/launcher3/dagger/ActivityContextSingleton.java @@ -28,5 +28,5 @@ import javax.inject.Scope; @Documented @Retention(RetentionPolicy.RUNTIME) @Scope -public @interface ActivityContextScope { +public @interface ActivityContextSingleton { } diff --git a/quickstep/dagger/com/android/launcher3/dagger/ActivityContextComponent.kt b/quickstep/dagger/com/android/launcher3/dagger/ActivityContextComponent.kt new file mode 100644 index 0000000000..facc90ad7f --- /dev/null +++ b/quickstep/dagger/com/android/launcher3/dagger/ActivityContextComponent.kt @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2025 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.launcher3.dagger + +import com.android.quickstep.dagger.QuickstepBaseActivityComponent +import dagger.Subcomponent + +/** Sub component for Dagger injection for ActivityContext. */ +@Subcomponent +@ActivityContextSingleton +interface ActivityContextComponent : QuickstepBaseActivityComponent { + + @Subcomponent.Builder + interface Builder : BaseActivityContextComponent.Builder { + + override fun build(): ActivityContextComponent + } +} diff --git a/quickstep/src/com/android/quickstep/dagger/QuickstepBaseActivityComponent.kt b/quickstep/src/com/android/quickstep/dagger/QuickstepBaseActivityComponent.kt new file mode 100644 index 0000000000..a34330c381 --- /dev/null +++ b/quickstep/src/com/android/quickstep/dagger/QuickstepBaseActivityComponent.kt @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2025 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.quickstep.dagger + +import com.android.launcher3.dagger.BaseActivityContextComponent + +/** Activity Quickstep base component for Dagger injection. */ +interface QuickstepBaseActivityComponent : BaseActivityContextComponent {} diff --git a/src/com/android/launcher3/BaseActivity.java b/src/com/android/launcher3/BaseActivity.java index ab500dc782..34aca9807d 100644 --- a/src/com/android/launcher3/BaseActivity.java +++ b/src/com/android/launcher3/BaseActivity.java @@ -43,6 +43,8 @@ import androidx.savedstate.SavedStateRegistry; import androidx.savedstate.SavedStateRegistryController; import com.android.launcher3.DeviceProfile.OnDeviceProfileChangeListener; +import com.android.launcher3.dagger.ActivityContextComponent; +import com.android.launcher3.dagger.LauncherComponentProvider; import com.android.launcher3.logging.StatsLogManager; import com.android.launcher3.model.data.ItemInfo; import com.android.launcher3.testing.TestLogging; @@ -189,6 +191,8 @@ public abstract class BaseActivity extends Activity implements ActivityContext, private ActionMode mCurrentActionMode; + private ActivityContextComponent mActivityComponent; + public BaseActivity() { mSavedStateRegistryController.performAttach(); registerActivityLifecycleCallbacks( @@ -210,6 +214,15 @@ public abstract class BaseActivity extends Activity implements ActivityContext, return mDPChangeListeners; } + @Override + public ActivityContextComponent getActivityComponent() { + if (mActivityComponent == null) { + mActivityComponent = (ActivityContextComponent) LauncherComponentProvider.get(this) + .getActivityContextComponentBuilder().activityContext(this).build(); + } + return mActivityComponent; + } + /** * Returns {@link StatsLogManager} for user event logging. */ diff --git a/src/com/android/launcher3/dagger/BaseActivityContextComponent.kt b/src/com/android/launcher3/dagger/BaseActivityContextComponent.kt new file mode 100644 index 0000000000..01334ee065 --- /dev/null +++ b/src/com/android/launcher3/dagger/BaseActivityContextComponent.kt @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2025 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.launcher3.dagger + +import com.android.launcher3.views.ActivityContext +import dagger.BindsInstance + +/** Base component for ActivityContext Dagger injection. */ +interface BaseActivityContextComponent { + + /** Builder for BaseActivityContextComponent. */ + interface Builder { + @BindsInstance fun activityContext(activityContext: ActivityContext): Builder + + fun build(): BaseActivityContextComponent + } +} diff --git a/src/com/android/launcher3/dagger/LauncherAppModule.java b/src/com/android/launcher3/dagger/LauncherAppModule.java index a1d55795c9..de798fd9f7 100644 --- a/src/com/android/launcher3/dagger/LauncherAppModule.java +++ b/src/com/android/launcher3/dagger/LauncherAppModule.java @@ -21,17 +21,18 @@ import com.android.launcher3.util.dagger.LauncherExecutorsModule; import dagger.Module; -@Module(includes = { - WindowManagerProxyModule.class, - ApiWrapperModule.class, - PluginManagerWrapperModule.class, - StaticObjectModule.class, - WidgetModule.class, - AppModule.class, - PerDisplayModule.class, - LauncherConcurrencyModule.class, - ExecutorsModule.class, - LauncherExecutorsModule.class, -}) -public class LauncherAppModule { -} +@Module( + includes = { + WindowManagerProxyModule.class, + ApiWrapperModule.class, + PluginManagerWrapperModule.class, + StaticObjectModule.class, + WidgetModule.class, + AppModule.class, + PerDisplayModule.class, + LauncherConcurrencyModule.class, + ExecutorsModule.class, + LauncherExecutorsModule.class, + }, + subcomponents = ActivityContextComponent.class) +public class LauncherAppModule { } diff --git a/src/com/android/launcher3/dagger/LauncherBaseAppComponent.java b/src/com/android/launcher3/dagger/LauncherBaseAppComponent.java index ad59b54145..10806f6083 100644 --- a/src/com/android/launcher3/dagger/LauncherBaseAppComponent.java +++ b/src/com/android/launcher3/dagger/LauncherBaseAppComponent.java @@ -100,6 +100,7 @@ public interface LauncherBaseAppComponent { InstantAppResolver getInstantAppResolver(); DumpManager getDumpManager(); StatsLogManager.StatsLogManagerFactory getStatsLogManagerFactory(); + ActivityContextComponent.Builder getActivityContextComponentBuilder(); /** Builder for LauncherBaseAppComponent. */ interface Builder { diff --git a/src/com/android/launcher3/graphics/LauncherPreviewRenderer.java b/src/com/android/launcher3/graphics/LauncherPreviewRenderer.java index dbf2b08109..ca404ea451 100644 --- a/src/com/android/launcher3/graphics/LauncherPreviewRenderer.java +++ b/src/com/android/launcher3/graphics/LauncherPreviewRenderer.java @@ -97,7 +97,6 @@ import com.android.launcher3.util.SandboxContext; import com.android.launcher3.util.Themes; import com.android.launcher3.util.dagger.LauncherExecutorsModule; import com.android.launcher3.util.window.WindowManagerProxy; -import com.android.launcher3.views.ActivityContext; import com.android.launcher3.views.BaseDragLayer; import com.android.launcher3.widget.LauncherWidgetHolder; import com.android.launcher3.widget.LauncherWidgetHolder.WidgetHolderFactory; @@ -123,7 +122,7 @@ import java.util.UUID; * 4) Measure and draw the view on a canvas */ public class LauncherPreviewRenderer extends BaseContext - implements ActivityContext, WorkspaceLayoutManager, LayoutInflater.Factory2 { + implements WorkspaceLayoutManager, LayoutInflater.Factory2 { /** * Context used just for preview. It also provides a few objects (e.g. UserCache) just for diff --git a/src/com/android/launcher3/util/BaseContext.kt b/src/com/android/launcher3/util/BaseContext.kt index 8aa10f3688..aa7362474d 100644 --- a/src/com/android/launcher3/util/BaseContext.kt +++ b/src/com/android/launcher3/util/BaseContext.kt @@ -34,6 +34,8 @@ import androidx.savedstate.SavedStateRegistryController import androidx.savedstate.setViewTreeSavedStateRegistryOwner import com.android.launcher3.DeviceProfile.OnDeviceProfileChangeListener import com.android.launcher3.Utilities +import com.android.launcher3.dagger.ActivityContextComponent +import com.android.launcher3.dagger.LauncherComponentProvider.appComponent import com.android.launcher3.views.ActivityContext /** @@ -59,6 +61,13 @@ constructor(base: Context, themeResId: Int, private val destroyOnDetach: Boolean private val viewCache = ViewCache() + private val activityComponentLazy: ActivityContextComponent by lazy { + appComponent.activityContextComponentBuilder.activityContext(this).build() + as ActivityContextComponent + } + + override fun getActivityComponent(): ActivityContextComponent = activityComponentLazy + init { Executors.MAIN_EXECUTOR.execute { savedStateRegistryController.performAttach() diff --git a/src/com/android/launcher3/views/ActivityContext.java b/src/com/android/launcher3/views/ActivityContext.java index 09d083c68d..183f088f1d 100644 --- a/src/com/android/launcher3/views/ActivityContext.java +++ b/src/com/android/launcher3/views/ActivityContext.java @@ -70,6 +70,7 @@ import com.android.launcher3.R; import com.android.launcher3.Utilities; import com.android.launcher3.allapps.ActivityAllAppsContainerView; import com.android.launcher3.celllayout.CellPosMapper; +import com.android.launcher3.dagger.ActivityContextComponent; import com.android.launcher3.dot.DotInfo; import com.android.launcher3.dragndrop.DragController; import com.android.launcher3.folder.FolderIcon; @@ -103,6 +104,9 @@ public interface ActivityContext extends SavedStateRegistryOwner { String TAG = "ActivityContext"; + /** Returns the dagger graph for this UI context */ + ActivityContextComponent getActivityComponent(); + default boolean finishAutoCancelActionMode() { return false; } diff --git a/src_no_quickstep/com/android/launcher3/dagger/ActivityContextComponent.kt b/src_no_quickstep/com/android/launcher3/dagger/ActivityContextComponent.kt new file mode 100644 index 0000000000..73498c5e06 --- /dev/null +++ b/src_no_quickstep/com/android/launcher3/dagger/ActivityContextComponent.kt @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2025 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.launcher3.dagger + +import dagger.Subcomponent + +/** Sub component for Dagger injection for ActivityContext. */ +@Subcomponent +@ActivityContextSingleton +interface ActivityContextComponent : BaseActivityContextComponent { + + @Subcomponent.Builder + interface Builder : BaseActivityContextComponent.Builder { + + override fun build(): ActivityContextComponent + } +}