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.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; |
|
import android.widget.Toast; |
|
|
import dk.thoerup.traininfo.provider.DepartureFactory; |
|
20 |
import dk.thoerup.traininfo.provider.DepartureProvider; |
import dk.thoerup.traininfo.provider.DepartureProvider; |
21 |
|
import dk.thoerup.traininfo.provider.ProviderFactory; |
22 |
|
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 |
|
@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 |
String name = launchedBy.getStringExtra("name"); |
|
54 |
((TextView) findViewById(R.id.stationName)).setText( name ); |
station = (StationBean) launchedBy.getSerializableExtra("stationbean"); |
55 |
|
|
56 |
String addr = launchedBy.getStringExtra("address"); |
((TextView) findViewById(R.id.stationName)).setText( station.getName() ); |
57 |
((TextView) findViewById(R.id.stationAddr)).setText( addr ); |
|
58 |
|
|
59 |
|
((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() ); |
60 |
|
|
61 |
|
|
62 |
NumberFormat format = NumberFormat.getNumberInstance(); |
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
|
format.setMaximumFractionDigits(1); |
|
|
format.setMinimumFractionDigits(1); |
|
|
int distance = launchedBy.getIntExtra("distance", 0); |
|
|
((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); |
|
|
|
|
63 |
|
|
64 |
showDialog(DLG_PROGRESS); |
int distance = station.getDistance(); |
65 |
provider = DepartureFactory.getProvider(); |
if (distance != 0) { |
66 |
|
NumberFormat format = NumberFormat.getNumberInstance(); |
67 |
|
format.setMaximumFractionDigits(1); |
68 |
|
format.setMinimumFractionDigits(1); |
69 |
|
|
70 |
fetcher = new DepartureFetcher(); |
((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); |
71 |
fetcher.execute(name); |
} else { |
72 |
|
((TextView) findViewById(R.id.stationDistance)).setVisibility(View.GONE); |
73 |
|
} |
74 |
|
|
75 |
|
|
76 |
|
if (station.isRegional() == false && station.isSTrain() == false) { |
77 |
|
getListView().setVisibility( View.GONE ); |
78 |
|
findViewById(R.id.metroonly).setVisibility( View.VISIBLE ); |
79 |
|
|
80 |
|
} else { |
81 |
|
provider = ProviderFactory.getDepartureProvider(); |
82 |
|
|
83 |
|
if (savedInstanceState == null) { |
84 |
|
startDepartureFetcher(); |
85 |
|
} else { |
86 |
|
departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures"); |
87 |
|
adapter.setDepartures(departures); |
88 |
|
selectedItemId = savedInstanceState.getInt("selectedItemId"); |
89 |
|
} |
90 |
|
} |
91 |
} |
} |
92 |
|
|
93 |
|
@Override |
94 |
|
public void onSaveInstanceState(Bundle outState) |
95 |
|
{ |
96 |
|
if (pgDialog != null && pgDialog.isShowing()) |
97 |
|
dismissDialog(DLG_PROGRESS); |
98 |
|
|
99 |
|
outState.putInt("selectedItemId", selectedItemId); |
100 |
|
|
101 |
|
outState.putSerializable("departures", (ArrayList<DepartureBean>) departures); |
102 |
|
} |
103 |
|
|
104 |
@Override |
@Override |
105 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
protected void onListItemClick(ListView l, View v, int position, long id) { |
106 |
super.onListItemClick(l, v, position, id); |
super.onListItemClick(l, v, position, id); |
107 |
|
|
108 |
|
selectedItemId = position; |
109 |
|
|
110 |
|
DepartureBean dep = departures.get(selectedItemId); |
111 |
|
|
112 |
currentDeparture = departures.get(position); |
Intent intent = new Intent(this, TimetableList.class); |
113 |
showDialog(DLG_DETAILS); |
intent.putExtra("departure", dep); |
114 |
|
|
115 |
|
startActivity(intent); |
116 |
|
|
117 |
} |
} |
118 |
|
|
119 |
|
|
120 |
@Override |
@Override |
121 |
protected void onPrepareDialog(int id, Dialog dialog) { |
protected void onPrepareDialog(int id, Dialog dialog) { |
122 |
super.onPrepareDialog(id, dialog); |
super.onPrepareDialog(id, dialog); |
123 |
|
|
124 |
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; |
|
125 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
126 |
pgDialog = (ProgressDialog) dialog; |
pgDialog = (ProgressDialog) dialog; |
127 |
break; |
break; |
133 |
switch (id) { |
switch (id) { |
134 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
135 |
ProgressDialog dlg = new ProgressDialog(this); |
ProgressDialog dlg = new ProgressDialog(this); |
136 |
dlg.setMessage("Fetch departure data"); |
dlg.setMessage( getString(departurelist_fetchdata) ); |
137 |
dlg.setCancelable(true); |
dlg.setCancelable(true); |
138 |
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; |
|
139 |
default: |
default: |
140 |
return super.onCreateDialog(id); |
return super.onCreateDialog(id); |
141 |
} |
} |
142 |
} |
} |
143 |
|
|
144 |
|
void startDepartureFetcher() { |
145 |
|
showDialog(DLG_PROGRESS); |
146 |
|
fetcher = new DepartureFetcher(); |
147 |
|
fetcher.execute(station.getId()); |
148 |
|
} |
149 |
|
|
150 |
class DialogDismisser implements View.OnClickListener { |
class DialogDismisser implements View.OnClickListener { |
151 |
|
|
152 |
Dialog dlg; |
Dialog dlg; |
158 |
public void onClick(View v) { |
public void onClick(View v) { |
159 |
if (dlg.isShowing()) |
if (dlg.isShowing()) |
160 |
dlg.dismiss(); |
dlg.dismiss(); |
161 |
} |
} |
162 |
} |
} |
163 |
|
|
164 |
|
View.OnClickListener mapLauncher = new View.OnClickListener() { |
165 |
|
@Override |
166 |
|
public void onClick(View v) { |
167 |
|
Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude()); |
168 |
|
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
169 |
|
} |
170 |
|
}; |
171 |
|
|
172 |
|
|
173 |
|
|
174 |
class DepartureFetcher extends AsyncTask<String, Void, Void> { |
class DepartureFetcher extends AsyncTask<Integer, Void, Void> { |
175 |
|
|
176 |
|
boolean success; |
177 |
|
|
178 |
@Override |
@Override |
179 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
180 |
super.onPostExecute(result); |
super.onPostExecute(result); |
181 |
|
|
182 |
adapter.setDepartures(departures); |
|
183 |
pgDialog.dismiss(); |
pgDialog.dismiss(); |
184 |
|
|
185 |
if (departures.size() == 0) |
if (success) { |
186 |
Toast.makeText(DepartureList.this, "No departures found", Toast.LENGTH_LONG); |
adapter.setDepartures(departures); |
187 |
|
if (departures.size() == 0) { |
188 |
|
MessageBox.showMessage(DepartureList.this, "No departures found"); |
189 |
|
} |
190 |
|
} else { // communication or parse error |
191 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
192 |
|
builder.setMessage("Error finding departures"); |
193 |
|
builder.setCancelable(true); |
194 |
|
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
195 |
|
public void onClick(DialogInterface dialog, int id) { |
196 |
|
dialog.dismiss(); |
197 |
|
startDepartureFetcher(); |
198 |
|
|
199 |
|
} |
200 |
|
}); |
201 |
|
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
202 |
|
public void onClick(DialogInterface dialog, int id) { |
203 |
|
dialog.dismiss(); |
204 |
|
} |
205 |
|
}); |
206 |
|
|
207 |
|
try { |
208 |
|
builder.show(); |
209 |
|
} catch (android.view.WindowManager.BadTokenException e) { |
210 |
|
Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running |
211 |
|
} |
212 |
|
} |
213 |
} |
} |
214 |
|
|
215 |
@Override |
@Override |
216 |
protected Void doInBackground(String... params) { |
protected Void doInBackground(Integer... params) { |
217 |
provider.lookupDepartures(params[0]); |
success = provider.lookupDepartures(params[0]); |
218 |
departures = provider.getDepartures(); |
departures = provider.getDepartures(params[0]); |
219 |
return null; |
return null; |
220 |
} |
} |
221 |
|
|