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

Diff of /android/TrainInfo/src/main/java/dk/thoerup/traininfo/WelcomeScreen.java

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

revision 1555 by torben, Fri Jul 8 12:24:48 2011 UTC revision 1626 by torben, Fri Nov 25 09:16:29 2011 UTC
# Line 47  public class WelcomeScreen extends Activ Line 47  public class WelcomeScreen extends Activ
47                    
48          SharedPreferences prefs;          SharedPreferences prefs;
49                    
50            StationLoader stationLoader;
51            
52          @Override          @Override
53          public void onCreate(Bundle savedInstanceState) {          public void onCreate(Bundle savedInstanceState) {
54                                    
# Line 91  public class WelcomeScreen extends Activ Line 93  public class WelcomeScreen extends Activ
93                          long last = prefs.getLong(stationsreload, 0);                          long last = prefs.getLong(stationsreload, 0);
94                          long now = System.currentTimeMillis();                          long now = System.currentTimeMillis();
95                          Log.i("TrainInfo", "Last Load: " + last);                          Log.i("TrainInfo", "Last Load: " + last);
96                            
97                          if ( (now-last) > (14*24*60*60*1000) ) {                          boolean didLoad = false;
98                                  new StationLoader(osp).execute( (Void)null);  
99                            try {
100                                    didLoad = osp.loadStations(this);
101                            }                                                                                              
102                            catch (InvalidClassException e) {
103                                    Log.i("TrainInfo", "invalid class - do a new download of stationlist");
104                            }
105                            catch (Exception e) {
106                                    Toast.makeText(this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();
107                                    Log.e("TrainInfo", "load error", e);
108                            }
109    
110                            if (didLoad == false) {                                
111                                    stationLoader = new StationLoader(osp, false);
112                                    stationLoader.execute( (Void)null);
113                          } else {                          } else {
114                                                            if ( (now-last) > (14*24*60*60*1000) ) { //if we had a stations list but it was too old, load a new one silent
115                                  boolean didLoad = false;                                          Log.i("TrainInfo", "Stationlist too old, do a silent download");
116                                                                            stationLoader = new StationLoader(osp, true);
117                                  try {                                          stationLoader.execute( (Void)null);
                                         didLoad = osp.loadStations(this);  
                                 }  
                                 catch (InvalidClassException e) {  
                                         Log.i("TrainInfo", "invalid class - do a new download of stationlist");  
                                 }  
                                 catch (Exception e) {  
                                         Toast.makeText(this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();  
                                         Log.e("TrainInfo", "load error", e);  
                                 }  
                                   
                                 if (didLoad == false) {                                  
                                         new StationLoader(osp).execute( (Void)null);  
118                                  }                                  }
119                          }                          }
120    
121                  }                  }
122          }          }
123            
124            
125                                    
126          @Override          @Override
127          protected void onDestroy() {          protected void onDestroy() {
128                  super.onDestroy();                  super.onDestroy();
129                    if (stationLoader != null) {
130                            stationLoader.cancel(true);
131                    }
132                    
133                  ProviderFactory.purgeOldEntries(); //exiting application, do some cleanup                  ProviderFactory.purgeOldEntries(); //exiting application, do some cleanup
134          }          }
135    
# Line 151  public class WelcomeScreen extends Activ Line 159  public class WelcomeScreen extends Activ
159                                                    
160                  case MENU_RELOAD:                  case MENU_RELOAD:
161                          OfflineStationProvider osp = (OfflineStationProvider) ProviderFactory.getStationProvider();                          OfflineStationProvider osp = (OfflineStationProvider) ProviderFactory.getStationProvider();
162                          new StationLoader(osp).execute( (Void)null);                                              new StationLoader(osp, false).execute( (Void)null);                    
163                          break;                          break;
164                                                    
165                  default:                  default:
# Line 191  public class WelcomeScreen extends Activ Line 199  public class WelcomeScreen extends Activ
199                                    
200                  @Override                  @Override
201                  public void onClick(View v) {                  public void onClick(View v) {
202                            
203                            StationProvider sp = ProviderFactory.getStationProvider();
204                            
205                            if (sp instanceof OfflineStationProvider ) {
206                                    OfflineStationProvider osp = (OfflineStationProvider) sp;
207                                    
208                                    if (! osp.hasStations()) {
209                                            stationLoader = new StationLoader(osp, false);
210                                            stationLoader.execute( (Void)null);    
211                                            return;
212                                    }
213                            }
214                            
215                          Intent intent = new Intent(WelcomeScreen.this, StationList.class);                          Intent intent = new Intent(WelcomeScreen.this, StationList.class);
216                          intent.putExtra("type", launchType);                          intent.putExtra("type", launchType);
217                          WelcomeScreen.this.startActivity(intent);                          WelcomeScreen.this.startActivity(intent);
# Line 205  public class WelcomeScreen extends Activ Line 226  public class WelcomeScreen extends Activ
226                  ProgressDialog dlg;                  ProgressDialog dlg;
227                  OfflineStationProvider osp;                  OfflineStationProvider osp;
228                  String exMsg;                  String exMsg;
229                    boolean silent;
230                                    
231                  public StationLoader(OfflineStationProvider osp) {                  public StationLoader(OfflineStationProvider osp, boolean silent) {
232                          this.osp = osp;                          this.osp = osp;
233                            this.silent = silent;
234                  }                  }
235                                    
236                  @Override                  @Override
# Line 217  public class WelcomeScreen extends Activ Line 240  public class WelcomeScreen extends Activ
240                                  succeeded = true;                                  succeeded = true;
241                          } catch (Exception e) {                          } catch (Exception e) {
242                                  succeeded = false;                                  succeeded = false;
243                                  exMsg = e.getMessage();                                                          exMsg = e.getMessage();
244                                    Log.e("TrainInfo", "download error", e);                                
245                          }                          }
246                          return null;                          return null;
247                  }                  }
248    
249                    
250                  @Override                  @Override
251                  protected void onPreExecute() {                  protected void onPreExecute() {
252                          super.onPreExecute();                          super.onPreExecute();
253                            Log.i("TrainInfo", "StationLoader.onPreExecute() ");
254                                                    
255                          dlg = new ProgressDialog(WelcomeScreen.this);                          if (silent == false) {
256                          dlg.setMessage( "Downloading stations list" );//TODO: translate                                  dlg = new ProgressDialog(WelcomeScreen.this);
257                          dlg.setCancelable(true);                                  dlg.setMessage( getText(R.string.welcome_downloadingstations) );
258                          dlg.show();                                  dlg.setCancelable(false);
259                                    dlg.show();
260                            }
261                  }                  }
262    
263                  @Override                  @Override
264                  protected void onPostExecute(Void result) {                  protected void onPostExecute(Void result) {
265                          super.onPostExecute(result);                          super.onPostExecute(result);
266                            Log.i("TrainInfo", "StationLoader.onPostExecute() ");
267                                                    
268                          dlg.dismiss();                          if (silent == false) {
269                          dlg = null;                                  dlg.dismiss();
270                                    dlg = null;
271                            }
272                                                    
273                          if (succeeded) {                          if (succeeded) {
274                                  Editor edit = prefs.edit();                                  Editor edit = prefs.edit();
275                                  edit.putLong(stationsreload, System.currentTimeMillis() );                                  edit.putLong(stationsreload, System.currentTimeMillis() );
276                                  edit.commit();                                  edit.commit();
277                          } else {                          } else {
278                                  Toast.makeText(WelcomeScreen.this, "Error " + exMsg, Toast.LENGTH_LONG).show();                                  if (silent == false) {
279                                            Toast.makeText(WelcomeScreen.this, "Error " + exMsg, Toast.LENGTH_LONG).show();
280                                    }
281                          }                          }
282                  }                        }      
283          }          }

Legend:
Removed from v.1555  
changed lines
  Added in v.1626

  ViewVC Help
Powered by ViewVC 1.1.20