Search in sources :

Example 1 with RenderProcessGoneDetail

use of android.webkit.RenderProcessGoneDetail in project android-browser-helper by GoogleChrome.

the class WebViewFallbackActivity method createWebViewClient.

private WebViewClient createWebViewClient() {
    return new WebViewClient() {

        @Override
        public boolean onRenderProcessGone(WebView view, RenderProcessGoneDetail detail) {
            ViewGroup vg = (ViewGroup) view.getParent();
            // Remove crashed WebView from the hierarchy
            // and ensure it is destroyed.
            vg.removeView(view);
            view.destroy();
            // Create a new instance, and ensure it also
            // handles crashes - in this case, re-using
            // the current WebViewClient
            mWebView = new WebView(view.getContext());
            mWebView.setWebViewClient(this);
            WebSettings webSettings = mWebView.getSettings();
            setupWebSettings(webSettings);
            vg.addView(mWebView);
            // With the crash recovered, decide what to do next.
            // We are sending a toast and loading the origin
            // URL, in this example.
            Toast.makeText(view.getContext(), "Recovering from crash", Toast.LENGTH_LONG).show();
            mWebView.loadUrl(mLaunchUrl.toString());
            return true;
        }

        private boolean shouldOverrideUrlLoading(Uri navigationUrl) {
            Uri launchUrl = WebViewFallbackActivity.this.mLaunchUrl;
            // usage
            if (!"data".equals(navigationUrl.getScheme()) && !uriOriginsMatch(navigationUrl, launchUrl) && !matchExtraOrigins(navigationUrl)) {
                // to try to handle it.
                try {
                    CustomTabsIntent intent = new CustomTabsIntent.Builder().setToolbarColor(mStatusBarColor).build();
                    intent.launchUrl(WebViewFallbackActivity.this, navigationUrl);
                    return true;
                } catch (ActivityNotFoundException ex) {
                    Log.e(TAG, String.format("ActivityNotFoundException while launching '%s'", navigationUrl));
                    return false;
                }
            }
            return false;
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return this.shouldOverrideUrlLoading(Uri.parse(url));
        }

        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            return this.shouldOverrideUrlLoading(request.getUrl());
        }

        private boolean matchExtraOrigins(Uri navigationUri) {
            for (Uri uri : mExtraOrigins) {
                if (uriOriginsMatch(uri, navigationUri)) {
                    return true;
                }
            }
            return false;
        }

        private boolean uriOriginsMatch(Uri uriA, Uri uriB) {
            return uriA.getScheme().equalsIgnoreCase(uriB.getScheme()) && uriA.getHost().equalsIgnoreCase(uriB.getHost()) && uriA.getPort() == uriB.getPort();
        }
    };
}
Also used : CustomTabsIntent(androidx.browser.customtabs.CustomTabsIntent) WebResourceRequest(android.webkit.WebResourceRequest) ActivityNotFoundException(android.content.ActivityNotFoundException) ViewGroup(android.view.ViewGroup) WebSettings(android.webkit.WebSettings) RenderProcessGoneDetail(android.webkit.RenderProcessGoneDetail) WebView(android.webkit.WebView) Uri(android.net.Uri) WebViewClient(android.webkit.WebViewClient)

Example 2 with RenderProcessGoneDetail

use of android.webkit.RenderProcessGoneDetail in project Anki-Android by ankidroid.

the class OnRenderProcessGoneDelegateTest method getCrashDetail.

protected RenderProcessGoneDetail getCrashDetail() {
    RenderProcessGoneDetail mock = mock(RenderProcessGoneDetail.class);
    // this value doesn't matter for now as it only defines a string
    when(mock.didCrash()).thenReturn(true);
    return mock;
}
Also used : RenderProcessGoneDetail(android.webkit.RenderProcessGoneDetail)

Aggregations

RenderProcessGoneDetail (android.webkit.RenderProcessGoneDetail)2 ActivityNotFoundException (android.content.ActivityNotFoundException)1 Uri (android.net.Uri)1 ViewGroup (android.view.ViewGroup)1 WebResourceRequest (android.webkit.WebResourceRequest)1 WebSettings (android.webkit.WebSettings)1 WebView (android.webkit.WebView)1 WebViewClient (android.webkit.WebViewClient)1 CustomTabsIntent (androidx.browser.customtabs.CustomTabsIntent)1