2626import android .graphics .Canvas ;
2727import android .graphics .Color ;
2828import android .graphics .Paint ;
29+ import android .graphics .Rect ;
2930import android .os .Build ;
3031import android .os .Parcel ;
3132import android .os .Parcelable ;
4142import android .widget .BaseAdapter ;
4243import android .widget .FrameLayout ;
4344import android .widget .RemoteViews ;
44- import android .widget .TextView ;
4545import 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 () {
0 commit comments