4 |
import java.util.ArrayList; |
import java.util.ArrayList; |
5 |
import java.util.List; |
import java.util.List; |
6 |
|
|
7 |
|
import android.app.AlertDialog; |
8 |
import android.app.Dialog; |
import android.app.Dialog; |
9 |
import android.app.ListActivity; |
import android.app.ListActivity; |
10 |
import android.app.ProgressDialog; |
import android.app.ProgressDialog; |
11 |
|
import android.content.DialogInterface; |
12 |
import android.content.Intent; |
import android.content.Intent; |
13 |
import android.net.Uri; |
import android.net.Uri; |
14 |
import android.os.AsyncTask; |
import android.os.AsyncTask; |
15 |
import android.os.Bundle; |
import android.os.Bundle; |
16 |
|
import android.util.Log; |
17 |
import android.view.View; |
import android.view.View; |
18 |
import android.widget.ListView; |
import android.widget.ListView; |
19 |
import android.widget.TextView; |
import android.widget.TextView; |
24 |
public class DepartureList extends ListActivity { |
public class DepartureList extends ListActivity { |
25 |
|
|
26 |
public static final int DLG_PROGRESS = 1; |
public static final int DLG_PROGRESS = 1; |
27 |
public static final int DLG_DETAILS = 2; |
|
28 |
|
|
29 |
DepartureListAdapter adapter; |
DepartureListAdapter adapter; |
30 |
DepartureProvider provider; |
DepartureProvider provider; |
34 |
//DepartureBean currentDeparture; |
//DepartureBean currentDeparture; |
35 |
|
|
36 |
ProgressDialog pgDialog; |
ProgressDialog pgDialog; |
37 |
Dialog detailsDialog; |
|
38 |
DepartureFetcher fetcher; |
DepartureFetcher fetcher; |
39 |
|
int stationId; |
40 |
|
|
41 |
double latitude,longitude; |
double latitude,longitude; |
42 |
|
|
56 |
|
|
57 |
String name = launchedBy.getStringExtra("name"); |
String name = launchedBy.getStringExtra("name"); |
58 |
((TextView) findViewById(R.id.stationName)).setText( name ); |
((TextView) findViewById(R.id.stationName)).setText( name ); |
59 |
|
|
60 |
|
String address = launchedBy.getStringExtra("address"); |
61 |
|
((TextView) findViewById(R.id.stationAddr)).setText( address ); |
62 |
|
|
63 |
String addr = launchedBy.getStringExtra("address"); |
stationId = launchedBy.getIntExtra("stationid", -1); |
|
((TextView) findViewById(R.id.stationAddr)).setText( addr ); |
|
|
|
|
|
String code = launchedBy.getStringExtra("code"); |
|
64 |
|
|
65 |
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
66 |
|
|
73 |
|
|
74 |
provider = ProviderFactory.getDepartureProvider(); |
provider = ProviderFactory.getDepartureProvider(); |
75 |
|
|
|
fetcher = new DepartureFetcher(); |
|
76 |
if (savedInstanceState == null) { |
if (savedInstanceState == null) { |
77 |
showDialog(DLG_PROGRESS); |
startDepartureFetcher(); |
|
fetcher.execute(code); |
|
78 |
} else { |
} else { |
79 |
departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures"); |
departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures"); |
80 |
adapter.setDepartures(departures); |
adapter.setDepartures(departures); |
81 |
selectedItemId = savedInstanceState.getInt("selectedItemId"); |
selectedItemId = savedInstanceState.getInt("selectedItemId"); |
|
boolean detailsShowing = savedInstanceState.getBoolean("detailsShowing"); |
|
|
if (detailsShowing) |
|
|
showDialog(DLG_DETAILS); |
|
82 |
} |
} |
83 |
} |
} |
84 |
|
|
87 |
{ |
{ |
88 |
if (pgDialog != null && pgDialog.isShowing()) |
if (pgDialog != null && pgDialog.isShowing()) |
89 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
90 |
boolean detailsShowing = (detailsDialog != null && detailsDialog.isShowing()); |
|
|
if (detailsShowing) { |
|
|
dismissDialog(DLG_DETAILS); |
|
|
} |
|
|
outState.putBoolean("detailsShowing", detailsShowing); |
|
91 |
outState.putInt("selectedItemId", selectedItemId); |
outState.putInt("selectedItemId", selectedItemId); |
92 |
|
|
93 |
outState.putSerializable("departures", (ArrayList<DepartureBean>) departures); |
outState.putSerializable("departures", (ArrayList<DepartureBean>) departures); |
96 |
@Override |
@Override |
97 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
protected void onListItemClick(ListView l, View v, int position, long id) { |
98 |
super.onListItemClick(l, v, position, id); |
super.onListItemClick(l, v, position, id); |
99 |
|
|
100 |
|
selectedItemId = position; |
101 |
|
|
102 |
|
DepartureBean dep = departures.get(selectedItemId); |
103 |
|
|
104 |
selectedItemId = position; |
Intent intent = new Intent(this, TimetableList.class); |
105 |
showDialog(DLG_DETAILS); |
intent.putExtra("departure", dep); |
106 |
|
|
107 |
|
startActivity(intent); |
108 |
|
|
109 |
} |
} |
110 |
|
|
111 |
|
|
114 |
super.onPrepareDialog(id, dialog); |
super.onPrepareDialog(id, dialog); |
115 |
|
|
116 |
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; |
|
117 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
118 |
pgDialog = (ProgressDialog) dialog; |
pgDialog = (ProgressDialog) dialog; |
119 |
break; |
break; |
127 |
ProgressDialog dlg = new ProgressDialog(this); |
ProgressDialog dlg = new ProgressDialog(this); |
128 |
dlg.setMessage("Fetch departure data"); |
dlg.setMessage("Fetch departure data"); |
129 |
dlg.setCancelable(true); |
dlg.setCancelable(true); |
130 |
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; |
|
131 |
default: |
default: |
132 |
return super.onCreateDialog(id); |
return super.onCreateDialog(id); |
133 |
} |
} |
134 |
} |
} |
135 |
|
|
136 |
|
void startDepartureFetcher() { |
137 |
|
showDialog(DLG_PROGRESS); |
138 |
|
fetcher = new DepartureFetcher(); |
139 |
|
fetcher.execute(stationId); |
140 |
|
} |
141 |
|
|
142 |
class DialogDismisser implements View.OnClickListener { |
class DialogDismisser implements View.OnClickListener { |
143 |
|
|
144 |
Dialog dlg; |
Dialog dlg; |
150 |
public void onClick(View v) { |
public void onClick(View v) { |
151 |
if (dlg.isShowing()) |
if (dlg.isShowing()) |
152 |
dlg.dismiss(); |
dlg.dismiss(); |
153 |
} |
} |
154 |
} |
} |
155 |
|
|
156 |
View.OnClickListener mapLauncher = new View.OnClickListener() { |
View.OnClickListener mapLauncher = new View.OnClickListener() { |
160 |
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
161 |
} |
} |
162 |
}; |
}; |
163 |
|
|
164 |
|
|
165 |
|
|
166 |
|
class DepartureFetcher extends AsyncTask<Integer, Void, Void> { |
167 |
class DepartureFetcher extends AsyncTask<String, Void, Void> { |
|
168 |
|
boolean success; |
169 |
|
|
170 |
@Override |
@Override |
171 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
172 |
super.onPostExecute(result); |
super.onPostExecute(result); |
173 |
|
|
174 |
adapter.setDepartures(departures); |
|
175 |
pgDialog.dismiss(); |
pgDialog.dismiss(); |
176 |
|
|
177 |
if (departures.size() == 0) |
if (success) { |
178 |
MessageBox.showMessage(DepartureList.this, "No departures found"); |
adapter.setDepartures(departures); |
179 |
|
if (departures.size() == 0) { |
180 |
|
MessageBox.showMessage(DepartureList.this, "No departures found"); |
181 |
|
} |
182 |
|
} else { // communication or parse error |
183 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
184 |
|
builder.setMessage("Error finding departures"); |
185 |
|
builder.setCancelable(true); |
186 |
|
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
187 |
|
public void onClick(DialogInterface dialog, int id) { |
188 |
|
dialog.dismiss(); |
189 |
|
startDepartureFetcher(); |
190 |
|
|
191 |
|
} |
192 |
|
}); |
193 |
|
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
194 |
|
public void onClick(DialogInterface dialog, int id) { |
195 |
|
dialog.dismiss(); |
196 |
|
} |
197 |
|
}); |
198 |
|
builder.show(); |
199 |
|
} |
200 |
} |
} |
201 |
|
|
202 |
@Override |
@Override |
203 |
protected Void doInBackground(String... params) { |
protected Void doInBackground(Integer... params) { |
204 |
provider.lookupDepartures(params[0]); |
success = provider.lookupDepartures(params[0]); |
205 |
departures = provider.getDepartures(); |
departures = provider.getDepartures(params[0]); |
206 |
return null; |
return null; |
207 |
} |
} |
208 |
|
|