OSDN Git Service

T28109
[everclip/source.git] / workspace / EverClip / src / com / yuji / ec / NoteUpdatorActivity.java
1 package com.yuji.ec;
2
3 import android.content.Context;
4 import android.content.Intent;
5 import android.os.Bundle;
6 import android.view.View;
7 import android.view.View.OnClickListener;
8 import android.widget.AdapterView;
9 import android.widget.AdapterView.OnItemSelectedListener;
10 import android.widget.ArrayAdapter;
11 import android.widget.Button;
12 import android.widget.LinearLayout;
13 import android.widget.Spinner;
14 import android.widget.TextView;
15 import android.widget.Toast;
16
17 import com.yuji.ec.common.CommonUtil;
18 import com.yuji.ec.common.Constant;
19 import com.yuji.ec.db.NoteDao;
20 import com.yuji.ec.task.NoteUpdateTask;
21 import com.yuji.ec.utility.AsyncTaskCommand;
22 import com.yuji.ec.utility.BaseActivity;
23 import com.yuji.ec.utility.EditPrefUtil;
24 import com.yuji.ec.utility.EvernoteUtil;
25
26 public class NoteUpdatorActivity extends BaseActivity {
27         private EditPrefUtil pref = new EditPrefUtil(this);
28
29         private TextView updatorMessage = null;
30         private LinearLayout noteLayout = null;
31         private Spinner noteTypeSpinner = null;
32         private LinearLayout noteTypeLayout = null;
33         private TextView noteTypeView = null;
34         private Button noteTypeButton = null;
35         private Button updateButton = null;
36         private Button closeButton = null;
37
38         private String guid;
39         private boolean isEmpty = true;
40
41         @Override
42         public void onCreate(Bundle savedInstanceState) {
43                 super.onCreate(savedInstanceState);
44                 setContentView(R.layout.note_updator);
45
46                 updatorMessage = (TextView) this.findViewById(R.id.updatorMessage);
47                 noteLayout = (LinearLayout) this.findViewById(R.id.updatorLayout);
48                 noteTypeSpinner = (Spinner) this.findViewById(R.id.updatorNoteSpinner);
49                 noteTypeLayout = (LinearLayout) this
50                                 .findViewById(R.id.updatorNoteLayout);
51                 noteTypeView = (TextView) this.findViewById(R.id.updatorNoteText);
52                 noteTypeButton = (Button) this.findViewById(R.id.updatorNoteButton);
53
54                 updateButton = (Button) this.findViewById(R.id.updatorUpdateButton);
55                 closeButton = (Button) this.findViewById(R.id.updatorCloseButton);
56
57                 noteTypeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
58                         public void onItemSelected(AdapterView<?> parent, View view,
59                                         int position, long id) {
60                                 noteTypeSpinnerItemSelected(parent, view, position, id);
61                         }
62
63                         public void onNothingSelected(AdapterView<?> view) {
64                                 ;
65                         }
66                 });
67                 noteTypeButton.setOnClickListener(new OnClickListener() {
68                         public void onClick(View view) {
69                                 noteTypeButtonOnClick();
70                         }
71                 });
72                 updateButton.setOnClickListener(new OnClickListener() {
73                         public void onClick(View view) {
74                                 updateButtonOnClick();
75                         }
76                 });
77                 closeButton.setOnClickListener(new OnClickListener() {
78                         public void onClick(View view) {
79                                 closeButtonOnClick();
80                         }
81                 });
82
83                 String msg = getString(R.string.updatorMessage);
84                 msg = CommonUtil.replaceString(msg, "\\\\n", "\n");
85                 updatorMessage.setText(msg);
86                 
87                 ArrayAdapter<CharSequence> noteTypeAdapter = ArrayAdapter
88                                 .createFromResource(this, R.array.note_item_array,
89                                                 android.R.layout.simple_spinner_item);
90                 noteTypeAdapter
91                                 .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
92                 noteTypeSpinner.setAdapter(noteTypeAdapter);
93         }
94
95         @Override
96         public void onStart() {
97                 super.onStart();
98
99                 int noteItemType = pref.getInt(Constant.PREF_NOTE_ITEM_TYPE_TMP,
100                                 Constant.NOTE_ITEM_TYPE_ALL);
101                 noteTypeSpinner.setSelection(noteItemType);
102
103                 guid = pref.get(Constant.PREF_NOTE_ITEM_GUID_TMP);
104
105                 String title = pref.get(Constant.PREF_NOTE_ITEM_TEXT);
106                 noteTypeView.setText(title);
107         }
108
109         @Override
110         public void onPause() {
111                 int noteItemType = noteTypeSpinner.getSelectedItemPosition();
112                 pref.put(Constant.PREF_NOTE_ITEM_TYPE_TMP, noteItemType);
113
114                 pref.update();
115
116                 super.onPause();
117         }
118
119         @Override
120         protected void onActivityResult(int requestCode, int resultCode, Intent data) {
121                 if (resultCode == RESULT_OK) {
122                         Bundle ex = data.getExtras();
123                         String guid = ex.getString(Constant.EXTRA_GUID);
124                         String title = ex.getString(Constant.EXTRA_TITLE);
125                         int noteItemType = noteTypeSpinner.getSelectedItemPosition();
126
127                         this.guid = guid;
128                         pref.put(Constant.PREF_NOTE_ITEM_GUID_TMP, guid);
129                         pref.put(Constant.PREF_NOTE_ITEM_TEXT, title);
130                         pref.put(Constant.PREF_NOTE_ITEM_GUID_TYPE_TMP, noteItemType);
131                         pref.update();
132
133                         noteTypeView.setText(title);
134                 }
135
136                 setStatus();
137         }
138
139         private void noteTypeSpinnerItemSelected(AdapterView<?> parent, View view,
140                         int position, long id) {
141                 setStatus();
142         }
143
144         private void noteTypeButtonOnClick() {
145                 Intent intent = new Intent(this, (Class<?>) NoteTypeListActivity.class);
146                 startActivityForResult(intent, 0);
147         }
148
149         private void updateButtonOnClick() {
150                 int noteItemType = noteTypeSpinner.getSelectedItemPosition();
151
152                 NoteUpdateTask task = new NoteUpdateTask(this, noteItemType, guid);
153                 AsyncTaskCommand command = new AsyncTaskCommand(this, task);
154                 command.setTitle(getString(R.string.updatorNoteDialogTitle));
155                 command.setButtonTitle(getString(R.string.cancelButton));
156                 command.setCancelable(false);
157                 command.execute("");
158         }
159
160         private void closeButtonOnClick() {
161                 finish();
162         }
163
164         public void done(EvernoteUtil.Error errorCode) {
165                 if (errorCode != null && !errorCode.equals(EvernoteUtil.Error.NONE)) {
166                         EvernoteUtil util = EvernoteUtil.getInstance();
167                         Toast.makeText(this, util.getErrorMessage(this, errorCode),
168                                         Toast.LENGTH_LONG).show();
169
170                         closeButtonOnClick();
171                         return;
172                 }
173
174                 int noteItemType = noteTypeSpinner.getSelectedItemPosition();
175                 pref.put(Constant.PREF_NOTE_ITEM_TYPE, noteItemType);
176                 pref.copy(Constant.PREF_NOTE_ITEM_GUID_TMP,
177                                 Constant.PREF_NOTE_ITEM_GUID);
178                 if (noteItemType == Constant.NOTE_ITEM_TYPE_ALL){
179                         pref.remove(Constant.PREF_NOTE_ITEM_GUID);
180                         pref.remove(Constant.PREF_NOTE_ITEM_TEXT);
181                 }
182                 pref.update();
183                 
184                 closeButtonOnClick();
185         }
186
187         private void setStatus() {
188                 /*
189                  * \81\9b\91Î\8fÛ type guid guidType(guid\82Ì\90Ý\92è\83^\83C\83~\83\93\83O\82Å\90Ý\92è\81A\8f\89\8aú\92lnull)
190                  * 
191                  * \81\9b\8dX\90V\83{\83^\83\93\82Ì\8a\88\90«\83\8b\81[\83\8b(\8a\88\90«\82Ì\8fð\8c\8f) type1=type2 guid1<>guid2 guidType=type2
192                  * 
193                  * type1<>type2 guidType=type2
194                  */
195                 int srcNoteItemType = pref.getInt(Constant.PREF_NOTE_ITEM_TYPE,
196                                 Constant.NOTE_ITEM_TYPE_ALL);
197                 String srcGuid = pref.get(Constant.PREF_NOTE_ITEM_GUID);
198
199                 int noteItemType = noteTypeSpinner.getSelectedItemPosition();
200                 int guidType = pref.getInt(Constant.PREF_NOTE_ITEM_GUID_TYPE_TMP, -1);
201
202                 boolean enabled = false;
203                 if (noteItemType == Constant.NOTE_ITEM_TYPE_ALL) {
204                         enabled = true;
205 //              } else if (srcNoteItemType == noteItemType) {
206 //                      if (!CommonUtil.isNull(guid) && !guid.equals(srcGuid)) {
207 //                              if (guidType >= 0 && guidType == noteItemType) {
208 //                                      enabled = true;
209 //                              }
210 //                      }
211 //              } else {
212 //                      if (guidType >= 0 && guidType == noteItemType) {
213 //                              enabled = true;
214 //                      }
215 //              }
216                 }
217                 else if (guidType >= 0 && guidType == noteItemType) {
218                         enabled = true;
219                 }
220
221                 updateButton.setEnabled(enabled);
222                 noteTypeLayout
223                                 .setVisibility((noteItemType == Constant.NOTE_ITEM_TYPE_ALL) ? View.GONE
224                                                 : View.VISIBLE);
225
226                 NoteDao dao = (NoteDao) NoteDao.getInstance();
227                 isEmpty = dao.isEmpty();
228                 if (isEmpty) {
229                         noteLayout.setVisibility(View.VISIBLE);
230                 } else {
231                         noteLayout.setVisibility(View.GONE);
232                 }
233         }
234
235         public static void init(Context context) {
236                 EditPrefUtil pref = new EditPrefUtil(context);
237
238                 pref.copy(Constant.PREF_NOTE_ITEM_TYPE,
239                                 Constant.PREF_NOTE_ITEM_TYPE_TMP);
240                 pref.copy(Constant.PREF_NOTE_ITEM_GUID,
241                                 Constant.PREF_NOTE_ITEM_GUID_TMP);
242                 pref.copy(Constant.PREF_NOTE_ITEM_TYPE,
243                                 Constant.PREF_NOTE_ITEM_GUID_TYPE_TMP);
244
245                 pref.update();
246         }
247 }