/[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

android/TrainInfo/src/dk/thoerup/traininfo/TrainInfoList.java revision 285 by torben, Fri Aug 28 06:34:42 2009 UTC android/TrainInfo/src/dk/thoerup/traininfo/StationList.java revision 368 by torben, Wed Sep 30 13:32:20 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.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.location.Address;
14    import android.location.Geocoder;
15    import android.location.Location;
16  import android.os.AsyncTask;  import android.os.AsyncTask;
17  import android.os.Bundle;  import android.os.Bundle;
18  import android.os.Handler;  import android.os.Handler;
19  import android.os.Message;  import android.os.Message;
20    import android.util.Log;
21    import android.view.Menu;
22    import android.view.MenuItem;
23  import android.view.View;  import android.view.View;
24  import android.widget.ListView;  import android.widget.ListView;
25    
26    
27    import dk.thoerup.traininfo.provider.ProviderFactory;
28    import dk.thoerup.traininfo.provider.StationProvider;
29    import dk.thoerup.traininfo.stationmap.GeoPair;
30    import dk.thoerup.traininfo.stationmap.StationMapView;
31  import dk.thoerup.traininfo.util.MessageBox;  import dk.thoerup.traininfo.util.MessageBox;
32    
33  public class TrainInfoList extends ListActivity  {  public class StationList extends ListActivity  {
34          public static final int GOTLOCATION = 1;          public static final int GOTLOCATION = 1001;
35          public static final int GOTSTATIONLIST = 2;          public static final int GOTSTATIONLIST = 1002;
36          public static final int NOPROVIDER = 3;          public static final int NOPROVIDER = 1003;
37          public static final int FIXTIMEOUT = 4;          public static final int LOCATIONFIXTIMEOUT = 1004;
38          public static final int LOOKUPSTATIONFAILED = 5;          
39            public static final int OPTIONS_MAP = 2001;
40            public static final int OPTIONS_ABOUT = 2002;
41    
42                    
43          public static final int DLG_PROGRESS = 1;          public static final int DLG_PROGRESS = 1001;
44                    
45          /** Called when the activity is first created. */          /** Called when the activity is first created. */
46            String dialogMessage = "";
47          ProgressDialog dialog;          ProgressDialog dialog;
48          StationLocator locator = null;          LocationLookup locator = null;
49          LocatorTask locatorTask = new LocatorTask();          LocatorTask locatorTask;
50                    
51          boolean isRunning = false;          boolean isRunning = false;
52          List<StationBean> stations = new ArrayList<StationBean>();          List<StationBean> stations = new ArrayList<StationBean>();
53                    
54            StationProvider stationProvider = ProviderFactory.getStationProvider();
55            
56                    
57          StationListAdapter adapter = null;          StationListAdapter adapter = null;
58          @SuppressWarnings("unchecked")          @SuppressWarnings("unchecked")
# Line 41  public class TrainInfoList extends ListA Line 61  public class TrainInfoList extends ListA
61                  super.onCreate(savedInstanceState);                  super.onCreate(savedInstanceState);
62                  setContentView(R.layout.main);                  setContentView(R.layout.main);
63                                    
                 StationLocator.removeMockLocation(this);  
                 //StationLocator.injectMockLocation(this);  
64                                    
65                  adapter = new StationListAdapter(this);                  adapter = new StationListAdapter(this);
66                  setListAdapter(adapter);                  setListAdapter(adapter);
67                                    
68                  locator = new StationLocator(this, stationsFetched);                  locator = new LocationLookup(this, stationsFetched);
69                  if (savedInstanceState == null) {                  if (savedInstanceState == null) {
70                          startLookup();                          startLookup();
71                  } else {                  } else {
# Line 67  public class TrainInfoList extends ListA Line 85  public class TrainInfoList extends ListA
85                    
86    
87          @Override          @Override
88            public boolean onCreateOptionsMenu(Menu menu) {
89                    menu.add(0, OPTIONS_MAP, 0, "Show station map");
90                    menu.add(0, OPTIONS_ABOUT, 0, "About");
91                    
92                    return true;
93            }
94    
95            @Override
96            public boolean onOptionsItemSelected(MenuItem item) {
97                    boolean retval;
98                    
99                    switch (item.getItemId()) {
100                    case OPTIONS_MAP:
101                            
102                            Intent intent = new Intent(this,StationMapView.class);
103                            intent.putExtra("userlocation", GeoPair.fromLocation(locator.getLocation()) );
104                            
105                            ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();
106                            for (StationBean st : stations ) {
107                                    stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude()) );
108                            }
109                            
110                            intent.putExtra("stations", stationPoints);
111                            
112                            startActivity(intent);
113                            retval = true;
114                            break;
115                    default:
116                            retval = super.onOptionsItemSelected(item);
117                    }
118                    
119                    return retval;
120            }
121    
122            @Override
123          protected Dialog onCreateDialog(int id) {          protected Dialog onCreateDialog(int id) {
124                  switch (id) {                  switch (id) {
125                  case DLG_PROGRESS:                  case DLG_PROGRESS:
126                          ProgressDialog dlg = new ProgressDialog(this);                          ProgressDialog dlg = new ProgressDialog(this);
127                          dlg.setMessage("Wait for location fix");                          dlg.setMessage("Wait for location fix");
128                          dlg.setCancelable(false);                          dlg.setCancelable(false);
129                          return dlg;                          return dlg;                    
130                  default:                  default:
131                          return super.onCreateDialog(id);                                          return super.onCreateDialog(id);                
132                  }                  }
# Line 88  public class TrainInfoList extends ListA Line 141  public class TrainInfoList extends ListA
141                  switch (id) {                  switch (id) {
142                  case DLG_PROGRESS:                  case DLG_PROGRESS:
143                          this.dialog = (ProgressDialog) dialog;                          this.dialog = (ProgressDialog) dialog;
144                            if (!dialogMessage.equals("")) {
145                                    this.dialog.setMessage(dialogMessage);
146                                    dialogMessage = "";
147                            }
148                          break;                          break;
149                  }                  }
150          }          }
# Line 97  public class TrainInfoList extends ListA Line 154  public class TrainInfoList extends ListA
154                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
155                                    
156                  locator.locateStations();                  locator.locateStations();
157                  stationsFetched.sendEmptyMessageDelayed(FIXTIMEOUT, 20000);                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);
158          }          }
159    
160    
161          Handler stationsFetched = new Handler() {          Handler stationsFetched = new Handler() {
162                  @Override                  @Override
163                  public void handleMessage(Message msg) {                  public void handleMessage(Message msg) {
164    
165                          switch (msg.what) {                          switch (msg.what) {
166                          case GOTLOCATION:                          case GOTLOCATION:
167                                  dialog.setMessage("Finding nearby stations");                                  dismissDialog(DLG_PROGRESS);
168                                  locatorTask.execute();                                  
169                                  break;                                  startLocatorTask();
170                          case GOTSTATIONLIST:                                  
                                 dialog.dismiss();  
                                 if (locator.getStations().size() == 0)  
                                         MessageBox.showMessage(TrainInfoList.this,"Error loading station list!");  
                                 stations = locator.getStations();  
                                 adapter.setStations( stations );  
171                                  break;                                  break;
172    
173                          case NOPROVIDER:                          case NOPROVIDER:
174                                  dialog.dismiss();                                  dismissDialog(DLG_PROGRESS);
175                                  MessageBox.showMessage(TrainInfoList.this,"No location provider enabled. Plase enable gps.");                                  MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps.");
176                                  break;                                  break;
177                          case FIXTIMEOUT:                          case LOCATIONFIXTIMEOUT:                                
                                 dialog.dismiss();  
178                                  if (isRunning) {                                  if (isRunning) {
179                                          locator.abortLocationListener();                                          locator.stopSearch();
180                                          if (locator.hasLocation()) {                                          if (locator.hasLocation()) {
181                                                  msg.what = GOTLOCATION;                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );
182                                                  handleMessage( msg ); // ToDo: ugly recursive call !!!                                          } else {                                                
183                                          } else {                                                  dismissDialog(DLG_PROGRESS);
184                                                  MessageBox.showMessage(TrainInfoList.this,"GPS fix timed out");                                                  
185                                                    AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
186                                                    builder.setMessage("GPS fix timed out");
187                                                    builder.setCancelable(true);
188                                                    builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
189                                                            public void onClick(DialogInterface dialog, int id) {
190                                                                    dialog.dismiss();
191                                                                    startLookup();
192                                                                    
193                                                            }
194                                                    });
195                                                    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
196                                                            public void onClick(DialogInterface dialog, int id) {
197                                                                    dialog.dismiss();
198                                                            }                                                      
199                                                    });                                                                                            
200                                                    builder.show();
201    
202                                          }                                          }
203                                  }                                  }
204                                  break;                                  break;
                         case LOOKUPSTATIONFAILED:  
                                 dialog.dismiss();  
                                 MessageBox.showMessage(TrainInfoList.this,"Error on finding nearby stations");  
                                 break;  
205                          }                          }
206                          isRunning = false;                          isRunning = false;
207                  }                  }
208          };          };
209                    
210                    void startLocatorTask()
211            {
212                    dialogMessage = "Finding nearby stations";
213                    showDialog(DLG_PROGRESS);
214                    
215                    locatorTask = new LocatorTask();
216                    locatorTask.execute();  
217            }
218                    
219          @Override          @Override
220          protected void onListItemClick(ListView l, View v, int position, long id) {          protected void onListItemClick(ListView l, View v, int position, long id) {
221                  super.onListItemClick(l, v, position, id);                  super.onListItemClick(l, v, position, id);
222                                                    
                   
223                  StationBean station = stations.get(position);                  StationBean station = stations.get(position);
224                    
225                    double latitude = station.getLatitude();
226                    double longitude = station.getLongitude();
227    
228    
229                                    
230                  Intent intent = new Intent(this, DepartureList.class);                  Intent intent = new Intent(this, DepartureList.class);
231                  intent.putExtra("name", station.getName());                  intent.putExtra("name", station.getName());
                 intent.putExtra("address", station.getAddress());  
232                  intent.putExtra("distance", station.getDistance());                  intent.putExtra("distance", station.getDistance());
233                  intent.putExtra("latitude", station.getLatitude());                  intent.putExtra("latitude", latitude);
234                  intent.putExtra("longitude", station.getLongitude());                  intent.putExtra("longitude", longitude);
235                    intent.putExtra("stationid", station.getId());
236                    intent.putExtra("address", station.getAddress());
237                  startActivity(intent);                  startActivity(intent);
238          }          }
239    
240            String lookupAddress(double latitude, double longitude) {
241                    
242                    Geocoder coder = new Geocoder(this, new Locale("da"));
243                    StringBuilder sb = new StringBuilder();
244                    Log.i("lookupaddr", "" + latitude + "/" + longitude);
245                    try {
246                            List<Address> addressList = coder.getFromLocation(latitude, longitude, 1);
247                            Address addr = addressList.get(0);
248                            
249                            
250                            int max = addr.getMaxAddressLineIndex();
251                            for (int i=0; i<max; i++) {
252                                    if (i>0)
253                                            sb.append(", ");
254                                    
255                                    sb.append(addr.getAddressLine(i));
256                            }
257                            
258                            
259                    } catch (Exception e) {
260                            Log.e("DepartureList", "geocoder failed", e);
261                    }
262                    
263                    return sb.toString();
264            }
265                    
266          class LocatorTask extends AsyncTask<Void,Void,Void> {          class LocatorTask extends AsyncTask<Void,Void,Void> {
267                    boolean success;
268                  @Override                  @Override
269                  protected void onPreExecute() {                  protected void onPreExecute() {
270    
# Line 170  public class TrainInfoList extends ListA Line 273  public class TrainInfoList extends ListA
273                                    
274                  @Override                  @Override
275                  protected Void doInBackground(Void... params) {                  protected Void doInBackground(Void... params) {
276                          locator.findNearestStations();                          Location loc = locator.getLocation();
277                            success = stationProvider.lookupStations(loc);
278                            
279                            
280                            List<StationBean> stations = stationProvider.getStations();
281                            for (StationBean station : stations) {
282                                    String addr = lookupAddress(station.getLatitude(), station.getLongitude());
283                                    station.setAddress(addr);
284                            }
285                                                    
286                          return null;                          return null;
287                  }                  }
# Line 178  public class TrainInfoList extends ListA Line 289  public class TrainInfoList extends ListA
289                  @Override                  @Override
290                  protected void onPostExecute(Void result) {                  protected void onPostExecute(Void result) {
291                          super.onPostExecute(result);                          super.onPostExecute(result);
292                            dialog.dismiss();
293                            
294                            if (success) {                          
295                                    if (stationProvider.getStations().size() == 0)
296                                            MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?!
297                                    stations = stationProvider.getStations();
298                                    adapter.setStations( stations );                                
299                                    
300                            } else { //communication or parse errors
301                                    AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
302                                    builder.setMessage("Error on finding nearby stations");
303                                    builder.setCancelable(true);
304                                    builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
305                                            public void onClick(DialogInterface dialog, int id) {
306                                                    dialog.dismiss();
307                                                    
308                                                    stationsFetched.post( new Runnable() {
309                                                            @Override
310                                                            public void run() {
311                                                                    startLocatorTask();                                                            
312                                                            }
313                                                    });
314                                            }
315                                    });
316                                    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
317                                            public void onClick(DialogInterface dialog, int id) {
318                                                    dialog.dismiss();
319                                            }                                                      
320                                    });                                                                                            
321                                    builder.show();                        
322                            }
323                  }                  }
324          }          }
325  }  }

Legend:
Removed from v.285  
changed lines
  Added in v.368

  ViewVC Help
Powered by ViewVC 1.1.20