/[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 243 - (show annotations) (download)
Sun Aug 9 12:12:33 2009 UTC (14 years, 9 months ago) by torben
File size: 5056 byte(s)
Make the activities behave better if the device is rotated while it is working
1 package dk.thoerup.traininfo;
2
3 import java.text.NumberFormat;
4 import java.util.List;
5
6 import android.app.Dialog;
7 import android.app.ListActivity;
8 import android.app.ProgressDialog;
9 import android.content.Intent;
10 import android.net.Uri;
11 import android.os.AsyncTask;
12 import android.os.Bundle;
13 import android.view.View;
14 import android.widget.ListView;
15 import android.widget.TextView;
16 import android.widget.Toast;
17 import dk.thoerup.traininfo.provider.DepartureFactory;
18 import dk.thoerup.traininfo.provider.DepartureProvider;
19
20 public class DepartureList extends ListActivity {
21
22 public static final int DLG_PROGRESS = 1;
23 public static final int DLG_DETAILS = 2;
24
25 DepartureListAdapter adapter;
26 DepartureProvider provider;
27 List<DepartureBean> departures;
28
29 static int itemId;
30 static DepartureBean currentDeparture;
31
32 ProgressDialog pgDialog;
33
34 DepartureFetcher fetcher;
35
36 double latitude,longitude;
37
38 @Override
39 protected void onCreate(Bundle savedInstanceState) {
40 super.onCreate(savedInstanceState);
41 setContentView(R.layout.departurelist);
42
43 adapter = new DepartureListAdapter(this);
44 setListAdapter(adapter);
45
46 Intent launchedBy = getIntent();
47
48 latitude = launchedBy.getDoubleExtra("latitude", 0.0);
49 longitude = launchedBy.getDoubleExtra("longitude", 0.0);
50
51 String name = launchedBy.getStringExtra("name");
52 ((TextView) findViewById(R.id.stationName)).setText( name );
53
54 String addr = launchedBy.getStringExtra("address");
55 ((TextView) findViewById(R.id.stationAddr)).setText( addr );
56
57 findViewById(R.id.header).setOnClickListener( mapLauncher );
58
59 NumberFormat format = NumberFormat.getNumberInstance();
60 format.setMaximumFractionDigits(1);
61 format.setMinimumFractionDigits(1);
62 int distance = launchedBy.getIntExtra("distance", 0);
63 ((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." );
64
65
66 showDialog(DLG_PROGRESS);
67 provider = DepartureFactory.getProvider();
68
69 fetcher = new DepartureFetcher();
70 fetcher.execute(name);
71 }
72
73 @Override
74 public void onSaveInstanceState(Bundle outState)
75 {
76 if (pgDialog.isShowing())
77 pgDialog.dismiss();
78 }
79
80 @Override
81 protected void onListItemClick(ListView l, View v, int position, long id) {
82 super.onListItemClick(l, v, position, id);
83
84 currentDeparture = departures.get(position);
85 showDialog(DLG_DETAILS);
86 }
87
88
89 @Override
90 protected void onPrepareDialog(int id, Dialog dialog) {
91 super.onPrepareDialog(id, dialog);
92
93 switch (id) {
94 case DLG_DETAILS:
95 ((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime());
96 ((TextView)dialog.findViewById(R.id.Destination)).setText( currentDeparture.getDestination());
97 ((TextView)dialog.findViewById(R.id.Origin)).setText(currentDeparture.getOrigin());
98 ((TextView)dialog.findViewById(R.id.Location)).setText(currentDeparture.getLocation());
99 ((TextView)dialog.findViewById(R.id.Updated)).setText(currentDeparture.getLastUpdateString());
100 ((TextView)dialog.findViewById(R.id.Status)).setText(currentDeparture.getStatus());
101 ((TextView)dialog.findViewById(R.id.Note)).setText(currentDeparture.getNote());
102 break;
103 case DLG_PROGRESS:
104 pgDialog = (ProgressDialog) dialog;
105 break;
106 }
107 }
108
109 @Override
110 protected Dialog onCreateDialog(int id) {
111 switch (id) {
112 case DLG_PROGRESS:
113 ProgressDialog dlg = new ProgressDialog(this);
114 dlg.setMessage("Fetch departure data");
115 dlg.setCancelable(true);
116 return dlg;
117 case DLG_DETAILS:
118 //Context mContext = getApplicationContext();
119 Dialog dialog = new Dialog(this);
120 dialog.setCancelable(true);
121
122 dialog.setContentView(R.layout.departuredetails);
123 dialog.setTitle("Departure details");
124
125 View root = dialog.findViewById(R.id.layout_root);
126 root.setOnClickListener( new DialogDismisser(dialog) );
127 return dialog;
128 default:
129 return super.onCreateDialog(id);
130 }
131 }
132
133 class DialogDismisser implements View.OnClickListener {
134
135 Dialog dlg;
136 public DialogDismisser(Dialog d) {
137 dlg = d;
138 }
139
140 @Override
141 public void onClick(View v) {
142 if (dlg.isShowing())
143 dlg.dismiss();
144 }
145 }
146
147 View.OnClickListener mapLauncher = new View.OnClickListener() {
148 @Override
149 public void onClick(View v) {
150 Uri uri = Uri.parse("geo:" + latitude + "," + longitude);
151 startActivity( new Intent(Intent.ACTION_VIEW, uri));
152 }
153 };
154
155
156 class DepartureFetcher extends AsyncTask<String, Void, Void> {
157
158 @Override
159 protected void onPostExecute(Void result) {
160 super.onPostExecute(result);
161
162 adapter.setDepartures(departures);
163 pgDialog.dismiss();
164
165 if (departures.size() == 0)
166 Toast.makeText(DepartureList.this, "No departures found", Toast.LENGTH_LONG);
167 }
168
169 @Override
170 protected Void doInBackground(String... params) {
171 provider.lookupDepartures(params[0]);
172 departures = provider.getDepartures();
173 return null;
174 }
175
176 }
177 }

  ViewVC Help
Powered by ViewVC 1.1.20