From b9806031ae8f06eaad042d53efb4f131c1dcf2cf Mon Sep 17 00:00:00 2001 From: TreeHugger Robot Date: Tue, 9 Feb 2021 21:53:18 +0000 Subject: [PATCH] Add new attributes for widget sizing controls. Design doc: go/widget-sizing-api-dd New attributes added: * maxResizeWidth * maxResizeHeight * targetCellWidth * targetCellHeight Bug: 179807199 Test: atest FrameworksServicesTests:AppWidgetServiceImplTest Change-Id: Icadc73da9bd010e42459735493f452cb892e5744 --- core/api/current.txt | 8 +++ .../appwidget/AppWidgetProviderInfo.java | 54 ++++++++++++++++++- core/res/res/values/attrs.xml | 8 +++ core/res/res/values/public.xml | 4 ++ .../appwidget/AppWidgetServiceImpl.java | 14 +++++ .../res/values/{strings.xml => values.xml} | 8 +++ .../res/xml/dummy_appwidget_info.xml | 12 +++-- .../appwidget/AppWidgetServiceImplTest.java | 28 ++++++++++ 8 files changed, 132 insertions(+), 4 deletions(-) rename services/tests/servicestests/res/values/{strings.xml => values.xml} (83%) diff --git a/core/api/current.txt b/core/api/current.txt index a280557fc09e0..65a1790a519f6 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -960,6 +960,8 @@ package android { field public static final int maxLines = 16843091; // 0x1010153 field public static final int maxLongVersionCode = 16844163; // 0x1010583 field public static final int maxRecents = 16843846; // 0x1010446 + field public static final int maxResizeHeight = 16844339; // 0x1010633 + field public static final int maxResizeWidth = 16844338; // 0x1010632 field public static final int maxRows = 16843059; // 0x1010133 field public static final int maxSdkVersion = 16843377; // 0x1010271 field public static final int maxWidth = 16843039; // 0x101011f @@ -1394,6 +1396,8 @@ package android { field public static final int tabWidgetStyle = 16842883; // 0x1010083 field public static final int tag = 16842961; // 0x10100d1 field public static final int targetActivity = 16843266; // 0x1010202 + field public static final int targetCellHeight = 16844341; // 0x1010635 + field public static final int targetCellWidth = 16844340; // 0x1010634 field public static final int targetClass = 16842799; // 0x101002f field @Deprecated public static final int targetDescriptions = 16843680; // 0x10103a0 field public static final int targetId = 16843740; // 0x10103dc @@ -8421,6 +8425,8 @@ package android.appwidget { field public int initialKeyguardLayout; field public int initialLayout; field @Deprecated public String label; + field public int maxResizeHeight; + field public int maxResizeWidth; field public int minHeight; field public int minResizeHeight; field public int minResizeWidth; @@ -8429,6 +8435,8 @@ package android.appwidget { field @IdRes public int previewLayout; field public android.content.ComponentName provider; field public int resizeMode; + field public int targetCellHeight; + field public int targetCellWidth; field public int updatePeriodMillis; field public int widgetCategory; field public int widgetFeatures; diff --git a/core/java/android/appwidget/AppWidgetProviderInfo.java b/core/java/android/appwidget/AppWidgetProviderInfo.java index 42214d0477405..d893a5e49aa9f 100644 --- a/core/java/android/appwidget/AppWidgetProviderInfo.java +++ b/core/java/android/appwidget/AppWidgetProviderInfo.java @@ -178,6 +178,44 @@ public class AppWidgetProviderInfo implements Parcelable { */ public int minResizeHeight; + /** + * Maximum width (in dp) which the widget can be resized to. This field has no effect if it is + * smaller than minWidth or if horizontal resizing isn't enabled (see {@link #resizeMode}). + * + *

This field corresponds to the android:maxResizeWidth attribute in the + * AppWidget meta-data file. + */ + @SuppressLint("MutableBareField") + public int maxResizeWidth; + + /** + * Maximum height (in dp) which the widget can be resized to. This field has no effect if it is + * smaller than minHeight or if vertical resizing isn't enabled (see {@link #resizeMode}). + * + *

This field corresponds to the android:maxResizeHeight attribute in the + * AppWidget meta-data file. + */ + @SuppressLint("MutableBareField") + public int maxResizeHeight; + + /** + * The default width of a widget when added to a host, in units of launcher grid cells. + * + *

This field corresponds to the android:targetCellWidth attribute in the + * AppWidget meta-data file. + */ + @SuppressLint("MutableBareField") + public int targetCellWidth; + + /** + * The default height of a widget when added to a host, in units of launcher grid cells. + * + *

This field corresponds to the android:targetCellHeight attribute in the + * AppWidget meta-data file. + */ + @SuppressLint("MutableBareField") + public int targetCellHeight; + /** * How often, in milliseconds, that this AppWidget wants to be updated. * The AppWidget manager may place a limit on how often a AppWidget is updated. @@ -330,6 +368,10 @@ public class AppWidgetProviderInfo implements Parcelable { this.minHeight = in.readInt(); this.minResizeWidth = in.readInt(); this.minResizeHeight = in.readInt(); + this.maxResizeWidth = in.readInt(); + this.maxResizeHeight = in.readInt(); + this.targetCellWidth = in.readInt(); + this.targetCellHeight = in.readInt(); this.updatePeriodMillis = in.readInt(); this.initialLayout = in.readInt(); this.initialKeyguardLayout = in.readInt(); @@ -440,6 +482,10 @@ public class AppWidgetProviderInfo implements Parcelable { out.writeInt(this.minHeight); out.writeInt(this.minResizeWidth); out.writeInt(this.minResizeHeight); + out.writeInt(this.maxResizeWidth); + out.writeInt(this.maxResizeHeight); + out.writeInt(this.targetCellWidth); + out.writeInt(this.targetCellHeight); out.writeInt(this.updatePeriodMillis); out.writeInt(this.initialLayout); out.writeInt(this.initialKeyguardLayout); @@ -463,8 +509,12 @@ public class AppWidgetProviderInfo implements Parcelable { that.provider = this.provider == null ? null : this.provider.clone(); that.minWidth = this.minWidth; that.minHeight = this.minHeight; - that.minResizeWidth = this.minResizeHeight; + that.minResizeWidth = this.minResizeWidth; that.minResizeHeight = this.minResizeHeight; + that.maxResizeWidth = this.maxResizeWidth; + that.maxResizeHeight = this.maxResizeHeight; + that.targetCellWidth = this.targetCellWidth; + that.targetCellHeight = this.targetCellHeight; that.updatePeriodMillis = this.updatePeriodMillis; that.initialLayout = this.initialLayout; that.initialKeyguardLayout = this.initialKeyguardLayout; @@ -512,6 +562,8 @@ public class AppWidgetProviderInfo implements Parcelable { minHeight = TypedValue.complexToDimensionPixelSize(minHeight, displayMetrics); minResizeWidth = TypedValue.complexToDimensionPixelSize(minResizeWidth, displayMetrics); minResizeHeight = TypedValue.complexToDimensionPixelSize(minResizeHeight, displayMetrics); + maxResizeWidth = TypedValue.complexToDimensionPixelSize(maxResizeWidth, displayMetrics); + maxResizeHeight = TypedValue.complexToDimensionPixelSize(maxResizeHeight, displayMetrics); } /** diff --git a/core/res/res/values/attrs.xml b/core/res/res/values/attrs.xml index 67b810e4ebdf5..69bb20cf9c1ee 100644 --- a/core/res/res/values/attrs.xml +++ b/core/res/res/values/attrs.xml @@ -8010,6 +8010,14 @@ + + + + + + + + diff --git a/core/res/res/values/public.xml b/core/res/res/values/public.xml index fdef08bc5faab..1ebcf77d1ad3e 100644 --- a/core/res/res/values/public.xml +++ b/core/res/res/values/public.xml @@ -3072,6 +3072,10 @@ + + + + diff --git a/services/appwidget/java/com/android/server/appwidget/AppWidgetServiceImpl.java b/services/appwidget/java/com/android/server/appwidget/AppWidgetServiceImpl.java index eff410caa2d4d..809304bb24aeb 100644 --- a/services/appwidget/java/com/android/server/appwidget/AppWidgetServiceImpl.java +++ b/services/appwidget/java/com/android/server/appwidget/AppWidgetServiceImpl.java @@ -2624,12 +2624,26 @@ class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBacku info.minWidth = value != null ? value.data : 0; value = sa.peekValue(com.android.internal.R.styleable.AppWidgetProviderInfo_minHeight); info.minHeight = value != null ? value.data : 0; + value = sa.peekValue( com.android.internal.R.styleable.AppWidgetProviderInfo_minResizeWidth); info.minResizeWidth = value != null ? value.data : info.minWidth; value = sa.peekValue( com.android.internal.R.styleable.AppWidgetProviderInfo_minResizeHeight); info.minResizeHeight = value != null ? value.data : info.minHeight; + + value = sa.peekValue( + com.android.internal.R.styleable.AppWidgetProviderInfo_maxResizeWidth); + info.maxResizeWidth = value != null ? value.data : 0; + value = sa.peekValue( + com.android.internal.R.styleable.AppWidgetProviderInfo_maxResizeHeight); + info.maxResizeHeight = value != null ? value.data : 0; + + info.targetCellWidth = sa.getInt( + com.android.internal.R.styleable.AppWidgetProviderInfo_targetCellWidth, 0); + info.targetCellHeight = sa.getInt( + com.android.internal.R.styleable.AppWidgetProviderInfo_targetCellHeight, 0); + info.updatePeriodMillis = sa.getInt( com.android.internal.R.styleable.AppWidgetProviderInfo_updatePeriodMillis, 0); info.initialLayout = sa.getResourceId( diff --git a/services/tests/servicestests/res/values/strings.xml b/services/tests/servicestests/res/values/values.xml similarity index 83% rename from services/tests/servicestests/res/values/strings.xml rename to services/tests/servicestests/res/values/values.xml index 1f07ad50cdbc2..0404ebcb60d07 100644 --- a/services/tests/servicestests/res/values/strings.xml +++ b/services/tests/servicestests/res/values/values.xml @@ -36,4 +36,12 @@ module_2_name widget description string + 80dp + 40dp + 40dp + 20dp + 160dp + 80dp + 2 + 1 diff --git a/services/tests/servicestests/res/xml/dummy_appwidget_info.xml b/services/tests/servicestests/res/xml/dummy_appwidget_info.xml index 72f025dbabe9b..de06191ec2810 100644 --- a/services/tests/servicestests/res/xml/dummy_appwidget_info.xml +++ b/services/tests/servicestests/res/xml/dummy_appwidget_info.xml @@ -16,12 +16,18 @@ --> - \ No newline at end of file + diff --git a/services/tests/servicestests/src/com/android/server/appwidget/AppWidgetServiceImplTest.java b/services/tests/servicestests/src/com/android/server/appwidget/AppWidgetServiceImplTest.java index 78eb2df58925a..ff8fedce93684 100644 --- a/services/tests/servicestests/src/com/android/server/appwidget/AppWidgetServiceImplTest.java +++ b/services/tests/servicestests/src/com/android/server/appwidget/AppWidgetServiceImplTest.java @@ -103,6 +103,26 @@ public class AppWidgetServiceImplTest extends InstrumentationTestCase { assertEquals(info.loadDescription(mTestContext), "widget description string"); } + public void testParseSizeConfiguration() { + AppWidgetProviderInfo info = + mManager.getInstalledProvidersForPackage(mPkgName, null).get(0); + + assertThat(info.minWidth).isEqualTo(getDimensionResource(R.dimen.widget_min_width)); + assertThat(info.minHeight).isEqualTo(getDimensionResource(R.dimen.widget_min_height)); + assertThat(info.minResizeWidth) + .isEqualTo(getDimensionResource(R.dimen.widget_min_resize_width)); + assertThat(info.minResizeHeight) + .isEqualTo(getDimensionResource(R.dimen.widget_min_resize_height)); + assertThat(info.maxResizeWidth) + .isEqualTo(getDimensionResource(R.dimen.widget_max_resize_width)); + assertThat(info.maxResizeHeight) + .isEqualTo(getDimensionResource(R.dimen.widget_max_resize_height)); + assertThat(info.targetCellWidth) + .isEqualTo(getIntegerResource(R.integer.widget_target_cell_width)); + assertThat(info.targetCellHeight) + .isEqualTo(getIntegerResource(R.integer.widget_target_cell_height)); + } + public void testRequestPinAppWidget_otherProvider() { ComponentName otherProvider = null; for (AppWidgetProviderInfo provider : mManager.getInstalledProviders()) { @@ -325,6 +345,14 @@ public class AppWidgetServiceImplTest extends InstrumentationTestCase { latch.await(); } + private int getDimensionResource(int resId) { + return mTestContext.getResources().getDimensionPixelSize(resId); + } + + private int getIntegerResource(int resId) { + return mTestContext.getResources().getInteger(resId); + } + private class TestContext extends ContextWrapper { public TestContext() {