1 |
package dk.thoerup.traininfo; |
package dk.thoerup.traininfo; |
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
import java.util.ArrayList; |
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.location.Location; |
13 |
import android.net.Uri; |
import android.net.Uri; |
14 |
|
import android.os.AsyncTask; |
15 |
import android.os.Bundle; |
import android.os.Bundle; |
16 |
import android.os.Handler; |
import android.os.Handler; |
17 |
import android.os.Message; |
import android.util.Log; |
18 |
|
import android.view.Menu; |
19 |
|
import android.view.MenuItem; |
20 |
import android.view.View; |
import android.view.View; |
|
import android.view.Window; |
|
21 |
import android.view.View.OnClickListener; |
import android.view.View.OnClickListener; |
22 |
|
import android.view.Window; |
23 |
import android.widget.Button; |
import android.widget.Button; |
24 |
|
import android.widget.Toast; |
25 |
|
|
26 |
import com.nullwire.trace.ExceptionHandler; |
import com.nullwire.trace.ExceptionHandler; |
27 |
|
|
28 |
import dk.thoerup.checkupdates.CheckUpdates; |
import dk.thoerup.android.traininfo.common.StationEntry; |
29 |
|
import dk.thoerup.androidutils.CheckUpdates; |
30 |
|
import dk.thoerup.traininfo.provider.OfflineStationProvider; |
31 |
|
import dk.thoerup.traininfo.provider.ProviderFactory; |
32 |
|
import dk.thoerup.traininfo.provider.StationProvider; |
33 |
|
import dk.thoerup.traininfo.stationmap.GeoPair; |
34 |
|
import dk.thoerup.traininfo.stationmap.StationMapView; |
35 |
|
import dk.thoerup.traininfo.util.MessageBox; |
36 |
|
|
37 |
public class WelcomeScreen extends Activity{ |
public class WelcomeScreen extends Activity{ |
38 |
|
|
39 |
|
final static String stationsreload = "stationsreload"; |
40 |
|
final static int MENU_SETTINGS = 1; |
41 |
|
final static int MENU_RELOAD = 2; |
42 |
|
|
43 |
|
|
44 |
public enum ListType { |
public enum ListType { |
45 |
ListNearest, |
ListNearest, |
46 |
ListSearch, |
ListSearch, |
49 |
|
|
50 |
Handler handler = new Handler(); |
Handler handler = new Handler(); |
51 |
|
|
52 |
|
SharedPreferences prefs; |
53 |
|
|
54 |
@Override |
@Override |
55 |
public void onCreate(Bundle savedInstanceState) { |
public void onCreate(Bundle savedInstanceState) { |
56 |
requestWindowFeature( Window.FEATURE_NO_TITLE ); |
|
57 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
58 |
|
|
59 |
|
prefs = getSharedPreferences("TrainStation", 0); |
60 |
|
|
61 |
|
requestWindowFeature( Window.FEATURE_NO_TITLE ); |
62 |
setContentView(R.layout.welcome); |
setContentView(R.layout.welcome); |
63 |
|
|
64 |
Button nearestButton = (Button) findViewById(R.id.nearest); |
Button nearestButton = (Button) findViewById(R.id.nearest); |
87 |
}; |
}; |
88 |
handler.postDelayed(r, 1500); |
handler.postDelayed(r, 1500); |
89 |
*/ |
*/ |
90 |
|
|
91 |
|
StationProvider sp = ProviderFactory.getStationProvider(); |
92 |
|
|
93 |
|
if (sp instanceof OfflineStationProvider ) { |
94 |
|
OfflineStationProvider osp = (OfflineStationProvider) sp; |
95 |
|
try { |
96 |
|
|
97 |
|
long last = prefs.getLong(stationsreload, 0); |
98 |
|
long now = System.currentTimeMillis(); |
99 |
|
|
100 |
|
Log.i("TrainInfo", "Last Load: " + last); |
101 |
|
|
102 |
|
if ( (now-last) > (14*24*60*60*1000) ) { |
103 |
|
new StationLoader(osp).execute( (Void)null); |
104 |
|
} else { |
105 |
|
|
106 |
|
boolean didLoad = osp.loadStations(this); |
107 |
|
|
108 |
|
if (didLoad == false) { |
109 |
|
new StationLoader(osp).execute( (Void)null); |
110 |
|
} |
111 |
|
} |
112 |
|
|
113 |
|
} catch (Exception e) { |
114 |
|
Toast.makeText(this, "" + e.getMessage(), Toast.LENGTH_SHORT).show(); |
115 |
|
} |
116 |
|
} |
117 |
} |
} |
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
@Override |
122 |
|
protected void onDestroy() { |
123 |
|
super.onDestroy(); |
124 |
|
ProviderFactory.purgeOldEntries(); //exiting application, do some cleanup |
125 |
|
} |
126 |
|
|
127 |
|
|
128 |
|
@Override |
129 |
|
public boolean onCreateOptionsMenu(Menu menu) { |
130 |
|
MenuItem item; |
131 |
|
|
132 |
|
item = menu.add(0, MENU_SETTINGS, 0, getString(R.string.welcome_settings) ); |
133 |
|
item.setIcon(android.R.drawable.ic_menu_preferences); |
134 |
|
|
135 |
|
item = menu.add(0, MENU_RELOAD, 0, getString(R.string.welcome_reloadstations)); |
136 |
|
item.setIcon(android.R.drawable.ic_menu_rotate); |
137 |
|
|
138 |
|
return true; |
139 |
|
} |
140 |
|
|
141 |
|
@Override |
142 |
|
public boolean onOptionsItemSelected(MenuItem item) { |
143 |
|
boolean retval = true; |
144 |
|
|
145 |
|
switch (item.getItemId()) { |
146 |
|
case MENU_SETTINGS: |
147 |
|
Intent intent = new Intent(WelcomeScreen.this, SettingsScreen.class); |
148 |
|
WelcomeScreen.this.startActivity(intent); |
149 |
|
break; |
150 |
|
|
151 |
|
case MENU_RELOAD: |
152 |
|
OfflineStationProvider osp = (OfflineStationProvider) ProviderFactory.getStationProvider(); |
153 |
|
new StationLoader(osp).execute( (Void)null); |
154 |
|
break; |
155 |
|
|
156 |
|
default: |
157 |
|
retval = super.onOptionsItemSelected(item); |
158 |
|
} |
159 |
|
|
160 |
|
return retval; |
161 |
|
} |
162 |
|
|
163 |
|
|
164 |
class AboutListener implements OnClickListener { |
class AboutListener implements OnClickListener { |
165 |
|
|
166 |
@Override |
@Override |
175 |
message.append("By Torben H. Nielsen\n"); |
message.append("By Torben H. Nielsen\n"); |
176 |
|
|
177 |
MessageBox.showMessage(WelcomeScreen.this, message.toString());*/ |
MessageBox.showMessage(WelcomeScreen.this, message.toString());*/ |
178 |
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/")); |
179 |
startActivity(browserIntent); |
startActivity(browserIntent); |
180 |
} |
} |
181 |
|
|
182 |
} |
} |
183 |
|
|
184 |
|
|
185 |
class StationListListener implements OnClickListener{ |
class StationListListener implements OnClickListener{ |
186 |
ListType launchType; |
ListType launchType; |
196 |
} |
} |
197 |
|
|
198 |
} |
} |
199 |
|
|
200 |
|
class StationLoader extends AsyncTask<Void,Void,Void> { |
201 |
|
|
202 |
|
|
203 |
|
boolean succeeded; |
204 |
|
ProgressDialog dlg; |
205 |
|
OfflineStationProvider osp; |
206 |
|
String exMsg; |
207 |
|
|
208 |
|
public StationLoader(OfflineStationProvider osp) { |
209 |
|
this.osp = osp; |
210 |
|
} |
211 |
|
|
212 |
|
@Override |
213 |
|
protected Void doInBackground(Void... params) { |
214 |
|
try { |
215 |
|
osp.downloadStations( WelcomeScreen.this ); |
216 |
|
succeeded = true; |
217 |
|
} catch (Exception e) { |
218 |
|
succeeded = false; |
219 |
|
exMsg = e.getMessage(); |
220 |
|
} |
221 |
|
return null; |
222 |
|
} |
223 |
|
|
224 |
|
@Override |
225 |
|
protected void onPreExecute() { |
226 |
|
super.onPreExecute(); |
227 |
|
|
228 |
|
dlg = new ProgressDialog(WelcomeScreen.this); |
229 |
|
dlg.setMessage( "Downloading stations list" );//TODO: translate |
230 |
|
dlg.setCancelable(true); |
231 |
|
dlg.show(); |
232 |
|
} |
233 |
|
|
234 |
|
@Override |
235 |
|
protected void onPostExecute(Void result) { |
236 |
|
super.onPostExecute(result); |
237 |
|
|
238 |
|
dlg.dismiss(); |
239 |
|
dlg = null; |
240 |
|
|
241 |
|
if (succeeded) { |
242 |
|
Editor edit = prefs.edit(); |
243 |
|
edit.putLong(stationsreload, System.currentTimeMillis() ); |
244 |
|
edit.commit(); |
245 |
|
} else { |
246 |
|
Toast.makeText(WelcomeScreen.this, "Error " + exMsg, Toast.LENGTH_LONG).show(); |
247 |
|
} |
248 |
|
} |
249 |
|
} |
250 |
|
|
251 |
} |
} |