[Dagger] Make CustomWidgetManager provided by DaggerSingletonObject

Test: presubmit
Bug: 361850561
Flag: NONE Dagger change
Change-Id: I9c2a9cc12aa2a409e8f9053a059426843f72deed
This commit is contained in:
Sihua Ma
2024-10-09 22:58:55 +00:00
parent d137856e26
commit 58fc65dedf
3 changed files with 44 additions and 22 deletions
@@ -33,7 +33,12 @@ import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import com.android.launcher3.R;
import com.android.launcher3.util.MainThreadInitializedObject;
import com.android.launcher3.dagger.ApplicationContext;
import com.android.launcher3.dagger.LauncherAppSingleton;
import com.android.launcher3.dagger.LauncherBaseAppComponent;
import com.android.launcher3.util.DaggerSingletonObject;
import com.android.launcher3.util.DaggerSingletonTracker;
import com.android.launcher3.util.ExecutorUtil;
import com.android.launcher3.util.PackageUserKey;
import com.android.launcher3.util.PluginManagerWrapper;
import com.android.launcher3.util.SafeCloseable;
@@ -50,13 +55,16 @@ import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Stream;
import javax.inject.Inject;
/**
* CustomWidgetManager handles custom widgets implemented as a plugin.
*/
@LauncherAppSingleton
public class CustomWidgetManager implements PluginListener<CustomWidgetPlugin>, SafeCloseable {
public static final MainThreadInitializedObject<CustomWidgetManager> INSTANCE =
new MainThreadInitializedObject<>(CustomWidgetManager::new);
public static final DaggerSingletonObject<CustomWidgetManager> INSTANCE =
new DaggerSingletonObject<>(LauncherBaseAppComponent::getCustomWidgetManager);
private static final String TAG = "CustomWidgetManager";
private static final String PLUGIN_PKG = "android";
@@ -66,34 +74,44 @@ public class CustomWidgetManager implements PluginListener<CustomWidgetPlugin>,
private Consumer<PackageUserKey> mWidgetRefreshCallback;
private final @NonNull AppWidgetManager mAppWidgetManager;
private CustomWidgetManager(Context context) {
this(context, AppWidgetManager.getInstance(context));
@Inject
CustomWidgetManager(@ApplicationContext Context context, DaggerSingletonTracker tracker) {
this(context, AppWidgetManager.getInstance(context), tracker);
}
@VisibleForTesting
CustomWidgetManager(Context context, @NonNull AppWidgetManager widgetManager) {
CustomWidgetManager(@ApplicationContext Context context,
@NonNull AppWidgetManager widgetManager,
DaggerSingletonTracker tracker) {
mContext = context;
mAppWidgetManager = widgetManager;
mPlugins = new HashMap<>();
mCustomWidgets = new ArrayList<>();
PluginManagerWrapper.INSTANCE.get(context)
.addPluginListener(this, CustomWidgetPlugin.class, true);
if (enableSmartspaceAsAWidget()) {
for (String s: context.getResources()
.getStringArray(R.array.custom_widget_providers)) {
try {
Class<?> cls = Class.forName(s);
CustomWidgetPlugin plugin = (CustomWidgetPlugin)
cls.getDeclaredConstructor(Context.class).newInstance(context);
onPluginConnected(plugin, context);
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| ClassCastException | NoSuchMethodException
| InvocationTargetException e) {
Log.e(TAG, "Exception found when trying to add custom widgets: " + e);
ExecutorUtil.executeSyncOnMainOrFail(() -> {
PluginManagerWrapper.INSTANCE.get(context)
.addPluginListener(this, CustomWidgetPlugin.class, true);
if (enableSmartspaceAsAWidget()) {
for (String s: context.getResources()
.getStringArray(R.array.custom_widget_providers)) {
try {
Class<?> cls = Class.forName(s);
CustomWidgetPlugin plugin = (CustomWidgetPlugin)
cls.getDeclaredConstructor(Context.class).newInstance(context);
onPluginConnected(plugin, context);
} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException
| ClassCastException | NoSuchMethodException
| InvocationTargetException e) {
Log.e(TAG, "Exception found when trying to add custom widgets: " + e);
}
}
}
}
tracker.addCloseable(this);
});
}
@Override