1 |
package dk.thoerup.traininfo; |
package dk.thoerup.traininfo; |
2 |
|
|
3 |
import android.app.Activity; |
import android.app.Activity; |
4 |
|
import android.app.ProgressDialog; |
5 |
import android.content.Intent; |
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; |
import android.os.Bundle; |
11 |
|
import android.os.Handler; |
12 |
|
import android.util.Log; |
13 |
import android.view.View; |
import android.view.View; |
14 |
import android.view.View.OnClickListener; |
import android.view.View.OnClickListener; |
15 |
|
import android.view.Window; |
16 |
import android.widget.Button; |
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{ |
public class WelcomeScreen extends Activity{ |
27 |
|
|
28 |
|
final static String stationsreload = "stationsreload"; |
29 |
|
|
30 |
public enum ListType { |
public enum ListType { |
31 |
ListNearest, |
ListNearest, |
32 |
ListSearch, |
ListSearch, |
33 |
ListFavorites |
ListFavorites |
34 |
} |
} |
35 |
|
|
36 |
|
Handler handler = new Handler(); |
37 |
|
|
38 |
|
SharedPreferences prefs; |
39 |
|
|
40 |
@Override |
@Override |
41 |
public void onCreate(Bundle savedInstanceState) { |
public void onCreate(Bundle savedInstanceState) { |
42 |
|
|
43 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
44 |
|
|
45 |
|
prefs = getSharedPreferences("TrainStation", 0); |
46 |
|
|
47 |
|
requestWindowFeature( Window.FEATURE_NO_TITLE ); |
48 |
setContentView(R.layout.welcome); |
setContentView(R.layout.welcome); |
49 |
|
|
50 |
Button nearestButton = (Button) findViewById(R.id.nearest); |
Button nearestButton = (Button) findViewById(R.id.nearest); |
56 |
Button favoritesButton = (Button) findViewById(R.id.favorites); |
Button favoritesButton = (Button) findViewById(R.id.favorites); |
57 |
favoritesButton.setOnClickListener( new StationListListener(ListType.ListFavorites)); |
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); |
Button aboutButton = (Button) findViewById(R.id.about); |
63 |
aboutButton.setOnClickListener( new AboutListener() ); |
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 { |
class AboutListener implements OnClickListener { |
117 |
|
|
118 |
@Override |
@Override |
119 |
public void onClick(View v) { |
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{ |
class StationListListener implements OnClickListener{ |
146 |
ListType launchType; |
ListType launchType; |
147 |
StationListListener(ListType type) { |
StationListListener(ListType type) { |
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 |
} |
} |