OSDN Git Service

Prefectureクラス作成。路線関連のDB作成中
[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.EditPrefUtil;
30 import com.td.webapi.RouteUpdater;
31
32 public class TrainDelayedActivity extends BaseActivity {
33         private TextView textView1;
34         private Button button1;
35         private Button button2;
36         private Button button3;
37         private Button button4;
38         private Button button5;
39         private Button button6;
40         private Button button7;
41         private Button button9;
42         private LinearLayout linearLayout1;
43         private LinearLayout linearLayout2;
44         private LinearLayout linearLayout3;
45         private LinearLayout linearLayout4;
46         private LinearLayout linearLayout5;
47         private LinearLayout linearLayout6;
48         private LinearLayout linearLayout7;
49         private LinearLayout linearLayout9;
50
51         private EditPrefUtil pref = new EditPrefUtil(this);
52         private int prefSvsStatus;
53         private boolean isClick = false;
54
55         //private DelayReceiver receiver = new DelayReceiver(this);
56         private BroadcastReceiver receiver = new BroadcastReceiver(){
57                 @Override
58                 public void onReceive(Context context, Intent intent) {
59                         String action = intent.getAction();
60                         
61                         if (action.compareTo(DelayReceiver.START_SERVICE) == 0){
62                                 setStatus();
63                         }
64                         else if (action.compareTo(DelayReceiver.STOP_SERVICE) == 0){
65                                 // \96¢\8eg\97p
66                         }
67                 }               
68         };
69         
70         @Override
71         public void onCreate(Bundle savedInstanceState) {
72                 super.onCreate(savedInstanceState);
73                 setContentView(R.layout.main);
74
75                 IntentFilter intentFilter = new IntentFilter();
76                 intentFilter.addAction(DelayReceiver.START_SERVICE);
77                 intentFilter.addAction(DelayReceiver.STOP_SERVICE);
78                 registerReceiver(receiver, intentFilter);
79               
80                 button1 = (Button) this.findViewById(R.id.button01);
81                 button1.setOnClickListener(new OnClickListener() {
82                         public void onClick(View v) {
83                                 RouteUpdater up = new RouteUpdater();
84                                 up.update();
85                                 
86 //                              HistoryDao.getInstance().delete(TrainDelayedActivity.this);
87
88 //                              Intent intent = new Intent("com.td.broadcast.DelayReceiver");
89 //                              intent.putExtra("TEXT", "\83T\83\93\83v\83\8b\83\81\83b\83Z\81[\83W");
90 //                              sendBroadcast(intent);
91 //                              
92 //                              SchedulerTest.repeat(TrainDelayedActivity.this);
93 //                              SchedulerTest.main();
94                         }});
95
96                 button2 = (Button) this.findViewById(R.id.button02);
97                 button2.setOnClickListener(new OnClickListener() {
98                         public void onClick(View view) {
99                                 // TrainListActivity
100                                 // PrefectureListActivity
101                                 Intent intent = new Intent(TrainDelayedActivity.this,
102                                                 (Class<?>) TrainListActivity.class);
103                                 startActivityForResult(intent, -1);
104                         }
105                 });
106
107                 button3 = (Button) this.findViewById(R.id.button03);
108                 button3.setOnClickListener(new OnClickListener() {
109                         public void onClick(View view) {
110                                 Intent intent = new Intent(TrainDelayedActivity.this,
111                                                 (Class<?>)MonitorTimeActivity.class);
112                                 startActivityForResult(intent, -1);
113                         }
114                 });
115                 
116                 button4 = (Button) this.findViewById(R.id.button04);
117                 button4.setOnClickListener(new OnClickListener() {
118                         public void onClick(View view) {
119                                 if (isClick){
120                                         return;
121                                 }
122                                 isClick = true;
123                                 
124                                 new AlertDialog.Builder(TrainDelayedActivity.this)
125                                         .setTitle(getString(R.string.warn_dialog_title))
126                                         .setMessage(getString(R.string.start_service_qmsg))
127                                         .setPositiveButton(getString(R.string.yes_button), new DialogInterface.OnClickListener() {
128                                                 public void onClick(DialogInterface dialog, int whichButton) {
129                                                         // \83T\81[\83r\83X\82ð\8aJ\8en
130                                                         Intent intent = new Intent(TrainDelayedActivity.this,
131                                                                         TrainDelayedServiceImpl.class);
132                                                         intent.setAction(TrainDelayedServiceImpl.START_ACTION);
133                                                         startService(intent);
134                                                         
135                                                         pref.put(TrainDelayedServiceImpl.PREF_SVS_STATUS, 1);
136                                                         pref.update();
137                                                         setStatus();
138                                                         
139                                                         isClick = false;
140                                                 }
141                                         })
142                                         .setNegativeButton(getString(R.string.cancel_button), new DialogInterface.OnClickListener() {
143                                                 public void onClick(DialogInterface dialog, int whichButton) {
144                                                         isClick = false;
145                                                 }
146                                         })
147                                         .create().show();
148                         }
149                 });
150
151                 button5 = (Button) this.findViewById(R.id.button05);
152                 button5.setOnClickListener(new OnClickListener() {
153                         public void onClick(View view) {
154                                 if (isClick){
155                                         return;
156                                 }
157                                 isClick = true;
158
159                                 new AlertDialog.Builder(TrainDelayedActivity.this)
160                                 .setTitle(getString(R.string.warn_dialog_title))
161                                 .setMessage(getString(R.string.end_service_qmsg))
162                                 .setPositiveButton(getString(R.string.yes_button), new DialogInterface.OnClickListener() {
163                                         public void onClick(DialogInterface dialog, int whichButton) {
164                                                 Intent intent = new Intent(TrainDelayedActivity.this,
165                                                                 TrainDelayedServiceImpl.class);
166                                                 PendingIntent alarmSender = PendingIntent.getService(
167                                                                 TrainDelayedActivity.this, 0, intent, 0);
168                                                 AlarmManager alerm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
169                                                 alerm.cancel(alarmSender);
170                                                 
171                                                 pref.put(TrainDelayedServiceImpl.PREF_SVS_STATUS, 0);
172                                                 pref.update();
173                                                 setStatus();
174
175                                                 isClick = false;
176                                         }
177                                 })
178                                 .setNegativeButton(getString(R.string.cancel_button), new DialogInterface.OnClickListener() {
179                                         public void onClick(DialogInterface dialog, int whichButton) {
180                                                 isClick = false;
181                                         }
182                                 })
183                                 .create().show();
184                         }
185                 });
186                 
187                 button6 = (Button) this.findViewById(R.id.button06);
188                 button6.setOnClickListener(new OnClickListener() {
189                         public void onClick(View view) {
190                                 HistoryActivity.init(TrainDelayedActivity.this);
191                                 Intent intent = new Intent(TrainDelayedActivity.this,
192                                                 (Class<?>) HistoryActivity.class);
193                                 startActivityForResult(intent, -1);
194                         }
195                 });
196                         
197                 button7 = (Button) this.findViewById(R.id.button07);
198                 button7.setOnClickListener(new OnClickListener() {
199                         public void onClick(View view) {
200                                 Intent intent = new Intent(TrainDelayedActivity.this,
201                                                 (Class<?>)SearchWordActivity.class);
202                                 startActivityForResult(intent, -1);
203                         }
204                 });
205
206                 button9 = (Button) this.findViewById(R.id.button09);
207                 button9.setOnClickListener(new OnClickListener() {
208                         public void onClick(View view) {
209                                 TrainDelayedActivity.this.finish();
210                         }
211                 });
212                 
213                 linearLayout1 = (LinearLayout) this.findViewById(R.id.linearLayout01);
214                 linearLayout2 = (LinearLayout) this.findViewById(R.id.linearLayout02);
215                 linearLayout3 = (LinearLayout) this.findViewById(R.id.linearLayout03);
216                 linearLayout4 = (LinearLayout) this.findViewById(R.id.linearLayout04);
217                 linearLayout5 = (LinearLayout) this.findViewById(R.id.linearLayout05);
218                 linearLayout6 = (LinearLayout) this.findViewById(R.id.linearLayout06);
219                 linearLayout7 = (LinearLayout) this.findViewById(R.id.linearLayout07);
220                 linearLayout9 = (LinearLayout) this.findViewById(R.id.linearLayout09);
221                 
222                 textView1 = (TextView) this.findViewById(R.id.textView01);      
223         }
224
225         @Override
226         protected void onStart() {
227                 super.onStart();
228                 
229                 setStatus();
230         }
231
232         private void setStatus() {
233                 String text = "";
234                 prefSvsStatus = pref.getInt(TrainDelayedServiceImpl.PREF_SVS_STATUS, 0);
235                 
236                 boolean enabled1 = true;
237                 boolean enabled2 = true;
238                 boolean enabled3 = true;
239                 boolean enabled4 = true;
240                 boolean enabled5 = true;
241                 boolean enabled6 = true;
242                 boolean enabled7 = true;
243                 boolean enabled9 = true;
244                 
245                 int c0 = Color.rgb(0xff, 0xff, 0xff);
246                 int c1 = Color.rgb(0xcc, 0x33, 0x33);
247                 int color1 = c0;
248                 int color2 = c0;
249                 int color3 = c0;
250                 int color4 = c0;
251                 int color5 = c0;
252                 int color6 = c0;
253                 int color7 = c0;
254                 int color9 = c0;
255                 
256                 int msgNo = -1;
257
258                 List<Train> trainList = TrainDao.getInstance().search(true);
259                 int trainCount = trainList.size();
260                 if (trainCount == 0){
261                         enabled4 = false;
262                         msgNo = (msgNo < 0)? 2 : msgNo;
263                 }
264                 
265                 List<Monitor> listMonitor = MonitorDao.getInstance().search();
266                 int monitorCount = listMonitor.size();
267                 if (monitorCount == 0){
268                         enabled4 = false;                       
269                         msgNo = (msgNo < 0)? 3 : msgNo;
270                 }
271                 
272                 switch (prefSvsStatus){
273                 case 0:
274                         msgNo = (msgNo < 0)? 4 : msgNo;
275                         enabled5 = false;                                               
276                         break;
277                 case 1:
278                         enabled4 = false;                       
279                         enabled5 = false;                                               
280                         break;
281                 case 2:
282                         enabled4 = false;                       
283                         break;
284                 }
285                 
286                 button1.setEnabled(enabled1);
287                 button2.setEnabled(enabled2);
288                 button3.setEnabled(enabled3);
289                 button4.setEnabled(enabled4);
290                 button5.setEnabled(enabled5);
291                 button6.setEnabled(enabled6);
292                 button7.setEnabled(enabled7);
293                 button9.setEnabled(enabled9);
294                 
295                 switch (msgNo){
296                 case 2:
297                         text = getString(R.string.conf_route_msg);
298                         color2 = c1;
299                         break;
300                 case 3:
301                         text = getString(R.string.conf_time_msg);
302                         color3 = c1;
303                         break;
304                 case 4:
305                         text = getString(R.string.start_service_msg);
306                         color4 = c1;
307                         break;
308                 default:
309                         if (trainCount > 0){
310                                 StringBuffer sb = new StringBuffer();
311                                 for (Train train : trainList){
312                                         if (sb.length() > 0){
313                                                 sb.append(getString(R.string.comma_msg));
314                                         }
315                                         sb.append(train.getName());
316                                 }
317                                 sb.append(getString(R.string.do_service_msg));
318                                 text = sb.toString();
319                         }
320                         break;
321                 }
322
323                 textView1.setText(text);
324                 linearLayout1.setBackgroundColor(color1);
325                 linearLayout2.setBackgroundColor(color2);
326                 linearLayout3.setBackgroundColor(color3);
327                 linearLayout4.setBackgroundColor(color4);
328                 linearLayout5.setBackgroundColor(color5);
329                 linearLayout6.setBackgroundColor(color6);
330                 linearLayout7.setBackgroundColor(color7);
331                 linearLayout9.setBackgroundColor(color9);
332         }
333 }