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

Commit f110b2fe authored by Smriti Gupta's avatar Smriti Gupta Committed by Gerrit - the friendly Code Review server
Browse files

SAP: Fix following issues in SAP Code

- Remove redundant code calling in intiSocket() to call
  closeServerSocket() as it will anyways be called from
  closeService(). Redundant code can lead to ANR's when
  starting/stopping service or BT on/off is done
  various times.
- Add check to prevent unregistering receiver when it
  is not registered.
- Add null checks to prevent pointer dereference.

CRs-Fixed: 952309
Change-Id: Ie364062243dc1902d49c4a858b6c11336789e8cc
parent 977ff92f
Loading
Loading
Loading
Loading
+37 −26
Original line number Diff line number Diff line
@@ -95,6 +95,7 @@ public class SapService extends ProfileService {
    private boolean mRemoveTimeoutMsg = false;

    private boolean mIsWaitingAuthorization = false;
    private boolean mIsRegistered = false;

    private SapServiceMessageHandler mSessionStatusHandler = null;

@@ -181,11 +182,6 @@ public class SapService extends ProfileService {
                break;
            }
        }
        if (mInterrupted) {
            initSocketOK = false;
            // close server socket to avoid resource leakage
            closeServerSocket();
        }

        if (initSocketOK) {
            if (VERBOSE) Log.v(TAG, "Succeed to create listening socket ");
@@ -201,6 +197,7 @@ public class SapService extends ProfileService {
        if (mServerSocket != null) {
            try {
                // this will cause mServerSocket.accept() return early with IOException
                if (VERBOSE) Log.v(TAG, "Closing server socket");
                mServerSocket.close();
                mServerSocket = null;
            } catch (IOException ex) {
@@ -212,6 +209,7 @@ public class SapService extends ProfileService {
    private final synchronized void closeConnectionSocket() {
        if (mConnSocket != null) {
            try {
                if (VERBOSE) Log.v(TAG, "Closing conenction socket");
                mConnSocket.close();
                mConnSocket = null;
            } catch (IOException e) {
@@ -235,7 +233,7 @@ public class SapService extends ProfileService {
                mAcceptThread.shutdown();
                mAcceptThread.join();
            } catch (InterruptedException ex) {
                Log.w(TAG, "mAcceptThread close error", ex);
                Log.e(TAG, "mAcceptThread close error", ex);
            }
            mAcceptThread = null;
        }
@@ -248,7 +246,7 @@ public class SapService extends ProfileService {
    }

    synchronized private void releaseWakeLockResources() {
        if (mWakeLock != null) {
        if ((mWakeLock != null) && (mSessionStatusHandler != null)) {
            mSessionStatusHandler.removeMessages(MSG_ACQUIRE_WAKE_LOCK);
            mSessionStatusHandler.removeMessages(MSG_RELEASE_WAKE_LOCK);
            mWakeLock.release();
@@ -261,6 +259,7 @@ public class SapService extends ProfileService {
        // E.g. we cannot have multiple receivers registered nor have multiple servers attached
        // to the RIL socket.
        if(mSapServer != null) {
            if (DEBUG) Log.d(TAG, "Awaiting SapServerSessionStop");
            boolean retry = true;
            while(retry == true) {
                retry = false;
@@ -351,14 +350,14 @@ public class SapService extends ProfileService {
                    if (VERBOSE) Log.v(TAG, "Accepting socket connection...");
                    serverSocket = mServerSocket;
                    if (serverSocket == null) {
                        Log.w(TAG, "mServerSocket is null");
                        Log.e(TAG, "mServerSocket is null");
                        break;
                    }
                    mConnSocket = mServerSocket.accept();
                    if (VERBOSE) Log.v(TAG, "Accepted socket connection...");
                    if (DEBUG) Log.d(TAG, "Accepted socket connection...");
                    synchronized (SapService.this) {
                        if (mConnSocket == null) {
                            Log.w(TAG, "mConnSocket is null");
                            Log.e(TAG, "mConnSocket is null");
                            break;
                        }
                        mRemoteDevice = mConnSocket.getRemoteDevice();
@@ -375,7 +374,7 @@ public class SapService extends ProfileService {
                    }
                    int permission = mRemoteDevice.getSimAccessPermission();

                    if (VERBOSE) Log.v(TAG, "getSimAccessPermission() = " + permission);
                    if (DEBUG) Log.d(TAG, "getSimAccessPermission() = " + permission);

                    if (permission == BluetoothDevice.ACCESS_ALLOWED) {
                        try {
@@ -408,7 +407,7 @@ public class SapService extends ProfileService {
                    stopped = true; // job done ,close this thread;
                } catch (IOException ex) {
                    stopped=true;
                    if (VERBOSE) Log.v(TAG, "Accept exception: ", ex);
                    if (DEBUG) Log.d(TAG, "Accept exception: ", ex);
                }
            }
        }
@@ -625,6 +624,7 @@ public class SapService extends ProfileService {

        try {
            registerReceiver(mSapReceiver, filter);
            mIsRegistered = true;
        } catch (Exception e) {
            Log.w(TAG,"Unable to register sap receiver",e);
        }
@@ -643,7 +643,13 @@ public class SapService extends ProfileService {
    @Override
    protected boolean stop() {
        if (VERBOSE) Log.v(TAG, "Stoping SAPService");

        if (!mIsRegistered){
            Log.i(TAG, "Avoid unregister when receiver it is not registered");
            return true;
        }
        try {
            mIsRegistered = false;
            unregisterReceiver(mSapReceiver);
        } catch (Exception e) {
            Log.w(TAG,"Unable to unregister sap receiver",e);
@@ -723,19 +729,22 @@ public class SapService extends ProfileService {
            mIsWaitingAuthorization = false;
            cancelUserTimeoutAlarm();
        }
        if (mSessionStatusHandler != null) {
            mSessionStatusHandler.removeCallbacksAndMessages(null);
            // Request release of all resources
            Message msg = mSessionStatusHandler.obtainMessage(SHUTDOWN,latch);
            if( mSessionStatusHandler.sendMessage(msg) == false) {
                /* most likely caused by shutdown being called from multiple sources - e.g.BT off
                 * signaled through intent and a service shutdown simultaneously.
             * Intended behavior not documented, hence we need to be able to handle all cases. */
                 * Intended behavior not documented, hence we need to be able to handle all cases.
                 */
                Log.e(TAG, "mSessionStatusHandler.sendMessage() failed trigger latch locally");
                if(latch != null) {
                    latch.countDown();
                }
            } else {
            if(DEBUG) Log.e(TAG, "mSessionStatusHandler.sendMessage() dispatched shutdown message");
                if(DEBUG) Log.e(TAG, "mSessionStatusHandler.sendMessage() dispatched shutdown msg");
            }
        }
    }

@@ -764,6 +773,7 @@ public class SapService extends ProfileService {
                } else if (state == BluetoothAdapter.STATE_ON) {
                    if (DEBUG) Log.d(TAG, "STATE_ON");
                    // start RFCOMM listener
                    if (mSessionStatusHandler != null)
                        mSessionStatusHandler.sendMessage(mSessionStatusHandler
                                  .obtainMessage(START_LISTENER));
                }
@@ -806,6 +816,7 @@ public class SapService extends ProfileService {
                        }
                    }
                    // Ensure proper cleanup, and prepare for new connect.
                    if (mSessionStatusHandler != null)
                        mSessionStatusHandler.sendEmptyMessage(MSG_SERVERSESSION_CLOSE);
                }
            } else if (action.equals(USER_CONFIRM_TIMEOUT_ACTION)){