--- android/TrainInfo/src/dk/thoerup/traininfo/WelcomeScreen.java 2011/07/07 19:44:01 1548 +++ android/TrainInfo/src/dk/thoerup/traininfo/WelcomeScreen.java 2011/08/31 20:23:36 1594 @@ -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,25 +90,34 @@ if (sp instanceof OfflineStationProvider ) { OfflineStationProvider osp = (OfflineStationProvider) sp; - try { - - long last = prefs.getLong(stationsreload, 0); - long now = System.currentTimeMillis(); - - 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); + } } + } } @@ -119,6 +126,10 @@ @Override protected void onDestroy() { super.onDestroy(); + if (stationLoader != null) { + stationLoader.cancel(true); + } + ProviderFactory.purgeOldEntries(); //exiting application, do some cleanup } @@ -188,6 +199,19 @@ @Override public void onClick(View v) { + + StationProvider sp = ProviderFactory.getStationProvider(); + + if (sp instanceof OfflineStationProvider ) { + OfflineStationProvider osp = (OfflineStationProvider) sp; + + if (! osp.hasStations()) { + stationLoader = new StationLoader(osp); + stationLoader.execute( (Void)null); + return; + } + } + Intent intent = new Intent(WelcomeScreen.this, StationList.class); intent.putExtra("type", launchType); WelcomeScreen.this.startActivity(intent); @@ -201,6 +225,7 @@ boolean succeeded; ProgressDialog dlg; OfflineStationProvider osp; + String exMsg; public StationLoader(OfflineStationProvider osp) { this.osp = osp; @@ -213,18 +238,19 @@ 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.setCancelable(true); + dlg.setMessage( getText(R.string.welcome_downloadingstations) ); + dlg.setCancelable(false); dlg.show(); } @@ -239,6 +265,8 @@ Editor edit = prefs.edit(); edit.putLong(stationsreload, System.currentTimeMillis() ); edit.commit(); + } else { + Toast.makeText(WelcomeScreen.this, "Error " + exMsg, Toast.LENGTH_LONG).show(); } } }