From 637827d79d6ba8318ce0ee2d742491234b6085a1 Mon Sep 17 00:00:00 2001 From: serso Date: Fri, 8 Jan 2016 16:18:03 +0100 Subject: [PATCH] Use partiallyUpdateAppWidget method to avoid settings all buttons again --- .../calculator/widget/CalculatorWidget.java | 43 +++++++++++-------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidget.java b/app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidget.java index e53d7915..ed25ac0b 100644 --- a/app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidget.java +++ b/app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidget.java @@ -107,18 +107,19 @@ public class CalculatorWidget extends AppWidgetProvider { @Nonnull int[] appWidgetIds) { super.onUpdate(context, appWidgetManager, appWidgetIds); - updateWidget(context, appWidgetManager, appWidgetIds); + updateWidget(context, appWidgetManager, appWidgetIds, false); } - public void updateWidget(@Nonnull Context context) { + public void updateWidget(@Nonnull Context context, boolean partially) { final AppWidgetManager manager = AppWidgetManager.getInstance(context); final int[] widgetIds = manager.getAppWidgetIds(new ComponentName(context, CalculatorWidget.class)); - updateWidget(context, manager, widgetIds); + updateWidget(context, manager, widgetIds, partially); } private void updateWidget(@Nonnull Context context, @Nonnull AppWidgetManager manager, - @Nonnull int[] widgetIds) { + @Nonnull int[] widgetIds, + boolean partially) { final EditorState editorState = Locator.getInstance().getEditor().getState(); final DisplayState displayState = Locator.getInstance().getDisplay().getViewState(); @@ -127,17 +128,19 @@ public class CalculatorWidget extends AppWidgetProvider { for (int widgetId : widgetIds) { final RemoteViews views = new RemoteViews(context.getPackageName(), getLayout(manager, widgetId, resources, theme)); - for (CalculatorButton button : CalculatorButton.values()) { - final PendingIntent intent = intents.get(context, button); - if (intent != null) { - final int buttonId; - if (button == CalculatorButton.settings_widget) { - // overriding default settings button behavior - buttonId = CalculatorButton.settings.getButtonId(); - } else { - buttonId = button.getButtonId(); + if (!partially || Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { + for (CalculatorButton button : CalculatorButton.values()) { + final PendingIntent intent = intents.get(context, button); + if (intent != null) { + final int buttonId; + if (button == CalculatorButton.settings_widget) { + // overriding default settings button behavior + buttonId = CalculatorButton.settings.getButtonId(); + } else { + buttonId = button.getButtonId(); + } + views.setOnClickPendingIntent(buttonId, intent); } - views.setOnClickPendingIntent(buttonId, intent); } } @@ -146,7 +149,11 @@ public class CalculatorWidget extends AppWidgetProvider { views.setTextViewText(R.id.cpp_button_multiplication, Locator.getInstance().getEngine().getMultiplicationSign()); - manager.updateAppWidget(widgetId, views); + if (partially && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { + manager.partiallyUpdateAppWidget(widgetId, views); + } else { + manager.updateAppWidget(widgetId, views); + } } } @@ -197,12 +204,14 @@ public class CalculatorWidget extends AppWidgetProvider { return; } switch (action) { - case ACTION_CONFIGURATION_CHANGED: case ACTION_EDITOR_STATE_CHANGED: case ACTION_DISPLAY_STATE_CHANGED: + updateWidget(context, true); + break; + case ACTION_CONFIGURATION_CHANGED: case ACTION_APPWIDGET_OPTIONS_CHANGED: case ACTION_THEME_CHANGED: - updateWidget(context); + updateWidget(context, false); break; } }