delete cached icons that have not been accessed in over a year

The icon files are downloaded for each version of the app.  Over time, old
versions will pile up.  This cleans out the ones that have not been used in
over a year.

On < android-21, this will delete icons that were downloaded over a year
ago even if they are still in use because it is only possible to check
mtime, not atime.
This commit is contained in:
Hans-Christoph Steiner 2016-08-16 00:13:30 +02:00
parent f6693ab1a1
commit 0614213de0
3 changed files with 18 additions and 4 deletions

View File

@ -61,6 +61,7 @@ public class CleanCacheService extends IntentService {
clearOldFiles(ApkCache.getApkCacheDir(getBaseContext()), Preferences.get().getKeepCacheTime());
deleteStrayIndexFiles();
deleteOldInstallerFiles();
deleteOldIcons();
}
/**
@ -118,6 +119,13 @@ public class CleanCacheService extends IntentService {
}
}
/**
* Delete cached icons that have not been accessed in over a year.
*/
private void deleteOldIcons() {
clearOldFiles(Utils.getIconsCacheDir(this), TimeUnit.DAYS.toMillis(365));
}
/**
* Recursively delete files in {@code f} that were last used
* {@code millisAgo} milliseconds ago. On {@code android-21} and newer, this

View File

@ -45,7 +45,6 @@ import com.nostra13.universalimageloader.cache.disc.impl.LimitedAgeDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.utils.StorageUtils;
import org.acra.ACRA;
import org.acra.ReportingInteractionMode;
@ -60,7 +59,6 @@ import org.fdroid.fdroid.data.Repo;
import org.fdroid.fdroid.net.IconDownloader;
import org.fdroid.fdroid.net.WifiStateChangeService;
import java.io.File;
import java.net.URL;
import java.net.URLStreamHandler;
import java.net.URLStreamHandlerFactory;
@ -262,8 +260,7 @@ public class FDroidApp extends Application {
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.imageDownloader(new IconDownloader(getApplicationContext()))
.diskCache(new LimitedAgeDiskCache(
new File(StorageUtils.getCacheDirectory(getApplicationContext(), true),
"icons"),
Utils.getIconsCacheDir(this),
null,
new FileNameGenerator() {
@Override

View File

@ -33,6 +33,7 @@ import android.util.Log;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.utils.StorageUtils;
import org.fdroid.fdroid.compat.FileCompat;
import org.fdroid.fdroid.data.Repo;
@ -107,6 +108,14 @@ public final class Utils {
return "/icons-120/";
}
/**
* @return the directory where cached icons are stored
*/
public static File getIconsCacheDir(Context context) {
File cacheDir = StorageUtils.getCacheDirectory(context.getApplicationContext(), true);
return new File(cacheDir, "icons");
}
public static void copy(InputStream input, OutputStream output) throws IOException {
byte[] buffer = new byte[BUFFER_SIZE];
while (true) {