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

@ -35,9 +35,10 @@ containing a value of this type.
public static final int button=0x7f020001; public static final int button=0x7f020001;
public static final int down=0x7f020002; public static final int down=0x7f020002;
public static final int icon=0x7f020003; public static final int icon=0x7f020003;
public static final int not_ok=0x7f020004; public static final int light_blue_button=0x7f020004;
public static final int ok=0x7f020005; public static final int not_ok=0x7f020005;
public static final int up=0x7f020006; public static final int ok=0x7f020006;
public static final int up=0x7f020007;
} }
public static final class id { public static final class id {
public static final int calibrationArrowLeft=0x7f090001; 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 calibrationButtonLeft=0x7f090002;
public static final int calibrationButtonRight=0x7f090003; public static final int calibrationButtonRight=0x7f090003;
public static final int calibrationStart=0x7f090005; public static final int calibrationStart=0x7f090005;
public static final int curlyBracketsButton=0x7f09001b; public static final int divisionButton=0x7f09000f;
public static final int divisionButton=0x7f090011;
public static final int editText=0x7f090006; public static final int editText=0x7f090006;
public static final int eigthDigitButton=0x7f090014; public static final int eigthDigitButton=0x7f090011;
public static final int equalsButton=0x7f09001d; public static final int equalsButton=0x7f090018;
public static final int fiveDigitButton=0x7f09000f; public static final int eraseButton=0x7f090019;
public static final int fourDigitButton=0x7f09000e; public static final int fiveDigitButton=0x7f09000d;
public static final int historyButton=0x7f09001e; public static final int fourDigitButton=0x7f09000c;
public static final int menu_item_help=0x7f090020; public static final int historyButton=0x7f09001c;
public static final int menu_item_settings=0x7f09001f; public static final int leftButton=0x7f09001a;
public static final int muliplicationButton=0x7f09000c; public static final int menu_item_help=0x7f09001e;
public static final int nineDigitButton=0x7f090015; public static final int menu_item_settings=0x7f09001d;
public static final int oneDigitButton=0x7f090009; public static final int muliplicationButton=0x7f09000b;
public static final int panelswitch=0x7f090008; public static final int nineDigitButton=0x7f090012;
public static final int piButton=0x7f09001c; public static final int oneDigitButton=0x7f090008;
public static final int plusButton=0x7f09000d; public static final int plusButton=0x7f090013;
public static final int pointDigitButton=0x7f090016;
public static final int resultEditText=0x7f090007; public static final int resultEditText=0x7f090007;
public static final int roundBracketsButton=0x7f090019; public static final int rightButton=0x7f09001b;
public static final int sevenDigitButton=0x7f090013; public static final int roundBracketsButton=0x7f090014;
public static final int sevenDigitButton=0x7f090010;
public static final int simplePad=0x7f090000; public static final int simplePad=0x7f090000;
public static final int sixDigitButton=0x7f090010; public static final int sixDigitButton=0x7f09000e;
public static final int sqrtButton=0x7f090017; public static final int squareBracketsButton=0x7f090016;
public static final int squareBracketsButton=0x7f09001a; public static final int subtractionButton=0x7f090017;
public static final int subtractionButton=0x7f090012; public static final int threeDigitButton=0x7f09000a;
public static final int threeDigitButton=0x7f09000b; public static final int twoDigitButton=0x7f090009;
public static final int twoDigitButton=0x7f09000a; public static final int zeroDigitButton=0x7f090015;
public static final int zeroDigitButton=0x7f090018;
} }
public static final class layout { public static final class layout {
public static final int drag_button_calibration=0x7f030000; 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 int syntax_error=0x7f060002;
} }
public static final class style { 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 button_style=0x7f070001;
public static final int controlButtonStyle=0x7f070003;
public static final int digitButtonStyle=0x7f070002; 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 int editTextInputStyle=0x7f070000;
} }
public static final class xml { public static final class xml {

View File

@ -15,7 +15,6 @@
--> -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"> <shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startColor="#071622" android:endColor="#253541" <gradient android:startColor="#071622" android:endColor="#253541" android:angle="90"/>
android:angle="90"/>
<corners android:radius="0dp" /> <corners android:radius="0dp" />
</shape> </shape>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startColor="#0f2e47" android:endColor="#3a5366" android:angle="90"/>
<corners android:radius="0dp" />
</shape>

View File

@ -8,165 +8,171 @@
a:background="#ff000000"> a:background="#ff000000">
<LinearLayout <LinearLayout
a:layout_width="match_parent"
a:layout_height="match_parent"
a:orientation="vertical" a:orientation="vertical"
a:layout_width="match_parent" a:layout_gravity="center">
a:layout_height="100dp"
a:layout_weight="2">
<EditText <LinearLayout a:layout_weight="2" a:layout_width="match_parent" a:layout_height="0dp">
a:id="@+id/editText"
style="@style/editTextInputStyle"
a:maxLines="5"
a:minLines="5"
a:gravity="top|left"
a:textAppearance="?android:attr/textAppearanceMedium"
a:typeface="monospace"
a:singleLine="false"/>
<EditText <EditText
a:id="@+id/resultEditText" a:id="@+id/editText"
style="@style/editTextInputStyle" style="@style/display_style"
a:gravity="top|left" a:singleLine="false"/>
a:textAppearance="?android:attr/textAppearanceMedium"
a:minLines="1"
a:height="10dp"
a:typeface="monospace"
a:editable="false"
a:focusable="false"/>
</LinearLayout>
<org.solovyev.android.view.PanelSwitcher
a:id="@+id/panelswitch"
a:layout_width="match_parent"
a:layout_height="0dp"
a:layout_weight="5">
<LinearLayout
a:id="@+id/simplePad"
a:layout_width="match_parent"
a:layout_height="match_parent"
a:orientation="vertical"
a:layout_gravity="center">
<LinearLayout a:layout_weight="1" a:layout_width="match_parent" a:layout_height="0dp">
<org.solovyev.android.view.DirectionDragButton a:id="@+id/oneDigitButton" a:text="1" calc:textUp="sin"
calc:textDown="asin" style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/twoDigitButton" a:text="2" calc:textUp="cos"
calc:textDown="acos" style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/threeDigitButton" a:text="3" calc:textUp="tg"
calc:textDown="atg" style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/muliplicationButton" a:text="*"
calc:textUp="^"
calc:textDown="^2" style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/plusButton" a:text="+"
style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
</LinearLayout>
<LinearLayout a:layout_weight="1" a:layout_width="match_parent" a:layout_height="0dp">
<org.solovyev.android.view.DirectionDragButton a:id="@+id/fourDigitButton" a:text="4" calc:textUp="exp"
calc:textDown="mod" style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/fiveDigitButton" a:text="5" calc:textUp="log"
calc:textDown="ln" style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/sixDigitButton" a:text="6" calc:textUp="!"
calc:textDown="" style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/divisionButton" a:text="/"
style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/subtractionButton" a:text="-"
style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
</LinearLayout>
<LinearLayout a:layout_weight="1" a:layout_width="match_parent" a:layout_height="0dp">
<org.solovyev.android.view.DirectionDragButton a:id="@+id/sevenDigitButton" a:text="7" calc:textUp=""
calc:textDown="" style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/eigthDigitButton" a:text="8" calc:textUp=""
calc:textDown="" style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/nineDigitButton" a:text="9" calc:textUp=""
calc:textDown="" style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/pointDigitButton" a:text="." calc:textDown=","
style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/sqrtButton" a:text="√"
style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
</LinearLayout>
<LinearLayout a:layout_weight="1" a:layout_width="match_parent" a:layout_height="0dp">
<org.solovyev.android.view.DirectionDragButton a:id="@+id/zeroDigitButton" a:text="0"
style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/roundBracketsButton" a:text="()"
calc:textUp="("
calc:textDown=")" style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/squareBracketsButton" a:text="[]"
calc:textUp="["
calc:textDown="]" style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/curlyBracketsButton" a:text="{}"
calc:textUp="{"
calc:textDown="}" style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/piButton" a:text="π" calc:textUp="e"
style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
</LinearLayout>
<LinearLayout a:layout_weight="1" a:layout_width="match_parent" a:layout_height="0dp">
<org.solovyev.android.view.DirectionDragButton a:id="@+id/equalsButton"
a:text="="
a:layout_width="fill_parent"
style="@style/digitButtonStyle"
a:onClick="numericButtonClickHandler"/>
<!-- <org.solovyev.android.view.DirectionDragButton a:id="@+id/numericButton" a:text="numeric"
style="@style/digitButtonStyle"
a:onClick="numericButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/simplifyButton" a:text="simplify"
style="@style/digitButtonStyle"
a:onClick="simplifyButtonClickHandler"/>-->
<org.solovyev.android.view.DirectionDragButton a:id="@+id/historyButton" calc:textUp="undo"
calc:textDown="redo" style="@style/digitButtonStyle"/>
</LinearLayout>
</LinearLayout> </LinearLayout>
</org.solovyev.android.view.PanelSwitcher>
<LinearLayout a:layout_weight="1" a:layout_width="match_parent" a:layout_height="0dp">
<EditText
a:id="@+id/resultEditText"
style="@style/display_style"
a:gravity="right|top"
a:minLines="1"
a:maxLines="1"
a:scrollHorizontally="true"
a:scrollbars="none"/>
</LinearLayout>
<LinearLayout a:layout_weight="1" a:layout_width="match_parent" a:layout_height="0dp">
<org.solovyev.android.view.DirectionDragButton a:id="@+id/oneDigitButton" a:text="1" calc:textUp="sin"
calc:textDown="asin" style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/twoDigitButton" a:text="2" calc:textUp="cos"
calc:textDown="acos" style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/threeDigitButton" a:text="3" calc:textUp="tg"
calc:textDown="atg" style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/muliplicationButton" a:text="*"
calc:textUp="^"
calc:textDown="^2" style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
</LinearLayout>
<LinearLayout a:layout_weight="1" a:layout_width="match_parent" a:layout_height="0dp">
<org.solovyev.android.view.DirectionDragButton a:id="@+id/fourDigitButton"
a:text="4"
calc:textUp=""
calc:textDown=""
style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/fiveDigitButton"
a:text="5"
calc:textUp="ln"
style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/sixDigitButton" a:text="6"
calc:textUp=""
calc:textDown="" style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/divisionButton" a:text="/"
calc:textUp="√"
style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
</LinearLayout>
<LinearLayout a:layout_weight="1" a:layout_width="match_parent" a:layout_height="0dp">
<org.solovyev.android.view.DirectionDragButton a:id="@+id/sevenDigitButton" a:text="7" calc:textUp=""
calc:textDown="" style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/eigthDigitButton" a:text="8" calc:textUp=""
calc:textDown="" style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/nineDigitButton" a:text="9"
calc:textDown="e"
calc:textUp="π"
style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/plusButton" a:text="+"
style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
</LinearLayout>
<LinearLayout a:layout_weight="1" a:layout_width="match_parent" a:layout_height="0dp">
<org.solovyev.android.view.DirectionDragButton a:id="@+id/roundBracketsButton" a:text="()"
calc:textUp="("
calc:textDown=")" style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/zeroDigitButton" a:text="0"
calc:textUp="."
calc:textDown=","
style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/squareBracketsButton" a:text="[]"
calc:textUp="["
calc:textDown="]" style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/subtractionButton" a:text="-"
calc:textUp="{"
calc:textDown="}"
style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>
</LinearLayout>
<LinearLayout a:layout_weight="1" a:layout_width="match_parent" a:layout_height="0dp">
<org.solovyev.android.view.DirectionDragButton a:id="@+id/equalsButton"
a:text="="
a:layout_width="fill_parent"
style="@style/controlButtonStyle"
a:onClick="numericButtonClickHandler"/>
<!-- <org.solovyev.android.view.DirectionDragButton a:id="@+id/numericButton" a:text="numeric"
style="@style/digitButtonStyle"
a:onClick="numericButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/simplifyButton" a:text="simplify"
style="@style/digitButtonStyle"
a:onClick="simplifyButtonClickHandler"/>-->
<org.solovyev.android.view.DirectionDragButton a:id="@+id/eraseButton"
a:text="↚"
style="@style/controlButtonStyle"
a:onClick="eraseButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/leftButton"
a:text="←"
style="@style/controlButtonStyle"
a:onClick="moveLeftButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/rightButton"
a:text="→"
style="@style/controlButtonStyle"
a:onClick="moveRightButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/historyButton"
a:text="clear"
calc:textUp="undo"
calc:textDown="redo"
style="@style/controlButtonStyle"
a:onClick="clearButtonClickHandler"/>
</LinearLayout>
</LinearLayout>
</LinearLayout> </LinearLayout>

View File

@ -20,20 +20,19 @@
<item name="android:background">@drawable/blue_button</item> <item name="android:background">@drawable/blue_button</item>
</style> </style>
<style name="controlButtonStyle" parent="button_style"/>
<style name="button_small_style" parent="button_style"> <style name="button_small_style" parent="button_style">
<item name="android:textSize">30dp</item> <item name="android:textSize">30dp</item>
</style> </style>
<style name="display_style"> <style name="display_style">
<item name="android:textSize">40dp</item> <item name="android:textSize">30dp</item>
<item name="android:gravity">right|center_vertical</item> <!-- <item name="android:background">#000000</item>
<item name="android:textColor">#ffffff</item> <item name="android:textColor">#ffffff</item>-->
<item name="android:maxLines">1</item> <item name="android:gravity">left|top</item>
<item name="android:scrollbars">none</item>
<item name="android:scrollHorizontally">true</item>
<item name="android:layout_width">match_parent</item> <item name="android:layout_width">match_parent</item>
<item name="android:layout_height">match_parent</item> <item name="android:layout_height">match_parent</item>
<item name="android:padding">8dp</item>
</style> </style>
</resources> </resources>

View File

@ -8,8 +8,6 @@ import java.util.List;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.*; import android.view.*;
import android.widget.TextView; import android.widget.TextView;
@ -28,9 +26,9 @@ import android.util.Log;
import android.widget.EditText; import android.widget.EditText;
import org.solovyev.util.math.Point2d; 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 @NotNull
private EditText editText; private EditText editText;
@ -42,7 +40,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster{
private Interpreter interpreter; private Interpreter interpreter;
@NotNull @NotNull
private HistoryHelper<EditorHistoryState> historyHelper; private HistoryHelper<CalculatorHistoryState> historyHelper;
@NotNull @NotNull
private BroadcastReceiver preferencesChangesReceiver; private BroadcastReceiver preferencesChangesReceiver;
@ -106,8 +104,8 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster{
Log.e(CalculatorActivity.class.getName(), e.getMessage()); Log.e(CalculatorActivity.class.getName(), e.getMessage());
} }
this.historyHelper = new SimpleHistoryHelper<EditorHistoryState>(); this.historyHelper = new SimpleHistoryHelper<CalculatorHistoryState>();
this.historyHelper.addState(getCurrentHistoryState()); saveHistoryState();
this.preferencesChangesReceiver = new BroadcastReceiver() { this.preferencesChangesReceiver = new BroadcastReceiver() {
@Override @Override
@ -123,6 +121,10 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster{
}; };
} }
private void saveHistoryState() {
historyHelper.addState(getCurrentHistoryState());
}
public void elementaryButtonClickHandler(@NotNull View v) { public void elementaryButtonClickHandler(@NotNull View v) {
eval(JsclOperation.elementary); eval(JsclOperation.elementary);
} }
@ -131,14 +133,46 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster{
eval(JsclOperation.numeric); eval(JsclOperation.numeric);
} }
public void eraseButtonClickHandler(@NotNull View v) {
editText.getText().delete(editText.getSelectionStart() - 1, editText.getSelectionStart());
saveHistoryState();
}
public void simplifyButtonClickHandler(@NotNull View v) { public void simplifyButtonClickHandler(@NotNull View v) {
eval(JsclOperation.simplify); 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) { private void eval(@NotNull JsclOperation operation) {
try { try {
final String preprocessedString = Preprocessor.process(String.valueOf(editText.getText())); final String preprocessedString = Preprocessor.process(String.valueOf(editText.getText()));
resultEditText.setText(String.valueOf(interpreter.eval(Preprocessor.wrap(operation, preprocessedString)))); 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) { } catch (EvalError e) {
Log.e(CalculatorActivity.class.getName(), e.getMessage()); Log.e(CalculatorActivity.class.getName(), e.getMessage());
resultEditText.setText(R.string.syntax_error); resultEditText.setText(R.string.syntax_error);
@ -165,7 +199,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster{
final HistoryAction historyAction = HistoryAction.valueOf(actionText); final HistoryAction historyAction = HistoryAction.valueOf(actionText);
if (historyHelper.isActionAvailable(historyAction)) { if (historyHelper.isActionAvailable(historyAction)) {
final EditorHistoryState newState = historyHelper.doAction(historyAction, getCurrentHistoryState()); final CalculatorHistoryState newState = historyHelper.doAction(historyAction, getCurrentHistoryState());
if (newState != null) { if (newState != null) {
setCurrentHistoryState(newState); setCurrentHistoryState(newState);
} }
@ -200,17 +234,26 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster{
return result; return result;
} }
public void setCurrentHistoryState(@NotNull EditorHistoryState editorHistoryState) { public void setCurrentHistoryState(@NotNull CalculatorHistoryState editorHistoryState) {
this.editText.setText(editorHistoryState.getText()); setValuesFromHistory(this.editText, editorHistoryState.getEditorState());
this.editText.setSelection(editorHistoryState.getCursorPosition(), editorHistoryState.getCursorPosition()); setValuesFromHistory(this.resultEditText, editorHistoryState.getResultEditorState());
}
private void setValuesFromHistory(@NotNull EditText editText, EditorHistoryState editorHistoryState) {
editText.setText(editorHistoryState.getText());
editText.setSelection(editorHistoryState.getCursorPosition());
} }
@NotNull @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(); final EditorHistoryState result = new EditorHistoryState();
result.setText(String.valueOf(this.editText.getText())); result.setText(String.valueOf(editorText.getText()));
result.setCursorPosition(this.editText.getSelectionStart()); result.setCursorPosition(editorText.getSelectionStart());
return result; return result;
} }
@ -241,7 +284,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster{
this.editText.getText().insert(this.editText.getSelectionStart(), text); this.editText.getText().insert(this.editText.getSelectionStart(), text);
this.editText.setSelection(this.editText.getSelectionStart() + cursorPositionOffset, this.editText.getSelectionEnd() + cursorPositionOffset); 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!"); Log.d(CalculatorActivity.class + "showHelp()", "Show help!");
} }
/** /**
* The font sizes in the layout files are specified for a HVGA 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 * Adjust the font sizes accordingly if we are running on a different
* display. * display.
*/ */
@Override @Override
public void adjustFontSize(@NotNull TextView view) { public void adjustFontSize(@NotNull TextView view) {
float fontPixelSize = view.getTextSize(); float fontPixelSize = view.getTextSize();
Display display = getWindowManager().getDefaultDisplay(); Display display = getWindowManager().getDefaultDisplay();
int h = Math.min(display.getWidth(), display.getHeight()); int h = Math.min(display.getWidth(), display.getHeight());
float ratio = (float)h/HVGA_WIDTH_PIXELS; float ratio = (float) h / HVGA_WIDTH_PIXELS;
view.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontPixelSize*ratio); 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('('); sb.append('(');
} else if (ch == ']' || ch == '}') { } else if (ch == ']' || ch == '}') {
sb.append(')'); sb.append(')');
} else if (ch == ',') {
sb.append('.');
} else if (ch == 'π') { } else if (ch == 'π') {
sb.append("pi"); 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 == '√') { } else if (ch == '√') {
sb.append("sqrt"); sb.append("sqrt");
} else { } 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<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("[]", "()", "{}"); private static final List<String> groupSymbols = Arrays.asList("[]", "()", "{}");