OSDN Git Service

04e4a684d4126c4616deadd9a10e1ea37c2072ff
[evermemo/source.git] / workspace / EverMemo / src / com / yuji / em / NoteListActivity.java
1 package com.yuji.em;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import android.content.Intent;
7 import android.os.Bundle;
8 import android.text.format.DateUtils;
9 import android.view.View;
10 import android.view.View.OnClickListener;
11 import android.widget.AdapterView;
12 import android.widget.AdapterView.OnItemClickListener;
13 import android.widget.Button;
14 import android.widget.ListView;
15
16 import com.evernote.edam.notestore.NoteList;
17 import com.yuji.em.common.Constant;
18 import com.yuji.em.data.Note;
19 import com.yuji.em.data.NoteDao;
20 import com.yuji.em.task.NoteListTask;
21 import com.yuji.em.utility.AsyncTaskCommand;
22 import com.yuji.em.utility.BaseActivity;
23
24 public class NoteListActivity extends BaseActivity {
25         private static List<NoteListItem> noteList = null;
26
27         private ListView listView = null;
28         private Button closeButton = null;
29         private int index = -1;
30
31         @Override
32         public void onCreate(Bundle savedInstanceState) {
33                 super.onCreate(savedInstanceState);
34                 setContentView(R.layout.note_list);
35
36                 try {
37                         listView = (ListView) this.findViewById(R.id.noteListView);
38                         closeButton = (Button) this.findViewById(R.id.noteListCloseButton);
39
40                         listView.setOnItemClickListener(new OnItemClickListener() {
41                                 public void onItemClick(AdapterView<?> parent, View view,
42                                                 int position, long id) {
43                                         listViewOnItemClickListener(parent, view, position, id);
44                                 }
45                         });
46
47                         closeButton.setOnClickListener(new OnClickListener() {
48                                 public void onClick(View v) {
49                                         closeButtonOnClick();
50                                 }
51                         });
52
53                         Bundle extras = getIntent().getExtras();
54                         index = extras.getInt(Constant.EXTRA_INDEX);
55
56                         if (noteList != null) {
57                                 setNoteList(noteList);
58                         } else {
59                                 NoteListTask task = new NoteListTask(this);
60                                 AsyncTaskCommand command = new AsyncTaskCommand(this, task);
61                                 command.setTitle(getString(R.string.noteListGetting));
62                                 command.setButtonTitle(getString(R.string.cancelButton));
63                                 command.execute("");
64                         }
65                 } catch (Exception e) {
66                         // TODO
67                         e.printStackTrace();
68                 }
69         }
70
71         private void closeButtonOnClick() {
72                 Intent intent = new Intent();
73                 setResult(RESULT_CANCELED, intent);
74                 finish();
75         }
76
77         private void listViewOnItemClickListener(AdapterView<?> parent, View view,
78                         int position, long id) {
79                 ListView listView = (ListView) parent;
80                 NoteListItem item = (NoteListItem) listView.getItemAtPosition(position);
81
82                 Note note = item.getNote();
83                 if (note == null) {
84                         return;
85                 }
86
87                 NoteDao dao = NoteDao.getInstance();
88                 int n = dao.search(this, note);
89                 if (n >= 0) {
90                         // #27613
91                         Intent intent = new Intent();
92                         setResult(RESULT_CANCELED, intent);
93                         finish();
94                         return;
95                 }
96                 dao.update(this, note, index);
97
98                 Intent intent = new Intent();
99                 setResult(RESULT_OK, intent);
100                 finish();
101         }
102
103         public void done(NoteList nList) {
104                 if (nList == null) {
105                         // \83L\83\83\83\93\83Z\83\8b\83{\83^\83\93\89\9f\89º\8e\9e
106                         closeButtonOnClick();
107                         return;
108                 }
109
110                 List<NoteListItem> list = getNoteList(nList);
111                 if (list == null) {
112                         closeButtonOnClick();
113                         return;
114                 }
115                 setNoteList(list);
116                 noteList = list;
117         }
118
119         private void setNoteList(List<NoteListItem> list) {
120                 NoteListViewArrayAdapter adapter = new NoteListViewArrayAdapter(this,
121                                 R.layout.simple_list_item_1, list);
122                 listView.setAdapter(adapter);
123         }
124
125         private List<NoteListItem> getNoteList(NoteList nList) {
126                 List<NoteListItem> list = new ArrayList<NoteListItem>();
127
128                 // TODO
129                 List<com.evernote.edam.type.Note> notes = nList.getNotes();
130                 if (notes == null) {
131                         return null;
132                 }
133
134                 String oldTitle = "";
135                 NoteListItem item;
136                 for (com.evernote.edam.type.Note note : notes) {
137                         long l = note.getUpdated();
138                         // long l = note.getCreated(); // TODO \83\\81[\83g\8f\87\82Í\81A\8dX\90V\93ú?
139                         String title = DateUtils.formatDateTime(this, l,
140                                         DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE);
141                         if (!title.equals(oldTitle)) {
142                                 item = new NoteListItem(title, null);
143                                 list.add(item);
144                                 oldTitle = title;
145                         }
146
147                         Note n = new Note(note.getGuid(), note.getTitle());
148                         item = new NoteListItem(null, n);
149                         list.add(item);
150                 }
151                 return list;
152         }
153         
154         public static void init(){
155                 noteList = null;
156         }
157 }