android.webkit.WebResourceResponse Java Examples

The following examples show how to use android.webkit.WebResourceResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KCWebViewClient.java    From kerkee_android with GNU General Public License v3.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private WebResourceResponse handleImageRequest(final WebView aWebView, final WebResourceRequest request, String strMimeType)
{
    KCWebView webView = (KCWebView) aWebView;
    if (mImageDownloader == null)
        mImageDownloader = new KCWebImageDownloader(webView.getContext(), webView.getWebPath());
    if (mWebImageHandler==null)
        mWebImageHandler = new KCWebImageHandler(mWebImageListener);

    KCWebImage webImage = mImageDownloader.downloadImageFile(request.getUrl().toString(), mWebImageHandler.add(request.getUrl().toString()));
    InputStream stream = webImage.getInputStream();
    if (stream == null)
    {
        Log.e("image", "current stream is null,download image from net");
        return null;
    }
    return new WebResourceResponse(strMimeType, "utf-8", stream);
}
 
Example #2
Source File: WebViewCacheImpl.java    From FastWebView with MIT License 6 votes vote down vote up
@Override
public WebResourceResponse getResource(WebResourceRequest webResourceRequest, int cacheMode, String userAgent) {
    if (mFastCacheMode == FastCacheMode.DEFAULT) {
        throw new IllegalStateException("an error occurred.");
    }
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        String url = webResourceRequest.getUrl().toString();
        String extension = MimeTypeMapUtils.getFileExtensionFromUrl(url);
        String mimeType = MimeTypeMapUtils.getMimeTypeFromExtension(extension);
        CacheRequest cacheRequest = new CacheRequest();
        cacheRequest.setUrl(url);
        cacheRequest.setMime(mimeType);
        cacheRequest.setForceMode(mFastCacheMode == FastCacheMode.FORCE);
        cacheRequest.setUserAgent(userAgent);
        cacheRequest.setWebViewCacheMode(cacheMode);
        Map<String, String> headers = webResourceRequest.getRequestHeaders();
        cacheRequest.setHeaders(headers);
        return getOfflineServer().get(cacheRequest);
    }
    throw new IllegalStateException("an error occurred.");
}
 
Example #3
Source File: WebViewActivity.java    From movienow with GNU General Public License v3.0 6 votes vote down vote up
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        String url = request.getUrl().toString();
        if (HostManager.getInstance().shouldIntercept(url)) {
            return new WebResourceResponse(null, null, null);
        } else {
            if (!isUsePlayer) {
                DetectorManager.getInstance().addTask(new Video(url, url));
            }
            if (needCache) {
                return WebViewCacheInterceptorInst.getInstance().interceptRequest(request);
            } else {
                return super.shouldInterceptRequest(view, request);
            }
        }
    }
    if (needCache) {
        return WebViewCacheInterceptorInst.getInstance().interceptRequest(request);
    } else {
        return super.shouldInterceptRequest(view, request);
    }
}
 
Example #4
Source File: SystemWebViewClient.java    From keemob with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("deprecation")
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
    try {
        // Check the against the whitelist and lock out access to the WebView directory
        // 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);
    }
}
 
Example #5
Source File: WebViewLocalServer.java    From OsmGo with MIT License 6 votes vote down vote up
/**
 * Attempt to retrieve the WebResourceResponse associated with the given <code>request</code>.
 * This method should be invoked from within
 * {@link android.webkit.WebViewClient#shouldInterceptRequest(android.webkit.WebView,
 * android.webkit.WebResourceRequest)}.
 *
 * @param request the request to process.
 * @return a response if the request URL had a matching handler, null if no handler was found.
 */
public WebResourceResponse shouldInterceptRequest(WebResourceRequest request) {
  Uri loadingUrl = request.getUrl();
  PathHandler handler;
  synchronized (uriMatcher) {
    handler = (PathHandler) uriMatcher.match(request.getUrl());
  }
  if (handler == null) {
    return null;
  }

  if (isLocalFile(loadingUrl) || loadingUrl.toString().startsWith(bridge.getLocalUrl())) {
    Log.d(LogUtils.getCoreTag(), "Handling local request: " + request.getUrl().toString());
    return handleLocalRequest(request, handler);
  } else {
    return handleProxyRequest(request, handler);
  }
}
 
Example #6
Source File: InnerFastClient.java    From FastWebView with MIT License 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
    if (mDelegate != null) {
        mDelegate.onReceivedHttpError(view, request, errorResponse);
        return;
    }
    super.onReceivedHttpError(view, request, errorResponse);
}
 
Example #7
Source File: LightningView.java    From browser with GNU General Public License v2.0 6 votes vote down vote up
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {

	/*
	ByteArrayInputStream EMPTY = new ByteArrayInputStream("".getBytes());
	if (mAdBlock.isAd(request.getUrl().getHost())) {

		return new WebResourceResponse("text/plain", "utf-8", EMPTY);
	}

	if(request.getUrl().getHost().indexOf("127.0.0.1")>=0){
		//ToastUtil.showMessage("this site is insecure");

		return new WebResourceResponse("text/plain", "utf-8", EMPTY);
	}
	*/

	return super.shouldInterceptRequest(view, request);
}
 
Example #8
Source File: BaseWebViewFragment.java    From edx-app-android with Apache License 2.0 6 votes vote down vote up
@Override
@TargetApi(Build.VERSION_CODES.M)
public void onPageLoadError(WebView view, WebResourceRequest request,
                            WebResourceResponse errorResponse,
                            boolean isMainRequestFailure) {
    if (isMainRequestFailure) {
        errorNotification.showError(getContext(),
                new HttpStatusException(Response.error(errorResponse.getStatusCode(),
                        ResponseBody.create(MediaType.parse(errorResponse.getMimeType()),
                                errorResponse.getReasonPhrase()))),
                R.string.lbl_reload, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onRefresh();
                    }
                });
        clearWebView();
    }
}
 
Example #9
Source File: IceCreamCordovaWebViewClient.java    From phonegap-plugin-loading-spinner with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example #10
Source File: SystemWebViewClient.java    From pychat with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("deprecation")
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
    try {
        // Check the against the whitelist and lock out access to the WebView directory
        // 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);
    }
}
 
Example #11
Source File: RexxarWebView.java    From rexxar-android with MIT License 5 votes vote down vote up
@TargetApi(21)
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
    String fileExtension = MimeTypeMap.getFileExtensionFromUrl(request.getUrl().toString());
    String mimeType = MimeUtils.guessMimeTypeFromExtension(fileExtension);
    return new WebResourceResponse(mimeType, "UTF-8", new ClosedInputStream());
}
 
Example #12
Source File: MainFragment.java    From traccar-manager-android with Apache License 2.0 5 votes vote down vote up
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
    Uri uri = Uri.parse(url);
    String host = uri.getHost();
    if (host != null && host.equals("cdnjs.cloudflare.com")) {
        String path = uri.getPath().substring("/ajax/libs".length());
        try {
            return loadFileFromAssets(url, "cdnjs" + path);
        } catch (IOException e) {
            return null;
        }
    }
    return null;
}
 
Example #13
Source File: InnerFastClient.java    From FastWebView with MIT License 5 votes vote down vote up
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
    // don't intercept request below android 5.0
    // bc we can not get request method, request body and request headers
    // delegate intercept first
    return mDelegate != null ? mDelegate.shouldInterceptRequest(view, url) : null;
}
 
Example #14
Source File: DefaultSonicRuntimeImpl.java    From AgentWeb with Apache License 2.0 5 votes vote down vote up
@Override
public Object createWebResourceResponse(String mimeType, String encoding, InputStream data, Map<String, String> headers) {
    WebResourceResponse resourceResponse =  new WebResourceResponse(mimeType, encoding, data);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        resourceResponse.setResponseHeaders(headers);
    }
    return resourceResponse;
}
 
Example #15
Source File: CaptionManager.java    From android-viewer-for-khan-academy with GNU General Public License v3.0 5 votes vote down vote up
public List<Caption> getCaptions(String youtubeId) {
	Log.d(LOG_TAG, "getCaptions: " + youtubeId);
	
	List<Caption> result = null;
	Dao<Caption, Integer> captionDao = null;
	try {
		captionDao = dataService.getHelper().getDao(Caption.class);
		QueryBuilder<Caption, Integer> q = captionDao.queryBuilder();
		q.where().eq("youtube_id", youtubeId);
		q.orderBy("sub_order", true);
		// TODO : Avoid inserting duplicates in the first place, and do a migration to clean up.
		q.groupBy("sub_order");
		result = q.query();
	} catch (SQLException e) {
		e.printStackTrace();
	}
	if (result != null && result.size() > 0) {
		Log.d(LOG_TAG, " already cached; returning");
		return result;
	}
	
	// If we do not already have the captions, try fetching them.
	// Clients will call this in a background thread, so we can take our time.
	WebResourceResponse response = fetchRawCaptionResponse(youtubeId);
	result = parseAPIResponse(response);
	result = pruneEmptyCaptions(result);
	result = persist(result, youtubeId);
	return result;
}
 
Example #16
Source File: SystemWebViewClient.java    From xmall with MIT License 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
    try {
        // Check the against the whitelist and lock out access to the WebView directory
        // 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);
    }
}
 
Example #17
Source File: InnerFastClient.java    From FastWebView with MIT License 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private WebResourceResponse loadFromWebViewCache(WebResourceRequest request) {
    String scheme = request.getUrl().getScheme().trim();
    String method = request.getMethod().trim();
    if ((TextUtils.equals(SCHEME_HTTP, scheme)
            || TextUtils.equals(SCHEME_HTTPS, scheme))
            && method.equalsIgnoreCase(METHOD_GET)) {
        return mWebViewCache.getResource(request, mWebViewCacheMode, mUserAgent);
    }
    return null;
}
 
Example #18
Source File: IceCreamCordovaWebViewClient.java    From phonegapbootcampsite with MIT License 5 votes vote down vote up
@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);
    }
}
 
Example #19
Source File: SystemWebViewClient.java    From app-icon with MIT License 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
    try {
        // Check the against the whitelist and lock out access to the WebView directory
        // 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);
    }
}
 
Example #20
Source File: NewsFragment.java    From 4pdaClient-plus with Apache License 2.0 5 votes vote down vote up
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
    boolean ad;
    url = IntentActivity.getRedirectUrl(url);
    if (!loadedUrls.containsKey(url)) {
        ad = AdBlocker.isAd(url);
        loadedUrls.put(url, ad);
    } else {
        ad = loadedUrls.get(url);
    }
    return ad ? AdBlocker.createEmptyResource() :
            super.shouldInterceptRequest(view, url);
}
 
Example #21
Source File: SystemWebViewClient.java    From cordova-plugin-app-update-demo with MIT License 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
    try {
        // Check the against the whitelist and lock out access to the WebView directory
        // 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);
    }
}
 
Example #22
Source File: QuickWebviewClient.java    From quickhybrid-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * 加载资源
 * android5.0+支持
 *
 * @param view
 * @param request
 * @return
 */
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
    WebResourceResponse resourceResponse = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        resourceResponse = loadPage.shouldInterceptRequest(view, request.getUrl().toString());
    }
    if (resourceResponse == null) {
        return super.shouldInterceptRequest(view, request);
    } else {
        return resourceResponse;
    }
}
 
Example #23
Source File: GoogleRecaptchaVerifyActivity.java    From v9porn with MIT License 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
    String url = request.getUrl().toString();
    if (url.contains("?__cf_chl_captcha_tk__")) {
        runOnUiThread(() -> webView.evaluateJavascript("javascript:getPostData()", value -> {
            String postData = value.replace("\"", "");
            Logger.t(TAG).d(postData);
            String[] data = postData.split(",");
            Logger.t(TAG).d(data);
            if (data.length >= 4) {
                doPost(data[0], data[1], data[2], data[3]);
            } else {
                showMessage("无法获取POST数据,请刷新重试", TastyToast.ERROR);
                btnRefresh.setText("刷新重试");
            }
        }));
        synchronized (lock) {
            try {
                //等待验证结果
                lock.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return new WebResourceResponse("", "", null);
    }
    Log.d(TAG, url);
    return super.shouldInterceptRequest(view, request);
}
 
Example #24
Source File: VenvyWebView.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
@Nullable
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
    if (mIwebViewClient != null) {
        return mIwebViewClient.shouldInterceptRequest(view, request);
    }
    return super.shouldInterceptRequest(view, request);
}
 
Example #25
Source File: AdBlocksWebViewActivity.java    From AdBlockedWebView-Android with MIT License 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
    boolean ad;
    if (!loadedUrls.containsKey(url)) {
        ad = AdBlocker.isAd(url);
        loadedUrls.put(url, ad);
    } else {
        ad = loadedUrls.get(url);
    }
    return ad ? AdBlocker.createEmptyResource() :
            super.shouldInterceptRequest(view, url);
}
 
Example #26
Source File: HybridCacheManager.java    From HybridCache with MIT License 5 votes vote down vote up
private WebResourceResponse interceptWebResRequest(String requestUrl, Map<String, String> requestHeaders) {
    if (cacheInterceptors.isEmpty()) {
        return null;
    }
    BaseInterceptor.Chain chain = new DefaultInterceptorChain(cacheInterceptors, requestUrl, requestHeaders, 0);
    return chain.proceed(requestUrl, requestHeaders);
}
 
Example #27
Source File: BrowserLayout.java    From SprintNBA with Apache License 2.0 5 votes vote down vote up
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
    String url = request.getUrl().toString().toLowerCase();
    LogUtils.d("req -- "+ url);
    if (!ADFilterUtils.hasAd(getContext(), url)) {
        return super.shouldInterceptRequest(view, request);
    } else {
        LogUtils.w("ad:"+url);
        return new WebResourceResponse(null, null, null);
    }
}
 
Example #28
Source File: WebViewLocalServer.java    From webview-local-server with Apache License 2.0 5 votes vote down vote up
/**
 * Attempt to retrieve the WebResourceResponse associated with the given <code>request</code>.
 * This method should be invoked from within
 * {@link android.webkit.WebViewClient#shouldInterceptRequest(android.webkit.WebView,
 * android.webkit.WebResourceRequest)}.
 *
 * @param request the request to process.
 * @return a response if the request URL had a matching handler, null if no handler was found.
 */
public WebResourceResponse shouldInterceptRequest(WebResourceRequest request) {
    PathHandler handler;
    synchronized (uriMatcher) {
        handler = (PathHandler) uriMatcher.match(request.getUrl());
    }
    if (handler == null) {
        return null;
    }

    return new WebResourceResponse(handler.getMimeType(), handler.getEncoding(),
            handler.getStatusCode(), handler.getReasonPhrase(), handler.getResponseHeaders(),
            new LollipopLazyInputStream(handler, request));
}
 
Example #29
Source File: FolioPageFragment.java    From ankihelper with GNU General Public License v3.0 5 votes vote down vote up
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
    if (url.toLowerCase().contains("/favicon.ico")) {
        try {
            return new WebResourceResponse("image/png", null, null);
        } catch (Exception e) {
            Log.e(LOG_TAG, "shouldInterceptRequest failed", e);
        }
    }
    return null;
}
 
Example #30
Source File: IceCreamCordovaWebViewClient.java    From IoTgo_Android_App with MIT License 5 votes vote down vote up
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
    try {
        // Check the against the whitelist and lock out access to the WebView directory
        // Changing this will cause problems for your application
        if (isUrlHarmful(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) || needsKitKatContentUrlFix(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);
    }
}