MoveJob.java revision d46ecbcc5322cf817e64591e985f1f2a6167e9a7
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.services.FileOperationService.OPERATION_MOVE;
20
21import android.app.Notification;
22import android.app.Notification.Builder;
23import android.content.Context;
24import android.net.Uri;
25import android.os.RemoteException;
26import android.provider.DocumentsContract;
27import android.provider.DocumentsContract.Document;
28import android.util.Log;
29
30import com.android.documentsui.R;
31import com.android.documentsui.model.DocumentInfo;
32import com.android.documentsui.model.DocumentStack;
33
34import java.util.List;
35
36final class MoveJob extends CopyJob {
37
38    private static final String TAG = "MoveJob";
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 be moved.
48     */
49    MoveJob(Context service, Context appContext, Listener listener,
50            String id, DocumentStack destination, List<DocumentInfo> srcs) {
51        super(service, appContext, listener, OPERATION_MOVE, id, destination, srcs);
52    }
53
54    @Override
55    Builder createProgressBuilder() {
56        return super.createProgressBuilder(
57                service.getString(R.string.move_notification_title),
58                R.drawable.ic_menu_copy,
59                service.getString(android.R.string.cancel),
60                R.drawable.ic_cab_cancel);
61    }
62
63    @Override
64    public Notification getSetupNotification() {
65        return getSetupNotification(service.getString(R.string.move_preparing));
66    }
67
68    @Override
69    public Notification getProgressNotification() {
70        return getProgressNotification(R.string.copy_preparing);
71    }
72
73    @Override
74    Notification getFailureNotification() {
75        return getFailureNotification(
76                R.plurals.move_error_notification_title, R.drawable.ic_menu_copy);
77    }
78
79    @Override
80    boolean processDocument(DocumentInfo src, DocumentInfo dest) throws RemoteException {
81
82        // TODO: When optimized move kicks in, we're not making any progress updates. FIX IT!
83
84        // When moving within the same provider, try to use optimized moving.
85        // If not supported, then fallback to byte-by-byte copy/move.
86        if (src.authority.equals(dest.authority)) {
87            if ((src.flags & Document.FLAG_SUPPORTS_MOVE) != 0) {
88                if (DocumentsContract.moveDocument(getClient(src), src.derivedUri,
89                        Uri.EMPTY /* Not used yet */, dest.derivedUri) == null) {
90                    onFileFailed(src);
91                    return false;
92                }
93                return true;
94            }
95        }
96
97        // Moving virtual files by bytes is not supported. This is because, it would involve
98        // conversion, and the source file should not be deleted in such case (as it's a different
99        // file).
100        if (src.isVirtualDocument()) {
101            Log.w(TAG, "Cannot move virtual files byte by byte.");
102            onFileFailed(src);
103            return false;
104        }
105
106        // If we couldn't do an optimized copy...we fall back to vanilla byte copy.
107        boolean copied = byteCopyDocument(src, dest);
108
109        return copied && !isCanceled() && deleteDocument(src);
110    }
111
112    @Override
113    public String toString() {
114        return new StringBuilder()
115                .append("MoveJob")
116                .append("{")
117                .append("id=" + id)
118                .append("srcs=" + mSrcs)
119                .append(", destination=" + stack)
120                .append("}")
121                .toString();
122    }
123}
124