Repository navigation
Expand file tree
/
Copy pathTextReflowEngine.java
More file actions
73 lines (61 loc) · 2.41 KB
/
Copy pathTextReflowEngine.java
File metadata and controls
73 lines (61 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.viaadvancedbrowser.reflow;
import android.webkit.WebView;
public class TextReflowEngine {
private WebView webView;
private float currentZoomLevel = 1.0f;
private boolean isReflowEnabled = true;
public TextReflowEngine(WebView webView) {
this.webView = webView;
setupReflow();
}
private void setupReflow() {
injectReflowScript();
}
private void injectReflowScript() {
String reflowScript =
"javascript:(function() {" +
"var meta = document.createElement('meta');" +
"meta.name = 'viewport';" +
"meta.content = 'width=device-width, initial-scale=1.0, maximum-scale=5.0, user-scalable=yes';" +
"document.getElementsByTagName('head')[0].appendChild(meta);" +
"" +
"function reflowText() {" +
" var elements = document.querySelectorAll('p, span, div, article, section');" +
" for (var i = 0; i < elements.length; i++) {" +
" var el = elements[i];" +
" el.style.wordWrap = 'break-word';" +
" el.style.overflowWrap = 'break-word';" +
" el.style.maxWidth = '100%';" +
" }" +
"}" +
"reflowText();" +
"window.addEventListener('resize', reflowText);" +
"window.addEventListener('orientationchange', reflowText);" +
"})();";
webView.evaluateJavascript(reflowScript, null);
}
public void setZoomLevel(float zoomLevel) {
this.currentZoomLevel = zoomLevel;
webView.getSettings().setTextZoom((int)(zoomLevel * 100));
if (isReflowEnabled) {
adjustLayoutForZoom(zoomLevel);
}
}
private void adjustLayoutForZoom(float zoomLevel) {
String adjustScript = String.format(
"javascript:(function() {" +
"document.body.style.width = '%f%%';" +
"var images = document.getElementsByTagName('img');" +
"for (var i = 0; i < images.length; i++) {" +
" images[i].style.maxWidth = '100%%';" +
" images[i].style.height = 'auto';" +
"}" +
"})();",
100 / zoomLevel
);
webView.evaluateJavascript(adjustScript, null);
}
public void enableReflow(boolean enable) {
this.isReflowEnabled = enable;
}
}