1 |
package dk.thoerup.traininfo;
|
2 |
|
3 |
import android.app.Activity;
|
4 |
import android.app.ProgressDialog;
|
5 |
import android.content.Intent;
|
6 |
import android.content.SharedPreferences;
|
7 |
import android.content.SharedPreferences.Editor;
|
8 |
import android.net.Uri;
|
9 |
import android.os.AsyncTask;
|
10 |
import android.os.Bundle;
|
11 |
import android.os.Handler;
|
12 |
import android.util.Log;
|
13 |
import android.view.View;
|
14 |
import android.view.View.OnClickListener;
|
15 |
import android.view.Window;
|
16 |
import android.widget.Button;
|
17 |
import android.widget.Toast;
|
18 |
|
19 |
import com.nullwire.trace.ExceptionHandler;
|
20 |
|
21 |
import dk.thoerup.androidutils.CheckUpdates;
|
22 |
import dk.thoerup.traininfo.provider.OfflineStationProvider;
|
23 |
import dk.thoerup.traininfo.provider.ProviderFactory;
|
24 |
import dk.thoerup.traininfo.provider.StationProvider;
|
25 |
|
26 |
public class WelcomeScreen extends Activity{
|
27 |
|
28 |
final static String stationsreload = "stationsreload";
|
29 |
|
30 |
public enum ListType {
|
31 |
ListNearest,
|
32 |
ListSearch,
|
33 |
ListFavorites
|
34 |
}
|
35 |
|
36 |
Handler handler = new Handler();
|
37 |
|
38 |
SharedPreferences prefs;
|
39 |
|
40 |
@Override
|
41 |
public void onCreate(Bundle savedInstanceState) {
|
42 |
|
43 |
super.onCreate(savedInstanceState);
|
44 |
|
45 |
prefs = getSharedPreferences("TrainStation", 0);
|
46 |
|
47 |
requestWindowFeature( Window.FEATURE_NO_TITLE );
|
48 |
setContentView(R.layout.welcome);
|
49 |
|
50 |
Button nearestButton = (Button) findViewById(R.id.nearest);
|
51 |
nearestButton.setOnClickListener( new StationListListener(ListType.ListNearest));
|
52 |
|
53 |
Button searchButton = (Button) findViewById(R.id.search);
|
54 |
searchButton.setOnClickListener( new StationListListener(ListType.ListSearch));
|
55 |
|
56 |
Button favoritesButton = (Button) findViewById(R.id.favorites);
|
57 |
favoritesButton.setOnClickListener( new StationListListener(ListType.ListFavorites));
|
58 |
|
59 |
Button settingsButton = (Button) findViewById(R.id.settings);
|
60 |
settingsButton.setOnClickListener( new SettingsListener() );
|
61 |
|
62 |
Button aboutButton = (Button) findViewById(R.id.about);
|
63 |
aboutButton.setOnClickListener( new AboutListener() );
|
64 |
|
65 |
ExceptionHandler.register(this, "http://t-hoerup.dk/android/trace.php");
|
66 |
|
67 |
CheckUpdates update = new CheckUpdates();
|
68 |
update.checkForUpdates(this, "http://t-hoerup.dk/android/traininfo/version.txt", "TrainInfo DK", null);
|
69 |
/*
|
70 |
Runnable r = new Runnable() {
|
71 |
@Override
|
72 |
public void run() {
|
73 |
View splash = findViewById(R.id.splash);
|
74 |
splash.setVisibility(View.GONE);
|
75 |
}
|
76 |
};
|
77 |
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 |
|
107 |
|
108 |
@Override
|
109 |
protected void onDestroy() {
|
110 |
super.onDestroy();
|
111 |
ProviderFactory.purgeOldEntries(); //exiting application, do some cleanup
|
112 |
}
|
113 |
|
114 |
|
115 |
|
116 |
class AboutListener implements OnClickListener {
|
117 |
|
118 |
@Override
|
119 |
public void onClick(View v) {
|
120 |
/*
|
121 |
String appName = WelcomeScreen.this.getResources().getString(R.string.app_name);
|
122 |
String ver = WelcomeScreen.this.getResources().getString(R.string.app_version);
|
123 |
|
124 |
StringBuffer message = new StringBuffer();
|
125 |
message.append(appName);
|
126 |
message.append(" v").append(ver).append("\n");
|
127 |
message.append("By Torben H. Nielsen\n");
|
128 |
|
129 |
MessageBox.showMessage(WelcomeScreen.this, message.toString());*/
|
130 |
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://t-hoerup.dk/android/traininfo/"));
|
131 |
startActivity(browserIntent);
|
132 |
}
|
133 |
|
134 |
}
|
135 |
|
136 |
class SettingsListener implements OnClickListener{
|
137 |
|
138 |
@Override
|
139 |
public void onClick(View v) {
|
140 |
Intent intent = new Intent(WelcomeScreen.this, SettingsScreen.class);
|
141 |
WelcomeScreen.this.startActivity(intent);
|
142 |
}
|
143 |
}
|
144 |
|
145 |
class StationListListener implements OnClickListener{
|
146 |
ListType launchType;
|
147 |
StationListListener(ListType type) {
|
148 |
launchType = type;
|
149 |
}
|
150 |
|
151 |
@Override
|
152 |
public void onClick(View v) {
|
153 |
Intent intent = new Intent(WelcomeScreen.this, StationList.class);
|
154 |
intent.putExtra("type", launchType);
|
155 |
WelcomeScreen.this.startActivity(intent);
|
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 |
}
|