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 |
torben |
500 |
import android.net.Uri;
|
13 |
torben |
1547 |
import android.os.AsyncTask;
|
14 |
torben |
481 |
import android.os.Bundle;
|
15 |
torben |
743 |
import android.os.Handler;
|
16 |
torben |
1159 |
import android.util.Log;
|
17 |
torben |
1548 |
import android.view.Menu;
|
18 |
|
|
import android.view.MenuItem;
|
19 |
torben |
481 |
import android.view.View;
|
20 |
torben |
1547 |
import android.view.View.OnClickListener;
|
21 |
torben |
482 |
import android.view.Window;
|
22 |
torben |
481 |
import android.widget.Button;
|
23 |
torben |
1547 |
import android.widget.Toast;
|
24 |
torben |
481 |
|
25 |
torben |
720 |
import com.nullwire.trace.ExceptionHandler;
|
26 |
|
|
|
27 |
torben |
1131 |
import dk.thoerup.androidutils.CheckUpdates;
|
28 |
torben |
1547 |
import dk.thoerup.traininfo.provider.OfflineStationProvider;
|
29 |
torben |
1159 |
import dk.thoerup.traininfo.provider.ProviderFactory;
|
30 |
torben |
1547 |
import dk.thoerup.traininfo.provider.StationProvider;
|
31 |
torben |
720 |
|
32 |
torben |
1551 |
|
33 |
torben |
481 |
public class WelcomeScreen extends Activity{
|
34 |
torben |
1547 |
|
35 |
|
|
final static String stationsreload = "stationsreload";
|
36 |
torben |
1548 |
final static int MENU_SETTINGS = 1;
|
37 |
|
|
final static int MENU_RELOAD = 2;
|
38 |
torben |
1547 |
|
39 |
torben |
1548 |
|
40 |
torben |
481 |
public enum ListType {
|
41 |
|
|
ListNearest,
|
42 |
|
|
ListSearch,
|
43 |
|
|
ListFavorites
|
44 |
|
|
}
|
45 |
|
|
|
46 |
torben |
743 |
Handler handler = new Handler();
|
47 |
|
|
|
48 |
torben |
1547 |
SharedPreferences prefs;
|
49 |
|
|
|
50 |
torben |
1567 |
StationLoader stationLoader;
|
51 |
|
|
|
52 |
torben |
481 |
@Override
|
53 |
|
|
public void onCreate(Bundle savedInstanceState) {
|
54 |
torben |
916 |
|
55 |
|
|
super.onCreate(savedInstanceState);
|
56 |
torben |
1547 |
|
57 |
|
|
prefs = getSharedPreferences("TrainStation", 0);
|
58 |
|
|
|
59 |
torben |
482 |
requestWindowFeature( Window.FEATURE_NO_TITLE );
|
60 |
torben |
481 |
setContentView(R.layout.welcome);
|
61 |
|
|
|
62 |
|
|
Button nearestButton = (Button) findViewById(R.id.nearest);
|
63 |
|
|
nearestButton.setOnClickListener( new StationListListener(ListType.ListNearest));
|
64 |
|
|
|
65 |
|
|
Button searchButton = (Button) findViewById(R.id.search);
|
66 |
|
|
searchButton.setOnClickListener( new StationListListener(ListType.ListSearch));
|
67 |
|
|
|
68 |
|
|
Button favoritesButton = (Button) findViewById(R.id.favorites);
|
69 |
|
|
favoritesButton.setOnClickListener( new StationListListener(ListType.ListFavorites));
|
70 |
|
|
|
71 |
|
|
Button aboutButton = (Button) findViewById(R.id.about);
|
72 |
|
|
aboutButton.setOnClickListener( new AboutListener() );
|
73 |
torben |
1714 |
|
74 |
|
|
//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 |
torben |
568 |
|
81 |
torben |
720 |
CheckUpdates update = new CheckUpdates();
|
82 |
torben |
911 |
update.checkForUpdates(this, "http://t-hoerup.dk/android/traininfo/version.txt", "TrainInfo DK", null);
|
83 |
torben |
846 |
/*
|
84 |
torben |
743 |
Runnable r = new Runnable() {
|
85 |
|
|
@Override
|
86 |
|
|
public void run() {
|
87 |
|
|
View splash = findViewById(R.id.splash);
|
88 |
|
|
splash.setVisibility(View.GONE);
|
89 |
|
|
}
|
90 |
|
|
};
|
91 |
|
|
handler.postDelayed(r, 1500);
|
92 |
torben |
846 |
*/
|
93 |
torben |
1547 |
|
94 |
|
|
StationProvider sp = ProviderFactory.getStationProvider();
|
95 |
|
|
|
96 |
|
|
if (sp instanceof OfflineStationProvider ) {
|
97 |
|
|
OfflineStationProvider osp = (OfflineStationProvider) sp;
|
98 |
torben |
1555 |
long last = prefs.getLong(stationsreload, 0);
|
99 |
|
|
long now = System.currentTimeMillis();
|
100 |
|
|
Log.i("TrainInfo", "Last Load: " + last);
|
101 |
torben |
1626 |
|
102 |
|
|
boolean didLoad = false;
|
103 |
|
|
|
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 |
torben |
1567 |
stationLoader.execute( (Void)null);
|
118 |
torben |
1555 |
} else {
|
119 |
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
|
120 |
|
|
Log.i("TrainInfo", "Stationlist too old, do a silent download");
|
121 |
|
|
stationLoader = new StationLoader(osp, true);
|
122 |
torben |
1567 |
stationLoader.execute( (Void)null);
|
123 |
torben |
1547 |
}
|
124 |
|
|
}
|
125 |
torben |
1555 |
|
126 |
torben |
1547 |
}
|
127 |
torben |
481 |
}
|
128 |
torben |
1626 |
|
129 |
|
|
|
130 |
torben |
743 |
|
131 |
torben |
1159 |
@Override
|
132 |
|
|
protected void onDestroy() {
|
133 |
|
|
super.onDestroy();
|
134 |
torben |
1567 |
if (stationLoader != null) {
|
135 |
torben |
1594 |
stationLoader.cancel(true);
|
136 |
torben |
1567 |
}
|
137 |
|
|
|
138 |
torben |
1159 |
ProviderFactory.purgeOldEntries(); //exiting application, do some cleanup
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
|
142 |
torben |
1548 |
@Override
|
143 |
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
144 |
|
|
MenuItem item;
|
145 |
|
|
|
146 |
|
|
item = menu.add(0, MENU_SETTINGS, 0, getString(R.string.welcome_settings) );
|
147 |
|
|
item.setIcon(android.R.drawable.ic_menu_preferences);
|
148 |
|
|
|
149 |
|
|
item = menu.add(0, MENU_RELOAD, 0, getString(R.string.welcome_reloadstations));
|
150 |
|
|
item.setIcon(android.R.drawable.ic_menu_rotate);
|
151 |
|
|
|
152 |
|
|
return true;
|
153 |
|
|
}
|
154 |
|
|
|
155 |
|
|
@Override
|
156 |
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
157 |
|
|
boolean retval = true;
|
158 |
|
|
|
159 |
|
|
switch (item.getItemId()) {
|
160 |
|
|
case MENU_SETTINGS:
|
161 |
|
|
Intent intent = new Intent(WelcomeScreen.this, SettingsScreen.class);
|
162 |
|
|
WelcomeScreen.this.startActivity(intent);
|
163 |
|
|
break;
|
164 |
|
|
|
165 |
|
|
case MENU_RELOAD:
|
166 |
|
|
OfflineStationProvider osp = (OfflineStationProvider) ProviderFactory.getStationProvider();
|
167 |
torben |
1626 |
new StationLoader(osp, false).execute( (Void)null);
|
168 |
torben |
1548 |
break;
|
169 |
|
|
|
170 |
|
|
default:
|
171 |
|
|
retval = super.onOptionsItemSelected(item);
|
172 |
|
|
}
|
173 |
|
|
|
174 |
|
|
return retval;
|
175 |
|
|
}
|
176 |
|
|
|
177 |
torben |
1159 |
|
178 |
torben |
481 |
class AboutListener implements OnClickListener {
|
179 |
|
|
|
180 |
|
|
@Override
|
181 |
|
|
public void onClick(View v) {
|
182 |
torben |
500 |
/*
|
183 |
torben |
484 |
String appName = WelcomeScreen.this.getResources().getString(R.string.app_name);
|
184 |
|
|
String ver = WelcomeScreen.this.getResources().getString(R.string.app_version);
|
185 |
|
|
|
186 |
torben |
483 |
StringBuffer message = new StringBuffer();
|
187 |
torben |
484 |
message.append(appName);
|
188 |
|
|
message.append(" v").append(ver).append("\n");
|
189 |
torben |
483 |
message.append("By Torben H. Nielsen\n");
|
190 |
|
|
|
191 |
torben |
500 |
MessageBox.showMessage(WelcomeScreen.this, message.toString());*/
|
192 |
torben |
1638 |
/*Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://t-hoerup.dk/android/traininfo/"));
|
193 |
|
|
startActivity(browserIntent);*/
|
194 |
|
|
|
195 |
|
|
Intent intent = new Intent(WelcomeScreen.this, AboutScreen.class);
|
196 |
|
|
WelcomeScreen.this.startActivity(intent);
|
197 |
torben |
481 |
}
|
198 |
torben |
483 |
|
199 |
torben |
481 |
}
|
200 |
torben |
1446 |
|
201 |
torben |
483 |
|
202 |
torben |
481 |
class StationListListener implements OnClickListener{
|
203 |
|
|
ListType launchType;
|
204 |
|
|
StationListListener(ListType type) {
|
205 |
|
|
launchType = type;
|
206 |
|
|
}
|
207 |
|
|
|
208 |
|
|
@Override
|
209 |
|
|
public void onClick(View v) {
|
210 |
torben |
1572 |
|
211 |
|
|
StationProvider sp = ProviderFactory.getStationProvider();
|
212 |
|
|
|
213 |
|
|
if (sp instanceof OfflineStationProvider ) {
|
214 |
|
|
OfflineStationProvider osp = (OfflineStationProvider) sp;
|
215 |
|
|
|
216 |
|
|
if (! osp.hasStations()) {
|
217 |
torben |
1626 |
stationLoader = new StationLoader(osp, false);
|
218 |
torben |
1572 |
stationLoader.execute( (Void)null);
|
219 |
|
|
return;
|
220 |
|
|
}
|
221 |
|
|
}
|
222 |
|
|
|
223 |
torben |
481 |
Intent intent = new Intent(WelcomeScreen.this, StationList.class);
|
224 |
|
|
intent.putExtra("type", launchType);
|
225 |
|
|
WelcomeScreen.this.startActivity(intent);
|
226 |
|
|
}
|
227 |
|
|
|
228 |
|
|
}
|
229 |
torben |
1547 |
|
230 |
|
|
class StationLoader extends AsyncTask<Void,Void,Void> {
|
231 |
|
|
|
232 |
|
|
|
233 |
|
|
boolean succeeded;
|
234 |
|
|
ProgressDialog dlg;
|
235 |
|
|
OfflineStationProvider osp;
|
236 |
torben |
1550 |
String exMsg;
|
237 |
torben |
1626 |
boolean silent;
|
238 |
torben |
1547 |
|
239 |
torben |
1626 |
public StationLoader(OfflineStationProvider osp, boolean silent) {
|
240 |
torben |
1547 |
this.osp = osp;
|
241 |
torben |
1626 |
this.silent = silent;
|
242 |
torben |
1547 |
}
|
243 |
|
|
|
244 |
|
|
@Override
|
245 |
|
|
protected Void doInBackground(Void... params) {
|
246 |
|
|
try {
|
247 |
|
|
osp.downloadStations( WelcomeScreen.this );
|
248 |
|
|
succeeded = true;
|
249 |
|
|
} catch (Exception e) {
|
250 |
|
|
succeeded = false;
|
251 |
torben |
1626 |
exMsg = e.getMessage();
|
252 |
|
|
Log.e("TrainInfo", "download error", e);
|
253 |
torben |
1547 |
}
|
254 |
|
|
return null;
|
255 |
|
|
}
|
256 |
|
|
|
257 |
torben |
1565 |
|
258 |
torben |
1547 |
@Override
|
259 |
|
|
protected void onPreExecute() {
|
260 |
|
|
super.onPreExecute();
|
261 |
torben |
1626 |
Log.i("TrainInfo", "StationLoader.onPreExecute() ");
|
262 |
torben |
1547 |
|
263 |
torben |
1626 |
if (silent == false) {
|
264 |
|
|
dlg = new ProgressDialog(WelcomeScreen.this);
|
265 |
|
|
dlg.setMessage( getText(R.string.welcome_downloadingstations) );
|
266 |
|
|
dlg.setCancelable(false);
|
267 |
|
|
dlg.show();
|
268 |
|
|
}
|
269 |
torben |
1547 |
}
|
270 |
|
|
|
271 |
|
|
@Override
|
272 |
|
|
protected void onPostExecute(Void result) {
|
273 |
|
|
super.onPostExecute(result);
|
274 |
torben |
1626 |
Log.i("TrainInfo", "StationLoader.onPostExecute() ");
|
275 |
torben |
1547 |
|
276 |
torben |
1626 |
if (silent == false) {
|
277 |
|
|
dlg.dismiss();
|
278 |
|
|
dlg = null;
|
279 |
|
|
}
|
280 |
torben |
1547 |
|
281 |
|
|
if (succeeded) {
|
282 |
|
|
Editor edit = prefs.edit();
|
283 |
|
|
edit.putLong(stationsreload, System.currentTimeMillis() );
|
284 |
|
|
edit.commit();
|
285 |
torben |
1550 |
} else {
|
286 |
torben |
1626 |
if (silent == false) {
|
287 |
|
|
Toast.makeText(WelcomeScreen.this, "Error " + exMsg, Toast.LENGTH_LONG).show();
|
288 |
|
|
}
|
289 |
torben |
1547 |
}
|
290 |
|
|
}
|
291 |
|
|
}
|
292 |
|
|
|
293 |
torben |
481 |
}
|