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

  ViewVC Help
Powered by ViewVC 1.1.20