1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 package android.provider; 17 18 import android.annotation.UnsupportedAppUsage; 19 import android.app.admin.DevicePolicyManager; 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.Process; 27 import android.os.UserHandle; 28 import android.text.TextUtils; 29 import android.widget.Toast; 30 31 import java.util.List; 32 33 /** 34 * Contacts related internal methods. 35 * 36 * @hide 37 */ 38 public class ContactsInternal { ContactsInternal()39 private ContactsInternal() { 40 } 41 42 /** URI matcher used to parse contact URIs. */ 43 private static final UriMatcher sContactsUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); 44 45 private static final int CONTACTS_URI_LOOKUP_ID = 1000; 46 private static final int CONTACTS_URI_LOOKUP = 1001; 47 48 static { 49 // Contacts URI matching table 50 final UriMatcher matcher = sContactsUriMatcher; matcher.addURI(ContactsContract.AUTHORITY, "contacts/lookup/*", CONTACTS_URI_LOOKUP)51 matcher.addURI(ContactsContract.AUTHORITY, "contacts/lookup/*", CONTACTS_URI_LOOKUP); matcher.addURI(ContactsContract.AUTHORITY, "contacts/lookup/*/#", CONTACTS_URI_LOOKUP_ID)52 matcher.addURI(ContactsContract.AUTHORITY, "contacts/lookup/*/#", CONTACTS_URI_LOOKUP_ID); 53 } 54 55 /** 56 * Called by {@link ContactsContract} to star Quick Contact, possibly on the managed profile. 57 */ 58 @UnsupportedAppUsage startQuickContactWithErrorToast(Context context, Intent intent)59 public static void startQuickContactWithErrorToast(Context context, Intent intent) { 60 final Uri uri = intent.getData(); 61 62 final int match = sContactsUriMatcher.match(uri); 63 switch (match) { 64 case CONTACTS_URI_LOOKUP: 65 case CONTACTS_URI_LOOKUP_ID: { 66 if (maybeStartManagedQuickContact(context, intent)) { 67 return; // Request handled by DPM. Just return here. 68 } 69 break; 70 } 71 } 72 // Launch on the current profile. 73 startQuickContactWithErrorToastForUser(context, intent, context.getUser()); 74 } 75 startQuickContactWithErrorToastForUser(Context context, Intent intent, UserHandle user)76 public static void startQuickContactWithErrorToastForUser(Context context, Intent intent, 77 UserHandle user) { 78 try { 79 context.startActivityAsUser(intent, user); 80 } catch (ActivityNotFoundException e) { 81 Toast.makeText(context, com.android.internal.R.string.quick_contacts_not_available, 82 Toast.LENGTH_SHORT).show(); 83 } 84 } 85 86 /** 87 * If the URI in {@code intent} is of a corp contact, launch quick contact on the managed 88 * profile. 89 * 90 * @return the URI in {@code intent} is of a corp contact thus launched on the managed profile. 91 */ maybeStartManagedQuickContact(Context context, Intent originalIntent)92 private static boolean maybeStartManagedQuickContact(Context context, Intent originalIntent) { 93 final Uri uri = originalIntent.getData(); 94 95 // Decompose into an ID and a lookup key. 96 final List<String> pathSegments = uri.getPathSegments(); 97 final boolean isContactIdIgnored = pathSegments.size() < 4; 98 final long contactId = isContactIdIgnored 99 ? ContactsContract.Contacts.ENTERPRISE_CONTACT_ID_BASE //contact id will be ignored 100 : ContentUris.parseId(uri); 101 final String lookupKey = pathSegments.get(2); 102 final String directoryIdStr = uri.getQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY); 103 final long directoryId = (directoryIdStr == null) 104 ? ContactsContract.Directory.ENTERPRISE_DIRECTORY_ID_BASE 105 : Long.parseLong(directoryIdStr); 106 107 // See if it has a corp lookupkey. 108 if (TextUtils.isEmpty(lookupKey) 109 || !lookupKey.startsWith( 110 ContactsContract.Contacts.ENTERPRISE_CONTACT_LOOKUP_PREFIX)) { 111 return false; // It's not a corp lookup key. 112 } 113 114 if (!ContactsContract.Contacts.isEnterpriseContactId(contactId)) { 115 throw new IllegalArgumentException("Invalid enterprise contact id: " + contactId); 116 } 117 if (!ContactsContract.Directory.isEnterpriseDirectoryId(directoryId)) { 118 throw new IllegalArgumentException("Invalid enterprise directory id: " + directoryId); 119 } 120 121 // Launch Quick Contact on the managed profile, if the policy allows. 122 final DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class); 123 final String actualLookupKey = lookupKey.substring( 124 ContactsContract.Contacts.ENTERPRISE_CONTACT_LOOKUP_PREFIX.length()); 125 final long actualContactId = 126 (contactId - ContactsContract.Contacts.ENTERPRISE_CONTACT_ID_BASE); 127 final long actualDirectoryId = (directoryId 128 - ContactsContract.Directory.ENTERPRISE_DIRECTORY_ID_BASE); 129 130 dpm.startManagedQuickContact(actualLookupKey, actualContactId, isContactIdIgnored, 131 actualDirectoryId, originalIntent); 132 return true; 133 } 134 } 135