From c9e364371232461317710f0de3375340e5b7717f Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Mon, 15 Aug 2016 22:47:45 +0200 Subject: [PATCH] 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. --- .../org/fdroid/fdroid/CleanCacheService.java | 15 +++++----- .../java/org/fdroid/fdroid/Preferences.java | 28 ++++++++++++++----- app/src/main/res/values/donottranslate.xml | 12 ++++---- app/src/main/res/xml/preferences.xml | 2 +- .../fdroid/fdroid/CleanCacheServiceTest.java | 4 +-- 5 files changed, 38 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/org/fdroid/fdroid/CleanCacheService.java b/app/src/main/java/org/fdroid/fdroid/CleanCacheService.java index b912992a9..a1f5d6c9b 100644 --- a/app/src/main/java/org/fdroid/fdroid/CleanCacheService.java +++ b/app/src/main/java/org/fdroid/fdroid/CleanCacheService.java @@ -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) { diff --git a/app/src/main/java/org/fdroid/fdroid/Preferences.java b/app/src/main/java/org/fdroid/fdroid/Preferences.java index 0a7a9faad..3216f4c87 100644 --- a/app/src/main/java/org/fdroid/fdroid/Preferences.java +++ b/app/src/main/java/org/fdroid/fdroid/Preferences.java @@ -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; } } diff --git a/app/src/main/res/values/donottranslate.xml b/app/src/main/res/values/donottranslate.xml index 148db8f22..ed9644aef 100644 --- a/app/src/main/res/values/donottranslate.xml +++ b/app/src/main/res/values/donottranslate.xml @@ -26,12 +26,12 @@ - 3600 - 86400 - 604800 - 2592000 - 31449600 - 2147483647 + 3600000 + 86400000 + 604800000 + 2592000000 + 31536000000 + 9223372036854775807 diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 454472b61..6f5b404f3 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -75,7 +75,7 @@