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 |
|
|
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; |
20 |
import dk.thoerup.traininfo.provider.DepartureProvider; |
import dk.thoerup.traininfo.provider.DepartureProvider; |
21 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
import dk.thoerup.traininfo.provider.ProviderFactory; |
22 |
import dk.thoerup.traininfo.util.MessageBox; |
import dk.thoerup.traininfo.util.MessageBox; |
23 |
|
import static dk.thoerup.traininfo.R.string.*; |
24 |
|
|
25 |
public class DepartureList extends ListActivity { |
public class DepartureList extends ListActivity { |
26 |
|
|
27 |
public static final int DLG_PROGRESS = 1; |
public static final int DLG_PROGRESS = 1; |
28 |
public static final int DLG_DETAILS = 2; |
|
29 |
|
|
30 |
DepartureListAdapter adapter; |
DepartureListAdapter adapter; |
31 |
DepartureProvider provider; |
DepartureProvider provider; |
32 |
List<DepartureBean> departures; |
List<DepartureBean> departures; |
33 |
|
|
34 |
static int itemId; |
int selectedItemId; |
35 |
static DepartureBean currentDeparture; |
//DepartureBean currentDeparture; |
36 |
|
|
37 |
ProgressDialog pgDialog; |
ProgressDialog pgDialog; |
38 |
|
|
39 |
DepartureFetcher fetcher; |
DepartureFetcher fetcher; |
40 |
|
|
41 |
|
StationBean station; |
42 |
|
|
43 |
double latitude,longitude; |
@SuppressWarnings("unchecked") |
|
|
|
44 |
@Override |
@Override |
45 |
protected void onCreate(Bundle savedInstanceState) { |
protected void onCreate(Bundle savedInstanceState) { |
46 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
50 |
setListAdapter(adapter); |
setListAdapter(adapter); |
51 |
|
|
52 |
Intent launchedBy = getIntent(); |
Intent launchedBy = getIntent(); |
53 |
|
|
54 |
|
station = (StationBean) launchedBy.getSerializableExtra("stationbean"); |
55 |
|
|
56 |
latitude = launchedBy.getDoubleExtra("latitude", 0.0); |
((TextView) findViewById(R.id.stationName)).setText( station.getName() ); |
57 |
longitude = launchedBy.getDoubleExtra("longitude", 0.0); |
|
58 |
|
|
59 |
String name = launchedBy.getStringExtra("name"); |
((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() ); |
|
((TextView) findViewById(R.id.stationName)).setText( name ); |
|
60 |
|
|
|
String addr = launchedBy.getStringExtra("address"); |
|
|
((TextView) findViewById(R.id.stationAddr)).setText( addr ); |
|
61 |
|
|
62 |
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
63 |
|
|
64 |
NumberFormat format = NumberFormat.getNumberInstance(); |
NumberFormat format = NumberFormat.getNumberInstance(); |
65 |
format.setMaximumFractionDigits(1); |
format.setMaximumFractionDigits(1); |
66 |
format.setMinimumFractionDigits(1); |
format.setMinimumFractionDigits(1); |
|
int distance = launchedBy.getIntExtra("distance", 0); |
|
|
((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); |
|
|
|
|
67 |
|
|
68 |
showDialog(DLG_PROGRESS); |
|
69 |
provider = ProviderFactory.getDepartureProvider(); |
int distance = station.getDistance(); |
70 |
|
((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); |
71 |
|
|
72 |
fetcher = new DepartureFetcher(); |
|
73 |
fetcher.execute(name); |
if (station.isRegional() == false && station.isSTrain() == false) { |
74 |
|
getListView().setVisibility( View.GONE ); |
75 |
|
findViewById(R.id.metroonly).setVisibility( View.VISIBLE ); |
76 |
|
|
77 |
|
} else { |
78 |
|
provider = ProviderFactory.getDepartureProvider(); |
79 |
|
|
80 |
|
if (savedInstanceState == null) { |
81 |
|
startDepartureFetcher(); |
82 |
|
} else { |
83 |
|
departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures"); |
84 |
|
adapter.setDepartures(departures); |
85 |
|
selectedItemId = savedInstanceState.getInt("selectedItemId"); |
86 |
|
} |
87 |
|
} |
88 |
} |
} |
89 |
|
|
90 |
@Override |
@Override |
91 |
public void onSaveInstanceState(Bundle outState) |
public void onSaveInstanceState(Bundle outState) |
92 |
{ |
{ |
93 |
if (pgDialog.isShowing()) |
if (pgDialog != null && pgDialog.isShowing()) |
94 |
pgDialog.dismiss(); |
dismissDialog(DLG_PROGRESS); |
95 |
|
|
96 |
|
outState.putInt("selectedItemId", selectedItemId); |
97 |
|
|
98 |
|
outState.putSerializable("departures", (ArrayList<DepartureBean>) departures); |
99 |
} |
} |
100 |
|
|
101 |
@Override |
@Override |
102 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
protected void onListItemClick(ListView l, View v, int position, long id) { |
103 |
super.onListItemClick(l, v, position, id); |
super.onListItemClick(l, v, position, id); |
104 |
|
|
105 |
|
selectedItemId = position; |
106 |
|
|
107 |
|
DepartureBean dep = departures.get(selectedItemId); |
108 |
|
|
109 |
currentDeparture = departures.get(position); |
Intent intent = new Intent(this, TimetableList.class); |
110 |
showDialog(DLG_DETAILS); |
intent.putExtra("departure", dep); |
111 |
|
|
112 |
|
startActivity(intent); |
113 |
|
|
114 |
} |
} |
115 |
|
|
116 |
|
|
117 |
@Override |
@Override |
118 |
protected void onPrepareDialog(int id, Dialog dialog) { |
protected void onPrepareDialog(int id, Dialog dialog) { |
119 |
super.onPrepareDialog(id, dialog); |
super.onPrepareDialog(id, dialog); |
120 |
|
|
121 |
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; |
|
122 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
123 |
pgDialog = (ProgressDialog) dialog; |
pgDialog = (ProgressDialog) dialog; |
124 |
break; |
break; |
130 |
switch (id) { |
switch (id) { |
131 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
132 |
ProgressDialog dlg = new ProgressDialog(this); |
ProgressDialog dlg = new ProgressDialog(this); |
133 |
dlg.setMessage("Fetch departure data"); |
dlg.setMessage( getString(departurelist_fetchdata) ); |
134 |
dlg.setCancelable(true); |
dlg.setCancelable(true); |
135 |
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; |
|
136 |
default: |
default: |
137 |
return super.onCreateDialog(id); |
return super.onCreateDialog(id); |
138 |
} |
} |
139 |
} |
} |
140 |
|
|
141 |
|
void startDepartureFetcher() { |
142 |
|
showDialog(DLG_PROGRESS); |
143 |
|
fetcher = new DepartureFetcher(); |
144 |
|
fetcher.execute(station.getId()); |
145 |
|
} |
146 |
|
|
147 |
class DialogDismisser implements View.OnClickListener { |
class DialogDismisser implements View.OnClickListener { |
148 |
|
|
149 |
Dialog dlg; |
Dialog dlg; |
155 |
public void onClick(View v) { |
public void onClick(View v) { |
156 |
if (dlg.isShowing()) |
if (dlg.isShowing()) |
157 |
dlg.dismiss(); |
dlg.dismiss(); |
158 |
} |
} |
159 |
} |
} |
160 |
|
|
161 |
View.OnClickListener mapLauncher = new View.OnClickListener() { |
View.OnClickListener mapLauncher = new View.OnClickListener() { |
162 |
@Override |
@Override |
163 |
public void onClick(View v) { |
public void onClick(View v) { |
164 |
Uri uri = Uri.parse("geo:" + latitude + "," + longitude); |
Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude()); |
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> { |
172 |
class DepartureFetcher extends AsyncTask<String, Void, Void> { |
|
173 |
|
boolean success; |
174 |
|
|
175 |
@Override |
@Override |
176 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
177 |
super.onPostExecute(result); |
super.onPostExecute(result); |
178 |
|
|
179 |
adapter.setDepartures(departures); |
|
180 |
pgDialog.dismiss(); |
pgDialog.dismiss(); |
181 |
|
|
182 |
if (departures.size() == 0) |
if (success) { |
183 |
MessageBox.showMessage(DepartureList.this, "No departures found"); |
adapter.setDepartures(departures); |
184 |
|
if (departures.size() == 0) { |
185 |
|
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(getString(generic_retry), new DialogInterface.OnClickListener() { |
192 |
|
public void onClick(DialogInterface dialog, int id) { |
193 |
|
dialog.dismiss(); |
194 |
|
startDepartureFetcher(); |
195 |
|
|
196 |
|
} |
197 |
|
}); |
198 |
|
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
199 |
|
public void onClick(DialogInterface dialog, int id) { |
200 |
|
dialog.dismiss(); |
201 |
|
} |
202 |
|
}); |
203 |
|
|
204 |
|
try { |
205 |
|
builder.show(); |
206 |
|
} catch (android.view.WindowManager.BadTokenException e) { |
207 |
|
Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running |
208 |
|
} |
209 |
|
} |
210 |
} |
} |
211 |
|
|
212 |
@Override |
@Override |
213 |
protected Void doInBackground(String... params) { |
protected Void doInBackground(Integer... params) { |
214 |
provider.lookupDepartures(params[0]); |
success = provider.lookupDepartures(params[0]); |
215 |
departures = provider.getDepartures(); |
departures = provider.getDepartures(params[0]); |
216 |
return null; |
return null; |
217 |
} |
} |
218 |
|
|