blob: 7f498f5fae23113c2314619974495eee14c2fa67 [file] [log] [blame]
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.mtp;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
class PipeManager {
final ExecutorService mExecutor;
PipeManager() {
this(Executors.newCachedThreadPool());
}
PipeManager(ExecutorService executor) {
this.mExecutor = executor;
}
ParcelFileDescriptor readDocument(MtpManager model, Identifier identifier) throws IOException {
final Task task = new ImportFileTask(model, identifier);
mExecutor.execute(task);
return task.getReadingFileDescriptor();
}
ParcelFileDescriptor readThumbnail(MtpManager model, Identifier identifier) throws IOException {
final Task task = new GetThumbnailTask(model, identifier);
mExecutor.execute(task);
return task.getReadingFileDescriptor();
}
private static abstract class Task implements Runnable {
protected final MtpManager mModel;
protected final Identifier mIdentifier;
protected final ParcelFileDescriptor[] mDescriptors;
Task(MtpManager model, Identifier identifier) throws IOException {
mModel = model;
mIdentifier = identifier;
mDescriptors = ParcelFileDescriptor.createReliablePipe();
}
ParcelFileDescriptor getReadingFileDescriptor() {
return mDescriptors[0];
}
}
private static class ImportFileTask extends Task {
ImportFileTask(MtpManager model, Identifier identifier) throws IOException {
super(model, identifier);
}
@Override
public void run() {
try {
mModel.importFile(
mIdentifier.mDeviceId, mIdentifier.mObjectHandle, mDescriptors[1]);
mDescriptors[1].close();
} catch (IOException error) {
try {
mDescriptors[1].closeWithError("Failed to stream a file.");
} catch (IOException closeError) {
Log.w(MtpDocumentsProvider.TAG, closeError.getMessage());
}
}
}
}
private static class GetThumbnailTask extends Task {
GetThumbnailTask(MtpManager model, Identifier identifier) throws IOException {
super(model, identifier);
}
@Override
public void run() {
try {
try (final ParcelFileDescriptor.AutoCloseOutputStream stream =
new ParcelFileDescriptor.AutoCloseOutputStream(mDescriptors[1])) {
try {
stream.write(mModel.getThumbnail(
mIdentifier.mDeviceId, mIdentifier.mObjectHandle));
} catch (IOException error) {
mDescriptors[1].closeWithError("Failed to stream a thumbnail.");
}
}
} catch (IOException closeError) {
Log.w(MtpDocumentsProvider.TAG, closeError.getMessage());
}
}
}
void close() {
mExecutor.shutdown();
}
}