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 import android.text.format.Formatter; 23 import android.webkit.MimeTypeMap; 24 import com.android.storagemanager.R; 25 import com.android.storagemanager.utils.IconUtils; 26 27 import java.io.File; 28 29 /** 30 * DownloadsFilePreference is a preference representing a file in the Downloads folder 31 * with a checkbox that represents if the file should be deleted. 32 */ 33 public class DownloadsFilePreference extends NestedCheckboxPreference { 34 private File mFile; 35 DownloadsFilePreference(Context context, File file)36 public DownloadsFilePreference(Context context, File file) { 37 super(context); 38 mFile = file; 39 setKey(mFile.getPath()); 40 setTitle(file.getName()); 41 setSummary(context.getString(R.string.deletion_helper_downloads_file_summary, 42 Formatter.formatFileSize(getContext(), file.length()), 43 DateUtils.formatDateTime(context, 44 mFile.lastModified(),DateUtils.FORMAT_SHOW_DATE))); 45 setIcon(context.getContentResolver().getTypeDrawable(getMimeType())); 46 47 // We turn off persistence because we need the file preferences to reset their check when 48 // you return to the view. 49 setPersistent(false); 50 } 51 getFile()52 public File getFile() { 53 return mFile; 54 } 55 56 @Override compareTo(Preference other)57 public int compareTo(Preference other) { 58 if (other == null) { 59 return 1; 60 } 61 62 if (other instanceof DownloadsFilePreference) { 63 DownloadsFilePreference preference = (DownloadsFilePreference) other; 64 return Long.compare(preference.getFile().length(), getFile().length()); 65 } else { 66 // If a non-DownloadsFilePreference appears, consider ourselves to be greater. 67 // This means if a non-DownloadsFilePreference sneaks into a DownloadsPreferenceGroup 68 // then the DownloadsFilePreference will appear higher. 69 return 1; 70 } 71 } 72 getMimeType()73 private String getMimeType() { 74 String name = getFile().getName(); 75 final int lastDot = name.lastIndexOf('.'); 76 if (lastDot >= 0) { 77 final String extension = name.substring(lastDot + 1); 78 final String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension( 79 extension.toLowerCase()); 80 if (mimeType != null) { 81 return mimeType; 82 } 83 } 84 return "application/octet-stream"; 85 } 86 } 87