Request Active Source when ARC is enabled in ArcInitiationActionFromAvr.

ag/5086550

This will trigger ARC input switch if users turn on System Audio Mode
when watching Active Source not under the current device.

Test: atest com.android.server.hdmi
Bug: 80296911
Change-Id: I3fbc336892131291ec3e67e5f75ff99f592e2f23
diff --git a/services/core/java/com/android/server/hdmi/ArcInitiationActionFromAvr.java b/services/core/java/com/android/server/hdmi/ArcInitiationActionFromAvr.java
index ed17de5..18d328d 100644
--- a/services/core/java/com/android/server/hdmi/ArcInitiationActionFromAvr.java
+++ b/services/core/java/com/android/server/hdmi/ArcInitiationActionFromAvr.java
@@ -21,14 +21,15 @@
  * Feature action that handles Audio Return Channel initiated by AVR devices.
  */
 public class ArcInitiationActionFromAvr extends HdmiCecFeatureAction {
-    // TODO(shubang): add tests
-
     // State in which waits for ARC response.
     private static final int STATE_WAITING_FOR_INITIATE_ARC_RESPONSE = 1;
     private static final int STATE_ARC_INITIATED = 2;
 
     // the required maximum response time specified in CEC 9.2
     private static final int TIMEOUT_MS = 1000;
+    private static final int MAX_RETRY_COUNT = 5;
+
+    private int mSendRequestActiveSourceRetryCount = 0;
 
     ArcInitiationActionFromAvr(HdmiCecLocalDevice source) {
         super(source);
@@ -56,7 +57,12 @@
                 return true;
             case Constants.MESSAGE_REPORT_ARC_INITIATED:
                 mState = STATE_ARC_INITIATED;
-                finish();
+                if (audioSystem().getActiveSource().physicalAddress != getSourcePath()
+                        && audioSystem().isSystemAudioActivated()) {
+                    sendRequestActiveSource();
+                } else {
+                    finish();
+                }
                 return true;
         }
         return false;
@@ -91,4 +97,19 @@
         finish();
     }
 
+    protected void sendRequestActiveSource() {
+        sendCommand(HdmiCecMessageBuilder.buildRequestActiveSource(getSourceAddress()),
+                result -> {
+                    if (result != SendMessageResult.SUCCESS) {
+                        if (mSendRequestActiveSourceRetryCount < MAX_RETRY_COUNT) {
+                            mSendRequestActiveSourceRetryCount++;
+                            sendRequestActiveSource();
+                        } else {
+                            finish();
+                        }
+                    } else {
+                        finish();
+                    }
+                });
+    }
 }