new interface implementation
This commit is contained in:
parent
8415e0d7d8
commit
87aa087ad9
@ -9,7 +9,7 @@
|
||||
|
||||
<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>
|
||||
<action a:name="android.intent.action.MAIN" />
|
||||
<category a:name="android.intent.category.LAUNCHER" />
|
||||
|
@ -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;
|
||||
|
@ -18,6 +18,7 @@
|
||||
<EditText
|
||||
a:id="@+id/editText"
|
||||
style="@style/display_style"
|
||||
a:inputType="textMultiLine"
|
||||
a:singleLine="false"/>
|
||||
|
||||
</LinearLayout>
|
||||
@ -25,12 +26,21 @@
|
||||
<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"
|
||||
style="@style/display_style"
|
||||
a:gravity="right|top"
|
||||
a:layout_weight="1"
|
||||
a:minLines="1"
|
||||
a:maxLines="1"
|
||||
a:textStyle="bold"
|
||||
a:scrollHorizontally="true"
|
||||
a:scrollbars="none"/>
|
||||
|
||||
@ -137,13 +147,6 @@
|
||||
<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"/>
|
||||
@ -157,19 +160,26 @@
|
||||
a:onClick="eraseButtonClickHandler"/>
|
||||
|
||||
<org.solovyev.android.view.DirectionDragButton a:id="@+id/leftButton"
|
||||
calc:textUp="↞"
|
||||
a:text="←"
|
||||
style="@style/controlButtonStyle"
|
||||
a:onClick="moveLeftButtonClickHandler"/>
|
||||
|
||||
<org.solovyev.android.view.DirectionDragButton a:id="@+id/rightButton"
|
||||
calc:textUp="↠"
|
||||
a:text="→"
|
||||
style="@style/controlButtonStyle"
|
||||
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"
|
||||
calc:textUp="undo"
|
||||
calc:textDown="redo"
|
||||
style="@style/controlButtonStyle"
|
||||
a:onClick="clearButtonClickHandler"/>
|
||||
|
||||
|
@ -28,9 +28,10 @@
|
||||
|
||||
<style name="display_style">
|
||||
<item name="android:textSize">30dp</item>
|
||||
<!-- <item name="android:background">#000000</item>
|
||||
<item name="android:textColor">#ffffff</item>-->
|
||||
<item name="android:background">#000000</item>
|
||||
<item name="android:textColor">#ffffff</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_height">match_parent</item>
|
||||
</style>
|
||||
|
@ -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) {
|
||||
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();
|
||||
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) {
|
||||
if (showError) {
|
||||
Toast.makeText(CalculatorActivity.this, R.string.syntax_error, Toast.LENGTH_SHORT).show();
|
||||
Log.e(CalculatorActivity.class.getName(), e.getMessage());
|
||||
resultEditText.setText(R.string.syntax_error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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));
|
||||
|
Loading…
Reference in New Issue
Block a user