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