Search in sources :

Example 31 with WIDGETS

use of com.android.launcher3.popup.SystemShortcut.WIDGETS in project android_packages_apps_Trebuchet by LineageOS.

the class AddConfigWidgetTest method runTest.

/**
 * @param acceptConfig accept the config activity
 */
private void runTest(boolean acceptConfig) throws Throwable {
    clearHomescreen();
    mDevice.pressHome();
    final Widgets widgets = mLauncher.getWorkspace().openAllWidgets();
    // Drag widget to homescreen
    WidgetConfigStartupMonitor monitor = new WidgetConfigStartupMonitor();
    widgets.getWidget(mWidgetInfo.getLabel(mTargetContext.getPackageManager())).dragToWorkspace(true, false);
    // Widget id for which the config activity was opened
    mWidgetId = monitor.getWidgetId();
    // Verify that the widget id is valid and bound
    assertNotNull(mAppWidgetManager.getAppWidgetInfo(mWidgetId));
    setResult(acceptConfig);
    if (acceptConfig) {
        Wait.atMost("", new WidgetSearchCondition(), DEFAULT_ACTIVITY_TIMEOUT, mLauncher);
        assertNotNull(mAppWidgetManager.getAppWidgetInfo(mWidgetId));
    } else {
        // Verify that the widget id is deleted.
        Wait.atMost("", () -> mAppWidgetManager.getAppWidgetInfo(mWidgetId) == null, DEFAULT_ACTIVITY_TIMEOUT, mLauncher);
    }
}
Also used : Widgets(com.android.launcher3.tapl.Widgets)

Example 32 with WIDGETS

use of com.android.launcher3.popup.SystemShortcut.WIDGETS in project android_packages_apps_Trebuchet by LineageOS.

the class Launcher method handleActivityResult.

private void handleActivityResult(final int requestCode, final int resultCode, final Intent data) {
    if (isWorkspaceLoading()) {
        // process the result once the workspace has loaded.
        mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
        return;
    }
    mPendingActivityResult = null;
    // Reset the startActivity waiting flag
    final PendingRequestArgs requestArgs = mPendingRequestArgs;
    setWaitingForResult(null);
    if (requestArgs == null) {
        return;
    }
    final int pendingAddWidgetId = requestArgs.getWidgetId();
    Runnable exitSpringLoaded = new Runnable() {

        @Override
        public void run() {
            mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
        }
    };
    if (requestCode == REQUEST_BIND_APPWIDGET) {
        // This is called only if the user did not previously have permissions to bind widgets
        final int appWidgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
        if (resultCode == RESULT_CANCELED) {
            completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
            mWorkspace.removeExtraEmptyScreenDelayed(ON_ACTIVITY_RESULT_ANIMATION_DELAY, false, exitSpringLoaded);
        } else if (resultCode == RESULT_OK) {
            addAppWidgetImpl(appWidgetId, requestArgs, null, requestArgs.getWidgetHandler(), ON_ACTIVITY_RESULT_ANIMATION_DELAY);
        }
        return;
    }
    boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET || requestCode == REQUEST_CREATE_APPWIDGET);
    // We have special handling for widgets
    if (isWidgetDrop) {
        final int appWidgetId;
        int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
        if (widgetId < 0) {
            appWidgetId = pendingAddWidgetId;
        } else {
            appWidgetId = widgetId;
        }
        final int result;
        if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
            Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " + "returned from the widget configuration activity.");
            result = RESULT_CANCELED;
            completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
            mWorkspace.removeExtraEmptyScreenDelayed(ON_ACTIVITY_RESULT_ANIMATION_DELAY, false, () -> getStateManager().goToState(NORMAL));
        } else {
            if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
                // When the screen id represents an actual screen (as opposed to a rank)
                // we make sure that the drop page actually exists.
                requestArgs.screenId = ensurePendingDropLayoutExists(requestArgs.screenId);
            }
            final CellLayout dropLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
            dropLayout.setDropPending(true);
            final Runnable onComplete = new Runnable() {

                @Override
                public void run() {
                    completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
                    dropLayout.setDropPending(false);
                }
            };
            mWorkspace.removeExtraEmptyScreenDelayed(ON_ACTIVITY_RESULT_ANIMATION_DELAY, false, onComplete);
        }
        return;
    }
    if (requestCode == REQUEST_RECONFIGURE_APPWIDGET || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
        if (resultCode == RESULT_OK) {
            // Update the widget view.
            completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
        }
        // Leave the widget in the pending state if the user canceled the configure.
        return;
    }
    if (requestCode == REQUEST_CREATE_SHORTCUT) {
        // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
        if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
            completeAdd(requestCode, data, -1, requestArgs);
            mWorkspace.removeExtraEmptyScreenDelayed(ON_ACTIVITY_RESULT_ANIMATION_DELAY, false, exitSpringLoaded);
        } else if (resultCode == RESULT_CANCELED) {
            mWorkspace.removeExtraEmptyScreenDelayed(ON_ACTIVITY_RESULT_ANIMATION_DELAY, false, exitSpringLoaded);
        }
    }
    mDragLayer.clearAnimatedView();
}
Also used : ActivityResultInfo(com.android.launcher3.util.ActivityResultInfo) PendingRequestArgs(com.android.launcher3.util.PendingRequestArgs)

Example 33 with WIDGETS

use of com.android.launcher3.popup.SystemShortcut.WIDGETS 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)

Example 34 with WIDGETS

use of com.android.launcher3.popup.SystemShortcut.WIDGETS in project android_packages_apps_Trebuchet by LineageOS.

the class WidgetsModel method setWidgetsAndShortcuts.

private synchronized void setWidgetsAndShortcuts(ArrayList<WidgetItem> rawWidgetsShortcuts, LauncherAppState app, @Nullable PackageUserKey packageUser) {
    if (DEBUG) {
        Log.d(TAG, "addWidgetsAndShortcuts, widgetsShortcuts#=" + rawWidgetsShortcuts.size());
    }
    // Temporary list for {@link PackageItemInfos} to avoid having to go through
    // {@link mPackageItemInfos} to locate the key to be used for {@link #mWidgetsList}
    HashMap<String, PackageItemInfo> tmpPackageItemInfos = new HashMap<>();
    // clear the lists.
    if (packageUser == null) {
        mWidgetsList.clear();
    } else {
        // Only clear the widgets for the given package/user.
        PackageItemInfo packageItem = null;
        for (PackageItemInfo item : mWidgetsList.keySet()) {
            if (item.packageName.equals(packageUser.mPackageName)) {
                packageItem = item;
                break;
            }
        }
        if (packageItem != null) {
            // We want to preserve the user that was on the packageItem previously,
            // so add it to tmpPackageItemInfos here to avoid creating a new entry.
            tmpPackageItemInfos.put(packageItem.packageName, packageItem);
            Iterator<WidgetItem> widgetItemIterator = mWidgetsList.get(packageItem).iterator();
            while (widgetItemIterator.hasNext()) {
                WidgetItem nextWidget = widgetItemIterator.next();
                if (nextWidget.componentName.getPackageName().equals(packageUser.mPackageName) && nextWidget.user.equals(packageUser.mUser)) {
                    widgetItemIterator.remove();
                }
            }
        }
    }
    InvariantDeviceProfile idp = app.getInvariantDeviceProfile();
    UserHandle myUser = Process.myUserHandle();
    // add and update.
    for (WidgetItem item : rawWidgetsShortcuts) {
        if (item.widgetInfo != null) {
            if ((item.widgetInfo.getWidgetFeatures() & WIDGET_FEATURE_HIDE_FROM_PICKER) != 0) {
                // Widget is hidden from picker
                continue;
            }
            // Ensure that all widgets we show can be added on a workspace of this size
            int minSpanX = Math.min(item.widgetInfo.spanX, item.widgetInfo.minSpanX);
            int minSpanY = Math.min(item.widgetInfo.spanY, item.widgetInfo.minSpanY);
            if (minSpanX > idp.numColumns || minSpanY > idp.numRows) {
                if (DEBUG) {
                    Log.d(TAG, String.format("Widget %s : (%d X %d) can't fit on this device", item.componentName, minSpanX, minSpanY));
                }
                continue;
            }
        }
        if (mAppFilter == null) {
            mAppFilter = AppFilter.newInstance(app.getContext());
        }
        if (!mAppFilter.shouldShowApp(item.componentName)) {
            if (DEBUG) {
                Log.d(TAG, String.format("%s is filtered and not added to the widget tray.", item.componentName));
            }
            continue;
        }
        String packageName = item.componentName.getPackageName();
        PackageItemInfo pInfo = tmpPackageItemInfos.get(packageName);
        if (pInfo == null) {
            pInfo = new PackageItemInfo(packageName);
            pInfo.user = item.user;
            tmpPackageItemInfos.put(packageName, pInfo);
        } else if (!myUser.equals(pInfo.user)) {
            // Keep updating the user, until we get the primary user.
            pInfo.user = item.user;
        }
        mWidgetsList.addToList(pInfo, item);
    }
    // Update each package entry
    IconCache iconCache = app.getIconCache();
    for (PackageItemInfo p : tmpPackageItemInfos.values()) {
        iconCache.getTitleAndIconForApp(p, true);
    }
}
Also used : HashMap(java.util.HashMap) MultiHashMap(com.android.launcher3.util.MultiHashMap) InvariantDeviceProfile(com.android.launcher3.InvariantDeviceProfile) UserHandle(android.os.UserHandle) IconCache(com.android.launcher3.icons.IconCache) PackageItemInfo(com.android.launcher3.model.data.PackageItemInfo)

Example 35 with WIDGETS

use of com.android.launcher3.popup.SystemShortcut.WIDGETS in project android_packages_apps_Trebuchet by LineageOS.

the class WidgetsModel method update.

/**
 * @param packageUser If null, all widgets and shortcuts are updated and returned, otherwise
 *                    only widgets and shortcuts associated with the package/user are.
 */
public List<ComponentWithLabelAndIcon> update(LauncherAppState app, @Nullable PackageUserKey packageUser) {
    Preconditions.assertWorkerThread();
    Context context = app.getContext();
    final ArrayList<WidgetItem> widgetsAndShortcuts = new ArrayList<>();
    List<ComponentWithLabelAndIcon> updatedItems = new ArrayList<>();
    try {
        InvariantDeviceProfile idp = app.getInvariantDeviceProfile();
        PackageManager pm = app.getContext().getPackageManager();
        // Widgets
        WidgetManagerHelper widgetManager = new WidgetManagerHelper(context);
        for (AppWidgetProviderInfo widgetInfo : widgetManager.getAllProviders(packageUser)) {
            LauncherAppWidgetProviderInfo launcherWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(context, widgetInfo);
            widgetsAndShortcuts.add(new WidgetItem(launcherWidgetInfo, idp, app.getIconCache()));
            updatedItems.add(launcherWidgetInfo);
        }
        // Shortcuts
        for (ShortcutConfigActivityInfo info : queryList(context, packageUser)) {
            widgetsAndShortcuts.add(new WidgetItem(info, app.getIconCache(), pm));
            updatedItems.add(info);
        }
        setWidgetsAndShortcuts(widgetsAndShortcuts, app, packageUser);
    } catch (Exception e) {
        if (!FeatureFlags.IS_STUDIO_BUILD && Utilities.isBinderSizeError(e)) {
        // the returned value may be incomplete and will not be refreshed until the next
        // time Launcher starts.
        // TODO: after figuring out a repro step, introduce a dirty bit to check when
        // onResume is called to refresh the widget provider list.
        } else {
            throw e;
        }
    }
    app.getWidgetCache().removeObsoletePreviews(widgetsAndShortcuts, packageUser);
    return updatedItems;
}
Also used : Context(android.content.Context) LauncherAppWidgetProviderInfo(com.android.launcher3.LauncherAppWidgetProviderInfo) InvariantDeviceProfile(com.android.launcher3.InvariantDeviceProfile) ArrayList(java.util.ArrayList) PackageManager(android.content.pm.PackageManager) ComponentWithLabelAndIcon(com.android.launcher3.icons.ComponentWithLabelAndIcon) AppWidgetProviderInfo(android.appwidget.AppWidgetProviderInfo) LauncherAppWidgetProviderInfo(com.android.launcher3.LauncherAppWidgetProviderInfo) ShortcutConfigActivityInfo(com.android.launcher3.pm.ShortcutConfigActivityInfo) WidgetManagerHelper(com.android.launcher3.widget.WidgetManagerHelper)

Aggregations

ArrayList (java.util.ArrayList)64 Test (org.junit.Test)64 WidgetItem (com.android.launcher3.model.WidgetItem)54 WidgetsListContentEntry (com.android.launcher3.widget.model.WidgetsListContentEntry)54 PackageUserKey (com.android.launcher3.util.PackageUserKey)50 List (java.util.List)50 AppWidgetProviderInfo (android.appwidget.AppWidgetProviderInfo)46 SmallTest (androidx.test.filters.SmallTest)44 ComponentName (android.content.ComponentName)42 WidgetsListBaseEntry (com.android.launcher3.widget.model.WidgetsListBaseEntry)41 Point (android.graphics.Point)40 View (android.view.View)37 LauncherAppWidgetHostView (com.android.launcher3.widget.LauncherAppWidgetHostView)32 Collectors (java.util.stream.Collectors)31 Context (android.content.Context)29 IconCache (com.android.launcher3.icons.IconCache)29 LauncherAppWidgetProviderInfo (com.android.launcher3.widget.LauncherAppWidgetProviderInfo)29 WidgetsListHeaderEntry (com.android.launcher3.widget.model.WidgetsListHeaderEntry)29 WorkspaceItemInfo (com.android.launcher3.model.data.WorkspaceItemInfo)27 PendingAddWidgetInfo (com.android.launcher3.widget.PendingAddWidgetInfo)27