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