From 90e10376185f9e23c234596ef89ead27d96d144c Mon Sep 17 00:00:00 2001 From: serso Date: Sat, 15 Oct 2011 14:52:05 +0400 Subject: [PATCH] history activity --- AndroidManifest.xml | 3 + res/layout-land/main.xml | 56 ++++++++--- res/layout/calc_history_button.xml | 7 +- res/layout/history.xml | 29 ++++++ res/layout/history_activity.xml | 20 ++++ res/layout/var.xml | 1 + res/menu/main_menu.xml | 3 + res/values-ru/strings.xml | 5 + res/values/strings.xml | 4 + .../calculator/AbstractHistoryState.java | 27 +++++ .../calculator/CalculatorActivity.java | 36 ++++++- .../CalculatorDisplayHistoryState.java | 1 + .../android/calculator/CalculatorHistory.java | 8 +- .../calculator/CalculatorHistoryActivity.java | 99 +++++++++++++++++++ .../calculator/CalculatorHistoryState.java | 5 +- .../android/view/widgets/ColorButton.java | 2 +- 16 files changed, 280 insertions(+), 26 deletions(-) create mode 100644 res/layout/history.xml create mode 100644 res/layout/history_activity.xml create mode 100644 src/main/java/org/solovyev/android/calculator/AbstractHistoryState.java create mode 100644 src/main/java/org/solovyev/android/calculator/CalculatorHistoryActivity.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index c00e4bea..f16cdb54 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -23,6 +23,9 @@ + + diff --git a/res/layout-land/main.xml b/res/layout-land/main.xml index 72a3b525..f6172c11 100644 --- a/res/layout-land/main.xml +++ b/res/layout-land/main.xml @@ -17,15 +17,27 @@ - + - + + - + - + diff --git a/res/layout/calc_history_button.xml b/res/layout/calc_history_button.xml index ee4bacc1..210556c0 100644 --- a/res/layout/calc_history_button.xml +++ b/res/layout/calc_history_button.xml @@ -8,7 +8,10 @@ \ No newline at end of file + style="@style/control_button_style" + a:textSize="18dp" + a:textStyle="bold" + a:onClick="historyButtonClickHandler"/> \ No newline at end of file diff --git a/res/layout/history.xml b/res/layout/history.xml new file mode 100644 index 00000000..4330976e --- /dev/null +++ b/res/layout/history.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/res/layout/history_activity.xml b/res/layout/history_activity.xml new file mode 100644 index 00000000..48b190c0 --- /dev/null +++ b/res/layout/history_activity.xml @@ -0,0 +1,20 @@ + + + + + + + + + \ No newline at end of file diff --git a/res/layout/var.xml b/res/layout/var.xml index 0c6b0707..8dbc7a6a 100644 --- a/res/layout/var.xml +++ b/res/layout/var.xml @@ -20,6 +20,7 @@ a:layout_width="match_parent" a:layout_height="match_parent" a:textSize="20dp" + a:textColor="@android:color/white" a:paddingBottom="3dp" a:paddingLeft="6dp" a:paddingRight="6dp" diff --git a/res/menu/main_menu.xml b/res/menu/main_menu.xml index 511a8f1b..16d64fa0 100644 --- a/res/menu/main_menu.xml +++ b/res/menu/main_menu.xml @@ -5,6 +5,9 @@ + + diff --git a/res/values-ru/strings.xml b/res/values-ru/strings.xml index 6dcb2b49..9907aab4 100644 --- a/res/values-ru/strings.xml +++ b/res/values-ru/strings.xml @@ -70,4 +70,9 @@ http://paypal.com\n + История + M + История пуста! + История + diff --git a/res/values/strings.xml b/res/values/strings.xml index 682052b0..fb303679 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -70,4 +70,8 @@ You can thank the author of this program by email\n\n se.solovyev@gmail.com\n\n or donate money via\n\nhttp://paypal.com\n + History + M + History is empty! + History diff --git a/src/main/java/org/solovyev/android/calculator/AbstractHistoryState.java b/src/main/java/org/solovyev/android/calculator/AbstractHistoryState.java new file mode 100644 index 00000000..d702df76 --- /dev/null +++ b/src/main/java/org/solovyev/android/calculator/AbstractHistoryState.java @@ -0,0 +1,27 @@ +/* + * 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; + +import org.jetbrains.annotations.NotNull; + +import java.util.Date; + +/** + * User: serso + * Date: 10/15/11 + * Time: 1:45 PM + */ +public class AbstractHistoryState { + + @NotNull + private final Date time = new Date(); + + @NotNull + public Date getTime() { + return time; + } +} diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java b/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java index 5bf0fdc2..dd4a1bcf 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java @@ -37,6 +37,10 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh public static final String INSERT_TEXT_INTENT = "org.solovyev.android.calculator.CalculatorActivity.insertText"; public static final String INSERT_TEXT_INTENT_EXTRA_STRING = "org.solovyev.android.calculator.CalculatorActivity.insertText.extraString"; + public static final String SET_TEXT_INTENT = "org.solovyev.android.calculator.CalculatorActivity.setText"; + public static final String SET_TEXT_INTENT_EXTRA_STRING = "org.solovyev.android.calculator.CalculatorActivity.settText.extraString"; + + private static final int HVGA_WIDTH_PIXELS = 320; @NotNull @@ -46,7 +50,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh private CalculatorView calculatorView; @NotNull - private BroadcastReceiver insertTextReceiver; + private BroadcastReceiver textReceiver; private volatile boolean initialized; @@ -106,7 +110,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh calculatorView = new CalculatorView(this, CalculatorModel.instance); - insertTextReceiver = new BroadcastReceiver() { + textReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (INSERT_TEXT_INTENT.equals(intent.getAction())) { @@ -119,11 +123,22 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh } }); } + } else if (SET_TEXT_INTENT.equals(intent.getAction())) { + final String s = intent.getStringExtra(SET_TEXT_INTENT_EXTRA_STRING); + if (!StringUtils.isEmpty(s)) { + calculatorView.doTextOperation(new CalculatorView.TextOperation() { + @Override + public void doOperation(@NotNull EditText editor) { + editor.setText(s); + } + }); + } } } }; - registerReceiver(insertTextReceiver, new IntentFilter(INSERT_TEXT_INTENT)); + registerReceiver(textReceiver, new IntentFilter(INSERT_TEXT_INTENT)); + registerReceiver(textReceiver, new IntentFilter(SET_TEXT_INTENT)); } private synchronized void firstTimeInit() { @@ -140,7 +155,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh @Override protected void onDestroy() { - unregisterReceiver(insertTextReceiver); + unregisterReceiver(textReceiver); super.onDestroy(); } @@ -154,6 +169,11 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh this.calculatorView.evaluate(); } + @SuppressWarnings({"UnusedDeclaration"}) + public void historyButtonClickHandler(@NotNull View v) { + this.showHistory(); + } + @SuppressWarnings({"UnusedDeclaration"}) public void eraseButtonClickHandler(@NotNull View v) { calculatorView.doTextOperation(new CalculatorView.TextOperation() { @@ -267,6 +287,10 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh showSettings(); result = true; break; + case R.id.main_menu_item_history: + showHistory(); + result = true; + break; case R.id.main_menu_item_about: showAbout(); result = true; @@ -282,6 +306,10 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh return result; } + private void showHistory() { + startActivity(new Intent(this, CalculatorHistoryActivity.class)); + } + private void showSettings() { startActivity(new Intent(this, CalculatorPreferencesActivity.class)); } diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorDisplayHistoryState.java b/src/main/java/org/solovyev/android/calculator/CalculatorDisplayHistoryState.java index 83106328..da058f3e 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorDisplayHistoryState.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorDisplayHistoryState.java @@ -42,6 +42,7 @@ public class CalculatorDisplayHistoryState { this.valid = valid; } + @NotNull public EditorHistoryState getEditorHistoryState() { return editorHistoryState; } diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorHistory.java b/src/main/java/org/solovyev/android/calculator/CalculatorHistory.java index b0deb3b0..0aaa110f 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorHistory.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorHistory.java @@ -12,6 +12,8 @@ import org.solovyev.common.utils.history.HistoryAction; import org.solovyev.common.utils.history.HistoryHelper; import org.solovyev.common.utils.history.SimpleHistoryHelper; +import java.util.List; + /** * User: serso * Date: 10/9/11 @@ -68,5 +70,9 @@ public enum CalculatorHistory implements HistoryHelper { historyHelper.addState(currentState); } - + @NotNull + @Override + public List getStates() { + return historyHelper.getStates(); + } } diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorHistoryActivity.java b/src/main/java/org/solovyev/android/calculator/CalculatorHistoryActivity.java new file mode 100644 index 00000000..09e156d0 --- /dev/null +++ b/src/main/java/org/solovyev/android/calculator/CalculatorHistoryActivity.java @@ -0,0 +1,99 @@ +/* + * 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; + +import android.app.ListActivity; +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.view.View; +import android.view.ViewGroup; +import android.widget.*; +import org.jetbrains.annotations.NotNull; +import org.solovyev.common.utils.Filter; +import org.solovyev.common.utils.FilterRule; +import org.solovyev.common.utils.FilterRulesChain; +import org.solovyev.common.utils.StringUtils; + +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.List; + +/** + * User: serso + * Date: 10/15/11 + * Time: 1:13 PM + */ +public class CalculatorHistoryActivity extends ListActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setContentView(R.layout.history_activity); + + final List historyList = getHistoryList(); + if ( historyList.isEmpty() ) { + Toast.makeText(this, R.string.c_history_is_empty, Toast.LENGTH_SHORT).show(); + this.finish(); + } + + setListAdapter(new HistoryArrayAdapter(this, R.layout.history, R.id.history_item, historyList)); + + final ListView lv = getListView(); + lv.setTextFilterEnabled(true); + + lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { + public void onItemClick(AdapterView parent, View view, + int position, long id) { + final Intent intent = new Intent(CalculatorActivity.SET_TEXT_INTENT); + intent.putExtra(CalculatorActivity.SET_TEXT_INTENT_EXTRA_STRING, ((CalculatorHistoryState) parent.getItemAtPosition(position)).getEditorState().getText()); + sendOrderedBroadcast(intent, null); + + CalculatorHistoryActivity.this.finish(); + } + }); + } + + private static List getHistoryList() { + final List calculatorHistoryStates = new ArrayList(CalculatorHistory.instance.getStates()); + + final FilterRulesChain filterRulesChain = new FilterRulesChain(); + filterRulesChain.addFilterRule(new FilterRule() { + @Override + public boolean isFiltered(CalculatorHistoryState object) { + return object == null || StringUtils.isEmpty(object.getEditorState().getText()); + } + }); + + new Filter(filterRulesChain).filter(calculatorHistoryStates.iterator()); + + return calculatorHistoryStates; + } + + private static 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(state.getTime())); + + final TextView editor = (TextView) result.findViewById(R.id.history_item); + editor.setText(state.getEditorState().getText() + "=" + state.getDisplayState().getEditorHistoryState().getText()); + + return result; + } + } +} diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorHistoryState.java b/src/main/java/org/solovyev/android/calculator/CalculatorHistoryState.java index 3e7136bf..545d2b02 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorHistoryState.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorHistoryState.java @@ -12,7 +12,7 @@ import org.jetbrains.annotations.NotNull; * Date: 9/11/11 * Time: 12:16 AM */ -public class CalculatorHistoryState { +public class CalculatorHistoryState extends AbstractHistoryState{ @NotNull private EditorHistoryState editorState; @@ -20,7 +20,8 @@ public class CalculatorHistoryState { @NotNull private CalculatorDisplayHistoryState displayState; - public CalculatorHistoryState(@NotNull EditorHistoryState editorState, @NotNull CalculatorDisplayHistoryState displayState) { + public CalculatorHistoryState(@NotNull EditorHistoryState editorState, + @NotNull CalculatorDisplayHistoryState displayState) { this.editorState = editorState; this.displayState = displayState; } diff --git a/src/main/java/org/solovyev/android/view/widgets/ColorButton.java b/src/main/java/org/solovyev/android/view/widgets/ColorButton.java index bcdc87fc..caa10219 100644 --- a/src/main/java/org/solovyev/android/view/widgets/ColorButton.java +++ b/src/main/java/org/solovyev/android/view/widgets/ColorButton.java @@ -167,7 +167,7 @@ public class ColorButton extends Button { if (topDr != null) { canvas.save(); canvas.translate(scrollX + compoundPaddingLeft + (hspace - topDr.getBounds().width()) / 2, - scrollY + getPaddingTop() + vspace / 2); + scrollY + getPaddingTop() + vspace / 2); topDr.draw(canvas); canvas.restore(); }