ContactsInternal.java revision 75a0882b946df6de4775c9e54ca023ff54f3f678
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 */ 16package android.provider; 17 18import android.app.admin.DevicePolicyManager; 19import android.content.ActivityNotFoundException; 20import android.content.ContentUris; 21import android.content.Context; 22import android.content.Intent; 23import android.content.UriMatcher; 24import android.net.Uri; 25import android.os.Process; 26import android.os.UserHandle; 27import android.text.TextUtils; 28import android.widget.Toast; 29 30import java.util.List; 31 32/** 33 * Contacts related internal methods. 34 * 35 * @hide 36 */ 37public class ContactsInternal { 38 private ContactsInternal() { 39 } 40 41 /** URI matcher used to parse contact URIs. */ 42 private static final UriMatcher sContactsUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); 43 44 private static final int CONTACTS_URI_LOOKUP_ID = 1000; 45 46 static { 47 // Contacts URI matching table 48 final UriMatcher matcher = sContactsUriMatcher; 49 matcher.addURI(ContactsContract.AUTHORITY, "contacts/lookup/*/#", CONTACTS_URI_LOOKUP_ID); 50 } 51 52 /** 53 * Called by {@link ContactsContract} to star Quick Contact, possibly on the managed profile. 54 */ 55 public static void startQuickContactWithErrorToast(Context context, Intent intent) { 56 final Uri uri = intent.getData(); 57 58 final int match = sContactsUriMatcher.match(uri); 59 switch (match) { 60 case CONTACTS_URI_LOOKUP_ID: { 61 if (maybeStartManagedQuickContact(context, intent)) { 62 return; // Request handled by DPM. Just return here. 63 } 64 break; 65 } 66 } 67 // Launch on the current profile. 68 startQuickContactWithErrorToastForUser(context, intent, Process.myUserHandle()); 69 } 70 71 public static void startQuickContactWithErrorToastForUser(Context context, Intent intent, 72 UserHandle user) { 73 try { 74 context.startActivityAsUser(intent, user); 75 } catch (ActivityNotFoundException e) { 76 Toast.makeText(context, com.android.internal.R.string.quick_contacts_not_available, 77 Toast.LENGTH_SHORT).show(); 78 } 79 } 80 81 /** 82 * If the URI in {@code intent} is of a corp contact, launch quick contact on the managed 83 * profile. 84 * 85 * @return the URI in {@code intent} is of a corp contact thus launched on the managed profile. 86 */ 87 private static boolean maybeStartManagedQuickContact(Context context, Intent originalIntent) { 88 final Uri uri = originalIntent.getData(); 89 90 // Decompose into an ID and a lookup key. 91 final List<String> pathSegments = uri.getPathSegments(); 92 final long contactId = ContentUris.parseId(uri); 93 final String lookupKey = pathSegments.get(2); 94 95 // See if it has a corp lookupkey. 96 if (TextUtils.isEmpty(lookupKey) 97 || !lookupKey.startsWith( 98 ContactsContract.Contacts.ENTERPRISE_CONTACT_LOOKUP_PREFIX)) { 99 return false; // It's not a corp lookup key. 100 } 101 102 // Launch Quick Contact on the managed profile, if the policy allows. 103 final DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class); 104 final String actualLookupKey = lookupKey.substring( 105 ContactsContract.Contacts.ENTERPRISE_CONTACT_LOOKUP_PREFIX.length()); 106 final long actualContactId = 107 (contactId - ContactsContract.Contacts.ENTERPRISE_CONTACT_ID_BASE); 108 109 dpm.startManagedQuickContact(actualLookupKey, actualContactId, originalIntent); 110 return true; 111 } 112} 113