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

Commit 98c3003a authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Do not block main thread when stop()" into main

parents 849b9a1e 0ac0acf2
Loading
Loading
Loading
Loading
+18 −26
Original line number Diff line number Diff line
@@ -79,9 +79,6 @@ public class BluetoothOppObexClientSession implements BluetoothOppObexSession {
    @VisibleForTesting
    volatile boolean mWaitingForRemote;

    @VisibleForTesting
    Handler mCallback;

    private int mNumFilesAttemptedToSend;

    public BluetoothOppObexClientSession(Context context, ObexTransport transport) {
@@ -97,8 +94,7 @@ public class BluetoothOppObexClientSession implements BluetoothOppObexSession {
        if (D) {
            Log.d(TAG, "Start!");
        }
        mCallback = handler;
        mThread = new ClientThread(mContext, mTransport, numShares);
        mThread = new ClientThread(mContext, mTransport, numShares, handler);
        mThread.start();
    }

@@ -109,21 +105,13 @@ public class BluetoothOppObexClientSession implements BluetoothOppObexSession {
        }
        if (mThread != null) {
            mInterrupted = true;
            try {
                mThread.interrupt();
            if (V) {
                    Log.v(TAG, "waiting for thread to terminate");
                Log.v(TAG, "Interrupt thread to terminate it");
            }
                mThread.join();
            mThread.interrupt();
            mThread = null;
            } catch (InterruptedException e) {
                if (V) {
                    Log.v(TAG, "Interrupted waiting for thread to join");
                }
            }
        }
        BluetoothOppUtility.cancelNotification(mContext);
        mCallback = null;
    }

    @Override
@@ -167,8 +155,10 @@ public class BluetoothOppObexClientSession implements BluetoothOppObexSession {
        private boolean mConnected = false;

        private int mNumShares;
        private final Handler mCallbackHandler;

        ClientThread(Context context, ObexTransport transport, int initialNumShares) {
        ClientThread(
                Context context, ObexTransport transport, int initialNumShares, Handler callback) {
            super("BtOpp ClientThread");
            mContext1 = context;
            mTransport1 = transport;
@@ -177,6 +167,7 @@ public class BluetoothOppObexClientSession implements BluetoothOppObexSession {
            mNumShares = initialNumShares;
            PowerManager pm = mContext.getSystemService(PowerManager.class);
            mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
            mCallbackHandler = callback;
        }

        public void addShare(BluetoothOppShareInfo info) {
@@ -235,11 +226,10 @@ public class BluetoothOppObexClientSession implements BluetoothOppObexSession {
                // Log outgoing OPP transfer if more than one file is accepted by remote
                MetricsLogger.logProfileConnectionEvent(BluetoothMetricsProto.ProfileId.OPP);
            }
            Message msg = Message.obtain(mCallback);
            Message msg = Message.obtain(mCallbackHandler);
            msg.what = BluetoothOppObexSession.MSG_SESSION_COMPLETE;
            msg.obj = mInfo;
            msg.sendToTarget();

        }

        private void disconnect() {
@@ -338,7 +328,7 @@ public class BluetoothOppObexClientSession implements BluetoothOppObexSession {
                Constants.updateShareStatus(mContext1, mInfo.mId, status);
            }

            Message msg = Message.obtain(mCallback);
            Message msg = Message.obtain(mCallbackHandler);
            msg.what = (status == BluetoothShare.STATUS_SUCCESS)
                    ? BluetoothOppObexSession.MSG_SHARE_COMPLETE
                    : BluetoothOppObexSession.MSG_SESSION_ERROR;
@@ -469,7 +459,8 @@ public class BluetoothOppObexClientSession implements BluetoothOppObexSession {
                    if (!mInterrupted && (position != fileInfo.mLength)) {
                        readLength = readFully(a, buffer, outputBufferSize);

                        mCallback.sendMessageDelayed(mCallback.obtainMessage(
                        mCallbackHandler.sendMessageDelayed(
                                mCallbackHandler.obtainMessage(
                                        BluetoothOppObexSession.MSG_CONNECT_TIMEOUT),
                                BluetoothOppObexSession.SESSION_TIMEOUT);
                        synchronized (this) {
@@ -491,7 +482,8 @@ public class BluetoothOppObexClientSession implements BluetoothOppObexSession {
                        /* check remote accept or reject */
                        responseCode = putOperation.getResponseCode();

                        mCallback.removeMessages(BluetoothOppObexSession.MSG_CONNECT_TIMEOUT);
                        mCallbackHandler.removeMessages(
                                BluetoothOppObexSession.MSG_CONNECT_TIMEOUT);
                        synchronized (this) {
                            mWaitingForRemote = false;
                        }
@@ -643,7 +635,7 @@ public class BluetoothOppObexClientSession implements BluetoothOppObexSession {
            // error during transfer.
            Constants.updateShareStatus(mContext1, mInfo.mId,
                    BluetoothShare.STATUS_OBEX_DATA_ERROR);
            mCallback.removeMessages(BluetoothOppObexSession.MSG_CONNECT_TIMEOUT);
            mCallbackHandler.removeMessages(BluetoothOppObexSession.MSG_CONNECT_TIMEOUT);
        }

        @Override
@@ -659,7 +651,7 @@ public class BluetoothOppObexClientSession implements BluetoothOppObexSession {
                    } catch (IOException e) {
                        Log.e(TAG, "mTransport.close error");
                    }
                    Message msg = Message.obtain(mCallback);
                    Message msg = Message.obtain(mCallbackHandler);
                    msg.what = BluetoothOppObexSession.MSG_SHARE_INTERRUPTED;
                    if (mInfo != null) {
                        msg.obj = mInfo;
+18 −13
Original line number Diff line number Diff line
@@ -135,7 +135,6 @@ public class BluetoothOppObexClientSessionTest {
        mClientSession.stop();

        BluetoothOppUtility.sSendFileMap.clear();

        assertThat(sessionCompletedLatch.await(3_000, TimeUnit.MILLISECONDS)).isTrue();
    }

@@ -162,8 +161,10 @@ public class BluetoothOppObexClientSessionTest {
        BluetoothOppSendFileInfo sendFileInfo = new BluetoothOppSendFileInfo(
                filename, mimetype, totalBytes, null, status);

        BluetoothOppObexClientSession.ClientThread thread = mClientSession.new ClientThread(
                mTargetContext, mTransport, 0);
        BluetoothOppObexClientSession.ClientThread thread =
                mClientSession
                .new ClientThread(
                        mTargetContext, mTransport, 0, new Handler(Looper.getMainLooper()));
        InputStream is = mock(InputStream.class);
        OutputStream os = mock(OutputStream.class);
        doReturn(is).when(mTransport).openInputStream();
@@ -178,7 +179,13 @@ public class BluetoothOppObexClientSessionTest {
    @Test
    public void clientThreadInterrupt_sendMessageShareInterrupted() throws InterruptedException {
        CountDownLatch sessionInterruptLatch = new CountDownLatch(1);
        mClientSession.mCallback = new Handler(Looper.getMainLooper()) {
        BluetoothOppObexClientSession.ClientThread thread =
                mClientSession
                .new ClientThread(
                        mTargetContext,
                        mTransport,
                        0,
                        new Handler(Looper.getMainLooper()) {
                            @Override
                            public void handleMessage(Message msg) {
                                super.handleMessage(msg);
@@ -186,9 +193,7 @@ public class BluetoothOppObexClientSessionTest {
                                    sessionInterruptLatch.countDown();
                                }
                            }
        };
        BluetoothOppObexClientSession.ClientThread thread =
                mClientSession.new ClientThread(mTargetContext, mTransport, 0);
                        });
        mClientSession.mWaitingForRemote = true;
        thread.interrupt();
        assertThat(sessionInterruptLatch.await(3_000, TimeUnit.MILLISECONDS)).isTrue();