| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.provider; |
| 17 |
|
| 18 |
import android.app.admin.DevicePolicyManager; |
| 19 |
import android.compat.annotation.UnsupportedAppUsage; |
| 20 |
import android.content.ActivityNotFoundException; |
| 21 |
import android.content.ContentUris; |
| 22 |
import android.content.Context; |
| 23 |
import android.content.Intent; |
| 24 |
import android.content.UriMatcher; |
| 25 |
import android.net.Uri; |
| 26 |
import android.os.UserHandle; |
| 27 |
import android.text.TextUtils; |
| 28 |
import android.widget.Toast; |
| 29 |
|
| 30 |
import java.util.List; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class ContactsInternal { |
| 38 |
private ContactsInternal() { |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
private static final UriMatcher sContactsUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); |
| 43 |
|
| 44 |
private static final int CONTACTS_URI_LOOKUP_ID = 1000; |
| 45 |
private static final int CONTACTS_URI_LOOKUP = 1001; |
| 46 |
|
| 47 |
static { |
| 48 |
|
| 49 |
final UriMatcher matcher = sContactsUriMatcher; |
| 50 |
matcher.addURI(ContactsContract.AUTHORITY, "contacts/lookup/*", CONTACTS_URI_LOOKUP); |
| 51 |
matcher.addURI(ContactsContract.AUTHORITY, "contacts/lookup/*/#", CONTACTS_URI_LOOKUP_ID); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
@UnsupportedAppUsage |
| 58 |
public static void startQuickContactWithErrorToast(Context context, Intent intent) { |
| 59 |
final Uri uri = intent.getData(); |
| 60 |
|
| 61 |
final int match = sContactsUriMatcher.match(uri); |
| 62 |
switch (match) { |
| 63 |
case CONTACTS_URI_LOOKUP: |
| 64 |
case CONTACTS_URI_LOOKUP_ID: { |
| 65 |
if (maybeStartManagedQuickContact(context, intent)) { |
| 66 |
return; |
| 67 |
} |
| 68 |
break; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
startQuickContactWithErrorToastForUser(context, intent, context.getUser()); |
| 73 |
} |
| 74 |
|
| 75 |
public static void startQuickContactWithErrorToastForUser(Context context, Intent intent, |
| 76 |
UserHandle user) { |
| 77 |
try { |
| 78 |
context.startActivityAsUser(intent, user); |
| 79 |
} catch (ActivityNotFoundException e) { |
| 80 |
Toast.makeText(context, com.android.internal.R.string.quick_contacts_not_available, |
| 81 |
Toast.LENGTH_SHORT).show(); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
private static boolean maybeStartManagedQuickContact(Context context, Intent originalIntent) { |
| 92 |
final Uri uri = originalIntent.getData(); |
| 93 |
|
| 94 |
|
| 95 |
final List<String> pathSegments = uri.getPathSegments(); |
| 96 |
final boolean isContactIdIgnored = pathSegments.size() < 4; |
| 97 |
final long contactId = isContactIdIgnored |
| 98 |
? ContactsContract.Contacts.ENTERPRISE_CONTACT_ID_BASE |
| 99 |
: ContentUris.parseId(uri); |
| 100 |
final String lookupKey = pathSegments.get(2); |
| 101 |
final String directoryIdStr = uri.getQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY); |
| 102 |
final long directoryId = (directoryIdStr == null) |
| 103 |
? ContactsContract.Directory.ENTERPRISE_DIRECTORY_ID_BASE |
| 104 |
: Long.parseLong(directoryIdStr); |
| 105 |
|
| 106 |
|
| 107 |
if (TextUtils.isEmpty(lookupKey) |
| 108 |
|| !lookupKey.startsWith( |
| 109 |
ContactsContract.Contacts.ENTERPRISE_CONTACT_LOOKUP_PREFIX)) { |
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
if (!ContactsContract.Contacts.isEnterpriseContactId(contactId)) { |
| 114 |
throw new IllegalArgumentException("Invalid enterprise contact id: " + contactId); |
| 115 |
} |
| 116 |
if (!ContactsContract.Directory.isEnterpriseDirectoryId(directoryId)) { |
| 117 |
throw new IllegalArgumentException("Invalid enterprise directory id: " + directoryId); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
final DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class); |
| 122 |
final String actualLookupKey = lookupKey.substring( |
| 123 |
ContactsContract.Contacts.ENTERPRISE_CONTACT_LOOKUP_PREFIX.length()); |
| 124 |
final long actualContactId = |
| 125 |
(contactId - ContactsContract.Contacts.ENTERPRISE_CONTACT_ID_BASE); |
| 126 |
final long actualDirectoryId = (directoryId |
| 127 |
- ContactsContract.Directory.ENTERPRISE_DIRECTORY_ID_BASE); |
| 128 |
|
| 129 |
dpm.startManagedQuickContact(actualLookupKey, actualContactId, isContactIdIgnored, |
| 130 |
actualDirectoryId, originalIntent); |
| 131 |
return true; |
| 132 |
} |
| 133 |
} |