Merge "Use grid size as the upper bound for widgets' default size in initSpans" into sc-dev am: eb1617521c
Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Launcher3/+/14776609 Change-Id: Ic74b543e4fb94ab377472cb5d8e25fe449818081
This commit is contained in:
+74
@@ -40,6 +40,8 @@ import org.robolectric.RuntimeEnvironment;
|
|||||||
public final class LauncherAppWidgetProviderInfoTest {
|
public final class LauncherAppWidgetProviderInfoTest {
|
||||||
|
|
||||||
private static final int CELL_SIZE = 50;
|
private static final int CELL_SIZE = 50;
|
||||||
|
private static final int NUM_OF_COLS = 4;
|
||||||
|
private static final int NUM_OF_ROWS = 5;
|
||||||
|
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
|
|
||||||
@@ -75,6 +77,33 @@ public final class LauncherAppWidgetProviderInfoTest {
|
|||||||
assertThat(info.spanY).isEqualTo(2);
|
assertThat(info.spanY).isEqualTo(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void
|
||||||
|
initSpans_minWidthLargerThanGridColumns_shouldInitializeSpansToAtMostTheGridColumns() {
|
||||||
|
LauncherAppWidgetProviderInfo info = new LauncherAppWidgetProviderInfo();
|
||||||
|
info.minWidth = CELL_SIZE * (NUM_OF_COLS + 1);
|
||||||
|
info.minHeight = 20;
|
||||||
|
InvariantDeviceProfile idp = createIDP();
|
||||||
|
|
||||||
|
info.initSpans(mContext, idp);
|
||||||
|
|
||||||
|
assertThat(info.spanX).isEqualTo(NUM_OF_COLS);
|
||||||
|
assertThat(info.spanY).isEqualTo(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void initSpans_minHeightLargerThanGridRows_shouldInitializeSpansToAtMostTheGridRows() {
|
||||||
|
LauncherAppWidgetProviderInfo info = new LauncherAppWidgetProviderInfo();
|
||||||
|
info.minWidth = 20;
|
||||||
|
info.minHeight = 50 * (NUM_OF_ROWS + 1);
|
||||||
|
InvariantDeviceProfile idp = createIDP();
|
||||||
|
|
||||||
|
info.initSpans(mContext, idp);
|
||||||
|
|
||||||
|
assertThat(info.spanX).isEqualTo(1);
|
||||||
|
assertThat(info.spanY).isEqualTo(NUM_OF_ROWS);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void initSpans_minResizeWidthUnspecified_shouldInitializeMinSpansToOne() {
|
public void initSpans_minResizeWidthUnspecified_shouldInitializeMinSpansToOne() {
|
||||||
LauncherAppWidgetProviderInfo info = new LauncherAppWidgetProviderInfo();
|
LauncherAppWidgetProviderInfo info = new LauncherAppWidgetProviderInfo();
|
||||||
@@ -153,6 +182,49 @@ public final class LauncherAppWidgetProviderInfoTest {
|
|||||||
assertThat(info.minSpanY).isEqualTo(3);
|
assertThat(info.minSpanY).isEqualTo(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isMinSizeFulfilled_minWidthAndHeightWithinGridSize_shouldReturnTrue() {
|
||||||
|
LauncherAppWidgetProviderInfo info = new LauncherAppWidgetProviderInfo();
|
||||||
|
info.minWidth = 80;
|
||||||
|
info.minHeight = 80;
|
||||||
|
info.minResizeWidth = 50;
|
||||||
|
info.minResizeHeight = 50;
|
||||||
|
InvariantDeviceProfile idp = createIDP();
|
||||||
|
|
||||||
|
info.initSpans(mContext, idp);
|
||||||
|
|
||||||
|
assertThat(info.isMinSizeFulfilled()).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void
|
||||||
|
isMinSizeFulfilled_minWidthAndMinResizeWidthExceededGridColumns_shouldReturnFalse() {
|
||||||
|
LauncherAppWidgetProviderInfo info = new LauncherAppWidgetProviderInfo();
|
||||||
|
info.minWidth = CELL_SIZE * (NUM_OF_COLS + 2);
|
||||||
|
info.minHeight = 80;
|
||||||
|
info.minResizeWidth = CELL_SIZE * (NUM_OF_COLS + 1);
|
||||||
|
info.minResizeHeight = 50;
|
||||||
|
InvariantDeviceProfile idp = createIDP();
|
||||||
|
|
||||||
|
info.initSpans(mContext, idp);
|
||||||
|
|
||||||
|
assertThat(info.isMinSizeFulfilled()).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isMinSizeFulfilled_minHeightAndMinResizeHeightExceededGridRows_shouldReturnFalse() {
|
||||||
|
LauncherAppWidgetProviderInfo info = new LauncherAppWidgetProviderInfo();
|
||||||
|
info.minWidth = 80;
|
||||||
|
info.minHeight = CELL_SIZE * (NUM_OF_ROWS + 2);
|
||||||
|
info.minResizeWidth = 50;
|
||||||
|
info.minResizeHeight = CELL_SIZE * (NUM_OF_ROWS + 1);
|
||||||
|
InvariantDeviceProfile idp = createIDP();
|
||||||
|
|
||||||
|
info.initSpans(mContext, idp);
|
||||||
|
|
||||||
|
assertThat(info.isMinSizeFulfilled()).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
private InvariantDeviceProfile createIDP() {
|
private InvariantDeviceProfile createIDP() {
|
||||||
DeviceProfile profile = Mockito.mock(DeviceProfile.class);
|
DeviceProfile profile = Mockito.mock(DeviceProfile.class);
|
||||||
doAnswer(i -> {
|
doAnswer(i -> {
|
||||||
@@ -163,6 +235,8 @@ public final class LauncherAppWidgetProviderInfoTest {
|
|||||||
|
|
||||||
InvariantDeviceProfile idp = new InvariantDeviceProfile();
|
InvariantDeviceProfile idp = new InvariantDeviceProfile();
|
||||||
idp.supportedProfiles.add(profile);
|
idp.supportedProfiles.add(profile);
|
||||||
|
idp.numColumns = NUM_OF_COLS;
|
||||||
|
idp.numRows = NUM_OF_ROWS;
|
||||||
return idp;
|
return idp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,13 +32,44 @@ public class LauncherAppWidgetProviderInfo extends AppWidgetProviderInfo
|
|||||||
|
|
||||||
public static final String CLS_CUSTOM_WIDGET_PREFIX = "#custom-widget-";
|
public static final String CLS_CUSTOM_WIDGET_PREFIX = "#custom-widget-";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The desired number of cells that this widget occupies horizontally in
|
||||||
|
* {@link com.android.launcher3.CellLayout}.
|
||||||
|
*/
|
||||||
public int spanX;
|
public int spanX;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The desired number of cells that this widget occupies vertically in
|
||||||
|
* {@link com.android.launcher3.CellLayout}.
|
||||||
|
*/
|
||||||
public int spanY;
|
public int spanY;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The minimum number of cells that this widget can occupy horizontally in
|
||||||
|
* {@link com.android.launcher3.CellLayout}.
|
||||||
|
*/
|
||||||
public int minSpanX;
|
public int minSpanX;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The minimum number of cells that this widget can occupy vertically in
|
||||||
|
* {@link com.android.launcher3.CellLayout}.
|
||||||
|
*/
|
||||||
public int minSpanY;
|
public int minSpanY;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The maximum number of cells that this widget can occupy horizontally in
|
||||||
|
* {@link com.android.launcher3.CellLayout}.
|
||||||
|
*/
|
||||||
public int maxSpanX;
|
public int maxSpanX;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The maximum number of cells that this widget can occupy vertically in
|
||||||
|
* {@link com.android.launcher3.CellLayout}.
|
||||||
|
*/
|
||||||
public int maxSpanY;
|
public int maxSpanY;
|
||||||
|
|
||||||
|
private boolean mIsMinSizeFulfilled;
|
||||||
|
|
||||||
public static LauncherAppWidgetProviderInfo fromProviderInfo(Context context,
|
public static LauncherAppWidgetProviderInfo fromProviderInfo(Context context,
|
||||||
AppWidgetProviderInfo info) {
|
AppWidgetProviderInfo info) {
|
||||||
final LauncherAppWidgetProviderInfo launcherInfo;
|
final LauncherAppWidgetProviderInfo launcherInfo;
|
||||||
@@ -133,8 +164,20 @@ public class LauncherAppWidgetProviderInfo extends AppWidgetProviderInfo
|
|||||||
this.minSpanY = minSpanY;
|
this.minSpanY = minSpanY;
|
||||||
this.maxSpanX = maxSpanX;
|
this.maxSpanX = maxSpanX;
|
||||||
this.maxSpanY = maxSpanY;
|
this.maxSpanY = maxSpanY;
|
||||||
this.spanX = spanX;
|
this.mIsMinSizeFulfilled = Math.min(spanX, minSpanX) <= idp.numColumns
|
||||||
this.spanY = spanY;
|
&& Math.min(spanY, minSpanY) <= idp.numRows;
|
||||||
|
// Ensures the default span X and span Y will not exceed the current grid size.
|
||||||
|
this.spanX = Math.min(spanX, idp.numColumns);
|
||||||
|
this.spanY = Math.min(spanY, idp.numRows);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns {@code true} if the widget's minimum size requirement can be fulfilled in the device
|
||||||
|
* grid setting, {@link InvariantDeviceProfile}, that was passed in
|
||||||
|
* {@link #initSpans(Context, InvariantDeviceProfile)}.
|
||||||
|
*/
|
||||||
|
public boolean isMinSizeFulfilled() {
|
||||||
|
return mIsMinSizeFulfilled;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getSpanX(Rect widgetPadding, int widgetWidth, int cellSpacing, float cellWidth) {
|
private int getSpanX(Rect widgetPadding, int widgetWidth, int cellSpacing, float cellWidth) {
|
||||||
|
|||||||
@@ -254,13 +254,11 @@ public class WidgetsModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Ensure that all widgets we show can be added on a workspace of this size
|
// 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);
|
if (!item.widgetInfo.isMinSizeFulfilled()) {
|
||||||
int minSpanY = Math.min(item.widgetInfo.spanY, item.widgetInfo.minSpanY);
|
|
||||||
if (minSpanX > mIdp.numColumns || minSpanY > mIdp.numRows) {
|
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
Log.d(TAG, String.format(
|
Log.d(TAG, String.format(
|
||||||
"Widget %s : (%d X %d) can't fit on this device",
|
"Widget %s : can't fit on this device with a grid size: %dx%d",
|
||||||
item.componentName, minSpanX, minSpanY));
|
item.componentName, mIdp.numColumns, mIdp.numRows));
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user