diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 53182fda..9ef36537 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -17,7 +17,7 @@ - s + @@ -29,10 +29,18 @@ a:label="@string/c_app_settings" a:configChanges="orientation|keyboardHidden"/> - + + + + diff --git a/res/layout-land/main_calculator.xml b/res/layout-land/main_calculator.xml index 3baa3a22..3832ce71 100644 --- a/res/layout-land/main_calculator.xml +++ b/res/layout-land/main_calculator.xml @@ -7,7 +7,7 @@ --> \ No newline at end of file diff --git a/res/layout/calc_copy_button.xml b/res/layout/calc_copy_button.xml index 25349d3a..19d6023e 100644 --- a/res/layout/calc_copy_button.xml +++ b/res/layout/calc_copy_button.xml @@ -7,7 +7,7 @@ --> \ No newline at end of file diff --git a/res/layout/calc_donate_button.xml b/res/layout/calc_donate_button.xml index 35b7e7c5..9b4d0f23 100644 --- a/res/layout/calc_donate_button.xml +++ b/res/layout/calc_donate_button.xml @@ -7,7 +7,7 @@ --> \ No newline at end of file diff --git a/res/layout/calc_eight_digit_button.xml b/res/layout/calc_eight_digit_button.xml index 2b2e3877..b2cb092b 100644 --- a/res/layout/calc_eight_digit_button.xml +++ b/res/layout/calc_eight_digit_button.xml @@ -7,11 +7,11 @@ --> \ No newline at end of file diff --git a/res/layout/calc_empty_button.xml b/res/layout/calc_empty_button.xml index 9d363fde..54e32de1 100644 --- a/res/layout/calc_empty_button.xml +++ b/res/layout/calc_empty_button.xml @@ -6,5 +6,5 @@ ~ or visit http://se.solovyev.org --> \ No newline at end of file diff --git a/res/layout/calc_equals_button.xml b/res/layout/calc_equals_button.xml index 28180d11..742bda6b 100644 --- a/res/layout/calc_equals_button.xml +++ b/res/layout/calc_equals_button.xml @@ -7,11 +7,11 @@ --> \ No newline at end of file diff --git a/res/layout/calc_four_digit_button.xml b/res/layout/calc_four_digit_button.xml index 35e06a0e..58980726 100644 --- a/res/layout/calc_four_digit_button.xml +++ b/res/layout/calc_four_digit_button.xml @@ -6,11 +6,11 @@ ~ or visit http://se.solovyev.org --> \ No newline at end of file diff --git a/res/layout/calc_functions_button.xml b/res/layout/calc_functions_button.xml index 1c2846bd..a86cb4fc 100644 --- a/res/layout/calc_functions_button.xml +++ b/res/layout/calc_functions_button.xml @@ -7,7 +7,7 @@ --> \ No newline at end of file diff --git a/res/layout/calc_left_button.xml b/res/layout/calc_left_button.xml index 94a44505..596fa4de 100644 --- a/res/layout/calc_left_button.xml +++ b/res/layout/calc_left_button.xml @@ -7,10 +7,10 @@ --> \ No newline at end of file diff --git a/res/layout/calc_multiplication_button.xml b/res/layout/calc_multiplication_button.xml index 1f76d1e6..84119e89 100644 --- a/res/layout/calc_multiplication_button.xml +++ b/res/layout/calc_multiplication_button.xml @@ -7,12 +7,12 @@ --> \ No newline at end of file diff --git a/res/layout/calc_nine_digit_button.xml b/res/layout/calc_nine_digit_button.xml index c178a105..69a1543c 100644 --- a/res/layout/calc_nine_digit_button.xml +++ b/res/layout/calc_nine_digit_button.xml @@ -7,11 +7,11 @@ --> \ No newline at end of file diff --git a/res/layout/calc_one_digit_button.xml b/res/layout/calc_one_digit_button.xml index 049207e4..decff8e3 100644 --- a/res/layout/calc_one_digit_button.xml +++ b/res/layout/calc_one_digit_button.xml @@ -7,11 +7,11 @@ --> \ No newline at end of file diff --git a/res/layout/calc_paste_button.xml b/res/layout/calc_paste_button.xml index 45e383ab..6062b12b 100644 --- a/res/layout/calc_paste_button.xml +++ b/res/layout/calc_paste_button.xml @@ -7,7 +7,7 @@ --> \ No newline at end of file diff --git a/res/layout/calc_right_button.xml b/res/layout/calc_right_button.xml index c3855471..9a926508 100644 --- a/res/layout/calc_right_button.xml +++ b/res/layout/calc_right_button.xml @@ -7,10 +7,10 @@ --> \ No newline at end of file diff --git a/res/layout/calc_round_brackets_button.xml b/res/layout/calc_round_brackets_button.xml index 87b91989..f421a8ff 100644 --- a/res/layout/calc_round_brackets_button.xml +++ b/res/layout/calc_round_brackets_button.xml @@ -7,11 +7,11 @@ --> \ No newline at end of file diff --git a/res/layout/calc_seven_digit_button.xml b/res/layout/calc_seven_digit_button.xml index c923e3a5..e0cd3b24 100644 --- a/res/layout/calc_seven_digit_button.xml +++ b/res/layout/calc_seven_digit_button.xml @@ -7,11 +7,11 @@ --> \ No newline at end of file diff --git a/res/layout/calc_six_digit_button.xml b/res/layout/calc_six_digit_button.xml index cd36b215..f81f525a 100644 --- a/res/layout/calc_six_digit_button.xml +++ b/res/layout/calc_six_digit_button.xml @@ -7,11 +7,12 @@ --> \ No newline at end of file diff --git a/res/layout/calc_subtraction_button.xml b/res/layout/calc_subtraction_button.xml index e30899d2..26be95de 100644 --- a/res/layout/calc_subtraction_button.xml +++ b/res/layout/calc_subtraction_button.xml @@ -6,10 +6,10 @@ ~ or visit http://se.solovyev.org --> \ No newline at end of file diff --git a/res/layout/calc_three_digit_button.xml b/res/layout/calc_three_digit_button.xml index a33d780a..48dbd072 100644 --- a/res/layout/calc_three_digit_button.xml +++ b/res/layout/calc_three_digit_button.xml @@ -7,11 +7,11 @@ --> \ No newline at end of file diff --git a/res/layout/calc_two_digit_button.xml b/res/layout/calc_two_digit_button.xml index 65b789c1..224f1970 100644 --- a/res/layout/calc_two_digit_button.xml +++ b/res/layout/calc_two_digit_button.xml @@ -7,11 +7,11 @@ --> \ No newline at end of file diff --git a/res/layout/calc_vars_button.xml b/res/layout/calc_vars_button.xml index 12e69256..b292aab2 100644 --- a/res/layout/calc_vars_button.xml +++ b/res/layout/calc_vars_button.xml @@ -7,10 +7,10 @@ --> \ No newline at end of file diff --git a/res/layout/drag_button_calibration.xml b/res/layout/drag_button_calibration.xml index 3795fc97..8dc2a923 100644 --- a/res/layout/drag_button_calibration.xml +++ b/res/layout/drag_button_calibration.xml @@ -1,7 +1,7 @@ diff --git a/res/layout/history_tabs.xml b/res/layout/history_tabs.xml new file mode 100644 index 00000000..d25fabe6 --- /dev/null +++ b/res/layout/history_tabs.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/res/values/attrs.xml b/res/values/attrs.xml index a523e340..5724143c 100644 --- a/res/values/attrs.xml +++ b/res/values/attrs.xml @@ -3,6 +3,7 @@ + diff --git a/res/values/strings.xml b/res/values/strings.xml index 8d895559..b7cae027 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -314,5 +314,6 @@ Check the \'Round result\' preference in application settings - it should be tur Save history Modify history Modify + Saved history diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java b/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java index 95d095e4..4d1a0973 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java @@ -162,7 +162,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh boolean result = false; if (dragButton instanceof AngleUnitsButton) { - if (dragDirection == DragDirection.up || dragDirection == DragDirection.down ) { + if (dragDirection != DragDirection.left ) { final String directionText = ((AngleUnitsButton) dragButton).getText(dragDirection); if ( directionText != null ) { try { @@ -564,7 +564,9 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh @Override public void onSharedPreferenceChanged(SharedPreferences preferences, @Nullable String key) { - dpclRegister.announce().onDragPreferencesChange(SimpleOnDragListener.getPreferences(preferences, this)); + if (key != null && key.startsWith("org.solovyev.android.calculator.DragButtonCalibrationActivity")) { + dpclRegister.announce().onDragPreferencesChange(SimpleOnDragListener.getPreferences(preferences, this)); + } if (CalculatorEngine.GROUPING_SEPARATOR_P_KEY.equals(key) || CalculatorEngine.MULTIPLICATION_SIGN_P_KEY.equals(key) || diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java b/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java index b948e75c..a0a66435 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java @@ -8,6 +8,7 @@ import jscl.math.function.Constant; import org.achartengine.ChartFactory; import org.jetbrains.annotations.NotNull; import org.solovyev.android.calculator.help.HelpActivity; +import org.solovyev.android.calculator.history.CalculatorHistoryActivity; import org.solovyev.common.utils.StringUtils; /** diff --git a/src/main/java/org/solovyev/android/calculator/DragButtonCalibrationActivity.java b/src/main/java/org/solovyev/android/calculator/DragButtonCalibrationActivity.java deleted file mode 100644 index 9577a147..00000000 --- a/src/main/java/org/solovyev/android/calculator/DragButtonCalibrationActivity.java +++ /dev/null @@ -1,242 +0,0 @@ -/* - * Copyright (c) 2009-2011. Created by serso aka se.solovyev. - * For more information, please, contact se.solovyev@gmail.com - */ - -package org.solovyev.android.calculator; - -import android.app.Activity; -import android.content.SharedPreferences; -import android.os.Bundle; -import android.os.Handler; -import android.preference.PreferenceManager; -import android.util.Log; -import android.view.KeyEvent; -import android.view.MotionEvent; -import android.view.View; -import android.widget.ImageView; -import org.jetbrains.annotations.NotNull; -import org.solovyev.android.view.widgets.*; -import org.solovyev.common.NumberIntervalMapper; -import org.solovyev.common.collections.ManyValuedHashMap; -import org.solovyev.common.collections.ManyValuedMap; -import org.solovyev.common.utils.*; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * User: serso - * Date: 7/16/11 - * Time: 7:28 PM - */ -public class DragButtonCalibrationActivity extends Activity { - - @NotNull - private final List dragHistory = new ArrayList(); - - private final Map map = new HashMap(); - - private static final float DEFAULT_VALUE = -999; - private static final int MIN_HISTORY_FOR_CALIBRATION = 10; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - setContentView(R.layout.drag_button_calibration); - - createCalibrationButton(R.id.calibrationButtonRight, R.id.calibrationArrowRight); - createCalibrationButton(R.id.calibrationButtonLeft, R.id.calibrationArrowLeft); - } - - private void createCalibrationButton(int buttonId, int arrowId) { - final DragButton calibrationButton = (DragButton) findViewById(buttonId); - calibrationButton.setOnDragListener(new CalibrationOnDragListener()); - - ImageView imageView = (ImageView) findViewById(arrowId); - CalibrationArrow calibrationArrow = new CalibrationArrow(imageView); - - createDragDirection(0, calibrationArrow); - - map.put(calibrationButton, calibrationArrow); - } - - private void createDragDirection(long timeout, @NotNull final CalibrationArrow calibrationArrow) { - new Handler().postDelayed(new Runnable() { - public void run() { - calibrationArrow.dragDirection = Math.random() > 0.5 ? DragDirection.up : DragDirection.down; - - calibrationArrow.calibrationArrow.setImageResource(calibrationArrow.dragDirection == DragDirection.down ? R.drawable.down : R.drawable.up); - } - }, timeout); - } - - public void restartClickHandler(View v) { - for (CalibrationArrow calibrationArrow : map.values()) { - createDragDirection(0, calibrationArrow); - } - } - - - private class CalibrationOnDragListener implements OnDragListener { - - @Override - public boolean isSuppressOnClickEvent() { - return true; - } - - @Override - public boolean onDrag(@NotNull DragButton dragButton, @NotNull DragEvent event) { - final Point2d startPoint = event.getStartPoint(); - final MotionEvent motionEvent = event.getMotionEvent(); - - // init end point - final Point2d endPoint = new Point2d(motionEvent.getX(), motionEvent.getY()); - - float distance = MathUtils.getDistance(startPoint, endPoint); - - double angle = Math.toDegrees(MathUtils.getAngle(startPoint, MathUtils.sum(startPoint, SimpleOnDragListener.axis), endPoint)); - - final CalibrationArrow calibrationArrow = map.get(dragButton); - final DragDirection dragDirection = calibrationArrow.dragDirection; - - assert dragDirection == DragDirection.up || dragDirection == DragDirection.down; - - double deviationAngle = angle; - if (dragDirection == DragDirection.up) { - deviationAngle = 180 - deviationAngle; - } - - if (deviationAngle > 45) { - calibrationArrow.calibrationArrow.setImageResource(R.drawable.not_ok); - } else { - calibrationArrow.calibrationArrow.setImageResource(R.drawable.ok); - dragHistory.add(new DragData(dragDirection, distance, angle, (motionEvent.getEventTime() - motionEvent.getDownTime()))); - } - - createDragDirection(500, calibrationArrow); - - return true; - } - } - - @Override - public boolean onKeyDown(int keyCode, KeyEvent event) { - if (keyCode == KeyEvent.KEYCODE_BACK) { - - if (dragHistory.size() > MIN_HISTORY_FOR_CALIBRATION) { - final ManyValuedMap anglesByDirection = new ManyValuedHashMap(); - final ManyValuedMap distancesByDirection = new ManyValuedHashMap(); - final ManyValuedMap timesByDirection = new ManyValuedHashMap(); - for (DragData dragData : dragHistory) { - anglesByDirection.put(dragData.getDirection(), dragData.getAngle()); - distancesByDirection.put(dragData.getDirection(), (double) dragData.getDistance()); - timesByDirection.put(dragData.getDirection(), dragData.getTime()); - } - - final Map angleStatData = getStatDataByDirection(anglesByDirection); - final Map distanceStatData = getStatDataByDirection(distancesByDirection); - final Map timeStatData = getStatDataByDirection(timesByDirection); - - Log.d(this.getClass().getName(), "Angle statistics: "); - logStatData(angleStatData); - - Log.d(this.getClass().getName(), "Distance statistics: "); - logStatData(distanceStatData); - - Log.d(this.getClass().getName(), "Time statistics: "); - logStatData(timeStatData); - - final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); - final SharedPreferences.Editor editor = settings.edit(); - - setPreferences(angleStatData, editor, SimpleOnDragListener.PreferenceType.angle); - setPreferences(distanceStatData, editor, SimpleOnDragListener.PreferenceType.distance); - setPreferences(timeStatData, editor, SimpleOnDragListener.PreferenceType.duration); - - editor.commit(); - } - } - - - return super.onKeyDown(keyCode, event); - } - - private void setPreferences(@NotNull Map statData, @NotNull SharedPreferences.Editor editor, @NotNull SimpleOnDragListener.PreferenceType preferenceType) { - final Mapper> mapper = new NumberIntervalMapper(Float.class); - for (Map.Entry entry : statData.entrySet()) { - final float min = (float) entry.getValue().getMean() - 2 * (float) entry.getValue().getStandardDeviation(); - final float max = (float) entry.getValue().getMean() + 2 * (float) entry.getValue().getStandardDeviation(); - editor.putString(SimpleOnDragListener.getPreferenceId(preferenceType, entry.getKey()), mapper.formatValue(SimpleOnDragListener.transformInterval(preferenceType, entry.getKey(), new NumberInterval(Math.max(0, min), max)))); - } - } - - - private void logStatData(@NotNull Map statData) { - for (Map.Entry entry : statData.entrySet()) { - Log.d(this.getClass().getName(), entry.getKey() + "-> m: " + entry.getValue().getMean() + ", d: " + entry.getValue().getStandardDeviation()); - } - } - - private Map getStatDataByDirection(@NotNull ManyValuedMap valuesByDirection) { - final Map result = new HashMap(); - - for (Map.Entry> entry : valuesByDirection.entrySet()) { - result.put(entry.getKey(), MathUtils.getStatData(entry.getValue())); - } - - return result; - } - - - private class DragData { - - private float distance; - - private double angle; - - private double time; - - @NotNull - private DragDirection direction; - - private DragData(@NotNull DragDirection direction, float distance, double angle, double time) { - this.distance = distance; - this.angle = angle; - this.direction = direction; - this.time = time; - } - - public float getDistance() { - return distance; - } - - public double getAngle() { - return angle; - } - - @NotNull - public DragDirection getDirection() { - return direction; - } - - public double getTime() { - return time; - } - } - - private class CalibrationArrow { - @NotNull - private ImageView calibrationArrow; - - @NotNull - private DragDirection dragDirection = DragDirection.up; - - private CalibrationArrow(@NotNull ImageView calibrationArrow) { - this.calibrationArrow = calibrationArrow; - } - } -} diff --git a/src/main/java/org/solovyev/android/calculator/help/HelpActivity.java b/src/main/java/org/solovyev/android/calculator/help/HelpActivity.java index edbb73c0..b73192f0 100644 --- a/src/main/java/org/solovyev/android/calculator/help/HelpActivity.java +++ b/src/main/java/org/solovyev/android/calculator/help/HelpActivity.java @@ -35,8 +35,6 @@ public class HelpActivity extends TabActivity { createTab(tabHost, "screens", R.string.c_screens, HelpScreensActivity.class); tabHost.setCurrentTab(0); - - } private void createTab(@NotNull TabHost tabHost, @@ -49,8 +47,7 @@ public class HelpActivity extends TabActivity { final Intent intent = new Intent().setClass(this, activityClass); // Initialize a TabSpec for each tab and add it to the TabHost - spec = tabHost.newTabSpec(tabId).setIndicator(getString(tabCaptionId)) - .setContent(intent); + spec = tabHost.newTabSpec(tabId).setIndicator(getString(tabCaptionId)).setContent(intent); tabHost.addTab(spec); } diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorHistoryActivity.java b/src/main/java/org/solovyev/android/calculator/history/AbstractHistoryActivity.java similarity index 71% rename from src/main/java/org/solovyev/android/calculator/CalculatorHistoryActivity.java rename to src/main/java/org/solovyev/android/calculator/history/AbstractHistoryActivity.java index 1bd2b2ac..dcc4868e 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorHistoryActivity.java +++ b/src/main/java/org/solovyev/android/calculator/history/AbstractHistoryActivity.java @@ -4,7 +4,7 @@ * or visit http://se.solovyev.org */ -package org.solovyev.android.calculator; +package org.solovyev.android.calculator.history; import android.app.AlertDialog; import android.app.ListActivity; @@ -15,16 +15,15 @@ import android.view.*; import android.widget.*; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.solovyev.android.calculator.history.*; +import org.solovyev.android.calculator.CalculatorModel; +import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.android.view.AMenu; import org.solovyev.android.view.AMenuItem; import org.solovyev.android.view.MenuImpl; -import org.solovyev.android.view.prefs.ResourceCache; import org.solovyev.common.utils.*; import org.solovyev.common.utils.Filter; -import java.text.SimpleDateFormat; import java.util.*; /** @@ -32,9 +31,9 @@ import java.util.*; * Date: 10/15/11 * Time: 1:13 PM */ -public class CalculatorHistoryActivity extends ListActivity { +public abstract class AbstractHistoryActivity extends ListActivity { - private static final Comparator COMPARATOR = new Comparator() { + public static final Comparator COMPARATOR = new Comparator() { @Override public int compare(CalculatorHistoryState state1, CalculatorHistoryState state2) { if (state1.isSaved() == state2.isSaved()) { @@ -59,13 +58,12 @@ public class CalculatorHistoryActivity extends ListActivity { setContentView(R.layout.history_activity); - final List historyList = getHistoryList(); - if ( historyList.isEmpty() ) { +/* if ( historyList.isEmpty() ) { Toast.makeText(this, R.string.c_history_is_empty, Toast.LENGTH_SHORT).show(); this.finish(); - } + }*/ - adapter = new HistoryArrayAdapter(this, R.layout.history, R.id.history_item, historyList); + adapter = new HistoryArrayAdapter(this, R.layout.history, R.id.history_item, new ArrayList()); setListAdapter(adapter); final ListView lv = getListView(); @@ -77,7 +75,7 @@ public class CalculatorHistoryActivity extends ListActivity { final int position, final long id) { - useHistoryItem((CalculatorHistoryState) parent.getItemAtPosition(position), CalculatorHistoryActivity.this); + useHistoryItem((CalculatorHistoryState) parent.getItemAtPosition(position), AbstractHistoryActivity.this); } }); @@ -86,7 +84,7 @@ public class CalculatorHistoryActivity extends ListActivity { public boolean onItemLongClick(AdapterView parent, View view, final int position, long id) { final CalculatorHistoryState historyState = (CalculatorHistoryState) parent.getItemAtPosition(position); - final Context context = CalculatorHistoryActivity.this; + final Context context = AbstractHistoryActivity.this; final HistoryItemMenuData data = new HistoryItemMenuData(historyState, adapter); @@ -105,10 +103,11 @@ public class CalculatorHistoryActivity extends ListActivity { if (historyState.getDisplayState().isValid() && StringUtils.isEmpty(historyState.getDisplayState().getEditorState().getText())) { menuItems.remove(HistoryItemMenuItem.copy_result); } + final AMenu historyItemMenu = new MenuImpl(menuItems); - AlertDialog.Builder builder = new AlertDialog.Builder(context); - builder.setItems(historyItemMenu.getMenuCaptions(), new DialogInterface.OnClickListener() { + final AlertDialog.Builder menuDialogBuilder = new AlertDialog.Builder(context); + menuDialogBuilder.setItems(historyItemMenu.getMenuCaptions(), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { final AMenuItem historyItemMenuItem = historyItemMenu.itemAt(item); if (historyItemMenuItem != null) { @@ -116,12 +115,31 @@ public class CalculatorHistoryActivity extends ListActivity { } } }); - builder.create().show(); + menuDialogBuilder.create().show(); + return true; } }); } + @Override + protected void onResume() { + super.onResume(); + + final List historyList = getHistoryList(); + try { + this.adapter.setNotifyOnChange(false); + this.adapter.clear(); + for (CalculatorHistoryState historyState : historyList) { + this.adapter.add(historyState); + } + } finally { + this.adapter.setNotifyOnChange(true); + } + + this.adapter.notifyDataSetChanged(); + } + public static boolean isAlreadySaved(@NotNull CalculatorHistoryState historyState) { assert !historyState.isSaved(); @@ -145,7 +163,7 @@ public class CalculatorHistoryActivity extends ListActivity { return result; } - public static void useHistoryItem(@NotNull final CalculatorHistoryState historyState, @NotNull CalculatorHistoryActivity activity) { + public static void useHistoryItem(@NotNull final CalculatorHistoryState historyState, @NotNull AbstractHistoryActivity activity) { CalculatorModel.instance.doTextOperation(new CalculatorModel.TextOperation() { @Override @@ -161,9 +179,9 @@ public class CalculatorHistoryActivity extends ListActivity { activity.finish(); } - private static List getHistoryList() { - final List calculatorHistoryStates = new ArrayList(CalculatorHistory.instance.getStates()); - calculatorHistoryStates.addAll(CalculatorHistory.instance.getSavedHistory()); + @NotNull + private List getHistoryList() { + final List calculatorHistoryStates = getHistoryItems(); Collections.sort(calculatorHistoryStates, COMPARATOR); @@ -180,54 +198,8 @@ public class CalculatorHistoryActivity extends ListActivity { return calculatorHistoryStates; } - public class HistoryArrayAdapter extends ArrayAdapter { - - private HistoryArrayAdapter(Context context, int resource, int textViewResourceId, @NotNull List historyList) { - super(context, resource, textViewResourceId, historyList); - } - - @Override - public View getView(int position, View convertView, ViewGroup parent) { - final ViewGroup result = (ViewGroup) super.getView(position, convertView, parent); - - final CalculatorHistoryState state = getItem(position); - - final TextView time = (TextView) result.findViewById(R.id.history_time); - time.setText(new SimpleDateFormat().format(new Date(state.getTime()))); - - final TextView editor = (TextView) result.findViewById(R.id.history_item); - editor.setText(getHistoryText(state)); - - final TextView commentView = (TextView) result.findViewById(R.id.history_item_comment); - final String comment = state.getComment(); - if (!StringUtils.isEmpty(comment)) { - commentView.setText(comment); - } else { - commentView.setText(""); - } - - final TextView status = (TextView) result.findViewById(R.id.history_item_status); - if (state.isSaved()) { - status.setText(ResourceCache.instance.getCaption("c_history_item_saved")); - } else { - if ( isAlreadySaved(state) ) { - status.setText(ResourceCache.instance.getCaption("c_history_item_already_saved")); - } else { - status.setText(ResourceCache.instance.getCaption("c_history_item_not_saved")); - } - } - - return result; - } - - @Override - public void notifyDataSetChanged() { - this.setNotifyOnChange(false); - this.sort(COMPARATOR); - this.setNotifyOnChange(true); - super.notifyDataSetChanged(); - } - } + @NotNull + protected abstract List getHistoryItems(); @NotNull public static String getHistoryText(@NotNull CalculatorHistoryState state) { diff --git a/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryActivity.java b/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryActivity.java new file mode 100644 index 00000000..7772417f --- /dev/null +++ b/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryActivity.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.android.calculator.history; + +import android.app.Activity; +import android.app.TabActivity; +import android.content.Intent; +import android.os.Bundle; +import android.widget.TabHost; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.solovyev.android.calculator.R; + +/** + * User: serso + * Date: 12/18/11 + * Time: 7:37 PM + */ +public class CalculatorHistoryActivity extends TabActivity { + + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setContentView(R.layout.history_tabs); + + final TabHost tabHost = getTabHost(); + + createTab(tabHost, "saved_history", R.string.c_saved_history, SavedHistoryActivityTab.class); + createTab(tabHost, "history", R.string.c_history, HistoryActivityTab.class); + + tabHost.setCurrentTab(0); + } + + private void createTab(@NotNull TabHost tabHost, + @NotNull String tabId, + int tabCaptionId, + @NotNull Class activityClass) { + + TabHost.TabSpec spec; + + final Intent intent = new Intent().setClass(this, activityClass); + + // Initialize a TabSpec for each tab and add it to the TabHost + spec = tabHost.newTabSpec(tabId).setIndicator(getString(tabCaptionId)).setContent(intent); + + tabHost.addTab(spec); + } +} diff --git a/src/main/java/org/solovyev/android/calculator/history/HistoryActivityTab.java b/src/main/java/org/solovyev/android/calculator/history/HistoryActivityTab.java new file mode 100644 index 00000000..0fdb1d19 --- /dev/null +++ b/src/main/java/org/solovyev/android/calculator/history/HistoryActivityTab.java @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.android.calculator.history; + +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.List; + +/** + * User: serso + * Date: 12/18/11 + * Time: 7:39 PM + */ +public class HistoryActivityTab extends AbstractHistoryActivity { + @NotNull + @Override + protected List getHistoryItems() { + return new ArrayList(CalculatorHistory.instance.getStates()); + } +} diff --git a/src/main/java/org/solovyev/android/calculator/history/HistoryArrayAdapter.java b/src/main/java/org/solovyev/android/calculator/history/HistoryArrayAdapter.java new file mode 100644 index 00000000..2ddaa33e --- /dev/null +++ b/src/main/java/org/solovyev/android/calculator/history/HistoryArrayAdapter.java @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.android.calculator.history; + +import android.content.Context; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.TextView; +import org.jetbrains.annotations.NotNull; +import org.solovyev.android.calculator.R; +import org.solovyev.android.view.prefs.ResourceCache; +import org.solovyev.common.utils.StringUtils; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.List; + +/** +* User: serso +* Date: 12/18/11 +* Time: 7:39 PM +*/ +public class HistoryArrayAdapter extends ArrayAdapter { + + HistoryArrayAdapter(Context context, int resource, int textViewResourceId, @NotNull List historyList) { + super(context, resource, textViewResourceId, historyList); + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + final ViewGroup result = (ViewGroup) super.getView(position, convertView, parent); + + final CalculatorHistoryState state = getItem(position); + + final TextView time = (TextView) result.findViewById(R.id.history_time); + time.setText(new SimpleDateFormat().format(new Date(state.getTime()))); + + final TextView editor = (TextView) result.findViewById(R.id.history_item); + editor.setText(AbstractHistoryActivity.getHistoryText(state)); + + final TextView commentView = (TextView) result.findViewById(R.id.history_item_comment); + final String comment = state.getComment(); + if (!StringUtils.isEmpty(comment)) { + commentView.setText(comment); + } else { + commentView.setText(""); + } + + final TextView status = (TextView) result.findViewById(R.id.history_item_status); + if (state.isSaved()) { + status.setText(ResourceCache.instance.getCaption("c_history_item_saved")); + } else { + if ( AbstractHistoryActivity.isAlreadySaved(state) ) { + status.setText(ResourceCache.instance.getCaption("c_history_item_already_saved")); + } else { + status.setText(ResourceCache.instance.getCaption("c_history_item_not_saved")); + } + } + + return result; + } + + @Override + public void notifyDataSetChanged() { + this.setNotifyOnChange(false); + this.sort(AbstractHistoryActivity.COMPARATOR); + this.setNotifyOnChange(true); + super.notifyDataSetChanged(); + } +} diff --git a/src/main/java/org/solovyev/android/calculator/history/HistoryItemMenuData.java b/src/main/java/org/solovyev/android/calculator/history/HistoryItemMenuData.java index ed7daeaa..32dc45ec 100644 --- a/src/main/java/org/solovyev/android/calculator/history/HistoryItemMenuData.java +++ b/src/main/java/org/solovyev/android/calculator/history/HistoryItemMenuData.java @@ -7,7 +7,6 @@ package org.solovyev.android.calculator.history; import org.jetbrains.annotations.NotNull; -import org.solovyev.android.calculator.CalculatorHistoryActivity; /** * User: serso @@ -17,12 +16,12 @@ import org.solovyev.android.calculator.CalculatorHistoryActivity; public class HistoryItemMenuData { @NotNull - private final CalculatorHistoryActivity.HistoryArrayAdapter adapter; + private final HistoryArrayAdapter adapter; @NotNull private final CalculatorHistoryState historyState; - public HistoryItemMenuData(@NotNull CalculatorHistoryState historyState, CalculatorHistoryActivity.HistoryArrayAdapter adapter) { + public HistoryItemMenuData(@NotNull CalculatorHistoryState historyState, HistoryArrayAdapter adapter) { this.historyState = historyState; this.adapter = adapter; } @@ -33,7 +32,7 @@ public class HistoryItemMenuData { } @NotNull - public CalculatorHistoryActivity.HistoryArrayAdapter getAdapter() { + public HistoryArrayAdapter getAdapter() { return adapter; } } diff --git a/src/main/java/org/solovyev/android/calculator/history/HistoryItemMenuItem.java b/src/main/java/org/solovyev/android/calculator/history/HistoryItemMenuItem.java index 75718c86..b89c4ebc 100644 --- a/src/main/java/org/solovyev/android/calculator/history/HistoryItemMenuItem.java +++ b/src/main/java/org/solovyev/android/calculator/history/HistoryItemMenuItem.java @@ -19,7 +19,6 @@ import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import org.jetbrains.annotations.NotNull; -import org.solovyev.android.calculator.CalculatorHistoryActivity; import org.solovyev.android.calculator.R; import org.solovyev.android.view.AMenuItem; import org.solovyev.android.view.prefs.ResourceCache; @@ -31,16 +30,18 @@ import org.solovyev.common.utils.StringUtils; * Time: 3:09 PM */ public enum HistoryItemMenuItem implements AMenuItem { + use("c_use_expression") { @Override public void doAction(@NotNull HistoryItemMenuData data, @NotNull Context context) { - if (context instanceof CalculatorHistoryActivity) { - CalculatorHistoryActivity.useHistoryItem(data.getHistoryState(), (CalculatorHistoryActivity) context); + if (context instanceof AbstractHistoryActivity) { + AbstractHistoryActivity.useHistoryItem(data.getHistoryState(), (AbstractHistoryActivity) context); } else { Log.e(HistoryItemMenuItem.class.getName(), CalculatorHistoryActivity.class + " must be passed as context!"); } } }, + copy_expression("c_copy_expression") { @Override public void doAction(@NotNull HistoryItemMenuData data, @NotNull Context context) { @@ -53,6 +54,7 @@ public enum HistoryItemMenuItem implements AMenuItem { } } }, + copy_result("c_copy_result") { @Override public void doAction(@NotNull HistoryItemMenuData data, @NotNull Context context) { @@ -65,6 +67,7 @@ public enum HistoryItemMenuItem implements AMenuItem { } } }, + save("c_save") { @Override public void doAction(@NotNull final HistoryItemMenuData data, @NotNull final Context context) { @@ -76,6 +79,7 @@ public enum HistoryItemMenuItem implements AMenuItem { } } }, + edit("c_edit") { @Override public void doAction(@NotNull final HistoryItemMenuData data, @NotNull final Context context) { @@ -87,6 +91,7 @@ public enum HistoryItemMenuItem implements AMenuItem { } } }, + remove("c_remove") { @Override public void doAction(@NotNull HistoryItemMenuData data, @NotNull Context context) { @@ -106,7 +111,7 @@ public enum HistoryItemMenuItem implements AMenuItem { final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); final View editView = layoutInflater.inflate(R.layout.history_edit, null); final TextView historyExpression = (TextView)editView.findViewById(R.id.history_edit_expression); - historyExpression.setText(CalculatorHistoryActivity.getHistoryText(historyState)); + historyExpression.setText(AbstractHistoryActivity.getHistoryText(historyState)); final EditText comment = (EditText)editView.findViewById(R.id.history_edit_comment); comment.setText(historyState.getComment()); @@ -122,7 +127,8 @@ public enum HistoryItemMenuItem implements AMenuItem { final CalculatorHistoryState savedHistoryItem = CalculatorHistory.instance.addSavedState(historyState); savedHistoryItem.setComment(comment.getText().toString()); CalculatorHistory.instance.save(context); - data.getAdapter().add(savedHistoryItem); + // we don't need to add element to the adapter as adapter of another activity must be updated and not this + //data.getAdapter().add(savedHistoryItem); } else { historyState.setComment(comment.getText().toString()); CalculatorHistory.instance.save(context); diff --git a/src/main/java/org/solovyev/android/calculator/history/SavedHistoryActivityTab.java b/src/main/java/org/solovyev/android/calculator/history/SavedHistoryActivityTab.java new file mode 100644 index 00000000..f2510011 --- /dev/null +++ b/src/main/java/org/solovyev/android/calculator/history/SavedHistoryActivityTab.java @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.android.calculator.history; + +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.List; + +/** + * User: serso + * Date: 12/18/11 + * Time: 7:40 PM + */ +public class SavedHistoryActivityTab extends AbstractHistoryActivity { + @NotNull + @Override + protected List getHistoryItems() { + return new ArrayList(CalculatorHistory.instance.getSavedHistory()); + } +} diff --git a/src/main/java/org/solovyev/android/view/widgets/DirectionDragButton.java b/src/main/java/org/solovyev/android/view/widgets/DirectionDragButton.java index 04539c9e..b787824f 100644 --- a/src/main/java/org/solovyev/android/view/widgets/DirectionDragButton.java +++ b/src/main/java/org/solovyev/android/view/widgets/DirectionDragButton.java @@ -136,26 +136,22 @@ public class DirectionDragButton extends DragButton { @NotNull @Override public Point2d getTextPosition(@NotNull Paint paint, @NotNull Paint basePaint, @NotNull CharSequence text, CharSequence baseText, int w, int h) { - final Point2d result = new Point2d(); - - float width = paint.measureText(" "); - result.setX(width); - - float selfHeight = paint.ascent() + paint.descent(); - - basePaint.measureText(StringUtils.getNotEmpty(baseText, "|")); - - result.setY(h / 2 - selfHeight / 2); - - return result; + return getLeftRightTextPosition(paint, basePaint, text, baseText, w, h, true); } - }/*, + }, + right(DragDirection.right, 1) { @Override public int getAttributeId() { - return 0; + return R.styleable.DragButton_textRight; } - }*/; + + @NotNull + @Override + public Point2d getTextPosition(@NotNull Paint paint, @NotNull Paint basePaint, @NotNull CharSequence text, CharSequence baseText, int w, int h) { + return getLeftRightTextPosition(paint, basePaint, text, baseText, w, h, false); + } + }; @NotNull private final DragDirection dragDirection; @@ -176,6 +172,27 @@ public class DirectionDragButton extends DragButton { @NotNull public abstract Point2d getTextPosition(@NotNull Paint paint, @NotNull Paint basePaint, @NotNull CharSequence text, CharSequence baseText, int w, int h); + @NotNull + private static Point2d getLeftRightTextPosition(@NotNull Paint paint, @NotNull Paint basePaint, CharSequence text, @NotNull CharSequence baseText, int w, int h, boolean left) { + final Point2d result = new Point2d(); + + if (left) { + float width = paint.measureText(" "); + result.setX(width); + } else { + float width = paint.measureText(text.toString() + " "); + result.setX(w - width); + } + + float selfHeight = paint.ascent() + paint.descent(); + + basePaint.measureText(StringUtils.getNotEmpty(baseText, "|")); + + result.setY(h / 2 - selfHeight / 2); + + return result; + } + @NotNull private static Point2d getUpDownTextPosition(@NotNull Paint paint, @NotNull Paint basePaint, @NotNull CharSequence text, CharSequence baseText, float direction, int w, int h) { final Point2d result = new Point2d(); diff --git a/src/main/java/org/solovyev/android/view/widgets/SimpleOnDragListener.java b/src/main/java/org/solovyev/android/view/widgets/SimpleOnDragListener.java index 1e4938c5..256adaf1 100644 --- a/src/main/java/org/solovyev/android/view/widgets/SimpleOnDragListener.java +++ b/src/main/java/org/solovyev/android/view/widgets/SimpleOnDragListener.java @@ -12,10 +12,7 @@ import android.view.MotionEvent; import org.jetbrains.annotations.NotNull; import org.solovyev.android.calculator.R; import org.solovyev.common.NumberIntervalMapper; -import org.solovyev.common.utils.Interval; -import org.solovyev.common.utils.Mapper; -import org.solovyev.common.utils.MathUtils; -import org.solovyev.common.utils.Point2d; +import org.solovyev.common.utils.*; import java.util.HashMap; import java.util.Map; @@ -53,7 +50,12 @@ public class SimpleOnDragListener implements OnDragListener, DragPreferencesChan final Point2d endPoint = new Point2d(motionEvent.getX(), motionEvent.getY()); final float distance = MathUtils.getDistance(startPoint, endPoint); - final double angle = Math.toDegrees(MathUtils.getAngle(startPoint, MathUtils.sum(startPoint, axis), endPoint)); + + final MutableObject right = new MutableObject(); + final double angle = Math.toDegrees(MathUtils.getAngle(startPoint, MathUtils.sum(startPoint, axis), endPoint, right)); + Log.d(String.valueOf(dragButton.getId()), "Angle: " + angle); + Log.d(String.valueOf(dragButton.getId()), "Is right?: " + right.getObject()); + final double duration = motionEvent.getEventTime() - motionEvent.getDownTime(); final Preference distancePreferences = preferences.getPreferencesMap().get(PreferenceType.distance); @@ -66,23 +68,21 @@ public class SimpleOnDragListener implements OnDragListener, DragPreferencesChan Log.d(String.valueOf(dragButton.getId()), "Trying direction interval: " + directionEntry.getValue().getInterval()); if (isInInterval(directionEntry.getValue().getInterval(), distance)) { - for (Map.Entry angleEntry : anglePreferences.getDirectionPreferences().entrySet()) { + final DragPreference anglePreference = anglePreferences.getDirectionPreferences().get(directionEntry.getKey()); - Log.d(String.valueOf(dragButton.getId()), "Trying angle interval: " + angleEntry.getValue().getInterval()); + Log.d(String.valueOf(dragButton.getId()), "Trying angle interval: " + anglePreference.getInterval()); - if (isInInterval(angleEntry.getValue().getInterval(), (float) angle)) { - - Log.d(String.valueOf(dragButton.getId()), "MATCH! Direction: " + angleEntry.getKey()); - - direction = angleEntry.getKey(); + if (directionEntry.getKey() == DragDirection.left && right.getObject()) { + } else if (directionEntry.getKey() == DragDirection.right && !right.getObject()) { + } else { + if (isInInterval(anglePreference.getInterval(), (float) angle)) { + direction = directionEntry.getKey(); + Log.d(String.valueOf(dragButton.getId()), "MATCH! Direction: " + direction); break; } } } - if (direction != null) { - break; - } } if (direction != null) { @@ -116,7 +116,9 @@ public class SimpleOnDragListener implements OnDragListener, DragPreferencesChan Log.d(String.valueOf(dragButton.getId()), "Start point: " + startPoint + ", End point: " + endPoint); Log.d(String.valueOf(dragButton.getId()), "Distance: " + MathUtils.getDistance(startPoint, endPoint)); - Log.d(String.valueOf(dragButton.getId()), "Angle: " + Math.toDegrees(MathUtils.getAngle(startPoint, MathUtils.sum(startPoint, axis), endPoint))); + final MutableObject right = new MutableObject(); + Log.d(String.valueOf(dragButton.getId()), "Angle: " + Math.toDegrees(MathUtils.getAngle(startPoint, MathUtils.sum(startPoint, axis), endPoint, right))); + Log.d(String.valueOf(dragButton.getId()), "Is right angle? " + right); Log.d(String.valueOf(dragButton.getId()), "Axis: " + axis + " Vector: " + MathUtils.subtract(endPoint, startPoint)); Log.d(String.valueOf(dragButton.getId()), "Total time: " + (motionEvent.getEventTime() - motionEvent.getDownTime()) + " ms"); } @@ -209,11 +211,11 @@ public class SimpleOnDragListener implements OnDragListener, DragPreferencesChan interval.setLeftBorder(180f - rightBorder); interval.setRightBorder(180f - leftBorder); } else if (dragDirection == DragDirection.left ) { - interval.setLeftBorder(90f - rightBorder / 2); - interval.setRightBorder(90f + rightBorder / 2); + interval.setLeftBorder(90f - rightBorder); + interval.setRightBorder(90f + rightBorder); } else if ( dragDirection == DragDirection.right ) { - interval.setLeftBorder(180f + 90f - rightBorder / 2); - interval.setRightBorder(180f + 90f + rightBorder / 2); + interval.setLeftBorder(90f - rightBorder); + interval.setRightBorder(90f + rightBorder); } }