/[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 257 by torben, Mon Aug 10 18:48:24 2009 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_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                    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                    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                                    
                 latitude = launchedBy.getDoubleExtra("latitude", 0.0);  
                 longitude = launchedBy.getDoubleExtra("longitude", 0.0);  
108                                    
                 String name = launchedBy.getStringExtra("name");  
                 ((TextView) findViewById(R.id.stationName)).setText( name );  
109                                    
                 String addr = launchedBy.getStringExtra("address");  
                 ((TextView) findViewById(R.id.stationAddr)).setText( addr );  
110                                    
111                  findViewById(R.id.header).setOnClickListener( mapLauncher );                  // findViewById(R.id.header).setOnClickListener( mapLauncher );
112                    
113                    int distance = station.getDistance();
114                    if (distance != 0) {
115                            NumberFormat format = NumberFormat.getNumberInstance();
116                            format.setMaximumFractionDigits(1);
117                            format.setMinimumFractionDigits(1);
118                                    
119                  NumberFormat format = NumberFormat.getNumberInstance();                          ((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." );
120                  format.setMaximumFractionDigits(1);                  } else {
121                  format.setMinimumFractionDigits(1);                          ((TextView) findViewById(R.id.stationDistance)).setVisibility(View.GONE);
122                  int distance = launchedBy.getIntExtra("distance", 0);                  }
                 ((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." );  
123                    
124                    ProviderFactory.purgeOldEntries(); //cleanup before fetching more data
125                                    
126                  provider = ProviderFactory.getDepartureProvider();                  Log.e("Station", station.toCSV() );
127                                    
128                  fetcher = new DepartureFetcher();                  if (station.isMetro() == false) {
129                  if (savedInstanceState == null) {                          metroBtn.setVisibility( View.GONE );
130                          showDialog(DLG_PROGRESS);                  }
131                          fetcher.execute(name);          
132                    if (station.isRegional() == false && station.isSTrain() == false) {
133                            getListView().setVisibility( View.GONE );              
134                            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                          departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures");                          provider = ProviderFactory.getDepartureProvider();
141                          adapter.setDepartures(departures);                          
142                          selectedItemId = savedInstanceState.getInt("selectedItemId");                            if (savedInstanceState == null) {
143                          boolean detailsShowing = savedInstanceState.getBoolean("detailsShowing");                                  startDepartureFetcher();
144                          if (detailsShowing)                          } else {
145                                  showDialog(DLG_DETAILS);                                  departures = (DepartureBean) savedInstanceState.getSerializable("departures");
146                                    
147                                    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      {      {
167          if (pgDialog != null && pgDialog.isShowing())          if (pgDialog != null && pgDialog.isShowing())
168                  dismissDialog(DLG_PROGRESS);                  dismissDialog(DLG_PROGRESS);
169          boolean detailsShowing = (detailsDialog != null && detailsDialog.isShowing());  
         if (detailsShowing) {  
                 dismissDialog(DLG_DETAILS);  
         }  
         outState.putBoolean("detailsShowing", detailsShowing);  
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;
191                    
192                    DepartureEntry dep = departures.entries.get(selectedItemId);
193                    
194                  selectedItemId = position;                                Intent intent = new Intent(this, TimetableList.class);
195                  showDialog(DLG_DETAILS);                                  intent.putExtra("departure", dep);
196                    
197                    startActivity(intent);
198                                    
199          }          }
200                    
201    
# Line 110  public class DepartureList extends ListA Line 204  public class DepartureList extends ListA
204                  super.onPrepareDialog(id, dialog);                  super.onPrepareDialog(id, dialog);
205                                    
206                  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;  
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 132  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("Fetch departure data");  
221                          dlg.setCancelable(true);                          dlg.setCancelable(true);
222                          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;                    
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() {
272                    showDialog(DLG_PROGRESS);
273                    fetcher = new DepartureFetcher();
274                    fetcher.execute(station.getId());
275            }
276            
277          class DialogDismisser implements View.OnClickListener {          class DialogDismisser implements View.OnClickListener {
278    
279                  Dialog dlg;                  Dialog dlg;
# Line 163  public class DepartureList extends ListA Line 285  public class DepartureList extends ListA
285                  public void onClick(View v) {                  public void onClick(View v) {
286                          if (dlg.isShowing())                          if (dlg.isShowing())
287                                  dlg.dismiss();                                  dlg.dismiss();
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:" + latitude + "," + longitude);                          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<String, Void, Void> {          class DepartureFetcher extends AsyncTask<Integer, Void, Void> {
302    
303    
304                  @Override                  @Override
305                  protected void onPostExecute(Void result) {                  protected void onPostExecute(Void result) {
306                          super.onPostExecute(result);                          super.onPostExecute(result);
307                                                    
308                          adapter.setDepartures(departures);                          
309                          pgDialog.dismiss();                          pgDialog.dismiss();
310                                                    
311                          if (departures.size() == 0)                          if (departures != null) {
312                                  MessageBox.showMessage(DepartureList.this, "No departures found");                                  commFailCounter = 0;
313                                    DepartureList.this.getListView().setVisibility(View.GONE); //Experimental, inspired by http://osdir.com/ml/Android-Developers/2010-04/msg01198.html
314                                    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
326                                    commFailCounter++;
327                                    AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this);                                              
328                                    builder.setMessage("Error finding departures");
329                                    builder.setCancelable(true);
330                                    if (commFailCounter < 3) {
331                                            builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
332                                                    public void onClick(DialogInterface dialog, int id) {
333                                                            dialog.dismiss();
334                                                            startDepartureFetcher();
335                                                            
336                                                    }
337                                            });
338                                    }
339                                    builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
340                                            public void onClick(DialogInterface dialog, int id) {
341                                                    dialog.dismiss();
342                                                    DepartureList.this.finish();
343                                            }                                                      
344                                    });
345                                    
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(String... params) {                  protected Void doInBackground(Integer... params) {
356                          provider.lookupDepartures(params[0]);                          departures = provider.lookupDepartures(params[0], DepartureList.this.arrival);
                         departures = provider.getDepartures();  
357                          return null;                          return null;
358                  }                  }
359                                    

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

  ViewVC Help
Powered by ViewVC 1.1.20