Skip to content

Commit 18d70c7

Browse files
committed
bugfix & refactoring
1 parent 4fd0a09 commit 18d70c7

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed
Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
package com.github.truefedex.apdfjs;
22

3+
import android.annotation.SuppressLint;
34
import android.content.Context;
5+
import android.net.Uri;
46
import android.os.Build;
57
import android.util.AttributeSet;
6-
import android.webkit.URLUtil;
78
import android.webkit.WebChromeClient;
8-
import android.webkit.WebResourceRequest;
99
import android.webkit.WebView;
1010
import android.webkit.WebViewClient;
11-
12-
import java.net.URLEncoder;
11+
import android.widget.FrameLayout;
1312

1413
/**
1514
* Created by PDT on 02.06.2017.
1615
*/
1716

18-
public class PDFJSView extends WebView {
17+
public class PDFJSView extends FrameLayout {
18+
private WebView webView;
19+
1920
public PDFJSView(Context context) {
2021
super(context);
2122
init();
@@ -31,35 +32,34 @@ public PDFJSView(Context context, AttributeSet attrs, int defStyleAttr) {
3132
init();
3233
}
3334

35+
@SuppressLint("SetJavaScriptEnabled")
3436
private void init() {
35-
getSettings().setJavaScriptEnabled(true);
36-
getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
37-
getSettings().setAllowFileAccess(true);
37+
webView = new WebView(getContext());
38+
addView(webView);
39+
webView.getSettings().setJavaScriptEnabled(true);
40+
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
41+
webView.getSettings().setAllowFileAccess(true);
3842
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
39-
getSettings().setAllowFileAccessFromFileURLs(true);
40-
getSettings().setAllowUniversalAccessFromFileURLs(true);
43+
webView.getSettings().setAllowFileAccessFromFileURLs(true);
44+
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
4145
}
4246

43-
setWebViewClient(new WebViewClient(){
47+
webView.setWebViewClient(new WebViewClient(){
4448
@Override
4549
public boolean shouldOverrideUrlLoading(WebView view, String url) {
4650
return super.shouldOverrideUrlLoading(view, url);
4751
}
4852
});
49-
setWebChromeClient(new WebChromeClient());
53+
webView.setWebChromeClient(new WebChromeClient());
5054
}
5155

5256
public void loadFromAssets(String pdfAssetsPath) {
53-
loadUrl("file:///android_asset/pdfjs-1.7.225-dist/web/viewer.html?file=file:///android_asset/" +
54-
URLEncoder.encode(pdfAssetsPath));
57+
webView.loadUrl("file:///android_asset/pdfjs-1.7.225-dist/web/viewer.html?file=file:///android_asset/" +
58+
Uri.encode(pdfAssetsPath, "UTF-8"));
5559
}
5660

5761
public void loadFromFile(String pdfFilePath) {
58-
loadUrl("file:///android_asset/pdfjs-1.7.225-dist/web/viewer.html?file=file://" +
59-
URLEncoder.encode(pdfFilePath));
60-
}
61-
62-
public void loadFromURL(String pdfURL) {
63-
loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdfURL);
62+
webView.loadUrl("file:///android_asset/pdfjs-1.7.225-dist/web/viewer.html?file=file://" +
63+
Uri.encode(pdfFilePath, "UTF-8"));
6464
}
6565
}

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.3.2'
8+
classpath 'com.android.tools.build:gradle:2.3.3'
99

1010
// NOTE: Do not place your application dependencies here; they belong
1111
// in the individual module build.gradle files

0 commit comments

Comments
 (0)