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; |
17 |
|
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, |
47 |
ListFavorites |
ListFavorites |
48 |
} |
} |
49 |
|
|
50 |
|
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); |
76 |
ExceptionHandler.register(this, "http://t-hoerup.dk/android/trace.php"); |
ExceptionHandler.register(this, "http://t-hoerup.dk/android/trace.php"); |
77 |
|
|
78 |
CheckUpdates update = new CheckUpdates(); |
CheckUpdates update = new CheckUpdates(); |
79 |
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); |
80 |
|
/* |
81 |
|
Runnable r = new Runnable() { |
82 |
|
@Override |
83 |
|
public void run() { |
84 |
|
View splash = findViewById(R.id.splash); |
85 |
|
splash.setVisibility(View.GONE); |
86 |
|
} |
87 |
|
}; |
88 |
|
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 |
|
if ( (now-last) > (14*24*60*60*1000) ) { |
101 |
|
new StationLoader(osp).execute( (Void)null); |
102 |
|
} else { |
103 |
|
|
104 |
|
boolean didLoad = osp.loadStations(this); |
105 |
|
|
106 |
|
if (didLoad == false) { |
107 |
|
new StationLoader(osp).execute( (Void)null); |
108 |
|
} |
109 |
|
} |
110 |
|
|
111 |
|
} catch (Exception e) { |
112 |
|
Toast.makeText(this, "" + e.getMessage(), Toast.LENGTH_SHORT).show(); |
113 |
|
} |
114 |
|
} |
115 |
|
} |
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
@Override |
120 |
|
protected void onDestroy() { |
121 |
|
super.onDestroy(); |
122 |
|
ProviderFactory.purgeOldEntries(); //exiting application, do some cleanup |
123 |
|
} |
124 |
|
|
125 |
|
|
126 |
|
@Override |
127 |
|
public boolean onCreateOptionsMenu(Menu menu) { |
128 |
|
MenuItem item; |
129 |
|
|
130 |
|
item = menu.add(0, MENU_SETTINGS, 0, getString(R.string.welcome_settings) ); |
131 |
|
item.setIcon(android.R.drawable.ic_menu_preferences); |
132 |
|
|
133 |
|
item = menu.add(0, MENU_RELOAD, 0, getString(R.string.welcome_reloadstations)); |
134 |
|
item.setIcon(android.R.drawable.ic_menu_rotate); |
135 |
|
|
136 |
|
return true; |
137 |
} |
} |
138 |
|
|
139 |
|
@Override |
140 |
|
public boolean onOptionsItemSelected(MenuItem item) { |
141 |
|
boolean retval = true; |
142 |
|
|
143 |
|
switch (item.getItemId()) { |
144 |
|
case MENU_SETTINGS: |
145 |
|
Intent intent = new Intent(WelcomeScreen.this, SettingsScreen.class); |
146 |
|
WelcomeScreen.this.startActivity(intent); |
147 |
|
break; |
148 |
|
|
149 |
|
case MENU_RELOAD: |
150 |
|
OfflineStationProvider osp = (OfflineStationProvider) ProviderFactory.getStationProvider(); |
151 |
|
new StationLoader(osp).execute( (Void)null); |
152 |
|
break; |
153 |
|
|
154 |
|
default: |
155 |
|
retval = super.onOptionsItemSelected(item); |
156 |
|
} |
157 |
|
|
158 |
|
return retval; |
159 |
|
} |
160 |
|
|
161 |
|
|
162 |
class AboutListener implements OnClickListener { |
class AboutListener implements OnClickListener { |
163 |
|
|
164 |
@Override |
@Override |
173 |
message.append("By Torben H. Nielsen\n"); |
message.append("By Torben H. Nielsen\n"); |
174 |
|
|
175 |
MessageBox.showMessage(WelcomeScreen.this, message.toString());*/ |
MessageBox.showMessage(WelcomeScreen.this, message.toString());*/ |
176 |
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/")); |
177 |
startActivity(browserIntent); |
startActivity(browserIntent); |
178 |
} |
} |
179 |
|
|
180 |
} |
} |
181 |
|
|
182 |
|
|
183 |
class StationListListener implements OnClickListener{ |
class StationListListener implements OnClickListener{ |
184 |
ListType launchType; |
ListType launchType; |
194 |
} |
} |
195 |
|
|
196 |
} |
} |
197 |
|
|
198 |
|
class StationLoader extends AsyncTask<Void,Void,Void> { |
199 |
|
|
200 |
|
|
201 |
|
boolean succeeded; |
202 |
|
ProgressDialog dlg; |
203 |
|
OfflineStationProvider osp; |
204 |
|
|
205 |
|
public StationLoader(OfflineStationProvider osp) { |
206 |
|
this.osp = osp; |
207 |
|
} |
208 |
|
|
209 |
|
@Override |
210 |
|
protected Void doInBackground(Void... params) { |
211 |
|
try { |
212 |
|
osp.downloadStations( WelcomeScreen.this ); |
213 |
|
succeeded = true; |
214 |
|
} catch (Exception e) { |
215 |
|
succeeded = false; |
216 |
|
Toast.makeText(WelcomeScreen.this, "Error " + e.getMessage(), Toast.LENGTH_LONG).show(); |
217 |
|
} |
218 |
|
return null; |
219 |
|
} |
220 |
|
|
221 |
|
@Override |
222 |
|
protected void onPreExecute() { |
223 |
|
super.onPreExecute(); |
224 |
|
|
225 |
|
dlg = new ProgressDialog(WelcomeScreen.this); |
226 |
|
dlg.setMessage( "Downloading stations list" );//TODO: translate |
227 |
|
dlg.setCancelable(true); |
228 |
|
dlg.show(); |
229 |
|
} |
230 |
|
|
231 |
|
@Override |
232 |
|
protected void onPostExecute(Void result) { |
233 |
|
super.onPostExecute(result); |
234 |
|
|
235 |
|
dlg.dismiss(); |
236 |
|
dlg = null; |
237 |
|
|
238 |
|
if (succeeded) { |
239 |
|
Editor edit = prefs.edit(); |
240 |
|
edit.putLong(stationsreload, System.currentTimeMillis() ); |
241 |
|
edit.commit(); |
242 |
|
} |
243 |
|
} |
244 |
|
} |
245 |
|
|
246 |
} |
} |