OSDN Git Service

Add work contacts directory support in Quick Contacts API
authorRicky Wai <rickywai@google.com>
Fri, 20 Nov 2015 16:07:15 +0000 (16:07 +0000)
committerRicky Wai <rickywai@google.com>
Mon, 23 Nov 2015 13:32:45 +0000 (13:32 +0000)
Bug: 25764505

Change-Id: I61f9d13ea03352e3df1686ee4b3bcc43e9a9a760

core/java/android/app/admin/DevicePolicyManager.java
core/java/android/app/admin/IDevicePolicyManager.aidl
core/java/android/provider/ContactsContract.java
core/java/android/provider/ContactsInternal.java
services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java

index 0fdf3d3..5c22ed5 100644 (file)
@@ -40,6 +40,7 @@ import android.os.ServiceManager;
 import android.os.UserHandle;
 import android.os.UserManager;
 import android.provider.Settings;
+import android.provider.ContactsContract.Directory;
 import android.security.Credentials;
 import android.service.restrictions.RestrictionsReceiver;
 import android.util.Log;
@@ -3202,11 +3203,11 @@ public class DevicePolicyManager {
      * @hide
      */
     public void startManagedQuickContact(String actualLookupKey, long actualContactId,
-            Intent originalIntent) {
+            long directoryId, Intent originalIntent) {
         if (mService != null) {
             try {
                 mService.startManagedQuickContact(
-                        actualLookupKey, actualContactId, originalIntent);
+                        actualLookupKey, actualContactId, directoryId, originalIntent);
             } catch (RemoteException e) {
                 Log.w(TAG, "Failed talking with device policy service", e);
             }
@@ -3214,6 +3215,16 @@ public class DevicePolicyManager {
     }
 
     /**
+     * Start Quick Contact on the managed profile for the current user, if the policy allows.
+     * @hide
+     */
+    public void startManagedQuickContact(String actualLookupKey, long actualContactId,
+            Intent originalIntent) {
+        startManagedQuickContact(actualLookupKey, actualContactId, Directory.DEFAULT,
+                originalIntent);
+    }
+
+    /**
      * Called by a profile owner of a managed profile to set whether bluetooth
      * devices can access enterprise contacts.
      * <p>
index ccaa8cb..b638060 100644 (file)
@@ -191,7 +191,7 @@ interface IDevicePolicyManager {
     void setCrossProfileCallerIdDisabled(in ComponentName who, boolean disabled);
     boolean getCrossProfileCallerIdDisabled(in ComponentName who);
     boolean getCrossProfileCallerIdDisabledForUser(int userId);
-    void startManagedQuickContact(String lookupKey, long contactId, in Intent originalIntent);
+    void startManagedQuickContact(String lookupKey, long contactId, long directoryId, in Intent originalIntent);
 
     void setBluetoothContactSharingDisabled(in ComponentName who, boolean disabled);
     boolean getBluetoothContactSharingDisabled(in ComponentName who);
index 11ed562..5a48c50 100644 (file)
@@ -8352,10 +8352,15 @@ public final class ContactsContract {
          * @hide
          */
         public static Intent rebuildManagedQuickContactsIntent(String lookupKey, long contactId,
-                Intent originalIntent) {
+                long directoryId, Intent originalIntent) {
             final Intent intent = new Intent(ACTION_QUICK_CONTACT);
             // Rebuild the URI from a lookup key and a contact ID.
-            intent.setData(Contacts.getLookupUri(contactId, lookupKey));
+            Uri uri = Contacts.getLookupUri(contactId, lookupKey);
+            if (uri != null && directoryId != Directory.DEFAULT) {
+                uri = uri.buildUpon().appendQueryParameter(
+                        ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(directoryId)).build();
+            }
+            intent.setData(uri);
 
             // Copy flags and always set NEW_TASK because it won't have a parent activity.
             intent.setFlags(originalIntent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
index 059a603..36ef52d 100644 (file)
@@ -91,6 +91,10 @@ public class ContactsInternal {
         final List<String> pathSegments = uri.getPathSegments();
         final long contactId = ContentUris.parseId(uri);
         final String lookupKey = pathSegments.get(2);
+        final String directoryIdStr = uri.getQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY);
+        final long directoryId = (directoryIdStr == null)
+                ? ContactsContract.Directory.ENTERPRISE_DIRECTORY_ID_BASE
+                : Long.parseLong(directoryIdStr);
 
         // See if it has a corp lookupkey.
         if (TextUtils.isEmpty(lookupKey)
@@ -99,14 +103,24 @@ public class ContactsInternal {
             return false; // It's not a corp lookup key.
         }
 
+        if (!ContactsContract.Contacts.isEnterpriseContactId(contactId)) {
+            throw new IllegalArgumentException("Invalid enterprise contact id: " + contactId);
+        }
+        if (!ContactsContract.Directory.isEnterpriseDirectoryId(directoryId)) {
+            throw new IllegalArgumentException("Invalid enterprise directory id: " + directoryId);
+        }
+
         // Launch Quick Contact on the managed profile, if the policy allows.
         final DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class);
         final String actualLookupKey = lookupKey.substring(
                 ContactsContract.Contacts.ENTERPRISE_CONTACT_LOOKUP_PREFIX.length());
         final long actualContactId =
                 (contactId - ContactsContract.Contacts.ENTERPRISE_CONTACT_ID_BASE);
+        final long actualDirectoryId = (directoryId
+                - ContactsContract.Directory.ENTERPRISE_DIRECTORY_ID_BASE);
 
-        dpm.startManagedQuickContact(actualLookupKey, actualContactId, originalIntent);
+        dpm.startManagedQuickContact(actualLookupKey, actualContactId, actualDirectoryId,
+                originalIntent);
         return true;
     }
 }
index bf7c745..e344343 100644 (file)
@@ -5918,9 +5918,9 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
 
     @Override
     public void startManagedQuickContact(String actualLookupKey, long actualContactId,
-            Intent originalIntent) {
+            long actualDirectoryId, Intent originalIntent) {
         final Intent intent = QuickContact.rebuildManagedQuickContactsIntent(
-                actualLookupKey, actualContactId, originalIntent);
+                actualLookupKey, actualContactId, actualDirectoryId, originalIntent);
         final int callingUserId = UserHandle.getCallingUserId();
 
         final long ident = mInjector.binderClearCallingIdentity();