Android Open Source - lightbox-android-webservices Bitmap Cache






From Project

Back to project page lightbox-android-webservices.

License

The source code is released under:

Apache License

If you think the Android project lightbox-android-webservices 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

/**
 * Copyright (c) 2012 Lightbox// w w w .j  a v a 2 s  .co m
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lightbox.android.cache;

import java.io.File;

import android.graphics.Bitmap;
import android.util.Log;

import com.googlecode.concurrentlinkedhashmap.Weigher;
import com.lightbox.android.bitmap.BitmapFileCleanerTask;
import com.lightbox.android.bitmap.BitmapUtils;
import com.lightbox.android.utils.debug.DebugLog;

/** 
 * BitmapCache 
 * @author Fabien Devos
 */
public class BitmapCache extends AbstractCache<Bitmap, Bitmap> {
  /** Used to tag logs */
  // @SuppressWarnings("unused")
  private static final String TAG = "BitmapCache";
  
  private static final int DEFAULT_MAX_MEM_CACHE_SIZE = 128 * 128 * 4 * 100; // 100 128x128 bitmaps (around 6.5 Mb)

  private int mCurrentMaxMemCacheSize = DEFAULT_MAX_MEM_CACHE_SIZE;

  //----------------------------------------------------------------------------
  // Singleton pattern
  private BitmapCache() {
    super((new Config<Bitmap>())
        .setMaximumWeightedCapacityInMemory(DEFAULT_MAX_MEM_CACHE_SIZE)
        .setWeigher(new BitmapWeigher()));
  }

  /** BitmapCacheHolder is loaded on the first execution of BitmapCache.getInstance() 
   * or the first access to BitmapCacheHolder.INSTANCE, not before. */
  private static class BitmapCacheHolder {
    private static final BitmapCache INSTANCE = new BitmapCache();
  }

  /** @return a unique instance of the class */
  public static BitmapCache getInstance() {
    return BitmapCacheHolder.INSTANCE;
  }

  /** Not supported
   * @throws CloneNotSupportedException (every time) */
  public Object clone() throws CloneNotSupportedException {
    // to prevent any kind of cheating
    throw new CloneNotSupportedException();
  }
  //----------------------------------------------------------------------------
  
  //----------------------------------------------
  // Disk cache methods
  
  @Override
  public boolean existOnDisk(String absoluteFileName) {
    return (new File(absoluteFileName)).exists();
  }
  
  @Override
  public Result<Bitmap> getFromDisk(String absoluteFileName, Object... objects) {
    Result<Bitmap> result = null;
    
    android.graphics.Bitmap.Config config = (objects.length == 0) ? null : (android.graphics.Bitmap.Config) objects[0];
        
    if (absoluteFileName != null) {
      Bitmap bitmap = BitmapUtils.readBitmapFromFile(absoluteFileName, config);
      // The bitmap never expires
      result = new Result<Bitmap>(bitmap, System.currentTimeMillis());      
    } else {
      result = new Result<Bitmap>(null, 0);
    }
    return result;
  }
  
  public void decreaseMemoryCacheSize() {
    clearMemory();
    mCurrentMaxMemCacheSize = (int) (mCurrentMaxMemCacheSize * 0.7f);
    resetMemoryCache(mCurrentMaxMemCacheSize);
  }

  @Override
  public void putOnDisk(String absoluteFileName, Bitmap bitmap) {
    if (absoluteFileName != null && bitmap != null) {
      try {
        BitmapUtils.writeBitmapInFile(absoluteFileName, bitmap);
        
        DebugLog.d(TAG, "Bitmap saved on disk. Absolute file name: %s", absoluteFileName);
      } catch (Exception e) {
        // ignore exception since this is just a cache
        Log.w(TAG, "Unable to save bitmap", e);
      }
    }
  }
  
  @Override
  public void clearDisk() {
    // TODO
    //FileUtils.deleteQuietly(new File(Photo.CACHE_DIRECTORY));
  }
  
  @Override
  public void startDiskCleanup() {
    BitmapFileCleanerTask cleanupTask = new BitmapFileCleanerTask();
    cleanupTask.execute();
  }
  
  //----------------------------------------------
  // Get from memory
  @Override
  public Bitmap getFromMemory(String key) {
    Bitmap bitmap = super.getFromMemory(key);
    if (bitmap != null && bitmap.isRecycled()) {
      // Good coding should mean this should never happen!
      DebugLog.d(TAG, "Found recycled bitmap in the cache! Key="+key);
      removeFromMemory(key);
      return null;
    }
    
    return bitmap;
  }

  
  /************************************************************
   * BitmapWeigher 
   */
  private static class BitmapWeigher implements Weigher<Bitmap> {
    @Override
    public int weightOf(Bitmap bitmap) {
      return bitmap.getWidth() * bitmap.getHeight() * (bitmap.getConfig() == Bitmap.Config.ARGB_8888 ? 4 : 2);//4;
    }
  }

}




Java Source Code List

com.lightbox.android.bitmap.BitmapFileCleanerTask.java
com.lightbox.android.bitmap.BitmapLoaderListener.java
com.lightbox.android.bitmap.BitmapLoaderTask.java
com.lightbox.android.bitmap.BitmapLoader.java
com.lightbox.android.bitmap.BitmapSize.java
com.lightbox.android.bitmap.BitmapSource.java
com.lightbox.android.bitmap.BitmapUtils.java
com.lightbox.android.cache.AbstractCache.java
com.lightbox.android.cache.ApiCache.java
com.lightbox.android.cache.BitmapCache.java
com.lightbox.android.cache.Cache.java
com.lightbox.android.data.ClearAndSaveBatchTask.java
com.lightbox.android.data.Data.java
com.lightbox.android.data.DatabaseCleanerTask.java
com.lightbox.android.data.DatabaseHelper.java
com.lightbox.android.data.DeleteBatchTask.java
com.lightbox.android.data.SaveBatchTask.java
com.lightbox.android.io.RandomAccessFileOutputStream.java
com.lightbox.android.lifecycle.LifeCycleListener.java
com.lightbox.android.lifecycle.ManagedLifeCycleActivity.java
com.lightbox.android.location.LocationHelper.java
com.lightbox.android.location.LocationListener.java
com.lightbox.android.network.HttpHelper.java
com.lightbox.android.network.NetworkUtils.java
com.lightbox.android.operations.AbstractOperation.java
com.lightbox.android.operations.CachedOperation.java
com.lightbox.android.operations.DeleteOperation.java
com.lightbox.android.operations.FailureOperation.java
com.lightbox.android.operations.ModificationNetworkOnlyOperation.java
com.lightbox.android.operations.NetworkOnlyOperation.java
com.lightbox.android.operations.OperationListener.java
com.lightbox.android.operations.OperationTask.java
com.lightbox.android.operations.Operation.java
com.lightbox.android.operations.Retrievable.java
com.lightbox.android.operations.RetrieveOperation.java
com.lightbox.android.operations.SaveOperation.java
com.lightbox.android.operations.Updatable.java
com.lightbox.android.tasks.BackgroundTaskWeak.java
com.lightbox.android.tasks.BackgroundTask.java
com.lightbox.android.tasks.DefaultExecutor.java
com.lightbox.android.utils.AndroidUtils.java
com.lightbox.android.utils.Base64.java
com.lightbox.android.utils.IntentUtils.java
com.lightbox.android.utils.MediaUtils.java
com.lightbox.android.utils.ResUtils.java
com.lightbox.android.utils.debug.DebugLifeCycleListener.java
com.lightbox.android.utils.debug.DebugLog.java
com.lightbox.android.views.RemoteImageView.java
com.lightbox.android.views.RemoteThumbImageView.java
com.lightbox.android.webservices.processors.GenerationException.java
com.lightbox.android.webservices.processors.JacksonProcessor.java
com.lightbox.android.webservices.processors.ParsingException.java
com.lightbox.android.webservices.processors.Processor.java
com.lightbox.android.webservices.requests.ApiRequestFactory.java
com.lightbox.android.webservices.requests.ApiRequestListener.java
com.lightbox.android.webservices.requests.ApiRequestTask.java
com.lightbox.android.webservices.requests.ApiRequest.java
com.lightbox.android.webservices.requests.ApiRequests.java
com.lightbox.android.webservices.requests.JacksonApiRequestFactory.java
com.lightbox.android.webservices.responses.ApiException.java
com.lightbox.android.webservices.responses.ApiResponse.java
com.lightbox.tweetsnearby.TweetsNearbyApplication.java
com.lightbox.tweetsnearby.activities.MainActivity.java
com.lightbox.tweetsnearby.activities.PickPlaceActivity.java
com.lightbox.tweetsnearby.model.Tweet.java
com.lightbox.tweetsnearby.model.Venue.java
com.lightbox.tweetsnearby.responses.foursquare.FoursquareApiResponse.java
com.lightbox.tweetsnearby.responses.foursquare.VenueListResponse.java
com.lightbox.tweetsnearby.responses.twitter.TweetListResponse.java
com.lightbox.tweetsnearby.responses.twitter.TwitterApiResponse.java