OSDN Git Service

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