Android Open Source - robotronic Cache Cleaner






From Project

Back to project page robotronic.

License

The source code is released under:

Copyright (C) 2011 by Drew Schrauf Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the So...

If you think the Android project robotronic 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.drewschrauf.robotronic.threads;
//  w  w w . j  a  v  a  2  s.c  om
import java.io.File;
import java.util.Arrays;
import java.util.Comparator;

import android.content.Context;
import android.os.Environment;

public class CacheCleaner {

  /**
   * The number of files to keep in the cache directory
   */
  public static final int KEEP_FILES_COUNT = 50;

  /**
   * Not yet implemented
   * Cleans the DB to remove old entries
   */
  public static void cleanDB() {
  }

  /**
   * Delete the oldest files from the cache directory
   * 
   * @param context
   *            The activity that created the ThreadHandler that spawned this
   *            CacheCleaner
   */
  public static void cleanFilesystem(Context context) {
    // make the folder for the cache
    String cacheDirString = Environment.getExternalStorageDirectory()
        .getAbsolutePath();
    cacheDirString += "/android/data/" + context.getPackageName()
        + "/cache/";
    File cacheDir = new File(cacheDirString);

    if (cacheDir.exists()) {
      File[] files = cacheDir.listFiles();

      // sort the files from oldest to newest
      Arrays.sort(files, new Comparator<File>() {
        public int compare(File o1, File o2) {

          if (((File) o1).lastModified() > ((File) o2).lastModified()) {
            return +1;
          } else if (((File) o1).lastModified() < ((File) o2)
              .lastModified()) {
            return -1;
          } else {
            return 0;
          }
        }
      });

      int filesToDelete = files.length > KEEP_FILES_COUNT ? files.length
          - KEEP_FILES_COUNT : 0;
      for (int i = 0; i < filesToDelete; i++) {
        File file = files[i];
        file.delete();
      }
    }

  }
}




Java Source Code List

com.drewschrauf.example.robotronic.ExampleHome.java
com.drewschrauf.example.robotronic.ExampleListItem.java
com.drewschrauf.example.robotronic.ExampleList.java
com.drewschrauf.example.robotronic.ExampleSimple.java
com.drewschrauf.robotronic.activities.RobotronicActivity.java
com.drewschrauf.robotronic.activities.RobotronicListActivity.java
com.drewschrauf.robotronic.database.DatabaseHandler.java
com.drewschrauf.robotronic.database.DatabaseHelper.java
com.drewschrauf.robotronic.threads.BinaryFetchThread.java
com.drewschrauf.robotronic.threads.CacheCleaner.java
com.drewschrauf.robotronic.threads.DataFetchThread.java
com.drewschrauf.robotronic.threads.ParsingException.java
com.drewschrauf.robotronic.threads.RobotronicProperties.java
com.drewschrauf.robotronic.threads.RobotronicThread.java
com.drewschrauf.robotronic.threads.RobotronicUtilities.java
com.drewschrauf.robotronic.threads.ThreadHandler.java