Skip to content

Commit 4c96a59

Browse files
author
Adam Cohen
committed
Making default widget padding public API
Change-Id: Ibf4f5dc1a36d84be1acc3ccdc4330276f82aa303
1 parent 2d9ccdb commit 4c96a59

File tree

4 files changed

+40
-23
lines changed

4 files changed

+40
-23
lines changed

api/current.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4145,6 +4145,7 @@ package android.appwidget {
41454145
ctor public AppWidgetHostView(android.content.Context, int, int);
41464146
method public int getAppWidgetId();
41474147
method public android.appwidget.AppWidgetProviderInfo getAppWidgetInfo();
4148+
method public static android.graphics.Rect getDefaultPaddingForWidget(android.content.Context, android.content.ComponentName, android.graphics.Rect);
41484149
method protected android.view.View getDefaultView();
41494150
method protected android.view.View getErrorView();
41504151
method protected void prepareView(android.view.View);

core/java/android/appwidget/AppWidgetHostView.java

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import android.graphics.Canvas;
2727
import android.graphics.Color;
2828
import android.graphics.Paint;
29+
import android.graphics.Rect;
2930
import android.os.Build;
3031
import android.os.Parcel;
3132
import android.os.Parcelable;
@@ -41,8 +42,8 @@
4142
import android.widget.BaseAdapter;
4243
import android.widget.FrameLayout;
4344
import android.widget.RemoteViews;
44-
import android.widget.TextView;
4545
import android.widget.RemoteViewsAdapter.RemoteAdapterConnectionCallback;
46+
import android.widget.TextView;
4647

4748
/**
4849
* Provides the glue to show AppWidget views. This class offers automatic animation
@@ -106,7 +107,9 @@ public AppWidgetHostView(Context context, int animationIn, int animationOut) {
106107
}
107108

108109
/**
109-
* Set the AppWidget that will be displayed by this view.
110+
* Set the AppWidget that will be displayed by this view. This method also adds default padding
111+
* to widgets, as described in {@link #getDefaultPaddingForWidget(Context, ComponentName, Rect)}
112+
* and can be overridden in order to add custom padding.
110113
*/
111114
public void setAppWidget(int appWidgetId, AppWidgetProviderInfo info) {
112115
mAppWidgetId = appWidgetId;
@@ -116,49 +119,57 @@ public void setAppWidget(int appWidgetId, AppWidgetProviderInfo info) {
116119
// a widget, eg. for some widgets in safe mode.
117120
if (info != null) {
118121
// We add padding to the AppWidgetHostView if necessary
119-
Padding padding = getPaddingForWidget(info.provider);
122+
Rect padding = getDefaultPaddingForWidget(mContext, info.provider, null);
120123
setPadding(padding.left, padding.top, padding.right, padding.bottom);
121124
}
122125
}
123126

124-
private static class Padding {
125-
int left = 0;
126-
int right = 0;
127-
int top = 0;
128-
int bottom = 0;
129-
}
130-
131127
/**
132128
* As of ICE_CREAM_SANDWICH we are automatically adding padding to widgets targeting
133129
* ICE_CREAM_SANDWICH and higher. The new widget design guidelines strongly recommend
134130
* that widget developers do not add extra padding to their widgets. This will help
135131
* achieve consistency among widgets.
132+
*
133+
* Note: this method is only needed by developers of AppWidgetHosts. The method is provided in
134+
* order for the AppWidgetHost to account for the automatic padding when computing the number
135+
* of cells to allocate to a particular widget.
136+
*
137+
* @param context the current context
138+
* @param component the component name of the widget
139+
* @param padding Rect in which to place the output, if null, a new Rect will be allocated and
140+
* returned
141+
* @return default padding for this widget
136142
*/
137-
private Padding getPaddingForWidget(ComponentName component) {
138-
PackageManager packageManager = mContext.getPackageManager();
139-
Padding p = new Padding();
143+
public static Rect getDefaultPaddingForWidget(Context context, ComponentName component,
144+
Rect padding) {
145+
PackageManager packageManager = context.getPackageManager();
140146
ApplicationInfo appInfo;
141147

148+
if (padding == null) {
149+
padding = new Rect(0, 0, 0, 0);
150+
} else {
151+
padding.set(0, 0, 0, 0);
152+
}
153+
142154
try {
143155
appInfo = packageManager.getApplicationInfo(component.getPackageName(), 0);
144-
} catch (Exception e) {
156+
} catch (NameNotFoundException e) {
145157
// if we can't find the package, return 0 padding
146-
return p;
158+
return padding;
147159
}
148160

149161
if (appInfo.targetSdkVersion >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
150-
Resources r = getResources();
151-
p.left = r.getDimensionPixelSize(com.android.internal.
162+
Resources r = context.getResources();
163+
padding.left = r.getDimensionPixelSize(com.android.internal.
152164
R.dimen.default_app_widget_padding_left);
153-
p.right = r.getDimensionPixelSize(com.android.internal.
165+
padding.right = r.getDimensionPixelSize(com.android.internal.
154166
R.dimen.default_app_widget_padding_right);
155-
p.top = r.getDimensionPixelSize(com.android.internal.
167+
padding.top = r.getDimensionPixelSize(com.android.internal.
156168
R.dimen.default_app_widget_padding_top);
157-
p.bottom = r.getDimensionPixelSize(com.android.internal.
169+
padding.bottom = r.getDimensionPixelSize(com.android.internal.
158170
R.dimen.default_app_widget_padding_bottom);
159171
}
160-
161-
return p;
172+
return padding;
162173
}
163174

164175
public int getAppWidgetId() {

core/res/res/values-sw600dp/dimens.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@
6060
<!-- Compensate for double margin : preference_screen_side_margin + 4 (frame background shadow) = -preference_screen_side_margin_negative -->
6161
<dimen name="preference_screen_side_margin_negative">-4dp</dimen>
6262

63+
<!-- Default padding to apply to AppWidgetHostViews containing widgets targeting API level 14 and up. -->
64+
<dimen name="default_app_widget_padding_left">12dp</dimen>
65+
<dimen name="default_app_widget_padding_top">12dp</dimen>
66+
<dimen name="default_app_widget_padding_right">4dp</dimen>
67+
<dimen name="default_app_widget_padding_bottom">20dp</dimen>
68+
6369
<!-- Minimum width for an action button in the menu area of an action bar -->
6470
<dimen name="action_button_min_width">64dip</dimen>
6571
</resources>

core/res/res/values/public.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1974,5 +1974,4 @@
19741974
<public type="color" name="holo_orange_dark" id="0x01060019" />
19751975
<public type="color" name="holo_purple" id="0x0106001a" />
19761976
<public type="color" name="holo_blue_bright" id="0x0106001b" />
1977-
19781977
</resources>

0 commit comments

Comments
 (0)