Use partiallyUpdateAppWidget method to avoid settings all buttons again

This commit is contained in:
serso 2016-01-08 16:18:03 +01:00
parent d77582e342
commit 637827d79d

View File

@ -107,18 +107,19 @@ public class CalculatorWidget extends AppWidgetProvider {
@Nonnull int[] appWidgetIds) { @Nonnull int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, 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 AppWidgetManager manager = AppWidgetManager.getInstance(context);
final int[] widgetIds = manager.getAppWidgetIds(new ComponentName(context, CalculatorWidget.class)); 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, private void updateWidget(@Nonnull Context context,
@Nonnull AppWidgetManager manager, @Nonnull AppWidgetManager manager,
@Nonnull int[] widgetIds) { @Nonnull int[] widgetIds,
boolean partially) {
final EditorState editorState = Locator.getInstance().getEditor().getState(); final EditorState editorState = Locator.getInstance().getEditor().getState();
final DisplayState displayState = Locator.getInstance().getDisplay().getViewState(); final DisplayState displayState = Locator.getInstance().getDisplay().getViewState();
@ -127,17 +128,19 @@ public class CalculatorWidget extends AppWidgetProvider {
for (int widgetId : widgetIds) { for (int widgetId : widgetIds) {
final RemoteViews views = new RemoteViews(context.getPackageName(), getLayout(manager, widgetId, resources, theme)); final RemoteViews views = new RemoteViews(context.getPackageName(), getLayout(manager, widgetId, resources, theme));
for (CalculatorButton button : CalculatorButton.values()) { if (!partially || Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
final PendingIntent intent = intents.get(context, button); for (CalculatorButton button : CalculatorButton.values()) {
if (intent != null) { final PendingIntent intent = intents.get(context, button);
final int buttonId; if (intent != null) {
if (button == CalculatorButton.settings_widget) { final int buttonId;
// overriding default settings button behavior if (button == CalculatorButton.settings_widget) {
buttonId = CalculatorButton.settings.getButtonId(); // overriding default settings button behavior
} else { buttonId = CalculatorButton.settings.getButtonId();
buttonId = button.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()); 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; return;
} }
switch (action) { switch (action) {
case ACTION_CONFIGURATION_CHANGED:
case ACTION_EDITOR_STATE_CHANGED: case ACTION_EDITOR_STATE_CHANGED:
case ACTION_DISPLAY_STATE_CHANGED: case ACTION_DISPLAY_STATE_CHANGED:
updateWidget(context, true);
break;
case ACTION_CONFIGURATION_CHANGED:
case ACTION_APPWIDGET_OPTIONS_CHANGED: case ACTION_APPWIDGET_OPTIONS_CHANGED:
case ACTION_THEME_CHANGED: case ACTION_THEME_CHANGED:
updateWidget(context); updateWidget(context, false);
break; break;
} }
} }