Search in sources :

Example 1 with Callback

use of android.webkit.GeolocationPermissions.Callback in project cyborg-core by nu-art.

the class CyborgWebView method setupClients.

private void setupClients() {
    setDownloadListener(new DownloadListener() {

        private String downloadingNow;

        @Override
        public void onDownloadStart(final String url, final String userAgent, final String contentDisposition, final String mimeType, final long contentLength) {
            logInfo("DEBUG-LOG: onDownloadStart... url: " + url + "  userAgent: " + userAgent + "  contentDisposition: " + contentDisposition + "  mimetype: " + mimeType + "  contentLength: " + contentLength);
            if (downloadingNow != null) {
                logWarning("DOWNLOAD IN PROGRESS: " + downloadingNow + "... NOT DOWNLOADING FILE FROM NEW URL: " + url);
                return;
            }
            if (downloadHandler == null) {
                if (getContext() instanceof Application) {
                    logWarning("APPLICATION CONTEXT FOUND!!! NOT DOWNLOADING FILE FROM: " + url);
                    return;
                }
                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setData(Uri.parse(url));
                getContext().startActivity(i);
                return;
            }
            HandlerThread fileDownloader = new HandlerThread("File Downloader: " + url);
            fileDownloader.start();
            Handler handler = new Handler(fileDownloader.getLooper());
            downloadingNow = url;
            handler.post(new Runnable() {

                @Override
                public void run() {
                    FileOutputStream os = null;
                    InputStream is = null;
                    String fileName = contentDisposition;
                    if (fileName == null)
                        fileName = "unknown-file";
                    int index = fileName.indexOf("filename=\"");
                    if (index != -1)
                        fileName = fileName.substring(index + "filename=\"".length(), fileName.length() - 1);
                    fileName = fileName.replaceAll("[\\*/:<>\\?\\\\\\|\\+,\\.;=\\[\\]\\\"\\'\\^]", "_");
                    try {
                        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
                        connection.setRequestProperty("User-Agent", userAgent);
                        connection.setRequestProperty("Content-Type", mimeType);
                        connection.connect();
                        is = connection.getInputStream();
                        File outputFile;
                        int counter = 0;
                        while (true) {
                            outputFile = new File(Storage.getDefaultStorage().getPath() + "/Download", fileName + (counter == 0 ? "" : "(" + counter + ")"));
                            if (!outputFile.exists())
                                break;
                        }
                        final File finalOutputFile = outputFile;
                        FileTools.createNewFile(finalOutputFile);
                        os = new FileOutputStream(finalOutputFile);
                        StreamTools.copy(is, contentLength, os, new ProgressNotifier() {

                            @Override
                            public void reportState(String report) {
                            }

                            @Override
                            public void onCopyStarted() {
                                downloadHandler.onDownloadStarted(url);
                            }

                            @Override
                            public void onProgressPercentage(double percentages) {
                                downloadHandler.onDownloadProgress(url, (float) percentages);
                            }

                            @Override
                            public void onCopyException(Throwable t) {
                                downloadHandler.onDownloadError(url, t);
                            }

                            @Override
                            public void onCopyEnded() {
                                downloadHandler.onDownloadEneded(url, finalOutputFile);
                            }
                        });
                    } catch (Exception e) {
                        downloadHandler.onDownloadError(url, e);
                    } finally {
                        downloadingNow = null;
                        if (os != null)
                            try {
                                os.close();
                            } catch (IOException ignored) {
                            }
                        if (is != null)
                            try {
                                is.close();
                            } catch (IOException ignored) {
                            }
                    }
                }
            });
        }
    });
    setWebChromeClient(new WebChromeClient() {

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (DEBUG)
                logInfo("DEBUG-LOG: onProgressChanged... " + newProgress);
            if (pageHandler == null)
                return;
            if (newProgress >= 89)
                onPageFinished(view, view.getUrl());
            pageHandler.onProgressChanged(view, newProgress);
        }

        @Override
        public void onReachedMaxAppCacheSize(long requiredStorage, long quota, QuotaUpdater quotaUpdater) {
            if (DEBUG)
                logInfo("DEBUG-LOG: onRequestFocus...");
            if (pageDetailsHandler == null)
                return;
            CyborgWebView.this.onReachedMaxAppCacheSize(requiredStorage, quota, quotaUpdater);
        }

        @Override
        public void onGeolocationPermissionsShowPrompt(final String origin, final Callback callback) {
            if (DEBUG)
                logInfo("DEBUG-LOG: onGeolocationPermissionsShowPrompt (origin: " + origin + ", callback: " + callback + ")");
            if (geoLocationHandler == null)
                return;
            geoLocationHandler.onGeolocationPermissionsShowPrompt(origin, new Processor<GeoLocationResponse>() {

                @Override
                public void process(GeoLocationResponse res) {
                    callback.invoke(origin, res.enable, res.remember);
                    settings.setGeolocationEnabled(res.enable);
                }
            });
        }

        @Override
        public void onGeolocationPermissionsHidePrompt() {
            if (DEBUG)
                logInfo("DEBUG-LOG: onGeolocationPermissionsHidePrompt...");
            if (geoLocationHandler == null)
                return;
            geoLocationHandler.onGeolocationPermissionsShowPrompt();
        }

        @SuppressWarnings("unused")
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
            if (DEBUG)
                logInfo("DEBUG-LOG: File Chooser: " + uploadMsg + ", acceptType: " + acceptType + ", capture: " + capture);
            openFileChooser(uploadMsg, acceptType);
        }

        public void openFileChooser(final ValueCallback<Uri> uploadMsg, String acceptType) {
            if (DEBUG)
                logInfo("DEBUG-LOG: File Chooser: " + uploadMsg + ", acceptType: " + acceptType);
            if (fileChooserHandler == null)
                uploadMsg.onReceiveValue(null);
            boolean handled = fileChooserHandler.openFileChooser(getUrl(), acceptType, new Processor<Uri>() {

                @Override
                public void process(Uri uri) {
                    uploadMsg.onReceiveValue(uri);
                }
            });
            if (!handled)
                uploadMsg.onReceiveValue(null);
        }

        @SuppressWarnings("unused")
        public void openFileChooser(ValueCallback<Uri> uploadMsg) {
            if (DEBUG)
                logInfo("DEBUG-LOG: File Chooser: " + uploadMsg);
            openFileChooser(uploadMsg, null);
        }

        @Override
        public Bitmap getDefaultVideoPoster() {
            if (DEBUG)
                logInfo("DEBUG-LOG: getDefaultVideoPoster...");
            if (javaScriptHandler == null)
                return null;
            return videoHandler.getDefaultVideoPoster();
        }

        @Override
        public View getVideoLoadingProgressView() {
            if (DEBUG)
                logInfo("DEBUG-LOG: getVideoLoadingProgressView...");
            if (javaScriptHandler == null)
                return null;
            return videoHandler.getVideoLoadingProgressView();
        }

        @Override
        public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
            if (DEBUG)
                logInfo("DEBUG-LOG: onJsAlert (url: " + url + ", message: " + message + ", result: " + result + ")");
            if (javaScriptHandler == null) {
                result.confirm();
                return true;
            }
            return javaScriptHandler.onJsAlert(view, url, message, result);
        }

        @Override
        public boolean onJsBeforeUnload(WebView view, String url, String message, JsResult result) {
            if (DEBUG)
                logInfo("DEBUG-LOG: onJsBeforeUnload (url: " + url + ", message: " + message + ", result: " + result + ")");
            if (javaScriptHandler == null) {
                result.cancel();
                return false;
            }
            return javaScriptHandler.onJsBeforeUnload(view, url, message, result);
        }

        @Override
        public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
            if (DEBUG)
                logInfo("DEBUG-LOG: onJsConfirm (url: " + url + ", message: " + message + ", result: " + result + ")");
            if (javaScriptHandler == null) {
                result.cancel();
                return false;
            }
            return javaScriptHandler.onJsConfirm(view, url, message, result);
        }

        @Override
        public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
            if (DEBUG)
                logInfo("DEBUG-LOG: onJsPrompt (url: " + url + ", message: " + message + ", defaultValue: " + defaultValue + ", result: " + result + ")");
            if (javaScriptHandler == null) {
                result.cancel();
                return false;
            }
            return javaScriptHandler.onJsPrompt(view, url, message, defaultValue, result);
        }

        @Override
        public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
            if (DEBUG)
                logInfo("DEBUG-LOG: onCreateWindow (isDialog: " + isDialog + ", isUserGesture: " + isUserGesture + ", resultMsg: " + resultMsg + ")");
            if (windowHandler == null)
                return false;
            return windowHandler.onCreateWindow(view, isDialog, isUserGesture, resultMsg);
        }

        @Override
        public void onCloseWindow(WebView window) {
            if (DEBUG)
                logInfo("DEBUG-LOG: onCloseWindow...");
            if (windowHandler == null)
                return;
            windowHandler.onCloseWindow(window);
        }

        @Override
        public void onShowCustomView(View view, int requestedOrientation, CustomViewCallback callback) {
            if (DEBUG)
                logInfo("DEBUG-LOG: onShowCustomView (callback: " + callback + ")");
            if (javaScriptHandler == null)
                return;
            customViewHandler.onShowCustomView(view, requestedOrientation, callback);
        }

        @Override
        public void onShowCustomView(View view, CustomViewCallback callback) {
            onShowCustomView(view, 0, callback);
        }

        @Override
        public void onHideCustomView() {
            if (DEBUG)
                logInfo("DEBUG-LOG: onHideCustomView...");
            if (javaScriptHandler == null)
                return;
            customViewHandler.onHideCustomView();
        }

        @Override
        public void onReceivedIcon(WebView view, Bitmap icon) {
            if (DEBUG)
                logInfo("DEBUG-LOG: onReceivedIcon (icon: " + icon + ")");
            if (pageDetailsHandler == null)
                return;
            pageDetailsHandler.onReceivedIcon(view, icon);
        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            if (DEBUG)
                logInfo("DEBUG-LOG: onReceivedTitle (title: " + title + ")");
            if (pageDetailsHandler == null)
                return;
            pageDetailsHandler.onReceivedTitle(view, title);
        }

        @Override
        public void onReceivedTouchIconUrl(WebView view, String url, boolean precomposed) {
            if (DEBUG)
                logInfo("DEBUG-LOG: onReceivedTouchIconUrl (url: " + url + ", precomposed: " + precomposed + ")");
            if (pageDetailsHandler == null)
                return;
            pageDetailsHandler.onReceivedTouchIconUrl(view, url, precomposed);
        }

        @Override
        public void onRequestFocus(WebView view) {
            if (DEBUG)
                logInfo("DEBUG-LOG: onRequestFocus...");
            if (pageDetailsHandler == null)
                return;
            pageDetailsHandler.onRequestFocus(view);
        }
    });
    setWebViewClient(new WebViewClient() {

        @Override
        public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {
            if (DEBUG)
                logInfo("DEBUG-LOG: doUpdateVisitedHistory (url: " + url + ", isReload: " + isReload + ")");
            if (pageHandler == null)
                return;
            pageHandler.doUpdateVisitedHistory(view, url, isReload);
        }

        @Override
        public void onLoadResource(WebView view, String url) {
            if (DEBUG)
                logInfo("DEBUG-LOG: onLoadResource (url: " + url + ")");
            if (pageHandler == null)
                return;
            pageHandler.onLoadResource(view, url);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            if (DEBUG)
                logInfo("DEBUG-LOG: onPageFinished (url: " + url + ")");
            CyborgWebView.this.onPageFinished(view, url);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            if (DEBUG)
                logInfo("DEBUG-LOG: onPageStarted (url: " + url + ", favicon: " + favicon + ")");
            finishedURL = null;
            if (pageHandler == null)
                return;
            pageHandler.onPageStarted(view, url, favicon);
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            if (DEBUG)
                logInfo("DEBUG-LOG: onReceivedError (errorCode: " + errorCode + ", description: " + description + ", failingUrl: " + failingUrl + ")");
            if (pageHandler == null)
                return;
            pageHandler.onReceivedError(view, errorCode, description, failingUrl);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (DEBUG)
                logInfo("DEBUG-LOG: shouldOverrideUrlLoading: " + url);
            if (url.toLowerCase().equals("about:blank"))
                return super.shouldOverrideUrlLoading(view, url);
            if (URLUtil.isHttpsUrl(url) || URLUtil.isHttpUrl(url)) {
                if (pageHandler != null && pageHandler.shouldOverrideUrlLoading(view, url))
                    return true;
                return super.shouldOverrideUrlLoading(view, url);
            }
            if (getContext() instanceof Activity && resolveUrl(url))
                return true;
            if (pageHandler != null && pageHandler.resolveNoneHttpUrl(view, url))
                return true;
            if (!(getContext() instanceof Activity))
                return true;
            try {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                getContext().startActivity(intent);
            } catch (Throwable e) {
                logError(e);
            }
            return true;
        }

        /*
			 * requestHandler
			 */
        @Override
        public void onFormResubmission(WebView view, Message dontResend, Message resend) {
            if (DEBUG)
                logInfo("DEBUG-LOG: onFormResubmission...");
            if (requestHandler == null)
                return;
            requestHandler.onFormResubmission(view, dontResend, resend);
        }

        @Override
        public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
            if (DEBUG)
                logInfo("DEBUG-LOG: onReceivedHttpAuthRequest (host: " + host + ", realm: " + realm + ")");
            if (requestHandler == null)
                return;
            requestHandler.onReceivedHttpAuthRequest(view, handler, host, realm);
        }

        @Override
        @SuppressWarnings("unused")
        public void onReceivedLoginRequest(WebView view, String realm, String account, String args) {
            if (DEBUG)
                logInfo("DEBUG-LOG: onReceivedLoginRequest (realm: " + realm + ", account: " + account + ", args: " + args + ")");
            if (requestHandler == null)
                return;
            requestHandler.onReceivedLoginRequest(view, realm, account, args);
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            if (DEBUG)
                logInfo("DEBUG-LOG: onReceivedSslError (error: " + error + ")");
            if (requestHandler == null)
                return;
            requestHandler.onReceivedSslError(view, handler, error);
        }

        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
            if (DEBUG)
                logInfo("DEBUG-LOG: shouldInterceptRequest: " + url);
            if (requestHandler == null)
                return null;
            return requestHandler.shouldInterceptRequest(view, url);
        }

        @Override
        public void onTooManyRedirects(WebView view, Message cancelMsg, Message continueMsg) {
            if (DEBUG)
                logInfo("DEBUG-LOG: onTooManyRedirects...");
            if (requestHandler == null)
                return;
            requestHandler.onTooManyRedirects(view, cancelMsg, continueMsg);
        }

        @Override
        public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
            if (DEBUG)
                logInfo("DEBUG-LOG: onUnhandledKeyEvent: " + event);
            if (systemHandler == null)
                return;
            systemHandler.onUnhandledKeyEvent(view, event);
        }

        @Override
        public void onScaleChanged(WebView view, float oldScale, float newScale) {
            if (DEBUG)
                logInfo("DEBUG-LOG: onScaleChanged: " + oldScale + " => " + newScale);
            if (systemHandler == null)
                return;
            systemHandler.onScaleChanged(view, oldScale, newScale);
        }

        @Override
        public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
            if (DEBUG)
                logInfo("DEBUG-LOG: shouldOverrideKeyEvent: " + event);
            if (systemHandler == null)
                return false;
            return systemHandler.shouldOverrideKeyEvent(view, event);
        }
    });
}
Also used : SslErrorHandler(android.webkit.SslErrorHandler) Processor(com.nu.art.core.generics.Processor) ProgressNotifier(com.nu.art.core.interfaces.ProgressNotifier) Message(android.os.Message) SslError(android.net.http.SslError) GeoLocationResponse(com.nu.art.cyborg.web.api.WebViewGeoLocationHandler.GeoLocationResponse) Activity(android.app.Activity) Uri(android.net.Uri) URL(java.net.URL) JsResult(android.webkit.JsResult) KeyEvent(android.view.KeyEvent) Bitmap(android.graphics.Bitmap) HttpURLConnection(java.net.HttpURLConnection) HttpAuthHandler(android.webkit.HttpAuthHandler) WebChromeClient(android.webkit.WebChromeClient) WebView(android.webkit.WebView) WebViewClient(android.webkit.WebViewClient) InputStream(java.io.InputStream) WebViewWindowHandler(com.nu.art.cyborg.web.api.WebViewWindowHandler) SslErrorHandler(android.webkit.SslErrorHandler) Handler(android.os.Handler) HttpAuthHandler(android.webkit.HttpAuthHandler) WebViewFileChooserHandler(com.nu.art.cyborg.web.api.WebViewFileChooserHandler) WebViewJavaScriptHandler(com.nu.art.cyborg.web.api.WebViewJavaScriptHandler) WebViewVideoHandler(com.nu.art.cyborg.web.api.WebViewVideoHandler) WebViewDownloadHandler(com.nu.art.cyborg.web.api.WebViewDownloadHandler) WebViewSystemHandler(com.nu.art.cyborg.web.api.WebViewSystemHandler) WebViewGeoLocationHandler(com.nu.art.cyborg.web.api.WebViewGeoLocationHandler) WebViewPageDetailsHandler(com.nu.art.cyborg.web.api.WebViewPageDetailsHandler) WebViewCustomViewHandler(com.nu.art.cyborg.web.api.WebViewCustomViewHandler) ScriptActionErrorHandler(com.nu.art.cyborg.web.api.ScriptActionErrorHandler) WebViewPageHandler(com.nu.art.cyborg.web.api.WebViewPageHandler) WebViewRequestHandler(com.nu.art.cyborg.web.api.WebViewRequestHandler) Intent(android.content.Intent) IOException(java.io.IOException) View(android.view.View) WebView(android.webkit.WebView) SuppressLint(android.annotation.SuppressLint) IOException(java.io.IOException) ValueCallback(android.webkit.ValueCallback) Callback(android.webkit.GeolocationPermissions.Callback) HandlerThread(android.os.HandlerThread) WebResourceResponse(android.webkit.WebResourceResponse) DownloadListener(android.webkit.DownloadListener) FileOutputStream(java.io.FileOutputStream) QuotaUpdater(android.webkit.WebStorage.QuotaUpdater) Application(android.app.Application) File(java.io.File) JsPromptResult(android.webkit.JsPromptResult)

Example 2 with Callback

use of android.webkit.GeolocationPermissions.Callback in project OpenMEAP by OpenMEAP.

the class MainActivity method createDefaultWebView.

/**
 * Sets up the window title, per the properties
 *
 * private void setupWindowTitle() { if( config.getApplicationTitle()!=null
 * ) { if( config.getApplicationTitle().equals("off") ) {
 * requestWindowFeature(Window.FEATURE_NO_TITLE); } else {
 * setTitle(config.getApplicationTitle()); } } else
 * setTitle(config.getApplicationName()); }
 */
/**
 * Creates the default WebView where we'll run javascript and render content
 */
public WebView createDefaultWebView() {
    WebView webView = new com.openmeap.android.WebView(this, this);
    // make sure javascript and our api is available to the webview
    JsApiCoreImpl jsApi = new JsApiCoreImpl(this, webView, updateHandler);
    webView.addJavascriptInterface(jsApi, JS_API_NAME);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new WebViewClient() {

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });
    // making web database enabled.
    webView.getSettings().setDatabaseEnabled(true);
    // making Dom storage enabled.
    webView.getSettings().setDomStorageEnabled(true);
    // requesting to create directory with name "localstorage" in /data/data/.../App_localstorage,
    // so that, localstorage related data files saved into that directory.
    String databasePath = this.getApplicationContext().getDir("localstorage", Context.MODE_PRIVATE).getPath();
    // setting local storage database path.
    webView.getSettings().setDatabasePath("/data/data/" + webView.getContext().getPackageName() + "/databases/");
    // enable navigator.geolocation
    webView.getSettings().setGeolocationEnabled(true);
    webView.getSettings().setGeolocationDatabasePath("/data/data/" + webView.getContext().getPackageName() + "/databases/");
    // removes vertical and horizontal scroll bars
    webView.setVerticalScrollBarEnabled(false);
    webView.setHorizontalScrollBarEnabled(false);
    // WebChromeClient class is set, so that the overridden methods are executed,
    // when something that might impact a browser UI happens.
    webView.setWebChromeClient(new WebChromeClient() {

        @Override
        public void onGeolocationPermissionsShowPrompt(String origin, Callback callback) {
            callback.invoke(origin, true, false);
        }

        @Override
        public void onExceededDatabaseQuota(String url, String databaseIdentifier, long quota, long estimatedDatabaseSize, long totalQuota, QuotaUpdater quotaUpdater) {
            // TODO Auto-generated method stub
            super.onExceededDatabaseQuota(url, databaseIdentifier, quota, estimatedDatabaseSize, totalQuota, quotaUpdater);
        }
    });
    // make sure the web view fills the viewable area
    webView.setLayoutParams(new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
    return webView;
}
Also used : LoginFormCallback(com.openmeap.thinclient.LoginFormCallback) Callback(android.webkit.GeolocationPermissions.Callback) WebChromeClient(android.webkit.WebChromeClient) QuotaUpdater(android.webkit.WebStorage.QuotaUpdater) OmWebView(com.openmeap.thinclient.OmWebView) JsApiCoreImpl(com.openmeap.thinclient.javascript.JsApiCoreImpl) LinearLayout(android.widget.LinearLayout) WebViewClient(android.webkit.WebViewClient)

Example 3 with Callback

use of android.webkit.GeolocationPermissions.Callback in project SmartMesh_Android by SmartMeshFoundation.

the class AdvancedWebView method init.

@SuppressLint({ "SetJavaScriptEnabled" })
protected void init(final Context context) {
    if (context instanceof Activity) {
        mActivity = new WeakReference<Activity>((Activity) context);
    }
    mLanguageIso3 = getLanguageIso3();
    setFocusable(true);
    setFocusableInTouchMode(true);
    setSaveEnabled(true);
    final String filesDir = context.getFilesDir().getPath();
    final String databaseDir = filesDir.substring(0, filesDir.lastIndexOf("/")) + DATABASES_SUB_FOLDER;
    final WebSettings webSettings = getSettings();
    webSettings.setAllowFileAccess(false);
    setAllowAccessFromFileUrls(webSettings, true);
    webSettings.setBuiltInZoomControls(false);
    webSettings.setJavaScriptEnabled(true);
    webSettings.setDomStorageEnabled(true);
    if (Build.VERSION.SDK_INT < 18) {
        webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);
    }
    webSettings.setDatabaseEnabled(true);
    if (Build.VERSION.SDK_INT < 19) {
        webSettings.setDatabasePath(databaseDir);
    }
    setMixedContentAllowed(webSettings, true);
    setThirdPartyCookiesEnabled(true);
    super.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            if (!hasError()) {
                if (mListener != null) {
                    mListener.onPageStarted(url, favicon);
                }
            }
            if (mCustomWebViewClient != null) {
                mCustomWebViewClient.onPageStarted(view, url, favicon);
            }
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            if (!hasError()) {
                if (mListener != null) {
                    mListener.onPageFinished(url);
                }
            }
            if (mCustomWebViewClient != null) {
                mCustomWebViewClient.onPageFinished(view, url);
            }
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            setLastError();
            if (mListener != null) {
                mListener.onPageError(errorCode, description, failingUrl);
            }
            if (mCustomWebViewClient != null) {
                mCustomWebViewClient.onReceivedError(view, errorCode, description, failingUrl);
            }
        }

        @Override
        public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
            if (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("www.")) {
                return super.shouldOverrideUrlLoading(view, url);
            } else {
                return true;
            }
        }

        @Override
        public void onLoadResource(WebView view, String url) {
            if (mCustomWebViewClient != null) {
                mCustomWebViewClient.onLoadResource(view, url);
            } else {
                super.onLoadResource(view, url);
            }
        }

        @SuppressLint("NewApi")
        @SuppressWarnings("all")
        public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
            if (Build.VERSION.SDK_INT >= 11) {
                if (mCustomWebViewClient != null) {
                    return mCustomWebViewClient.shouldInterceptRequest(view, url);
                } else {
                    return super.shouldInterceptRequest(view, url);
                }
            } else {
                return null;
            }
        }

        @SuppressLint("NewApi")
        @SuppressWarnings("all")
        public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
            if (Build.VERSION.SDK_INT >= 21) {
                if (mCustomWebViewClient != null) {
                    return mCustomWebViewClient.shouldInterceptRequest(view, request);
                } else {
                    return super.shouldInterceptRequest(view, request);
                }
            } else {
                return null;
            }
        }

        @Override
        public void onFormResubmission(WebView view, Message dontResend, Message resend) {
            if (mCustomWebViewClient != null) {
                mCustomWebViewClient.onFormResubmission(view, dontResend, resend);
            } else {
                super.onFormResubmission(view, dontResend, resend);
            }
        }

        @Override
        public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {
            if (mCustomWebViewClient != null) {
                mCustomWebViewClient.doUpdateVisitedHistory(view, url, isReload);
            } else {
                super.doUpdateVisitedHistory(view, url, isReload);
            }
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            if (mCustomWebViewClient != null) {
                mCustomWebViewClient.onReceivedSslError(view, handler, error);
            } else {
                super.onReceivedSslError(view, handler, error);
            }
        }

        @SuppressLint("NewApi")
        @SuppressWarnings("all")
        public void onReceivedClientCertRequest(WebView view, ClientCertRequest request) {
            if (Build.VERSION.SDK_INT >= 21) {
                if (mCustomWebViewClient != null) {
                    mCustomWebViewClient.onReceivedClientCertRequest(view, request);
                } else {
                    super.onReceivedClientCertRequest(view, request);
                }
            }
        }

        @Override
        public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
            if (mCustomWebViewClient != null) {
                mCustomWebViewClient.onReceivedHttpAuthRequest(view, handler, host, realm);
            } else {
                super.onReceivedHttpAuthRequest(view, handler, host, realm);
            }
        }

        @Override
        public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
            if (mCustomWebViewClient != null) {
                return mCustomWebViewClient.shouldOverrideKeyEvent(view, event);
            } else {
                return super.shouldOverrideKeyEvent(view, event);
            }
        }

        @Override
        public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
            if (mCustomWebViewClient != null) {
                mCustomWebViewClient.onUnhandledKeyEvent(view, event);
            } else {
                super.onUnhandledKeyEvent(view, event);
            }
        }

        @SuppressLint("NewApi")
        @SuppressWarnings("all")
        public void onUnhandledInputEvent(WebView view, InputEvent event) {
            if (Build.VERSION.SDK_INT >= 21) {
                if (mCustomWebViewClient != null) {
                    mCustomWebViewClient.onUnhandledInputEvent(view, event);
                } else {
                    super.onUnhandledInputEvent(view, event);
                }
            }
        }

        @Override
        public void onScaleChanged(WebView view, float oldScale, float newScale) {
            if (mCustomWebViewClient != null) {
                mCustomWebViewClient.onScaleChanged(view, oldScale, newScale);
            } else {
                super.onScaleChanged(view, oldScale, newScale);
            }
        }

        @SuppressLint("NewApi")
        @SuppressWarnings("all")
        public void onReceivedLoginRequest(WebView view, String realm, String account, String args) {
            if (Build.VERSION.SDK_INT >= 12) {
                if (mCustomWebViewClient != null) {
                    mCustomWebViewClient.onReceivedLoginRequest(view, realm, account, args);
                } else {
                    super.onReceivedLoginRequest(view, realm, account, args);
                }
            }
        }
    });
    super.setWebChromeClient(new WebChromeClient() {

        // file upload callback (Android 2.2 (API level 8) -- Android 2.3 (API level 10)) (hidden method)
        @SuppressWarnings("unused")
        public void openFileChooser(ValueCallback<Uri> uploadMsg) {
            openFileChooser(uploadMsg, null);
        }

        // file upload callback (Android 3.0 (API level 11) -- Android 4.0 (API level 15)) (hidden method)
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
            openFileChooser(uploadMsg, acceptType, null);
        }

        // file upload callback (Android 4.1 (API level 16) -- Android 4.3 (API level 18)) (hidden method)
        @SuppressWarnings("unused")
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
            openFileInput(uploadMsg, null);
        }

        // file upload callback (Android 5.0 (API level 21) -- current) (public method)
        @SuppressWarnings("all")
        public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
            openFileInput(null, filePathCallback);
            return true;
        }

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (mCustomWebChromeClient != null) {
                mCustomWebChromeClient.onProgressChanged(view, newProgress);
            } else {
                super.onProgressChanged(view, newProgress);
            }
        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            if (mCustomWebChromeClient != null) {
                mCustomWebChromeClient.onReceivedTitle(view, title);
            } else {
                super.onReceivedTitle(view, title);
            }
        }

        @Override
        public void onReceivedIcon(WebView view, Bitmap icon) {
            if (mCustomWebChromeClient != null) {
                mCustomWebChromeClient.onReceivedIcon(view, icon);
            } else {
                super.onReceivedIcon(view, icon);
            }
        }

        @Override
        public void onReceivedTouchIconUrl(WebView view, String url, boolean precomposed) {
            if (mCustomWebChromeClient != null) {
                mCustomWebChromeClient.onReceivedTouchIconUrl(view, url, precomposed);
            } else {
                super.onReceivedTouchIconUrl(view, url, precomposed);
            }
        }

        @Override
        public void onShowCustomView(View view, CustomViewCallback callback) {
            if (mCustomWebChromeClient != null) {
                mCustomWebChromeClient.onShowCustomView(view, callback);
            } else {
                super.onShowCustomView(view, callback);
            }
        }

        @SuppressLint("NewApi")
        @SuppressWarnings("all")
        public void onShowCustomView(View view, int requestedOrientation, CustomViewCallback callback) {
            if (Build.VERSION.SDK_INT >= 14) {
                if (mCustomWebChromeClient != null) {
                    mCustomWebChromeClient.onShowCustomView(view, requestedOrientation, callback);
                } else {
                    super.onShowCustomView(view, requestedOrientation, callback);
                }
            }
        }

        @Override
        public void onHideCustomView() {
            if (mCustomWebChromeClient != null) {
                mCustomWebChromeClient.onHideCustomView();
            } else {
                super.onHideCustomView();
            }
        }

        @Override
        public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
            if (mCustomWebChromeClient != null) {
                return mCustomWebChromeClient.onCreateWindow(view, isDialog, isUserGesture, resultMsg);
            } else {
                return super.onCreateWindow(view, isDialog, isUserGesture, resultMsg);
            }
        }

        @Override
        public void onRequestFocus(WebView view) {
            if (mCustomWebChromeClient != null) {
                mCustomWebChromeClient.onRequestFocus(view);
            } else {
                super.onRequestFocus(view);
            }
        }

        @Override
        public void onCloseWindow(WebView window) {
            if (mCustomWebChromeClient != null) {
                mCustomWebChromeClient.onCloseWindow(window);
            } else {
                super.onCloseWindow(window);
            }
        }

        @Override
        public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
            if (mCustomWebChromeClient != null) {
                return mCustomWebChromeClient.onJsAlert(view, url, message, result);
            } else {
                return super.onJsAlert(view, url, message, result);
            }
        }

        @Override
        public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
            if (mCustomWebChromeClient != null) {
                return mCustomWebChromeClient.onJsConfirm(view, url, message, result);
            } else {
                return super.onJsConfirm(view, url, message, result);
            }
        }

        @Override
        public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
            if (mCustomWebChromeClient != null) {
                return mCustomWebChromeClient.onJsPrompt(view, url, message, defaultValue, result);
            } else {
                return super.onJsPrompt(view, url, message, defaultValue, result);
            }
        }

        @Override
        public boolean onJsBeforeUnload(WebView view, String url, String message, JsResult result) {
            if (mCustomWebChromeClient != null) {
                return mCustomWebChromeClient.onJsBeforeUnload(view, url, message, result);
            } else {
                return super.onJsBeforeUnload(view, url, message, result);
            }
        }

        @Override
        public void onGeolocationPermissionsShowPrompt(String origin, Callback callback) {
            if (mGeolocationEnabled) {
                callback.invoke(origin, true, false);
            } else {
                if (mCustomWebChromeClient != null) {
                    mCustomWebChromeClient.onGeolocationPermissionsShowPrompt(origin, callback);
                } else {
                    super.onGeolocationPermissionsShowPrompt(origin, callback);
                }
            }
        }

        @Override
        public void onGeolocationPermissionsHidePrompt() {
            if (mCustomWebChromeClient != null) {
                mCustomWebChromeClient.onGeolocationPermissionsHidePrompt();
            } else {
                super.onGeolocationPermissionsHidePrompt();
            }
        }

        @SuppressLint("NewApi")
        @SuppressWarnings("all")
        public void onPermissionRequest(PermissionRequest request) {
            if (Build.VERSION.SDK_INT >= 21) {
                if (mCustomWebChromeClient != null) {
                    mCustomWebChromeClient.onPermissionRequest(request);
                } else {
                    super.onPermissionRequest(request);
                }
            }
        }

        @SuppressLint("NewApi")
        @SuppressWarnings("all")
        public void onPermissionRequestCanceled(PermissionRequest request) {
            if (Build.VERSION.SDK_INT >= 21) {
                if (mCustomWebChromeClient != null) {
                    mCustomWebChromeClient.onPermissionRequestCanceled(request);
                } else {
                    super.onPermissionRequestCanceled(request);
                }
            }
        }

        @Override
        public boolean onJsTimeout() {
            if (mCustomWebChromeClient != null) {
                return mCustomWebChromeClient.onJsTimeout();
            } else {
                return super.onJsTimeout();
            }
        }

        @Override
        public void onConsoleMessage(String message, int lineNumber, String sourceID) {
            if (mCustomWebChromeClient != null) {
                mCustomWebChromeClient.onConsoleMessage(message, lineNumber, sourceID);
            } else {
                super.onConsoleMessage(message, lineNumber, sourceID);
            }
        }

        @Override
        public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
            if (mCustomWebChromeClient != null) {
                return mCustomWebChromeClient.onConsoleMessage(consoleMessage);
            } else {
                return super.onConsoleMessage(consoleMessage);
            }
        }

        @Override
        public Bitmap getDefaultVideoPoster() {
            if (mCustomWebChromeClient != null) {
                return mCustomWebChromeClient.getDefaultVideoPoster();
            } else {
                return super.getDefaultVideoPoster();
            }
        }

        @Override
        public View getVideoLoadingProgressView() {
            if (mCustomWebChromeClient != null) {
                return mCustomWebChromeClient.getVideoLoadingProgressView();
            } else {
                return super.getVideoLoadingProgressView();
            }
        }

        @Override
        public void getVisitedHistory(ValueCallback<String[]> callback) {
            if (mCustomWebChromeClient != null) {
                mCustomWebChromeClient.getVisitedHistory(callback);
            } else {
                super.getVisitedHistory(callback);
            }
        }

        @Override
        public void onExceededDatabaseQuota(String url, String databaseIdentifier, long quota, long estimatedDatabaseSize, long totalQuota, QuotaUpdater quotaUpdater) {
            if (mCustomWebChromeClient != null) {
                mCustomWebChromeClient.onExceededDatabaseQuota(url, databaseIdentifier, quota, estimatedDatabaseSize, totalQuota, quotaUpdater);
            } else {
                super.onExceededDatabaseQuota(url, databaseIdentifier, quota, estimatedDatabaseSize, totalQuota, quotaUpdater);
            }
        }

        @Override
        public void onReachedMaxAppCacheSize(long requiredStorage, long quota, QuotaUpdater quotaUpdater) {
            if (mCustomWebChromeClient != null) {
                mCustomWebChromeClient.onReachedMaxAppCacheSize(requiredStorage, quota, quotaUpdater);
            } else {
                super.onReachedMaxAppCacheSize(requiredStorage, quota, quotaUpdater);
            }
        }
    });
    setDownloadListener(new DownloadListener() {

        @Override
        public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
            if (mListener != null) {
                mListener.onDownloadRequested(url, userAgent, contentDisposition, mimetype, contentLength);
            }
        }
    });
}
Also used : SslErrorHandler(android.webkit.SslErrorHandler) PermissionRequest(android.webkit.PermissionRequest) ConsoleMessage(android.webkit.ConsoleMessage) Message(android.os.Message) SslError(android.net.http.SslError) ClientCertRequest(android.webkit.ClientCertRequest) FragmentActivity(android.support.v4.app.FragmentActivity) Activity(android.app.Activity) Uri(android.net.Uri) ConsoleMessage(android.webkit.ConsoleMessage) KeyEvent(android.view.KeyEvent) JsResult(android.webkit.JsResult) Bitmap(android.graphics.Bitmap) HttpAuthHandler(android.webkit.HttpAuthHandler) WebChromeClient(android.webkit.WebChromeClient) InputEvent(android.view.InputEvent) WebView(android.webkit.WebView) WebViewClient(android.webkit.WebViewClient) WebResourceRequest(android.webkit.WebResourceRequest) View(android.view.View) WebView(android.webkit.WebView) SuppressLint(android.annotation.SuppressLint) ValueCallback(android.webkit.ValueCallback) Callback(android.webkit.GeolocationPermissions.Callback) WebResourceResponse(android.webkit.WebResourceResponse) DownloadListener(android.webkit.DownloadListener) WebSettings(android.webkit.WebSettings) QuotaUpdater(android.webkit.WebStorage.QuotaUpdater) SuppressLint(android.annotation.SuppressLint) JsPromptResult(android.webkit.JsPromptResult) SuppressLint(android.annotation.SuppressLint)

Aggregations

Callback (android.webkit.GeolocationPermissions.Callback)3 WebChromeClient (android.webkit.WebChromeClient)3 QuotaUpdater (android.webkit.WebStorage.QuotaUpdater)3 WebViewClient (android.webkit.WebViewClient)3 SuppressLint (android.annotation.SuppressLint)2 Activity (android.app.Activity)2 Bitmap (android.graphics.Bitmap)2 Uri (android.net.Uri)2 SslError (android.net.http.SslError)2 Message (android.os.Message)2 KeyEvent (android.view.KeyEvent)2 View (android.view.View)2 DownloadListener (android.webkit.DownloadListener)2 HttpAuthHandler (android.webkit.HttpAuthHandler)2 JsPromptResult (android.webkit.JsPromptResult)2 JsResult (android.webkit.JsResult)2 SslErrorHandler (android.webkit.SslErrorHandler)2 ValueCallback (android.webkit.ValueCallback)2 WebResourceResponse (android.webkit.WebResourceResponse)2 WebView (android.webkit.WebView)2