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.content.SharedPreferences; |
11 |
|
import android.content.SharedPreferences.Editor; |
12 |
import android.net.Uri; |
import android.net.Uri; |
13 |
|
import android.os.AsyncTask; |
14 |
import android.os.Bundle; |
import android.os.Bundle; |
15 |
import android.os.Handler; |
import android.os.Handler; |
16 |
import android.os.Message; |
import android.util.Log; |
17 |
|
import android.view.Menu; |
18 |
|
import android.view.MenuItem; |
19 |
import android.view.View; |
import android.view.View; |
|
import android.view.Window; |
|
20 |
import android.view.View.OnClickListener; |
import android.view.View.OnClickListener; |
21 |
|
import android.view.Window; |
22 |
import android.widget.Button; |
import android.widget.Button; |
23 |
|
import android.widget.Toast; |
24 |
|
|
25 |
import com.nullwire.trace.ExceptionHandler; |
import com.nullwire.trace.ExceptionHandler; |
26 |
|
|
27 |
import dk.thoerup.checkupdates.CheckUpdates; |
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{ |
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 { |
public enum ListType { |
41 |
ListNearest, |
ListNearest, |
42 |
ListSearch, |
ListSearch, |
45 |
|
|
46 |
Handler handler = new Handler(); |
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); |
74 |
ExceptionHandler.register(this, "http://t-hoerup.dk/android/trace.php"); |
ExceptionHandler.register(this, "http://t-hoerup.dk/android/trace.php"); |
75 |
|
|
76 |
CheckUpdates update = new CheckUpdates(); |
CheckUpdates update = new CheckUpdates(); |
77 |
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); |
78 |
/* |
/* |
79 |
Runnable r = new Runnable() { |
Runnable r = new Runnable() { |
80 |
@Override |
@Override |
85 |
}; |
}; |
86 |
handler.postDelayed(r, 1500); |
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 |
|
if ( (now-last) > (14*24*60*60*1000) ) { |
98 |
|
stationLoader = new StationLoader(osp); |
99 |
|
stationLoader.execute( (Void)null); |
100 |
|
} else { |
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); |
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(false); |
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).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 { |
class AboutListener implements OnClickListener { |
174 |
|
|
175 |
@Override |
@Override |
184 |
message.append("By Torben H. Nielsen\n"); |
message.append("By Torben H. Nielsen\n"); |
185 |
|
|
186 |
MessageBox.showMessage(WelcomeScreen.this, message.toString());*/ |
MessageBox.showMessage(WelcomeScreen.this, message.toString());*/ |
187 |
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/")); |
188 |
startActivity(browserIntent); |
startActivity(browserIntent); |
189 |
} |
} |
190 |
|
|
191 |
} |
} |
192 |
|
|
193 |
|
|
194 |
class StationListListener implements OnClickListener{ |
class StationListListener implements OnClickListener{ |
195 |
ListType launchType; |
ListType launchType; |
199 |
|
|
200 |
@Override |
@Override |
201 |
public void onClick(View v) { |
public void onClick(View v) { |
202 |
|
|
203 |
|
StationProvider sp = ProviderFactory.getStationProvider(); |
204 |
|
|
205 |
|
if (sp instanceof OfflineStationProvider ) { |
206 |
|
OfflineStationProvider osp = (OfflineStationProvider) sp; |
207 |
|
|
208 |
|
if (! osp.hasStations()) { |
209 |
|
stationLoader = new StationLoader(osp); |
210 |
|
stationLoader.execute( (Void)null); |
211 |
|
return; |
212 |
|
} |
213 |
|
} |
214 |
|
|
215 |
Intent intent = new Intent(WelcomeScreen.this, StationList.class); |
Intent intent = new Intent(WelcomeScreen.this, StationList.class); |
216 |
intent.putExtra("type", launchType); |
intent.putExtra("type", launchType); |
217 |
WelcomeScreen.this.startActivity(intent); |
WelcomeScreen.this.startActivity(intent); |
218 |
} |
} |
219 |
|
|
220 |
} |
} |
221 |
|
|
222 |
|
class StationLoader extends AsyncTask<Void,Void,Void> { |
223 |
|
|
224 |
|
|
225 |
|
boolean succeeded; |
226 |
|
ProgressDialog dlg; |
227 |
|
OfflineStationProvider osp; |
228 |
|
String exMsg; |
229 |
|
|
230 |
|
public StationLoader(OfflineStationProvider osp) { |
231 |
|
this.osp = osp; |
232 |
|
} |
233 |
|
|
234 |
|
@Override |
235 |
|
protected Void doInBackground(Void... params) { |
236 |
|
try { |
237 |
|
osp.downloadStations( WelcomeScreen.this ); |
238 |
|
succeeded = true; |
239 |
|
} catch (Exception e) { |
240 |
|
succeeded = false; |
241 |
|
exMsg = e.getMessage(); |
242 |
|
} |
243 |
|
return null; |
244 |
|
} |
245 |
|
|
246 |
|
|
247 |
|
@Override |
248 |
|
protected void onPreExecute() { |
249 |
|
super.onPreExecute(); |
250 |
|
|
251 |
|
dlg = new ProgressDialog(WelcomeScreen.this); |
252 |
|
dlg.setMessage( getText(R.string.welcome_downloadingstations) ); |
253 |
|
dlg.setCancelable(false); |
254 |
|
dlg.show(); |
255 |
|
} |
256 |
|
|
257 |
|
@Override |
258 |
|
protected void onPostExecute(Void result) { |
259 |
|
super.onPostExecute(result); |
260 |
|
|
261 |
|
dlg.dismiss(); |
262 |
|
dlg = null; |
263 |
|
|
264 |
|
if (succeeded) { |
265 |
|
Editor edit = prefs.edit(); |
266 |
|
edit.putLong(stationsreload, System.currentTimeMillis() ); |
267 |
|
edit.commit(); |
268 |
|
} else { |
269 |
|
Toast.makeText(WelcomeScreen.this, "Error " + exMsg, Toast.LENGTH_LONG).show(); |
270 |
|
} |
271 |
|
} |
272 |
|
} |
273 |
|
|
274 |
} |
} |