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 |
//Vi kan ikke styre LocationManager - kun læse nuværende tilstand |
114 |
if (!locMgr.isProviderEnabled(LocationManager.GPS_PROVIDER )) { |
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 |
web.loadUrl( URL ); |
140 |
|
141 |
} |
142 |
|
143 |
|
144 |
@Override |
145 |
protected void onStop() { |
146 |
|
147 |
Log.e(LOGTAG, "Stopping"); |
148 |
|
149 |
if (wakeLock != null && wakeLock.isHeld()) |
150 |
wakeLock.release(); |
151 |
|
152 |
if (!web.getUrl().equals("about:blank")) |
153 |
web.loadUrl("about:blank"); |
154 |
|
155 |
super.onStop(); |
156 |
} |
157 |
|
158 |
static final int RELOAD = 1; |
159 |
static final int FLUSH = 2; |
160 |
static final int EXIT = 3; |
161 |
|
162 |
@Override |
163 |
public boolean onCreateOptionsMenu(Menu menu) { |
164 |
MenuItem item; |
165 |
item = menu.add(0, RELOAD, 0, "Genindlæs side"); |
166 |
item.setIcon( R.drawable.ic_menu_refresh ); |
167 |
|
168 |
item = menu.add(0, FLUSH, 0, "Slet cache"); |
169 |
item.setIcon( R.drawable.ic_menu_delete ); |
170 |
|
171 |
item = menu.add(0, EXIT, 0, "Forlad"); |
172 |
item.setIcon( android.R.drawable.ic_menu_close_clear_cancel); |
173 |
|
174 |
return super.onCreateOptionsMenu(menu); |
175 |
} |
176 |
|
177 |
@Override |
178 |
public boolean onMenuItemSelected(int featureId, MenuItem item) { |
179 |
switch (item.getItemId()) { |
180 |
case RELOAD: |
181 |
web.reload(); |
182 |
break; |
183 |
case FLUSH: |
184 |
web.clearCache(true); |
185 |
|
186 |
WebStorage.getInstance().deleteAllData(); |
187 |
WebStorage.getInstance().deleteOrigin(URL); |
188 |
|
189 |
web.reload(); |
190 |
break; |
191 |
case EXIT: |
192 |
this.finish(); |
193 |
break; |
194 |
} |
195 |
|
196 |
return super.onMenuItemSelected(featureId, item); |
197 |
} |
198 |
|
199 |
/*void showMessage(String msg) { |
200 |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
201 |
builder.setMessage(msg) |
202 |
.setPositiveButton("OK", new DialogInterface.OnClickListener() { |
203 |
public void onClick(DialogInterface dialog, int id) { |
204 |
dialog.dismiss(); |
205 |
} |
206 |
}) |
207 |
.create() |
208 |
.show(); |
209 |
|
210 |
}*/ |
211 |
|
212 |
/* Proof-of-concept code |
213 |
Handler timeoutHandler = new Handler() { |
214 |
@Override |
215 |
public void handleMessage(Message msg) { |
216 |
super.handleMessage(msg); |
217 |
web.reload(); |
218 |
} |
219 |
|
220 |
}; |
221 |
|
222 |
|
223 |
|
224 |
class SimHandler extends Handler { |
225 |
|
226 |
public void testNetwork() { |
227 |
NetworkInfo network = connMgr.getActiveNetworkInfo(); |
228 |
|
229 |
if ( (telMgr.getSimState() == TelephonyManager.SIM_STATE_READY && telMgr.getDataState() == TelephonyManager.DATA_CONNECTED) || ( network != null && network.getType() == ConnectivityManager.TYPE_WIFI) ) { |
230 |
web.loadUrl( URL ); |
231 |
} else { |
232 |
web.loadData("<html><body><h2>afventer netværksforbindelse</h2></body></html>", "text/html", "iso-8859-1"); |
233 |
this.sendEmptyMessageDelayed(0, 1000); |
234 |
} |
235 |
} |
236 |
|
237 |
@Override |
238 |
public void handleMessage(Message msg) { |
239 |
super.handleMessage(msg); |
240 |
|
241 |
testNetwork(); |
242 |
} |
243 |
}; |
244 |
*/ |
245 |
|
246 |
} |