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 |
|
import java.util.Locale; |
7 |
|
|
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.Intent; |
import android.content.Intent; |
12 |
|
import android.location.Address; |
13 |
|
import android.location.Geocoder; |
14 |
import android.net.Uri; |
import android.net.Uri; |
15 |
import android.os.AsyncTask; |
import android.os.AsyncTask; |
16 |
import android.os.Bundle; |
import android.os.Bundle; |
17 |
|
import android.util.Log; |
18 |
import android.view.View; |
import android.view.View; |
19 |
import android.widget.ListView; |
import android.widget.ListView; |
20 |
import android.widget.TextView; |
import android.widget.TextView; |
|
import android.widget.Toast; |
|
|
import dk.thoerup.traininfo.provider.ProviderFactory; |
|
21 |
import dk.thoerup.traininfo.provider.DepartureProvider; |
import dk.thoerup.traininfo.provider.DepartureProvider; |
22 |
|
import dk.thoerup.traininfo.provider.ProviderFactory; |
23 |
import dk.thoerup.traininfo.util.MessageBox; |
import dk.thoerup.traininfo.util.MessageBox; |
24 |
|
|
25 |
public class DepartureList extends ListActivity { |
public class DepartureList extends ListActivity { |
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 |
|
Dialog detailsDialog; |
39 |
DepartureFetcher fetcher; |
DepartureFetcher fetcher; |
40 |
|
|
41 |
double latitude,longitude; |
double latitude,longitude; |
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); |
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"); |
int stationId = launchedBy.getIntExtra("stationid", -1); |
|
((TextView) findViewById(R.id.stationAddr)).setText( addr ); |
|
64 |
|
|
65 |
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
66 |
|
|
71 |
((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); |
((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); |
72 |
|
|
73 |
|
|
|
showDialog(DLG_PROGRESS); |
|
74 |
provider = ProviderFactory.getDepartureProvider(); |
provider = ProviderFactory.getDepartureProvider(); |
75 |
|
|
76 |
fetcher = new DepartureFetcher(); |
fetcher = new DepartureFetcher(); |
77 |
fetcher.execute(name); |
if (savedInstanceState == null) { |
78 |
|
showDialog(DLG_PROGRESS); |
79 |
|
fetcher.execute(stationId); |
80 |
|
} else { |
81 |
|
departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures"); |
82 |
|
adapter.setDepartures(departures); |
83 |
|
selectedItemId = savedInstanceState.getInt("selectedItemId"); |
84 |
|
boolean detailsShowing = savedInstanceState.getBoolean("detailsShowing"); |
85 |
|
if (detailsShowing) |
86 |
|
showDialog(DLG_DETAILS); |
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 |
|
boolean detailsShowing = (detailsDialog != null && detailsDialog.isShowing()); |
96 |
|
if (detailsShowing) { |
97 |
|
dismissDialog(DLG_DETAILS); |
98 |
|
} |
99 |
|
outState.putBoolean("detailsShowing", detailsShowing); |
100 |
|
outState.putInt("selectedItemId", selectedItemId); |
101 |
|
|
102 |
|
outState.putSerializable("departures", (ArrayList<DepartureBean>) departures); |
103 |
} |
} |
104 |
|
|
105 |
@Override |
@Override |
106 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
protected void onListItemClick(ListView l, View v, int position, long id) { |
107 |
super.onListItemClick(l, v, position, id); |
super.onListItemClick(l, v, position, id); |
108 |
|
|
109 |
currentDeparture = departures.get(position); |
selectedItemId = position; |
110 |
showDialog(DLG_DETAILS); |
showDialog(DLG_DETAILS); |
111 |
} |
} |
112 |
|
|
114 |
@Override |
@Override |
115 |
protected void onPrepareDialog(int id, Dialog dialog) { |
protected void onPrepareDialog(int id, Dialog dialog) { |
116 |
super.onPrepareDialog(id, dialog); |
super.onPrepareDialog(id, dialog); |
117 |
|
|
118 |
switch (id) { |
switch (id) { |
119 |
case DLG_DETAILS: |
case DLG_DETAILS: |
120 |
|
DepartureBean currentDeparture = departures.get(selectedItemId); |
121 |
((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime()); |
((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime()); |
122 |
((TextView)dialog.findViewById(R.id.Train)).setText(currentDeparture.getTrainNumber()); |
((TextView)dialog.findViewById(R.id.Train)).setText(currentDeparture.getTrainNumber()); |
123 |
((TextView)dialog.findViewById(R.id.Destination)).setText( currentDeparture.getDestination()); |
((TextView)dialog.findViewById(R.id.Destination)).setText( currentDeparture.getDestination()); |
126 |
((TextView)dialog.findViewById(R.id.Updated)).setText(currentDeparture.getLastUpdateString()); |
((TextView)dialog.findViewById(R.id.Updated)).setText(currentDeparture.getLastUpdateString()); |
127 |
((TextView)dialog.findViewById(R.id.Status)).setText(currentDeparture.getStatus()); |
((TextView)dialog.findViewById(R.id.Status)).setText(currentDeparture.getStatus()); |
128 |
((TextView)dialog.findViewById(R.id.Note)).setText(currentDeparture.getNote()); |
((TextView)dialog.findViewById(R.id.Note)).setText(currentDeparture.getNote()); |
129 |
|
detailsDialog = dialog; |
130 |
break; |
break; |
131 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
132 |
pgDialog = (ProgressDialog) dialog; |
pgDialog = (ProgressDialog) dialog; |
157 |
return super.onCreateDialog(id); |
return super.onCreateDialog(id); |
158 |
} |
} |
159 |
} |
} |
160 |
|
|
161 |
class DialogDismisser implements View.OnClickListener { |
class DialogDismisser implements View.OnClickListener { |
162 |
|
|
163 |
Dialog dlg; |
Dialog dlg; |
181 |
}; |
}; |
182 |
|
|
183 |
|
|
184 |
class DepartureFetcher extends AsyncTask<String, Void, Void> { |
class DepartureFetcher extends AsyncTask<Integer, Void, Void> { |
185 |
|
|
186 |
|
boolean success; |
187 |
|
String addr; |
188 |
@Override |
@Override |
189 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
190 |
super.onPostExecute(result); |
super.onPostExecute(result); |
192 |
adapter.setDepartures(departures); |
adapter.setDepartures(departures); |
193 |
pgDialog.dismiss(); |
pgDialog.dismiss(); |
194 |
|
|
195 |
if (departures.size() == 0) |
if (success) { |
196 |
MessageBox.showMessage(DepartureList.this, "No departures found"); |
if (departures.size() == 0) { |
197 |
|
MessageBox.showMessage(DepartureList.this, "No departures found"); |
198 |
|
} |
199 |
|
} else { // communication or parse error |
200 |
|
MessageBox.showMessage(DepartureList.this, "Error finding departures"); |
201 |
|
} |
202 |
} |
} |
203 |
|
|
204 |
@Override |
@Override |
205 |
protected Void doInBackground(String... params) { |
protected Void doInBackground(Integer... params) { |
206 |
provider.lookupDepartures(params[0]); |
success = provider.lookupDepartures(params[0]); |
207 |
departures = provider.getDepartures(); |
departures = provider.getDepartures(params[0]); |
208 |
return null; |
return null; |
209 |
} |
} |
210 |
|
|