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.app.Activity; 20 import android.app.LoaderManager.LoaderCallbacks; 21 import android.content.Context; 22 import android.content.Loader; 23 import android.os.AsyncTask; 24 import android.os.Bundle; 25 import android.os.Environment; 26 import android.util.ArraySet; 27 import com.android.internal.logging.MetricsLogger; 28 import com.android.internal.logging.MetricsProto.MetricsEvent; 29 import com.android.storagemanager.deletionhelper.FetchDownloadsLoader.DownloadsResult; 30 31 import java.io.File; 32 import java.util.Collections; 33 import java.util.Set; 34 35 /** 36 * The DownloadsDeletionType provides stale download file information to the 37 * {@link DownloadsDeletionPreferenceGroup}. 38 */ 39 public class DownloadsDeletionType implements DeletionType, LoaderCallbacks<DownloadsResult> { 40 public static final String EXTRA_UNCHECKED_DOWNLOADS = "uncheckedFiles"; 41 private long mBytes; 42 private long mMostRecent; 43 private FreeableChangedListener mListener; 44 private Context mContext; 45 private ArraySet<File> mFiles; 46 private ArraySet<String> mUncheckedFiles; 47 DownloadsDeletionType(Context context, String[] uncheckedFiles)48 public DownloadsDeletionType(Context context, String[] uncheckedFiles) { 49 mContext = context; 50 mFiles = new ArraySet<>(); 51 mUncheckedFiles = new ArraySet<>(); 52 if (uncheckedFiles != null) { 53 Collections.addAll(mUncheckedFiles, uncheckedFiles); 54 } 55 } 56 57 @Override registerFreeableChangedListener(FreeableChangedListener listener)58 public void registerFreeableChangedListener(FreeableChangedListener listener) { 59 mListener = listener; 60 if (mFiles != null) { 61 maybeUpdateListener(); 62 } 63 } 64 65 @Override onResume()66 public void onResume() { 67 } 68 69 @Override onPause()70 public void onPause() { 71 } 72 73 @Override onSaveInstanceStateBundle(Bundle savedInstanceState)74 public void onSaveInstanceStateBundle(Bundle savedInstanceState) { 75 savedInstanceState.putStringArray(EXTRA_UNCHECKED_DOWNLOADS, 76 mUncheckedFiles.toArray(new String[mUncheckedFiles.size()])); 77 } 78 79 @Override clearFreeableData(Activity activity)80 public void clearFreeableData(Activity activity) { 81 if (mFiles != null) { 82 AsyncTask.execute(new Runnable() { 83 @Override 84 public void run() { 85 boolean succeeded = true; 86 for (File entry : mFiles) { 87 if (isChecked(entry)) { 88 succeeded = succeeded && entry.delete(); 89 } 90 } 91 92 if (!succeeded) { 93 MetricsLogger.action(activity, 94 MetricsEvent.ACTION_DELETION_HELPER_DOWNLOADS_DELETION_FAIL); 95 } 96 } 97 }); 98 } 99 } 100 101 @Override onCreateLoader(int id, Bundle args)102 public Loader<DownloadsResult> onCreateLoader(int id, Bundle args) { 103 return new FetchDownloadsLoader(mContext, 104 Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)); 105 } 106 107 @Override onLoadFinished(Loader<DownloadsResult> loader, DownloadsResult data)108 public void onLoadFinished(Loader<DownloadsResult> loader, DownloadsResult data) { 109 mMostRecent = data.youngestLastModified; 110 for (File file : data.files) { 111 mFiles.add(file); 112 } 113 mBytes = data.totalSize; 114 maybeUpdateListener(); 115 } 116 117 @Override onLoaderReset(Loader<DownloadsResult> loader)118 public void onLoaderReset(Loader<DownloadsResult> loader) { 119 } 120 121 /** 122 * Returns the most recent last modified time for any clearable file. 123 * @return The last modified time. 124 */ getMostRecentLastModified()125 public long getMostRecentLastModified() { 126 return mMostRecent; 127 } 128 129 /** 130 * Returns the files in the Downloads folder after the loader task finishes. 131 */ getFiles()132 public Set<File> getFiles() { 133 if (mFiles == null) { 134 return null; 135 } 136 return mFiles; 137 } 138 139 /** 140 * Toggle if a file should be deleted when the service is asked to clear files. 141 */ toggleFile(File file, boolean checked)142 public void toggleFile(File file, boolean checked) { 143 if (checked) { 144 mUncheckedFiles.remove(file.getPath()); 145 } else { 146 mUncheckedFiles.add(file.getPath()); 147 } 148 } 149 150 /** 151 * Returns the number of bytes that would be cleared if the deletion tasks runs. 152 */ getFreeableBytes()153 public long getFreeableBytes() { 154 long freedBytes = 0; 155 for (File file : mFiles) { 156 if (isChecked(file)) { 157 freedBytes += file.length(); 158 } 159 } 160 return freedBytes; 161 } 162 163 /** 164 * Return if a given file is checked for deletion. 165 * @param file The file to check. 166 */ isChecked(File file)167 public boolean isChecked(File file) { 168 return !mUncheckedFiles.contains(file.getPath()); 169 } 170 maybeUpdateListener()171 private void maybeUpdateListener() { 172 if (mListener != null) { 173 mListener.onFreeableChanged(mFiles.size(), mBytes); 174 } 175 } 176 } 177