Merge "Ensure to complete background thread of PipeManager." into nyc-dev
am: a30918dc26
* commit 'a30918dc26876a16ee2a56d2549806f0b5291e2c':
Ensure to complete background thread of PipeManager.
This commit is contained in:
@@ -440,7 +440,7 @@ public class MtpDocumentsProvider extends DocumentsProvider {
|
||||
if (DEBUG) {
|
||||
Log.d(TAG, "Close device " + deviceId);
|
||||
}
|
||||
getDeviceToolkit(deviceId).mDocumentLoader.close();
|
||||
getDeviceToolkit(deviceId).close();
|
||||
mDeviceToolkits.remove(deviceId);
|
||||
mMtpManager.closeDevice(deviceId);
|
||||
if (mDeviceToolkits.size() == 0) {
|
||||
@@ -496,7 +496,7 @@ public class MtpDocumentsProvider extends DocumentsProvider {
|
||||
return cursor;
|
||||
}
|
||||
|
||||
private static class DeviceToolkit {
|
||||
private static class DeviceToolkit implements AutoCloseable {
|
||||
public final PipeManager mPipeManager;
|
||||
public final DocumentLoader mDocumentLoader;
|
||||
public final MtpDeviceRecord mDeviceRecord;
|
||||
@@ -509,6 +509,12 @@ public class MtpDocumentsProvider extends DocumentsProvider {
|
||||
mDocumentLoader = new DocumentLoader(record, manager, resolver, database);
|
||||
mDeviceRecord = record;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws InterruptedException {
|
||||
mPipeManager.close();
|
||||
mDocumentLoader.close();
|
||||
}
|
||||
}
|
||||
|
||||
private class AppFuseCallback implements AppFuse.Callback {
|
||||
|
||||
@@ -26,8 +26,14 @@ import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
class PipeManager {
|
||||
/**
|
||||
* Milliseconds we wait for background thread when pausing.
|
||||
*/
|
||||
private final static long AWAIT_TERMINATION_TIMEOUT = 2000;
|
||||
|
||||
final ExecutorService mExecutor;
|
||||
final MtpDatabase mDatabase;
|
||||
|
||||
@@ -203,7 +209,8 @@ class PipeManager {
|
||||
}
|
||||
}
|
||||
|
||||
void close() {
|
||||
mExecutor.shutdown();
|
||||
boolean close() throws InterruptedException {
|
||||
mExecutor.shutdownNow();
|
||||
return mExecutor.awaitTermination(AWAIT_TERMINATION_TIMEOUT, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,6 +45,11 @@ public class PipeManagerTest extends AndroidTestCase {
|
||||
mPipeManager = new PipeManager(mDatabase, mExecutor);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
assertTrue(mPipeManager.close());
|
||||
}
|
||||
|
||||
public void testReadDocument_basic() throws Exception {
|
||||
mtpManager.setImportFileBytes(0, 1, HELLO_BYTES);
|
||||
final ParcelFileDescriptor descriptor = mPipeManager.readDocument(
|
||||
|
||||
Reference in New Issue
Block a user