Home | History | Annotate | Download | only in services
      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.documentsui.services;
     18 
     19 import static com.android.documentsui.base.SharedMinimal.DEBUG;
     20 import static com.android.documentsui.services.FileOperationService.OPERATION_DELETE;
     21 
     22 import android.app.Notification;
     23 import android.app.Notification.Builder;
     24 import android.content.ContentResolver;
     25 import android.content.Context;
     26 import android.net.Uri;
     27 import android.util.Log;
     28 
     29 import com.android.documentsui.Metrics;
     30 import com.android.documentsui.R;
     31 import com.android.documentsui.base.DocumentInfo;
     32 import com.android.documentsui.base.DocumentStack;
     33 import com.android.documentsui.base.Features;
     34 import com.android.documentsui.clipping.UrisSupplier;
     35 
     36 import java.io.FileNotFoundException;
     37 
     38 import javax.annotation.Nullable;
     39 
     40 final class DeleteJob extends ResolvedResourcesJob {
     41 
     42     private static final String TAG = "DeleteJob";
     43 
     44     private final Uri mParentUri;
     45 
     46     private volatile int mDocsProcessed = 0;
     47     /**
     48      * Moves files to a destination identified by {@code destination}.
     49      * Performs most work by delegating to CopyJob, then deleting
     50      * a file after it has been copied.
     51      *
     52      * @see @link {@link Job} constructor for most param descriptions.
     53      */
     54     DeleteJob(Context service, Listener listener, String id, DocumentStack stack,
     55             UrisSupplier srcs, @Nullable Uri srcParent, Features features) {
     56         super(service, listener, id, OPERATION_DELETE, stack, srcs, features);
     57         mParentUri = srcParent;
     58     }
     59 
     60     @Override
     61     Builder createProgressBuilder() {
     62         return super.createProgressBuilder(
     63                 service.getString(R.string.delete_notification_title),
     64                 R.drawable.ic_menu_delete,
     65                 service.getString(android.R.string.cancel),
     66                 R.drawable.ic_cab_cancel);
     67     }
     68 
     69     @Override
     70     public Notification getSetupNotification() {
     71         return getSetupNotification(service.getString(R.string.delete_preparing));
     72     }
     73 
     74     @Override
     75     public Notification getProgressNotification() {
     76         mProgressBuilder.setProgress(mResourceUris.getItemCount(), mDocsProcessed, false);
     77         String format = service.getString(R.string.delete_progress);
     78         mProgressBuilder.setSubText(
     79                 String.format(format, mDocsProcessed, mResourceUris.getItemCount()));
     80 
     81         mProgressBuilder.setContentText(null);
     82 
     83         return mProgressBuilder.build();
     84     }
     85 
     86     @Override
     87     Notification getFailureNotification() {
     88         return getFailureNotification(
     89                 R.plurals.delete_error_notification_title, R.drawable.ic_menu_delete);
     90     }
     91 
     92     @Override
     93     Notification getWarningNotification() {
     94         throw new UnsupportedOperationException();
     95     }
     96 
     97     @Override
     98     void start() {
     99         ContentResolver resolver = appContext.getContentResolver();
    100 
    101         DocumentInfo parentDoc;
    102         try {
    103             parentDoc = mParentUri != null
    104                 ? DocumentInfo.fromUri(resolver, mParentUri)
    105                 : null;
    106         } catch (FileNotFoundException e) {
    107           Log.e(TAG, "Failed to resolve parent from Uri: " + mParentUri + ". Cannot continue.", e);
    108           failureCount += this.mResourceUris.getItemCount();
    109           return;
    110         }
    111 
    112         for (DocumentInfo doc : mResolvedDocs) {
    113             if (DEBUG) Log.d(TAG, "Deleting document @ " + doc.derivedUri);
    114             try {
    115                 deleteDocument(doc, parentDoc);
    116             } catch (ResourceException e) {
    117                 Metrics.logFileOperationFailure(
    118                         appContext, Metrics.SUBFILEOP_DELETE_DOCUMENT, doc.derivedUri);
    119                 Log.e(TAG, "Failed to delete document @ " + doc.derivedUri, e);
    120                 onFileFailed(doc);
    121             }
    122 
    123             mDocsProcessed++;
    124             if (isCanceled()) {
    125                 return;
    126             }
    127         }
    128 
    129         Metrics.logFileOperation(service, operationType, mResolvedDocs, null);
    130     }
    131 
    132     @Override
    133     public String toString() {
    134         return new StringBuilder()
    135                 .append("DeleteJob")
    136                 .append("{")
    137                 .append("id=" + id)
    138                 .append(", uris=" + mResourceUris)
    139                 .append(", docs=" + mResolvedDocs)
    140                 .append(", srcParent=" + mParentUri)
    141                 .append(", location=" + stack)
    142                 .append("}")
    143                 .toString();
    144     }
    145 }
    146