Android Open Source - GalDroid Image Cache






From Project

Back to project page GalDroid.

License

The source code is released under:

GNU General Public License

If you think the Android project GalDroid 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

/*
 * GalDroid - a webgallery frontend for android
 * Copyright (C) 2011  Raptor 2101 [raptor2101@gmx.de]
 *    /*w  w  w. j av a2s  .  c o m*/
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.  
 */

package de.raptor2101.GalDroid.WebGallery;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.ref.WeakReference;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Hashtable;

import de.raptor2101.GalDroid.Config.GalDroidPreference;

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

public class ImageCache {
  private static final String ClassTag = "GalleryCache";
  private static long mMaxCacheSize = 50 * 1024 * 1024; // convert MByte to
  // Byte
  private File mCacheDir;
  private MessageDigest mDigester;
  private Hashtable<String, WeakReference<Bitmap>> mCachedBitmaps;

  public ImageCache(Context context) throws NoSuchAlgorithmException {
    Log.d(ClassTag, "Recreate cache");
    if (mCacheDir == null) {
      mCacheDir = context.getExternalCacheDir();

      if (mCacheDir == null) {
        mCacheDir = context.getCacheDir();
      }
    }
    if (!mCacheDir.exists()) {
      mCacheDir.mkdirs();
    }
    mCachedBitmaps = new Hashtable<String, WeakReference<Bitmap>>(50);
    mDigester = MessageDigest.getInstance("MD5");
  }

  public Bitmap getBitmap(String sourceUrl) {
    String hash = buildHash(sourceUrl);
    WeakReference<Bitmap> reference;

    synchronized (mCachedBitmaps) {
      reference = mCachedBitmaps.get(hash);
    }

    if (reference == null) {
      Log.d(ClassTag, "Cache Miss Hash Reference " + sourceUrl);
      return null;
    }
    Bitmap bitmap = reference.get();
    if (bitmap == null || bitmap.isRecycled()) {
      if (bitmap != null) {
        Log.d(ClassTag, "Bitmap Recycled " + sourceUrl);
      } else {
        Log.d(ClassTag, "Cache Miss Object Reference " + sourceUrl);
      }

      synchronized (mCachedBitmaps) {
        mCachedBitmaps.remove(hash);
      }
      return null;
    }
    Log.d(ClassTag, "Cache Hit Reference " + sourceUrl);
    return bitmap;
  }

  public void storeBitmap(String sourceUrl, Bitmap bitmap) {
    String hash = buildHash(sourceUrl);
    File cacheFile = new File(mCacheDir, hash);

    synchronized (mCachedBitmaps) {
      mCachedBitmaps.put(hash, new WeakReference<Bitmap>(bitmap));
    }

    Log.d(ClassTag, "Bitmap referenced " + sourceUrl);

    if (cacheFile.exists()) {
      cacheFile.delete();
    }
    try {
      cacheFile.createNewFile();

      FileOutputStream output = new FileOutputStream(cacheFile);
      bitmap.compress(Bitmap.CompressFormat.JPEG, 100, output);
      output.close();
      Log.d(ClassTag, "Bitmap stored local " + sourceUrl);
      GalDroidPreference.accessCacheObject(hash, cacheFile.length());
    } catch (IOException e) {
      Log.e(ClassTag, "Error while storing");
    }
  }

  public void cacheBitmap(String sourceUrl, Bitmap bitmap) {
    String hash = buildHash(sourceUrl);
    synchronized (mCachedBitmaps) {
      mCachedBitmaps.put(hash, new WeakReference<Bitmap>(bitmap));
    }
    Log.d(ClassTag, "Bitmap referenced " + sourceUrl);
  }

  public FileInputStream getFileStream(String sourceUrl) {
    String hash = buildHash(sourceUrl);
    File cacheFile = new File(mCacheDir, hash);
    if (GalDroidPreference.cacheObjectExists(hash) && cacheFile.exists()) {
      try {
        Log.d(ClassTag, "Cache Hit " + sourceUrl);
        GalDroidPreference.accessCacheObject(hash, cacheFile.length());
        return new FileInputStream(cacheFile);

      } catch (IOException e) {
        Log.e(ClassTag, "Error while accessing");
        return null;
      }
    }
    Log.d(ClassTag, "Cache Mis " + sourceUrl);
    return null;
  }

  public File getFile(String sourceUrl) {
    String hash = buildHash(sourceUrl);
    return new File(mCacheDir, hash);
  }

  public OutputStream createCacheFile(String sourceUrl) throws IOException {
    String hash = buildHash(sourceUrl);
    File cacheFile = new File(mCacheDir, hash);

    if (cacheFile.exists()) {
      cacheFile.delete();
    }

    try {
      Log.d(ClassTag, "Create CacheFile " + sourceUrl);
      cacheFile.createNewFile();
      return new FileOutputStream(cacheFile);

    } catch (IOException e) {
      Log.e(ClassTag, "Error while accessing");
      throw e;
    }
  }

  public void removeCacheFile(String uniqueId) {
    String hash = buildHash(uniqueId);
    File cacheFile = new File(mCacheDir, hash);

    if (cacheFile.exists()) {
      cacheFile.delete();
    }

  }

  public void refreshCacheFile(String sourceUrl) {
    String hash = buildHash(sourceUrl);
    File cacheFile = new File(mCacheDir, hash);

    if (cacheFile.exists()) {
      GalDroidPreference.accessCacheObject(hash, cacheFile.length());
    }
  }

  private String buildHash(String sourceUrl) {
    synchronized (mDigester) {
      mDigester.reset();
      byte[] bytes = sourceUrl.getBytes();
      mDigester.update(bytes, 0, bytes.length);
      byte[] diggest = mDigester.digest();
      StringBuffer returnString = new StringBuffer(diggest.length);
      for (byte b : diggest) {
        returnString.append(Integer.toHexString(0xFF & b));
      }
      return returnString.toString();
    }
  }

  public void clearCachedBitmaps(boolean recycleBitmaps) {
    if (recycleBitmaps) {
      for (WeakReference<Bitmap> reference : mCachedBitmaps.values()) {
        Bitmap bitmap = reference.get();
        if (bitmap != null) {
          bitmap.recycle();
        }
      }
    }
    mCachedBitmaps.clear();
  }

  public File[] ListCachedFiles() {
    if (mCacheDir.exists()) {
      return mCacheDir.listFiles();
    } else {
      return new File[0];
    }
  }

  public long getMaxCacheSize() {
    return mMaxCacheSize;
  }

  public boolean isCleanUpNeeded() {
    long currentCacheSize = GalDroidPreference.getCacheSpaceNeeded();
    return currentCacheSize > mMaxCacheSize;
  }

  public File getCacheDir() {
    return mCacheDir;
  }
}




Java Source Code List

de.raptor2101.GalDroid.Activities.EditGalleryActivity.java
de.raptor2101.GalDroid.Activities.GalDroidApp.java
de.raptor2101.GalDroid.Activities.GalleryActivity.java
de.raptor2101.GalDroid.Activities.GalleryListingActivitiy.java
de.raptor2101.GalDroid.Activities.GridViewActivity.java
de.raptor2101.GalDroid.Activities.ImageViewActivity.java
de.raptor2101.GalDroid.Activities.Helpers.ActionBarHider.java
de.raptor2101.GalDroid.Activities.Helpers.ImageAdapter.java
de.raptor2101.GalDroid.Activities.Listeners.ImageViewOnTouchListener.java
de.raptor2101.GalDroid.Activities.Views.GalleryImageViewListener.java
de.raptor2101.GalDroid.Activities.Views.GalleryImageView.java
de.raptor2101.GalDroid.Activities.Views.ImageInformationView.java
de.raptor2101.GalDroid.Config.GalDroidPreference.java
de.raptor2101.GalDroid.Config.GalleryConfig.java
de.raptor2101.GalDroid.WebGallery.DegMinSec.java
de.raptor2101.GalDroid.WebGallery.GalleryFactory.java
de.raptor2101.GalDroid.WebGallery.ImageCache.java
de.raptor2101.GalDroid.WebGallery.ImageInformation.java
de.raptor2101.GalDroid.WebGallery.Stream.java
de.raptor2101.GalDroid.WebGallery.TitleConfig.java
de.raptor2101.GalDroid.WebGallery.Gallery3.DownloadObject.java
de.raptor2101.GalDroid.WebGallery.Gallery3.Gallery3Imp.java
de.raptor2101.GalDroid.WebGallery.Gallery3.ProgressListener.java
de.raptor2101.GalDroid.WebGallery.Gallery3.RestCall.java
de.raptor2101.GalDroid.WebGallery.Gallery3.JSON.AlbumEntity.java
de.raptor2101.GalDroid.WebGallery.Gallery3.JSON.CommentEntity.java
de.raptor2101.GalDroid.WebGallery.Gallery3.JSON.EntityFactory.java
de.raptor2101.GalDroid.WebGallery.Gallery3.JSON.Entity.java
de.raptor2101.GalDroid.WebGallery.Gallery3.JSON.PictureEntity.java
de.raptor2101.GalDroid.WebGallery.Gallery3.Tasks.JSONArrayLoaderTask.java
de.raptor2101.GalDroid.WebGallery.Interfaces.GalleryDownloadObject.java
de.raptor2101.GalDroid.WebGallery.Interfaces.GalleryObjectComment.java
de.raptor2101.GalDroid.WebGallery.Interfaces.GalleryObject.java
de.raptor2101.GalDroid.WebGallery.Interfaces.GalleryProgressListener.java
de.raptor2101.GalDroid.WebGallery.Interfaces.WebGallery.java
de.raptor2101.GalDroid.WebGallery.Tasks.CacheTaskListener.java
de.raptor2101.GalDroid.WebGallery.Tasks.CleanUpCacheTask.java
de.raptor2101.GalDroid.WebGallery.Tasks.GalleryLoaderTaskListener.java
de.raptor2101.GalDroid.WebGallery.Tasks.GalleryLoaderTask.java
de.raptor2101.GalDroid.WebGallery.Tasks.GalleryVerifyTask.java
de.raptor2101.GalDroid.WebGallery.Tasks.ImageInformationLoaderTaskListener.java
de.raptor2101.GalDroid.WebGallery.Tasks.ImageInformationLoaderTask.java
de.raptor2101.GalDroid.WebGallery.Tasks.ImageLoaderTaskListener.java
de.raptor2101.GalDroid.WebGallery.Tasks.ImageLoaderTask.java
de.raptor2101.GalDroid.WebGallery.Tasks.Progress.java
de.raptor2101.GalDroid.WebGallery.Tasks.RepeatingTask.java
de.raptor2101.GalDroid.WebGallery.Tasks.SyncronizeCacheTask.java
de.raptor2101.GalDroid.WebGallery.Tasks.TaskInterface.java