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.graphics.Bitmap;
21 import android.media.ThumbnailUtils;
22 import android.os.SystemProperties;
23 import android.provider.MediaStore;
24 import androidx.annotation.VisibleForTesting;
25 import android.text.format.DateUtils;
26 
27 import com.android.storagemanager.utils.AsyncLoader;
28 import com.android.storagemanager.utils.IconProvider;
29 
30 import java.io.File;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 
34 /**
35  * FetchDownloadsLoader is an asynchronous task which returns files in the Downloads
36  * directory which have not been modified in longer than 90 days.
37  */
38 public class FetchDownloadsLoader extends AsyncLoader<FetchDownloadsLoader.DownloadsResult> {
39     private static final String DEBUG_FILE_AGE_OVERRIDE = "debug.asm.file_age_limit";
40     private static final int MINIMUM_AGE_DAYS = 0;
41     private File mDirectory;
42 
43     /**
44      * Sets up a FetchDownloadsLoader in any directory.
45      *
46      * @param directory The directory to look into.
47      */
FetchDownloadsLoader(Context context, File directory)48     public FetchDownloadsLoader(Context context, File directory) {
49         super(context);
50         mDirectory = directory;
51     }
52 
53     @Override
onDiscardResult(DownloadsResult result)54     protected void onDiscardResult(DownloadsResult result) {
55     }
56 
57     @Override
loadInBackground()58     public DownloadsResult loadInBackground() {
59         return collectFiles(mDirectory);
60     }
61 
62     @VisibleForTesting
collectFiles(File dir)63     static DownloadsResult collectFiles(File dir) {
64         return collectFiles(dir, new DownloadsResult());
65     }
66 
collectFiles(File dir, DownloadsResult result)67     private static DownloadsResult collectFiles(File dir, DownloadsResult result) {
68         int minimumAgeDays = SystemProperties.getInt(DEBUG_FILE_AGE_OVERRIDE, MINIMUM_AGE_DAYS);
69         final long last_modified_threshold = System.currentTimeMillis() -
70                 minimumAgeDays * DateUtils.DAY_IN_MILLIS;
71         File downloadFiles[] = dir.listFiles();
72         if (downloadFiles != null && downloadFiles.length > 0) {
73             for (File currentFile : downloadFiles) {
74                 if (currentFile.isDirectory()) {
75                     collectFiles(currentFile, result);
76                 } else {
77                     // Skip files that have been modified too recently.
78                     if (last_modified_threshold < currentFile.lastModified()) {
79                         continue;
80                     }
81 
82                     if (currentFile.lastModified() < result.youngestLastModified) {
83                         result.youngestLastModified = currentFile.lastModified();
84                     }
85                     result.files.add(currentFile);
86                     result.totalSize += currentFile.length();
87 
88                     if (IconProvider.isImageType(currentFile)) {
89                         Bitmap thumbnail =
90                                 ThumbnailUtils.createImageThumbnail(
91                                         currentFile.getAbsolutePath(),
92                                         MediaStore.Images.Thumbnails.MINI_KIND);
93                         result.thumbnails.put(currentFile, thumbnail);
94                     }
95                 }
96             }
97         }
98 
99         return result;
100     }
101 
102     /**
103      * The DownloadsResult is the result of a {@link FetchDownloadsLoader} with the files
104      * and the amount of space they use.
105      */
106     public static class DownloadsResult {
107         public long totalSize;
108         public long youngestLastModified;
109         public ArrayList<File> files;
110         public HashMap<File, Bitmap> thumbnails;
111 
DownloadsResult()112         public DownloadsResult() {
113             this(0, Long.MAX_VALUE, new ArrayList<File>(), new HashMap<>());
114         }
115 
DownloadsResult( long totalSize, long youngestLastModified, ArrayList<File> files, HashMap<File, Bitmap> thumbnails)116         public DownloadsResult(
117                 long totalSize,
118                 long youngestLastModified,
119                 ArrayList<File> files,
120                 HashMap<File, Bitmap> thumbnails) {
121             this.totalSize = totalSize;
122             this.youngestLastModified = youngestLastModified;
123             this.files = files;
124             this.thumbnails = thumbnails;
125         }
126     }
127 }