avoid deleting cached files while they are being used
If CleanCacheService runs while an APK is being installed, it should not delete the APK that is in the process of being installed. This does that by only deleting those files if they are older than an hour. Same goes for the index files. #738
This commit is contained in:
parent
09829515e8
commit
f6693ab1a1
@ -80,7 +80,7 @@ public class CleanCacheService extends IntentService {
|
||||
|
||||
for (File f : files) {
|
||||
if (f.getName().startsWith("install-")) {
|
||||
FileUtils.deleteQuietly(f);
|
||||
clearOldFiles(f, TimeUnit.HOURS.toMillis(1));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -110,39 +110,39 @@ public class CleanCacheService extends IntentService {
|
||||
|
||||
for (File f : files) {
|
||||
if (f.getName().startsWith("index-")) {
|
||||
FileUtils.deleteQuietly(f);
|
||||
clearOldFiles(f, TimeUnit.HOURS.toMillis(1));
|
||||
}
|
||||
if (f.getName().startsWith("dl-")) {
|
||||
FileUtils.deleteQuietly(f);
|
||||
clearOldFiles(f, TimeUnit.HOURS.toMillis(1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively delete files in {@code dir} that were last used
|
||||
* Recursively delete files in {@code f} that were last used
|
||||
* {@code millisAgo} milliseconds 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 f The file or directory to clean
|
||||
* @param millisAgo The number of milliseconds old that marks a file for deletion.
|
||||
*/
|
||||
@TargetApi(21)
|
||||
public static void clearOldFiles(File dir, long millisAgo) {
|
||||
if (dir == null) {
|
||||
return;
|
||||
}
|
||||
File[] files = dir.listFiles();
|
||||
if (files == null) {
|
||||
public static void clearOldFiles(File f, long millisAgo) {
|
||||
if (f == null) {
|
||||
return;
|
||||
}
|
||||
long olderThan = System.currentTimeMillis() - millisAgo;
|
||||
for (File f : files) {
|
||||
if (f.isDirectory()) {
|
||||
clearOldFiles(f, millisAgo);
|
||||
f.delete();
|
||||
File[] files = f.listFiles();
|
||||
if (files == null) {
|
||||
return;
|
||||
}
|
||||
if (Build.VERSION.SDK_INT < 21) {
|
||||
for (File file : files) {
|
||||
clearOldFiles(file, millisAgo);
|
||||
}
|
||||
f.delete();
|
||||
} else if (Build.VERSION.SDK_INT < 21) {
|
||||
if (FileUtils.isFileOlder(f, olderThan)) {
|
||||
f.delete();
|
||||
}
|
||||
@ -158,4 +158,3 @@ public class CleanCacheService extends IntentService {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user