3 |
import java.text.NumberFormat; |
import java.text.NumberFormat; |
4 |
import java.util.ArrayList; |
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; |
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 |
String addr = launchedBy.getStringExtra("address"); |
stationId = launchedBy.getIntExtra("stationid", -1); |
|
((TextView) findViewById(R.id.stationAddr)).setText( addr ); |
|
|
|
|
|
String code = launchedBy.getStringExtra("code"); |
|
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(code); |
|
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 |
selectedItemId = position; |
Intent intent = new Intent(this, TimetableList.class); |
106 |
showDialog(DLG_DETAILS); |
intent.putExtra("asd", 0); //Add all necessary data |
107 |
|
|
108 |
|
startActivity(intent); |
109 |
|
|
110 |
} |
} |
111 |
|
|
112 |
|
|
115 |
super.onPrepareDialog(id, dialog); |
super.onPrepareDialog(id, dialog); |
116 |
|
|
117 |
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; |
|
118 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
119 |
pgDialog = (ProgressDialog) dialog; |
pgDialog = (ProgressDialog) dialog; |
120 |
break; |
break; |
128 |
ProgressDialog dlg = new ProgressDialog(this); |
ProgressDialog dlg = new ProgressDialog(this); |
129 |
dlg.setMessage("Fetch departure data"); |
dlg.setMessage("Fetch departure data"); |
130 |
dlg.setCancelable(true); |
dlg.setCancelable(true); |
131 |
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; |
|
132 |
default: |
default: |
133 |
return super.onCreateDialog(id); |
return super.onCreateDialog(id); |
134 |
} |
} |
135 |
} |
} |
136 |
|
|
137 |
|
void startDepartureFetcher() { |
138 |
|
showDialog(DLG_PROGRESS); |
139 |
|
fetcher = new DepartureFetcher(); |
140 |
|
fetcher.execute(stationId); |
141 |
|
} |
142 |
|
|
143 |
class DialogDismisser implements View.OnClickListener { |
class DialogDismisser implements View.OnClickListener { |
144 |
|
|
145 |
Dialog dlg; |
Dialog dlg; |
152 |
if (dlg.isShowing()) |
if (dlg.isShowing()) |
153 |
dlg.dismiss(); |
dlg.dismiss(); |
154 |
} |
} |
155 |
} |
|
156 |
|
|
157 |
|
} |
158 |
|
|
159 |
View.OnClickListener mapLauncher = new View.OnClickListener() { |
View.OnClickListener mapLauncher = new View.OnClickListener() { |
160 |
@Override |
@Override |
163 |
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
164 |
} |
} |
165 |
}; |
}; |
166 |
|
|
167 |
|
|
168 |
|
|
169 |
|
class DepartureFetcher extends AsyncTask<Integer, Void, Void> { |
|
class DepartureFetcher extends AsyncTask<String, Void, Void> { |
|
170 |
|
|
171 |
|
boolean success; |
172 |
|
String addr; |
173 |
@Override |
@Override |
174 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
175 |
super.onPostExecute(result); |
super.onPostExecute(result); |
176 |
|
|
177 |
adapter.setDepartures(departures); |
|
178 |
pgDialog.dismiss(); |
pgDialog.dismiss(); |
179 |
|
|
180 |
if (departures.size() == 0) |
if (success) { |
181 |
MessageBox.showMessage(DepartureList.this, "No departures found"); |
adapter.setDepartures(departures); |
182 |
|
if (departures.size() == 0) { |
183 |
|
MessageBox.showMessage(DepartureList.this, "No departures found"); |
184 |
|
} |
185 |
|
} else { // communication or parse error |
186 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
187 |
|
builder.setMessage("Error finding departures"); |
188 |
|
builder.setCancelable(true); |
189 |
|
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
190 |
|
public void onClick(DialogInterface dialog, int id) { |
191 |
|
dialog.dismiss(); |
192 |
|
startDepartureFetcher(); |
193 |
|
|
194 |
|
} |
195 |
|
}); |
196 |
|
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
197 |
|
public void onClick(DialogInterface dialog, int id) { |
198 |
|
dialog.dismiss(); |
199 |
|
} |
200 |
|
}); |
201 |
|
builder.show(); |
202 |
|
} |
203 |
} |
} |
204 |
|
|
205 |
@Override |
@Override |
206 |
protected Void doInBackground(String... params) { |
protected Void doInBackground(Integer... params) { |
207 |
provider.lookupDepartures(params[0]); |
success = provider.lookupDepartures(params[0]); |
208 |
departures = provider.getDepartures(); |
departures = provider.getDepartures(params[0]); |
209 |
return null; |
return null; |
210 |
} |
} |
211 |
|
|