/[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 742 by torben, Wed May 19 16:27:41 2010 UTC revision 982 by torben, Sun Jul 11 14:58:13 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_fetchdepartures;
4    import static dk.thoerup.traininfo.R.string.departurelist_fetcharrivals;
5    import static dk.thoerup.traininfo.R.string.generic_cancel;
6    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 14  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.ContextMenu;
23    import android.view.Menu;
24    import android.view.MenuItem;
25  import android.view.View;  import android.view.View;
26    import android.view.ContextMenu.ContextMenuInfo;
27    import android.view.View.OnClickListener;
28    import android.widget.Button;
29  import android.widget.ListView;  import android.widget.ListView;
30  import android.widget.TextView;  import android.widget.TextView;
31  import dk.thoerup.traininfo.provider.DepartureProvider;  import dk.thoerup.traininfo.provider.DepartureProvider;
32  import dk.thoerup.traininfo.provider.ProviderFactory;  import dk.thoerup.traininfo.provider.ProviderFactory;
33  import dk.thoerup.traininfo.util.MessageBox;  import dk.thoerup.traininfo.util.MessageBox;
 import static dk.thoerup.traininfo.R.string.*;  
34    
35  public class DepartureList extends ListActivity {  public class DepartureList extends ListActivity {
36    
37          public static final int DLG_PROGRESS = 1;          public static final int DLG_PROGRESS = 1;
38            static final int MENU_MAP = 100;
39            static final int MENU_NOTIFICATIONS = 101;
40                    
41                    
42          DepartureListAdapter adapter;          DepartureListAdapter adapter;
43          DepartureProvider provider;          DepartureProvider provider;
44          List<DepartureBean> departures;          DepartureBean departures;
45                    
46          int selectedItemId;          int selectedItemId;
47          //DepartureBean currentDeparture;          //DepartureBean currentDeparture;
# Line 40  public class DepartureList extends ListA Line 52  public class DepartureList extends ListA
52    
53          StationBean station;          StationBean station;
54                    
55          @SuppressWarnings("unchecked")          boolean arrival = false;
56            
57    
58          @Override          @Override
59          protected void onCreate(Bundle savedInstanceState) {          protected void onCreate(Bundle savedInstanceState) {
60                  super.onCreate(savedInstanceState);                  super.onCreate(savedInstanceState);
# Line 58  public class DepartureList extends ListA Line 72  public class DepartureList extends ListA
72    
73                  ((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() );                  ((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() );
74                                    
75                    final Button departureBtn = (Button) findViewById(R.id.departurebtn);
76                    final Button arrivalBtn = (Button) findViewById(R.id.arrivalbtn);
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                                    arrival = false;
84                                    startDepartureFetcher();
85                            }
86                    });
87                    arrivalBtn.setOnClickListener( new OnClickListener() {
88                            @Override
89                            public void onClick(View arg0) {        
90                                    arrivalBtn.setBackgroundResource(R.drawable.custom_button_hilight);
91                                    departureBtn.setBackgroundResource(R.drawable.custom_button);
92                                    arrival = true;                
93                                    startDepartureFetcher();
94                            }
95                    });
96                    
97                    
98                    
99                                    
100                  findViewById(R.id.header).setOnClickListener( mapLauncher );                  // findViewById(R.id.header).setOnClickListener( mapLauncher );
101                                    
102                  int distance = station.getDistance();                  int distance = station.getDistance();
103                  if (distance != 0) {                  if (distance != 0) {
# Line 76  public class DepartureList extends ListA Line 114  public class DepartureList extends ListA
114                  if (station.isRegional() == false && station.isSTrain() == false) {                  if (station.isRegional() == false && station.isSTrain() == false) {
115                          getListView().setVisibility( View.GONE );                                        getListView().setVisibility( View.GONE );              
116                          findViewById(R.id.metroonly).setVisibility( View.VISIBLE );                          findViewById(R.id.metroonly).setVisibility( View.VISIBLE );
117                            departureBtn.setVisibility( View.GONE );
118                            arrivalBtn.setVisibility(View.GONE);
119                                                    
120                  } else {                  } else {
121                          provider = ProviderFactory.getDepartureProvider();                          provider = ProviderFactory.getDepartureProvider();
# Line 83  public class DepartureList extends ListA Line 123  public class DepartureList extends ListA
123                          if (savedInstanceState == null) {                          if (savedInstanceState == null) {
124                                  startDepartureFetcher();                                  startDepartureFetcher();
125                          } else {                          } else {
126                                  departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures");                                  departures = (DepartureBean) savedInstanceState.getSerializable("departures");
127                                  adapter.setDepartures(departures);                                  adapter.setDepartures(departures.entries);
128                                  selectedItemId = savedInstanceState.getInt("selectedItemId");                                    selectedItemId = savedInstanceState.getInt("selectedItemId");
129                                    
130                                    if ( hasNotifications() ) {
131                                            findViewById(R.id.notifIcon).setVisibility(View.VISIBLE);
132                                    }
133                                    
134                          }                          }
135                  }                  }
136          }          }
137                    
138            boolean hasNotifications() {
139                    return (departures != null && departures.notifications.size() > 0);
140            }
141            
142      @Override      @Override
143      public void onSaveInstanceState(Bundle outState)      public void onSaveInstanceState(Bundle outState)
144      {      {
# Line 98  public class DepartureList extends ListA Line 147  public class DepartureList extends ListA
147    
148          outState.putInt("selectedItemId", selectedItemId);          outState.putInt("selectedItemId", selectedItemId);
149                    
150          outState.putSerializable("departures", (ArrayList<DepartureBean>) departures);          outState.putSerializable("departures",  departures);
151      }      }
152        
153        
154                    
155          @Override          @Override
156            protected void onDestroy() {
157                    super.onDestroy();
158                    
159                    if (fetcher != null) {
160                            fetcher.cancel(true);
161                    }
162            }
163    
164            @Override
165          protected void onListItemClick(ListView l, View v, int position, long id) {          protected void onListItemClick(ListView l, View v, int position, long id) {
166                  super.onListItemClick(l, v, position, id);                  super.onListItemClick(l, v, position, id);
167                                    
168                  selectedItemId = position;                  selectedItemId = position;
169                                    
170                  DepartureBean dep = departures.get(selectedItemId);                  DepartureEntry dep = departures.entries.get(selectedItemId);
171                    
172                  Intent intent = new Intent(this, TimetableList.class);                  Intent intent = new Intent(this, TimetableList.class);
173                  intent.putExtra("departure", dep);                  intent.putExtra("departure", dep);
# Line 124  public class DepartureList extends ListA Line 184  public class DepartureList extends ListA
184                  switch (id) {                  switch (id) {
185                  case DLG_PROGRESS:                  case DLG_PROGRESS:
186                          pgDialog = (ProgressDialog) dialog;                          pgDialog = (ProgressDialog) dialog;
187                            int messageId = arrival == false ? departurelist_fetchdepartures : departurelist_fetcharrivals;
188                            pgDialog.setMessage( getString(messageId) );
189                          break;                          break;
190                  }                  }
191          }          }
# Line 132  public class DepartureList extends ListA Line 194  public class DepartureList extends ListA
194          protected Dialog onCreateDialog(int id) {          protected Dialog onCreateDialog(int id) {
195                  switch (id) {                  switch (id) {
196                  case DLG_PROGRESS:                  case DLG_PROGRESS:
197    
198                          ProgressDialog dlg = new ProgressDialog(this);                          ProgressDialog dlg = new ProgressDialog(this);
                         dlg.setMessage( getString(departurelist_fetchdata) );  
199                          dlg.setCancelable(true);                          dlg.setCancelable(true);
200                          return dlg;                                              return dlg;                    
201                  default:                  default:
202                          return super.onCreateDialog(id);                                          return super.onCreateDialog(id);                
203                  }                  }
204          }          }
205            
206            
207    
208    
209    
210            @Override
211            public boolean onCreateOptionsMenu(Menu menu) {
212                    MenuItem item;
213                    item = menu.add(0, MENU_MAP, 0, "Show on map");
214                    item.setIcon(android.R.drawable.ic_menu_mapmode);
215                    
216                    item = menu.add(0, MENU_NOTIFICATIONS, 0, "Notifications");
217                    item.setIcon(android.R.drawable.ic_menu_info_details);
218                    
219                    
220                    boolean notifEnabled = hasNotifications();
221                    item.setEnabled(notifEnabled);
222                    
223    
224                    return true;
225            }
226    
227            @Override
228            public boolean onOptionsItemSelected(MenuItem item) {          
229                    boolean res;
230                    switch(item.getItemId()) {
231                    case MENU_MAP:
232                            Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude());
233                            startActivity( new Intent(Intent.ACTION_VIEW, uri));
234                            res = true;
235                            break;
236                    case MENU_NOTIFICATIONS:
237                            Intent i = new Intent(this,dk.thoerup.traininfo.NotificationList.class);
238                            i.putExtra(NotificationList.EXTRA_NOTIFICATIONS, departures.notifications);
239                            startActivity(i);
240                            res = true;
241                            break;
242                    default:
243                            res = super.onOptionsItemSelected(item);
244                    }
245                    return res;
246            }
247    
248          void startDepartureFetcher() {          void startDepartureFetcher() {
249                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
# Line 161  public class DepartureList extends ListA Line 265  public class DepartureList extends ListA
265                  }                        }      
266          }          }
267                    
268          View.OnClickListener mapLauncher = new View.OnClickListener() {          /*View.OnClickListener mapLauncher = new View.OnClickListener() {
269                  @Override                  @Override
270                  public void onClick(View v) {                                    public void onClick(View v) {                  
271                          Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude());                          Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude());
272                          startActivity( new Intent(Intent.ACTION_VIEW, uri));                          startActivity( new Intent(Intent.ACTION_VIEW, uri));
273                  }                  }
274          };          };*/
275    
276    
277                    
# Line 183  public class DepartureList extends ListA Line 287  public class DepartureList extends ListA
287                          pgDialog.dismiss();                          pgDialog.dismiss();
288                                                    
289                          if (success) {                          if (success) {
290                                  adapter.setDepartures(departures);                                  DepartureList.this.getListView().setVisibility(View.GONE); //Experimental, inspired by http://osdir.com/ml/Android-Developers/2010-04/msg01198.html
291                                  if (departures.size() == 0) {                                  adapter.setDepartures(departures.entries);
292                                          MessageBox.showMessage(DepartureList.this, "No departures found");                                  DepartureList.this.getListView().setVisibility(View.VISIBLE);
293                                    
294                                    
295                                    if ( hasNotifications() ) {
296                                            findViewById(R.id.notifIcon).setVisibility(View.VISIBLE);
297                                    }
298                                    
299                                    if (departures.entries.size() == 0) {
300                                            MessageBox.showMessage(DepartureList.this, "No departures found", true);
301                                  }                                  }
302                          } else { // communication or parse error                          } else { // communication or parse error
303                                  AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this);                                                                                AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this);                                              
# Line 201  public class DepartureList extends ListA Line 313  public class DepartureList extends ListA
313                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
314                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
315                                                  dialog.dismiss();                                                  dialog.dismiss();
316                                                    DepartureList.this.finish();
317                                          }                                                                                                }                                                      
318                                  });                                  });
319                                                                    
# Line 214  public class DepartureList extends ListA Line 327  public class DepartureList extends ListA
327    
328                  @Override                  @Override
329                  protected Void doInBackground(Integer... params) {                  protected Void doInBackground(Integer... params) {
330                          success = provider.lookupDepartures(params[0]);                          success = provider.lookupDepartures(params[0], DepartureList.this.arrival);
331                          departures = provider.getDepartures(params[0]);                          departures = provider.getDepartures(params[0], DepartureList.this.arrival);
332                          return null;                          return null;
333                  }                  }
334                                    

Legend:
Removed from v.742  
changed lines
  Added in v.982

  ViewVC Help
Powered by ViewVC 1.1.20