1 |
package dk.thoerup.traininfo;
|
2 |
|
3 |
|
4 |
|
5 |
import android.app.Activity;
|
6 |
import android.app.ProgressDialog;
|
7 |
import android.content.Intent;
|
8 |
import android.content.SharedPreferences;
|
9 |
import android.content.SharedPreferences.Editor;
|
10 |
import android.net.Uri;
|
11 |
import android.os.AsyncTask;
|
12 |
import android.os.Bundle;
|
13 |
import android.os.Handler;
|
14 |
import android.util.Log;
|
15 |
import android.view.Menu;
|
16 |
import android.view.MenuItem;
|
17 |
import android.view.View;
|
18 |
import android.view.View.OnClickListener;
|
19 |
import android.view.Window;
|
20 |
import android.widget.Button;
|
21 |
import android.widget.Toast;
|
22 |
|
23 |
import com.nullwire.trace.ExceptionHandler;
|
24 |
|
25 |
import dk.thoerup.androidutils.CheckUpdates;
|
26 |
import dk.thoerup.traininfo.provider.OfflineStationProvider;
|
27 |
import dk.thoerup.traininfo.provider.ProviderFactory;
|
28 |
import dk.thoerup.traininfo.provider.StationProvider;
|
29 |
|
30 |
|
31 |
public class WelcomeScreen extends Activity{
|
32 |
|
33 |
final static String stationsreload = "stationsreload";
|
34 |
final static int MENU_SETTINGS = 1;
|
35 |
final static int MENU_RELOAD = 2;
|
36 |
|
37 |
|
38 |
public enum ListType {
|
39 |
ListNearest,
|
40 |
ListSearch,
|
41 |
ListFavorites
|
42 |
}
|
43 |
|
44 |
Handler handler = new Handler();
|
45 |
|
46 |
SharedPreferences prefs;
|
47 |
|
48 |
@Override
|
49 |
public void onCreate(Bundle savedInstanceState) {
|
50 |
|
51 |
super.onCreate(savedInstanceState);
|
52 |
|
53 |
prefs = getSharedPreferences("TrainStation", 0);
|
54 |
|
55 |
requestWindowFeature( Window.FEATURE_NO_TITLE );
|
56 |
setContentView(R.layout.welcome);
|
57 |
|
58 |
Button nearestButton = (Button) findViewById(R.id.nearest);
|
59 |
nearestButton.setOnClickListener( new StationListListener(ListType.ListNearest));
|
60 |
|
61 |
Button searchButton = (Button) findViewById(R.id.search);
|
62 |
searchButton.setOnClickListener( new StationListListener(ListType.ListSearch));
|
63 |
|
64 |
Button favoritesButton = (Button) findViewById(R.id.favorites);
|
65 |
favoritesButton.setOnClickListener( new StationListListener(ListType.ListFavorites));
|
66 |
|
67 |
Button aboutButton = (Button) findViewById(R.id.about);
|
68 |
aboutButton.setOnClickListener( new AboutListener() );
|
69 |
|
70 |
ExceptionHandler.register(this, "http://t-hoerup.dk/android/trace.php");
|
71 |
|
72 |
CheckUpdates update = new CheckUpdates();
|
73 |
update.checkForUpdates(this, "http://t-hoerup.dk/android/traininfo/version.txt", "TrainInfo DK", null);
|
74 |
/*
|
75 |
Runnable r = new Runnable() {
|
76 |
@Override
|
77 |
public void run() {
|
78 |
View splash = findViewById(R.id.splash);
|
79 |
splash.setVisibility(View.GONE);
|
80 |
}
|
81 |
};
|
82 |
handler.postDelayed(r, 1500);
|
83 |
*/
|
84 |
|
85 |
StationProvider sp = ProviderFactory.getStationProvider();
|
86 |
|
87 |
if (sp instanceof OfflineStationProvider ) {
|
88 |
OfflineStationProvider osp = (OfflineStationProvider) sp;
|
89 |
try {
|
90 |
|
91 |
long last = prefs.getLong(stationsreload, 0);
|
92 |
long now = System.currentTimeMillis();
|
93 |
|
94 |
Log.i("TrainInfo", "Last Load: " + last);
|
95 |
|
96 |
if ( (now-last) > (14*24*60*60*1000) ) {
|
97 |
new StationLoader(osp).execute( (Void)null);
|
98 |
} else {
|
99 |
|
100 |
boolean didLoad = osp.loadStations(this);
|
101 |
|
102 |
if (didLoad == false) {
|
103 |
new StationLoader(osp).execute( (Void)null);
|
104 |
}
|
105 |
}
|
106 |
|
107 |
} catch (Exception e) {
|
108 |
Toast.makeText(this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();
|
109 |
}
|
110 |
}
|
111 |
}
|
112 |
|
113 |
|
114 |
|
115 |
@Override
|
116 |
protected void onDestroy() {
|
117 |
super.onDestroy();
|
118 |
ProviderFactory.purgeOldEntries(); //exiting application, do some cleanup
|
119 |
}
|
120 |
|
121 |
|
122 |
@Override
|
123 |
public boolean onCreateOptionsMenu(Menu menu) {
|
124 |
MenuItem item;
|
125 |
|
126 |
item = menu.add(0, MENU_SETTINGS, 0, getString(R.string.welcome_settings) );
|
127 |
item.setIcon(android.R.drawable.ic_menu_preferences);
|
128 |
|
129 |
item = menu.add(0, MENU_RELOAD, 0, getString(R.string.welcome_reloadstations));
|
130 |
item.setIcon(android.R.drawable.ic_menu_rotate);
|
131 |
|
132 |
return true;
|
133 |
}
|
134 |
|
135 |
@Override
|
136 |
public boolean onOptionsItemSelected(MenuItem item) {
|
137 |
boolean retval = true;
|
138 |
|
139 |
switch (item.getItemId()) {
|
140 |
case MENU_SETTINGS:
|
141 |
Intent intent = new Intent(WelcomeScreen.this, SettingsScreen.class);
|
142 |
WelcomeScreen.this.startActivity(intent);
|
143 |
break;
|
144 |
|
145 |
case MENU_RELOAD:
|
146 |
OfflineStationProvider osp = (OfflineStationProvider) ProviderFactory.getStationProvider();
|
147 |
new StationLoader(osp).execute( (Void)null);
|
148 |
break;
|
149 |
|
150 |
default:
|
151 |
retval = super.onOptionsItemSelected(item);
|
152 |
}
|
153 |
|
154 |
return retval;
|
155 |
}
|
156 |
|
157 |
|
158 |
class AboutListener implements OnClickListener {
|
159 |
|
160 |
@Override
|
161 |
public void onClick(View v) {
|
162 |
/*
|
163 |
String appName = WelcomeScreen.this.getResources().getString(R.string.app_name);
|
164 |
String ver = WelcomeScreen.this.getResources().getString(R.string.app_version);
|
165 |
|
166 |
StringBuffer message = new StringBuffer();
|
167 |
message.append(appName);
|
168 |
message.append(" v").append(ver).append("\n");
|
169 |
message.append("By Torben H. Nielsen\n");
|
170 |
|
171 |
MessageBox.showMessage(WelcomeScreen.this, message.toString());*/
|
172 |
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://t-hoerup.dk/android/traininfo/"));
|
173 |
startActivity(browserIntent);
|
174 |
}
|
175 |
|
176 |
}
|
177 |
|
178 |
|
179 |
class StationListListener implements OnClickListener{
|
180 |
ListType launchType;
|
181 |
StationListListener(ListType type) {
|
182 |
launchType = type;
|
183 |
}
|
184 |
|
185 |
@Override
|
186 |
public void onClick(View v) {
|
187 |
Intent intent = new Intent(WelcomeScreen.this, StationList.class);
|
188 |
intent.putExtra("type", launchType);
|
189 |
WelcomeScreen.this.startActivity(intent);
|
190 |
}
|
191 |
|
192 |
}
|
193 |
|
194 |
class StationLoader extends AsyncTask<Void,Void,Void> {
|
195 |
|
196 |
|
197 |
boolean succeeded;
|
198 |
ProgressDialog dlg;
|
199 |
OfflineStationProvider osp;
|
200 |
String exMsg;
|
201 |
|
202 |
public StationLoader(OfflineStationProvider osp) {
|
203 |
this.osp = osp;
|
204 |
}
|
205 |
|
206 |
@Override
|
207 |
protected Void doInBackground(Void... params) {
|
208 |
try {
|
209 |
osp.downloadStations( WelcomeScreen.this );
|
210 |
succeeded = true;
|
211 |
} catch (Exception e) {
|
212 |
succeeded = false;
|
213 |
exMsg = e.getMessage();
|
214 |
}
|
215 |
return null;
|
216 |
}
|
217 |
|
218 |
@Override
|
219 |
protected void onPreExecute() {
|
220 |
super.onPreExecute();
|
221 |
|
222 |
dlg = new ProgressDialog(WelcomeScreen.this);
|
223 |
dlg.setMessage( "Downloading stations list" );//TODO: translate
|
224 |
dlg.setCancelable(true);
|
225 |
dlg.show();
|
226 |
}
|
227 |
|
228 |
@Override
|
229 |
protected void onPostExecute(Void result) {
|
230 |
super.onPostExecute(result);
|
231 |
|
232 |
dlg.dismiss();
|
233 |
dlg = null;
|
234 |
|
235 |
if (succeeded) {
|
236 |
Editor edit = prefs.edit();
|
237 |
edit.putLong(stationsreload, System.currentTimeMillis() );
|
238 |
edit.commit();
|
239 |
} else {
|
240 |
Toast.makeText(WelcomeScreen.this, "Error " + exMsg, Toast.LENGTH_LONG).show();
|
241 |
}
|
242 |
}
|
243 |
}
|
244 |
|
245 |
}
|