/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/TrainInfoList.java
ViewVC logotype

Annotation of /android/TrainInfo/src/dk/thoerup/traininfo/TrainInfoList.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 245 - (hide annotations) (download)
Sun Aug 9 19:40:05 2009 UTC (14 years, 9 months ago) by torben
File size: 3931 byte(s)
Small mods to the messagebox
1 torben 237 package dk.thoerup.traininfo;
2    
3     import android.app.Dialog;
4     import android.app.ListActivity;
5     import android.app.ProgressDialog;
6     import android.content.Intent;
7 torben 241 import android.os.AsyncTask;
8 torben 237 import android.os.Bundle;
9     import android.os.Handler;
10     import android.os.Message;
11     import android.view.View;
12     import android.widget.ListView;
13 torben 245 import dk.thoerup.traininfo.util.MessageBox;
14 torben 237
15     public class TrainInfoList extends ListActivity {
16     public static final int GOTLOCATION = 1;
17     public static final int GOTSTATIONLIST = 2;
18     public static final int NOPROVIDER = 3;
19     public static final int FIXTIMEOUT = 4;
20     public static final int LOOKUPSTATIONFAILED = 5;
21    
22     public static final int DLG_PROGRESS = 1;
23    
24     /** Called when the activity is first created. */
25     ProgressDialog dialog;
26     StationLocator locator = null;
27 torben 241 LocatorTask locatorTask = new LocatorTask();
28 torben 237
29     boolean isRunning;
30    
31     StationListAdapter adapter = null;
32     @Override
33     public void onCreate(Bundle savedInstanceState) {
34     super.onCreate(savedInstanceState);
35     setContentView(R.layout.main);
36    
37 torben 241 //StationLocator.injectMockLocation(this);
38    
39 torben 237 adapter = new StationListAdapter(this);
40     setListAdapter(adapter);
41    
42     locator = new StationLocator(this, stationsFetched);
43    
44     startLookup();
45     }
46    
47 torben 243 @Override
48     public void onSaveInstanceState(Bundle outState)
49     {
50     if (dialog.isShowing())
51     dialog.dismiss();
52     }
53 torben 237
54 torben 243
55 torben 237
56     @Override
57     protected Dialog onCreateDialog(int id) {
58     switch (id) {
59     case DLG_PROGRESS:
60     ProgressDialog dlg = new ProgressDialog(this);
61     dlg.setMessage("Wait for location fix");
62     dlg.setCancelable(false);
63     return dlg;
64     default:
65     return super.onCreateDialog(id);
66     }
67    
68     }
69    
70    
71    
72     @Override
73     protected void onPrepareDialog(int id, Dialog dialog) {
74     super.onPrepareDialog(id, dialog);
75     switch (id) {
76     case DLG_PROGRESS:
77     this.dialog = (ProgressDialog) dialog;
78     break;
79     }
80     }
81    
82     public void startLookup() {
83     isRunning = true;
84     showDialog(DLG_PROGRESS);
85    
86     locator.locateStations();
87     stationsFetched.sendEmptyMessageDelayed(FIXTIMEOUT, 20000);
88     }
89    
90    
91     Handler stationsFetched = new Handler() {
92     @Override
93     public void handleMessage(Message msg) {
94    
95     switch (msg.what) {
96     case GOTLOCATION:
97     dialog.setMessage("Finding nearby stations");
98 torben 241 locatorTask.execute();
99 torben 237 break;
100     case GOTSTATIONLIST:
101     dialog.dismiss();
102 torben 245 if (locator.getStations().size() == 0)
103     MessageBox.showMessage(TrainInfoList.this,"Error loading station list!");
104 torben 237 adapter.setStations( locator.getStations() );
105     break;
106     case NOPROVIDER:
107     dialog.dismiss();
108 torben 245 MessageBox.showMessage(TrainInfoList.this,"No location provider enabled. Plase enable gps.");
109 torben 237 break;
110     case FIXTIMEOUT:
111     dialog.dismiss();
112     if (isRunning) {
113     locator.abortLocationListener();
114 torben 245 MessageBox.showMessage(TrainInfoList.this,"GPS fix timed out");
115 torben 237 }
116     break;
117     case LOOKUPSTATIONFAILED:
118     dialog.dismiss();
119 torben 245 MessageBox.showMessage(TrainInfoList.this,"Error on finding nearby stations");
120 torben 237 break;
121     }
122    
123     isRunning = false;
124     }
125     };
126    
127    
128    
129     @Override
130     protected void onListItemClick(ListView l, View v, int position, long id) {
131     super.onListItemClick(l, v, position, id);
132    
133     StationBean station = adapter.getStation(position);
134    
135    
136     Intent intent = new Intent(this, DepartureList.class);
137     intent.putExtra("name", station.getName());
138     intent.putExtra("address", station.getAddress());
139     intent.putExtra("distance", station.getDistance());
140 torben 239 intent.putExtra("latitude", station.getLatitude());
141     intent.putExtra("longitude", station.getLongitude());
142 torben 237 startActivity(intent);
143     }
144    
145 torben 241
146     class LocatorTask extends AsyncTask<Void,Void,Void> {
147     @Override
148     protected void onPreExecute() {
149     super.onPreExecute();
150     }
151    
152     @Override
153     protected Void doInBackground(Void... params) {
154     locator.findNearestStations();
155     return null;
156     }
157    
158     @Override
159     protected void onPostExecute(Void result) {
160     super.onPostExecute(result);
161     }
162     }
163 torben 237 }

  ViewVC Help
Powered by ViewVC 1.1.20