--- android/TrainInfo/src/dk/thoerup/traininfo/WelcomeScreen.java 2011/07/07 20:39:26 1549 +++ android/TrainInfo/src/dk/thoerup/traininfo/WelcomeScreen.java 2011/07/09 07:20:57 1567 @@ -2,14 +2,13 @@ -import java.util.ArrayList; +import java.io.InvalidClassException; import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; -import android.location.Location; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; @@ -25,14 +24,11 @@ import com.nullwire.trace.ExceptionHandler; -import dk.thoerup.android.traininfo.common.StationEntry; import dk.thoerup.androidutils.CheckUpdates; import dk.thoerup.traininfo.provider.OfflineStationProvider; import dk.thoerup.traininfo.provider.ProviderFactory; import dk.thoerup.traininfo.provider.StationProvider; -import dk.thoerup.traininfo.stationmap.GeoPair; -import dk.thoerup.traininfo.stationmap.StationMapView; -import dk.thoerup.traininfo.util.MessageBox; + public class WelcomeScreen extends Activity{ @@ -51,6 +47,8 @@ SharedPreferences prefs; + StationLoader stationLoader; + @Override public void onCreate(Bundle savedInstanceState) { @@ -92,27 +90,34 @@ if (sp instanceof OfflineStationProvider ) { OfflineStationProvider osp = (OfflineStationProvider) sp; - try { - - long last = prefs.getLong(stationsreload, 0); - long now = System.currentTimeMillis(); - - Log.i("TrainInfo", "Last Load: " + last); - - if ( (now-last) > (14*24*60*60*1000) ) { - new StationLoader(osp).execute( (Void)null); - } else { + long last = prefs.getLong(stationsreload, 0); + long now = System.currentTimeMillis(); + Log.i("TrainInfo", "Last Load: " + last); + + if ( (now-last) > (14*24*60*60*1000) ) { + stationLoader = new StationLoader(osp); + stationLoader.execute( (Void)null); + } else { + + boolean didLoad = false; - boolean didLoad = osp.loadStations(this); - - if (didLoad == false) { - new StationLoader(osp).execute( (Void)null); - } + try { + 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); } - } catch (Exception e) { - Toast.makeText(this, "" + e.getMessage(), Toast.LENGTH_SHORT).show(); + if (didLoad == false) { + stationLoader = new StationLoader(osp); + stationLoader.execute( (Void)null); + } } + } } @@ -121,6 +126,10 @@ @Override protected void onDestroy() { super.onDestroy(); + if (stationLoader != null) { + stationLoader.cancel(false); + } + ProviderFactory.purgeOldEntries(); //exiting application, do some cleanup } @@ -203,6 +212,7 @@ boolean succeeded; ProgressDialog dlg; OfflineStationProvider osp; + String exMsg; public StationLoader(OfflineStationProvider osp) { this.osp = osp; @@ -215,17 +225,18 @@ succeeded = true; } catch (Exception e) { succeeded = false; - Toast.makeText(WelcomeScreen.this, "Error " + e.getMessage(), Toast.LENGTH_LONG).show(); + exMsg = e.getMessage(); } return null; } + @Override protected void onPreExecute() { super.onPreExecute(); dlg = new ProgressDialog(WelcomeScreen.this); - dlg.setMessage( "Downloading stations list" );//TODO: translate + dlg.setMessage( getText(R.string.welcome_downloadingstations) ); dlg.setCancelable(true); dlg.show(); } @@ -241,6 +252,8 @@ Editor edit = prefs.edit(); edit.putLong(stationsreload, System.currentTimeMillis() ); edit.commit(); + } else { + Toast.makeText(WelcomeScreen.this, "Error " + exMsg, Toast.LENGTH_LONG).show(); } } }