1 |
package dk.thoerup.ekllauncher; |
2 |
|
3 |
import android.annotation.SuppressLint; |
4 |
import android.app.Activity; |
5 |
import android.app.AlertDialog; |
6 |
import android.content.Context; |
7 |
import android.content.DialogInterface; |
8 |
import android.content.Intent; |
9 |
import android.location.LocationManager; |
10 |
import android.net.ConnectivityManager; |
11 |
import android.net.NetworkInfo; |
12 |
import android.os.Bundle; |
13 |
import android.os.Handler; |
14 |
import android.os.Message; |
15 |
import android.os.PowerManager; |
16 |
import android.provider.Settings; |
17 |
import android.telephony.TelephonyManager; |
18 |
import android.text.InputType; |
19 |
import android.util.Log; |
20 |
import android.view.Menu; |
21 |
import android.view.MenuItem; |
22 |
import android.view.Window; |
23 |
import android.webkit.GeolocationPermissions.Callback; |
24 |
import android.webkit.JsPromptResult; |
25 |
import android.webkit.WebChromeClient; |
26 |
import android.webkit.WebSettings; |
27 |
import android.webkit.WebStorage; |
28 |
import android.webkit.WebStorage.QuotaUpdater; |
29 |
import android.webkit.WebView; |
30 |
import android.webkit.WebViewClient; |
31 |
import android.widget.EditText; |
32 |
import android.widget.Toast; |
33 |
|
34 |
public class EKLLauncherActivity extends Activity { |
35 |
|
36 |
class CustomChromeClient extends WebChromeClient { |
37 |
|
38 |
@Override |
39 |
public void onReachedMaxAppCacheSize(long spaceNeeded, long totalUsedQuota, QuotaUpdater quotaUpdater) { |
40 |
quotaUpdater.updateQuota(totalUsedQuota + 4096); |
41 |
} |
42 |
|
43 |
|
44 |
|
45 |
public void onProgressChanged(WebView view, int progress) { |
46 |
// Activities and WebViews measure progress with different scales. |
47 |
// The progress meter will automatically disappear when we reach 100% |
48 |
EKLLauncherActivity.this.setProgress(progress * 100); |
49 |
} |
50 |
|
51 |
|
52 |
@Override |
53 |
public void onExceededDatabaseQuota(String url, String databaseIdentifier, long currentQuota, long estimatedSize, long totalUsedQuota, QuotaUpdater quotaUpdater) { |
54 |
quotaUpdater.updateQuota(estimatedSize); // altid giv tilladelse til større db quota |
55 |
} |
56 |
|
57 |
|
58 |
@Override |
59 |
public void onGeolocationPermissionsShowPrompt(String origin, Callback callback) { |
60 |
callback.invoke(origin, true, true); //altid give tilladelse til geo location |
61 |
} |
62 |
/* |
63 |
@Override |
64 |
public boolean onJsAlert(WebView view, String url, String message, JsResult result) { |
65 |
|
66 |
return super.onJsAlert(view, url, message, result); |
67 |
} |
68 |
|
69 |
@Override |
70 |
public boolean onJsConfirm(WebView view, String url, String message, JsResult result) { |
71 |
Toast.makeText(EKLLauncherActivity.this, "Oh no! " + message, Toast.LENGTH_SHORT).show(); |
72 |
return super.onJsConfirm(view, url, message, result); |
73 |
}*/ |
74 |
|
75 |
@Override |
76 |
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, final JsPromptResult result) { |
77 |
String msg = message.trim().toLowerCase(); |
78 |
|
79 |
if (msg.equals("ekl identifikation mangler")) { |
80 |
if (device != null && device.equals("")==false) { |
81 |
result.confirm(device); |
82 |
|
83 |
timeoutHandler.sendEmptyMessageDelayed(1, 500); //efter pdaID er sendt til app'en skal vi genindlæse siden for at tvinge den til at hente data |
84 |
return true; |
85 |
} else { |
86 |
//Hvis vi ikke kunne aflæse device, spørg user via en custom Input Dialog |
87 |
//Dialogen kan kun tage imod tal & |
88 |
//når at vi får en confirm på den tvinger vi ud i en reload af siden |
89 |
|
90 |
AlertDialog.Builder alert = new AlertDialog.Builder(EKLLauncherActivity.this); |
91 |
|
92 |
//alert.setTitle("Title"); |
93 |
alert.setMessage(message); |
94 |
|
95 |
// Set an EditText view to get user input |
96 |
final EditText input = new EditText(EKLLauncherActivity.this); |
97 |
input.setInputType(InputType.TYPE_CLASS_NUMBER); |
98 |
alert.setView(input); |
99 |
|
100 |
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { |
101 |
public void onClick(DialogInterface dialog, int whichButton) { |
102 |
result.confirm(input.getText().toString()); |
103 |
timeoutHandler.sendEmptyMessageDelayed(1, 500); |
104 |
} |
105 |
}); |
106 |
alert.show(); |
107 |
|
108 |
return true; |
109 |
|
110 |
} |
111 |
} else { |
112 |
return super.onJsPrompt(view, url, message, defaultValue, result); |
113 |
} |
114 |
} |
115 |
} |
116 |
|
117 |
class DummyWebViewClient extends WebViewClient { |
118 |
} |
119 |
|
120 |
static final String LOGTAG = "EklLauncher"; |
121 |
static final String URL = "http://omdeling.info/mobil_gpstrack/ekl/"; |
122 |
|
123 |
LocationManager locMgr; |
124 |
ConnectivityManager connMgr; |
125 |
TelephonyManager telMgr; |
126 |
WebView web; |
127 |
String device = ""; |
128 |
|
129 |
PowerManager.WakeLock wakeLock; |
130 |
|
131 |
SimHandler simHandler = new SimHandler(); |
132 |
|
133 |
/** Called when the activity is first created. */ |
134 |
@SuppressLint("SetJavaScriptEnabled") |
135 |
@Override |
136 |
public void onCreate(Bundle savedInstanceState) { |
137 |
super.onCreate(savedInstanceState); |
138 |
|
139 |
Log.e(LOGTAG, "Starting"); |
140 |
|
141 |
getWindow().requestFeature(Window.FEATURE_PROGRESS); |
142 |
|
143 |
|
144 |
try { |
145 |
int airplane = Settings.System.getInt(this.getContentResolver(), Settings.System.AIRPLANE_MODE_ON); |
146 |
|
147 |
if (airplane > 0) { |
148 |
Settings.System.putInt(this.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0); |
149 |
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
150 |
intent.putExtra("state", false); |
151 |
sendBroadcast(intent); |
152 |
} |
153 |
} catch (Settings.SettingNotFoundException e) { |
154 |
Log.d("EKL", "Exception " + e.getMessage() ); |
155 |
} |
156 |
|
157 |
locMgr = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); |
158 |
telMgr = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); |
159 |
connMgr = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); |
160 |
|
161 |
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); |
162 |
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "EklLauncher"); |
163 |
wakeLock.acquire(); |
164 |
|
165 |
|
166 |
|
167 |
// |
168 |
|
169 |
device = telMgr.getLine1Number(); |
170 |
if (device == null || device.equals("")) { |
171 |
device = telMgr.getSimSerialNumber(); |
172 |
|
173 |
if(device == null) |
174 |
device = ""; |
175 |
} |
176 |
Log.i(LOGTAG, "Device Line1: " + telMgr.getLine1Number()); |
177 |
Log.i(LOGTAG, "Device simserial: " + telMgr.getSimSerialNumber()); |
178 |
Log.i(LOGTAG, "Device subscriber: " + telMgr.getSubscriberId()); |
179 |
Log.i(LOGTAG, "Device deviceid: " + telMgr.getDeviceId());//IMEI |
180 |
|
181 |
|
182 |
Toast.makeText(EKLLauncherActivity.this, "Device: "+device, Toast.LENGTH_SHORT).show(); |
183 |
|
184 |
|
185 |
setContentView(R.layout.main); |
186 |
web = (WebView) findViewById(R.id.web); |
187 |
|
188 |
WebSettings settings = web.getSettings(); |
189 |
|
190 |
|
191 |
settings.setJavaScriptEnabled(true); |
192 |
|
193 |
settings.setAppCacheEnabled(true); |
194 |
settings.setAppCachePath( this.getCacheDir().toString() ); |
195 |
|
196 |
settings.setJavaScriptCanOpenWindowsAutomatically(false); |
197 |
settings.setGeolocationEnabled(true); |
198 |
|
199 |
settings.setDomStorageEnabled(true); |
200 |
|
201 |
settings.setDatabaseEnabled(true); |
202 |
settings.setDatabasePath( this.getCacheDir().toString() ); |
203 |
|
204 |
|
205 |
settings.setSupportMultipleWindows(false); |
206 |
|
207 |
web.setWebChromeClient(new CustomChromeClient() ); |
208 |
web.setWebViewClient( new DummyWebViewClient() ); // skal have en webviewclient for at kunne styre ved forlad tur |
209 |
|
210 |
|
211 |
|
212 |
//Hvis at tlf rebooter mit i et område uden dækning er det ikke smart at vi skal vente på data |
213 |
//simHandler.sendEmptyMessage(0); |
214 |
web.loadUrl( URL ); |
215 |
|
216 |
|
217 |
if (!locMgr.isProviderEnabled(LocationManager.GPS_PROVIDER )) { |
218 |
//Settings.Secure.putString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "network,gps"); |
219 |
|
220 |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
221 |
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)") |
222 |
.setPositiveButton("OK", new DialogInterface.OnClickListener() { |
223 |
public void onClick(DialogInterface dialog, int id) { |
224 |
Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS ); |
225 |
startActivity(myIntent); |
226 |
} |
227 |
}) |
228 |
.create() |
229 |
.show(); |
230 |
} |
231 |
|
232 |
} |
233 |
|
234 |
void showMessage(String msg) { |
235 |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
236 |
builder.setMessage(msg) |
237 |
.setPositiveButton("OK", new DialogInterface.OnClickListener() { |
238 |
public void onClick(DialogInterface dialog, int id) { |
239 |
dialog.dismiss(); |
240 |
} |
241 |
}) |
242 |
.create() |
243 |
.show(); |
244 |
|
245 |
} |
246 |
|
247 |
@Override |
248 |
protected void onDestroy() { |
249 |
super.onDestroy(); |
250 |
wakeLock.release(); |
251 |
} |
252 |
|
253 |
@Override |
254 |
public boolean onCreateOptionsMenu(Menu menu) { |
255 |
MenuItem item; |
256 |
item = menu.add(0, 1, 0, "Genindlæs side"); |
257 |
item.setIcon( R.drawable.ic_menu_refresh ); |
258 |
|
259 |
item = menu.add(0, 2, 0, "Slet cache"); |
260 |
item.setIcon( R.drawable.ic_menu_delete ); |
261 |
return super.onCreateOptionsMenu(menu); |
262 |
} |
263 |
|
264 |
@Override |
265 |
public boolean onMenuItemSelected(int featureId, MenuItem item) { |
266 |
switch (item.getItemId()) { |
267 |
case 1: |
268 |
web.reload(); |
269 |
break; |
270 |
case 2: |
271 |
web.clearCache(true); |
272 |
|
273 |
WebStorage.getInstance().deleteAllData(); |
274 |
WebStorage.getInstance().deleteOrigin(URL); |
275 |
|
276 |
web.reload(); |
277 |
break; |
278 |
} |
279 |
return super.onMenuItemSelected(featureId, item); |
280 |
} |
281 |
|
282 |
Handler timeoutHandler = new Handler() { |
283 |
@Override |
284 |
public void handleMessage(Message msg) { |
285 |
super.handleMessage(msg); |
286 |
web.reload(); |
287 |
} |
288 |
|
289 |
}; |
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
class SimHandler extends Handler { |
296 |
|
297 |
public void testNetwork() { |
298 |
NetworkInfo network = connMgr.getActiveNetworkInfo(); |
299 |
|
300 |
if ( (telMgr.getSimState() == TelephonyManager.SIM_STATE_READY && telMgr.getDataState() == TelephonyManager.DATA_CONNECTED) || ( network != null && network.getType() == ConnectivityManager.TYPE_WIFI) ) { |
301 |
web.loadUrl( URL ); |
302 |
} else { |
303 |
web.loadData("<html><body><h2>afventer netværksforbindelse</h2></body></html>", "text/html", "iso-8859-1"); |
304 |
this.sendEmptyMessageDelayed(0, 1000); |
305 |
} |
306 |
} |
307 |
|
308 |
@Override |
309 |
public void handleMessage(Message msg) { |
310 |
super.handleMessage(msg); |
311 |
|
312 |
testNetwork(); |
313 |
} |
314 |
}; |
315 |
|
316 |
|
317 |
} |