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.Dialog; |
import android.app.Dialog; |
8 |
import android.app.ListActivity; |
import android.app.ListActivity; |
9 |
import android.app.ProgressDialog; |
import android.app.ProgressDialog; |
10 |
import android.content.Intent; |
import android.content.Intent; |
11 |
|
import android.net.Uri; |
12 |
import android.os.AsyncTask; |
import android.os.AsyncTask; |
13 |
import android.os.Bundle; |
import android.os.Bundle; |
14 |
import android.view.View; |
import android.view.View; |
15 |
import android.widget.ListView; |
import android.widget.ListView; |
16 |
import android.widget.TextView; |
import android.widget.TextView; |
|
import dk.thoerup.traininfo.provider.DepartureFactory; |
|
17 |
import dk.thoerup.traininfo.provider.DepartureProvider; |
import dk.thoerup.traininfo.provider.DepartureProvider; |
18 |
|
import dk.thoerup.traininfo.provider.ProviderFactory; |
19 |
|
import dk.thoerup.traininfo.util.MessageBox; |
20 |
|
|
21 |
public class DepartureList extends ListActivity { |
public class DepartureList extends ListActivity { |
22 |
|
|
27 |
DepartureProvider provider; |
DepartureProvider provider; |
28 |
List<DepartureBean> departures; |
List<DepartureBean> departures; |
29 |
|
|
30 |
static int itemId; |
int selectedItemId; |
31 |
static DepartureBean currentDeparture; |
//DepartureBean currentDeparture; |
32 |
|
|
33 |
ProgressDialog pgDialog; |
ProgressDialog pgDialog; |
34 |
|
Dialog detailsDialog; |
35 |
|
DepartureFetcher fetcher; |
36 |
|
|
37 |
|
double latitude,longitude; |
38 |
|
|
39 |
|
@SuppressWarnings("unchecked") |
40 |
@Override |
@Override |
41 |
protected void onCreate(Bundle savedInstanceState) { |
protected void onCreate(Bundle savedInstanceState) { |
42 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
46 |
setListAdapter(adapter); |
setListAdapter(adapter); |
47 |
|
|
48 |
Intent launchedBy = getIntent(); |
Intent launchedBy = getIntent(); |
49 |
|
|
50 |
|
latitude = launchedBy.getDoubleExtra("latitude", 0.0); |
51 |
|
longitude = launchedBy.getDoubleExtra("longitude", 0.0); |
52 |
|
|
53 |
String name = launchedBy.getStringExtra("name"); |
String name = launchedBy.getStringExtra("name"); |
54 |
((TextView) findViewById(R.id.stationName)).setText( name ); |
((TextView) findViewById(R.id.stationName)).setText( name ); |
55 |
|
|
56 |
String addr = launchedBy.getStringExtra("address"); |
String addr = launchedBy.getStringExtra("address"); |
57 |
((TextView) findViewById(R.id.stationAddr)).setText( addr ); |
((TextView) findViewById(R.id.stationAddr)).setText( addr ); |
58 |
|
|
59 |
|
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
60 |
|
|
61 |
NumberFormat format = NumberFormat.getNumberInstance(); |
NumberFormat format = NumberFormat.getNumberInstance(); |
62 |
format.setMaximumFractionDigits(1); |
format.setMaximumFractionDigits(1); |
64 |
int distance = launchedBy.getIntExtra("distance", 0); |
int distance = launchedBy.getIntExtra("distance", 0); |
65 |
((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." ); |
66 |
|
|
67 |
showDialog(DLG_PROGRESS); |
|
68 |
provider = DepartureFactory.getProvider(); |
provider = ProviderFactory.getDepartureProvider(); |
69 |
provider.lookupDepartures(name); |
|
70 |
departures = provider.getDepartures(); |
fetcher = new DepartureFetcher(); |
71 |
adapter.setDepartures(departures); |
if (savedInstanceState == null) { |
72 |
pgDialog.dismiss(); |
showDialog(DLG_PROGRESS); |
73 |
|
fetcher.execute(name); |
74 |
|
} else { |
75 |
|
departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures"); |
76 |
|
adapter.setDepartures(departures); |
77 |
|
selectedItemId = savedInstanceState.getInt("selectedItemId"); |
78 |
|
boolean detailsShowing = savedInstanceState.getBoolean("detailsShowing"); |
79 |
|
if (detailsShowing) |
80 |
|
showDialog(DLG_DETAILS); |
81 |
|
} |
82 |
} |
} |
83 |
|
|
84 |
|
@Override |
85 |
|
public void onSaveInstanceState(Bundle outState) |
86 |
|
{ |
87 |
|
if (pgDialog != null && pgDialog.isShowing()) |
88 |
|
dismissDialog(DLG_PROGRESS); |
89 |
|
boolean detailsShowing = (detailsDialog != null && detailsDialog.isShowing()); |
90 |
|
if (detailsShowing) { |
91 |
|
dismissDialog(DLG_DETAILS); |
92 |
|
} |
93 |
|
outState.putBoolean("detailsShowing", detailsShowing); |
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 |
currentDeparture = departures.get(position); |
selectedItemId = position; |
104 |
showDialog(DLG_DETAILS); |
showDialog(DLG_DETAILS); |
105 |
} |
} |
106 |
|
|
108 |
@Override |
@Override |
109 |
protected void onPrepareDialog(int id, Dialog dialog) { |
protected void onPrepareDialog(int id, Dialog dialog) { |
110 |
super.onPrepareDialog(id, dialog); |
super.onPrepareDialog(id, dialog); |
111 |
|
|
112 |
switch (id) { |
switch (id) { |
113 |
case DLG_DETAILS: |
case DLG_DETAILS: |
114 |
|
DepartureBean currentDeparture = departures.get(selectedItemId); |
115 |
((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime()); |
((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime()); |
116 |
|
((TextView)dialog.findViewById(R.id.Train)).setText(currentDeparture.getTrainNumber()); |
117 |
((TextView)dialog.findViewById(R.id.Destination)).setText( currentDeparture.getDestination()); |
((TextView)dialog.findViewById(R.id.Destination)).setText( currentDeparture.getDestination()); |
118 |
((TextView)dialog.findViewById(R.id.Origin)).setText(currentDeparture.getOrigin()); |
((TextView)dialog.findViewById(R.id.Origin)).setText(currentDeparture.getOrigin()); |
119 |
((TextView)dialog.findViewById(R.id.Location)).setText(currentDeparture.getLocation()); |
((TextView)dialog.findViewById(R.id.Location)).setText(currentDeparture.getLocation()); |
120 |
((TextView)dialog.findViewById(R.id.Updated)).setText(currentDeparture.getLastUpdateString()); |
((TextView)dialog.findViewById(R.id.Updated)).setText(currentDeparture.getLastUpdateString()); |
121 |
((TextView)dialog.findViewById(R.id.Status)).setText(currentDeparture.getStatus()); |
((TextView)dialog.findViewById(R.id.Status)).setText(currentDeparture.getStatus()); |
122 |
((TextView)dialog.findViewById(R.id.Note)).setText(currentDeparture.getNote()); |
((TextView)dialog.findViewById(R.id.Note)).setText(currentDeparture.getNote()); |
123 |
|
detailsDialog = dialog; |
124 |
break; |
break; |
125 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
126 |
pgDialog = (ProgressDialog) dialog; |
pgDialog = (ProgressDialog) dialog; |
165 |
dlg.dismiss(); |
dlg.dismiss(); |
166 |
} |
} |
167 |
} |
} |
168 |
|
|
169 |
|
View.OnClickListener mapLauncher = new View.OnClickListener() { |
170 |
|
@Override |
171 |
|
public void onClick(View v) { |
172 |
|
Uri uri = Uri.parse("geo:" + latitude + "," + longitude); |
173 |
|
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
174 |
|
} |
175 |
|
}; |
176 |
|
|
177 |
|
|
178 |
|
class DepartureFetcher extends AsyncTask<String, Void, Void> { |
179 |
|
|
180 |
|
@Override |
181 |
|
protected void onPostExecute(Void result) { |
182 |
|
super.onPostExecute(result); |
183 |
|
|
184 |
|
adapter.setDepartures(departures); |
185 |
|
pgDialog.dismiss(); |
186 |
|
|
187 |
|
if (departures.size() == 0) |
188 |
|
MessageBox.showMessage(DepartureList.this, "No departures found"); |
189 |
|
} |
190 |
|
|
191 |
|
@Override |
192 |
|
protected Void doInBackground(String... params) { |
193 |
|
provider.lookupDepartures(params[0]); |
194 |
|
departures = provider.getDepartures(); |
195 |
|
return null; |
196 |
|
} |
197 |
|
|
198 |
|
} |
199 |
} |
} |