From cbd6da5267b5b107c4ddb211837d3906b9b9847e Mon Sep 17 00:00:00 2001 From: Peter Serwylo Date: Wed, 17 Aug 2016 22:46:23 +1000 Subject: [PATCH] Move code causing verify error into separate helper class I'm not 100% sure on how the `@TargetApi` and `VerifyError` work together. However it is something along the lines of: * Class loader needs `CleanCacheService`. * At this point, it loads the bytecode for that class and verifies that it all makes sense. * The bytecode within the method targeted at API 21 is not understood by earlier APIs, because the entire `Os` class was introduced in 21. * By putting it into a different class, that class is only loaded at runtime on devices with API of 21 or higher. Previously, `@TargetApi` + the relevant guard condition to check the build version at runtime suffices to prevent this. However it seems that if the entire class does not even exist on earlier APIs, then it is no longer good enough. --- .../org/fdroid/fdroid/CleanCacheService.java | 16 ++---------- .../fdroid/fdroid/CleanCacheService21.java | 26 +++++++++++++++++++ 2 files changed, 28 insertions(+), 14 deletions(-) create mode 100644 app/src/main/java/org/fdroid/fdroid/CleanCacheService21.java diff --git a/app/src/main/java/org/fdroid/fdroid/CleanCacheService.java b/app/src/main/java/org/fdroid/fdroid/CleanCacheService.java index 9fca46e7d..a1befc144 100644 --- a/app/src/main/java/org/fdroid/fdroid/CleanCacheService.java +++ b/app/src/main/java/org/fdroid/fdroid/CleanCacheService.java @@ -1,6 +1,5 @@ package org.fdroid.fdroid; -import android.annotation.TargetApi; import android.app.AlarmManager; import android.app.IntentService; import android.app.PendingIntent; @@ -9,9 +8,6 @@ import android.content.Intent; import android.os.Build; import android.os.Process; import android.os.SystemClock; -import android.system.ErrnoException; -import android.system.Os; -import android.system.StructStat; import org.apache.commons.io.FileUtils; import org.fdroid.fdroid.installer.ApkCache; @@ -28,7 +24,7 @@ import java.util.concurrent.TimeUnit; * These files should only be deleted when they are at least an hour-ish old, * in case they are actively in use while {@code CleanCacheService} is running. * {@link #clearOldFiles(File, long)} checks the file age using access time from - * {@link StructStat#st_atime} on {@link android.os.Build.VERSION_CODES#LOLLIPOP} + * {@link android.system.StructStat#st_atime} on {@link android.os.Build.VERSION_CODES#LOLLIPOP} * and newer. On older Android, last modified time from {@link File#lastModified()} * is used. */ @@ -152,7 +148,6 @@ public class CleanCacheService extends IntentService { * @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 f, long millisAgo) { if (f == null) { return; @@ -172,14 +167,7 @@ public class CleanCacheService extends IntentService { f.delete(); } } else { - try { - StructStat stat = Os.lstat(f.getAbsolutePath()); - if ((stat.st_atime * 1000L) < olderThan) { - f.delete(); - } - } catch (ErrnoException e) { - e.printStackTrace(); - } + CleanCacheService21.deleteIfOld(f, millisAgo); } } } \ No newline at end of file diff --git a/app/src/main/java/org/fdroid/fdroid/CleanCacheService21.java b/app/src/main/java/org/fdroid/fdroid/CleanCacheService21.java new file mode 100644 index 000000000..308d70c2a --- /dev/null +++ b/app/src/main/java/org/fdroid/fdroid/CleanCacheService21.java @@ -0,0 +1,26 @@ +package org.fdroid.fdroid; + +import android.annotation.TargetApi; +import android.system.ErrnoException; +import android.system.Os; +import android.system.StructStat; + +import java.io.File; + +/** + * Helper class to prevent {@link VerifyError}s from occurring in {@link CleanCacheService#clearOldFiles(File, long)} + * due to the fact that {@link Os} was only introduced in API 21. + */ +@TargetApi(21) +class CleanCacheService21 { + static void deleteIfOld(File file, long olderThan) { + try { + StructStat stat = Os.lstat(file.getAbsolutePath()); + if ((stat.st_atime * 1000L) < olderThan) { + file.delete(); + } + } catch (ErrnoException e) { + e.printStackTrace(); + } + } +}