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