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

  ViewVC Help
Powered by ViewVC 1.1.20