Created
November 24, 2017 09:19
-
-
Save chethann/bfa24f3517864a4be39757b987d58a17 to your computer and use it in GitHub Desktop.
WebviewResourceMappingHelper Sample Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
You need to change com.test.android.Application to appropriate package name.
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 !
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.
Hey where can i get ImageUtils?
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
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,