history activity

This commit is contained in:
serso 2011-10-15 14:52:05 +04:00
parent 4d7675ebd2
commit 56f224cd16
16 changed files with 280 additions and 26 deletions

View File

@ -23,6 +23,9 @@
<activity a:name=".CalculatorPreferencesActivity" <activity a:name=".CalculatorPreferencesActivity"
a:label="@string/c_app_settings"/> a:label="@string/c_app_settings"/>
<activity a:name=".CalculatorHistoryActivity"
a:label="@string/c_app_history"/>
<activity a:name=".AboutActivity" <activity a:name=".AboutActivity"
a:label="@string/c_about"/> a:label="@string/c_about"/>

View File

@ -17,15 +17,27 @@
<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">
<include layout="@layout/calc_left_button" <org.solovyev.android.view.widgets.DirectionDragButton xmlns:a="http://schemas.android.com/apk/res/android"
a:layout_width="match_parent" xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
a:layout_height="match_parent" a:id="@+id/leftButton"
a:layout_weight="1.5"/> calc:textUp="↞"
a:text="←"
style="@style/control_button_style"
a:onClick="moveLeftButtonClickHandler"
a:layout_width="match_parent"
a:layout_height="match_parent"
a:layout_weight="1.5"/>
<include layout="@layout/calc_erase_button"
a:layout_width="match_parent" <org.solovyev.android.view.widgets.ColorButton xmlns:a="http://schemas.android.com/apk/res/android"
a:layout_height="match_parent" xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
a:layout_weight="1.5"/> a:id="@+id/eraseButton"
a:drawableTop="@drawable/sym_keyboard_delete"
style="@style/control_image_button_style"
a:onClick="eraseButtonClickHandler"
a:layout_width="match_parent"
a:layout_height="match_parent"
a:layout_weight="1.5"/>
<include layout="@layout/calc_display" <include layout="@layout/calc_display"
@ -33,15 +45,27 @@
a:layout_height="match_parent" a:layout_height="match_parent"
a:layout_weight="1"/> a:layout_weight="1"/>
<include layout="@layout/calc_clear_button" <org.solovyev.android.view.widgets.ColorButton xmlns:a="http://schemas.android.com/apk/res/android"
a:layout_width="match_parent" xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
a:layout_height="match_parent" a:id="@+id/clearButton"
a:layout_weight="1.5"/> a:text="@string/c_clear"
a:textStyle="bold"
style="@style/control_image_button_style"
a:onClick="clearButtonClickHandler"
a:layout_width="match_parent"
a:layout_height="match_parent"
a:layout_weight="1.5"/>
<include layout="@layout/calc_right_button" <org.solovyev.android.view.widgets.DirectionDragButton xmlns:a="http://schemas.android.com/apk/res/android"
a:layout_width="match_parent" xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
a:layout_height="match_parent" a:id="@+id/rightButton"
a:layout_weight="1.5"/> calc:textUp="↠"
a:text="→"
style="@style/control_button_style"
a:onClick="moveRightButtonClickHandler"
a:layout_width="match_parent"
a:layout_height="match_parent"
a:layout_weight="1.5"/>
</LinearLayout> </LinearLayout>

View File

@ -8,7 +8,10 @@
<org.solovyev.android.view.widgets.DirectionDragButton xmlns:a="http://schemas.android.com/apk/res/android" <org.solovyev.android.view.widgets.DirectionDragButton xmlns:a="http://schemas.android.com/apk/res/android"
xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator" xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
a:id="@+id/historyButton" a:id="@+id/historyButton"
a:text="" a:text="@string/c_history_button"
calc:textUp="@string/c_undo" calc:textUp="@string/c_undo"
calc:textDown="@string/c_redo" calc:textDown="@string/c_redo"
style="@style/control_button_style"/> style="@style/control_button_style"
a:textSize="18dp"
a:textStyle="bold"
a:onClick="historyButtonClickHandler"/>

29
res/layout/history.xml Normal file
View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ 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
-->
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
a:orientation="vertical"
a:layout_width="match_parent"
a:layout_height="match_parent">
<TextView a:id="@+id/history_time"
a:layout_width="match_parent"
a:layout_height="match_parent"
a:textColor="@android:color/white"
a:paddingLeft="6dp"
a:paddingRight="6dp"
a:textStyle="bold"/>
<TextView a:id="@+id/history_item"
a:layout_width="match_parent"
a:layout_height="match_parent"
a:paddingBottom="6dp"
a:paddingLeft="6dp"
a:paddingRight="6dp"/>
</LinearLayout>

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ 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
-->
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
a:orientation="vertical"
a:layout_width="match_parent"
a:layout_height="match_parent">
<ListView
a:layout_width="match_parent"
a:layout_height="match_parent"
a:layout_weight="1"
a:id="@android:id/list"/>
</LinearLayout>

View File

@ -20,6 +20,7 @@
a:layout_width="match_parent" a:layout_width="match_parent"
a:layout_height="match_parent" a:layout_height="match_parent"
a:textSize="20dp" a:textSize="20dp"
a:textColor="@android:color/white"
a:paddingBottom="3dp" a:paddingBottom="3dp"
a:paddingLeft="6dp" a:paddingLeft="6dp"
a:paddingRight="6dp" a:paddingRight="6dp"

View File

@ -5,6 +5,9 @@
<item a:id="@+id/main_menu_item_settings" <item a:id="@+id/main_menu_item_settings"
a:title="@string/c_settings"/> a:title="@string/c_settings"/>
<item a:id="@+id/main_menu_item_history"
a:title="@string/c_history"/>
<item a:id="@+id/main_menu_item_about" <item a:id="@+id/main_menu_item_about"
a:title="@string/c_about"/> a:title="@string/c_about"/>

View File

@ -70,4 +70,9 @@
http://paypal.com</a>\n http://paypal.com</a>\n
</string> </string>
<string name="c_history">История</string>
<string name="c_history_button">M</string>
<string name="c_history_is_empty">История пуста!</string>
<string name="c_app_history">История</string>
</resources> </resources>

View File

@ -70,4 +70,8 @@
<string name="c_donate_text">You can thank the author of this program by email\n\n <string name="c_donate_text">You can thank the author of this program by email\n\n
<a href="mailto:se.solovyev@gmail.com">se.solovyev@gmail.com</a>\n\n <a href="mailto:se.solovyev@gmail.com">se.solovyev@gmail.com</a>\n\n
or donate money via\n\n<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&amp;business=se%2esolovyev%40gmail%2ecom&amp;lc=RU&amp;item_name=Android%20Calculator&amp;currency_code=USD&amp;bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted">http://paypal.com</a>\n</string> or donate money via\n\n<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&amp;business=se%2esolovyev%40gmail%2ecom&amp;lc=RU&amp;item_name=Android%20Calculator&amp;currency_code=USD&amp;bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted">http://paypal.com</a>\n</string>
<string name="c_history">History</string>
<string name="c_history_button">M</string>
<string name="c_history_is_empty">History is empty!</string>
<string name="c_app_history">History</string>
</resources> </resources>

View File

@ -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;
}
}

View File

@ -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 = "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 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; private static final int HVGA_WIDTH_PIXELS = 320;
@NotNull @NotNull
@ -46,7 +50,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
private CalculatorView calculatorView; private CalculatorView calculatorView;
@NotNull @NotNull
private BroadcastReceiver insertTextReceiver; private BroadcastReceiver textReceiver;
private volatile boolean initialized; private volatile boolean initialized;
@ -106,7 +110,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
calculatorView = new CalculatorView(this, CalculatorModel.instance); calculatorView = new CalculatorView(this, CalculatorModel.instance);
insertTextReceiver = new BroadcastReceiver() { textReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (INSERT_TEXT_INTENT.equals(intent.getAction())) { 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() { private synchronized void firstTimeInit() {
@ -140,7 +155,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
@Override @Override
protected void onDestroy() { protected void onDestroy() {
unregisterReceiver(insertTextReceiver); unregisterReceiver(textReceiver);
super.onDestroy(); super.onDestroy();
} }
@ -154,6 +169,11 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
this.calculatorView.evaluate(); this.calculatorView.evaluate();
} }
@SuppressWarnings({"UnusedDeclaration"})
public void historyButtonClickHandler(@NotNull View v) {
this.showHistory();
}
@SuppressWarnings({"UnusedDeclaration"}) @SuppressWarnings({"UnusedDeclaration"})
public void eraseButtonClickHandler(@NotNull View v) { public void eraseButtonClickHandler(@NotNull View v) {
calculatorView.doTextOperation(new CalculatorView.TextOperation() { calculatorView.doTextOperation(new CalculatorView.TextOperation() {
@ -267,6 +287,10 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
showSettings(); showSettings();
result = true; result = true;
break; break;
case R.id.main_menu_item_history:
showHistory();
result = true;
break;
case R.id.main_menu_item_about: case R.id.main_menu_item_about:
showAbout(); showAbout();
result = true; result = true;
@ -282,6 +306,10 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
return result; return result;
} }
private void showHistory() {
startActivity(new Intent(this, CalculatorHistoryActivity.class));
}
private void showSettings() { private void showSettings() {
startActivity(new Intent(this, CalculatorPreferencesActivity.class)); startActivity(new Intent(this, CalculatorPreferencesActivity.class));
} }

View File

@ -42,6 +42,7 @@ public class CalculatorDisplayHistoryState {
this.valid = valid; this.valid = valid;
} }
@NotNull
public EditorHistoryState getEditorHistoryState() { public EditorHistoryState getEditorHistoryState() {
return editorHistoryState; return editorHistoryState;
} }

View File

@ -12,6 +12,8 @@ import org.solovyev.common.utils.history.HistoryAction;
import org.solovyev.common.utils.history.HistoryHelper; import org.solovyev.common.utils.history.HistoryHelper;
import org.solovyev.common.utils.history.SimpleHistoryHelper; import org.solovyev.common.utils.history.SimpleHistoryHelper;
import java.util.List;
/** /**
* User: serso * User: serso
* Date: 10/9/11 * Date: 10/9/11
@ -68,5 +70,9 @@ public enum CalculatorHistory implements HistoryHelper<CalculatorHistoryState> {
historyHelper.addState(currentState); historyHelper.addState(currentState);
} }
@NotNull
@Override
public List<CalculatorHistoryState> getStates() {
return historyHelper.getStates();
}
} }

View File

@ -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<CalculatorHistoryState> 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<CalculatorHistoryState> getHistoryList() {
final List<CalculatorHistoryState> calculatorHistoryStates = new ArrayList<CalculatorHistoryState>(CalculatorHistory.instance.getStates());
final FilterRulesChain<CalculatorHistoryState> filterRulesChain = new FilterRulesChain<CalculatorHistoryState>();
filterRulesChain.addFilterRule(new FilterRule<CalculatorHistoryState>() {
@Override
public boolean isFiltered(CalculatorHistoryState object) {
return object == null || StringUtils.isEmpty(object.getEditorState().getText());
}
});
new Filter<CalculatorHistoryState>(filterRulesChain).filter(calculatorHistoryStates.iterator());
return calculatorHistoryStates;
}
private static class HistoryArrayAdapter extends ArrayAdapter<CalculatorHistoryState> {
private HistoryArrayAdapter(Context context, int resource, int textViewResourceId, @NotNull List<CalculatorHistoryState> 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;
}
}
}

View File

@ -12,7 +12,7 @@ import org.jetbrains.annotations.NotNull;
* Date: 9/11/11 * Date: 9/11/11
* Time: 12:16 AM * Time: 12:16 AM
*/ */
public class CalculatorHistoryState { public class CalculatorHistoryState extends AbstractHistoryState{
@NotNull @NotNull
private EditorHistoryState editorState; private EditorHistoryState editorState;
@ -20,7 +20,8 @@ public class CalculatorHistoryState {
@NotNull @NotNull
private CalculatorDisplayHistoryState displayState; private CalculatorDisplayHistoryState displayState;
public CalculatorHistoryState(@NotNull EditorHistoryState editorState, @NotNull CalculatorDisplayHistoryState displayState) { public CalculatorHistoryState(@NotNull EditorHistoryState editorState,
@NotNull CalculatorDisplayHistoryState displayState) {
this.editorState = editorState; this.editorState = editorState;
this.displayState = displayState; this.displayState = displayState;
} }

View File

@ -167,7 +167,7 @@ public class ColorButton extends Button {
if (topDr != null) { if (topDr != null) {
canvas.save(); canvas.save();
canvas.translate(scrollX + compoundPaddingLeft + (hspace - topDr.getBounds().width()) / 2, canvas.translate(scrollX + compoundPaddingLeft + (hspace - topDr.getBounds().width()) / 2,
scrollY + getPaddingTop() + vspace / 2); scrollY + getPaddingTop() + vspace / 2);
topDr.draw(canvas); topDr.draw(canvas);
canvas.restore(); canvas.restore();
} }