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
17package com.android.documentsui.services;
18
19import static com.android.documentsui.Shared.DEBUG;
20import static com.android.documentsui.services.FileOperationService.OPERATION_DELETE;
21
22import android.app.Notification;
23import android.app.Notification.Builder;
24import android.content.Context;
25import android.util.Log;
26
27import com.android.documentsui.Metrics;
28import com.android.documentsui.R;
29import com.android.documentsui.model.DocumentInfo;
30import com.android.documentsui.model.DocumentStack;
31
32import java.util.List;
33
34final class DeleteJob extends Job {
35
36    private static final String TAG = "DeleteJob";
37    private List<DocumentInfo> mSrcs;
38    final DocumentInfo mSrcParent;
39
40    /**
41     * Moves files to a destination identified by {@code destination}.
42     * Performs most work by delegating to CopyJob, then deleting
43     * a file after it has been copied.
44     *
45     * @see @link {@link Job} constructor for most param descriptions.
46     *
47     * @param srcs List of files to delete.
48     * @param srcParent Parent of all source files.
49     */
50    DeleteJob(Context service, Context appContext, Listener listener,
51            String id, DocumentStack stack, List<DocumentInfo> srcs, DocumentInfo srcParent) {
52        super(service, appContext, listener, OPERATION_DELETE, id, stack);
53        this.mSrcs = srcs;
54        this.mSrcParent = srcParent;
55    }
56
57    @Override
58    Builder createProgressBuilder() {
59        return super.createProgressBuilder(
60                service.getString(R.string.delete_notification_title),
61                R.drawable.ic_menu_delete,
62                service.getString(android.R.string.cancel),
63                R.drawable.ic_cab_cancel);
64    }
65
66    @Override
67    public Notification getSetupNotification() {
68        return getSetupNotification(service.getString(R.string.delete_preparing));
69    }
70
71    @Override
72    Notification getFailureNotification() {
73        return getFailureNotification(
74                R.plurals.delete_error_notification_title, R.drawable.ic_menu_delete);
75    }
76
77    @Override
78    Notification getWarningNotification() {
79        throw new UnsupportedOperationException();
80    }
81
82    @Override
83    void start() {
84        for (DocumentInfo doc : mSrcs) {
85            if (DEBUG) Log.d(TAG, "Deleting document @ " + doc.derivedUri);
86            try {
87                deleteDocument(doc, mSrcParent);
88            } catch (ResourceException e) {
89                Log.e(TAG, "Failed to delete document @ " + doc.derivedUri, e);
90                onFileFailed(doc);
91            }
92        }
93        Metrics.logFileOperation(service, operationType, mSrcs, null);
94    }
95
96    @Override
97    public String toString() {
98        return new StringBuilder()
99                .append("DeleteJob")
100                .append("{")
101                .append("id=" + id)
102                .append(", srcs=" + mSrcs)
103                .append(", srcParent=" + mSrcParent)
104                .append(", location=" + stack)
105                .append("}")
106                .toString();
107    }
108}
109