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