OSDN Git Service

#25723
[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.Context;
9 import android.content.DialogInterface;
10 import android.content.Intent;
11 import android.graphics.Color;
12 import android.os.Bundle;
13 import android.view.View;
14 import android.view.View.OnClickListener;
15 import android.widget.Button;
16 import android.widget.LinearLayout;
17 import android.widget.TextView;
18
19 import com.td.db.Monitor;
20 import com.td.db.MonitorDao;
21 import com.td.db.Train;
22 import com.td.db.TrainDao;
23 import com.td.service.TrainDelayedServiceImpl;
24 import com.td.utility.BaseActivity;
25 import com.td.utility.EditPrefUtil;
26 import com.td.utility.Util;
27
28 public class TrainDelayedActivity extends BaseActivity {
29         public static final String PREF_TRD_SFLG = "TRD_SFLG";
30         
31         private TextView textView1;
32         private Button button1;
33         private Button button2;
34         private Button button3;
35         private Button button4;
36         private Button button5;
37         private Button button6;
38         private Button button7;
39         private Button button9;
40         private LinearLayout linearLayout1;
41         private LinearLayout linearLayout2;
42         private LinearLayout linearLayout3;
43         private LinearLayout linearLayout4;
44         private LinearLayout linearLayout5;
45         private LinearLayout linearLayout6;
46         private LinearLayout linearLayout7;
47         private LinearLayout linearLayout9;
48
49         private EditPrefUtil pref = new EditPrefUtil(this);
50         private int prefSFlag;
51
52         @Override
53         public void onCreate(Bundle savedInstanceState) {
54                 super.onCreate(savedInstanceState);
55                 setContentView(R.layout.main);
56
57                 button1 = (Button) this.findViewById(R.id.button01);
58                 button1.setOnClickListener(new OnClickListener() {
59                         public void onClick(View v) {
60                                 pref.put(TrainDelayedServiceImpl.PREF_SVS_DBFLG, 0);
61                                 pref.update();
62                                 //SchedulerTest.repeat(TrainDelayedActivity.this);
63                                 //SchedulerTest.main();
64                         }});
65
66                 button2 = (Button) this.findViewById(R.id.button02);
67                 button2.setOnClickListener(new OnClickListener() {
68                         public void onClick(View view) {
69                                 Intent intent = new Intent(TrainDelayedActivity.this,
70                                                 (Class<?>) TrainListActivity.class);
71                                 startActivityForResult(intent, -1);
72                         }
73                 });
74
75                 button3 = (Button) this.findViewById(R.id.button03);
76                 button3.setOnClickListener(new OnClickListener() {
77                         public void onClick(View view) {
78                                 Intent intent = new Intent(TrainDelayedActivity.this,
79                                                 (Class<?>)MonitorTimeActivity.class);
80                                 startActivityForResult(intent, -1);
81                         }
82                 });
83                 
84                 button4 = (Button) this.findViewById(R.id.button04);
85                 button4.setOnClickListener(new OnClickListener() {
86                         public void onClick(View view) {
87                                 new AlertDialog.Builder(TrainDelayedActivity.this)
88                                         .setTitle(getString(R.string.warn_dialog_title))
89                                         .setMessage(getString(R.string.start_service_qmsg))
90                                         .setPositiveButton(getString(R.string.yes_button), new DialogInterface.OnClickListener() {
91                                                 public void onClick(DialogInterface dialog, int whichButton) {
92                                                         // \83T\81[\83r\83X\82ð\8aJ\8en
93                                                         Intent intent = new Intent(TrainDelayedActivity.this,
94                                                                         TrainDelayedServiceImpl.class);
95                                                         startService(intent);
96                                                         
97                                                         // TODO
98                                                         pref.put(PREF_TRD_SFLG, 1);
99                                                         pref.update();
100                                                         setStatus();
101                                                 }
102                                         })
103                                         .setNegativeButton(getString(R.string.cancel_button), new DialogInterface.OnClickListener() {
104                                                 public void onClick(DialogInterface dialog, int whichButton) {
105                                                         ;
106                                                 }
107                                         })
108                                         .create().show();
109                         }
110                 });
111
112                 button5 = (Button) this.findViewById(R.id.button05);
113                 button5.setOnClickListener(new OnClickListener() {
114                         public void onClick(View view) {
115                                 // TODO
116                                 //HistoryDao.getInstance().delete(TrainDelayedActivity.this);
117
118                                 new AlertDialog.Builder(TrainDelayedActivity.this)
119                                 .setTitle(getString(R.string.warn_dialog_title))
120                                 .setMessage(getString(R.string.end_service_qmsg))
121                                 .setPositiveButton(getString(R.string.yes_button), new DialogInterface.OnClickListener() {
122                                         public void onClick(DialogInterface dialog, int whichButton) {
123                                                 Intent intent = new Intent(TrainDelayedActivity.this,
124                                                                 TrainDelayedServiceImpl.class);
125                                                 PendingIntent alarmSender = PendingIntent.getService(
126                                                                 TrainDelayedActivity.this, 0, intent, 0);
127                                                 AlarmManager alerm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
128                                                 alerm.cancel(alarmSender);
129                                                 
130                                                 // TODO
131                                                 pref.put(PREF_TRD_SFLG, 0);
132                                                 pref.update();
133                                                 setStatus();
134                                         }
135                                 })
136                                 .setNegativeButton(getString(R.string.cancel_button), new DialogInterface.OnClickListener() {
137                                         public void onClick(DialogInterface dialog, int whichButton) {
138                                                 ;
139                                         }
140                                 })
141                                 .create().show();
142                         }
143                 });
144                 
145                 button6 = (Button) this.findViewById(R.id.button06);
146                 button6.setOnClickListener(new OnClickListener() {
147                         public void onClick(View view) {
148                                 HistoryActivity.init(TrainDelayedActivity.this);
149                                 Intent intent = new Intent(TrainDelayedActivity.this,
150                                                 (Class<?>) HistoryActivity.class);
151                                 startActivityForResult(intent, -1);
152                         }
153                 });
154                         
155                 button7 = (Button) this.findViewById(R.id.button07);
156                 button7.setOnClickListener(new OnClickListener() {
157                         public void onClick(View view) {
158                                 Intent intent = new Intent(TrainDelayedActivity.this,
159                                                 (Class<?>)SearchWordActivity.class);
160                                 startActivityForResult(intent, -1);
161                         }
162                 });
163
164                 button9 = (Button) this.findViewById(R.id.button09);
165                 button9.setOnClickListener(new OnClickListener() {
166                         public void onClick(View view) {
167                                 TrainDelayedActivity.this.finish();
168                         }
169                 });
170                 
171                 linearLayout1 = (LinearLayout) this.findViewById(R.id.linearLayout01);
172                 linearLayout2 = (LinearLayout) this.findViewById(R.id.linearLayout02);
173                 linearLayout3 = (LinearLayout) this.findViewById(R.id.linearLayout03);
174                 linearLayout4 = (LinearLayout) this.findViewById(R.id.linearLayout04);
175                 linearLayout5 = (LinearLayout) this.findViewById(R.id.linearLayout05);
176                 linearLayout6 = (LinearLayout) this.findViewById(R.id.linearLayout06);
177                 linearLayout7 = (LinearLayout) this.findViewById(R.id.linearLayout07);
178                 linearLayout9 = (LinearLayout) this.findViewById(R.id.linearLayout09);
179                 
180                 textView1 = (TextView) this.findViewById(R.id.textView01);      
181         }
182
183         @Override
184         protected void onStart() {
185                 super.onStart();
186                 
187                 setStatus();
188         }
189
190         private void setStatus() {
191                 String text = "";
192                 prefSFlag = pref.getInt(PREF_TRD_SFLG, 0);
193                 
194                 boolean enabled1 = true;
195                 boolean enabled2 = true;
196                 boolean enabled3 = true;
197                 boolean enabled4 = true;
198                 boolean enabled5 = true;
199                 boolean enabled6 = true;
200                 boolean enabled7 = true;
201                 boolean enabled9 = true;
202                 
203                 int c0 = Color.rgb(0xff, 0xff, 0xff);
204                 int c1 = Color.rgb(0xff, 0x33, 0x33);
205                 int color1 = c0;
206                 int color2 = c0;
207                 int color3 = c0;
208                 int color4 = c0;
209                 int color5 = c0;
210                 int color6 = c0;
211                 int color7 = c0;
212                 int color9 = c0;
213                 
214                 int msgNo = -1;
215
216                 List<Train> trainList = TrainDao.getInstance().search(true);
217                 int trainCount = trainList.size();
218                 if (trainCount == 0){
219                         enabled4 = false;
220                         msgNo = (msgNo < 0)? 2 : msgNo;
221                 }
222                 
223                 List<Monitor> listMonitor = MonitorDao.getInstance().search();
224                 int monitorCount = listMonitor.size();
225                 if (monitorCount == 0){
226                         enabled4 = false;                       
227                         msgNo = (msgNo < 0)? 3 : msgNo;
228                 }
229                 
230                 if (Util.toBoolean(prefSFlag)){
231                         enabled4 = false;                       
232                 }
233                 else {
234                         msgNo = (msgNo < 0)? 4 : msgNo;
235                         enabled5 = false;                                               
236                 }
237                 
238                 button1.setEnabled(enabled1);
239                 button2.setEnabled(enabled2);
240                 button3.setEnabled(enabled3);
241                 button4.setEnabled(enabled4);
242                 button5.setEnabled(enabled5);
243                 button6.setEnabled(enabled6);
244                 button7.setEnabled(enabled7);
245                 button9.setEnabled(enabled9);
246                 
247                 switch (msgNo){
248                 case 2:
249                         text = getString(R.string.conf_route_msg);
250                         color2 = c1;
251                         break;
252                 case 3:
253                         text = getString(R.string.conf_time_msg);
254                         color3 = c1;
255                         break;
256                 case 4:
257                         text = getString(R.string.start_service_msg);
258                         color4 = c1;
259                         break;
260                 default:
261                         if (trainCount > 0){
262                                 StringBuffer sb = new StringBuffer();
263                                 for (Train train : trainList){
264                                         if (sb.length() > 0){
265                                                 sb.append(getString(R.string.comma_msg));
266                                         }
267                                         sb.append(train.getName());
268                                 }
269                                 sb.append(getString(R.string.do_service_msg));
270                                 text = sb.toString();
271                         }
272                         break;
273                 }
274
275                 textView1.setText(text);
276                 linearLayout1.setBackgroundColor(color1);
277                 linearLayout2.setBackgroundColor(color2);
278                 linearLayout3.setBackgroundColor(color3);
279                 linearLayout4.setBackgroundColor(color4);
280                 linearLayout5.setBackgroundColor(color5);
281                 linearLayout6.setBackgroundColor(color6);
282                 linearLayout7.setBackgroundColor(color7);
283                 linearLayout9.setBackgroundColor(color9);
284         }
285 }