Search in sources :

Example 1 with LauncherAppWidgetHost

use of com.android.launcher3.widget.LauncherAppWidgetHost in project android_packages_apps_Launcher3 by crdroidandroid.

the class AddItemActivity method setupWidget.

private boolean setupWidget() {
    LauncherAppWidgetProviderInfo widgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this, mRequest.getAppWidgetProviderInfo(this));
    if (widgetInfo.minSpanX > mIdp.numColumns || widgetInfo.minSpanY > mIdp.numRows) {
        // Cannot add widget
        return false;
    }
    mWidgetCell.setRemoteViewsPreview(PinItemDragListener.getPreview(mRequest));
    mAppWidgetManager = new WidgetManagerHelper(this);
    mAppWidgetHost = new LauncherAppWidgetHost(this);
    PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(widgetInfo, CONTAINER_PIN_WIDGETS);
    pendingInfo.spanX = Math.min(mIdp.numColumns, widgetInfo.spanX);
    pendingInfo.spanY = Math.min(mIdp.numRows, widgetInfo.spanY);
    mWidgetOptions = pendingInfo.getDefaultSizeOptions(this);
    mWidgetCell.getWidgetView().setTag(pendingInfo);
    applyWidgetItemAsync(() -> new WidgetItem(widgetInfo, mIdp, mApp.getIconCache()));
    return true;
}
Also used : LauncherAppWidgetProviderInfo(com.android.launcher3.widget.LauncherAppWidgetProviderInfo) LauncherAppWidgetHost(com.android.launcher3.widget.LauncherAppWidgetHost) PendingAddWidgetInfo(com.android.launcher3.widget.PendingAddWidgetInfo) WidgetManagerHelper(com.android.launcher3.widget.WidgetManagerHelper) WidgetItem(com.android.launcher3.model.WidgetItem)

Example 2 with LauncherAppWidgetHost

use of com.android.launcher3.widget.LauncherAppWidgetHost in project android_packages_apps_Launcher3 by crdroidandroid.

the class AppWidgetsRestoredReceiver method restoreAppWidgetIds.

/**
 * Updates the app widgets whose id has changed during the restore process.
 */
@WorkerThread
public static void restoreAppWidgetIds(Context context, int[] oldWidgetIds, int[] newWidgetIds) {
    AppWidgetHost appWidgetHost = new LauncherAppWidgetHost(context);
    if (WidgetsModel.GO_DISABLE_WIDGETS) {
        Log.e(TAG, "Skipping widget ID remap as widgets not supported");
        appWidgetHost.deleteHost();
        return;
    }
    if (!RestoreDbTask.isPending(context)) {
        // Someone has already gone through our DB once, probably LoaderTask. Skip any further
        // modifications of the DB.
        Log.e(TAG, "Skipping widget ID remap as DB already in use");
        for (int widgetId : newWidgetIds) {
            Log.d(TAG, "Deleting widgetId: " + widgetId);
            appWidgetHost.deleteAppWidgetId(widgetId);
        }
        return;
    }
    final ContentResolver cr = context.getContentResolver();
    final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
    for (int i = 0; i < oldWidgetIds.length; i++) {
        Log.i(TAG, "Widget state restore id " + oldWidgetIds[i] + " => " + newWidgetIds[i]);
        final AppWidgetProviderInfo provider = widgets.getAppWidgetInfo(newWidgetIds[i]);
        final int state;
        if (LoaderTask.isValidProvider(provider)) {
            // This will ensure that we show 'Click to setup' UI if required.
            state = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
        } else {
            state = LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY;
        }
        // b/135926478: Work profile widget restore is broken in platform. This forces us to
        // recreate the widget during loading with the correct host provider.
        long mainProfileId = UserCache.INSTANCE.get(context).getSerialNumberForUser(myUserHandle());
        String oldWidgetId = Integer.toString(oldWidgetIds[i]);
        final String where = "appWidgetId=? and (restored & 1) = 1 and profileId=?";
        final String[] args = new String[] { oldWidgetId, Long.toString(mainProfileId) };
        int result = new ContentWriter(context, new ContentWriter.CommitParams(where, args)).put(LauncherSettings.Favorites.APPWIDGET_ID, newWidgetIds[i]).put(LauncherSettings.Favorites.RESTORED, state).commit();
        if (result == 0) {
            Cursor cursor = cr.query(Favorites.CONTENT_URI, new String[] { Favorites.APPWIDGET_ID }, "appWidgetId=?", new String[] { oldWidgetId }, null);
            try {
                if (!cursor.moveToFirst()) {
                    // The widget no long exists.
                    appWidgetHost.deleteAppWidgetId(newWidgetIds[i]);
                }
            } finally {
                cursor.close();
            }
        }
        // attempt to update widget id in backup table as well
        new ContentWriter(context, ContentWriter.CommitParams.backupCommitParams("appWidgetId=? and profileId=?", args)).put(LauncherSettings.Favorites.APPWIDGET_ID, newWidgetIds[i]).put(LauncherSettings.Favorites.RESTORED, state).commit();
    }
    LauncherAppState app = LauncherAppState.getInstanceNoCreate();
    if (app != null) {
        app.getModel().forceReload();
    }
}
Also used : AppWidgetHost(android.appwidget.AppWidgetHost) LauncherAppWidgetHost(com.android.launcher3.widget.LauncherAppWidgetHost) AppWidgetManager(android.appwidget.AppWidgetManager) Cursor(android.database.Cursor) ContentResolver(android.content.ContentResolver) ContentWriter(com.android.launcher3.util.ContentWriter) LauncherAppWidgetHost(com.android.launcher3.widget.LauncherAppWidgetHost) AppWidgetProviderInfo(android.appwidget.AppWidgetProviderInfo) WorkerThread(androidx.annotation.WorkerThread)

Example 3 with LauncherAppWidgetHost

use of com.android.launcher3.widget.LauncherAppWidgetHost in project android_packages_apps_Launcher3 by crdroidandroid.

the class WidgetUtils method createWidgetInfo.

/**
 * Creates a LauncherAppWidgetInfo corresponding to {@param info}
 *
 * @param bindWidget if true the info is bound and a valid widgetId is assigned to
 *                   the LauncherAppWidgetInfo
 */
public static LauncherAppWidgetInfo createWidgetInfo(LauncherAppWidgetProviderInfo info, Context targetContext, boolean bindWidget) {
    LauncherAppWidgetInfo item = new LauncherAppWidgetInfo(LauncherAppWidgetInfo.NO_ID, info.provider);
    item.spanX = info.minSpanX;
    item.spanY = info.minSpanY;
    item.minSpanX = info.minSpanX;
    item.minSpanY = info.minSpanY;
    item.user = info.getProfile();
    item.cellX = 0;
    item.cellY = 1;
    item.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
    if (bindWidget) {
        PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(info, LauncherSettings.Favorites.CONTAINER_WIDGETS_TRAY);
        pendingInfo.spanX = item.spanX;
        pendingInfo.spanY = item.spanY;
        pendingInfo.minSpanX = item.minSpanX;
        pendingInfo.minSpanY = item.minSpanY;
        Bundle options = pendingInfo.getDefaultSizeOptions(targetContext);
        AppWidgetHost host = new LauncherAppWidgetHost(targetContext);
        int widgetId = host.allocateAppWidgetId();
        if (!new WidgetManagerHelper(targetContext).bindAppWidgetIdIfAllowed(widgetId, info, options)) {
            host.deleteAppWidgetId(widgetId);
            throw new IllegalArgumentException("Unable to bind widget id");
        }
        item.appWidgetId = widgetId;
    }
    return item;
}
Also used : PendingAddWidgetInfo(com.android.launcher3.widget.PendingAddWidgetInfo) AppWidgetHost(android.appwidget.AppWidgetHost) LauncherAppWidgetHost(com.android.launcher3.widget.LauncherAppWidgetHost) LauncherAppWidgetHost(com.android.launcher3.widget.LauncherAppWidgetHost) Bundle(android.os.Bundle) WidgetManagerHelper(com.android.launcher3.widget.WidgetManagerHelper) LauncherAppWidgetInfo(com.android.launcher3.model.data.LauncherAppWidgetInfo)

Aggregations

LauncherAppWidgetHost (com.android.launcher3.widget.LauncherAppWidgetHost)3 AppWidgetHost (android.appwidget.AppWidgetHost)2 PendingAddWidgetInfo (com.android.launcher3.widget.PendingAddWidgetInfo)2 WidgetManagerHelper (com.android.launcher3.widget.WidgetManagerHelper)2 AppWidgetManager (android.appwidget.AppWidgetManager)1 AppWidgetProviderInfo (android.appwidget.AppWidgetProviderInfo)1 ContentResolver (android.content.ContentResolver)1 Cursor (android.database.Cursor)1 Bundle (android.os.Bundle)1 WorkerThread (androidx.annotation.WorkerThread)1 WidgetItem (com.android.launcher3.model.WidgetItem)1 LauncherAppWidgetInfo (com.android.launcher3.model.data.LauncherAppWidgetInfo)1 ContentWriter (com.android.launcher3.util.ContentWriter)1 LauncherAppWidgetProviderInfo (com.android.launcher3.widget.LauncherAppWidgetProviderInfo)1