/[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 310 by torben, Thu Sep 10 19:09:09 2009 UTC revision 1028 by torben, Wed Sep 8 06:25: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;
 import java.util.ArrayList;  
 import java.util.List;  
10    
11    
12    import android.app.AlertDialog;
13  import android.app.Dialog;  import android.app.Dialog;
14  import android.app.ListActivity;  import android.app.ListActivity;
15  import android.app.ProgressDialog;  import android.app.ProgressDialog;
16    import android.content.DialogInterface;
17  import android.content.Intent;  import android.content.Intent;
18  import android.net.Uri;  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;
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 21  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          public static final int DLG_DETAILS = 2;          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;
46                    
47          ProgressDialog pgDialog;          ProgressDialog pgDialog;
         Dialog detailsDialog;  
         DepartureFetcher fetcher;  
48                    
49          double latitude,longitude;          DepartureFetcher fetcher;
50    
51            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 46  public class DepartureList extends ListA Line 63  public class DepartureList extends ListA
63                  setListAdapter(adapter);                  setListAdapter(adapter);
64                                    
65                  Intent launchedBy = getIntent();                  Intent launchedBy = getIntent();
66            
67                    station = (StationBean) launchedBy.getSerializableExtra("stationbean");
68                    
69                    ((TextView) findViewById(R.id.stationName)).setText( station.getName() );
70    
71    
72                    ((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() );
73                                    
74                  latitude = launchedBy.getDoubleExtra("latitude", 0.0);                  final Button departureBtn = (Button) findViewById(R.id.departurebtn);
75                  longitude = launchedBy.getDoubleExtra("longitude", 0.0);                  final Button arrivalBtn = (Button) findViewById(R.id.arrivalbtn);
76                                    
77                  String name = launchedBy.getStringExtra("name");                  departureBtn.setOnClickListener( new OnClickListener() {
78                  ((TextView) findViewById(R.id.stationName)).setText( name );                          @Override
79                            public void onClick(View arg0) {        
80                                    arrivalBtn.setBackgroundResource(R.drawable.custom_button);
81                                    departureBtn.setBackgroundResource(R.drawable.custom_button_hilight);
82                                    arrival = false;
83                                    startDepartureFetcher();
84                            }
85                    });
86                    arrivalBtn.setOnClickListener( new OnClickListener() {
87                            @Override
88                            public void onClick(View arg0) {        
89                                    arrivalBtn.setBackgroundResource(R.drawable.custom_button_hilight);
90                                    departureBtn.setBackgroundResource(R.drawable.custom_button);
91                                    arrival = true;                
92                                    startDepartureFetcher();
93                            }
94                    });
95                                    
                 String addr = launchedBy.getStringExtra("address");  
                 ((TextView) findViewById(R.id.stationAddr)).setText( addr );  
96                                    
                 int stationId = launchedBy.getIntExtra("stationid", -1);  
97                                    
                 findViewById(R.id.header).setOnClickListener( mapLauncher );  
98                                    
99                  NumberFormat format = NumberFormat.getNumberInstance();                  // findViewById(R.id.header).setOnClickListener( mapLauncher );
                 format.setMaximumFractionDigits(1);  
                 format.setMinimumFractionDigits(1);  
                 int distance = launchedBy.getIntExtra("distance", 0);  
                 ((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." );  
           
100                                    
101                  provider = ProviderFactory.getDepartureProvider();                  int distance = station.getDistance();
102                    if (distance != 0) {
103                            NumberFormat format = NumberFormat.getNumberInstance();
104                            format.setMaximumFractionDigits(1);
105                            format.setMinimumFractionDigits(1);
106                                    
107                  fetcher = new DepartureFetcher();                          ((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." );
108                  if (savedInstanceState == null) {                  } else {
109                          showDialog(DLG_PROGRESS);                          ((TextView) findViewById(R.id.stationDistance)).setVisibility(View.GONE);
110                          fetcher.execute(stationId);                  }
111            
112                    ProviderFactory.purgeOldEntries(); //cleanup before fetching more data
113            
114                    if (station.isRegional() == false && station.isSTrain() == false) {
115                            getListView().setVisibility( View.GONE );              
116                            findViewById(R.id.metroonly).setVisibility( View.VISIBLE );
117                            departureBtn.setVisibility( View.GONE );
118                            arrivalBtn.setVisibility(View.GONE);
119                            
120                  } else {                  } else {
121                          departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures");                          provider = ProviderFactory.getDepartureProvider();
122                          adapter.setDepartures(departures);                          
123                          selectedItemId = savedInstanceState.getInt("selectedItemId");                            if (savedInstanceState == null) {
124                          boolean detailsShowing = savedInstanceState.getBoolean("detailsShowing");                                  startDepartureFetcher();
125                          if (detailsShowing)                          } else {
126                                  showDialog(DLG_DETAILS);                                  departures = (DepartureBean) savedInstanceState.getSerializable("departures");
127                                    
128                                    if ( (departures != null) && (departures.entries != null) ) {
129                                            adapter.setDepartures(departures.entries);
130                                    }
131                                    selectedItemId = savedInstanceState.getInt("selectedItemId");
132                                    
133                                    if ( hasNotifications() ) {
134                                            findViewById(R.id.notifIcon).setVisibility(View.VISIBLE);
135                                    }
136                                    
137                            }
138                  }                  }
139          }          }
140                    
141            boolean hasNotifications() {
142                    return (departures != null && departures.notifications.size() > 0);
143            }
144            
145      @Override      @Override
146      public void onSaveInstanceState(Bundle outState)      public void onSaveInstanceState(Bundle outState)
147      {      {
148          if (pgDialog != null && pgDialog.isShowing())          if (pgDialog != null && pgDialog.isShowing())
149                  dismissDialog(DLG_PROGRESS);                  dismissDialog(DLG_PROGRESS);
150          boolean detailsShowing = (detailsDialog != null && detailsDialog.isShowing());  
         if (detailsShowing) {  
                 dismissDialog(DLG_DETAILS);  
         }  
         outState.putBoolean("detailsShowing", detailsShowing);  
151          outState.putInt("selectedItemId", selectedItemId);          outState.putInt("selectedItemId", selectedItemId);
152                    
153          outState.putSerializable("departures", (ArrayList<DepartureBean>) departures);          outState.putSerializable("departures",  departures);
154      }      }
155        
156        
157                    
158          @Override          @Override
159            protected void onDestroy() {
160                    super.onDestroy();
161                    
162                    if (fetcher != null) {
163                            fetcher.cancel(true);
164                    }
165            }
166    
167            @Override
168          protected void onListItemClick(ListView l, View v, int position, long id) {          protected void onListItemClick(ListView l, View v, int position, long id) {
169                  super.onListItemClick(l, v, position, id);                  super.onListItemClick(l, v, position, id);
170                    
171                    selectedItemId = position;
172                    
173                    DepartureEntry dep = departures.entries.get(selectedItemId);
174                    
175                  selectedItemId = position;                                Intent intent = new Intent(this, TimetableList.class);
176                  showDialog(DLG_DETAILS);                                  intent.putExtra("departure", dep);
177                    
178                    startActivity(intent);
179                                    
180          }          }
181                    
182    
# Line 112  public class DepartureList extends ListA Line 185  public class DepartureList extends ListA
185                  super.onPrepareDialog(id, dialog);                  super.onPrepareDialog(id, dialog);
186                                    
187                  switch (id) {                  switch (id) {
                 case DLG_DETAILS:  
                         DepartureBean currentDeparture = departures.get(selectedItemId);  
                         ((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime());  
                         ((TextView)dialog.findViewById(R.id.Train)).setText(currentDeparture.getTrainNumber());  
                         ((TextView)dialog.findViewById(R.id.Destination)).setText( currentDeparture.getDestination());  
                         ((TextView)dialog.findViewById(R.id.Origin)).setText(currentDeparture.getOrigin());  
                         ((TextView)dialog.findViewById(R.id.Location)).setText(currentDeparture.getLocation());  
                         ((TextView)dialog.findViewById(R.id.Updated)).setText(currentDeparture.getLastUpdateString());  
                         ((TextView)dialog.findViewById(R.id.Status)).setText(currentDeparture.getStatus());  
                         ((TextView)dialog.findViewById(R.id.Note)).setText(currentDeparture.getNote());  
                         detailsDialog = dialog;  
                         break;  
188                  case DLG_PROGRESS:                  case DLG_PROGRESS:
189                          pgDialog = (ProgressDialog) dialog;                          pgDialog = (ProgressDialog) dialog;
190                            int messageId = arrival == false ? departurelist_fetchdepartures : departurelist_fetcharrivals;
191                            pgDialog.setMessage( getString(messageId) );
192                          break;                          break;
193                  }                  }
194          }          }
# Line 134  public class DepartureList extends ListA Line 197  public class DepartureList extends ListA
197          protected Dialog onCreateDialog(int id) {          protected Dialog onCreateDialog(int id) {
198                  switch (id) {                  switch (id) {
199                  case DLG_PROGRESS:                  case DLG_PROGRESS:
200    
201                          ProgressDialog dlg = new ProgressDialog(this);                          ProgressDialog dlg = new ProgressDialog(this);
                         dlg.setMessage("Fetch departure data");  
202                          dlg.setCancelable(true);                          dlg.setCancelable(true);
203                          return dlg;                          return dlg;                    
                 case DLG_DETAILS:  
                         //Context mContext = getApplicationContext();  
                         Dialog dialog = new Dialog(this);  
                         dialog.setCancelable(true);  
   
                         dialog.setContentView(R.layout.departuredetails);  
                         dialog.setTitle("Departure details");  
   
                         View root = dialog.findViewById(R.id.layout_root);  
                         root.setOnClickListener( new DialogDismisser(dialog) );  
                         return dialog;                    
204                  default:                  default:
205                          return super.onCreateDialog(id);                                          return super.onCreateDialog(id);                
206                  }                  }
207          }          }
208            
209            
210    
211    
212    
213            @Override
214            public boolean onCreateOptionsMenu(Menu menu) {
215                    MenuItem item;
216                    
217                    item = menu.add(0, MENU_MAP, 0, getString(R.string.departurelist_showonmap) );
218                    item.setIcon(android.R.drawable.ic_menu_mapmode);
219                    
220                    item = menu.add(0, MENU_NOTIFICATIONS, 0, getString(R.string.departurelist_notifications) );
221                    item.setIcon(android.R.drawable.ic_menu_info_details);
222                    
223                    
224                    boolean notifEnabled = hasNotifications();
225                    item.setEnabled(notifEnabled);
226                    
227    
228                    return true;
229            }
230    
231            @Override
232            public boolean onOptionsItemSelected(MenuItem item) {          
233                    boolean res;
234                    switch(item.getItemId()) {
235                    case MENU_MAP:
236                            Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude());
237                            startActivity( new Intent(Intent.ACTION_VIEW, uri));
238                            res = true;
239                            break;
240                    case MENU_NOTIFICATIONS:
241                            Intent i = new Intent(this,dk.thoerup.traininfo.NotificationList.class);
242                            i.putExtra(NotificationList.EXTRA_NOTIFICATIONS, departures.notifications);
243                            startActivity(i);
244                            res = true;
245                            break;
246                    default:
247                            res = super.onOptionsItemSelected(item);
248                    }
249                    return res;
250            }
251    
252            void startDepartureFetcher() {
253                    showDialog(DLG_PROGRESS);
254                    fetcher = new DepartureFetcher();
255                    fetcher.execute(station.getId());
256            }
257            
258          class DialogDismisser implements View.OnClickListener {          class DialogDismisser implements View.OnClickListener {
259    
260                  Dialog dlg;                  Dialog dlg;
# Line 165  public class DepartureList extends ListA Line 266  public class DepartureList extends ListA
266                  public void onClick(View v) {                  public void onClick(View v) {
267                          if (dlg.isShowing())                          if (dlg.isShowing())
268                                  dlg.dismiss();                                  dlg.dismiss();
269                  }                  }      
270          }          }
271                    
272          View.OnClickListener mapLauncher = new View.OnClickListener() {          /*View.OnClickListener mapLauncher = new View.OnClickListener() {
273                  @Override                  @Override
274                  public void onClick(View v) {                  public void onClick(View v) {                  
275                          Uri uri = Uri.parse("geo:" + latitude + "," + longitude);                          Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude());
276                          startActivity( new Intent(Intent.ACTION_VIEW, uri));                          startActivity( new Intent(Intent.ACTION_VIEW, uri));
277                  }                  }
278          };          };*/
279            
280    
281                    
282          class DepartureFetcher extends AsyncTask<Integer, Void, Void> {          class DepartureFetcher extends AsyncTask<Integer, Void, Void> {
283    
284    
285                  @Override                  @Override
286                  protected void onPostExecute(Void result) {                  protected void onPostExecute(Void result) {
287                          super.onPostExecute(result);                          super.onPostExecute(result);
288                                                    
289                          adapter.setDepartures(departures);                          
290                          pgDialog.dismiss();                          pgDialog.dismiss();
291                                                    
292                          if (departures.size() == 0)                          if (departures != null) {
293                                  MessageBox.showMessage(DepartureList.this, "No departures found");                                  commFailCounter = 0;
294                                    DepartureList.this.getListView().setVisibility(View.GONE); //Experimental, inspired by http://osdir.com/ml/Android-Developers/2010-04/msg01198.html
295                                    adapter.setDepartures(departures.entries);
296                                    DepartureList.this.getListView().setVisibility(View.VISIBLE);
297                                    
298                                    
299                                    if ( hasNotifications() ) {
300                                            findViewById(R.id.notifIcon).setVisibility(View.VISIBLE);
301                                    }
302                                    
303                                    if (departures.entries.size() == 0) {
304                                            MessageBox.showMessage(DepartureList.this, "No departures found", true);
305                                    }
306                            } else { // communication or parse error
307                                    commFailCounter++;
308                                    AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this);                                              
309                                    builder.setMessage("Error finding departures");
310                                    builder.setCancelable(true);
311                                    if (commFailCounter < 3) {
312                                            builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
313                                                    public void onClick(DialogInterface dialog, int id) {
314                                                            dialog.dismiss();
315                                                            startDepartureFetcher();
316                                                            
317                                                    }
318                                            });
319                                    }
320                                    builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
321                                            public void onClick(DialogInterface dialog, int id) {
322                                                    dialog.dismiss();
323                                                    DepartureList.this.finish();
324                                            }                                                      
325                                    });
326                                    
327                                    try {
328                                            builder.show();
329                                    } catch (android.view.WindowManager.BadTokenException e) {                                      
330                                            Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running
331                                    }                              
332                            }
333                  }                  }
334    
335                  @Override                  @Override
336                  protected Void doInBackground(Integer... params) {                  protected Void doInBackground(Integer... params) {
337                          provider.lookupDepartures(params[0]);                          departures = provider.lookupDepartures(params[0], DepartureList.this.arrival);
                         departures = provider.getDepartures();  
338                          return null;                          return null;
339                  }                  }
340                                    

Legend:
Removed from v.310  
changed lines
  Added in v.1028

  ViewVC Help
Powered by ViewVC 1.1.20