Search in sources :

Example 86 with WebViewClient

use of android.webkit.WebViewClient in project cordova-android-chromeview by thedracle.

the class InAppBrowser method showWebPage.

/**
 * Display a new browser with the specified URL.
 *
 * @param url           The url to load.
 * @param jsonObject
 */
public String showWebPage(final String url, HashMap<String, Boolean> features) {
    // Determine if we should hide the location bar.
    showLocationBar = true;
    openWindowHidden = false;
    if (features != null) {
        Boolean show = features.get(LOCATION);
        if (show != null) {
            showLocationBar = show.booleanValue();
        }
        Boolean hidden = features.get(HIDDEN);
        if (hidden != null) {
            openWindowHidden = hidden.booleanValue();
        }
    }
    final CordovaWebView thatWebView = this.webView;
    // Create dialog in new thread
    Runnable runnable = new Runnable() {

        /**
         * Convert our DIP units to Pixels
         *
         * @return int
         */
        private int dpToPixels(int dipValue) {
            int value = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) dipValue, cordova.getActivity().getResources().getDisplayMetrics());
            return value;
        }

        public void run() {
            // Let's create the main dialog
            dialog = new Dialog(cordova.getActivity(), android.R.style.Theme_NoTitleBar);
            dialog.getWindow().getAttributes().windowAnimations = android.R.style.Animation_Dialog;
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setCancelable(true);
            dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {

                public void onDismiss(DialogInterface dialog) {
                    try {
                        JSONObject obj = new JSONObject();
                        obj.put("type", EXIT_EVENT);
                        sendUpdate(obj, false);
                    } catch (JSONException e) {
                        Log.d(LOG_TAG, "Should never happen");
                    }
                }
            });
            // Main container layout
            LinearLayout main = new LinearLayout(cordova.getActivity());
            main.setOrientation(LinearLayout.VERTICAL);
            // Toolbar layout
            RelativeLayout toolbar = new RelativeLayout(cordova.getActivity());
            toolbar.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, this.dpToPixels(44)));
            toolbar.setPadding(this.dpToPixels(2), this.dpToPixels(2), this.dpToPixels(2), this.dpToPixels(2));
            toolbar.setHorizontalGravity(Gravity.LEFT);
            toolbar.setVerticalGravity(Gravity.TOP);
            // Action Button Container layout
            RelativeLayout actionButtonContainer = new RelativeLayout(cordova.getActivity());
            actionButtonContainer.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            actionButtonContainer.setHorizontalGravity(Gravity.LEFT);
            actionButtonContainer.setVerticalGravity(Gravity.CENTER_VERTICAL);
            actionButtonContainer.setId(1);
            // Back button
            Button back = new Button(cordova.getActivity());
            RelativeLayout.LayoutParams backLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
            backLayoutParams.addRule(RelativeLayout.ALIGN_LEFT);
            back.setLayoutParams(backLayoutParams);
            back.setContentDescription("Back Button");
            back.setId(2);
            back.setText("<");
            back.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    goBack();
                }
            });
            // Forward button
            Button forward = new Button(cordova.getActivity());
            RelativeLayout.LayoutParams forwardLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
            forwardLayoutParams.addRule(RelativeLayout.RIGHT_OF, 2);
            forward.setLayoutParams(forwardLayoutParams);
            forward.setContentDescription("Forward Button");
            forward.setId(3);
            forward.setText(">");
            forward.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    goForward();
                }
            });
            // Edit Text Box
            edittext = new EditText(cordova.getActivity());
            RelativeLayout.LayoutParams textLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            textLayoutParams.addRule(RelativeLayout.RIGHT_OF, 1);
            textLayoutParams.addRule(RelativeLayout.LEFT_OF, 5);
            edittext.setLayoutParams(textLayoutParams);
            edittext.setId(4);
            edittext.setSingleLine(true);
            edittext.setText(url);
            edittext.setInputType(InputType.TYPE_TEXT_VARIATION_URI);
            edittext.setImeOptions(EditorInfo.IME_ACTION_GO);
            // Will not except input... Makes the text NON-EDITABLE
            edittext.setInputType(InputType.TYPE_NULL);
            edittext.setOnKeyListener(new View.OnKeyListener() {

                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    // If the event is a key-down event on the "enter" button
                    if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                        navigate(edittext.getText().toString());
                        return true;
                    }
                    return false;
                }
            });
            // Close button
            Button close = new Button(cordova.getActivity());
            RelativeLayout.LayoutParams closeLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
            closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            close.setLayoutParams(closeLayoutParams);
            forward.setContentDescription("Close Button");
            close.setId(5);
            close.setText(buttonLabel);
            close.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    closeDialog();
                }
            });
            // WebView
            inAppWebView = new WebView(cordova.getActivity());
            inAppWebView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            inAppWebView.setWebChromeClient(new InAppChromeClient(thatWebView));
            WebViewClient client = new InAppBrowserClient(thatWebView, edittext);
            inAppWebView.setWebViewClient(client);
            WebSettings settings = inAppWebView.getSettings();
            settings.setJavaScriptEnabled(true);
            settings.setJavaScriptCanOpenWindowsAutomatically(true);
            settings.setBuiltInZoomControls(true);
            /**
             * We need to be careful of this line as a future Android release may deprecate it out of existence.
             * Can't replace it with the API 8 level call right now as our minimum SDK is 7 until May 2013
             */
            // @TODO: replace with settings.setPluginState(android.webkit.WebSettings.PluginState.ON)
            settings.setPluginsEnabled(true);
            // Toggle whether this is enabled or not!
            Bundle appSettings = cordova.getActivity().getIntent().getExtras();
            boolean enableDatabase = appSettings == null ? true : appSettings.getBoolean("InAppBrowserStorageEnabled", true);
            if (enableDatabase) {
                String databasePath = cordova.getActivity().getApplicationContext().getDir("inAppBrowserDB", Context.MODE_PRIVATE).getPath();
                settings.setDatabasePath(databasePath);
                settings.setDatabaseEnabled(true);
            }
            settings.setDomStorageEnabled(true);
            inAppWebView.loadUrl(url);
            inAppWebView.setId(6);
            inAppWebView.getSettings().setLoadWithOverviewMode(true);
            inAppWebView.getSettings().setUseWideViewPort(true);
            inAppWebView.requestFocus();
            inAppWebView.requestFocusFromTouch();
            // Add the back and forward buttons to our action button container layout
            actionButtonContainer.addView(back);
            actionButtonContainer.addView(forward);
            // Add the views to our toolbar
            toolbar.addView(actionButtonContainer);
            toolbar.addView(edittext);
            toolbar.addView(close);
            // Don't add the toolbar if its been disabled
            if (getShowLocationBar()) {
                // Add our toolbar to our main view/layout
                main.addView(toolbar);
            }
            // Add our webview to our main view/layout
            main.addView(inAppWebView);
            WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
            lp.copyFrom(dialog.getWindow().getAttributes());
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.MATCH_PARENT;
            dialog.setContentView(main);
            dialog.show();
            dialog.getWindow().setAttributes(lp);
            // Show() needs to be called to cause the URL to be loaded
            if (openWindowHidden) {
                dialog.hide();
            }
        }
    };
    this.cordova.getActivity().runOnUiThread(runnable);
    return "";
}
Also used : DialogInterface(android.content.DialogInterface) WindowManager(android.view.WindowManager) KeyEvent(android.view.KeyEvent) Button(android.widget.Button) Dialog(android.app.Dialog) WebView(android.webkit.WebView) WebViewClient(android.webkit.WebViewClient) EditText(android.widget.EditText) LayoutParams(android.view.WindowManager.LayoutParams) Bundle(android.os.Bundle) JSONException(org.json.JSONException) View(android.view.View) WebView(android.webkit.WebView) SuppressLint(android.annotation.SuppressLint) JSONObject(org.json.JSONObject) WebSettings(android.webkit.WebSettings) RelativeLayout(android.widget.RelativeLayout) LayoutParams(android.view.WindowManager.LayoutParams) LinearLayout(android.widget.LinearLayout)

Example 87 with WebViewClient

use of android.webkit.WebViewClient in project BookReader by JustWayward.

the class ReadCHMActivity method initVweView.

private void initVweView() {
    mProgressBar.setMax(100);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.setWebChromeClient(new WebChromeClient() {

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            visible(mProgressBar);
            mProgressBar.setProgress(newProgress);
        }
    });
    mWebView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            if (!url.startsWith("http") && !url.endsWith(md5File)) {
                String temp = url.substring("file://".length());
                if (!temp.startsWith(extractPath)) {
                    url = "file://" + extractPath + temp;
                }
            }
            super.onPageStarted(view, url, favicon);
            mProgressBar.setProgress(50);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            mProgressBar.setProgress(100);
            gone(mProgressBar);
        }

        @Override
        public void onLoadResource(WebView view, String url) {
            if (!url.startsWith("http") && !url.endsWith(md5File)) {
                String temp = url.substring("file://".length());
                if (!temp.startsWith(extractPath)) {
                    url = "file://" + extractPath + temp;
                }
            }
            super.onLoadResource(view, url);
        }

        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
            String url = request.getUrl().toString();
            if (!url.startsWith("http") && !url.endsWith(md5File)) {
                String temp = url.substring("file://".length());
                String insideFileName;
                if (!temp.startsWith(extractPath)) {
                    url = "file://" + extractPath + temp;
                    insideFileName = temp;
                } else {
                    insideFileName = temp.substring(extractPath.length());
                }
                if (insideFileName.contains("#")) {
                    insideFileName = insideFileName.substring(0, insideFileName.indexOf("#"));
                }
                if (insideFileName.contains("?")) {
                    insideFileName = insideFileName.substring(0, insideFileName.indexOf("?"));
                }
                if (insideFileName.contains("%20")) {
                    insideFileName = insideFileName.replaceAll("%20", " ");
                }
                if (url.endsWith(".gif") || url.endsWith(".jpg") || url.endsWith(".png")) {
                    try {
                        return new WebResourceResponse("image/*", "", Utils.chm.getResourceAsStream(insideFileName));
                    } catch (IOException e) {
                        e.printStackTrace();
                        return super.shouldInterceptRequest(view, request);
                    }
                } else if (url.endsWith(".css") || url.endsWith(".js")) {
                    try {
                        return new WebResourceResponse("", "", Utils.chm.getResourceAsStream(insideFileName));
                    } catch (IOException e) {
                        e.printStackTrace();
                        return super.shouldInterceptRequest(view, request);
                    }
                } else {
                    Utils.extractSpecificFile(chmFilePath, extractPath + insideFileName, insideFileName);
                }
            }
            Log.e("2, webviewrequest", url);
            return super.shouldInterceptRequest(view, request);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (!url.startsWith("http") && !url.endsWith(md5File)) {
                String temp = url.substring("file://".length());
                if (!temp.startsWith(extractPath)) {
                    url = "file://" + extractPath + temp;
                    view.loadUrl(url);
                    return true;
                }
            }
            return false;
        }

        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            return shouldOverrideUrlLoading(view, request.getUrl().toString());
        }
    });
    mWebView.getSettings().setBuiltInZoomControls(true);
    mWebView.getSettings().setDisplayZoomControls(false);
    mWebView.getSettings().setUseWideViewPort(true);
    mWebView.getSettings().setLoadWithOverviewMode(true);
    mWebView.getSettings().setLoadsImagesAutomatically(true);
}
Also used : Bitmap(android.graphics.Bitmap) WebResourceRequest(android.webkit.WebResourceRequest) WebResourceResponse(android.webkit.WebResourceResponse) WebChromeClient(android.webkit.WebChromeClient) IOException(java.io.IOException) WebView(android.webkit.WebView) WebViewClient(android.webkit.WebViewClient)

Example 88 with WebViewClient

use of android.webkit.WebViewClient in project JsBridge by lzyzsd.

the class CustomWebView method init.

private void init() {
    this.setVerticalScrollBarEnabled(false);
    this.setHorizontalScrollBarEnabled(false);
    this.getSettings().setJavaScriptEnabled(true);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        WebView.setWebContentsDebuggingEnabled(true);
    }
    bridgeHelper = new BridgeHelper(this);
    this.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageFinished(WebView webView, String s) {
            bridgeHelper.onPageFinished();
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView webView, String s) {
            return bridgeHelper.shouldOverrideUrlLoading(s);
        }
    });
}
Also used : BridgeHelper(com.github.lzyzsd.jsbridge.BridgeHelper) IWebView(com.github.lzyzsd.jsbridge.IWebView) WebView(android.webkit.WebView) WebViewClient(android.webkit.WebViewClient)

Example 89 with WebViewClient

use of android.webkit.WebViewClient in project materialistic by hidroh.

the class CacheableWebView method init.

private void init() {
    enableCache();
    setLoadSettings();
    setWebViewClient(new WebViewClient());
    setWebChromeClient(mArchiveClient);
}
Also used : WebViewClient(android.webkit.WebViewClient)

Example 90 with WebViewClient

use of android.webkit.WebViewClient in project Anki-Android by Ramblurr.

the class SearchImageActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null) {
        boolean b = savedInstanceState.getBoolean(BUNDLE_KEY_SHUT_OFF, false);
        if (b) {
            finishCancel();
            return;
        }
    }
    setContentView(R.layout.activity_search_image);
    try {
        mSource = getIntent().getExtras().getString(EXTRA_SOURCE).toString();
    } catch (Exception e) {
        mSource = "";
    }
    // If translation fails this is a default - source will be returned.
    mWebView = (WebView) findViewById(R.id.ImageSearchWebView);
    mWebView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            processPageLoadFinished();
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            processPageLoadStarted();
        }
    });
    mPickButton = (Button) findViewById(R.id.ImageSearchPick);
    mPickButton.setEnabled(false);
    mPickButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            pickImage();
        }
    });
    mNextButton = (Button) findViewById(R.id.ImageSearchNext);
    mNextButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            nextClicked();
        }
    });
    mPrevButton = (Button) findViewById(R.id.ImageSearchPrev);
    mPrevButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            prevClicked();
        }
    });
    mPrevButton.setEnabled(false);
}
Also used : Bitmap(android.graphics.Bitmap) OnClickListener(android.view.View.OnClickListener) WebView(android.webkit.WebView) View(android.view.View) WebView(android.webkit.WebView) WebViewClient(android.webkit.WebViewClient)

Aggregations

WebViewClient (android.webkit.WebViewClient)247 WebView (android.webkit.WebView)229 WebSettings (android.webkit.WebSettings)77 View (android.view.View)71 WebChromeClient (android.webkit.WebChromeClient)69 SuppressLint (android.annotation.SuppressLint)61 Intent (android.content.Intent)60 Bitmap (android.graphics.Bitmap)59 WebResourceRequest (android.webkit.WebResourceRequest)35 TextView (android.widget.TextView)26 LinearLayout (android.widget.LinearLayout)22 WebResourceError (android.webkit.WebResourceError)21 WebResourceResponse (android.webkit.WebResourceResponse)16 Uri (android.net.Uri)15 SslError (android.net.http.SslError)13 Bundle (android.os.Bundle)13 Toolbar (android.support.v7.widget.Toolbar)13 KeyEvent (android.view.KeyEvent)13 SslErrorHandler (android.webkit.SslErrorHandler)13 TargetApi (android.annotation.TargetApi)12