Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 78494823 authored by Pinyao Ting's avatar Pinyao Ting
Browse files

Include following chagnes to custom widget plugin

1. removed unsupported methods
2. auto-generate providerId
3. piggy-back context for the plugin to resolve resources

Bug: 139888225
Change-Id: If5014099eb80efd64025230037aae3d6079b69a0
parent ebbc33f4
Loading
Loading
Loading
Loading
+27 −18
Original line number Diff line number Diff line
@@ -50,13 +50,18 @@ public class CustomWidgetManager implements PluginListener<CustomWidgetPlugin> {
    public static final MainThreadInitializedObject<CustomWidgetManager> INSTANCE =
            new MainThreadInitializedObject<>(CustomWidgetManager::new);

    private final List<CustomWidgetPlugin> 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<CustomWidgetPlugin> mPlugins;
    private final List<CustomAppWidgetProviderInfo> mCustomWidgets;
    private final SparseArray<ComponentName> mWidgetsIdMap;
    private Consumer<PackageUserKey> 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<CustomWidgetPlugin> {

    @Override
    public void onPluginConnected(CustomWidgetPlugin plugin, Context context) {
        mPlugins.add(plugin);
        mPlugins.put(mAutoProviderId, plugin);
        List<AppWidgetProviderInfo> 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<CustomWidgetPlugin> {
     */
    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<CustomWidgetPlugin> {
        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<CustomWidgetPlugin> {
        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;
    }
}
+0 −17
Original line number Diff line number Diff line
@@ -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.