Revert "Revert "Test app for call companion app" am: 456dc7619c am: d93cf54a7f am: a2594245eb"

This reverts commit 60599ffcab79ae0b34b5c914ecbf5d82c8c56402.

Reason for revert: <aosp/1535284 causes build broken in aosp because DEVICE_PROFILE_WATCH is not in aosp and TH didn't find it, but no conflict in master; however, it was removed in master because the revert (aosp/1534575) in aosp automerges into master; we still need it in master>

Change-Id: I797a550f940f46c130971bf86b95610b46c6ee95
diff --git a/testapps/companionapp/Android.bp b/testapps/companionapp/Android.bp
new file mode 100644
index 0000000..e3612a1
--- /dev/null
+++ b/testapps/companionapp/Android.bp
@@ -0,0 +1,24 @@
+//
+// Copyright (C) 2019 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.
+//
+
+android_test {
+    name: "TelecomCompanionApp",
+    static_libs: [
+        "androidx.legacy_legacy-support-v4",
+        "guava",
+    ],
+    srcs: ["src/**/*.java"],
+}
diff --git a/testapps/companionapp/AndroidManifest.xml b/testapps/companionapp/AndroidManifest.xml
new file mode 100644
index 0000000..7569d8f
--- /dev/null
+++ b/testapps/companionapp/AndroidManifest.xml
@@ -0,0 +1,47 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2019 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.
+-->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+     coreApp="true"
+     package="com.android.server.telecom.companionapp">
+
+    <uses-sdk android:minSdkVersion="28"
+         android:targetSdkVersion="29"/>
+
+    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
+    <uses-permission android:name="android.permission.MANAGE_ONGOING_CALLS" />
+    <uses-feature android:name="android.software.companion_device_setup"/>
+
+    <application android:label="Telecom Companion">
+        <uses-library android:name="android.test.runner"/>
+
+        <activity android:name="com.android.server.telecom.companionapp.CompanionTestApp"
+                  android:label="CompanionTestApp">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+
+        <service android:name="com.android.server.telecom.companionapp.CompanionInCallServiceImpl"
+             android:permission="android.permission.BIND_INCALL_SERVICE"
+             android:exported="true">
+          <intent-filter>
+              <action android:name="android.telecom.InCallService"/>
+          </intent-filter>
+        </service>
+    </application>
+</manifest>
diff --git a/testapps/companionapp/res/drawable-hdpi/ic_android_black_24dp.png b/testapps/companionapp/res/drawable-hdpi/ic_android_black_24dp.png
new file mode 100644
index 0000000..ed3ee45
--- /dev/null
+++ b/testapps/companionapp/res/drawable-hdpi/ic_android_black_24dp.png
Binary files differ
diff --git a/testapps/companionapp/res/drawable-mdpi/ic_android_black_24dp.png b/testapps/companionapp/res/drawable-mdpi/ic_android_black_24dp.png
new file mode 100644
index 0000000..a4add51
--- /dev/null
+++ b/testapps/companionapp/res/drawable-mdpi/ic_android_black_24dp.png
Binary files differ
diff --git a/testapps/companionapp/res/drawable-xhdpi/ic_android_black_24dp.png b/testapps/companionapp/res/drawable-xhdpi/ic_android_black_24dp.png
new file mode 100644
index 0000000..41558f2
--- /dev/null
+++ b/testapps/companionapp/res/drawable-xhdpi/ic_android_black_24dp.png
Binary files differ
diff --git a/testapps/companionapp/res/drawable-xxhdpi/ic_android_black_24dp.png b/testapps/companionapp/res/drawable-xxhdpi/ic_android_black_24dp.png
new file mode 100644
index 0000000..6006b12
--- /dev/null
+++ b/testapps/companionapp/res/drawable-xxhdpi/ic_android_black_24dp.png
Binary files differ
diff --git a/testapps/companionapp/res/drawable-xxxhdpi/ic_android_black_24dp.png b/testapps/companionapp/res/drawable-xxxhdpi/ic_android_black_24dp.png
new file mode 100644
index 0000000..4f935bf
--- /dev/null
+++ b/testapps/companionapp/res/drawable-xxxhdpi/ic_android_black_24dp.png
Binary files differ
diff --git a/testapps/companionapp/res/layout/main.xml b/testapps/companionapp/res/layout/main.xml
new file mode 100644
index 0000000..e553754
--- /dev/null
+++ b/testapps/companionapp/res/layout/main.xml
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 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.
+-->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:orientation="vertical"
+    android:layout_width="fill_parent"
+    android:layout_height="fill_parent">
+    <TextView
+        android:layout_width="fill_parent"
+        android:layout_height="wrap_content"
+        android:text="This is a test app for companion in call service."
+        android:padding="10dp"
+        android:textColor="#FFFFFF"/>
+    <Button
+        android:id="@+id/buttonStart"
+        android:layout_width="fill_parent"
+        android:layout_height="wrap_content"
+        android:text="Start Association"/>
+    <Button
+        android:id="@+id/buttonStop"
+        android:layout_width="fill_parent"
+        android:layout_height="wrap_content"
+        android:text="Stop Association"/>
+</LinearLayout>
diff --git a/testapps/companionapp/src/com/android/server/telecom/companionapp/CompanionInCallServiceImpl.java b/testapps/companionapp/src/com/android/server/telecom/companionapp/CompanionInCallServiceImpl.java
new file mode 100644
index 0000000..890cd77
--- /dev/null
+++ b/testapps/companionapp/src/com/android/server/telecom/companionapp/CompanionInCallServiceImpl.java
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2019 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.server.telecom.companionapp;
+
+import android.content.Intent;
+import android.telecom.Call;
+import android.telecom.InCallService;
+import android.telecom.Phone;
+import android.util.Log;
+import android.widget.Toast;
+
+import java.lang.Override;
+import java.lang.String;
+
+/**
+ * Test Companion In-Call service implementation.
+ */
+public class CompanionInCallServiceImpl extends InCallService {
+    private static final String TAG = "CompanionInCallServiceImpl";
+
+    private Phone mPhone;
+
+    private Phone.Listener mPhoneListener = new Phone.Listener() {
+        @Override
+        public void onCallAdded(Phone phone, Call call) {
+            Log.i(TAG, "onCallAdded: " + call.toString());
+            Toast.makeText(
+                    CompanionInCallServiceImpl.this, "onCallAdded", Toast.LENGTH_LONG).show();
+        }
+
+        @Override
+        public void onCallRemoved(Phone phone, Call call) {
+            Log.i(TAG, "onCallRemoved: " + call.toString());
+            Toast.makeText(
+                    CompanionInCallServiceImpl.this, "onCallRemoved", Toast.LENGTH_LONG).show();
+        }
+    };
+
+    @Override
+    public void onPhoneCreated(Phone phone) {
+        Log.i(TAG, "onPhoneCreated");
+        mPhone = phone;
+        mPhone.addListener(mPhoneListener);
+    }
+
+    @Override
+    public boolean onUnbind(Intent intent) {
+        Log.i(TAG, "Companion TestApp InCallService unbind");
+        mPhone.removeListener(mPhoneListener);
+        mPhone = null;
+        return super.onUnbind(intent);
+    }
+
+    /**
+     * Used to bind a call
+     * @param intent
+     * @return
+     */
+    @Override
+    public android.os.IBinder onBind(Intent intent) {
+        Log.d(TAG, "Companion TestApp InCallService bind");
+        return super.onBind(intent);
+    }
+}
diff --git a/testapps/companionapp/src/com/android/server/telecom/companionapp/CompanionTestApp.java b/testapps/companionapp/src/com/android/server/telecom/companionapp/CompanionTestApp.java
new file mode 100644
index 0000000..7ed1e1e
--- /dev/null
+++ b/testapps/companionapp/src/com/android/server/telecom/companionapp/CompanionTestApp.java
@@ -0,0 +1,168 @@
+/*
+ * Copyright (C) 2020 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.server.telecom.companionapp;
+
+import android.app.Activity;
+import android.bluetooth.BluetoothDevice;
+import android.companion.AssociationRequest;
+import android.companion.BluetoothDeviceFilter;
+import android.companion.CompanionDeviceManager;
+import android.content.Intent;
+import android.content.IntentSender;
+import android.os.Bundle;
+import android.os.ParcelUuid;
+import android.util.Log;
+import android.view.View;
+import android.view.View.OnClickListener;
+import android.widget.Button;
+import android.widget.Toast;
+
+import java.util.UUID;
+import java.util.regex.Pattern;
+
+/**
+ * Companion Test App in Telecom
+ */
+public class CompanionTestApp extends Activity {
+
+    private static String logtag = "CompanionTestApp";
+    private CompanionDeviceManager mDeviceManager;
+    private AssociationRequest mPairingRequest;
+    private BluetoothDeviceFilter mDeviceFilter;
+
+    private static final int SELECT_DEVICE_REQUEST_CODE = 42;
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        setupDeviceAssociation();
+
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.main);
+
+        Button buttonStart = (Button)findViewById(R.id.buttonStart);
+        buttonStart.setOnClickListener(startListener);
+
+        Button buttonStop = (Button)findViewById(R.id.buttonStop);
+        buttonStop.setOnClickListener(stopListener);
+    }
+
+    private OnClickListener startListener = new OnClickListener() {
+        public void onClick(View v) {
+            Log.d(logtag,"onClick() called - start button");
+            Toast.makeText(CompanionTestApp.this, "Start Association", Toast.LENGTH_LONG).show();
+            assosicate();
+            Log.d(logtag,"onClick() ended - start button");
+        }
+    };
+
+    private OnClickListener stopListener = new OnClickListener() {
+        public void onClick(View v) {
+            Log.d(logtag,"onClick() called - stop button");
+            Toast.makeText(CompanionTestApp.this, "Stop Association", Toast.LENGTH_LONG).show();
+            disassosicate();
+            Log.d(logtag,"onClick() ended - stop button");
+        }
+    };
+
+    // Setup Device Association Preparation, per
+    // https://developer.android.com/guide/topics/connectivity/companion-device-pairing
+    private void setupDeviceAssociation() {
+        mDeviceManager = getSystemService(CompanionDeviceManager.class);
+
+        mDeviceFilter = new BluetoothDeviceFilter.Builder()
+                .build();
+
+        mPairingRequest = new AssociationRequest.Builder()
+                .addDeviceFilter(mDeviceFilter)
+                .setDeviceProfile(AssociationRequest.DEVICE_PROFILE_WATCH)
+                .build();
+
+    }
+
+    // Associate bluetooth device, per
+    // https://developer.android.com/guide/topics/connectivity/companion-device-pairing
+    private void assosicate() {
+        // When the app tries to pair with the Bluetooth device, show the
+        // appropriate pairing request dialog to the user.
+        mDeviceManager.associate(mPairingRequest,
+                new CompanionDeviceManager.Callback() {
+                    @Override
+                    public void onDeviceFound(IntentSender chooserLauncher) {
+                        try {
+                            startIntentSenderForResult(chooserLauncher,
+                                SELECT_DEVICE_REQUEST_CODE, null, 0, 0, 0);
+                        } catch (Exception ex) {
+                            Log.d(logtag, "Callback onDeviceFound() Exception: " + ex);
+                        }
+                    }
+
+                    @Override
+                    public void onFailure(CharSequence error) {
+                        Log.d(logtag, "Callback onFailure() called - error: " + error);
+                    }
+                },
+                null);
+    }
+
+    // Disassociate the associated bluetooth device
+    private void disassosicate() {
+        for (String macAddress : mDeviceManager.getAssociations()) {
+            mDeviceManager.disassociate(macAddress);
+        }
+    }
+
+    @Override
+    public void onActivityResult(int requestCode, int resultCode, Intent data) {
+        if (requestCode == SELECT_DEVICE_REQUEST_CODE &&
+                resultCode == Activity.RESULT_OK) {
+            // User has chosen to pair with the Bluetooth device.
+            BluetoothDevice deviceToPair =
+                    data.getParcelableExtra(CompanionDeviceManager.EXTRA_DEVICE);
+            deviceToPair.createBond();
+        }
+    }
+
+    @Override
+    protected void onStart() {
+        Log.d(logtag,"onStart() called");
+        super.onStart();
+    }
+
+    @Override
+    protected void onResume() {
+        Log.d(logtag,"onResume() called");
+        super.onResume();
+    }
+
+    @Override
+    protected void onPause() {
+        Log.d(logtag,"onPause() called");
+        super.onPause();
+    }
+
+    @Override
+    protected void onStop() {
+        Log.d(logtag,"onStop() called");
+        super.onStop();
+    }
+
+    @Override
+    protected void onDestroy() {
+        Log.d(logtag,"onDestroy() called");
+        super.onDestroy();
+    }
+}