OSDN Git Service

T28079
[everclip/source.git] / workspace / EverClip / src / com / yuji / ec / SettingActivity.java
1 package com.yuji.ec;
2
3 import java.util.Calendar;
4
5 import android.app.TimePickerDialog;
6 import android.content.Context;
7 import android.content.Intent;
8 import android.os.Bundle;
9 import android.view.KeyEvent;
10 import android.view.View;
11 import android.view.View.OnClickListener;
12 import android.widget.AdapterView;
13 import android.widget.AdapterView.OnItemSelectedListener;
14 import android.widget.ArrayAdapter;
15 import android.widget.Button;
16 import android.widget.CheckBox;
17 import android.widget.CompoundButton;
18 import android.widget.CompoundButton.OnCheckedChangeListener;
19 import android.widget.LinearLayout;
20 import android.widget.RadioButton;
21 import android.widget.RadioGroup;
22 import android.widget.Spinner;
23 import android.widget.TextView;
24 import android.widget.TimePicker;
25
26 import com.yuji.ec.common.Constant;
27 import com.yuji.ec.utility.BaseActivity;
28 import com.yuji.ec.utility.DateUtil;
29 import com.yuji.ec.utility.EditPrefUtil;
30
31 public class SettingActivity extends BaseActivity {
32         private EditPrefUtil pref = new EditPrefUtil(this);
33
34         private LinearLayout messageLayout = null;
35         private TextView messageView = null;
36         private Button userButton = null;
37 //      private LinearLayout noteTypeLayout = null;
38 //      private Spinner noteTypeSpinner = null;
39 //      private TextView noteTypeView = null;
40 //      private Button noteTypeButton = null;
41 //      private Button updateButton = null;
42         private Button noteButton = null;
43         private Spinner sortSpinner = null;
44         private RadioGroup sortRadioGroup = null;
45         private RadioButton sortRadio1 = null;
46         private RadioButton sortRadio2 = null;
47         private CheckBox timeCheckBox = null;
48         private LinearLayout timeLayout = null;
49         private TextView timeView = null;
50         private Button timeButton = null;
51         private Button closeButton = null;
52
53         private String guid;
54
55         private long updateTime;
56
57         @Override
58         public void onCreate(Bundle savedInstanceState) {
59                 super.onCreate(savedInstanceState);
60                 setContentView(R.layout.setting);
61
62                 messageLayout = (LinearLayout) this.findViewById(R.id.settingLayout);
63                 messageView = (TextView) this.findViewById(R.id.settingMessage);
64                 userButton = (Button) this.findViewById(R.id.settingUserButton);
65 //              noteTypeSpinner = (Spinner) this.findViewById(R.id.settingNoteSpinner);
66 //              noteTypeLayout = (LinearLayout) this
67 //                              .findViewById(R.id.settingNoteLayout);
68 //              noteTypeView = (TextView) this.findViewById(R.id.settingNoteText);
69 //              noteTypeButton = (Button) this.findViewById(R.id.settingNoteButton);
70 //              updateButton = (Button) this.findViewById(R.id.settingUpdateButton);
71                 noteButton = (Button) this.findViewById(R.id.settingNoteButton);
72                 sortSpinner = (Spinner) this.findViewById(R.id.settingSortSpinner);
73                 sortRadioGroup = (RadioGroup) this
74                                 .findViewById(R.id.settingSortRadioGroup);
75                 sortRadio1 = (RadioButton) this.findViewById(R.id.settingSortRadio1);
76                 sortRadio2 = (RadioButton) this.findViewById(R.id.settingSortRadio2);
77                 timeCheckBox = (CheckBox) this.findViewById(R.id.settingTimeCheckBox);
78                 timeLayout = (LinearLayout) this.findViewById(R.id.settingTimeLayout);
79                 timeView = (TextView) this.findViewById(R.id.settingTimeText);
80                 timeButton = (Button) this.findViewById(R.id.settingTimeButton);
81
82                 closeButton = (Button) this.findViewById(R.id.settingCloseButton);
83
84                 userButton.setOnClickListener(new OnClickListener() {
85                         public void onClick(View view) {
86                                 userButtonOnClick();
87                         }
88                 });
89 //              noteTypeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
90 //                      public void onItemSelected(AdapterView<?> parent, View view,
91 //                                      int position, long id) {
92 //                              noteTypeSpinnerItemSelected(parent, view, position, id);
93 //                      }
94 //
95 //                      public void onNothingSelected(AdapterView<?> view) {
96 //                              ;
97 //                      }
98 //              });
99 //              noteTypeButton.setOnClickListener(new OnClickListener() {
100 //                      public void onClick(View view) {
101 //                              noteTypeButtonOnClick();
102 //                      }
103 //              });
104 //              updateButton.setOnClickListener(new OnClickListener() {
105 //                      public void onClick(View view) {
106 //                              updateButtonOnClick();
107 //                      }
108 //              });
109
110                 noteButton.setOnClickListener(new OnClickListener() {
111                 public void onClick(View view) {
112                         noteButtonOnClick();
113                 }
114         });
115
116                 sortSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
117                         public void onItemSelected(AdapterView<?> parent, View view,
118                                         int position, long id) {
119                                 sortSpinnerItemSelected(parent, view, position, id);
120                         }
121
122                         public void onNothingSelected(AdapterView<?> view) {
123                                 ;
124                         }
125                 });
126                 sortRadio1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
127                         public void onCheckedChanged(CompoundButton buttonView,
128                                         boolean isChecked) {
129                                 sortCheckedChanged(buttonView, isChecked, 1);
130                         }
131                 });
132                 sortRadio2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
133                         public void onCheckedChanged(CompoundButton buttonView,
134                                         boolean isChecked) {
135                                 sortCheckedChanged(buttonView, isChecked, 2);
136                         }
137                 });
138                 timeCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
139                         public void onCheckedChanged(CompoundButton buttonView,
140                                         boolean isChecked) {
141                                 timeCheckBoxOnClick(buttonView, isChecked);
142                         }
143                 });
144                 timeButton.setOnClickListener(new OnClickListener() {
145                         public void onClick(View view) {
146                                 timeButtonOnClick();
147                         }
148                 });
149                 closeButton.setOnClickListener(new OnClickListener() {
150                         public void onClick(View view) {
151                                 closeButtonOnClick();
152                         }
153                 });
154
155 //              ArrayAdapter<CharSequence> noteTypeAdapter = ArrayAdapter
156 //                              .createFromResource(this, R.array.note_item_array,
157 //                                              android.R.layout.simple_spinner_item);
158 //              noteTypeAdapter
159 //                              .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
160 //              noteTypeSpinner.setAdapter(noteTypeAdapter);
161
162                 ArrayAdapter<CharSequence> noteOrderAdapter = ArrayAdapter
163                                 .createFromResource(this, R.array.note_order_array,
164                                                 android.R.layout.simple_spinner_item);
165                 noteOrderAdapter
166                                 .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
167                 sortSpinner.setAdapter(noteOrderAdapter);
168
169                 setStatus();
170         }
171
172         @Override
173         public void onStart() {
174                 super.onStart();
175
176 //              int noteItemType = pref.getInt(Constant.PREF_NOTE_ITEM_TYPE,
177 //                              Constant.NOTE_ITEM_TYPE_ALL);
178 //              noteTypeSpinner.setSelection(noteItemType);
179 //
180 //              guid = pref.get(Constant.PREF_NOTE_ITEM_GUID);
181 //              String title = pref.get(Constant.PREF_NOTE_ITEM_TEXT);
182 //              noteTypeView.setText(title);
183
184                 int noteOrder = pref.getInt(Constant.PREF_NOTE_ORDER,
185                                 Constant.NOTE_ORDER_CREATE);
186                 sortSpinner.setSelection(noteOrder);
187
188                 int orderBy = pref.getInt(Constant.PREF_NOTE_ORDER_BY,
189                                 Constant.NOTE_ORDER_BY_ASC);
190                 switch (orderBy) {
191                 case Constant.NOTE_ORDER_BY_ASC:
192                         sortRadioGroup.check(sortRadio1.getId());
193                         break;
194                 case Constant.NOTE_ORDER_BY_DES:
195                         sortRadioGroup.check(sortRadio2.getId());
196                         break;
197                 }
198
199                 int autoUpdate = pref.getInt(Constant.PREF_AUTO_UPDATE, Constant.OFF);
200                 timeCheckBox.setChecked(autoUpdate == Constant.ON);
201
202                 updateTime = pref.getLong(Constant.PREF_UPDATE_TIME, DateUtil.getTime(0, 0, 0));
203                 String str = DateUtil.toDateString(SettingActivity.this, updateTime);
204                 timeView.setText(str);
205 }
206
207         @Override
208         public void onPause() {
209 //              int noteItemType = noteTypeSpinner.getSelectedItemPosition();
210 //              pref.put(Constant.PREF_NOTE_ITEM_TYPE, noteItemType);
211
212                 int noteOrder = sortSpinner.getSelectedItemPosition();
213                 pref.put(Constant.PREF_NOTE_ORDER, noteOrder);
214
215                 int orderById = sortRadioGroup.getCheckedRadioButtonId();
216                 int orderBy = Constant.NOTE_ORDER_BY_ASC;
217                 if (orderById == sortRadio1.getId()) {
218                         orderBy = Constant.NOTE_ORDER_BY_ASC;
219                 } else if (orderById == sortRadio2.getId()) {
220                         orderBy = Constant.NOTE_ORDER_BY_DES;
221                 }
222                 pref.put(Constant.PREF_NOTE_ORDER_BY, orderBy);
223
224                 int autoUpdate = timeCheckBox.isChecked() ? Constant.ON : Constant.OFF;
225                 pref.put(Constant.PREF_AUTO_UPDATE, autoUpdate);
226
227                 pref.put(Constant.PREF_UPDATE_TIME, updateTime);
228                 
229                 pref.update();
230
231                 super.onPause();
232         }
233
234         @Override
235         public boolean onKeyDown(int keyCode, KeyEvent event) {
236                 if (keyCode != KeyEvent.KEYCODE_BACK) {
237                         return super.onKeyDown(keyCode, event);
238                 } else {
239                         closeButtonOnClick();
240                         return false;
241                 }
242         }
243
244         @Override
245         protected void onActivityResult(int requestCode, int resultCode, Intent data) {
246 //              if (resultCode == RESULT_OK) {
247 //                      Bundle ex = data.getExtras();
248 //                      String guid = ex.getString(Constant.EXTRA_GUID);
249 //                      String title = ex.getString(Constant.EXTRA_TITLE);
250 //
251 //                      pref.put(Constant.PREF_NOTE_ITEM_GUID, guid);
252 //                      pref.put(Constant.PREF_NOTE_ITEM_TEXT, title);
253 //                      pref.update();
254 //
255 //                      noteTypeView.setText(title);
256 //              }
257
258                 setStatus();
259         }
260
261         private void userButtonOnClick() {
262                 Intent intent = new Intent(this, (Class<?>) UserPasswordActivity.class);
263                 startActivityForResult(intent, 0);
264         }
265
266 //      private void noteTypeSpinnerItemSelected(AdapterView<?> parent, View view,
267 //                      int position, long id) {
268 //              setStatus();
269 //      }
270 //
271 //      private void noteTypeButtonOnClick() {
272 //              Intent intent = new Intent(this, (Class<?>) NoteTypeListActivity.class);
273 //              startActivityForResult(intent, 1); // TODO
274 //      }
275 //
276 //      private void updateButtonOnClick() {
277 //              NoteUpdateTask task = new NoteUpdateTask(this);
278 //              AsyncTaskCommand command = new AsyncTaskCommand(this, task);
279 //              // command.setTitle(getString(R.string.noteListGetting)); // TODO
280 //              command.setTitle("Title");
281 //              command.setButtonTitle(getString(R.string.cancelButton));
282 //              command.execute("");
283 //      }
284
285         private void noteButtonOnClick(){
286 //              pref.copy(Constant.PREF_NOTE_ITEM_TYPE, Constant.PREF_NOTE_ITEM_TYPE_TMP);
287 //              pref.copy(Constant.PREF_NOTE_ITEM_GUID, Constant.PREF_NOTE_ITEM_GUID_TMP);
288 //              pref.remove(Constant.PREF_NOTE_ITEM_GUID_TYPE_TMP);
289 //              pref.update();
290                 
291                 NoteUpdatorActivity.init(this);
292                 Intent intent = new Intent(this, (Class<?>) NoteUpdatorActivity.class);
293                 startActivityForResult(intent, 1); // TODO              
294         }
295         
296         private void sortSpinnerItemSelected(AdapterView<?> parent, View view,
297                         int position, long id) {
298
299         }
300
301         private void sortCheckedChanged(CompoundButton buttonView,
302                         boolean isChecked, int i) {
303
304         }
305
306         // private void noteButtonOnClick() {
307         // NoteListActivity.init();
308         // SelectNoteListActivity.init(this);
309         //
310         // Intent intent = new Intent(this,
311         // (Class<?>) SelectNoteListActivity.class);
312         // startActivityForResult(intent, 0);
313         // }
314         //
315         // private void headerButtonOnClick() {
316         // Intent intent = new Intent(this, (Class<?>) HeaderActivity.class);
317         // startActivityForResult(intent, 0);
318         // }
319
320 //      public void done(EvernoteUtil.Error errorCode) {
321 //              // if (errorCode != null && !errorCode.equals(Error.NONE)) {
322 //              // EvernoteUtil util = EvernoteUtil.getInstance();
323 //              // Toast.makeText(this, util.getErrorMessage(this, errorCode),
324 //              // Toast.LENGTH_LONG).show();
325 //              // }
326 //              //
327 //              // if (nList == null) {
328 //              // // \83L\83\83\83\93\83Z\83\8b\83{\83^\83\93\89\9f\89ยบ\8e\9e
329 //              // closeButtonOnClick();
330 //              // return;
331 //              // }
332 //              //
333 //              // List<NoteListItem> list = getNoteList(nList);
334 //              // if (list == null) {
335 //              // closeButtonOnClick();
336 //              // return;
337 //              // }
338 //              // setNoteList(list);
339 //              // noteList = list;
340 //      }
341
342         protected void timeCheckBoxOnClick(CompoundButton buttonView,
343                         boolean isChecked) {
344                 if (isChecked){
345                         
346                 }
347                 else {
348                         pref.put(Constant.PREF_UPDATE_TIME_TMP, -1);                            
349                         pref.update();                  
350                 }
351                 
352                 setStatus();
353         }
354
355         private void timeButtonOnClick() {
356         Calendar calendar = Calendar.getInstance();
357         int hour = calendar.get(Calendar.HOUR_OF_DAY);
358         int minute = calendar.get(Calendar.MINUTE);
359
360         TimePickerDialog timePickerDialog = new TimePickerDialog(this,
361             new TimePickerDialog.OnTimeSetListener() {
362                                 public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
363                                         int second = 0;
364                                         
365                                         long t = DateUtil.getTime(hourOfDay, minute, second);
366                                         String str = DateUtil.toDateString(SettingActivity.this, t);
367                                         
368                                         updateTime = t;
369                                         timeView.setText(str);
370
371                                         pref.put(Constant.PREF_UPDATE_TIME_TMP, t);                             
372                                         pref.update();                  
373                                 }
374             }, hour, minute, true);
375         timePickerDialog.show();
376         }
377
378         private void closeButtonOnClick() {
379                 Intent intent = new Intent();
380                 setResult(RESULT_OK, intent);
381                 finish();
382
383                 // TODO
384                 // EverMemoActivity.init(false);
385         }
386
387         private void setStatus() {
388                 timeLayout.setVisibility(timeCheckBox.isChecked() ? View.VISIBLE
389                                 : View.GONE);
390
391                 // TODO
392                 // String passwd = PasswordUtil.getPassword(this);
393                 // if (CommonUtil.isNull(passwd)) {
394                 // messageLayout.setVisibility(View.VISIBLE);
395                 // messageView.setText(getString(R.string.settingConfirmUserMsg));
396                 // noteButton.setEnabled(false);
397                 // headerButton.setEnabled(false);
398                 // return;
399                 // }
400                 //
401                 // NoteDao dao = NoteDao.getInstance();
402                 // if (dao.isEmpty(this)) {
403                 // messageLayout.setVisibility(View.VISIBLE);
404                 // messageView.setText(getString(R.string.settingConfirmNoteMsg));
405                 // noteButton.setEnabled(true);
406                 // headerButton.setEnabled(false);
407                 // return;
408                 // }
409
410 //              int noteType = noteTypeSpinner.getSelectedItemPosition();
411 //              noteTypeLayout
412 //                              .setVisibility((noteType == Constant.NOTE_ITEM_TYPE_ALL) ? View.GONE
413 //                                              : View.VISIBLE);
414
415                 messageLayout.setVisibility(View.GONE);
416                 // noteButton.setEnabled(true);
417                 // headerButton.setEnabled(true);
418         }
419         
420         public static void init(Context context){
421                 EditPrefUtil pref = new EditPrefUtil(context);
422
423                 pref.put(Constant.PREF_UPDATE_TIME_TMP, -1);                            
424                 pref.update();
425         }               
426 }