Search in sources :

Example 6 with WidgetItem

use of com.android.launcher3.model.WidgetItem 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 7 with WidgetItem

use of com.android.launcher3.model.WidgetItem 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 8 with WidgetItem

use of com.android.launcher3.model.WidgetItem in project android_packages_apps_Launcher3 by crdroidandroid.

the class WidgetsRecommendationTableLayout method bindData.

private void bindData(RecommendationTableData data) {
    if (data.mRecommendationTable.size() == 0) {
        setVisibility(GONE);
        return;
    }
    removeAllViews();
    for (int i = 0; i < data.mRecommendationTable.size(); i++) {
        List<WidgetItem> widgetItems = data.mRecommendationTable.get(i);
        TableRow tableRow = new TableRow(getContext());
        tableRow.setGravity(Gravity.TOP);
        for (WidgetItem widgetItem : widgetItems) {
            WidgetCell widgetCell = addItemCell(tableRow);
            widgetCell.setPreviewSize(widgetItem, data.mPreviewScale);
            widgetCell.applyFromCellItem(widgetItem, LauncherAppState.getInstance(getContext()).getWidgetCache());
            widgetCell.ensurePreview();
        }
        addView(tableRow);
    }
    setVisibility(VISIBLE);
}
Also used : TableRow(android.widget.TableRow) WidgetItem(com.android.launcher3.model.WidgetItem) WidgetCell(com.android.launcher3.widget.WidgetCell)

Example 9 with WidgetItem

use of com.android.launcher3.model.WidgetItem in project android_packages_apps_Launcher3 by crdroidandroid.

the class CachingWidgetPreviewLoaderTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    mLoader = new CachingWidgetPreviewLoader(mDelegate);
    mTestActivity = Robolectric.buildActivity(TestActivity.class).setup().get();
    mTestActivity.setDeviceProfile(mDeviceProfile);
    when(mDelegate.loadPreview(any(), any(), any(), any())).thenReturn(mCancellationSignal);
    mProviderInfo.provider = TEST_PROVIDER;
    when(mProviderInfo.getProfile()).thenReturn(new UserHandle(0));
    mProviderInfo2.provider = TEST_PROVIDER2;
    when(mProviderInfo2.getProfile()).thenReturn(new UserHandle(0));
    InvariantDeviceProfile testProfile = new InvariantDeviceProfile();
    testProfile.numRows = 5;
    testProfile.numColumns = 5;
    mWidgetItem = new WidgetItem(mProviderInfo, testProfile, mIconCache);
    mWidgetItem2 = new WidgetItem(mProviderInfo2, testProfile, mIconCache);
}
Also used : UserHandle(android.os.UserHandle) InvariantDeviceProfile(com.android.launcher3.InvariantDeviceProfile) TestActivity(com.android.launcher3.testing.TestActivity) WidgetItem(com.android.launcher3.model.WidgetItem) Before(org.junit.Before)

Example 10 with WidgetItem

use of com.android.launcher3.model.WidgetItem in project android_packages_apps_Launcher3 by crdroidandroid.

the class WidgetsDiffReporterTest method generateWidgetItems.

private List<WidgetItem> generateWidgetItems(String packageName, int numOfWidgets) {
    ShadowPackageManager packageManager = shadowOf(mContext.getPackageManager());
    ArrayList<WidgetItem> widgetItems = new ArrayList<>();
    for (int i = 0; i < numOfWidgets; i++) {
        ComponentName cn = ComponentName.createRelative(packageName, ".SampleWidget" + i);
        AppWidgetProviderInfo widgetInfo = new AppWidgetProviderInfo();
        widgetInfo.provider = cn;
        ReflectionHelpers.setField(widgetInfo, "providerInfo", packageManager.addReceiverIfNotPresent(cn));
        WidgetItem widgetItem = new WidgetItem(LauncherAppWidgetProviderInfo.fromProviderInfo(mContext, widgetInfo), mTestProfile, mIconCache);
        widgetItems.add(widgetItem);
    }
    return widgetItems;
}
Also used : ShadowPackageManager(org.robolectric.shadows.ShadowPackageManager) ArrayList(java.util.ArrayList) AppWidgetProviderInfo(android.appwidget.AppWidgetProviderInfo) LauncherAppWidgetProviderInfo(com.android.launcher3.widget.LauncherAppWidgetProviderInfo) WidgetItem(com.android.launcher3.model.WidgetItem) ComponentName(android.content.ComponentName)

Aggregations

WidgetItem (com.android.launcher3.model.WidgetItem)37 WidgetsListContentEntry (com.android.launcher3.widget.model.WidgetsListContentEntry)17 ArrayList (java.util.ArrayList)15 LauncherAppWidgetProviderInfo (com.android.launcher3.widget.LauncherAppWidgetProviderInfo)11 AppWidgetProviderInfo (android.appwidget.AppWidgetProviderInfo)10 ComponentName (android.content.ComponentName)9 Test (org.junit.Test)9 ShadowPackageManager (org.robolectric.shadows.ShadowPackageManager)9 PackageItemInfo (com.android.launcher3.model.data.PackageItemInfo)8 WidgetsListBaseEntry (com.android.launcher3.widget.model.WidgetsListBaseEntry)7 Size (android.util.Size)6 WidgetsListHeaderEntry (com.android.launcher3.widget.model.WidgetsListHeaderEntry)6 List (java.util.List)6 Collectors (java.util.stream.Collectors)5 TableRow (android.widget.TableRow)4 PackageUserKey (com.android.launcher3.util.PackageUserKey)4 PackageManager (android.content.pm.PackageManager)3 Nullable (androidx.annotation.Nullable)3 DeviceProfile (com.android.launcher3.DeviceProfile)3 InvariantDeviceProfile (com.android.launcher3.InvariantDeviceProfile)3