summaryrefslogtreecommitdiffstats
path: root/packages/DocumentsUI/src/com/android/documentsui/CopyService.java
diff options
context:
space:
mode:
authorBen Kwa <kenobi@google.com>2015-04-08 19:05:24 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2015-04-08 19:05:26 +0000
commit6e08dbc366befde334fc9cf5728d4043d8b86a37 (patch)
treeec53455f9a5960afd3150510446db7ccbe2e80e4 /packages/DocumentsUI/src/com/android/documentsui/CopyService.java
parent6d5c0ba98c93ce3c9d43c7131ec787c6b762a71a (diff)
parentef3f2620b3a755856d70345fc7a90df896985c26 (diff)
downloadframeworks_base-6e08dbc366befde334fc9cf5728d4043d8b86a37.zip
frameworks_base-6e08dbc366befde334fc9cf5728d4043d8b86a37.tar.gz
frameworks_base-6e08dbc366befde334fc9cf5728d4043d8b86a37.tar.bz2
Merge "Prototype the destination picking."
Diffstat (limited to 'packages/DocumentsUI/src/com/android/documentsui/CopyService.java')
-rw-r--r--packages/DocumentsUI/src/com/android/documentsui/CopyService.java57
1 files changed, 35 insertions, 22 deletions
diff --git a/packages/DocumentsUI/src/com/android/documentsui/CopyService.java b/packages/DocumentsUI/src/com/android/documentsui/CopyService.java
index f7d8cc4..f135af49b 100644
--- a/packages/DocumentsUI/src/com/android/documentsui/CopyService.java
+++ b/packages/DocumentsUI/src/com/android/documentsui/CopyService.java
@@ -24,7 +24,11 @@ import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
+import android.os.CancellationSignal;
+import android.os.Environment;
+import android.os.ParcelFileDescriptor;
import android.os.SystemClock;
+import android.provider.DocumentsContract;
import android.text.format.DateUtils;
import android.util.Log;
@@ -82,18 +86,15 @@ public class CopyService extends IntentService {
}
ArrayList<DocumentInfo> srcs = intent.getParcelableArrayListExtra(EXTRA_SRC_LIST);
- // Use the app local files dir as a copy destination for now. This resolves to
- // /data/data/com.android.documentsui/files.
- // TODO: Add actual destination picking.
- File destinationDir = getFilesDir();
+ Uri destinationUri = intent.getData();
- setupCopyJob(srcs, destinationDir);
+ setupCopyJob(srcs, destinationUri);
ArrayList<String> failedFilenames = new ArrayList<String>();
for (int i = 0; i < srcs.size() && !mIsCancelled; ++i) {
DocumentInfo src = srcs.get(i);
try {
- copyFile(src, destinationDir);
+ copyFile(src, destinationUri);
} catch (IOException e) {
Log.e(TAG, "Failed to copy " + src.displayName, e);
failedFilenames.add(src.displayName);
@@ -121,8 +122,9 @@ public class CopyService extends IntentService {
* files.
*
* @param srcs A list of src files to copy.
+ * @param destinationUri The URI of the destination directory.
*/
- private void setupCopyJob(ArrayList<DocumentInfo> srcs, File destinationDir) {
+ private void setupCopyJob(ArrayList<DocumentInfo> srcs, Uri destinationUri) {
// Create an ID for this copy job. Use the timestamp.
mJobId = String.valueOf(SystemClock.elapsedRealtime());
// Reset the cancellation flag.
@@ -238,42 +240,53 @@ public class CopyService extends IntentService {
* Copies a file to a given location.
*
* @param srcInfo The source file.
- * @param destination The directory to copy into.
+ * @param destinationUri The URI of the destination directory.
* @throws IOException
*/
- private void copyFile(DocumentInfo srcInfo, File destinationDir)
- throws IOException {
+ private void copyFile(DocumentInfo srcInfo, Uri destinationUri) throws IOException {
final Context context = getApplicationContext();
final ContentResolver resolver = context.getContentResolver();
- final File destinationFile = new File(destinationDir, srcInfo.displayName);
- final Uri destinationUri = Uri.fromFile(destinationFile);
- InputStream source = null;
- OutputStream destination = null;
+ final Uri writableDstUri = DocumentsContract.buildDocumentUriUsingTree(destinationUri,
+ DocumentsContract.getTreeDocumentId(destinationUri));
+ final Uri dstFileUri = DocumentsContract.createDocument(resolver, writableDstUri,
+ srcInfo.mimeType, srcInfo.displayName);
+
+ CancellationSignal canceller = new CancellationSignal();
+ ParcelFileDescriptor srcFile = null;
+ ParcelFileDescriptor dstFile = null;
+ InputStream src = null;
+ OutputStream dst = null;
boolean errorOccurred = false;
try {
- source = resolver.openInputStream(srcInfo.derivedUri);
- destination = resolver.openOutputStream(destinationUri);
+ srcFile = resolver.openFileDescriptor(srcInfo.derivedUri, "r", canceller);
+ dstFile = resolver.openFileDescriptor(dstFileUri, "w", canceller);
+ src = new ParcelFileDescriptor.AutoCloseInputStream(srcFile);
+ dst = new ParcelFileDescriptor.AutoCloseOutputStream(dstFile);
byte[] buffer = new byte[8192];
int len;
- while (!mIsCancelled && ((len = source.read(buffer)) != -1)) {
- destination.write(buffer, 0, len);
+ while (!mIsCancelled && ((len = src.read(buffer)) != -1)) {
+ dst.write(buffer, 0, len);
makeProgress(len);
}
+ srcFile.checkError();
+ dstFile.checkError();
} catch (IOException e) {
errorOccurred = true;
Log.e(TAG, "Error while copying " + srcInfo.displayName, e);
} finally {
- IoUtils.closeQuietly(source);
- IoUtils.closeQuietly(destination);
+ // This also ensures the file descriptors are closed.
+ IoUtils.closeQuietly(src);
+ IoUtils.closeQuietly(dst);
}
if (errorOccurred || mIsCancelled) {
// Clean up half-copied files.
- if (!destinationFile.delete()) {
- Log.w(TAG, "Failed to clean up partially copied file " + srcInfo.displayName);
+ canceller.cancel();
+ if (!DocumentsContract.deleteDocument(resolver, dstFileUri)) {
+ Log.w(TAG, "Failed to clean up: " + srcInfo.displayName);
}
}
}