Include an attribute for description of the widget in the attributes file for AppWidgetProviderInfo.

Test: Instrumentation test added- AppWidgetServiceImplTest_testLoadDescription

Bug: 178460757

Change-Id: Ic9b9cd8c592936bbe421183f8715ba5d8f770742
This commit is contained in:
Alina Zaidi
2021-01-21 21:31:25 +00:00
parent d8d8acfd9f
commit 016e50a980
7 changed files with 46 additions and 1 deletions

View File

@@ -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;

View File

@@ -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.
* <p>This field corresponds to the <code>android:description</code> 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;
}

View File

@@ -7997,6 +7997,8 @@
the provided configuration activity. -->
<flag name="configuration_optional" value="0x3" />
</attr>
<!-- A resource identifier for a string containing a short description of the widget. -->
<attr name="description" />
</declare-styleable>
<!-- =============================== -->

View File

@@ -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) {

View File

@@ -34,4 +34,6 @@
<string name="config_batterySaverDeviceSpecificConfig_3">cpufreq-n=2:222,cpufreq-i=3:333/4:444</string>
<string name="module_1_name" translatable="false">module_1_name</string>
<string name="module_2_name" translatable="false">module_2_name</string>
<string name="widget_description">widget description string</string>
</resources>

View File

@@ -20,5 +20,6 @@
android:updatePeriodMillis="86400000"
android:previewImage="@drawable/icon1"
android:resizeMode="horizontal|vertical"
android:description="@string/widget_description"
android:widgetCategory="home_screen">
</appwidget-provider>

View File

@@ -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()) {