diff --git a/core/api/current.txt b/core/api/current.txt index aeeecf89dd0e2..55f81c5bd21e1 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -8349,6 +8349,7 @@ package android.appwidget { method public android.appwidget.AppWidgetProviderInfo clone(); method public int describeContents(); method public final android.os.UserHandle getProfile(); + method @Nullable public final String loadDescription(@NonNull android.content.Context); method public final android.graphics.drawable.Drawable loadIcon(@NonNull android.content.Context, int); method public final String loadLabel(android.content.pm.PackageManager); method public final android.graphics.drawable.Drawable loadPreviewImage(@NonNull android.content.Context, int); @@ -8366,6 +8367,7 @@ package android.appwidget { field public static final int WIDGET_FEATURE_RECONFIGURABLE = 1; // 0x1 field public int autoAdvanceViewId; field public android.content.ComponentName configure; + field @IdRes public int descriptionResource; field public int icon; field public int initialKeyguardLayout; field public int initialLayout; diff --git a/core/java/android/appwidget/AppWidgetProviderInfo.java b/core/java/android/appwidget/AppWidgetProviderInfo.java index 062076bb15ef3..93d96d074db8b 100644 --- a/core/java/android/appwidget/AppWidgetProviderInfo.java +++ b/core/java/android/appwidget/AppWidgetProviderInfo.java @@ -16,8 +16,11 @@ package android.appwidget; +import android.annotation.IdRes; import android.annotation.IntDef; import android.annotation.NonNull; +import android.annotation.Nullable; +import android.annotation.SuppressLint; import android.app.PendingIntent; import android.compat.annotation.UnsupportedAppUsage; import android.content.ComponentName; @@ -272,6 +275,15 @@ public class AppWidgetProviderInfo implements Parcelable { @CategoryFlags public int widgetCategory; + /** + * Resource id for the description of the AppWidget. + *

This field corresponds to the android:description attribute in the AppWidget + * meta-data file. + */ + @SuppressLint("MutableBareField") + @IdRes + public int descriptionResource; + /** * Flags indicating various features supported by the widget. These are hints to the widget * host, and do not actually change the behavior of the widget. @@ -313,6 +325,7 @@ public class AppWidgetProviderInfo implements Parcelable { this.widgetCategory = in.readInt(); this.providerInfo = in.readTypedObject(ActivityInfo.CREATOR); this.widgetFeatures = in.readInt(); + this.descriptionResource = in.readInt(); } /** @@ -369,6 +382,21 @@ public class AppWidgetProviderInfo implements Parcelable { return loadDrawable(context, density, previewImage, false); } + /** Loads localized description for the app widget. */ + @Nullable + public final String loadDescription(@NonNull Context context) { + if (ResourceId.isValid(descriptionResource)) { + return context.getPackageManager() + .getText( + providerInfo.packageName, + descriptionResource, + providerInfo.applicationInfo) + .toString() + .trim(); + } + return null; + } + /** * Gets the user profile in which the provider resides. * @@ -398,6 +426,7 @@ public class AppWidgetProviderInfo implements Parcelable { out.writeInt(this.widgetCategory); out.writeTypedObject(this.providerInfo, flags); out.writeInt(this.widgetFeatures); + out.writeInt(this.descriptionResource); } @Override @@ -421,6 +450,7 @@ public class AppWidgetProviderInfo implements Parcelable { that.widgetCategory = this.widgetCategory; that.providerInfo = this.providerInfo; that.widgetFeatures = this.widgetFeatures; + that.descriptionResource = this.descriptionResource; return that; } diff --git a/core/res/res/values/attrs.xml b/core/res/res/values/attrs.xml index 453beaf0de354..243c2448a3b10 100644 --- a/core/res/res/values/attrs.xml +++ b/core/res/res/values/attrs.xml @@ -7997,6 +7997,8 @@ the provided configuration activity. --> + + diff --git a/services/appwidget/java/com/android/server/appwidget/AppWidgetServiceImpl.java b/services/appwidget/java/com/android/server/appwidget/AppWidgetServiceImpl.java index d4bd5adb7b250..9da6ac7496954 100644 --- a/services/appwidget/java/com/android/server/appwidget/AppWidgetServiceImpl.java +++ b/services/appwidget/java/com/android/server/appwidget/AppWidgetServiceImpl.java @@ -2710,7 +2710,9 @@ class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBacku AppWidgetProviderInfo.WIDGET_CATEGORY_HOME_SCREEN); info.widgetFeatures = sa.getInt( com.android.internal.R.styleable.AppWidgetProviderInfo_widgetFeatures, 0); - + info.descriptionResource = sa.getResourceId( + com.android.internal.R.styleable.AppWidgetProviderInfo_description, + Resources.ID_NULL); sa.recycle(); return info; } catch (IOException | PackageManager.NameNotFoundException | XmlPullParserException e) { diff --git a/services/tests/servicestests/res/values/strings.xml b/services/tests/servicestests/res/values/strings.xml index 50ccd1f769f55..1f07ad50cdbc2 100644 --- a/services/tests/servicestests/res/values/strings.xml +++ b/services/tests/servicestests/res/values/strings.xml @@ -34,4 +34,6 @@ cpufreq-n=2:222,cpufreq-i=3:333/4:444 module_1_name module_2_name + + widget description string diff --git a/services/tests/servicestests/res/xml/dummy_appwidget_info.xml b/services/tests/servicestests/res/xml/dummy_appwidget_info.xml index c7144da18ddeb..65462168c5d81 100644 --- a/services/tests/servicestests/res/xml/dummy_appwidget_info.xml +++ b/services/tests/servicestests/res/xml/dummy_appwidget_info.xml @@ -20,5 +20,6 @@ android:updatePeriodMillis="86400000" android:previewImage="@drawable/icon1" android:resizeMode="horizontal|vertical" + android:description="@string/widget_description" android:widgetCategory="home_screen"> \ 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 9a5c6337824ab..96f4344051991 100644 --- a/services/tests/servicestests/src/com/android/server/appwidget/AppWidgetServiceImplTest.java +++ b/services/tests/servicestests/src/com/android/server/appwidget/AppWidgetServiceImplTest.java @@ -94,6 +94,12 @@ public class AppWidgetServiceImplTest extends InstrumentationTestCase { mService.onStart(); } + public void testLoadDescription() { + AppWidgetProviderInfo info = + mManager.getInstalledProvidersForPackage(mPkgName, null).get(0); + assertEquals(info.loadDescription(mTestContext), "widget description string"); + } + public void testRequestPinAppWidget_otherProvider() { ComponentName otherProvider = null; for (AppWidgetProviderInfo provider : mManager.getInstalledProviders()) {