From 344018a44ca152bc55a1ab59c906fc2256a742a7 Mon Sep 17 00:00:00 2001 From: Sergey Solovyev Date: Sun, 18 Nov 2012 16:22:14 +0400 Subject: [PATCH] Changes --- .../android/calculator/CalculatorImpl.java | 79 +++++++++++++++---- .../calculator_messages_dialog_message.xml | 4 +- .../AndroidCalculatorPreferenceService.java | 2 +- 3 files changed, 68 insertions(+), 17 deletions(-) diff --git a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java index 191a471a..62fef50a 100644 --- a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java +++ b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java @@ -38,6 +38,25 @@ import java.util.concurrent.atomic.AtomicLong; */ public class CalculatorImpl implements Calculator, CalculatorEventListener { + /* + ********************************************************************** + * + * CONSTANTS + * + ********************************************************************** + */ + + // one minute + private static final long PREFERENCE_CHECK_INTERVAL = 1000L * 60L; + + /* + ********************************************************************** + * + * FIELDS + * + ********************************************************************** + */ + @NotNull private final CalculatorEventContainer calculatorEventContainer = new ListCalculatorEventContainer(); @@ -56,10 +75,29 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { private volatile boolean calculateOnFly = true; + private volatile long lastPreferenceCheck = 0L; + + + /* + ********************************************************************** + * + * CONSTRUCTORS + * + ********************************************************************** + */ + public CalculatorImpl() { this.addCalculatorEventListener(this); } + /* + ********************************************************************** + * + * METHODS + * + ********************************************************************** + */ + @NotNull private CalculatorEventData nextEventData() { long eventId = counter.incrementAndGet(); @@ -163,7 +201,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { @NotNull String expression, @Nullable MessageRegistry mr) { - CalculatorLocatorImpl.getInstance().getPreferenceService().checkPreferredPreferences(false); + checkPreferredPreferences(); PreparedExpression preparedExpression = null; @@ -190,21 +228,23 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { // NOTE: toString() method must be called here as ArithmeticOperationException may occur in it (just to avoid later check!) result.toString(); - final CalculatorLogger logger = CalculatorLocatorImpl.getInstance().getLogger(); - try { - final List messages = new ArrayList(); - while (messageRegistry.hasMessage()) { - messages.add(messageRegistry.getMessage()); - } - if (!messages.isEmpty()) { - fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_messages, messages); + if (messageRegistry.hasMessage()) { + final CalculatorLogger logger = CalculatorLocatorImpl.getInstance().getLogger(); + try { + final List messages = new ArrayList(); + while (messageRegistry.hasMessage()) { + messages.add(messageRegistry.getMessage()); + } + if (!messages.isEmpty()) { + fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_messages, messages); + } + } catch (Throwable e) { + // todo serso: not good but we need proper synchronization + logger.error("Calculator", e.getMessage(), e); } - } catch (Throwable e) { - // todo serso: not good be we need proper synchronization - logger.error("Calculator", e.getMessage(), e); - } + } - final CalculatorOutput data = CalculatorOutputImpl.newOutput(operation.getFromProcessor().process(result), operation, result); + final CalculatorOutput data = CalculatorOutputImpl.newOutput(operation.getFromProcessor().process(result), operation, result); fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_result, data); } catch (AbstractJsclArithmeticException e) { @@ -228,7 +268,16 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { } } - @NotNull + private void checkPreferredPreferences() { + final long currentTime = System.currentTimeMillis(); + + if ( currentTime - lastPreferenceCheck > PREFERENCE_CHECK_INTERVAL ) { + lastPreferenceCheck = currentTime; + CalculatorLocatorImpl.getInstance().getPreferenceService().checkPreferredPreferences(false); + } + } + + @NotNull @Override public PreparedExpression prepareExpression(@NotNull String expression) throws CalculatorParseException { return preprocessor.process(expression); diff --git a/calculatorpp/res/layout/calculator_messages_dialog_message.xml b/calculatorpp/res/layout/calculator_messages_dialog_message.xml index 497f4178..ac9db718 100644 --- a/calculatorpp/res/layout/calculator_messages_dialog_message.xml +++ b/calculatorpp/res/layout/calculator_messages_dialog_message.xml @@ -9,7 +9,9 @@ + a:layout_weight="5" + a:layout_gravity="fill" + a:text="@+id/calculation_messages_text_view"/>