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

Commit 63805cc6 authored by Danny Baumann's avatar Danny Baumann Committed by Gerrit Code Review
Browse files

Merge "Fix MAP accept threads sometimes spinning endlessly." into cm-10.1

parents c66689ae bc965770
Loading
Loading
Loading
Loading
+39 −40
Original line number Diff line number Diff line
@@ -560,7 +560,7 @@ public class BluetoothMasService extends Service {
            for (BluetoothMasObexConnection connection : mConnections) {
                // Stop the possible trying to init serverSocket
                connection.mInterrupted = true;
                connection.closeConnection();
                connection.closeConnection(true);
            }
        }

@@ -645,12 +645,6 @@ public class BluetoothMasService extends Service {
            if (VERBOSE)
                Log.v(TAG, "Map Service startRfcommSocketListener");

            if (mServerSocket == null) {
                if (!initSocket()) {
                    closeService();
                    return;
                }
            }
            if (mAcceptThread == null) {
                mAcceptThread = new SocketAcceptThread(mnsClient, mMasId);
                mAcceptThread.setName("BluetoothMapAcceptThread " + mPortNum);
@@ -701,14 +695,24 @@ public class BluetoothMasService extends Service {
            return initSocketOK;
        }

        private final void closeSocket() throws IOException {
            if (mConnSocket != null) {
        private final void closeSocket(boolean server, boolean accept) throws IOException {
            if (server) {
                // Stop the possible trying to init serverSocket
                mInterrupted = true;

                if (mServerSocket != null) {
                    mServerSocket.close();
                    mServerSocket = null;
                }
            }

            if (accept && mConnSocket != null) {
                mConnSocket.close();
                mConnSocket = null;
            }
        }

        public void closeConnection() {
        public void closeConnection(boolean closeServer) {
            if (VERBOSE) Log.v(TAG, "Mas connection closing");

            // Release the wake lock if obex transaction is over
@@ -716,16 +720,27 @@ public class BluetoothMasService extends Service {
                if (mWakeLock.isHeld()) {
                    if (VERBOSE) Log.v(TAG,"Release full wake lock");
                    mWakeLock.release();
                }
                mWakeLock = null;
                } else {
                    mWakeLock = null;
            }

            if (mServerSession != null) {
                mServerSession.close();
                mServerSession = null;
            }

            try {
                closeSocket(closeServer, true);
            } catch (IOException ex) {
                Log.e(TAG, "CloseSocket error: " + ex);
            }

            if (mAcceptThread != null) {
                try {
                    if (closeServer) {
                        mAcceptThread.shutdown();
                        mAcceptThread.join();
                    }
                } catch (InterruptedException ex) {
                    Log.w(TAG, "mAcceptThread  close error" + ex);
                } finally {
@@ -733,16 +748,6 @@ public class BluetoothMasService extends Service {
                }
            }

            if (mServerSession != null) {
                mServerSession.close();
                mServerSession = null;
            }

            try {
                closeSocket();
            } catch (IOException ex) {
                Log.e(TAG, "CloseSocket error: " + ex);
            }
            if (VERBOSE) Log.v(TAG, "Mas connection closed");
        }

@@ -795,7 +800,7 @@ public class BluetoothMasService extends Service {
        private void stopObexServerSession() {
            if (VERBOSE) Log.v(TAG, "Map Service stopObexServerSession ");

            closeConnection();
            closeConnection(false);

            // Last obex transaction is finished, we start to listen for incoming
            // connection again
@@ -822,11 +827,15 @@ public class BluetoothMasService extends Service {

            @Override
            public void run() {
                while (!stopped) {
                    try {
                if (mServerSocket == null) {
                            break;
                    if (!initSocket()) {
                        closeService();
                        return;
                    }
                }

                while (!stopped) {
                    try {
                        BluetoothSocket connSocket = mServerSocket.accept();

                        BluetoothDevice device = connSocket.getRemoteDevice();
@@ -875,9 +884,7 @@ public class BluetoothMasService extends Service {
                        }
                        stopped = true; // job done ,close this thread;
                    } catch (IOException ex) {
                        if (stopped) {
                            break;
                        }
                        stopped = true;
                        if (VERBOSE)
                            Log.v(TAG, "Accept exception: " + ex.toString());
                    }
@@ -888,14 +895,6 @@ public class BluetoothMasService extends Service {
                if (VERBOSE) Log.v(TAG, "AcceptThread shutdown for MAS id: " + mMasId);
                stopped = true;
                interrupt();
                if (mServerSocket != null) {
                    try {
                        mServerSocket.close();
                        mServerSocket = null;
                    } catch (IOException e) {
                        Log.e(TAG, "Failed to close socket", e);
                    }
                }
            }
        }
    }