OSDN Git Service

初期化処理追加
[train-delayed/source.git] / workspace / TrainDelayed / src / com / td / TrainDelayedActivity.java
1 package com.td;
2
3 import java.util.Calendar;
4 import java.util.Iterator;
5 import java.util.List;
6 import java.util.Set;
7
8 import android.app.AlarmManager;
9 import android.app.AlertDialog;
10 import android.app.PendingIntent;
11 import android.content.BroadcastReceiver;
12 import android.content.Context;
13 import android.content.DialogInterface;
14 import android.content.Intent;
15 import android.content.IntentFilter;
16 import android.graphics.Color;
17 import android.os.Bundle;
18 import android.view.View;
19 import android.view.View.OnClickListener;
20 import android.widget.Button;
21 import android.widget.LinearLayout;
22 import android.widget.TextView;
23
24 import com.td.broadcast.DelayReceiver;
25 import com.td.db.Keyword;
26 import com.td.db.Monitor;
27 import com.td.db.MonitorDao;
28 import com.td.db.PrefectureDao;
29 import com.td.db.Train;
30 import com.td.db.TrainDao;
31 import com.td.service.SchedulerTest;
32 import com.td.service.TrainDelayedService;
33 import com.td.service.TrainDelayedServiceImpl;
34 import com.td.service.TrainService;
35 import com.td.service.TrainTwitterAdapterTest;
36 import com.td.utility.BaseActivity;
37 import com.td.utility.ConfUtil;
38 import com.td.utility.Debug;
39 import com.td.utility.EditPrefUtil;
40 import com.td.utility.StatusFlag;
41 import com.td.utility.StatusFlagFactory;
42 import com.td.utility.StatusFlagTest;
43 import com.td.utility.StringMap;
44 import com.td.wait.TrainServiceWaiting;
45
46 public class TrainDelayedActivity extends BaseActivity implements TrainDelayedService {
47         public static final String PREF_TRD_DELAY = "TRD_DELAY";
48         public static final String PREF_TRD_DELAY_MSG = "TRD_DELAY_MSG";
49         
50         private TextView textView0;
51         private TextView textView1;
52         private Button button1;
53         private Button button2;
54         private Button button3;
55         private Button button4;
56         private Button button5;
57         private Button button6;
58         private Button button7;
59         private Button button9;
60         private Button buttonz;
61         private LinearLayout linearLayout0;
62         private LinearLayout linearLayout1;
63         private LinearLayout linearLayout2;
64         private LinearLayout linearLayout3;
65         private LinearLayout linearLayout4;
66         private LinearLayout linearLayout5;
67         private LinearLayout linearLayout6;
68         private LinearLayout linearLayout7;
69         private LinearLayout linearLayout9;
70
71         private EditPrefUtil pref = new EditPrefUtil(this);
72         private boolean isClick = false;
73
74         // \88ê\82©\8f\8a\82É\92è\8b`\82µ\82½\82¢ \81¨ ConfUtil\82Í\82Ç\82¤?
75         // \8fC\90³ // TODO
76         // \83G\83\89\81[\83P\81[\83X\82ð\8dl\97
77         //pref.put(TrainDelayedServiceImpl.PREF_SVS_STATUS
78         //private StatusFlag tsSf = new StatusFlag(this,
79         //              TrainService.class.getName(), StatusFlag.TERM_30M);
80         private StatusFlag tsSf = StatusFlagFactory.getInstance().getStatusFlag(
81                         this, StatusFlagFactory.Type.TRAIN_SERVICE);
82         // FOO
83         private StatusFlag tdSf = StatusFlagFactory.getInstance().getStatusFlag(
84                         this, StatusFlagFactory.Type.TRAIN_DELAY_SERVICE);
85         
86         private BroadcastReceiver receiver = new BroadcastReceiver(){
87                 @Override
88                 public void onReceive(Context context, Intent intent) {
89                         String action = intent.getAction();
90                         
91                         if (action.compareTo(DelayReceiver.START_SERVICE) == 0){
92                                 setStatus();
93                         }
94                         else if (action.compareTo(DelayReceiver.STOP_SERVICE) == 0){
95                                 // \96¢\8eg\97p
96                         }
97                         else if (action.compareTo(DelayReceiver.UPDATE) == 0){
98                                 setStatus();
99                         }
100                         else if (action.compareTo(DelayReceiver.DELAY) == 0){
101                                 updateDelay(intent);
102                                 
103                                 setStatus();
104                         }
105                 }               
106         };
107         
108         @Override
109         public void onCreate(Bundle savedInstanceState) {
110                 super.onCreate(savedInstanceState);
111                 setContentView(R.layout.main);
112
113                 IntentFilter intentFilter = new IntentFilter();
114                 intentFilter.addAction(DelayReceiver.START_SERVICE);
115                 intentFilter.addAction(DelayReceiver.STOP_SERVICE);
116                 intentFilter.addAction(DelayReceiver.UPDATE);
117                 intentFilter.addAction(DelayReceiver.DELAY);
118                 registerReceiver(receiver, intentFilter);
119               
120                 button1 = (Button) this.findViewById(R.id.button01);
121                 button1.setOnClickListener(new OnClickListener() {
122                         public void onClick(View v) {
123                                 if (isClick){
124                                         return;
125                                 }
126                                 isClick = true;
127                                 
128                                 new AlertDialog.Builder(TrainDelayedActivity.this)
129                                         .setTitle(getString(R.string.warn_dialog_title))
130                                         .setMessage(getString(R.string.update_route_qmsg))
131                                         .setPositiveButton(getString(R.string.yes_button), new DialogInterface.OnClickListener() {
132                                                 public void onClick(DialogInterface dialog, int whichButton) {
133                                                         doExecuteTrainDelayedActivity();
134                                                         
135                                                         isClick = false;
136                                                 }
137                                         })
138                                         .setNegativeButton(getString(R.string.cancel_button), new DialogInterface.OnClickListener() {
139                                                 public void onClick(DialogInterface dialog, int whichButton) {
140                                                         isClick = false;
141                                                 }
142                                         })
143                                         .create().show();
144                         }});
145
146                 button2 = (Button) this.findViewById(R.id.button02);
147                 button2.setOnClickListener(new OnClickListener() {
148                         public void onClick(View view) {
149                                 Intent intent = new Intent(TrainDelayedActivity.this,
150                                                 (Class<?>) PrefectureListActivity.class);
151                                 startActivityForResult(intent, -1);
152                         }
153                 });
154
155                 button3 = (Button) this.findViewById(R.id.button03);
156                 button3.setOnClickListener(new OnClickListener() {
157                         public void onClick(View view) {
158                                 Intent intent = new Intent(TrainDelayedActivity.this,
159                                                 (Class<?>)MonitorTimeActivity.class);
160                                 startActivityForResult(intent, -1);
161                         }
162                 });
163                 
164                 button4 = (Button) this.findViewById(R.id.button04);
165                 button4.setOnClickListener(new OnClickListener() {
166                         public void onClick(View view) {
167                                 if (isClick){
168                                         return;
169                                 }
170                                 isClick = true;
171                                 
172                                 new AlertDialog.Builder(TrainDelayedActivity.this)
173                                         .setTitle(getString(R.string.warn_dialog_title))
174                                         .setMessage(getString(R.string.start_service_qmsg))
175                                         .setPositiveButton(getString(R.string.yes_button), new DialogInterface.OnClickListener() {
176                                                 public void onClick(DialogInterface dialog, int whichButton) {
177                                                         // \83T\81[\83r\83X\82ð\8aJ\8en
178                                                         startService();
179                                                         
180                                                         isClick = false;
181                                                 }
182                                         })
183                                         .setNegativeButton(getString(R.string.cancel_button), new DialogInterface.OnClickListener() {
184                                                 public void onClick(DialogInterface dialog, int whichButton) {
185                                                         isClick = false;
186                                                 }
187                                         })
188                                         .create().show();
189                         }
190                 });
191
192                 button5 = (Button) this.findViewById(R.id.button05);
193                 button5.setOnClickListener(new OnClickListener() {
194                         public void onClick(View view) {
195                                 if (isClick){
196                                         return;
197                                 }
198                                 isClick = true;
199
200                                 new AlertDialog.Builder(TrainDelayedActivity.this)
201                                 .setTitle(getString(R.string.warn_dialog_title))
202                                 .setMessage(getString(R.string.end_service_qmsg))
203                                 .setPositiveButton(getString(R.string.yes_button), new DialogInterface.OnClickListener() {
204                                         public void onClick(DialogInterface dialog, int whichButton) {
205                                                 stopService();
206
207                                                 isClick = false;
208                                         }
209                                 })
210                                 .setNegativeButton(getString(R.string.cancel_button), new DialogInterface.OnClickListener() {
211                                         public void onClick(DialogInterface dialog, int whichButton) {
212                                                 isClick = false;
213                                         }
214                                 })
215                                 .create().show();
216                         }
217                 });
218                 
219                 button6 = (Button) this.findViewById(R.id.button06);
220                 button6.setOnClickListener(new OnClickListener() {
221                         public void onClick(View view) {
222                                 HistoryActivity.init(TrainDelayedActivity.this);
223                                 Intent intent = new Intent(TrainDelayedActivity.this,
224                                                 (Class<?>) HistoryActivity.class);
225                                 startActivityForResult(intent, -1);
226                         }
227                 });
228                         
229                 button7 = (Button) this.findViewById(R.id.button07);
230                 button7.setOnClickListener(new OnClickListener() {
231                         public void onClick(View view) {
232                                 Intent intent = new Intent(TrainDelayedActivity.this,
233                                                 (Class<?>)ConfActivity.class);
234                                 startActivityForResult(intent, -1);
235                         }
236                 });
237
238                 button9 = (Button) this.findViewById(R.id.button09);
239                 button9.setOnClickListener(new OnClickListener() {
240                         public void onClick(View view) {
241                                 TrainDelayedActivity.this.finish();
242                         }
243                 });
244                 
245                 buttonz = (Button) this.findViewById(R.id.button0z);
246                 buttonz.setOnClickListener(new OnClickListener() {
247                         public void onClick(View view) {
248                                 debug();
249                         }
250                 });
251                 if (!Debug.isDebug()){
252                         buttonz.setVisibility(View.GONE);
253                 }
254
255                 linearLayout0 = (LinearLayout) this.findViewById(R.id.linearLayout00);
256                 linearLayout1 = (LinearLayout) this.findViewById(R.id.linearLayout01);
257                 linearLayout2 = (LinearLayout) this.findViewById(R.id.linearLayout02);
258                 linearLayout3 = (LinearLayout) this.findViewById(R.id.linearLayout03);
259                 linearLayout4 = (LinearLayout) this.findViewById(R.id.linearLayout04);
260                 linearLayout5 = (LinearLayout) this.findViewById(R.id.linearLayout05);
261                 linearLayout6 = (LinearLayout) this.findViewById(R.id.linearLayout06);
262                 linearLayout7 = (LinearLayout) this.findViewById(R.id.linearLayout07);
263                 linearLayout9 = (LinearLayout) this.findViewById(R.id.linearLayout09);
264                 
265                 textView0 = (TextView) this.findViewById(R.id.textView00);      
266                 textView1 = (TextView) this.findViewById(R.id.textView01);      
267         }
268
269         @Override
270         protected void onStart() {
271                 super.onStart();
272                 
273                 setStatus();
274         }
275
276         @Override
277         protected void onDestroy() {
278                 if (receiver != null){
279                         unregisterReceiver(receiver);
280                 }
281                 
282                 super.onDestroy();
283         }
284         
285         private void doExecuteTrainDelayedActivity() {
286                 tsSf.setOn();
287                 
288                 stopService();
289                 
290                 Intent intent = new Intent(TrainDelayedActivity.this,
291                                 (Class<?>) WaitActivity.class);
292                 intent.putExtra(WaitActivity.CLASS,
293                                 TrainServiceWaiting.class.getName());
294                 intent.putExtra(WaitActivity.MESSAGE0,
295                                 getString(R.string.waiting_msg));
296                 intent.putExtra(WaitActivity.MESSAGE1,
297                                 getString(R.string.start_rouote_msg));
298                 intent.putExtra(WaitActivity.MESSAGE2,
299                                 getString(R.string.end_rouote_msg));
300                 intent.putExtra(WaitActivity.MESSAGE3,
301                                 getString(R.string.end_rouote_emsg));
302                 
303                 WaitActivity.init(TrainDelayedActivity.this);
304                 startActivityForResult(intent, -1);
305         }
306
307         private void startService() {
308                 Intent intent = new Intent(TrainDelayedActivity.this,
309                                 TrainDelayedServiceImpl.class);
310                 intent.setAction(TrainDelayedServiceImpl.START_ACTION);
311                 startService(intent);
312                 
313                 // FOO
314                 //pref.put(TrainDelayedServiceImpl.PREF_SVS_STATUS, 1);
315                 //pref.update();
316                 tdSf.setRdy();
317
318                 setStatus();
319         }
320
321         private void stopService() {
322                 Intent intent = new Intent(TrainDelayedActivity.this,
323                                 TrainDelayedServiceImpl.class);
324                 PendingIntent alarmSender = PendingIntent.getService(
325                                 TrainDelayedActivity.this, 0, intent, 0);
326                 AlarmManager alerm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
327                 alerm.cancel(alarmSender);
328                 
329                 // FOO
330                 //pref.put(TrainDelayedServiceImpl.PREF_SVS_STATUS, 0);
331                 //pref.update();
332                 tdSf.setOff();
333                 setStatus();
334         }
335
336         public void updateDelay(Intent intent){
337                 Calendar cal = Calendar.getInstance();
338                 long t = cal.getTimeInMillis();
339
340                 updateDelay(intent, t);
341         }
342         
343         public void updateDelay(Intent intent, long t){
344                 StringMap map = new StringMap();
345                 String data = pref.get(PREF_TRD_DELAY);
346                 if (data != null){
347                         map.setData(data);
348                 }
349                 
350                 long id = intent.getLongExtra("ID", -1);
351                 map.put(String.valueOf(id), String.valueOf(t));
352                 
353                 data = map.getData();
354                 pref.put(PREF_TRD_DELAY, data);
355                 pref.update();          
356         }
357         
358         public boolean updateDelayMsg(){
359                 Calendar cal = Calendar.getInstance();
360                 long t = cal.getTimeInMillis();
361                 
362                 return updateDelayMsg(t);
363         }
364         
365         public boolean updateDelayMsg(long t){
366                 StringMap map = new StringMap();
367                 String data = pref.get(PREF_TRD_DELAY);
368                 if (data != null){
369                         map.setData(data);
370                 }
371
372                 Set<String> ks = map.keySet();
373                 for (Iterator<String> it = ks.iterator(); it.hasNext(); ){
374                         String key = it.next();
375                         String value = map.get(key);
376                         long dt = Long.valueOf(value);
377                         
378                         long mi = ConfUtil.getWidth(pref);
379                         if (t - dt > mi * 60 * 1000){
380                                 it.remove();
381                         }
382                 }
383                 data = map.getData();
384                 pref.put(PREF_TRD_DELAY, data);
385                 
386                 StringBuffer sb = new StringBuffer();
387                 TrainDao dao = TrainDao.getInstance();
388                 for (Iterator<String> it = ks.iterator(); it.hasNext(); ){
389                         String key = it.next();
390                         
391                         long id = Long.valueOf(key);
392                         Train train = dao.searchById(id);
393                         if (train != null){
394                                 if (sb.length() > 0){
395                                         sb.append("\81A");
396                                 }
397                                 sb.append(train.getName());
398                         }
399                 }
400                 boolean ret;
401                 if (sb.length() > 0){
402                         sb.append(getString(R.string.is_delay_msg));
403                         
404                         String text = sb.toString();
405                         pref.put(PREF_TRD_DELAY_MSG, text);
406                         
407                         textView0.setText(text);
408                         
409                         ret = true;
410                 }
411                 else {
412                         pref.remove(PREF_TRD_DELAY_MSG);
413                         ret = false;
414                 }
415                 pref.update();
416                 
417                 return ret;
418         }
419         
420         private void setStatus() {
421                 String text = "";
422                 // FOO
423                 //prefSvsStatus = pref.getInt(TrainDelayedServiceImpl.PREF_SVS_STATUS, 0);
424                 
425                 boolean enabled1 = true;
426                 boolean enabled2 = true;
427                 boolean enabled3 = true;
428                 boolean enabled4 = true;
429                 boolean enabled5 = true;
430                 boolean enabled6 = true;
431                 boolean enabled7 = true;
432                 boolean enabled9 = true;
433                 
434                 int c0 = Color.rgb(0xff, 0xff, 0xff);
435                 int c1 = Color.rgb(0xcc, 0x33, 0x33);
436                 int color1 = c0;
437                 int color2 = c0;
438                 int color3 = c0;
439                 int color4 = c0;
440                 int color5 = c0;
441                 int color6 = c0;
442                 int color7 = c0;
443                 int color9 = c0;
444                 
445                 int msgNo = -1;
446
447                 if (tsSf.isOn()){
448                         enabled1 = false;
449                         enabled2 = false;
450                         enabled3 = false;
451                         enabled4 = false;
452                         enabled5 = false;
453                         enabled6 = false;
454                         enabled7 = false;
455                         msgNo = 0;                                              
456                 }
457                 
458                 int count = TrainDao.getInstance().count();
459                 if (count <= 0){
460                         enabled2 = false;
461                         enabled3 = false;
462                         enabled4 = false;
463                         enabled5 = false;
464                         enabled6 = false;
465                         enabled7 = false;
466                         msgNo = 1;                      
467                 }
468                 
469                 List<Train> trainList = TrainDao.getInstance().search(true);
470                 int trainCount = trainList.size();
471                 if (trainCount == 0){
472                         enabled4 = false;
473                         msgNo = (msgNo < 0)? 2 : msgNo;
474                 }
475                 
476                 List<Monitor> listMonitor = MonitorDao.getInstance().search();
477                 int monitorCount = listMonitor.size();
478                 if (monitorCount == 0){
479                         enabled4 = false;                       
480                         msgNo = (msgNo < 0)? 3 : msgNo;
481                 }
482                 
483                 // FOO
484                 //switch (prefSvsStatus){
485                 switch (tdSf.get()){
486                 case StatusFlag.OFF:
487                         msgNo = (msgNo < 0)? 4 : msgNo;
488                         enabled5 = false;                                               
489                         break;
490                 case StatusFlag.RDY:
491                         msgNo = (msgNo < 0)? 5 : msgNo;
492                         enabled1 = false;                       
493                         enabled2 = false;                                               
494                         enabled3 = false;                                               
495                         enabled4 = false;                       
496                         enabled5 = false;                                               
497                         enabled6 = false;                       
498                         enabled7 = false;                                               
499                         break;
500                 case StatusFlag.ON:
501                         enabled4 = false;                       
502                         break;
503                 case StatusFlag.ERR:
504                         enabled2 = false;                                               
505                         enabled3 = false;                                               
506                         enabled4 = false;                       
507                         enabled5 = false;                                               
508                         enabled6 = false;                       
509                         enabled7 = false;                                               
510                         break;
511                 default:
512                         break;
513                 }
514                 
515                 button1.setEnabled(enabled1);
516                 button2.setEnabled(enabled2);
517                 button3.setEnabled(enabled3);
518                 button4.setEnabled(enabled4);
519                 button5.setEnabled(enabled5);
520                 button6.setEnabled(enabled6);
521                 button7.setEnabled(enabled7);
522                 button9.setEnabled(enabled9);
523                 
524                 switch (msgNo){
525                 case 0:
526                         text = getString(R.string.start_rouote_msg);
527                         break;
528                 case 1:
529                         text = getString(R.string.update_route_msg);
530                         color1 = c1;
531                         break;
532                 case 2:
533                         text = getString(R.string.conf_route_msg);
534                         color2 = c1;
535                         break;
536                 case 3:
537                         text = getString(R.string.conf_time_msg);
538                         color3 = c1;
539                         break;
540                 case 4:
541                         text = getString(R.string.start_service_msg);
542                         color4 = c1;
543                         break;
544                 case 5:
545                         text = getString(R.string.starting_service_msg);
546                         break;
547                 default:
548                         if (trainCount > 0){
549                                 StringBuffer sb = new StringBuffer();
550                                 for (Train train : trainList){
551                                         if (sb.length() > 0){
552                                                 sb.append(getString(R.string.comma_msg));
553                                         }
554                                         sb.append(train.getName());
555                                 }
556                                 sb.append(getString(R.string.do_service_msg));
557                                 text = sb.toString();
558                         }
559                         break;
560                 }
561
562                 boolean msgFlag = updateDelayMsg();
563                 if (msgFlag){
564                         linearLayout0.setVisibility(View.VISIBLE);
565                         color6 = c1;                    
566                 }
567                 else {
568                         linearLayout0.setVisibility(View.GONE);                 
569                 }
570                 
571                 textView1.setText(text);
572                 linearLayout1.setBackgroundColor(color1);
573                 linearLayout2.setBackgroundColor(color2);
574                 linearLayout3.setBackgroundColor(color3);
575                 linearLayout4.setBackgroundColor(color4);
576                 linearLayout5.setBackgroundColor(color5);
577                 linearLayout6.setBackgroundColor(color6);
578                 linearLayout7.setBackgroundColor(color7);
579                 linearLayout9.setBackgroundColor(color9);
580         }
581         
582         private void debug(){
583                 Intent intent;
584                 
585                 int n = 8;
586                 
587                 switch (n){
588                 case 1:
589                         // \92x\89\84\92Ê\92m
590                         List<Train> trainList = TrainDao.getInstance().search(true);
591                         for (Train train : trainList){
592                                 long id = train.getId();
593                                 intent = new Intent(DelayReceiver.DELAY);
594                                 intent.putExtra("ID", id);
595                                 sendBroadcast(intent);
596                         }
597                         setStatus();            
598                         break;
599                 case 2:
600                         // \83X\83P\83W\83\85\81[\83\8b\82Ì\83e\83X\83g
601                         SchedulerTest.repeat(TrainDelayedActivity.this);
602                         SchedulerTest.main();
603                         break;
604                 case 3:
605                         // \83u\83\8d\81[\83h\83L\83\83\83X\83g
606                         intent = new Intent("com.td.broadcast.DelayReceiver");
607                         intent.putExtra("TEXT", "\83T\83\93\83v\83\8b\83\81\83b\83Z\81[\83W");
608                         sendBroadcast(intent);
609                         break;
610                 case 4:
611                         // StatusFlag\82Ì\83e\83X\83g
612                         StatusFlagTest.main(this);
613                         break;
614                 case 5:
615                         // \83N\83\8a\83A
616                         tsSf.setOff();
617                         tdSf.setOff();
618                         WaitActivity.init(this);
619                         pref.remove(PREF_TRD_DELAY);
620                         pref.remove(PREF_TRD_DELAY_MSG);
621                         pref.update();
622
623                         setStatus();            
624                         break;
625                 case 6:
626                         TrainTwitterAdapterTest.main(this);
627                         break;
628                 case 7:
629                         TrainDelayedActivityTest.main(this);
630                         break;
631                 case 8:
632                         // DB\8dí\8f\9c
633                         PrefectureDao.getInstance().delete();
634                         break;
635                 default:
636                         break;
637                 }
638         }
639
640         public Context getContext() {
641                 return this;
642         }
643
644         public List<Train> getTrains() {
645                 return null;
646         }
647
648         public List<Keyword> getKeywords() {
649                 return null;
650         }
651
652         public void notificate(String name) {
653                 
654         }
655 }