|
16 | 16 |
|
17 | 17 | package android.appwidget; |
18 | 18 |
|
| 19 | +import android.content.ComponentName; |
19 | 20 | import android.content.Context; |
| 21 | +import android.content.pm.ApplicationInfo; |
20 | 22 | import android.content.pm.PackageManager; |
21 | 23 | import android.content.pm.PackageManager.NameNotFoundException; |
| 24 | +import android.content.res.Resources; |
22 | 25 | import android.graphics.Bitmap; |
23 | 26 | import android.graphics.Canvas; |
24 | 27 | import android.graphics.Color; |
25 | 28 | import android.graphics.Paint; |
| 29 | +import android.os.Build; |
26 | 30 | import android.os.Parcel; |
27 | 31 | import android.os.Parcelable; |
28 | 32 | import android.os.SystemClock; |
@@ -107,6 +111,54 @@ public AppWidgetHostView(Context context, int animationIn, int animationOut) { |
107 | 111 | public void setAppWidget(int appWidgetId, AppWidgetProviderInfo info) { |
108 | 112 | mAppWidgetId = appWidgetId; |
109 | 113 | 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; |
110 | 162 | } |
111 | 163 |
|
112 | 164 | public int getAppWidgetId() { |
|
0 commit comments