Merge "Update CP2 snippeting argument defaults (1/2)"
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 771aba1..e335e40 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -20,6 +20,7 @@
     <uses-permission android:name="android.permission.UPDATE_APP_OPS_STATS" />
     <uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
     <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS" />
+    <uses-permission android:name="android.permission.MANAGE_USERS" />
     <uses-permission android:name="com.android.voicemail.permission.ADD_VOICEMAIL" />
     <uses-permission android:name="com.android.voicemail.permission.READ_WRITE_ALL_VOICEMAIL" />
 
diff --git a/src/com/android/providers/contacts/ContactsProvider2.java b/src/com/android/providers/contacts/ContactsProvider2.java
index c9d8fac..6a15e59 100644
--- a/src/com/android/providers/contacts/ContactsProvider2.java
+++ b/src/com/android/providers/contacts/ContactsProvider2.java
@@ -3402,7 +3402,8 @@
         if (VERBOSE_LOGGING) {
             Log.v(TAG, "deleteInTransaction: uri=" + uri +
                     "  selection=[" + selection + "]  args=" + Arrays.toString(selectionArgs) +
-                    " CPID=" + Binder.getCallingPid());
+                    " CPID=" + Binder.getCallingPid() +
+                    " User=" + UserUtils.getCurrentUserHandle(getContext()));
         }
 
         final SQLiteDatabase db = mDbHelper.get().getWritableDatabase();
@@ -3812,7 +3813,8 @@
         if (VERBOSE_LOGGING) {
             Log.v(TAG, "updateInTransaction: uri=" + uri +
                     "  selection=[" + selection + "]  args=" + Arrays.toString(selectionArgs) +
-                    "  values=[" + values + "] CPID=" + Binder.getCallingPid());
+                    "  values=[" + values + "] CPID=" + Binder.getCallingPid() +
+                    " User=" + UserUtils.getCurrentUserHandle(getContext()));
         }
 
         final SQLiteDatabase db = mDbHelper.get().getWritableDatabase();
@@ -4967,7 +4969,8 @@
         if (VERBOSE_LOGGING) {
             Log.v(TAG, "query: uri=" + uri + "  projection=" + Arrays.toString(projection) +
                     "  selection=[" + selection + "]  args=" + Arrays.toString(selectionArgs) +
-                    "  order=[" + sortOrder + "] CPID=" + Binder.getCallingPid());
+                    "  order=[" + sortOrder + "] CPID=" + Binder.getCallingPid() +
+                    " User=" + UserUtils.getCurrentUserHandle(getContext()));
         }
 
         waitForAccess(mReadAccessLatch);
@@ -7556,7 +7559,8 @@
         } finally {
             if (VERBOSE_LOGGING) {
                 Log.v(TAG, "openAssetFile uri=" + uri + " mode=" + mode + " success=" + success +
-                        " CPID=" + Binder.getCallingPid());
+                        " CPID=" + Binder.getCallingPid() +
+                        " User=" + UserUtils.getCurrentUserHandle(getContext()));
             }
         }
     }
diff --git a/src/com/android/providers/contacts/UserUtils.java b/src/com/android/providers/contacts/UserUtils.java
new file mode 100644
index 0000000..b42ab8d
--- /dev/null
+++ b/src/com/android/providers/contacts/UserUtils.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+package com.android.providers.contacts;
+
+import android.content.Context;
+import android.content.pm.UserInfo;
+import android.os.UserManager;
+import android.util.Log;
+
+public final class UserUtils {
+    public static final String TAG = ContactsProvider2.TAG;
+
+    public static final boolean VERBOSE_LOGGING = Log.isLoggable(TAG, Log.VERBOSE);
+
+    private UserUtils() {
+    }
+
+    private static UserManager getUserManager(Context context) {
+        return (UserManager) context.getSystemService(Context.USER_SERVICE);
+    }
+
+    public static int getCurrentUserHandle(Context context) {
+        return getUserManager(context).getUserHandle();
+    }
+
+    /**
+     * @return the user ID of the enterprise user that is linked to the current user, if any.
+     * If there's no such user, returns -1.
+     *
+     * STOPSHIP: Have amith look at it.
+     */
+    public static int getEnterpriseUserId(Context context) {
+        final UserManager um = getUserManager(context);
+        final int currentUser = um.getUserHandle();
+
+        if (VERBOSE_LOGGING) {
+            Log.v(TAG, "getEnterpriseUserId: current=" + currentUser);
+        }
+
+        // TODO: Skip if the current is not the primary user?
+
+        // Check each user.
+        for (UserInfo ui : um.getUsers()) {
+            if (!ui.isManagedProfile()) {
+                continue; // Not a managed user.
+            }
+            final UserInfo parent = um.getProfileParent(ui.id);
+            if (parent == null) {
+                continue; // No parent.
+            }
+            // Check if it's linked to the current user.
+            if (um.getProfileParent(ui.id).id == currentUser) {
+                if (VERBOSE_LOGGING) {
+                    Log.v(TAG, "Corp user=" + ui.id);
+                }
+                return ui.id;
+            }
+        }
+        if (VERBOSE_LOGGING) {
+            Log.v(TAG, "Corp user not found.");
+        }
+        return -1;
+    }
+}