/[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 331 by torben, Tue Sep 22 13:57:13 2009 UTC android/TrainInfo/src/dk/thoerup/traininfo/StationList.java revision 368 by torben, Wed Sep 30 13:32:20 2009 UTC
# Line 4  import java.util.ArrayList; Line 4  import java.util.ArrayList;
4  import java.util.List;  import java.util.List;
5  import java.util.Locale;  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;  import android.location.Address;
14  import android.location.Geocoder;  import android.location.Geocoder;
# Line 16  import android.os.Bundle; Line 18  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;  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;  import dk.thoerup.traininfo.provider.ProviderFactory;
28  import dk.thoerup.traininfo.provider.StationProvider;  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          LocationLookup 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>();
# Line 49  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                                    
                 //LocationLookup.removeMockLocation(this);  
                 LocationLookup.injectMockLocation(this);  
64                                    
65                  adapter = new StationListAdapter(this);                  adapter = new StationListAdapter(this);
66                  setListAdapter(adapter);                  setListAdapter(adapter);
# Line 75  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 96  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 105  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                                    startLocatorTask();
170                                    
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) {
# Line 222  public class TrainInfoList extends ListA Line 293  public class TrainInfoList extends ListA
293                                                    
294                          if (success) {                                                    if (success) {                          
295                                  if (stationProvider.getStations().size() == 0)                                  if (stationProvider.getStations().size() == 0)
296                                          MessageBox.showMessage(TrainInfoList.this, "No stations found!"); // this should not be possible !?!                                          MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?!
297                                  stations = stationProvider.getStations();                                  stations = stationProvider.getStations();
298                                  adapter.setStations( stations );                                                                  adapter.setStations( stations );                                
299                                                                    
300                          } else { //communication or parse errors                          } else { //communication or parse errors
301                                  MessageBox.showMessage(TrainInfoList.this, "Error finding stations!");                                  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          }          }

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

  ViewVC Help
Powered by ViewVC 1.1.20