1 |
package dk.thoerup.traininfo; |
package dk.thoerup.traininfo; |
2 |
|
|
3 |
import java.text.NumberFormat; |
import java.text.NumberFormat; |
4 |
|
import java.util.ArrayList; |
5 |
import java.util.List; |
import java.util.List; |
6 |
|
import java.util.Locale; |
7 |
|
|
8 |
|
import android.app.AlertDialog; |
9 |
import android.app.Dialog; |
import android.app.Dialog; |
10 |
import android.app.ListActivity; |
import android.app.ListActivity; |
11 |
import android.app.ProgressDialog; |
import android.app.ProgressDialog; |
12 |
|
import android.content.DialogInterface; |
13 |
import android.content.Intent; |
import android.content.Intent; |
14 |
|
import android.location.Address; |
15 |
|
import android.location.Geocoder; |
16 |
import android.net.Uri; |
import android.net.Uri; |
17 |
import android.os.AsyncTask; |
import android.os.AsyncTask; |
18 |
import android.os.Bundle; |
import android.os.Bundle; |
19 |
|
import android.util.Log; |
20 |
import android.view.View; |
import android.view.View; |
21 |
import android.widget.ListView; |
import android.widget.ListView; |
22 |
import android.widget.TextView; |
import android.widget.TextView; |
|
import android.widget.Toast; |
|
|
import dk.thoerup.traininfo.provider.ProviderFactory; |
|
23 |
import dk.thoerup.traininfo.provider.DepartureProvider; |
import dk.thoerup.traininfo.provider.DepartureProvider; |
24 |
|
import dk.thoerup.traininfo.provider.ProviderFactory; |
25 |
import dk.thoerup.traininfo.util.MessageBox; |
import dk.thoerup.traininfo.util.MessageBox; |
26 |
|
|
27 |
public class DepartureList extends ListActivity { |
public class DepartureList extends ListActivity { |
33 |
DepartureProvider provider; |
DepartureProvider provider; |
34 |
List<DepartureBean> departures; |
List<DepartureBean> departures; |
35 |
|
|
36 |
static int itemId; |
int selectedItemId; |
37 |
static DepartureBean currentDeparture; |
//DepartureBean currentDeparture; |
38 |
|
|
39 |
ProgressDialog pgDialog; |
ProgressDialog pgDialog; |
40 |
|
Dialog detailsDialog; |
41 |
DepartureFetcher fetcher; |
DepartureFetcher fetcher; |
42 |
|
int stationId; |
43 |
|
|
44 |
double latitude,longitude; |
double latitude,longitude; |
45 |
|
|
46 |
|
@SuppressWarnings("unchecked") |
47 |
@Override |
@Override |
48 |
protected void onCreate(Bundle savedInstanceState) { |
protected void onCreate(Bundle savedInstanceState) { |
49 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
59 |
|
|
60 |
String name = launchedBy.getStringExtra("name"); |
String name = launchedBy.getStringExtra("name"); |
61 |
((TextView) findViewById(R.id.stationName)).setText( name ); |
((TextView) findViewById(R.id.stationName)).setText( name ); |
62 |
|
|
63 |
|
String address = launchedBy.getStringExtra("address"); |
64 |
|
((TextView) findViewById(R.id.stationAddr)).setText( address ); |
65 |
|
|
66 |
String addr = launchedBy.getStringExtra("address"); |
stationId = launchedBy.getIntExtra("stationid", -1); |
|
((TextView) findViewById(R.id.stationAddr)).setText( addr ); |
|
67 |
|
|
68 |
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
69 |
|
|
74 |
((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); |
((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); |
75 |
|
|
76 |
|
|
|
showDialog(DLG_PROGRESS); |
|
77 |
provider = ProviderFactory.getDepartureProvider(); |
provider = ProviderFactory.getDepartureProvider(); |
78 |
|
|
79 |
fetcher = new DepartureFetcher(); |
if (savedInstanceState == null) { |
80 |
fetcher.execute(name); |
startDepartureFetcher(); |
81 |
|
} else { |
82 |
|
departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures"); |
83 |
|
adapter.setDepartures(departures); |
84 |
|
selectedItemId = savedInstanceState.getInt("selectedItemId"); |
85 |
|
boolean detailsShowing = savedInstanceState.getBoolean("detailsShowing"); |
86 |
|
if (detailsShowing) |
87 |
|
showDialog(DLG_DETAILS); |
88 |
|
} |
89 |
} |
} |
90 |
|
|
91 |
@Override |
@Override |
92 |
public void onSaveInstanceState(Bundle outState) |
public void onSaveInstanceState(Bundle outState) |
93 |
{ |
{ |
94 |
if (pgDialog.isShowing()) |
if (pgDialog != null && pgDialog.isShowing()) |
95 |
pgDialog.dismiss(); |
dismissDialog(DLG_PROGRESS); |
96 |
|
boolean detailsShowing = (detailsDialog != null && detailsDialog.isShowing()); |
97 |
|
if (detailsShowing) { |
98 |
|
dismissDialog(DLG_DETAILS); |
99 |
|
} |
100 |
|
outState.putBoolean("detailsShowing", detailsShowing); |
101 |
|
outState.putInt("selectedItemId", selectedItemId); |
102 |
|
|
103 |
|
outState.putSerializable("departures", (ArrayList<DepartureBean>) departures); |
104 |
} |
} |
105 |
|
|
106 |
@Override |
@Override |
107 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
protected void onListItemClick(ListView l, View v, int position, long id) { |
108 |
super.onListItemClick(l, v, position, id); |
super.onListItemClick(l, v, position, id); |
109 |
|
|
110 |
currentDeparture = departures.get(position); |
selectedItemId = position; |
111 |
showDialog(DLG_DETAILS); |
showDialog(DLG_DETAILS); |
112 |
} |
} |
113 |
|
|
115 |
@Override |
@Override |
116 |
protected void onPrepareDialog(int id, Dialog dialog) { |
protected void onPrepareDialog(int id, Dialog dialog) { |
117 |
super.onPrepareDialog(id, dialog); |
super.onPrepareDialog(id, dialog); |
118 |
|
|
119 |
switch (id) { |
switch (id) { |
120 |
case DLG_DETAILS: |
case DLG_DETAILS: |
121 |
|
DepartureBean currentDeparture = departures.get(selectedItemId); |
122 |
((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime()); |
((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime()); |
123 |
((TextView)dialog.findViewById(R.id.Train)).setText(currentDeparture.getTrainNumber()); |
((TextView)dialog.findViewById(R.id.Train)).setText(currentDeparture.getTrainNumber()); |
124 |
((TextView)dialog.findViewById(R.id.Destination)).setText( currentDeparture.getDestination()); |
((TextView)dialog.findViewById(R.id.Destination)).setText( currentDeparture.getDestination()); |
127 |
((TextView)dialog.findViewById(R.id.Updated)).setText(currentDeparture.getLastUpdateString()); |
((TextView)dialog.findViewById(R.id.Updated)).setText(currentDeparture.getLastUpdateString()); |
128 |
((TextView)dialog.findViewById(R.id.Status)).setText(currentDeparture.getStatus()); |
((TextView)dialog.findViewById(R.id.Status)).setText(currentDeparture.getStatus()); |
129 |
((TextView)dialog.findViewById(R.id.Note)).setText(currentDeparture.getNote()); |
((TextView)dialog.findViewById(R.id.Note)).setText(currentDeparture.getNote()); |
130 |
|
detailsDialog = dialog; |
131 |
break; |
break; |
132 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
133 |
pgDialog = (ProgressDialog) dialog; |
pgDialog = (ProgressDialog) dialog; |
159 |
} |
} |
160 |
} |
} |
161 |
|
|
162 |
|
void startDepartureFetcher() { |
163 |
|
showDialog(DLG_PROGRESS); |
164 |
|
fetcher = new DepartureFetcher(); |
165 |
|
fetcher.execute(stationId); |
166 |
|
} |
167 |
|
|
168 |
class DialogDismisser implements View.OnClickListener { |
class DialogDismisser implements View.OnClickListener { |
169 |
|
|
170 |
Dialog dlg; |
Dialog dlg; |
177 |
if (dlg.isShowing()) |
if (dlg.isShowing()) |
178 |
dlg.dismiss(); |
dlg.dismiss(); |
179 |
} |
} |
180 |
} |
|
181 |
|
|
182 |
|
} |
183 |
|
|
184 |
View.OnClickListener mapLauncher = new View.OnClickListener() { |
View.OnClickListener mapLauncher = new View.OnClickListener() { |
185 |
@Override |
@Override |
188 |
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
189 |
} |
} |
190 |
}; |
}; |
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
class DepartureFetcher extends AsyncTask<Integer, Void, Void> { |
|
class DepartureFetcher extends AsyncTask<String, Void, Void> { |
|
195 |
|
|
196 |
|
boolean success; |
197 |
|
String addr; |
198 |
@Override |
@Override |
199 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
200 |
super.onPostExecute(result); |
super.onPostExecute(result); |
201 |
|
|
202 |
adapter.setDepartures(departures); |
|
203 |
pgDialog.dismiss(); |
pgDialog.dismiss(); |
204 |
|
|
205 |
if (departures.size() == 0) |
if (success) { |
206 |
MessageBox.showMessage(DepartureList.this, "No departures found"); |
adapter.setDepartures(departures); |
207 |
|
if (departures.size() == 0) { |
208 |
|
MessageBox.showMessage(DepartureList.this, "No departures found"); |
209 |
|
} |
210 |
|
} else { // communication or parse error |
211 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
212 |
|
builder.setMessage("Error finding departures"); |
213 |
|
builder.setCancelable(true); |
214 |
|
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
215 |
|
public void onClick(DialogInterface dialog, int id) { |
216 |
|
dialog.dismiss(); |
217 |
|
startDepartureFetcher(); |
218 |
|
|
219 |
|
} |
220 |
|
}); |
221 |
|
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
222 |
|
public void onClick(DialogInterface dialog, int id) { |
223 |
|
dialog.dismiss(); |
224 |
|
} |
225 |
|
}); |
226 |
|
builder.show(); |
227 |
|
} |
228 |
} |
} |
229 |
|
|
230 |
@Override |
@Override |
231 |
protected Void doInBackground(String... params) { |
protected Void doInBackground(Integer... params) { |
232 |
provider.lookupDepartures(params[0]); |
success = provider.lookupDepartures(params[0]); |
233 |
departures = provider.getDepartures(); |
departures = provider.getDepartures(params[0]); |
234 |
return null; |
return null; |
235 |
} |
} |
236 |
|
|