This commit is contained in:
Sergey Solovyev 2012-11-21 11:54:45 +04:00
parent 6cd382e273
commit 60f1bd1e73
12 changed files with 66 additions and 48 deletions

View File

@ -1,6 +1,7 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.external.CalculatorExternalListenersContainer;
import org.solovyev.android.calculator.history.CalculatorHistory; import org.solovyev.android.calculator.history.CalculatorHistory;
/** /**
@ -17,7 +18,8 @@ public interface CalculatorLocator {
@NotNull CalculatorHistory history, @NotNull CalculatorHistory history,
@NotNull CalculatorLogger logger, @NotNull CalculatorLogger logger,
@NotNull CalculatorPreferenceService preferenceService, @NotNull CalculatorPreferenceService preferenceService,
@NotNull CalculatorKeyboard keyboard); @NotNull CalculatorKeyboard keyboard,
@NotNull CalculatorExternalListenersContainer externalListenersContainer);
@NotNull @NotNull
Calculator getCalculator(); Calculator getCalculator();
@ -48,4 +50,7 @@ public interface CalculatorLocator {
@NotNull @NotNull
CalculatorPreferenceService getPreferenceService(); CalculatorPreferenceService getPreferenceService();
@NotNull
CalculatorExternalListenersContainer getExternalListenersContainer();
} }

View File

@ -1,6 +1,7 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.external.CalculatorExternalListenersContainer;
import org.solovyev.android.calculator.history.CalculatorHistory; import org.solovyev.android.calculator.history.CalculatorHistory;
/** /**
@ -43,6 +44,9 @@ public class CalculatorLocatorImpl implements CalculatorLocator {
@NotNull @NotNull
private CalculatorPreferenceService calculatorPreferenceService; private CalculatorPreferenceService calculatorPreferenceService;
@NotNull
private CalculatorExternalListenersContainer calculatorExternalListenersContainer;
public CalculatorLocatorImpl() { public CalculatorLocatorImpl() {
} }
@ -54,7 +58,8 @@ public class CalculatorLocatorImpl implements CalculatorLocator {
@NotNull CalculatorHistory history, @NotNull CalculatorHistory history,
@NotNull CalculatorLogger logger, @NotNull CalculatorLogger logger,
@NotNull CalculatorPreferenceService preferenceService, @NotNull CalculatorPreferenceService preferenceService,
@NotNull CalculatorKeyboard keyboard) { @NotNull CalculatorKeyboard keyboard,
@NotNull CalculatorExternalListenersContainer externalListenersContainer) {
this.calculator = calculator; this.calculator = calculator;
this.calculatorEngine = engine; this.calculatorEngine = engine;
@ -63,6 +68,7 @@ public class CalculatorLocatorImpl implements CalculatorLocator {
this.calculatorHistory = history; this.calculatorHistory = history;
this.calculatorLogger = logger; this.calculatorLogger = logger;
this.calculatorPreferenceService = preferenceService; this.calculatorPreferenceService = preferenceService;
this.calculatorExternalListenersContainer = externalListenersContainer;
calculatorEditor = new CalculatorEditorImpl(this.calculator); calculatorEditor = new CalculatorEditorImpl(this.calculator);
calculatorDisplay = new CalculatorDisplayImpl(this.calculator); calculatorDisplay = new CalculatorDisplayImpl(this.calculator);
@ -133,4 +139,10 @@ public class CalculatorLocatorImpl implements CalculatorLocator {
public CalculatorPreferenceService getPreferenceService() { public CalculatorPreferenceService getPreferenceService() {
return this.calculatorPreferenceService; return this.calculatorPreferenceService;
} }
@Override
@NotNull
public CalculatorExternalListenersContainer getExternalListenersContainer() {
return calculatorExternalListenersContainer;
}
} }

View File

@ -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);
}

View File

@ -1,6 +1,7 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.solovyev.android.calculator.external.CalculatorExternalListenersContainer;
import org.solovyev.android.calculator.history.CalculatorHistory; import org.solovyev.android.calculator.history.CalculatorHistory;
/** /**
@ -11,7 +12,7 @@ import org.solovyev.android.calculator.history.CalculatorHistory;
public class AbstractCalculatorTest { public class AbstractCalculatorTest {
protected void setUp() throws Exception { 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(); CalculatorLocatorImpl.getInstance().getEngine().init();
} }

View File

@ -5,6 +5,7 @@ import junit.framework.Assert;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.solovyev.android.calculator.external.CalculatorExternalListenersContainer;
import org.solovyev.android.calculator.history.CalculatorHistory; import org.solovyev.android.calculator.history.CalculatorHistory;
import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.android.calculator.jscl.JsclOperation;
@ -23,7 +24,7 @@ public class CalculatorTestUtils {
public static final int TIMEOUT = 3; public static final int TIMEOUT = 3;
public static void staticSetUp() throws Exception { 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(); CalculatorLocatorImpl.getInstance().getEngine().init();
} }

View File

@ -11,10 +11,10 @@ import org.acra.ReportingInteractionMode;
import org.acra.annotation.ReportsCrashes; import org.acra.annotation.ReportsCrashes;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.solovyev.android.ads.AdsController; 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.history.AndroidCalculatorHistory;
import org.solovyev.android.calculator.model.AndroidCalculatorEngine; import org.solovyev.android.calculator.model.AndroidCalculatorEngine;
import org.solovyev.android.calculator.overlay.CalculatorOverlayService; import org.solovyev.android.calculator.overlay.CalculatorOverlayService;
import org.solovyev.android.calculator.widget.CalculatorWidgetHelper;
/** /**
* User: serso * User: serso
@ -49,11 +49,6 @@ public class CalculatorApplication extends android.app.Application {
@NotNull @NotNull
private static CalculatorApplication instance; 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 AndroidCalculatorHistory(this, calculator),
new AndroidCalculatorLogger(), new AndroidCalculatorLogger(),
new AndroidCalculatorPreferenceService(this), new AndroidCalculatorPreferenceService(this),
new AndroidCalculatorKeyboard(this, new CalculatorKeyboardImpl(calculator))); new AndroidCalculatorKeyboard(this, new CalculatorKeyboardImpl(calculator)),
new AndroidExternalListenersContainer(calculator));
CalculatorLocatorImpl.getInstance().getCalculator().init(); CalculatorLocatorImpl.getInstance().getCalculator().init();
// in order to not to be garbage collected
widgetHelper = new CalculatorWidgetHelper();
CalculatorLocatorImpl.getInstance().getCalculator().addCalculatorEventListener(widgetHelper);
BillingDB.init(CalculatorApplication.this); BillingDB.init(CalculatorApplication.this);
AdsController.getInstance().init(ADMOB_USER_ID, AD_FREE_PRODUCT_ID, new BillingController.IConfiguration() { AdsController.getInstance().init(ADMOB_USER_ID, AD_FREE_PRODUCT_ID, new BillingController.IConfiguration() {

View File

@ -1,10 +1,11 @@
package org.solovyev.android.calculator.widget; package org.solovyev.android.calculator.external;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Parcelable; import android.os.Parcelable;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.Calculator;
import org.solovyev.android.calculator.CalculatorApplication; import org.solovyev.android.calculator.CalculatorApplication;
import org.solovyev.android.calculator.CalculatorDisplayChangeEventData; import org.solovyev.android.calculator.CalculatorDisplayChangeEventData;
import org.solovyev.android.calculator.CalculatorDisplayViewState; import org.solovyev.android.calculator.CalculatorDisplayViewState;
@ -27,7 +28,7 @@ import java.util.Set;
* Date: 10/19/12 * Date: 10/19/12
* Time: 11:11 PM * 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 String TAG = "Calculator++ External Listener Helper";
private static final Set<Class<?>> externalListeners = new HashSet<Class<?>>(); private final Set<Class<?>> externalListeners = new HashSet<Class<?>>();
@NotNull @NotNull
private final CalculatorEventHolder lastEvent = new CalculatorEventHolder(CalculatorUtils.createFirstEventDataId()); private final CalculatorEventHolder lastEvent = new CalculatorEventHolder(CalculatorUtils.createFirstEventDataId());
public CalculatorWidgetHelper() { public AndroidExternalListenersContainer(@NotNull Calculator calculator) {
CalculatorLocatorImpl.getInstance().getCalculator().addCalculatorEventListener(this); calculator.addCalculatorEventListener(this);
} }
public static void onEditorStateChanged(@NotNull Context context, public void onEditorStateChanged(@NotNull Context context,
@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventData calculatorEventData,
@NotNull CalculatorEditorViewState editorViewState) { @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 CalculatorEventData calculatorEventData,
@NotNull CalculatorDisplayViewState displayViewState) { @NotNull CalculatorDisplayViewState displayViewState) {
for (Class<?> externalListener : externalListeners) { 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); externalListeners.add(externalCalculatorClass);
} }
public static boolean removeExternalListener(@NotNull Class<?> externalCalculatorClass) { @Override
public boolean removeExternalListener(@NotNull Class<?> externalCalculatorClass) {
return externalListeners.remove(externalCalculatorClass); return externalListeners.remove(externalCalculatorClass);
} }

View File

@ -7,7 +7,6 @@ import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.CalculatorDisplayViewState; import org.solovyev.android.calculator.CalculatorDisplayViewState;
import org.solovyev.android.calculator.CalculatorEditorViewState; import org.solovyev.android.calculator.CalculatorEditorViewState;
import org.solovyev.android.calculator.CalculatorLocatorImpl; import org.solovyev.android.calculator.CalculatorLocatorImpl;
import org.solovyev.android.calculator.widget.CalculatorWidgetHelper;
import org.solovyev.common.MutableObject; import org.solovyev.common.MutableObject;
/** /**
@ -35,10 +34,10 @@ public class DefaultExternalCalculatorIntentHandler implements ExternalCalculato
@Override @Override
public void onIntent(@NotNull Context context, @NotNull Intent intent) { 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!"); 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; boolean updateEditor = false;
synchronized (lastEditorEventId) { synchronized (lastEditorEventId) {
@ -49,15 +48,15 @@ public class DefaultExternalCalculatorIntentHandler implements ExternalCalculato
} }
if (updateEditor) { if (updateEditor) {
final Parcelable object = intent.getParcelableExtra(CalculatorWidgetHelper.EDITOR_STATE_EXTRA); final Parcelable object = intent.getParcelableExtra(AndroidExternalListenersContainer.EDITOR_STATE_EXTRA);
if (object instanceof CalculatorEditorViewState) { if (object instanceof CalculatorEditorViewState) {
onEditorStateChanged(context, (CalculatorEditorViewState) object); 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!"); 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; boolean updateDisplay = false;
synchronized (lastDisplayEventId) { synchronized (lastDisplayEventId) {
if (eventId > lastDisplayEventId.getObject()) { if (eventId > lastDisplayEventId.getObject()) {
@ -67,7 +66,7 @@ public class DefaultExternalCalculatorIntentHandler implements ExternalCalculato
} }
if (updateDisplay) { if (updateDisplay) {
final Parcelable object = intent.getParcelableExtra(CalculatorWidgetHelper.DISPLAY_STATE_EXTRA); final Parcelable object = intent.getParcelableExtra(AndroidExternalListenersContainer.DISPLAY_STATE_EXTRA);
if (object instanceof CalculatorDisplayViewState) { if (object instanceof CalculatorDisplayViewState) {
onDisplayStateChanged(context, (CalculatorDisplayViewState) object); onDisplayStateChanged(context, (CalculatorDisplayViewState) object);
} }

View File

@ -1,10 +0,0 @@
package org.solovyev.android.calculator.external;
/**
* User: serso
* Date: 11/20/12
* Time: 10:14 PM
*/
public class ExternalCalculatorHelper {
}

View File

@ -14,11 +14,11 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.CalculatorDisplayViewState; import org.solovyev.android.calculator.CalculatorDisplayViewState;
import org.solovyev.android.calculator.CalculatorEditorViewState; import org.solovyev.android.calculator.CalculatorEditorViewState;
import org.solovyev.android.calculator.CalculatorLocatorImpl;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.external.DefaultExternalCalculatorIntentHandler; import org.solovyev.android.calculator.external.DefaultExternalCalculatorIntentHandler;
import org.solovyev.android.calculator.external.ExternalCalculatorIntentHandler; import org.solovyev.android.calculator.external.ExternalCalculatorIntentHandler;
import org.solovyev.android.calculator.external.ExternalCalculatorStateUpdater; import org.solovyev.android.calculator.external.ExternalCalculatorStateUpdater;
import org.solovyev.android.calculator.widget.CalculatorWidgetHelper;
import org.solovyev.android.calculator.widget.WidgetButton; import org.solovyev.android.calculator.widget.WidgetButton;
/** /**
@ -75,7 +75,7 @@ public class CalculatorOverlayService extends Service implements ExternalCalcula
} }
private void startCalculatorListening() { private void startCalculatorListening() {
CalculatorWidgetHelper.addExternalListener(getIntentListenerClass()); CalculatorLocatorImpl.getInstance().getExternalListenersContainer().addExternalListener(getIntentListenerClass());
} }
@NotNull @NotNull
@ -84,7 +84,7 @@ public class CalculatorOverlayService extends Service implements ExternalCalcula
} }
private void stopCalculatorListening() { private void stopCalculatorListening() {
CalculatorWidgetHelper.removeExternalListener(getIntentListenerClass()); CalculatorLocatorImpl.getInstance().getExternalListenersContainer().removeExternalListener(getIntentListenerClass());
} }
@Override @Override

View File

@ -10,7 +10,11 @@ import android.text.Html;
import android.widget.RemoteViews; import android.widget.RemoteViews;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; 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.ExternalCalculatorIntentHandler;
import org.solovyev.android.calculator.external.ExternalCalculatorStateUpdater; import org.solovyev.android.calculator.external.ExternalCalculatorStateUpdater;
@ -52,7 +56,7 @@ abstract class AbstractCalculatorWidgetProvider extends AppWidgetProvider implem
protected AbstractCalculatorWidgetProvider() { protected AbstractCalculatorWidgetProvider() {
final Class<? extends AppWidgetProvider> componentClass = this.getComponentClass(); final Class<? extends AppWidgetProvider> componentClass = this.getComponentClass();
CalculatorWidgetHelper.addExternalListener(componentClass); CalculatorLocatorImpl.getInstance().getExternalListenersContainer().addExternalListener(componentClass);
} }
/* /*

View File

@ -5,6 +5,7 @@ import jscl.JsclMathEngine;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.solovyev.android.calculator.external.CalculatorExternalListenersContainer;
import org.solovyev.android.calculator.history.CalculatorHistory; import org.solovyev.android.calculator.history.CalculatorHistory;
/** /**
@ -15,7 +16,7 @@ import org.solovyev.android.calculator.history.CalculatorHistory;
public class CalculatorTestUtils { public class CalculatorTestUtils {
public static void staticSetUp(@Nullable Context context) throws Exception { 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(); CalculatorLocatorImpl.getInstance().getEngine().init();
if ( context != null ) { if ( context != null ) {