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.support.v7.preference.Preference;
21 import android.text.format.DateUtils;
22 
23 import com.android.storagemanager.utils.IconProvider;
24 
25 import java.io.File;
26 
27 /**
28  * DownloadsFilePreference is a preference representing a file in the Downloads folder with a
29  * checkbox that represents if the file should be deleted.
30  */
31 public class DownloadsFilePreference extends NestedDeletionPreference {
32     private File mFile;
33 
DownloadsFilePreference(Context context, File file, IconProvider iconProvider)34     public DownloadsFilePreference(Context context, File file, IconProvider iconProvider) {
35         super(context);
36         mFile = file;
37         setKey(mFile.getPath());
38         setTitle(file.getName());
39         setItemSize(file.length());
40         setSummary(
41                 DateUtils.formatDateTime(
42                         context, mFile.lastModified(), DateUtils.FORMAT_SHOW_DATE));
43         setIcon(iconProvider.loadMimeIcon(IconProvider.getMimeType(mFile)));
44 
45         // We turn off persistence because we need the file preferences to reset their check when
46         // you return to the view.
47         setPersistent(false);
48     }
49 
getFile()50     public File getFile() {
51         return mFile;
52     }
53 
54     @Override
compareTo(Preference other)55     public int compareTo(Preference other) {
56         if (other == null) {
57             return 1;
58         }
59 
60         if (other instanceof DownloadsFilePreference) {
61             File otherFile = ((DownloadsFilePreference) other).getFile();
62             File file = getFile();
63             // Note: The order is reversed in this comparison because we want the value to be less
64             // than 0 if we're bigger. Long.compare returns less than 0 if first < second.
65             int comparison = Long.compare(otherFile.length(), file.length());
66             if (comparison == 0) {
67                 comparison = file.compareTo(otherFile);
68             }
69             return comparison;
70         } else {
71             // If a non-DownloadsFilePreference appears, consider ourselves to be greater.
72             // This means if a non-DownloadsFilePreference sneaks into a DownloadsPreferenceGroup
73             // then the DownloadsFilePreference will appear higher.
74             return 1;
75         }
76     }
77 }
78