Use static cache for intents

This commit is contained in:
serso 2016-01-08 15:57:34 +01:00
parent 7774d87d97
commit d77582e342

View File

@ -41,9 +41,18 @@ import android.text.SpannedString;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.style.StyleSpan; import android.text.style.StyleSpan;
import android.widget.RemoteViews; import android.widget.RemoteViews;
import org.solovyev.android.Check;
import org.solovyev.android.Views; import org.solovyev.android.Views;
import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.CalculatorButton;
import org.solovyev.android.calculator.DisplayState;
import org.solovyev.android.calculator.EditorState;
import org.solovyev.android.calculator.Locator;
import org.solovyev.android.calculator.Preferences.SimpleTheme; import org.solovyev.android.calculator.Preferences.SimpleTheme;
import org.solovyev.android.calculator.R;
import java.util.EnumMap;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -51,7 +60,9 @@ import javax.annotation.Nullable;
import static android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT; import static android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT;
import static android.content.Intent.ACTION_CONFIGURATION_CHANGED; import static android.content.Intent.ACTION_CONFIGURATION_CHANGED;
import static android.os.Build.VERSION_CODES.JELLY_BEAN; import static android.os.Build.VERSION_CODES.JELLY_BEAN;
import static org.solovyev.android.calculator.CalculatorBroadcaster.*; import static org.solovyev.android.calculator.CalculatorBroadcaster.ACTION_DISPLAY_STATE_CHANGED;
import static org.solovyev.android.calculator.CalculatorBroadcaster.ACTION_EDITOR_STATE_CHANGED;
import static org.solovyev.android.calculator.CalculatorBroadcaster.ACTION_THEME_CHANGED;
import static org.solovyev.android.calculator.CalculatorReceiver.newButtonClickedIntent; import static org.solovyev.android.calculator.CalculatorReceiver.newButtonClickedIntent;
public class CalculatorWidget extends AppWidgetProvider { public class CalculatorWidget extends AppWidgetProvider {
@ -60,7 +71,8 @@ public class CalculatorWidget extends AppWidgetProvider {
private static final int WIDGET_CATEGORY_KEYGUARD = 2; private static final int WIDGET_CATEGORY_KEYGUARD = 2;
private static final String OPTION_APPWIDGET_HOST_CATEGORY = "appWidgetCategory"; private static final String OPTION_APPWIDGET_HOST_CATEGORY = "appWidgetCategory";
private static final String ACTION_APPWIDGET_OPTIONS_CHANGED = "android.appwidget.action.APPWIDGET_UPDATE_OPTIONS"; private static final String ACTION_APPWIDGET_OPTIONS_CHANGED = "android.appwidget.action.APPWIDGET_UPDATE_OPTIONS";
@Nonnull
private static final Intents intents = new Intents();
@Nullable @Nullable
private SpannedString cursorString; private SpannedString cursorString;
@ -98,7 +110,7 @@ public class CalculatorWidget extends AppWidgetProvider {
updateWidget(context, appWidgetManager, appWidgetIds); updateWidget(context, appWidgetManager, appWidgetIds);
} }
public void updateState(@Nonnull Context context) { public void updateWidget(@Nonnull Context context) {
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);
@ -116,6 +128,8 @@ public class CalculatorWidget extends AppWidgetProvider {
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()) { for (CalculatorButton button : CalculatorButton.values()) {
final PendingIntent intent = intents.get(context, button);
if (intent != null) {
final int buttonId; final int buttonId;
if (button == CalculatorButton.settings_widget) { if (button == CalculatorButton.settings_widget) {
// overriding default settings button behavior // overriding default settings button behavior
@ -123,8 +137,6 @@ public class CalculatorWidget extends AppWidgetProvider {
} else { } else {
buttonId = button.getButtonId(); buttonId = button.getButtonId();
} }
final PendingIntent intent = PendingIntent.getBroadcast(context, buttonId, newButtonClickedIntent(context, button), PendingIntent.FLAG_UPDATE_CURRENT);
if (intent != null) {
views.setOnClickPendingIntent(buttonId, intent); views.setOnClickPendingIntent(buttonId, intent);
} }
} }
@ -190,7 +202,7 @@ public class CalculatorWidget extends AppWidgetProvider {
case ACTION_DISPLAY_STATE_CHANGED: case ACTION_DISPLAY_STATE_CHANGED:
case ACTION_APPWIDGET_OPTIONS_CHANGED: case ACTION_APPWIDGET_OPTIONS_CHANGED:
case ACTION_THEME_CHANGED: case ACTION_THEME_CHANGED:
updateState(context); updateWidget(context);
break; break;
} }
} }
@ -234,4 +246,25 @@ public class CalculatorWidget extends AppWidgetProvider {
private String unspan(@Nonnull CharSequence spannable) { private String unspan(@Nonnull CharSequence spannable) {
return spannable.toString(); return spannable.toString();
} }
private static class Intents {
@Nonnull
private final EnumMap<CalculatorButton, PendingIntent> map = new EnumMap<>(CalculatorButton.class);
@Nullable
PendingIntent get(@Nonnull Context context, @Nonnull CalculatorButton button) {
Check.isMainThread();
PendingIntent intent = map.get(button);
if (intent != null) {
return intent;
}
intent = PendingIntent.getBroadcast(context, button.getButtonId(), newButtonClickedIntent(context, button), PendingIntent.FLAG_UPDATE_CURRENT);
if (intent == null) {
return null;
}
map.put(button, intent);
return intent;
}
}
} }