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

revision 237 by torben, Sat Aug 8 19:02:20 2009 UTC revision 285 by torben, Fri Aug 28 06:34:42 2009 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfo;  package dk.thoerup.traininfo;
2    
3  import android.app.AlertDialog;  import java.util.ArrayList;
4    import java.util.List;
5    
6  import android.app.Dialog;  import android.app.Dialog;
7  import android.app.ListActivity;  import android.app.ListActivity;
8  import android.app.ProgressDialog;  import android.app.ProgressDialog;
 import android.content.DialogInterface;  
9  import android.content.Intent;  import android.content.Intent;
10    import android.os.AsyncTask;
11  import android.os.Bundle;  import android.os.Bundle;
12  import android.os.Handler;  import android.os.Handler;
13  import android.os.Message;  import android.os.Message;
14    
15  import android.view.View;  import android.view.View;
16  import android.widget.ListView;  import android.widget.ListView;
17    import dk.thoerup.traininfo.util.MessageBox;
18    
19  public class TrainInfoList extends ListActivity  {  public class TrainInfoList extends ListActivity  {
20          public static final int GOTLOCATION = 1;          public static final int GOTLOCATION = 1;
# Line 24  public class TrainInfoList extends ListA Line 28  public class TrainInfoList extends ListA
28          /** Called when the activity is first created. */          /** Called when the activity is first created. */
29          ProgressDialog dialog;          ProgressDialog dialog;
30          StationLocator locator = null;          StationLocator locator = null;
31            LocatorTask locatorTask = new LocatorTask();
32            
33            boolean isRunning = false;
34            List<StationBean> stations = new ArrayList<StationBean>();
35            
36                    
         boolean isRunning;  
   
37          StationListAdapter adapter = null;          StationListAdapter adapter = null;
38            @SuppressWarnings("unchecked")
39          @Override          @Override
40          public void onCreate(Bundle savedInstanceState) {          public void onCreate(Bundle savedInstanceState) {
41                  super.onCreate(savedInstanceState);                  super.onCreate(savedInstanceState);
42                  setContentView(R.layout.main);                  setContentView(R.layout.main);
43                                    
44                    StationLocator.removeMockLocation(this);
45                    //StationLocator.injectMockLocation(this);
46                    
47                  adapter = new StationListAdapter(this);                  adapter = new StationListAdapter(this);
48                  setListAdapter(adapter);                  setListAdapter(adapter);
49                                    
50                  locator = new StationLocator(this, stationsFetched);                  locator = new StationLocator(this, stationsFetched);
51                                    if (savedInstanceState == null) {
52                  startLookup();                          startLookup();
53                    } else {
54                            stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations");
55                            adapter.setStations(stations);
56                    }
57          }          }
58                    
59        @Override
60        public void onSaveInstanceState(Bundle outState)
61        {
62            if (dialog != null && dialog.isShowing())
63                    dialog.dismiss();
64            outState.putSerializable("stations", (ArrayList<StationBean>) stations);
65        }
66            
67                    
68    
69          @Override          @Override
# Line 59  public class TrainInfoList extends ListA Line 82  public class TrainInfoList extends ListA
82                    
83                    
84    
   
85          @Override          @Override
86          protected void onPrepareDialog(int id, Dialog dialog) {          protected void onPrepareDialog(int id, Dialog dialog) {
87                  super.onPrepareDialog(id, dialog);                  super.onPrepareDialog(id, dialog);
# Line 70  public class TrainInfoList extends ListA Line 92  public class TrainInfoList extends ListA
92                  }                  }
93          }          }
94    
   
   
         public void progressDialog() {  
                 dialog = new ProgressDialog(this);  
                 dialog.setMessage("Wait for location fix");  
                 dialog.setCancelable(false);  
                 dialog.show();  
         }  
   
95          public void startLookup() {          public void startLookup() {
96                  isRunning = true;                  isRunning = true;
97                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
                 //progressDialog();  
98                                    
99                  locator.locateStations();                  locator.locateStations();
100                  stationsFetched.sendEmptyMessageDelayed(FIXTIMEOUT, 20000);                              stationsFetched.sendEmptyMessageDelayed(FIXTIMEOUT, 20000);
101          }          }
102    
103    
104          Handler stationsFetched = new Handler() {          Handler stationsFetched = new Handler() {
105                  @Override                  @Override
106                  public void handleMessage(Message msg) {                  public void handleMessage(Message msg) {
                           
107                          switch (msg.what) {                          switch (msg.what) {
108                          case GOTLOCATION:                          case GOTLOCATION:
109                                  dialog.setMessage("Finding nearby stations");                                  dialog.setMessage("Finding nearby stations");
110                                    locatorTask.execute();
111                                  break;                                  break;
112                          case GOTSTATIONLIST:                          case GOTSTATIONLIST:
113                                  dialog.dismiss();                                  dialog.dismiss();
114                                  adapter.setStations( locator.getStations() );                                  if (locator.getStations().size() == 0)
115                                            MessageBox.showMessage(TrainInfoList.this,"Error loading station list!");
116                                    stations = locator.getStations();
117                                    adapter.setStations( stations );
118                                  break;                                  break;
119                          case NOPROVIDER:                          case NOPROVIDER:
120                                  dialog.dismiss();                                  dialog.dismiss();
121                                  showMessageBox("No Location provider enabled. Plase enabled gps.");                                  MessageBox.showMessage(TrainInfoList.this,"No location provider enabled. Plase enable gps.");
122                                  break;                                  break;
123                          case FIXTIMEOUT:                          case FIXTIMEOUT:
124                                  dialog.dismiss();                                  dialog.dismiss();
125                                  if (isRunning) {                                  if (isRunning) {
126                                          locator.abortLocationListener();                                          locator.abortLocationListener();
127                                          showMessageBox("GPS fix timed out");                                          if (locator.hasLocation()) {
128                                                    msg.what = GOTLOCATION;
129                                                    handleMessage( msg ); // ToDo: ugly recursive call !!!
130                                            } else {
131                                                    MessageBox.showMessage(TrainInfoList.this,"GPS fix timed out");
132                                            }
133                                  }                                  }
134                                  break;                                  break;
135                          case LOOKUPSTATIONFAILED:                          case LOOKUPSTATIONFAILED:
136                                  dialog.dismiss();                                  dialog.dismiss();
137                                  showMessageBox("Error on finding nearby stations");                                  MessageBox.showMessage(TrainInfoList.this,"Error on finding nearby stations");
138                                  break;                                  break;
139                          }                          }
                           
140                          isRunning = false;                          isRunning = false;
141                  }                  }
142          };          };
# Line 128  public class TrainInfoList extends ListA Line 147  public class TrainInfoList extends ListA
147          protected void onListItemClick(ListView l, View v, int position, long id) {          protected void onListItemClick(ListView l, View v, int position, long id) {
148                  super.onListItemClick(l, v, position, id);                  super.onListItemClick(l, v, position, id);
149                                    
150                  StationBean station = adapter.getStation(position);                  
151                    StationBean station = stations.get(position);
152                                    
153                                    
154                  Intent intent = new Intent(this, DepartureList.class);                  Intent intent = new Intent(this, DepartureList.class);
155                  intent.putExtra("name", station.getName());                  intent.putExtra("name", station.getName());
156                  intent.putExtra("address", station.getAddress());                  intent.putExtra("address", station.getAddress());
157                  intent.putExtra("distance", station.getDistance());                  intent.putExtra("distance", station.getDistance());
158                    intent.putExtra("latitude", station.getLatitude());
159                    intent.putExtra("longitude", station.getLongitude());
160                  startActivity(intent);                  startActivity(intent);
161          }          }
162    
163          public void showMessageBox(String message) {          
164                  AlertDialog.Builder builder = new AlertDialog.Builder(this);          class LocatorTask extends AsyncTask<Void,Void,Void> {
165                  builder.setMessage(message)                  @Override
166                  .setCancelable(false)                  protected void onPreExecute() {
                 .setPositiveButton("OK", new DialogInterface.OnClickListener() {  
                         public void onClick(DialogInterface dialog, int id) {  
                                 dialog.dismiss();  
                         }  
                 })  
                 .show();  
167    
168                            super.onPreExecute();
169                    }
170                    
171                    @Override
172                    protected Void doInBackground(Void... params) {
173                            locator.findNearestStations();
174                            
175                            return null;
176                    }
177    
178                    @Override
179                    protected void onPostExecute(Void result) {
180                            super.onPostExecute(result);
181    
182                    }
183          }          }
184  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20