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.View.OnClickListener; import android.view.Window; import android.widget.Button; import android.widget.Toast; import com.nullwire.trace.ExceptionHandler; 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) { super.onCreate(savedInstanceState); prefs = getSharedPreferences("TrainStation", 0); requestWindowFeature( Window.FEATURE_NO_TITLE ); setContentView(R.layout.welcome); Button nearestButton = (Button) findViewById(R.id.nearest); nearestButton.setOnClickListener( new StationListListener(ListType.ListNearest)); Button searchButton = (Button) findViewById(R.id.search); searchButton.setOnClickListener( new StationListListener(ListType.ListSearch)); Button favoritesButton = (Button) findViewById(R.id.favorites); favoritesButton.setOnClickListener( new StationListListener(ListType.ListFavorites)); Button aboutButton = (Button) findViewById(R.id.about); aboutButton.setOnClickListener( new AboutListener() ); //Got stacktraces / reports that the register thing sometimes crashes try { ExceptionHandler.register(this, "http://t-hoerup.dk/android/trace.php"); } catch (Exception e) { Log.e("TrainInfo", "Error registering exception handler", e); } 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; long last = prefs.getLong(stationsreload, 0); long now = System.currentTimeMillis(); Log.i("TrainInfo", "Last Load: " + last); 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, false); stationLoader.execute( (Void)null); } else { if ( (now-last) > (14*24*60*60*1000) ) { //if we had a stations list but it was too old, load a new one silent Log.i("TrainInfo", "Stationlist too old, do a silent download"); stationLoader = new StationLoader(osp, true); stationLoader.execute( (Void)null); } } } } @Override protected void onDestroy() { super.onDestroy(); if (stationLoader != null) { stationLoader.cancel(true); } 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, false).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); StringBuffer message = new StringBuffer(); message.append(appName); message.append(" v").append(ver).append("\n"); message.append("By Torben H. Nielsen\n"); MessageBox.showMessage(WelcomeScreen.this, message.toString());*/ /*Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://t-hoerup.dk/android/traininfo/")); startActivity(browserIntent);*/ Intent intent = new Intent(WelcomeScreen.this, AboutScreen.class); WelcomeScreen.this.startActivity(intent); } } class StationListListener implements OnClickListener{ ListType launchType; StationListListener(ListType type) { launchType = type; } @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, false); stationLoader.execute( (Void)null); return; } } Intent intent = new Intent(WelcomeScreen.this, StationList.class); intent.putExtra("type", launchType); WelcomeScreen.this.startActivity(intent); } } class StationLoader extends AsyncTask { boolean succeeded; ProgressDialog dlg; OfflineStationProvider osp; String exMsg; boolean silent; public StationLoader(OfflineStationProvider osp, boolean silent) { this.osp = osp; this.silent = silent; } @Override protected Void doInBackground(Void... params) { try { osp.downloadStations( WelcomeScreen.this ); succeeded = true; } catch (Exception e) { succeeded = false; exMsg = e.getMessage(); Log.e("TrainInfo", "download error", e); } return null; } @Override protected void onPreExecute() { super.onPreExecute(); Log.i("TrainInfo", "StationLoader.onPreExecute() "); if (silent == false) { 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); Log.i("TrainInfo", "StationLoader.onPostExecute() "); if (silent == false) { dlg.dismiss(); dlg = null; } if (succeeded) { Editor edit = prefs.edit(); edit.putLong(stationsreload, System.currentTimeMillis() ); edit.commit(); } else { if (silent == false) { Toast.makeText(WelcomeScreen.this, "Error " + exMsg, Toast.LENGTH_LONG).show(); } } } } }