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.removeMockLocation(this); |
38 |
|
//StationLocator.injectMockLocation(this); |
39 |
|
|
40 |
adapter = new StationListAdapter(this); |
adapter = new StationListAdapter(this); |
41 |
setListAdapter(adapter); |
setListAdapter(adapter); |
42 |
|
|
45 |
startLookup(); |
startLookup(); |
46 |
} |
} |
47 |
|
|
48 |
|
@Override |
49 |
|
public void onSaveInstanceState(Bundle outState) |
50 |
|
{ |
51 |
|
if (dialog.isShowing()) |
52 |
|
dialog.dismiss(); |
53 |
|
} |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
@Override |
@Override |
96 |
switch (msg.what) { |
switch (msg.what) { |
97 |
case GOTLOCATION: |
case GOTLOCATION: |
98 |
dialog.setMessage("Finding nearby stations"); |
dialog.setMessage("Finding nearby stations"); |
99 |
|
locatorTask.execute(); |
100 |
break; |
break; |
101 |
case GOTSTATIONLIST: |
case GOTSTATIONLIST: |
102 |
dialog.dismiss(); |
dialog.dismiss(); |
103 |
|
if (locator.getStations().size() == 0) |
104 |
|
MessageBox.showMessage(TrainInfoList.this,"Error loading station list!"); |
105 |
adapter.setStations( locator.getStations() ); |
adapter.setStations( locator.getStations() ); |
106 |
break; |
break; |
107 |
case NOPROVIDER: |
case NOPROVIDER: |
108 |
dialog.dismiss(); |
dialog.dismiss(); |
109 |
showMessageBox("No Location provider enabled. Plase enabled gps."); |
MessageBox.showMessage(TrainInfoList.this,"No location provider enabled. Plase enable gps."); |
110 |
break; |
break; |
111 |
case FIXTIMEOUT: |
case FIXTIMEOUT: |
112 |
dialog.dismiss(); |
dialog.dismiss(); |
113 |
if (isRunning) { |
if (isRunning) { |
114 |
locator.abortLocationListener(); |
locator.abortLocationListener(); |
115 |
showMessageBox("GPS fix timed out"); |
MessageBox.showMessage(TrainInfoList.this,"GPS fix timed out"); |
116 |
} |
} |
117 |
break; |
break; |
118 |
case LOOKUPSTATIONFAILED: |
case LOOKUPSTATIONFAILED: |
119 |
dialog.dismiss(); |
dialog.dismiss(); |
120 |
showMessageBox("Error on finding nearby stations"); |
MessageBox.showMessage(TrainInfoList.this,"Error on finding nearby stations"); |
121 |
break; |
break; |
122 |
} |
} |
123 |
|
|
143 |
startActivity(intent); |
startActivity(intent); |
144 |
} |
} |
145 |
|
|
146 |
public void showMessageBox(String message) { |
|
147 |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
class LocatorTask extends AsyncTask<Void,Void,Void> { |
148 |
builder.setMessage(message) |
@Override |
149 |
.setCancelable(false) |
protected void onPreExecute() { |
150 |
.setPositiveButton("OK", new DialogInterface.OnClickListener() { |
super.onPreExecute(); |
151 |
public void onClick(DialogInterface dialog, int id) { |
} |
152 |
dialog.dismiss(); |
|
153 |
} |
@Override |
154 |
}) |
protected Void doInBackground(Void... params) { |
155 |
.show(); |
locator.findNearestStations(); |
156 |
|
return null; |
157 |
|
} |
158 |
|
|
159 |
|
@Override |
160 |
|
protected void onPostExecute(Void result) { |
161 |
|
super.onPostExecute(result); |
162 |
|
} |
163 |
} |
} |
164 |
} |
} |