1 |
package dk.thoerup.traininfo; |
package dk.thoerup.traininfo; |
2 |
|
|
3 |
|
|
4 |
|
|
5 |
import android.app.Activity; |
import android.app.Activity; |
6 |
|
import android.app.ProgressDialog; |
7 |
import android.content.Intent; |
import android.content.Intent; |
8 |
|
import android.content.SharedPreferences; |
9 |
|
import android.content.SharedPreferences.Editor; |
10 |
import android.net.Uri; |
import android.net.Uri; |
11 |
|
import android.os.AsyncTask; |
12 |
import android.os.Bundle; |
import android.os.Bundle; |
13 |
import android.os.Handler; |
import android.os.Handler; |
14 |
import android.os.Message; |
import android.util.Log; |
15 |
|
import android.view.Menu; |
16 |
|
import android.view.MenuItem; |
17 |
import android.view.View; |
import android.view.View; |
|
import android.view.Window; |
|
18 |
import android.view.View.OnClickListener; |
import android.view.View.OnClickListener; |
19 |
|
import android.view.Window; |
20 |
import android.widget.Button; |
import android.widget.Button; |
21 |
|
import android.widget.Toast; |
22 |
|
|
23 |
import com.nullwire.trace.ExceptionHandler; |
import com.nullwire.trace.ExceptionHandler; |
24 |
|
|
25 |
import dk.thoerup.checkupdates.CheckUpdates; |
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{ |
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 { |
public enum ListType { |
39 |
ListNearest, |
ListNearest, |
40 |
ListSearch, |
ListSearch, |
43 |
|
|
44 |
Handler handler = new Handler(); |
Handler handler = new Handler(); |
45 |
|
|
46 |
|
SharedPreferences prefs; |
47 |
|
|
48 |
@Override |
@Override |
49 |
public void onCreate(Bundle savedInstanceState) { |
public void onCreate(Bundle savedInstanceState) { |
50 |
requestWindowFeature( Window.FEATURE_NO_TITLE ); |
|
51 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
52 |
|
|
53 |
|
prefs = getSharedPreferences("TrainStation", 0); |
54 |
|
|
55 |
|
requestWindowFeature( Window.FEATURE_NO_TITLE ); |
56 |
setContentView(R.layout.welcome); |
setContentView(R.layout.welcome); |
57 |
|
|
58 |
Button nearestButton = (Button) findViewById(R.id.nearest); |
Button nearestButton = (Button) findViewById(R.id.nearest); |
70 |
ExceptionHandler.register(this, "http://t-hoerup.dk/android/trace.php"); |
ExceptionHandler.register(this, "http://t-hoerup.dk/android/trace.php"); |
71 |
|
|
72 |
CheckUpdates update = new CheckUpdates(); |
CheckUpdates update = new CheckUpdates(); |
73 |
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); |
74 |
|
/* |
75 |
Runnable r = new Runnable() { |
Runnable r = new Runnable() { |
76 |
@Override |
@Override |
77 |
public void run() { |
public void run() { |
80 |
} |
} |
81 |
}; |
}; |
82 |
handler.postDelayed(r, 1500); |
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 { |
class AboutListener implements OnClickListener { |
159 |
|
|
160 |
@Override |
@Override |
169 |
message.append("By Torben H. Nielsen\n"); |
message.append("By Torben H. Nielsen\n"); |
170 |
|
|
171 |
MessageBox.showMessage(WelcomeScreen.this, message.toString());*/ |
MessageBox.showMessage(WelcomeScreen.this, message.toString());*/ |
172 |
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/")); |
173 |
startActivity(browserIntent); |
startActivity(browserIntent); |
174 |
} |
} |
175 |
|
|
176 |
} |
} |
177 |
|
|
178 |
|
|
179 |
class StationListListener implements OnClickListener{ |
class StationListListener implements OnClickListener{ |
180 |
ListType launchType; |
ListType launchType; |
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 |
} |
} |