Use NEW_TASK flag for launching in-call UI

To make Intent creation consistent, prepare relevant methods in
ContactsUtils and use them everywhere.

Bug: 5691827
Change-Id: I41509bfc7d914ba87ab614450b53fc93ce0eba4b
diff --git a/src/com/android/contacts/CallContactActivity.java b/src/com/android/contacts/CallContactActivity.java
index b7c472a..793770b 100644
--- a/src/com/android/contacts/CallContactActivity.java
+++ b/src/com/android/contacts/CallContactActivity.java
@@ -18,10 +18,8 @@
 
 import com.android.contacts.interactions.PhoneNumberInteraction;
 
-import android.app.Dialog;
 import android.content.DialogInterface;
 import android.content.DialogInterface.OnDismissListener;
-import android.content.Intent;
 import android.net.Uri;
 import android.os.Bundle;
 import android.provider.ContactsContract.Contacts;
@@ -50,7 +48,7 @@
         if (Contacts.CONTENT_ITEM_TYPE.equals(getContentResolver().getType(contactUri))) {
             PhoneNumberInteraction.startInteractionForPhoneCall(this, contactUri);
         } else {
-            startActivity(new Intent(Intent.ACTION_CALL_PRIVILEGED, contactUri));
+            startActivity(ContactsUtils.getCallIntent(contactUri));
             finish();
         }
     }
diff --git a/src/com/android/contacts/CallDetailActivity.java b/src/com/android/contacts/CallDetailActivity.java
index 6224d19..ac26494 100644
--- a/src/com/android/contacts/CallDetailActivity.java
+++ b/src/com/android/contacts/CallDetailActivity.java
@@ -332,9 +332,8 @@
                 TelephonyManager tm = (TelephonyManager)
                         getSystemService(Context.TELEPHONY_SERVICE);
                 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
-                    Intent callIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
-                            Uri.fromParts("tel", mNumber, null));
-                    startActivity(callIntent);
+                    startActivity(ContactsUtils.getCallIntent(
+                            Uri.fromParts("tel", mNumber, null)));
                     return true;
                 }
             }
@@ -389,7 +388,6 @@
                 mPhoneCallDetailsHelper.setCallDetailsHeader(mHeaderTextView, firstDetails);
 
                 // Cache the details about the phone number.
-                final Uri numberCallUri = mPhoneNumberHelper.getCallUri(mNumber);
                 final boolean canPlaceCallsTo = mPhoneNumberHelper.canPlaceCallsTo(mNumber);
                 final boolean isVoicemailNumber = mPhoneNumberHelper.isVoicemailNumber(mNumber);
                 final boolean isSipNumber = mPhoneNumberHelper.isSipNumber(mNumber);
@@ -472,8 +470,8 @@
                     ViewEntry entry = new ViewEntry(
                             getString(R.string.menu_callNumber,
                                     FormatUtils.forceLeftToRight(displayNumber)),
-                            new Intent(Intent.ACTION_CALL_PRIVILEGED, numberCallUri),
-                            getString(R.string.description_call, nameOrNumber));
+                                    ContactsUtils.getCallIntent(mNumber),
+                                    getString(R.string.description_call, nameOrNumber));
 
                     // Only show a label if the number is shown and it is not a SIP address.
                     if (!TextUtils.isEmpty(firstDetails.name)
@@ -773,7 +771,7 @@
     }
 
     public void onMenuEditNumberBeforeCall(MenuItem menuItem) {
-        startActivity(new Intent(Intent.ACTION_DIAL, mPhoneNumberHelper.getCallUri(mNumber)));
+        startActivity(new Intent(Intent.ACTION_DIAL, ContactsUtils.getCallUri(mNumber)));
     }
 
     public void onMenuTrashVoicemail(MenuItem menuItem) {
diff --git a/src/com/android/contacts/ContactsUtils.java b/src/com/android/contacts/ContactsUtils.java
index cb19713..f7afd22 100644
--- a/src/com/android/contacts/ContactsUtils.java
+++ b/src/com/android/contacts/ContactsUtils.java
@@ -16,6 +16,8 @@
 
 package com.android.contacts;
 
+import com.android.contacts.activities.DialtactsActivity;
+import com.android.contacts.calllog.PhoneNumberHelper;
 import com.android.contacts.model.AccountType;
 import com.android.contacts.model.AccountTypeManager;
 import com.android.contacts.model.AccountWithDataSet;
@@ -30,6 +32,7 @@
 import android.provider.ContactsContract;
 import android.provider.ContactsContract.CommonDataKinds.Im;
 import android.provider.ContactsContract.CommonDataKinds.Phone;
+import android.provider.ContactsContract.QuickContact;
 import android.telephony.PhoneNumberUtils;
 import android.text.TextUtils;
 import android.view.View;
@@ -213,6 +216,67 @@
     }
 
     /**
+     * Return Uri with an appropriate scheme, accepting Voicemail, SIP, and usual phone call
+     * numbers.
+     */
+    public static Uri getCallUri(String number) {
+        if (PhoneNumberUtils.isVoiceMailNumber(number)) {
+            return Uri.parse("voicemail:");
+        }
+        if (PhoneNumberUtils.isUriNumber(number)) {
+             return Uri.fromParts("sip", number, null);
+        }
+        return Uri.fromParts("tel", number, null);
+     }
+
+    /**
+     * Return an Intent for making a phone call. Scheme (e.g. tel, sip) will be determined
+     * automatically.
+     */
+    public static Intent getCallIntent(String number) {
+        return getCallIntent(number, null);
+    }
+
+    /**
+     * Return an Intent for making a phone call. A given Uri will be used as is (without any
+     * sanity check).
+     */
+    public static Intent getCallIntent(Uri uri) {
+        return getCallIntent(uri, null);
+    }
+
+    /**
+     * A variant of {@link #getCallIntent(String)} but also accept a call origin. For more
+     * information about call origin, see comments in Phone package (PhoneApp).
+     */
+    public static Intent getCallIntent(String number, String callOrigin) {
+        return getCallIntent(getCallUri(number), callOrigin);
+    }
+
+    /**
+     * A variant of {@link #getCallIntent(Uri)} but also accept a call origin. For more
+     * information about call origin, see comments in Phone package (PhoneApp).
+     */
+    public static Intent getCallIntent(Uri uri, String callOrigin) {
+        final Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, uri);
+        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+        if (callOrigin != null) {
+            intent.putExtra(DialtactsActivity.EXTRA_CALL_ORIGIN, callOrigin);
+        }
+        return intent;
+    }
+
+    /**
+     * Return an Intent for launching voicemail screen.
+     */
+    public static Intent getVoicemailIntent() {
+        final Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
+                Uri.fromParts("voicemail", "", null));
+        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+        return intent;
+    }
+
+    /**
      * Returns a header view based on the R.layout.list_separator, where the
      * containing {@link TextView} is set using the given textResourceId.
      */
diff --git a/src/com/android/contacts/calllog/CallLogFragment.java b/src/com/android/contacts/calllog/CallLogFragment.java
index 40fa5bc..ec94209 100644
--- a/src/com/android/contacts/calllog/CallLogFragment.java
+++ b/src/com/android/contacts/calllog/CallLogFragment.java
@@ -329,8 +329,7 @@
             Intent intent;
             // If "number" is really a SIP address, construct a sip: URI.
             if (PhoneNumberUtils.isUriNumber(number)) {
-                intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
-                                    Uri.fromParts("sip", number, null));
+                intent = ContactsUtils.getCallIntent(Uri.fromParts("sip", number, null));
             } else {
                 // We're calling a regular PSTN phone number.
                 // Construct a tel: URI, but do some other possible cleanup first.
@@ -342,8 +341,7 @@
                     String countryIso = cursor.getString(CallLogQuery.COUNTRY_ISO);
                     number = mAdapter.getBetterNumberFromContacts(number, countryIso);
                 }
-                intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
-                                    Uri.fromParts("tel", number, null));
+                intent = ContactsUtils.getCallIntent(Uri.fromParts("tel", number, null));
             }
             intent.setFlags(
                     Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
diff --git a/src/com/android/contacts/calllog/CallLogListItemHelper.java b/src/com/android/contacts/calllog/CallLogListItemHelper.java
index bfedba5..9485b54 100644
--- a/src/com/android/contacts/calllog/CallLogListItemHelper.java
+++ b/src/com/android/contacts/calllog/CallLogListItemHelper.java
@@ -45,7 +45,7 @@
     public CallLogListItemHelper(PhoneCallDetailsHelper phoneCallDetailsHelper,
             PhoneNumberHelper phoneNumberHelper, Resources resources) {
         mPhoneCallDetailsHelper = phoneCallDetailsHelper;
-        mPhoneNumberHelper= phoneNumberHelper;
+        mPhoneNumberHelper = phoneNumberHelper;
         mResources = resources;
     }
 
diff --git a/src/com/android/contacts/calllog/IntentProvider.java b/src/com/android/contacts/calllog/IntentProvider.java
index bfee5ec..7ddfecc 100644
--- a/src/com/android/contacts/calllog/IntentProvider.java
+++ b/src/com/android/contacts/calllog/IntentProvider.java
@@ -17,6 +17,7 @@
 package com.android.contacts.calllog;
 
 import com.android.contacts.CallDetailActivity;
+import com.android.contacts.ContactsUtils;
 
 import android.content.ContentUris;
 import android.content.Context;
@@ -38,16 +39,7 @@
         return new IntentProvider() {
             @Override
             public Intent getIntent(Context context) {
-                // Here, "number" can either be a PSTN phone number or a
-                // SIP address.  So turn it into either a tel: URI or a
-                // sip: URI, as appropriate.
-                Uri uri;
-                if (PhoneNumberUtils.isUriNumber(number)) {
-                    uri = Uri.fromParts("sip", number, null);
-                } else {
-                    uri = Uri.fromParts("tel", number, null);
-                }
-                return new Intent(Intent.ACTION_CALL_PRIVILEGED, uri);
+                return ContactsUtils.getCallIntent(number);
             }
         };
     }
diff --git a/src/com/android/contacts/calllog/PhoneNumberHelper.java b/src/com/android/contacts/calllog/PhoneNumberHelper.java
index 20031b2..af7c2f6 100644
--- a/src/com/android/contacts/calllog/PhoneNumberHelper.java
+++ b/src/com/android/contacts/calllog/PhoneNumberHelper.java
@@ -16,6 +16,7 @@
 
 package com.android.contacts.calllog;
 
+import com.android.contacts.ContactsUtils;
 import com.android.contacts.R;
 import com.android.internal.telephony.CallerInfo;
 
@@ -76,23 +77,18 @@
         }
     }
 
-    /** Returns a URI that can be used to place a call to this number. */
-    public Uri getCallUri(String number) {
-        if (isVoicemailNumber(number)) {
-            return Uri.parse("voicemail:x");
-        }
-        if (isSipNumber(number)) {
-             return Uri.fromParts("sip", number, null);
-        }
-         return Uri.fromParts("tel", number, null);
-     }
-
-    /** Returns true if the given number is the number of the configured voicemail. */
+    /**
+     * Returns true if the given number is the number of the configured voicemail.
+     * To be able to mock-out this, it is not a static method.
+     */
     public boolean isVoicemailNumber(CharSequence number) {
         return PhoneNumberUtils.isVoiceMailNumber(number.toString());
     }
 
-    /** Returns true if the given number is a SIP address. */
+    /**
+     * Returns true if the given number is a SIP address.
+     * To be able to mock-out this, it is not a static method.
+     */
     public boolean isSipNumber(CharSequence number) {
         return PhoneNumberUtils.isUriNumber(number.toString());
     }
diff --git a/src/com/android/contacts/detail/ContactDetailFragment.java b/src/com/android/contacts/detail/ContactDetailFragment.java
index 577062e..f3d7451 100644
--- a/src/com/android/contacts/detail/ContactDetailFragment.java
+++ b/src/com/android/contacts/detail/ContactDetailFragment.java
@@ -609,8 +609,8 @@
                             entryValues.getAsString(PhoneLookup.NORMALIZED_NUMBER);
                     entry.data = PhoneNumberUtils.formatNumber(
                             entry.data, phoneNumberE164, mDefaultCountryIso);
-                    final Intent phoneIntent = mHasPhone ? new Intent(Intent.ACTION_CALL_PRIVILEGED,
-                            Uri.fromParts(Constants.SCHEME_TEL, entry.data, null)) : null;
+                    final Intent phoneIntent = mHasPhone ?
+                            ContactsUtils.getCallIntent(entry.data) : null;
                     final Intent smsIntent = mHasSms ? new Intent(Intent.ACTION_SENDTO,
                             Uri.fromParts(Constants.SCHEME_SMSTO, entry.data, null)) : null;
 
@@ -706,7 +706,7 @@
                     entry.uri = null;
                     entry.maxLines = SIP_ADDRESS_MAX_LINES;
                     if (mHasSip) {
-                        entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
+                        entry.intent = ContactsUtils.getCallIntent(
                                 Uri.fromParts(Constants.SCHEME_SIP, entry.data, null));
                     } else {
                         entry.intent = null;
@@ -1937,9 +1937,7 @@
                     }
                 } else if (mPrimaryPhoneUri != null) {
                     // There isn't anything selected, call the default number
-                    final Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
-                            mPrimaryPhoneUri);
-                    mContext.startActivity(intent);
+                    mContext.startActivity(ContactsUtils.getCallIntent(mPrimaryPhoneUri));
                     return true;
                 }
                 return false;
diff --git a/src/com/android/contacts/dialpad/DialpadFragment.java b/src/com/android/contacts/dialpad/DialpadFragment.java
index 41f713d..6de7851 100644
--- a/src/com/android/contacts/dialpad/DialpadFragment.java
+++ b/src/com/android/contacts/dialpad/DialpadFragment.java
@@ -829,7 +829,7 @@
     }
 
     public void callVoicemail() {
-        startActivity(newVoicemailIntent());
+        startActivity(ContactsUtils.getVoicemailIntent());
         mDigits.getText().clear(); // TODO: Fix bug 1745781
         getActivity().finish();
     }
@@ -958,11 +958,9 @@
                 // Clear the digits just in case.
                 mDigits.getText().clear();
             } else {
-                final Intent intent = newDialNumberIntent(number);
-                if (getActivity() instanceof DialtactsActivity) {
-                    intent.putExtra(DialtactsActivity.EXTRA_CALL_ORIGIN,
-                            DialtactsActivity.CALL_ORIGIN_DIALTACTS);
-                }
+                final Intent intent = ContactsUtils.getCallIntent(number,
+                        (getActivity() instanceof DialtactsActivity ?
+                                DialtactsActivity.CALL_ORIGIN_DIALTACTS : null));
                 startActivity(intent);
                 mDigits.getText().clear();  // TODO: Fix bug 1745781
                 getActivity().finish();
@@ -1420,27 +1418,12 @@
         mCallLog.getLastOutgoingCall(lastCallArgs);
     }
 
-    // Helpers for the call intents.
-    private Intent newVoicemailIntent() {
-        final Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
-                                         Uri.fromParts("voicemail", EMPTY_NUMBER, null));
-        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-        return intent;
-    }
-
     private Intent newFlashIntent() {
-        final Intent intent = newDialNumberIntent(EMPTY_NUMBER);
+        final Intent intent = ContactsUtils.getCallIntent(EMPTY_NUMBER);
         intent.putExtra(EXTRA_SEND_EMPTY_FLASH, true);
         return intent;
     }
 
-    private Intent newDialNumberIntent(String number) {
-        final Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
-                                         Uri.fromParts("tel", number, null));
-        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-        return intent;
-    }
-
     public void setListener(Listener listener) {
         mListener = listener;
     }
diff --git a/src/com/android/contacts/interactions/PhoneNumberInteraction.java b/src/com/android/contacts/interactions/PhoneNumberInteraction.java
index 4033c71..401cbb1 100644
--- a/src/com/android/contacts/interactions/PhoneNumberInteraction.java
+++ b/src/com/android/contacts/interactions/PhoneNumberInteraction.java
@@ -322,11 +322,7 @@
                         Intent.ACTION_SENDTO, Uri.fromParts("sms", phoneNumber, null));
                 break;
             default:
-                intent = new Intent(
-                        Intent.ACTION_CALL_PRIVILEGED, Uri.fromParts("tel", phoneNumber, null));
-                if (callOrigin != null) {
-                    intent.putExtra(DialtactsActivity.EXTRA_CALL_ORIGIN, callOrigin);
-                }
+                intent = ContactsUtils.getCallIntent(phoneNumber, callOrigin);
                 break;
         }
         context.startActivity(intent);
diff --git a/src/com/android/contacts/list/ContactsIntentResolver.java b/src/com/android/contacts/list/ContactsIntentResolver.java
index 8d14591..0584f5e 100644
--- a/src/com/android/contacts/list/ContactsIntentResolver.java
+++ b/src/com/android/contacts/list/ContactsIntentResolver.java
@@ -18,6 +18,7 @@
 
 import com.android.contacts.CallContactActivity;
 import com.android.contacts.ContactsSearchManager;
+import com.android.contacts.ContactsUtils;
 
 import android.app.Activity;
 import android.app.SearchManager;
@@ -128,9 +129,7 @@
             if ("call".equals(intent.getStringExtra(SearchManager.ACTION_MSG))) {
                 String query = intent.getStringExtra(SearchManager.QUERY);
                 if (!TextUtils.isEmpty(query)) {
-                    Intent newIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
-                            Uri.fromParts("tel", query, null));
-                    request.setRedirectIntent(newIntent);
+                    request.setRedirectIntent(ContactsUtils.getCallIntent(query));
                 }
             } else {
                 request.setQueryString(intent.getStringExtra(SearchManager.QUERY));
@@ -182,7 +181,7 @@
                 intent.setData(null);
             }
         } else if (Intents.SEARCH_SUGGESTION_DIAL_NUMBER_CLICKED.equals(action)) {
-            request.setRedirectIntent(new Intent(Intent.ACTION_CALL_PRIVILEGED, intent.getData()));
+            request.setRedirectIntent(ContactsUtils.getCallIntent(intent.getData()));
         } else if (Intents.SEARCH_SUGGESTION_CREATE_CONTACT_CLICKED.equals(action)) {
             // TODO actually support this in EditContactActivity.
             String number = intent.getData().getSchemeSpecificPart();
diff --git a/src/com/android/contacts/quickcontact/DataAction.java b/src/com/android/contacts/quickcontact/DataAction.java
index 266fd02..415fa18 100644
--- a/src/com/android/contacts/quickcontact/DataAction.java
+++ b/src/com/android/contacts/quickcontact/DataAction.java
@@ -116,8 +116,8 @@
                 final String number = getAsString(cursor, Phone.NUMBER);
                 if (!TextUtils.isEmpty(number)) {
 
-                    final Intent phoneIntent = hasPhone ? new Intent(Intent.ACTION_CALL_PRIVILEGED,
-                            Uri.fromParts(Constants.SCHEME_TEL, number, null)) : null;
+                    final Intent phoneIntent = hasPhone ? ContactsUtils.getCallIntent(number)
+                            : null;
                     final Intent smsIntent = hasSms ? new Intent(Intent.ACTION_SENDTO,
                             Uri.fromParts(Constants.SCHEME_SMSTO, number, null)) : null;
 
@@ -139,7 +139,7 @@
                 final String address = getAsString(cursor, SipAddress.SIP_ADDRESS);
                 if (!TextUtils.isEmpty(address)) {
                     final Uri callUri = Uri.fromParts(Constants.SCHEME_SIP, address, null);
-                    mIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED, callUri);
+                    mIntent = ContactsUtils.getCallIntent(callUri);
                     // Note that this item will get a SIP-specific variant
                     // of the "call phone" icon, rather than the standard
                     // app icon for the Phone app (which we show for