From 26a0fbc02ddfdc923ae594b6d88a8e1af4fc7c23 Mon Sep 17 00:00:00 2001 From: serso Date: Sun, 11 Sep 2011 17:17:20 +0400 Subject: [PATCH] new interface implementation --- AndroidManifest.xml | 2 +- gen/org/solovyev/android/calculator/R.java | 43 ++--- res/layout/main.xml | 40 +++-- res/values/styles.xml | 5 +- .../calculator/CalculatorActivity.java | 148 ++++++++++++++---- .../DragButtonCalibrationActivity.java | 1 + .../android/view/DirectionDragButton.java | 15 ++ src/org/solovyev/util/math/MathUtils.java | 10 ++ 8 files changed, 195 insertions(+), 69 deletions(-) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index c18c9208..76500aba 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -9,7 +9,7 @@ - + diff --git a/gen/org/solovyev/android/calculator/R.java b/gen/org/solovyev/android/calculator/R.java index 9b960dbc..31adea3c 100644 --- a/gen/org/solovyev/android/calculator/R.java +++ b/gen/org/solovyev/android/calculator/R.java @@ -46,32 +46,33 @@ 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 divisionButton=0x7f09000f; + public static final int clearButton=0x7f09001d; + public static final int divisionButton=0x7f090010; public static final int editText=0x7f090006; - public static final int eigthDigitButton=0x7f090011; - public static final int equalsButton=0x7f090018; + public static final int eigthDigitButton=0x7f090012; + public static final int equalsButton=0x7f090007; 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 fiveDigitButton=0x7f09000e; + public static final int fourDigitButton=0x7f09000d; 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 menu_item_help=0x7f09001f; + public static final int menu_item_settings=0x7f09001e; + public static final int muliplicationButton=0x7f09000c; + public static final int nineDigitButton=0x7f090013; + public static final int oneDigitButton=0x7f090009; + public static final int pasteButton=0x7f09001c; + public static final int plusButton=0x7f090014; + public static final int resultEditText=0x7f090008; public static final int rightButton=0x7f09001b; - public static final int roundBracketsButton=0x7f090014; - public static final int sevenDigitButton=0x7f090010; + public static final int roundBracketsButton=0x7f090015; + public static final int sevenDigitButton=0x7f090011; public static final int simplePad=0x7f090000; - 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 int sixDigitButton=0x7f09000f; + public static final int squareBracketsButton=0x7f090017; + public static final int subtractionButton=0x7f090018; + public static final int threeDigitButton=0x7f09000b; + public static final int twoDigitButton=0x7f09000a; + public static final int zeroDigitButton=0x7f090016; } public static final class layout { public static final int drag_button_calibration=0x7f030000; diff --git a/res/layout/main.xml b/res/layout/main.xml index 7c6cae20..0a1a4c40 100644 --- a/res/layout/main.xml +++ b/res/layout/main.xml @@ -18,6 +18,7 @@ @@ -25,12 +26,21 @@ - + + @@ -137,39 +147,39 @@ - - - + --> - + + diff --git a/res/values/styles.xml b/res/values/styles.xml index 49225a9a..8dea7232 100644 --- a/res/values/styles.xml +++ b/res/values/styles.xml @@ -28,9 +28,10 @@ diff --git a/src/org/solovyev/android/calculator/CalculatorActivity.java b/src/org/solovyev/android/calculator/CalculatorActivity.java index d874f9ca..116f71b5 100644 --- a/src/org/solovyev/android/calculator/CalculatorActivity.java +++ b/src/org/solovyev/android/calculator/CalculatorActivity.java @@ -8,9 +8,14 @@ import java.util.List; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; +import android.text.ClipboardManager; +import android.text.InputType; import android.util.TypedValue; import android.view.*; +import android.view.inputmethod.InputMethodManager; import android.widget.TextView; +import android.widget.Toast; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.solovyev.android.view.*; @@ -24,6 +29,7 @@ import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.EditText; +import org.solovyev.util.math.MathUtils; import org.solovyev.util.math.Point2d; public class CalculatorActivity extends Activity implements FontSizeAdjuster { @@ -34,7 +40,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster { private EditText editText; @NotNull - private EditText resultEditText; + private TextView resultEditText; @NotNull private Interpreter interpreter; @@ -56,9 +62,25 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster { super.onCreate(savedInstanceState); setContentView(R.layout.main); - this.editText = (EditText) findViewById(R.id.editText); + final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); + + this.editText = (EditText) findViewById(R.id.editText); + this.editText.setInputType(InputType.TYPE_NULL); + imm.hideSoftInputFromWindow(this.editText.getWindowToken(), 0); + + this.resultEditText = (TextView) findViewById(R.id.resultEditText); + this.resultEditText.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + final CharSequence text = ((TextView) v).getText(); + if (!StringUtils.isEmpty(text)) { + final ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); + clipboard.setText(text); + Toast.makeText(CalculatorActivity.this, "Result copied to clipboard!", Toast.LENGTH_SHORT).show(); + } + } + }); - this.resultEditText = (EditText) findViewById(R.id.resultEditText); final DragButtonCalibrationActivity.Preferences dragPreferences = DragButtonCalibrationActivity.getPreferences(this); @@ -93,9 +115,31 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster { } final SimpleOnDragListener historyOnDragListener = new SimpleOnDragListener(new HistoryDragProcessor(), dragPreferences); - ((DragButton) findViewById(R.id.historyButton)).setOnDragListener(historyOnDragListener); + ((DragButton) findViewById(R.id.clearButton)).setOnDragListener(historyOnDragListener); + ((DragButton) findViewById(R.id.pasteButton)).setOnDragListener(historyOnDragListener); onDragListeners.add(historyOnDragListener); + final SimpleOnDragListener toPositionOnDragListener = new SimpleOnDragListener(new SimpleOnDragListener.DragProcessor() { + @Override + public boolean processDragEvent(@NotNull DragDirection dragDirection, @NotNull DragButton dragButton, @NotNull Point2d startPoint2d, @NotNull MotionEvent motionEvent) { + boolean result = false; + + if (dragButton instanceof DirectionDragButton) { + String text = ((DirectionDragButton) dragButton).getText(dragDirection); + if ("↞".equals(text)) { + CalculatorActivity.this.editText.setSelection(0); + } else if ("↠".equals(text)) { + CalculatorActivity.this.editText.setSelection(CalculatorActivity.this.editText.getText().length()); + } + } + + return result; + } + }, dragPreferences); + ((DragButton) findViewById(R.id.rightButton)).setOnDragListener(toPositionOnDragListener); + ((DragButton) findViewById(R.id.leftButton)).setOnDragListener(toPositionOnDragListener); + onDragListeners.add(toPositionOnDragListener); + this.interpreter = new Interpreter(); try { @@ -119,6 +163,8 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster { } } }; + + registerReceiver(this.preferencesChangesReceiver, new IntentFilter(DragButtonCalibrationActivity.INTENT_ACTION)); } private void saveHistoryState() { @@ -126,20 +172,22 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster { } public void elementaryButtonClickHandler(@NotNull View v) { - eval(JsclOperation.elementary); + eval(JsclOperation.elementary, true); } public void numericButtonClickHandler(@NotNull View v) { - eval(JsclOperation.numeric); + eval(JsclOperation.numeric, true); } public void eraseButtonClickHandler(@NotNull View v) { - editText.getText().delete(editText.getSelectionStart() - 1, editText.getSelectionStart()); - saveHistoryState(); + if (editText.getSelectionStart() > 0) { + editText.getText().delete(editText.getSelectionStart() - 1, editText.getSelectionStart()); + saveHistoryState(); + } } public void simplifyButtonClickHandler(@NotNull View v) { - eval(JsclOperation.simplify); + eval(JsclOperation.simplify, true); } public void moveLeftButtonClickHandler(@NotNull View v) { @@ -154,16 +202,36 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster { } } - public void clearButtonClickHandler(@NotNull View v) { - editText.getText().clear(); - resultEditText.getText().clear(); - saveHistoryState(); + public void pasteButtonClickHandler(@NotNull View v) { + final ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); + if ( clipboard.hasText() ) { + editText.getText().append(clipboard.getText()); + saveHistoryState(); + } } - private void eval(@NotNull JsclOperation operation) { + + public void clearButtonClickHandler(@NotNull View v) { + if (!StringUtils.isEmpty(editText.getText()) || !StringUtils.isEmpty(resultEditText.getText())) { + editText.getText().clear(); + resultEditText.setText(""); + saveHistoryState(); + } + } + + private void eval(@NotNull JsclOperation operation, boolean showError) { try { final String preprocessedString = Preprocessor.process(String.valueOf(editText.getText())); - resultEditText.setText(String.valueOf(interpreter.eval(Preprocessor.wrap(operation, preprocessedString)))); + + String result = String.valueOf(interpreter.eval(Preprocessor.wrap(operation, preprocessedString))).trim(); + + try { + final Double dResult = Double.valueOf(result); + result = String.valueOf(MathUtils.round(dResult, 5)); + } catch (NumberFormatException e) { + } + + resultEditText.setText(result); // result editor might be changed (but main editor - no) => make undo and add new state with saved result CalculatorHistoryState currentHistoryState = getCurrentHistoryState(); @@ -174,8 +242,10 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster { this.historyHelper.addState(currentHistoryState); } catch (EvalError e) { - Log.e(CalculatorActivity.class.getName(), e.getMessage()); - resultEditText.setText(R.string.syntax_error); + if (showError) { + Toast.makeText(CalculatorActivity.this, R.string.syntax_error, Toast.LENGTH_SHORT).show(); + Log.e(CalculatorActivity.class.getName(), e.getMessage()); + } } } @@ -198,12 +268,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster { result = true; final HistoryAction historyAction = HistoryAction.valueOf(actionText); - if (historyHelper.isActionAvailable(historyAction)) { - final CalculatorHistoryState newState = historyHelper.doAction(historyAction, getCurrentHistoryState()); - if (newState != null) { - setCurrentHistoryState(newState); - } - } + doHistoryAction(historyAction); } catch (IllegalArgumentException e) { Log.e(String.valueOf(dragButton.getId()), "Unsupported history action: " + actionText); } @@ -213,6 +278,15 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster { } } + private void doHistoryAction(@NotNull HistoryAction historyAction) { + if (historyHelper.isActionAvailable(historyAction)) { + final CalculatorHistoryState newState = historyHelper.doAction(historyAction, getCurrentHistoryState()); + if (newState != null) { + setCurrentHistoryState(newState); + } + } + } + @Nullable private static String getActionText(@NotNull DirectionDragButton dragButton, @NotNull DragDirection direction) { final String result; @@ -239,9 +313,11 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster { setValuesFromHistory(this.resultEditText, editorHistoryState.getResultEditorState()); } - private void setValuesFromHistory(@NotNull EditText editText, EditorHistoryState editorHistoryState) { + private void setValuesFromHistory(@NotNull TextView editText, EditorHistoryState editorHistoryState) { editText.setText(editorHistoryState.getText()); - editText.setSelection(editorHistoryState.getCursorPosition()); + if (editText instanceof EditText) { + ((EditText) editText).setSelection(editorHistoryState.getCursorPosition()); + } } @NotNull @@ -249,11 +325,11 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster { return new CalculatorHistoryState(getEditorHistoryState(this.editText), getEditorHistoryState(this.resultEditText)); } - private EditorHistoryState getEditorHistoryState(@NotNull EditText editorText) { + private EditorHistoryState getEditorHistoryState(@NotNull TextView textView) { final EditorHistoryState result = new EditorHistoryState(); - result.setText(String.valueOf(editorText.getText())); - result.setCursorPosition(editorText.getSelectionStart()); + result.setText(String.valueOf(textView.getText())); + result.setCursorPosition(textView.getSelectionStart()); return result; } @@ -285,13 +361,25 @@ 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); saveHistoryState(); + eval(JsclOperation.numeric, false); } } + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + if (keyCode == KeyEvent.KEYCODE_BACK) { + doHistoryAction(HistoryAction.undo); + return true; + } + return super.onKeyDown(keyCode, event); + } + + @Override public boolean onCreateOptionsMenu(Menu menu) { - final MenuInflater menuInflater = getMenuInflater(); - menuInflater.inflate(R.menu.main_menu, menu); + // todo serso: inflate menu as soon as it will implemented in proper way +/* final MenuInflater menuInflater = getMenuInflater(); + menuInflater.inflate(R.menu.main_menu, menu);*/ return true; } diff --git a/src/org/solovyev/android/calculator/DragButtonCalibrationActivity.java b/src/org/solovyev/android/calculator/DragButtonCalibrationActivity.java index a2efa2b2..5c72311f 100644 --- a/src/org/solovyev/android/calculator/DragButtonCalibrationActivity.java +++ b/src/org/solovyev/android/calculator/DragButtonCalibrationActivity.java @@ -4,6 +4,7 @@ import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.util.Log; diff --git a/src/org/solovyev/android/view/DirectionDragButton.java b/src/org/solovyev/android/view/DirectionDragButton.java index 9828a844..5b4953ad 100644 --- a/src/org/solovyev/android/view/DirectionDragButton.java +++ b/src/org/solovyev/android/view/DirectionDragButton.java @@ -167,4 +167,19 @@ public class DirectionDragButton extends DragButton { public String getTextMiddle() { return textMiddle; } + + @Nullable + public String getText(@NotNull DragDirection direction) { + final String result; + + if (direction == DragDirection.up) { + result = getTextUp(); + } else if ( direction == DragDirection.down ) { + result = getTextDown(); + } else { + result = null; + } + + return result; + } } diff --git a/src/org/solovyev/util/math/MathUtils.java b/src/org/solovyev/util/math/MathUtils.java index 6d649ee2..cea513f8 100644 --- a/src/org/solovyev/util/math/MathUtils.java +++ b/src/org/solovyev/util/math/MathUtils.java @@ -8,6 +8,16 @@ public class MathUtils { public static final float MIN_AMOUNT = 0.05f; + public static double round(@NotNull Double value, int numberOfFractionDigits) { + double roundFactor = Math.pow(10, numberOfFractionDigits); + + if (value < Double.MAX_VALUE / roundFactor) { + return ((double)Math.round(value * roundFactor)) / roundFactor; + } else { + return value; + } + } + public static float getDistance(@NotNull Point2d startPoint, @NotNull Point2d endPoint) { return getNorm(subtract(endPoint, startPoint));