diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/external/DefaultExternalCalculatorIntentHandler.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/external/DefaultExternalCalculatorIntentHandler.java index ecd1c90b..b4c88bcf 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/external/DefaultExternalCalculatorIntentHandler.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/external/DefaultExternalCalculatorIntentHandler.java @@ -7,6 +7,7 @@ import org.jetbrains.annotations.NotNull; import org.solovyev.android.calculator.CalculatorDisplayViewState; import org.solovyev.android.calculator.CalculatorEditorViewState; import org.solovyev.android.calculator.CalculatorLocatorImpl; +import org.solovyev.android.calculator.widget.CalculatorWidgetHelper; import org.solovyev.common.MutableObject; /** @@ -34,10 +35,10 @@ public class DefaultExternalCalculatorIntentHandler implements ExternalCalculato @Override public void onIntent(@NotNull Context context, @NotNull Intent intent) { - if (ExternalCalculatorHelper.EDITOR_STATE_CHANGED_ACTION.equals(intent.getAction())) { + if (CalculatorWidgetHelper.EDITOR_STATE_CHANGED_ACTION.equals(intent.getAction())) { CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Editor state changed broadcast received!"); - final Long eventId = intent.getLongExtra(ExternalCalculatorHelper.EVENT_ID_EXTRA, 0L); + final Long eventId = intent.getLongExtra(CalculatorWidgetHelper.EVENT_ID_EXTRA, 0L); boolean updateEditor = false; synchronized (lastEditorEventId) { @@ -48,15 +49,15 @@ public class DefaultExternalCalculatorIntentHandler implements ExternalCalculato } if (updateEditor) { - final Parcelable object = intent.getParcelableExtra(ExternalCalculatorHelper.EDITOR_STATE_EXTRA); + final Parcelable object = intent.getParcelableExtra(CalculatorWidgetHelper.EDITOR_STATE_EXTRA); if (object instanceof CalculatorEditorViewState) { onEditorStateChanged(context, (CalculatorEditorViewState) object); } } - } else if (ExternalCalculatorHelper.DISPLAY_STATE_CHANGED_ACTION.equals(intent.getAction())) { + } else if (CalculatorWidgetHelper.DISPLAY_STATE_CHANGED_ACTION.equals(intent.getAction())) { CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Display state changed broadcast received!"); - final Long eventId = intent.getLongExtra(ExternalCalculatorHelper.EVENT_ID_EXTRA, 0L); + final Long eventId = intent.getLongExtra(CalculatorWidgetHelper.EVENT_ID_EXTRA, 0L); boolean updateDisplay = false; synchronized (lastDisplayEventId) { if (eventId > lastDisplayEventId.getObject()) { @@ -66,7 +67,7 @@ public class DefaultExternalCalculatorIntentHandler implements ExternalCalculato } if (updateDisplay) { - final Parcelable object = intent.getParcelableExtra(ExternalCalculatorHelper.DISPLAY_STATE_EXTRA); + final Parcelable object = intent.getParcelableExtra(CalculatorWidgetHelper.DISPLAY_STATE_EXTRA); if (object instanceof CalculatorDisplayViewState) { onDisplayStateChanged(context, (CalculatorDisplayViewState) object); } diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/external/ExternalCalculatorHelper.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/external/ExternalCalculatorHelper.java index 5f0357d3..06dfbc73 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/external/ExternalCalculatorHelper.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/external/ExternalCalculatorHelper.java @@ -1,69 +1,10 @@ package org.solovyev.android.calculator.external; -import android.content.Context; -import android.content.Intent; -import android.os.Parcelable; -import org.jetbrains.annotations.NotNull; -import org.solovyev.android.calculator.*; - -import java.util.HashSet; -import java.util.Set; - /** * User: serso * Date: 11/20/12 * Time: 10:14 PM */ public class ExternalCalculatorHelper { - /* - ********************************************************************** - * - * CONSTANTS - * - ********************************************************************** - */ - public static final String EVENT_ID_EXTRA = "eventId"; - public static final String EDITOR_STATE_CHANGED_ACTION = "org.solovyev.calculator.widget.EDITOR_STATE_CHANGED"; - public static final String EDITOR_STATE_EXTRA = "editorState"; - public static final String DISPLAY_STATE_CHANGED_ACTION = "org.solovyev.calculator.widget.DISPLAY_STATE_CHANGED"; - public static final String DISPLAY_STATE_EXTRA = "displayState"; - private static final String TAG = ExternalCalculatorHelper.class.getSimpleName(); - - private static final Set> externalListeners = new HashSet>(); - - public static void onEditorStateChanged(@NotNull Context context, - @NotNull CalculatorEventData calculatorEventData, - @NotNull CalculatorEditorViewState editorViewState) { - - for (Class externalListener : externalListeners) { - final Intent intent = new Intent(EDITOR_STATE_CHANGED_ACTION); - intent.setClass(context, externalListener); - intent.putExtra(EVENT_ID_EXTRA, calculatorEventData.getEventId()); - intent.putExtra(EDITOR_STATE_EXTRA, (Parcelable) new ParcelableCalculatorEditorViewState(editorViewState)); - context.sendBroadcast(intent); - CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Editor state changed broadcast sent"); - } - } - - public static void onDisplayStateChanged(@NotNull Context context, - @NotNull CalculatorEventData calculatorEventData, - @NotNull CalculatorDisplayViewState displayViewState) { - for (Class externalListener : externalListeners) { - final Intent intent = new Intent(DISPLAY_STATE_CHANGED_ACTION); - intent.setClass(context, externalListener); - intent.putExtra(EVENT_ID_EXTRA, calculatorEventData.getEventId()); - intent.putExtra(DISPLAY_STATE_EXTRA, (Parcelable) new ParcelableCalculatorDisplayViewState(displayViewState)); - context.sendBroadcast(intent); - CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Display state changed broadcast sent"); - } - } - - public static void addExternalListener(@NotNull Class externalCalculatorClass) { - externalListeners.add(externalCalculatorClass); - } - - public static boolean removeExternalListener(@NotNull Class externalCalculatorClass) { - return externalListeners.remove(externalCalculatorClass); - } } diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/overlay/CalculatorOverlayService.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/overlay/CalculatorOverlayService.java index d859b2bc..2beb30de 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/overlay/CalculatorOverlayService.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/overlay/CalculatorOverlayService.java @@ -16,9 +16,9 @@ import org.solovyev.android.calculator.CalculatorDisplayViewState; import org.solovyev.android.calculator.CalculatorEditorViewState; import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.external.DefaultExternalCalculatorIntentHandler; -import org.solovyev.android.calculator.external.ExternalCalculatorHelper; import org.solovyev.android.calculator.external.ExternalCalculatorIntentHandler; import org.solovyev.android.calculator.external.ExternalCalculatorStateUpdater; +import org.solovyev.android.calculator.widget.CalculatorWidgetHelper; import org.solovyev.android.calculator.widget.WidgetButton; /** @@ -75,7 +75,7 @@ public class CalculatorOverlayService extends Service implements ExternalCalcula } private void startCalculatorListening() { - ExternalCalculatorHelper.addExternalListener(getIntentListenerClass()); + CalculatorWidgetHelper.addExternalListener(getIntentListenerClass()); } @NotNull @@ -84,7 +84,7 @@ public class CalculatorOverlayService extends Service implements ExternalCalcula } private void stopCalculatorListening() { - ExternalCalculatorHelper.removeExternalListener(getIntentListenerClass()); + CalculatorWidgetHelper.removeExternalListener(getIntentListenerClass()); } @Override diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/widget/AbstractCalculatorWidgetProvider.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/widget/AbstractCalculatorWidgetProvider.java index d1f89d09..925988f5 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/widget/AbstractCalculatorWidgetProvider.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/widget/AbstractCalculatorWidgetProvider.java @@ -11,7 +11,6 @@ import android.widget.RemoteViews; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.solovyev.android.calculator.*; -import org.solovyev.android.calculator.external.ExternalCalculatorHelper; import org.solovyev.android.calculator.external.ExternalCalculatorIntentHandler; import org.solovyev.android.calculator.external.ExternalCalculatorStateUpdater; @@ -53,7 +52,7 @@ abstract class AbstractCalculatorWidgetProvider extends AppWidgetProvider implem protected AbstractCalculatorWidgetProvider() { final Class componentClass = this.getComponentClass(); - ExternalCalculatorHelper.addExternalListener(componentClass); + CalculatorWidgetHelper.addExternalListener(componentClass); } /* diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidgetHelper.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidgetHelper.java index b38b27f3..e0e2b1c8 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidgetHelper.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidgetHelper.java @@ -1,9 +1,26 @@ package org.solovyev.android.calculator.widget; +import android.content.Context; +import android.content.Intent; +import android.os.Parcelable; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.solovyev.android.calculator.*; -import org.solovyev.android.calculator.external.ExternalCalculatorHelper; +import org.solovyev.android.calculator.CalculatorApplication; +import org.solovyev.android.calculator.CalculatorDisplayChangeEventData; +import org.solovyev.android.calculator.CalculatorDisplayViewState; +import org.solovyev.android.calculator.CalculatorEditorChangeEventData; +import org.solovyev.android.calculator.CalculatorEditorViewState; +import org.solovyev.android.calculator.CalculatorEventData; +import org.solovyev.android.calculator.CalculatorEventHolder; +import org.solovyev.android.calculator.CalculatorEventListener; +import org.solovyev.android.calculator.CalculatorEventType; +import org.solovyev.android.calculator.CalculatorLocatorImpl; +import org.solovyev.android.calculator.CalculatorUtils; +import org.solovyev.android.calculator.ParcelableCalculatorDisplayViewState; +import org.solovyev.android.calculator.ParcelableCalculatorEditorViewState; + +import java.util.HashSet; +import java.util.Set; /** * User: serso @@ -12,16 +29,66 @@ import org.solovyev.android.calculator.external.ExternalCalculatorHelper; */ public class CalculatorWidgetHelper implements CalculatorEventListener { - private static final String TAG = "Calculator++ Widget Helper"; + /* + ********************************************************************** + * + * CONSTANTS + * + ********************************************************************** + */ + public static final String EVENT_ID_EXTRA = "eventId"; + public static final String EDITOR_STATE_CHANGED_ACTION = "org.solovyev.calculator.widget.EDITOR_STATE_CHANGED"; + public static final String EDITOR_STATE_EXTRA = "editorState"; + public static final String DISPLAY_STATE_CHANGED_ACTION = "org.solovyev.calculator.widget.DISPLAY_STATE_CHANGED"; + public static final String DISPLAY_STATE_EXTRA = "displayState"; - @NotNull + private static final String TAG = "Calculator++ External Listener Helper"; + + private static final Set> externalListeners = new HashSet>(); + + @NotNull private final CalculatorEventHolder lastEvent = new CalculatorEventHolder(CalculatorUtils.createFirstEventDataId()); public CalculatorWidgetHelper() { CalculatorLocatorImpl.getInstance().getCalculator().addCalculatorEventListener(this); } - @Override + public static void onEditorStateChanged(@NotNull Context context, + @NotNull CalculatorEventData calculatorEventData, + @NotNull CalculatorEditorViewState editorViewState) { + + for (Class externalListener : externalListeners) { + final Intent intent = new Intent(EDITOR_STATE_CHANGED_ACTION); + intent.setClass(context, externalListener); + intent.putExtra(EVENT_ID_EXTRA, calculatorEventData.getEventId()); + intent.putExtra(EDITOR_STATE_EXTRA, (Parcelable) new ParcelableCalculatorEditorViewState(editorViewState)); + context.sendBroadcast(intent); + CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Editor state changed broadcast sent"); + } + } + + public static void onDisplayStateChanged(@NotNull Context context, + @NotNull CalculatorEventData calculatorEventData, + @NotNull CalculatorDisplayViewState displayViewState) { + for (Class externalListener : externalListeners) { + final Intent intent = new Intent(DISPLAY_STATE_CHANGED_ACTION); + intent.setClass(context, externalListener); + intent.putExtra(EVENT_ID_EXTRA, calculatorEventData.getEventId()); + intent.putExtra(DISPLAY_STATE_EXTRA, (Parcelable) new ParcelableCalculatorDisplayViewState(displayViewState)); + context.sendBroadcast(intent); + CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Display state changed broadcast sent"); + } + } + + public static void addExternalListener(@NotNull Class externalCalculatorClass) { + externalListeners.add(externalCalculatorClass); + } + + public static boolean removeExternalListener(@NotNull Class externalCalculatorClass) { + return externalListeners.remove(externalCalculatorClass); + } + + @Override public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) { final CalculatorEventHolder.Result result = lastEvent.apply(calculatorEventData); if (result.isNewAfter()) { @@ -33,7 +100,7 @@ public class CalculatorWidgetHelper implements CalculatorEventListener { CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Editor state changed: " + newEditorState.getText()); - ExternalCalculatorHelper.onEditorStateChanged(CalculatorApplication.getInstance(), calculatorEventData, newEditorState); + onEditorStateChanged(CalculatorApplication.getInstance(), calculatorEventData, newEditorState); break; case display_state_changed: @@ -42,7 +109,7 @@ public class CalculatorWidgetHelper implements CalculatorEventListener { CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Display state changed: " + newDisplayState.getText()); - ExternalCalculatorHelper.onDisplayStateChanged(CalculatorApplication.getInstance(), calculatorEventData, newDisplayState); + onDisplayStateChanged(CalculatorApplication.getInstance(), calculatorEventData, newDisplayState); break; } }