Add condition to check case of accessory combinations

Before this CL, we only compare whether the value of function is
equal to accessory to ensure usb is in accessory mode. But in some
cases there are different accessory combinations, like
"accessory,audio source". It will make the condition return false
when encountering accessory combinations.

This CL will check whether the function will include accessory mode
to fix this issue.

Bug: 162451162
Test: make -j42 RunSettingsRoboTests
Change-Id: I7c80f02de5340799e292949608e19b86b187b982
Merged-In: I7c80f02de5340799e292949608e19b86b187b982
(cherry picked from commit 7d71081d45be8346c3ce342422644f6c7c86f398)
diff --git a/src/com/android/settings/connecteddevice/usb/UsbDetailsFunctionsController.java b/src/com/android/settings/connecteddevice/usb/UsbDetailsFunctionsController.java
index b91bb79..99e9d50 100644
--- a/src/com/android/settings/connecteddevice/usb/UsbDetailsFunctionsController.java
+++ b/src/com/android/settings/connecteddevice/usb/UsbDetailsFunctionsController.java
@@ -22,6 +22,7 @@
 import android.content.Context;
 import android.hardware.usb.UsbManager;
 import android.net.ConnectivityManager;
+import android.util.Log;
 
 import androidx.annotation.VisibleForTesting;
 import androidx.preference.PreferenceCategory;
@@ -40,6 +41,9 @@
 public class UsbDetailsFunctionsController extends UsbDetailsController
         implements RadioButtonPreference.OnClickListener {
 
+    private static final String TAG = "UsbFunctionsCtrl";
+    private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
+
     static final Map<Long, Integer> FUNCTIONS_MAP = new LinkedHashMap<>();
 
     static {
@@ -88,6 +92,10 @@
 
     @Override
     protected void refresh(boolean connected, long functions, int powerRole, int dataRole) {
+        if (DEBUG) {
+            Log.d(TAG, "refresh() connected : " + connected + ", functions : " + functions
+                    + ", powerRole : " + powerRole + ", dataRole : " + dataRole);
+        }
         if (!connected || dataRole != DATA_ROLE_DEVICE) {
             mProfilesContainer.setEnabled(false);
         } else {
@@ -100,7 +108,7 @@
             pref = getProfilePreference(UsbBackend.usbFunctionsToString(option), title);
             // Only show supported options
             if (mUsbBackend.areFunctionsSupported(option)) {
-                if (functions == UsbManager.FUNCTION_ACCESSORY) {
+                if (isAccessoryMode(functions)) {
                     pref.setChecked(UsbManager.FUNCTION_MTP == option);
                 } else {
                     pref.setChecked(functions == option);
@@ -115,6 +123,12 @@
     public void onRadioButtonClicked(RadioButtonPreference preference) {
         final long function = UsbBackend.usbFunctionsFromString(preference.getKey());
         final long previousFunction = mUsbBackend.getCurrentFunctions();
+        if (DEBUG) {
+            Log.d(TAG, "onRadioButtonClicked() function : " + function + ", toString() : "
+                    + UsbManager.usbFunctionsToString(function) + ", previousFunction : "
+                    + previousFunction + ", toString() : "
+                    + UsbManager.usbFunctionsToString(previousFunction));
+        }
         if (function != previousFunction && !Utils.isMonkeyRunning()
                 && !shouldIgnoreClickEvent(function, previousFunction)) {
             mPreviousFunction = previousFunction;
@@ -140,8 +154,11 @@
     }
 
     private boolean shouldIgnoreClickEvent(long function, long previousFunction) {
-        return previousFunction == UsbManager.FUNCTION_ACCESSORY
-                && function == UsbManager.FUNCTION_MTP;
+        return isAccessoryMode(previousFunction) && function == UsbManager.FUNCTION_MTP;
+    }
+
+    private boolean isAccessoryMode(long function) {
+        return (function & UsbManager.FUNCTION_ACCESSORY) != 0;
     }
 
     @Override
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 9e31ca1..3ea2756 100644
--- a/tests/robotests/src/com/android/settings/connecteddevice/usb/UsbDetailsFunctionsControllerTest.java
+++ b/tests/robotests/src/com/android/settings/connecteddevice/usb/UsbDetailsFunctionsControllerTest.java
@@ -276,6 +276,18 @@
     }
 
     @Test
+    public void onRadioButtonClicked_functionMtp_inAccessoryCombinationsMode_doNothing() {
+        final long function = UsbManager.FUNCTION_ACCESSORY | UsbManager.FUNCTION_AUDIO_SOURCE;
+        mRadioButtonPreference.setKey(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_MTP));
+        doReturn(UsbManager.FUNCTION_ACCESSORY).when(mUsbBackend).getCurrentFunctions();
+
+        mDetailsFunctionsController.mPreviousFunction = function;
+        mDetailsFunctionsController.onRadioButtonClicked(mRadioButtonPreference);
+
+        assertThat(mDetailsFunctionsController.mPreviousFunction).isEqualTo(function);
+    }
+
+    @Test
     public void onRadioButtonClicked_clickSameButton_doNothing() {
         mRadioButtonPreference.setKey(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_PTP));
         doReturn(UsbManager.FUNCTION_PTP).when(mUsbBackend).getCurrentFunctions();