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 |
|
32 |
public class EKLLauncherActivity extends Activity { |
33 |
|
34 |
class CustomChromeClient extends WebChromeClient { |
35 |
|
36 |
@Override |
37 |
public void onReachedMaxAppCacheSize(long spaceNeeded, long totalUsedQuota, QuotaUpdater quotaUpdater) { |
38 |
quotaUpdater.updateQuota(totalUsedQuota + 4096); |
39 |
} |
40 |
|
41 |
|
42 |
|
43 |
public void onProgressChanged(WebView view, int progress) { |
44 |
// Activities and WebViews measure progress with different scales. |
45 |
// The progress meter will automatically disappear when we reach 100% |
46 |
EKLLauncherActivity.this.setProgress(progress * 100); |
47 |
} |
48 |
|
49 |
|
50 |
@Override |
51 |
public void onExceededDatabaseQuota(String url, String databaseIdentifier, long currentQuota, long estimatedSize, long totalUsedQuota, QuotaUpdater quotaUpdater) { |
52 |
quotaUpdater.updateQuota(estimatedSize); // altid giv tilladelse til større db quota |
53 |
} |
54 |
|
55 |
|
56 |
@Override |
57 |
public void onGeolocationPermissionsShowPrompt(String origin, Callback callback) { |
58 |
callback.invoke(origin, true, true); //altid give tilladelse til geo location |
59 |
} |
60 |
/* |
61 |
@Override |
62 |
public boolean onJsAlert(WebView view, String url, String message, JsResult result) { |
63 |
|
64 |
return super.onJsAlert(view, url, message, result); |
65 |
} |
66 |
|
67 |
@Override |
68 |
public boolean onJsConfirm(WebView view, String url, String message, JsResult result) { |
69 |
Toast.makeText(EKLLauncherActivity.this, "Oh no! " + message, Toast.LENGTH_SHORT).show(); |
70 |
return super.onJsConfirm(view, url, message, result); |
71 |
}*/ |
72 |
|
73 |
@Override |
74 |
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, final JsPromptResult result) { |
75 |
String msg = message.trim().toLowerCase(); |
76 |
|
77 |
if (msg.equals("pda identifikation mangler")) { |
78 |
result.confirm(device); |
79 |
|
80 |
timeoutHandler.sendEmptyMessageDelayed(1, 500); //efter pdaID er sendt til app'en skal vi genindlæse siden for at tvinge den til at hente data |
81 |
return true; |
82 |
} else if (msg.startsWith("indtast retur antal")) { |
83 |
|
84 |
AlertDialog.Builder alert = new AlertDialog.Builder(EKLLauncherActivity.this); |
85 |
|
86 |
//alert.setTitle("Title"); |
87 |
alert.setMessage(message); |
88 |
|
89 |
// Set an EditText view to get user input |
90 |
final EditText input = new EditText(EKLLauncherActivity.this); |
91 |
input.setInputType(InputType.TYPE_CLASS_NUMBER); |
92 |
alert.setView(input); |
93 |
|
94 |
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { |
95 |
public void onClick(DialogInterface dialog, int whichButton) { |
96 |
result.confirm(input.getText().toString()); |
97 |
} |
98 |
}); |
99 |
alert.show(); |
100 |
|
101 |
return true; |
102 |
} else { |
103 |
return super.onJsPrompt(view, url, message, defaultValue, result); |
104 |
} |
105 |
} |
106 |
|
107 |
} |
108 |
|
109 |
class DummyWebViewClient extends WebViewClient { |
110 |
} |
111 |
|
112 |
static final String URL = "http://omdeling.info/mobil/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 |
/** Called when the activity is first created. */ |
123 |
@Override |
124 |
public void onCreate(Bundle savedInstanceState) { |
125 |
super.onCreate(savedInstanceState); |
126 |
|
127 |
getWindow().requestFeature(Window.FEATURE_PROGRESS); |
128 |
|
129 |
|
130 |
try { |
131 |
int airplane = Settings.System.getInt(this.getContentResolver(), Settings.System.AIRPLANE_MODE_ON); |
132 |
if (airplane > 0) { |
133 |
Settings.System.putInt(this.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0); |
134 |
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
135 |
intent.putExtra("state", false); |
136 |
sendBroadcast(intent); |
137 |
} |
138 |
} catch (Settings.SettingNotFoundException e) { |
139 |
Log.d("EKL", "Exception " + e.getMessage() ); |
140 |
} |
141 |
|
142 |
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); |
143 |
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "EklLauncher"); |
144 |
wakeLock.acquire(); |
145 |
|
146 |
|
147 |
locMgr = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); |
148 |
telMgr = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); |
149 |
connMgr = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); |
150 |
|
151 |
device = telMgr.getLine1Number(); |
152 |
if (device == null || device.equals("")) { |
153 |
device = telMgr.getDeviceId(); |
154 |
|
155 |
if(device == null) |
156 |
device = ""; |
157 |
} |
158 |
|
159 |
|
160 |
|
161 |
setContentView(R.layout.main); |
162 |
web = (WebView) findViewById(R.id.web); |
163 |
|
164 |
WebSettings settings = web.getSettings(); |
165 |
|
166 |
|
167 |
settings.setJavaScriptEnabled(true); |
168 |
|
169 |
settings.setAppCacheEnabled(true); |
170 |
settings.setAppCachePath( this.getCacheDir().toString() ); |
171 |
|
172 |
settings.setJavaScriptCanOpenWindowsAutomatically(false); |
173 |
settings.setGeolocationEnabled(true); |
174 |
|
175 |
settings.setDomStorageEnabled(true); |
176 |
|
177 |
settings.setDatabaseEnabled(true); |
178 |
settings.setDatabasePath( this.getCacheDir().toString() ); |
179 |
|
180 |
|
181 |
settings.setSupportMultipleWindows(false); |
182 |
|
183 |
web.setWebChromeClient(new CustomChromeClient() ); |
184 |
web.setWebViewClient( new DummyWebViewClient() ); // skal have en webviewclient for at kunne styre ved forlad tur |
185 |
|
186 |
|
187 |
simHandler.sendEmptyMessage(0); |
188 |
|
189 |
|
190 |
|
191 |
|
192 |
if (!locMgr.isProviderEnabled(LocationManager.GPS_PROVIDER )) { |
193 |
//Settings.Secure.putString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "network,gps"); |
194 |
|
195 |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
196 |
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)") |
197 |
.setPositiveButton("OK", new DialogInterface.OnClickListener() { |
198 |
public void onClick(DialogInterface dialog, int id) { |
199 |
Intent myIntent = new Intent( Settings.ACTION_SECURITY_SETTINGS ); |
200 |
startActivity(myIntent); |
201 |
} |
202 |
}) |
203 |
.create() |
204 |
.show(); |
205 |
} |
206 |
} |
207 |
|
208 |
@Override |
209 |
protected void onDestroy() { |
210 |
super.onDestroy(); |
211 |
wakeLock.release(); |
212 |
} |
213 |
|
214 |
@Override |
215 |
public boolean onCreateOptionsMenu(Menu menu) { |
216 |
MenuItem item; |
217 |
item = menu.add(0, 1, 0, "Genindlæs side"); |
218 |
item.setIcon( R.drawable.ic_menu_refresh ); |
219 |
|
220 |
item = menu.add(0, 2, 0, "Slet cache"); |
221 |
item.setIcon( R.drawable.ic_menu_delete ); |
222 |
return super.onCreateOptionsMenu(menu); |
223 |
} |
224 |
|
225 |
@Override |
226 |
public boolean onMenuItemSelected(int featureId, MenuItem item) { |
227 |
switch (item.getItemId()) { |
228 |
case 1: |
229 |
web.reload(); |
230 |
break; |
231 |
case 2: |
232 |
WebStorage.getInstance().deleteAllData(); |
233 |
web.clearCache(true); |
234 |
web.reload(); |
235 |
break; |
236 |
} |
237 |
return super.onMenuItemSelected(featureId, item); |
238 |
} |
239 |
|
240 |
Handler timeoutHandler = new Handler() { |
241 |
@Override |
242 |
public void handleMessage(Message msg) { |
243 |
super.handleMessage(msg); |
244 |
web.reload(); |
245 |
} |
246 |
|
247 |
}; |
248 |
|
249 |
|
250 |
|
251 |
SimHandler simHandler = new SimHandler(); |
252 |
|
253 |
class SimHandler extends Handler { |
254 |
|
255 |
public void testNetwork() { |
256 |
NetworkInfo network = connMgr.getActiveNetworkInfo(); |
257 |
|
258 |
if ( (telMgr.getSimState() == TelephonyManager.SIM_STATE_READY && telMgr.getDataState() == TelephonyManager.DATA_CONNECTED) || ( network != null && network.getType() == ConnectivityManager.TYPE_WIFI) ) { |
259 |
web.loadUrl( URL ); |
260 |
} else { |
261 |
web.loadData("<html><body><h2>afventer netværksforbindelse</h2></body></html>", "text/html", "iso-8859-1"); |
262 |
this.sendEmptyMessageDelayed(0, 1000); |
263 |
} |
264 |
} |
265 |
|
266 |
@Override |
267 |
public void handleMessage(Message msg) { |
268 |
super.handleMessage(msg); |
269 |
|
270 |
testNetwork(); |
271 |
} |
272 |
}; |
273 |
|
274 |
|
275 |
} |