Fix NPE crash when clearing files in a directory

This was very probably introduced with the new CleanCacheService stuff.
Reported via ACRA:

	ANDROID_VERSION=6.0.1
	APP_VERSION_NAME=0.100-alpha6
	STACK_TRACE=java.lang.NullPointerException: Attempt to get length of null array
		at org.fdroid.fdroid.Utils.clearOldFiles(Utils.java:349)
		at org.fdroid.fdroid.Utils.clearOldFiles(Utils.java:351)
		at org.fdroid.fdroid.Utils.clearOldFiles(Utils.java:351)
		at org.fdroid.fdroid.CleanCacheService.onHandleIntent(CleanCacheService.java:54)
		at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:66)
		at android.os.Handler.dispatchMessage(Handler.java:102)
		at android.os.Looper.loop(Looper.java:148)
		at android.os.HandlerThread.run(HandlerThread.java:61)

There is nothing to clear if the directory could not be obtained for
some reason, so this seems like a reasonable fix. Anything is better
than a crash anyway.
This commit is contained in:
Daniel Martí 2016-05-07 22:30:31 +01:00
parent ee696b4737
commit 1a94c46e87

View File

@ -344,6 +344,9 @@ public final class Utils {
* @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.
*/ */
public static void clearOldFiles(File dir, long secondsAgo) { public static void clearOldFiles(File dir, long secondsAgo) {
if (dir == null) {
return;
}
long olderThan = System.currentTimeMillis() - (secondsAgo * 1000L); long olderThan = System.currentTimeMillis() - (secondsAgo * 1000L);
for (File f : dir.listFiles()) { for (File f : dir.listFiles()) {
if (f.isDirectory()) { if (f.isDirectory()) {