2022-06-13 14:38:43 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2022 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
package com.android.launcher3.celllayout;
|
|
|
|
|
|
2023-05-25 19:20:10 -07:00
|
|
|
import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
|
|
|
|
|
|
|
|
|
|
import static com.android.launcher3.ui.TestViewHelpers.findWidgetProvider;
|
2022-06-13 14:38:43 -07:00
|
|
|
import static com.android.launcher3.util.WidgetUtils.createWidgetInfo;
|
|
|
|
|
|
|
|
|
|
import android.content.ComponentName;
|
2022-06-13 14:38:43 -07:00
|
|
|
import android.content.ContentResolver;
|
|
|
|
|
import android.content.Context;
|
2022-06-13 14:38:43 -07:00
|
|
|
import android.graphics.Rect;
|
|
|
|
|
import android.os.Process;
|
|
|
|
|
import android.os.UserHandle;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
2022-06-13 14:38:43 -07:00
|
|
|
import com.android.launcher3.InvariantDeviceProfile;
|
2022-06-13 14:38:43 -07:00
|
|
|
import com.android.launcher3.LauncherSettings;
|
|
|
|
|
import com.android.launcher3.model.data.AppInfo;
|
2022-06-13 14:38:43 -07:00
|
|
|
import com.android.launcher3.model.data.ItemInfo;
|
2022-06-13 14:38:43 -07:00
|
|
|
import com.android.launcher3.model.data.LauncherAppWidgetInfo;
|
|
|
|
|
import com.android.launcher3.model.data.WorkspaceItemInfo;
|
|
|
|
|
import com.android.launcher3.widget.LauncherAppWidgetProviderInfo;
|
|
|
|
|
|
2023-05-25 19:20:10 -07:00
|
|
|
import java.util.function.Supplier;
|
|
|
|
|
import java.util.stream.IntStream;
|
|
|
|
|
|
2022-06-13 14:38:43 -07:00
|
|
|
public class TestWorkspaceBuilder {
|
|
|
|
|
|
2022-06-13 14:38:43 -07:00
|
|
|
private static final String TAG = "CellLayoutBoardBuilder";
|
2022-06-13 14:38:43 -07:00
|
|
|
private static final ComponentName APP_COMPONENT_NAME = new ComponentName(
|
|
|
|
|
"com.google.android.calculator", "com.android.calculator2.Calculator");
|
|
|
|
|
|
|
|
|
|
private UserHandle mMyUser;
|
|
|
|
|
|
2022-06-13 14:38:43 -07:00
|
|
|
private Context mContext;
|
|
|
|
|
private ContentResolver mResolver;
|
|
|
|
|
|
2023-05-25 19:20:10 -07:00
|
|
|
public TestWorkspaceBuilder(Context context) {
|
2022-06-13 14:38:43 -07:00
|
|
|
mMyUser = Process.myUserHandle();
|
2022-06-13 14:38:43 -07:00
|
|
|
mContext = context;
|
|
|
|
|
mResolver = mContext.getContentResolver();
|
2022-06-13 14:38:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fills the given rect in WidgetRect with 1x1 widgets. This is useful to equalize cases.
|
|
|
|
|
*/
|
2022-06-13 14:38:43 -07:00
|
|
|
private FavoriteItemsTransaction fillWithWidgets(CellLayoutBoard.WidgetRect widgetRect,
|
2022-12-08 11:08:00 -08:00
|
|
|
FavoriteItemsTransaction transaction, int screenId) {
|
2022-06-13 14:38:43 -07:00
|
|
|
int initX = widgetRect.getCellX();
|
|
|
|
|
int initY = widgetRect.getCellY();
|
|
|
|
|
for (int x = initX; x < initX + widgetRect.getSpanX(); x++) {
|
|
|
|
|
for (int y = initY; y < initY + widgetRect.getSpanY(); y++) {
|
|
|
|
|
try {
|
|
|
|
|
// this widgets are filling, we don't care if we can't place them
|
2023-05-25 19:20:10 -07:00
|
|
|
transaction.addItem(createWidgetInCell(
|
2022-06-13 14:38:43 -07:00
|
|
|
new CellLayoutBoard.WidgetRect(CellLayoutBoard.CellType.IGNORE,
|
2023-05-25 19:20:10 -07:00
|
|
|
new Rect(x, y, x, y)), screenId));
|
2022-06-13 14:38:43 -07:00
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.d(TAG, "Unable to place filling widget at " + x + "," + y);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-13 14:38:43 -07:00
|
|
|
return transaction;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private AppInfo getApp() {
|
|
|
|
|
return new AppInfo(APP_COMPONENT_NAME, "test icon", mMyUser,
|
|
|
|
|
AppInfo.makeLaunchIntent(APP_COMPONENT_NAME));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void addCorrespondingWidgetRect(CellLayoutBoard.WidgetRect widgetRect,
|
2022-12-08 11:08:00 -08:00
|
|
|
FavoriteItemsTransaction transaction, int screenId) {
|
2022-06-13 14:38:43 -07:00
|
|
|
if (widgetRect.mType == 'x') {
|
2022-12-08 11:08:00 -08:00
|
|
|
fillWithWidgets(widgetRect, transaction, screenId);
|
2022-06-13 14:38:43 -07:00
|
|
|
} else {
|
2022-12-08 11:08:00 -08:00
|
|
|
transaction.addItem(createWidgetInCell(widgetRect, screenId));
|
2022-06-13 14:38:43 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Builds the given board into the transaction
|
|
|
|
|
*/
|
|
|
|
|
public FavoriteItemsTransaction buildFromBoard(CellLayoutBoard board,
|
2022-12-08 11:08:00 -08:00
|
|
|
FavoriteItemsTransaction transaction, final int screenId) {
|
2022-06-13 14:38:43 -07:00
|
|
|
board.getWidgets().forEach(
|
2022-12-08 11:08:00 -08:00
|
|
|
(widgetRect) -> addCorrespondingWidgetRect(widgetRect, transaction, screenId));
|
2022-06-13 14:38:43 -07:00
|
|
|
board.getIcons().forEach((iconPoint) ->
|
2023-05-25 19:20:10 -07:00
|
|
|
transaction.addItem(() -> createIconInCell(iconPoint, screenId))
|
2022-06-13 14:38:43 -07:00
|
|
|
);
|
|
|
|
|
return transaction;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fills the hotseat row with apps instead of suggestions, for this to work the workspace should
|
|
|
|
|
* be clean otherwise this doesn't overrides the existing icons.
|
|
|
|
|
*/
|
|
|
|
|
public FavoriteItemsTransaction fillHotseatIcons(FavoriteItemsTransaction transaction) {
|
2023-05-25 19:20:10 -07:00
|
|
|
IntStream.range(0, InvariantDeviceProfile.INSTANCE.get(mContext).numDatabaseHotseatIcons)
|
|
|
|
|
.forEach(i -> transaction.addItem(() -> getHotseatValues(i)));
|
2022-06-13 14:38:43 -07:00
|
|
|
return transaction;
|
2022-06-13 14:38:43 -07:00
|
|
|
}
|
|
|
|
|
|
2023-05-25 19:20:10 -07:00
|
|
|
private Supplier<ItemInfo> createWidgetInCell(
|
|
|
|
|
CellLayoutBoard.WidgetRect widgetRect, int screenId) {
|
|
|
|
|
// Create the widget lazily since the appWidgetId can get lost during setup
|
|
|
|
|
return () -> {
|
|
|
|
|
LauncherAppWidgetProviderInfo info = findWidgetProvider(false);
|
|
|
|
|
LauncherAppWidgetInfo item = createWidgetInfo(info, getApplicationContext(), true);
|
|
|
|
|
item.cellX = widgetRect.getCellX();
|
|
|
|
|
item.cellY = widgetRect.getCellY();
|
|
|
|
|
item.spanX = widgetRect.getSpanX();
|
|
|
|
|
item.spanY = widgetRect.getSpanY();
|
|
|
|
|
item.screenId = screenId;
|
|
|
|
|
return item;
|
|
|
|
|
};
|
2022-06-13 14:38:43 -07:00
|
|
|
}
|
|
|
|
|
|
2022-12-08 11:08:00 -08:00
|
|
|
private ItemInfo createIconInCell(CellLayoutBoard.IconPoint iconPoint, int screenId) {
|
2022-06-13 14:38:43 -07:00
|
|
|
WorkspaceItemInfo item = new WorkspaceItemInfo(getApp());
|
2022-12-08 11:08:00 -08:00
|
|
|
item.screenId = screenId;
|
2022-06-13 14:38:43 -07:00
|
|
|
item.cellX = iconPoint.getCoord().x;
|
|
|
|
|
item.cellY = iconPoint.getCoord().y;
|
|
|
|
|
item.minSpanY = item.minSpanX = item.spanX = item.spanY = 1;
|
|
|
|
|
item.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
|
|
|
|
|
return item;
|
2022-06-13 14:38:43 -07:00
|
|
|
}
|
|
|
|
|
|
2022-06-13 14:38:43 -07:00
|
|
|
private ItemInfo getHotseatValues(int x) {
|
|
|
|
|
WorkspaceItemInfo item = new WorkspaceItemInfo(getApp());
|
|
|
|
|
item.cellX = x;
|
|
|
|
|
item.cellY = 0;
|
|
|
|
|
item.minSpanY = item.minSpanX = item.spanX = item.spanY = 1;
|
|
|
|
|
item.rank = x;
|
|
|
|
|
item.screenId = x;
|
|
|
|
|
item.container = LauncherSettings.Favorites.CONTAINER_HOTSEAT;
|
|
|
|
|
return item;
|
2022-06-13 14:38:43 -07:00
|
|
|
}
|
|
|
|
|
}
|