HDMICEC: Add utility to invoke OTP on device

Test: atest HdmiCecOneTouchPlayTest#cect_11_2_1_1_OneTouchPlay
Bug: 153300312
Change-Id: I80f07d7c6520adee995d2d66868628f24b6e9978
Merged-In: I80f07d7c6520adee995d2d66868628f24b6e9978
diff --git a/hostsidetests/hdmicec/app/Android.bp b/hostsidetests/hdmicec/app/Android.bp
index ed41e1a..2ac24a3 100644
--- a/hostsidetests/hdmicec/app/Android.bp
+++ b/hostsidetests/hdmicec/app/Android.bp
@@ -15,6 +15,8 @@
 android_test_helper_app {
     name: "HdmiCecHelperApp",
     defaults: ["cts_defaults"],
+    platform_apis: true,
+    certificate: "platform",
     srcs: ["src/**/*.java"],
     static_libs: [
         "services.core",
diff --git a/hostsidetests/hdmicec/app/AndroidManifest.xml b/hostsidetests/hdmicec/app/AndroidManifest.xml
index 9be9136..e21fb53 100644
--- a/hostsidetests/hdmicec/app/AndroidManifest.xml
+++ b/hostsidetests/hdmicec/app/AndroidManifest.xml
@@ -19,6 +19,7 @@
     package="android.hdmicec.app">
     <uses-feature android:name="android.software.leanback"
         android:required="false" />
+    <uses-permission android:name="android.permission.HDMI_CEC" />
     <application >
         <activity android:name=".HdmiCecKeyEventCapture" >
             <intent-filter>
@@ -36,6 +37,11 @@
                 <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
             </intent-filter>
         </activity>
+        <activity android:name=".HdmiControlManagerHelper" >
+            <intent-filter>
+                <action android:name="android.hdmicec.app.OTP" />
+            </intent-filter>
+        </activity>
     </application>
 
     <uses-sdk android:minSdkVersion="28"   android:targetSdkVersion="28" />
diff --git a/hostsidetests/hdmicec/app/src/android/hdmicec/app/HdmiControlManagerHelper.java b/hostsidetests/hdmicec/app/src/android/hdmicec/app/HdmiControlManagerHelper.java
new file mode 100755
index 0000000..1276df1
--- /dev/null
+++ b/hostsidetests/hdmicec/app/src/android/hdmicec/app/HdmiControlManagerHelper.java
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.hdmicec.app;
+
+import android.app.Activity;
+import android.hardware.hdmi.HdmiControlManager;
+import android.hardware.hdmi.HdmiPlaybackClient;
+import android.os.Bundle;
+import android.util.Log;
+
+/**
+ * A simple activity that can be used to trigger actions using the HdmiControlManager.
+ * The action supported is:
+ *
+ * 1. android.hdmicec.app.OTP: Triggers the OTP
+ *    Usage: START_COMMAND -a android.hdmicec.app.OTP
+ *
+ * where START_COMMAND is
+ * adb shell am start -n "android.hdmicec.app/android.hdmicec.app.HdmiCecControlManagerHelper"
+ */
+public class HdmiControlManagerHelper extends Activity {
+
+    private static final String TAG = HdmiControlManagerHelper.class.getSimpleName();
+
+    @Override
+    public void onCreate(Bundle icicle) {
+        super.onCreate(icicle);
+
+        HdmiControlManager hdmiControlManager = getSystemService(HdmiControlManager.class);
+        if (hdmiControlManager == null) {
+            Log.i(TAG, "Failed to get HdmiControlManager");
+            return;
+        }
+
+        switch (getIntent().getAction()) {
+            case "android.hdmicec.app.OTP":
+                initiateOtp(hdmiControlManager);
+                break;
+            default:
+                Log.w(TAG, "Unknown intent!");
+        }
+    }
+
+    private void initiateOtp(HdmiControlManager hdmiControlManager) {
+        HdmiPlaybackClient client = hdmiControlManager.getPlaybackClient();
+        if (client == null) {
+            Log.i(TAG, "Failed to get HdmiPlaybackClient");
+            return;
+        }
+
+        client.oneTouchPlay((result) -> {
+            if (result == HdmiControlManager.RESULT_SUCCESS) {
+                Log.i(TAG, "OTP successful");
+            } else {
+                Log.i(TAG, "OTP failed");
+            }
+        });
+    }
+}
diff --git a/hostsidetests/hdmicec/src/android/hdmicec/cts/playback/HdmiCecOneTouchPlayTest.java b/hostsidetests/hdmicec/src/android/hdmicec/cts/playback/HdmiCecOneTouchPlayTest.java
index 56a7df2..ecd0ea8 100644
--- a/hostsidetests/hdmicec/src/android/hdmicec/cts/playback/HdmiCecOneTouchPlayTest.java
+++ b/hostsidetests/hdmicec/src/android/hdmicec/cts/playback/HdmiCecOneTouchPlayTest.java
@@ -16,29 +16,43 @@
 
 package android.hdmicec.cts.playback;
 
-import static com.google.common.truth.Truth.assertThat;
-
 import android.hdmicec.cts.CecMessage;
 import android.hdmicec.cts.CecOperand;
 import android.hdmicec.cts.HdmiCecClientWrapper;
 import android.hdmicec.cts.LogicalAddress;
-import android.hdmicec.cts.RequiredPropertyRule;
 import android.hdmicec.cts.RequiredFeatureRule;
+import android.hdmicec.cts.RequiredPropertyRule;
 
 import com.android.tradefed.device.ITestDevice;
 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
 
 import org.junit.Rule;
+import org.junit.Test;
 import org.junit.rules.RuleChain;
 import org.junit.runner.RunWith;
-import org.junit.Test;
+
+import static com.google.common.truth.Truth.assertThat;
 
 /** HDMI CEC tests for One Touch Play (Section 11.2.1) */
 @RunWith(DeviceJUnit4ClassRunner.class)
 public final class HdmiCecOneTouchPlayTest extends BaseHostJUnit4Test {
 
     private static final int PHYSICAL_ADDRESS = 0x1000;
+    /**
+     * The package name of the APK.
+     */
+    private static final String HDMI_CEC_HELPER_PACKAGE = "android.hdmicec.app";
+    /**
+     * The class name of the main activity in the APK.
+     */
+    private static final String HDMI_CONTROL_HELPER_CLASS = "HdmiControlManagerHelper";
+    /**
+     * Intent to trigger an OTP.
+     */
+    private static final String OTP_ACTION = String.format(
+        "android.hdmicec.app.OTP -n %s/%s.%s", HDMI_CEC_HELPER_PACKAGE, HDMI_CEC_HELPER_PACKAGE,
+        HDMI_CONTROL_HELPER_CLASS);
 
     /** Intent to launch the remote pairing activity */
     private static final String ACTION_CONNECT_INPUT_NORMAL =
@@ -76,7 +90,7 @@
     public void cect_11_2_1_1_OneTouchPlay() throws Exception {
         ITestDevice device = getDevice();
         device.reboot();
-        device.executeShellCommand("input keyevent KEYCODE_HOME");
+        sendOtp(device);
         hdmiCecClient.checkExpectedOutput(LogicalAddress.TV, CecOperand.TEXT_VIEW_ON);
         String message = hdmiCecClient.checkExpectedOutput(CecOperand.ACTIVE_SOURCE);
         assertThat(CecMessage.getParams(message)).isEqualTo(PHYSICAL_ADDRESS);
@@ -96,4 +110,13 @@
         assertThat(CecMessage.getParams(message)).isEqualTo(PHYSICAL_ADDRESS);
         device.executeShellCommand(FORCE_STOP_COMMAND + SETTINGS_PACKAGE);
     }
+
+    private void sendOtp(ITestDevice device) throws Exception {
+        // Clear activity
+        device.executeShellCommand(FORCE_STOP_COMMAND + HDMI_CEC_HELPER_PACKAGE);
+        // Clear logcat.
+        device.executeAdbCommand("logcat", "-c");
+        // Start the APK and wait for it to complete.
+        device.executeShellCommand(START_COMMAND + OTP_ACTION);
+    }
 }