Search in sources :

Example 81 with Widget

use of com.android.launcher3.tapl.Widget in project android_packages_apps_Trebuchet by LineageOS.

the class Launcher method addAppWidgetFromDrop.

/**
 * Process a widget drop.
 */
private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
    AppWidgetHostView hostView = info.boundWidget;
    final int appWidgetId;
    WidgetAddFlowHandler addFlowHandler = info.getHandler();
    if (hostView != null) {
        // In the case where we've prebound the widget, we remove it from the DragLayer
        if (LOGD) {
            Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
        }
        getDragLayer().removeView(hostView);
        appWidgetId = hostView.getAppWidgetId();
        addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
        // Clear the boundWidget so that it doesn't get destroyed.
        info.boundWidget = null;
    } else {
        // the widget, or we need to start an activity to configure the widget, or both.
        if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
            appWidgetId = CustomWidgetManager.INSTANCE.get(this).getWidgetIdForCustomProvider(info.componentName);
        } else {
            appWidgetId = getAppWidgetHost().allocateAppWidgetId();
        }
        Bundle options = info.bindOptions;
        boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.info, options);
        if (success) {
            addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
        } else {
            addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
        }
    }
}
Also used : LauncherAppWidgetHostView(com.android.launcher3.widget.LauncherAppWidgetHostView) AppWidgetHostView(android.appwidget.AppWidgetHostView) PendingAppWidgetHostView(com.android.launcher3.widget.PendingAppWidgetHostView) Bundle(android.os.Bundle) WidgetAddFlowHandler(com.android.launcher3.widget.WidgetAddFlowHandler)

Example 82 with Widget

use of com.android.launcher3.tapl.Widget in project android_packages_apps_Trebuchet by LineageOS.

the class Launcher method inflateAppWidget.

private View inflateAppWidget(LauncherAppWidgetInfo item) {
    if (item.hasOptionFlag(LauncherAppWidgetInfo.OPTION_SEARCH_WIDGET)) {
        item.providerName = QsbContainerView.getSearchComponentName(this);
        if (item.providerName == null) {
            getModelWriter().deleteItemFromDatabase(item);
            return null;
        }
    }
    final AppWidgetHostView view;
    if (mIsSafeModeEnabled) {
        view = new PendingAppWidgetHostView(this, item, mIconCache, true);
        prepareAppWidget(view, item);
        return view;
    }
    Object traceToken = TraceHelper.INSTANCE.beginSection("BIND_WIDGET_id=" + item.appWidgetId);
    try {
        final LauncherAppWidgetProviderInfo appWidgetInfo;
        if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
            // If the provider is not ready, bind as a pending widget.
            appWidgetInfo = null;
        } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
            // The widget id is not valid. Try to find the widget based on the provider info.
            appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
        } else {
            appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
        }
        // If the provider is ready, but the width is not yet restored, try to restore it.
        if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) && (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
            if (appWidgetInfo == null) {
                Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId + " belongs to component " + item.providerName + ", as the provider is null");
                getModelWriter().deleteItemFromDatabase(item);
                return null;
            }
            // If we do not have a valid id, try to bind an id.
            if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
                if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
                    // Id has not been allocated yet. Allocate a new id.
                    item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
                    item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
                    // Also try to bind the widget. If the bind fails, the user will be shown
                    // a click to setup UI, which will ask for the bind permission.
                    PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
                    pendingInfo.spanX = item.spanX;
                    pendingInfo.spanY = item.spanY;
                    pendingInfo.minSpanX = item.minSpanX;
                    pendingInfo.minSpanY = item.minSpanY;
                    Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
                    boolean isDirectConfig = item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
                    if (isDirectConfig && item.bindOptions != null) {
                        Bundle newOptions = item.bindOptions.getExtras();
                        if (options != null) {
                            newOptions.putAll(options);
                        }
                        options = newOptions;
                    }
                    boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(item.appWidgetId, appWidgetInfo, options);
                    // We tried to bind once. If we were not able to bind, we would need to
                    // go through the permission dialog, which means we cannot skip the config
                    // activity.
                    item.bindOptions = null;
                    item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
                    // Bind succeeded
                    if (success) {
                        // If the widget has a configure activity, it is still needs to set it
                        // up, otherwise the widget is ready to go.
                        item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig ? LauncherAppWidgetInfo.RESTORE_COMPLETED : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
                    }
                    getModelWriter().updateItemInDatabase(item);
                }
            } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY) && (appWidgetInfo.configure == null)) {
                // The widget was marked as UI not ready, but there is no configure activity to
                // update the UI.
                item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
                getModelWriter().updateItemInDatabase(item);
            } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY) && appWidgetInfo.configure != null) {
                if (mAppWidgetManager.isAppWidgetRestored(item.appWidgetId)) {
                    item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
                    getModelWriter().updateItemInDatabase(item);
                }
            }
        }
        if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
            // Verify that we own the widget
            if (appWidgetInfo == null) {
                FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
                getModelWriter().deleteWidgetInfo(item, getAppWidgetHost());
                return null;
            }
            item.minSpanX = appWidgetInfo.minSpanX;
            item.minSpanY = appWidgetInfo.minSpanY;
            view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
        } else if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) && appWidgetInfo != null) {
            mAppWidgetHost.addPendingView(item.appWidgetId, new PendingAppWidgetHostView(this, item, mIconCache, false));
            view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
        } else {
            view = new PendingAppWidgetHostView(this, item, mIconCache, false);
        }
        prepareAppWidget(view, item);
    } finally {
        TraceHelper.INSTANCE.endSection(traceToken);
    }
    return view;
}
Also used : LauncherAppWidgetHostView(com.android.launcher3.widget.LauncherAppWidgetHostView) AppWidgetHostView(android.appwidget.AppWidgetHostView) PendingAppWidgetHostView(com.android.launcher3.widget.PendingAppWidgetHostView) PendingAddWidgetInfo(com.android.launcher3.widget.PendingAddWidgetInfo) Bundle(android.os.Bundle) DragObject(com.android.launcher3.DropTarget.DragObject) PendingAppWidgetHostView(com.android.launcher3.widget.PendingAppWidgetHostView)

Example 83 with Widget

use of com.android.launcher3.tapl.Widget in project android_packages_apps_Trebuchet by LineageOS.

the class Launcher method completeAddAppWidget.

/**
 * Add a widget to the workspace.
 *
 * @param appWidgetId The app widget id
 */
@Thunk
void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo, AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
    if (appWidgetInfo == null) {
        appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
    }
    LauncherAppWidgetInfo launcherInfo;
    launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
    launcherInfo.spanX = itemInfo.spanX;
    launcherInfo.spanY = itemInfo.spanY;
    launcherInfo.minSpanX = itemInfo.minSpanX;
    launcherInfo.minSpanY = itemInfo.minSpanY;
    launcherInfo.user = appWidgetInfo.getProfile();
    getModelWriter().addItemToDatabase(launcherInfo, itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
    if (hostView == null) {
        // Perform actual inflation because we're live
        hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
    }
    hostView.setVisibility(View.VISIBLE);
    prepareAppWidget(hostView, launcherInfo);
    mWorkspace.addInScreen(hostView, launcherInfo);
    announceForAccessibility(R.string.item_added_to_workspace);
}
Also used : LauncherAppWidgetInfo(com.android.launcher3.model.data.LauncherAppWidgetInfo) Thunk(com.android.launcher3.util.Thunk)

Example 84 with Widget

use of com.android.launcher3.tapl.Widget in project android_packages_apps_Trebuchet by LineageOS.

the class AppWidgetResizeFrame method showForWidget.

public static void showForWidget(LauncherAppWidgetHostView widget, CellLayout cellLayout) {
    Launcher launcher = Launcher.getLauncher(cellLayout.getContext());
    AbstractFloatingView.closeAllOpenViews(launcher);
    DragLayer dl = launcher.getDragLayer();
    AppWidgetResizeFrame frame = (AppWidgetResizeFrame) launcher.getLayoutInflater().inflate(R.layout.app_widget_resize_frame, dl, false);
    frame.setupForWidget(widget, cellLayout, dl);
    ((DragLayer.LayoutParams) frame.getLayoutParams()).customPosition = true;
    dl.addView(frame);
    frame.mIsOpen = true;
    frame.snapToWidget(false);
}
Also used : DragLayer(com.android.launcher3.dragndrop.DragLayer)

Example 85 with Widget

use of com.android.launcher3.tapl.Widget in project android_packages_apps_Trebuchet by LineageOS.

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]);
        int result = new ContentWriter(context, new ContentWriter.CommitParams("appWidgetId=? and (restored & 1) = 1 and profileId=?", new String[] { oldWidgetId, Long.toString(mainProfileId) })).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();
            }
        }
    }
    LauncherAppState app = LauncherAppState.getInstanceNoCreate();
    if (app != null) {
        app.getModel().forceReload();
    }
}
Also used : ContentWriter(com.android.launcher3.util.ContentWriter) AppWidgetHost(android.appwidget.AppWidgetHost) AppWidgetManager(android.appwidget.AppWidgetManager) AppWidgetProviderInfo(android.appwidget.AppWidgetProviderInfo) Cursor(android.database.Cursor) ContentResolver(android.content.ContentResolver) WorkerThread(androidx.annotation.WorkerThread)

Aggregations

LauncherAppWidgetInfo (com.android.launcher3.model.data.LauncherAppWidgetInfo)105 Test (org.junit.Test)87 LauncherAppWidgetProviderInfo (com.android.launcher3.widget.LauncherAppWidgetProviderInfo)71 LauncherAppWidgetHostView (com.android.launcher3.widget.LauncherAppWidgetHostView)66 Point (android.graphics.Point)62 ArrayList (java.util.ArrayList)60 AppWidgetProviderInfo (android.appwidget.AppWidgetProviderInfo)56 View (android.view.View)55 LargeTest (androidx.test.filters.LargeTest)55 ComponentName (android.content.ComponentName)53 AppWidgetHostView (android.appwidget.AppWidgetHostView)52 Bundle (android.os.Bundle)50 WidgetItem (com.android.launcher3.model.WidgetItem)49 AbstractLauncherUiTest (com.android.launcher3.ui.AbstractLauncherUiTest)49 PendingAppWidgetHostView (com.android.launcher3.widget.PendingAppWidgetHostView)47 ItemInfo (com.android.launcher3.model.data.ItemInfo)44 PackageUserKey (com.android.launcher3.util.PackageUserKey)41 PendingAddWidgetInfo (com.android.launcher3.widget.PendingAddWidgetInfo)41 Context (android.content.Context)31 WidgetManagerHelper (com.android.launcher3.widget.WidgetManagerHelper)31