3 import java.util.Calendar;
4 import java.util.Iterator;
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;
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;
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";
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;
71 private EditPrefUtil pref = new EditPrefUtil(this);
72 private boolean isClick = false;
74 //
\88ê
\82©
\8f\8a\82É
\92è
\8b`
\82µ
\82½
\82¢
\81¨ ConfUtil
\82Í
\82Ç
\82¤?
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);
83 private StatusFlag tdSf = StatusFlagFactory.getInstance().getStatusFlag(
84 this, StatusFlagFactory.Type.TRAIN_DELAY_SERVICE);
86 private BroadcastReceiver receiver = new BroadcastReceiver(){
88 public void onReceive(Context context, Intent intent) {
89 String action = intent.getAction();
91 if (action.compareTo(DelayReceiver.START_SERVICE) == 0){
94 else if (action.compareTo(DelayReceiver.STOP_SERVICE) == 0){
97 else if (action.compareTo(DelayReceiver.UPDATE) == 0){
100 else if (action.compareTo(DelayReceiver.DELAY) == 0){
109 public void onCreate(Bundle savedInstanceState) {
110 super.onCreate(savedInstanceState);
111 setContentView(R.layout.main);
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);
120 button1 = (Button) this.findViewById(R.id.button01);
121 button1.setOnClickListener(new OnClickListener() {
122 public void onClick(View v) {
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();
138 .setNegativeButton(getString(R.string.cancel_button), new DialogInterface.OnClickListener() {
139 public void onClick(DialogInterface dialog, int whichButton) {
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);
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);
164 button4 = (Button) this.findViewById(R.id.button04);
165 button4.setOnClickListener(new OnClickListener() {
166 public void onClick(View view) {
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
183 .setNegativeButton(getString(R.string.cancel_button), new DialogInterface.OnClickListener() {
184 public void onClick(DialogInterface dialog, int whichButton) {
192 button5 = (Button) this.findViewById(R.id.button05);
193 button5.setOnClickListener(new OnClickListener() {
194 public void onClick(View view) {
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) {
210 .setNegativeButton(getString(R.string.cancel_button), new DialogInterface.OnClickListener() {
211 public void onClick(DialogInterface dialog, int whichButton) {
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);
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);
238 button9 = (Button) this.findViewById(R.id.button09);
239 button9.setOnClickListener(new OnClickListener() {
240 public void onClick(View view) {
241 TrainDelayedActivity.this.finish();
245 buttonz = (Button) this.findViewById(R.id.button0z);
246 buttonz.setOnClickListener(new OnClickListener() {
247 public void onClick(View view) {
251 if (!Debug.isDebug()){
252 buttonz.setVisibility(View.GONE);
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);
265 textView0 = (TextView) this.findViewById(R.id.textView00);
266 textView1 = (TextView) this.findViewById(R.id.textView01);
270 protected void onStart() {
277 protected void onDestroy() {
278 if (receiver != null){
279 unregisterReceiver(receiver);
285 private void doExecuteTrainDelayedActivity() {
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));
303 WaitActivity.init(TrainDelayedActivity.this);
304 startActivityForResult(intent, -1);
307 private void startService() {
308 Intent intent = new Intent(TrainDelayedActivity.this,
309 TrainDelayedServiceImpl.class);
310 intent.setAction(TrainDelayedServiceImpl.START_ACTION);
311 startService(intent);
314 //pref.put(TrainDelayedServiceImpl.PREF_SVS_STATUS, 1);
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);
330 //pref.put(TrainDelayedServiceImpl.PREF_SVS_STATUS, 0);
336 public void updateDelay(Intent intent){
337 Calendar cal = Calendar.getInstance();
338 long t = cal.getTimeInMillis();
340 updateDelay(intent, t);
343 public void updateDelay(Intent intent, long t){
344 StringMap map = new StringMap();
345 String data = pref.get(PREF_TRD_DELAY);
350 long id = intent.getLongExtra("ID", -1);
351 map.put(String.valueOf(id), String.valueOf(t));
353 data = map.getData();
354 pref.put(PREF_TRD_DELAY, data);
358 public boolean updateDelayMsg(){
359 Calendar cal = Calendar.getInstance();
360 long t = cal.getTimeInMillis();
362 return updateDelayMsg(t);
365 public boolean updateDelayMsg(long t){
366 StringMap map = new StringMap();
367 String data = pref.get(PREF_TRD_DELAY);
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);
378 long mi = ConfUtil.getWidth(pref);
379 if (t - dt > mi * 60 * 1000){
383 data = map.getData();
384 pref.put(PREF_TRD_DELAY, data);
386 StringBuffer sb = new StringBuffer();
387 TrainDao dao = TrainDao.getInstance();
388 for (Iterator<String> it = ks.iterator(); it.hasNext(); ){
389 String key = it.next();
391 long id = Long.valueOf(key);
392 Train train = dao.searchById(id);
394 if (sb.length() > 0){
397 sb.append(train.getName());
401 if (sb.length() > 0){
402 sb.append(getString(R.string.is_delay_msg));
404 String text = sb.toString();
405 pref.put(PREF_TRD_DELAY_MSG, text);
407 textView0.setText(text);
412 pref.remove(PREF_TRD_DELAY_MSG);
420 private void setStatus() {
423 //prefSvsStatus = pref.getInt(TrainDelayedServiceImpl.PREF_SVS_STATUS, 0);
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;
434 int c0 = Color.rgb(0xff, 0xff, 0xff);
435 int c1 = Color.rgb(0xcc, 0x33, 0x33);
458 int count = TrainDao.getInstance().count();
469 List<Train> trainList = TrainDao.getInstance().search(true);
470 int trainCount = trainList.size();
471 if (trainCount == 0){
473 msgNo = (msgNo < 0)? 2 : msgNo;
476 List<Monitor> listMonitor = MonitorDao.getInstance().search();
477 int monitorCount = listMonitor.size();
478 if (monitorCount == 0){
480 msgNo = (msgNo < 0)? 3 : msgNo;
484 //switch (prefSvsStatus){
487 msgNo = (msgNo < 0)? 4 : msgNo;
491 msgNo = (msgNo < 0)? 5 : msgNo;
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);
526 text = getString(R.string.start_rouote_msg);
529 text = getString(R.string.update_route_msg);
533 text = getString(R.string.conf_route_msg);
537 text = getString(R.string.conf_time_msg);
541 text = getString(R.string.start_service_msg);
545 text = getString(R.string.starting_service_msg);
549 StringBuffer sb = new StringBuffer();
550 for (Train train : trainList){
551 if (sb.length() > 0){
552 sb.append(getString(R.string.comma_msg));
554 sb.append(train.getName());
556 sb.append(getString(R.string.do_service_msg));
557 text = sb.toString();
562 boolean msgFlag = updateDelayMsg();
564 linearLayout0.setVisibility(View.VISIBLE);
568 linearLayout0.setVisibility(View.GONE);
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);
582 private void debug(){
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);
600 //
\83X
\83P
\83W
\83\85\81[
\83\8b\82Ì
\83e
\83X
\83g
601 SchedulerTest.repeat(TrainDelayedActivity.this);
602 SchedulerTest.main();
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);
611 // StatusFlag
\82Ì
\83e
\83X
\83g
612 StatusFlagTest.main(this);
618 WaitActivity.init(this);
619 pref.remove(PREF_TRD_DELAY);
620 pref.remove(PREF_TRD_DELAY_MSG);
626 TrainTwitterAdapterTest.main(this);
629 TrainDelayedActivityTest.main(this);
633 PrefectureDao.getInstance().delete();
640 public Context getContext() {
644 public List<Train> getTrains() {
648 public List<Keyword> getKeywords() {
652 public void notificate(String name) {