Search in sources :

Example 21 with Widget

use of com.android.launcher3.tapl.Widget in project android_packages_apps_Launcher3 by crdroidandroid.

the class WidgetsTableUtils method groupWidgetItemsIntoTable.

/**
 * Groups widgets items into a 2D array which matches their appearance in a UI table.
 *
 * <p>Grouping:
 * 1. Widgets and shortcuts never group together in the same row.
 * 2. The ordered widgets are grouped together in the same row until their total horizontal
 *    spans exceed the {@code maxSpansPerRow} - 1.
 * 3. The order shortcuts are grouped together in the same row until their total horizontal
 *    spans exceed the {@code maxSpansPerRow} - 1.
 * 4. If there is only one widget in a row, its width may exceed the {@code maxSpansPerRow}.
 *
 * <p>Let's say the {@code maxSpansPerRow} is set to 6. Widgets can be grouped in the same row
 * if their total horizontal spans added don't exceed 5.
 * Example 1: Row 1: 2x2, 2x3, 1x1. Total horizontal spans is 5. This is okay.
 * Example 2: Row 1: 2x2, 4x3, 1x1. the total horizontal spans is 7. This is wrong. 4x3 and 1x1
 * should be moved to a new row.
 * Example 3: Row 1: 6x4. This is okay because this is the only item in the row.
 */
public static List<ArrayList<WidgetItem>> groupWidgetItemsIntoTable(List<WidgetItem> widgetItems, final int maxSpansPerRow) {
    List<WidgetItem> sortedWidgetItems = widgetItems.stream().sorted(WIDGET_SHORTCUT_COMPARATOR).collect(Collectors.toList());
    List<ArrayList<WidgetItem>> widgetItemsTable = new ArrayList<>();
    ArrayList<WidgetItem> widgetItemsAtRow = null;
    for (WidgetItem widgetItem : sortedWidgetItems) {
        if (widgetItemsAtRow == null) {
            widgetItemsAtRow = new ArrayList<>();
            widgetItemsTable.add(widgetItemsAtRow);
        }
        int numOfWidgetItems = widgetItemsAtRow.size();
        int totalHorizontalSpan = widgetItemsAtRow.stream().map(item -> item.spanX).reduce(/* default= */
        0, Integer::sum);
        int totalHorizontalSpanAfterAddingWidget = widgetItem.spanX + totalHorizontalSpan;
        if (numOfWidgetItems == 0) {
            widgetItemsAtRow.add(widgetItem);
        } else if (// widget's description.
        totalHorizontalSpanAfterAddingWidget <= maxSpansPerRow - 1 && widgetItem.hasSameType(widgetItemsAtRow.get(numOfWidgetItems - 1))) {
            // Group items in the same row if
            // 1. they are with the same type, i.e. a row can only have widgets or shortcuts but
            // never a mix of both.
            // 2. the total number of horizontal spans are smaller than or equal to
            // MAX_SPAN_PER_ROW. If an item has a horizontal span > MAX_SPAN_PER_ROW, we just
            // place it in its own row regardless of the horizontal span limit.
            widgetItemsAtRow.add(widgetItem);
        } else {
            widgetItemsAtRow = new ArrayList<>();
            widgetItemsTable.add(widgetItemsAtRow);
            widgetItemsAtRow.add(widgetItem);
        }
    }
    return widgetItemsTable;
}
Also used : List(java.util.List) WidgetItem(com.android.launcher3.model.WidgetItem) Comparator(java.util.Comparator) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) WidgetItem(com.android.launcher3.model.WidgetItem)

Example 22 with Widget

use of com.android.launcher3.tapl.Widget in project android_packages_apps_Launcher3 by crdroidandroid.

the class WidgetsBottomSheet method onWidgetsBound.

@Override
public void onWidgetsBound() {
    List<WidgetItem> widgets = mActivityContext.getPopupDataProvider().getWidgetsForPackageUser(new PackageUserKey(mOriginalItemInfo.getTargetComponent().getPackageName(), mOriginalItemInfo.user));
    TableLayout widgetsTable = findViewById(R.id.widgets_table);
    widgetsTable.removeAllViews();
    WidgetsTableUtils.groupWidgetItemsIntoTable(widgets, mMaxHorizontalSpan).forEach(row -> {
        TableRow tableRow = new TableRow(getContext());
        tableRow.setGravity(Gravity.TOP);
        row.forEach(widgetItem -> {
            WidgetCell widget = addItemCell(tableRow);
            widget.setPreviewSize(widgetItem);
            widget.applyFromCellItem(widgetItem, LauncherAppState.getInstance(mActivityContext).getWidgetCache());
            widget.ensurePreview();
            widget.setVisibility(View.VISIBLE);
        });
        widgetsTable.addView(tableRow);
    });
}
Also used : TableRow(android.widget.TableRow) WidgetItem(com.android.launcher3.model.WidgetItem) PackageUserKey(com.android.launcher3.util.PackageUserKey) TableLayout(android.widget.TableLayout)

Example 23 with Widget

use of com.android.launcher3.tapl.Widget in project android_packages_apps_Launcher3 by crdroidandroid.

the class PackageInstallStateChangedTask method execute.

@Override
public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
    if (mInstallInfo.state == PackageInstallInfo.STATUS_INSTALLED) {
        try {
            // For instant apps we do not get package-add. Use setting events to update
            // any pinned icons.
            ApplicationInfo ai = app.getContext().getPackageManager().getApplicationInfo(mInstallInfo.packageName, 0);
            if (InstantAppResolver.newInstance(app.getContext()).isInstantApp(ai)) {
                app.getModel().onPackageAdded(ai.packageName, mInstallInfo.user);
            }
        } catch (PackageManager.NameNotFoundException e) {
        // Ignore
        }
        // Ignore install success events as they are handled by Package add events.
        return;
    }
    synchronized (apps) {
        List<AppInfo> updatedAppInfos = apps.updatePromiseInstallInfo(mInstallInfo);
        if (!updatedAppInfos.isEmpty()) {
            for (AppInfo appInfo : updatedAppInfos) {
                scheduleCallbackTask(c -> c.bindIncrementalDownloadProgressUpdated(appInfo));
            }
        }
        bindApplicationsIfNeeded();
    }
    synchronized (dataModel) {
        final HashSet<ItemInfo> updates = new HashSet<>();
        dataModel.forAllWorkspaceItemInfos(mInstallInfo.user, si -> {
            if (si.hasPromiseIconUi() && mInstallInfo.packageName.equals(si.getTargetPackage())) {
                si.setProgressLevel(mInstallInfo);
                updates.add(si);
            }
        });
        for (LauncherAppWidgetInfo widget : dataModel.appWidgets) {
            if (widget.providerName.getPackageName().equals(mInstallInfo.packageName)) {
                widget.installProgress = mInstallInfo.progress;
                updates.add(widget);
            }
        }
        if (!updates.isEmpty()) {
            scheduleCallbackTask(callbacks -> callbacks.bindRestoreItemsChange(updates));
        }
    }
}
Also used : PackageManager(android.content.pm.PackageManager) ItemInfo(com.android.launcher3.model.data.ItemInfo) ApplicationInfo(android.content.pm.ApplicationInfo) LauncherAppWidgetInfo(com.android.launcher3.model.data.LauncherAppWidgetInfo) AppInfo(com.android.launcher3.model.data.AppInfo) HashSet(java.util.HashSet)

Example 24 with Widget

use of com.android.launcher3.tapl.Widget in project android_packages_apps_Launcher3 by crdroidandroid.

the class CellLayout method applyColorExtractionOnWidget.

/**
 * Applies the local color extraction to a dragging widget object.
 */
private void applyColorExtractionOnWidget(DropTarget.DragObject dragObject, int[] targetCell, int spanX, int spanY) {
    // Apply local extracted color if the DragView is an AppWidgetHostViewDrawable.
    View view = dragObject.dragView.getContentView();
    if (view instanceof LauncherAppWidgetHostView) {
        Launcher launcher = Launcher.getLauncher(dragObject.dragView.getContext());
        Workspace workspace = launcher.getWorkspace();
        int screenId = workspace.getIdForScreen(this);
        int pageId = workspace.getPageIndexForScreenId(screenId);
        cellToRect(targetCell[0], targetCell[1], spanX, spanY, mTempRect);
        // Now get the rect in drag layer coordinates.
        getBoundsForViewInDragLayer(launcher.getDragLayer(), this, mTempRect, true, mTmpFloatArray, mTempRectF);
        Utilities.setRect(mTempRectF, mTempRect);
        ((LauncherAppWidgetHostView) view).handleDrag(mTempRect, pageId);
    }
}
Also used : LauncherAppWidgetHostView(com.android.launcher3.widget.LauncherAppWidgetHostView) View(android.view.View) LauncherAppWidgetHostView(com.android.launcher3.widget.LauncherAppWidgetHostView) Paint(android.graphics.Paint) SuppressLint(android.annotation.SuppressLint) Point(android.graphics.Point)

Example 25 with Widget

use of com.android.launcher3.tapl.Widget in project android_packages_apps_Launcher3 by crdroidandroid.

the class WidgetsListAdapterTest method setWidgets_expandedApp_moreWidgets_shouldNotifyItemChangedWithWidgetItemInfoDiff.

@Test
public void setWidgets_expandedApp_moreWidgets_shouldNotifyItemChangedWithWidgetItemInfoDiff() {
    // GIVEN the adapter was first populated with com.google.test0 & com.google.test1. Each app
    // has one widget.
    ArrayList<WidgetsListBaseEntry> allEntries = generateSampleMap(2);
    mAdapter.setWidgets(allEntries);
    // GIVEN test com.google.test1 is expanded.
    // Visible entries in the adapter are:
    // [com.google.test0, com.google.test1, com.google.test1 content]
    mAdapter.onHeaderClicked(/* showWidgets= */
    true, new PackageUserKey(TEST_PACKAGE_PLACEHOLDER + 1, mUserHandle));
    Mockito.reset(mListener);
    // WHEN the adapter is updated with the same list of apps but com.google.test1 has 2 widgets
    // now.
    WidgetsListContentEntry testPackage1ContentEntry = (WidgetsListContentEntry) allEntries.get(3);
    WidgetItem widgetItem = testPackage1ContentEntry.mWidgets.get(0);
    WidgetsListContentEntry newTestPackage1ContentEntry = new WidgetsListContentEntry(testPackage1ContentEntry.mPkgItem, testPackage1ContentEntry.mTitleSectionName, List.of(widgetItem, widgetItem));
    allEntries.set(3, newTestPackage1ContentEntry);
    mAdapter.setWidgets(allEntries);
    // THEN the onItemRangeChanged is invoked for "com.google.test1 content" at index 2.
    verify(mListener).onItemRangeChanged(eq(2), eq(1), isNull());
}
Also used : WidgetsListBaseEntry(com.android.launcher3.widget.model.WidgetsListBaseEntry) WidgetsListContentEntry(com.android.launcher3.widget.model.WidgetsListContentEntry) PackageUserKey(com.android.launcher3.util.PackageUserKey) WidgetItem(com.android.launcher3.model.WidgetItem) Test(org.junit.Test)

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