--- android/TrainInfo/src/dk/thoerup/traininfo/WelcomeScreen.java 2010/06/14 06:34:29 846 +++ android/TrainInfo/src/dk/thoerup/traininfo/WelcomeScreen.java 2011/07/07 19:07:55 1547 @@ -1,21 +1,32 @@ package dk.thoerup.traininfo; 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.os.Message; +import android.util.Log; 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"; + public enum ListType { ListNearest, ListSearch, @@ -24,10 +35,16 @@ Handler handler = new Handler(); + SharedPreferences prefs; + @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); @@ -39,13 +56,16 @@ 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() ); 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 @@ -56,8 +76,43 @@ }; handler.postDelayed(r, 1500); */ + + StationProvider sp = ProviderFactory.getStationProvider(); + + 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 { + + boolean didLoad = osp.loadStations(this); + + if (didLoad == false) { + new StationLoader(osp).execute( (Void)null); + } + } + + } catch (Exception e) { + Toast.makeText(this, "" + e.getMessage(), Toast.LENGTH_SHORT).show(); + } + } } + + + @Override + protected void onDestroy() { + super.onDestroy(); + ProviderFactory.purgeOldEntries(); //exiting application, do some cleanup + } + + + class AboutListener implements OnClickListener { @Override @@ -72,11 +127,20 @@ 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 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; @@ -92,4 +156,53 @@ } } + + class StationLoader extends AsyncTask { + + + boolean succeeded; + ProgressDialog dlg; + OfflineStationProvider osp; + + 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; + Toast.makeText(WelcomeScreen.this, "Error " + e.getMessage(), Toast.LENGTH_LONG).show(); + } + return null; + } + + @Override + protected void onPreExecute() { + super.onPreExecute(); + + dlg = new ProgressDialog(WelcomeScreen.this); + dlg.setMessage( "Downloading stations list" );//TODO: translate + dlg.setCancelable(true); + 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(); + } + } + } + }