Fixing several issues of legacy support:

1. Security constraints fully enforced.
2. Default account is used for all operations.
diff --git a/src/com/android/providers/contacts/ContactsProvider2.java b/src/com/android/providers/contacts/ContactsProvider2.java
index 491c4e8..9a9e9e2 100644
--- a/src/com/android/providers/contacts/ContactsProvider2.java
+++ b/src/com/android/providers/contacts/ContactsProvider2.java
@@ -31,6 +31,7 @@
 import com.google.android.collect.Lists;
 
 import android.accounts.Account;
+import android.accounts.AccountManager;
 import android.app.SearchManager;
 import android.content.ContentProvider;
 import android.content.ContentProviderOperation;
@@ -44,7 +45,6 @@
 import android.content.SharedPreferences;
 import android.content.UriMatcher;
 import android.content.SharedPreferences.Editor;
-import android.content.pm.PackageManager;
 import android.database.Cursor;
 import android.database.DatabaseUtils;
 import android.database.sqlite.SQLiteCursor;
@@ -52,7 +52,6 @@
 import android.database.sqlite.SQLiteQueryBuilder;
 import android.database.sqlite.SQLiteStatement;
 import android.net.Uri;
-import android.os.Binder;
 import android.os.RemoteException;
 import android.preference.PreferenceManager;
 import android.provider.BaseColumns;
@@ -74,7 +73,6 @@
 import android.provider.ContactsContract.CommonDataKinds.Phone;
 import android.provider.ContactsContract.CommonDataKinds.StructuredName;
 import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
-import android.provider.ContactsContract.Contacts.AggregationSuggestions;
 import android.telephony.PhoneNumberUtils;
 import android.text.TextUtils;
 import android.util.Log;
@@ -87,6 +85,7 @@
  * is defined in {@link ContactsContract}.
  */
 public class ContactsProvider2 extends ContentProvider {
+
     // TODO: clean up debug tag and rename this class
     private static final String TAG = "ContactsProvider ~~~~";
 
@@ -304,6 +303,9 @@
                 DISPLAY_NAME_PRIORITY_EMAIL);
     }
 
+    public static final String DEFAULT_ACCOUNT_TYPE = "com.google.GAIA";
+    public static final String FEATURE_APPS_FOR_DOMAIN = "google_or_dasher";
+
     /** Contains just the contacts columns
      * @deprecated*/
     @Deprecated
@@ -375,8 +377,6 @@
     private SQLiteStatement mContactDisplayNameUpdate;
     /** Precompiled sql statement for marking a raw contact as dirty */
     private SQLiteStatement mRawContactDirtyUpdate;
-    /** Precompiled sql statement for marking a group as dirty */
-    private SQLiteStatement mGroupDirtyUpdate;
 
     static {
         // Contacts URI matching table
@@ -1126,9 +1126,6 @@
         mRawContactDirtyUpdate = db.compileStatement("UPDATE " + Tables.RAW_CONTACTS + " SET "
                 + RawContacts.DIRTY + "=1 WHERE " + RawContacts._ID + "=?");
 
-        mGroupDirtyUpdate = db.compileStatement("UPDATE " + Tables.GROUPS + " SET "
-                + Groups.DIRTY + "=1 WHERE " + Groups._ID + "=?");
-
         mNameSplitter = new NameSplitter(
                 context.getString(com.android.internal.R.string.common_name_prefixes),
                 context.getString(com.android.internal.R.string.common_last_name_prefixes),
@@ -2615,16 +2612,6 @@
     }
 
     /**
-     * Find any exceptions that have been granted to the
-     * {@link Binder#getCallingUid()}, and add a limiting clause to the given
-     * {@link SQLiteQueryBuilder} to hide restricted data.
-     */
-    void applyDataRestrictionExceptions(SQLiteQueryBuilder qb) {
-        String restrictions = getContactsRestrictions();
-        qb.appendWhere(restrictions);
-    }
-
-    /**
      * An implementation of EntityIterator that joins the contacts and data tables
      * and consumes all the data rows for a contact in order to build the Entity for a contact.
      */
@@ -3138,23 +3125,6 @@
         }
     }
 
-    private String buildContactLookupWhereClause(String filterParam) {
-        StringBuilder filter = new StringBuilder();
-        filter.append(Tables.CONTACTS);
-        filter.append(".");
-        filter.append(Contacts._ID);
-        filter.append(" IN (SELECT ");
-        filter.append(RawContacts.CONTACT_ID);
-        filter.append(" FROM ");
-        filter.append(Tables.RAW_CONTACTS);
-        filter.append(" WHERE ");
-        filter.append(RawContacts._ID);
-        filter.append(" IN ");
-        appendRawContactsByFilterAsNestedQuery(filter, filterParam, null);
-        filter.append(")");
-        return filter.toString();
-    }
-
     public String getRawContactsByFilterAsNestedQuery(String filterParam) {
         StringBuilder sb = new StringBuilder();
         appendRawContactsByFilterAsNestedQuery(sb, filterParam, null);
@@ -3186,4 +3156,20 @@
             return newSelectionArgs;
         }
     }
+
+    protected Account getDefaultAccount() {
+        AccountManager accountManager = AccountManager.get(getContext());
+        try {
+            Account[] accounts = accountManager.blockingGetAccountsWithTypeAndFeatures(
+                    DEFAULT_ACCOUNT_TYPE, new String[] {FEATURE_APPS_FOR_DOMAIN});
+            if (accounts != null && accounts.length > 0) {
+                return accounts[0];
+            }
+        } catch (Throwable e) {
+            throw new RuntimeException("Cannot determine the default account "
+                    + "for contacts compatibility", e);
+        }
+        throw new RuntimeException("Cannot determine the default account "
+                + "for contacts compatibility");
+    }
 }
diff --git a/src/com/android/providers/contacts/LegacyApiSupport.java b/src/com/android/providers/contacts/LegacyApiSupport.java
index 604c4ba..04f601d 100644
--- a/src/com/android/providers/contacts/LegacyApiSupport.java
+++ b/src/com/android/providers/contacts/LegacyApiSupport.java
@@ -23,6 +23,8 @@
 import com.android.providers.contacts.OpenHelper.RawContactsColumns;
 import com.android.providers.contacts.OpenHelper.Tables;
 
+import android.accounts.Account;
+import android.accounts.AccountManager;
 import android.app.SearchManager;
 import android.content.ContentUris;
 import android.content.ContentValues;
@@ -56,6 +58,8 @@
 
 public class LegacyApiSupport implements OpenHelper.Delegate {
 
+    private static final String TAG = "ContactsProviderV1";
+
     private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
 
     private static final int PEOPLE = 1;
@@ -91,8 +95,6 @@
     private static final int DELETED_GROUPS = 31;
     private static final int SEARCH_SUGGESTIONS = 32;
 
-
-
     private static final String PEOPLE_JOINS =
             " LEFT OUTER JOIN data name ON (raw_contacts._id = name.raw_contact_id"
             + " AND (SELECT mimetype FROM mimetypes WHERE mimetypes._id = name.mimetype_id)"
@@ -428,7 +430,7 @@
     private final SQLiteStatement mLastTimeContactedUpdate;
 
     private final ContentValues mValues = new ContentValues();
-
+    private Account mAccount;
 
     public LegacyApiSupport(Context context, OpenHelper openHelper,
             ContactsProvider2 contactsProvider, GlobalSearchSupport globalSearchSupport) {
@@ -449,6 +451,11 @@
                 + RawContacts._ID + "=?");
     }
 
+    private void ensureDefaultAccount() {
+        if (mAccount == null) {
+            mAccount = mContactsProvider.getDefaultAccount();
+        }
+    }
 
     public void createDatabase(SQLiteDatabase db) {
 
@@ -464,6 +471,8 @@
                         + " AS " + People.PHONETIC_NAME + " , " +
                 "note." + Note.NOTE
                         + " AS " + People.NOTES + ", " +
+                RawContacts.ACCOUNT_NAME + ", " +
+                RawContacts.ACCOUNT_TYPE + ", " +
                 Tables.RAW_CONTACTS + "." + RawContacts.TIMES_CONTACTED
                         + " AS " + People.TIMES_CONTACTED + ", " +
                 Tables.RAW_CONTACTS + "." + RawContacts.LAST_TIME_CONTACTED
@@ -487,10 +496,10 @@
                 "phone." + Phone.LABEL
                         + " AS " + People.LABEL + ", " +
                 "phone." + PhoneColumns.NORMALIZED_NUMBER
-                        + " AS " + People.NUMBER_KEY + ", " +
-                RawContacts.IS_RESTRICTED +
+                        + " AS " + People.NUMBER_KEY +
                 " FROM " + Tables.RAW_CONTACTS + PEOPLE_JOINS +
-                " WHERE " + Tables.RAW_CONTACTS + "." + RawContacts.DELETED + "=0" +
+                " WHERE " + Tables.RAW_CONTACTS + "." + RawContacts.DELETED + "=0"
+                        + " AND " + RawContacts.IS_RESTRICTED + "=0" +
         ";");
 
         db.execSQL("DROP VIEW IF EXISTS " + LegacyTables.ORGANIZATIONS + ";");
@@ -501,6 +510,8 @@
                         + " AS " + android.provider.Contacts.Organizations.PERSON_ID + ", " +
                 Data.IS_PRIMARY
                         + " AS " + android.provider.Contacts.Organizations.ISPRIMARY + ", " +
+                RawContacts.ACCOUNT_NAME + ", " +
+                RawContacts.ACCOUNT_TYPE + ", " +
                 Organization.COMPANY
                         + " AS " + android.provider.Contacts.Organizations.COMPANY + ", " +
                 Organization.TYPE
@@ -508,12 +519,12 @@
                 Organization.LABEL
                         + " AS " + android.provider.Contacts.Organizations.LABEL + ", " +
                 Organization.TITLE
-                        + " AS " + android.provider.Contacts.Organizations.TITLE + ", " +
-                RawContacts.IS_RESTRICTED +
+                        + " AS " + android.provider.Contacts.Organizations.TITLE +
                 " FROM " + Tables.DATA_JOIN_MIMETYPE_RAW_CONTACTS +
                 " WHERE " + MimetypesColumns.CONCRETE_MIMETYPE + "='"
                         + Organization.CONTENT_ITEM_TYPE + "'"
-                        + " AND " + Tables.RAW_CONTACTS + "." + RawContacts.DELETED + "=0" +
+                        + " AND " + Tables.RAW_CONTACTS + "." + RawContacts.DELETED + "=0"
+                        + " AND " + RawContacts.IS_RESTRICTED + "=0" +
         ";");
 
         db.execSQL("DROP VIEW IF EXISTS " + LegacyTables.CONTACT_METHODS + ";");
@@ -538,6 +549,8 @@
                         + " AS " + ContactMethods.NAME + ", " +
                 Tables.RAW_CONTACTS + "." + RawContactsColumns.DISPLAY_NAME
                         + " AS " + ContactMethods.DISPLAY_NAME + ", " +
+                RawContacts.ACCOUNT_NAME + ", " +
+                RawContacts.ACCOUNT_TYPE + ", " +
                 PHONETIC_NAME_SQL
                         + " AS " + ContactMethods.PHONETIC_NAME + " , " +
                 "note." + Note.NOTE
@@ -551,11 +564,11 @@
                 Tables.RAW_CONTACTS + "." + RawContacts.SEND_TO_VOICEMAIL
                         + " AS " + ContactMethods.SEND_TO_VOICEMAIL + ", " +
                 Tables.RAW_CONTACTS + "." + RawContacts.STARRED
-                        + " AS " + ContactMethods.STARRED + ", " +
-                RawContacts.IS_RESTRICTED +
+                        + " AS " + ContactMethods.STARRED +
                 " FROM " + Tables.DATA + DATA_JOINS +
                 " WHERE " + ContactMethods.KIND + " IS NOT NULL"
-                    + " AND " + Tables.RAW_CONTACTS + "." + RawContacts.DELETED + "=0" +
+                    + " AND " + Tables.RAW_CONTACTS + "." + RawContacts.DELETED + "=0"
+                    + " AND " + RawContacts.IS_RESTRICTED + "=0" +
         ";");
 
 
@@ -579,6 +592,8 @@
                         + " AS " + android.provider.Contacts.Phones.NAME + ", " +
                 Tables.RAW_CONTACTS + "." + RawContactsColumns.DISPLAY_NAME
                         + " AS " + android.provider.Contacts.Phones.DISPLAY_NAME + ", " +
+                RawContacts.ACCOUNT_NAME + ", " +
+                RawContacts.ACCOUNT_TYPE + ", " +
                 PHONETIC_NAME_SQL
                         + " AS " + android.provider.Contacts.Phones.PHONETIC_NAME + " , " +
                 "note." + Note.NOTE
@@ -592,12 +607,12 @@
                 Tables.RAW_CONTACTS + "." + RawContacts.SEND_TO_VOICEMAIL
                         + " AS " + android.provider.Contacts.Phones.SEND_TO_VOICEMAIL + ", " +
                 Tables.RAW_CONTACTS + "." + RawContacts.STARRED
-                        + " AS " + android.provider.Contacts.Phones.STARRED + ", " +
-                RawContacts.IS_RESTRICTED +
+                        + " AS " + android.provider.Contacts.Phones.STARRED +
                 " FROM " + Tables.DATA + DATA_JOINS +
                 " WHERE " + MimetypesColumns.CONCRETE_MIMETYPE + "='"
                         + Phone.CONTENT_ITEM_TYPE + "'"
-                        + " AND " + Tables.RAW_CONTACTS + "." + RawContacts.DELETED + "=0" +
+                        + " AND " + Tables.RAW_CONTACTS + "." + RawContacts.DELETED + "=0"
+                        + " AND " + RawContacts.IS_RESTRICTED + "=0" +
         ";");
 
         db.execSQL("DROP VIEW IF EXISTS " + LegacyTables.EXTENSIONS + ";");
@@ -606,20 +621,24 @@
                         + " AS " + android.provider.Contacts.Extensions._ID + ", " +
                 DataColumns.CONCRETE_RAW_CONTACT_ID
                         + " AS " + android.provider.Contacts.Extensions.PERSON_ID + ", " +
+                RawContacts.ACCOUNT_NAME + ", " +
+                RawContacts.ACCOUNT_TYPE + ", " +
                 ExtensionsColumns.NAME
                         + " AS " + android.provider.Contacts.Extensions.NAME + ", " +
                 ExtensionsColumns.VALUE
-                        + " AS " + android.provider.Contacts.Extensions.VALUE + ", " +
-                RawContacts.IS_RESTRICTED +
+                        + " AS " + android.provider.Contacts.Extensions.VALUE +
                 " FROM " + Tables.DATA_JOIN_MIMETYPE_RAW_CONTACTS +
                 " WHERE " + MimetypesColumns.CONCRETE_MIMETYPE + "='"
                         + android.provider.Contacts.Extensions.CONTENT_ITEM_TYPE + "'"
-                        + " AND " + Tables.RAW_CONTACTS + "." + RawContacts.DELETED + "=0" +
+                        + " AND " + Tables.RAW_CONTACTS + "." + RawContacts.DELETED + "=0"
+                        + " AND " + RawContacts.IS_RESTRICTED + "=0" +
         ";");
 
         db.execSQL("DROP VIEW IF EXISTS " + LegacyTables.GROUPS + ";");
         db.execSQL("CREATE VIEW " + LegacyTables.GROUPS + " AS SELECT " +
                 GroupsColumns.CONCRETE_ID + " AS " + android.provider.Contacts.Groups._ID + ", " +
+                Groups.ACCOUNT_NAME + ", " +
+                Groups.ACCOUNT_TYPE + ", " +
                 Groups.TITLE + " AS " + android.provider.Contacts.Groups.NAME + ", " +
                 Groups.NOTES + " AS " + android.provider.Contacts.Groups.NOTES + " , " +
                 Groups.SYSTEM_ID + " AS " + android.provider.Contacts.Groups.SYSTEM_ID +
@@ -632,6 +651,10 @@
                         + " AS " + android.provider.Contacts.GroupMembership._ID + ", " +
                 DataColumns.CONCRETE_RAW_CONTACT_ID
                         + " AS " + android.provider.Contacts.GroupMembership.PERSON_ID + ", " +
+                Tables.RAW_CONTACTS + "." + RawContacts.ACCOUNT_NAME
+                        + " AS " +  RawContacts.ACCOUNT_NAME + ", " +
+                Tables.RAW_CONTACTS + "." + RawContacts.ACCOUNT_TYPE
+                        + " AS " +  RawContacts.ACCOUNT_TYPE + ", " +
                 GroupMembership.GROUP_ROW_ID
                         + " AS " + android.provider.Contacts.GroupMembership.GROUP_ID + ", " +
                 Groups.TITLE
@@ -639,8 +662,7 @@
                 Groups.NOTES
                         + " AS " + android.provider.Contacts.GroupMembership.NOTES + " , " +
                 Groups.SYSTEM_ID
-                        + " AS " + android.provider.Contacts.GroupMembership.SYSTEM_ID + ", " +
-                RawContacts.IS_RESTRICTED +
+                        + " AS " + android.provider.Contacts.GroupMembership.SYSTEM_ID +
                 " FROM " + Tables.DATA_JOIN_PACKAGES_MIMETYPES_RAW_CONTACTS_GROUPS +
                 " WHERE " + MimetypesColumns.CONCRETE_MIMETYPE + "='"
                         + GroupMembership.CONTENT_ITEM_TYPE + "'"
@@ -653,6 +675,8 @@
                         + " AS " + android.provider.Contacts.Photos._ID + ", " +
                 DataColumns.CONCRETE_RAW_CONTACT_ID
                         + " AS " + android.provider.Contacts.Photos.PERSON_ID + ", " +
+                RawContacts.ACCOUNT_NAME + ", " +
+                RawContacts.ACCOUNT_TYPE + ", " +
                 Tables.DATA + "." + Photo.PHOTO
                         + " AS " + android.provider.Contacts.Photos.DATA + ", " +
                 "legacy_photo." + LegacyPhotoData.EXISTS_ON_SERVER
@@ -662,12 +686,12 @@
                 "legacy_photo." + LegacyPhotoData.LOCAL_VERSION
                         + " AS " + android.provider.Contacts.Photos.LOCAL_VERSION + ", " +
                 "legacy_photo." + LegacyPhotoData.SYNC_ERROR
-                        + " AS " + android.provider.Contacts.Photos.SYNC_ERROR + ", " +
-                RawContacts.IS_RESTRICTED +
+                        + " AS " + android.provider.Contacts.Photos.SYNC_ERROR +
                 " FROM " + Tables.DATA + DATA_JOINS + LEGACY_PHOTO_JOIN +
                 " WHERE " + MimetypesColumns.CONCRETE_MIMETYPE + "='"
                         + Photo.CONTENT_ITEM_TYPE + "'"
-                        + " AND " + Tables.RAW_CONTACTS + "." + RawContacts.DELETED + "=0" +
+                        + " AND " + Tables.RAW_CONTACTS + "." + RawContacts.DELETED + "=0"
+                        + " AND " + RawContacts.IS_RESTRICTED + "=0" +
         ";");
     }
 
@@ -749,6 +773,8 @@
     }
 
     private long insertPeople(ContentValues values) {
+        ensureDefaultAccount();
+
         mValues.clear();
 
         OpenHelper.copyStringValue(mValues, RawContacts.CUSTOM_RINGTONE,
@@ -761,6 +787,8 @@
                 values, People.TIMES_CONTACTED);
         OpenHelper.copyLongValue(mValues, RawContacts.STARRED,
                 values, People.STARRED);
+        mValues.put(RawContacts.ACCOUNT_NAME, mAccount.mName);
+        mValues.put(RawContacts.ACCOUNT_TYPE, mAccount.mType);
         Uri contactUri = mContactsProvider.insert(RawContacts.CONTENT_URI, mValues);
         long rawContactId = ContentUris.parseId(contactUri);
 
@@ -902,6 +930,7 @@
     }
 
     private long insertGroup(ContentValues values) {
+        ensureDefaultAccount();
         mValues.clear();
 
         OpenHelper.copyStringValue(mValues, Groups.TITLE,
@@ -911,6 +940,9 @@
         OpenHelper.copyStringValue(mValues, Groups.SYSTEM_ID,
                 values, android.provider.Contacts.Groups.SYSTEM_ID);
 
+        mValues.put(Groups.ACCOUNT_NAME, mAccount.mName);
+        mValues.put(Groups.ACCOUNT_TYPE, mAccount.mType);
+
         Uri uri = mContactsProvider.insert(Groups.CONTENT_URI, mValues);
         return ContentUris.parseId(uri);
     }
@@ -929,12 +961,22 @@
     private long insertPresence(ContentValues values) {
         mValues.clear();
 
+        String protocol = values.getAsString(android.provider.Contacts.Presence.IM_PROTOCOL);
+        if (protocol == null) {
+            throw new IllegalArgumentException("IM_PROTOCOL is required");
+        }
+
+        if (protocol.startsWith("pre:")) {
+            mValues.put(Presence.IM_PROTOCOL, Integer.parseInt(protocol.substring(4)));
+        } else if (protocol.startsWith("custom:")) {
+            mValues.put(Presence.IM_PROTOCOL, Im.PROTOCOL_CUSTOM);
+            // TODO add support for custom protocol
+        }
+
         OpenHelper.copyLongValue(mValues, Presence._ID,
                 values, android.provider.Contacts.Presence._ID);
         OpenHelper.copyLongValue(mValues, Presence.RAW_CONTACT_ID,
                 values, android.provider.Contacts.Presence.PERSON_ID);
-        OpenHelper.copyStringValue(mValues, Presence.IM_PROTOCOL,
-                values, android.provider.Contacts.Presence.IM_PROTOCOL);
         OpenHelper.copyStringValue(mValues, Presence.IM_HANDLE,
                 values, android.provider.Contacts.Presence.IM_HANDLE);
         OpenHelper.copyStringValue(mValues, Presence.IM_ACCOUNT,
@@ -1093,6 +1135,8 @@
 
     public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
             String sortOrder, String limit) {
+        ensureDefaultAccount();
+
         final SQLiteDatabase db = mOpenHelper.getReadableDatabase();
         SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
         String groupBy = null;
@@ -1102,21 +1146,24 @@
             case PEOPLE: {
                 qb.setTables(LegacyTables.PEOPLE_JOIN_PRESENCE);
                 qb.setProjectionMap(sPeopleProjectionMap);
+                applyRawContactsAccount(qb, uri);
                 break;
             }
 
             case PEOPLE_ID:
                 qb.setTables(LegacyTables.PEOPLE_JOIN_PRESENCE);
                 qb.setProjectionMap(sPeopleProjectionMap);
-                qb.appendWhere(People._ID + "=");
+                applyRawContactsAccount(qb, uri);
+                qb.appendWhere(" AND " + People._ID + "=");
                 qb.appendWhere(uri.getPathSegments().get(1));
                 break;
 
             case PEOPLE_FILTER: {
                 qb.setTables(LegacyTables.PEOPLE_JOIN_PRESENCE);
                 qb.setProjectionMap(sPeopleProjectionMap);
+                applyRawContactsAccount(qb, uri);
                 String filterParam = uri.getPathSegments().get(2);
-                qb.appendWhere(People._ID + " IN "
+                qb.appendWhere(" AND " + People._ID + " IN "
                         + mContactsProvider.getRawContactsByFilterAsNestedQuery(filterParam));
                 break;
             }
@@ -1124,13 +1171,13 @@
             case ORGANIZATIONS:
                 qb.setTables(LegacyTables.ORGANIZATIONS);
                 qb.setProjectionMap(sOrganizationProjectionMap);
-                mContactsProvider.applyDataRestrictionExceptions(qb);
+                applyRawContactsAccount(qb, uri);
                 break;
 
             case ORGANIZATIONS_ID:
                 qb.setTables(LegacyTables.ORGANIZATIONS);
                 qb.setProjectionMap(sOrganizationProjectionMap);
-                mContactsProvider.applyDataRestrictionExceptions(qb);
+                applyRawContactsAccount(qb, uri);
                 qb.appendWhere(" AND " + android.provider.Contacts.Organizations._ID + "=");
                 qb.appendWhere(uri.getPathSegments().get(1));
                 break;
@@ -1138,13 +1185,13 @@
             case CONTACTMETHODS:
                 qb.setTables(LegacyTables.CONTACT_METHODS);
                 qb.setProjectionMap(sContactMethodProjectionMap);
-                mContactsProvider.applyDataRestrictionExceptions(qb);
+                applyRawContactsAccount(qb, uri);
                 break;
 
             case CONTACTMETHODS_ID:
                 qb.setTables(LegacyTables.CONTACT_METHODS);
                 qb.setProjectionMap(sContactMethodProjectionMap);
-                mContactsProvider.applyDataRestrictionExceptions(qb);
+                applyRawContactsAccount(qb, uri);
                 qb.appendWhere(" AND " + ContactMethods._ID + "=");
                 qb.appendWhere(uri.getPathSegments().get(1));
                 break;
@@ -1152,7 +1199,7 @@
             case PEOPLE_CONTACTMETHODS:
                 qb.setTables(LegacyTables.CONTACT_METHODS);
                 qb.setProjectionMap(sContactMethodProjectionMap);
-                mContactsProvider.applyDataRestrictionExceptions(qb);
+                applyRawContactsAccount(qb, uri);
                 qb.appendWhere(" AND " + ContactMethods.PERSON_ID + "=");
                 qb.appendWhere(uri.getPathSegments().get(1));
                 qb.appendWhere(" AND " + ContactMethods.KIND + " IS NOT NULL");
@@ -1161,7 +1208,7 @@
             case PEOPLE_CONTACTMETHODS_ID:
                 qb.setTables(LegacyTables.CONTACT_METHODS);
                 qb.setProjectionMap(sContactMethodProjectionMap);
-                mContactsProvider.applyDataRestrictionExceptions(qb);
+                applyRawContactsAccount(qb, uri);
                 qb.appendWhere(" AND " + ContactMethods.PERSON_ID + "=");
                 qb.appendWhere(uri.getPathSegments().get(1));
                 qb.appendWhere(" AND " + ContactMethods._ID + "=");
@@ -1172,13 +1219,13 @@
             case PHONES:
                 qb.setTables(LegacyTables.PHONES);
                 qb.setProjectionMap(sPhoneProjectionMap);
-                mContactsProvider.applyDataRestrictionExceptions(qb);
+                applyRawContactsAccount(qb, uri);
                 break;
 
             case PHONES_ID:
                 qb.setTables(LegacyTables.PHONES);
                 qb.setProjectionMap(sPhoneProjectionMap);
-                mContactsProvider.applyDataRestrictionExceptions(qb);
+                applyRawContactsAccount(qb, uri);
                 qb.appendWhere(" AND " + android.provider.Contacts.Phones._ID + "=");
                 qb.appendWhere(uri.getPathSegments().get(1));
                 break;
@@ -1186,7 +1233,7 @@
             case PEOPLE_PHONES:
                 qb.setTables(LegacyTables.PHONES);
                 qb.setProjectionMap(sPhoneProjectionMap);
-                mContactsProvider.applyDataRestrictionExceptions(qb);
+                applyRawContactsAccount(qb, uri);
                 qb.appendWhere(" AND " + android.provider.Contacts.Phones.PERSON_ID + "=");
                 qb.appendWhere(uri.getPathSegments().get(1));
                 break;
@@ -1194,7 +1241,7 @@
             case PEOPLE_PHONES_ID:
                 qb.setTables(LegacyTables.PHONES);
                 qb.setProjectionMap(sPhoneProjectionMap);
-                mContactsProvider.applyDataRestrictionExceptions(qb);
+                applyRawContactsAccount(qb, uri);
                 qb.appendWhere(" AND " + android.provider.Contacts.Phones.PERSON_ID + "=");
                 qb.appendWhere(uri.getPathSegments().get(1));
                 qb.appendWhere(" AND " + android.provider.Contacts.Phones._ID + "=");
@@ -1204,13 +1251,13 @@
             case EXTENSIONS:
                 qb.setTables(LegacyTables.EXTENSIONS);
                 qb.setProjectionMap(sExtensionProjectionMap);
-                mContactsProvider.applyDataRestrictionExceptions(qb);
+                applyRawContactsAccount(qb, uri);
                 break;
 
             case EXTENSIONS_ID:
                 qb.setTables(LegacyTables.EXTENSIONS);
                 qb.setProjectionMap(sExtensionProjectionMap);
-                mContactsProvider.applyDataRestrictionExceptions(qb);
+                applyRawContactsAccount(qb, uri);
                 qb.appendWhere(" AND " + android.provider.Contacts.Extensions._ID + "=");
                 qb.appendWhere(uri.getPathSegments().get(1));
                 break;
@@ -1218,7 +1265,7 @@
             case PEOPLE_EXTENSIONS:
                 qb.setTables(LegacyTables.EXTENSIONS);
                 qb.setProjectionMap(sExtensionProjectionMap);
-                mContactsProvider.applyDataRestrictionExceptions(qb);
+                applyRawContactsAccount(qb, uri);
                 qb.appendWhere(" AND " + android.provider.Contacts.Extensions.PERSON_ID + "=");
                 qb.appendWhere(uri.getPathSegments().get(1));
                 break;
@@ -1226,7 +1273,7 @@
             case PEOPLE_EXTENSIONS_ID:
                 qb.setTables(LegacyTables.EXTENSIONS);
                 qb.setProjectionMap(sExtensionProjectionMap);
-                mContactsProvider.applyDataRestrictionExceptions(qb);
+                applyRawContactsAccount(qb, uri);
                 qb.appendWhere(" AND " + android.provider.Contacts.Extensions.PERSON_ID + "=");
                 qb.appendWhere(uri.getPathSegments().get(1));
                 qb.appendWhere(" AND " + android.provider.Contacts.Extensions._ID + "=");
@@ -1236,25 +1283,27 @@
             case GROUPS:
                 qb.setTables(LegacyTables.GROUPS);
                 qb.setProjectionMap(sGroupProjectionMap);
+                applyGroupAccount(qb, uri);
                 break;
 
             case GROUPS_ID:
                 qb.setTables(LegacyTables.GROUPS);
                 qb.setProjectionMap(sGroupProjectionMap);
-                qb.appendWhere(android.provider.Contacts.Groups._ID + "=");
+                applyGroupAccount(qb, uri);
+                qb.appendWhere(" AND " + android.provider.Contacts.Groups._ID + "=");
                 qb.appendWhere(uri.getPathSegments().get(1));
                 break;
 
             case GROUPMEMBERSHIP:
                 qb.setTables(LegacyTables.GROUP_MEMBERSHIP);
                 qb.setProjectionMap(sGroupMembershipProjectionMap);
-                mContactsProvider.applyDataRestrictionExceptions(qb);
+                applyRawContactsAccount(qb, uri);
                 break;
 
             case GROUPMEMBERSHIP_ID:
                 qb.setTables(LegacyTables.GROUP_MEMBERSHIP);
                 qb.setProjectionMap(sGroupMembershipProjectionMap);
-                mContactsProvider.applyDataRestrictionExceptions(qb);
+                applyRawContactsAccount(qb, uri);
                 qb.appendWhere(" AND " + android.provider.Contacts.GroupMembership._ID + "=");
                 qb.appendWhere(uri.getPathSegments().get(1));
                 break;
@@ -1262,7 +1311,7 @@
             case PEOPLE_GROUPMEMBERSHIP:
                 qb.setTables(LegacyTables.GROUP_MEMBERSHIP);
                 qb.setProjectionMap(sGroupMembershipProjectionMap);
-                mContactsProvider.applyDataRestrictionExceptions(qb);
+                applyRawContactsAccount(qb, uri);
                 qb.appendWhere(" AND " + android.provider.Contacts.GroupMembership.PERSON_ID + "=");
                 qb.appendWhere(uri.getPathSegments().get(1));
                 break;
@@ -1270,7 +1319,7 @@
             case PEOPLE_GROUPMEMBERSHIP_ID:
                 qb.setTables(LegacyTables.GROUP_MEMBERSHIP);
                 qb.setProjectionMap(sGroupMembershipProjectionMap);
-                mContactsProvider.applyDataRestrictionExceptions(qb);
+                applyRawContactsAccount(qb, uri);
                 qb.appendWhere(" AND " + android.provider.Contacts.GroupMembership.PERSON_ID + "=");
                 qb.appendWhere(uri.getPathSegments().get(1));
                 qb.appendWhere(" AND " + android.provider.Contacts.GroupMembership._ID + "=");
@@ -1280,7 +1329,7 @@
             case PEOPLE_PHOTO:
                 qb.setTables(LegacyTables.PHOTOS);
                 qb.setProjectionMap(sPhotoProjectionMap);
-                mContactsProvider.applyDataRestrictionExceptions(qb);
+                applyRawContactsAccount(qb, uri);
                 qb.appendWhere(" AND " + android.provider.Contacts.Photos.PERSON_ID + "=");
                 qb.appendWhere(uri.getPathSegments().get(1));
                 limit = "1";
@@ -1321,10 +1370,27 @@
         if (c != null) {
             c.setNotificationUri(mContext.getContentResolver(), RawContacts.CONTENT_URI);
         }
-        DatabaseUtils.dumpCursor(c);
         return c;
     }
 
+    private void applyRawContactsAccount(SQLiteQueryBuilder qb, Uri uri) {
+        StringBuilder sb = new StringBuilder();
+        sb.append(RawContacts.ACCOUNT_NAME + "=");
+        DatabaseUtils.appendEscapedSQLString(sb, mAccount.mName);
+        sb.append(" AND " + RawContacts.ACCOUNT_TYPE + "=");
+        DatabaseUtils.appendEscapedSQLString(sb, mAccount.mType);
+        qb.appendWhere(sb.toString());
+    }
+
+    private void applyGroupAccount(SQLiteQueryBuilder qb, Uri uri) {
+        StringBuilder sb = new StringBuilder();
+        sb.append(Groups.ACCOUNT_NAME + "=");
+        DatabaseUtils.appendEscapedSQLString(sb, mAccount.mName);
+        sb.append(" AND " + Groups.ACCOUNT_TYPE + "=");
+        DatabaseUtils.appendEscapedSQLString(sb, mAccount.mType);
+        qb.appendWhere(sb.toString());
+    }
+
     /**
      * Called when a change has been made.
      *
diff --git a/src/com/android/providers/contacts/OpenHelper.java b/src/com/android/providers/contacts/OpenHelper.java
index e08a8ed..5cd901b 100644
--- a/src/com/android/providers/contacts/OpenHelper.java
+++ b/src/com/android/providers/contacts/OpenHelper.java
@@ -59,7 +59,7 @@
 /* package */ class OpenHelper extends SQLiteOpenHelper {
     private static final String TAG = "OpenHelper";
 
-    private static final int DATABASE_VERSION = 63;
+    private static final int DATABASE_VERSION = 64;
     private static final String DATABASE_NAME = "contacts2.db";
     private static final String DATABASE_PRESENCE = "presence_db";
 
@@ -68,7 +68,6 @@
     }
 
     public interface Tables {
-        public static final String ACCOUNTS = "accounts";
         public static final String CONTACTS = "contacts";
         public static final String RAW_CONTACTS = "raw_contacts";
         public static final String PACKAGES = "packages";
@@ -1034,7 +1033,6 @@
         Log.i(TAG, "Upgrading from version " + oldVersion + " to " + newVersion
                 + ", data will be lost!");
 
-        db.execSQL("DROP TABLE IF EXISTS " + Tables.ACCOUNTS + ";");
         db.execSQL("DROP TABLE IF EXISTS " + Tables.CONTACTS + ";");
         db.execSQL("DROP TABLE IF EXISTS " + Tables.RAW_CONTACTS + ";");
         db.execSQL("DROP TABLE IF EXISTS " + Tables.PACKAGES + ";");
diff --git a/tests/src/com/android/providers/contacts/SynchronousContactsProvider2.java b/tests/src/com/android/providers/contacts/SynchronousContactsProvider2.java
index aad0660..79e7bcb 100644
--- a/tests/src/com/android/providers/contacts/SynchronousContactsProvider2.java
+++ b/tests/src/com/android/providers/contacts/SynchronousContactsProvider2.java
@@ -16,6 +16,7 @@
 
 package com.android.providers.contacts;
 
+import android.accounts.Account;
 import android.content.Context;
 
 /**
@@ -25,6 +26,7 @@
 public class SynchronousContactsProvider2 extends ContactsProvider2 {
     private static Boolean sDataWiped = false;
     private static OpenHelper mOpenHelper;
+    private Account mAccount;
 
     public SynchronousContactsProvider2() {
         super(new SynchronousAggregationScheduler());
@@ -55,6 +57,14 @@
     }
 
     @Override
+    protected Account getDefaultAccount() {
+        if (mAccount == null) {
+            mAccount = new Account("androidtest@gmail.com", "com.google.GAIA");
+        }
+        return mAccount;
+    }
+
+    @Override
     protected boolean isLegacyContactImportNeeded() {
 
         // We have an explicit test for data conversion - no need to do it every time