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

Contents of /android/TrainInfo/src/dk/thoerup/traininfo/WelcomeScreen.java

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20