/[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 260 by torben, Mon Aug 10 19:44:52 2009 UTC android/TrainInfo/src/dk/thoerup/traininfo/StationList.java revision 371 by torben, Wed Sep 30 19:09:59 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 55  public class TrainInfoList extends ListA Line 73  public class TrainInfoList extends ListA
73                          adapter.setStations(stations);                          adapter.setStations(stations);
74                  }                  }
75          }          }
76            
77    
78      @Override      @Override
79      public void onSaveInstanceState(Bundle outState)      public void onSaveInstanceState(Bundle outState)
80      {      {
# Line 67  public class TrainInfoList extends ListA Line 86  public class TrainInfoList extends ListA
86                    
87    
88          @Override          @Override
89            public boolean onCreateOptionsMenu(Menu menu) {
90                    menu.add(0, OPTIONS_MAP, 0, "Show station map");
91                    menu.add(0, OPTIONS_ABOUT, 0, "About");
92                    
93                    return true;
94            }
95    
96            @Override
97            public boolean onOptionsItemSelected(MenuItem item) {
98                    boolean retval;
99                    
100                    switch (item.getItemId()) {
101                    case OPTIONS_MAP:
102                            
103                            Intent intent = new Intent(this,StationMapView.class);
104                            intent.putExtra("userlocation", GeoPair.fromLocation(locator.getLocation()) );
105                            
106                            ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();
107                            for (StationBean st : stations ) {
108                                    stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) );
109                            }
110                            
111                            intent.putExtra("stations", stationPoints);
112                            
113                            startActivity(intent);
114                            retval = true;
115                            break;
116                    case OPTIONS_ABOUT:
117                            String ver = this.getResources().getString(R.string.app_version);
118                            
119                            StringBuffer message = new StringBuffer();
120                            message.append("TrainInfo DK v").append(ver).append("\n");
121                            message.append("By Torben Nielsen\n");
122    
123                            MessageBox.showMessage(this, message.toString());
124                            retval = true;
125                            break;
126                    default:
127                            retval = super.onOptionsItemSelected(item);
128                    }
129                    
130                    return retval;
131            }
132    
133            @Override
134          protected Dialog onCreateDialog(int id) {          protected Dialog onCreateDialog(int id) {
135                  switch (id) {                  switch (id) {
136                  case DLG_PROGRESS:                  case DLG_PROGRESS:
137                          ProgressDialog dlg = new ProgressDialog(this);                          ProgressDialog dlg = new ProgressDialog(this);
138                          dlg.setMessage("Wait for location fix");                          dlg.setMessage("Wait for location fix");
139                          dlg.setCancelable(false);                          dlg.setCancelable(false);
140                          return dlg;                          return dlg;                    
141                  default:                  default:
142                          return super.onCreateDialog(id);                                          return super.onCreateDialog(id);                
143                  }                  }
# Line 88  public class TrainInfoList extends ListA Line 152  public class TrainInfoList extends ListA
152                  switch (id) {                  switch (id) {
153                  case DLG_PROGRESS:                  case DLG_PROGRESS:
154                          this.dialog = (ProgressDialog) dialog;                          this.dialog = (ProgressDialog) dialog;
155                            if (!dialogMessage.equals("")) {
156                                    this.dialog.setMessage(dialogMessage);
157                                    dialogMessage = "";
158                            }
159                          break;                          break;
160                  }                  }
161          }          }
# Line 97  public class TrainInfoList extends ListA Line 165  public class TrainInfoList extends ListA
165                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
166                                    
167                  locator.locateStations();                  locator.locateStations();
168                  stationsFetched.sendEmptyMessageDelayed(FIXTIMEOUT, 20000);                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);
169          }          }
170    
171    
172          Handler stationsFetched = new Handler() {          Handler stationsFetched = new Handler() {
173                  @Override                  @Override
174                  public void handleMessage(Message msg) {                  public void handleMessage(Message msg) {
175    
176                          switch (msg.what) {                          switch (msg.what) {
177                          case GOTLOCATION:                          case GOTLOCATION:
178                                  dialog.setMessage("Finding nearby stations");                                  dismissDialog(DLG_PROGRESS);
179                                  locatorTask.execute();                                  
180                                  break;                                  startLocatorTask();
181                          case GOTSTATIONLIST:                                  
                                 dialog.dismiss();  
                                 if (locator.getStations().size() == 0)  
                                         MessageBox.showMessage(TrainInfoList.this,"Error loading station list!");  
                                 stations = locator.getStations();  
                                 adapter.setStations( stations );  
182                                  break;                                  break;
183    
184                          case NOPROVIDER:                          case NOPROVIDER:
185                                  dialog.dismiss();                                  dismissDialog(DLG_PROGRESS);
186                                  MessageBox.showMessage(TrainInfoList.this,"No location provider enabled. Plase enable gps.");                                  MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps.");
187                                  break;                                  break;
188                          case FIXTIMEOUT:                          case LOCATIONFIXTIMEOUT:                                
                                 dialog.dismiss();  
189                                  if (isRunning) {                                  if (isRunning) {
190                                          locator.abortLocationListener();                                          locator.stopSearch();
191                                          MessageBox.showMessage(TrainInfoList.this,"GPS fix timed out");                                          if (locator.hasLocation()) {
192                                                    stationsFetched.sendEmptyMessage( GOTLOCATION );
193                                            } else {                                                
194                                                    dismissDialog(DLG_PROGRESS);
195                                                    
196                                                    AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
197                                                    builder.setMessage("GPS fix timed out");
198                                                    builder.setCancelable(true);
199                                                    builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
200                                                            public void onClick(DialogInterface dialog, int id) {
201                                                                    dialog.dismiss();
202                                                                    startLookup();
203                                                                    
204                                                            }
205                                                    });
206                                                    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
207                                                            public void onClick(DialogInterface dialog, int id) {
208                                                                    dialog.dismiss();
209                                                            }                                                      
210                                                    });                                                                                            
211                                                    builder.show();
212    
213                                            }
214                                  }                                  }
215                                  break;                                  break;
                         case LOOKUPSTATIONFAILED:  
                                 dialog.dismiss();  
                                 MessageBox.showMessage(TrainInfoList.this,"Error on finding nearby stations");  
                                 break;  
216                          }                          }
217                          isRunning = false;                          isRunning = false;
218                  }                  }
219          };          };
220                    
221                    void startLocatorTask()
222            {
223                    dialogMessage = "Finding nearby stations";
224                    showDialog(DLG_PROGRESS);
225                    
226                    locatorTask = new LocatorTask();
227                    locatorTask.execute();  
228            }
229                    
230          @Override          @Override
231          protected void onListItemClick(ListView l, View v, int position, long id) {          protected void onListItemClick(ListView l, View v, int position, long id) {
232                  super.onListItemClick(l, v, position, id);                  super.onListItemClick(l, v, position, id);
233                                                    
                   
234                  StationBean station = stations.get(position);                  StationBean station = stations.get(position);
235                    
236                    double latitude = station.getLatitude();
237                    double longitude = station.getLongitude();
238    
239    
240                                    
241                  Intent intent = new Intent(this, DepartureList.class);                  Intent intent = new Intent(this, DepartureList.class);
242                  intent.putExtra("name", station.getName());                  intent.putExtra("name", station.getName());
                 intent.putExtra("address", station.getAddress());  
243                  intent.putExtra("distance", station.getDistance());                  intent.putExtra("distance", station.getDistance());
244                  intent.putExtra("latitude", station.getLatitude());                  intent.putExtra("latitude", latitude);
245                  intent.putExtra("longitude", station.getLongitude());                  intent.putExtra("longitude", longitude);
246                    intent.putExtra("stationid", station.getId());
247                    intent.putExtra("address", station.getAddress());
248                  startActivity(intent);                  startActivity(intent);
249          }          }
250    
251            String lookupAddress(double latitude, double longitude) {
252                    
253                    Geocoder coder = new Geocoder(this, new Locale("da"));
254                    StringBuilder sb = new StringBuilder();
255                    Log.i("lookupaddr", "" + latitude + "/" + longitude);
256                    try {
257                            List<Address> addressList = coder.getFromLocation(latitude, longitude, 1);
258                            Address addr = addressList.get(0);
259                            
260                            
261                            int max = addr.getMaxAddressLineIndex();
262                            for (int i=0; i<max; i++) {
263                                    if (i>0)
264                                            sb.append(", ");
265                                    
266                                    sb.append(addr.getAddressLine(i));
267                            }
268                            
269                            
270                    } catch (Exception e) {
271                            Log.e("DepartureList", "geocoder failed", e);
272                    }
273                    
274                    return sb.toString();
275            }
276                    
277          class LocatorTask extends AsyncTask<Void,Void,Void> {          class LocatorTask extends AsyncTask<Void,Void,Void> {
278                    boolean success;
279                  @Override                  @Override
280                  protected void onPreExecute() {                  protected void onPreExecute() {
281    
# Line 165  public class TrainInfoList extends ListA Line 284  public class TrainInfoList extends ListA
284                                    
285                  @Override                  @Override
286                  protected Void doInBackground(Void... params) {                  protected Void doInBackground(Void... params) {
287                          locator.findNearestStations();                          Location loc = locator.getLocation();
288                            success = stationProvider.lookupStations(loc);
289                            
290                            
291                            List<StationBean> stations = stationProvider.getStations();
292                            for (StationBean station : stations) {
293                                    String addr = lookupAddress(station.getLatitude(), station.getLongitude());
294                                    station.setAddress(addr);
295                            }
296                                                    
297                          return null;                          return null;
298                  }                  }
# Line 173  public class TrainInfoList extends ListA Line 300  public class TrainInfoList extends ListA
300                  @Override                  @Override
301                  protected void onPostExecute(Void result) {                  protected void onPostExecute(Void result) {
302                          super.onPostExecute(result);                          super.onPostExecute(result);
303                            dialog.dismiss();
304                            
305                            if (success) {                          
306                                    if (stationProvider.getStations().size() == 0)
307                                            MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?!
308                                    stations = stationProvider.getStations();
309                                    adapter.setStations( stations );                                
310                                    
311                            } else { //communication or parse errors
312                                    AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
313                                    builder.setMessage("Error on finding nearby stations");
314                                    builder.setCancelable(true);
315                                    builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
316                                            public void onClick(DialogInterface dialog, int id) {
317                                                    dialog.dismiss();
318                                                    
319                                                    stationsFetched.post( new Runnable() {
320                                                            @Override
321                                                            public void run() {
322                                                                    startLocatorTask();                                                            
323                                                            }
324                                                    });
325                                            }
326                                    });
327                                    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
328                                            public void onClick(DialogInterface dialog, int id) {
329                                                    dialog.dismiss();
330                                            }                                                      
331                                    });                                                                                            
332                                    builder.show();                        
333                            }
334                  }                  }
335          }          }
336  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20