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 dk.thoerup.traininfo.provider.DepartureProvider;
|
17 |
import dk.thoerup.traininfo.provider.ProviderFactory;
|
18 |
import dk.thoerup.traininfo.util.MessageBox;
|
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 = ProviderFactory.getDepartureProvider();
|
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.Train)).setText(currentDeparture.getTrainNumber());
|
97 |
((TextView)dialog.findViewById(R.id.Destination)).setText( currentDeparture.getDestination());
|
98 |
((TextView)dialog.findViewById(R.id.Origin)).setText(currentDeparture.getOrigin());
|
99 |
((TextView)dialog.findViewById(R.id.Location)).setText(currentDeparture.getLocation());
|
100 |
((TextView)dialog.findViewById(R.id.Updated)).setText(currentDeparture.getLastUpdateString());
|
101 |
((TextView)dialog.findViewById(R.id.Status)).setText(currentDeparture.getStatus());
|
102 |
((TextView)dialog.findViewById(R.id.Note)).setText(currentDeparture.getNote());
|
103 |
break;
|
104 |
case DLG_PROGRESS:
|
105 |
pgDialog = (ProgressDialog) dialog;
|
106 |
break;
|
107 |
}
|
108 |
}
|
109 |
|
110 |
@Override
|
111 |
protected Dialog onCreateDialog(int id) {
|
112 |
switch (id) {
|
113 |
case DLG_PROGRESS:
|
114 |
ProgressDialog dlg = new ProgressDialog(this);
|
115 |
dlg.setMessage("Fetch departure data");
|
116 |
dlg.setCancelable(true);
|
117 |
return dlg;
|
118 |
case DLG_DETAILS:
|
119 |
//Context mContext = getApplicationContext();
|
120 |
Dialog dialog = new Dialog(this);
|
121 |
dialog.setCancelable(true);
|
122 |
|
123 |
dialog.setContentView(R.layout.departuredetails);
|
124 |
dialog.setTitle("Departure details");
|
125 |
|
126 |
View root = dialog.findViewById(R.id.layout_root);
|
127 |
root.setOnClickListener( new DialogDismisser(dialog) );
|
128 |
return dialog;
|
129 |
default:
|
130 |
return super.onCreateDialog(id);
|
131 |
}
|
132 |
}
|
133 |
|
134 |
class DialogDismisser implements View.OnClickListener {
|
135 |
|
136 |
Dialog dlg;
|
137 |
public DialogDismisser(Dialog d) {
|
138 |
dlg = d;
|
139 |
}
|
140 |
|
141 |
@Override
|
142 |
public void onClick(View v) {
|
143 |
if (dlg.isShowing())
|
144 |
dlg.dismiss();
|
145 |
}
|
146 |
}
|
147 |
|
148 |
View.OnClickListener mapLauncher = new View.OnClickListener() {
|
149 |
@Override
|
150 |
public void onClick(View v) {
|
151 |
Uri uri = Uri.parse("geo:" + latitude + "," + longitude);
|
152 |
startActivity( new Intent(Intent.ACTION_VIEW, uri));
|
153 |
}
|
154 |
};
|
155 |
|
156 |
|
157 |
class DepartureFetcher extends AsyncTask<String, Void, Void> {
|
158 |
|
159 |
@Override
|
160 |
protected void onPostExecute(Void result) {
|
161 |
super.onPostExecute(result);
|
162 |
|
163 |
adapter.setDepartures(departures);
|
164 |
pgDialog.dismiss();
|
165 |
|
166 |
if (departures.size() == 0)
|
167 |
MessageBox.showMessage(DepartureList.this, "No departures found");
|
168 |
}
|
169 |
|
170 |
@Override
|
171 |
protected Void doInBackground(String... params) {
|
172 |
provider.lookupDepartures(params[0]);
|
173 |
departures = provider.getDepartures();
|
174 |
return null;
|
175 |
}
|
176 |
|
177 |
}
|
178 |
}
|