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; |
14 |
import android.os.AsyncTask; |
import android.os.AsyncTask; |
15 |
import android.os.Bundle; |
import android.os.Bundle; |
16 |
import android.view.View; |
import android.view.View; |
17 |
import android.widget.ListView; |
import android.widget.ListView; |
18 |
import android.widget.TextView; |
import android.widget.TextView; |
|
import android.widget.Toast; |
|
|
import dk.thoerup.traininfo.provider.DepartureFactory; |
|
19 |
import dk.thoerup.traininfo.provider.DepartureProvider; |
import dk.thoerup.traininfo.provider.DepartureProvider; |
20 |
|
import dk.thoerup.traininfo.provider.ProviderFactory; |
21 |
|
import dk.thoerup.traininfo.util.MessageBox; |
22 |
|
|
23 |
public class DepartureList extends ListActivity { |
public class DepartureList extends ListActivity { |
24 |
|
|
25 |
public static final int DLG_PROGRESS = 1; |
public static final int DLG_PROGRESS = 1; |
26 |
public static final int DLG_DETAILS = 2; |
|
27 |
|
|
28 |
DepartureListAdapter adapter; |
DepartureListAdapter adapter; |
29 |
DepartureProvider provider; |
DepartureProvider provider; |
30 |
List<DepartureBean> departures; |
List<DepartureBean> departures; |
31 |
|
|
32 |
static int itemId; |
int selectedItemId; |
33 |
static DepartureBean currentDeparture; |
//DepartureBean currentDeparture; |
34 |
|
|
35 |
ProgressDialog pgDialog; |
ProgressDialog pgDialog; |
36 |
|
|
37 |
DepartureFetcher fetcher; |
DepartureFetcher fetcher; |
38 |
|
|
39 |
|
StationBean station; |
40 |
|
|
41 |
|
@SuppressWarnings("unchecked") |
42 |
@Override |
@Override |
43 |
protected void onCreate(Bundle savedInstanceState) { |
protected void onCreate(Bundle savedInstanceState) { |
44 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
48 |
setListAdapter(adapter); |
setListAdapter(adapter); |
49 |
|
|
50 |
Intent launchedBy = getIntent(); |
Intent launchedBy = getIntent(); |
51 |
String name = launchedBy.getStringExtra("name"); |
|
52 |
((TextView) findViewById(R.id.stationName)).setText( name ); |
station = (StationBean) launchedBy.getSerializableExtra("stationbean"); |
53 |
|
|
54 |
|
((TextView) findViewById(R.id.stationName)).setText( station.getName() ); |
55 |
|
|
56 |
|
|
57 |
|
((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() ); |
58 |
|
|
|
String addr = launchedBy.getStringExtra("address"); |
|
|
((TextView) findViewById(R.id.stationAddr)).setText( addr ); |
|
59 |
|
|
60 |
|
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
61 |
|
|
62 |
NumberFormat format = NumberFormat.getNumberInstance(); |
NumberFormat format = NumberFormat.getNumberInstance(); |
63 |
format.setMaximumFractionDigits(1); |
format.setMaximumFractionDigits(1); |
64 |
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." ); |
|
|
|
|
65 |
|
|
66 |
showDialog(DLG_PROGRESS); |
|
67 |
provider = DepartureFactory.getProvider(); |
int distance = station.getDistance(); |
68 |
|
((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); |
69 |
|
|
70 |
fetcher = new DepartureFetcher(); |
|
71 |
fetcher.execute(name); |
if (station.isRegional() == false && station.isSTrain() == false) { |
72 |
|
getListView().setVisibility( View.GONE ); |
73 |
|
findViewById(R.id.metroonly).setVisibility( View.VISIBLE ); |
74 |
|
|
75 |
|
} else { |
76 |
|
provider = ProviderFactory.getDepartureProvider(); |
77 |
|
|
78 |
|
if (savedInstanceState == null) { |
79 |
|
startDepartureFetcher(); |
80 |
|
} else { |
81 |
|
departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures"); |
82 |
|
adapter.setDepartures(departures); |
83 |
|
selectedItemId = savedInstanceState.getInt("selectedItemId"); |
84 |
|
} |
85 |
|
} |
86 |
} |
} |
87 |
|
|
88 |
|
@Override |
89 |
|
public void onSaveInstanceState(Bundle outState) |
90 |
|
{ |
91 |
|
if (pgDialog != null && pgDialog.isShowing()) |
92 |
|
dismissDialog(DLG_PROGRESS); |
93 |
|
|
94 |
|
outState.putInt("selectedItemId", selectedItemId); |
95 |
|
|
96 |
|
outState.putSerializable("departures", (ArrayList<DepartureBean>) departures); |
97 |
|
} |
98 |
|
|
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 |
currentDeparture = departures.get(position); |
Intent intent = new Intent(this, TimetableList.class); |
108 |
showDialog(DLG_DETAILS); |
intent.putExtra("departure", dep); |
109 |
|
|
110 |
|
startActivity(intent); |
111 |
|
|
112 |
} |
} |
113 |
|
|
114 |
|
|
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) { |
|
case DLG_DETAILS: |
|
|
((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime()); |
|
|
((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; |
|
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 |
|
void startDepartureFetcher() { |
140 |
|
showDialog(DLG_PROGRESS); |
141 |
|
fetcher = new DepartureFetcher(); |
142 |
|
fetcher.execute(station.getId()); |
143 |
|
} |
144 |
|
|
145 |
class DialogDismisser implements View.OnClickListener { |
class DialogDismisser implements View.OnClickListener { |
146 |
|
|
147 |
Dialog dlg; |
Dialog dlg; |
153 |
public void onClick(View v) { |
public void onClick(View v) { |
154 |
if (dlg.isShowing()) |
if (dlg.isShowing()) |
155 |
dlg.dismiss(); |
dlg.dismiss(); |
156 |
} |
} |
157 |
} |
} |
158 |
|
|
159 |
|
View.OnClickListener mapLauncher = new View.OnClickListener() { |
160 |
|
@Override |
161 |
|
public void onClick(View v) { |
162 |
|
Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude()); |
163 |
|
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
164 |
|
} |
165 |
|
}; |
166 |
|
|
167 |
|
|
168 |
|
|
169 |
class DepartureFetcher extends AsyncTask<String, Void, Void> { |
class DepartureFetcher extends AsyncTask<Integer, Void, Void> { |
170 |
|
|
171 |
|
boolean success; |
172 |
|
|
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 |
Toast.makeText(DepartureList.this, "No departures found", Toast.LENGTH_LONG); |
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 |
|
|