/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java
ViewVC logotype

Diff of /android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 917 by torben, Sat Jun 26 10:50:34 2010 UTC revision 1017 by torben, Tue Aug 24 19:36:27 2010 UTC
# Line 7  import static dk.thoerup.traininfo.R.str Line 7  import static dk.thoerup.traininfo.R.str
7    
8    
9  import java.text.NumberFormat;  import java.text.NumberFormat;
10  import java.util.ArrayList;  
 import java.util.List;  
11    
12  import android.app.AlertDialog;  import android.app.AlertDialog;
13  import android.app.Dialog;  import android.app.Dialog;
# Line 20  import android.net.Uri; Line 19  import android.net.Uri;
19  import android.os.AsyncTask;  import android.os.AsyncTask;
20  import android.os.Bundle;  import android.os.Bundle;
21  import android.util.Log;  import android.util.Log;
22    import android.view.Menu;
23    import android.view.MenuItem;
24  import android.view.View;  import android.view.View;
25  import android.view.View.OnClickListener;  import android.view.View.OnClickListener;
26  import android.widget.Button;  import android.widget.Button;
# Line 32  import dk.thoerup.traininfo.util.Message Line 33  import dk.thoerup.traininfo.util.Message
33  public class DepartureList extends ListActivity {  public class DepartureList extends ListActivity {
34    
35          public static final int DLG_PROGRESS = 1;          public static final int DLG_PROGRESS = 1;
36            static final int MENU_MAP = 100;
37            static final int MENU_NOTIFICATIONS = 101;
38                    
39                    
40          DepartureListAdapter adapter;          DepartureListAdapter adapter;
41          DepartureProvider provider;          DepartureProvider provider;
42          List<DepartureBean> departures;          DepartureBean departures;
43                    
44          int selectedItemId;          int selectedItemId;
45          //DepartureBean currentDeparture;          //DepartureBean currentDeparture;
# Line 48  public class DepartureList extends ListA Line 51  public class DepartureList extends ListA
51          StationBean station;          StationBean station;
52                    
53          boolean arrival = false;          boolean arrival = false;
54            
55          @SuppressWarnings("unchecked")          int commFailCounter = 0;
56    
57          @Override          @Override
58          protected void onCreate(Bundle savedInstanceState) {          protected void onCreate(Bundle savedInstanceState) {
59                  super.onCreate(savedInstanceState);                  super.onCreate(savedInstanceState);
# Line 92  public class DepartureList extends ListA Line 96  public class DepartureList extends ListA
96                                    
97                                    
98                                    
99                  findViewById(R.id.header).setOnClickListener( mapLauncher );                  // findViewById(R.id.header).setOnClickListener( mapLauncher );
100                                    
101                  int distance = station.getDistance();                  int distance = station.getDistance();
102                  if (distance != 0) {                  if (distance != 0) {
# Line 118  public class DepartureList extends ListA Line 122  public class DepartureList extends ListA
122                          if (savedInstanceState == null) {                          if (savedInstanceState == null) {
123                                  startDepartureFetcher();                                  startDepartureFetcher();
124                          } else {                          } else {
125                                  departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures");                                  departures = (DepartureBean) savedInstanceState.getSerializable("departures");
126                                  adapter.setDepartures(departures);                                  
127                                  selectedItemId = savedInstanceState.getInt("selectedItemId");                                    if ( (departures != null) && (departures.entries != null) ) {
128                                            adapter.setDepartures(departures.entries);
129                                    }
130                                    selectedItemId = savedInstanceState.getInt("selectedItemId");
131                                    
132                                    if ( hasNotifications() ) {
133                                            findViewById(R.id.notifIcon).setVisibility(View.VISIBLE);
134                                    }
135                                    
136                          }                          }
137                  }                  }
138          }          }
139                    
140            boolean hasNotifications() {
141                    return (departures != null && departures.notifications.size() > 0);
142            }
143            
144      @Override      @Override
145      public void onSaveInstanceState(Bundle outState)      public void onSaveInstanceState(Bundle outState)
146      {      {
# Line 133  public class DepartureList extends ListA Line 149  public class DepartureList extends ListA
149    
150          outState.putInt("selectedItemId", selectedItemId);          outState.putInt("selectedItemId", selectedItemId);
151                    
152          outState.putSerializable("departures", (ArrayList<DepartureBean>) departures);          outState.putSerializable("departures",  departures);
153      }      }
154        
155        
156                    
157          @Override          @Override
158            protected void onDestroy() {
159                    super.onDestroy();
160                    
161                    if (fetcher != null) {
162                            fetcher.cancel(true);
163                    }
164            }
165    
166            @Override
167          protected void onListItemClick(ListView l, View v, int position, long id) {          protected void onListItemClick(ListView l, View v, int position, long id) {
168                  super.onListItemClick(l, v, position, id);                  super.onListItemClick(l, v, position, id);
169                                    
170                  selectedItemId = position;                  selectedItemId = position;
171                                    
172                  DepartureBean dep = departures.get(selectedItemId);                  DepartureEntry dep = departures.entries.get(selectedItemId);
173                    
174                  Intent intent = new Intent(this, TimetableList.class);                  Intent intent = new Intent(this, TimetableList.class);
175                  intent.putExtra("departure", dep);                  intent.putExtra("departure", dep);
# Line 177  public class DepartureList extends ListA Line 204  public class DepartureList extends ListA
204                          return super.onCreateDialog(id);                                          return super.onCreateDialog(id);                
205                  }                  }
206          }          }
207            
208            
209    
210    
211    
212            @Override
213            public boolean onCreateOptionsMenu(Menu menu) {
214                    MenuItem item;
215                    
216                    item = menu.add(0, MENU_MAP, 0, getString(R.string.departurelist_showonmap) );
217                    item.setIcon(android.R.drawable.ic_menu_mapmode);
218                    
219                    item = menu.add(0, MENU_NOTIFICATIONS, 0, getString(R.string.departurelist_notifications) );
220                    item.setIcon(android.R.drawable.ic_menu_info_details);
221                    
222                    
223                    boolean notifEnabled = hasNotifications();
224                    item.setEnabled(notifEnabled);
225                    
226    
227                    return true;
228            }
229    
230            @Override
231            public boolean onOptionsItemSelected(MenuItem item) {          
232                    boolean res;
233                    switch(item.getItemId()) {
234                    case MENU_MAP:
235                            Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude());
236                            startActivity( new Intent(Intent.ACTION_VIEW, uri));
237                            res = true;
238                            break;
239                    case MENU_NOTIFICATIONS:
240                            Intent i = new Intent(this,dk.thoerup.traininfo.NotificationList.class);
241                            i.putExtra(NotificationList.EXTRA_NOTIFICATIONS, departures.notifications);
242                            startActivity(i);
243                            res = true;
244                            break;
245                    default:
246                            res = super.onOptionsItemSelected(item);
247                    }
248                    return res;
249            }
250    
251          void startDepartureFetcher() {          void startDepartureFetcher() {
252                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
# Line 198  public class DepartureList extends ListA Line 268  public class DepartureList extends ListA
268                  }                        }      
269          }          }
270                    
271          View.OnClickListener mapLauncher = new View.OnClickListener() {          /*View.OnClickListener mapLauncher = new View.OnClickListener() {
272                  @Override                  @Override
273                  public void onClick(View v) {                                    public void onClick(View v) {                  
274                          Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude());                          Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude());
275                          startActivity( new Intent(Intent.ACTION_VIEW, uri));                          startActivity( new Intent(Intent.ACTION_VIEW, uri));
276                  }                  }
277          };          };*/
278    
279    
280                    
281          class DepartureFetcher extends AsyncTask<Integer, Void, Void> {          class DepartureFetcher extends AsyncTask<Integer, Void, Void> {
282    
                 boolean success;  
283    
284                  @Override                  @Override
285                  protected void onPostExecute(Void result) {                  protected void onPostExecute(Void result) {
# Line 219  public class DepartureList extends ListA Line 288  public class DepartureList extends ListA
288                                                    
289                          pgDialog.dismiss();                          pgDialog.dismiss();
290                                                    
291                          if (success) {                          if (departures != null) {
292                                    commFailCounter = 0;
293                                  DepartureList.this.getListView().setVisibility(View.GONE); //Experimental, inspired by http://osdir.com/ml/Android-Developers/2010-04/msg01198.html                                  DepartureList.this.getListView().setVisibility(View.GONE); //Experimental, inspired by http://osdir.com/ml/Android-Developers/2010-04/msg01198.html
294                                  adapter.setDepartures(departures);                                  adapter.setDepartures(departures.entries);
295                                  DepartureList.this.getListView().setVisibility(View.VISIBLE);                                  DepartureList.this.getListView().setVisibility(View.VISIBLE);
296                                                                    
297                                  if (departures.size() == 0) {                                  
298                                    if ( hasNotifications() ) {
299                                            findViewById(R.id.notifIcon).setVisibility(View.VISIBLE);
300                                    }
301                                    
302                                    if (departures.entries.size() == 0) {
303                                          MessageBox.showMessage(DepartureList.this, "No departures found", true);                                          MessageBox.showMessage(DepartureList.this, "No departures found", true);
304                                  }                                  }
305                          } else { // communication or parse error                          } else { // communication or parse error
306                                    commFailCounter++;
307                                  AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this);                                                                                AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this);                                              
308                                  builder.setMessage("Error finding departures");                                  builder.setMessage("Error finding departures");
309                                  builder.setCancelable(true);                                  builder.setCancelable(true);
310                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {                                  if (commFailCounter < 3) {
311                                          public void onClick(DialogInterface dialog, int id) {                                          builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
312                                                  dialog.dismiss();                                                  public void onClick(DialogInterface dialog, int id) {
313                                                  startDepartureFetcher();                                                          dialog.dismiss();
314                                                                                                            startDepartureFetcher();
315                                          }                                                          
316                                  });                                                  }
317                                            });
318                                    }
319                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
320                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
321                                                  dialog.dismiss();                                                  dialog.dismiss();
# Line 255  public class DepartureList extends ListA Line 333  public class DepartureList extends ListA
333    
334                  @Override                  @Override
335                  protected Void doInBackground(Integer... params) {                  protected Void doInBackground(Integer... params) {
336                          success = provider.lookupDepartures(params[0], DepartureList.this.arrival);                          departures = provider.lookupDepartures(params[0], DepartureList.this.arrival);
                         departures = provider.getDepartures(params[0], DepartureList.this.arrival);  
337                          return null;                          return null;
338                  }                  }
339                                    

Legend:
Removed from v.917  
changed lines
  Added in v.1017

  ViewVC Help
Powered by ViewVC 1.1.20