Search in sources :

Example 1 with WebResourceResponse

use of android.webkit.WebResourceResponse in project phonegap-facebook-plugin by Wizcorp.

the class IceCreamCordovaWebViewClient method shouldInterceptRequest.

@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
    try {
        // Check the against the white-list.
        if ((url.startsWith("http:") || url.startsWith("https:")) && !Config.isUrlWhiteListed(url)) {
            LOG.w(TAG, "URL blocked by whitelist: " + url);
            // Results in a 404.
            return new WebResourceResponse("text/plain", "UTF-8", null);
        }
        CordovaResourceApi resourceApi = appView.getResourceApi();
        Uri origUri = Uri.parse(url);
        // Allow plugins to intercept WebView requests.
        Uri remappedUri = resourceApi.remapUri(origUri);
        if (!origUri.equals(remappedUri) || needsSpecialsInAssetUrlFix(origUri)) {
            OpenForReadResult result = resourceApi.openForRead(remappedUri, true);
            return new WebResourceResponse(result.mimeType, "UTF-8", result.inputStream);
        }
        // If we don't need to special-case the request, let the browser load it.
        return null;
    } catch (IOException e) {
        if (!(e instanceof FileNotFoundException)) {
            LOG.e("IceCreamCordovaWebViewClient", "Error occurred while loading a file (returning a 404).", e);
        }
        // Results in a 404.
        return new WebResourceResponse("text/plain", "UTF-8", null);
    }
}
Also used : WebResourceResponse(android.webkit.WebResourceResponse) OpenForReadResult(org.apache.cordova.CordovaResourceApi.OpenForReadResult) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Uri(android.net.Uri)

Example 2 with WebResourceResponse

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

the class AdBlockerTest method testCreateEmptyResource.

@SuppressLint("NewApi")
@Test
public void testCreateEmptyResource() throws IOException {
    WebResourceResponse resource = AdBlocker.createEmptyResource();
    assertThat(Okio.buffer(Okio.source(resource.getData())).readUtf8()).isEmpty();
}
Also used : WebResourceResponse(android.webkit.WebResourceResponse) Test(org.junit.Test) SuppressLint(android.annotation.SuppressLint)

Example 3 with WebResourceResponse

use of android.webkit.WebResourceResponse in project cordova-android by apache.

the class SystemWebViewClient method shouldInterceptRequest.

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
    try {
        // Changing this will cause problems for your application
        if (!parentEngine.pluginManager.shouldAllowRequest(url)) {
            LOG.w(TAG, "URL blocked by whitelist: " + url);
            // Results in a 404.
            return new WebResourceResponse("text/plain", "UTF-8", null);
        }
        CordovaResourceApi resourceApi = parentEngine.resourceApi;
        Uri origUri = Uri.parse(url);
        // Allow plugins to intercept WebView requests.
        Uri remappedUri = resourceApi.remapUri(origUri);
        if (!origUri.equals(remappedUri) || needsSpecialsInAssetUrlFix(origUri) || needsKitKatContentUrlFix(origUri)) {
            CordovaResourceApi.OpenForReadResult result = resourceApi.openForRead(remappedUri, true);
            return new WebResourceResponse(result.mimeType, "UTF-8", result.inputStream);
        }
        // If we don't need to special-case the request, let the browser load it.
        return null;
    } catch (IOException e) {
        if (!(e instanceof FileNotFoundException)) {
            LOG.e(TAG, "Error occurred while loading a file (returning a 404).", e);
        }
        // Results in a 404.
        return new WebResourceResponse("text/plain", "UTF-8", null);
    }
}
Also used : WebResourceResponse(android.webkit.WebResourceResponse) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) CordovaResourceApi(org.apache.cordova.CordovaResourceApi) Uri(android.net.Uri) TargetApi(android.annotation.TargetApi)

Example 4 with WebResourceResponse

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

the class IceCreamCordovaWebViewClient method getWhitelistResponse.

private WebResourceResponse getWhitelistResponse() {
    WebResourceResponse emptyResponse;
    String empty = "";
    ByteArrayInputStream data = new ByteArrayInputStream(empty.getBytes());
    return new WebResourceResponse("text/plain", "UTF-8", data);
}
Also used : WebResourceResponse(android.webkit.WebResourceResponse) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 5 with WebResourceResponse

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

the class IceCreamCordovaWebViewClient method generateWebResourceResponse.

private WebResourceResponse generateWebResourceResponse(String url) {
    if (url.startsWith("file:///android_asset/")) {
        String mimetype = FileHelper.getMimeType(url, cordova);
        try {
            InputStream stream = FileHelper.getInputStreamFromUriString(url, cordova);
            WebResourceResponse response = new WebResourceResponse(mimetype, "UTF-8", stream);
            return response;
        } catch (IOException e) {
            LOG.e("generateWebResourceResponse", e.getMessage(), e);
        }
    }
    return null;
}
Also used : WebResourceResponse(android.webkit.WebResourceResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Aggregations

WebResourceResponse (android.webkit.WebResourceResponse)11 IOException (java.io.IOException)6 WebResourceRequest (android.webkit.WebResourceRequest)4 WebView (android.webkit.WebView)4 WebViewClient (android.webkit.WebViewClient)4 Uri (android.net.Uri)3 WebChromeClient (android.webkit.WebChromeClient)3 SuppressLint (android.annotation.SuppressLint)2 TargetApi (android.annotation.TargetApi)2 Bitmap (android.graphics.Bitmap)2 WebResourceError (android.webkit.WebResourceError)2 WebSettings (android.webkit.WebSettings)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 InputStream (java.io.InputStream)2 HttpURLConnection (java.net.HttpURLConnection)2 ActivityNotFoundException (android.content.ActivityNotFoundException)1 ContentResolver (android.content.ContentResolver)1 Context (android.content.Context)1 SslError (android.net.http.SslError)1