From cd5c9b0642c691f6f2d0e432052828964fbdbbf3 Mon Sep 17 00:00:00 2001 From: Steven Ng Date: Sat, 6 Feb 2021 00:31:42 +0000 Subject: [PATCH] Add a layout attribute in widget metadata for widget preview Test: atest FrameworksServicesTests:AppWidgetServiceImplTest. Newly added test passed. Bug: 179063291 Change-Id: I17cf97b580ac03d9688d82eeccd3a68e77287655 --- core/api/current.txt | 2 ++ .../appwidget/AppWidgetProviderInfo.java | 24 +++++++++++++++++-- core/res/res/values/attrs.xml | 12 ++++++++-- core/res/res/values/public.xml | 1 + .../appwidget/AppWidgetServiceImpl.java | 2 ++ .../res/layout/widget_preview.xml | 21 ++++++++++++++++ .../res/xml/dummy_appwidget_info.xml | 2 ++ .../appwidget/AppWidgetServiceImplTest.java | 10 ++++++++ 8 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 services/tests/servicestests/res/layout/widget_preview.xml diff --git a/core/api/current.txt b/core/api/current.txt index f5c67c1709316..a58208f2152a5 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -1100,6 +1100,7 @@ package android { field public static final int presentationTheme = 16843712; // 0x10103c0 field public static final int preserveLegacyExternalStorage = 16844308; // 0x1010614 field public static final int previewImage = 16843482; // 0x10102da + field public static final int previewLayout = 16844327; // 0x1010627 field public static final int primaryContentAlpha = 16844114; // 0x1010552 field public static final int priority = 16842780; // 0x101001c field public static final int privateImeOptions = 16843299; // 0x1010223 @@ -8386,6 +8387,7 @@ package android.appwidget { field public int minResizeWidth; field public int minWidth; field public int previewImage; + field @IdRes public int previewLayout; field public android.content.ComponentName provider; field public int resizeMode; field public int updatePeriodMillis; diff --git a/core/java/android/appwidget/AppWidgetProviderInfo.java b/core/java/android/appwidget/AppWidgetProviderInfo.java index e96e22c4764a6..42214d0477405 100644 --- a/core/java/android/appwidget/AppWidgetProviderInfo.java +++ b/core/java/android/appwidget/AppWidgetProviderInfo.java @@ -247,11 +247,28 @@ public class AppWidgetProviderInfo implements Parcelable { * A preview of what the AppWidget will look like after it's configured. * If not supplied, the AppWidget's icon will be used. * - *

This field corresponds to the android:previewImage attribute in - * the <receiver> element in the AndroidManifest.xml file. + *

This field corresponds to the android:previewImage attribute in the AppWidget + * meta-data file. */ public int previewImage; + /** + * The layout resource id of a preview of what the AppWidget will look like after it's + * configured. + * + *

Unlike previewImage, previewLayout can better showcase AppWidget in different locales, + * system themes, display sizes & density etc. + * + *

If supplied, this will take precedence over the previewImage on supported widget hosts. + * Otherwise, previewImage will be used. + * + *

This field corresponds to the android:previewLayout attribute in the + * AppWidget meta-data file. + */ + @SuppressLint("MutableBareField") + @IdRes + public int previewLayout; + /** * The rules by which a widget can be resized. See {@link #RESIZE_NONE}, * {@link #RESIZE_NONE}, {@link #RESIZE_HORIZONTAL}, @@ -320,6 +337,7 @@ public class AppWidgetProviderInfo implements Parcelable { this.label = in.readString(); this.icon = in.readInt(); this.previewImage = in.readInt(); + this.previewLayout = in.readInt(); this.autoAdvanceViewId = in.readInt(); this.resizeMode = in.readInt(); this.widgetCategory = in.readInt(); @@ -429,6 +447,7 @@ public class AppWidgetProviderInfo implements Parcelable { out.writeString(this.label); out.writeInt(this.icon); out.writeInt(this.previewImage); + out.writeInt(this.previewLayout); out.writeInt(this.autoAdvanceViewId); out.writeInt(this.resizeMode); out.writeInt(this.widgetCategory); @@ -453,6 +472,7 @@ public class AppWidgetProviderInfo implements Parcelable { that.label = this.label; that.icon = this.icon; that.previewImage = this.previewImage; + that.previewLayout = this.previewLayout; that.autoAdvanceViewId = this.autoAdvanceViewId; that.resizeMode = this.resizeMode; that.widgetCategory = this.widgetCategory; diff --git a/core/res/res/values/attrs.xml b/core/res/res/values/attrs.xml index 14df77527a086..e567c3d7d486e 100644 --- a/core/res/res/values/attrs.xml +++ b/core/res/res/values/attrs.xml @@ -7969,9 +7969,17 @@ - + + + diff --git a/core/res/res/values/public.xml b/core/res/res/values/public.xml index 30cfb89860353..97ec0f4fa71e1 100644 --- a/core/res/res/values/public.xml +++ b/core/res/res/values/public.xml @@ -3061,6 +3061,7 @@ + diff --git a/services/appwidget/java/com/android/server/appwidget/AppWidgetServiceImpl.java b/services/appwidget/java/com/android/server/appwidget/AppWidgetServiceImpl.java index e4a86c3744ce0..eff410caa2d4d 100644 --- a/services/appwidget/java/com/android/server/appwidget/AppWidgetServiceImpl.java +++ b/services/appwidget/java/com/android/server/appwidget/AppWidgetServiceImpl.java @@ -2647,6 +2647,8 @@ class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBacku info.icon = activityInfo.getIconResource(); info.previewImage = sa.getResourceId( com.android.internal.R.styleable.AppWidgetProviderInfo_previewImage, 0); + info.previewLayout = sa.getResourceId( + com.android.internal.R.styleable.AppWidgetProviderInfo_previewLayout, 0); info.autoAdvanceViewId = sa.getResourceId( com.android.internal.R.styleable.AppWidgetProviderInfo_autoAdvanceViewId, -1); info.resizeMode = sa.getInt( diff --git a/services/tests/servicestests/res/layout/widget_preview.xml b/services/tests/servicestests/res/layout/widget_preview.xml new file mode 100644 index 0000000000000..137ff46663d55 --- /dev/null +++ b/services/tests/servicestests/res/layout/widget_preview.xml @@ -0,0 +1,21 @@ + + + \ No newline at end of file diff --git a/services/tests/servicestests/res/xml/dummy_appwidget_info.xml b/services/tests/servicestests/res/xml/dummy_appwidget_info.xml index 65462168c5d81..72f025dbabe9b 100644 --- a/services/tests/servicestests/res/xml/dummy_appwidget_info.xml +++ b/services/tests/servicestests/res/xml/dummy_appwidget_info.xml @@ -1,3 +1,4 @@ +