1 |
package dk.thoerup.traininfo; |
package dk.thoerup.traininfo; |
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
import java.io.InvalidClassException; |
6 |
|
|
7 |
import android.app.Activity; |
import android.app.Activity; |
8 |
|
import android.app.ProgressDialog; |
9 |
import android.content.Intent; |
import android.content.Intent; |
10 |
import android.net.Uri; |
import android.content.SharedPreferences; |
11 |
|
import android.content.SharedPreferences.Editor; |
12 |
|
import android.os.AsyncTask; |
13 |
import android.os.Bundle; |
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; |
import android.view.View; |
|
import android.view.Window; |
|
19 |
import android.view.View.OnClickListener; |
import android.view.View.OnClickListener; |
20 |
|
import android.view.Window; |
21 |
import android.widget.Button; |
import android.widget.Button; |
22 |
|
import android.widget.Toast; |
23 |
|
|
24 |
import com.nullwire.trace.ExceptionHandler; |
//import com.nullwire.trace.ExceptionHandler; |
25 |
|
|
26 |
|
import dk.thoerup.androidutils.CheckUpdates; |
27 |
|
import dk.thoerup.traininfo.provider.OfflineStationProvider; |
28 |
|
import dk.thoerup.traininfo.provider.ProviderFactory; |
29 |
|
import dk.thoerup.traininfo.provider.StationProvider; |
30 |
|
|
|
import dk.thoerup.checkupdates.CheckUpdates; |
|
31 |
|
|
32 |
public class WelcomeScreen extends Activity{ |
public class WelcomeScreen extends Activity{ |
33 |
|
|
34 |
|
final static String stationsreload = "stationsreload"; |
35 |
|
final static int MENU_SETTINGS = 1; |
36 |
|
final static int MENU_RELOAD = 2; |
37 |
|
|
38 |
|
|
39 |
public enum ListType { |
public enum ListType { |
40 |
ListNearest, |
ListNearest, |
41 |
ListSearch, |
ListSearch, |
42 |
ListFavorites |
ListFavorites |
43 |
} |
} |
44 |
|
|
45 |
|
|
46 |
|
Handler handler = new Handler(); |
47 |
|
|
48 |
|
SharedPreferences prefs; |
49 |
|
|
50 |
|
StationLoader stationLoader; |
51 |
|
|
52 |
@Override |
@Override |
53 |
public void onCreate(Bundle savedInstanceState) { |
public void onCreate(Bundle savedInstanceState) { |
54 |
requestWindowFeature( Window.FEATURE_NO_TITLE ); |
|
55 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
56 |
|
|
57 |
|
prefs = getSharedPreferences("TrainStation", 0); |
58 |
|
|
59 |
|
requestWindowFeature( Window.FEATURE_NO_TITLE ); |
60 |
setContentView(R.layout.welcome); |
setContentView(R.layout.welcome); |
61 |
|
|
62 |
Button nearestButton = (Button) findViewById(R.id.nearest); |
Button nearestButton = (Button) findViewById(R.id.nearest); |
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"); |
update.checkForUpdates(this, "http://t-hoerup.dk/android/traininfo/version.txt", "TrainInfo DK", null); |
83 |
|
/* |
84 |
|
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 |
|
*/ |
93 |
|
|
94 |
|
StationProvider sp = ProviderFactory.getStationProvider(); |
95 |
|
|
96 |
|
if (sp instanceof OfflineStationProvider ) { |
97 |
|
OfflineStationProvider osp = (OfflineStationProvider) sp; |
98 |
|
long last = prefs.getLong(stationsreload, 0); |
99 |
|
long now = System.currentTimeMillis(); |
100 |
|
Log.i("TrainInfo", "Last Load: " + last); |
101 |
|
|
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 |
|
stationLoader.execute( (Void)null); |
118 |
|
} 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 |
|
Log.i("TrainInfo", "Stationlist too old, do a silent download"); |
121 |
|
stationLoader = new StationLoader(osp, true); |
122 |
|
stationLoader.execute( (Void)null); |
123 |
|
} |
124 |
|
} |
125 |
|
|
126 |
|
} |
127 |
|
|
128 |
|
|
129 |
|
} |
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
@Override |
134 |
|
protected void onDestroy() { |
135 |
|
super.onDestroy(); |
136 |
|
if (stationLoader != null) { |
137 |
|
stationLoader.cancel(true); |
138 |
|
stationLoader = null; |
139 |
|
} |
140 |
|
|
141 |
|
ProviderFactory.purgeOldEntries(); //exiting application, do some cleanup |
142 |
|
} |
143 |
|
|
144 |
|
|
145 |
|
@Override |
146 |
|
public boolean onCreateOptionsMenu(Menu menu) { |
147 |
|
MenuItem item; |
148 |
|
|
149 |
|
item = menu.add(0, MENU_SETTINGS, 0, getString(R.string.welcome_settings) ); |
150 |
|
item.setIcon(android.R.drawable.ic_menu_preferences); |
151 |
|
|
152 |
|
item = menu.add(0, MENU_RELOAD, 0, getString(R.string.welcome_reloadstations)); |
153 |
|
item.setIcon(android.R.drawable.ic_menu_rotate); |
154 |
|
|
155 |
|
return true; |
156 |
|
} |
157 |
|
|
158 |
|
@Override |
159 |
|
public boolean onOptionsItemSelected(MenuItem item) { |
160 |
|
boolean retval = true; |
161 |
|
|
162 |
|
switch (item.getItemId()) { |
163 |
|
case MENU_SETTINGS: |
164 |
|
Intent intent = new Intent(WelcomeScreen.this, SettingsScreen.class); |
165 |
|
WelcomeScreen.this.startActivity(intent); |
166 |
|
break; |
167 |
|
|
168 |
|
case MENU_RELOAD: |
169 |
|
OfflineStationProvider osp = (OfflineStationProvider) ProviderFactory.getStationProvider(); |
170 |
|
new StationLoader(osp, false).execute( (Void)null); |
171 |
|
break; |
172 |
|
|
173 |
|
default: |
174 |
|
retval = super.onOptionsItemSelected(item); |
175 |
|
} |
176 |
|
|
177 |
|
return retval; |
178 |
} |
} |
179 |
|
|
180 |
|
|
181 |
class AboutListener implements OnClickListener { |
class AboutListener implements OnClickListener { |
182 |
|
|
183 |
@Override |
@Override |
192 |
message.append("By Torben H. Nielsen\n"); |
message.append("By Torben H. Nielsen\n"); |
193 |
|
|
194 |
MessageBox.showMessage(WelcomeScreen.this, message.toString());*/ |
MessageBox.showMessage(WelcomeScreen.this, message.toString());*/ |
195 |
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.t-hoerup.dk/android/traininfo/")); |
/*Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://t-hoerup.dk/android/traininfo/")); |
196 |
startActivity(browserIntent); |
startActivity(browserIntent);*/ |
197 |
|
|
198 |
|
Intent intent = new Intent(WelcomeScreen.this, AboutScreen.class); |
199 |
|
WelcomeScreen.this.startActivity(intent); |
200 |
} |
} |
201 |
|
|
202 |
} |
} |
203 |
|
|
204 |
|
|
205 |
class StationListListener implements OnClickListener{ |
class StationListListener implements OnClickListener{ |
206 |
ListType launchType; |
ListType launchType; |
210 |
|
|
211 |
@Override |
@Override |
212 |
public void onClick(View v) { |
public void onClick(View v) { |
213 |
|
|
214 |
|
StationProvider sp = ProviderFactory.getStationProvider(); |
215 |
|
|
216 |
|
if (sp instanceof OfflineStationProvider ) { |
217 |
|
OfflineStationProvider osp = (OfflineStationProvider) sp; |
218 |
|
|
219 |
|
if (! osp.hasStations()) { |
220 |
|
stationLoader = new StationLoader(osp, false); |
221 |
|
stationLoader.execute( (Void)null); |
222 |
|
return; |
223 |
|
} |
224 |
|
} |
225 |
|
|
226 |
Intent intent = new Intent(WelcomeScreen.this, StationList.class); |
Intent intent = new Intent(WelcomeScreen.this, StationList.class); |
227 |
intent.putExtra("type", launchType); |
intent.putExtra("type", launchType); |
228 |
WelcomeScreen.this.startActivity(intent); |
WelcomeScreen.this.startActivity(intent); |
229 |
} |
} |
230 |
|
|
231 |
} |
} |
232 |
|
|
233 |
|
class StationLoader extends AsyncTask<Void,Void,Void> { |
234 |
|
|
235 |
|
|
236 |
|
boolean succeeded; |
237 |
|
ProgressDialog dlg; |
238 |
|
OfflineStationProvider osp; |
239 |
|
String exMsg; |
240 |
|
boolean silent; |
241 |
|
|
242 |
|
public StationLoader(OfflineStationProvider osp, boolean silent) { |
243 |
|
this.osp = osp; |
244 |
|
this.silent = silent; |
245 |
|
} |
246 |
|
|
247 |
|
@Override |
248 |
|
protected Void doInBackground(Void... params) { |
249 |
|
try { |
250 |
|
osp.downloadStations( WelcomeScreen.this ); |
251 |
|
succeeded = true; |
252 |
|
} catch (Exception e) { |
253 |
|
succeeded = false; |
254 |
|
exMsg = e.getMessage(); |
255 |
|
Log.e("TrainInfo", "download error", e); |
256 |
|
} |
257 |
|
return null; |
258 |
|
} |
259 |
|
|
260 |
|
|
261 |
|
@Override |
262 |
|
protected void onPreExecute() { |
263 |
|
super.onPreExecute(); |
264 |
|
Log.i("TrainInfo", "StationLoader.onPreExecute() "); |
265 |
|
|
266 |
|
if (silent == false) { |
267 |
|
dlg = new ProgressDialog(WelcomeScreen.this); |
268 |
|
dlg.setMessage( getText(R.string.welcome_downloadingstations) ); |
269 |
|
dlg.setCancelable(false); |
270 |
|
dlg.show(); |
271 |
|
} |
272 |
|
} |
273 |
|
|
274 |
|
@Override |
275 |
|
protected void onPostExecute(Void result) { |
276 |
|
super.onPostExecute(result); |
277 |
|
Log.i("TrainInfo", "StationLoader.onPostExecute() "); |
278 |
|
|
279 |
|
if (silent == false) { |
280 |
|
try { |
281 |
|
dlg.dismiss(); |
282 |
|
} catch (Exception e) { |
283 |
|
Log.e("TrainInfo", "Exception while closing dialog", e); // don't crash program |
284 |
|
} |
285 |
|
dlg = null; |
286 |
|
} |
287 |
|
|
288 |
|
if (succeeded) { |
289 |
|
Editor edit = prefs.edit(); |
290 |
|
edit.putLong(stationsreload, System.currentTimeMillis() ); |
291 |
|
edit.commit(); |
292 |
|
} else { |
293 |
|
if (silent == false) { |
294 |
|
Toast.makeText(WelcomeScreen.this, "Error " + exMsg, Toast.LENGTH_LONG).show(); |
295 |
|
} |
296 |
|
} |
297 |
|
} |
298 |
|
} |
299 |
|
|
300 |
} |
} |