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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2123 - (hide annotations) (download)
Wed Mar 5 12:11:16 2014 UTC (10 years, 2 months ago) by torben
Original Path: android/TrainInfo/src/dk/thoerup/traininfo/WelcomeScreen.java
File size: 9680 byte(s)
Add billing code
1 torben 481 package dk.thoerup.traininfo;
2    
3 torben 1548
4    
5 torben 1555 import java.io.InvalidClassException;
6    
7 torben 481 import android.app.Activity;
8 torben 1547 import android.app.ProgressDialog;
9 torben 481 import android.content.Intent;
10 torben 1547 import android.content.SharedPreferences;
11     import android.content.SharedPreferences.Editor;
12     import android.os.AsyncTask;
13 torben 481 import android.os.Bundle;
14 torben 743 import android.os.Handler;
15 torben 1159 import android.util.Log;
16 torben 1548 import android.view.Menu;
17     import android.view.MenuItem;
18 torben 481 import android.view.View;
19 torben 1547 import android.view.View.OnClickListener;
20 torben 482 import android.view.Window;
21 torben 481 import android.widget.Button;
22 torben 1547 import android.widget.Toast;
23 torben 481
24 torben 2123 import com.example.android.trivialdrivesample.util.IabHelper;
25     import com.example.android.trivialdrivesample.util.IabResult;
26 torben 720 import com.nullwire.trace.ExceptionHandler;
27    
28 torben 1131 import dk.thoerup.androidutils.CheckUpdates;
29 torben 1547 import dk.thoerup.traininfo.provider.OfflineStationProvider;
30 torben 1159 import dk.thoerup.traininfo.provider.ProviderFactory;
31 torben 1547 import dk.thoerup.traininfo.provider.StationProvider;
32 torben 720
33 torben 1551
34 torben 481 public class WelcomeScreen extends Activity{
35 torben 1547
36     final static String stationsreload = "stationsreload";
37 torben 1548 final static int MENU_SETTINGS = 1;
38     final static int MENU_RELOAD = 2;
39 torben 1547
40 torben 1548
41 torben 481 public enum ListType {
42     ListNearest,
43     ListSearch,
44     ListFavorites
45     }
46    
47 torben 2123 IabHelper mHelper;
48    
49 torben 743 Handler handler = new Handler();
50    
51 torben 1547 SharedPreferences prefs;
52    
53 torben 1567 StationLoader stationLoader;
54    
55 torben 481 @Override
56     public void onCreate(Bundle savedInstanceState) {
57 torben 916
58     super.onCreate(savedInstanceState);
59 torben 1547
60     prefs = getSharedPreferences("TrainStation", 0);
61    
62 torben 482 requestWindowFeature( Window.FEATURE_NO_TITLE );
63 torben 481 setContentView(R.layout.welcome);
64    
65     Button nearestButton = (Button) findViewById(R.id.nearest);
66     nearestButton.setOnClickListener( new StationListListener(ListType.ListNearest));
67    
68     Button searchButton = (Button) findViewById(R.id.search);
69     searchButton.setOnClickListener( new StationListListener(ListType.ListSearch));
70    
71     Button favoritesButton = (Button) findViewById(R.id.favorites);
72     favoritesButton.setOnClickListener( new StationListListener(ListType.ListFavorites));
73    
74     Button aboutButton = (Button) findViewById(R.id.about);
75     aboutButton.setOnClickListener( new AboutListener() );
76 torben 1714
77     //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 torben 568
84 torben 720 CheckUpdates update = new CheckUpdates();
85 torben 911 update.checkForUpdates(this, "http://t-hoerup.dk/android/traininfo/version.txt", "TrainInfo DK", null);
86 torben 846 /*
87 torben 743 Runnable r = new Runnable() {
88     @Override
89     public void run() {
90     View splash = findViewById(R.id.splash);
91     splash.setVisibility(View.GONE);
92     }
93     };
94     handler.postDelayed(r, 1500);
95 torben 846 */
96 torben 1547
97     StationProvider sp = ProviderFactory.getStationProvider();
98    
99     if (sp instanceof OfflineStationProvider ) {
100     OfflineStationProvider osp = (OfflineStationProvider) sp;
101 torben 1555 long last = prefs.getLong(stationsreload, 0);
102     long now = System.currentTimeMillis();
103     Log.i("TrainInfo", "Last Load: " + last);
104 torben 1626
105     boolean didLoad = false;
106    
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 torben 1567 stationLoader.execute( (Void)null);
121 torben 1555 } else {
122 torben 1626 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     Log.i("TrainInfo", "Stationlist too old, do a silent download");
124     stationLoader = new StationLoader(osp, true);
125 torben 1567 stationLoader.execute( (Void)null);
126 torben 1547 }
127     }
128 torben 1555
129 torben 1547 }
130 torben 2123
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 torben 481 }
140 torben 2123
141     private void initBilling() {
142     String base64EncodedPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAln3XRJzM8Eo/SSMBLzSOj9pLedftZrWONRLO9KC/BU1Bv0EK3PQhza1S6apS69fa300ZitKSB91WDkf88eW4VJq8txmq8W1J2x/WJCFRQnFaPP0pDgERo9EdEULH2ybsOape/7+ypqNaDWxuqBh4fQfAd+Qpgdz8E7y5ZNbIKqJnRn2gyEfpzytiNQKNATI+XxJfyrS7urg/u/hXYw1R7VBNoAO7KU3b4D205WtXBn2u+Zexjd5disVGmZ5jd0BiuVOyPIWK5e9tA/VnKl/6XDfB9utCtXcAWFVUoVE6+8HWwo9ugFauuwYmbl6pTZydPKjyh9WxVR5MS2TPue/wEwIDAQAB";
143 torben 1626
144 torben 2123 // 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 torben 1626
151 torben 2123 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 torben 1159 @Override
166     protected void onDestroy() {
167     super.onDestroy();
168 torben 1567 if (stationLoader != null) {
169 torben 1594 stationLoader.cancel(true);
170 torben 2123 stationLoader = null;
171 torben 1567 }
172 torben 2123 if (mHelper != null) {
173     try {
174     mHelper.dispose();
175     } catch (Exception e) {
176     //Do Nothing
177     }
178     mHelper = null;
179     }
180 torben 1567
181 torben 2123
182 torben 1159 ProviderFactory.purgeOldEntries(); //exiting application, do some cleanup
183     }
184    
185    
186 torben 1548 @Override
187     public boolean onCreateOptionsMenu(Menu menu) {
188     MenuItem item;
189    
190     item = menu.add(0, MENU_SETTINGS, 0, getString(R.string.welcome_settings) );
191     item.setIcon(android.R.drawable.ic_menu_preferences);
192    
193     item = menu.add(0, MENU_RELOAD, 0, getString(R.string.welcome_reloadstations));
194     item.setIcon(android.R.drawable.ic_menu_rotate);
195    
196     return true;
197     }
198    
199     @Override
200     public boolean onOptionsItemSelected(MenuItem item) {
201     boolean retval = true;
202    
203     switch (item.getItemId()) {
204     case MENU_SETTINGS:
205     Intent intent = new Intent(WelcomeScreen.this, SettingsScreen.class);
206     WelcomeScreen.this.startActivity(intent);
207     break;
208    
209     case MENU_RELOAD:
210     OfflineStationProvider osp = (OfflineStationProvider) ProviderFactory.getStationProvider();
211 torben 1626 new StationLoader(osp, false).execute( (Void)null);
212 torben 1548 break;
213    
214     default:
215     retval = super.onOptionsItemSelected(item);
216     }
217    
218     return retval;
219     }
220    
221 torben 1159
222 torben 481 class AboutListener implements OnClickListener {
223    
224     @Override
225     public void onClick(View v) {
226 torben 500 /*
227 torben 484 String appName = WelcomeScreen.this.getResources().getString(R.string.app_name);
228     String ver = WelcomeScreen.this.getResources().getString(R.string.app_version);
229    
230 torben 483 StringBuffer message = new StringBuffer();
231 torben 484 message.append(appName);
232     message.append(" v").append(ver).append("\n");
233 torben 483 message.append("By Torben H. Nielsen\n");
234    
235 torben 500 MessageBox.showMessage(WelcomeScreen.this, message.toString());*/
236 torben 1638 /*Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://t-hoerup.dk/android/traininfo/"));
237     startActivity(browserIntent);*/
238    
239     Intent intent = new Intent(WelcomeScreen.this, AboutScreen.class);
240     WelcomeScreen.this.startActivity(intent);
241 torben 481 }
242 torben 483
243 torben 481 }
244 torben 1446
245 torben 483
246 torben 481 class StationListListener implements OnClickListener{
247     ListType launchType;
248     StationListListener(ListType type) {
249     launchType = type;
250     }
251    
252     @Override
253     public void onClick(View v) {
254 torben 1572
255     StationProvider sp = ProviderFactory.getStationProvider();
256    
257     if (sp instanceof OfflineStationProvider ) {
258     OfflineStationProvider osp = (OfflineStationProvider) sp;
259    
260     if (! osp.hasStations()) {
261 torben 1626 stationLoader = new StationLoader(osp, false);
262 torben 1572 stationLoader.execute( (Void)null);
263     return;
264     }
265     }
266    
267 torben 481 Intent intent = new Intent(WelcomeScreen.this, StationList.class);
268     intent.putExtra("type", launchType);
269     WelcomeScreen.this.startActivity(intent);
270     }
271    
272     }
273 torben 1547
274     class StationLoader extends AsyncTask<Void,Void,Void> {
275    
276    
277     boolean succeeded;
278     ProgressDialog dlg;
279     OfflineStationProvider osp;
280 torben 1550 String exMsg;
281 torben 1626 boolean silent;
282 torben 1547
283 torben 1626 public StationLoader(OfflineStationProvider osp, boolean silent) {
284 torben 1547 this.osp = osp;
285 torben 1626 this.silent = silent;
286 torben 1547 }
287    
288     @Override
289     protected Void doInBackground(Void... params) {
290     try {
291     osp.downloadStations( WelcomeScreen.this );
292     succeeded = true;
293     } catch (Exception e) {
294     succeeded = false;
295 torben 1626 exMsg = e.getMessage();
296     Log.e("TrainInfo", "download error", e);
297 torben 1547 }
298     return null;
299     }
300    
301 torben 1565
302 torben 1547 @Override
303     protected void onPreExecute() {
304     super.onPreExecute();
305 torben 1626 Log.i("TrainInfo", "StationLoader.onPreExecute() ");
306 torben 1547
307 torben 1626 if (silent == false) {
308     dlg = new ProgressDialog(WelcomeScreen.this);
309     dlg.setMessage( getText(R.string.welcome_downloadingstations) );
310     dlg.setCancelable(false);
311     dlg.show();
312     }
313 torben 1547 }
314    
315     @Override
316     protected void onPostExecute(Void result) {
317     super.onPostExecute(result);
318 torben 1626 Log.i("TrainInfo", "StationLoader.onPostExecute() ");
319 torben 1547
320 torben 1626 if (silent == false) {
321 torben 1718 try {
322     dlg.dismiss();
323     } catch (Exception e) {
324     Log.e("TrainInfo", "Exception while closing dialog", e); // don't crash program
325     }
326 torben 1626 dlg = null;
327     }
328 torben 1547
329     if (succeeded) {
330     Editor edit = prefs.edit();
331     edit.putLong(stationsreload, System.currentTimeMillis() );
332     edit.commit();
333 torben 1550 } else {
334 torben 1626 if (silent == false) {
335     Toast.makeText(WelcomeScreen.this, "Error " + exMsg, Toast.LENGTH_LONG).show();
336     }
337 torben 1547 }
338     }
339     }
340    
341 torben 481 }

  ViewVC Help
Powered by ViewVC 1.1.20