Skip to content

Instantly share code, notes, and snippets.

@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();
@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 / 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 / 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 / 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);
{
"hash": "5d5a03b6cc4edc0cc7d44f46c8ad3b30",
"urls": [
"http://myntra.myntassets.com/checkout/build/css/mobile-cart-android-v2-b0533de818b7c79a0c51249a869214dca3f09d53.zip.css",
"http://myntra.myntassets.com/checkout/build/js/cart-app-android-v2-319197031848b6cc165d220a48a4c96764cb5cc6.zip.js",
"http://myntra.myntassets.com/checkout/build/js/common-app-android-v2-b001242bcde0440a307bd801bc67f9cb54869924.zip.js",
"http://myntra.myntassets.com/checkout/build/js/common-app-v2-b001242bcde0440a307bd801bc67f9cb54869924.zip.js",
"http://myntra.myntassets.com/checkout/images/checkout_sprite_2x_v22.png",
"http://myntra.myntassets.com/checkout/images/checkout_sprite_v22.png",
"http://myntra.myntassets.com/checkout/images/sprite-global_v70.png",
private static void downloadBitmap(ImageRequest imageRequest, Context context, final IBitmapDownloader iBitmapDownloader) {
DataSource<CloseableReference<CloseableImage>> dataSourceImage = Fresco.getImagePipeline().fetchDecodedImage(imageRequest, context);
DataSubscriber<CloseableReference<CloseableImage>> dataSubscriberImage = new BaseDataSubscriber<CloseableReference<CloseableImage>>() {
@Override
public void onNewResultImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
CloseableReference<CloseableImage> imageReference = dataSource.getResult();
if (imageReference != null) {
iBitmapDownloader.onSuccess(imageReference);
}
else{
CloseableReference<CloseableImage> imageRef = imageReference.clone();
imageRefMap.put(imageUrl,imageRef);
CloseableImage image = imageRef.get();
final Bitmap bitmap = ((CloseableBitmap) image).getUnderlyingBitmap();
Iterator<Map.Entry<String, CloseableReference<CloseableImage>>> entries = imageRefMap.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<String, CloseableReference<CloseableImage>> entry = entries.next();
CloseableReference<CloseableImage> closeableRef = entry.getValue();
if(closeableRef != null){
closeableRef.close();
}
}
try {
CacheKey cacheKey = new SimpleCacheKey(url);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
final byte[] byteArray = stream.toByteArray();
Fresco.getImagePipelineFactory().getMainDiskStorageCache().insert(cacheKey, new WriterCallback() {
@Override
public void write(OutputStream outputStream) throws IOException {
outputStream.write(byteArray);
}