/[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 742 - (show annotations) (download)
Wed May 19 16:27:41 2010 UTC (14 years ago) by torben
File size: 6113 byte(s)
Don't show distance to station, unless we really have one
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.util.Log;
17 import android.view.View;
18 import android.widget.ListView;
19 import android.widget.TextView;
20 import dk.thoerup.traininfo.provider.DepartureProvider;
21 import dk.thoerup.traininfo.provider.ProviderFactory;
22 import dk.thoerup.traininfo.util.MessageBox;
23 import static dk.thoerup.traininfo.R.string.*;
24
25 public class DepartureList extends ListActivity {
26
27 public static final int DLG_PROGRESS = 1;
28
29
30 DepartureListAdapter adapter;
31 DepartureProvider provider;
32 List<DepartureBean> departures;
33
34 int selectedItemId;
35 //DepartureBean currentDeparture;
36
37 ProgressDialog pgDialog;
38
39 DepartureFetcher fetcher;
40
41 StationBean station;
42
43 @SuppressWarnings("unchecked")
44 @Override
45 protected void onCreate(Bundle savedInstanceState) {
46 super.onCreate(savedInstanceState);
47 setContentView(R.layout.departurelist);
48
49 adapter = new DepartureListAdapter(this);
50 setListAdapter(adapter);
51
52 Intent launchedBy = getIntent();
53
54 station = (StationBean) launchedBy.getSerializableExtra("stationbean");
55
56 ((TextView) findViewById(R.id.stationName)).setText( station.getName() );
57
58
59 ((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() );
60
61
62 findViewById(R.id.header).setOnClickListener( mapLauncher );
63
64 int distance = station.getDistance();
65 if (distance != 0) {
66 NumberFormat format = NumberFormat.getNumberInstance();
67 format.setMaximumFractionDigits(1);
68 format.setMinimumFractionDigits(1);
69
70 ((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." );
71 } else {
72 ((TextView) findViewById(R.id.stationDistance)).setVisibility(View.GONE);
73 }
74
75
76 if (station.isRegional() == false && station.isSTrain() == false) {
77 getListView().setVisibility( View.GONE );
78 findViewById(R.id.metroonly).setVisibility( View.VISIBLE );
79
80 } else {
81 provider = ProviderFactory.getDepartureProvider();
82
83 if (savedInstanceState == null) {
84 startDepartureFetcher();
85 } else {
86 departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures");
87 adapter.setDepartures(departures);
88 selectedItemId = savedInstanceState.getInt("selectedItemId");
89 }
90 }
91 }
92
93 @Override
94 public void onSaveInstanceState(Bundle outState)
95 {
96 if (pgDialog != null && pgDialog.isShowing())
97 dismissDialog(DLG_PROGRESS);
98
99 outState.putInt("selectedItemId", selectedItemId);
100
101 outState.putSerializable("departures", (ArrayList<DepartureBean>) departures);
102 }
103
104 @Override
105 protected void onListItemClick(ListView l, View v, int position, long id) {
106 super.onListItemClick(l, v, position, id);
107
108 selectedItemId = position;
109
110 DepartureBean dep = departures.get(selectedItemId);
111
112 Intent intent = new Intent(this, TimetableList.class);
113 intent.putExtra("departure", dep);
114
115 startActivity(intent);
116
117 }
118
119
120 @Override
121 protected void onPrepareDialog(int id, Dialog dialog) {
122 super.onPrepareDialog(id, dialog);
123
124 switch (id) {
125 case DLG_PROGRESS:
126 pgDialog = (ProgressDialog) dialog;
127 break;
128 }
129 }
130
131 @Override
132 protected Dialog onCreateDialog(int id) {
133 switch (id) {
134 case DLG_PROGRESS:
135 ProgressDialog dlg = new ProgressDialog(this);
136 dlg.setMessage( getString(departurelist_fetchdata) );
137 dlg.setCancelable(true);
138 return dlg;
139 default:
140 return super.onCreateDialog(id);
141 }
142 }
143
144 void startDepartureFetcher() {
145 showDialog(DLG_PROGRESS);
146 fetcher = new DepartureFetcher();
147 fetcher.execute(station.getId());
148 }
149
150 class DialogDismisser implements View.OnClickListener {
151
152 Dialog dlg;
153 public DialogDismisser(Dialog d) {
154 dlg = d;
155 }
156
157 @Override
158 public void onClick(View v) {
159 if (dlg.isShowing())
160 dlg.dismiss();
161 }
162 }
163
164 View.OnClickListener mapLauncher = new View.OnClickListener() {
165 @Override
166 public void onClick(View v) {
167 Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude());
168 startActivity( new Intent(Intent.ACTION_VIEW, uri));
169 }
170 };
171
172
173
174 class DepartureFetcher extends AsyncTask<Integer, Void, Void> {
175
176 boolean success;
177
178 @Override
179 protected void onPostExecute(Void result) {
180 super.onPostExecute(result);
181
182
183 pgDialog.dismiss();
184
185 if (success) {
186 adapter.setDepartures(departures);
187 if (departures.size() == 0) {
188 MessageBox.showMessage(DepartureList.this, "No departures found");
189 }
190 } else { // communication or parse error
191 AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this);
192 builder.setMessage("Error finding departures");
193 builder.setCancelable(true);
194 builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
195 public void onClick(DialogInterface dialog, int id) {
196 dialog.dismiss();
197 startDepartureFetcher();
198
199 }
200 });
201 builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
202 public void onClick(DialogInterface dialog, int id) {
203 dialog.dismiss();
204 }
205 });
206
207 try {
208 builder.show();
209 } catch (android.view.WindowManager.BadTokenException e) {
210 Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running
211 }
212 }
213 }
214
215 @Override
216 protected Void doInBackground(Integer... params) {
217 success = provider.lookupDepartures(params[0]);
218 departures = provider.getDepartures(params[0]);
219 return null;
220 }
221
222 }
223 }

  ViewVC Help
Powered by ViewVC 1.1.20