/[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 237 by torben, Sat Aug 8 19:02:20 2009 UTC android/TrainInfo/src/dk/thoerup/traininfo/StationList.java revision 336 by torben, Wed Sep 23 12:51:49 2009 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfo;  package dk.thoerup.traininfo;
2    
3    import java.util.ArrayList;
4    import java.util.List;
5    import java.util.Locale;
6    
7  import android.app.AlertDialog;  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;  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;
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.View;  import android.view.View;
22  import android.widget.ListView;  import android.widget.ListView;
23    import dk.thoerup.traininfo.provider.ProviderFactory;
24    import dk.thoerup.traininfo.provider.StationProvider;
25    import dk.thoerup.traininfo.util.MessageBox;
26    
27    public class StationList extends ListActivity  {
28            public static final int GOTLOCATION = 1001;
29            public static final int GOTSTATIONLIST = 1002;
30            public static final int NOPROVIDER = 1003;
31            public static final int LOCATIONFIXTIMEOUT = 1004;
32    
 public class TrainInfoList extends ListActivity  {  
         public static final int GOTLOCATION = 1;  
         public static final int GOTSTATIONLIST = 2;  
         public static final int NOPROVIDER = 3;  
         public static final int FIXTIMEOUT = 4;  
         public static final int LOOKUPSTATIONFAILED = 5;  
33                    
34          public static final int DLG_PROGRESS = 1;          public static final int DLG_PROGRESS = 1001;
35                    
36          /** Called when the activity is first created. */          /** Called when the activity is first created. */
37            String dialogMessage = "";
38          ProgressDialog dialog;          ProgressDialog dialog;
39          StationLocator locator = null;          LocationLookup locator = null;
40            LocatorTask locatorTask;
41            
42            boolean isRunning = false;
43            List<StationBean> stations = new ArrayList<StationBean>();
44            
45            StationProvider stationProvider = ProviderFactory.getStationProvider();
46            
47                    
         boolean isRunning;  
   
48          StationListAdapter adapter = null;          StationListAdapter adapter = null;
49            @SuppressWarnings("unchecked")
50          @Override          @Override
51          public void onCreate(Bundle savedInstanceState) {          public void onCreate(Bundle savedInstanceState) {
52                  super.onCreate(savedInstanceState);                  super.onCreate(savedInstanceState);
53                  setContentView(R.layout.main);                  setContentView(R.layout.main);
54                                    
55                    LocationLookup.removeMockLocation(this);
56                    //LocationLookup.injectMockLocation(this);
57                    
58                  adapter = new StationListAdapter(this);                  adapter = new StationListAdapter(this);
59                  setListAdapter(adapter);                  setListAdapter(adapter);
60                                    
61                  locator = new StationLocator(this, stationsFetched);                  locator = new LocationLookup(this, stationsFetched);
62                                    if (savedInstanceState == null) {
63                  startLookup();                          startLookup();
64                    } else {
65                            stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations");
66                            adapter.setStations(stations);
67                    }
68          }          }
69                    
70        @Override
71        public void onSaveInstanceState(Bundle outState)
72        {
73            if (dialog != null && dialog.isShowing())
74                    dialog.dismiss();
75            outState.putSerializable("stations", (ArrayList<StationBean>) stations);
76        }
77            
78                    
79    
80          @Override          @Override
# Line 50  public class TrainInfoList extends ListA Line 84  public class TrainInfoList extends ListA
84                          ProgressDialog dlg = new ProgressDialog(this);                          ProgressDialog dlg = new ProgressDialog(this);
85                          dlg.setMessage("Wait for location fix");                          dlg.setMessage("Wait for location fix");
86                          dlg.setCancelable(false);                          dlg.setCancelable(false);
87                          return dlg;                          return dlg;                    
88                  default:                  default:
89                          return super.onCreateDialog(id);                                          return super.onCreateDialog(id);                
90                  }                  }
# Line 59  public class TrainInfoList extends ListA Line 93  public class TrainInfoList extends ListA
93                    
94                    
95    
   
96          @Override          @Override
97          protected void onPrepareDialog(int id, Dialog dialog) {          protected void onPrepareDialog(int id, Dialog dialog) {
98                  super.onPrepareDialog(id, dialog);                  super.onPrepareDialog(id, dialog);
99                  switch (id) {                  switch (id) {
100                  case DLG_PROGRESS:                  case DLG_PROGRESS:
101                          this.dialog = (ProgressDialog) dialog;                          this.dialog = (ProgressDialog) dialog;
102                            if (!dialogMessage.equals("")) {
103                                    this.dialog.setMessage(dialogMessage);
104                                    dialogMessage = "";
105                            }
106                          break;                          break;
107                  }                  }
108          }          }
109    
   
   
         public void progressDialog() {  
                 dialog = new ProgressDialog(this);  
                 dialog.setMessage("Wait for location fix");  
                 dialog.setCancelable(false);  
                 dialog.show();  
         }  
   
110          public void startLookup() {          public void startLookup() {
111                  isRunning = true;                  isRunning = true;
112                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
                 //progressDialog();  
113                                    
114                  locator.locateStations();                  locator.locateStations();
115                  stationsFetched.sendEmptyMessageDelayed(FIXTIMEOUT, 20000);                              stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);
116          }          }
117    
118    
119          Handler stationsFetched = new Handler() {          Handler stationsFetched = new Handler() {
120                  @Override                  @Override
121                  public void handleMessage(Message msg) {                  public void handleMessage(Message msg) {
122                            
123                          switch (msg.what) {                          switch (msg.what) {
124                          case GOTLOCATION:                          case GOTLOCATION:
125                                  dialog.setMessage("Finding nearby stations");                                  dismissDialog(DLG_PROGRESS);
126                                  break;                                  
127                          case GOTSTATIONLIST:                                  startLocatorTask();
128                                  dialog.dismiss();                                  
                                 adapter.setStations( locator.getStations() );  
129                                  break;                                  break;
130    
131                          case NOPROVIDER:                          case NOPROVIDER:
132                                  dialog.dismiss();                                  dismissDialog(DLG_PROGRESS);
133                                  showMessageBox("No Location provider enabled. Plase enabled gps.");                                  MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps.");
134                                  break;                                  break;
135                          case FIXTIMEOUT:                          case LOCATIONFIXTIMEOUT:                                
                                 dialog.dismiss();  
136                                  if (isRunning) {                                  if (isRunning) {
137                                          locator.abortLocationListener();                                          locator.stopSearch();
138                                          showMessageBox("GPS fix timed out");                                          if (locator.hasLocation()) {
139                                                    stationsFetched.sendEmptyMessage( GOTLOCATION );
140                                            } else {                                                
141                                                    dismissDialog(DLG_PROGRESS);
142                                                    
143                                                    AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
144                                                    builder.setMessage("GPS fix timed out");
145                                                    builder.setCancelable(true);
146                                                    builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
147                                                            public void onClick(DialogInterface dialog, int id) {
148                                                                    dialog.dismiss();
149                                                                    startLookup();
150                                                                    
151                                                            }
152                                                    });
153                                                    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
154                                                            public void onClick(DialogInterface dialog, int id) {
155                                                                    dialog.dismiss();
156                                                            }                                                      
157                                                    });                                                                                            
158                                                    builder.show();
159    
160                                            }
161                                  }                                  }
162                                  break;                                  break;
                         case LOOKUPSTATIONFAILED:  
                                 dialog.dismiss();  
                                 showMessageBox("Error on finding nearby stations");  
                                 break;  
163                          }                          }
                           
164                          isRunning = false;                          isRunning = false;
165                  }                  }
166          };          };
167                    
168                    void startLocatorTask()
169            {
170                    dialogMessage = "Finding nearby stations";
171                    showDialog(DLG_PROGRESS);
172                    
173                    locatorTask = new LocatorTask();
174                    locatorTask.execute();  
175            }
176                    
177          @Override          @Override
178          protected void onListItemClick(ListView l, View v, int position, long id) {          protected void onListItemClick(ListView l, View v, int position, long id) {
179                  super.onListItemClick(l, v, position, id);                  super.onListItemClick(l, v, position, id);
180                                                    
181                  StationBean station = adapter.getStation(position);                  StationBean station = stations.get(position);
182                    
183                    double latitude = station.getLatitude();
184                    double longitude = station.getLongitude();
185    
186    
187                                    
188                  Intent intent = new Intent(this, DepartureList.class);                  Intent intent = new Intent(this, DepartureList.class);
189                  intent.putExtra("name", station.getName());                  intent.putExtra("name", station.getName());
                 intent.putExtra("address", station.getAddress());  
190                  intent.putExtra("distance", station.getDistance());                  intent.putExtra("distance", station.getDistance());
191                    intent.putExtra("latitude", latitude);
192                    intent.putExtra("longitude", longitude);
193                    intent.putExtra("stationid", station.getId());
194                    intent.putExtra("address", station.getAddress());
195                  startActivity(intent);                  startActivity(intent);
196          }          }
197    
198          public void showMessageBox(String message) {          String lookupAddress(double latitude, double longitude) {
199                  AlertDialog.Builder builder = new AlertDialog.Builder(this);                  
200                  builder.setMessage(message)                  Geocoder coder = new Geocoder(this, new Locale("da"));
201                  .setCancelable(false)                  StringBuilder sb = new StringBuilder();
202                  .setPositiveButton("OK", new DialogInterface.OnClickListener() {                  Log.i("lookupaddr", "" + latitude + "/" + longitude);
203                          public void onClick(DialogInterface dialog, int id) {                  try {
204                                  dialog.dismiss();                          List<Address> addressList = coder.getFromLocation(latitude, longitude, 1);
205                            Address addr = addressList.get(0);
206                            
207                            
208                            int max = addr.getMaxAddressLineIndex();
209                            for (int i=0; i<max; i++) {
210                                    if (i>0)
211                                            sb.append(", ");
212                                    
213                                    sb.append(addr.getAddressLine(i));
214                            }
215                            
216                            
217                    } catch (Exception e) {
218                            Log.e("DepartureList", "geocoder failed", e);
219                    }
220                    
221                    return sb.toString();
222            }
223            
224            class LocatorTask extends AsyncTask<Void,Void,Void> {
225                    boolean success;
226                    @Override
227                    protected void onPreExecute() {
228    
229                            super.onPreExecute();
230                    }
231                    
232                    @Override
233                    protected Void doInBackground(Void... params) {
234                            Location loc = locator.getLocation();
235                            success = stationProvider.lookupStations(loc);
236                            
237                            
238                            List<StationBean> stations = stationProvider.getStations();
239                            for (StationBean station : stations) {
240                                    String addr = lookupAddress(station.getLatitude(), station.getLongitude());
241                                    station.setAddress(addr);
242                          }                          }
243                  })                          
244                  .show();                          return null;
245                    }
246    
247                    @Override
248                    protected void onPostExecute(Void result) {
249                            super.onPostExecute(result);
250                            dialog.dismiss();
251                            
252                            if (success) {                          
253                                    if (stationProvider.getStations().size() == 0)
254                                            MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?!
255                                    stations = stationProvider.getStations();
256                                    adapter.setStations( stations );                                
257                                    
258                            } else { //communication or parse errors
259                                    AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
260                                    builder.setMessage("Error on finding nearby stations");
261                                    builder.setCancelable(true);
262                                    builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
263                                            public void onClick(DialogInterface dialog, int id) {
264                                                    dialog.dismiss();
265                                                    
266                                                    stationsFetched.post( new Runnable() {
267                                                            @Override
268                                                            public void run() {
269                                                                    startLocatorTask();                                                            
270                                                            }
271                                                    });
272                                            }
273                                    });
274                                    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
275                                            public void onClick(DialogInterface dialog, int id) {
276                                                    dialog.dismiss();
277                                            }                                                      
278                                    });                                                                                            
279                                    builder.show();                        
280                            }
281                    }
282          }          }
283  }  }

Legend:
Removed from v.237  
changed lines
  Added in v.336

  ViewVC Help
Powered by ViewVC 1.1.20