Merge "Remove Guava references from Telecom." into lmp-mr1-dev
diff --git a/src/com/android/server/telecom/AsyncRingtonePlayer.java b/src/com/android/server/telecom/AsyncRingtonePlayer.java
index 4434424..3030fea 100644
--- a/src/com/android/server/telecom/AsyncRingtonePlayer.java
+++ b/src/com/android/server/telecom/AsyncRingtonePlayer.java
@@ -196,7 +196,9 @@
         }
 
         Ringtone ringtone = RingtoneManager.getRingtone(mContext, ringtoneUri);
-        ringtone.setStreamType(AudioManager.STREAM_RING);
+        if (ringtone != null) {
+            ringtone.setStreamType(AudioManager.STREAM_RING);
+        }
         return ringtone;
     }
 }
diff --git a/src/com/android/server/telecom/Call.java b/src/com/android/server/telecom/Call.java
index 67ff03a..47b4aa6f 100644
--- a/src/com/android/server/telecom/Call.java
+++ b/src/com/android/server/telecom/Call.java
@@ -189,6 +189,12 @@
      */
     private long mCreationTimeMillis = System.currentTimeMillis();
 
+    /** The time this call was made active. */
+    private long mConnectTimeMillis = 0;
+
+    /** The time this call was disconnected. */
+    private long mDisconnectTimeMillis = 0;
+
     /** The gateway information associated with this call. This stores the original call handle
      * that the user is attempting to connect to via the gateway, the actual handle to dial in
      * order to connect the call via the gateway, as well as the package name of the gateway
@@ -203,8 +209,6 @@
 
     private final List<Call> mConferenceableCalls = new ArrayList<>();
 
-    private long mConnectTimeMillis = 0;
-
     /** The state of the call. */
     private int mState;
 
@@ -386,7 +390,18 @@
             mState = newState;
             maybeLoadCannedSmsResponses();
 
-            if (mState == CallState.DISCONNECTED) {
+            if (mState == CallState.ACTIVE || mState == CallState.ON_HOLD) {
+                if (mConnectTimeMillis == 0) {
+                    // We check to see if mConnectTime is already set to prevent the
+                    // call from resetting active time when it goes in and out of
+                    // ACTIVE/ON_HOLD
+                    mConnectTimeMillis = System.currentTimeMillis();
+                }
+
+                // We're clearly not disconnected, so reset the disconnected time.
+                mDisconnectTimeMillis = 0;
+            } else if (mState == CallState.DISCONNECTED) {
+                mDisconnectTimeMillis = System.currentTimeMillis();
                 setLocallyDisconnecting(false);
                 fixParentAfterDisconnect();
                 if ((oldState == CallState.DIALING || oldState == CallState.CONNECTING)
@@ -559,7 +574,21 @@
      *     mCreationTimeMillis.
      */
     long getAgeMillis() {
-        return System.currentTimeMillis() - mCreationTimeMillis;
+        if (mState == CallState.DISCONNECTED &&
+                (mDisconnectCause.getCode() == DisconnectCause.REJECTED ||
+                 mDisconnectCause.getCode() == DisconnectCause.MISSED)) {
+            // Rejected and missed calls have no age. They're immortal!!
+            return 0;
+        } else if (mConnectTimeMillis == 0) {
+            // Age is measured in the amount of time the call was active. A zero connect time
+            // indicates that we never went active, so return 0 for the age.
+            return 0;
+        } else if (mDisconnectTimeMillis == 0) {
+            // We connected, but have not yet disconnected
+            return System.currentTimeMillis() - mConnectTimeMillis;
+        }
+
+        return mDisconnectTimeMillis - mConnectTimeMillis;
     }
 
     /**
@@ -578,10 +607,6 @@
         return mConnectTimeMillis;
     }
 
-    void setConnectTimeMillis(long connectTimeMillis) {
-        mConnectTimeMillis = connectTimeMillis;
-    }
-
     int getCallCapabilities() {
         return mCallCapabilities;
     }
diff --git a/src/com/android/server/telecom/CallLogManager.java b/src/com/android/server/telecom/CallLogManager.java
index 8764a04..781f2b1 100755
--- a/src/com/android/server/telecom/CallLogManager.java
+++ b/src/com/android/server/telecom/CallLogManager.java
@@ -140,13 +140,12 @@
 
         Log.d(TAG, "logNumber set to: %s", Log.pii(logNumber));
 
-        final int presentation = getPresentation(call);
         final PhoneAccountHandle accountHandle = call.getTargetPhoneAccount();
 
         // TODO(vt): Once data usage is available, wire it up here.
         int callFeatures = getCallFeatures(call.getVideoStateHistory());
-        logCall(call.getCallerInfo(), logNumber, presentation, callLogType, callFeatures,
-                accountHandle, creationTime, age, null);
+        logCall(call.getCallerInfo(), logNumber, call.getHandlePresentation(),
+                callLogType, callFeatures, accountHandle, creationTime, age, null);
     }
 
     /**
@@ -232,21 +231,6 @@
     }
 
     /**
-     * Gets the presentation from the {@link Call}.
-     *
-     * TODO: There needs to be a way to pass information from
-     * Connection.getNumberPresentation() into a {@link Call} object. Until then, always return
-     * PhoneConstants.PRESENTATION_ALLOWED. On top of that, we might need to introduce
-     * getNumberPresentation to the ContactInfo object as well.
-     *
-     * @param call The call object to retrieve caller details from.
-     * @return The number presentation constant to insert into the call logs.
-     */
-    private int getPresentation(Call call) {
-        return PhoneConstants.PRESENTATION_ALLOWED;
-    }
-
-    /**
      * Adds the call defined by the parameters in the provided AddCallArgs to the CallLogProvider
      * using an AsyncTask to avoid blocking the main thread.
      *
diff --git a/src/com/android/server/telecom/CallsManager.java b/src/com/android/server/telecom/CallsManager.java
index eb5b7ab..84bdff8 100644
--- a/src/com/android/server/telecom/CallsManager.java
+++ b/src/com/android/server/telecom/CallsManager.java
@@ -406,7 +406,6 @@
                 // to the existing connection instead of trying to create a new one.
                 true /* isIncoming */,
                 false /* isConference */);
-        call.setConnectTimeMillis(System.currentTimeMillis());
         call.setIsUnknown(true);
         call.setExtras(extras);
         call.addListener(this);
@@ -799,9 +798,6 @@
     }
 
     void markCallAsActive(Call call) {
-        if (call.getConnectTimeMillis() == 0) {
-            call.setConnectTimeMillis(System.currentTimeMillis());
-        }
         setCallState(call, CallState.ACTIVE);
 
         if (call.getStartWithSpeakerphoneOn()) {
@@ -993,9 +989,6 @@
                 true /* isConference */);
 
         setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState()));
-        if (call.getState() == CallState.ACTIVE) {
-            call.setConnectTimeMillis(System.currentTimeMillis());
-        }
         call.setCallCapabilities(parcelableConference.getCapabilities());
 
         // TODO: Move this to be a part of addCall()
@@ -1339,7 +1332,6 @@
                 false /* isConference */);
 
         setCallState(call, Call.getStateFromConnectionState(connection.getState()));
-        call.setConnectTimeMillis(System.currentTimeMillis());
         call.setCallCapabilities(connection.getCapabilities());
         call.setCallerDisplayName(connection.getCallerDisplayName(),
                 connection.getCallerDisplayNamePresentation());
diff --git a/src/com/android/server/telecom/ConnectionServiceWrapper.java b/src/com/android/server/telecom/ConnectionServiceWrapper.java
index cb7a1fe..b57d86f 100644
--- a/src/com/android/server/telecom/ConnectionServiceWrapper.java
+++ b/src/com/android/server/telecom/ConnectionServiceWrapper.java
@@ -200,20 +200,6 @@
                         ParcelableConference parcelableConference =
                                 (ParcelableConference) args.arg2;
 
-                        // Make sure that there's at least one valid call. For remote connections
-                        // we'll get a add conference msg from both the remote connection service
-                        // and from the real connection service.
-                        boolean hasValidCalls = false;
-                        for (String callId : parcelableConference.getConnectionIds()) {
-                            if (mCallIdMapper.getCall(callId) != null) {
-                                hasValidCalls = true;
-                            }
-                        }
-                        if (!hasValidCalls) {
-                            Log.d(this, "Attempting to add a conference with no valid calls");
-                            break;
-                        }
-
                         // need to create a new Call
                         Call conferenceCall = mCallsManager.createConferenceCall(
                                 null, parcelableConference);
diff --git a/src/com/android/server/telecom/InCallAdapter.java b/src/com/android/server/telecom/InCallAdapter.java
index d4af791..e39b5a5 100644
--- a/src/com/android/server/telecom/InCallAdapter.java
+++ b/src/com/android/server/telecom/InCallAdapter.java
@@ -102,8 +102,6 @@
                     break;
                 case MSG_POST_DIAL_CONTINUE:
                     call = mCallIdMapper.getCall(msg.obj);
-                    mCallsManager.postDialContinue(call, msg.arg1 == 1);
-                    call = mCallIdMapper.getCall(msg.obj);
                     if (call != null) {
                         mCallsManager.postDialContinue(call, msg.arg1 == 1);
                     } else {
diff --git a/src/com/android/server/telecom/InCallController.java b/src/com/android/server/telecom/InCallController.java
index 9bb2826..d3cc232 100644
--- a/src/com/android/server/telecom/InCallController.java
+++ b/src/com/android/server/telecom/InCallController.java
@@ -443,7 +443,7 @@
      *
      * @param call The {@link Call} to parcel.
      * @param includeVideoProvider When {@code true}, the {@link IVideoProvider} is included in the
-     *      parcelled call.  When {@code false}, the {@link IVideoProvider} is not included.
+     *      parceled call.  When {@code false}, the {@link IVideoProvider} is not included.
      * @return The {@link ParcelableCall} containing all call information from the {@link Call}.
      */
     private ParcelableCall toParcelableCall(Call call, boolean includeVideoProvider) {
@@ -452,7 +452,10 @@
         int state = call.getState();
         int capabilities = call.getCallCapabilities();
 
-        if (call.isRespondViaSmsCapable()) {
+        boolean isDefaultSmsAccount =
+                CallsManager.getInstance().getPhoneAccountRegistrar().isUserSelectedSmsPhoneAccount(
+                        call.getTargetPhoneAccount());
+        if (call.isRespondViaSmsCapable() && isDefaultSmsAccount) {
             capabilities |= PhoneCapabilities.RESPOND_VIA_TEXT;
         }
 
diff --git a/src/com/android/server/telecom/NewOutgoingCallIntentBroadcaster.java b/src/com/android/server/telecom/NewOutgoingCallIntentBroadcaster.java
index b0495ac..c52f2bb 100644
--- a/src/com/android/server/telecom/NewOutgoingCallIntentBroadcaster.java
+++ b/src/com/android/server/telecom/NewOutgoingCallIntentBroadcaster.java
@@ -173,7 +173,8 @@
 
         boolean isVoicemailNumber = PhoneAccount.SCHEME_VOICEMAIL.equals(handle.getScheme());
         if (isVoicemailNumber) {
-            if (Intent.ACTION_CALL.equals(action)) {
+            if (Intent.ACTION_CALL.equals(action)
+                    || Intent.ACTION_CALL_PRIVILEGED.equals(action)) {
                 // Voicemail calls will be handled directly by the telephony connection manager
                 Log.i(this, "Placing call immediately instead of waiting for "
                         + " OutgoingCallBroadcastReceiver: %s", intent);
diff --git a/src/com/android/server/telecom/PhoneAccountRegistrar.java b/src/com/android/server/telecom/PhoneAccountRegistrar.java
index 1d7993d..ac02a88 100644
--- a/src/com/android/server/telecom/PhoneAccountRegistrar.java
+++ b/src/com/android/server/telecom/PhoneAccountRegistrar.java
@@ -92,6 +92,7 @@
     private final List<Listener> mListeners = new CopyOnWriteArrayList<>();
     private final AtomicFile mAtomicFile;
     private final Context mContext;
+    private final SubscriptionManager mSubscriptionManager;
     private State mState;
 
     @VisibleForTesting
@@ -113,6 +114,7 @@
 
         mState = new State();
         mContext = context;
+        mSubscriptionManager = SubscriptionManager.from(mContext);
         read();
     }
 
@@ -130,7 +132,7 @@
                 !TextUtils.isDigitsOnly(accountHandle.getId())) {
             // Since no decimals or negative numbers can be valid subscription ids, only a string of
             // numbers can be subscription id
-            return SubscriptionManager.INVALID_SUB_ID;
+            return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
         }
         return Integer.parseInt(accountHandle.getId());
     }
@@ -212,7 +214,7 @@
                 // If the account selected is a SIM account, propagate down to the subscription
                 // record.
                 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
-                SubscriptionManager.setDefaultVoiceSubId(subId);
+                mSubscriptionManager.setDefaultVoiceSubId(subId);
             }
 
             mState.defaultOutgoing = accountHandle;
@@ -222,6 +224,11 @@
         fireDefaultOutgoingChanged();
     }
 
+    boolean isUserSelectedSmsPhoneAccount(PhoneAccountHandle accountHandle) {
+        return getSubscriptionIdForPhoneAccount(accountHandle) ==
+                SubscriptionManager.getDefaultSmsSubId();
+    }
+
     public void setSimCallManager(PhoneAccountHandle callManager) {
         if (callManager != null) {
             PhoneAccount callManagerAccount = getPhoneAccount(callManager);
diff --git a/src/com/android/server/telecom/RespondViaSmsManager.java b/src/com/android/server/telecom/RespondViaSmsManager.java
index ae7e713..8164df0 100644
--- a/src/com/android/server/telecom/RespondViaSmsManager.java
+++ b/src/com/android/server/telecom/RespondViaSmsManager.java
@@ -191,7 +191,7 @@
                 final Uri uri = Uri.fromParts(Constants.SCHEME_SMSTO, phoneNumber, null);
                 final Intent intent = new Intent(TelephonyManager.ACTION_RESPOND_VIA_MESSAGE, uri);
                 intent.putExtra(Intent.EXTRA_TEXT, textMessage);
-                if (subId != SubscriptionManager.INVALID_SUB_ID) {
+                if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
                     intent.putExtra(PhoneConstants.SUBSCRIPTION_KEY, subId);
                 }