Add "Enable Phone Account" CTS Verifier Test.

This CTS Verifier test ensures that a third-party ConnectionService can
register a phone account, and that the user is able to use the device's
"calling accounts" screen to enable the newly registered phone account.

Test: Manual / CTS Verifier
Bug: 36098357
Change-Id: I3a8a6465734c18487195c9cc13cf7aa2aa56f3ef
diff --git a/apps/CtsVerifier/AndroidManifest.xml b/apps/CtsVerifier/AndroidManifest.xml
index b1d1afb..cbd46d5 100644
--- a/apps/CtsVerifier/AndroidManifest.xml
+++ b/apps/CtsVerifier/AndroidManifest.xml
@@ -2981,6 +2981,28 @@
             </intent-filter>
         </activity>
 
+        <activity
+            android:name=".telecom.EnablePhoneAccountTestActivity"
+            android:label="@string/telecom_enable_phone_account_test">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN"/>
+                <category android:name="android.cts.intent.category.MANUAL_TEST"/>
+            </intent-filter>
+
+            <meta-data
+                android:name="test_category"
+                android:value="@string/test_category_telecom"/>
+            <meta-data
+                android:name="test_required_features"
+                android:value="android.hardware.telephony"/>
+        </activity>
+
+        <service android:name="com.android.cts.verifier.telecom.CtsConnectionService"
+            android:permission="android.permission.BIND_TELECOM_CONNECTION_SERVICE" >
+            <intent-filter>
+                <action android:name="android.telecom.ConnectionService" />
+            </intent-filter>
+        </service>
     </application>
 
 </manifest>
diff --git a/apps/CtsVerifier/res/layout/telecom_enable_phone_account.xml b/apps/CtsVerifier/res/layout/telecom_enable_phone_account.xml
new file mode 100644
index 0000000..981fbf5
--- /dev/null
+++ b/apps/CtsVerifier/res/layout/telecom_enable_phone_account.xml
@@ -0,0 +1,98 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2017 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
+  -->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:orientation="vertical" android:layout_width="match_parent"
+    android:layout_height="match_parent">
+
+    <TextView
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:text="@string/telecom_enable_phone_account_info"/>
+
+    <RelativeLayout
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_marginTop="@dimen/js_padding"
+        android:layout_marginBottom="@dimen/js_padding">
+
+        <ImageView
+            android:id="@+id/step_1_status"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:src="@drawable/fs_indeterminate"
+            android:layout_marginRight="@dimen/js_padding"
+            android:layout_alignParentStart="true"
+            android:layout_alignParentTop="true" />
+        <TextView
+            android:id="@+id/step_1_instructions"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:text="@string/telecom_enable_phone_account_step_1"
+            android:textSize="16dp"
+            android:layout_alignParentRight="true"
+            android:layout_alignParentTop="true"
+            android:layout_toRightOf="@id/step_1_status" />
+        <Button
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_alignParentRight="true"
+            android:layout_below="@id/step_1_instructions"
+            android:layout_marginLeft="20dip"
+            android:layout_marginRight="20dip"
+            android:layout_toRightOf="@id/step_1_status"
+            android:id="@+id/telecom_enable_phone_account_register_button"
+            android:text="@string/telecom_enable_phone_account_register_button"/>
+    </RelativeLayout>
+
+    <RelativeLayout
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_marginTop="@dimen/js_padding"
+        android:layout_marginBottom="@dimen/js_padding">
+
+        <ImageView
+            android:id="@+id/step_2_status"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:src="@drawable/fs_indeterminate"
+            android:layout_marginRight="@dimen/js_padding"
+            android:layout_alignParentStart="true"
+            android:layout_alignParentTop="true" />
+        <TextView
+            android:id="@+id/step_2_instructions"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:text="@string/telecom_enable_phone_account_step_2"
+            android:textSize="16dp"
+            android:layout_alignParentRight="true"
+            android:layout_alignParentTop="true"
+            android:layout_toRightOf="@id/step_2_status" />
+        <Button
+            android:id="@+id/telecom_enable_phone_account_confirm_button"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_alignParentRight="true"
+            android:layout_below="@id/step_2_instructions"
+            android:layout_marginLeft="20dip"
+            android:layout_marginRight="20dip"
+            android:layout_toRightOf="@id/step_2_status"
+            android:text="@string/telecom_enable_phone_account_confirm_button"/>
+    </RelativeLayout>
+
+    <include layout="@layout/pass_fail_buttons" />
+</LinearLayout>
\ No newline at end of file
diff --git a/apps/CtsVerifier/res/values/strings.xml b/apps/CtsVerifier/res/values/strings.xml
index b790feb..5157a5a 100755
--- a/apps/CtsVerifier/res/values/strings.xml
+++ b/apps/CtsVerifier/res/values/strings.xml
@@ -38,6 +38,7 @@
     <string name="test_category_features">Features</string>
     <string name="test_category_deskclock">Clock</string>
     <string name="test_category_jobscheduler">Job Scheduler</string>
+    <string name="test_category_telecom">Telecom</string>
     <string name="test_category_telephony">Telephony</string>
     <string name="test_category_tv">TV</string>
     <string name="test_category_other">Other</string>
@@ -3764,4 +3765,20 @@
         This test requires that you have connected a mandated USB Audio headset. Press each
         \"transport\" button and verify that it is recognized by the test.
        </string>
+
+    <!-- Telecom tests -->
+    <string name="telecom_enable_phone_account_test"> Telecom Enable Phone Account Test</string>
+    <string name="telecom_enable_phone_account_info">
+        This test verifies that a third party ConnectionService can be enabled by the user.
+    </string>
+    <string name="telecom_enable_phone_account_step_1">
+        Click the button below to register a test PhoneAccount and ConnectionService.
+    </string>
+    <string name="telecom_enable_phone_account_register_button">Register Phone Account</string>
+    <string name="telecom_enable_phone_account_step_2">
+        In the Calling accounts settings, choose "All calling accounts" and enable the "CTS Verifier Test" account.
+        On AOSP this is accessible from: Phone app > Settings > Calls > Calling Accounts.
+        Once you have completed this step, return here and click the Confirm button.
+    </string>
+    <string name="telecom_enable_phone_account_confirm_button">Confirm</string>
 </resources>
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/telecom/CtsConnectionService.java b/apps/CtsVerifier/src/com/android/cts/verifier/telecom/CtsConnectionService.java
new file mode 100644
index 0000000..5ff3138
--- /dev/null
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/telecom/CtsConnectionService.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2017 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 com.android.cts.verifier.telecom;
+
+import android.telecom.ConnectionService;
+
+/**
+ * CTS Verifier ConnectionService implementation.
+ */
+public class CtsConnectionService extends ConnectionService {
+
+
+}
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/telecom/EnablePhoneAccountTestActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/telecom/EnablePhoneAccountTestActivity.java
new file mode 100644
index 0000000..2b7201a
--- /dev/null
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/telecom/EnablePhoneAccountTestActivity.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2017 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 com.android.cts.verifier.telecom;
+
+import android.os.Bundle;
+import android.telecom.PhoneAccount;
+import android.view.View;
+import android.widget.Button;
+import android.widget.ImageView;
+
+import com.android.cts.verifier.PassFailButtons;
+import com.android.cts.verifier.R;
+
+/**
+ * Tests that a new {@link android.telecom.ConnectionService} be added and its associated
+ * {@link android.telecom.PhoneAccount} enabled using the calling accounts settings screen.
+ */
+public class EnablePhoneAccountTestActivity extends PassFailButtons.Activity {
+
+    private Button mRegisterPhoneAccount;
+    private Button mConfirm;
+    private ImageView mStep1Status;
+    private ImageView mStep2Status;
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        View view = getLayoutInflater().inflate(R.layout.telecom_enable_phone_account, null);
+        setContentView(view);
+        setInfoResources(R.string.telecom_enable_phone_account_test,
+                R.string.telecom_enable_phone_account_info, -1);
+        setPassFailButtonClickListeners();
+        getPassButton().setEnabled(false);
+
+        mRegisterPhoneAccount = (Button) view.findViewById(
+                R.id.telecom_enable_phone_account_register_button);
+        mRegisterPhoneAccount.setOnClickListener(v -> {
+            PhoneAccountUtils.registerTestPhoneAccount(this);
+            PhoneAccount account = PhoneAccountUtils.getPhoneAccount(this);
+            if (account != null) {
+                mConfirm.setEnabled(true);
+                mRegisterPhoneAccount.setEnabled(false);
+                mStep1Status.setImageResource(R.drawable.fs_good);
+            } else {
+                mStep1Status.setImageResource(R.drawable.fs_error);
+            }
+        });
+
+        mConfirm = (Button) view.findViewById(R.id.telecom_enable_phone_account_confirm_button);
+        mConfirm.setOnClickListener(v -> {
+            PhoneAccount account = PhoneAccountUtils.getPhoneAccount(this);
+            if (account != null && account.isEnabled()) {
+                getPassButton().setEnabled(true);
+                mStep2Status.setImageResource(R.drawable.fs_good);
+                mConfirm.setEnabled(false);
+                PhoneAccountUtils.unRegisterTestPhoneAccount(this);
+            } else {
+                mStep2Status.setImageResource(R.drawable.fs_error);
+            }
+        });
+        mConfirm.setEnabled(false);
+
+        mStep1Status = (ImageView) view.findViewById(R.id.step_1_status);
+        mStep2Status = (ImageView) view.findViewById(R.id.step_2_status);
+    }
+
+}
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/telecom/PhoneAccountUtils.java b/apps/CtsVerifier/src/com/android/cts/verifier/telecom/PhoneAccountUtils.java
new file mode 100644
index 0000000..4201ffd
--- /dev/null
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/telecom/PhoneAccountUtils.java
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2017 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 com.android.cts.verifier.telecom;
+
+import android.content.ComponentName;
+import android.content.Context;
+import android.net.Uri;
+import android.telecom.PhoneAccount;
+import android.telecom.PhoneAccountHandle;
+import android.telecom.TelecomManager;
+
+import com.android.cts.verifier.PassFailButtons;
+
+/**
+ * Utilities class for dealing with the Telecom CTS Verifier PhoneAccounts.
+ */
+public class PhoneAccountUtils {
+    public static final String TEST_PHONE_ACCOUNT_ID = "test";
+    public static final String TEST_PHONE_ACCOUNT_LABEL = "CTS Verifier Test";
+    public static final Uri TEST_PHONE_ACCOUNT_ADDRESS = Uri.parse("sip:test@android.com");
+
+    public static final PhoneAccountHandle TEST_PHONE_ACCOUNT_HANDLE =
+            new PhoneAccountHandle(new ComponentName(
+                    PassFailButtons.class.getPackage().getName(),
+                    CtsConnectionService.class.getName()), TEST_PHONE_ACCOUNT_ID);
+    public static final PhoneAccount TEST_PHONE_ACCOUNT = new PhoneAccount.Builder(
+            TEST_PHONE_ACCOUNT_HANDLE, TEST_PHONE_ACCOUNT_LABEL)
+            .setAddress(TEST_PHONE_ACCOUNT_ADDRESS)
+            .setCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER)
+            .build();
+
+    /**
+     * Registers the test phone account.
+     * @param context The context.
+     */
+    public static void registerTestPhoneAccount(Context context) {
+        TelecomManager telecomManager = (TelecomManager) context.getSystemService(
+                Context.TELECOM_SERVICE);
+        telecomManager.registerPhoneAccount(TEST_PHONE_ACCOUNT);
+    }
+
+    /**
+     * Retrieves the test phone account, or null if not registered.
+     * @param context The context.
+     * @return The Phone Account.
+     */
+    public static PhoneAccount getPhoneAccount(Context context) {
+        TelecomManager telecomManager = (TelecomManager) context.getSystemService(
+                Context.TELECOM_SERVICE);
+        return telecomManager.getPhoneAccount(TEST_PHONE_ACCOUNT_HANDLE);
+    }
+
+    /**
+     * Unregisters the test phone account.
+     * @param context The context.
+     */
+    public static void unRegisterTestPhoneAccount(Context context) {
+        TelecomManager telecomManager = (TelecomManager) context.getSystemService(
+                Context.TELECOM_SERVICE);
+        telecomManager.unregisterPhoneAccount(TEST_PHONE_ACCOUNT_HANDLE);
+    }
+}