refactor
This commit is contained in:
parent
d59f7b8b50
commit
6cd382e273
@ -7,6 +7,7 @@ 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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -34,10 +35,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 (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!");
|
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;
|
boolean updateEditor = false;
|
||||||
synchronized (lastEditorEventId) {
|
synchronized (lastEditorEventId) {
|
||||||
@ -48,15 +49,15 @@ public class DefaultExternalCalculatorIntentHandler implements ExternalCalculato
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (updateEditor) {
|
if (updateEditor) {
|
||||||
final Parcelable object = intent.getParcelableExtra(ExternalCalculatorHelper.EDITOR_STATE_EXTRA);
|
final Parcelable object = intent.getParcelableExtra(CalculatorWidgetHelper.EDITOR_STATE_EXTRA);
|
||||||
if (object instanceof CalculatorEditorViewState) {
|
if (object instanceof CalculatorEditorViewState) {
|
||||||
onEditorStateChanged(context, (CalculatorEditorViewState) object);
|
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!");
|
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;
|
boolean updateDisplay = false;
|
||||||
synchronized (lastDisplayEventId) {
|
synchronized (lastDisplayEventId) {
|
||||||
if (eventId > lastDisplayEventId.getObject()) {
|
if (eventId > lastDisplayEventId.getObject()) {
|
||||||
@ -66,7 +67,7 @@ public class DefaultExternalCalculatorIntentHandler implements ExternalCalculato
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (updateDisplay) {
|
if (updateDisplay) {
|
||||||
final Parcelable object = intent.getParcelableExtra(ExternalCalculatorHelper.DISPLAY_STATE_EXTRA);
|
final Parcelable object = intent.getParcelableExtra(CalculatorWidgetHelper.DISPLAY_STATE_EXTRA);
|
||||||
if (object instanceof CalculatorDisplayViewState) {
|
if (object instanceof CalculatorDisplayViewState) {
|
||||||
onDisplayStateChanged(context, (CalculatorDisplayViewState) object);
|
onDisplayStateChanged(context, (CalculatorDisplayViewState) object);
|
||||||
}
|
}
|
||||||
|
@ -1,69 +1,10 @@
|
|||||||
package org.solovyev.android.calculator.external;
|
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
|
* User: serso
|
||||||
* Date: 11/20/12
|
* Date: 11/20/12
|
||||||
* Time: 10:14 PM
|
* Time: 10:14 PM
|
||||||
*/
|
*/
|
||||||
public class ExternalCalculatorHelper {
|
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<Class<?>> externalListeners = new HashSet<Class<?>>();
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -16,9 +16,9 @@ import org.solovyev.android.calculator.CalculatorDisplayViewState;
|
|||||||
import org.solovyev.android.calculator.CalculatorEditorViewState;
|
import org.solovyev.android.calculator.CalculatorEditorViewState;
|
||||||
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.ExternalCalculatorHelper;
|
|
||||||
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() {
|
||||||
ExternalCalculatorHelper.addExternalListener(getIntentListenerClass());
|
CalculatorWidgetHelper.addExternalListener(getIntentListenerClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@ -84,7 +84,7 @@ public class CalculatorOverlayService extends Service implements ExternalCalcula
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void stopCalculatorListening() {
|
private void stopCalculatorListening() {
|
||||||
ExternalCalculatorHelper.removeExternalListener(getIntentListenerClass());
|
CalculatorWidgetHelper.removeExternalListener(getIntentListenerClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -11,7 +11,6 @@ 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.*;
|
||||||
import org.solovyev.android.calculator.external.ExternalCalculatorHelper;
|
|
||||||
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;
|
||||||
|
|
||||||
@ -53,7 +52,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();
|
||||||
|
|
||||||
ExternalCalculatorHelper.addExternalListener(componentClass);
|
CalculatorWidgetHelper.addExternalListener(componentClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -1,9 +1,26 @@
|
|||||||
package org.solovyev.android.calculator.widget;
|
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.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.solovyev.android.calculator.*;
|
import org.solovyev.android.calculator.CalculatorApplication;
|
||||||
import org.solovyev.android.calculator.external.ExternalCalculatorHelper;
|
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
|
* User: serso
|
||||||
@ -12,16 +29,66 @@ import org.solovyev.android.calculator.external.ExternalCalculatorHelper;
|
|||||||
*/
|
*/
|
||||||
public class CalculatorWidgetHelper implements CalculatorEventListener {
|
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<Class<?>> externalListeners = new HashSet<Class<?>>();
|
||||||
|
|
||||||
|
@NotNull
|
||||||
private final CalculatorEventHolder lastEvent = new CalculatorEventHolder(CalculatorUtils.createFirstEventDataId());
|
private final CalculatorEventHolder lastEvent = new CalculatorEventHolder(CalculatorUtils.createFirstEventDataId());
|
||||||
|
|
||||||
public CalculatorWidgetHelper() {
|
public CalculatorWidgetHelper() {
|
||||||
CalculatorLocatorImpl.getInstance().getCalculator().addCalculatorEventListener(this);
|
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) {
|
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
|
||||||
final CalculatorEventHolder.Result result = lastEvent.apply(calculatorEventData);
|
final CalculatorEventHolder.Result result = lastEvent.apply(calculatorEventData);
|
||||||
if (result.isNewAfter()) {
|
if (result.isNewAfter()) {
|
||||||
@ -33,7 +100,7 @@ public class CalculatorWidgetHelper implements CalculatorEventListener {
|
|||||||
|
|
||||||
CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Editor state changed: " + newEditorState.getText());
|
CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Editor state changed: " + newEditorState.getText());
|
||||||
|
|
||||||
ExternalCalculatorHelper.onEditorStateChanged(CalculatorApplication.getInstance(), calculatorEventData, newEditorState);
|
onEditorStateChanged(CalculatorApplication.getInstance(), calculatorEventData, newEditorState);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case display_state_changed:
|
case display_state_changed:
|
||||||
@ -42,7 +109,7 @@ public class CalculatorWidgetHelper implements CalculatorEventListener {
|
|||||||
|
|
||||||
CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Display state changed: " + newDisplayState.getText());
|
CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Display state changed: " + newDisplayState.getText());
|
||||||
|
|
||||||
ExternalCalculatorHelper.onDisplayStateChanged(CalculatorApplication.getInstance(), calculatorEventData, newDisplayState);
|
onDisplayStateChanged(CalculatorApplication.getInstance(), calculatorEventData, newDisplayState);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user