Make sandboxContext extend LauncherApplication (4/n)

See
https://docs.google.com/drawings/d/1JHFi_nhmQt2xPT1N3FB_1mnaRK5TVqKZ9-fSl3EA7sU/edit?usp=sharing
and
https://docs.google.com/drawings/d/1bx4WURP4uHZGzZ1bWQgpw701jkTkVqlNfA02Yt-ZtSI/edit?usp=sharing&resourcekey=0-oySjsnaCsOSrNIPqqEa0gw
for design details.

We need to make SandboxContext extend LauncherApplication because we
want create MainThreadInitializedObjects in SandboxContext's
AppComponent scope. Since MainThreadInitiliazedObjects are closed in
SandboxContext's OnDestroy() , we need to replicate same thing using
dagger as well.

- DaggerSingletonObject is same as MainThreadInitializedObject but is
  used for fetching the dagger created singletons so that we can avoid
  major refactors for accessing singletons. This will be deleted soon.

- DaggerSingletonTracker to track dagger created singletons and call
  close() on those singleton objects created in SandboxContext scope.

- Annotate the singleton object SettingsChangeLogger constructor with @Inject and execute the statements in Main thread.
- Added createSandboxContextForTest(only for Test) to avoid creation of
  dagger component in test. As follow up, I will delete this method and
  introduce fakeDaggerComponents in test.

Bug: 361850561
Test: Manual
Flag: NONE Dagger Integration

Change-Id: I2d3762ea64e53baa4de190790568aec750b54201
This commit is contained in:
Anushree Ganjam
2024-09-13 15:03:11 -07:00
parent b41def0366
commit 26a5f65afd
11 changed files with 205 additions and 39 deletions

View File

@@ -18,6 +18,7 @@ package com.android.launcher3;
import android.app.Application;
import com.android.launcher3.dagger.DaggerLauncherAppComponent;
import com.android.launcher3.dagger.LauncherAppComponent;
import com.android.launcher3.dagger.LauncherBaseAppComponent;
/**
@@ -30,10 +31,18 @@ public class LauncherApplication extends Application {
public void onCreate() {
super.onCreate();
MainProcessInitializer.initialize(this);
mAppComponent = DaggerLauncherAppComponent.builder().appContext(this).build();
initDagger();
}
public LauncherBaseAppComponent getAppComponent() {
return mAppComponent;
public LauncherAppComponent getAppComponent() {
// Since supertype setters will return a supertype.builder and @Component.Builder types
// must not have any generic types.
// We need to cast mAppComponent to {@link LauncherAppComponent} since appContext()
// method is defined in the super class LauncherBaseComponent#Builder.
return (LauncherAppComponent) mAppComponent;
}
protected void initDagger() {
mAppComponent = DaggerLauncherAppComponent.builder().appContext(this).build();
}
}