DO NOT MERGE Add ability to simulate missed calls

After initiating a call using
adb shell am start -a android.telecom.testapps.ACTION_START_INCOMING_CALL
-d "tel:14082932268"

Hang up the call using:
adb shell am start -a android.telecom.testapps.ACTION_HANGUP_CALLS

Bug: 19803865
Change-Id: I9ba12bac1eb5db5c19d0d830fc1e1e77cb3b86cb
diff --git a/testapps/Android.mk b/testapps/Android.mk
index 9c11d34..ab10380 100644
--- a/testapps/Android.mk
+++ b/testapps/Android.mk
@@ -19,6 +19,7 @@
 
 LOCAL_STATIC_JAVA_LIBRARIES := \
         android-ex-camera2 \
+        android-support-v4 \
         guava
 
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
diff --git a/testapps/AndroidManifest.xml b/testapps/AndroidManifest.xml
index 747d377..728c64a 100644
--- a/testapps/AndroidManifest.xml
+++ b/testapps/AndroidManifest.xml
@@ -66,6 +66,10 @@
                 <data android:scheme="tel" />
                 <data android:scheme="sip" />
             </intent-filter>
+            <intent-filter>
+                <action android:name="android.telecom.testapps.ACTION_HANGUP_CALLS" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
         </activity>
 
         <receiver android:name="com.android.server.telecom.testapps.CallNotificationReceiver"
diff --git a/testapps/src/com/android/server/telecom/testapps/CallNotificationReceiver.java b/testapps/src/com/android/server/telecom/testapps/CallNotificationReceiver.java
index a835bf1..0589a8e 100644
--- a/testapps/src/com/android/server/telecom/testapps/CallNotificationReceiver.java
+++ b/testapps/src/com/android/server/telecom/testapps/CallNotificationReceiver.java
@@ -22,6 +22,7 @@
 import android.content.Intent;
 import android.net.Uri;
 import android.os.Bundle;
+import android.support.v4.content.LocalBroadcastManager;
 import android.telecom.CallState;
 import android.telecom.PhoneAccountHandle;
 import android.telecom.TelecomManager;
@@ -105,4 +106,10 @@
 
         TelecomManager.from(context).addNewUnknownCall(phoneAccount, extras);
     }
+
+    public static void hangupCalls(Context context) {
+        Log.i(TAG, "Hanging up all calls");
+        LocalBroadcastManager.getInstance(context).sendBroadcast(
+                new Intent(TestCallActivity.ACTION_HANGUP_CALLS));
+    }
 }
diff --git a/testapps/src/com/android/server/telecom/testapps/TestCallActivity.java b/testapps/src/com/android/server/telecom/testapps/TestCallActivity.java
index 6f4ae20..38d2565 100644
--- a/testapps/src/com/android/server/telecom/testapps/TestCallActivity.java
+++ b/testapps/src/com/android/server/telecom/testapps/TestCallActivity.java
@@ -42,6 +42,12 @@
     public static final String ACTION_NEW_UNKNOWN_CALL =
             "android.telecom.testapps.ACTION_NEW_UNKNOWN_CALL";
 
+    /*
+     * Hang up any test incoming calls, to simulate the user missing a call.
+     */
+    public static final String ACTION_HANGUP_CALLS =
+            "android.telecom.testapps.ACTION_HANGUP_CALLS";
+
     @Override
     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);
@@ -52,6 +58,8 @@
             CallNotificationReceiver.sendIncomingCallIntent(this, data, false);
         } else if (ACTION_NEW_UNKNOWN_CALL.equals(action) && data != null) {
             CallNotificationReceiver.addNewUnknownCall(this, data, intent.getExtras());
+        } else if (ACTION_HANGUP_CALLS.equals(action)) {
+            CallNotificationReceiver.hangupCalls(this);
         } else {
             CallServiceNotifier.getInstance().updateNotification(this);
         }
diff --git a/testapps/src/com/android/server/telecom/testapps/TestConnectionService.java b/testapps/src/com/android/server/telecom/testapps/TestConnectionService.java
index 3eb0bc5..b380e78 100644
--- a/testapps/src/com/android/server/telecom/testapps/TestConnectionService.java
+++ b/testapps/src/com/android/server/telecom/testapps/TestConnectionService.java
@@ -16,12 +16,16 @@
 
 package com.android.server.telecom.testapps;
 
+import android.content.BroadcastReceiver;
 import android.content.ComponentName;
+import android.content.Context;
 import android.content.Intent;
+import android.content.IntentFilter;
 import android.media.MediaPlayer;
 import android.net.Uri;
 import android.os.Bundle;
 import android.os.Handler;
+import android.support.v4.content.LocalBroadcastManager;
 import android.telecom.AudioState;
 import android.telecom.Conference;
 import android.telecom.Connection;
@@ -131,6 +135,15 @@
         /** Used to cleanup camera and media when done with connection. */
         private TestVideoProvider mTestVideoCallProvider;
 
+        private BroadcastReceiver mHangupReceiver = new BroadcastReceiver() {
+            @Override
+            public void onReceive(Context context, Intent intent) {
+                setDisconnected(new DisconnectCause(DisconnectCause.MISSED));
+                destroyCall(TestConnection.this);
+                destroy();
+            }
+        };
+
         TestConnection(boolean isIncoming) {
             mIsIncoming = isIncoming;
             // Assume all calls are video capable.
@@ -142,6 +155,9 @@
             capabilities |= CAPABILITY_SUPPORT_HOLD;
             capabilities |= CAPABILITY_HOLD;
             setConnectionCapabilities(capabilities);
+
+            LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(
+                    mHangupReceiver, new IntentFilter(TestCallActivity.ACTION_HANGUP_CALLS));
         }
 
         void startOutgoing() {
@@ -219,6 +235,11 @@
             mTestVideoCallProvider = testVideoCallProvider;
         }
 
+        public void cleanup() {
+            LocalBroadcastManager.getInstance(getApplicationContext()).unregisterReceiver(
+                    mHangupReceiver);
+        }
+
         /**
          * Stops playback of test videos.
          */
@@ -384,6 +405,7 @@
     }
 
     private void destroyCall(TestConnection connection) {
+        connection.cleanup();
         mCalls.remove(connection);
 
         // Ensure any playing media and camera resources are released.
diff --git a/tests/Android.mk b/tests/Android.mk
index ca1b835..377abbd 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -39,6 +39,7 @@
     --extra-packages com.android.server.telecom
 
 LOCAL_PACKAGE_NAME := TelecomUnitTests
+
 LOCAL_CERTIFICATE := platform
 
 LOCAL_MODULE_TAGS := tests