am 98cf19a5: am 20c63bb4: Merge "Add "Call with a note" action." into mnc-dr-dev

* commit '98cf19a53d5f0c722def08a075854f7a52a97d83':
  Add "Call with a note" action.
diff --git a/res/layout/call_log_list_item_actions.xml b/res/layout/call_log_list_item_actions.xml
index b427206..16a712b 100644
--- a/res/layout/call_log_list_item_actions.xml
+++ b/res/layout/call_log_list_item_actions.xml
@@ -105,6 +105,20 @@
     </LinearLayout>
 
     <LinearLayout
+        android:id="@+id/call_with_note_action"
+        style="@style/CallLogActionStyle">
+
+        <ImageView
+            style="@style/CallLogActionIconStyle"
+            android:src="@drawable/ic_call_note_white_24dp" />
+
+        <TextView
+            style="@style/CallLogActionTextStyle"
+            android:text="@string/call_with_a_note" />
+
+    </LinearLayout>
+
+    <LinearLayout
         android:id="@+id/details_action"
         style="@style/CallLogActionStyle">
 
diff --git a/src/com/android/dialer/CallDetailActivity.java b/src/com/android/dialer/CallDetailActivity.java
index c6ec8e1..56f2cb1 100644
--- a/src/com/android/dialer/CallDetailActivity.java
+++ b/src/com/android/dialer/CallDetailActivity.java
@@ -50,6 +50,7 @@
 import com.android.contacts.common.util.PermissionsUtil;
 import com.android.contacts.common.GeoUtil;
 import com.android.contacts.common.CallUtil;
+import com.android.contacts.common.util.UriUtils;
 import com.android.dialer.calllog.CallDetailHistoryAdapter;
 import com.android.dialer.calllog.CallLogAsyncTaskUtil.CallLogAsyncTaskListener;
 import com.android.dialer.calllog.CallLogAsyncTaskUtil;
@@ -162,7 +163,7 @@
                     new CallDetailHistoryAdapter(mContext, mInflater, mCallTypeHelper, details));
 
             String lookupKey = contactUri == null ? null
-                    : ContactInfoHelper.getLookupKeyFromUri(contactUri);
+                    : UriUtils.getLookupKeyFromUri(contactUri);
 
             final boolean isBusiness = mContactInfoHelper.isBusiness(firstDetails.sourceType);
 
diff --git a/src/com/android/dialer/calllog/CallLogAdapter.java b/src/com/android/dialer/calllog/CallLogAdapter.java
index 85d1c38..5a87bc8 100644
--- a/src/com/android/dialer/calllog/CallLogAdapter.java
+++ b/src/com/android/dialer/calllog/CallLogAdapter.java
@@ -22,6 +22,8 @@
 import android.content.res.Resources;
 import android.database.Cursor;
 import android.net.Uri;
+import android.provider.ContactsContract;
+import android.provider.ContactsContract.CommonDataKinds.Phone;
 import android.support.v7.widget.RecyclerView;
 import android.os.Bundle;
 import android.os.Trace;
@@ -526,13 +528,16 @@
         views.rowId = c.getLong(CallLogQuery.ID);
         // Store values used when the actions ViewStub is inflated on expansion.
         views.number = number;
+        views.displayNumber = details.displayNumber;
         views.numberPresentation = numberPresentation;
         views.callType = c.getInt(CallLogQuery.CALL_TYPE);
         views.accountHandle = accountHandle;
         views.voicemailUri = c.getString(CallLogQuery.VOICEMAIL_URI);
         // Stash away the Ids of the calls so that we can support deleting a row in the call log.
         views.callIds = getCallIds(c, count);
-
+        views.isBusiness = mContactInfoHelper.isBusiness(info.sourceType);
+        views.numberType = (String) Phone.getTypeLabel(mContext.getResources(), details.numberType,
+                details.numberLabel);
         // Default case: an item in the call log.
         views.primaryActionView.setVisibility(View.VISIBLE);
 
@@ -563,7 +568,7 @@
             nameForDefaultImage = info.name;
         }
         views.setPhoto(info.photoId, info.photoUri, info.lookupUri, nameForDefaultImage,
-                isVoicemailNumber, mContactInfoHelper.isBusiness(info.sourceType));
+                isVoicemailNumber, views.isBusiness);
 
         mCallLogListItemHelper.setPhoneCallDetails(views, details);
     }
diff --git a/src/com/android/dialer/calllog/CallLogListItemViewHolder.java b/src/com/android/dialer/calllog/CallLogListItemViewHolder.java
index 361e1c7..0fa5e6d 100644
--- a/src/com/android/dialer/calllog/CallLogListItemViewHolder.java
+++ b/src/com/android/dialer/calllog/CallLogListItemViewHolder.java
@@ -16,6 +16,7 @@
 
 package com.android.dialer.calllog;
 
+import android.app.Activity;
 import android.content.Context;
 import android.content.res.Resources;
 import android.content.Intent;
@@ -29,18 +30,16 @@
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.ViewStub;
-import android.view.ViewTreeObserver;
 import android.widget.QuickContactBadge;
 import android.widget.ImageView;
 import android.widget.TextView;
 
-import com.android.contacts.common.CallUtil;
 import com.android.contacts.common.ContactPhotoManager;
 import com.android.contacts.common.ContactPhotoManager.DefaultImageRequest;
+import com.android.contacts.common.dialog.CallSubjectDialog;
 import com.android.contacts.common.testing.NeededForTesting;
 import com.android.contacts.common.util.UriUtils;
 import com.android.dialer.R;
-import com.android.dialer.calllog.CallLogAsyncTaskUtil;
 import com.android.dialer.util.DialerUtils;
 import com.android.dialer.util.PhoneNumberUtil;
 import com.android.dialer.voicemail.VoicemailPlaybackPresenter;
@@ -80,6 +79,7 @@
     public View addToExistingContactButtonView;
     public View sendMessageView;
     public View detailsButtonView;
+    public View callWithNoteButtonView;
 
     /**
      * The row Id for the first call associated with the call log entry.  Used as a key for the
@@ -100,12 +100,22 @@
     public String number;
 
     /**
+     * The formatted phone number to display.
+     */
+    public String displayNumber;
+
+    /**
      * The phone number presentation for the current call log entry.  Cached here as the call back
      * intent is set only when the actions ViewStub is inflated.
      */
     public int numberPresentation;
 
     /**
+     * The type of the phone number (e.g. main, work, etc).
+     */
+    public String numberType;
+
+    /**
      * The type of call for the current call log entry.  Cached here as the call back
      * intent is set only when the actions ViewStub is inflated.
      */
@@ -131,6 +141,11 @@
     public CharSequence nameOrNumber;
 
     /**
+     * Whether this row is for a business or not.
+     */
+    public boolean isBusiness;
+
+    /**
      * The contact info for the contact displayed in this list item.
      */
     public ContactInfo info;
@@ -245,6 +260,9 @@
 
             detailsButtonView = actionsView.findViewById(R.id.details_action);
             detailsButtonView.setOnClickListener(this);
+
+            callWithNoteButtonView = actionsView.findViewById(R.id.call_with_note_action);
+            callWithNoteButtonView.setOnClickListener(this);
         }
 
         bindActionButtons();
@@ -349,6 +367,13 @@
         sendMessageView.setTag(IntentProvider.getSendSmsIntentProvider(number));
 
         mCallLogListItemHelper.setActionContentDescriptions(this);
+
+        boolean supportsCallSubject =
+                mTelecomCallLogCache.doesAccountSupportCallSubject(accountHandle);
+        boolean isVoicemailNumber =
+                mTelecomCallLogCache.isVoicemailNumber(accountHandle, number);
+        callWithNoteButtonView.setVisibility(
+                supportsCallSubject && !isVoicemailNumber ? View.VISIBLE : View.GONE);
     }
 
     /**
@@ -403,7 +428,7 @@
 
         String lookupKey = null;
         if (contactUri != null) {
-            lookupKey = ContactInfoHelper.getLookupKeyFromUri(contactUri);
+            lookupKey = UriUtils.getLookupKeyFromUri(contactUri);
         }
 
         DefaultImageRequest request = new DefaultImageRequest(
@@ -423,6 +448,19 @@
         if (view.getId() == R.id.primary_action_button && !TextUtils.isEmpty(voicemailUri)) {
             mVoicemailPrimaryActionButtonClicked = true;
             mExpandCollapseListener.onClick(primaryActionView);
+        } else if (view.getId() == R.id.call_with_note_action) {
+            CallSubjectDialog.start(
+                    (Activity) mContext,
+                    info.photoId,
+                    info.photoUri,
+                    info.lookupUri,
+                    (String) nameOrNumber /* top line of contact view in call subject dialog */,
+                    isBusiness,
+                    number, /* callable number used for ACTION_CALL intent */
+                    TextUtils.isEmpty(info.name) ? null : displayNumber, /* second line of contact
+                                                                           view in dialog. */
+                    numberType, /* phone number type (e.g. mobile) in second line of contact view */
+                    accountHandle);
         } else {
             final IntentProvider intentProvider = (IntentProvider) view.getTag();
             if (intentProvider != null) {
diff --git a/src/com/android/dialer/calllog/ContactInfoHelper.java b/src/com/android/dialer/calllog/ContactInfoHelper.java
index 20d33a0..2e07a03 100644
--- a/src/com/android/dialer/calllog/ContactInfoHelper.java
+++ b/src/com/android/dialer/calllog/ContactInfoHelper.java
@@ -394,24 +394,6 @@
     }
 
     /**
-     * Parses the given URI to determine the original lookup key of the contact.
-     */
-    public static String getLookupKeyFromUri(Uri lookupUri) {
-        // Would be nice to be able to persist the lookup key somehow to avoid having to parse
-        // the uri entirely just to retrieve the lookup key, but every uri is already parsed
-        // once anyway to check if it is an encoded JSON uri, so this has negligible effect
-        // on performance.
-        if (lookupUri != null && !UriUtils.isEncodedContactUri(lookupUri)) {
-            final List<String> segments = lookupUri.getPathSegments();
-            // This returns the third path segment of the uri, where the lookup key is located.
-            // See {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
-            return (segments.size() < 3) ? null : Uri.encode(segments.get(2));
-        } else {
-            return null;
-        }
-    }
-
-    /**
      * Returns the contact information stored in an entry of the call log.
      *
      * @param c A cursor pointing to an entry in the call log.
diff --git a/src/com/android/dialer/calllog/PhoneAccountUtils.java b/src/com/android/dialer/calllog/PhoneAccountUtils.java
index 7eaa523..143d13e 100644
--- a/src/com/android/dialer/calllog/PhoneAccountUtils.java
+++ b/src/com/android/dialer/calllog/PhoneAccountUtils.java
@@ -84,6 +84,21 @@
     }
 
     /**
+     * Determine whether a phone account supports call subjects.
+     *
+     * @return {@code true} if call subjects are supported, {@code false} otherwise.
+     */
+    public static boolean getAccountSupportsCallSubject(Context context,
+            PhoneAccountHandle accountHandle) {
+        TelecomManager telecomManager =
+                (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
+        final PhoneAccount account = telecomManager.getPhoneAccount(accountHandle);
+
+        return account == null ? false :
+                account.hasCapabilities(PhoneAccount.CAPABILITY_CALL_SUBJECT);
+    }
+
+    /**
      * Retrieve the account metadata, but if the account does not exist or the device has only a
      * single registered and enabled account, return null.
      */
diff --git a/src/com/android/dialer/calllog/TelecomCallLogCache.java b/src/com/android/dialer/calllog/TelecomCallLogCache.java
index ec1d241..7071669 100644
--- a/src/com/android/dialer/calllog/TelecomCallLogCache.java
+++ b/src/com/android/dialer/calllog/TelecomCallLogCache.java
@@ -52,6 +52,7 @@
             new HashMap<>();
     private final Map<PhoneAccountHandle, String> mPhoneAccountLabelCache = new HashMap<>();
     private final Map<PhoneAccountHandle, Integer> mPhoneAccountColorCache = new HashMap<>();
+    private final Map<PhoneAccountHandle, Boolean> mPhoneAccountCallWithNoteCache = new HashMap<>();
 
     private boolean mHasCheckedForVideoEnabled;
     private boolean mIsVideoEnabled;
@@ -64,6 +65,7 @@
         mVoicemailQueryCache.clear();
         mPhoneAccountLabelCache.clear();
         mPhoneAccountColorCache.clear();
+        mPhoneAccountCallWithNoteCache.clear();
 
         mHasCheckedForVideoEnabled = false;
         mIsVideoEnabled = false;
@@ -121,4 +123,22 @@
         }
         return mIsVideoEnabled;
     }
+
+    /**
+     * Determines if the PhoneAccount supports specifying a call subject (i.e. calling with a note)
+     * for outgoing calls.
+     *
+     * @param accountHandle The PhoneAccount handle.
+     * @return {@code true} if calling with a note is supported, {@code false} otherwise.
+     */
+    public boolean doesAccountSupportCallSubject(PhoneAccountHandle accountHandle) {
+        if (mPhoneAccountCallWithNoteCache.containsKey(accountHandle)) {
+            return mPhoneAccountCallWithNoteCache.get(accountHandle);
+        } else {
+            Boolean supportsCallWithNote =
+                    PhoneAccountUtils.getAccountSupportsCallSubject(mContext, accountHandle);
+            mPhoneAccountCallWithNoteCache.put(accountHandle, supportsCallWithNote);
+            return supportsCallWithNote;
+        }
+    }
 }