1 |
torben |
481 |
package dk.thoerup.traininfo;
|
2 |
|
|
|
3 |
torben |
1548 |
|
4 |
|
|
|
5 |
|
|
import java.util.ArrayList;
|
6 |
|
|
|
7 |
torben |
481 |
import android.app.Activity;
|
8 |
torben |
1547 |
import android.app.ProgressDialog;
|
9 |
torben |
481 |
import android.content.Intent;
|
10 |
torben |
1547 |
import android.content.SharedPreferences;
|
11 |
|
|
import android.content.SharedPreferences.Editor;
|
12 |
torben |
1548 |
import android.location.Location;
|
13 |
torben |
500 |
import android.net.Uri;
|
14 |
torben |
1547 |
import android.os.AsyncTask;
|
15 |
torben |
481 |
import android.os.Bundle;
|
16 |
torben |
743 |
import android.os.Handler;
|
17 |
torben |
1159 |
import android.util.Log;
|
18 |
torben |
1548 |
import android.view.Menu;
|
19 |
|
|
import android.view.MenuItem;
|
20 |
torben |
481 |
import android.view.View;
|
21 |
torben |
1547 |
import android.view.View.OnClickListener;
|
22 |
torben |
482 |
import android.view.Window;
|
23 |
torben |
481 |
import android.widget.Button;
|
24 |
torben |
1547 |
import android.widget.Toast;
|
25 |
torben |
481 |
|
26 |
torben |
720 |
import com.nullwire.trace.ExceptionHandler;
|
27 |
|
|
|
28 |
torben |
1548 |
import dk.thoerup.android.traininfo.common.StationEntry;
|
29 |
torben |
1131 |
import dk.thoerup.androidutils.CheckUpdates;
|
30 |
torben |
1547 |
import dk.thoerup.traininfo.provider.OfflineStationProvider;
|
31 |
torben |
1159 |
import dk.thoerup.traininfo.provider.ProviderFactory;
|
32 |
torben |
1547 |
import dk.thoerup.traininfo.provider.StationProvider;
|
33 |
torben |
1548 |
import dk.thoerup.traininfo.stationmap.GeoPair;
|
34 |
|
|
import dk.thoerup.traininfo.stationmap.StationMapView;
|
35 |
|
|
import dk.thoerup.traininfo.util.MessageBox;
|
36 |
torben |
720 |
|
37 |
torben |
481 |
public class WelcomeScreen extends Activity{
|
38 |
torben |
1547 |
|
39 |
|
|
final static String stationsreload = "stationsreload";
|
40 |
torben |
1548 |
final static int MENU_SETTINGS = 1;
|
41 |
|
|
final static int MENU_RELOAD = 2;
|
42 |
torben |
1547 |
|
43 |
torben |
1548 |
|
44 |
torben |
481 |
public enum ListType {
|
45 |
|
|
ListNearest,
|
46 |
|
|
ListSearch,
|
47 |
|
|
ListFavorites
|
48 |
|
|
}
|
49 |
|
|
|
50 |
torben |
743 |
Handler handler = new Handler();
|
51 |
|
|
|
52 |
torben |
1547 |
SharedPreferences prefs;
|
53 |
|
|
|
54 |
torben |
481 |
@Override
|
55 |
|
|
public void onCreate(Bundle savedInstanceState) {
|
56 |
torben |
916 |
|
57 |
|
|
super.onCreate(savedInstanceState);
|
58 |
torben |
1547 |
|
59 |
|
|
prefs = getSharedPreferences("TrainStation", 0);
|
60 |
|
|
|
61 |
torben |
482 |
requestWindowFeature( Window.FEATURE_NO_TITLE );
|
62 |
torben |
481 |
setContentView(R.layout.welcome);
|
63 |
|
|
|
64 |
|
|
Button nearestButton = (Button) findViewById(R.id.nearest);
|
65 |
|
|
nearestButton.setOnClickListener( new StationListListener(ListType.ListNearest));
|
66 |
|
|
|
67 |
|
|
Button searchButton = (Button) findViewById(R.id.search);
|
68 |
|
|
searchButton.setOnClickListener( new StationListListener(ListType.ListSearch));
|
69 |
|
|
|
70 |
|
|
Button favoritesButton = (Button) findViewById(R.id.favorites);
|
71 |
|
|
favoritesButton.setOnClickListener( new StationListListener(ListType.ListFavorites));
|
72 |
|
|
|
73 |
|
|
Button aboutButton = (Button) findViewById(R.id.about);
|
74 |
|
|
aboutButton.setOnClickListener( new AboutListener() );
|
75 |
torben |
568 |
|
76 |
|
|
ExceptionHandler.register(this, "http://t-hoerup.dk/android/trace.php");
|
77 |
torben |
720 |
|
78 |
|
|
CheckUpdates update = new CheckUpdates();
|
79 |
torben |
911 |
update.checkForUpdates(this, "http://t-hoerup.dk/android/traininfo/version.txt", "TrainInfo DK", null);
|
80 |
torben |
846 |
/*
|
81 |
torben |
743 |
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 |
torben |
846 |
*/
|
90 |
torben |
1547 |
|
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 |
torben |
1549 |
Log.i("TrainInfo", "Last Load: " + last);
|
101 |
|
|
|
102 |
torben |
1547 |
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 |
torben |
481 |
}
|
118 |
torben |
1159 |
|
119 |
|
|
|
120 |
torben |
743 |
|
121 |
torben |
1159 |
@Override
|
122 |
|
|
protected void onDestroy() {
|
123 |
|
|
super.onDestroy();
|
124 |
|
|
ProviderFactory.purgeOldEntries(); //exiting application, do some cleanup
|
125 |
|
|
}
|
126 |
|
|
|
127 |
|
|
|
128 |
torben |
1548 |
@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 |
torben |
1159 |
|
164 |
torben |
481 |
class AboutListener implements OnClickListener {
|
165 |
|
|
|
166 |
|
|
@Override
|
167 |
|
|
public void onClick(View v) {
|
168 |
torben |
500 |
/*
|
169 |
torben |
484 |
String appName = WelcomeScreen.this.getResources().getString(R.string.app_name);
|
170 |
|
|
String ver = WelcomeScreen.this.getResources().getString(R.string.app_version);
|
171 |
|
|
|
172 |
torben |
483 |
StringBuffer message = new StringBuffer();
|
173 |
torben |
484 |
message.append(appName);
|
174 |
|
|
message.append(" v").append(ver).append("\n");
|
175 |
torben |
483 |
message.append("By Torben H. Nielsen\n");
|
176 |
|
|
|
177 |
torben |
500 |
MessageBox.showMessage(WelcomeScreen.this, message.toString());*/
|
178 |
torben |
1393 |
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://t-hoerup.dk/android/traininfo/"));
|
179 |
torben |
500 |
startActivity(browserIntent);
|
180 |
torben |
481 |
}
|
181 |
torben |
483 |
|
182 |
torben |
481 |
}
|
183 |
torben |
1446 |
|
184 |
torben |
483 |
|
185 |
torben |
481 |
class StationListListener implements OnClickListener{
|
186 |
|
|
ListType launchType;
|
187 |
|
|
StationListListener(ListType type) {
|
188 |
|
|
launchType = type;
|
189 |
|
|
}
|
190 |
|
|
|
191 |
|
|
@Override
|
192 |
|
|
public void onClick(View v) {
|
193 |
|
|
Intent intent = new Intent(WelcomeScreen.this, StationList.class);
|
194 |
|
|
intent.putExtra("type", launchType);
|
195 |
|
|
WelcomeScreen.this.startActivity(intent);
|
196 |
|
|
}
|
197 |
|
|
|
198 |
|
|
}
|
199 |
torben |
1547 |
|
200 |
|
|
class StationLoader extends AsyncTask<Void,Void,Void> {
|
201 |
|
|
|
202 |
|
|
|
203 |
|
|
boolean succeeded;
|
204 |
|
|
ProgressDialog dlg;
|
205 |
|
|
OfflineStationProvider osp;
|
206 |
|
|
|
207 |
|
|
public StationLoader(OfflineStationProvider osp) {
|
208 |
|
|
this.osp = osp;
|
209 |
|
|
}
|
210 |
|
|
|
211 |
|
|
@Override
|
212 |
|
|
protected Void doInBackground(Void... params) {
|
213 |
|
|
try {
|
214 |
|
|
osp.downloadStations( WelcomeScreen.this );
|
215 |
|
|
succeeded = true;
|
216 |
|
|
} catch (Exception e) {
|
217 |
|
|
succeeded = false;
|
218 |
|
|
Toast.makeText(WelcomeScreen.this, "Error " + e.getMessage(), Toast.LENGTH_LONG).show();
|
219 |
|
|
}
|
220 |
|
|
return null;
|
221 |
|
|
}
|
222 |
|
|
|
223 |
|
|
@Override
|
224 |
|
|
protected void onPreExecute() {
|
225 |
|
|
super.onPreExecute();
|
226 |
|
|
|
227 |
|
|
dlg = new ProgressDialog(WelcomeScreen.this);
|
228 |
|
|
dlg.setMessage( "Downloading stations list" );//TODO: translate
|
229 |
|
|
dlg.setCancelable(true);
|
230 |
|
|
dlg.show();
|
231 |
|
|
}
|
232 |
|
|
|
233 |
|
|
@Override
|
234 |
|
|
protected void onPostExecute(Void result) {
|
235 |
|
|
super.onPostExecute(result);
|
236 |
|
|
|
237 |
|
|
dlg.dismiss();
|
238 |
|
|
dlg = null;
|
239 |
|
|
|
240 |
|
|
if (succeeded) {
|
241 |
|
|
Editor edit = prefs.edit();
|
242 |
|
|
edit.putLong(stationsreload, System.currentTimeMillis() );
|
243 |
|
|
edit.commit();
|
244 |
|
|
}
|
245 |
|
|
}
|
246 |
|
|
}
|
247 |
|
|
|
248 |
torben |
481 |
}
|