From ef1508dcc72044282c8a051d8038ad42bc65fef7 Mon Sep 17 00:00:00 2001 From: serso Date: Sun, 11 Sep 2011 02:38:45 +0400 Subject: [PATCH] new interface implementation --- gen/org/solovyev/android/calculator/R.java | 60 ++-- res/drawable/blue_button.xml | 3 +- res/drawable/light_blue_button.xml | 6 + res/layout/main.xml | 318 +++++++++--------- res/values/styles.xml | 13 +- .../calculator/CalculatorActivity.java | 97 ++++-- .../calculator/CalculatorHistoryState.java | 40 +++ .../android/calculator/Preprocessor.java | 16 +- .../solovyev/android/view/PanelSwitcher.java | 144 -------- .../solovyev/util/math/MathEntityType.java | 2 +- 10 files changed, 330 insertions(+), 369 deletions(-) create mode 100644 res/drawable/light_blue_button.xml create mode 100644 src/org/solovyev/android/calculator/CalculatorHistoryState.java delete mode 100644 src/org/solovyev/android/view/PanelSwitcher.java diff --git a/gen/org/solovyev/android/calculator/R.java b/gen/org/solovyev/android/calculator/R.java index b7275ca9..9b960dbc 100644 --- a/gen/org/solovyev/android/calculator/R.java +++ b/gen/org/solovyev/android/calculator/R.java @@ -35,9 +35,10 @@ containing a value of this type. public static final int button=0x7f020001; public static final int down=0x7f020002; public static final int icon=0x7f020003; - public static final int not_ok=0x7f020004; - public static final int ok=0x7f020005; - public static final int up=0x7f020006; + public static final int light_blue_button=0x7f020004; + public static final int not_ok=0x7f020005; + public static final int ok=0x7f020006; + public static final int up=0x7f020007; } public static final class id { public static final int calibrationArrowLeft=0x7f090001; @@ -45,34 +46,32 @@ containing a value of this type. public static final int calibrationButtonLeft=0x7f090002; public static final int calibrationButtonRight=0x7f090003; public static final int calibrationStart=0x7f090005; - public static final int curlyBracketsButton=0x7f09001b; - public static final int divisionButton=0x7f090011; + public static final int divisionButton=0x7f09000f; public static final int editText=0x7f090006; - public static final int eigthDigitButton=0x7f090014; - public static final int equalsButton=0x7f09001d; - public static final int fiveDigitButton=0x7f09000f; - public static final int fourDigitButton=0x7f09000e; - public static final int historyButton=0x7f09001e; - public static final int menu_item_help=0x7f090020; - public static final int menu_item_settings=0x7f09001f; - public static final int muliplicationButton=0x7f09000c; - public static final int nineDigitButton=0x7f090015; - public static final int oneDigitButton=0x7f090009; - public static final int panelswitch=0x7f090008; - public static final int piButton=0x7f09001c; - public static final int plusButton=0x7f09000d; - public static final int pointDigitButton=0x7f090016; + public static final int eigthDigitButton=0x7f090011; + public static final int equalsButton=0x7f090018; + public static final int eraseButton=0x7f090019; + public static final int fiveDigitButton=0x7f09000d; + public static final int fourDigitButton=0x7f09000c; + public static final int historyButton=0x7f09001c; + public static final int leftButton=0x7f09001a; + public static final int menu_item_help=0x7f09001e; + public static final int menu_item_settings=0x7f09001d; + public static final int muliplicationButton=0x7f09000b; + public static final int nineDigitButton=0x7f090012; + public static final int oneDigitButton=0x7f090008; + public static final int plusButton=0x7f090013; public static final int resultEditText=0x7f090007; - public static final int roundBracketsButton=0x7f090019; - public static final int sevenDigitButton=0x7f090013; + public static final int rightButton=0x7f09001b; + public static final int roundBracketsButton=0x7f090014; + public static final int sevenDigitButton=0x7f090010; public static final int simplePad=0x7f090000; - public static final int sixDigitButton=0x7f090010; - public static final int sqrtButton=0x7f090017; - public static final int squareBracketsButton=0x7f09001a; - public static final int subtractionButton=0x7f090012; - public static final int threeDigitButton=0x7f09000b; - public static final int twoDigitButton=0x7f09000a; - public static final int zeroDigitButton=0x7f090018; + public static final int sixDigitButton=0x7f09000e; + public static final int squareBracketsButton=0x7f090016; + public static final int subtractionButton=0x7f090017; + public static final int threeDigitButton=0x7f09000a; + public static final int twoDigitButton=0x7f090009; + public static final int zeroDigitButton=0x7f090015; } public static final class layout { public static final int drag_button_calibration=0x7f030000; @@ -95,10 +94,11 @@ containing a value of this type. public static final int syntax_error=0x7f060002; } public static final class style { - public static final int button_small_style=0x7f070003; + public static final int button_small_style=0x7f070004; public static final int button_style=0x7f070001; + public static final int controlButtonStyle=0x7f070003; public static final int digitButtonStyle=0x7f070002; - public static final int display_style=0x7f070004; + public static final int display_style=0x7f070005; public static final int editTextInputStyle=0x7f070000; } public static final class xml { diff --git a/res/drawable/blue_button.xml b/res/drawable/blue_button.xml index f4d6059b..f686a06a 100644 --- a/res/drawable/blue_button.xml +++ b/res/drawable/blue_button.xml @@ -15,7 +15,6 @@ --> - + diff --git a/res/drawable/light_blue_button.xml b/res/drawable/light_blue_button.xml new file mode 100644 index 00000000..4b0623ee --- /dev/null +++ b/res/drawable/light_blue_button.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/res/layout/main.xml b/res/layout/main.xml index 3a3be71d..7c6cae20 100644 --- a/res/layout/main.xml +++ b/res/layout/main.xml @@ -8,165 +8,171 @@ a:background="#ff000000"> + a:layout_gravity="center"> - + - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/res/values/styles.xml b/res/values/styles.xml index be73b39e..49225a9a 100644 --- a/res/values/styles.xml +++ b/res/values/styles.xml @@ -20,20 +20,19 @@ @drawable/blue_button + diff --git a/src/org/solovyev/android/calculator/CalculatorActivity.java b/src/org/solovyev/android/calculator/CalculatorActivity.java index d778e0ab..d874f9ca 100644 --- a/src/org/solovyev/android/calculator/CalculatorActivity.java +++ b/src/org/solovyev/android/calculator/CalculatorActivity.java @@ -8,8 +8,6 @@ import java.util.List; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; -import android.content.IntentFilter; -import android.os.Handler; import android.util.TypedValue; import android.view.*; import android.widget.TextView; @@ -28,9 +26,9 @@ import android.util.Log; import android.widget.EditText; import org.solovyev.util.math.Point2d; -public class CalculatorActivity extends Activity implements FontSizeAdjuster{ +public class CalculatorActivity extends Activity implements FontSizeAdjuster { - private static final int HVGA_WIDTH_PIXELS = 320; + private static final int HVGA_WIDTH_PIXELS = 320; @NotNull private EditText editText; @@ -42,7 +40,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster{ private Interpreter interpreter; @NotNull - private HistoryHelper historyHelper; + private HistoryHelper historyHelper; @NotNull private BroadcastReceiver preferencesChangesReceiver; @@ -106,8 +104,8 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster{ Log.e(CalculatorActivity.class.getName(), e.getMessage()); } - this.historyHelper = new SimpleHistoryHelper(); - this.historyHelper.addState(getCurrentHistoryState()); + this.historyHelper = new SimpleHistoryHelper(); + saveHistoryState(); this.preferencesChangesReceiver = new BroadcastReceiver() { @Override @@ -123,6 +121,10 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster{ }; } + private void saveHistoryState() { + historyHelper.addState(getCurrentHistoryState()); + } + public void elementaryButtonClickHandler(@NotNull View v) { eval(JsclOperation.elementary); } @@ -131,14 +133,46 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster{ eval(JsclOperation.numeric); } + public void eraseButtonClickHandler(@NotNull View v) { + editText.getText().delete(editText.getSelectionStart() - 1, editText.getSelectionStart()); + saveHistoryState(); + } + public void simplifyButtonClickHandler(@NotNull View v) { eval(JsclOperation.simplify); } + public void moveLeftButtonClickHandler(@NotNull View v) { + if (editText.getSelectionStart() > 0) { + editText.setSelection(editText.getSelectionStart() - 1); + } + } + + public void moveRightButtonClickHandler(@NotNull View v) { + if (editText.getSelectionStart() < editText.getText().length()) { + editText.setSelection(editText.getSelectionStart() + 1); + } + } + + public void clearButtonClickHandler(@NotNull View v) { + editText.getText().clear(); + resultEditText.getText().clear(); + saveHistoryState(); + } + private void eval(@NotNull JsclOperation operation) { try { final String preprocessedString = Preprocessor.process(String.valueOf(editText.getText())); resultEditText.setText(String.valueOf(interpreter.eval(Preprocessor.wrap(operation, preprocessedString)))); + + // result editor might be changed (but main editor - no) => make undo and add new state with saved result + CalculatorHistoryState currentHistoryState = getCurrentHistoryState(); + if (this.historyHelper.isUndoAvailable()) { + this.historyHelper.undo(currentHistoryState); + } + + this.historyHelper.addState(currentHistoryState); + } catch (EvalError e) { Log.e(CalculatorActivity.class.getName(), e.getMessage()); resultEditText.setText(R.string.syntax_error); @@ -165,7 +199,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster{ final HistoryAction historyAction = HistoryAction.valueOf(actionText); if (historyHelper.isActionAvailable(historyAction)) { - final EditorHistoryState newState = historyHelper.doAction(historyAction, getCurrentHistoryState()); + final CalculatorHistoryState newState = historyHelper.doAction(historyAction, getCurrentHistoryState()); if (newState != null) { setCurrentHistoryState(newState); } @@ -200,17 +234,26 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster{ return result; } - public void setCurrentHistoryState(@NotNull EditorHistoryState editorHistoryState) { - this.editText.setText(editorHistoryState.getText()); - this.editText.setSelection(editorHistoryState.getCursorPosition(), editorHistoryState.getCursorPosition()); + public void setCurrentHistoryState(@NotNull CalculatorHistoryState editorHistoryState) { + setValuesFromHistory(this.editText, editorHistoryState.getEditorState()); + setValuesFromHistory(this.resultEditText, editorHistoryState.getResultEditorState()); + } + + private void setValuesFromHistory(@NotNull EditText editText, EditorHistoryState editorHistoryState) { + editText.setText(editorHistoryState.getText()); + editText.setSelection(editorHistoryState.getCursorPosition()); } @NotNull - public EditorHistoryState getCurrentHistoryState() { + public CalculatorHistoryState getCurrentHistoryState() { + return new CalculatorHistoryState(getEditorHistoryState(this.editText), getEditorHistoryState(this.resultEditText)); + } + + private EditorHistoryState getEditorHistoryState(@NotNull EditText editorText) { final EditorHistoryState result = new EditorHistoryState(); - result.setText(String.valueOf(this.editText.getText())); - result.setCursorPosition(this.editText.getSelectionStart()); + result.setText(String.valueOf(editorText.getText())); + result.setCursorPosition(editorText.getSelectionStart()); return result; } @@ -241,7 +284,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster{ this.editText.getText().insert(this.editText.getSelectionStart(), text); this.editText.setSelection(this.editText.getSelectionStart() + cursorPositionOffset, this.editText.getSelectionEnd() + cursorPositionOffset); - this.historyHelper.addState(getCurrentHistoryState()); + saveHistoryState(); } } @@ -278,17 +321,17 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster{ Log.d(CalculatorActivity.class + "showHelp()", "Show help!"); } - /** - * The font sizes in the layout files are specified for a HVGA display. - * Adjust the font sizes accordingly if we are running on a different - * display. - */ + /** + * The font sizes in the layout files are specified for a HVGA display. + * Adjust the font sizes accordingly if we are running on a different + * display. + */ @Override - public void adjustFontSize(@NotNull TextView view) { - float fontPixelSize = view.getTextSize(); - Display display = getWindowManager().getDefaultDisplay(); - int h = Math.min(display.getWidth(), display.getHeight()); - float ratio = (float)h/HVGA_WIDTH_PIXELS; - view.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontPixelSize*ratio); - } + public void adjustFontSize(@NotNull TextView view) { + float fontPixelSize = view.getTextSize(); + Display display = getWindowManager().getDefaultDisplay(); + int h = Math.min(display.getWidth(), display.getHeight()); + float ratio = (float) h / HVGA_WIDTH_PIXELS; + view.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontPixelSize * ratio); + } } \ No newline at end of file diff --git a/src/org/solovyev/android/calculator/CalculatorHistoryState.java b/src/org/solovyev/android/calculator/CalculatorHistoryState.java new file mode 100644 index 00000000..f4468778 --- /dev/null +++ b/src/org/solovyev/android/calculator/CalculatorHistoryState.java @@ -0,0 +1,40 @@ +package org.solovyev.android.calculator; + +import org.jetbrains.annotations.NotNull; + +/** + * User: serso + * Date: 9/11/11 + * Time: 12:16 AM + */ +public class CalculatorHistoryState { + + @NotNull + private EditorHistoryState editorState; + + @NotNull + private EditorHistoryState resultEditorState; + + public CalculatorHistoryState(@NotNull EditorHistoryState editorState, @NotNull EditorHistoryState resultEditorState) { + this.editorState = editorState; + this.resultEditorState = resultEditorState; + } + + @NotNull + public EditorHistoryState getEditorState() { + return editorState; + } + + public void setEditorState(@NotNull EditorHistoryState editorState) { + this.editorState = editorState; + } + + @NotNull + public EditorHistoryState getResultEditorState() { + return resultEditorState; + } + + public void setResultEditorState(@NotNull EditorHistoryState resultEditorState) { + this.resultEditorState = resultEditorState; + } +} diff --git a/src/org/solovyev/android/calculator/Preprocessor.java b/src/org/solovyev/android/calculator/Preprocessor.java index 60e54bc4..49701d76 100644 --- a/src/org/solovyev/android/calculator/Preprocessor.java +++ b/src/org/solovyev/android/calculator/Preprocessor.java @@ -15,10 +15,22 @@ public class Preprocessor { sb.append('('); } else if (ch == ']' || ch == '}') { sb.append(')'); - } else if (ch == ',') { - sb.append('.'); } else if (ch == 'π') { sb.append("pi"); + } else if (s.startsWith("ln", i)) { + sb.append("log"); + i += 1; + } else if (s.startsWith("tg", i)) { + sb.append("tan"); + i += 1; + } else if (s.startsWith("atg", i)) { + sb.append("atan"); + i += 2; + } else if (s.startsWith("e(", i)) { + sb.append("exp("); + i += 1; + } else if (ch == 'e') { + sb.append("exp(1)"); } else if (ch == '√') { sb.append("sqrt"); } else { diff --git a/src/org/solovyev/android/view/PanelSwitcher.java b/src/org/solovyev/android/view/PanelSwitcher.java deleted file mode 100644 index b8719de9..00000000 --- a/src/org/solovyev/android/view/PanelSwitcher.java +++ /dev/null @@ -1,144 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.solovyev.android.view; - -import android.view.animation.TranslateAnimation; -import android.view.MotionEvent; -import android.view.View; -import android.view.GestureDetector; -import android.widget.FrameLayout; -import android.content.Context; -import android.util.AttributeSet; - -class PanelSwitcher extends FrameLayout { - private static final int MAJOR_MOVE = 60; - private static final int ANIM_DURATION = 400; - - private GestureDetector mGestureDetector; - private int mCurrentView; - private View mChildren[] = new View[0]; - - private int mWidth; - private TranslateAnimation inLeft; - private TranslateAnimation outLeft; - - private TranslateAnimation inRight; - private TranslateAnimation outRight; - - private static final int LEFT = 1; - private static final int RIGHT = 2; - private int mPreviousMove; - - public PanelSwitcher(Context context, AttributeSet attrs) { - super(context, attrs); - mCurrentView = 0; - mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() { - public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, - float velocityY) { - int dx = (int) (e2.getX() - e1.getX()); - - // don't accept the fling if it's too short - // as it may conflict with a button push - if (Math.abs(dx) > MAJOR_MOVE && Math.abs(velocityX) > Math.abs(velocityY)) { - if (velocityX > 0) { - moveRight(); - } else { - moveLeft(); - } - return true; - } else { - return false; - } - } - }); - } - - void setCurrentIndex(int current) { - mCurrentView = current; - updateCurrentView(); - } - - private void updateCurrentView() { - for (int i = mChildren.length-1; i >= 0 ; --i) { - mChildren[i].setVisibility(i==mCurrentView ? View.VISIBLE : View.GONE); - } - } - - @Override - public void onSizeChanged(int w, int h, int oldW, int oldH) { - mWidth = w; - inLeft = new TranslateAnimation(mWidth, 0, 0, 0); - outLeft = new TranslateAnimation(0, -mWidth, 0, 0); - inRight = new TranslateAnimation(-mWidth, 0, 0, 0); - outRight = new TranslateAnimation(0, mWidth, 0, 0); - - inLeft.setDuration(ANIM_DURATION); - outLeft.setDuration(ANIM_DURATION); - inRight.setDuration(ANIM_DURATION); - outRight.setDuration(ANIM_DURATION); - } - - protected void onFinishInflate() { - int count = getChildCount(); - mChildren = new View[count]; - for (int i = 0; i < count; ++i) { - mChildren[i] = getChildAt(i); - } - updateCurrentView(); - } - - @Override - public boolean onTouchEvent(MotionEvent event) { - mGestureDetector.onTouchEvent(event); - return true; - } - - @Override - public boolean onInterceptTouchEvent(MotionEvent event) { - return mGestureDetector.onTouchEvent(event); - } - - void moveLeft() { - // <-- - if (mCurrentView < mChildren.length - 1 && mPreviousMove != LEFT) { - mChildren[mCurrentView+1].setVisibility(View.VISIBLE); - mChildren[mCurrentView+1].startAnimation(inLeft); - mChildren[mCurrentView].startAnimation(outLeft); - mChildren[mCurrentView].setVisibility(View.GONE); - - mCurrentView++; - mPreviousMove = LEFT; - } - } - - void moveRight() { - // --> - if (mCurrentView > 0 && mPreviousMove != RIGHT) { - mChildren[mCurrentView-1].setVisibility(View.VISIBLE); - mChildren[mCurrentView-1].startAnimation(inRight); - mChildren[mCurrentView].startAnimation(outRight); - mChildren[mCurrentView].setVisibility(View.GONE); - - mCurrentView--; - mPreviousMove = RIGHT; - } - } - - int getCurrentIndex() { - return mCurrentView; - } -} diff --git a/src/org/solovyev/util/math/MathEntityType.java b/src/org/solovyev/util/math/MathEntityType.java index db4e5ccb..f7800b55 100644 --- a/src/org/solovyev/util/math/MathEntityType.java +++ b/src/org/solovyev/util/math/MathEntityType.java @@ -19,7 +19,7 @@ public enum MathEntityType { private static final List binaryOperations = Arrays.asList('-', '+', '*', '/', '^' ); - private static final List functions = Arrays.asList("sin", "asin", "cos", "acos", "tg", "atg", "exp", "log", "ln", "mod", "√"); + private static final List functions = Arrays.asList("sin", "asin", "cos", "acos", "tg", "atg", "log", "ln", "mod", "√"); private static final List groupSymbols = Arrays.asList("[]", "()", "{}");