From 21b56bbe59bf3edace4eec7ab896118bc341fa8d Mon Sep 17 00:00:00 2001 From: serso Date: Sat, 9 Jan 2016 18:08:42 +0100 Subject: [PATCH] Intents in Broabcaster are now cached --- .../calculator/CalculatorBroadcaster.java | 35 +++++++++++++------ 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorBroadcaster.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorBroadcaster.java index 78bff9eb..2a319f45 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorBroadcaster.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorBroadcaster.java @@ -6,6 +6,8 @@ import android.content.SharedPreferences; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import java.util.HashMap; +import java.util.Map; public final class CalculatorBroadcaster implements CalculatorEventListener, SharedPreferences.OnSharedPreferenceChangeListener { @@ -17,6 +19,9 @@ public final class CalculatorBroadcaster implements CalculatorEventListener, Sha @Nonnull private final Context context; + @Nonnull + private final Intents intents = new Intents(); + public CalculatorBroadcaster(@Nonnull Context context, @Nonnull SharedPreferences preferences) { this.context = context; preferences.registerOnSharedPreferenceChangeListener(this); @@ -27,28 +32,20 @@ public final class CalculatorBroadcaster implements CalculatorEventListener, Sha switch (calculatorEventType) { case editor_state_changed: case editor_state_changed_light: - sendEditorStateChangedIntent(); + sendBroadcastIntent(ACTION_EDITOR_STATE_CHANGED); break; case display_state_changed: - sendDisplayStateChanged(); + sendBroadcastIntent(ACTION_DISPLAY_STATE_CHANGED); break; } } - public void sendDisplayStateChanged() { - sendBroadcastIntent(ACTION_DISPLAY_STATE_CHANGED); - } - - public void sendEditorStateChangedIntent() { - sendBroadcastIntent(ACTION_EDITOR_STATE_CHANGED); - } - public void sendInitIntent() { sendBroadcastIntent(ACTION_INIT); } public void sendBroadcastIntent(@Nonnull String action) { - context.sendBroadcast(new Intent(action)); + context.sendBroadcast(intents.get(action)); } @Override @@ -57,4 +54,20 @@ public final class CalculatorBroadcaster implements CalculatorEventListener, Sha sendBroadcastIntent(ACTION_THEME_CHANGED); } } + + private static final class Intents { + @Nonnull + private Map map = new HashMap<>(); + + @Nonnull + Intent get(@Nonnull String action) { + Intent intent = map.get(action); + if (intent != null) { + return intent; + } + intent = new Intent(action); + map.put(action, intent); + return intent; + } + } }