Passing subscription info in the calls logic to log it properly

In order to log the subscription information in call history and call
log, the calls logic layer needs to pass the information between classes
in the process.

Bug: 15473965

Change-Id: I85ca5444e5881a8ceb0eb950dfc6385cacd045cf
diff --git a/src/com/android/telecomm/Call.java b/src/com/android/telecomm/Call.java
index 9b4bf80..09f60cf 100644
--- a/src/com/android/telecomm/Call.java
+++ b/src/com/android/telecomm/Call.java
@@ -29,6 +29,7 @@
 import android.telecomm.GatewayInfo;
 import android.telecomm.InCallService;
 import android.telecomm.Response;
+import android.telecomm.Subscription;
 import android.telecomm.TelecommConstants;
 import android.telephony.DisconnectCause;
 import android.telephony.PhoneNumberUtils;
@@ -115,6 +116,8 @@
      * service. */
     private final GatewayInfo mGatewayInfo;
 
+    private final Subscription mSubscription;
+
     private final Handler mHandler = new Handler();
 
     private long mConnectTimeMillis;
@@ -209,7 +212,7 @@
      * @param isIncoming True if this is an incoming call.
      */
     Call(boolean isIncoming, boolean isConference) {
-        this(null, null, isIncoming, isConference);
+        this(null, null, null, isIncoming, isConference);
     }
 
     /**
@@ -217,12 +220,15 @@
      *
      * @param handle The handle to dial.
      * @param gatewayInfo Gateway information to use for the call.
+     * @param subscription Subscription information to use for the call.
      * @param isIncoming True if this is an incoming call.
      */
-    Call(Uri handle, GatewayInfo gatewayInfo, boolean isIncoming, boolean isConference) {
+    Call(Uri handle, GatewayInfo gatewayInfo, Subscription subscription,
+            boolean isIncoming, boolean isConference) {
         mState = isConference ? CallState.ACTIVE : CallState.NEW;
         setHandle(handle);
         mGatewayInfo = gatewayInfo;
+        mSubscription = subscription;
         mIsIncoming = isIncoming;
         mIsConference = isConference;
         maybeLoadCannedSmsResponses();
@@ -348,6 +354,10 @@
         return mGatewayInfo;
     }
 
+    Subscription getSubscription() {
+        return mSubscription;
+    }
+
     boolean isIncoming() {
         return mIsIncoming;
     }
@@ -712,7 +722,8 @@
                     mGatewayInfo.getOriginalHandle());
 
         }
-        return new CallInfo(callId, mState, mHandle, mGatewayInfo, extras, descriptor);
+        return new CallInfo(callId, mState, mHandle, mGatewayInfo, mSubscription,
+                extras, descriptor);
     }
 
     /** Checks if this is a live call or not. */
diff --git a/src/com/android/telecomm/CallLogManager.java b/src/com/android/telecomm/CallLogManager.java
index 2dd6214..53d2072 100644
--- a/src/com/android/telecomm/CallLogManager.java
+++ b/src/com/android/telecomm/CallLogManager.java
@@ -21,6 +21,7 @@
 import android.os.AsyncTask;
 import android.provider.CallLog.Calls;
 import android.telecomm.CallState;
+import android.telecomm.Subscription;
 import android.telephony.PhoneNumberUtils;
 
 import com.android.internal.telephony.PhoneConstants;
@@ -48,12 +49,14 @@
          * @param durationInMillis Duration of the call (milliseconds).
          */
         public AddCallArgs(Context context, ContactInfo contactInfo, String number,
-                int presentation, int callType, long creationDate, long durationInMillis) {
+                int presentation, int callType, Subscription subscription,
+                long creationDate, long durationInMillis) {
             this.context = context;
             this.contactInfo = contactInfo;
             this.number = number;
             this.presentation = presentation;
             this.callType = callType;
+            this.subscription = subscription;
             this.timestamp = creationDate;
             this.durationInSec = (int)(durationInMillis / 1000);
         }
@@ -64,6 +67,7 @@
         public final String number;
         public final int presentation;
         public final int callType;
+        public final Subscription subscription;
         public final long timestamp;
         public final int durationInSec;
     }
@@ -111,8 +115,9 @@
         Log.d(TAG, "logNumber set to: %s", Log.pii(logNumber));
 
         final int presentation = getPresentation(call, contactInfo);
+        final Subscription subscription = call.getSubscription();
 
-        logCall(contactInfo, logNumber, presentation, callLogType, creationTime, age);
+        logCall(contactInfo, logNumber, presentation, callLogType, subscription, creationTime, age);
     }
 
     /**
@@ -130,6 +135,7 @@
             String number,
             int presentation,
             int callType,
+            Subscription subscription,
             long start,
             long duration) {
         boolean isEmergencyNumber = PhoneNumberUtils.isLocalEmergencyNumber(mContext, number);
@@ -148,7 +154,7 @@
                     + Log.pii(number) + "," + presentation + ", " + callType
                     + ", " + start + ", " + duration);
             AddCallArgs args = new AddCallArgs(mContext, contactInfo, number, presentation,
-                    callType, start, duration);
+                    callType, subscription, start, duration);
             logCallAsync(args);
         } else {
           Log.d(TAG, "Not adding emergency call to call log.");
@@ -220,7 +226,7 @@
                 try {
                     // May block.
                     result[i] = Calls.addCall(null, c.context, c.number, c.presentation,
-                            c.callType, c.timestamp, c.durationInSec);
+                            c.callType, c.subscription, c.timestamp, c.durationInSec);
                 } catch (Exception e) {
                     // This is very rare but may happen in legitimate cases.
                     // E.g. If the phone is encrypted and thus write request fails, it may cause
diff --git a/src/com/android/telecomm/CallsManager.java b/src/com/android/telecomm/CallsManager.java
index 174e59f..d53b583 100644
--- a/src/com/android/telecomm/CallsManager.java
+++ b/src/com/android/telecomm/CallsManager.java
@@ -23,6 +23,7 @@
 import android.telecomm.CallServiceDescriptor;
 import android.telecomm.CallState;
 import android.telecomm.GatewayInfo;
+import android.telecomm.Subscription;
 import android.telephony.DisconnectCause;
 
 import com.google.common.base.Preconditions;
@@ -283,7 +284,7 @@
      * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
      */
     void placeOutgoingCall(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo,
-            boolean speakerphoneOn) {
+            Subscription subscription, boolean speakerphoneOn) {
         final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
 
         if (gatewayInfo == null) {
@@ -294,7 +295,8 @@
         }
 
         Call call = new Call(
-                uriHandle, gatewayInfo, false /* isIncoming */, false /* isConference */);
+                uriHandle, gatewayInfo, subscription,
+                false /* isIncoming */, false /* isConference */);
         call.setStartWithSpeakerphoneOn(speakerphoneOn);
 
         // TODO(santoscordon): Move this to be a part of addCall()
@@ -474,7 +476,8 @@
         // particular the original call's call service will be updated to the new call's call
         // service.
         Call tempCall = new Call(
-                originalCall.getHandoffHandle(), originalCall.getGatewayInfo(), false, false);
+                originalCall.getHandoffHandle(), originalCall.getGatewayInfo(),
+                originalCall.getSubscription(), false, false);
         tempCall.setOriginalCall(originalCall);
         tempCall.setExtras(originalCall.getExtras());
         mPendingHandoffCalls.add(tempCall);
diff --git a/src/com/android/telecomm/InCallController.java b/src/com/android/telecomm/InCallController.java
index e971c81..bd41dcc 100644
--- a/src/com/android/telecomm/InCallController.java
+++ b/src/com/android/telecomm/InCallController.java
@@ -310,8 +310,8 @@
 
         return new InCallCall(callId, state, call.getDisconnectCause(), call.getDisconnectMessage(),
                 call.getCannedSmsResponses(), capabilities, connectTimeMillis, call.getHandle(),
-                call.getGatewayInfo(), descriptor, call.getHandoffCallServiceDescriptor(),
-                parentCallId, childCallIds);
+                call.getGatewayInfo(), call.getSubscription(), descriptor,
+                call.getHandoffCallServiceDescriptor(), parentCallId, childCallIds);
     }
 
 }
diff --git a/src/com/android/telecomm/NewOutgoingCallIntentBroadcaster.java b/src/com/android/telecomm/NewOutgoingCallIntentBroadcaster.java
index e080191..c18dbc5 100644
--- a/src/com/android/telecomm/NewOutgoingCallIntentBroadcaster.java
+++ b/src/com/android/telecomm/NewOutgoingCallIntentBroadcaster.java
@@ -38,6 +38,7 @@
 import android.os.UserHandle;
 import android.provider.ContactsContract;
 import android.telecomm.GatewayInfo;
+import android.telecomm.Subscription;
 import android.telecomm.TelecommConstants;
 import android.telephony.PhoneNumberUtils;
 import android.telephony.TelephonyManager;
@@ -134,7 +135,9 @@
             }
 
             GatewayInfo gatewayInfo = getGateWayInfoFromIntent(intent, resultHandleUri);
+            Subscription subscription = getSubscriptionFromIntent(intent);
             mCallsManager.placeOutgoingCall(resultHandleUri, mContactInfo, gatewayInfo,
+                    subscription,
                     mIntent.getBooleanExtra(TelecommConstants.EXTRA_START_CALL_WITH_SPEAKERPHONE,
                             false));
         }
@@ -203,7 +206,7 @@
                     + " OutgoingCallBroadcastReceiver: %s", intent);
             String scheme = isUriNumber ? SCHEME_SIP : SCHEME_TEL;
             mCallsManager.placeOutgoingCall(
-                    Uri.fromParts(scheme, handle, null), mContactInfo, null,
+                    Uri.fromParts(scheme, handle, null), mContactInfo, null, null,
                     mIntent.getBooleanExtra(TelecommConstants.EXTRA_START_CALL_WITH_SPEAKERPHONE,
                             false));
 
@@ -241,7 +244,7 @@
         broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
         Log.v(this, "Broadcasting intent: %s.", broadcastIntent);
 
-        checkAndCopyGatewayProviderExtras(originalCallIntent, broadcastIntent);
+        checkAndCopyProviderExtras(originalCallIntent, broadcastIntent);
 
         context.sendOrderedBroadcastAsUser(
                 broadcastIntent,
@@ -261,7 +264,10 @@
      * @param src Intent which may contain the provider's extras.
      * @param dst Intent where a copy of the extras will be added if applicable.
      */
-    public void checkAndCopyGatewayProviderExtras(Intent src, Intent dst) {
+    public void checkAndCopyProviderExtras(Intent src, Intent dst) {
+        if (src == null) {
+            return;
+        }
         if (hasGatewayProviderExtras(src)) {
             dst.putExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE,
                     src.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE));
@@ -270,8 +276,14 @@
             Log.d(this, "Found and copied gateway provider extras to broadcast intent.");
             return;
         }
+        Subscription extraSubscription = src.getParcelableExtra(
+                TelephonyManager.EXTRA_SUBSCRIPTION);
+        if (extraSubscription != null) {
+            dst.putExtra(TelephonyManager.EXTRA_SUBSCRIPTION, extraSubscription);
+            Log.d(this, "Found and copied subscription extra to broadcast intent.");
+        }
 
-        Log.d(this, "No gateway provider extras found in call intent.");
+        Log.d(this, "No provider extras found in call intent.");
     }
 
     /**
@@ -281,9 +293,6 @@
      * @return true if the intent has all the gateway information extras needed.
      */
     private boolean hasGatewayProviderExtras(Intent intent) {
-        if (intent == null) {
-            return false;
-        }
         final String name = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
         final String uriString = intent.getStringExtra(EXTRA_GATEWAY_URI);
 
@@ -317,6 +326,20 @@
         return null;
     }
 
+    /**
+     * Extracts subscription/connection provider information from a provided intent..
+     *
+     * @param intent to extract subscription information from.
+     * @return Subscription object containing extracted subscription information
+     */
+    public static Subscription getSubscriptionFromIntent(Intent intent) {
+        if (intent == null) {
+            return null;
+        }
+
+        return intent.getParcelableExtra(TelephonyManager.EXTRA_SUBSCRIPTION);
+    }
+
     private void launchSystemDialer(Context context, Uri handle) {
         Intent systemDialerIntent = new Intent();
         final Resources resources = context.getResources();