Skip to content

Commit 5d5f340

Browse files
author
Adam Cohen
committed
Adding default padding to AppWidgetHostView for widgets >= API level 14
Change-Id: Idbbeafba664847f01393bb0c6102f774390380d5
1 parent c8f68e5 commit 5d5f340

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

core/java/android/appwidget/AppWidgetHostView.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@
1616

1717
package android.appwidget;
1818

19+
import android.content.ComponentName;
1920
import android.content.Context;
21+
import android.content.pm.ApplicationInfo;
2022
import android.content.pm.PackageManager;
2123
import android.content.pm.PackageManager.NameNotFoundException;
24+
import android.content.res.Resources;
2225
import android.graphics.Bitmap;
2326
import android.graphics.Canvas;
2427
import android.graphics.Color;
2528
import android.graphics.Paint;
29+
import android.os.Build;
2630
import android.os.Parcel;
2731
import android.os.Parcelable;
2832
import android.os.SystemClock;
@@ -107,6 +111,54 @@ public AppWidgetHostView(Context context, int animationIn, int animationOut) {
107111
public void setAppWidget(int appWidgetId, AppWidgetProviderInfo info) {
108112
mAppWidgetId = appWidgetId;
109113
mInfo = info;
114+
115+
// Sometimes the AppWidgetManager returns a null AppWidgetProviderInfo object for
116+
// a widget, eg. for some widgets in safe mode.
117+
if (info != null) {
118+
// We add padding to the AppWidgetHostView if necessary
119+
Padding padding = getPaddingForWidget(info.provider);
120+
setPadding(padding.left, padding.top, padding.right, padding.bottom);
121+
}
122+
}
123+
124+
private static class Padding {
125+
int left = 0;
126+
int right = 0;
127+
int top = 0;
128+
int bottom = 0;
129+
}
130+
131+
/**
132+
* As of ICE_CREAM_SANDWICH we are automatically adding padding to widgets targeting
133+
* ICE_CREAM_SANDWICH and higher. The new widget design guidelines strongly recommend
134+
* that widget developers do not add extra padding to their widgets. This will help
135+
* achieve consistency among widgets.
136+
*/
137+
private Padding getPaddingForWidget(ComponentName component) {
138+
PackageManager packageManager = mContext.getPackageManager();
139+
Padding p = new Padding();
140+
ApplicationInfo appInfo;
141+
142+
try {
143+
appInfo = packageManager.getApplicationInfo(component.getPackageName(), 0);
144+
} catch (Exception e) {
145+
// if we can't find the package, return 0 padding
146+
return p;
147+
}
148+
149+
if (appInfo.targetSdkVersion >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
150+
Resources r = getResources();
151+
p.left = r.getDimensionPixelSize(com.android.internal.
152+
R.dimen.default_app_widget_padding_left);
153+
p.right = r.getDimensionPixelSize(com.android.internal.
154+
R.dimen.default_app_widget_padding_right);
155+
p.top = r.getDimensionPixelSize(com.android.internal.
156+
R.dimen.default_app_widget_padding_top);
157+
p.bottom = r.getDimensionPixelSize(com.android.internal.
158+
R.dimen.default_app_widget_padding_bottom);
159+
}
160+
161+
return p;
110162
}
111163

112164
public int getAppWidgetId() {

core/res/res/values/dimens.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,10 @@
183183

184184
<!-- Volume panel y offset -->
185185
<dimen name="volume_panel_top">80dp</dimen>
186+
187+
<!-- Default padding to apply to AppWidgetHostViews containing widgets targeting API level 14 and up. -->
188+
<dimen name="default_app_widget_padding_left">8dp</dimen>
189+
<dimen name="default_app_widget_padding_top">8dp</dimen>
190+
<dimen name="default_app_widget_padding_right">8dp</dimen>
191+
<dimen name="default_app_widget_padding_bottom">8dp</dimen>
186192
</resources>

0 commit comments

Comments
 (0)