Add downloads to the deletion helper.

This adds a deletion service to delete files in the Downloads
folder. All of the files there are collected and offered to the
user to delete.

Bug: 28621781
Change-Id: I94431f9abc3a0afa2d07dbab763312c09e830aef
This commit is contained in:
Daniel Nishi
2016-05-11 12:54:13 -07:00
parent f4ffd6daff
commit 2ef20fb36d
10 changed files with 630 additions and 88 deletions

View File

@@ -25,6 +25,7 @@ import android.util.ArraySet;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.android.settings.deletionhelper.DownloadsDeletionPreference;
import com.android.settings.PhotosDeletionPreference;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.R;
@@ -54,10 +55,14 @@ public class DeletionHelperFragment extends SettingsPreferenceFragment implement
private static final String KEY_APPS_GROUP = "apps_group";
private static final String KEY_PHOTOS_VIDEOS_PREFERENCE = "delete_photos";
private static final String KEY_DOWNLOADS_PREFERENCE = "delete_downloads";
private static final int DOWNLOADS_LOADER_ID = 1;
private Button mCancel, mFree;
private PreferenceGroup mApps;
private PhotosDeletionPreference mPhotoPreference;
private DownloadsDeletionPreference mDownloadsPreference;
private ApplicationsState mState;
private Session mSession;
@@ -67,6 +72,7 @@ public class DeletionHelperFragment extends SettingsPreferenceFragment implement
private boolean mHasReceivedAppEntries, mHasReceivedBridgeCallback, mFinishedLoading;
private DeletionHelperFeatureProvider mProvider;
private DeletionType mPhotoVideoDeletion;
private DownloadsDeletionType mDownloadsDeletion;
@Override
public void onCreate(Bundle savedInstanceState) {
@@ -80,11 +86,14 @@ public class DeletionHelperFragment extends SettingsPreferenceFragment implement
addPreferencesFromResource(R.xml.deletion_helper_list);
mApps = (PreferenceGroup) findPreference(KEY_APPS_GROUP);
mPhotoPreference = (PhotosDeletionPreference) findPreference(KEY_PHOTOS_VIDEOS_PREFERENCE);
mDownloadsPreference =
(DownloadsDeletionPreference) findPreference(KEY_DOWNLOADS_PREFERENCE);
mProvider =
FeatureFactory.getFactory(app).getDeletionHelperFeatureProvider();
if (mProvider != null) {
mPhotoVideoDeletion = mProvider.createPhotoVideoDeletionType();
}
mDownloadsDeletion = new DownloadsDeletionType(getActivity());
if (savedInstanceState != null) {
mHasReceivedAppEntries =
@@ -117,6 +126,9 @@ public class DeletionHelperFragment extends SettingsPreferenceFragment implement
if (mPhotoPreference != null && mPhotoPreference.isChecked()) {
mPhotoVideoDeletion.clearFreeableData();
}
if (mDownloadsPreference != null && mDownloadsPreference.isChecked()) {
mDownloadsDeletion.clearFreeableData();
}
ArraySet<String> apps = new ArraySet<>();
for (AppEntry entry : mAppEntries) {
@@ -153,6 +165,9 @@ public class DeletionHelperFragment extends SettingsPreferenceFragment implement
mPhotoPreference.registerFreeableChangedListener(this);
mPhotoPreference.registerDeletionService(mPhotoVideoDeletion);
}
mDownloadsPreference.registerFreeableChangedListener(this);
mDownloadsPreference.registerDeletionService(mDownloadsDeletion);
}
@Override
@@ -172,6 +187,10 @@ public class DeletionHelperFragment extends SettingsPreferenceFragment implement
if (mPhotoVideoDeletion != null) {
mPhotoVideoDeletion.onResume();
}
if (mDownloadsDeletion != null) {
mDownloadsDeletion.onResume();
getLoaderManager().initLoader(DOWNLOADS_LOADER_ID, new Bundle(), mDownloadsDeletion);
}
}
@@ -193,6 +212,9 @@ public class DeletionHelperFragment extends SettingsPreferenceFragment implement
if (mPhotoVideoDeletion != null) {
mPhotoVideoDeletion.onPause();
}
if (mDownloadsDeletion != null) {
mDownloadsDeletion.onPause();
}
}
private void rebuild() {
@@ -211,7 +233,8 @@ public class DeletionHelperFragment extends SettingsPreferenceFragment implement
for (int i = 0; i < entryCount; i++) {
AppEntry entry = apps.get(i);
final String packageName = entry.label;
AppDeletionPreference preference = (AppDeletionPreference) getCachedPreference(entry.label);
AppDeletionPreference preference =
(AppDeletionPreference) getCachedPreference(entry.label);
if (preference == null) {
preference = new AppDeletionPreference(getActivity(), entry,
mState);
@@ -322,6 +345,9 @@ public class DeletionHelperFragment extends SettingsPreferenceFragment implement
if (mPhotoPreference != null) {
freeableSpace += mPhotoPreference.getFreeableBytes();
}
if (mDownloadsPreference != null) {
freeableSpace += mDownloadsPreference.getFreeableBytes();
}
return freeableSpace;
}
}

View File

@@ -0,0 +1,60 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.deletionhelper;
import android.content.Context;
import android.text.format.DateUtils;
import android.util.AttributeSet;
import android.text.format.Formatter;
import com.android.settings.DeletionPreference;
import com.android.settings.R;
/**
* Preference to handle the deletion of photos and videos in the Deletion Helper.
*/
public class DownloadsDeletionPreference extends DeletionPreference {
public DownloadsDeletionPreference(Context context, AttributeSet attrs) {
super(context, attrs);
updatePreferenceText(0, 0, Long.MAX_VALUE);
}
@Override
public void onFreeableChanged(int numItems, long freeableBytes) {
super.onFreeableChanged(numItems, freeableBytes);
DownloadsDeletionType deletionService = (DownloadsDeletionType) getDeletionService();
updatePreferenceText(numItems, freeableBytes, deletionService.getMostRecentLastModified());
}
private void updatePreferenceText(int items, long bytes, long mostRecent) {
Context context = getContext();
setTitle(context.getString(R.string.deletion_helper_downloads_title,
items));
// If there are no files to clear, show the empty text instead.
if (mostRecent < Long.MAX_VALUE) {
setSummary(context.getString(R.string.deletion_helper_downloads_summary,
Formatter.formatFileSize(context, bytes),
DateUtils.getRelativeTimeSpanString(mostRecent,
System.currentTimeMillis(),
DateUtils.DAY_IN_MILLIS,
DateUtils.FORMAT_ABBREV_RELATIVE)));
} else {
setSummary(context.getString(R.string.deletion_helper_downloads_summary_empty,
Formatter.formatFileSize(context, bytes)));
}
}
}

View File

@@ -0,0 +1,109 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.deletionhelper;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.Context;
import android.content.Loader;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import com.android.settings.deletionhelper.FetchDownloadsLoader.DownloadsResult;
import java.io.File;
import java.util.ArrayList;
/**
* The DownloadsDeletionType provides stale download file information to the
* {@link DownloadsDeletionPreference}.
*/
public class DownloadsDeletionType implements DeletionType, LoaderCallbacks<DownloadsResult> {
private int mItems;
private long mBytes;
private long mMostRecent;
private FreeableChangedListener mListener;
private FetchDownloadsLoader mTask;
private ArrayList<File> mFiles;
private Context mContext;
public DownloadsDeletionType(Context context) {
mContext = context;
}
@Override
public void registerFreeableChangedListener(FreeableChangedListener listener) {
mListener = listener;
if (mFiles != null) {
maybeUpdateListener();
}
}
@Override
public void onResume() {
}
@Override
public void onPause() {
}
@Override
public void clearFreeableData() {
if (mFiles != null) {
AsyncTask.execute(new Runnable() {
@Override
public void run() {
for (File file : mFiles) {
file.delete();
}
}
});
}
}
@Override
public Loader<DownloadsResult> onCreateLoader(int id, Bundle args) {
return new FetchDownloadsLoader(mContext,
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS));
}
@Override
public void onLoadFinished(Loader<DownloadsResult> loader, DownloadsResult data) {
mMostRecent = data.youngestLastModified;
mFiles = data.files;
mBytes = data.totalSize;
mItems = mFiles.size();
maybeUpdateListener();
}
@Override
public void onLoaderReset(Loader<DownloadsResult> loader) {
}
/**
* Returns the most recent last modified time for any clearable file.
* @return The last modified time.
*/
public long getMostRecentLastModified() {
return mMostRecent;
}
private void maybeUpdateListener() {
if (mListener != null) {
mListener.onFreeableChanged(mItems, mBytes);
}
}
}

View File

@@ -0,0 +1,96 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.deletionhelper;
import android.content.Context;
import android.support.annotation.VisibleForTesting;
import com.android.settings.utils.AsyncLoader;
import java.io.File;
import java.util.ArrayList;
/**
* FetchDownloadsLoader is an asynchronous task which returns files in the Downloads
* directory which have not been modified in longer than 90 days.
*/
public class FetchDownloadsLoader extends
AsyncLoader<FetchDownloadsLoader.DownloadsResult> {
private File mDirectory;
/**
* Sets up a FetchDownloadsLoader in any directory.
* @param directory The directory to look into.
*/
public FetchDownloadsLoader(Context context, File directory) {
super(context);
mDirectory = directory;
}
@Override
protected void onDiscardResult(DownloadsResult result) {}
@Override
public DownloadsResult loadInBackground() {
return collectFiles(mDirectory);
}
@VisibleForTesting
static DownloadsResult collectFiles(File dir) {
return collectFiles(dir, new DownloadsResult());
}
private static DownloadsResult collectFiles(File dir, DownloadsResult result) {
File downloadFiles[] = dir.listFiles();
if (downloadFiles == null) {
}
if (downloadFiles != null && downloadFiles.length > 0) {
for (File currentFile : downloadFiles) {
if (currentFile.isDirectory()) {
collectFiles(currentFile, result);
} else {
if (currentFile.lastModified() < result.youngestLastModified) {
result.youngestLastModified = currentFile.lastModified();
}
result.files.add(currentFile);
result.totalSize += currentFile.length();
}
}
}
return result;
}
/**
* The DownloadsResult is the result of a {@link FetchDownloadsLoader} with the files
* and the amount of space they use.
*/
public static class DownloadsResult {
public long totalSize;
public long youngestLastModified;
public ArrayList<File> files;
public DownloadsResult() {
this(0, Long.MAX_VALUE, new ArrayList<File>());
}
public DownloadsResult(long totalSize, long youngestLastModified, ArrayList<File> files) {
this.totalSize = totalSize;
this.youngestLastModified = youngestLastModified;
this.files = files;
}
}
}