/[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 1638 by torben, Sun Nov 27 19:44:28 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 176  public class WelcomeScreen extends Activ Line 184  public class WelcomeScreen extends Activ
184                          message.append("By Torben H. Nielsen\n");                          message.append("By Torben H. Nielsen\n");
185    
186                          MessageBox.showMessage(WelcomeScreen.this, message.toString());*/                          MessageBox.showMessage(WelcomeScreen.this, message.toString());*/
187                          Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://t-hoerup.dk/android/traininfo/"));                          /*Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://t-hoerup.dk/android/traininfo/"));
188                          startActivity(browserIntent);                          startActivity(browserIntent);*/
189                            
190                            Intent intent = new Intent(WelcomeScreen.this, AboutScreen.class);
191                            WelcomeScreen.this.startActivity(intent);
192                  }                  }
193    
194          }          }
# Line 191  public class WelcomeScreen extends Activ Line 202  public class WelcomeScreen extends Activ
202                                    
203                  @Override                  @Override
204                  public void onClick(View v) {                  public void onClick(View v) {
205                            
206                            StationProvider sp = ProviderFactory.getStationProvider();
207                            
208                            if (sp instanceof OfflineStationProvider ) {
209                                    OfflineStationProvider osp = (OfflineStationProvider) sp;
210                                    
211                                    if (! osp.hasStations()) {
212                                            stationLoader = new StationLoader(osp, false);
213                                            stationLoader.execute( (Void)null);    
214                                            return;
215                                    }
216                            }
217                            
218                          Intent intent = new Intent(WelcomeScreen.this, StationList.class);                          Intent intent = new Intent(WelcomeScreen.this, StationList.class);
219                          intent.putExtra("type", launchType);                          intent.putExtra("type", launchType);
220                          WelcomeScreen.this.startActivity(intent);                          WelcomeScreen.this.startActivity(intent);
# Line 205  public class WelcomeScreen extends Activ Line 229  public class WelcomeScreen extends Activ
229                  ProgressDialog dlg;                  ProgressDialog dlg;
230                  OfflineStationProvider osp;                  OfflineStationProvider osp;
231                  String exMsg;                  String exMsg;
232                    boolean silent;
233                                    
234                  public StationLoader(OfflineStationProvider osp) {                  public StationLoader(OfflineStationProvider osp, boolean silent) {
235                          this.osp = osp;                          this.osp = osp;
236                            this.silent = silent;
237                  }                  }
238                                    
239                  @Override                  @Override
# Line 217  public class WelcomeScreen extends Activ Line 243  public class WelcomeScreen extends Activ
243                                  succeeded = true;                                  succeeded = true;
244                          } catch (Exception e) {                          } catch (Exception e) {
245                                  succeeded = false;                                  succeeded = false;
246                                  exMsg = e.getMessage();                                                          exMsg = e.getMessage();
247                                    Log.e("TrainInfo", "download error", e);                                
248                          }                          }
249                          return null;                          return null;
250                  }                  }
251    
252                    
253                  @Override                  @Override
254                  protected void onPreExecute() {                  protected void onPreExecute() {
255                          super.onPreExecute();                          super.onPreExecute();
256                            Log.i("TrainInfo", "StationLoader.onPreExecute() ");
257                                                    
258                          dlg = new ProgressDialog(WelcomeScreen.this);                          if (silent == false) {
259                          dlg.setMessage( "Downloading stations list" );//TODO: translate                                  dlg = new ProgressDialog(WelcomeScreen.this);
260                          dlg.setCancelable(true);                                  dlg.setMessage( getText(R.string.welcome_downloadingstations) );
261                          dlg.show();                                  dlg.setCancelable(false);
262                                    dlg.show();
263                            }
264                  }                  }
265    
266                  @Override                  @Override
267                  protected void onPostExecute(Void result) {                  protected void onPostExecute(Void result) {
268                          super.onPostExecute(result);                          super.onPostExecute(result);
269                            Log.i("TrainInfo", "StationLoader.onPostExecute() ");
270                                                    
271                          dlg.dismiss();                          if (silent == false) {
272                          dlg = null;                                  dlg.dismiss();
273                                    dlg = null;
274                            }
275                                                    
276                          if (succeeded) {                          if (succeeded) {
277                                  Editor edit = prefs.edit();                                  Editor edit = prefs.edit();
278                                  edit.putLong(stationsreload, System.currentTimeMillis() );                                  edit.putLong(stationsreload, System.currentTimeMillis() );
279                                  edit.commit();                                  edit.commit();
280                          } else {                          } else {
281                                  Toast.makeText(WelcomeScreen.this, "Error " + exMsg, Toast.LENGTH_LONG).show();                                  if (silent == false) {
282                                            Toast.makeText(WelcomeScreen.this, "Error " + exMsg, Toast.LENGTH_LONG).show();
283                                    }
284                          }                          }
285                  }                        }      
286          }          }

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

  ViewVC Help
Powered by ViewVC 1.1.20