9cd3154952
Multiple singletons depend on Plugin which can cause deadlock if PluginManager is initialized on main thread Test: presubmit Bug: 361850561 Bug: 373557167 Flag: EXEMPT dagger Change-Id: I79f17ac6b78a2ce60df2d27a6e794b9e4eba1b51
196 lines
7.4 KiB
Java
196 lines
7.4 KiB
Java
/*
|
|
* Copyright (C) 2019 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.widget.custom;
|
|
|
|
import static com.android.launcher3.Flags.enableSmartspaceAsAWidget;
|
|
import static com.android.launcher3.model.data.LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
|
|
import static com.android.launcher3.widget.LauncherAppWidgetProviderInfo.CLS_CUSTOM_WIDGET_PREFIX;
|
|
|
|
import android.appwidget.AppWidgetManager;
|
|
import android.appwidget.AppWidgetProviderInfo;
|
|
import android.content.ComponentName;
|
|
import android.content.Context;
|
|
import android.os.Parcel;
|
|
import android.os.Process;
|
|
import android.util.Log;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.Nullable;
|
|
import androidx.annotation.VisibleForTesting;
|
|
|
|
import com.android.launcher3.R;
|
|
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.widget.LauncherAppWidgetHostView;
|
|
import com.android.launcher3.widget.LauncherAppWidgetProviderInfo;
|
|
import com.android.systemui.plugins.CustomWidgetPlugin;
|
|
import com.android.systemui.plugins.PluginListener;
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
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> {
|
|
|
|
public static final DaggerSingletonObject<CustomWidgetManager> INSTANCE =
|
|
new DaggerSingletonObject<>(LauncherBaseAppComponent::getCustomWidgetManager);
|
|
|
|
private static final String TAG = "CustomWidgetManager";
|
|
private static final String PLUGIN_PKG = "android";
|
|
private final Context mContext;
|
|
private final HashMap<ComponentName, CustomWidgetPlugin> mPlugins;
|
|
private final List<CustomAppWidgetProviderInfo> mCustomWidgets;
|
|
private Consumer<PackageUserKey> mWidgetRefreshCallback;
|
|
private final @NonNull AppWidgetManager mAppWidgetManager;
|
|
|
|
@Inject
|
|
CustomWidgetManager(@ApplicationContext Context context, PluginManagerWrapper pluginManager,
|
|
DaggerSingletonTracker tracker) {
|
|
this(context, pluginManager, AppWidgetManager.getInstance(context), tracker);
|
|
}
|
|
|
|
@VisibleForTesting
|
|
CustomWidgetManager(@ApplicationContext Context context,
|
|
PluginManagerWrapper pluginManager,
|
|
@NonNull AppWidgetManager widgetManager,
|
|
DaggerSingletonTracker tracker) {
|
|
mContext = context;
|
|
mAppWidgetManager = widgetManager;
|
|
mPlugins = new HashMap<>();
|
|
mCustomWidgets = new ArrayList<>();
|
|
|
|
pluginManager.addPluginListener(this, CustomWidgetPlugin.class, true);
|
|
|
|
ExecutorUtil.executeSyncOnMainOrFail(() -> {
|
|
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(() -> pluginManager.removePluginListener(this));
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public void onPluginConnected(CustomWidgetPlugin plugin, Context context) {
|
|
List<AppWidgetProviderInfo> providers = mAppWidgetManager
|
|
.getInstalledProvidersForProfile(Process.myUserHandle());
|
|
if (providers.isEmpty()) return;
|
|
Parcel parcel = Parcel.obtain();
|
|
providers.get(0).writeToParcel(parcel, 0);
|
|
parcel.setDataPosition(0);
|
|
CustomAppWidgetProviderInfo info = newInfo(plugin, parcel);
|
|
parcel.recycle();
|
|
mPlugins.put(info.provider, plugin);
|
|
mCustomWidgets.add(info);
|
|
}
|
|
|
|
@Override
|
|
public void onPluginDisconnected(CustomWidgetPlugin plugin) {
|
|
ComponentName cn = getWidgetProviderComponent(plugin);
|
|
mPlugins.remove(cn);
|
|
mCustomWidgets.removeIf(w -> w.getComponent().equals(cn));
|
|
}
|
|
|
|
@VisibleForTesting
|
|
@NonNull
|
|
Map<ComponentName, CustomWidgetPlugin> getPlugins() {
|
|
return mPlugins;
|
|
}
|
|
|
|
/**
|
|
* Inject a callback function to refresh the widgets.
|
|
*/
|
|
public void setWidgetRefreshCallback(Consumer<PackageUserKey> cb) {
|
|
mWidgetRefreshCallback = cb;
|
|
}
|
|
|
|
/**
|
|
* Callback method to inform a plugin it's corresponding widget has been created.
|
|
*/
|
|
public void onViewCreated(LauncherAppWidgetHostView view) {
|
|
CustomAppWidgetProviderInfo info = (CustomAppWidgetProviderInfo) view.getAppWidgetInfo();
|
|
CustomWidgetPlugin plugin = mPlugins.get(info.provider);
|
|
if (plugin == null) return;
|
|
plugin.onViewCreated(view);
|
|
}
|
|
|
|
/**
|
|
* Returns the stream of custom widgets.
|
|
*/
|
|
@NonNull
|
|
public Stream<CustomAppWidgetProviderInfo> stream() {
|
|
return mCustomWidgets.stream();
|
|
}
|
|
|
|
/**
|
|
* Returns the widget provider in respect to given widget id.
|
|
*/
|
|
@Nullable
|
|
public LauncherAppWidgetProviderInfo getWidgetProvider(ComponentName cn) {
|
|
return mCustomWidgets.stream()
|
|
.filter(w -> w.getComponent().equals(cn)).findAny().orElse(null);
|
|
}
|
|
|
|
private CustomAppWidgetProviderInfo newInfo(CustomWidgetPlugin plugin, Parcel parcel) {
|
|
CustomAppWidgetProviderInfo info = new CustomAppWidgetProviderInfo(parcel, false);
|
|
info.provider = getWidgetProviderComponent(plugin);
|
|
plugin.updateWidgetInfo(info, mContext);
|
|
return info;
|
|
}
|
|
|
|
/**
|
|
* Returns an id to set as the appWidgetId for a custom widget.
|
|
*/
|
|
public int allocateCustomAppWidgetId(ComponentName componentName) {
|
|
return CUSTOM_WIDGET_ID - mCustomWidgets.indexOf(getWidgetProvider(componentName));
|
|
}
|
|
|
|
private ComponentName getWidgetProviderComponent(CustomWidgetPlugin plugin) {
|
|
return new ComponentName(
|
|
PLUGIN_PKG, CLS_CUSTOM_WIDGET_PREFIX + plugin.getClass().getName());
|
|
}
|
|
}
|