use access time to remove old cache files >= android-21

In android-21, they exposed the formerly internal method for getting stat
structs of files.  From that, we can get the last access time, which is a
much better way to determine which files to delete rather than last
modified time.

closes #644
This commit is contained in:
Hans-Christoph Steiner 2016-08-15 17:14:17 +02:00
parent 6204a16024
commit 28dfe970da

View File

@ -141,12 +141,15 @@ public class ApkCache {
} }
/** /**
* Recursively delete files in {@code dir} that were last modified * Recursively delete files in {@code dir} that were last used
* {@code secondsAgo} seconds ago, e.g. when it was downloaded. * {@code secondsAgo} seconds ago. On {@code android-21} and newer, this
* is based on the last access of the file, on older Android versions, it is
* based on the last time the file was modified, e.g. downloaded.
* *
* @param dir The directory to recurse in * @param dir The directory to recurse in
* @param secondsAgo The number of seconds old that marks a file for deletion. * @param secondsAgo The number of seconds old that marks a file for deletion.
*/ */
@TargetApi(21)
public static void clearOldFiles(File dir, long secondsAgo) { public static void clearOldFiles(File dir, long secondsAgo) {
if (dir == null) { if (dir == null) {
return; return;
@ -161,8 +164,19 @@ public class ApkCache {
clearOldFiles(f, olderThan); clearOldFiles(f, olderThan);
f.delete(); f.delete();
} }
if (FileUtils.isFileOlder(f, olderThan)) { if (Build.VERSION.SDK_INT < 21) {
f.delete(); if (FileUtils.isFileOlder(f, olderThan)) {
f.delete();
}
} else {
try {
StructStat stat = Os.lstat(f.getAbsolutePath());
if (stat.st_atime < olderThan) {
f.delete();
}
} catch (ErrnoException e) {
e.printStackTrace();
}
} }
} }
} }