Android Open Source - GalDroid Clean Up Cache Task






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

package de.raptor2101.GalDroid.WebGallery.Tasks;
// w  w w. ja v  a 2  s.  co  m
import java.io.File;
import java.lang.ref.WeakReference;
import java.util.List;

import de.raptor2101.GalDroid.Config.GalDroidPreference;
import de.raptor2101.GalDroid.WebGallery.ImageCache;
import android.os.AsyncTask;

public class CleanUpCacheTask extends AsyncTask<Void, Long, Void> {
  private WeakReference<CacheTaskListener> mListener;
  private List<String> mCachedObjects;
  private File mCacheDir;
  private long mCleanUpSize;

  public CleanUpCacheTask(ImageCache cache, CacheTaskListener listener) {
    mListener = new WeakReference<CacheTaskListener>(listener);
    mCachedObjects = GalDroidPreference.getCacheOjectsOrderedByAccessTime();

    mCacheDir = cache.getCacheDir();
    long maxCacheSize = cache.getMaxCacheSize();
    long cleanUpToSize = maxCacheSize - (maxCacheSize / 3);
    long currentCacheSize = GalDroidPreference.getCacheSpaceNeeded();
    mCleanUpSize = currentCacheSize - cleanUpToSize;
  }

  @Override
  protected void onPreExecute() {
    CacheTaskListener listener = mListener.get();
    if (listener != null) {
      listener.onCacheOperationStart(100);
    }
  }

  @Override
  protected Void doInBackground(Void... params) {
    long currentCleanUp = 0;
    if (mCleanUpSize > 0) {
      for (int i = 0; currentCleanUp < mCleanUpSize && i < mCachedObjects.size(); i++) {
        String hash = mCachedObjects.get(i);
        File cacheFile = new File(mCacheDir, hash);
        if (cacheFile.exists()) {
          currentCleanUp += cacheFile.length();
          cacheFile.delete();
        }
        GalDroidPreference.deleteCacheObject(hash);

        publishProgress(currentCleanUp);
      }
    }
    return null;
  }

  @Override
  protected void onProgressUpdate(Long... values) {
    CacheTaskListener listener = mListener.get();
    if (listener != null) {
      long calculatedValue = values[0] * 100 / mCleanUpSize;
      listener.onCacheOperationProgress((int) calculatedValue);
    }
  }

  @Override
  protected void onPostExecute(Void result) {
    CacheTaskListener listener = mListener.get();
    if (listener != null) {
      listener.onCacheOperationDone();
    }
  }

}




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