1 |
package dk.thoerup.traininfo; |
package dk.thoerup.traininfo; |
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
import java.io.InvalidClassException; |
6 |
|
|
7 |
import android.app.Activity; |
import android.app.Activity; |
8 |
import android.app.ProgressDialog; |
import android.app.ProgressDialog; |
9 |
import android.content.Intent; |
import android.content.Intent; |
14 |
import android.os.Bundle; |
import android.os.Bundle; |
15 |
import android.os.Handler; |
import android.os.Handler; |
16 |
import android.util.Log; |
import android.util.Log; |
17 |
|
import android.view.Menu; |
18 |
|
import android.view.MenuItem; |
19 |
import android.view.View; |
import android.view.View; |
20 |
import android.view.View.OnClickListener; |
import android.view.View.OnClickListener; |
21 |
import android.view.Window; |
import android.view.Window; |
29 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
import dk.thoerup.traininfo.provider.ProviderFactory; |
30 |
import dk.thoerup.traininfo.provider.StationProvider; |
import dk.thoerup.traininfo.provider.StationProvider; |
31 |
|
|
32 |
|
|
33 |
public class WelcomeScreen extends Activity{ |
public class WelcomeScreen extends Activity{ |
34 |
|
|
35 |
final static String stationsreload = "stationsreload"; |
final static String stationsreload = "stationsreload"; |
36 |
|
final static int MENU_SETTINGS = 1; |
37 |
|
final static int MENU_RELOAD = 2; |
38 |
|
|
39 |
|
|
40 |
public enum ListType { |
public enum ListType { |
41 |
ListNearest, |
ListNearest, |
66 |
Button favoritesButton = (Button) findViewById(R.id.favorites); |
Button favoritesButton = (Button) findViewById(R.id.favorites); |
67 |
favoritesButton.setOnClickListener( new StationListListener(ListType.ListFavorites)); |
favoritesButton.setOnClickListener( new StationListListener(ListType.ListFavorites)); |
68 |
|
|
|
Button settingsButton = (Button) findViewById(R.id.settings); |
|
|
settingsButton.setOnClickListener( new SettingsListener() ); |
|
|
|
|
69 |
Button aboutButton = (Button) findViewById(R.id.about); |
Button aboutButton = (Button) findViewById(R.id.about); |
70 |
aboutButton.setOnClickListener( new AboutListener() ); |
aboutButton.setOnClickListener( new AboutListener() ); |
71 |
|
|
88 |
|
|
89 |
if (sp instanceof OfflineStationProvider ) { |
if (sp instanceof OfflineStationProvider ) { |
90 |
OfflineStationProvider osp = (OfflineStationProvider) sp; |
OfflineStationProvider osp = (OfflineStationProvider) sp; |
91 |
try { |
long last = prefs.getLong(stationsreload, 0); |
92 |
|
long now = System.currentTimeMillis(); |
93 |
|
Log.i("TrainInfo", "Last Load: " + last); |
94 |
|
|
95 |
|
if ( (now-last) > (14*24*60*60*1000) ) { |
96 |
|
new StationLoader(osp).execute( (Void)null); |
97 |
|
} else { |
98 |
|
|
99 |
|
boolean didLoad = false; |
100 |
|
|
101 |
long last = prefs.getLong(stationsreload, 0); |
try { |
102 |
long now = System.currentTimeMillis(); |
didLoad = osp.loadStations(this); |
103 |
|
} |
104 |
|
catch (InvalidClassException e) { |
105 |
|
Log.i("TrainInfo", "invalid class - do a new download of stationlist"); |
106 |
|
} |
107 |
|
catch (Exception e) { |
108 |
|
Toast.makeText(this, "" + e.getMessage(), Toast.LENGTH_SHORT).show(); |
109 |
|
Log.e("TrainInfo", "load error", e); |
110 |
|
} |
111 |
|
|
112 |
if ( (now-last) > (14*24*60*60*1000) ) { |
if (didLoad == false) { |
113 |
new StationLoader(osp).execute( (Void)null); |
new StationLoader(osp).execute( (Void)null); |
|
} else { |
|
|
|
|
|
boolean didLoad = osp.loadStations(this); |
|
|
|
|
|
if (didLoad == false) { |
|
|
new StationLoader(osp).execute( (Void)null); |
|
|
} |
|
114 |
} |
} |
|
|
|
|
} catch (Exception e) { |
|
|
Toast.makeText(this, "" + e.getMessage(), Toast.LENGTH_SHORT).show(); |
|
115 |
} |
} |
116 |
|
|
117 |
} |
} |
118 |
} |
} |
119 |
|
|
126 |
} |
} |
127 |
|
|
128 |
|
|
129 |
|
@Override |
130 |
|
public boolean onCreateOptionsMenu(Menu menu) { |
131 |
|
MenuItem item; |
132 |
|
|
133 |
|
item = menu.add(0, MENU_SETTINGS, 0, getString(R.string.welcome_settings) ); |
134 |
|
item.setIcon(android.R.drawable.ic_menu_preferences); |
135 |
|
|
136 |
|
item = menu.add(0, MENU_RELOAD, 0, getString(R.string.welcome_reloadstations)); |
137 |
|
item.setIcon(android.R.drawable.ic_menu_rotate); |
138 |
|
|
139 |
|
return true; |
140 |
|
} |
141 |
|
|
142 |
|
@Override |
143 |
|
public boolean onOptionsItemSelected(MenuItem item) { |
144 |
|
boolean retval = true; |
145 |
|
|
146 |
|
switch (item.getItemId()) { |
147 |
|
case MENU_SETTINGS: |
148 |
|
Intent intent = new Intent(WelcomeScreen.this, SettingsScreen.class); |
149 |
|
WelcomeScreen.this.startActivity(intent); |
150 |
|
break; |
151 |
|
|
152 |
|
case MENU_RELOAD: |
153 |
|
OfflineStationProvider osp = (OfflineStationProvider) ProviderFactory.getStationProvider(); |
154 |
|
new StationLoader(osp).execute( (Void)null); |
155 |
|
break; |
156 |
|
|
157 |
|
default: |
158 |
|
retval = super.onOptionsItemSelected(item); |
159 |
|
} |
160 |
|
|
161 |
|
return retval; |
162 |
|
} |
163 |
|
|
164 |
|
|
165 |
class AboutListener implements OnClickListener { |
class AboutListener implements OnClickListener { |
166 |
|
|
182 |
|
|
183 |
} |
} |
184 |
|
|
|
class SettingsListener implements OnClickListener{ |
|
|
|
|
|
@Override |
|
|
public void onClick(View v) { |
|
|
Intent intent = new Intent(WelcomeScreen.this, SettingsScreen.class); |
|
|
WelcomeScreen.this.startActivity(intent); |
|
|
} |
|
|
} |
|
185 |
|
|
186 |
class StationListListener implements OnClickListener{ |
class StationListListener implements OnClickListener{ |
187 |
ListType launchType; |
ListType launchType; |
204 |
boolean succeeded; |
boolean succeeded; |
205 |
ProgressDialog dlg; |
ProgressDialog dlg; |
206 |
OfflineStationProvider osp; |
OfflineStationProvider osp; |
207 |
|
String exMsg; |
208 |
|
|
209 |
public StationLoader(OfflineStationProvider osp) { |
public StationLoader(OfflineStationProvider osp) { |
210 |
this.osp = osp; |
this.osp = osp; |
217 |
succeeded = true; |
succeeded = true; |
218 |
} catch (Exception e) { |
} catch (Exception e) { |
219 |
succeeded = false; |
succeeded = false; |
220 |
Toast.makeText(WelcomeScreen.this, "Error " + e.getMessage(), Toast.LENGTH_LONG).show(); |
exMsg = e.getMessage(); |
221 |
} |
} |
222 |
return null; |
return null; |
223 |
} |
} |
224 |
|
|
225 |
|
|
226 |
@Override |
@Override |
227 |
protected void onPreExecute() { |
protected void onPreExecute() { |
228 |
super.onPreExecute(); |
super.onPreExecute(); |
229 |
|
|
230 |
dlg = new ProgressDialog(WelcomeScreen.this); |
dlg = new ProgressDialog(WelcomeScreen.this); |
231 |
dlg.setMessage( "Downloading stations list" );//TODO: translate |
dlg.setMessage( getText(R.string.welcome_downloadingstations) ); |
232 |
dlg.setCancelable(true); |
dlg.setCancelable(true); |
233 |
dlg.show(); |
dlg.show(); |
234 |
} |
} |
244 |
Editor edit = prefs.edit(); |
Editor edit = prefs.edit(); |
245 |
edit.putLong(stationsreload, System.currentTimeMillis() ); |
edit.putLong(stationsreload, System.currentTimeMillis() ); |
246 |
edit.commit(); |
edit.commit(); |
247 |
|
} else { |
248 |
|
Toast.makeText(WelcomeScreen.this, "Error " + exMsg, Toast.LENGTH_LONG).show(); |
249 |
} |
} |
250 |
} |
} |
251 |
} |
} |