/[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 1565 by torben, Fri Jul 8 17:37:18 2011 UTC revision 1718 by torben, Mon Mar 12 09:27:14 2012 UTC
# Line 47  public class WelcomeScreen extends Activ Line 47  public class WelcomeScreen extends Activ
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          @Override          @Override
132          protected void onDestroy() {          protected void onDestroy() {
133                  super.onDestroy();                  super.onDestroy();
134                    if (stationLoader != null) {
135                            stationLoader.cancel(true);
136                    }
137                    
138                  ProviderFactory.purgeOldEntries(); //exiting application, do some cleanup                  ProviderFactory.purgeOldEntries(); //exiting application, do some cleanup
139          }          }
140    
# Line 151  public class WelcomeScreen extends Activ Line 164  public class WelcomeScreen extends Activ
164                                                    
165                  case MENU_RELOAD:                  case MENU_RELOAD:
166                          OfflineStationProvider osp = (OfflineStationProvider) ProviderFactory.getStationProvider();                          OfflineStationProvider osp = (OfflineStationProvider) ProviderFactory.getStationProvider();
167                          new StationLoader(osp).execute( (Void)null);                                              new StationLoader(osp, false).execute( (Void)null);                    
168                          break;                          break;
169                                                    
170                  default:                  default:
# Line 176  public class WelcomeScreen extends Activ Line 189  public class WelcomeScreen extends Activ
189                          message.append("By Torben H. Nielsen\n");                          message.append("By Torben H. Nielsen\n");
190    
191                          MessageBox.showMessage(WelcomeScreen.this, message.toString());*/                          MessageBox.showMessage(WelcomeScreen.this, message.toString());*/
192                          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/"));
193                          startActivity(browserIntent);                          startActivity(browserIntent);*/
194                            
195                            Intent intent = new Intent(WelcomeScreen.this, AboutScreen.class);
196                            WelcomeScreen.this.startActivity(intent);
197                  }                  }
198    
199          }          }
# Line 191  public class WelcomeScreen extends Activ Line 207  public class WelcomeScreen extends Activ
207                                    
208                  @Override                  @Override
209                  public void onClick(View v) {                  public void onClick(View v) {
210                            
211                            StationProvider sp = ProviderFactory.getStationProvider();
212                            
213                            if (sp instanceof OfflineStationProvider ) {
214                                    OfflineStationProvider osp = (OfflineStationProvider) sp;
215                                    
216                                    if (! osp.hasStations()) {
217                                            stationLoader = new StationLoader(osp, false);
218                                            stationLoader.execute( (Void)null);    
219                                            return;
220                                    }
221                            }
222                            
223                          Intent intent = new Intent(WelcomeScreen.this, StationList.class);                          Intent intent = new Intent(WelcomeScreen.this, StationList.class);
224                          intent.putExtra("type", launchType);                          intent.putExtra("type", launchType);
225                          WelcomeScreen.this.startActivity(intent);                          WelcomeScreen.this.startActivity(intent);
# Line 205  public class WelcomeScreen extends Activ Line 234  public class WelcomeScreen extends Activ
234                  ProgressDialog dlg;                  ProgressDialog dlg;
235                  OfflineStationProvider osp;                  OfflineStationProvider osp;
236                  String exMsg;                  String exMsg;
237                    boolean silent;
238                                    
239                  public StationLoader(OfflineStationProvider osp) {                  public StationLoader(OfflineStationProvider osp, boolean silent) {
240                          this.osp = osp;                          this.osp = osp;
241                            this.silent = silent;
242                  }                  }
243                                    
244                  @Override                  @Override
# Line 217  public class WelcomeScreen extends Activ Line 248  public class WelcomeScreen extends Activ
248                                  succeeded = true;                                  succeeded = true;
249                          } catch (Exception e) {                          } catch (Exception e) {
250                                  succeeded = false;                                  succeeded = false;
251                                  exMsg = e.getMessage();                                                          exMsg = e.getMessage();
252                                    Log.e("TrainInfo", "download error", e);                                
253                          }                          }
254                          return null;                          return null;
255                  }                  }
# Line 226  public class WelcomeScreen extends Activ Line 258  public class WelcomeScreen extends Activ
258                  @Override                  @Override
259                  protected void onPreExecute() {                  protected void onPreExecute() {
260                          super.onPreExecute();                          super.onPreExecute();
261                            Log.i("TrainInfo", "StationLoader.onPreExecute() ");
262                                                    
263                          dlg = new ProgressDialog(WelcomeScreen.this);                          if (silent == false) {
264                          dlg.setMessage( getText(R.string.welcome_downloadingstations) );                                  dlg = new ProgressDialog(WelcomeScreen.this);
265                          dlg.setCancelable(true);                                  dlg.setMessage( getText(R.string.welcome_downloadingstations) );
266                          dlg.show();                                  dlg.setCancelable(false);
267                                    dlg.show();
268                            }
269                  }                  }
270    
271                  @Override                  @Override
272                  protected void onPostExecute(Void result) {                  protected void onPostExecute(Void result) {
273                          super.onPostExecute(result);                          super.onPostExecute(result);
274                            Log.i("TrainInfo", "StationLoader.onPostExecute() ");
275                                                    
276                          dlg.dismiss();                          if (silent == false) {
277                          dlg = null;                                  try {
278                                            dlg.dismiss();
279                                    } catch (Exception e) {
280                                            Log.e("TrainInfo", "Exception while closing dialog", e); // don't crash program
281                                    }
282                                    dlg = null;
283                            }
284                                                    
285                          if (succeeded) {                          if (succeeded) {
286                                  Editor edit = prefs.edit();                                  Editor edit = prefs.edit();
287                                  edit.putLong(stationsreload, System.currentTimeMillis() );                                  edit.putLong(stationsreload, System.currentTimeMillis() );
288                                  edit.commit();                                  edit.commit();
289                          } else {                          } else {
290                                  Toast.makeText(WelcomeScreen.this, "Error " + exMsg, Toast.LENGTH_LONG).show();                                  if (silent == false) {
291                                            Toast.makeText(WelcomeScreen.this, "Error " + exMsg, Toast.LENGTH_LONG).show();
292                                    }
293                          }                          }
294                  }                        }      
295          }          }

Legend:
Removed from v.1565  
changed lines
  Added in v.1718

  ViewVC Help
Powered by ViewVC 1.1.20