--- android/TrainInfo/src/dk/thoerup/traininfo/WelcomeScreen.java 2011/07/07 19:07:55 1547 +++ android/TrainInfo/src/dk/thoerup/traininfo/WelcomeScreen.java 2011/07/09 07:20:57 1567 @@ -1,5 +1,9 @@ package dk.thoerup.traininfo; + + +import java.io.InvalidClassException; + import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; @@ -10,6 +14,8 @@ import android.os.Bundle; import android.os.Handler; import android.util.Log; +import android.view.Menu; +import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; @@ -23,9 +29,13 @@ import dk.thoerup.traininfo.provider.ProviderFactory; import dk.thoerup.traininfo.provider.StationProvider; + public class WelcomeScreen extends Activity{ final static String stationsreload = "stationsreload"; + final static int MENU_SETTINGS = 1; + final static int MENU_RELOAD = 2; + public enum ListType { ListNearest, @@ -37,6 +47,8 @@ SharedPreferences prefs; + StationLoader stationLoader; + @Override public void onCreate(Bundle savedInstanceState) { @@ -56,9 +68,6 @@ Button favoritesButton = (Button) findViewById(R.id.favorites); favoritesButton.setOnClickListener( new StationListListener(ListType.ListFavorites)); - Button settingsButton = (Button) findViewById(R.id.settings); - settingsButton.setOnClickListener( new SettingsListener() ); - Button aboutButton = (Button) findViewById(R.id.about); aboutButton.setOnClickListener( new AboutListener() ); @@ -81,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); + } } + } } @@ -108,10 +126,49 @@ @Override protected void onDestroy() { super.onDestroy(); + if (stationLoader != null) { + stationLoader.cancel(false); + } + ProviderFactory.purgeOldEntries(); //exiting application, do some cleanup } + @Override + public boolean onCreateOptionsMenu(Menu menu) { + MenuItem item; + + item = menu.add(0, MENU_SETTINGS, 0, getString(R.string.welcome_settings) ); + item.setIcon(android.R.drawable.ic_menu_preferences); + + item = menu.add(0, MENU_RELOAD, 0, getString(R.string.welcome_reloadstations)); + item.setIcon(android.R.drawable.ic_menu_rotate); + + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + boolean retval = true; + + switch (item.getItemId()) { + case MENU_SETTINGS: + Intent intent = new Intent(WelcomeScreen.this, SettingsScreen.class); + WelcomeScreen.this.startActivity(intent); + break; + + case MENU_RELOAD: + OfflineStationProvider osp = (OfflineStationProvider) ProviderFactory.getStationProvider(); + new StationLoader(osp).execute( (Void)null); + break; + + default: + retval = super.onOptionsItemSelected(item); + } + + return retval; + } + class AboutListener implements OnClickListener { @@ -133,14 +190,6 @@ } - class SettingsListener implements OnClickListener{ - - @Override - public void onClick(View v) { - Intent intent = new Intent(WelcomeScreen.this, SettingsScreen.class); - WelcomeScreen.this.startActivity(intent); - } - } class StationListListener implements OnClickListener{ ListType launchType; @@ -163,6 +212,7 @@ boolean succeeded; ProgressDialog dlg; OfflineStationProvider osp; + String exMsg; public StationLoader(OfflineStationProvider osp) { this.osp = osp; @@ -175,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(); } @@ -201,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(); } } }