Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a30918dc authored by Daichi Hirono's avatar Daichi Hirono Committed by Android (Google) Code Review
Browse files

Merge "Ensure to complete background thread of PipeManager." into nyc-dev

parents e95ba66b 24ab92a5
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
@@ -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 {
+9 −2
Original line number Diff line number Diff line
@@ -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);
    }
}
+5 −0
Original line number Diff line number Diff line
@@ -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(