/[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 572 by torben, Mon Feb 1 16:49:02 2010 UTC revision 1044 by torben, Mon Sep 13 21:59:32 2010 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfo;  package dk.thoerup.traininfo;
2    
3  import static dk.thoerup.traininfo.R.string.departurelist_fetchdata;  import static dk.thoerup.traininfo.R.string.departurelist_fetchdepartures;
4    import static dk.thoerup.traininfo.R.string.departurelist_fetcharrivals;
5  import static dk.thoerup.traininfo.R.string.generic_cancel;  import static dk.thoerup.traininfo.R.string.generic_cancel;
6  import static dk.thoerup.traininfo.R.string.generic_retry;  import static dk.thoerup.traininfo.R.string.generic_retry;
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 18  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;
26    import android.widget.Button;
27  import android.widget.ListView;  import android.widget.ListView;
28  import android.widget.TextView;  import android.widget.TextView;
29  import dk.thoerup.traininfo.provider.DepartureProvider;  import dk.thoerup.traininfo.provider.DepartureProvider;
# Line 28  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 43  public class DepartureList extends ListA Line 50  public class DepartureList extends ListA
50    
51          StationBean station;          StationBean station;
52                    
53          @SuppressWarnings("unchecked")          boolean arrival = false;
54    
55            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 61  public class DepartureList extends ListA Line 71  public class DepartureList extends ListA
71    
72                  ((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() );                  ((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() );
73                                    
74                    final Button departureBtn = (Button) findViewById(R.id.departurebtn);
75                    final Button arrivalBtn = (Button) findViewById(R.id.arrivalbtn);
76                    final Button metroBtn = (Button) findViewById(R.id.metrobtn);
77                    
78                    departureBtn.setOnClickListener( new OnClickListener() {
79                            @Override
80                            public void onClick(View arg0) {        
81                                    arrivalBtn.setBackgroundResource(R.drawable.custom_button);
82                                    departureBtn.setBackgroundResource(R.drawable.custom_button_hilight);
83                                    metroBtn.setBackgroundResource(R.drawable.custom_button);
84                                    arrival = false;
85                                    startDepartureFetcher();
86                            }
87                    });
88                    arrivalBtn.setOnClickListener( new OnClickListener() {
89                            @Override
90                            public void onClick(View arg0) {        
91                                    arrivalBtn.setBackgroundResource(R.drawable.custom_button_hilight);
92                                    departureBtn.setBackgroundResource(R.drawable.custom_button);
93                                    metroBtn.setBackgroundResource(R.drawable.custom_button);
94                                    arrival = true;                
95                                    startDepartureFetcher();
96                            }
97                    });
98                                    
99                  findViewById(R.id.header).setOnClickListener( mapLauncher );                  metroBtn.setOnClickListener( new OnClickListener() {                    
100                            @Override
101                            public void onClick(View v) {
102                                    arrivalBtn.setBackgroundResource(R.drawable.custom_button);
103                                    departureBtn.setBackgroundResource(R.drawable.custom_button);
104                                    metroBtn.setBackgroundResource(R.drawable.custom_button_hilight);
105                            }
106                    });
107                                    
                 NumberFormat format = NumberFormat.getNumberInstance();  
                 format.setMaximumFractionDigits(1);  
                 format.setMinimumFractionDigits(1);  
108                                    
109                    
110                    
111                    // findViewById(R.id.header).setOnClickListener( mapLauncher );
112                    
113                  int distance = station.getDistance();                  int distance = station.getDistance();
114                  ((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." );                  if (distance != 0) {
115                  station.setRegional(true);                          NumberFormat format = NumberFormat.getNumberInstance();
116                  Log.e("ASD", "" + station.isRegional() + "/" + station.isSTrain() + "/" + station.isMetro());                          format.setMaximumFractionDigits(1);
117                            format.setMinimumFractionDigits(1);
118                    
119                            ((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." );
120                    } else {
121                            ((TextView) findViewById(R.id.stationDistance)).setVisibility(View.GONE);
122                    }
123            
124                    ProviderFactory.purgeOldEntries(); //cleanup before fetching more data
125                    
126                    Log.e("Station", station.toCSV() );
127                    
128                    if (station.isMetro() == false) {
129                            metroBtn.setVisibility( View.GONE );
130                    }
131                    
132                  if (station.isRegional() == false && station.isSTrain() == false) {                  if (station.isRegional() == false && station.isSTrain() == false) {
133                          getListView().setVisibility( View.GONE );                                        getListView().setVisibility( View.GONE );              
134                          findViewById(R.id.metroonly).setVisibility( View.VISIBLE );                          findViewById(R.id.metroonly).setVisibility( View.VISIBLE );
135                            departureBtn.setVisibility( View.GONE );
136                            arrivalBtn.setVisibility(View.GONE);
137                            metroBtn.setVisibility( View.GONE );
138                                                    
139                  } else {                  } else {
140                          provider = ProviderFactory.getDepartureProvider();                          provider = ProviderFactory.getDepartureProvider();
# Line 84  public class DepartureList extends ListA Line 142  public class DepartureList extends ListA
142                          if (savedInstanceState == null) {                          if (savedInstanceState == null) {
143                                  startDepartureFetcher();                                  startDepartureFetcher();
144                          } else {                          } else {
145                                  departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures");                                  departures = (DepartureBean) savedInstanceState.getSerializable("departures");
146                                  adapter.setDepartures(departures);                                  
147                                  selectedItemId = savedInstanceState.getInt("selectedItemId");                                    if ( (departures != null) && (departures.entries != null) ) {
148                                            adapter.setDepartures(departures.entries);
149                                    }
150                                    selectedItemId = savedInstanceState.getInt("selectedItemId");
151                                    
152                                    if ( hasNotifications() ) {
153                                            findViewById(R.id.notifIcon).setVisibility(View.VISIBLE);
154                                    }
155                                    
156                          }                          }
157                  }                  }
158          }          }
159                    
160            boolean hasNotifications() {
161                    return (departures != null && departures.notifications.size() > 0);
162            }
163            
164      @Override      @Override
165      public void onSaveInstanceState(Bundle outState)      public void onSaveInstanceState(Bundle outState)
166      {      {
# Line 99  public class DepartureList extends ListA Line 169  public class DepartureList extends ListA
169    
170          outState.putInt("selectedItemId", selectedItemId);          outState.putInt("selectedItemId", selectedItemId);
171                    
172          outState.putSerializable("departures", (ArrayList<DepartureBean>) departures);          outState.putSerializable("departures",  departures);
173      }      }
174        
175        
176                    
177          @Override          @Override
178            protected void onDestroy() {
179                    super.onDestroy();
180                    
181                    if (fetcher != null) {
182                            fetcher.cancel(true);
183                    }
184            }
185    
186            @Override
187          protected void onListItemClick(ListView l, View v, int position, long id) {          protected void onListItemClick(ListView l, View v, int position, long id) {
188                  super.onListItemClick(l, v, position, id);                  super.onListItemClick(l, v, position, id);
189                                    
190                  selectedItemId = position;                  selectedItemId = position;
191                                    
192                  DepartureBean dep = departures.get(selectedItemId);                  DepartureEntry dep = departures.entries.get(selectedItemId);
193                    
194                  Intent intent = new Intent(this, TimetableList.class);                  Intent intent = new Intent(this, TimetableList.class);
195                  intent.putExtra("departure", dep);                  intent.putExtra("departure", dep);
# Line 125  public class DepartureList extends ListA Line 206  public class DepartureList extends ListA
206                  switch (id) {                  switch (id) {
207                  case DLG_PROGRESS:                  case DLG_PROGRESS:
208                          pgDialog = (ProgressDialog) dialog;                          pgDialog = (ProgressDialog) dialog;
209                            int messageId = arrival == false ? departurelist_fetchdepartures : departurelist_fetcharrivals;
210                            pgDialog.setMessage( getString(messageId) );
211                          break;                          break;
212                  }                  }
213          }          }
# Line 133  public class DepartureList extends ListA Line 216  public class DepartureList extends ListA
216          protected Dialog onCreateDialog(int id) {          protected Dialog onCreateDialog(int id) {
217                  switch (id) {                  switch (id) {
218                  case DLG_PROGRESS:                  case DLG_PROGRESS:
219    
220                          ProgressDialog dlg = new ProgressDialog(this);                          ProgressDialog dlg = new ProgressDialog(this);
                         dlg.setMessage( getString(departurelist_fetchdata) );  
221                          dlg.setCancelable(true);                          dlg.setCancelable(true);
222                          return dlg;                                              return dlg;                    
223                  default:                  default:
224                          return super.onCreateDialog(id);                                          return super.onCreateDialog(id);                
225                  }                  }
226          }          }
227            
228            
229    
230    
231    
232            @Override
233            public boolean onCreateOptionsMenu(Menu menu) {
234                    MenuItem item;
235                    
236                    item = menu.add(0, MENU_MAP, 0, getString(R.string.departurelist_showonmap) );
237                    item.setIcon(android.R.drawable.ic_menu_mapmode);
238                    
239                    item = menu.add(0, MENU_NOTIFICATIONS, 0, getString(R.string.departurelist_notifications) );
240                    item.setIcon(android.R.drawable.ic_menu_info_details);
241                    
242                    
243                    boolean notifEnabled = hasNotifications();
244                    item.setEnabled(notifEnabled);
245                    
246    
247                    return true;
248            }
249    
250            @Override
251            public boolean onOptionsItemSelected(MenuItem item) {          
252                    boolean res;
253                    switch(item.getItemId()) {
254                    case MENU_MAP:
255                            Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude());
256                            startActivity( new Intent(Intent.ACTION_VIEW, uri));
257                            res = true;
258                            break;
259                    case MENU_NOTIFICATIONS:
260                            Intent i = new Intent(this,dk.thoerup.traininfo.NotificationList.class);
261                            i.putExtra(NotificationList.EXTRA_NOTIFICATIONS, departures.notifications);
262                            startActivity(i);
263                            res = true;
264                            break;
265                    default:
266                            res = super.onOptionsItemSelected(item);
267                    }
268                    return res;
269            }
270    
271          void startDepartureFetcher() {          void startDepartureFetcher() {
272                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
# Line 162  public class DepartureList extends ListA Line 288  public class DepartureList extends ListA
288                  }                        }      
289          }          }
290                    
291          View.OnClickListener mapLauncher = new View.OnClickListener() {          /*View.OnClickListener mapLauncher = new View.OnClickListener() {
292                  @Override                  @Override
293                  public void onClick(View v) {                                    public void onClick(View v) {                  
294                          Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude());                          Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude());
295                          startActivity( new Intent(Intent.ACTION_VIEW, uri));                          startActivity( new Intent(Intent.ACTION_VIEW, uri));
296                  }                  }
297          };          };*/
298    
299    
300                    
301          class DepartureFetcher extends AsyncTask<Integer, Void, Void> {          class DepartureFetcher extends AsyncTask<Integer, Void, Void> {
302    
                 boolean success;  
303    
304                  @Override                  @Override
305                  protected void onPostExecute(Void result) {                  protected void onPostExecute(Void result) {
# Line 183  public class DepartureList extends ListA Line 308  public class DepartureList extends ListA
308                                                    
309                          pgDialog.dismiss();                          pgDialog.dismiss();
310                                                    
311                          if (success) {                          if (departures != null) {
312                                  adapter.setDepartures(departures);                                  commFailCounter = 0;
313                                  if (departures.size() == 0) {                                  DepartureList.this.getListView().setVisibility(View.GONE); //Experimental, inspired by http://osdir.com/ml/Android-Developers/2010-04/msg01198.html
314                                          MessageBox.showMessage(DepartureList.this, "No departures found");                                  adapter.setDepartures(departures.entries);
315                                    DepartureList.this.getListView().setVisibility(View.VISIBLE);
316                                    
317                                    
318                                    if ( hasNotifications() ) {
319                                            findViewById(R.id.notifIcon).setVisibility(View.VISIBLE);
320                                    }
321                                    
322                                    if (departures.entries.size() == 0) {
323                                            MessageBox.showMessage(DepartureList.this, "No departures found", true);
324                                  }                                  }
325                          } else { // communication or parse error                          } else { // communication or parse error
326                                    commFailCounter++;
327                                  AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this);                                                                                AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this);                                              
328                                  builder.setMessage("Error finding departures");                                  builder.setMessage("Error finding departures");
329                                  builder.setCancelable(true);                                  builder.setCancelable(true);
330                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {                                  if (commFailCounter < 3) {
331                                          public void onClick(DialogInterface dialog, int id) {                                          builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
332                                                  dialog.dismiss();                                                  public void onClick(DialogInterface dialog, int id) {
333                                                  startDepartureFetcher();                                                          dialog.dismiss();
334                                                                                                            startDepartureFetcher();
335                                          }                                                          
336                                  });                                                  }
337                                            });
338                                    }
339                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
340                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
341                                                  dialog.dismiss();                                                  dialog.dismiss();
342                                                    DepartureList.this.finish();
343                                          }                                                                                                }                                                      
344                                  });                                                                                                                              });
345                                  builder.show();                                                          
346                                    try {
347                                            builder.show();
348                                    } catch (android.view.WindowManager.BadTokenException e) {                                      
349                                            Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running
350                                    }                              
351                          }                          }
352                  }                  }
353    
354                  @Override                  @Override
355                  protected Void doInBackground(Integer... params) {                  protected Void doInBackground(Integer... params) {
356                          success = provider.lookupDepartures(params[0]);                          departures = provider.lookupDepartures(params[0], DepartureList.this.arrival);
                         departures = provider.getDepartures(params[0]);  
357                          return null;                          return null;
358                  }                  }
359                                    

Legend:
Removed from v.572  
changed lines
  Added in v.1044

  ViewVC Help
Powered by ViewVC 1.1.20