new interface implementation

This commit is contained in:
serso
2011-09-11 02:38:45 +04:00
parent 70d0c21c9e
commit b46ce7743c
10 changed files with 330 additions and 369 deletions

View File

@@ -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<EditorHistoryState> historyHelper;
private HistoryHelper<CalculatorHistoryState> 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<EditorHistoryState>();
this.historyHelper.addState(getCurrentHistoryState());
this.historyHelper = new SimpleHistoryHelper<CalculatorHistoryState>();
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);
}
}

View File

@@ -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;
}
}

View File

@@ -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 {

View File

@@ -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;
}
}

View File

@@ -19,7 +19,7 @@ public enum MathEntityType {
private static final List<Character> binaryOperations = Arrays.asList('-', '+', '*', '/', '^' );
private static final List<String> functions = Arrays.asList("sin", "asin", "cos", "acos", "tg", "atg", "exp", "log", "ln", "mod", "");
private static final List<String> functions = Arrays.asList("sin", "asin", "cos", "acos", "tg", "atg", "log", "ln", "mod", "");
private static final List<String> groupSymbols = Arrays.asList("[]", "()", "{}");