Add call screening service binding rule for outgoing call.

Unbind the call screening service after it screening the call.

Test: CTS
Bug: 135929421
Change-Id: Ifa767b2d6c2e28feb9a5b8e866cefb34ff9150f1
diff --git a/src/com/android/server/telecom/CallScreeningServiceHelper.java b/src/com/android/server/telecom/CallScreeningServiceHelper.java
index 79d5286..29e9bb0 100644
--- a/src/com/android/server/telecom/CallScreeningServiceHelper.java
+++ b/src/com/android/server/telecom/CallScreeningServiceHelper.java
@@ -57,25 +57,35 @@
      * from the call screening service to be handled.
      */
     private class CallScreeningAdapter extends ICallScreeningAdapter.Stub {
+        private ServiceConnection mServiceConnection;
+
+        public CallScreeningAdapter(ServiceConnection connection) {
+            mServiceConnection = connection;
+        }
+
         @Override
         public void allowCall(String s) throws RemoteException {
-            // no-op; we don't allow this on outgoing calls.
+            unbindCallScreeningService();
         }
 
         @Override
         public void silenceCall(String s) throws RemoteException {
-            // no-op; we don't allow this on outgoing calls.
+            unbindCallScreeningService();
         }
 
         @Override
         public void screenCallFurther(String callId) throws RemoteException {
-            // no-op; we don't allow this on outgoing calls.
+            unbindCallScreeningService();
         }
 
         @Override
         public void disallowCall(String s, boolean b, boolean b1, boolean b2,
                 ComponentName componentName) throws RemoteException {
-            // no-op; we don't allow this on outgoing calls.
+            unbindCallScreeningService();
+        }
+
+        private void unbindCallScreeningService() {
+            mContext.unbindService(mServiceConnection);
         }
     }
 
@@ -128,7 +138,7 @@
                 try {
                     try {
                         // Note: for outgoing calls, never include the restricted extras.
-                        screeningService.screenCall(new CallScreeningAdapter(),
+                        screeningService.screenCall(new CallScreeningAdapter(this),
                                 mParcelableCallUtilsConverter.toParcelableCallForScreening(mCall,
                                         false /* areRestrictedExtrasIncluded */));
                     } catch (RemoteException e) {
diff --git a/src/com/android/server/telecom/CallsManager.java b/src/com/android/server/telecom/CallsManager.java
index 599eb91..58f94c8 100644
--- a/src/com/android/server/telecom/CallsManager.java
+++ b/src/com/android/server/telecom/CallsManager.java
@@ -16,6 +16,7 @@
 
 package com.android.server.telecom;
 
+import android.Manifest;
 import android.annotation.NonNull;
 import android.app.ActivityManager;
 import android.app.KeyguardManager;
@@ -1581,8 +1582,16 @@
                                 isInContacts);
 
                         // We only want to provide a CallScreeningService with a call if its not in
-                        // contacts.
-                        if (!isInContacts) {
+                        // contacts or the package has READ_CONTACT permission.
+                        PackageManager packageManager = mContext.getPackageManager();
+                        int permission = packageManager.checkPermission(
+                                Manifest.permission.READ_CONTACTS,
+                                mRoleManagerAdapter.getDefaultCallScreeningApp());
+                        Log.d(CallsManager.this,
+                                "default call screening service package %s has permissions=%s",
+                                mRoleManagerAdapter.getDefaultCallScreeningApp(),
+                                permission == PackageManager.PERMISSION_GRANTED);
+                        if ((!isInContacts) || (permission == PackageManager.PERMISSION_GRANTED)) {
                             bindForOutgoingCallerId(theCall);
                         }
             }, new LoggedHandlerExecutor(outgoingCallHandler, "CM.pCSB", mLock));