1 |
package dk.thoerup.ekllauncher; |
2 |
|
3 |
import android.app.Activity; |
4 |
import android.app.AlertDialog; |
5 |
import android.content.Context; |
6 |
import android.content.DialogInterface; |
7 |
import android.content.Intent; |
8 |
import android.location.LocationManager; |
9 |
import android.net.ConnectivityManager; |
10 |
import android.net.NetworkInfo; |
11 |
import android.os.Bundle; |
12 |
import android.os.Handler; |
13 |
import android.os.Message; |
14 |
import android.os.PowerManager; |
15 |
import android.provider.Settings; |
16 |
import android.telephony.TelephonyManager; |
17 |
import android.util.Log; |
18 |
import android.view.Menu; |
19 |
import android.view.MenuItem; |
20 |
import android.view.Window; |
21 |
import android.view.WindowManager; |
22 |
import android.webkit.WebSettings; |
23 |
import android.webkit.WebStorage; |
24 |
import android.webkit.WebView; |
25 |
import android.webkit.WebViewClient; |
26 |
|
27 |
public class EKLLauncherActivity extends Activity { |
28 |
|
29 |
|
30 |
|
31 |
|
32 |
class DummyWebViewClient extends WebViewClient { |
33 |
} |
34 |
|
35 |
public static final String LOGTAG = "EklLauncher"; |
36 |
static final String URL = "http://omdeling.info/mobil/ekl/"; |
37 |
//static final String URL = "http://t-hoerup.dk/android_js.html"; |
38 |
|
39 |
LocationManager locMgr; |
40 |
ConnectivityManager connMgr; |
41 |
TelephonyManager telMgr; |
42 |
WebView web; |
43 |
|
44 |
PowerManager.WakeLock wakeLock; |
45 |
|
46 |
SimHandler simHandler = new SimHandler(); |
47 |
|
48 |
@Override |
49 |
public void onAttachedToWindow() { |
50 |
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD); |
51 |
super.onAttachedToWindow(); |
52 |
} |
53 |
|
54 |
/** Called when the activity is first created. */ |
55 |
@Override |
56 |
public void onCreate(Bundle savedInstanceState) { |
57 |
super.onCreate(savedInstanceState); |
58 |
|
59 |
Log.e(LOGTAG, "Creating"); |
60 |
|
61 |
getWindow().requestFeature(Window.FEATURE_PROGRESS); |
62 |
|
63 |
|
64 |
try { |
65 |
int airplane = Settings.System.getInt(this.getContentResolver(), Settings.System.AIRPLANE_MODE_ON); |
66 |
|
67 |
if (airplane > 0) { |
68 |
Settings.System.putInt(this.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0); |
69 |
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
70 |
intent.putExtra("state", false); |
71 |
sendBroadcast(intent); |
72 |
} |
73 |
} catch (Settings.SettingNotFoundException e) { |
74 |
Log.d("EKL", "Exception " + e.getMessage() ); |
75 |
} |
76 |
|
77 |
locMgr = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); |
78 |
telMgr = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); |
79 |
connMgr = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); |
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
setContentView(R.layout.main); |
86 |
web = (WebView) findViewById(R.id.web); |
87 |
|
88 |
WebSettings settings = web.getSettings(); |
89 |
|
90 |
|
91 |
settings.setJavaScriptEnabled(true); |
92 |
|
93 |
settings.setAppCacheEnabled(true); |
94 |
settings.setAppCachePath( this.getCacheDir().toString() ); |
95 |
|
96 |
settings.setJavaScriptCanOpenWindowsAutomatically(false); |
97 |
settings.setGeolocationEnabled(true); |
98 |
|
99 |
settings.setDomStorageEnabled(true); |
100 |
|
101 |
settings.setDatabaseEnabled(true); |
102 |
settings.setDatabasePath( this.getCacheDir().toString() ); |
103 |
|
104 |
|
105 |
settings.setSupportMultipleWindows(false); |
106 |
|
107 |
JsPhoneObject jsPhone = new JsPhoneObject(telMgr); |
108 |
|
109 |
web.addJavascriptInterface( jsPhone , "jsPhone"); |
110 |
|
111 |
web.setWebChromeClient(new EKLChromeClient(this) ); |
112 |
web.setWebViewClient( new DummyWebViewClient() ); // skal have en webviewclient for at kunne styre ved forlad tur |
113 |
|
114 |
|
115 |
|
116 |
|
117 |
if (!locMgr.isProviderEnabled(LocationManager.GPS_PROVIDER )) { |
118 |
//Settings.Secure.putString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "network,gps"); |
119 |
|
120 |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
121 |
builder.setMessage("GPS er slået fra på telefonen!\nSæt et flueben ved GPS i næste vindue og tryk derefter på tilbage knappen (nederst på telefonen)") |
122 |
.setPositiveButton("OK", new DialogInterface.OnClickListener() { |
123 |
public void onClick(DialogInterface dialog, int id) { |
124 |
Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS ); |
125 |
startActivity(myIntent); |
126 |
} |
127 |
}) |
128 |
.create() |
129 |
.show(); |
130 |
} |
131 |
|
132 |
} |
133 |
|
134 |
@Override |
135 |
protected void onStart() { |
136 |
super.onStart(); |
137 |
|
138 |
Log.e(LOGTAG, "Starting"); |
139 |
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); |
140 |
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "EklLauncher"); |
141 |
wakeLock.acquire(); |
142 |
|
143 |
//Hvis at tlf rebooter mit i et område uden dækning er det ikke smart at vi skal vente på data |
144 |
//simHandler.sendEmptyMessage(0); |
145 |
web.loadUrl( URL ); |
146 |
|
147 |
} |
148 |
|
149 |
void showMessage(String msg) { |
150 |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
151 |
builder.setMessage(msg) |
152 |
.setPositiveButton("OK", new DialogInterface.OnClickListener() { |
153 |
public void onClick(DialogInterface dialog, int id) { |
154 |
dialog.dismiss(); |
155 |
} |
156 |
}) |
157 |
.create() |
158 |
.show(); |
159 |
|
160 |
} |
161 |
|
162 |
@Override |
163 |
protected void onStop() { |
164 |
|
165 |
Log.e(LOGTAG, "Stopping"); |
166 |
|
167 |
if (wakeLock != null && wakeLock.isHeld()) |
168 |
wakeLock.release(); |
169 |
|
170 |
if (!web.getUrl().equals("about:blank")) |
171 |
web.loadUrl("about:blank"); |
172 |
|
173 |
super.onStop(); |
174 |
} |
175 |
|
176 |
static final int RELOAD = 1; |
177 |
static final int FLUSH = 2; |
178 |
static final int EXIT = 3; |
179 |
|
180 |
@Override |
181 |
public boolean onCreateOptionsMenu(Menu menu) { |
182 |
MenuItem item; |
183 |
item = menu.add(0, RELOAD, 0, "Genindlæs side"); |
184 |
item.setIcon( R.drawable.ic_menu_refresh ); |
185 |
|
186 |
item = menu.add(0, FLUSH, 0, "Slet cache"); |
187 |
item.setIcon( R.drawable.ic_menu_delete ); |
188 |
|
189 |
item = menu.add(0, EXIT, 0, "Forlad"); |
190 |
item.setIcon( android.R.drawable.ic_menu_close_clear_cancel); |
191 |
|
192 |
return super.onCreateOptionsMenu(menu); |
193 |
} |
194 |
|
195 |
@Override |
196 |
public boolean onMenuItemSelected(int featureId, MenuItem item) { |
197 |
switch (item.getItemId()) { |
198 |
case RELOAD: |
199 |
web.reload(); |
200 |
break; |
201 |
case FLUSH: |
202 |
web.clearCache(true); |
203 |
|
204 |
WebStorage.getInstance().deleteAllData(); |
205 |
WebStorage.getInstance().deleteOrigin(URL); |
206 |
|
207 |
web.reload(); |
208 |
break; |
209 |
case EXIT: |
210 |
this.finish(); |
211 |
break; |
212 |
} |
213 |
|
214 |
return super.onMenuItemSelected(featureId, item); |
215 |
} |
216 |
|
217 |
Handler timeoutHandler = new Handler() { |
218 |
@Override |
219 |
public void handleMessage(Message msg) { |
220 |
super.handleMessage(msg); |
221 |
web.reload(); |
222 |
} |
223 |
|
224 |
}; |
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
class SimHandler extends Handler { |
231 |
|
232 |
public void testNetwork() { |
233 |
NetworkInfo network = connMgr.getActiveNetworkInfo(); |
234 |
|
235 |
if ( (telMgr.getSimState() == TelephonyManager.SIM_STATE_READY && telMgr.getDataState() == TelephonyManager.DATA_CONNECTED) || ( network != null && network.getType() == ConnectivityManager.TYPE_WIFI) ) { |
236 |
web.loadUrl( URL ); |
237 |
} else { |
238 |
web.loadData("<html><body><h2>afventer netværksforbindelse</h2></body></html>", "text/html", "iso-8859-1"); |
239 |
this.sendEmptyMessageDelayed(0, 1000); |
240 |
} |
241 |
} |
242 |
|
243 |
@Override |
244 |
public void handleMessage(Message msg) { |
245 |
super.handleMessage(msg); |
246 |
|
247 |
testNetwork(); |
248 |
} |
249 |
}; |
250 |
|
251 |
|
252 |
} |