Android Open Source - TileView Tile Cache






From Project

Back to project page TileView.

License

The source code is released under:

MIT License

If you think the Android project TileView listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.qozix.tileview.tiles;
//from   w  ww.j a  v  a2  s  . com
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.support.v4.util.LruCache;

import com.jakewharton.DiskLruCache;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class TileCache {
  
  private static final int DISK_CACHE_CAPACITY = 8 * 1024;
  private static final int IO_BUFFER_SIZE = 8 * 1024;
  
  private static final int COMPRESSION_QUALITY = 40;
  
  private static final BitmapFactory.Options BITMAPFACTORY_OPTIONS = new BitmapFactory.Options();
  static {
    BITMAPFACTORY_OPTIONS.inPreferredConfig = Bitmap.Config.RGB_565;
  }

  private MessageDigest digest;
  
  private LruCache<String, Bitmap> memoryCache;
  private DiskLruCache diskCache;

  // TODO: register local broadcast receiver to destroy the cache during onDestroy of containing Activity
  public TileCache( final Context context ) {
    try {
      digest = MessageDigest.getInstance( "MD5" );
    } catch ( NoSuchAlgorithmException e1 ) {
      
    }
    // in memory cache
    final int memory = (int) ( Runtime.getRuntime().maxMemory() / 1024 );
    final int size = memory / 8;
    memoryCache = new LruCache<String, Bitmap>( size ) {
      @Override
      protected int sizeOf( String key, Bitmap bitmap ) {
        // The cache size will be measured in kilobytes rather than number of items.
        // emulate bitmap.getByteCount for APIs less than 12
        int byteCount = bitmap.getRowBytes() * bitmap.getHeight();
        return byteCount / 1024;
      }
    };
    // disk cache
    new Thread(new Runnable(){
      @Override
      public void run(){
        File cacheDirectory = new File( context.getCacheDir().getPath() + File.separator + "com/qozix/mapview" );
        try {
          diskCache = DiskLruCache.open( cacheDirectory, 1, 1, DISK_CACHE_CAPACITY );
        } catch ( IOException e ) {
          
        }
      }
    }).start();
  }

  public void addBitmap( String key, Bitmap bitmap ) {
    addBitmapToMemoryCache( key, bitmap );
    addBitmapToDiskCache( key, bitmap );
  }
  
  public Bitmap getBitmap( String key ) {
    Bitmap bitmap = getBitmapFromMemoryCache( key );
    if ( bitmap == null ) {
      bitmap = getBitmapFromDiskCache( key );
    }
    return bitmap;
  }
  
  public void destroy(){
    memoryCache.evictAll();
    memoryCache = null;
    new Thread(new Runnable(){
      @Override
      public void run(){
        try {
          diskCache.delete();
          diskCache = null;
        } catch ( IOException e ) {
          
        }
      }
    }).start();    
  }
  
  public void clear() {
    memoryCache.evictAll();
    new Thread(new Runnable(){
      @Override
      public void run(){
        try {
          diskCache.delete();
        } catch ( IOException e ) {
          
        }
      }
    }).start();    
  }
  
  private void addBitmapToMemoryCache( String key, Bitmap bitmap ) {
    if ( key == null || bitmap == null ) {
          return;
      }
    if ( getBitmapFromMemoryCache( key ) == null ) {
      memoryCache.put( key, bitmap );
    }
  }

  private Bitmap getBitmapFromMemoryCache( String key ) {
    return memoryCache.get( key );
  }
  
  private void addBitmapToDiskCache( String key, Bitmap bitmap ) {
    if ( diskCache == null ) {
      return;
    }
    key = getMD5( key );
    DiskLruCache.Editor editor = null;
    try {
      editor = diskCache.edit( key );
      if ( editor == null ) {
        return;
      }
      OutputStream output = null;
      try {
        output = new BufferedOutputStream( editor.newOutputStream( 0 ), IO_BUFFER_SIZE );
        boolean compressed = bitmap.compress( CompressFormat.JPEG, COMPRESSION_QUALITY, output );
        if ( compressed ) {
          diskCache.flush();
          editor.commit();
        } else {
          editor.abort();
        }
      } finally {
        if ( output != null ) {
          output.close();
        }
      }
    } catch ( Exception e ) {
      try {
        if ( editor != null ) {
          editor.abort();
        }
      } catch ( IOException io ) {
        
      }
    }
  }
  
  private Bitmap getBitmapFromDiskCache( String key ) {
    if ( diskCache == null ) {
      return null;
    }
    key = getMD5( key );
    Bitmap bitmap = null;
    DiskLruCache.Snapshot snapshot = null;
    try {
      snapshot = diskCache.get( key );
      if ( snapshot == null ) {
        return null;
      }
      final InputStream input = snapshot.getInputStream( 0 );
      if ( input != null ) {
        BufferedInputStream buffered = new BufferedInputStream( input, IO_BUFFER_SIZE );
        bitmap = BitmapFactory.decodeStream( buffered, null, BITMAPFACTORY_OPTIONS );
      }
    } catch ( Exception e ) {
      
    } finally {
      if ( snapshot != null ) {
        snapshot.close();
      }
    }
    return bitmap;
  }
  
  private String getMD5( String fileName ) {
    if ( digest != null ) {
      digest.update( fileName.getBytes(), 0, fileName.length() );
      return new BigInteger( 1, digest.digest() ).toString( 16 );
    }
    // if digest is unavailable, at least make some attempt at an acceptable filename
    return fileName.replace("[^a-z0-9_-]", "_");
  }
}




Java Source Code List

com.qozix.animation.AnimationListener.java
com.qozix.animation.Animator.java
com.qozix.animation.TweenHandler.java
com.qozix.animation.TweenListener.java
com.qozix.animation.Tween.java
com.qozix.animation.easing.EasingEquation.java
com.qozix.animation.easing.Linear.java
com.qozix.animation.easing.Strong.java
com.qozix.layouts.AnchorLayout.java
com.qozix.layouts.FixedLayout.java
com.qozix.layouts.ScalingLayout.java
com.qozix.layouts.StaticLayout.java
com.qozix.layouts.TranslationLayout.java
com.qozix.layouts.ZoomPanLayout.java
com.qozix.os.AsyncTask.java
com.qozix.tileview.TileView.java
com.qozix.tileview.detail.DetailLevelEventListener.java
com.qozix.tileview.detail.DetailLevelPatternParserDefault.java
com.qozix.tileview.detail.DetailLevelPatternParser.java
com.qozix.tileview.detail.DetailLevelSet.java
com.qozix.tileview.detail.DetailLevelSetupListener.java
com.qozix.tileview.detail.DetailLevel.java
com.qozix.tileview.detail.DetailManager.java
com.qozix.tileview.geom.PositionManager.java
com.qozix.tileview.graphics.BitmapDecoderAssets.java
com.qozix.tileview.graphics.BitmapDecoderHttp.java
com.qozix.tileview.graphics.BitmapDecoder.java
com.qozix.tileview.hotspots.HotSpotEventListener.java
com.qozix.tileview.hotspots.HotSpotManager.java
com.qozix.tileview.hotspots.HotSpot.java
com.qozix.tileview.markers.CalloutManager.java
com.qozix.tileview.markers.MarkerEventListener.java
com.qozix.tileview.markers.MarkerManager.java
com.qozix.tileview.paths.DrawablePath.java
com.qozix.tileview.paths.PathHelper.java
com.qozix.tileview.paths.PathManager.java
com.qozix.tileview.samples.SampleManager.java
com.qozix.tileview.tiles.TileCache.java
com.qozix.tileview.tiles.TileManager.java
com.qozix.tileview.tiles.TileRenderHandler.java
com.qozix.tileview.tiles.TileRenderListener.java
com.qozix.tileview.tiles.TileRenderTask.java
com.qozix.tileview.tiles.TileTransitionListener.java
com.qozix.tileview.tiles.Tile.java
com.qozix.tileview.tiles.selector.TileSetSelectorByRange.java
com.qozix.tileview.tiles.selector.TileSetSelectorClosest.java
com.qozix.tileview.tiles.selector.TileSetSelectorMinimalUpScale.java
com.qozix.tileview.tiles.selector.TileSetSelector.java
com.qozix.utils.ViewCurator.java
com.qozix.widgets.Scroller.java