/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/TrainInfoList.java
ViewVC logotype

Contents of /android/TrainInfo/src/dk/thoerup/traininfo/TrainInfoList.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 237 - (show annotations) (download)
Sat Aug 8 19:02:20 2009 UTC (14 years, 9 months ago) by torben
File size: 3537 byte(s)
First version
1 package dk.thoerup.traininfo;
2
3 import android.app.AlertDialog;
4 import android.app.Dialog;
5 import android.app.ListActivity;
6 import android.app.ProgressDialog;
7 import android.content.DialogInterface;
8 import android.content.Intent;
9 import android.os.Bundle;
10 import android.os.Handler;
11 import android.os.Message;
12 import android.view.View;
13 import android.widget.ListView;
14
15 public class TrainInfoList extends ListActivity {
16 public static final int GOTLOCATION = 1;
17 public static final int GOTSTATIONLIST = 2;
18 public static final int NOPROVIDER = 3;
19 public static final int FIXTIMEOUT = 4;
20 public static final int LOOKUPSTATIONFAILED = 5;
21
22 public static final int DLG_PROGRESS = 1;
23
24 /** Called when the activity is first created. */
25 ProgressDialog dialog;
26 StationLocator locator = null;
27
28 boolean isRunning;
29
30 StationListAdapter adapter = null;
31 @Override
32 public void onCreate(Bundle savedInstanceState) {
33 super.onCreate(savedInstanceState);
34 setContentView(R.layout.main);
35
36 adapter = new StationListAdapter(this);
37 setListAdapter(adapter);
38
39 locator = new StationLocator(this, stationsFetched);
40
41 startLookup();
42 }
43
44
45
46 @Override
47 protected Dialog onCreateDialog(int id) {
48 switch (id) {
49 case DLG_PROGRESS:
50 ProgressDialog dlg = new ProgressDialog(this);
51 dlg.setMessage("Wait for location fix");
52 dlg.setCancelable(false);
53 return dlg;
54 default:
55 return super.onCreateDialog(id);
56 }
57
58 }
59
60
61
62
63 @Override
64 protected void onPrepareDialog(int id, Dialog dialog) {
65 super.onPrepareDialog(id, dialog);
66 switch (id) {
67 case DLG_PROGRESS:
68 this.dialog = (ProgressDialog) dialog;
69 break;
70 }
71 }
72
73
74
75 public void progressDialog() {
76 dialog = new ProgressDialog(this);
77 dialog.setMessage("Wait for location fix");
78 dialog.setCancelable(false);
79 dialog.show();
80 }
81
82 public void startLookup() {
83 isRunning = true;
84 showDialog(DLG_PROGRESS);
85 //progressDialog();
86
87 locator.locateStations();
88 stationsFetched.sendEmptyMessageDelayed(FIXTIMEOUT, 20000);
89 }
90
91
92 Handler stationsFetched = new Handler() {
93 @Override
94 public void handleMessage(Message msg) {
95
96 switch (msg.what) {
97 case GOTLOCATION:
98 dialog.setMessage("Finding nearby stations");
99 break;
100 case GOTSTATIONLIST:
101 dialog.dismiss();
102 adapter.setStations( locator.getStations() );
103 break;
104 case NOPROVIDER:
105 dialog.dismiss();
106 showMessageBox("No Location provider enabled. Plase enabled gps.");
107 break;
108 case FIXTIMEOUT:
109 dialog.dismiss();
110 if (isRunning) {
111 locator.abortLocationListener();
112 showMessageBox("GPS fix timed out");
113 }
114 break;
115 case LOOKUPSTATIONFAILED:
116 dialog.dismiss();
117 showMessageBox("Error on finding nearby stations");
118 break;
119 }
120
121 isRunning = false;
122 }
123 };
124
125
126
127 @Override
128 protected void onListItemClick(ListView l, View v, int position, long id) {
129 super.onListItemClick(l, v, position, id);
130
131 StationBean station = adapter.getStation(position);
132
133
134 Intent intent = new Intent(this, DepartureList.class);
135 intent.putExtra("name", station.getName());
136 intent.putExtra("address", station.getAddress());
137 intent.putExtra("distance", station.getDistance());
138 startActivity(intent);
139 }
140
141 public void showMessageBox(String message) {
142 AlertDialog.Builder builder = new AlertDialog.Builder(this);
143 builder.setMessage(message)
144 .setCancelable(false)
145 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
146 public void onClick(DialogInterface dialog, int id) {
147 dialog.dismiss();
148 }
149 })
150 .show();
151
152 }
153 }

  ViewVC Help
Powered by ViewVC 1.1.20