-
-
Save chethann/3edcc6f702430e42fe4f018546766481 to your computer and use it in GitHub Desktop.
@Override | |
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) { | |
String resourceUrl = request.getUrl().toString(); | |
String fileExtension = WebviewResourceMappingHelper.getInstance().getFileExt(resourceUrl); | |
if(WebviewResourceMappingHelper.getInstance().getOverridableExtensions().contains(fileExtension)){ | |
String encoding = "UTF-8"; | |
String assetName = WebviewResourceMappingHelper.getInstance().getLocalAssetPath(resourceUrl); | |
if (StringUtils.isNotEmpty(assetName)) { | |
String mimeType = WebviewResourceMappingHelper.getInstance().getMimeType(fileExtension); | |
if (StringUtils.isNotEmpty(mimeType)) { | |
try { | |
Log.e(TAG, assetName); | |
return WebviewResourceMappingHelper.getWebResourceResponseFromAsset(assetName, mimeType, encoding); | |
} catch (IOException e) { | |
return super.shouldInterceptRequest(view, request); | |
} | |
} | |
} | |
String localFilePath = WebviewResourceMappingHelper.getInstance().getLocalFilePath(resourceUrl); | |
if (StringUtils.isNotEmpty(localFilePath)) { | |
String mimeType = WebviewResourceMappingHelper.getInstance().getMimeType(fileExtension); | |
if(StringUtils.isNotEmpty(mimeType)){ | |
try { | |
Log.e(TAG, localFilePath); | |
return WebviewResourceMappingHelper.getWebResourceResponseFromFile(localFilePath, mimeType, encoding); | |
} catch (FileNotFoundException e) { | |
return super.shouldInterceptRequest(view,request); | |
} | |
} | |
} | |
} | |
if (fileExtension.endsWith("jpg")) { | |
try { | |
InputStream inputStream = ImageUtils.readFromCacheSync(resourceUrl); | |
if (inputStream != null) { | |
return new WebResourceResponse("image/jpg", "UTF-8", inputStream); | |
} | |
} catch (Exception e) { | |
return super.shouldInterceptRequest(view,request); | |
} | |
} | |
return super.shouldInterceptRequest(view,request); | |
} |
Yes, could you provide the source code of WebviewResourceMappingHelper
?
Hi, I have added code for WebviewResourceMappingHelper class. Hope this will help.
https://gist.github.com/chethann/bfa24f3517864a4be39757b987d58a17
Hi, I read your code and have a question, how can you improve webview loading since you cannot replace the remote resource with local resource when you surf others website that not yours?
AFAIK when we surf others website we cannot do much as they define there very own cache policy and web view by default manage cache throughout the lifetime of running a application.
Yes, this solution is designed to be used inside a hybrid app (some pages in native and some in web)
please share ImageUtils
Hi,
Can you share your Intentservice class where you are hitting json file and syncing resouces
Can you provide WebviewResourceMappingHelper?