Loading android/app/src/com/android/bluetooth/a2dp/A2dpService.java +5 −2 Original line number Diff line number Diff line Loading @@ -331,12 +331,15 @@ public class A2dpService extends ProfileService { * The check considers a number of factors during the evaluation. * * @param device the peer device to connect to * @param isOutgoingRequest if true, the check is for outgoing connection * request, otherwise is for incoming connection request * @return true if connection is allowed, otherwise false */ @VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE) public boolean okToConnect(BluetoothDevice device) { public boolean okToConnect(BluetoothDevice device, boolean isOutgoingRequest) { Log.i(TAG, "okToConnect: device " + device + " isOutgoingRequest: " + isOutgoingRequest); // Check if this is an incoming connection in Quiet mode. if (mAdapterService.isQuietModeEnabled()) { if (mAdapterService.isQuietModeEnabled() && !isOutgoingRequest) { Log.e(TAG, "okToConnect: cannot connect to " + device + " : quiet mode enabled"); return false; } Loading android/app/src/com/android/bluetooth/a2dp/A2dpStateMachine.java +5 −5 Original line number Diff line number Diff line Loading @@ -185,7 +185,7 @@ final class A2dpStateMachine extends StateMachine { Log.e(TAG, "Disconnected: error connecting to " + mDevice); break; } if (mA2dpService.okToConnect(mDevice)) { if (mA2dpService.okToConnect(mDevice, true)) { transitionTo(mConnecting); } else { // Reject the request and stay in Disconnected state Loading Loading @@ -226,7 +226,7 @@ final class A2dpStateMachine extends StateMachine { Log.w(TAG, "Ignore A2DP DISCONNECTED event: " + mDevice); break; case A2dpStackEvent.CONNECTION_STATE_CONNECTING: if (mA2dpService.okToConnect(mDevice)) { if (mA2dpService.okToConnect(mDevice, false)) { Log.i(TAG, "Incoming A2DP Connecting request accepted: " + mDevice); transitionTo(mConnecting); } else { Loading @@ -237,7 +237,7 @@ final class A2dpStateMachine extends StateMachine { break; case A2dpStackEvent.CONNECTION_STATE_CONNECTED: Log.w(TAG, "A2DP Connected from Disconnected state: " + mDevice); if (mA2dpService.okToConnect(mDevice)) { if (mA2dpService.okToConnect(mDevice, false)) { Log.i(TAG, "Incoming A2DP Connected request accepted: " + mDevice); transitionTo(mConnected); } else { Loading Loading @@ -428,7 +428,7 @@ final class A2dpStateMachine extends StateMachine { transitionTo(mDisconnected); break; case A2dpStackEvent.CONNECTION_STATE_CONNECTED: if (mA2dpService.okToConnect(mDevice)) { if (mA2dpService.okToConnect(mDevice, false)) { Log.w(TAG, "Disconnecting interrupted: device is connected: " + mDevice); transitionTo(mConnected); } else { Loading @@ -438,7 +438,7 @@ final class A2dpStateMachine extends StateMachine { } break; case A2dpStackEvent.CONNECTION_STATE_CONNECTING: if (mA2dpService.okToConnect(mDevice)) { if (mA2dpService.okToConnect(mDevice, false)) { Log.i(TAG, "Disconnecting interrupted: try to reconnect: " + mDevice); transitionTo(mConnecting); } else { Loading android/app/tests/unit/src/com/android/bluetooth/a2dp/A2dpServiceTest.java +18 −7 Original line number Diff line number Diff line Loading @@ -90,6 +90,7 @@ public class A2dpServiceTest { TestUtils.setAdapterService(mAdapterService); doReturn(MAX_CONNECTED_AUDIO_DEVICES).when(mAdapterService).getMaxConnectedAudioDevices(); doReturn(false).when(mAdapterService).isQuietModeEnabled(); mAdapter = BluetoothAdapter.getDefaultAdapter(); Loading Loading @@ -851,7 +852,7 @@ public class A2dpServiceTest { } /** * Helper function to test okToConnect() method * Helper function to test okToConnect() method. * * @param device test device * @param bondState bond state value, could be invalid Loading @@ -862,7 +863,17 @@ public class A2dpServiceTest { boolean expected) { doReturn(bondState).when(mAdapterService).getBondState(device); Assert.assertTrue(mA2dpService.setPriority(device, priority)); Assert.assertEquals(expected, mA2dpService.okToConnect(device)); } // Test when the AdapterService is in non-quiet mode: the result should not depend // on whether the connection request is outgoing or incoming. doReturn(false).when(mAdapterService).isQuietModeEnabled(); Assert.assertEquals(expected, mA2dpService.okToConnect(device, true)); // Outgoing Assert.assertEquals(expected, mA2dpService.okToConnect(device, false)); // Incoming // Test when the AdapterService is in quiet mode: the result should always be // false when the connection request is incoming. doReturn(true).when(mAdapterService).isQuietModeEnabled(); Assert.assertEquals(expected, mA2dpService.okToConnect(device, true)); // Outgoing Assert.assertEquals(false, mA2dpService.okToConnect(device, false)); // Incoming } } android/app/tests/unit/src/com/android/bluetooth/a2dp/A2dpStateMachineTest.java +2 −1 Original line number Diff line number Diff line Loading @@ -107,7 +107,8 @@ public class A2dpStateMachineTest { * @param allow if true, connection is allowed */ private void allowConnection(boolean allow) { doReturn(allow).when(mA2dpService).okToConnect(any(BluetoothDevice.class)); doReturn(allow).when(mA2dpService).okToConnect(any(BluetoothDevice.class), anyBoolean()); } /** Loading Loading
android/app/src/com/android/bluetooth/a2dp/A2dpService.java +5 −2 Original line number Diff line number Diff line Loading @@ -331,12 +331,15 @@ public class A2dpService extends ProfileService { * The check considers a number of factors during the evaluation. * * @param device the peer device to connect to * @param isOutgoingRequest if true, the check is for outgoing connection * request, otherwise is for incoming connection request * @return true if connection is allowed, otherwise false */ @VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE) public boolean okToConnect(BluetoothDevice device) { public boolean okToConnect(BluetoothDevice device, boolean isOutgoingRequest) { Log.i(TAG, "okToConnect: device " + device + " isOutgoingRequest: " + isOutgoingRequest); // Check if this is an incoming connection in Quiet mode. if (mAdapterService.isQuietModeEnabled()) { if (mAdapterService.isQuietModeEnabled() && !isOutgoingRequest) { Log.e(TAG, "okToConnect: cannot connect to " + device + " : quiet mode enabled"); return false; } Loading
android/app/src/com/android/bluetooth/a2dp/A2dpStateMachine.java +5 −5 Original line number Diff line number Diff line Loading @@ -185,7 +185,7 @@ final class A2dpStateMachine extends StateMachine { Log.e(TAG, "Disconnected: error connecting to " + mDevice); break; } if (mA2dpService.okToConnect(mDevice)) { if (mA2dpService.okToConnect(mDevice, true)) { transitionTo(mConnecting); } else { // Reject the request and stay in Disconnected state Loading Loading @@ -226,7 +226,7 @@ final class A2dpStateMachine extends StateMachine { Log.w(TAG, "Ignore A2DP DISCONNECTED event: " + mDevice); break; case A2dpStackEvent.CONNECTION_STATE_CONNECTING: if (mA2dpService.okToConnect(mDevice)) { if (mA2dpService.okToConnect(mDevice, false)) { Log.i(TAG, "Incoming A2DP Connecting request accepted: " + mDevice); transitionTo(mConnecting); } else { Loading @@ -237,7 +237,7 @@ final class A2dpStateMachine extends StateMachine { break; case A2dpStackEvent.CONNECTION_STATE_CONNECTED: Log.w(TAG, "A2DP Connected from Disconnected state: " + mDevice); if (mA2dpService.okToConnect(mDevice)) { if (mA2dpService.okToConnect(mDevice, false)) { Log.i(TAG, "Incoming A2DP Connected request accepted: " + mDevice); transitionTo(mConnected); } else { Loading Loading @@ -428,7 +428,7 @@ final class A2dpStateMachine extends StateMachine { transitionTo(mDisconnected); break; case A2dpStackEvent.CONNECTION_STATE_CONNECTED: if (mA2dpService.okToConnect(mDevice)) { if (mA2dpService.okToConnect(mDevice, false)) { Log.w(TAG, "Disconnecting interrupted: device is connected: " + mDevice); transitionTo(mConnected); } else { Loading @@ -438,7 +438,7 @@ final class A2dpStateMachine extends StateMachine { } break; case A2dpStackEvent.CONNECTION_STATE_CONNECTING: if (mA2dpService.okToConnect(mDevice)) { if (mA2dpService.okToConnect(mDevice, false)) { Log.i(TAG, "Disconnecting interrupted: try to reconnect: " + mDevice); transitionTo(mConnecting); } else { Loading
android/app/tests/unit/src/com/android/bluetooth/a2dp/A2dpServiceTest.java +18 −7 Original line number Diff line number Diff line Loading @@ -90,6 +90,7 @@ public class A2dpServiceTest { TestUtils.setAdapterService(mAdapterService); doReturn(MAX_CONNECTED_AUDIO_DEVICES).when(mAdapterService).getMaxConnectedAudioDevices(); doReturn(false).when(mAdapterService).isQuietModeEnabled(); mAdapter = BluetoothAdapter.getDefaultAdapter(); Loading Loading @@ -851,7 +852,7 @@ public class A2dpServiceTest { } /** * Helper function to test okToConnect() method * Helper function to test okToConnect() method. * * @param device test device * @param bondState bond state value, could be invalid Loading @@ -862,7 +863,17 @@ public class A2dpServiceTest { boolean expected) { doReturn(bondState).when(mAdapterService).getBondState(device); Assert.assertTrue(mA2dpService.setPriority(device, priority)); Assert.assertEquals(expected, mA2dpService.okToConnect(device)); } // Test when the AdapterService is in non-quiet mode: the result should not depend // on whether the connection request is outgoing or incoming. doReturn(false).when(mAdapterService).isQuietModeEnabled(); Assert.assertEquals(expected, mA2dpService.okToConnect(device, true)); // Outgoing Assert.assertEquals(expected, mA2dpService.okToConnect(device, false)); // Incoming // Test when the AdapterService is in quiet mode: the result should always be // false when the connection request is incoming. doReturn(true).when(mAdapterService).isQuietModeEnabled(); Assert.assertEquals(expected, mA2dpService.okToConnect(device, true)); // Outgoing Assert.assertEquals(false, mA2dpService.okToConnect(device, false)); // Incoming } }
android/app/tests/unit/src/com/android/bluetooth/a2dp/A2dpStateMachineTest.java +2 −1 Original line number Diff line number Diff line Loading @@ -107,7 +107,8 @@ public class A2dpStateMachineTest { * @param allow if true, connection is allowed */ private void allowConnection(boolean allow) { doReturn(allow).when(mA2dpService).okToConnect(any(BluetoothDevice.class)); doReturn(allow).when(mA2dpService).okToConnect(any(BluetoothDevice.class), anyBoolean()); } /** Loading