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