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

Diff of /android/TrainInfo/src/dk/thoerup/traininfo/StationList.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 260 by torben, Mon Aug 10 19:44:52 2009 UTC revision 319 by torben, Fri Sep 11 12:24:53 2009 UTC
# Line 2  package dk.thoerup.traininfo; Line 2  package dk.thoerup.traininfo;
2    
3  import java.util.ArrayList;  import java.util.ArrayList;
4  import java.util.List;  import java.util.List;
5    import java.util.Locale;
6    
7  import android.app.Dialog;  import android.app.Dialog;
8  import android.app.ListActivity;  import android.app.ListActivity;
9  import android.app.ProgressDialog;  import android.app.ProgressDialog;
10  import android.content.Intent;  import android.content.Intent;
11    import android.location.Address;
12    import android.location.Geocoder;
13    import android.location.Location;
14  import android.os.AsyncTask;  import android.os.AsyncTask;
15  import android.os.Bundle;  import android.os.Bundle;
16  import android.os.Handler;  import android.os.Handler;
17  import android.os.Message;  import android.os.Message;
18    import android.util.Log;
19  import android.view.View;  import android.view.View;
20  import android.widget.ListView;  import android.widget.ListView;
21    import dk.thoerup.traininfo.provider.ProviderFactory;
22    import dk.thoerup.traininfo.provider.StationProvider;
23  import dk.thoerup.traininfo.util.MessageBox;  import dk.thoerup.traininfo.util.MessageBox;
24    
25  public class TrainInfoList extends ListActivity  {  public class TrainInfoList extends ListActivity  {
# Line 27  public class TrainInfoList extends ListA Line 33  public class TrainInfoList extends ListA
33                    
34          /** Called when the activity is first created. */          /** Called when the activity is first created. */
35          ProgressDialog dialog;          ProgressDialog dialog;
36          StationLocator locator = null;          LocationLookup locator = null;
37          LocatorTask locatorTask = new LocatorTask();          LocatorTask locatorTask = new LocatorTask();
38                    
39          boolean isRunning = false;          boolean isRunning = false;
40          List<StationBean> stations = new ArrayList<StationBean>();          List<StationBean> stations = new ArrayList<StationBean>();
41                    
42            StationProvider stationProvider = ProviderFactory.getStationProvider();
43            
44                    
45          StationListAdapter adapter = null;          StationListAdapter adapter = null;
46          @SuppressWarnings("unchecked")          @SuppressWarnings("unchecked")
# Line 41  public class TrainInfoList extends ListA Line 49  public class TrainInfoList extends ListA
49                  super.onCreate(savedInstanceState);                  super.onCreate(savedInstanceState);
50                  setContentView(R.layout.main);                  setContentView(R.layout.main);
51                                    
52                  StationLocator.removeMockLocation(this);                  LocationLookup.removeMockLocation(this);
53                  //StationLocator.injectMockLocation(this);                  //StationLocator.injectMockLocation(this);
54                                    
55                  adapter = new StationListAdapter(this);                  adapter = new StationListAdapter(this);
56                  setListAdapter(adapter);                  setListAdapter(adapter);
57                                    
58                  locator = new StationLocator(this, stationsFetched);                  locator = new LocationLookup(this, stationsFetched);
59                  if (savedInstanceState == null) {                  if (savedInstanceState == null) {
60                          startLookup();                          startLookup();
61                  } else {                  } else {
# Line 109  public class TrainInfoList extends ListA Line 117  public class TrainInfoList extends ListA
117                                  dialog.setMessage("Finding nearby stations");                                  dialog.setMessage("Finding nearby stations");
118                                  locatorTask.execute();                                  locatorTask.execute();
119                                  break;                                  break;
120                          case GOTSTATIONLIST:  
                                 dialog.dismiss();  
                                 if (locator.getStations().size() == 0)  
                                         MessageBox.showMessage(TrainInfoList.this,"Error loading station list!");  
                                 stations = locator.getStations();  
                                 adapter.setStations( stations );  
                                 break;  
121                          case NOPROVIDER:                          case NOPROVIDER:
122                                  dialog.dismiss();                                  dialog.dismiss();
123                                  MessageBox.showMessage(TrainInfoList.this,"No location provider enabled. Plase enable gps.");                                  MessageBox.showMessage(TrainInfoList.this,"No location provider enabled. Plase enable gps.");
# Line 124  public class TrainInfoList extends ListA Line 126  public class TrainInfoList extends ListA
126                                  dialog.dismiss();                                  dialog.dismiss();
127                                  if (isRunning) {                                  if (isRunning) {
128                                          locator.abortLocationListener();                                          locator.abortLocationListener();
129                                          MessageBox.showMessage(TrainInfoList.this,"GPS fix timed out");                                          if (locator.hasLocation()) {
130                                                    msg.what = GOTLOCATION;
131                                                    handleMessage( msg ); // ToDo: ugly recursive call !!!
132                                            } else {
133                                                    MessageBox.showMessage(TrainInfoList.this,"GPS fix timed out");
134                                            }
135                                  }                                  }
136                                  break;                                  break;
137                          case LOOKUPSTATIONFAILED:                          case LOOKUPSTATIONFAILED:
# Line 141  public class TrainInfoList extends ListA Line 148  public class TrainInfoList extends ListA
148          @Override          @Override
149          protected void onListItemClick(ListView l, View v, int position, long id) {          protected void onListItemClick(ListView l, View v, int position, long id) {
150                  super.onListItemClick(l, v, position, id);                  super.onListItemClick(l, v, position, id);
151                                                    
                   
152                  StationBean station = stations.get(position);                  StationBean station = stations.get(position);
153                    
154                    double latitude = station.getLatitude();
155                    double longitude = station.getLongitude();
156    
157    
158                                    
159                  Intent intent = new Intent(this, DepartureList.class);                  Intent intent = new Intent(this, DepartureList.class);
160                  intent.putExtra("name", station.getName());                  intent.putExtra("name", station.getName());
                 intent.putExtra("address", station.getAddress());  
161                  intent.putExtra("distance", station.getDistance());                  intent.putExtra("distance", station.getDistance());
162                  intent.putExtra("latitude", station.getLatitude());                  intent.putExtra("latitude", latitude);
163                  intent.putExtra("longitude", station.getLongitude());                  intent.putExtra("longitude", longitude);
164                    intent.putExtra("stationid", station.getId());
165                    intent.putExtra("address", station.getAddress());
166                  startActivity(intent);                  startActivity(intent);
167          }          }
168    
169            String lookupAddress(double latitude, double longitude) {
170                    
171                    Geocoder coder = new Geocoder(this, new Locale("da"));
172                    StringBuilder sb = new StringBuilder();
173                    Log.i("lookupaddr", "" + latitude + "/" + longitude);
174                    try {
175                            List<Address> addressList = coder.getFromLocation(latitude, longitude, 1);
176                            Address addr = addressList.get(0);
177                            
178                            
179                            int max = addr.getMaxAddressLineIndex();
180                            for (int i=0; i<max; i++) {
181                                    if (i>0)
182                                            sb.append(", ");
183                                    
184                                    sb.append(addr.getAddressLine(i));
185                            }
186                            
187                            
188                    } catch (Exception e) {
189                            Log.e("DepartureList", "geocoder failed", e);
190                    }
191                    
192                    return sb.toString();
193            }
194                    
195          class LocatorTask extends AsyncTask<Void,Void,Void> {          class LocatorTask extends AsyncTask<Void,Void,Void> {
196                    boolean success;
197                  @Override                  @Override
198                  protected void onPreExecute() {                  protected void onPreExecute() {
199    
# Line 165  public class TrainInfoList extends ListA Line 202  public class TrainInfoList extends ListA
202                                    
203                  @Override                  @Override
204                  protected Void doInBackground(Void... params) {                  protected Void doInBackground(Void... params) {
205                          locator.findNearestStations();                          Location loc = locator.getLocation();
206                            success = stationProvider.lookupStations(loc);
207                            
208                            
209                            List<StationBean> stations = stationProvider.getStations();
210                            for (StationBean station : stations) {
211                                    String addr = lookupAddress(station.getLatitude(), station.getLongitude());
212                                    station.setAddress(addr);
213                            }
214                                                    
215                          return null;                          return null;
216                  }                  }
# Line 173  public class TrainInfoList extends ListA Line 218  public class TrainInfoList extends ListA
218                  @Override                  @Override
219                  protected void onPostExecute(Void result) {                  protected void onPostExecute(Void result) {
220                          super.onPostExecute(result);                          super.onPostExecute(result);
221                            dialog.dismiss();
222                            
223                            if (success) {                          
224                                    if (stationProvider.getStations().size() == 0)
225                                            MessageBox.showMessage(TrainInfoList.this, "No stations found!"); // this should not be possible !?!
226                                    stations = stationProvider.getStations();
227                                    adapter.setStations( stations );                                
228                                    
229                            } else { //communication or parse errors
230                                    MessageBox.showMessage(TrainInfoList.this, "Error finding stations!");
231                            }
232                  }                  }
233          }          }
234  }  }

Legend:
Removed from v.260  
changed lines
  Added in v.319

  ViewVC Help
Powered by ViewVC 1.1.20