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,6 +128,7 @@ 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));
if (!partially || Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
for (CalculatorButton button : CalculatorButton.values()) { for (CalculatorButton button : CalculatorButton.values()) {
final PendingIntent intent = intents.get(context, button); final PendingIntent intent = intents.get(context, button);
if (intent != null) { if (intent != null) {
@ -140,15 +142,20 @@ public class CalculatorWidget extends AppWidgetProvider {
views.setOnClickPendingIntent(buttonId, intent); views.setOnClickPendingIntent(buttonId, intent);
} }
} }
}
updateEditorState(context, views, editorState, theme); updateEditorState(context, views, editorState, theme);
updateDisplayState(context, views, displayState, theme); updateDisplayState(context, views, displayState, theme);
views.setTextViewText(R.id.cpp_button_multiplication, Locator.getInstance().getEngine().getMultiplicationSign()); views.setTextViewText(R.id.cpp_button_multiplication, Locator.getInstance().getEngine().getMultiplicationSign());
if (partially && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
manager.partiallyUpdateAppWidget(widgetId, views);
} else {
manager.updateAppWidget(widgetId, views); manager.updateAppWidget(widgetId, views);
} }
} }
}
private int getLayout(@Nonnull AppWidgetManager manager, int widgetId, @Nonnull Resources resources, @Nonnull SimpleTheme theme) { private int getLayout(@Nonnull AppWidgetManager manager, int widgetId, @Nonnull Resources resources, @Nonnull SimpleTheme theme) {
if (Build.VERSION.SDK_INT >= JELLY_BEAN) { if (Build.VERSION.SDK_INT >= JELLY_BEAN) {
@ -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;
} }
} }