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; |
import android.net.Uri; |
14 |
import android.os.AsyncTask; |
import android.os.AsyncTask; |
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; |
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 |
|
int stationId; |
39 |
|
|
40 |
double latitude,longitude; |
double latitude,longitude; |
41 |
|
|
42 |
|
@SuppressWarnings("unchecked") |
43 |
@Override |
@Override |
44 |
protected void onCreate(Bundle savedInstanceState) { |
protected void onCreate(Bundle savedInstanceState) { |
45 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
55 |
|
|
56 |
String name = launchedBy.getStringExtra("name"); |
String name = launchedBy.getStringExtra("name"); |
57 |
((TextView) findViewById(R.id.stationName)).setText( name ); |
((TextView) findViewById(R.id.stationName)).setText( name ); |
58 |
|
|
59 |
|
String address = launchedBy.getStringExtra("address"); |
60 |
|
((TextView) findViewById(R.id.stationAddr)).setText( address ); |
61 |
|
|
62 |
String addr = launchedBy.getStringExtra("address"); |
stationId = launchedBy.getIntExtra("stationid", -1); |
|
((TextView) findViewById(R.id.stationAddr)).setText( addr ); |
|
63 |
|
|
64 |
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
65 |
|
|
70 |
((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." ); |
71 |
|
|
72 |
|
|
73 |
showDialog(DLG_PROGRESS); |
provider = ProviderFactory.getDepartureProvider(); |
|
provider = DepartureFactory.getProvider(); |
|
74 |
|
|
75 |
fetcher = new DepartureFetcher(); |
if (savedInstanceState == null) { |
76 |
fetcher.execute(name); |
startDepartureFetcher(); |
77 |
|
} else { |
78 |
|
departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures"); |
79 |
|
adapter.setDepartures(departures); |
80 |
|
selectedItemId = savedInstanceState.getInt("selectedItemId"); |
81 |
|
} |
82 |
} |
} |
83 |
|
|
84 |
@Override |
@Override |
85 |
public void onSaveInstanceState(Bundle outState) |
public void onSaveInstanceState(Bundle outState) |
86 |
{ |
{ |
87 |
if (pgDialog.isShowing()) |
if (pgDialog != null && pgDialog.isShowing()) |
88 |
pgDialog.dismiss(); |
dismissDialog(DLG_PROGRESS); |
89 |
|
|
90 |
|
outState.putInt("selectedItemId", selectedItemId); |
91 |
|
|
92 |
|
outState.putSerializable("departures", (ArrayList<DepartureBean>) departures); |
93 |
} |
} |
94 |
|
|
95 |
@Override |
@Override |
96 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
protected void onListItemClick(ListView l, View v, int position, long id) { |
97 |
super.onListItemClick(l, v, position, id); |
super.onListItemClick(l, v, position, id); |
98 |
|
|
99 |
|
selectedItemId = position; |
100 |
|
|
101 |
|
DepartureBean dep = departures.get(selectedItemId); |
102 |
|
|
103 |
currentDeparture = departures.get(position); |
Intent intent = new Intent(this, TimetableList.class); |
104 |
showDialog(DLG_DETAILS); |
intent.putExtra("departure", dep); |
105 |
|
|
106 |
|
startActivity(intent); |
107 |
|
|
108 |
} |
} |
109 |
|
|
110 |
|
|
111 |
@Override |
@Override |
112 |
protected void onPrepareDialog(int id, Dialog dialog) { |
protected void onPrepareDialog(int id, Dialog dialog) { |
113 |
super.onPrepareDialog(id, dialog); |
super.onPrepareDialog(id, dialog); |
114 |
|
|
115 |
switch (id) { |
switch (id) { |
|
case DLG_DETAILS: |
|
|
((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime()); |
|
|
((TextView)dialog.findViewById(R.id.Train)).setText(currentDeparture.getTrainNumber()); |
|
|
((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; |
|
116 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
117 |
pgDialog = (ProgressDialog) dialog; |
pgDialog = (ProgressDialog) dialog; |
118 |
break; |
break; |
126 |
ProgressDialog dlg = new ProgressDialog(this); |
ProgressDialog dlg = new ProgressDialog(this); |
127 |
dlg.setMessage("Fetch departure data"); |
dlg.setMessage("Fetch departure data"); |
128 |
dlg.setCancelable(true); |
dlg.setCancelable(true); |
129 |
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; |
|
130 |
default: |
default: |
131 |
return super.onCreateDialog(id); |
return super.onCreateDialog(id); |
132 |
} |
} |
133 |
} |
} |
134 |
|
|
135 |
|
void startDepartureFetcher() { |
136 |
|
showDialog(DLG_PROGRESS); |
137 |
|
fetcher = new DepartureFetcher(); |
138 |
|
fetcher.execute(stationId); |
139 |
|
} |
140 |
|
|
141 |
class DialogDismisser implements View.OnClickListener { |
class DialogDismisser implements View.OnClickListener { |
142 |
|
|
143 |
Dialog dlg; |
Dialog dlg; |
149 |
public void onClick(View v) { |
public void onClick(View v) { |
150 |
if (dlg.isShowing()) |
if (dlg.isShowing()) |
151 |
dlg.dismiss(); |
dlg.dismiss(); |
152 |
} |
} |
153 |
} |
} |
154 |
|
|
155 |
View.OnClickListener mapLauncher = new View.OnClickListener() { |
View.OnClickListener mapLauncher = new View.OnClickListener() { |
159 |
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
160 |
} |
} |
161 |
}; |
}; |
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
class DepartureFetcher extends AsyncTask<Integer, Void, Void> { |
166 |
class DepartureFetcher extends AsyncTask<String, Void, Void> { |
|
167 |
|
boolean success; |
168 |
|
|
169 |
@Override |
@Override |
170 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
171 |
super.onPostExecute(result); |
super.onPostExecute(result); |
172 |
|
|
173 |
adapter.setDepartures(departures); |
|
174 |
pgDialog.dismiss(); |
pgDialog.dismiss(); |
175 |
|
|
176 |
if (departures.size() == 0) |
if (success) { |
177 |
MessageBox.showMessage(DepartureList.this, "No departures found"); |
adapter.setDepartures(departures); |
178 |
|
if (departures.size() == 0) { |
179 |
|
MessageBox.showMessage(DepartureList.this, "No departures found"); |
180 |
|
} |
181 |
|
} else { // communication or parse error |
182 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
183 |
|
builder.setMessage("Error finding departures"); |
184 |
|
builder.setCancelable(true); |
185 |
|
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
186 |
|
public void onClick(DialogInterface dialog, int id) { |
187 |
|
dialog.dismiss(); |
188 |
|
startDepartureFetcher(); |
189 |
|
|
190 |
|
} |
191 |
|
}); |
192 |
|
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
193 |
|
public void onClick(DialogInterface dialog, int id) { |
194 |
|
dialog.dismiss(); |
195 |
|
} |
196 |
|
}); |
197 |
|
builder.show(); |
198 |
|
} |
199 |
} |
} |
200 |
|
|
201 |
@Override |
@Override |
202 |
protected Void doInBackground(String... params) { |
protected Void doInBackground(Integer... params) { |
203 |
provider.lookupDepartures(params[0]); |
success = provider.lookupDepartures(params[0]); |
204 |
departures = provider.getDepartures(); |
departures = provider.getDepartures(params[0]); |
205 |
return null; |
return null; |
206 |
} |
} |
207 |
|
|