/[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 2123 by torben, Wed Mar 5 12:11:16 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 22  import android.view.Window; Line 21  import android.view.Window;
21  import android.widget.Button;  import android.widget.Button;
22  import android.widget.Toast;  import android.widget.Toast;
23    
24    import com.example.android.trivialdrivesample.util.IabHelper;
25    import com.example.android.trivialdrivesample.util.IabResult;
26  import com.nullwire.trace.ExceptionHandler;  import com.nullwire.trace.ExceptionHandler;
27    
28  import dk.thoerup.androidutils.CheckUpdates;  import dk.thoerup.androidutils.CheckUpdates;
# Line 43  public class WelcomeScreen extends Activ Line 44  public class WelcomeScreen extends Activ
44                  ListFavorites                  ListFavorites
45          }          }
46                    
47            IabHelper mHelper;
48            
49          Handler handler = new Handler();          Handler handler = new Handler();
50                    
51          SharedPreferences prefs;          SharedPreferences prefs;
52                    
53            StationLoader stationLoader;
54            
55          @Override          @Override
56          public void onCreate(Bundle savedInstanceState) {          public void onCreate(Bundle savedInstanceState) {
57                                    
# Line 68  public class WelcomeScreen extends Activ Line 73  public class WelcomeScreen extends Activ
73                                    
74                  Button aboutButton = (Button) findViewById(R.id.about);                  Button aboutButton = (Button) findViewById(R.id.about);
75                  aboutButton.setOnClickListener( new AboutListener() );                  aboutButton.setOnClickListener( new AboutListener() );
76                            
77                  ExceptionHandler.register(this, "http://t-hoerup.dk/android/trace.php");                  //Got stacktraces / reports that the register thing sometimes crashes
78                    try {  
79                            ExceptionHandler.register(this, "http://t-hoerup.dk/android/trace.php");
80                    } catch (Exception e) {
81                            Log.e("TrainInfo", "Error registering exception handler", e);
82                    }
83                                    
84                  CheckUpdates update = new CheckUpdates();                  CheckUpdates update = new CheckUpdates();
85                  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 101  public class WelcomeScreen extends Activ
101                          long last = prefs.getLong(stationsreload, 0);                          long last = prefs.getLong(stationsreload, 0);
102                          long now = System.currentTimeMillis();                          long now = System.currentTimeMillis();
103                          Log.i("TrainInfo", "Last Load: " + last);                          Log.i("TrainInfo", "Last Load: " + last);
104                            
105                          if ( (now-last) > (14*24*60*60*1000) ) {                          boolean didLoad = false;
106                                  new StationLoader(osp).execute( (Void)null);  
107                            try {
108                                    didLoad = osp.loadStations(this);
109                            }                                                                                              
110                            catch (InvalidClassException e) {
111                                    Log.i("TrainInfo", "invalid class - do a new download of stationlist");
112                            }
113                            catch (Exception e) {
114                                    Toast.makeText(this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();
115                                    Log.e("TrainInfo", "load error", e);
116                            }
117    
118                            if (didLoad == false) {                                
119                                    stationLoader = new StationLoader(osp, false);
120                                    stationLoader.execute( (Void)null);
121                          } else {                          } else {
122                                                            if ( (now-last) > (14*24*60*60*1000) ) { //if we had a stations list but it was too old, load a new one silent
123                                  boolean didLoad = false;                                          Log.i("TrainInfo", "Stationlist too old, do a silent download");
124                                                                            stationLoader = new StationLoader(osp, true);
125                                  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);  
126                                  }                                  }
127                          }                          }
128    
129                  }                  }
130                    
131                    
132                    try {
133                            initBilling();
134                    } catch (Exception e) {
135                            //Do nothing - just don't crash if billing failed to initialize
136                    }
137    
138    
139          }          }
140                    
141            private void initBilling() {
142                    String base64EncodedPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAln3XRJzM8Eo/SSMBLzSOj9pLedftZrWONRLO9KC/BU1Bv0EK3PQhza1S6apS69fa300ZitKSB91WDkf88eW4VJq8txmq8W1J2x/WJCFRQnFaPP0pDgERo9EdEULH2ybsOape/7+ypqNaDWxuqBh4fQfAd+Qpgdz8E7y5ZNbIKqJnRn2gyEfpzytiNQKNATI+XxJfyrS7urg/u/hXYw1R7VBNoAO7KU3b4D205WtXBn2u+Zexjd5disVGmZ5jd0BiuVOyPIWK5e9tA/VnKl/6XDfB9utCtXcAWFVUoVE6+8HWwo9ugFauuwYmbl6pTZydPKjyh9WxVR5MS2TPue/wEwIDAQAB";
143    
144                    // compute your public key and store it in base64EncodedPublicKey
145                    mHelper = new IabHelper(this, base64EncodedPublicKey);
146                    mHelper.enableDebugLogging(true);
147                    
148                    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
149                            public void onIabSetupFinished(IabResult result) {
150    
151                                    Log.d("TrainInfo", "onIabSetupFinished");
152    
153                                    if (!result.isSuccess()) {
154                                            // Oh noes, there was a problem.
155                                            Log.d("TrainInfo", "Problem setting up In-app Billing: " + result);
156                                    }            
157                                    // Hooray, IAB is fully set up!  
158                            }
159                    });
160                    
161                            }
162    
163    
164    
165          @Override          @Override
166          protected void onDestroy() {          protected void onDestroy() {
167                  super.onDestroy();                  super.onDestroy();
168                    if (stationLoader != null) {
169                            stationLoader.cancel(true);
170                            stationLoader = null;
171                    }
172                    if (mHelper != null) {
173                            try {
174                                    mHelper.dispose();
175                            } catch (Exception e) {
176                                    //Do Nothing
177                            }
178                            mHelper = null;
179                    }
180                    
181                    
182                  ProviderFactory.purgeOldEntries(); //exiting application, do some cleanup                  ProviderFactory.purgeOldEntries(); //exiting application, do some cleanup
183          }          }
184    
# Line 151  public class WelcomeScreen extends Activ Line 208  public class WelcomeScreen extends Activ
208                                                    
209                  case MENU_RELOAD:                  case MENU_RELOAD:
210                          OfflineStationProvider osp = (OfflineStationProvider) ProviderFactory.getStationProvider();                          OfflineStationProvider osp = (OfflineStationProvider) ProviderFactory.getStationProvider();
211                          new StationLoader(osp).execute( (Void)null);                                              new StationLoader(osp, false).execute( (Void)null);                    
212                          break;                          break;
213                                                    
214                  default:                  default:
# Line 176  public class WelcomeScreen extends Activ Line 233  public class WelcomeScreen extends Activ
233                          message.append("By Torben H. Nielsen\n");                          message.append("By Torben H. Nielsen\n");
234    
235                          MessageBox.showMessage(WelcomeScreen.this, message.toString());*/                          MessageBox.showMessage(WelcomeScreen.this, message.toString());*/
236                          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/"));
237                          startActivity(browserIntent);                          startActivity(browserIntent);*/
238                            
239                            Intent intent = new Intent(WelcomeScreen.this, AboutScreen.class);
240                            WelcomeScreen.this.startActivity(intent);
241                  }                  }
242    
243          }          }
# Line 191  public class WelcomeScreen extends Activ Line 251  public class WelcomeScreen extends Activ
251                                    
252                  @Override                  @Override
253                  public void onClick(View v) {                  public void onClick(View v) {
254                            
255                            StationProvider sp = ProviderFactory.getStationProvider();
256                            
257                            if (sp instanceof OfflineStationProvider ) {
258                                    OfflineStationProvider osp = (OfflineStationProvider) sp;
259                                    
260                                    if (! osp.hasStations()) {
261                                            stationLoader = new StationLoader(osp, false);
262                                            stationLoader.execute( (Void)null);    
263                                            return;
264                                    }
265                            }
266                            
267                          Intent intent = new Intent(WelcomeScreen.this, StationList.class);                          Intent intent = new Intent(WelcomeScreen.this, StationList.class);
268                          intent.putExtra("type", launchType);                          intent.putExtra("type", launchType);
269                          WelcomeScreen.this.startActivity(intent);                          WelcomeScreen.this.startActivity(intent);
# Line 205  public class WelcomeScreen extends Activ Line 278  public class WelcomeScreen extends Activ
278                  ProgressDialog dlg;                  ProgressDialog dlg;
279                  OfflineStationProvider osp;                  OfflineStationProvider osp;
280                  String exMsg;                  String exMsg;
281                    boolean silent;
282                                    
283                  public StationLoader(OfflineStationProvider osp) {                  public StationLoader(OfflineStationProvider osp, boolean silent) {
284                          this.osp = osp;                          this.osp = osp;
285                            this.silent = silent;
286                  }                  }
287                                    
288                  @Override                  @Override
# Line 217  public class WelcomeScreen extends Activ Line 292  public class WelcomeScreen extends Activ
292                                  succeeded = true;                                  succeeded = true;
293                          } catch (Exception e) {                          } catch (Exception e) {
294                                  succeeded = false;                                  succeeded = false;
295                                  exMsg = e.getMessage();                                                          exMsg = e.getMessage();
296                                    Log.e("TrainInfo", "download error", e);                                
297                          }                          }
298                          return null;                          return null;
299                  }                  }
# Line 226  public class WelcomeScreen extends Activ Line 302  public class WelcomeScreen extends Activ
302                  @Override                  @Override
303                  protected void onPreExecute() {                  protected void onPreExecute() {
304                          super.onPreExecute();                          super.onPreExecute();
305                            Log.i("TrainInfo", "StationLoader.onPreExecute() ");
306                                                    
307                          dlg = new ProgressDialog(WelcomeScreen.this);                          if (silent == false) {
308                          dlg.setMessage( getText(R.string.welcome_downloadingstations) );                                  dlg = new ProgressDialog(WelcomeScreen.this);
309                          dlg.setCancelable(true);                                  dlg.setMessage( getText(R.string.welcome_downloadingstations) );
310                          dlg.show();                                  dlg.setCancelable(false);
311                                    dlg.show();
312                            }
313                  }                  }
314    
315                  @Override                  @Override
316                  protected void onPostExecute(Void result) {                  protected void onPostExecute(Void result) {
317                          super.onPostExecute(result);                          super.onPostExecute(result);
318                            Log.i("TrainInfo", "StationLoader.onPostExecute() ");
319                                                    
320                          dlg.dismiss();                          if (silent == false) {
321                          dlg = null;                                  try {
322                                            dlg.dismiss();
323                                    } catch (Exception e) {
324                                            Log.e("TrainInfo", "Exception while closing dialog", e); // don't crash program
325                                    }
326                                    dlg = null;
327                            }
328                                                    
329                          if (succeeded) {                          if (succeeded) {
330                                  Editor edit = prefs.edit();                                  Editor edit = prefs.edit();
331                                  edit.putLong(stationsreload, System.currentTimeMillis() );                                  edit.putLong(stationsreload, System.currentTimeMillis() );
332                                  edit.commit();                                  edit.commit();
333                          } else {                          } else {
334                                  Toast.makeText(WelcomeScreen.this, "Error " + exMsg, Toast.LENGTH_LONG).show();                                  if (silent == false) {
335                                            Toast.makeText(WelcomeScreen.this, "Error " + exMsg, Toast.LENGTH_LONG).show();
336                                    }
337                          }                          }
338                  }                        }      
339          }          }

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

  ViewVC Help
Powered by ViewVC 1.1.20