/[projects]/android/TrainInfo/src/main/java/dk/thoerup/traininfo/WelcomeScreen.java
ViewVC logotype

Diff of /android/TrainInfo/src/main/java/dk/thoerup/traininfo/WelcomeScreen.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1555 by torben, Fri Jul 8 12:24:48 2011 UTC revision 2124 by torben, Wed Mar 5 21:02:04 2014 UTC
# Line 9  import android.app.ProgressDialog; Line 9  import android.app.ProgressDialog;
9  import android.content.Intent;  import android.content.Intent;
10  import android.content.SharedPreferences;  import android.content.SharedPreferences;
11  import android.content.SharedPreferences.Editor;  import android.content.SharedPreferences.Editor;
 import android.net.Uri;  
12  import android.os.AsyncTask;  import android.os.AsyncTask;
13  import android.os.Bundle;  import android.os.Bundle;
14  import android.os.Handler;  import android.os.Handler;
# Line 43  public class WelcomeScreen extends Activ Line 42  public class WelcomeScreen extends Activ
42                  ListFavorites                  ListFavorites
43          }          }
44                    
45            
46          Handler handler = new Handler();          Handler handler = new Handler();
47                    
48          SharedPreferences prefs;          SharedPreferences prefs;
49                    
50            StationLoader stationLoader;
51            
52          @Override          @Override
53          public void onCreate(Bundle savedInstanceState) {          public void onCreate(Bundle savedInstanceState) {
54                                    
# Line 68  public class WelcomeScreen extends Activ Line 70  public class WelcomeScreen extends Activ
70                                    
71                  Button aboutButton = (Button) findViewById(R.id.about);                  Button aboutButton = (Button) findViewById(R.id.about);
72                  aboutButton.setOnClickListener( new AboutListener() );                  aboutButton.setOnClickListener( new AboutListener() );
73                            
74                  ExceptionHandler.register(this, "http://t-hoerup.dk/android/trace.php");                  //Got stacktraces / reports that the register thing sometimes crashes
75                    try {  
76                            ExceptionHandler.register(this, "http://t-hoerup.dk/android/trace.php");
77                    } catch (Exception e) {
78                            Log.e("TrainInfo", "Error registering exception handler", e);
79                    }
80                                    
81                  CheckUpdates update = new CheckUpdates();                  CheckUpdates update = new CheckUpdates();
82                  update.checkForUpdates(this, "http://t-hoerup.dk/android/traininfo/version.txt", "TrainInfo DK", null);                  update.checkForUpdates(this, "http://t-hoerup.dk/android/traininfo/version.txt", "TrainInfo DK", null);
# Line 91  public class WelcomeScreen extends Activ Line 98  public class WelcomeScreen extends Activ
98                          long last = prefs.getLong(stationsreload, 0);                          long last = prefs.getLong(stationsreload, 0);
99                          long now = System.currentTimeMillis();                          long now = System.currentTimeMillis();
100                          Log.i("TrainInfo", "Last Load: " + last);                          Log.i("TrainInfo", "Last Load: " + last);
101                            
102                          if ( (now-last) > (14*24*60*60*1000) ) {                          boolean didLoad = false;
103                                  new StationLoader(osp).execute( (Void)null);  
104                            try {
105                                    didLoad = osp.loadStations(this);
106                            }                                                                                              
107                            catch (InvalidClassException e) {
108                                    Log.i("TrainInfo", "invalid class - do a new download of stationlist");
109                            }
110                            catch (Exception e) {
111                                    Toast.makeText(this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();
112                                    Log.e("TrainInfo", "load error", e);
113                            }
114    
115                            if (didLoad == false) {                                
116                                    stationLoader = new StationLoader(osp, false);
117                                    stationLoader.execute( (Void)null);
118                          } else {                          } else {
119                                                            if ( (now-last) > (14*24*60*60*1000) ) { //if we had a stations list but it was too old, load a new one silent
120                                  boolean didLoad = false;                                          Log.i("TrainInfo", "Stationlist too old, do a silent download");
121                                                                            stationLoader = new StationLoader(osp, true);
122                                  try {                                          stationLoader.execute( (Void)null);
                                         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) {                                  
                                         new StationLoader(osp).execute( (Void)null);  
123                                  }                                  }
124                          }                          }
125    
126                  }                  }
127                    
128                    
129          }          }
130                    
131            
132                    
133          @Override          @Override
134          protected void onDestroy() {          protected void onDestroy() {
135                  super.onDestroy();                  super.onDestroy();
136                    if (stationLoader != null) {
137                            stationLoader.cancel(true);
138                            stationLoader = null;
139                    }              
140                    
141                  ProviderFactory.purgeOldEntries(); //exiting application, do some cleanup                  ProviderFactory.purgeOldEntries(); //exiting application, do some cleanup
142          }          }
143    
# Line 151  public class WelcomeScreen extends Activ Line 167  public class WelcomeScreen extends Activ
167                                                    
168                  case MENU_RELOAD:                  case MENU_RELOAD:
169                          OfflineStationProvider osp = (OfflineStationProvider) ProviderFactory.getStationProvider();                          OfflineStationProvider osp = (OfflineStationProvider) ProviderFactory.getStationProvider();
170                          new StationLoader(osp).execute( (Void)null);                                              new StationLoader(osp, false).execute( (Void)null);                    
171                          break;                          break;
172                                                    
173                  default:                  default:
# Line 176  public class WelcomeScreen extends Activ Line 192  public class WelcomeScreen extends Activ
192                          message.append("By Torben H. Nielsen\n");                          message.append("By Torben H. Nielsen\n");
193    
194                          MessageBox.showMessage(WelcomeScreen.this, message.toString());*/                          MessageBox.showMessage(WelcomeScreen.this, message.toString());*/
195                          Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://t-hoerup.dk/android/traininfo/"));                          /*Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://t-hoerup.dk/android/traininfo/"));
196                          startActivity(browserIntent);                          startActivity(browserIntent);*/
197                            
198                            Intent intent = new Intent(WelcomeScreen.this, AboutScreen.class);
199                            WelcomeScreen.this.startActivity(intent);
200                  }                  }
201    
202          }          }
# Line 191  public class WelcomeScreen extends Activ Line 210  public class WelcomeScreen extends Activ
210                                    
211                  @Override                  @Override
212                  public void onClick(View v) {                  public void onClick(View v) {
213                            
214                            StationProvider sp = ProviderFactory.getStationProvider();
215                            
216                            if (sp instanceof OfflineStationProvider ) {
217                                    OfflineStationProvider osp = (OfflineStationProvider) sp;
218                                    
219                                    if (! osp.hasStations()) {
220                                            stationLoader = new StationLoader(osp, false);
221                                            stationLoader.execute( (Void)null);    
222                                            return;
223                                    }
224                            }
225                            
226                          Intent intent = new Intent(WelcomeScreen.this, StationList.class);                          Intent intent = new Intent(WelcomeScreen.this, StationList.class);
227                          intent.putExtra("type", launchType);                          intent.putExtra("type", launchType);
228                          WelcomeScreen.this.startActivity(intent);                          WelcomeScreen.this.startActivity(intent);
# Line 205  public class WelcomeScreen extends Activ Line 237  public class WelcomeScreen extends Activ
237                  ProgressDialog dlg;                  ProgressDialog dlg;
238                  OfflineStationProvider osp;                  OfflineStationProvider osp;
239                  String exMsg;                  String exMsg;
240                    boolean silent;
241                                    
242                  public StationLoader(OfflineStationProvider osp) {                  public StationLoader(OfflineStationProvider osp, boolean silent) {
243                          this.osp = osp;                          this.osp = osp;
244                            this.silent = silent;
245                  }                  }
246                                    
247                  @Override                  @Override
# Line 217  public class WelcomeScreen extends Activ Line 251  public class WelcomeScreen extends Activ
251                                  succeeded = true;                                  succeeded = true;
252                          } catch (Exception e) {                          } catch (Exception e) {
253                                  succeeded = false;                                  succeeded = false;
254                                  exMsg = e.getMessage();                                                          exMsg = e.getMessage();
255                                    Log.e("TrainInfo", "download error", e);                                
256                          }                          }
257                          return null;                          return null;
258                  }                  }
259    
260                    
261                  @Override                  @Override
262                  protected void onPreExecute() {                  protected void onPreExecute() {
263                          super.onPreExecute();                          super.onPreExecute();
264                            Log.i("TrainInfo", "StationLoader.onPreExecute() ");
265                                                    
266                          dlg = new ProgressDialog(WelcomeScreen.this);                          if (silent == false) {
267                          dlg.setMessage( "Downloading stations list" );//TODO: translate                                  dlg = new ProgressDialog(WelcomeScreen.this);
268                          dlg.setCancelable(true);                                  dlg.setMessage( getText(R.string.welcome_downloadingstations) );
269                          dlg.show();                                  dlg.setCancelable(false);
270                                    dlg.show();
271                            }
272                  }                  }
273    
274                  @Override                  @Override
275                  protected void onPostExecute(Void result) {                  protected void onPostExecute(Void result) {
276                          super.onPostExecute(result);                          super.onPostExecute(result);
277                            Log.i("TrainInfo", "StationLoader.onPostExecute() ");
278                                                    
279                          dlg.dismiss();                          if (silent == false) {
280                          dlg = null;                                  try {
281                                            dlg.dismiss();
282                                    } catch (Exception e) {
283                                            Log.e("TrainInfo", "Exception while closing dialog", e); // don't crash program
284                                    }
285                                    dlg = null;
286                            }
287                                                    
288                          if (succeeded) {                          if (succeeded) {
289                                  Editor edit = prefs.edit();                                  Editor edit = prefs.edit();
290                                  edit.putLong(stationsreload, System.currentTimeMillis() );                                  edit.putLong(stationsreload, System.currentTimeMillis() );
291                                  edit.commit();                                  edit.commit();
292                          } else {                          } else {
293                                  Toast.makeText(WelcomeScreen.this, "Error " + exMsg, Toast.LENGTH_LONG).show();                                  if (silent == false) {
294                                            Toast.makeText(WelcomeScreen.this, "Error " + exMsg, Toast.LENGTH_LONG).show();
295                                    }
296                          }                          }
297                  }                        }      
298          }          }

Legend:
Removed from v.1555  
changed lines
  Added in v.2124

  ViewVC Help
Powered by ViewVC 1.1.20