Skip to content

Commit a21bf0c

Browse files
author
Adam Cohen
committed
Suppress unnecessary calls to update widget size (issue 7287744)
Change-Id: I2cec03a32b42f7a56e0f538dcd5c09de4ce2e076
1 parent f752202 commit a21bf0c

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

core/java/android/appwidget/AppWidgetHostView.java

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public void setAppWidget(int appWidgetId, AppWidgetProviderInfo info) {
161161
* @param component the component name of the widget
162162
* @param padding Rect in which to place the output, if null, a new Rect will be allocated and
163163
* returned
164-
* @return default padding for this widget
164+
* @return default padding for this widget, in pixels
165165
*/
166166
public static Rect getDefaultPaddingForWidget(Context context, ComponentName component,
167167
Rect padding) {
@@ -241,18 +241,18 @@ protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
241241
* AppWidget options and causes a callback to the AppWidgetProvider.
242242
* @see AppWidgetProvider#onAppWidgetOptionsChanged(Context, AppWidgetManager, int, Bundle)
243243
*
244-
* @param options The bundle of options, in addition to the size information,
244+
* @param newOptions The bundle of options, in addition to the size information,
245245
* can be null.
246246
* @param minWidth The minimum width that the widget will be displayed at.
247247
* @param minHeight The maximum height that the widget will be displayed at.
248248
* @param maxWidth The maximum width that the widget will be displayed at.
249249
* @param maxHeight The maximum height that the widget will be displayed at.
250250
*
251251
*/
252-
public void updateAppWidgetSize(Bundle options, int minWidth, int minHeight, int maxWidth,
252+
public void updateAppWidgetSize(Bundle newOptions, int minWidth, int minHeight, int maxWidth,
253253
int maxHeight) {
254-
if (options == null) {
255-
options = new Bundle();
254+
if (newOptions == null) {
255+
newOptions = new Bundle();
256256
}
257257

258258
Rect padding = new Rect();
@@ -264,11 +264,30 @@ public void updateAppWidgetSize(Bundle options, int minWidth, int minHeight, int
264264
int xPaddingDips = (int) ((padding.left + padding.right) / density);
265265
int yPaddingDips = (int) ((padding.top + padding.bottom) / density);
266266

267-
options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, minWidth - xPaddingDips);
268-
options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, minHeight - yPaddingDips);
269-
options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, maxWidth - xPaddingDips);
270-
options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, maxHeight - yPaddingDips);
271-
updateAppWidgetOptions(options);
267+
int newMinWidth = minWidth - xPaddingDips;
268+
int newMinHeight = minHeight - yPaddingDips;
269+
int newMaxWidth = maxWidth - xPaddingDips;
270+
int newMaxHeight = maxHeight - yPaddingDips;
271+
272+
AppWidgetManager widgetManager = AppWidgetManager.getInstance(mContext);
273+
274+
// We get the old options to see if the sizes have changed
275+
Bundle oldOptions = widgetManager.getAppWidgetOptions(mAppWidgetId);
276+
boolean needsUpdate = false;
277+
if (newMinWidth != oldOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH) ||
278+
newMinHeight != oldOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT) ||
279+
newMaxWidth != oldOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH) ||
280+
newMaxHeight != oldOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT)) {
281+
needsUpdate = true;
282+
}
283+
284+
if (needsUpdate) {
285+
newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, newMinWidth);
286+
newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, newMinHeight);
287+
newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, newMaxWidth);
288+
newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, newMaxHeight);
289+
updateAppWidgetOptions(newOptions);
290+
}
272291
}
273292

274293
/**

0 commit comments

Comments
 (0)