1 |
package dk.thoerup.traininfo; |
package dk.thoerup.traininfo; |
2 |
|
|
3 |
|
import static dk.thoerup.traininfo.R.string.departurelist_fetchdepartures; |
4 |
|
import static dk.thoerup.traininfo.R.string.departurelist_fetcharrivals; |
5 |
|
import static dk.thoerup.traininfo.R.string.generic_cancel; |
6 |
|
import static dk.thoerup.traininfo.R.string.generic_retry; |
7 |
|
|
8 |
|
|
9 |
import java.text.NumberFormat; |
import java.text.NumberFormat; |
10 |
|
import java.util.ArrayList; |
11 |
import java.util.List; |
import java.util.List; |
12 |
|
|
13 |
|
import android.app.AlertDialog; |
14 |
import android.app.Dialog; |
import android.app.Dialog; |
15 |
import android.app.ListActivity; |
import android.app.ListActivity; |
16 |
import android.app.ProgressDialog; |
import android.app.ProgressDialog; |
17 |
|
import android.content.DialogInterface; |
18 |
import android.content.Intent; |
import android.content.Intent; |
19 |
import android.net.Uri; |
import android.net.Uri; |
20 |
import android.os.AsyncTask; |
import android.os.AsyncTask; |
21 |
import android.os.Bundle; |
import android.os.Bundle; |
22 |
|
import android.util.Log; |
23 |
import android.view.View; |
import android.view.View; |
24 |
|
import android.view.View.OnClickListener; |
25 |
|
import android.widget.Button; |
26 |
import android.widget.ListView; |
import android.widget.ListView; |
27 |
import android.widget.TextView; |
import android.widget.TextView; |
28 |
import dk.thoerup.traininfo.provider.DepartureProvider; |
import dk.thoerup.traininfo.provider.DepartureProvider; |
32 |
public class DepartureList extends ListActivity { |
public class DepartureList extends ListActivity { |
33 |
|
|
34 |
public static final int DLG_PROGRESS = 1; |
public static final int DLG_PROGRESS = 1; |
35 |
public static final int DLG_DETAILS = 2; |
|
36 |
|
|
37 |
DepartureListAdapter adapter; |
DepartureListAdapter adapter; |
38 |
DepartureProvider provider; |
DepartureProvider provider; |
39 |
List<DepartureBean> departures; |
List<DepartureBean> departures; |
40 |
|
|
41 |
static int itemId; |
int selectedItemId; |
42 |
static DepartureBean currentDeparture; |
//DepartureBean currentDeparture; |
43 |
|
|
44 |
ProgressDialog pgDialog; |
ProgressDialog pgDialog; |
45 |
|
|
46 |
DepartureFetcher fetcher; |
DepartureFetcher fetcher; |
47 |
|
|
48 |
|
StationBean station; |
49 |
|
|
50 |
double latitude,longitude; |
boolean arrival = false; |
51 |
|
|
52 |
|
@SuppressWarnings("unchecked") |
53 |
@Override |
@Override |
54 |
protected void onCreate(Bundle savedInstanceState) { |
protected void onCreate(Bundle savedInstanceState) { |
55 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
59 |
setListAdapter(adapter); |
setListAdapter(adapter); |
60 |
|
|
61 |
Intent launchedBy = getIntent(); |
Intent launchedBy = getIntent(); |
62 |
|
|
63 |
|
station = (StationBean) launchedBy.getSerializableExtra("stationbean"); |
64 |
|
|
65 |
latitude = launchedBy.getDoubleExtra("latitude", 0.0); |
((TextView) findViewById(R.id.stationName)).setText( station.getName() ); |
66 |
longitude = launchedBy.getDoubleExtra("longitude", 0.0); |
|
67 |
|
|
68 |
|
((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() ); |
69 |
|
|
70 |
String name = launchedBy.getStringExtra("name"); |
final Button departureBtn = (Button) findViewById(R.id.departurebtn); |
71 |
((TextView) findViewById(R.id.stationName)).setText( name ); |
final Button arrivalBtn = (Button) findViewById(R.id.arrivalbtn); |
72 |
|
|
73 |
|
departureBtn.setOnClickListener( new OnClickListener() { |
74 |
|
@Override |
75 |
|
public void onClick(View arg0) { |
76 |
|
arrivalBtn.setBackgroundResource(R.drawable.custom_button); |
77 |
|
departureBtn.setBackgroundResource(R.drawable.custom_button_hilight); |
78 |
|
arrival = false; |
79 |
|
startDepartureFetcher(); |
80 |
|
} |
81 |
|
}); |
82 |
|
arrivalBtn.setOnClickListener( new OnClickListener() { |
83 |
|
@Override |
84 |
|
public void onClick(View arg0) { |
85 |
|
arrivalBtn.setBackgroundResource(R.drawable.custom_button_hilight); |
86 |
|
departureBtn.setBackgroundResource(R.drawable.custom_button); |
87 |
|
arrival = true; |
88 |
|
startDepartureFetcher(); |
89 |
|
} |
90 |
|
}); |
91 |
|
|
|
String addr = launchedBy.getStringExtra("address"); |
|
|
((TextView) findViewById(R.id.stationAddr)).setText( addr ); |
|
92 |
|
|
|
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
|
93 |
|
|
|
NumberFormat format = NumberFormat.getNumberInstance(); |
|
|
format.setMaximumFractionDigits(1); |
|
|
format.setMinimumFractionDigits(1); |
|
|
int distance = launchedBy.getIntExtra("distance", 0); |
|
|
((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); |
|
|
|
|
94 |
|
|
95 |
showDialog(DLG_PROGRESS); |
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
|
provider = ProviderFactory.getDepartureProvider(); |
|
96 |
|
|
97 |
fetcher = new DepartureFetcher(); |
int distance = station.getDistance(); |
98 |
fetcher.execute(name); |
if (distance != 0) { |
99 |
|
NumberFormat format = NumberFormat.getNumberInstance(); |
100 |
|
format.setMaximumFractionDigits(1); |
101 |
|
format.setMinimumFractionDigits(1); |
102 |
|
|
103 |
|
((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); |
104 |
|
} else { |
105 |
|
((TextView) findViewById(R.id.stationDistance)).setVisibility(View.GONE); |
106 |
|
} |
107 |
|
|
108 |
|
|
109 |
|
if (station.isRegional() == false && station.isSTrain() == false) { |
110 |
|
getListView().setVisibility( View.GONE ); |
111 |
|
findViewById(R.id.metroonly).setVisibility( View.VISIBLE ); |
112 |
|
departureBtn.setVisibility( View.GONE ); |
113 |
|
arrivalBtn.setVisibility(View.GONE); |
114 |
|
|
115 |
|
} else { |
116 |
|
provider = ProviderFactory.getDepartureProvider(); |
117 |
|
|
118 |
|
if (savedInstanceState == null) { |
119 |
|
startDepartureFetcher(); |
120 |
|
} else { |
121 |
|
departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures"); |
122 |
|
adapter.setDepartures(departures); |
123 |
|
selectedItemId = savedInstanceState.getInt("selectedItemId"); |
124 |
|
} |
125 |
|
} |
126 |
} |
} |
127 |
|
|
128 |
@Override |
@Override |
129 |
public void onSaveInstanceState(Bundle outState) |
public void onSaveInstanceState(Bundle outState) |
130 |
{ |
{ |
131 |
if (pgDialog.isShowing()) |
if (pgDialog != null && pgDialog.isShowing()) |
132 |
pgDialog.dismiss(); |
dismissDialog(DLG_PROGRESS); |
133 |
|
|
134 |
|
outState.putInt("selectedItemId", selectedItemId); |
135 |
|
|
136 |
|
outState.putSerializable("departures", (ArrayList<DepartureBean>) departures); |
137 |
} |
} |
138 |
|
|
139 |
@Override |
@Override |
140 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
protected void onListItemClick(ListView l, View v, int position, long id) { |
141 |
super.onListItemClick(l, v, position, id); |
super.onListItemClick(l, v, position, id); |
142 |
|
|
143 |
|
selectedItemId = position; |
144 |
|
|
145 |
|
DepartureBean dep = departures.get(selectedItemId); |
146 |
|
|
147 |
currentDeparture = departures.get(position); |
Intent intent = new Intent(this, TimetableList.class); |
148 |
showDialog(DLG_DETAILS); |
intent.putExtra("departure", dep); |
149 |
|
|
150 |
|
startActivity(intent); |
151 |
|
|
152 |
} |
} |
153 |
|
|
154 |
|
|
155 |
@Override |
@Override |
156 |
protected void onPrepareDialog(int id, Dialog dialog) { |
protected void onPrepareDialog(int id, Dialog dialog) { |
157 |
super.onPrepareDialog(id, dialog); |
super.onPrepareDialog(id, dialog); |
158 |
|
|
159 |
switch (id) { |
switch (id) { |
|
case DLG_DETAILS: |
|
|
((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime()); |
|
|
((TextView)dialog.findViewById(R.id.Train)).setText(currentDeparture.getTrainNumber()); |
|
|
((TextView)dialog.findViewById(R.id.Destination)).setText( currentDeparture.getDestination()); |
|
|
((TextView)dialog.findViewById(R.id.Origin)).setText(currentDeparture.getOrigin()); |
|
|
((TextView)dialog.findViewById(R.id.Location)).setText(currentDeparture.getLocation()); |
|
|
((TextView)dialog.findViewById(R.id.Updated)).setText(currentDeparture.getLastUpdateString()); |
|
|
((TextView)dialog.findViewById(R.id.Status)).setText(currentDeparture.getStatus()); |
|
|
((TextView)dialog.findViewById(R.id.Note)).setText(currentDeparture.getNote()); |
|
|
break; |
|
160 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
161 |
pgDialog = (ProgressDialog) dialog; |
pgDialog = (ProgressDialog) dialog; |
162 |
|
int messageId = arrival == false ? departurelist_fetchdepartures : departurelist_fetcharrivals; |
163 |
|
pgDialog.setMessage( getString(messageId) ); |
164 |
break; |
break; |
165 |
} |
} |
166 |
} |
} |
169 |
protected Dialog onCreateDialog(int id) { |
protected Dialog onCreateDialog(int id) { |
170 |
switch (id) { |
switch (id) { |
171 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
172 |
|
|
173 |
ProgressDialog dlg = new ProgressDialog(this); |
ProgressDialog dlg = new ProgressDialog(this); |
|
dlg.setMessage("Fetch departure data"); |
|
174 |
dlg.setCancelable(true); |
dlg.setCancelable(true); |
175 |
return dlg; |
return dlg; |
|
case DLG_DETAILS: |
|
|
//Context mContext = getApplicationContext(); |
|
|
Dialog dialog = new Dialog(this); |
|
|
dialog.setCancelable(true); |
|
|
|
|
|
dialog.setContentView(R.layout.departuredetails); |
|
|
dialog.setTitle("Departure details"); |
|
|
|
|
|
View root = dialog.findViewById(R.id.layout_root); |
|
|
root.setOnClickListener( new DialogDismisser(dialog) ); |
|
|
return dialog; |
|
176 |
default: |
default: |
177 |
return super.onCreateDialog(id); |
return super.onCreateDialog(id); |
178 |
} |
} |
179 |
} |
} |
180 |
|
|
181 |
|
void startDepartureFetcher() { |
182 |
|
showDialog(DLG_PROGRESS); |
183 |
|
fetcher = new DepartureFetcher(); |
184 |
|
fetcher.execute(station.getId()); |
185 |
|
} |
186 |
|
|
187 |
class DialogDismisser implements View.OnClickListener { |
class DialogDismisser implements View.OnClickListener { |
188 |
|
|
189 |
Dialog dlg; |
Dialog dlg; |
195 |
public void onClick(View v) { |
public void onClick(View v) { |
196 |
if (dlg.isShowing()) |
if (dlg.isShowing()) |
197 |
dlg.dismiss(); |
dlg.dismiss(); |
198 |
} |
} |
199 |
} |
} |
200 |
|
|
201 |
View.OnClickListener mapLauncher = new View.OnClickListener() { |
View.OnClickListener mapLauncher = new View.OnClickListener() { |
202 |
@Override |
@Override |
203 |
public void onClick(View v) { |
public void onClick(View v) { |
204 |
Uri uri = Uri.parse("geo:" + latitude + "," + longitude); |
Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude()); |
205 |
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
206 |
} |
} |
207 |
}; |
}; |
208 |
|
|
209 |
|
|
210 |
|
|
211 |
|
class DepartureFetcher extends AsyncTask<Integer, Void, Void> { |
212 |
class DepartureFetcher extends AsyncTask<String, Void, Void> { |
|
213 |
|
boolean success; |
214 |
|
|
215 |
@Override |
@Override |
216 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
217 |
super.onPostExecute(result); |
super.onPostExecute(result); |
218 |
|
|
219 |
adapter.setDepartures(departures); |
|
220 |
pgDialog.dismiss(); |
pgDialog.dismiss(); |
221 |
|
|
222 |
if (departures.size() == 0) |
if (success) { |
223 |
MessageBox.showMessage(DepartureList.this, "No departures found"); |
adapter.setDepartures(departures); |
224 |
|
if (departures.size() == 0) { |
225 |
|
MessageBox.showMessage(DepartureList.this, "No departures found"); |
226 |
|
} |
227 |
|
} else { // communication or parse error |
228 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
229 |
|
builder.setMessage("Error finding departures"); |
230 |
|
builder.setCancelable(true); |
231 |
|
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
232 |
|
public void onClick(DialogInterface dialog, int id) { |
233 |
|
dialog.dismiss(); |
234 |
|
startDepartureFetcher(); |
235 |
|
|
236 |
|
} |
237 |
|
}); |
238 |
|
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
239 |
|
public void onClick(DialogInterface dialog, int id) { |
240 |
|
dialog.dismiss(); |
241 |
|
} |
242 |
|
}); |
243 |
|
|
244 |
|
try { |
245 |
|
builder.show(); |
246 |
|
} catch (android.view.WindowManager.BadTokenException e) { |
247 |
|
Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running |
248 |
|
} |
249 |
|
} |
250 |
} |
} |
251 |
|
|
252 |
@Override |
@Override |
253 |
protected Void doInBackground(String... params) { |
protected Void doInBackground(Integer... params) { |
254 |
provider.lookupDepartures(params[0]); |
success = provider.lookupDepartures(params[0], DepartureList.this.arrival); |
255 |
departures = provider.getDepartures(); |
departures = provider.getDepartures(params[0], DepartureList.this.arrival); |
256 |
return null; |
return null; |
257 |
} |
} |
258 |
|
|