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 |
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 if (msg.startsWith("indtast retur antal")) { |
85 |
|
86 |
AlertDialog.Builder alert = new AlertDialog.Builder(EKLLauncherActivity.this); |
87 |
|
88 |
//alert.setTitle("Title"); |
89 |
alert.setMessage(message); |
90 |
|
91 |
// Set an EditText view to get user input |
92 |
final EditText input = new EditText(EKLLauncherActivity.this); |
93 |
input.setInputType(InputType.TYPE_CLASS_NUMBER); |
94 |
alert.setView(input); |
95 |
|
96 |
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { |
97 |
public void onClick(DialogInterface dialog, int whichButton) { |
98 |
result.confirm(input.getText().toString()); |
99 |
} |
100 |
}); |
101 |
alert.show(); |
102 |
|
103 |
return true; |
104 |
} else { |
105 |
return super.onJsPrompt(view, url, message, defaultValue, result); |
106 |
} |
107 |
} |
108 |
} |
109 |
|
110 |
class DummyWebViewClient extends WebViewClient { |
111 |
} |
112 |
|
113 |
static final String LOGTAG = "EklLauncher"; |
114 |
static final String URL = "http://omdeling.info/mobil_gpstrack/ekl/login.php"; |
115 |
|
116 |
LocationManager locMgr; |
117 |
ConnectivityManager connMgr; |
118 |
TelephonyManager telMgr; |
119 |
WebView web; |
120 |
String device = ""; |
121 |
|
122 |
PowerManager.WakeLock wakeLock; |
123 |
|
124 |
SimHandler simHandler = new SimHandler(); |
125 |
|
126 |
/** Called when the activity is first created. */ |
127 |
@SuppressLint("SetJavaScriptEnabled") |
128 |
@Override |
129 |
public void onCreate(Bundle savedInstanceState) { |
130 |
super.onCreate(savedInstanceState); |
131 |
|
132 |
Log.e(LOGTAG, "Starting"); |
133 |
|
134 |
getWindow().requestFeature(Window.FEATURE_PROGRESS); |
135 |
|
136 |
|
137 |
try { |
138 |
int airplane = Settings.System.getInt(this.getContentResolver(), Settings.System.AIRPLANE_MODE_ON); |
139 |
|
140 |
if (airplane > 0) { |
141 |
Settings.System.putInt(this.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0); |
142 |
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
143 |
intent.putExtra("state", false); |
144 |
sendBroadcast(intent); |
145 |
} |
146 |
} catch (Settings.SettingNotFoundException e) { |
147 |
Log.d("EKL", "Exception " + e.getMessage() ); |
148 |
} |
149 |
|
150 |
locMgr = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); |
151 |
telMgr = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); |
152 |
connMgr = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); |
153 |
|
154 |
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); |
155 |
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "EklLauncher"); |
156 |
wakeLock.acquire(); |
157 |
|
158 |
|
159 |
|
160 |
device = telMgr.getLine1Number(); |
161 |
if (device == null || device.equals("")) { |
162 |
device = telMgr.getSimSerialNumber(); |
163 |
|
164 |
if(device == null) |
165 |
device = ""; |
166 |
} |
167 |
|
168 |
Toast.makeText(EKLLauncherActivity.this, "Device: "+device, Toast.LENGTH_SHORT).show(); |
169 |
|
170 |
|
171 |
|
172 |
setContentView(R.layout.main); |
173 |
web = (WebView) findViewById(R.id.web); |
174 |
|
175 |
WebSettings settings = web.getSettings(); |
176 |
|
177 |
|
178 |
settings.setJavaScriptEnabled(true); |
179 |
|
180 |
settings.setAppCacheEnabled(true); |
181 |
settings.setAppCachePath( this.getCacheDir().toString() ); |
182 |
|
183 |
settings.setJavaScriptCanOpenWindowsAutomatically(false); |
184 |
settings.setGeolocationEnabled(true); |
185 |
|
186 |
settings.setDomStorageEnabled(true); |
187 |
|
188 |
settings.setDatabaseEnabled(true); |
189 |
settings.setDatabasePath( this.getCacheDir().toString() ); |
190 |
|
191 |
|
192 |
settings.setSupportMultipleWindows(false); |
193 |
|
194 |
web.setWebChromeClient(new CustomChromeClient() ); |
195 |
web.setWebViewClient( new DummyWebViewClient() ); // skal have en webviewclient for at kunne styre ved forlad tur |
196 |
|
197 |
//Hvis at tlf rebooter mit i et område uden dækning er det ikke smart at vi skal vente på data |
198 |
//simHandler.sendEmptyMessage(0); |
199 |
web.loadUrl( URL ); |
200 |
|
201 |
|
202 |
if (!locMgr.isProviderEnabled(LocationManager.GPS_PROVIDER )) { |
203 |
//Settings.Secure.putString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "network,gps"); |
204 |
|
205 |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
206 |
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)") |
207 |
.setPositiveButton("OK", new DialogInterface.OnClickListener() { |
208 |
public void onClick(DialogInterface dialog, int id) { |
209 |
Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS ); |
210 |
startActivity(myIntent); |
211 |
} |
212 |
}) |
213 |
.create() |
214 |
.show(); |
215 |
} |
216 |
|
217 |
} |
218 |
|
219 |
void showMessage(String msg) { |
220 |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
221 |
builder.setMessage(msg) |
222 |
.setPositiveButton("OK", new DialogInterface.OnClickListener() { |
223 |
public void onClick(DialogInterface dialog, int id) { |
224 |
dialog.dismiss(); |
225 |
} |
226 |
}) |
227 |
.create() |
228 |
.show(); |
229 |
|
230 |
} |
231 |
|
232 |
@Override |
233 |
protected void onDestroy() { |
234 |
super.onDestroy(); |
235 |
wakeLock.release(); |
236 |
} |
237 |
|
238 |
@Override |
239 |
public boolean onCreateOptionsMenu(Menu menu) { |
240 |
MenuItem item; |
241 |
item = menu.add(0, 1, 0, "Genindlæs side"); |
242 |
item.setIcon( R.drawable.ic_menu_refresh ); |
243 |
|
244 |
item = menu.add(0, 2, 0, "Slet cache"); |
245 |
item.setIcon( R.drawable.ic_menu_delete ); |
246 |
return super.onCreateOptionsMenu(menu); |
247 |
} |
248 |
|
249 |
@Override |
250 |
public boolean onMenuItemSelected(int featureId, MenuItem item) { |
251 |
switch (item.getItemId()) { |
252 |
case 1: |
253 |
web.reload(); |
254 |
break; |
255 |
case 2: |
256 |
WebStorage.getInstance().deleteAllData(); |
257 |
web.clearCache(true); |
258 |
web.reload(); |
259 |
break; |
260 |
} |
261 |
return super.onMenuItemSelected(featureId, item); |
262 |
} |
263 |
|
264 |
Handler timeoutHandler = new Handler() { |
265 |
@Override |
266 |
public void handleMessage(Message msg) { |
267 |
super.handleMessage(msg); |
268 |
web.reload(); |
269 |
} |
270 |
|
271 |
}; |
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
class SimHandler extends Handler { |
278 |
|
279 |
public void testNetwork() { |
280 |
NetworkInfo network = connMgr.getActiveNetworkInfo(); |
281 |
|
282 |
if ( (telMgr.getSimState() == TelephonyManager.SIM_STATE_READY && telMgr.getDataState() == TelephonyManager.DATA_CONNECTED) || ( network != null && network.getType() == ConnectivityManager.TYPE_WIFI) ) { |
283 |
web.loadUrl( URL ); |
284 |
} else { |
285 |
web.loadData("<html><body><h2>afventer netværksforbindelse</h2></body></html>", "text/html", "iso-8859-1"); |
286 |
this.sendEmptyMessageDelayed(0, 1000); |
287 |
} |
288 |
} |
289 |
|
290 |
@Override |
291 |
public void handleMessage(Message msg) { |
292 |
super.handleMessage(msg); |
293 |
|
294 |
testNetwork(); |
295 |
} |
296 |
}; |
297 |
|
298 |
|
299 |
} |