Skip to content

Instantly share code, notes, and snippets.

@chethann
Created November 24, 2017 09:19
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save chethann/bfa24f3517864a4be39757b987d58a17 to your computer and use it in GitHub Desktop.
Save chethann/bfa24f3517864a4be39757b987d58a17 to your computer and use it in GitHub Desktop.
WebviewResourceMappingHelper Sample Code
package com.test.android.helpers;
import android.os.Build;
import android.webkit.WebResourceResponse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.test.android.Application;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class WebviewResourceMappingHelper {
private static WebviewResourceMappingHelper instance;
private List<LocalAssetMapModel> localAssetMapModelList;
private List<String> overridableExtensions = new ArrayList<>(Arrays.asList("js", "css", "png", "jpg", "woff", "ttf", "eot", "ico"));
private WebviewResourceMappingHelper(){
}
public static WebviewResourceMappingHelper getInstance(){
if(instance == null){
instance = new WebviewResourceMappingHelper();
}
return instance;
}
public String getLocalAssetPath(String url){
if(StringUtils.isEmpty(url)){
return "";
}
if(localAssetMapModelList == null){
localAssetMapModelList = getLocalAssetList();
}
if(CollectionUtils.isNotEmpty(localAssetMapModelList)){
for(LocalAssetMapModel localAssetMapModel : localAssetMapModelList){
if(localAssetMapModel.url.equals(url)){
return localAssetMapModel.asset_url;
}
}
}
return "";
}
public String getLocalFilePath(String url){
String localFilePath = "";
String fileNameForUrl = getLocalFileNameForUrl(url);
if(StringUtils.isNotEmpty(fileNameForUrl) && fileExists(fileNameForUrl)){
localFilePath = getFileFullPath(fileNameForUrl);
}
return localFilePath;
}
public String getLocalFileNameForUrl(String url){
String localFileName = "";
String[] parts = url.split("/");
if(parts.length > 0){
localFileName = parts[parts.length-1];
}
return localFileName;
}
private boolean fileExists(String fileName){
String path = Application.getInstance()
.getFilesDir() + "/cart/" + fileName;
return new File(path).exists();
}
private String getFileFullPath(String relativePath){
return Application.getInstance().getFilesDir() + "/cart/" + relativePath;
}
private List<LocalAssetMapModel> getLocalAssetList(){
List<LocalAssetMapModel> localAssetMapModelList = new ArrayList<>();
String pageData = null;
try {
pageData = ResourceAccessHelper.getJsonData(Application.getCurrentInstance(), "web-assets/map.json");
} catch (IOException e) {
}
if(pageData !=null){
Type listType = new TypeToken<ArrayList<LocalAssetMapModel>>() {
}.getType();
localAssetMapModelList = new Gson().fromJson(pageData,listType);
}
pageData = null;
try {
pageData = ResourceAccessHelper.getJsonData(Application.getCurrentInstance(), "web-assets/fonts-map.json");
} catch (IOException e) {
}
if(pageData !=null){
Type listType = new TypeToken<ArrayList<LocalAssetMapModel>>() {
}.getType();
List<LocalAssetMapModel> fontsMap = new Gson().fromJson(pageData,listType);
localAssetMapModelList.addAll(fontsMap);
}
return localAssetMapModelList;
}
public List<String> getOverridableExtensions(){
return overridableExtensions;
}
public String getFileExt(String fileName) {
return fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
}
public String getMimeType(String fileExtension){
String mimeType = "";
switch (fileExtension){
case "css" :
mimeType = "text/css";
break;
case "js" :
mimeType = "text/javascript";
break;
case "png" :
mimeType = "image/png";
break;
case "jpg" :
mimeType = "image/jpeg";
break;
case "ico" :
mimeType = "image/x-icon";
break;
case "woff" :
case "ttf" :
case "eot" :
mimeType = "application/x-font-opentype";
break;
}
return mimeType;
}
public static WebResourceResponse getWebResourceResponseFromAsset(String assetPath, String mimeType, String encoding) throws IOException{
InputStream inputStream = Application.getInstance().getAssets().open(assetPath);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int statusCode = 200;
String reasonPhase = "OK";
Map<String, String> responseHeaders = new HashMap<String, String>();
responseHeaders.put("Access-Control-Allow-Origin", "*");
return new WebResourceResponse(mimeType, encoding, statusCode, reasonPhase, responseHeaders, inputStream);
}
return new WebResourceResponse(mimeType, encoding, inputStream);
}
public static WebResourceResponse getWebResourceResponseFromFile(String filePath, String mimeType, String encoding) throws FileNotFoundException {
File file = new File(filePath);
FileInputStream fileInputStream = new FileInputStream(file);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int statusCode = 200;
String reasonPhase = "OK";
Map<String, String> responseHeaders = new HashMap<String, String>();
responseHeaders.put("Access-Control-Allow-Origin","*");
return new WebResourceResponse(mimeType, encoding, statusCode, reasonPhase, responseHeaders, fileInputStream);
}
return new WebResourceResponse(mimeType, encoding, fileInputStream);
}
private class LocalAssetMapModel{
String url;
String asset_url;
}
}
@MultiplexerLab
Copy link

Dear brother, where's the ResourceAccessHelper file or code?

@Abhinav-Biswas
Copy link

Indeed where is the ResourceAccessHelper class?

@avadhesh18
Copy link

Please provide the ResourceAccessHelper class anybody?

@agradipyahoo
Copy link

agradipyahoo commented Aug 22, 2019

import android.content.Context;

import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**

  • @author a0s
    */
    public class ResourceAccessHelper {
    private static Map<String, String> resourceMap;

    public static String getJsonData(Context context, String filename) throws IOException {
    if(resourceMap == null){
    resourceMap = new HashMap<>();
    }
    if(resourceMap.containsKey(filename)){
    return resourceMap.get(filename);
    }
    InputStream inputStream = context.getAssets().open(filename);
    String jsonStr = IOUtils.toString(inputStream);
    resourceMap.put(filename, jsonStr);
    return jsonStr;
    }
    }

@bidhan616
Copy link

can not resolve 'test' for import com.test.android.Application .
please help me sir !

@maktaduai
Copy link

maktaduai commented Feb 21, 2020

can not resolve 'test' for import com.test.android.Application .
please help me sir !

I'm not an expert but obviously you have to make your own Application class extending [Android Application base class](https://developer.android.com/reference/android/app/Application) and initialize whatever you need to do in that App (Application class) that you created. Then, put that 'Application' in your manifest file as thus,

 <application>
 android:name=".YourCustomAppClass"
 android:icon="@mipmap/ic_launcher"
 android:label="@string/app_name"
 android:supportsRtl="true"....
   <activity
         android:name="....."/>
</application>

@chethann
Copy link
Author

You need to change com.test.android.Application to appropriate package name.

@bidhan616
Copy link

I replace "import com.test.android.Application" to "import android.app.Application" which is not working on Application.getInstance() and Application.getCurrentInstance()
please help me to solve it sir !

@maktaduai
Copy link

maktaduai commented Feb 27, 2020

He means, change it to whatever package name you name your app. Change it to package com.yourdomainname.nameofyourapp.Application;. It should be on top of your class.

@lalitgoswami-db
Copy link

Hey where can i get ImageUtils?

@ChoiJeongHyun
Copy link

Good morning.
web-assets / map.json
File and
Web asset/font map.json

Where are the files?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment