Merge "Use Received Uri as Gateway Uri" into qt-dev
diff --git a/src/com/android/server/telecom/callredirection/CallRedirectionProcessor.java b/src/com/android/server/telecom/callredirection/CallRedirectionProcessor.java
index 5b58568..fa65fed 100644
--- a/src/com/android/server/telecom/callredirection/CallRedirectionProcessor.java
+++ b/src/com/android/server/telecom/callredirection/CallRedirectionProcessor.java
@@ -93,15 +93,15 @@
         private void onServiceBound(ICallRedirectionService service) {
             mService = service;
             try {
-                mHandle = mCallRedirectionProcessorHelper.formatNumberForRedirection(mHandle);
                 // Telecom does not perform user interactions for carrier call redirection.
-                mService.placeCall(new CallRedirectionAdapter(), mHandle, mPhoneAccountHandle,
-                        mAllowInteractiveResponse
+                mService.placeCall(new CallRedirectionAdapter(), mProcessedDestinationUri,
+                        mPhoneAccountHandle, mAllowInteractiveResponse
                                 && mServiceType.equals(SERVICE_TYPE_USER_DEFINED));
                 Log.addEvent(mCall, mServiceType.equals(SERVICE_TYPE_USER_DEFINED)
                         ? LogUtils.Events.REDIRECTION_SENT_USER
                         : LogUtils.Events.REDIRECTION_SENT_CARRIER, mComponentName);
-                Log.d(this, "Requested placeCall with [handle]" + Log.pii(mHandle)
+                Log.d(this, "Requested placeCall with [Destination Uri] "
+                        + Log.pii(mProcessedDestinationUri)
                         + " [phoneAccountHandle]" + mPhoneAccountHandle);
             } catch (RemoteException e) {
                 Log.e(this, e, "Failed to request with the found " + mServiceType + " call"
@@ -188,19 +188,22 @@
             }
 
             @Override
-            public void redirectCall(Uri handle, PhoneAccountHandle targetPhoneAccount,
+            public void redirectCall(Uri gatewayUri, PhoneAccountHandle targetPhoneAccount,
                                      boolean confirmFirst) {
                 Log.startSession("CRA.rC");
                 long token = Binder.clearCallingIdentity();
                 try {
                     synchronized (mTelecomLock) {
-                        mHandle = handle;
+                        mRedirectionGatewayInfo = mCallRedirectionProcessorHelper
+                                .getGatewayInfoFromGatewayUri(mComponentName.getPackageName(),
+                                        gatewayUri, mDestinationUri);
                         mPhoneAccountHandle = targetPhoneAccount;
                         mUiAction = (confirmFirst && mServiceType.equals(SERVICE_TYPE_USER_DEFINED)
                                 && mAllowInteractiveResponse)
                                 ? UI_TYPE_USER_DEFINED_ASK_FOR_CONFIRM : mUiAction;
-                        Log.d(this, "Received redirectCall with [handle]" + Log.pii(mHandle)
-                                + " [phoneAccountHandle]" + mPhoneAccountHandle + " from "
+                        Log.d(this, "Received redirectCall with [gatewayUri]"
+                                + Log.pii(gatewayUri) + " [phoneAccountHandle]"
+                                + mPhoneAccountHandle + "[confirmFirst]" + confirmFirst + " from "
                                 + mServiceType + " call redirection service");
                         finishCallRedirection();
                     }
@@ -216,7 +219,7 @@
     private final CallsManager mCallsManager;
     private final Call mCall;
     private final boolean mAllowInteractiveResponse;
-    private final GatewayInfo mGatewayInfo;
+    private GatewayInfo mRedirectionGatewayInfo;
     private final boolean mSpeakerphoneOn;
     private final int mVideoState;
     private final Timeouts.Adapter mTimeoutsAdapter;
@@ -234,7 +237,12 @@
             = "user_defined_ask_for_confirm";
 
     private PhoneAccountHandle mPhoneAccountHandle;
-    private Uri mHandle;
+    private Uri mDestinationUri;
+    /**
+     * Try to send the implemented service with processed destination uri by formatting it to E.164
+     * and removing post dial digits.
+     */
+    private Uri mProcessedDestinationUri;
 
     /**
      * Indicates if Telecom should cancel the call when the whole call redirection finishes.
@@ -267,9 +275,9 @@
         mContext = context;
         mCallsManager = callsManager;
         mCall = call;
-        mHandle = handle;
+        mDestinationUri = handle;
         mPhoneAccountHandle = call.getTargetPhoneAccount();
-        mGatewayInfo = gatewayInfo;
+        mRedirectionGatewayInfo = gatewayInfo;
         mSpeakerphoneOn = speakerphoneOn;
         mVideoState = videoState;
         mTimeoutsAdapter = callsManager.getTimeoutsAdapter();
@@ -281,6 +289,8 @@
         mAllowInteractiveResponse = !callsManager.getSystemStateHelper().isCarMode();
         mCallRedirectionProcessorHelper = new CallRedirectionProcessorHelper(
                 context, callsManager, phoneAccountRegistrar);
+        mProcessedDestinationUri = mCallRedirectionProcessorHelper.formatNumberForRedirection(
+                mDestinationUri);
     }
 
     @Override
@@ -289,15 +299,13 @@
         mHandler.post(new Runnable("CRP.oCRC", mTelecomLock) {
             @Override
             public void loggedRun() {
-                mHandle = mCallRedirectionProcessorHelper.processNumberWhenRedirectionComplete(
-                        mHandle);
                 if (mIsUserDefinedRedirectionPending) {
                     Log.addEvent(mCall, LogUtils.Events.REDIRECTION_COMPLETED_USER);
                     mIsUserDefinedRedirectionPending = false;
                     if (mShouldCancelCall) {
-                        mCallsManager.onCallRedirectionComplete(mCall, mHandle,
-                                mPhoneAccountHandle, mGatewayInfo, mSpeakerphoneOn, mVideoState,
-                                mShouldCancelCall, mUiAction);
+                        mCallsManager.onCallRedirectionComplete(mCall, mDestinationUri,
+                                mPhoneAccountHandle, mRedirectionGatewayInfo, mSpeakerphoneOn,
+                                mVideoState, mShouldCancelCall, mUiAction);
                     } else {
                         performCarrierCallRedirection();
                     }
@@ -305,9 +313,9 @@
                 if (mIsCarrierRedirectionPending) {
                     Log.addEvent(mCall, LogUtils.Events.REDIRECTION_COMPLETED_CARRIER);
                     mIsCarrierRedirectionPending = false;
-                    mCallsManager.onCallRedirectionComplete(mCall, mHandle,
-                            mPhoneAccountHandle, mGatewayInfo, mSpeakerphoneOn, mVideoState,
-                            mShouldCancelCall, mUiAction);
+                    mCallsManager.onCallRedirectionComplete(mCall, mDestinationUri,
+                            mPhoneAccountHandle, mRedirectionGatewayInfo, mSpeakerphoneOn,
+                            mVideoState, mShouldCancelCall, mUiAction);
                 }
             }
         }.prepare());
@@ -317,12 +325,12 @@
      * The entry to perform call redirection of the call from (@link CallsManager)
      */
     public void performCallRedirection() {
-        // If the Gateway Info is set with intent, do not perform call redirection.
-        if (mGatewayInfo != null) {
-            mCallsManager.onCallRedirectionComplete(mCall, mHandle, mPhoneAccountHandle,
-                    mGatewayInfo, mSpeakerphoneOn, mVideoState, mShouldCancelCall, mUiAction);
+        // If the Gateway Info is set with intent, do not request more call redirection.
+        if (mRedirectionGatewayInfo != null) {
+            mCallsManager.onCallRedirectionComplete(mCall, mDestinationUri, mPhoneAccountHandle,
+                    mRedirectionGatewayInfo, mSpeakerphoneOn, mVideoState, mShouldCancelCall,
+                    mUiAction);
         } else {
-            mCallRedirectionProcessorHelper.storePostDialDigits(mHandle);
             performUserDefinedCallRedirection();
         }
     }
@@ -356,8 +364,8 @@
         } else {
             Log.i(this, "There are no carrier call redirection services installed on this"
                     + " device.");
-            mCallsManager.onCallRedirectionComplete(mCall, mHandle,
-                    mPhoneAccountHandle, mGatewayInfo, mSpeakerphoneOn, mVideoState,
+            mCallsManager.onCallRedirectionComplete(mCall, mDestinationUri,
+                    mPhoneAccountHandle, mRedirectionGatewayInfo, mSpeakerphoneOn, mVideoState,
                     mShouldCancelCall, mUiAction);
         }
     }
@@ -399,7 +407,7 @@
                 mCallRedirectionProcessorHelper.getUserDefinedCallRedirectionService() != null
                         || mCallRedirectionProcessorHelper.getCarrierCallRedirectionService(
                                 mPhoneAccountHandle) != null;
-        Log.w(this, "Can make call redirection with any available service: "
+        Log.i(this, "Can make call redirection with any available service: "
                 + canMakeCallRedirectionWithService);
         return canMakeCallRedirectionWithService;
     }
diff --git a/src/com/android/server/telecom/callredirection/CallRedirectionProcessorHelper.java b/src/com/android/server/telecom/callredirection/CallRedirectionProcessorHelper.java
index 855d022..12c8c57 100644
--- a/src/com/android/server/telecom/callredirection/CallRedirectionProcessorHelper.java
+++ b/src/com/android/server/telecom/callredirection/CallRedirectionProcessorHelper.java
@@ -22,9 +22,9 @@
 import android.content.Intent;
 import android.content.pm.ResolveInfo;
 import android.net.Uri;
-import android.os.Binder;
 import android.os.PersistableBundle;
 import android.telecom.CallRedirectionService;
+import android.telecom.GatewayInfo;
 import android.telecom.Log;
 import android.telecom.PhoneAccountHandle;
 import android.telephony.CarrierConfigManager;
@@ -42,7 +42,6 @@
     private final Context mContext;
     private final CallsManager mCallsManager;
     private final PhoneAccountRegistrar mPhoneAccountRegistrar;
-    private String mOriginalPostDialDigits = "";
 
     public CallRedirectionProcessorHelper(
             Context context,
@@ -130,27 +129,6 @@
         return removePostDialDigits(formatNumberToE164(handle));
     }
 
-    protected Uri processNumberWhenRedirectionComplete(Uri handle) {
-        return appendStoredPostDialDigits(formatNumberForRedirection(handle));
-    }
-
-    protected void storePostDialDigits(Uri handle) {
-        String number = handle.getSchemeSpecificPart();
-        String postDialPortion = PhoneNumberUtils.extractPostDialPortion(number);
-        if (postDialPortion != null) {
-            mOriginalPostDialDigits = postDialPortion;
-        }
-        Log.i(this, "storePostDialDigits, stored post dial digits: "
-                + Log.pii(mOriginalPostDialDigits));
-    }
-
-    protected Uri appendStoredPostDialDigits(Uri handle) {
-        String number = handle.getSchemeSpecificPart();
-        number += mOriginalPostDialDigits;
-        Log.i(this, "appendStoredPostDialDigits, appended number: " + Log.pii(number));
-        return Uri.fromParts(handle.getScheme(), number, null);
-    }
-
     protected Uri formatNumberToE164(Uri handle) {
         String number = handle.getSchemeSpecificPart();
 
@@ -185,4 +163,11 @@
         }
     }
 
+    protected GatewayInfo getGatewayInfoFromGatewayUri(
+            String gatewayPackageName, Uri gatewayUri, Uri destinationUri) {
+        if (!TextUtils.isEmpty(gatewayPackageName) && gatewayUri != null) {
+            return new GatewayInfo(gatewayPackageName, gatewayUri, destinationUri);
+        }
+        return null;
+    }
 }
diff --git a/tests/src/com/android/server/telecom/tests/CallRedirectionProcessorTest.java b/tests/src/com/android/server/telecom/tests/CallRedirectionProcessorTest.java
index fa78383..f0f0fe4 100644
--- a/tests/src/com/android/server/telecom/tests/CallRedirectionProcessorTest.java
+++ b/tests/src/com/android/server/telecom/tests/CallRedirectionProcessorTest.java
@@ -27,6 +27,7 @@
 import android.os.UserHandle;
 import android.telecom.GatewayInfo;
 import android.telecom.PhoneAccountHandle;
+import android.telephony.TelephonyManager;
 import com.android.internal.telecom.ICallRedirectionService;
 import com.android.server.telecom.Call;
 import com.android.server.telecom.CallsManager;
@@ -66,6 +67,7 @@
     @Mock private Call mCall;
 
     @Mock private PackageManager mPackageManager;
+    @Mock private TelephonyManager mTelephonyManager;
     @Mock private IBinder mBinder;
     @Mock private ICallRedirectionService mCallRedirectionService;
 
@@ -118,6 +120,8 @@
                 .thenReturn(CARRIER_SHORT_TIMEOUT_MS);
         when(mCallsManager.getLock()).thenReturn(mLock);
         when(mCallsManager.getCurrentUserHandle()).thenReturn(mUserHandle);
+        when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
+        when(mTelephonyManager.getNetworkCountryIso()).thenReturn("");
         when(mContext.bindServiceAsUser(nullable(Intent.class), nullable(ServiceConnection.class),
                 anyInt(), eq(UserHandle.CURRENT))).thenReturn(true);
     }