11package com .github .truefedex .apdfjs ;
22
3+ import android .annotation .SuppressLint ;
34import android .content .Context ;
5+ import android .net .Uri ;
46import android .os .Build ;
57import android .util .AttributeSet ;
6- import android .webkit .URLUtil ;
78import android .webkit .WebChromeClient ;
8- import android .webkit .WebResourceRequest ;
99import android .webkit .WebView ;
1010import 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}
0 commit comments