Skip to content

Instantly share code, notes, and snippets.

@chethann
chethann / FrescoSyncCacheRead.java
Created November 28, 2016 15:59
Code to get InputStream of an image synchronously in Fresco
public static InputStream readFromCacheSync(String imageUrl) {
CacheKey cacheKey = DefaultCacheKeyFactory.getInstance().getEncodedCacheKey(ImageRequest.fromUri(imageUrl), null);
StagingArea stagingArea = StagingArea.getInstance();
EncodedImage encodedImage = stagingArea.get(cacheKey);
if (encodedImage != null) {
return encodedImage.getInputStream();
}
try {
return readFromDiskCache(cacheKey);
@chethann
chethann / WebResourceResponse.java
Last active May 4, 2021 03:50
Sample code to create WebResourceResponse
public WebResourceResponse getWebResourceResponseFromAsset(String assetPath, String mimeType, String encoding) throws IOException{
InputStream inputStream = getActivity().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);
@chethann
chethann / WebResourceResponse.java
Last active September 29, 2020 18:49
Sample code of WebResourceResponse to load WebView resources from local assets!
@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)) {
@chethann
chethann / FrescoUtils.java
Created July 28, 2016 06:28
Fresco (android image loading library) useful utility functions
package com.android.fresco;
import org.apache.commons.lang3.StringUtils;
import android.graphics.Bitmap;
import android.net.Uri;
import android.util.Log;
import com.facebook.cache.common.CacheKey;
import com.facebook.cache.common.SimpleCacheKey;
@chethann
chethann / simple hovercard
Created September 17, 2014 08:36
Script to create a simple hovercard with facebook like style!
<html>
<head>
<title>Simple hovercard</title>
<script src="http://code.jquery.com/jquery-latest.min.js"
type="text/javascript"></script>
<script>
$(function(){
$('#hoverbox').mouseleave(function(){
$('#hoverbox').hide();