Do nothing when choosing file transfer when in accessory mode

- Before this CL, the device will be disconnected and reconnected
  to accessory mode when choosing "File transfer/Android Auto" in
  accessory mode. Because the USB menu didn't check state of
  function, it should do nothing when choosing
  "File transfer/Android Auto" in accessory mode.

  This CL add condition to check state of function, it will do
  nothing when choosing "File transfer/Android Auto" in
  accessory mode.

Bug: 162451162
Test: make -j42 RunSettingsRoboTests
Change-Id: I1749c6c43d2a192e4ce1bf1ae5343ff8deafbe48
Merged-In: I1749c6c43d2a192e4ce1bf1ae5343ff8deafbe48
(cherry picked from commit 3251a04ba3bb21bf5309bfd4431250ad567eca94)
diff --git a/src/com/android/settings/connecteddevice/usb/UsbDetailsFunctionsController.java b/src/com/android/settings/connecteddevice/usb/UsbDetailsFunctionsController.java
index ded1294..b91bb79 100644
--- a/src/com/android/settings/connecteddevice/usb/UsbDetailsFunctionsController.java
+++ b/src/com/android/settings/connecteddevice/usb/UsbDetailsFunctionsController.java
@@ -115,7 +115,8 @@
     public void onRadioButtonClicked(RadioButtonPreference preference) {
         final long function = UsbBackend.usbFunctionsFromString(preference.getKey());
         final long previousFunction = mUsbBackend.getCurrentFunctions();
-        if (function != previousFunction && !Utils.isMonkeyRunning()) {
+        if (function != previousFunction && !Utils.isMonkeyRunning()
+                && !shouldIgnoreClickEvent(function, previousFunction)) {
             mPreviousFunction = previousFunction;
 
             //Update the UI in advance to make it looks smooth
@@ -138,6 +139,11 @@
         }
     }
 
+    private boolean shouldIgnoreClickEvent(long function, long previousFunction) {
+        return previousFunction == UsbManager.FUNCTION_ACCESSORY
+                && function == UsbManager.FUNCTION_MTP;
+    }
+
     @Override
     public boolean isAvailable() {
         return !Utils.isMonkeyRunning();
diff --git a/tests/robotests/src/com/android/settings/connecteddevice/usb/UsbDetailsFunctionsControllerTest.java b/tests/robotests/src/com/android/settings/connecteddevice/usb/UsbDetailsFunctionsControllerTest.java
index 149df6e..9e31ca1 100644
--- a/tests/robotests/src/com/android/settings/connecteddevice/usb/UsbDetailsFunctionsControllerTest.java
+++ b/tests/robotests/src/com/android/settings/connecteddevice/usb/UsbDetailsFunctionsControllerTest.java
@@ -264,6 +264,18 @@
     }
 
     @Test
+    public void onRadioButtonClicked_functionMtp_inAccessoryMode_doNothing() {
+        mRadioButtonPreference.setKey(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_MTP));
+        doReturn(UsbManager.FUNCTION_ACCESSORY).when(mUsbBackend).getCurrentFunctions();
+
+        mDetailsFunctionsController.mPreviousFunction = UsbManager.FUNCTION_ACCESSORY;
+        mDetailsFunctionsController.onRadioButtonClicked(mRadioButtonPreference);
+
+        assertThat(mDetailsFunctionsController.mPreviousFunction).isEqualTo(
+                UsbManager.FUNCTION_ACCESSORY);
+    }
+
+    @Test
     public void onRadioButtonClicked_clickSameButton_doNothing() {
         mRadioButtonPreference.setKey(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_PTP));
         doReturn(UsbManager.FUNCTION_PTP).when(mUsbBackend).getCurrentFunctions();