1 package net.kazhik.android.tokyorunners;
3 import java.text.ParseException;
4 import java.text.SimpleDateFormat;
5 import java.util.ArrayList;
7 import java.util.HashMap;
9 import android.app.ListActivity;
10 import android.content.Intent;
11 import android.database.Cursor;
12 import android.os.Bundle;
13 import android.util.Log;
14 import android.view.ContextMenu;
15 import android.view.GestureDetector;
16 import android.view.Menu;
17 import android.view.MenuItem;
18 import android.view.MotionEvent;
19 import android.view.View;
20 import android.view.ContextMenu.ContextMenuInfo;
21 import android.view.View.OnCreateContextMenuListener;
22 import android.widget.AdapterView;
23 import android.widget.ArrayAdapter;
24 import android.widget.AdapterView.AdapterContextMenuInfo;
25 import android.widget.AdapterView.OnItemClickListener;
27 public class RunningHistory extends ListActivity implements OnItemClickListener {
28 private SimpleDateFormat m_startTimeFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
29 private HashMap<Long, Long> m_startTimeMap = new HashMap<Long, Long>();
30 private GestureDetector m_gestureDetector;
32 private static final int MENU_DELETE_THIS = Menu.FIRST + 200;
33 private static final int MENU_DELETE_ALL = Menu.FIRST + 201;
35 private class GestureListener extends GestureDetector.SimpleOnGestureListener {
36 public void onLongPress(MotionEvent arg0) {
37 Log.d(this.getClass().getName(), "onLongPress():" );
42 public boolean onDoubleTap(MotionEvent arg0) {
43 Log.d(this.getClass().getName(), "onDoubleTap():" );
52 public void onCreate(Bundle savedInstanceState) {
53 super.onCreate(savedInstanceState);
55 m_gestureDetector = new GestureDetector(new GestureListener());
57 setContentView(R.layout.history);
60 RunningRecordProvider.START_TIME,
61 RunningRecordProvider.DISTANCE};
62 String sortOrder = RunningRecordProvider.CURRENT_TIME + " desc";
63 Cursor cursor = managedQuery(RunningRecordProvider.REC_URI, columns, null, null, sortOrder);
68 ArrayList<String> history = new ArrayList<String>();
71 long prevStartTime = 0;
72 while (cursor.moveToNext()) {
73 startTime = cursor.getLong(0);
74 distance = (float)cursor.getLong(1);
75 if (startTime == prevStartTime) {
76 // I need "select distinct"!
79 m_startTimeMap.put(startTime / 1000 * 1000, startTime);
81 prevStartTime = startTime;
82 StringBuffer rowStr = new StringBuffer();
83 rowStr.append(m_startTimeFormatter.format(new Date(startTime)));
86 rowStr.append(distance / 1000);
87 history.add(rowStr.toString());
89 ArrayAdapter<String> historyArray = new ArrayAdapter<String>(this, R.layout.runningrecord, history);
91 setListAdapter(historyArray);
93 getListView().setOnItemClickListener(this);
95 getListView().setOnCreateContextMenuListener(this);
98 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
99 Log.d(this.getClass().getName(),
100 "onItemClick(): position: " + position);
101 ArrayAdapter<String> historyArray = (ArrayAdapter<String>)getListAdapter();
103 Log.d(this.getClass().getName(),
104 "onItemClick(): " + historyArray.getItem(position));
106 Intent returnData = new Intent();
108 String[] splitStr = historyArray.getItem(position).split(" ");
109 String dateStr = splitStr[0] + " " + splitStr[1];
110 Date startTime = m_startTimeFormatter.parse(dateStr);
112 returnData.setAction(Intent.ACTION_VIEW);
113 returnData.putExtra("startTime", m_startTimeMap.get(startTime.getTime()));
115 setResult(RESULT_OK, returnData);
116 } catch (ParseException e) {
123 public boolean onTouchEvent(MotionEvent event) {
124 Log.d(this.getClass().getName(), "onTouchEvent():" + event.getAction() );
125 if (m_gestureDetector.onTouchEvent(event)) {
128 return super.onTouchEvent(event);
131 public void onCreateContextMenu(ContextMenu menu, View v,
132 ContextMenuInfo menuInfo) {
133 super.onCreateContextMenu(menu, v, menuInfo);
134 menu.add(0, MENU_DELETE_THIS, 0, R.string.contextmenu_deletethis);
135 menu.add(0, MENU_DELETE_ALL, 0, R.string.contextmenu_deleteall);
138 public boolean onContextItemSelected(MenuItem item) {
139 AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
140 ArrayAdapter<String> historyArray = (ArrayAdapter<String>)getListAdapter();
141 int position = info.position;
142 switch (item.getItemId()) {
143 case MENU_DELETE_THIS:
145 String[] splitStr = historyArray.getItem(position).split(" ");
146 String dateStr = splitStr[0] + " " + splitStr[1];
147 Date startTime = m_startTimeFormatter.parse(dateStr);
149 String selection = RunningRecordProvider.START_TIME + " = ?";
150 String[] selectionArgs = {Long.toString(startTime.getTime())};
152 getContentResolver().delete(RunningRecordProvider.REC_URI, selection, selectionArgs);
154 historyArray.remove(historyArray.getItem(position));
155 } catch (ParseException e) {
159 case MENU_DELETE_ALL:
160 getContentResolver().delete(RunningRecordProvider.REC_URI, null, null);
161 historyArray.clear();
164 return super.onContextItemSelected(item);