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 |
@Override |
@Override |
51 |
public void onCreate(Bundle savedInstanceState) { |
public void onCreate(Bundle savedInstanceState) { |
52 |
requestWindowFeature( Window.FEATURE_NO_TITLE ); |
|
53 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
54 |
|
|
55 |
|
prefs = getSharedPreferences("TrainStation", 0); |
56 |
|
|
57 |
|
requestWindowFeature( Window.FEATURE_NO_TITLE ); |
58 |
setContentView(R.layout.welcome); |
setContentView(R.layout.welcome); |
59 |
|
|
60 |
Button nearestButton = (Button) findViewById(R.id.nearest); |
Button nearestButton = (Button) findViewById(R.id.nearest); |
72 |
ExceptionHandler.register(this, "http://t-hoerup.dk/android/trace.php"); |
ExceptionHandler.register(this, "http://t-hoerup.dk/android/trace.php"); |
73 |
|
|
74 |
CheckUpdates update = new CheckUpdates(); |
CheckUpdates update = new CheckUpdates(); |
75 |
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); |
76 |
/* |
/* |
77 |
Runnable r = new Runnable() { |
Runnable r = new Runnable() { |
78 |
@Override |
@Override |
83 |
}; |
}; |
84 |
handler.postDelayed(r, 1500); |
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 { |
class AboutListener implements OnClickListener { |
166 |
|
|
167 |
@Override |
@Override |
176 |
message.append("By Torben H. Nielsen\n"); |
message.append("By Torben H. Nielsen\n"); |
177 |
|
|
178 |
MessageBox.showMessage(WelcomeScreen.this, message.toString());*/ |
MessageBox.showMessage(WelcomeScreen.this, message.toString());*/ |
179 |
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/")); |
180 |
startActivity(browserIntent); |
startActivity(browserIntent); |
181 |
} |
} |
182 |
|
|
183 |
} |
} |
184 |
|
|
185 |
|
|
186 |
class StationListListener implements OnClickListener{ |
class StationListListener implements OnClickListener{ |
187 |
ListType launchType; |
ListType launchType; |
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 |
|
|
226 |
|
@Override |
227 |
|
protected void onPreExecute() { |
228 |
|
super.onPreExecute(); |
229 |
|
|
230 |
|
dlg = new ProgressDialog(WelcomeScreen.this); |
231 |
|
dlg.setMessage( getText(R.string.welcome_downloadingstations) ); |
232 |
|
dlg.setCancelable(true); |
233 |
|
dlg.show(); |
234 |
|
} |
235 |
|
|
236 |
|
@Override |
237 |
|
protected void onPostExecute(Void result) { |
238 |
|
super.onPostExecute(result); |
239 |
|
|
240 |
|
dlg.dismiss(); |
241 |
|
dlg = null; |
242 |
|
|
243 |
|
if (succeeded) { |
244 |
|
Editor edit = prefs.edit(); |
245 |
|
edit.putLong(stationsreload, System.currentTimeMillis() ); |
246 |
|
edit.commit(); |
247 |
|
} else { |
248 |
|
Toast.makeText(WelcomeScreen.this, "Error " + exMsg, Toast.LENGTH_LONG).show(); |
249 |
|
} |
250 |
|
} |
251 |
|
} |
252 |
|
|
253 |
} |
} |