diff --git a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorLocator.java b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorLocator.java index ef3f49a1..458d0271 100644 --- a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorLocator.java +++ b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorLocator.java @@ -1,6 +1,7 @@ package org.solovyev.android.calculator; import org.jetbrains.annotations.NotNull; +import org.solovyev.android.calculator.external.CalculatorExternalListenersContainer; import org.solovyev.android.calculator.history.CalculatorHistory; /** @@ -17,7 +18,8 @@ public interface CalculatorLocator { @NotNull CalculatorHistory history, @NotNull CalculatorLogger logger, @NotNull CalculatorPreferenceService preferenceService, - @NotNull CalculatorKeyboard keyboard); + @NotNull CalculatorKeyboard keyboard, + @NotNull CalculatorExternalListenersContainer externalListenersContainer); @NotNull Calculator getCalculator(); @@ -48,4 +50,7 @@ public interface CalculatorLocator { @NotNull CalculatorPreferenceService getPreferenceService(); + + @NotNull + CalculatorExternalListenersContainer getExternalListenersContainer(); } diff --git a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorLocatorImpl.java b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorLocatorImpl.java index e427a45e..42e179cf 100644 --- a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorLocatorImpl.java +++ b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorLocatorImpl.java @@ -1,6 +1,7 @@ package org.solovyev.android.calculator; import org.jetbrains.annotations.NotNull; +import org.solovyev.android.calculator.external.CalculatorExternalListenersContainer; import org.solovyev.android.calculator.history.CalculatorHistory; /** @@ -43,6 +44,9 @@ public class CalculatorLocatorImpl implements CalculatorLocator { @NotNull private CalculatorPreferenceService calculatorPreferenceService; + @NotNull + private CalculatorExternalListenersContainer calculatorExternalListenersContainer; + public CalculatorLocatorImpl() { } @@ -54,7 +58,8 @@ public class CalculatorLocatorImpl implements CalculatorLocator { @NotNull CalculatorHistory history, @NotNull CalculatorLogger logger, @NotNull CalculatorPreferenceService preferenceService, - @NotNull CalculatorKeyboard keyboard) { + @NotNull CalculatorKeyboard keyboard, + @NotNull CalculatorExternalListenersContainer externalListenersContainer) { this.calculator = calculator; this.calculatorEngine = engine; @@ -63,6 +68,7 @@ public class CalculatorLocatorImpl implements CalculatorLocator { this.calculatorHistory = history; this.calculatorLogger = logger; this.calculatorPreferenceService = preferenceService; + this.calculatorExternalListenersContainer = externalListenersContainer; calculatorEditor = new CalculatorEditorImpl(this.calculator); calculatorDisplay = new CalculatorDisplayImpl(this.calculator); @@ -133,4 +139,10 @@ public class CalculatorLocatorImpl implements CalculatorLocator { public CalculatorPreferenceService getPreferenceService() { return this.calculatorPreferenceService; } + + @Override + @NotNull + public CalculatorExternalListenersContainer getExternalListenersContainer() { + return calculatorExternalListenersContainer; + } } diff --git a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/external/CalculatorExternalListenersContainer.java b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/external/CalculatorExternalListenersContainer.java new file mode 100644 index 00000000..b2a87669 --- /dev/null +++ b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/external/CalculatorExternalListenersContainer.java @@ -0,0 +1,10 @@ +package org.solovyev.android.calculator.external; + +import org.jetbrains.annotations.NotNull; + +public interface CalculatorExternalListenersContainer { + + void addExternalListener(@NotNull Class externalCalculatorClass); + + boolean removeExternalListener(@NotNull Class externalCalculatorClass); +} diff --git a/calculatorpp-core/src/test/java/org/solovyev/android/calculator/AbstractCalculatorTest.java b/calculatorpp-core/src/test/java/org/solovyev/android/calculator/AbstractCalculatorTest.java index 4483804e..080a90dc 100644 --- a/calculatorpp-core/src/test/java/org/solovyev/android/calculator/AbstractCalculatorTest.java +++ b/calculatorpp-core/src/test/java/org/solovyev/android/calculator/AbstractCalculatorTest.java @@ -1,6 +1,7 @@ package org.solovyev.android.calculator; import org.mockito.Mockito; +import org.solovyev.android.calculator.external.CalculatorExternalListenersContainer; import org.solovyev.android.calculator.history.CalculatorHistory; /** @@ -11,7 +12,7 @@ import org.solovyev.android.calculator.history.CalculatorHistory; public class AbstractCalculatorTest { protected void setUp() throws Exception { - CalculatorLocatorImpl.getInstance().init(new CalculatorImpl(), CalculatorTestUtils.newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), Mockito.mock(CalculatorHistory.class), new SystemOutCalculatorLogger(), Mockito.mock(CalculatorPreferenceService.class), Mockito.mock(CalculatorKeyboard.class)); + CalculatorLocatorImpl.getInstance().init(new CalculatorImpl(), CalculatorTestUtils.newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), Mockito.mock(CalculatorHistory.class), new SystemOutCalculatorLogger(), Mockito.mock(CalculatorPreferenceService.class), Mockito.mock(CalculatorKeyboard.class), Mockito.mock(CalculatorExternalListenersContainer.class)); CalculatorLocatorImpl.getInstance().getEngine().init(); } diff --git a/calculatorpp-core/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java b/calculatorpp-core/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java index d024db58..20d4335a 100644 --- a/calculatorpp-core/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java +++ b/calculatorpp-core/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java @@ -5,6 +5,7 @@ import junit.framework.Assert; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.mockito.Mockito; +import org.solovyev.android.calculator.external.CalculatorExternalListenersContainer; import org.solovyev.android.calculator.history.CalculatorHistory; import org.solovyev.android.calculator.jscl.JsclOperation; @@ -23,7 +24,7 @@ public class CalculatorTestUtils { public static final int TIMEOUT = 3; public static void staticSetUp() throws Exception { - CalculatorLocatorImpl.getInstance().init(new CalculatorImpl(), newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), Mockito.mock(CalculatorHistory.class), new SystemOutCalculatorLogger(), Mockito.mock(CalculatorPreferenceService.class), Mockito.mock(CalculatorKeyboard.class)); + CalculatorLocatorImpl.getInstance().init(new CalculatorImpl(), newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), Mockito.mock(CalculatorHistory.class), new SystemOutCalculatorLogger(), Mockito.mock(CalculatorPreferenceService.class), Mockito.mock(CalculatorKeyboard.class), Mockito.mock(CalculatorExternalListenersContainer.class)); CalculatorLocatorImpl.getInstance().getEngine().init(); } diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java index 98364902..b16eb04c 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java @@ -11,10 +11,10 @@ import org.acra.ReportingInteractionMode; import org.acra.annotation.ReportsCrashes; import org.jetbrains.annotations.NotNull; import org.solovyev.android.ads.AdsController; +import org.solovyev.android.calculator.external.AndroidExternalListenersContainer; import org.solovyev.android.calculator.history.AndroidCalculatorHistory; import org.solovyev.android.calculator.model.AndroidCalculatorEngine; import org.solovyev.android.calculator.overlay.CalculatorOverlayService; -import org.solovyev.android.calculator.widget.CalculatorWidgetHelper; /** * User: serso @@ -49,11 +49,6 @@ public class CalculatorApplication extends android.app.Application { @NotNull private static CalculatorApplication instance; - - @SuppressWarnings("FieldCanBeLocal")// in order not to be garbage collected - @NotNull - private CalculatorWidgetHelper widgetHelper; - /* ********************************************************************** * @@ -94,14 +89,11 @@ public class CalculatorApplication extends android.app.Application { new AndroidCalculatorHistory(this, calculator), new AndroidCalculatorLogger(), new AndroidCalculatorPreferenceService(this), - new AndroidCalculatorKeyboard(this, new CalculatorKeyboardImpl(calculator))); + new AndroidCalculatorKeyboard(this, new CalculatorKeyboardImpl(calculator)), + new AndroidExternalListenersContainer(calculator)); CalculatorLocatorImpl.getInstance().getCalculator().init(); - // in order to not to be garbage collected - widgetHelper = new CalculatorWidgetHelper(); - CalculatorLocatorImpl.getInstance().getCalculator().addCalculatorEventListener(widgetHelper); - BillingDB.init(CalculatorApplication.this); AdsController.getInstance().init(ADMOB_USER_ID, AD_FREE_PRODUCT_ID, new BillingController.IConfiguration() { diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidgetHelper.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/external/AndroidExternalListenersContainer.java similarity index 87% rename from calculatorpp/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidgetHelper.java rename to calculatorpp/src/main/java/org/solovyev/android/calculator/external/AndroidExternalListenersContainer.java index e0e2b1c8..49a25da2 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidgetHelper.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/external/AndroidExternalListenersContainer.java @@ -1,10 +1,11 @@ -package org.solovyev.android.calculator.widget; +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.jetbrains.annotations.Nullable; +import org.solovyev.android.calculator.Calculator; import org.solovyev.android.calculator.CalculatorApplication; import org.solovyev.android.calculator.CalculatorDisplayChangeEventData; import org.solovyev.android.calculator.CalculatorDisplayViewState; @@ -27,7 +28,7 @@ import java.util.Set; * Date: 10/19/12 * Time: 11:11 PM */ -public class CalculatorWidgetHelper implements CalculatorEventListener { +public class AndroidExternalListenersContainer implements CalculatorExternalListenersContainer, CalculatorEventListener { /* ********************************************************************** @@ -44,16 +45,16 @@ public class CalculatorWidgetHelper implements CalculatorEventListener { private static final String TAG = "Calculator++ External Listener Helper"; - private static final Set> externalListeners = new HashSet>(); + private final Set> externalListeners = new HashSet>(); @NotNull private final CalculatorEventHolder lastEvent = new CalculatorEventHolder(CalculatorUtils.createFirstEventDataId()); - public CalculatorWidgetHelper() { - CalculatorLocatorImpl.getInstance().getCalculator().addCalculatorEventListener(this); - } + public AndroidExternalListenersContainer(@NotNull Calculator calculator) { + calculator.addCalculatorEventListener(this); + } - public static void onEditorStateChanged(@NotNull Context context, + public void onEditorStateChanged(@NotNull Context context, @NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEditorViewState editorViewState) { @@ -67,7 +68,7 @@ public class CalculatorWidgetHelper implements CalculatorEventListener { } } - public static void onDisplayStateChanged(@NotNull Context context, + private void onDisplayStateChanged(@NotNull Context context, @NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorDisplayViewState displayViewState) { for (Class externalListener : externalListeners) { @@ -80,11 +81,13 @@ public class CalculatorWidgetHelper implements CalculatorEventListener { } } - public static void addExternalListener(@NotNull Class externalCalculatorClass) { + @Override + public void addExternalListener(@NotNull Class externalCalculatorClass) { externalListeners.add(externalCalculatorClass); } - public static boolean removeExternalListener(@NotNull Class externalCalculatorClass) { + @Override + public boolean removeExternalListener(@NotNull Class externalCalculatorClass) { return externalListeners.remove(externalCalculatorClass); } 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 b4c88bcf..d63b3406 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,7 +7,6 @@ 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; /** @@ -35,10 +34,10 @@ public class DefaultExternalCalculatorIntentHandler implements ExternalCalculato @Override public void onIntent(@NotNull Context context, @NotNull Intent intent) { - if (CalculatorWidgetHelper.EDITOR_STATE_CHANGED_ACTION.equals(intent.getAction())) { + if (AndroidExternalListenersContainer.EDITOR_STATE_CHANGED_ACTION.equals(intent.getAction())) { CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Editor state changed broadcast received!"); - final Long eventId = intent.getLongExtra(CalculatorWidgetHelper.EVENT_ID_EXTRA, 0L); + final Long eventId = intent.getLongExtra(AndroidExternalListenersContainer.EVENT_ID_EXTRA, 0L); boolean updateEditor = false; synchronized (lastEditorEventId) { @@ -49,15 +48,15 @@ public class DefaultExternalCalculatorIntentHandler implements ExternalCalculato } if (updateEditor) { - final Parcelable object = intent.getParcelableExtra(CalculatorWidgetHelper.EDITOR_STATE_EXTRA); + final Parcelable object = intent.getParcelableExtra(AndroidExternalListenersContainer.EDITOR_STATE_EXTRA); if (object instanceof CalculatorEditorViewState) { onEditorStateChanged(context, (CalculatorEditorViewState) object); } } - } else if (CalculatorWidgetHelper.DISPLAY_STATE_CHANGED_ACTION.equals(intent.getAction())) { + } else if (AndroidExternalListenersContainer.DISPLAY_STATE_CHANGED_ACTION.equals(intent.getAction())) { CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Display state changed broadcast received!"); - final Long eventId = intent.getLongExtra(CalculatorWidgetHelper.EVENT_ID_EXTRA, 0L); + final Long eventId = intent.getLongExtra(AndroidExternalListenersContainer.EVENT_ID_EXTRA, 0L); boolean updateDisplay = false; synchronized (lastDisplayEventId) { if (eventId > lastDisplayEventId.getObject()) { @@ -67,7 +66,7 @@ public class DefaultExternalCalculatorIntentHandler implements ExternalCalculato } if (updateDisplay) { - final Parcelable object = intent.getParcelableExtra(CalculatorWidgetHelper.DISPLAY_STATE_EXTRA); + final Parcelable object = intent.getParcelableExtra(AndroidExternalListenersContainer.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 deleted file mode 100644 index 06dfbc73..00000000 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/external/ExternalCalculatorHelper.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.solovyev.android.calculator.external; - -/** - * User: serso - * Date: 11/20/12 - * Time: 10:14 PM - */ -public class ExternalCalculatorHelper { - -} 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 2beb30de..ed5b1cb9 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 @@ -14,11 +14,11 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.solovyev.android.calculator.CalculatorDisplayViewState; import org.solovyev.android.calculator.CalculatorEditorViewState; +import org.solovyev.android.calculator.CalculatorLocatorImpl; import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.external.DefaultExternalCalculatorIntentHandler; 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() { - CalculatorWidgetHelper.addExternalListener(getIntentListenerClass()); + CalculatorLocatorImpl.getInstance().getExternalListenersContainer().addExternalListener(getIntentListenerClass()); } @NotNull @@ -84,7 +84,7 @@ public class CalculatorOverlayService extends Service implements ExternalCalcula } private void stopCalculatorListening() { - CalculatorWidgetHelper.removeExternalListener(getIntentListenerClass()); + CalculatorLocatorImpl.getInstance().getExternalListenersContainer().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 925988f5..633546aa 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 @@ -10,7 +10,11 @@ import android.text.Html; import android.widget.RemoteViews; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.solovyev.android.calculator.*; +import org.solovyev.android.calculator.CalculatorButtons; +import org.solovyev.android.calculator.CalculatorDisplayViewState; +import org.solovyev.android.calculator.CalculatorEditorViewState; +import org.solovyev.android.calculator.CalculatorLocatorImpl; +import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.external.ExternalCalculatorIntentHandler; import org.solovyev.android.calculator.external.ExternalCalculatorStateUpdater; @@ -52,7 +56,7 @@ abstract class AbstractCalculatorWidgetProvider extends AppWidgetProvider implem protected AbstractCalculatorWidgetProvider() { final Class componentClass = this.getComponentClass(); - CalculatorWidgetHelper.addExternalListener(componentClass); + CalculatorLocatorImpl.getInstance().getExternalListenersContainer().addExternalListener(componentClass); } /* diff --git a/calculatorpp/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java b/calculatorpp/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java index 71229446..60010215 100644 --- a/calculatorpp/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java +++ b/calculatorpp/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java @@ -5,6 +5,7 @@ import jscl.JsclMathEngine; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.mockito.Mockito; +import org.solovyev.android.calculator.external.CalculatorExternalListenersContainer; import org.solovyev.android.calculator.history.CalculatorHistory; /** @@ -15,7 +16,7 @@ import org.solovyev.android.calculator.history.CalculatorHistory; public class CalculatorTestUtils { public static void staticSetUp(@Nullable Context context) throws Exception { - CalculatorLocatorImpl.getInstance().init(new CalculatorImpl(), newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), Mockito.mock(CalculatorHistory.class), new SystemOutCalculatorLogger(), Mockito.mock(CalculatorPreferenceService.class), Mockito.mock(CalculatorKeyboard.class)); + CalculatorLocatorImpl.getInstance().init(new CalculatorImpl(), newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), Mockito.mock(CalculatorHistory.class), new SystemOutCalculatorLogger(), Mockito.mock(CalculatorPreferenceService.class), Mockito.mock(CalculatorKeyboard.class), Mockito.mock(CalculatorExternalListenersContainer.class)); CalculatorLocatorImpl.getInstance().getEngine().init(); if ( context != null ) {