/[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 1555 - (show annotations) (download)
Fri Jul 8 12:24:48 2011 UTC (12 years, 10 months ago) by torben
File size: 6766 byte(s)
catch invalid class exception - if serializable layout changes just download a new list
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 @Override
51 public void onCreate(Bundle savedInstanceState) {
52
53 super.onCreate(savedInstanceState);
54
55 prefs = getSharedPreferences("TrainStation", 0);
56
57 requestWindowFeature( Window.FEATURE_NO_TITLE );
58 setContentView(R.layout.welcome);
59
60 Button nearestButton = (Button) findViewById(R.id.nearest);
61 nearestButton.setOnClickListener( new StationListListener(ListType.ListNearest));
62
63 Button searchButton = (Button) findViewById(R.id.search);
64 searchButton.setOnClickListener( new StationListListener(ListType.ListSearch));
65
66 Button favoritesButton = (Button) findViewById(R.id.favorites);
67 favoritesButton.setOnClickListener( new StationListListener(ListType.ListFavorites));
68
69 Button aboutButton = (Button) findViewById(R.id.about);
70 aboutButton.setOnClickListener( new AboutListener() );
71
72 ExceptionHandler.register(this, "http://t-hoerup.dk/android/trace.php");
73
74 CheckUpdates update = new CheckUpdates();
75 update.checkForUpdates(this, "http://t-hoerup.dk/android/traininfo/version.txt", "TrainInfo DK", null);
76 /*
77 Runnable r = new Runnable() {
78 @Override
79 public void run() {
80 View splash = findViewById(R.id.splash);
81 splash.setVisibility(View.GONE);
82 }
83 };
84 handler.postDelayed(r, 1500);
85 */
86
87 StationProvider sp = ProviderFactory.getStationProvider();
88
89 if (sp instanceof OfflineStationProvider ) {
90 OfflineStationProvider osp = (OfflineStationProvider) sp;
91 long last = prefs.getLong(stationsreload, 0);
92 long now = System.currentTimeMillis();
93 Log.i("TrainInfo", "Last Load: " + last);
94
95 if ( (now-last) > (14*24*60*60*1000) ) {
96 new StationLoader(osp).execute( (Void)null);
97 } else {
98
99 boolean didLoad = false;
100
101 try {
102 didLoad = osp.loadStations(this);
103 }
104 catch (InvalidClassException e) {
105 Log.i("TrainInfo", "invalid class - do a new download of stationlist");
106 }
107 catch (Exception e) {
108 Toast.makeText(this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();
109 Log.e("TrainInfo", "load error", e);
110 }
111
112 if (didLoad == false) {
113 new StationLoader(osp).execute( (Void)null);
114 }
115 }
116
117 }
118 }
119
120
121
122 @Override
123 protected void onDestroy() {
124 super.onDestroy();
125 ProviderFactory.purgeOldEntries(); //exiting application, do some cleanup
126 }
127
128
129 @Override
130 public boolean onCreateOptionsMenu(Menu menu) {
131 MenuItem item;
132
133 item = menu.add(0, MENU_SETTINGS, 0, getString(R.string.welcome_settings) );
134 item.setIcon(android.R.drawable.ic_menu_preferences);
135
136 item = menu.add(0, MENU_RELOAD, 0, getString(R.string.welcome_reloadstations));
137 item.setIcon(android.R.drawable.ic_menu_rotate);
138
139 return true;
140 }
141
142 @Override
143 public boolean onOptionsItemSelected(MenuItem item) {
144 boolean retval = true;
145
146 switch (item.getItemId()) {
147 case MENU_SETTINGS:
148 Intent intent = new Intent(WelcomeScreen.this, SettingsScreen.class);
149 WelcomeScreen.this.startActivity(intent);
150 break;
151
152 case MENU_RELOAD:
153 OfflineStationProvider osp = (OfflineStationProvider) ProviderFactory.getStationProvider();
154 new StationLoader(osp).execute( (Void)null);
155 break;
156
157 default:
158 retval = super.onOptionsItemSelected(item);
159 }
160
161 return retval;
162 }
163
164
165 class AboutListener implements OnClickListener {
166
167 @Override
168 public void onClick(View v) {
169 /*
170 String appName = WelcomeScreen.this.getResources().getString(R.string.app_name);
171 String ver = WelcomeScreen.this.getResources().getString(R.string.app_version);
172
173 StringBuffer message = new StringBuffer();
174 message.append(appName);
175 message.append(" v").append(ver).append("\n");
176 message.append("By Torben H. Nielsen\n");
177
178 MessageBox.showMessage(WelcomeScreen.this, message.toString());*/
179 Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://t-hoerup.dk/android/traininfo/"));
180 startActivity(browserIntent);
181 }
182
183 }
184
185
186 class StationListListener implements OnClickListener{
187 ListType launchType;
188 StationListListener(ListType type) {
189 launchType = type;
190 }
191
192 @Override
193 public void onClick(View v) {
194 Intent intent = new Intent(WelcomeScreen.this, StationList.class);
195 intent.putExtra("type", launchType);
196 WelcomeScreen.this.startActivity(intent);
197 }
198
199 }
200
201 class StationLoader extends AsyncTask<Void,Void,Void> {
202
203
204 boolean succeeded;
205 ProgressDialog dlg;
206 OfflineStationProvider osp;
207 String exMsg;
208
209 public StationLoader(OfflineStationProvider osp) {
210 this.osp = osp;
211 }
212
213 @Override
214 protected Void doInBackground(Void... params) {
215 try {
216 osp.downloadStations( WelcomeScreen.this );
217 succeeded = true;
218 } catch (Exception e) {
219 succeeded = false;
220 exMsg = e.getMessage();
221 }
222 return null;
223 }
224
225 @Override
226 protected void onPreExecute() {
227 super.onPreExecute();
228
229 dlg = new ProgressDialog(WelcomeScreen.this);
230 dlg.setMessage( "Downloading stations list" );//TODO: translate
231 dlg.setCancelable(true);
232 dlg.show();
233 }
234
235 @Override
236 protected void onPostExecute(Void result) {
237 super.onPostExecute(result);
238
239 dlg.dismiss();
240 dlg = null;
241
242 if (succeeded) {
243 Editor edit = prefs.edit();
244 edit.putLong(stationsreload, System.currentTimeMillis() );
245 edit.commit();
246 } else {
247 Toast.makeText(WelcomeScreen.this, "Error " + exMsg, Toast.LENGTH_LONG).show();
248 }
249 }
250 }
251
252 }

  ViewVC Help
Powered by ViewVC 1.1.20