5 |
import java.util.List; |
import java.util.List; |
6 |
import java.util.Locale; |
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; |
import android.location.Address; |
15 |
import android.location.Geocoder; |
import android.location.Geocoder; |
27 |
public class DepartureList extends ListActivity { |
public class DepartureList extends ListActivity { |
28 |
|
|
29 |
public static final int DLG_PROGRESS = 1; |
public static final int DLG_PROGRESS = 1; |
30 |
public static final int DLG_DETAILS = 2; |
|
31 |
|
|
32 |
DepartureListAdapter adapter; |
DepartureListAdapter adapter; |
33 |
DepartureProvider provider; |
DepartureProvider provider; |
37 |
//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 |
|
|
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 |
((TextView) findViewById(R.id.stationAddr)).setText( "searching..." ); |
stationId = launchedBy.getIntExtra("stationid", -1); |
|
|
|
|
int stationId = launchedBy.getIntExtra("stationid", -1); |
|
67 |
|
|
68 |
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
69 |
|
|
76 |
|
|
77 |
provider = ProviderFactory.getDepartureProvider(); |
provider = ProviderFactory.getDepartureProvider(); |
78 |
|
|
|
fetcher = new DepartureFetcher(); |
|
79 |
if (savedInstanceState == null) { |
if (savedInstanceState == null) { |
80 |
showDialog(DLG_PROGRESS); |
startDepartureFetcher(); |
|
fetcher.execute(stationId); |
|
81 |
} else { |
} else { |
82 |
departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures"); |
departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures"); |
83 |
adapter.setDepartures(departures); |
adapter.setDepartures(departures); |
84 |
selectedItemId = savedInstanceState.getInt("selectedItemId"); |
selectedItemId = savedInstanceState.getInt("selectedItemId"); |
|
boolean detailsShowing = savedInstanceState.getBoolean("detailsShowing"); |
|
|
if (detailsShowing) |
|
|
showDialog(DLG_DETAILS); |
|
85 |
} |
} |
86 |
} |
} |
87 |
|
|
90 |
{ |
{ |
91 |
if (pgDialog != null && pgDialog.isShowing()) |
if (pgDialog != null && pgDialog.isShowing()) |
92 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
93 |
boolean detailsShowing = (detailsDialog != null && detailsDialog.isShowing()); |
|
|
if (detailsShowing) { |
|
|
dismissDialog(DLG_DETAILS); |
|
|
} |
|
|
outState.putBoolean("detailsShowing", detailsShowing); |
|
94 |
outState.putInt("selectedItemId", selectedItemId); |
outState.putInt("selectedItemId", selectedItemId); |
95 |
|
|
96 |
outState.putSerializable("departures", (ArrayList<DepartureBean>) departures); |
outState.putSerializable("departures", (ArrayList<DepartureBean>) departures); |
99 |
@Override |
@Override |
100 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
protected void onListItemClick(ListView l, View v, int position, long id) { |
101 |
super.onListItemClick(l, v, position, id); |
super.onListItemClick(l, v, position, id); |
102 |
|
|
103 |
|
selectedItemId = position; |
104 |
|
|
105 |
|
DepartureBean dep = departures.get(selectedItemId); |
106 |
|
|
107 |
selectedItemId = position; |
Intent intent = new Intent(this, TimetableList.class); |
108 |
showDialog(DLG_DETAILS); |
intent.putExtra("departure", dep); |
109 |
|
|
110 |
|
startActivity(intent); |
111 |
|
|
112 |
} |
} |
113 |
|
|
114 |
|
|
117 |
super.onPrepareDialog(id, dialog); |
super.onPrepareDialog(id, dialog); |
118 |
|
|
119 |
switch (id) { |
switch (id) { |
|
case DLG_DETAILS: |
|
|
DepartureBean currentDeparture = departures.get(selectedItemId); |
|
|
((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()); |
|
|
detailsDialog = dialog; |
|
|
break; |
|
120 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
121 |
pgDialog = (ProgressDialog) dialog; |
pgDialog = (ProgressDialog) dialog; |
122 |
break; |
break; |
130 |
ProgressDialog dlg = new ProgressDialog(this); |
ProgressDialog dlg = new ProgressDialog(this); |
131 |
dlg.setMessage("Fetch departure data"); |
dlg.setMessage("Fetch departure data"); |
132 |
dlg.setCancelable(true); |
dlg.setCancelable(true); |
133 |
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; |
|
134 |
default: |
default: |
135 |
return super.onCreateDialog(id); |
return super.onCreateDialog(id); |
136 |
} |
} |
137 |
} |
} |
138 |
|
|
139 |
String lookupAddress(double latitude, double longitude) { |
void startDepartureFetcher() { |
140 |
|
showDialog(DLG_PROGRESS); |
141 |
Geocoder coder = new Geocoder(this, new Locale("da")); |
fetcher = new DepartureFetcher(); |
142 |
StringBuilder sb = new StringBuilder(); |
fetcher.execute(stationId); |
|
Log.i("lookupaddr", "" + latitude + "/" + longitude); |
|
|
try { |
|
|
List<Address> addressList = coder.getFromLocation(latitude, longitude, 1); |
|
|
Address addr = addressList.get(0); |
|
|
|
|
|
|
|
|
int max = addr.getMaxAddressLineIndex(); |
|
|
for (int i=0; i<max; i++) { |
|
|
if (i>0) |
|
|
sb.append(", "); |
|
|
|
|
|
sb.append(addr.getAddressLine(i)); |
|
|
} |
|
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
Log.e("DepartureList", "geocoder failed", e); |
|
|
} |
|
|
|
|
|
return sb.toString(); |
|
143 |
} |
} |
144 |
|
|
145 |
class DialogDismisser implements View.OnClickListener { |
class DialogDismisser implements View.OnClickListener { |
154 |
if (dlg.isShowing()) |
if (dlg.isShowing()) |
155 |
dlg.dismiss(); |
dlg.dismiss(); |
156 |
} |
} |
157 |
} |
|
158 |
|
|
159 |
|
} |
160 |
|
|
161 |
View.OnClickListener mapLauncher = new View.OnClickListener() { |
View.OnClickListener mapLauncher = new View.OnClickListener() { |
162 |
@Override |
@Override |
165 |
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
166 |
} |
} |
167 |
}; |
}; |
168 |
|
|
169 |
|
|
170 |
|
|
171 |
class DepartureFetcher extends AsyncTask<Integer, Void, Void> { |
class DepartureFetcher extends AsyncTask<Integer, Void, Void> { |
172 |
|
|
173 |
|
boolean success; |
174 |
String addr; |
String addr; |
175 |
@Override |
@Override |
176 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
177 |
super.onPostExecute(result); |
super.onPostExecute(result); |
178 |
|
|
|
adapter.setDepartures(departures); |
|
|
pgDialog.dismiss(); |
|
179 |
|
|
180 |
|
pgDialog.dismiss(); |
181 |
|
|
182 |
((TextView) findViewById(R.id.stationAddr)).setText( addr ); |
if (success) { |
183 |
|
adapter.setDepartures(departures); |
184 |
if (departures.size() == 0) |
if (departures.size() == 0) { |
185 |
MessageBox.showMessage(DepartureList.this, "No departures found"); |
MessageBox.showMessage(DepartureList.this, "No departures found"); |
186 |
|
} |
187 |
|
} else { // communication or parse error |
188 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
189 |
|
builder.setMessage("Error finding departures"); |
190 |
|
builder.setCancelable(true); |
191 |
|
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
192 |
|
public void onClick(DialogInterface dialog, int id) { |
193 |
|
dialog.dismiss(); |
194 |
|
startDepartureFetcher(); |
195 |
|
|
196 |
|
} |
197 |
|
}); |
198 |
|
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
199 |
|
public void onClick(DialogInterface dialog, int id) { |
200 |
|
dialog.dismiss(); |
201 |
|
} |
202 |
|
}); |
203 |
|
builder.show(); |
204 |
|
} |
205 |
} |
} |
206 |
|
|
207 |
@Override |
@Override |
208 |
protected Void doInBackground(Integer... params) { |
protected Void doInBackground(Integer... params) { |
209 |
addr = lookupAddress( latitude , longitude); |
success = provider.lookupDepartures(params[0]); |
210 |
provider.lookupDepartures(params[0]); |
departures = provider.getDepartures(params[0]); |
|
departures = provider.getDepartures(); |
|
211 |
return null; |
return null; |
212 |
} |
} |
213 |
|
|