1 |
package dk.thoerup.traininfo; |
package dk.thoerup.traininfo; |
2 |
|
|
|
import android.app.AlertDialog; |
|
3 |
import android.app.Dialog; |
import android.app.Dialog; |
4 |
import android.app.ListActivity; |
import android.app.ListActivity; |
5 |
import android.app.ProgressDialog; |
import android.app.ProgressDialog; |
|
import android.content.DialogInterface; |
|
6 |
import android.content.Intent; |
import android.content.Intent; |
7 |
|
import android.os.AsyncTask; |
8 |
import android.os.Bundle; |
import android.os.Bundle; |
9 |
import android.os.Handler; |
import android.os.Handler; |
10 |
import android.os.Message; |
import android.os.Message; |
11 |
import android.view.View; |
import android.view.View; |
12 |
import android.widget.ListView; |
import android.widget.ListView; |
13 |
|
import dk.thoerup.traininfo.util.MessageBox; |
14 |
|
|
15 |
public class TrainInfoList extends ListActivity { |
public class TrainInfoList extends ListActivity { |
16 |
public static final int GOTLOCATION = 1; |
public static final int GOTLOCATION = 1; |
24 |
/** Called when the activity is first created. */ |
/** Called when the activity is first created. */ |
25 |
ProgressDialog dialog; |
ProgressDialog dialog; |
26 |
StationLocator locator = null; |
StationLocator locator = null; |
27 |
|
LocatorTask locatorTask = new LocatorTask(); |
28 |
|
|
29 |
boolean isRunning; |
boolean isRunning; |
30 |
|
|
34 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
35 |
setContentView(R.layout.main); |
setContentView(R.layout.main); |
36 |
|
|
37 |
|
//StationLocator.injectMockLocation(this); |
38 |
|
|
39 |
adapter = new StationListAdapter(this); |
adapter = new StationListAdapter(this); |
40 |
setListAdapter(adapter); |
setListAdapter(adapter); |
41 |
|
|
44 |
startLookup(); |
startLookup(); |
45 |
} |
} |
46 |
|
|
47 |
|
@Override |
48 |
|
public void onSaveInstanceState(Bundle outState) |
49 |
|
{ |
50 |
|
if (dialog.isShowing()) |
51 |
|
dialog.dismiss(); |
52 |
|
} |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
@Override |
@Override |
69 |
|
|
70 |
|
|
71 |
|
|
|
|
|
72 |
@Override |
@Override |
73 |
protected void onPrepareDialog(int id, Dialog dialog) { |
protected void onPrepareDialog(int id, Dialog dialog) { |
74 |
super.onPrepareDialog(id, dialog); |
super.onPrepareDialog(id, dialog); |
79 |
} |
} |
80 |
} |
} |
81 |
|
|
|
|
|
|
|
|
|
public void progressDialog() { |
|
|
dialog = new ProgressDialog(this); |
|
|
dialog.setMessage("Wait for location fix"); |
|
|
dialog.setCancelable(false); |
|
|
dialog.show(); |
|
|
} |
|
|
|
|
82 |
public void startLookup() { |
public void startLookup() { |
83 |
isRunning = true; |
isRunning = true; |
84 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
|
//progressDialog(); |
|
85 |
|
|
86 |
locator.locateStations(); |
locator.locateStations(); |
87 |
stationsFetched.sendEmptyMessageDelayed(FIXTIMEOUT, 20000); |
stationsFetched.sendEmptyMessageDelayed(FIXTIMEOUT, 20000); |
95 |
switch (msg.what) { |
switch (msg.what) { |
96 |
case GOTLOCATION: |
case GOTLOCATION: |
97 |
dialog.setMessage("Finding nearby stations"); |
dialog.setMessage("Finding nearby stations"); |
98 |
|
locatorTask.execute(); |
99 |
break; |
break; |
100 |
case GOTSTATIONLIST: |
case GOTSTATIONLIST: |
101 |
dialog.dismiss(); |
dialog.dismiss(); |
102 |
|
if (locator.getStations().size() == 0) |
103 |
|
MessageBox.showMessage(TrainInfoList.this,"Error loading station list!"); |
104 |
adapter.setStations( locator.getStations() ); |
adapter.setStations( locator.getStations() ); |
105 |
break; |
break; |
106 |
case NOPROVIDER: |
case NOPROVIDER: |
107 |
dialog.dismiss(); |
dialog.dismiss(); |
108 |
showMessageBox("No Location provider enabled. Plase enabled gps."); |
MessageBox.showMessage(TrainInfoList.this,"No location provider enabled. Plase enable gps."); |
109 |
break; |
break; |
110 |
case FIXTIMEOUT: |
case FIXTIMEOUT: |
111 |
dialog.dismiss(); |
dialog.dismiss(); |
112 |
if (isRunning) { |
if (isRunning) { |
113 |
locator.abortLocationListener(); |
locator.abortLocationListener(); |
114 |
showMessageBox("GPS fix timed out"); |
MessageBox.showMessage(TrainInfoList.this,"GPS fix timed out"); |
115 |
} |
} |
116 |
break; |
break; |
117 |
case LOOKUPSTATIONFAILED: |
case LOOKUPSTATIONFAILED: |
118 |
dialog.dismiss(); |
dialog.dismiss(); |
119 |
showMessageBox("Error on finding nearby stations"); |
MessageBox.showMessage(TrainInfoList.this,"Error on finding nearby stations"); |
120 |
break; |
break; |
121 |
} |
} |
122 |
|
|
137 |
intent.putExtra("name", station.getName()); |
intent.putExtra("name", station.getName()); |
138 |
intent.putExtra("address", station.getAddress()); |
intent.putExtra("address", station.getAddress()); |
139 |
intent.putExtra("distance", station.getDistance()); |
intent.putExtra("distance", station.getDistance()); |
140 |
|
intent.putExtra("latitude", station.getLatitude()); |
141 |
|
intent.putExtra("longitude", station.getLongitude()); |
142 |
startActivity(intent); |
startActivity(intent); |
143 |
} |
} |
144 |
|
|
145 |
public void showMessageBox(String message) { |
|
146 |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
class LocatorTask extends AsyncTask<Void,Void,Void> { |
147 |
builder.setMessage(message) |
@Override |
148 |
.setCancelable(false) |
protected void onPreExecute() { |
149 |
.setPositiveButton("OK", new DialogInterface.OnClickListener() { |
super.onPreExecute(); |
150 |
public void onClick(DialogInterface dialog, int id) { |
} |
151 |
dialog.dismiss(); |
|
152 |
} |
@Override |
153 |
}) |
protected Void doInBackground(Void... params) { |
154 |
.show(); |
locator.findNearestStations(); |
155 |
|
return null; |
156 |
|
} |
157 |
|
|
158 |
|
@Override |
159 |
|
protected void onPostExecute(Void result) { |
160 |
|
super.onPostExecute(result); |
161 |
|
} |
162 |
} |
} |
163 |
} |
} |