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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 474 - (show annotations) (download)
Tue Oct 27 13:40:15 2009 UTC (14 years, 6 months ago) by torben
File size: 5607 byte(s)
Remove unneeded imports
1 package dk.thoerup.traininfo;
2
3 import java.text.NumberFormat;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import android.app.AlertDialog;
8 import android.app.Dialog;
9 import android.app.ListActivity;
10 import android.app.ProgressDialog;
11 import android.content.DialogInterface;
12 import android.content.Intent;
13 import android.net.Uri;
14 import android.os.AsyncTask;
15 import android.os.Bundle;
16 import android.view.View;
17 import android.widget.ListView;
18 import android.widget.TextView;
19 import dk.thoerup.traininfo.provider.DepartureProvider;
20 import dk.thoerup.traininfo.provider.ProviderFactory;
21 import dk.thoerup.traininfo.util.MessageBox;
22
23 public class DepartureList extends ListActivity {
24
25 public static final int DLG_PROGRESS = 1;
26
27
28 DepartureListAdapter adapter;
29 DepartureProvider provider;
30 List<DepartureBean> departures;
31
32 int selectedItemId;
33 //DepartureBean currentDeparture;
34
35 ProgressDialog pgDialog;
36
37 DepartureFetcher fetcher;
38 int stationId;
39
40 double latitude,longitude;
41
42 @SuppressWarnings("unchecked")
43 @Override
44 protected void onCreate(Bundle savedInstanceState) {
45 super.onCreate(savedInstanceState);
46 setContentView(R.layout.departurelist);
47
48 adapter = new DepartureListAdapter(this);
49 setListAdapter(adapter);
50
51 Intent launchedBy = getIntent();
52
53 latitude = launchedBy.getDoubleExtra("latitude", 0.0);
54 longitude = launchedBy.getDoubleExtra("longitude", 0.0);
55
56 String name = launchedBy.getStringExtra("name");
57 ((TextView) findViewById(R.id.stationName)).setText( name );
58
59 String address = launchedBy.getStringExtra("address");
60 ((TextView) findViewById(R.id.stationAddr)).setText( address );
61
62 stationId = launchedBy.getIntExtra("stationid", -1);
63
64 findViewById(R.id.header).setOnClickListener( mapLauncher );
65
66 NumberFormat format = NumberFormat.getNumberInstance();
67 format.setMaximumFractionDigits(1);
68 format.setMinimumFractionDigits(1);
69 int distance = launchedBy.getIntExtra("distance", 0);
70 ((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." );
71
72
73 provider = ProviderFactory.getDepartureProvider();
74
75 if (savedInstanceState == null) {
76 startDepartureFetcher();
77 } else {
78 departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures");
79 adapter.setDepartures(departures);
80 selectedItemId = savedInstanceState.getInt("selectedItemId");
81 }
82 }
83
84 @Override
85 public void onSaveInstanceState(Bundle outState)
86 {
87 if (pgDialog != null && pgDialog.isShowing())
88 dismissDialog(DLG_PROGRESS);
89
90 outState.putInt("selectedItemId", selectedItemId);
91
92 outState.putSerializable("departures", (ArrayList<DepartureBean>) departures);
93 }
94
95 @Override
96 protected void onListItemClick(ListView l, View v, int position, long id) {
97 super.onListItemClick(l, v, position, id);
98
99 selectedItemId = position;
100
101 DepartureBean dep = departures.get(selectedItemId);
102
103 Intent intent = new Intent(this, TimetableList.class);
104 intent.putExtra("departure", dep);
105
106 startActivity(intent);
107
108 }
109
110
111 @Override
112 protected void onPrepareDialog(int id, Dialog dialog) {
113 super.onPrepareDialog(id, dialog);
114
115 switch (id) {
116 case DLG_PROGRESS:
117 pgDialog = (ProgressDialog) dialog;
118 break;
119 }
120 }
121
122 @Override
123 protected Dialog onCreateDialog(int id) {
124 switch (id) {
125 case DLG_PROGRESS:
126 ProgressDialog dlg = new ProgressDialog(this);
127 dlg.setMessage("Fetch departure data");
128 dlg.setCancelable(true);
129 return dlg;
130 default:
131 return super.onCreateDialog(id);
132 }
133 }
134
135 void startDepartureFetcher() {
136 showDialog(DLG_PROGRESS);
137 fetcher = new DepartureFetcher();
138 fetcher.execute(stationId);
139 }
140
141 class DialogDismisser implements View.OnClickListener {
142
143 Dialog dlg;
144 public DialogDismisser(Dialog d) {
145 dlg = d;
146 }
147
148 @Override
149 public void onClick(View v) {
150 if (dlg.isShowing())
151 dlg.dismiss();
152 }
153 }
154
155 View.OnClickListener mapLauncher = new View.OnClickListener() {
156 @Override
157 public void onClick(View v) {
158 Uri uri = Uri.parse("geo:" + latitude + "," + longitude);
159 startActivity( new Intent(Intent.ACTION_VIEW, uri));
160 }
161 };
162
163
164
165 class DepartureFetcher extends AsyncTask<Integer, Void, Void> {
166
167 boolean success;
168
169 @Override
170 protected void onPostExecute(Void result) {
171 super.onPostExecute(result);
172
173
174 pgDialog.dismiss();
175
176 if (success) {
177 adapter.setDepartures(departures);
178 if (departures.size() == 0) {
179 MessageBox.showMessage(DepartureList.this, "No departures found");
180 }
181 } else { // communication or parse error
182 AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this);
183 builder.setMessage("Error finding departures");
184 builder.setCancelable(true);
185 builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
186 public void onClick(DialogInterface dialog, int id) {
187 dialog.dismiss();
188 startDepartureFetcher();
189
190 }
191 });
192 builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
193 public void onClick(DialogInterface dialog, int id) {
194 dialog.dismiss();
195 }
196 });
197 builder.show();
198 }
199 }
200
201 @Override
202 protected Void doInBackground(Integer... params) {
203 success = provider.lookupDepartures(params[0]);
204 departures = provider.getDepartures(params[0]);
205 return null;
206 }
207
208 }
209 }

  ViewVC Help
Powered by ViewVC 1.1.20