1 |
package dk.thoerup.traininfo;
|
2 |
|
3 |
|
4 |
|
5 |
import java.io.InvalidClassException;
|
6 |
|
7 |
import android.app.Activity;
|
8 |
import android.app.ProgressDialog;
|
9 |
import android.content.Intent;
|
10 |
import android.content.SharedPreferences;
|
11 |
import android.content.SharedPreferences.Editor;
|
12 |
import android.os.AsyncTask;
|
13 |
import android.os.Bundle;
|
14 |
import android.os.Handler;
|
15 |
import android.util.Log;
|
16 |
import android.view.Menu;
|
17 |
import android.view.MenuItem;
|
18 |
import android.view.View;
|
19 |
import android.view.View.OnClickListener;
|
20 |
import android.view.Window;
|
21 |
import android.widget.Button;
|
22 |
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;
|
27 |
|
28 |
import dk.thoerup.androidutils.CheckUpdates;
|
29 |
import dk.thoerup.traininfo.provider.OfflineStationProvider;
|
30 |
import dk.thoerup.traininfo.provider.ProviderFactory;
|
31 |
import dk.thoerup.traininfo.provider.StationProvider;
|
32 |
|
33 |
|
34 |
public class WelcomeScreen extends Activity{
|
35 |
|
36 |
final static String stationsreload = "stationsreload";
|
37 |
final static int MENU_SETTINGS = 1;
|
38 |
final static int MENU_RELOAD = 2;
|
39 |
|
40 |
|
41 |
public enum ListType {
|
42 |
ListNearest,
|
43 |
ListSearch,
|
44 |
ListFavorites
|
45 |
}
|
46 |
|
47 |
IabHelper mHelper;
|
48 |
|
49 |
Handler handler = new Handler();
|
50 |
|
51 |
SharedPreferences prefs;
|
52 |
|
53 |
StationLoader stationLoader;
|
54 |
|
55 |
@Override
|
56 |
public void onCreate(Bundle savedInstanceState) {
|
57 |
|
58 |
super.onCreate(savedInstanceState);
|
59 |
|
60 |
prefs = getSharedPreferences("TrainStation", 0);
|
61 |
|
62 |
requestWindowFeature( Window.FEATURE_NO_TITLE );
|
63 |
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 |
|
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 |
|
84 |
CheckUpdates update = new CheckUpdates();
|
85 |
update.checkForUpdates(this, "http://t-hoerup.dk/android/traininfo/version.txt", "TrainInfo DK", null);
|
86 |
/*
|
87 |
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 |
*/
|
96 |
|
97 |
StationProvider sp = ProviderFactory.getStationProvider();
|
98 |
|
99 |
if (sp instanceof OfflineStationProvider ) {
|
100 |
OfflineStationProvider osp = (OfflineStationProvider) sp;
|
101 |
long last = prefs.getLong(stationsreload, 0);
|
102 |
long now = System.currentTimeMillis();
|
103 |
Log.i("TrainInfo", "Last Load: " + last);
|
104 |
|
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 |
stationLoader.execute( (Void)null);
|
121 |
} 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 |
Log.i("TrainInfo", "Stationlist too old, do a silent download");
|
124 |
stationLoader = new StationLoader(osp, true);
|
125 |
stationLoader.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
|
166 |
protected void onDestroy() {
|
167 |
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
|
183 |
}
|
184 |
|
185 |
|
186 |
@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 |
new StationLoader(osp, false).execute( (Void)null);
|
212 |
break;
|
213 |
|
214 |
default:
|
215 |
retval = super.onOptionsItemSelected(item);
|
216 |
}
|
217 |
|
218 |
return retval;
|
219 |
}
|
220 |
|
221 |
|
222 |
class AboutListener implements OnClickListener {
|
223 |
|
224 |
@Override
|
225 |
public void onClick(View v) {
|
226 |
/*
|
227 |
String appName = WelcomeScreen.this.getResources().getString(R.string.app_name);
|
228 |
String ver = WelcomeScreen.this.getResources().getString(R.string.app_version);
|
229 |
|
230 |
StringBuffer message = new StringBuffer();
|
231 |
message.append(appName);
|
232 |
message.append(" v").append(ver).append("\n");
|
233 |
message.append("By Torben H. Nielsen\n");
|
234 |
|
235 |
MessageBox.showMessage(WelcomeScreen.this, message.toString());*/
|
236 |
/*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 |
}
|
242 |
|
243 |
}
|
244 |
|
245 |
|
246 |
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 |
|
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);
|
268 |
intent.putExtra("type", launchType);
|
269 |
WelcomeScreen.this.startActivity(intent);
|
270 |
}
|
271 |
|
272 |
}
|
273 |
|
274 |
class StationLoader extends AsyncTask<Void,Void,Void> {
|
275 |
|
276 |
|
277 |
boolean succeeded;
|
278 |
ProgressDialog dlg;
|
279 |
OfflineStationProvider osp;
|
280 |
String exMsg;
|
281 |
boolean silent;
|
282 |
|
283 |
public StationLoader(OfflineStationProvider osp, boolean silent) {
|
284 |
this.osp = osp;
|
285 |
this.silent = silent;
|
286 |
}
|
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 |
exMsg = e.getMessage();
|
296 |
Log.e("TrainInfo", "download error", e);
|
297 |
}
|
298 |
return null;
|
299 |
}
|
300 |
|
301 |
|
302 |
@Override
|
303 |
protected void onPreExecute() {
|
304 |
super.onPreExecute();
|
305 |
Log.i("TrainInfo", "StationLoader.onPreExecute() ");
|
306 |
|
307 |
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 |
}
|
314 |
|
315 |
@Override
|
316 |
protected void onPostExecute(Void result) {
|
317 |
super.onPostExecute(result);
|
318 |
Log.i("TrainInfo", "StationLoader.onPostExecute() ");
|
319 |
|
320 |
if (silent == false) {
|
321 |
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) {
|
330 |
Editor edit = prefs.edit();
|
331 |
edit.putLong(stationsreload, System.currentTimeMillis() );
|
332 |
edit.commit();
|
333 |
} else {
|
334 |
if (silent == false) {
|
335 |
Toast.makeText(WelcomeScreen.this, "Error " + exMsg, Toast.LENGTH_LONG).show();
|
336 |
}
|
337 |
}
|
338 |
}
|
339 |
}
|
340 |
|
341 |
}
|