1 |
package dk.thoerup.traininfo; |
package dk.thoerup.traininfo; |
2 |
|
|
3 |
|
import static dk.thoerup.traininfo.R.string.departurelist_fetchdata; |
4 |
|
import static dk.thoerup.traininfo.R.string.generic_cancel; |
5 |
|
import static dk.thoerup.traininfo.R.string.generic_retry; |
6 |
|
|
7 |
import java.text.NumberFormat; |
import java.text.NumberFormat; |
8 |
|
import java.util.ArrayList; |
9 |
import java.util.List; |
import java.util.List; |
10 |
|
|
11 |
|
import android.app.AlertDialog; |
12 |
import android.app.Dialog; |
import android.app.Dialog; |
13 |
import android.app.ListActivity; |
import android.app.ListActivity; |
14 |
import android.app.ProgressDialog; |
import android.app.ProgressDialog; |
15 |
|
import android.content.DialogInterface; |
16 |
import android.content.Intent; |
import android.content.Intent; |
17 |
|
import android.net.Uri; |
18 |
import android.os.AsyncTask; |
import android.os.AsyncTask; |
19 |
import android.os.Bundle; |
import android.os.Bundle; |
20 |
|
import android.util.Log; |
21 |
import android.view.View; |
import android.view.View; |
22 |
import android.widget.ListView; |
import android.widget.ListView; |
23 |
import android.widget.TextView; |
import android.widget.TextView; |
|
import android.widget.Toast; |
|
|
import dk.thoerup.traininfo.provider.DepartureFactory; |
|
24 |
import dk.thoerup.traininfo.provider.DepartureProvider; |
import dk.thoerup.traininfo.provider.DepartureProvider; |
25 |
|
import dk.thoerup.traininfo.provider.ProviderFactory; |
26 |
|
import dk.thoerup.traininfo.util.MessageBox; |
27 |
|
|
28 |
public class DepartureList extends ListActivity { |
public class DepartureList extends ListActivity { |
29 |
|
|
30 |
public static final int DLG_PROGRESS = 1; |
public static final int DLG_PROGRESS = 1; |
31 |
public static final int DLG_DETAILS = 2; |
|
32 |
|
|
33 |
DepartureListAdapter adapter; |
DepartureListAdapter adapter; |
34 |
DepartureProvider provider; |
DepartureProvider provider; |
35 |
List<DepartureBean> departures; |
List<DepartureBean> departures; |
36 |
|
|
37 |
static int itemId; |
int selectedItemId; |
38 |
static DepartureBean currentDeparture; |
//DepartureBean currentDeparture; |
39 |
|
|
40 |
ProgressDialog pgDialog; |
ProgressDialog pgDialog; |
41 |
|
|
42 |
DepartureFetcher fetcher; |
DepartureFetcher fetcher; |
43 |
|
|
44 |
|
StationBean station; |
45 |
|
|
46 |
|
@SuppressWarnings("unchecked") |
47 |
@Override |
@Override |
48 |
protected void onCreate(Bundle savedInstanceState) { |
protected void onCreate(Bundle savedInstanceState) { |
49 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
53 |
setListAdapter(adapter); |
setListAdapter(adapter); |
54 |
|
|
55 |
Intent launchedBy = getIntent(); |
Intent launchedBy = getIntent(); |
56 |
String name = launchedBy.getStringExtra("name"); |
|
57 |
((TextView) findViewById(R.id.stationName)).setText( name ); |
station = (StationBean) launchedBy.getSerializableExtra("stationbean"); |
58 |
|
|
59 |
String addr = launchedBy.getStringExtra("address"); |
((TextView) findViewById(R.id.stationName)).setText( station.getName() ); |
60 |
((TextView) findViewById(R.id.stationAddr)).setText( addr ); |
|
61 |
|
|
62 |
|
((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() ); |
63 |
|
|
64 |
|
|
65 |
|
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
66 |
|
|
67 |
NumberFormat format = NumberFormat.getNumberInstance(); |
NumberFormat format = NumberFormat.getNumberInstance(); |
68 |
format.setMaximumFractionDigits(1); |
format.setMaximumFractionDigits(1); |
69 |
format.setMinimumFractionDigits(1); |
format.setMinimumFractionDigits(1); |
70 |
int distance = launchedBy.getIntExtra("distance", 0); |
|
71 |
|
|
72 |
|
int distance = station.getDistance(); |
73 |
((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." ); |
74 |
|
station.setRegional(true); |
75 |
|
Log.e("ASD", "" + station.isRegional() + "/" + station.isSTrain() + "/" + station.isMetro()); |
76 |
|
|
77 |
|
if (station.isRegional() == false && station.isSTrain() == false) { |
78 |
showDialog(DLG_PROGRESS); |
getListView().setVisibility( View.GONE ); |
79 |
provider = DepartureFactory.getProvider(); |
findViewById(R.id.metroonly).setVisibility( View.VISIBLE ); |
80 |
|
|
81 |
fetcher = new DepartureFetcher(); |
} else { |
82 |
fetcher.execute(name); |
provider = ProviderFactory.getDepartureProvider(); |
83 |
|
|
84 |
|
if (savedInstanceState == null) { |
85 |
|
startDepartureFetcher(); |
86 |
|
} else { |
87 |
|
departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures"); |
88 |
|
adapter.setDepartures(departures); |
89 |
|
selectedItemId = savedInstanceState.getInt("selectedItemId"); |
90 |
|
} |
91 |
|
} |
92 |
} |
} |
93 |
|
|
94 |
|
@Override |
95 |
|
public void onSaveInstanceState(Bundle outState) |
96 |
|
{ |
97 |
|
if (pgDialog != null && pgDialog.isShowing()) |
98 |
|
dismissDialog(DLG_PROGRESS); |
99 |
|
|
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 |
|
selectedItemId = position; |
110 |
|
|
111 |
|
DepartureBean dep = departures.get(selectedItemId); |
112 |
|
|
113 |
currentDeparture = departures.get(position); |
Intent intent = new Intent(this, TimetableList.class); |
114 |
showDialog(DLG_DETAILS); |
intent.putExtra("departure", dep); |
115 |
|
|
116 |
|
startActivity(intent); |
117 |
|
|
118 |
} |
} |
119 |
|
|
120 |
|
|
121 |
@Override |
@Override |
122 |
protected void onPrepareDialog(int id, Dialog dialog) { |
protected void onPrepareDialog(int id, Dialog dialog) { |
123 |
super.onPrepareDialog(id, dialog); |
super.onPrepareDialog(id, dialog); |
124 |
|
|
125 |
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; |
|
126 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
127 |
pgDialog = (ProgressDialog) dialog; |
pgDialog = (ProgressDialog) dialog; |
128 |
break; |
break; |
134 |
switch (id) { |
switch (id) { |
135 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
136 |
ProgressDialog dlg = new ProgressDialog(this); |
ProgressDialog dlg = new ProgressDialog(this); |
137 |
dlg.setMessage("Fetch departure data"); |
dlg.setMessage( getString(departurelist_fetchdata) ); |
138 |
dlg.setCancelable(true); |
dlg.setCancelable(true); |
139 |
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; |
|
140 |
default: |
default: |
141 |
return super.onCreateDialog(id); |
return super.onCreateDialog(id); |
142 |
} |
} |
143 |
} |
} |
144 |
|
|
145 |
|
void startDepartureFetcher() { |
146 |
|
showDialog(DLG_PROGRESS); |
147 |
|
fetcher = new DepartureFetcher(); |
148 |
|
fetcher.execute(station.getId()); |
149 |
|
} |
150 |
|
|
151 |
class DialogDismisser implements View.OnClickListener { |
class DialogDismisser implements View.OnClickListener { |
152 |
|
|
153 |
Dialog dlg; |
Dialog dlg; |
159 |
public void onClick(View v) { |
public void onClick(View v) { |
160 |
if (dlg.isShowing()) |
if (dlg.isShowing()) |
161 |
dlg.dismiss(); |
dlg.dismiss(); |
162 |
} |
} |
163 |
} |
} |
164 |
|
|
165 |
|
View.OnClickListener mapLauncher = new View.OnClickListener() { |
166 |
|
@Override |
167 |
|
public void onClick(View v) { |
168 |
|
Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude()); |
169 |
|
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
170 |
|
} |
171 |
|
}; |
172 |
|
|
173 |
|
|
174 |
|
|
175 |
class DepartureFetcher extends AsyncTask<String, Void, Void> { |
class DepartureFetcher extends AsyncTask<Integer, Void, Void> { |
176 |
|
|
177 |
|
boolean success; |
178 |
|
|
179 |
@Override |
@Override |
180 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
181 |
super.onPostExecute(result); |
super.onPostExecute(result); |
182 |
|
|
183 |
adapter.setDepartures(departures); |
|
184 |
pgDialog.dismiss(); |
pgDialog.dismiss(); |
185 |
|
|
186 |
if (departures.size() == 0) |
if (success) { |
187 |
Toast.makeText(DepartureList.this, "No departures found", Toast.LENGTH_LONG); |
adapter.setDepartures(departures); |
188 |
|
if (departures.size() == 0) { |
189 |
|
MessageBox.showMessage(DepartureList.this, "No departures found"); |
190 |
|
} |
191 |
|
} else { // communication or parse error |
192 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
193 |
|
builder.setMessage("Error finding departures"); |
194 |
|
builder.setCancelable(true); |
195 |
|
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
196 |
|
public void onClick(DialogInterface dialog, int id) { |
197 |
|
dialog.dismiss(); |
198 |
|
startDepartureFetcher(); |
199 |
|
|
200 |
|
} |
201 |
|
}); |
202 |
|
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
203 |
|
public void onClick(DialogInterface dialog, int id) { |
204 |
|
dialog.dismiss(); |
205 |
|
} |
206 |
|
}); |
207 |
|
builder.show(); |
208 |
|
} |
209 |
} |
} |
210 |
|
|
211 |
@Override |
@Override |
212 |
protected Void doInBackground(String... params) { |
protected Void doInBackground(Integer... params) { |
213 |
provider.lookupDepartures(params[0]); |
success = provider.lookupDepartures(params[0]); |
214 |
departures = provider.getDepartures(); |
departures = provider.getDepartures(params[0]); |
215 |
return null; |
return null; |
216 |
} |
} |
217 |
|
|