new interface implementation

This commit is contained in:
serso 2011-09-11 17:17:20 +04:00
parent ef1508dcc7
commit 26a0fbc02d
8 changed files with 195 additions and 69 deletions

View File

@ -9,7 +9,7 @@
<application a:icon="@drawable/icon" a:label="@string/c_app_name"> <application a:icon="@drawable/icon" a:label="@string/c_app_name">
<activity a:name=".CalculatorActivity" a:label="@string/c_app_name"> <activity a:name=".CalculatorActivity" a:label="@string/c_app_name" a:screenOrientation="portrait">
<intent-filter> <intent-filter>
<action a:name="android.intent.action.MAIN" /> <action a:name="android.intent.action.MAIN" />
<category a:name="android.intent.category.LAUNCHER" /> <category a:name="android.intent.category.LAUNCHER" />

View File

@ -46,32 +46,33 @@ 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 divisionButton=0x7f09000f; public static final int clearButton=0x7f09001d;
public static final int divisionButton=0x7f090010;
public static final int editText=0x7f090006; public static final int editText=0x7f090006;
public static final int eigthDigitButton=0x7f090011; public static final int eigthDigitButton=0x7f090012;
public static final int equalsButton=0x7f090018; public static final int equalsButton=0x7f090007;
public static final int eraseButton=0x7f090019; public static final int eraseButton=0x7f090019;
public static final int fiveDigitButton=0x7f09000d; public static final int fiveDigitButton=0x7f09000e;
public static final int fourDigitButton=0x7f09000c; public static final int fourDigitButton=0x7f09000d;
public static final int historyButton=0x7f09001c;
public static final int leftButton=0x7f09001a; public static final int leftButton=0x7f09001a;
public static final int menu_item_help=0x7f09001e; public static final int menu_item_help=0x7f09001f;
public static final int menu_item_settings=0x7f09001d; public static final int menu_item_settings=0x7f09001e;
public static final int muliplicationButton=0x7f09000b; public static final int muliplicationButton=0x7f09000c;
public static final int nineDigitButton=0x7f090012; public static final int nineDigitButton=0x7f090013;
public static final int oneDigitButton=0x7f090008; public static final int oneDigitButton=0x7f090009;
public static final int plusButton=0x7f090013; public static final int pasteButton=0x7f09001c;
public static final int resultEditText=0x7f090007; public static final int plusButton=0x7f090014;
public static final int resultEditText=0x7f090008;
public static final int rightButton=0x7f09001b; public static final int rightButton=0x7f09001b;
public static final int roundBracketsButton=0x7f090014; public static final int roundBracketsButton=0x7f090015;
public static final int sevenDigitButton=0x7f090010; public static final int sevenDigitButton=0x7f090011;
public static final int simplePad=0x7f090000; public static final int simplePad=0x7f090000;
public static final int sixDigitButton=0x7f09000e; public static final int sixDigitButton=0x7f09000f;
public static final int squareBracketsButton=0x7f090016; public static final int squareBracketsButton=0x7f090017;
public static final int subtractionButton=0x7f090017; public static final int subtractionButton=0x7f090018;
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=0x7f090016;
} }
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;

View File

@ -18,6 +18,7 @@
<EditText <EditText
a:id="@+id/editText" a:id="@+id/editText"
style="@style/display_style" style="@style/display_style"
a:inputType="textMultiLine"
a:singleLine="false"/> a:singleLine="false"/>
</LinearLayout> </LinearLayout>
@ -25,12 +26,21 @@
<LinearLayout a:layout_weight="1" a:layout_width="match_parent" a:layout_height="0dp"> <LinearLayout a:layout_weight="1" a:layout_width="match_parent" a:layout_height="0dp">
<EditText <org.solovyev.android.view.DirectionDragButton a:id="@+id/equalsButton"
a:text="="
a:layout_width="fill_parent"
a:layout_weight="3"
style="@style/controlButtonStyle"
a:onClick="numericButtonClickHandler"/>
<TextView
a:id="@+id/resultEditText" a:id="@+id/resultEditText"
style="@style/display_style" style="@style/display_style"
a:gravity="right|top" a:gravity="right|top"
a:layout_weight="1"
a:minLines="1" a:minLines="1"
a:maxLines="1" a:maxLines="1"
a:textStyle="bold"
a:scrollHorizontally="true" a:scrollHorizontally="true"
a:scrollbars="none"/> a:scrollbars="none"/>
@ -137,39 +147,39 @@
<LinearLayout a:layout_weight="1" a:layout_width="match_parent" a:layout_height="0dp"> <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" <!-- <org.solovyev.android.view.DirectionDragButton a:id="@+id/numericButton" a:text="numeric"
style="@style/digitButtonStyle" style="@style/digitButtonStyle"
a:onClick="numericButtonClickHandler"/> a:onClick="numericButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/simplifyButton" a:text="simplify" <org.solovyev.android.view.DirectionDragButton a:id="@+id/simplifyButton" a:text="simplify"
style="@style/digitButtonStyle" style="@style/digitButtonStyle"
a:onClick="simplifyButtonClickHandler"/>--> a:onClick="simplifyButtonClickHandler"/>-->
<org.solovyev.android.view.DirectionDragButton a:id="@+id/eraseButton" <org.solovyev.android.view.DirectionDragButton a:id="@+id/eraseButton"
a:text="↚" a:text="↚"
style="@style/controlButtonStyle" style="@style/controlButtonStyle"
a:onClick="eraseButtonClickHandler"/> a:onClick="eraseButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/leftButton" <org.solovyev.android.view.DirectionDragButton a:id="@+id/leftButton"
calc:textUp="↞"
a:text="←" a:text="←"
style="@style/controlButtonStyle" style="@style/controlButtonStyle"
a:onClick="moveLeftButtonClickHandler"/> a:onClick="moveLeftButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/rightButton" <org.solovyev.android.view.DirectionDragButton a:id="@+id/rightButton"
calc:textUp="↠"
a:text="→" a:text="→"
style="@style/controlButtonStyle" style="@style/controlButtonStyle"
a:onClick="moveRightButtonClickHandler"/> a:onClick="moveRightButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/historyButton" <org.solovyev.android.view.DirectionDragButton a:id="@+id/pasteButton"
a:text="paste"
calc:textUp="redo"
style="@style/controlButtonStyle"
a:onClick="pasteButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/clearButton"
a:text="clear" a:text="clear"
calc:textUp="undo" calc:textUp="undo"
calc:textDown="redo"
style="@style/controlButtonStyle" style="@style/controlButtonStyle"
a:onClick="clearButtonClickHandler"/> a:onClick="clearButtonClickHandler"/>

View File

@ -28,9 +28,10 @@
<style name="display_style"> <style name="display_style">
<item name="android:textSize">30dp</item> <item name="android:textSize">30dp</item>
<!-- <item name="android:background">#000000</item> <item name="android:background">#000000</item>
<item name="android:textColor">#ffffff</item>--> <item name="android:textColor">#ffffff</item>
<item name="android:gravity">left|top</item> <item name="android:gravity">left|top</item>
<item name="android:padding">5dp</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>
</style> </style>

View File

@ -8,9 +8,14 @@ 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.text.ClipboardManager;
import android.text.InputType;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.*; import android.view.*;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.view.*; import org.solovyev.android.view.*;
@ -24,6 +29,7 @@ import android.app.Activity;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.widget.EditText; import android.widget.EditText;
import org.solovyev.util.math.MathUtils;
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 {
@ -34,7 +40,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster {
private EditText editText; private EditText editText;
@NotNull @NotNull
private EditText resultEditText; private TextView resultEditText;
@NotNull @NotNull
private Interpreter interpreter; private Interpreter interpreter;
@ -56,9 +62,25 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.main); 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); 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); 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); 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(); this.interpreter = new Interpreter();
try { try {
@ -119,6 +163,8 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster {
} }
} }
}; };
registerReceiver(this.preferencesChangesReceiver, new IntentFilter(DragButtonCalibrationActivity.INTENT_ACTION));
} }
private void saveHistoryState() { private void saveHistoryState() {
@ -126,20 +172,22 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster {
} }
public void elementaryButtonClickHandler(@NotNull View v) { public void elementaryButtonClickHandler(@NotNull View v) {
eval(JsclOperation.elementary); eval(JsclOperation.elementary, true);
} }
public void numericButtonClickHandler(@NotNull View v) { public void numericButtonClickHandler(@NotNull View v) {
eval(JsclOperation.numeric); eval(JsclOperation.numeric, true);
} }
public void eraseButtonClickHandler(@NotNull View v) { public void eraseButtonClickHandler(@NotNull View v) {
editText.getText().delete(editText.getSelectionStart() - 1, editText.getSelectionStart()); if (editText.getSelectionStart() > 0) {
saveHistoryState(); 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, true);
} }
public void moveLeftButtonClickHandler(@NotNull View v) { public void moveLeftButtonClickHandler(@NotNull View v) {
@ -154,16 +202,36 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster {
} }
} }
public void clearButtonClickHandler(@NotNull View v) { public void pasteButtonClickHandler(@NotNull View v) {
editText.getText().clear(); final ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
resultEditText.getText().clear(); if ( clipboard.hasText() ) {
saveHistoryState(); 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 { 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))));
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 // result editor might be changed (but main editor - no) => make undo and add new state with saved result
CalculatorHistoryState currentHistoryState = getCurrentHistoryState(); CalculatorHistoryState currentHistoryState = getCurrentHistoryState();
@ -174,8 +242,10 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster {
this.historyHelper.addState(currentHistoryState); this.historyHelper.addState(currentHistoryState);
} catch (EvalError e) { } catch (EvalError e) {
Log.e(CalculatorActivity.class.getName(), e.getMessage()); if (showError) {
resultEditText.setText(R.string.syntax_error); 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; result = true;
final HistoryAction historyAction = HistoryAction.valueOf(actionText); final HistoryAction historyAction = HistoryAction.valueOf(actionText);
if (historyHelper.isActionAvailable(historyAction)) { doHistoryAction(historyAction);
final CalculatorHistoryState newState = historyHelper.doAction(historyAction, getCurrentHistoryState());
if (newState != null) {
setCurrentHistoryState(newState);
}
}
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
Log.e(String.valueOf(dragButton.getId()), "Unsupported history action: " + actionText); 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 @Nullable
private static String getActionText(@NotNull DirectionDragButton dragButton, @NotNull DragDirection direction) { private static String getActionText(@NotNull DirectionDragButton dragButton, @NotNull DragDirection direction) {
final String result; final String result;
@ -239,9 +313,11 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster {
setValuesFromHistory(this.resultEditText, editorHistoryState.getResultEditorState()); 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.setText(editorHistoryState.getText());
editText.setSelection(editorHistoryState.getCursorPosition()); if (editText instanceof EditText) {
((EditText) editText).setSelection(editorHistoryState.getCursorPosition());
}
} }
@NotNull @NotNull
@ -249,11 +325,11 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster {
return new CalculatorHistoryState(getEditorHistoryState(this.editText), getEditorHistoryState(this.resultEditText)); 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(); final EditorHistoryState result = new EditorHistoryState();
result.setText(String.valueOf(editorText.getText())); result.setText(String.valueOf(textView.getText()));
result.setCursorPosition(editorText.getSelectionStart()); result.setCursorPosition(textView.getSelectionStart());
return result; return result;
} }
@ -285,13 +361,25 @@ 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);
saveHistoryState(); 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 @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
final MenuInflater menuInflater = getMenuInflater(); // todo serso: inflate menu as soon as it will implemented in proper way
menuInflater.inflate(R.menu.main_menu, menu); /* final MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main_menu, menu);*/
return true; return true;
} }

View File

@ -4,6 +4,7 @@ import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.util.Log; import android.util.Log;

View File

@ -167,4 +167,19 @@ public class DirectionDragButton extends DragButton {
public String getTextMiddle() { public String getTextMiddle() {
return textMiddle; 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;
}
} }

View File

@ -8,6 +8,16 @@ public class MathUtils {
public static final float MIN_AMOUNT = 0.05f; 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, public static float getDistance(@NotNull Point2d startPoint,
@NotNull Point2d endPoint) { @NotNull Point2d endPoint) {
return getNorm(subtract(endPoint, startPoint)); return getNorm(subtract(endPoint, startPoint));