OSDN Git Service

Improve copy/move performance with nio and reintroduce cancel.
[android-x86/packages-apps-CMFileManager.git] / src / com / cyanogenmod / filemanager / commands / java / MoveCommand.java
1 /*
2  * Copyright (C) 2012 The CyanogenMod 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.cyanogenmod.filemanager.commands.java;
18
19 import android.util.Log;
20
21 import com.cyanogenmod.filemanager.commands.MoveExecutable;
22 import com.cyanogenmod.filemanager.console.CancelledOperationException;
23 import com.cyanogenmod.filemanager.console.ExecutionException;
24 import com.cyanogenmod.filemanager.console.InsufficientPermissionsException;
25 import com.cyanogenmod.filemanager.console.NoSuchFileOrDirectory;
26 import com.cyanogenmod.filemanager.model.MountPoint;
27 import com.cyanogenmod.filemanager.util.FileHelper;
28 import com.cyanogenmod.filemanager.util.MountPointHelper;
29
30 import java.io.File;
31
32
33 /**
34  * A class for move a file or directory.
35  */
36 public class MoveCommand extends Program implements MoveExecutable {
37
38     private static final String TAG = "MoveCommand"; //$NON-NLS-1$
39
40     private final String mSrc;
41     private final String mDst;
42
43     /**
44      * Constructor of <code>MoveCommand</code>.
45      *
46      * @param src The name of the file or directory to be moved
47      * @param dst The name of the file or directory in which move the source file or directory
48      */
49     public MoveCommand(String src, String dst) {
50         super();
51         this.mSrc = src;
52         this.mDst = dst;
53     }
54
55     /**
56      * {@inheritDoc}
57      */
58     @Override
59     public Boolean getResult() {
60         return Boolean.TRUE;
61     }
62
63     /**
64      * {@inheritDoc}
65      */
66     @Override
67     public void execute()
68             throws InsufficientPermissionsException, NoSuchFileOrDirectory, ExecutionException,
69                    CancelledOperationException {
70         if (isTrace()) {
71             Log.v(TAG,
72                     String.format("Creating from %s to %s", this.mSrc, this.mDst)); //$NON-NLS-1$
73         }
74
75         File s = new File(this.mSrc);
76         File d = new File(this.mDst);
77         if (!s.exists()) {
78             if (isTrace()) {
79                 Log.v(TAG, "Result: FAIL. NoSuchFileOrDirectory"); //$NON-NLS-1$
80             }
81             throw new NoSuchFileOrDirectory(this.mSrc);
82         }
83
84         //Move or copy recursively
85         if (d.exists()) {
86             if (!FileHelper.copyRecursive(s, d, this)) {
87                 if (isTrace()) {
88                     Log.v(TAG, "Result: FAIL. InsufficientPermissionsException"); //$NON-NLS-1$
89                 }
90                 throw new InsufficientPermissionsException();
91             }
92             if (!FileHelper.deleteFolder(s)) {
93                 if (isTrace()) {
94                     Log.v(TAG, "Result: OK. WARNING. Source not deleted."); //$NON-NLS-1$
95                 }
96             }
97         } else {
98             // Move between filesystem is not allow. If rename fails then use copy operation
99             if (!s.renameTo(d)) {
100                 if (!FileHelper.copyRecursive(s, d, this)) {
101                     if (isTrace()) {
102                         Log.v(TAG, "Result: FAIL. InsufficientPermissionsException"); //$NON-NLS-1$
103                     }
104                     throw new InsufficientPermissionsException();
105                 }
106                 if (!FileHelper.deleteFolder(s)) {
107                     if (isTrace()) {
108                         Log.v(TAG, "Result: OK. WARNING. Source not deleted."); //$NON-NLS-1$
109                     }
110                 }
111             }
112         }
113
114         if (isTrace()) {
115             Log.v(TAG, "Result: OK"); //$NON-NLS-1$
116         }
117     }
118
119     /**
120      * {@inheritDoc}
121      */
122     @Override
123     public MountPoint getSrcWritableMountPoint() {
124         return MountPointHelper.getMountPointFromDirectory(this.mSrc);
125     }
126
127     /**
128      * {@inheritDoc}
129      */
130     @Override
131     public MountPoint getDstWritableMountPoint() {
132         return MountPointHelper.getMountPointFromDirectory(this.mDst);
133     }
134
135 }