1 |
package dk.thoerup.traininfo; |
package dk.thoerup.traininfo; |
2 |
|
|
3 |
import android.app.Activity; |
import android.app.Activity; |
4 |
|
import android.app.ProgressDialog; |
5 |
import android.content.Intent; |
import android.content.Intent; |
6 |
|
import android.content.SharedPreferences; |
7 |
|
import android.content.SharedPreferences.Editor; |
8 |
import android.net.Uri; |
import android.net.Uri; |
9 |
|
import android.os.AsyncTask; |
10 |
import android.os.Bundle; |
import android.os.Bundle; |
11 |
import android.os.Handler; |
import android.os.Handler; |
12 |
import android.util.Log; |
import android.util.Log; |
13 |
import android.view.View; |
import android.view.View; |
|
import android.view.Window; |
|
14 |
import android.view.View.OnClickListener; |
import android.view.View.OnClickListener; |
15 |
|
import android.view.Window; |
16 |
import android.widget.Button; |
import android.widget.Button; |
17 |
|
import android.widget.Toast; |
18 |
|
|
19 |
import com.nullwire.trace.ExceptionHandler; |
import com.nullwire.trace.ExceptionHandler; |
20 |
|
|
21 |
import dk.thoerup.androidutils.CheckUpdates; |
import dk.thoerup.androidutils.CheckUpdates; |
22 |
|
import dk.thoerup.traininfo.provider.OfflineStationProvider; |
23 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
import dk.thoerup.traininfo.provider.ProviderFactory; |
24 |
|
import dk.thoerup.traininfo.provider.StationProvider; |
25 |
|
|
26 |
public class WelcomeScreen extends Activity{ |
public class WelcomeScreen extends Activity{ |
27 |
|
|
28 |
|
final static String stationsreload = "stationsreload"; |
29 |
|
|
30 |
public enum ListType { |
public enum ListType { |
31 |
ListNearest, |
ListNearest, |
32 |
ListSearch, |
ListSearch, |
35 |
|
|
36 |
Handler handler = new Handler(); |
Handler handler = new Handler(); |
37 |
|
|
38 |
|
SharedPreferences prefs; |
39 |
|
|
40 |
@Override |
@Override |
41 |
public void onCreate(Bundle savedInstanceState) { |
public void onCreate(Bundle savedInstanceState) { |
42 |
|
|
43 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
44 |
|
|
45 |
|
prefs = getSharedPreferences("TrainStation", 0); |
46 |
|
|
47 |
requestWindowFeature( Window.FEATURE_NO_TITLE ); |
requestWindowFeature( Window.FEATURE_NO_TITLE ); |
48 |
setContentView(R.layout.welcome); |
setContentView(R.layout.welcome); |
49 |
|
|
76 |
}; |
}; |
77 |
handler.postDelayed(r, 1500); |
handler.postDelayed(r, 1500); |
78 |
*/ |
*/ |
79 |
|
|
80 |
|
StationProvider sp = ProviderFactory.getStationProvider(); |
81 |
|
|
82 |
|
if (sp instanceof OfflineStationProvider ) { |
83 |
|
OfflineStationProvider osp = (OfflineStationProvider) sp; |
84 |
|
try { |
85 |
|
|
86 |
|
long last = prefs.getLong(stationsreload, 0); |
87 |
|
long now = System.currentTimeMillis(); |
88 |
|
|
89 |
|
if ( (now-last) > (14*24*60*60*1000) ) { |
90 |
|
new StationLoader(osp).execute( (Void)null); |
91 |
|
} else { |
92 |
|
|
93 |
|
boolean didLoad = osp.loadStations(this); |
94 |
|
|
95 |
|
if (didLoad == false) { |
96 |
|
new StationLoader(osp).execute( (Void)null); |
97 |
|
} |
98 |
|
} |
99 |
|
|
100 |
|
} catch (Exception e) { |
101 |
|
Toast.makeText(this, "" + e.getMessage(), Toast.LENGTH_SHORT).show(); |
102 |
|
} |
103 |
|
} |
104 |
} |
} |
105 |
|
|
106 |
|
|
111 |
ProviderFactory.purgeOldEntries(); //exiting application, do some cleanup |
ProviderFactory.purgeOldEntries(); //exiting application, do some cleanup |
112 |
} |
} |
113 |
|
|
|
|
|
114 |
|
|
115 |
|
|
116 |
class AboutListener implements OnClickListener { |
class AboutListener implements OnClickListener { |
156 |
} |
} |
157 |
|
|
158 |
} |
} |
159 |
|
|
160 |
|
class StationLoader extends AsyncTask<Void,Void,Void> { |
161 |
|
|
162 |
|
|
163 |
|
boolean succeeded; |
164 |
|
ProgressDialog dlg; |
165 |
|
OfflineStationProvider osp; |
166 |
|
|
167 |
|
public StationLoader(OfflineStationProvider osp) { |
168 |
|
this.osp = osp; |
169 |
|
} |
170 |
|
|
171 |
|
@Override |
172 |
|
protected Void doInBackground(Void... params) { |
173 |
|
try { |
174 |
|
osp.downloadStations( WelcomeScreen.this ); |
175 |
|
succeeded = true; |
176 |
|
} catch (Exception e) { |
177 |
|
succeeded = false; |
178 |
|
Toast.makeText(WelcomeScreen.this, "Error " + e.getMessage(), Toast.LENGTH_LONG).show(); |
179 |
|
} |
180 |
|
return null; |
181 |
|
} |
182 |
|
|
183 |
|
@Override |
184 |
|
protected void onPreExecute() { |
185 |
|
super.onPreExecute(); |
186 |
|
|
187 |
|
dlg = new ProgressDialog(WelcomeScreen.this); |
188 |
|
dlg.setMessage( "Downloading stations list" );//TODO: translate |
189 |
|
dlg.setCancelable(true); |
190 |
|
dlg.show(); |
191 |
|
} |
192 |
|
|
193 |
|
@Override |
194 |
|
protected void onPostExecute(Void result) { |
195 |
|
super.onPostExecute(result); |
196 |
|
|
197 |
|
dlg.dismiss(); |
198 |
|
dlg = null; |
199 |
|
|
200 |
|
if (succeeded) { |
201 |
|
Editor edit = prefs.edit(); |
202 |
|
edit.putLong(stationsreload, System.currentTimeMillis() ); |
203 |
|
edit.commit(); |
204 |
|
} |
205 |
|
} |
206 |
|
} |
207 |
|
|
208 |
} |
} |