1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.storagemanager.deletionhelper; 18 19 import android.content.Context; 20 import android.os.SystemProperties; 21 import android.support.annotation.VisibleForTesting; 22 import android.text.format.DateUtils; 23 import com.android.storagemanager.utils.AsyncLoader; 24 25 import java.io.File; 26 import java.util.ArrayList; 27 28 /** 29 * FetchDownloadsLoader is an asynchronous task which returns files in the Downloads 30 * directory which have not been modified in longer than 90 days. 31 */ 32 public class FetchDownloadsLoader extends AsyncLoader<FetchDownloadsLoader.DownloadsResult> { 33 private static final String DEBUG_FILE_AGE_OVERRIDE = "debug.asm.file_age_limit"; 34 private static final int MINIMUM_AGE_DAYS = 30; 35 private File mDirectory; 36 37 /** 38 * Sets up a FetchDownloadsLoader in any directory. 39 * 40 * @param directory The directory to look into. 41 */ FetchDownloadsLoader(Context context, File directory)42 public FetchDownloadsLoader(Context context, File directory) { 43 super(context); 44 mDirectory = directory; 45 } 46 47 @Override onDiscardResult(DownloadsResult result)48 protected void onDiscardResult(DownloadsResult result) { 49 } 50 51 @Override loadInBackground()52 public DownloadsResult loadInBackground() { 53 return collectFiles(mDirectory); 54 } 55 56 @VisibleForTesting collectFiles(File dir)57 static DownloadsResult collectFiles(File dir) { 58 return collectFiles(dir, new DownloadsResult()); 59 } 60 collectFiles(File dir, DownloadsResult result)61 private static DownloadsResult collectFiles(File dir, DownloadsResult result) { 62 int minimumAgeDays = SystemProperties.getInt(DEBUG_FILE_AGE_OVERRIDE, MINIMUM_AGE_DAYS); 63 final long last_modified_threshold = System.currentTimeMillis() - 64 minimumAgeDays * DateUtils.DAY_IN_MILLIS; 65 File downloadFiles[] = dir.listFiles(); 66 if (downloadFiles != null && downloadFiles.length > 0) { 67 for (File currentFile : downloadFiles) { 68 if (currentFile.isDirectory()) { 69 collectFiles(currentFile, result); 70 } else { 71 // Skip files that have been modified too recently. 72 if (last_modified_threshold < currentFile.lastModified()) { 73 continue; 74 } 75 76 if (currentFile.lastModified() < result.youngestLastModified) { 77 result.youngestLastModified = currentFile.lastModified(); 78 } 79 result.files.add(currentFile); 80 result.totalSize += currentFile.length(); 81 } 82 } 83 } 84 85 return result; 86 } 87 88 /** 89 * The DownloadsResult is the result of a {@link FetchDownloadsLoader} with the files 90 * and the amount of space they use. 91 */ 92 public static class DownloadsResult { 93 public long totalSize; 94 public long youngestLastModified; 95 public ArrayList<File> files; 96 DownloadsResult()97 public DownloadsResult() { 98 this(0, Long.MAX_VALUE, new ArrayList<File>()); 99 } 100 DownloadsResult(long totalSize, long youngestLastModified, ArrayList<File> files)101 public DownloadsResult(long totalSize, long youngestLastModified, ArrayList<File> files) { 102 this.totalSize = totalSize; 103 this.youngestLastModified = youngestLastModified; 104 this.files = files; 105 } 106 } 107 }