Debian Codegraph @

core/java/android/provider/ContactsInternal.java

raw symbols SCIP
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.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 * Contacts related internal methods.
34 *
35 * @hide
36 */
37 public 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 private static final int CONTACTS_URI_LOOKUP = 1001;
46
47 static {
48 // Contacts URI matching table
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 * Called by {@link ContactsContract} to star Quick Contact, possibly on the managed profile.
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; // Request handled by DPM. Just return here.
67 }
68 break;
69 }
70 }
71 // Launch on the current profile.
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 * If the URI in {@code intent} is of a corp contact, launch quick contact on the managed
87 * profile.
88 *
89 * @return the URI in {@code intent} is of a corp contact thus launched on the managed profile.
90 */
91 private static boolean maybeStartManagedQuickContact(Context context, Intent originalIntent) {
92 final Uri uri = originalIntent.getData();
93
94 // Decompose into an ID and a lookup key.
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 //contact id will be ignored
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 // See if it has a corp lookupkey.
107 if (TextUtils.isEmpty(lookupKey)
108 || !lookupKey.startsWith(
109 ContactsContract.Contacts.ENTERPRISE_CONTACT_LOOKUP_PREFIX)) {
110 return false; // It's not a corp lookup key.
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 // Launch Quick Contact on the managed profile, if the policy allows.
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 }