Only show "add connection" button if relevant

- We don't want to present the user with the "add connection"
button if the user doesn't use the account that provides the
service

- Check if app contributing the account type is disabled or not

- Check if there is an activity to handle the "add connection"
intent

- Check if there are raw contacts in the database with that
account type

- Store this in a cache, and refresh it after a certain
period of time (i.e. 1 second) using an AsyncTask.
This is to prevent computing the list each time the contact
is loaded (which can happen many times especially when looking
at a detail page during a sync).

- Make sure public AccountTypeManager methods
first check ensureAccountsLoaded()

Bug: 5398529
Change-Id: I004f9562a587035a3168aaddb6eb43710fd201e6
diff --git a/src/com/android/contacts/ContactsUtils.java b/src/com/android/contacts/ContactsUtils.java
index 9a3f2ef..b0c0508 100644
--- a/src/com/android/contacts/ContactsUtils.java
+++ b/src/com/android/contacts/ContactsUtils.java
@@ -25,6 +25,8 @@
 import android.content.Context;
 import android.content.Intent;
 import android.location.CountryDetector;
+import android.net.Uri;
+import android.provider.ContactsContract;
 import android.provider.ContactsContract.CommonDataKinds.Im;
 import android.provider.ContactsContract.CommonDataKinds.Phone;
 import android.telephony.PhoneNumberUtils;
@@ -184,4 +186,26 @@
                 AccountTypeManager.getInstance(context).getGroupWritableAccounts();
         return !accounts.isEmpty();
     }
+
+    /**
+     * Returns the intent to launch for the given invitable account type and contact lookup URI.
+     * This will return null if the account type is not invitable (i.e. there is no
+     * {@link AccountType#getInviteContactActivityClassName()} or
+     * {@link AccountType#resPackageName}).
+     */
+    public static Intent getInvitableIntent(AccountType accountType, Uri lookupUri) {
+        String resPackageName = accountType.resPackageName;
+        String className = accountType.getInviteContactActivityClassName();
+        if (TextUtils.isEmpty(resPackageName) || TextUtils.isEmpty(className)) {
+            return null;
+        }
+        Intent intent = new Intent();
+        intent.setClassName(resPackageName, className);
+
+        intent.setAction(ContactsContract.Intents.INVITE_CONTACT);
+
+        // Data is the lookup URI.
+        intent.setData(lookupUri);
+        return intent;
+    }
 }