switch "Keep Cache Time" units to milliseconds

Both the Android task scheduler and the Java File operations want millis.
For "Forever", Long.MAX_VALUE is used.
This commit is contained in:
Hans-Christoph Steiner 2016-08-15 22:47:45 +02:00
parent fc98820c93
commit c9e3643712
5 changed files with 38 additions and 23 deletions

View File

@ -17,6 +17,7 @@ import org.apache.commons.io.FileUtils;
import org.fdroid.fdroid.installer.ApkCache;
import java.io.File;
import java.util.concurrent.TimeUnit;
/**
* Handles cleaning up caches files that are not going to be used, and do not
@ -33,9 +34,9 @@ public class CleanCacheService extends IntentService {
*/
public static void schedule(Context context) {
long keepTime = Preferences.get().getKeepCacheTime();
long interval = 604800000; // 1 day
long interval = TimeUnit.DAYS.toMillis(1);
if (keepTime < interval) {
interval = keepTime * 1000;
interval = keepTime;
}
Intent intent = new Intent(context, CleanCacheService.class);
@ -119,15 +120,15 @@ public class CleanCacheService extends IntentService {
/**
* Recursively delete files in {@code dir} that were last used
* {@code secondsAgo} seconds ago. On {@code android-21} and newer, this
* {@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 secondsAgo The number of seconds old that marks a file for deletion.
* @param millisAgo The number of milliseconds old that marks a file for deletion.
*/
@TargetApi(21)
public static void clearOldFiles(File dir, long secondsAgo) {
public static void clearOldFiles(File dir, long millisAgo) {
if (dir == null) {
return;
}
@ -135,7 +136,7 @@ public class CleanCacheService extends IntentService {
if (files == null) {
return;
}
long olderThan = System.currentTimeMillis() - (secondsAgo * 1000L);
long olderThan = System.currentTimeMillis() - millisAgo;
for (File f : files) {
if (f.isDirectory()) {
clearOldFiles(f, olderThan);
@ -148,7 +149,7 @@ public class CleanCacheService extends IntentService {
} else {
try {
StructStat stat = Os.lstat(f.getAbsolutePath());
if (stat.st_atime < olderThan) {
if ((stat.st_atime * 1000L) < olderThan) {
f.delete();
}
} catch (ErrnoException e) {

View File

@ -16,6 +16,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import info.guardianproject.netcipher.NetCipher;
@ -30,11 +31,9 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh
private static final String TAG = "Preferences";
private final Context context;
private final SharedPreferences preferences;
private Preferences(Context context) {
this.context = context;
preferences = PreferenceManager.getDefaultSharedPreferences(context);
preferences.registerOnSharedPreferenceChangeListener(this);
if (preferences.getString(PREF_LOCAL_REPO_NAME, null) == null) {
@ -72,7 +71,7 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh
private static final int DEFAULT_UPD_HISTORY = 14;
private static final boolean DEFAULT_PRIVILEGED_INSTALLER = false;
//private static final boolean DEFAULT_LOCAL_REPO_BONJOUR = true;
private static final long DEFAULT_KEEP_CACHE_SECONDS = 86400; // one day
private static final long DEFAULT_KEEP_CACHE_TIME = TimeUnit.DAYS.toMillis(1); // one day
private static final boolean DEFAULT_UNSTABLE_UPDATES = false;
//private static final boolean DEFAULT_LOCAL_REPO_HTTPS = false;
private static final boolean DEFAULT_INCOMP_VER = false;
@ -136,14 +135,29 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh
private static final String PREF_CACHE_APK = "cacheDownloaded";
/**
* Time in seconds to keep cached files. Anything that has been around longer will be deleted
* Time in millis to keep cached files. Anything that has been around longer will be deleted
*/
public long getKeepCacheTime() {
String value = preferences.getString(PREF_KEEP_CACHE_TIME, String.valueOf(DEFAULT_KEEP_CACHE_SECONDS));
String value = preferences.getString(PREF_KEEP_CACHE_TIME,
String.valueOf(DEFAULT_KEEP_CACHE_TIME));
// the first time this was migrated, it was botched, so reset to default
switch (value) {
case "3600":
case "86400":
case "604800":
case "2592000":
case "31449600":
case "2147483647":
SharedPreferences.Editor editor = preferences.edit();
editor.remove(PREF_KEEP_CACHE_TIME);
editor.apply();
return Preferences.DEFAULT_KEEP_CACHE_TIME;
}
if (preferences.contains(PREF_CACHE_APK)) {
if (preferences.getBoolean(PREF_CACHE_APK, false)) {
value = context.getString(R.string.keep_forever);
value = String.valueOf(Long.MAX_VALUE);
}
SharedPreferences.Editor editor = preferences.edit();
editor.remove(PREF_CACHE_APK);
@ -154,7 +168,7 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh
try {
return Long.parseLong(value);
} catch (NumberFormatException e) {
return DEFAULT_KEEP_CACHE_SECONDS;
return DEFAULT_KEEP_CACHE_TIME;
}
}

View File

@ -26,12 +26,12 @@
</string-array>
<string-array name="keepCacheValues">
<item>3600</item>
<item>86400</item>
<item>604800</item>
<item>2592000</item>
<item>31449600</item>
<item>2147483647</item>
<item>3600000</item>
<item>86400000</item>
<item>604800000</item>
<item>2592000000</item>
<item>31536000000</item>
<item>9223372036854775807</item>
</string-array>
<string-array name="themeValues">

View File

@ -75,7 +75,7 @@
<PreferenceCategory android:title="@string/other">
<ListPreference android:title="@string/cache_downloaded"
android:key="keepCacheFor"
android:defaultValue="86400"
android:defaultValue="86400000"
android:entries="@array/keepCacheNames"
android:entryValues="@array/keepCacheValues" />
<CheckBoxPreference android:title="@string/expert"

View File

@ -46,12 +46,12 @@ public class CleanCacheServiceTest {
assertTrue(second.createNewFile());
assertTrue(second.exists());
CleanCacheService.clearOldFiles(dir, 3);
CleanCacheService.clearOldFiles(dir, 3000);
assertFalse(first.exists());
assertTrue(second.exists());
Thread.sleep(7000);
CleanCacheService.clearOldFiles(dir, 3);
CleanCacheService.clearOldFiles(dir, 3000);
assertFalse(first.exists());
assertFalse(second.exists());
}