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.text.InputType; |
18 |
import android.util.Log; |
19 |
import android.view.Menu; |
20 |
import android.view.MenuItem; |
21 |
import android.view.Window; |
22 |
import android.webkit.GeolocationPermissions.Callback; |
23 |
import android.webkit.JsPromptResult; |
24 |
import android.webkit.WebChromeClient; |
25 |
import android.webkit.WebSettings; |
26 |
import android.webkit.WebStorage; |
27 |
import android.webkit.WebStorage.QuotaUpdater; |
28 |
import android.webkit.WebView; |
29 |
import android.webkit.WebViewClient; |
30 |
import android.widget.EditText; |
31 |
import android.widget.Toast; |
32 |
|
33 |
public class EKLLauncherActivity extends Activity { |
34 |
|
35 |
class CustomChromeClient extends WebChromeClient { |
36 |
|
37 |
@Override |
38 |
public void onReachedMaxAppCacheSize(long spaceNeeded, long totalUsedQuota, QuotaUpdater quotaUpdater) { |
39 |
quotaUpdater.updateQuota(totalUsedQuota + 4096); |
40 |
} |
41 |
|
42 |
|
43 |
|
44 |
public void onProgressChanged(WebView view, int progress) { |
45 |
// Activities and WebViews measure progress with different scales. |
46 |
// The progress meter will automatically disappear when we reach 100% |
47 |
EKLLauncherActivity.this.setProgress(progress * 100); |
48 |
} |
49 |
|
50 |
|
51 |
@Override |
52 |
public void onExceededDatabaseQuota(String url, String databaseIdentifier, long currentQuota, long estimatedSize, long totalUsedQuota, QuotaUpdater quotaUpdater) { |
53 |
quotaUpdater.updateQuota(estimatedSize); // altid giv tilladelse til større db quota |
54 |
} |
55 |
|
56 |
|
57 |
@Override |
58 |
public void onGeolocationPermissionsShowPrompt(String origin, Callback callback) { |
59 |
callback.invoke(origin, true, true); //altid give tilladelse til geo location |
60 |
} |
61 |
/* |
62 |
@Override |
63 |
public boolean onJsAlert(WebView view, String url, String message, JsResult result) { |
64 |
|
65 |
return super.onJsAlert(view, url, message, result); |
66 |
} |
67 |
|
68 |
@Override |
69 |
public boolean onJsConfirm(WebView view, String url, String message, JsResult result) { |
70 |
Toast.makeText(EKLLauncherActivity.this, "Oh no! " + message, Toast.LENGTH_SHORT).show(); |
71 |
return super.onJsConfirm(view, url, message, result); |
72 |
}*/ |
73 |
|
74 |
@Override |
75 |
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, final JsPromptResult result) { |
76 |
String msg = message.trim().toLowerCase(); |
77 |
|
78 |
if (msg.equals("ekl identifikation mangler")) { |
79 |
if (device != null && device.equals("")==false) { |
80 |
result.confirm(device); |
81 |
|
82 |
timeoutHandler.sendEmptyMessageDelayed(1, 500); //efter pdaID er sendt til app'en skal vi genindlæse siden for at tvinge den til at hente data |
83 |
return true; |
84 |
} else { |
85 |
//Hvis vi ikke kunne aflæse device, spørg user via en custom Input Dialog |
86 |
//Dialogen kan kun tage imod tal & |
87 |
//når at vi får en confirm på den tvinger vi ud i en reload af siden |
88 |
|
89 |
AlertDialog.Builder alert = new AlertDialog.Builder(EKLLauncherActivity.this); |
90 |
|
91 |
//alert.setTitle("Title"); |
92 |
alert.setMessage(message); |
93 |
|
94 |
// Set an EditText view to get user input |
95 |
final EditText input = new EditText(EKLLauncherActivity.this); |
96 |
input.setInputType(InputType.TYPE_CLASS_NUMBER); |
97 |
alert.setView(input); |
98 |
|
99 |
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { |
100 |
public void onClick(DialogInterface dialog, int whichButton) { |
101 |
result.confirm(input.getText().toString()); |
102 |
timeoutHandler.sendEmptyMessageDelayed(1, 500); |
103 |
} |
104 |
}); |
105 |
alert.show(); |
106 |
|
107 |
return true; |
108 |
|
109 |
} |
110 |
} else { |
111 |
return super.onJsPrompt(view, url, message, defaultValue, result); |
112 |
} |
113 |
} |
114 |
} |
115 |
|
116 |
class DummyWebViewClient extends WebViewClient { |
117 |
} |
118 |
|
119 |
static final String LOGTAG = "EklLauncher"; |
120 |
static final String URL = "http://omdeling.info/mobil_gpstrack/ekl/"; |
121 |
|
122 |
LocationManager locMgr; |
123 |
ConnectivityManager connMgr; |
124 |
TelephonyManager telMgr; |
125 |
WebView web; |
126 |
String device = ""; |
127 |
|
128 |
PowerManager.WakeLock wakeLock; |
129 |
|
130 |
SimHandler simHandler = new SimHandler(); |
131 |
|
132 |
/** Called when the activity is first created. */ |
133 |
@Override |
134 |
public void onCreate(Bundle savedInstanceState) { |
135 |
super.onCreate(savedInstanceState); |
136 |
|
137 |
Log.e(LOGTAG, "Starting"); |
138 |
|
139 |
getWindow().requestFeature(Window.FEATURE_PROGRESS); |
140 |
|
141 |
|
142 |
try { |
143 |
int airplane = Settings.System.getInt(this.getContentResolver(), Settings.System.AIRPLANE_MODE_ON); |
144 |
|
145 |
if (airplane > 0) { |
146 |
Settings.System.putInt(this.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0); |
147 |
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
148 |
intent.putExtra("state", false); |
149 |
sendBroadcast(intent); |
150 |
} |
151 |
} catch (Settings.SettingNotFoundException e) { |
152 |
Log.d("EKL", "Exception " + e.getMessage() ); |
153 |
} |
154 |
|
155 |
locMgr = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); |
156 |
telMgr = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); |
157 |
connMgr = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); |
158 |
|
159 |
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); |
160 |
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "EklLauncher"); |
161 |
wakeLock.acquire(); |
162 |
|
163 |
|
164 |
|
165 |
// |
166 |
|
167 |
device = telMgr.getLine1Number(); |
168 |
/* if (device == null || device.equals("")) { |
169 |
device = telMgr.getSimSerialNumber(); |
170 |
|
171 |
if(device == null) |
172 |
device = ""; |
173 |
}*/ |
174 |
if(device == null) |
175 |
device = ""; |
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 |
} |