--- android/TrainInfo/src/dk/thoerup/traininfo/WelcomeScreen.java 2009/10/29 11:38:58 484 +++ android/TrainInfo/src/dk/thoerup/traininfo/WelcomeScreen.java 2011/07/07 20:42:03 1550 @@ -1,25 +1,64 @@ package dk.thoerup.traininfo; -import dk.thoerup.traininfo.util.MessageBox; + + +import java.util.ArrayList; + 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; +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.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{ + + 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; + @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); @@ -33,13 +72,100 @@ 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", 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; + 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 { + + 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 + } + + + @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 public void onClick(View v) { - + /* String appName = WelcomeScreen.this.getResources().getString(R.string.app_name); String ver = WelcomeScreen.this.getResources().getString(R.string.app_version); @@ -48,10 +174,13 @@ message.append(" v").append(ver).append("\n"); message.append("By Torben H. Nielsen\n"); - MessageBox.showMessage(WelcomeScreen.this, message.toString()); + MessageBox.showMessage(WelcomeScreen.this, message.toString());*/ + Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://t-hoerup.dk/android/traininfo/")); + startActivity(browserIntent); } } + class StationListListener implements OnClickListener{ ListType launchType; @@ -67,4 +196,56 @@ } } + + 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( "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(); + } else { + Toast.makeText(WelcomeScreen.this, "Error " + exMsg, Toast.LENGTH_LONG).show(); + } + } + } + }