/[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 374 by torben, Wed Sep 30 20:38:47 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;
         public static final int LOOKUPSTATIONFAILED = 5;  
38                    
39          public static final int DLG_PROGRESS = 1;          public static final int OPTIONS_MAP = 2001;
40            public static final int OPTIONS_ABOUT = 2002;
41    
42            
43            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            GeoPair location = new GeoPair();
52                    
53          boolean isRunning = false;          boolean isRunning = false;
54          List<StationBean> stations = new ArrayList<StationBean>();          List<StationBean> stations = new ArrayList<StationBean>();
# Line 49  public class TrainInfoList extends ListA Line 63  public class TrainInfoList extends ListA
63                  super.onCreate(savedInstanceState);                  super.onCreate(savedInstanceState);
64                  setContentView(R.layout.main);                  setContentView(R.layout.main);
65                                    
                 //LocationLookup.removeMockLocation(this);  
                 LocationLookup.injectMockLocation(this);  
66                                    
67                  adapter = new StationListAdapter(this);                  adapter = new StationListAdapter(this);
68                  setListAdapter(adapter);                  setListAdapter(adapter);
# Line 61  public class TrainInfoList extends ListA Line 73  public class TrainInfoList extends ListA
73                  } else {                  } else {
74                          stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations");                          stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations");
75                          adapter.setStations(stations);                          adapter.setStations(stations);
76                            location = (GeoPair) savedInstanceState.getSerializable("location");
77                  }                  }
78          }          }
79            
80    
81      @Override      @Override
82      public void onSaveInstanceState(Bundle outState)      public void onSaveInstanceState(Bundle outState)
83      {      {
84          if (dialog != null && dialog.isShowing())          if (dialog != null && dialog.isShowing())
85                  dialog.dismiss();                  dialog.dismiss();
86          outState.putSerializable("stations", (ArrayList<StationBean>) stations);          outState.putSerializable("stations", (ArrayList<StationBean>) stations);
87            outState.putSerializable("location", location);
88      }      }
89                    
90                    
91    
92          @Override          @Override
93            public boolean onCreateOptionsMenu(Menu menu) {
94                    menu.add(0, OPTIONS_MAP, 0, "Show station map");
95                    menu.add(0, OPTIONS_ABOUT, 0, "About");
96                    
97                    return true;
98            }
99    
100            @Override
101            public boolean onOptionsItemSelected(MenuItem item) {
102                    boolean retval;
103                    
104                    switch (item.getItemId()) {
105                    case OPTIONS_MAP:
106                            
107                            Intent intent = new Intent(this,StationMapView.class);
108                            intent.putExtra("userlocation", location );
109                            
110                            ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();
111                            for (StationBean st : stations ) {
112                                    stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) );
113                            }
114                            
115                            intent.putExtra("stations", stationPoints);
116                            
117                            startActivity(intent);
118                            retval = true;
119                            break;
120                    case OPTIONS_ABOUT:
121                            String ver = this.getResources().getString(R.string.app_version);
122                            
123                            StringBuffer message = new StringBuffer();
124                            message.append("TrainInfo DK v").append(ver).append("\n");
125                            message.append("By Torben Nielsen\n");
126    
127                            MessageBox.showMessage(this, message.toString());
128                            retval = true;
129                            break;
130                    default:
131                            retval = super.onOptionsItemSelected(item);
132                    }
133                    
134                    return retval;
135            }
136    
137            @Override
138          protected Dialog onCreateDialog(int id) {          protected Dialog onCreateDialog(int id) {
139                  switch (id) {                  switch (id) {
140                  case DLG_PROGRESS:                  case DLG_PROGRESS:
141                          ProgressDialog dlg = new ProgressDialog(this);                          ProgressDialog dlg = new ProgressDialog(this);
142                          dlg.setMessage("Wait for location fix");                          dlg.setMessage("Wait for location fix");
143                          dlg.setCancelable(false);                          dlg.setCancelable(false);
144                          return dlg;                          return dlg;                    
145                  default:                  default:
146                          return super.onCreateDialog(id);                                          return super.onCreateDialog(id);                
147                  }                  }
# Line 96  public class TrainInfoList extends ListA Line 156  public class TrainInfoList extends ListA
156                  switch (id) {                  switch (id) {
157                  case DLG_PROGRESS:                  case DLG_PROGRESS:
158                          this.dialog = (ProgressDialog) dialog;                          this.dialog = (ProgressDialog) dialog;
159                            if (!dialogMessage.equals("")) {
160                                    this.dialog.setMessage(dialogMessage);
161                                    dialogMessage = "";
162                            }
163                          break;                          break;
164                  }                  }
165          }          }
# Line 105  public class TrainInfoList extends ListA Line 169  public class TrainInfoList extends ListA
169                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
170                                    
171                  locator.locateStations();                  locator.locateStations();
172                  stationsFetched.sendEmptyMessageDelayed(FIXTIMEOUT, 20000);                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);
173          }          }
174    
175    
176          Handler stationsFetched = new Handler() {          Handler stationsFetched = new Handler() {
177                  @Override                  @Override
178                  public void handleMessage(Message msg) {                  public void handleMessage(Message msg) {
179    
180                          switch (msg.what) {                          switch (msg.what) {
181                          case GOTLOCATION:                          case GOTLOCATION:
182                                  dialog.setMessage("Finding nearby stations");                                  dismissDialog(DLG_PROGRESS);
183                                  locatorTask.execute();                                  
184                                    startLocatorTask();
185                                    location = GeoPair.fromLocation( locator.getLocation() );
186                                    
187                                  break;                                  break;
188    
189                          case NOPROVIDER:                          case NOPROVIDER:
190                                  dialog.dismiss();                                  dismissDialog(DLG_PROGRESS);
191                                  MessageBox.showMessage(TrainInfoList.this,"No location provider enabled. Plase enable gps.");                                  MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps.");
192                                  break;                                  break;
193                          case FIXTIMEOUT:                          case LOCATIONFIXTIMEOUT:                                
                                 dialog.dismiss();  
194                                  if (isRunning) {                                  if (isRunning) {
195                                          locator.abortLocationListener();                                          locator.stopSearch();
196                                          if (locator.hasLocation()) {                                          if (locator.hasLocation()) {
197                                                  msg.what = GOTLOCATION;                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );
198                                                  handleMessage( msg ); // ToDo: ugly recursive call !!!                                          } else {                                                
199                                          } else {                                                  dismissDialog(DLG_PROGRESS);
200                                                  MessageBox.showMessage(TrainInfoList.this,"GPS fix timed out");                                                  
201                                                    AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
202                                                    builder.setMessage("GPS fix timed out");
203                                                    builder.setCancelable(true);
204                                                    builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
205                                                            public void onClick(DialogInterface dialog, int id) {
206                                                                    dialog.dismiss();
207                                                                    startLookup();
208                                                                    
209                                                            }
210                                                    });
211                                                    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
212                                                            public void onClick(DialogInterface dialog, int id) {
213                                                                    dialog.dismiss();
214                                                            }                                                      
215                                                    });                                                                                            
216                                                    builder.show();
217    
218                                          }                                          }
219                                  }                                  }
220                                  break;                                  break;
                         case LOOKUPSTATIONFAILED:  
                                 dialog.dismiss();  
                                 MessageBox.showMessage(TrainInfoList.this,"Error on finding nearby stations");  
                                 break;  
221                          }                          }
222                          isRunning = false;                          isRunning = false;
223                  }                  }
224          };          };
225                    
226                    void startLocatorTask()
227            {
228                    dialogMessage = "Finding nearby stations";
229                    showDialog(DLG_PROGRESS);
230                    
231                    locatorTask = new LocatorTask();
232                    locatorTask.execute();  
233            }
234                    
235          @Override          @Override
236          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 309  public class TrainInfoList extends ListA
309                                                    
310                          if (success) {                                                    if (success) {                          
311                                  if (stationProvider.getStations().size() == 0)                                  if (stationProvider.getStations().size() == 0)
312                                          MessageBox.showMessage(TrainInfoList.this, "No stations found!"); // this should not be possible !?!                                          MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?!
313                                  stations = stationProvider.getStations();                                  stations = stationProvider.getStations();
314                                  adapter.setStations( stations );                                                                  adapter.setStations( stations );                                
315                                                                    
316                          } else { //communication or parse errors                          } else { //communication or parse errors
317                                  MessageBox.showMessage(TrainInfoList.this, "Error finding stations!");                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
318                                    builder.setMessage("Error on finding nearby stations");
319                                    builder.setCancelable(true);
320                                    builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
321                                            public void onClick(DialogInterface dialog, int id) {
322                                                    dialog.dismiss();
323                                                    
324                                                    stationsFetched.post( new Runnable() {
325                                                            @Override
326                                                            public void run() {
327                                                                    startLocatorTask();                                                            
328                                                            }
329                                                    });
330                                            }
331                                    });
332                                    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
333                                            public void onClick(DialogInterface dialog, int id) {
334                                                    dialog.dismiss();
335                                            }                                                      
336                                    });                                                                                            
337                                    builder.show();                        
338                          }                          }
339                  }                  }
340          }          }

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

  ViewVC Help
Powered by ViewVC 1.1.20