OSDN Git Service

StatusFlagクラス作成
[train-delayed/source.git] / workspace / TrainDelayed / src / com / td / TrainDelayedActivity.java
1 package com.td;
2
3 import java.util.List;
4
5 import android.app.AlarmManager;
6 import android.app.AlertDialog;
7 import android.app.PendingIntent;
8 import android.content.BroadcastReceiver;
9 import android.content.Context;
10 import android.content.DialogInterface;
11 import android.content.Intent;
12 import android.content.IntentFilter;
13 import android.graphics.Color;
14 import android.os.Bundle;
15 import android.view.View;
16 import android.view.View.OnClickListener;
17 import android.widget.Button;
18 import android.widget.LinearLayout;
19 import android.widget.TextView;
20
21 import com.td.broadcast.DelayReceiver;
22 import com.td.db.Monitor;
23 import com.td.db.MonitorDao;
24 import com.td.db.Train;
25 import com.td.db.TrainDao;
26 import com.td.service.SchedulerTest;
27 import com.td.service.TrainDelayedServiceImpl;
28 import com.td.utility.BaseActivity;
29 import com.td.utility.Debug;
30 import com.td.utility.EditPrefUtil;
31 import com.td.utility.StatusFlagTest;
32 import com.td.wait.InitializeWating;
33 import com.td.wait.TrainServiceWaiting;
34
35 public class TrainDelayedActivity extends BaseActivity {
36         private TextView textView1;
37         private Button button1;
38         private Button button2;
39         private Button button3;
40         private Button button4;
41         private Button button5;
42         private Button button6;
43         private Button button7;
44         private Button button9;
45         private Button buttonf;
46         private Button buttonz;
47         private LinearLayout linearLayout1;
48         private LinearLayout linearLayout2;
49         private LinearLayout linearLayout3;
50         private LinearLayout linearLayout4;
51         private LinearLayout linearLayout5;
52         private LinearLayout linearLayout6;
53         private LinearLayout linearLayout7;
54         private LinearLayout linearLayout9;
55
56         private EditPrefUtil pref = new EditPrefUtil(this);
57         private int prefSvsStatus;
58         private boolean isClick = false;
59
60         private BroadcastReceiver receiver = new BroadcastReceiver(){
61                 @Override
62                 public void onReceive(Context context, Intent intent) {
63                         String action = intent.getAction();
64                         
65                         if (action.compareTo(DelayReceiver.START_SERVICE) == 0){
66                                 setStatus();
67                         }
68                         else if (action.compareTo(DelayReceiver.STOP_SERVICE) == 0){
69                                 // \96¢\8eg\97p
70                         }
71                 }               
72         };
73         
74         @Override
75         public void onCreate(Bundle savedInstanceState) {
76                 super.onCreate(savedInstanceState);
77                 setContentView(R.layout.main);
78
79                 IntentFilter intentFilter = new IntentFilter();
80                 intentFilter.addAction(DelayReceiver.START_SERVICE);
81                 intentFilter.addAction(DelayReceiver.STOP_SERVICE);
82                 registerReceiver(receiver, intentFilter);
83               
84                 button1 = (Button) this.findViewById(R.id.button01);
85                 button1.setOnClickListener(new OnClickListener() {
86                         public void onClick(View v) {
87                                 Intent intent = new Intent(TrainDelayedActivity.this,
88                                                 (Class<?>) WaitActivity.class);
89                                 intent.putExtra(WaitActivity.CLASS,
90                                                 TrainServiceWaiting.class.getName());
91                                 intent.putExtra(WaitActivity.MESSAGE1,
92                                                 getString(R.string.start_rouote_msg));
93                                 intent.putExtra(WaitActivity.MESSAGE2,
94                                                 getString(R.string.end_rouote_msg));
95                                 intent.putExtra(WaitActivity.MESSAGE3,
96                                                 "");
97                                 
98                                 WaitActivity.init(TrainDelayedActivity.this);
99                                 startActivityForResult(intent, -1);
100                         }});
101
102                 button2 = (Button) this.findViewById(R.id.button02);
103                 button2.setOnClickListener(new OnClickListener() {
104                         public void onClick(View view) {
105                                 Intent intent = new Intent(TrainDelayedActivity.this,
106                                                 (Class<?>) PrefectureListActivity.class);
107                                 startActivityForResult(intent, -1);
108                         }
109                 });
110
111                 button3 = (Button) this.findViewById(R.id.button03);
112                 button3.setOnClickListener(new OnClickListener() {
113                         public void onClick(View view) {
114                                 Intent intent = new Intent(TrainDelayedActivity.this,
115                                                 (Class<?>)MonitorTimeActivity.class);
116                                 startActivityForResult(intent, -1);
117                         }
118                 });
119                 
120                 button4 = (Button) this.findViewById(R.id.button04);
121                 button4.setOnClickListener(new OnClickListener() {
122                         public void onClick(View view) {
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.start_service_qmsg))
131                                         .setPositiveButton(getString(R.string.yes_button), new DialogInterface.OnClickListener() {
132                                                 public void onClick(DialogInterface dialog, int whichButton) {
133                                                         // \83T\81[\83r\83X\82ð\8aJ\8en
134                                                         Intent intent = new Intent(TrainDelayedActivity.this,
135                                                                         TrainDelayedServiceImpl.class);
136                                                         intent.setAction(TrainDelayedServiceImpl.START_ACTION);
137                                                         startService(intent);
138                                                         
139                                                         pref.put(TrainDelayedServiceImpl.PREF_SVS_STATUS, 1);
140                                                         pref.update();
141                                                         setStatus();
142                                                         
143                                                         isClick = false;
144                                                 }
145                                         })
146                                         .setNegativeButton(getString(R.string.cancel_button), new DialogInterface.OnClickListener() {
147                                                 public void onClick(DialogInterface dialog, int whichButton) {
148                                                         isClick = false;
149                                                 }
150                                         })
151                                         .create().show();
152                         }
153                 });
154
155                 button5 = (Button) this.findViewById(R.id.button05);
156                 button5.setOnClickListener(new OnClickListener() {
157                         public void onClick(View view) {
158                                 if (isClick){
159                                         return;
160                                 }
161                                 isClick = true;
162
163                                 new AlertDialog.Builder(TrainDelayedActivity.this)
164                                 .setTitle(getString(R.string.warn_dialog_title))
165                                 .setMessage(getString(R.string.end_service_qmsg))
166                                 .setPositiveButton(getString(R.string.yes_button), new DialogInterface.OnClickListener() {
167                                         public void onClick(DialogInterface dialog, int whichButton) {
168                                                 Intent intent = new Intent(TrainDelayedActivity.this,
169                                                                 TrainDelayedServiceImpl.class);
170                                                 PendingIntent alarmSender = PendingIntent.getService(
171                                                                 TrainDelayedActivity.this, 0, intent, 0);
172                                                 AlarmManager alerm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
173                                                 alerm.cancel(alarmSender);
174                                                 
175                                                 pref.put(TrainDelayedServiceImpl.PREF_SVS_STATUS, 0);
176                                                 pref.update();
177                                                 setStatus();
178
179                                                 isClick = false;
180                                         }
181                                 })
182                                 .setNegativeButton(getString(R.string.cancel_button), new DialogInterface.OnClickListener() {
183                                         public void onClick(DialogInterface dialog, int whichButton) {
184                                                 isClick = false;
185                                         }
186                                 })
187                                 .create().show();
188                         }
189                 });
190                 
191                 button6 = (Button) this.findViewById(R.id.button06);
192                 button6.setOnClickListener(new OnClickListener() {
193                         public void onClick(View view) {
194                                 HistoryActivity.init(TrainDelayedActivity.this);
195                                 Intent intent = new Intent(TrainDelayedActivity.this,
196                                                 (Class<?>) HistoryActivity.class);
197                                 startActivityForResult(intent, -1);
198                         }
199                 });
200                         
201                 button7 = (Button) this.findViewById(R.id.button07);
202                 button7.setOnClickListener(new OnClickListener() {
203                         public void onClick(View view) {
204                                 Intent intent = new Intent(TrainDelayedActivity.this,
205                                                 (Class<?>)SearchWordActivity.class);
206                                 startActivityForResult(intent, -1);
207                         }
208                 });
209
210                 button9 = (Button) this.findViewById(R.id.button09);
211                 button9.setOnClickListener(new OnClickListener() {
212                         public void onClick(View view) {
213                                 TrainDelayedActivity.this.finish();
214                         }
215                 });
216                 
217                 buttonf = (Button) this.findViewById(R.id.button0f);
218                 buttonf.setOnClickListener(new OnClickListener() {
219                         public void onClick(View view) {
220                                 // TODO \83_\83C\83A\83\8d\83O\82Å\8am\94F
221                                 initizlize();
222                         }
223                 });
224
225                 buttonz = (Button) this.findViewById(R.id.button0z);
226                 buttonz.setOnClickListener(new OnClickListener() {
227                         public void onClick(View view) {
228                                 debug();
229                         }
230                 });
231                 if (!Debug.isDebug()){
232                         buttonz.setVisibility(View.GONE);
233                 }
234
235                 linearLayout1 = (LinearLayout) this.findViewById(R.id.linearLayout01);
236                 linearLayout2 = (LinearLayout) this.findViewById(R.id.linearLayout02);
237                 linearLayout3 = (LinearLayout) this.findViewById(R.id.linearLayout03);
238                 linearLayout4 = (LinearLayout) this.findViewById(R.id.linearLayout04);
239                 linearLayout5 = (LinearLayout) this.findViewById(R.id.linearLayout05);
240                 linearLayout6 = (LinearLayout) this.findViewById(R.id.linearLayout06);
241                 linearLayout7 = (LinearLayout) this.findViewById(R.id.linearLayout07);
242                 linearLayout9 = (LinearLayout) this.findViewById(R.id.linearLayout09);
243                 
244                 textView1 = (TextView) this.findViewById(R.id.textView01);      
245         }
246
247         @Override
248         protected void onDestroy() {
249                 if (receiver != null){
250                         unregisterReceiver(receiver);
251                 }
252                 
253                 super.onDestroy();
254         }
255         
256         @Override
257         protected void onStart() {
258                 super.onStart();
259                 
260                 setStatus();
261         }
262
263         private void setStatus() {
264                 String text = "";
265                 prefSvsStatus = pref.getInt(TrainDelayedServiceImpl.PREF_SVS_STATUS, 0);
266                 
267                 boolean enabled1 = true;
268                 boolean enabled2 = true;
269                 boolean enabled3 = true;
270                 boolean enabled4 = true;
271                 boolean enabled5 = true;
272                 boolean enabled6 = true;
273                 boolean enabled7 = true;
274                 boolean enabled9 = true;
275                 
276                 int c0 = Color.rgb(0xff, 0xff, 0xff);
277                 int c1 = Color.rgb(0xcc, 0x33, 0x33);
278                 int color1 = c0;
279                 int color2 = c0;
280                 int color3 = c0;
281                 int color4 = c0;
282                 int color5 = c0;
283                 int color6 = c0;
284                 int color7 = c0;
285                 int color9 = c0;
286                 
287                 int msgNo = -1;
288
289                 int count = TrainDao.getInstance().count();
290                 if (count <= 0){
291                         enabled2 = false;
292                         enabled3 = false;
293                         enabled4 = false;
294                         enabled5 = false;
295                         enabled6 = false;
296                         enabled7 = false;
297                         msgNo = 1;                      
298                 }
299                 
300                 List<Train> trainList = TrainDao.getInstance().search(true);
301                 int trainCount = trainList.size();
302                 if (trainCount == 0){
303                         enabled4 = false;
304                         msgNo = (msgNo < 0)? 2 : msgNo;
305                 }
306                 
307                 List<Monitor> listMonitor = MonitorDao.getInstance().search();
308                 int monitorCount = listMonitor.size();
309                 if (monitorCount == 0){
310                         enabled4 = false;                       
311                         msgNo = (msgNo < 0)? 3 : msgNo;
312                 }
313                 
314                 switch (prefSvsStatus){
315                 case 0:
316                         msgNo = (msgNo < 0)? 4 : msgNo;
317                         enabled5 = false;                                               
318                         break;
319                 case 1:
320                         msgNo = (msgNo < 0)? 5 : msgNo;
321                         enabled1 = false;                       
322                         enabled2 = false;                                               
323                         enabled3 = false;                                               
324                         enabled4 = false;                       
325                         enabled5 = false;                                               
326                         enabled6 = false;                       
327                         enabled7 = false;                                               
328                         break;
329                 case 2:
330                         enabled4 = false;                       
331                         break;
332                 }
333                 
334                 button1.setEnabled(enabled1);
335                 button2.setEnabled(enabled2);
336                 button3.setEnabled(enabled3);
337                 button4.setEnabled(enabled4);
338                 button5.setEnabled(enabled5);
339                 button6.setEnabled(enabled6);
340                 button7.setEnabled(enabled7);
341                 button9.setEnabled(enabled9);
342                 
343                 switch (msgNo){
344                 case 1:
345                         text = getString(R.string.update_route_msg);
346                         color1 = c1;
347                         break;
348                 case 2:
349                         text = getString(R.string.conf_route_msg);
350                         color2 = c1;
351                         break;
352                 case 3:
353                         text = getString(R.string.conf_time_msg);
354                         color3 = c1;
355                         break;
356                 case 4:
357                         text = getString(R.string.start_service_msg);
358                         color4 = c1;
359                         break;
360                 case 5:
361                         text = getString(R.string.starting_service_msg);
362                         break;
363                 default:
364                         if (trainCount > 0){
365                                 StringBuffer sb = new StringBuffer();
366                                 for (Train train : trainList){
367                                         if (sb.length() > 0){
368                                                 sb.append(getString(R.string.comma_msg));
369                                         }
370                                         sb.append(train.getName());
371                                 }
372                                 sb.append(getString(R.string.do_service_msg));
373                                 text = sb.toString();
374                         }
375                         break;
376                 }
377
378                 textView1.setText(text);
379                 linearLayout1.setBackgroundColor(color1);
380                 linearLayout2.setBackgroundColor(color2);
381                 linearLayout3.setBackgroundColor(color3);
382                 linearLayout4.setBackgroundColor(color4);
383                 linearLayout5.setBackgroundColor(color5);
384                 linearLayout6.setBackgroundColor(color6);
385                 linearLayout7.setBackgroundColor(color7);
386                 linearLayout9.setBackgroundColor(color9);
387         }
388         
389         private void initizlize(){
390                 Intent intent = new Intent(this,
391                                 (Class<?>) WaitActivity.class);
392                 intent.putExtra(WaitActivity.CLASS,
393                                 InitializeWating.class.getName());
394                 intent.putExtra(WaitActivity.MESSAGE1,
395                                 getString(R.string.start_rouote_msg));
396                 intent.putExtra(WaitActivity.MESSAGE2,
397                                 getString(R.string.end_rouote_msg));
398                 intent.putExtra(WaitActivity.MESSAGE3,
399                                 "");
400                 
401                 WaitActivity.init(this);
402                 startActivityForResult(intent, -1);
403         }
404         
405         private void debug(){
406                 int n = 4;
407                 
408                 switch (n){
409                 case 1:
410                         // \83T\81[\83r\83X\83t\83\89\83O\83N\83\8a\83A
411                         pref.put(TrainDelayedServiceImpl.PREF_SVS_STATUS, 0);
412                         pref.update();
413                         setStatus();            
414                         break;
415                 case 2:
416                         // \83X\83P\83W\83\85\81[\83\8b\82Ì\83e\83X\83g
417                         SchedulerTest.repeat(TrainDelayedActivity.this);
418                         SchedulerTest.main();
419                         break;
420                 case 3:
421                         // \83u\83\8d\81[\83h\83L\83\83\83X\83g
422                         Intent intent = new Intent("com.td.broadcast.DelayReceiver");
423                         intent.putExtra("TEXT", "\83T\83\93\83v\83\8b\83\81\83b\83Z\81[\83W");
424                         sendBroadcast(intent);
425                         break;
426                 case 4:
427                         // StatusFlag\82Ì\83e\83X\83g
428                         StatusFlagTest.main(this);
429                         break;
430                 default:
431                         break;
432                 }
433         }
434 }