--- android/TrainInfo/src/dk/thoerup/traininfo/WelcomeScreen.java 2010/05/10 19:36:43 720 +++ android/TrainInfo/src/dk/thoerup/traininfo/WelcomeScreen.java 2011/07/09 08:31:26 1571 @@ -1,29 +1,62 @@ package dk.thoerup.traininfo; + + +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.net.Uri; +import android.os.AsyncTask; 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.Window; import android.view.View.OnClickListener; +import android.view.Window; import android.widget.Button; +import android.widget.Toast; import com.nullwire.trace.ExceptionHandler; -import dk.thoerup.checkupdates.CheckUpdates; +import dk.thoerup.androidutils.CheckUpdates; +import dk.thoerup.traininfo.provider.OfflineStationProvider; +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, ListSearch, ListFavorites } + Handler handler = new Handler(); + + SharedPreferences prefs; + + StationLoader stationLoader; + @Override public void onCreate(Bundle savedInstanceState) { - requestWindowFeature( Window.FEATURE_NO_TITLE ); + super.onCreate(savedInstanceState); + + prefs = getSharedPreferences("TrainStation", 0); + + requestWindowFeature( Window.FEATURE_NO_TITLE ); setContentView(R.layout.welcome); Button nearestButton = (Button) findViewById(R.id.nearest); @@ -41,9 +74,102 @@ ExceptionHandler.register(this, "http://t-hoerup.dk/android/trace.php"); CheckUpdates update = new CheckUpdates(); - update.checkForUpdates(this, "http://t-hoerup.dk/android/traininfo/version.txt", "TrainInfo DK"); + update.checkForUpdates(this, "http://t-hoerup.dk/android/traininfo/version.txt", "TrainInfo DK", null); + /* + Runnable r = new Runnable() { + @Override + public void run() { + View splash = findViewById(R.id.splash); + splash.setVisibility(View.GONE); + } + }; + handler.postDelayed(r, 1500); + */ + + StationProvider sp = ProviderFactory.getStationProvider(); + + if (sp instanceof OfflineStationProvider ) { + OfflineStationProvider osp = (OfflineStationProvider) sp; + 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; + + 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); + } + + if (didLoad == false) { + stationLoader = new StationLoader(osp); + stationLoader.execute( (Void)null); + } + } + + } } + + + @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 { @Override @@ -58,11 +184,12 @@ message.append("By Torben H. Nielsen\n"); MessageBox.showMessage(WelcomeScreen.this, message.toString());*/ - Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.t-hoerup.dk/android/traininfo/")); + Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://t-hoerup.dk/android/traininfo/")); startActivity(browserIntent); } } + class StationListListener implements OnClickListener{ ListType launchType; @@ -78,4 +205,57 @@ } } + + class StationLoader extends AsyncTask { + + + boolean succeeded; + ProgressDialog dlg; + OfflineStationProvider osp; + String exMsg; + + public StationLoader(OfflineStationProvider osp) { + this.osp = osp; + } + + @Override + protected Void doInBackground(Void... params) { + try { + osp.downloadStations( WelcomeScreen.this ); + succeeded = true; + } catch (Exception e) { + succeeded = false; + exMsg = e.getMessage(); + } + return null; + } + + + @Override + protected void onPreExecute() { + super.onPreExecute(); + + dlg = new ProgressDialog(WelcomeScreen.this); + dlg.setMessage( getText(R.string.welcome_downloadingstations) ); + dlg.setCancelable(false); + dlg.show(); + } + + @Override + protected void onPostExecute(Void result) { + super.onPostExecute(result); + + dlg.dismiss(); + dlg = null; + + if (succeeded) { + Editor edit = prefs.edit(); + edit.putLong(stationsreload, System.currentTimeMillis() ); + edit.commit(); + } else { + Toast.makeText(WelcomeScreen.this, "Error " + exMsg, Toast.LENGTH_LONG).show(); + } + } + } + }