refactor
This commit is contained in:
parent
6cd382e273
commit
60f1bd1e73
@ -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();
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
@ -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();
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
@ -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() {
|
||||
|
@ -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<Class<?>> externalListeners = new HashSet<Class<?>>();
|
||||
private final Set<Class<?>> externalListeners = new HashSet<Class<?>>();
|
||||
|
||||
@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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -1,10 +0,0 @@
|
||||
package org.solovyev.android.calculator.external;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 11/20/12
|
||||
* Time: 10:14 PM
|
||||
*/
|
||||
public class ExternalCalculatorHelper {
|
||||
|
||||
}
|
@ -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
|
||||
|
@ -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<? extends AppWidgetProvider> componentClass = this.getComponentClass();
|
||||
|
||||
CalculatorWidgetHelper.addExternalListener(componentClass);
|
||||
CalculatorLocatorImpl.getInstance().getExternalListenersContainer().addExternalListener(componentClass);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -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 ) {
|
||||
|
Loading…
Reference in New Issue
Block a user