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