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