diff --git a/src/com/android/launcher3/widget/custom/CustomWidgetManager.java b/src/com/android/launcher3/widget/custom/CustomWidgetManager.java index cf3e26d03d..b3569f23b0 100644 --- a/src/com/android/launcher3/widget/custom/CustomWidgetManager.java +++ b/src/com/android/launcher3/widget/custom/CustomWidgetManager.java @@ -50,13 +50,18 @@ public class CustomWidgetManager implements PluginListener { public static final MainThreadInitializedObject INSTANCE = new MainThreadInitializedObject<>(CustomWidgetManager::new); - private final List mPlugins; + /** + * auto provider Id is an ever-increasing number that serves as the providerId whenever a new + * custom widget has been connected. + */ + private int mAutoProviderId = 0; + private final SparseArray mPlugins; private final List mCustomWidgets; private final SparseArray mWidgetsIdMap; private Consumer mWidgetRefreshCallback; private CustomWidgetManager(Context context) { - mPlugins = new ArrayList<>(); + mPlugins = new SparseArray<>(); mCustomWidgets = new ArrayList<>(); mWidgetsIdMap = new SparseArray<>(); PluginManagerWrapper.INSTANCE.get(context) @@ -65,25 +70,28 @@ public class CustomWidgetManager implements PluginListener { @Override public void onPluginConnected(CustomWidgetPlugin plugin, Context context) { - mPlugins.add(plugin); + mPlugins.put(mAutoProviderId, plugin); List providers = AppWidgetManager.getInstance(context) .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, context); + CustomAppWidgetProviderInfo info = newInfo(mAutoProviderId, plugin, parcel, context); parcel.recycle(); mCustomWidgets.add(info); - mWidgetsIdMap.put(plugin.getProviderId(), info.provider); + mWidgetsIdMap.put(mAutoProviderId, info.provider); mWidgetRefreshCallback.accept(null); + mAutoProviderId++; } @Override public void onPluginDisconnected(CustomWidgetPlugin plugin) { - mPlugins.remove(plugin); - mCustomWidgets.remove(getWidgetProvider(plugin.getProviderId())); - mWidgetsIdMap.remove(plugin.getProviderId()); + int providerId = findProviderId(plugin); + if (providerId == -1) return; + mPlugins.remove(providerId); + mCustomWidgets.remove(getWidgetProvider(providerId)); + mWidgetsIdMap.remove(providerId); } /** @@ -98,7 +106,7 @@ public class CustomWidgetManager implements PluginListener { */ public void onViewCreated(LauncherAppWidgetHostView view) { CustomAppWidgetProviderInfo info = (CustomAppWidgetProviderInfo) view.getAppWidgetInfo(); - CustomWidgetPlugin plugin = findPlugin(info.providerId); + CustomWidgetPlugin plugin = mPlugins.get(info.providerId); if (plugin == null) return; plugin.onViewCreated(view); } @@ -135,17 +143,14 @@ public class CustomWidgetManager implements PluginListener { return null; } - private static CustomAppWidgetProviderInfo newInfo( - CustomWidgetPlugin plugin, Parcel parcel, Context context) { - int providerId = plugin.getProviderId(); + private static CustomAppWidgetProviderInfo newInfo(int providerId, CustomWidgetPlugin plugin, + Parcel parcel, Context context) { CustomAppWidgetProviderInfo info = new CustomAppWidgetProviderInfo( parcel, false, providerId); info.provider = new ComponentName( context.getPackageName(), CLS_CUSTOM_WIDGET_PREFIX + providerId); info.label = plugin.getLabel(); - info.icon = plugin.getIcon(); - info.previewImage = plugin.getPreviewImage(); info.resizeMode = plugin.getResizeMode(); info.spanX = plugin.getSpanX(); @@ -155,9 +160,13 @@ public class CustomWidgetManager implements PluginListener { return info; } - @Nullable - private CustomWidgetPlugin findPlugin(int providerId) { - return mPlugins.stream().filter((p) -> p.getProviderId() == providerId).findFirst() - .orElse(null); + private int findProviderId(CustomWidgetPlugin plugin) { + for (int i = 0; i < mPlugins.size(); i++) { + int providerId = mPlugins.keyAt(i); + if (mPlugins.get(providerId) == plugin) { + return providerId; + } + } + return -1; } } diff --git a/src_plugins/com/android/systemui/plugins/CustomWidgetPlugin.java b/src_plugins/com/android/systemui/plugins/CustomWidgetPlugin.java index 77ad7ea30a..56ebcc5dc6 100644 --- a/src_plugins/com/android/systemui/plugins/CustomWidgetPlugin.java +++ b/src_plugins/com/android/systemui/plugins/CustomWidgetPlugin.java @@ -29,28 +29,11 @@ public interface CustomWidgetPlugin extends Plugin { String ACTION = "com.android.systemui.action.PLUGIN_CUSTOM_WIDGET"; int VERSION = 1; - /** - * An unique identifier for this widget. Must be a non-negative integer. - */ - int getProviderId(); - /** * The label to display to the user in the AppWidget picker. */ String getLabel(); - /** - * A preview of what the AppWidget will look like after it's configured. - * If not supplied, the AppWidget's icon will be used. - */ - int getPreviewImage(); - - /** - * The icon to display for this AppWidget in the AppWidget picker. If not supplied in the - * xml, the application icon will be used. - */ - int getIcon(); - /** * The default width of the widget when added to a host, in dp. The widget will get * at least this width, and will often be given more, depending on the host.