Make SyncManager and AccountManagerService multi-user aware.

AccountManagerService
- Maintain multiple account lists, one per user
- Keep multiple databases of accounts
- Account db moved to /data/system/users/<userid>/

SyncManager
- SyncStorageEngine keeps track of multiple users' accounts.
- SyncQueue maintained as a single instance, queueing requests from
  multiple users.
- Changed some methods to take userId arguments
- Removed some deadc0de
- Store the userId in the SyncOperation, so we know which provider
  instance to bind to when queued operations are processed.

ContentService
- Pass along the userid to sync manager calls.

ActivityManagerService:
- Fixed a bug in cancelIntentSender
- Don't bring other user's task forward when resetting tasks.

Updated tests

Change-Id: If317340ef68e902787aa3f5ceb4cf96f14aea695
diff --git a/core/java/android/accounts/AccountManagerService.java b/core/java/android/accounts/AccountManagerService.java
index 5fee4de..adc7d35 100644
--- a/core/java/android/accounts/AccountManagerService.java
+++ b/core/java/android/accounts/AccountManagerService.java
@@ -18,6 +18,7 @@
 
 import android.Manifest;
 import android.app.ActivityManager;
+import android.app.AppGlobals;
 import android.app.Notification;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
@@ -33,6 +34,7 @@
 import android.content.pm.PackageManager;
 import android.content.pm.RegisteredServicesCache;
 import android.content.pm.RegisteredServicesCacheListener;
+import android.content.pm.UserInfo;
 import android.content.res.Resources;
 import android.database.Cursor;
 import android.database.DatabaseUtils;
@@ -48,11 +50,14 @@
 import android.os.Message;
 import android.os.RemoteException;
 import android.os.SystemClock;
+import android.os.UserId;
 import android.text.TextUtils;
 import android.util.Log;
 import android.util.Pair;
+import android.util.SparseArray;
 
 import com.android.internal.R;
+import com.android.internal.util.IndentingPrintWriter;
 
 import java.io.File;
 import java.io.FileDescriptor;
@@ -62,6 +67,7 @@
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicReference;
@@ -94,7 +100,6 @@
     private static final int MESSAGE_TIMED_OUT = 3;
 
     private final IAccountAuthenticatorCache mAuthenticatorCache;
-    private final DatabaseHelper mOpenHelper;
 
     private static final String TABLE_ACCOUNTS = "accounts";
     private static final String ACCOUNTS_ID = "_id";
@@ -148,14 +153,36 @@
     private final LinkedHashMap<String, Session> mSessions = new LinkedHashMap<String, Session>();
     private final AtomicInteger mNotificationIds = new AtomicInteger(1);
 
-    private final HashMap<Pair<Pair<Account, String>, Integer>, Integer>
-            mCredentialsPermissionNotificationIds =
-            new HashMap<Pair<Pair<Account, String>, Integer>, Integer>();
-    private final HashMap<Account, Integer> mSigninRequiredNotificationIds =
-            new HashMap<Account, Integer>();
+    static class UserAccounts {
+        private final int userId;
+        private final DatabaseHelper openHelper;
+        private final HashMap<Pair<Pair<Account, String>, Integer>, Integer>
+                credentialsPermissionNotificationIds =
+                new HashMap<Pair<Pair<Account, String>, Integer>, Integer>();
+        private final HashMap<Account, Integer> signinRequiredNotificationIds =
+                new HashMap<Account, Integer>();
+        private final Object cacheLock = new Object();
+        /** protected by the {@link #cacheLock} */
+        private final HashMap<String, Account[]> accountCache = new HashMap<String, Account[]>();
+        /** protected by the {@link #cacheLock} */
+        private HashMap<Account, HashMap<String, String>> userDataCache =
+                new HashMap<Account, HashMap<String, String>>();
+        /** protected by the {@link #cacheLock} */
+        private HashMap<Account, HashMap<String, String>> authTokenCache =
+                new HashMap<Account, HashMap<String, String>>();
+
+        UserAccounts(Context context, int userId) {
+            this.userId = userId;
+            synchronized (cacheLock) {
+                openHelper = new DatabaseHelper(context, userId);
+            }
+        }
+    }
+
+    private final SparseArray<UserAccounts> mUsers = new SparseArray<UserAccounts>();
+
     private static AtomicReference<AccountManagerService> sThis =
             new AtomicReference<AccountManagerService>();
-
     private static final Account[] EMPTY_ACCOUNT_ARRAY = new Account[]{};
 
     static {
@@ -163,15 +190,6 @@
         ACCOUNTS_CHANGED_INTENT.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
     }
 
-    private final Object mCacheLock = new Object();
-    /** protected by the {@link #mCacheLock} */
-    private final HashMap<String, Account[]> mAccountCache = new HashMap<String, Account[]>();
-    /** protected by the {@link #mCacheLock} */
-    private HashMap<Account, HashMap<String, String>> mUserDataCache =
-            new HashMap<Account, HashMap<String, String>>();
-    /** protected by the {@link #mCacheLock} */
-    private HashMap<Account, HashMap<String, String>> mAuthTokenCache =
-            new HashMap<Account, HashMap<String, String>>();
 
     /**
      * This should only be called by system code. One should only call this after the service
@@ -192,10 +210,6 @@
         mContext = context;
         mPackageManager = packageManager;
 
-        synchronized (mCacheLock) {
-            mOpenHelper = new DatabaseHelper(mContext);
-        }
-
         mMessageThread = new HandlerThread("AccountManagerService");
         mMessageThread.start();
         mMessageHandler = new MessageHandler(mMessageThread.getLooper());
@@ -203,6 +217,8 @@
         mAuthenticatorCache = authenticatorCache;
         mAuthenticatorCache.setListener(this, null /* Handler */);
 
+        UserAccounts accounts = initUser(0);
+
         sThis.set(this);
 
         IntentFilter intentFilter = new IntentFilter();
@@ -211,17 +227,36 @@
         mContext.registerReceiver(new BroadcastReceiver() {
             @Override
             public void onReceive(Context context1, Intent intent) {
-                purgeOldGrants();
+                purgeOldGrantsAll();
             }
         }, intentFilter);
-        purgeOldGrants();
 
-        validateAccountsAndPopulateCache();
     }
 
-    private void purgeOldGrants() {
-        synchronized (mCacheLock) {
-            final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
+    private UserAccounts initUser(int userId) {
+        synchronized (mUsers) {
+            UserAccounts accounts = mUsers.get(userId);
+            if (accounts == null) {
+                accounts = new UserAccounts(mContext, userId);
+                mUsers.append(userId, accounts);
+                purgeOldGrants(accounts);
+                validateAccountsAndPopulateCache(accounts);
+            }
+            return accounts;
+        }
+    }
+
+    private void purgeOldGrantsAll() {
+        synchronized (mUsers) {
+            for (int i = 0; i < mUsers.size(); i++) {
+                purgeOldGrants(mUsers.valueAt(i));
+            }
+        }
+    }
+
+    private void purgeOldGrants(UserAccounts accounts) {
+        synchronized (accounts.cacheLock) {
+            final SQLiteDatabase db = accounts.openHelper.getWritableDatabase();
             final Cursor cursor = db.query(TABLE_GRANTS,
                     new String[]{GRANTS_GRANTEE_UID},
                     null, null, GRANTS_GRANTEE_UID, null, null);
@@ -243,15 +278,15 @@
         }
     }
 
-    private void validateAccountsAndPopulateCache() {
-        synchronized (mCacheLock) {
-            final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
+    private void validateAccountsAndPopulateCache(UserAccounts accounts) {
+        synchronized (accounts.cacheLock) {
+            final SQLiteDatabase db = accounts.openHelper.getWritableDatabase();
             boolean accountDeleted = false;
             Cursor cursor = db.query(TABLE_ACCOUNTS,
                     new String[]{ACCOUNTS_ID, ACCOUNTS_TYPE, ACCOUNTS_NAME},
                     null, null, null, null, null);
             try {
-                mAccountCache.clear();
+                accounts.accountCache.clear();
                 final HashMap<String, ArrayList<String>> accountNamesByType =
                         new HashMap<String, ArrayList<String>>();
                 while (cursor.moveToNext()) {
@@ -265,8 +300,8 @@
                         db.delete(TABLE_ACCOUNTS, ACCOUNTS_ID + "=" + accountId, null);
                         accountDeleted = true;
                         final Account account = new Account(accountName, accountType);
-                        mUserDataCache.remove(account);
-                        mAuthTokenCache.remove(account);
+                        accounts.userDataCache.remove(account);
+                        accounts.authTokenCache.remove(account);
                     } else {
                         ArrayList<String> accountNames = accountNamesByType.get(accountType);
                         if (accountNames == null) {
@@ -286,19 +321,51 @@
                         accountsForType[i] = new Account(accountName, accountType);
                         ++i;
                     }
-                    mAccountCache.put(accountType, accountsForType);
+                    accounts.accountCache.put(accountType, accountsForType);
                 }
             } finally {
                 cursor.close();
                 if (accountDeleted) {
-                    sendAccountsChangedBroadcast();
+                    sendAccountsChangedBroadcast(accounts.userId);
                 }
             }
         }
     }
 
+    private UserAccounts getUserAccountsForCaller() {
+        return getUserAccounts(UserId.getCallingUserId());
+    }
+
+    protected UserAccounts getUserAccounts(int userId) {
+        synchronized (mUsers) {
+            UserAccounts accounts = mUsers.get(userId);
+            if (accounts == null) {
+                accounts = initUser(userId);
+                mUsers.append(userId, accounts);
+            }
+            return accounts;
+        }
+    }
+
+    private List<UserInfo> getAllUsers() {
+        try {
+            return AppGlobals.getPackageManager().getUsers();
+        } catch (RemoteException re) {
+            // Local to system process, shouldn't happen
+        }
+        return null;
+    }
+
     public void onServiceChanged(AuthenticatorDescription desc, boolean removed) {
-        validateAccountsAndPopulateCache();
+        // Validate accounts for all users
+        List<UserInfo> users = getAllUsers();
+        if (users == null) {
+            validateAccountsAndPopulateCache(getUserAccountsForCaller());
+        } else {
+            for (UserInfo user : users) {
+                validateAccountsAndPopulateCache(getUserAccounts(user.id));
+            }
+        }
     }
 
     public String getPassword(Account account) {
@@ -310,21 +377,22 @@
         if (account == null) throw new IllegalArgumentException("account is null");
         checkAuthenticateAccountsPermission(account);
 
+        UserAccounts accounts = getUserAccountsForCaller();
         long identityToken = clearCallingIdentity();
         try {
-            return readPasswordInternal(account);
+            return readPasswordInternal(accounts, account);
         } finally {
             restoreCallingIdentity(identityToken);
         }
     }
 
-    private String readPasswordInternal(Account account) {
+    private String readPasswordInternal(UserAccounts accounts, Account account) {
         if (account == null) {
             return null;
         }
 
-        synchronized (mCacheLock) {
-            final SQLiteDatabase db = mOpenHelper.getReadableDatabase();
+        synchronized (accounts.cacheLock) {
+            final SQLiteDatabase db = accounts.openHelper.getReadableDatabase();
             Cursor cursor = db.query(TABLE_ACCOUNTS, new String[]{ACCOUNTS_PASSWORD},
                     ACCOUNTS_NAME + "=? AND " + ACCOUNTS_TYPE+ "=?",
                     new String[]{account.name, account.type}, null, null, null);
@@ -349,9 +417,10 @@
         if (account == null) throw new IllegalArgumentException("account is null");
         if (key == null) throw new IllegalArgumentException("key is null");
         checkAuthenticateAccountsPermission(account);
+        UserAccounts accounts = getUserAccountsForCaller();
         long identityToken = clearCallingIdentity();
         try {
-            return readUserDataInternal(account, key);
+            return readUserDataInternal(accounts, account, key);
         } finally {
             restoreCallingIdentity(identityToken);
         }
@@ -390,21 +459,23 @@
         if (account == null) throw new IllegalArgumentException("account is null");
         checkAuthenticateAccountsPermission(account);
 
+        UserAccounts accounts = getUserAccountsForCaller();
         // fails if the account already exists
         long identityToken = clearCallingIdentity();
         try {
-            return addAccountInternal(account, password, extras);
+            return addAccountInternal(accounts, account, password, extras);
         } finally {
             restoreCallingIdentity(identityToken);
         }
     }
 
-    private boolean addAccountInternal(Account account, String password, Bundle extras) {
+    private boolean addAccountInternal(UserAccounts accounts, Account account, String password,
+            Bundle extras) {
         if (account == null) {
             return false;
         }
-        synchronized (mCacheLock) {
-            final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
+        synchronized (accounts.cacheLock) {
+            final SQLiteDatabase db = accounts.openHelper.getWritableDatabase();
             db.beginTransaction();
             try {
                 long numMatches = DatabaseUtils.longForQuery(db,
@@ -437,11 +508,11 @@
                     }
                 }
                 db.setTransactionSuccessful();
-                insertAccountIntoCacheLocked(account);
+                insertAccountIntoCacheLocked(accounts, account);
             } finally {
                 db.endTransaction();
             }
-            sendAccountsChangedBroadcast();
+            sendAccountsChangedBroadcast(accounts.userId);
             return true;
         }
     }
@@ -467,9 +538,10 @@
         if (account == null) throw new IllegalArgumentException("account is null");
         if (features == null) throw new IllegalArgumentException("features is null");
         checkReadAccountsPermission();
+        UserAccounts accounts = getUserAccountsForCaller();
         long identityToken = clearCallingIdentity();
         try {
-            new TestFeaturesSession(response, account, features).bind();
+            new TestFeaturesSession(accounts, response, account, features).bind();
         } finally {
             restoreCallingIdentity(identityToken);
         }
@@ -479,9 +551,9 @@
         private final String[] mFeatures;
         private final Account mAccount;
 
-        public TestFeaturesSession(IAccountManagerResponse response,
+        public TestFeaturesSession(UserAccounts accounts, IAccountManagerResponse response,
                 Account account, String[] features) {
-            super(response, account.type, false /* expectActivityLaunch */,
+            super(accounts, response, account.type, false /* expectActivityLaunch */,
                     true /* stripAuthTokenFromResult */);
             mFeatures = features;
             mAccount = account;
@@ -537,21 +609,22 @@
         if (response == null) throw new IllegalArgumentException("response is null");
         if (account == null) throw new IllegalArgumentException("account is null");
         checkManageAccountsPermission();
+        UserAccounts accounts = getUserAccountsForCaller();
         long identityToken = clearCallingIdentity();
 
-        cancelNotification(getSigninRequiredNotificationId(account));
-        synchronized(mCredentialsPermissionNotificationIds) {
+        cancelNotification(getSigninRequiredNotificationId(accounts, account));
+        synchronized(accounts.credentialsPermissionNotificationIds) {
             for (Pair<Pair<Account, String>, Integer> pair:
-                mCredentialsPermissionNotificationIds.keySet()) {
+                accounts.credentialsPermissionNotificationIds.keySet()) {
                 if (account.equals(pair.first.first)) {
-                    int id = mCredentialsPermissionNotificationIds.get(pair);
+                    int id = accounts.credentialsPermissionNotificationIds.get(pair);
                     cancelNotification(id);
                 }
             }
         }
 
         try {
-            new RemoveAccountSession(response, account).bind();
+            new RemoveAccountSession(accounts, response, account).bind();
         } finally {
             restoreCallingIdentity(identityToken);
         }
@@ -559,8 +632,9 @@
 
     private class RemoveAccountSession extends Session {
         final Account mAccount;
-        public RemoveAccountSession(IAccountManagerResponse response, Account account) {
-            super(response, account.type, false /* expectActivityLaunch */,
+        public RemoveAccountSession(UserAccounts accounts, IAccountManagerResponse response,
+                Account account) {
+            super(accounts, response, account.type, false /* expectActivityLaunch */,
                     true /* stripAuthTokenFromResult */);
             mAccount = account;
         }
@@ -579,7 +653,7 @@
                     && !result.containsKey(AccountManager.KEY_INTENT)) {
                 final boolean removalAllowed = result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT);
                 if (removalAllowed) {
-                    removeAccountInternal(mAccount);
+                    removeAccountInternal(mAccounts, mAccount);
                 }
                 IAccountManagerResponse response = getResponseAndClose();
                 if (response != null) {
@@ -600,13 +674,18 @@
         }
     }
 
+    /* For testing */
     protected void removeAccountInternal(Account account) {
-        synchronized (mCacheLock) {
-            final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
+        removeAccountInternal(getUserAccountsForCaller(), account);
+    }
+
+    private void removeAccountInternal(UserAccounts accounts, Account account) {
+        synchronized (accounts.cacheLock) {
+            final SQLiteDatabase db = accounts.openHelper.getWritableDatabase();
             db.delete(TABLE_ACCOUNTS, ACCOUNTS_NAME + "=? AND " + ACCOUNTS_TYPE+ "=?",
                     new String[]{account.name, account.type});
-            removeAccountFromCacheLocked(account);
-            sendAccountsChangedBroadcast();
+            removeAccountFromCacheLocked(accounts, account);
+            sendAccountsChangedBroadcast(accounts.userId);
         }
     }
 
@@ -619,13 +698,14 @@
         if (accountType == null) throw new IllegalArgumentException("accountType is null");
         if (authToken == null) throw new IllegalArgumentException("authToken is null");
         checkManageAccountsOrUseCredentialsPermissions();
+        UserAccounts accounts = getUserAccountsForCaller();
         long identityToken = clearCallingIdentity();
         try {
-            synchronized (mCacheLock) {
-                final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
+            synchronized (accounts.cacheLock) {
+                final SQLiteDatabase db = accounts.openHelper.getWritableDatabase();
                 db.beginTransaction();
                 try {
-                    invalidateAuthTokenLocked(db, accountType, authToken);
+                    invalidateAuthTokenLocked(accounts, db, accountType, authToken);
                     db.setTransactionSuccessful();
                 } finally {
                     db.endTransaction();
@@ -636,7 +716,8 @@
         }
     }
 
-    private void invalidateAuthTokenLocked(SQLiteDatabase db, String accountType, String authToken) {
+    private void invalidateAuthTokenLocked(UserAccounts accounts, SQLiteDatabase db,
+            String accountType, String authToken) {
         if (authToken == null || accountType == null) {
             return;
         }
@@ -657,7 +738,7 @@
                 String accountName = cursor.getString(1);
                 String authTokenType = cursor.getString(2);
                 db.delete(TABLE_AUTHTOKENS, AUTHTOKENS_ID + "=" + authTokenId, null);
-                writeAuthTokenIntoCacheLocked(db, new Account(accountName, accountType),
+                writeAuthTokenIntoCacheLocked(accounts, db, new Account(accountName, accountType),
                         authTokenType, null);
             }
         } finally {
@@ -665,13 +746,14 @@
         }
     }
 
-    private boolean saveAuthTokenToDatabase(Account account, String type, String authToken) {
+    private boolean saveAuthTokenToDatabase(UserAccounts accounts, Account account, String type,
+            String authToken) {
         if (account == null || type == null) {
             return false;
         }
-        cancelNotification(getSigninRequiredNotificationId(account));
-        synchronized (mCacheLock) {
-            final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
+        cancelNotification(getSigninRequiredNotificationId(accounts, account));
+        synchronized (accounts.cacheLock) {
+            final SQLiteDatabase db = accounts.openHelper.getWritableDatabase();
             db.beginTransaction();
             try {
                 long accountId = getAccountIdLocked(db, account);
@@ -687,7 +769,7 @@
                 values.put(AUTHTOKENS_AUTHTOKEN, authToken);
                 if (db.insert(TABLE_AUTHTOKENS, AUTHTOKENS_AUTHTOKEN, values) >= 0) {
                     db.setTransactionSuccessful();
-                    writeAuthTokenIntoCacheLocked(db, account, type, authToken);
+                    writeAuthTokenIntoCacheLocked(accounts, db, account, type, authToken);
                     return true;
                 }
                 return false;
@@ -707,9 +789,10 @@
         if (account == null) throw new IllegalArgumentException("account is null");
         if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
         checkAuthenticateAccountsPermission(account);
+        UserAccounts accounts = getUserAccountsForCaller();
         long identityToken = clearCallingIdentity();
         try {
-            return readAuthTokenInternal(account, authTokenType);
+            return readAuthTokenInternal(accounts, account, authTokenType);
         } finally {
             restoreCallingIdentity(identityToken);
         }
@@ -725,9 +808,10 @@
         if (account == null) throw new IllegalArgumentException("account is null");
         if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
         checkAuthenticateAccountsPermission(account);
+        UserAccounts accounts = getUserAccountsForCaller();
         long identityToken = clearCallingIdentity();
         try {
-            saveAuthTokenToDatabase(account, authTokenType, authToken);
+            saveAuthTokenToDatabase(accounts, account, authTokenType, authToken);
         } finally {
             restoreCallingIdentity(identityToken);
         }
@@ -741,20 +825,21 @@
         }
         if (account == null) throw new IllegalArgumentException("account is null");
         checkAuthenticateAccountsPermission(account);
+        UserAccounts accounts = getUserAccountsForCaller();
         long identityToken = clearCallingIdentity();
         try {
-            setPasswordInternal(account, password);
+            setPasswordInternal(accounts, account, password);
         } finally {
             restoreCallingIdentity(identityToken);
         }
     }
 
-    private void setPasswordInternal(Account account, String password) {
+    private void setPasswordInternal(UserAccounts accounts, Account account, String password) {
         if (account == null) {
             return;
         }
-        synchronized (mCacheLock) {
-            final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
+        synchronized (accounts.cacheLock) {
+            final SQLiteDatabase db = accounts.openHelper.getWritableDatabase();
             db.beginTransaction();
             try {
                 final ContentValues values = new ContentValues();
@@ -764,20 +849,20 @@
                     final String[] argsAccountId = {String.valueOf(accountId)};
                     db.update(TABLE_ACCOUNTS, values, ACCOUNTS_ID + "=?", argsAccountId);
                     db.delete(TABLE_AUTHTOKENS, AUTHTOKENS_ACCOUNTS_ID + "=?", argsAccountId);
-                    mAuthTokenCache.remove(account);
+                    accounts.authTokenCache.remove(account);
                     db.setTransactionSuccessful();
                 }
             } finally {
                 db.endTransaction();
             }
-            sendAccountsChangedBroadcast();
+            sendAccountsChangedBroadcast(accounts.userId);
         }
     }
 
-    private void sendAccountsChangedBroadcast() {
+    private void sendAccountsChangedBroadcast(int userId) {
         Log.i(TAG, "the accounts changed, sending broadcast of "
                 + ACCOUNTS_CHANGED_INTENT.getAction());
-        mContext.sendBroadcast(ACCOUNTS_CHANGED_INTENT);
+        mContext.sendBroadcast(ACCOUNTS_CHANGED_INTENT, userId);
     }
 
     public void clearPassword(Account account) {
@@ -788,9 +873,10 @@
         }
         if (account == null) throw new IllegalArgumentException("account is null");
         checkManageAccountsPermission();
+        UserAccounts accounts = getUserAccountsForCaller();
         long identityToken = clearCallingIdentity();
         try {
-            setPasswordInternal(account, null);
+            setPasswordInternal(accounts, account, null);
         } finally {
             restoreCallingIdentity(identityToken);
         }
@@ -806,20 +892,22 @@
         if (key == null) throw new IllegalArgumentException("key is null");
         if (account == null) throw new IllegalArgumentException("account is null");
         checkAuthenticateAccountsPermission(account);
+        UserAccounts accounts = getUserAccountsForCaller();
         long identityToken = clearCallingIdentity();
         try {
-            setUserdataInternal(account, key, value);
+            setUserdataInternal(accounts, account, key, value);
         } finally {
             restoreCallingIdentity(identityToken);
         }
     }
 
-    private void setUserdataInternal(Account account, String key, String value) {
+    private void setUserdataInternal(UserAccounts accounts, Account account, String key,
+            String value) {
         if (account == null || key == null) {
             return;
         }
-        synchronized (mCacheLock) {
-            final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
+        synchronized (accounts.cacheLock) {
+            final SQLiteDatabase db = accounts.openHelper.getWritableDatabase();
             db.beginTransaction();
             try {
                 long accountId = getAccountIdLocked(db, account);
@@ -840,7 +928,7 @@
                     }
 
                 }
-                writeUserDataIntoCacheLocked(db, account, key, value);
+                writeUserDataIntoCacheLocked(accounts, db, account, key, value);
                 db.setTransactionSuccessful();
             } finally {
                 db.endTransaction();
@@ -868,15 +956,16 @@
     }
 
     void getAuthTokenLabel(final IAccountManagerResponse response,
-            final Account account, final String authTokenType) {
+            final Account account,
+            final String authTokenType, int uid) {
         if (account == null) throw new IllegalArgumentException("account is null");
         if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
 
         checkBinderPermission(Manifest.permission.USE_CREDENTIALS);
-
+        UserAccounts accounts = getUserAccounts(UserId.getUserId(uid));
         long identityToken = clearCallingIdentity();
         try {
-            new Session(response, account.type, false,
+            new Session(accounts, response, account.type, false,
                     false /* stripAuthTokenFromResult */) {
                 protected String toDebugString(long now) {
                     return super.toDebugString(now) + ", getAuthTokenLabel"
@@ -921,6 +1010,7 @@
         if (account == null) throw new IllegalArgumentException("account is null");
         if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
         checkBinderPermission(Manifest.permission.USE_CREDENTIALS);
+        UserAccounts accounts = getUserAccountsForCaller();
         AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription> authenticatorInfo =
             mAuthenticatorCache.getServiceInfo(
                     AuthenticatorDescription.newKey(account.type));
@@ -946,7 +1036,7 @@
             // if the caller has permission, do the peek. otherwise go the more expensive
             // route of starting a Session
             if (!customTokens && permissionGranted) {
-                String authToken = readAuthTokenInternal(account, authTokenType);
+                String authToken = readAuthTokenInternal(accounts, account, authTokenType);
                 if (authToken != null) {
                     Bundle result = new Bundle();
                     result.putString(AccountManager.KEY_AUTHTOKEN, authToken);
@@ -957,7 +1047,7 @@
                 }
             }
 
-            new Session(response, account.type, expectActivityLaunch,
+            new Session(accounts, response, account.type, expectActivityLaunch,
                     false /* stripAuthTokenFromResult */) {
                 protected String toDebugString(long now) {
                     if (loginOptions != null) loginOptions.keySet();
@@ -1000,14 +1090,14 @@
                                 return;
                             }
                             if (!customTokens) {
-                                saveAuthTokenToDatabase(new Account(name, type),
+                                saveAuthTokenToDatabase(mAccounts, new Account(name, type),
                                         authTokenType, authToken);
                             }
                         }
 
                         Intent intent = result.getParcelable(AccountManager.KEY_INTENT);
                         if (intent != null && notifyOnAuthFailure && !customTokens) {
-                            doNotification(
+                            doNotification(mAccounts,
                                     account, result.getString(AccountManager.KEY_AUTH_FAILED_MESSAGE),
                                     intent);
                         }
@@ -1090,26 +1180,27 @@
     private Integer getCredentialPermissionNotificationId(Account account, String authTokenType,
             int uid) {
         Integer id;
-        synchronized(mCredentialsPermissionNotificationIds) {
+        UserAccounts accounts = getUserAccounts(UserId.getUserId(uid));
+        synchronized (accounts.credentialsPermissionNotificationIds) {
             final Pair<Pair<Account, String>, Integer> key =
                     new Pair<Pair<Account, String>, Integer>(
                             new Pair<Account, String>(account, authTokenType), uid);
-            id = mCredentialsPermissionNotificationIds.get(key);
+            id = accounts.credentialsPermissionNotificationIds.get(key);
             if (id == null) {
                 id = mNotificationIds.incrementAndGet();
-                mCredentialsPermissionNotificationIds.put(key, id);
+                accounts.credentialsPermissionNotificationIds.put(key, id);
             }
         }
         return id;
     }
 
-    private Integer getSigninRequiredNotificationId(Account account) {
+    private Integer getSigninRequiredNotificationId(UserAccounts accounts, Account account) {
         Integer id;
-        synchronized(mSigninRequiredNotificationIds) {
-            id = mSigninRequiredNotificationIds.get(account);
+        synchronized (accounts.signinRequiredNotificationIds) {
+            id = accounts.signinRequiredNotificationIds.get(account);
             if (id == null) {
                 id = mNotificationIds.incrementAndGet();
-                mSigninRequiredNotificationIds.put(account, id);
+                accounts.signinRequiredNotificationIds.put(account, id);
             }
         }
         return id;
@@ -1131,6 +1222,7 @@
         if (accountType == null) throw new IllegalArgumentException("accountType is null");
         checkManageAccountsPermission();
 
+        UserAccounts accounts = getUserAccountsForCaller();
         final int pid = Binder.getCallingPid();
         final int uid = Binder.getCallingUid();
         final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn;
@@ -1139,7 +1231,7 @@
 
         long identityToken = clearCallingIdentity();
         try {
-            new Session(response, accountType, expectActivityLaunch,
+            new Session(accounts, response, accountType, expectActivityLaunch,
                     true /* stripAuthTokenFromResult */) {
                 public void run() throws RemoteException {
                     mAuthenticator.addAccount(this, mAccountType, authTokenType, requiredFeatures,
@@ -1172,9 +1264,10 @@
         if (response == null) throw new IllegalArgumentException("response is null");
         if (account == null) throw new IllegalArgumentException("account is null");
         checkManageAccountsPermission();
+        UserAccounts accounts = getUserAccountsForCaller();
         long identityToken = clearCallingIdentity();
         try {
-            new Session(response, account.type, expectActivityLaunch,
+            new Session(accounts, response, account.type, expectActivityLaunch,
                     true /* stripAuthTokenFromResult */) {
                 public void run() throws RemoteException {
                     mAuthenticator.confirmCredentials(this, account, options);
@@ -1204,9 +1297,10 @@
         if (account == null) throw new IllegalArgumentException("account is null");
         if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
         checkManageAccountsPermission();
+        UserAccounts accounts = getUserAccountsForCaller();
         long identityToken = clearCallingIdentity();
         try {
-            new Session(response, account.type, expectActivityLaunch,
+            new Session(accounts, response, account.type, expectActivityLaunch,
                     true /* stripAuthTokenFromResult */) {
                 public void run() throws RemoteException {
                     mAuthenticator.updateCredentials(this, account, authTokenType, loginOptions);
@@ -1236,9 +1330,10 @@
         if (response == null) throw new IllegalArgumentException("response is null");
         if (accountType == null) throw new IllegalArgumentException("accountType is null");
         checkManageAccountsPermission();
+        UserAccounts accounts = getUserAccountsForCaller();
         long identityToken = clearCallingIdentity();
         try {
-            new Session(response, accountType, expectActivityLaunch,
+            new Session(accounts, response, accountType, expectActivityLaunch,
                     true /* stripAuthTokenFromResult */) {
                 public void run() throws RemoteException {
                     mAuthenticator.editProperties(this, mAccountType);
@@ -1259,16 +1354,16 @@
         private volatile ArrayList<Account> mAccountsWithFeatures = null;
         private volatile int mCurrentAccount = 0;
 
-        public GetAccountsByTypeAndFeatureSession(IAccountManagerResponse response,
-            String type, String[] features) {
-            super(response, type, false /* expectActivityLaunch */,
+        public GetAccountsByTypeAndFeatureSession(UserAccounts accounts,
+                IAccountManagerResponse response, String type, String[] features) {
+            super(accounts, response, type, false /* expectActivityLaunch */,
                     true /* stripAuthTokenFromResult */);
             mFeatures = features;
         }
 
         public void run() throws RemoteException {
-            synchronized (mCacheLock) {
-                mAccountsOfType = getAccountsFromCacheLocked(mAccountType);
+            synchronized (mAccounts.cacheLock) {
+                mAccountsOfType = getAccountsFromCacheLocked(mAccounts, mAccountType);
             }
             // check whether each account matches the requested features
             mAccountsWithFeatures = new ArrayList<Account>(mAccountsOfType.length);
@@ -1346,6 +1441,23 @@
         }
     }
 
+    /**
+     * Returns the accounts for a specific user
+     * @hide
+     */
+    public Account[] getAccounts(int userId) {
+        checkReadAccountsPermission();
+        UserAccounts accounts = getUserAccounts(userId);
+        long identityToken = clearCallingIdentity();
+        try {
+            synchronized (accounts.cacheLock) {
+                return getAccountsFromCacheLocked(accounts, null);
+            }
+        } finally {
+            restoreCallingIdentity(identityToken);
+        }
+    }
+
     public Account[] getAccounts(String type) {
         if (Log.isLoggable(TAG, Log.VERBOSE)) {
             Log.v(TAG, "getAccounts: accountType " + type
@@ -1353,10 +1465,11 @@
                     + ", pid " + Binder.getCallingPid());
         }
         checkReadAccountsPermission();
+        UserAccounts accounts = getUserAccountsForCaller();
         long identityToken = clearCallingIdentity();
         try {
-            synchronized (mCacheLock) {
-                return getAccountsFromCacheLocked(type);
+            synchronized (accounts.cacheLock) {
+                return getAccountsFromCacheLocked(accounts, type);
             }
         } finally {
             restoreCallingIdentity(identityToken);
@@ -1375,19 +1488,20 @@
         if (response == null) throw new IllegalArgumentException("response is null");
         if (type == null) throw new IllegalArgumentException("accountType is null");
         checkReadAccountsPermission();
+        UserAccounts userAccounts = getUserAccountsForCaller();
         long identityToken = clearCallingIdentity();
         try {
             if (features == null || features.length == 0) {
                 Account[] accounts;
-                synchronized (mCacheLock) {
-                    accounts = getAccountsFromCacheLocked(type);
+                synchronized (userAccounts.cacheLock) {
+                    accounts = getAccountsFromCacheLocked(userAccounts, type);
                 }
                 Bundle result = new Bundle();
                 result.putParcelableArray(AccountManager.KEY_ACCOUNTS, accounts);
                 onResult(response, result);
                 return;
             }
-            new GetAccountsByTypeAndFeatureSession(response, type, features).bind();
+            new GetAccountsByTypeAndFeatureSession(userAccounts, response, type, features).bind();
         } finally {
             restoreCallingIdentity(identityToken);
         }
@@ -1435,12 +1549,14 @@
         IAccountAuthenticator mAuthenticator = null;
 
         private final boolean mStripAuthTokenFromResult;
+        protected final UserAccounts mAccounts;
 
-        public Session(IAccountManagerResponse response, String accountType,
+        public Session(UserAccounts accounts, IAccountManagerResponse response, String accountType,
                 boolean expectActivityLaunch, boolean stripAuthTokenFromResult) {
             super();
             if (response == null) throw new IllegalArgumentException("response is null");
             if (accountType == null) throw new IllegalArgumentException("accountType is null");
+            mAccounts = accounts;
             mStripAuthTokenFromResult = stripAuthTokenFromResult;
             mResponse = response;
             mAccountType = accountType;
@@ -1578,7 +1694,7 @@
                 String accountType = result.getString(AccountManager.KEY_ACCOUNT_TYPE);
                 if (!TextUtils.isEmpty(accountName) && !TextUtils.isEmpty(accountType)) {
                     Account account = new Account(accountName, accountType);
-                    cancelNotification(getSigninRequiredNotificationId(account));
+                    cancelNotification(getSigninRequiredNotificationId(mAccounts, account));
                 }
             }
             IAccountManagerResponse response;
@@ -1694,20 +1810,23 @@
         }
     }
 
-    private static String getDatabaseName() {
-        if(Environment.isEncryptedFilesystemEnabled()) {
-            // Hard-coded path in case of encrypted file system
-            return Environment.getSystemSecureDirectory().getPath() + File.separator + DATABASE_NAME;
-        } else {
-            // Regular path in case of non-encrypted file system
-            return DATABASE_NAME;
+    private static String getDatabaseName(int userId) {
+        File systemDir = Environment.getSystemSecureDirectory();
+        File databaseFile = new File(systemDir, "users/" + userId + "/" + DATABASE_NAME);
+        if (userId == 0) {
+            // Migrate old file, if it exists, to the new location
+            File oldFile = new File(systemDir, DATABASE_NAME);
+            if (oldFile.exists()) {
+                oldFile.renameTo(databaseFile);
+            }
         }
+        return databaseFile.getPath();
     }
 
-    private class DatabaseHelper extends SQLiteOpenHelper {
+    static class DatabaseHelper extends SQLiteOpenHelper {
 
-        public DatabaseHelper(Context context) {
-            super(context, AccountManagerService.getDatabaseName(), null, DATABASE_VERSION);
+        public DatabaseHelper(Context context, int userId) {
+            super(context, AccountManagerService.getDatabaseName(userId), null, DATABASE_VERSION);
         }
 
         /**
@@ -1799,15 +1918,6 @@
         }
     }
 
-    private void setMetaValue(String key, String value) {
-        ContentValues values = new ContentValues();
-        values.put(META_KEY, key);
-        values.put(META_VALUE, value);
-        synchronized (mCacheLock) {
-            mOpenHelper.getWritableDatabase().replace(TABLE_META, META_KEY, values);
-        }
-    }
-
     public IBinder onBind(Intent intent) {
         return asBinder();
     }
@@ -1837,11 +1947,25 @@
                     + " without permission " + android.Manifest.permission.DUMP);
             return;
         }
+        final boolean isCheckinRequest = scanArgs(args, "--checkin") || scanArgs(args, "-c");
 
-        synchronized (mCacheLock) {
-            final SQLiteDatabase db = mOpenHelper.getReadableDatabase();
+        fout = new IndentingPrintWriter(fout, "  ");
+        int size = mUsers.size();
+        for (int i = 0; i < size; i++) {
+            fout.println("User " + mUsers.keyAt(i) + ":");
+            ((IndentingPrintWriter) fout).increaseIndent();
+            dumpUser(mUsers.valueAt(i), fd, fout, args, isCheckinRequest);
+            ((IndentingPrintWriter) fout).decreaseIndent();
+            if (i < size - 1) {
+                fout.println();
+            }
+        }
+    }
 
-            final boolean isCheckinRequest = scanArgs(args, "--checkin") || scanArgs(args, "-c");
+    private void dumpUser(UserAccounts userAccounts, FileDescriptor fd, PrintWriter fout,
+            String[] args, boolean isCheckinRequest) {
+        synchronized (userAccounts.cacheLock) {
+            final SQLiteDatabase db = userAccounts.openHelper.getReadableDatabase();
 
             if (isCheckinRequest) {
                 // This is a checkin request. *Only* upload the account types and the count of each.
@@ -1858,7 +1982,7 @@
                     }
                 }
             } else {
-                Account[] accounts = getAccountsFromCacheLocked(null /* type */);
+                Account[] accounts = getAccountsFromCacheLocked(userAccounts, null /* type */);
                 fout.println("Accounts: " + accounts.length);
                 for (Account account : accounts) {
                     fout.println("  " + account);
@@ -1879,7 +2003,8 @@
         }
     }
 
-    private void doNotification(Account account, CharSequence message, Intent intent) {
+    private void doNotification(UserAccounts accounts, Account account, CharSequence message,
+            Intent intent) {
         long identityToken = clearCallingIdentity();
         try {
             if (Log.isLoggable(TAG, Log.VERBOSE)) {
@@ -1891,7 +2016,7 @@
                             intent.getComponent().getClassName())) {
                 createNoCredentialsPermissionNotification(account, intent);
             } else {
-                final Integer notificationId = getSigninRequiredNotificationId(account);
+                final Integer notificationId = getSigninRequiredNotificationId(accounts, account);
                 intent.addCategory(String.valueOf(notificationId));
                 Notification n = new Notification(android.R.drawable.stat_sys_warning, null,
                         0 /* when */);
@@ -1962,7 +2087,7 @@
         final boolean fromAuthenticator = account != null
                 && hasAuthenticatorUid(account.type, callerUid);
         final boolean hasExplicitGrants = account != null
-                && hasExplicitlyGrantedPermission(account, authTokenType);
+                && hasExplicitlyGrantedPermission(account, authTokenType, callerUid);
         if (Log.isLoggable(TAG, Log.VERBOSE)) {
             Log.v(TAG, "checkGrantsOrCallingUidAgainstAuthenticator: caller uid "
                     + callerUid + ", " + account
@@ -1984,13 +2109,15 @@
         return false;
     }
 
-    private boolean hasExplicitlyGrantedPermission(Account account, String authTokenType) {
-        if (Binder.getCallingUid() == android.os.Process.SYSTEM_UID) {
+    private boolean hasExplicitlyGrantedPermission(Account account, String authTokenType,
+            int callerUid) {
+        if (callerUid == android.os.Process.SYSTEM_UID) {
             return true;
         }
-        synchronized (mCacheLock) {
-            final SQLiteDatabase db = mOpenHelper.getReadableDatabase();
-            String[] args = {String.valueOf(Binder.getCallingUid()), authTokenType,
+        UserAccounts accounts = getUserAccountsForCaller();
+        synchronized (accounts.cacheLock) {
+            final SQLiteDatabase db = accounts.openHelper.getReadableDatabase();
+            String[] args = { String.valueOf(callerUid), authTokenType,
                     account.name, account.type};
             final boolean permissionGranted =
                     DatabaseUtils.longForQuery(db, COUNT_OF_MATCHING_GRANTS, args) != 0;
@@ -1998,7 +2125,7 @@
                 // TODO: Skip this check when running automated tests. Replace this
                 // with a more general solution.
                 Log.d(TAG, "no credentials permission for usage of " + account + ", "
-                        + authTokenType + " by uid " + Binder.getCallingUid()
+                        + authTokenType + " by uid " + callerUid
                         + " but ignoring since device is in test harness.");
                 return true;
             }
@@ -2048,8 +2175,9 @@
             Log.e(TAG, "grantAppPermission: called with invalid arguments", new Exception());
             return;
         }
-        synchronized (mCacheLock) {
-            final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
+        UserAccounts accounts = getUserAccounts(UserId.getUserId(uid));
+        synchronized (accounts.cacheLock) {
+            final SQLiteDatabase db = accounts.openHelper.getWritableDatabase();
             db.beginTransaction();
             try {
                 long accountId = getAccountIdLocked(db, account);
@@ -2081,8 +2209,9 @@
             Log.e(TAG, "revokeAppPermission: called with invalid arguments", new Exception());
             return;
         }
-        synchronized (mCacheLock) {
-            final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
+        UserAccounts accounts = getUserAccounts(UserId.getUserId(uid));
+        synchronized (accounts.cacheLock) {
+            final SQLiteDatabase db = accounts.openHelper.getWritableDatabase();
             db.beginTransaction();
             try {
                 long accountId = getAccountIdLocked(db, account);
@@ -2105,8 +2234,8 @@
         return value != null ? ("[" + TextUtils.join(",", value) + "]") : null;
     }
 
-    private void removeAccountFromCacheLocked(Account account) {
-        final Account[] oldAccountsForType = mAccountCache.get(account.type);
+    private void removeAccountFromCacheLocked(UserAccounts accounts, Account account) {
+        final Account[] oldAccountsForType = accounts.accountCache.get(account.type);
         if (oldAccountsForType != null) {
             ArrayList<Account> newAccountsList = new ArrayList<Account>();
             for (Account curAccount : oldAccountsForType) {
@@ -2115,34 +2244,34 @@
                 }
             }
             if (newAccountsList.isEmpty()) {
-                mAccountCache.remove(account.type);
+                accounts.accountCache.remove(account.type);
             } else {
                 Account[] newAccountsForType = new Account[newAccountsList.size()];
                 newAccountsForType = newAccountsList.toArray(newAccountsForType);
-                mAccountCache.put(account.type, newAccountsForType);
+                accounts.accountCache.put(account.type, newAccountsForType);
             }
         }
-        mUserDataCache.remove(account);
-        mAuthTokenCache.remove(account);
+        accounts.userDataCache.remove(account);
+        accounts.authTokenCache.remove(account);
     }
 
     /**
      * This assumes that the caller has already checked that the account is not already present.
      */
-    private void insertAccountIntoCacheLocked(Account account) {
-        Account[] accountsForType = mAccountCache.get(account.type);
+    private void insertAccountIntoCacheLocked(UserAccounts accounts, Account account) {
+        Account[] accountsForType = accounts.accountCache.get(account.type);
         int oldLength = (accountsForType != null) ? accountsForType.length : 0;
         Account[] newAccountsForType = new Account[oldLength + 1];
         if (accountsForType != null) {
             System.arraycopy(accountsForType, 0, newAccountsForType, 0, oldLength);
         }
         newAccountsForType[oldLength] = account;
-        mAccountCache.put(account.type, newAccountsForType);
+        accounts.accountCache.put(account.type, newAccountsForType);
     }
 
-    protected Account[] getAccountsFromCacheLocked(String accountType) {
+    protected Account[] getAccountsFromCacheLocked(UserAccounts userAccounts, String accountType) {
         if (accountType != null) {
-            final Account[] accounts = mAccountCache.get(accountType);
+            final Account[] accounts = userAccounts.accountCache.get(accountType);
             if (accounts == null) {
                 return EMPTY_ACCOUNT_ARRAY;
             } else {
@@ -2150,7 +2279,7 @@
             }
         } else {
             int totalLength = 0;
-            for (Account[] accounts : mAccountCache.values()) {
+            for (Account[] accounts : userAccounts.accountCache.values()) {
                 totalLength += accounts.length;
             }
             if (totalLength == 0) {
@@ -2158,7 +2287,7 @@
             }
             Account[] accounts = new Account[totalLength];
             totalLength = 0;
-            for (Account[] accountsOfType : mAccountCache.values()) {
+            for (Account[] accountsOfType : userAccounts.accountCache.values()) {
                 System.arraycopy(accountsOfType, 0, accounts, totalLength,
                         accountsOfType.length);
                 totalLength += accountsOfType.length;
@@ -2167,12 +2296,12 @@
         }
     }
 
-    protected void writeUserDataIntoCacheLocked(final SQLiteDatabase db, Account account,
-            String key, String value) {
-        HashMap<String, String> userDataForAccount = mUserDataCache.get(account);
+    protected void writeUserDataIntoCacheLocked(UserAccounts accounts, final SQLiteDatabase db,
+            Account account, String key, String value) {
+        HashMap<String, String> userDataForAccount = accounts.userDataCache.get(account);
         if (userDataForAccount == null) {
             userDataForAccount = readUserDataForAccountFromDatabaseLocked(db, account);
-            mUserDataCache.put(account, userDataForAccount);
+            accounts.userDataCache.put(account, userDataForAccount);
         }
         if (value == null) {
             userDataForAccount.remove(key);
@@ -2181,12 +2310,12 @@
         }
     }
 
-    protected void writeAuthTokenIntoCacheLocked(final SQLiteDatabase db, Account account,
-            String key, String value) {
-        HashMap<String, String> authTokensForAccount = mAuthTokenCache.get(account);
+    protected void writeAuthTokenIntoCacheLocked(UserAccounts accounts, final SQLiteDatabase db,
+            Account account, String key, String value) {
+        HashMap<String, String> authTokensForAccount = accounts.authTokenCache.get(account);
         if (authTokensForAccount == null) {
             authTokensForAccount = readAuthTokensForAccountFromDatabaseLocked(db, account);
-            mAuthTokenCache.put(account, authTokensForAccount);
+            accounts.authTokenCache.put(account, authTokensForAccount);
         }
         if (value == null) {
             authTokensForAccount.remove(key);
@@ -2195,27 +2324,28 @@
         }
     }
 
-    protected String readAuthTokenInternal(Account account, String authTokenType) {
-        synchronized (mCacheLock) {
-            HashMap<String, String> authTokensForAccount = mAuthTokenCache.get(account);
+    protected String readAuthTokenInternal(UserAccounts accounts, Account account,
+            String authTokenType) {
+        synchronized (accounts.cacheLock) {
+            HashMap<String, String> authTokensForAccount = accounts.authTokenCache.get(account);
             if (authTokensForAccount == null) {
                 // need to populate the cache for this account
-                final SQLiteDatabase db = mOpenHelper.getReadableDatabase();
+                final SQLiteDatabase db = accounts.openHelper.getReadableDatabase();
                 authTokensForAccount = readAuthTokensForAccountFromDatabaseLocked(db, account);
-                mAuthTokenCache.put(account, authTokensForAccount);
+                accounts.authTokenCache.put(account, authTokensForAccount);
             }
             return authTokensForAccount.get(authTokenType);
         }
     }
 
-    protected String readUserDataInternal(Account account, String key) {
-        synchronized (mCacheLock) {
-            HashMap<String, String> userDataForAccount = mUserDataCache.get(account);
+    protected String readUserDataInternal(UserAccounts accounts, Account account, String key) {
+        synchronized (accounts.cacheLock) {
+            HashMap<String, String> userDataForAccount = accounts.userDataCache.get(account);
             if (userDataForAccount == null) {
                 // need to populate the cache for this account
-                final SQLiteDatabase db = mOpenHelper.getReadableDatabase();
+                final SQLiteDatabase db = accounts.openHelper.getReadableDatabase();
                 userDataForAccount = readUserDataForAccountFromDatabaseLocked(db, account);
-                mUserDataCache.put(account, userDataForAccount);
+                accounts.userDataCache.put(account, userDataForAccount);
             }
             return userDataForAccount.get(key);
         }
diff --git a/core/java/android/accounts/GrantCredentialsPermissionActivity.java b/core/java/android/accounts/GrantCredentialsPermissionActivity.java
index 0ee683c..4419c8c 100644
--- a/core/java/android/accounts/GrantCredentialsPermissionActivity.java
+++ b/core/java/android/accounts/GrantCredentialsPermissionActivity.java
@@ -113,8 +113,7 @@
             }
         };
 
-        accountManagerService.getAuthTokenLabel(
-                response, mAccount, mAuthTokenType);
+        accountManagerService.getAuthTokenLabel(response, mAccount, mAuthTokenType, mUid);
 
         findViewById(R.id.allow_button).setOnClickListener(this);
         findViewById(R.id.deny_button).setOnClickListener(this);
diff --git a/core/java/android/accounts/OnAccountsUpdateListener.java b/core/java/android/accounts/OnAccountsUpdateListener.java
index 38b371d..2b4ee50 100644
--- a/core/java/android/accounts/OnAccountsUpdateListener.java
+++ b/core/java/android/accounts/OnAccountsUpdateListener.java
@@ -17,11 +17,11 @@
 package android.accounts;
 
 /**
- * An interface that contains the callback used by the AccountMonitor
+ * An interface that contains the callback used by the AccountManager
  */
 public interface OnAccountsUpdateListener {
     /**
-     * This invoked when the AccountMonitor starts up and whenever the account
+     * This invoked when the AccountManager starts up and whenever the account
      * set changes.
      * @param accounts the current accounts
      */
diff --git a/core/java/android/content/ContentService.java b/core/java/android/content/ContentService.java
index fc4c262..f827c3d 100644
--- a/core/java/android/content/ContentService.java
+++ b/core/java/android/content/ContentService.java
@@ -26,6 +26,7 @@
 import android.os.Parcel;
 import android.os.RemoteException;
 import android.os.ServiceManager;
+import android.os.UserId;
 import android.util.Log;
 import android.util.SparseIntArray;
 import android.Manifest;
@@ -163,6 +164,8 @@
             Log.v(TAG, "Notifying update of " + uri + " from observer " + observer
                     + ", syncToNetwork " + syncToNetwork);
         }
+
+        int userId = UserId.getCallingUserId();
         // This makes it so that future permission checks will be in the context of this
         // process rather than the caller's process. We will restore this before returning.
         long identityToken = clearCallingIdentity();
@@ -201,7 +204,8 @@
             if (syncToNetwork) {
                 SyncManager syncManager = getSyncManager();
                 if (syncManager != null) {
-                    syncManager.scheduleLocalSync(null /* all accounts */, uri.getAuthority());
+                    syncManager.scheduleLocalSync(null /* all accounts */, userId,
+                            uri.getAuthority());
                 }
             }
         } finally {
@@ -229,13 +233,15 @@
 
     public void requestSync(Account account, String authority, Bundle extras) {
         ContentResolver.validateSyncExtrasBundle(extras);
+        int userId = UserId.getCallingUserId();
+
         // This makes it so that future permission checks will be in the context of this
         // process rather than the caller's process. We will restore this before returning.
         long identityToken = clearCallingIdentity();
         try {
             SyncManager syncManager = getSyncManager();
             if (syncManager != null) {
-                syncManager.scheduleSync(account, authority, extras, 0 /* no delay */,
+                syncManager.scheduleSync(account, userId, authority, extras, 0 /* no delay */,
                         false /* onlyThoseWithUnkownSyncableState */);
             }
         } finally {
@@ -250,14 +256,16 @@
      * @param authority filter the pending and active syncs to cancel using this authority
      */
     public void cancelSync(Account account, String authority) {
+        int userId = UserId.getCallingUserId();
+
         // This makes it so that future permission checks will be in the context of this
         // process rather than the caller's process. We will restore this before returning.
         long identityToken = clearCallingIdentity();
         try {
             SyncManager syncManager = getSyncManager();
             if (syncManager != null) {
-                syncManager.clearScheduledSyncOperations(account, authority);
-                syncManager.cancelActiveSync(account, authority);
+                syncManager.clearScheduledSyncOperations(account, userId, authority);
+                syncManager.cancelActiveSync(account, userId, authority);
             }
         } finally {
             restoreCallingIdentity(identityToken);
@@ -283,12 +291,14 @@
     public boolean getSyncAutomatically(Account account, String providerName) {
         mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_SETTINGS,
                 "no permission to read the sync settings");
+        int userId = UserId.getCallingUserId();
+
         long identityToken = clearCallingIdentity();
         try {
             SyncManager syncManager = getSyncManager();
             if (syncManager != null) {
                 return syncManager.getSyncStorageEngine().getSyncAutomatically(
-                        account, providerName);
+                        account, userId, providerName);
             }
         } finally {
             restoreCallingIdentity(identityToken);
@@ -299,12 +309,14 @@
     public void setSyncAutomatically(Account account, String providerName, boolean sync) {
         mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS,
                 "no permission to write the sync settings");
+        int userId = UserId.getCallingUserId();
+
         long identityToken = clearCallingIdentity();
         try {
             SyncManager syncManager = getSyncManager();
             if (syncManager != null) {
                 syncManager.getSyncStorageEngine().setSyncAutomatically(
-                        account, providerName, sync);
+                        account, userId, providerName, sync);
             }
         } finally {
             restoreCallingIdentity(identityToken);
@@ -315,10 +327,12 @@
             long pollFrequency) {
         mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS,
                 "no permission to write the sync settings");
+        int userId = UserId.getCallingUserId();
+
         long identityToken = clearCallingIdentity();
         try {
             getSyncManager().getSyncStorageEngine().addPeriodicSync(
-                    account, authority, extras, pollFrequency);
+                    account, userId, authority, extras, pollFrequency);
         } finally {
             restoreCallingIdentity(identityToken);
         }
@@ -327,9 +341,12 @@
     public void removePeriodicSync(Account account, String authority, Bundle extras) {
         mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS,
                 "no permission to write the sync settings");
+        int userId = UserId.getCallingUserId();
+
         long identityToken = clearCallingIdentity();
         try {
-            getSyncManager().getSyncStorageEngine().removePeriodicSync(account, authority, extras);
+            getSyncManager().getSyncStorageEngine().removePeriodicSync(account, userId, authority,
+                    extras);
         } finally {
             restoreCallingIdentity(identityToken);
         }
@@ -338,10 +355,12 @@
     public List<PeriodicSync> getPeriodicSyncs(Account account, String providerName) {
         mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_SETTINGS,
                 "no permission to read the sync settings");
+        int userId = UserId.getCallingUserId();
+
         long identityToken = clearCallingIdentity();
         try {
             return getSyncManager().getSyncStorageEngine().getPeriodicSyncs(
-                    account, providerName);
+                    account, userId, providerName);
         } finally {
             restoreCallingIdentity(identityToken);
         }
@@ -350,12 +369,14 @@
     public int getIsSyncable(Account account, String providerName) {
         mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_SETTINGS,
                 "no permission to read the sync settings");
+        int userId = UserId.getCallingUserId();
+
         long identityToken = clearCallingIdentity();
         try {
             SyncManager syncManager = getSyncManager();
             if (syncManager != null) {
                 return syncManager.getSyncStorageEngine().getIsSyncable(
-                        account, providerName);
+                        account, userId, providerName);
             }
         } finally {
             restoreCallingIdentity(identityToken);
@@ -366,12 +387,14 @@
     public void setIsSyncable(Account account, String providerName, int syncable) {
         mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS,
                 "no permission to write the sync settings");
+        int userId = UserId.getCallingUserId();
+
         long identityToken = clearCallingIdentity();
         try {
             SyncManager syncManager = getSyncManager();
             if (syncManager != null) {
                 syncManager.getSyncStorageEngine().setIsSyncable(
-                        account, providerName, syncable);
+                        account, userId, providerName, syncable);
             }
         } finally {
             restoreCallingIdentity(identityToken);
@@ -381,11 +404,13 @@
     public boolean getMasterSyncAutomatically() {
         mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_SETTINGS,
                 "no permission to read the sync settings");
+        int userId = UserId.getCallingUserId();
+
         long identityToken = clearCallingIdentity();
         try {
             SyncManager syncManager = getSyncManager();
             if (syncManager != null) {
-                return syncManager.getSyncStorageEngine().getMasterSyncAutomatically();
+                return syncManager.getSyncStorageEngine().getMasterSyncAutomatically(userId);
             }
         } finally {
             restoreCallingIdentity(identityToken);
@@ -396,11 +421,13 @@
     public void setMasterSyncAutomatically(boolean flag) {
         mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS,
                 "no permission to write the sync settings");
+        int userId = UserId.getCallingUserId();
+
         long identityToken = clearCallingIdentity();
         try {
             SyncManager syncManager = getSyncManager();
             if (syncManager != null) {
-                syncManager.getSyncStorageEngine().setMasterSyncAutomatically(flag);
+                syncManager.getSyncStorageEngine().setMasterSyncAutomatically(flag, userId);
             }
         } finally {
             restoreCallingIdentity(identityToken);
@@ -410,12 +437,14 @@
     public boolean isSyncActive(Account account, String authority) {
         mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_STATS,
                 "no permission to read the sync stats");
+        int userId = UserId.getCallingUserId();
+
         long identityToken = clearCallingIdentity();
         try {
             SyncManager syncManager = getSyncManager();
             if (syncManager != null) {
                 return syncManager.getSyncStorageEngine().isSyncActive(
-                        account, authority);
+                        account, userId, authority);
             }
         } finally {
             restoreCallingIdentity(identityToken);
@@ -426,9 +455,11 @@
     public List<SyncInfo> getCurrentSyncs() {
         mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_STATS,
                 "no permission to read the sync stats");
+        int userId = UserId.getCallingUserId();
+
         long identityToken = clearCallingIdentity();
         try {
-            return getSyncManager().getSyncStorageEngine().getCurrentSyncs();
+            return getSyncManager().getSyncStorageEngine().getCurrentSyncs(userId);
         } finally {
             restoreCallingIdentity(identityToken);
         }
@@ -437,12 +468,14 @@
     public SyncStatusInfo getSyncStatus(Account account, String authority) {
         mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_STATS,
                 "no permission to read the sync stats");
+        int userId = UserId.getCallingUserId();
+
         long identityToken = clearCallingIdentity();
         try {
             SyncManager syncManager = getSyncManager();
             if (syncManager != null) {
                 return syncManager.getSyncStorageEngine().getStatusByAccountAndAuthority(
-                    account, authority);
+                        account, userId, authority);
             }
         } finally {
             restoreCallingIdentity(identityToken);
@@ -453,11 +486,13 @@
     public boolean isSyncPending(Account account, String authority) {
         mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_STATS,
                 "no permission to read the sync stats");
+        int userId = UserId.getCallingUserId();
+
         long identityToken = clearCallingIdentity();
         try {
             SyncManager syncManager = getSyncManager();
             if (syncManager != null) {
-                return syncManager.getSyncStorageEngine().isSyncPending(account, authority);
+                return syncManager.getSyncStorageEngine().isSyncPending(account, userId, authority);
             }
         } finally {
             restoreCallingIdentity(identityToken);
diff --git a/core/java/android/content/SyncManager.java b/core/java/android/content/SyncManager.java
index ba24036..b7dfe92 100644
--- a/core/java/android/content/SyncManager.java
+++ b/core/java/android/content/SyncManager.java
@@ -23,18 +23,23 @@
 
 import android.accounts.Account;
 import android.accounts.AccountManager;
+import android.accounts.AccountManagerService;
 import android.accounts.OnAccountsUpdateListener;
 import android.app.ActivityManager;
 import android.app.AlarmManager;
+import android.app.AppGlobals;
 import android.app.Notification;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
+import android.app.DownloadManager.Request;
+import android.content.SyncStorageEngine.OnSyncRequestListener;
 import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager;
 import android.content.pm.ProviderInfo;
 import android.content.pm.RegisteredServicesCache;
 import android.content.pm.RegisteredServicesCacheListener;
 import android.content.pm.ResolveInfo;
+import android.content.pm.UserInfo;
 import android.net.ConnectivityManager;
 import android.net.NetworkInfo;
 import android.os.Bundle;
@@ -48,6 +53,7 @@
 import android.os.RemoteException;
 import android.os.SystemClock;
 import android.os.SystemProperties;
+import android.os.UserId;
 import android.os.WorkSource;
 import android.provider.Settings;
 import android.text.format.DateUtils;
@@ -132,7 +138,9 @@
 
     private Context mContext;
 
-    private volatile Account[] mAccounts = INITIAL_ACCOUNTS_ARRAY;
+    private static final AccountAndUser[] INITIAL_ACCOUNTS_ARRAY = new AccountAndUser[0];
+
+    private volatile AccountAndUser[] mAccounts = INITIAL_ACCOUNTS_ARRAY;
 
     volatile private PowerManager.WakeLock mHandleAlarmWakeLock;
     volatile private PowerManager.WakeLock mSyncManagerWakeLock;
@@ -166,7 +174,8 @@
                             Log.v(TAG, "Internal storage is low.");
                         }
                         mStorageIsLow = true;
-                        cancelActiveSync(null /* any account */, null /* any authority */);
+                        cancelActiveSync(null /* any account */, UserId.USER_ALL,
+                                null /* any authority */);
                     } else if (Intent.ACTION_DEVICE_STORAGE_OK.equals(action)) {
                         if (Log.isLoggable(TAG, Log.VERBOSE)) {
                             Log.v(TAG, "Internal storage is ok.");
@@ -186,28 +195,73 @@
     private BroadcastReceiver mBackgroundDataSettingChanged = new BroadcastReceiver() {
         public void onReceive(Context context, Intent intent) {
             if (getConnectivityManager().getBackgroundDataSetting()) {
-                scheduleSync(null /* account */, null /* authority */, new Bundle(), 0 /* delay */,
+                scheduleSync(null /* account */, UserId.USER_ALL, null /* authority */,
+                        new Bundle(), 0 /* delay */,
                         false /* onlyThoseWithUnknownSyncableState */);
             }
         }
     };
 
-    private static final Account[] INITIAL_ACCOUNTS_ARRAY = new Account[0];
-
     private final PowerManager mPowerManager;
 
     private static final long SYNC_ALARM_TIMEOUT_MIN = 30 * 1000; // 30 seconds
     private static final long SYNC_ALARM_TIMEOUT_MAX = 2 * 60 * 60 * 1000; // two hours
 
+    private List<UserInfo> getAllUsers() {
+        try {
+            return AppGlobals.getPackageManager().getUsers();
+        } catch (RemoteException re) {
+            // Local to system process, shouldn't happen
+        }
+        return null;
+    }
+
+    private boolean containsAccountAndUser(AccountAndUser[] accounts, Account account, int userId) {
+        boolean found = false;
+        for (int i = 0; i < accounts.length; i++) {
+            if (accounts[i].userId == userId
+                    && accounts[i].account.equals(account)) {
+                found = true;
+                break;
+            }
+        }
+        return found;
+    }
+
     public void onAccountsUpdated(Account[] accounts) {
         // remember if this was the first time this was called after an update
         final boolean justBootedUp = mAccounts == INITIAL_ACCOUNTS_ARRAY;
-        mAccounts = accounts;
 
-        // if a sync is in progress yet it is no longer in the accounts list,
-        // cancel it
+        List<UserInfo> users = getAllUsers();
+        if (users == null)  return;
+
+        int count = 0;
+
+        // For all known users on the system, get their accounts and add them to the list
+        // TODO: Limit this to active users, when such a concept exists.
+        for (UserInfo user : users) {
+            accounts = AccountManagerService.getSingleton().getAccounts(user.id);
+            count += accounts.length;
+        }
+
+        AccountAndUser[] allAccounts = new AccountAndUser[count];
+        int index = 0;
+        for (UserInfo user : users) {
+            accounts = AccountManagerService.getSingleton().getAccounts(user.id);
+            for (Account account : accounts) {
+                allAccounts[index++] = new AccountAndUser(account, user.id);
+            }
+            if (mBootCompleted) {
+                mSyncStorageEngine.doDatabaseCleanup(accounts, user.id);
+            }
+        }
+
+        mAccounts = allAccounts;
+
         for (ActiveSyncContext currentSyncContext : mActiveSyncContexts) {
-            if (!ArrayUtils.contains(accounts, currentSyncContext.mSyncOperation.account)) {
+            if (!containsAccountAndUser(allAccounts,
+                    currentSyncContext.mSyncOperation.account,
+                    currentSyncContext.mSyncOperation.userId)) {
                 Log.d(TAG, "canceling sync since the account has been removed");
                 sendSyncFinishedOrCanceledMessage(currentSyncContext,
                         null /* no result since this is a cancel */);
@@ -218,11 +272,7 @@
         // the accounts are not set yet
         sendCheckAlarmsMessage();
 
-        if (mBootCompleted) {
-            mSyncStorageEngine.doDatabaseCleanup(accounts);
-        }
-
-        if (accounts.length > 0) {
+        if (allAccounts.length > 0) {
             // If this is the first time this was called after a bootup then
             // the accounts haven't really changed, instead they were just loaded
             // from the AccountManager. Otherwise at least one of the accounts
@@ -238,7 +288,8 @@
             // a chance to set their syncable state.
 
             boolean onlyThoseWithUnkownSyncableState = justBootedUp;
-            scheduleSync(null, null, null, 0 /* no delay */, onlyThoseWithUnkownSyncableState);
+            scheduleSync(null, UserId.USER_ALL, null, null, 0 /* no delay */,
+                    onlyThoseWithUnkownSyncableState);
         }
     }
 
@@ -277,10 +328,36 @@
 
     private static final String ACTION_SYNC_ALARM = "android.content.syncmanager.SYNC_ALARM";
     private final SyncHandler mSyncHandler;
-    private final Handler mMainHandler;
 
     private volatile boolean mBootCompleted = false;
 
+    static class AccountAndUser {
+        Account account;
+        int userId;
+
+        AccountAndUser(Account account, int userId) {
+            this.account = account;
+            this.userId = userId;
+        }
+
+        public boolean equals(Object o) {
+            if (this == o) return true;
+            if (!(o instanceof AccountAndUser)) return false;
+            final AccountAndUser other = (AccountAndUser) o;
+            return this.account.equals(other.account)
+                    && this.userId == other.userId;
+        }
+
+        @Override
+        public int hashCode() {
+            return account.hashCode() + userId;
+        }
+
+        public String toString() {
+            return account.toString() + " u" + userId;
+        }
+    }
+
     private ConnectivityManager getConnectivityManager() {
         synchronized (this) {
             if (mConnManagerDoNotUseDirectly == null) {
@@ -297,6 +374,13 @@
         mContext = context;
         SyncStorageEngine.init(context);
         mSyncStorageEngine = SyncStorageEngine.getSingleton();
+        mSyncStorageEngine.setOnSyncRequestListener(new OnSyncRequestListener() {
+            public void onSyncRequest(Account account, int userId, String authority,
+                    Bundle extras) {
+                scheduleSync(account, userId, authority, extras, 0, false);
+            }
+        });
+
         mSyncAdapters = new SyncAdaptersCache(mContext);
         mSyncQueue = new SyncQueue(mSyncStorageEngine, mSyncAdapters);
 
@@ -304,12 +388,11 @@
                 Process.THREAD_PRIORITY_BACKGROUND);
         syncThread.start();
         mSyncHandler = new SyncHandler(syncThread.getLooper());
-        mMainHandler = new Handler(mContext.getMainLooper());
 
         mSyncAdapters.setListener(new RegisteredServicesCacheListener<SyncAdapterType>() {
             public void onServiceChanged(SyncAdapterType type, boolean removed) {
                 if (!removed) {
-                    scheduleSync(null, type.authority, null, 0 /* no delay */,
+                    scheduleSync(null, UserId.USER_ALL, type.authority, null, 0 /* no delay */,
                             false /* onlyThoseWithUnkownSyncableState */);
                 }
             }
@@ -376,7 +459,7 @@
             AccountManager.get(mContext).addOnAccountsUpdatedListener(SyncManager.this,
                 mSyncHandler, false /* updateImmediately */);
             // do this synchronously to ensure we have the accounts before this call returns
-            onAccountsUpdated(AccountManager.get(mContext).getAccounts());
+            onAccountsUpdated(null);
         }
     }
 
@@ -404,82 +487,6 @@
         }
     }
 
-    private void initializeSyncAdapter(Account account, String authority) {
-        if (Log.isLoggable(TAG, Log.VERBOSE)) {
-            Log.v(TAG, "initializeSyncAdapter: " + account + ", authority " + authority);
-        }
-        SyncAdapterType syncAdapterType = SyncAdapterType.newKey(authority, account.type);
-        RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo =
-                mSyncAdapters.getServiceInfo(syncAdapterType);
-        if (syncAdapterInfo == null) {
-            Log.w(TAG, "can't find a sync adapter for " + syncAdapterType + ", removing");
-            mSyncStorageEngine.removeAuthority(account, authority);
-            return;
-        }
-
-        Intent intent = new Intent();
-        intent.setAction("android.content.SyncAdapter");
-        intent.setComponent(syncAdapterInfo.componentName);
-        if (!mContext.bindService(intent,
-                new InitializerServiceConnection(account, authority, mContext, mMainHandler),
-                Context.BIND_AUTO_CREATE | Context.BIND_NOT_FOREGROUND
-                | Context.BIND_ALLOW_OOM_MANAGEMENT)) {
-            Log.w(TAG, "initializeSyncAdapter: failed to bind to " + intent);
-        }
-    }
-
-    private static class InitializerServiceConnection implements ServiceConnection {
-        private final Account mAccount;
-        private final String mAuthority;
-        private final Handler mHandler;
-        private volatile Context mContext;
-        private volatile boolean mInitialized;
-
-        public InitializerServiceConnection(Account account, String authority, Context context,
-                Handler handler) {
-            mAccount = account;
-            mAuthority = authority;
-            mContext = context;
-            mHandler = handler;
-            mInitialized = false;
-        }
-
-        public void onServiceConnected(ComponentName name, IBinder service) {
-            try {
-                if (!mInitialized) {
-                    mInitialized = true;
-                    if (Log.isLoggable(TAG, Log.VERBOSE)) {
-                        Log.v(TAG, "calling initialize: " + mAccount + ", authority " + mAuthority);
-                    }
-                    ISyncAdapter.Stub.asInterface(service).initialize(mAccount, mAuthority);
-                }
-            } catch (RemoteException e) {
-                // doesn't matter, we will retry again later
-                Log.d(TAG, "error while initializing: " + mAccount + ", authority " + mAuthority,
-                        e);
-            } finally {
-                // give the sync adapter time to initialize before unbinding from it
-                // TODO: change this API to not rely on this timing, http://b/2500805
-                mHandler.postDelayed(new Runnable() {
-                    public void run() {
-                        if (mContext != null) {
-                            mContext.unbindService(InitializerServiceConnection.this);
-                            mContext = null;
-                        }
-                    }
-                }, INITIALIZATION_UNBIND_DELAY_MS);
-            }
-        }
-
-        public void onServiceDisconnected(ComponentName name) {
-            if (mContext != null) {
-                mContext.unbindService(InitializerServiceConnection.this);
-                mContext = null;
-            }
-        }
-
-    }
-
     /**
      * Initiate a sync. This can start a sync for all providers
      * (pass null to url, set onlyTicklable to false), only those
@@ -500,6 +507,8 @@
      * <p>You'll start getting callbacks after this.
      *
      * @param requestedAccount the account to sync, may be null to signify all accounts
+     * @param userId the id of the user whose accounts are to be synced. If userId is USER_ALL,
+     *          then all users' accounts are considered.
      * @param requestedAuthority the authority to sync, may be null to indicate all authorities
      * @param extras a Map of SyncAdapter-specific information to control
      *          syncs of a specific provider. Can be null. Is ignored
@@ -507,7 +516,7 @@
      * @param delay how many milliseconds in the future to wait before performing this
      * @param onlyThoseWithUnkownSyncableState
      */
-    public void scheduleSync(Account requestedAccount, String requestedAuthority,
+    public void scheduleSync(Account requestedAccount, int userId, String requestedAuthority,
             Bundle extras, long delay, boolean onlyThoseWithUnkownSyncableState) {
         boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE);
 
@@ -521,9 +530,9 @@
             delay = -1; // this means schedule at the front of the queue
         }
 
-        Account[] accounts;
-        if (requestedAccount != null) {
-            accounts = new Account[]{requestedAccount};
+        AccountAndUser[] accounts;
+        if (requestedAccount != null && userId != UserId.USER_ALL) {
+            accounts = new AccountAndUser[] { new AccountAndUser(requestedAccount, userId) };
         } else {
             // if the accounts aren't configured yet then we can't support an account-less
             // sync request
@@ -574,24 +583,23 @@
             if (hasSyncAdapter) syncableAuthorities.add(requestedAuthority);
         }
 
-        final boolean masterSyncAutomatically = mSyncStorageEngine.getMasterSyncAutomatically();
-
         for (String authority : syncableAuthorities) {
-            for (Account account : accounts) {
-                int isSyncable = mSyncStorageEngine.getIsSyncable(account, authority);
+            for (AccountAndUser account : accounts) {
+                int isSyncable = mSyncStorageEngine.getIsSyncable(account.account, account.userId,
+                        authority);
                 if (isSyncable == 0) {
                     continue;
                 }
                 final RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo =
                         mSyncAdapters.getServiceInfo(
-                                SyncAdapterType.newKey(authority, account.type));
+                                SyncAdapterType.newKey(authority, account.account.type));
                 if (syncAdapterInfo == null) {
                     continue;
                 }
                 final boolean allowParallelSyncs = syncAdapterInfo.type.allowParallelSyncs();
                 final boolean isAlwaysSyncable = syncAdapterInfo.type.isAlwaysSyncable();
                 if (isSyncable < 0 && isAlwaysSyncable) {
-                    mSyncStorageEngine.setIsSyncable(account, authority, 1);
+                    mSyncStorageEngine.setIsSyncable(account.account, account.userId, authority, 1);
                     isSyncable = 1;
                 }
                 if (onlyThoseWithUnkownSyncableState && isSyncable >= 0) {
@@ -605,8 +613,10 @@
                 boolean syncAllowed =
                         (isSyncable < 0)
                         || ignoreSettings
-                        || (backgroundDataUsageAllowed && masterSyncAutomatically
-                            && mSyncStorageEngine.getSyncAutomatically(account, authority));
+                        || (backgroundDataUsageAllowed
+                                && mSyncStorageEngine.getMasterSyncAutomatically(account.userId)
+                                && mSyncStorageEngine.getSyncAutomatically(account.account,
+                                        account.userId, authority));
                 if (!syncAllowed) {
                     if (isLoggable) {
                         Log.d(TAG, "scheduleSync: sync of " + account + ", " + authority
@@ -615,8 +625,10 @@
                     continue;
                 }
 
-                Pair<Long, Long> backoff = mSyncStorageEngine.getBackoff(account, authority);
-                long delayUntil = mSyncStorageEngine.getDelayUntilTime(account, authority);
+                Pair<Long, Long> backoff = mSyncStorageEngine
+                        .getBackoff(account.account, account.userId, authority);
+                long delayUntil = mSyncStorageEngine.getDelayUntilTime(account.account,
+                        account.userId, authority);
                 final long backoffTime = backoff != null ? backoff.first : 0;
                 if (isSyncable < 0) {
                     Bundle newExtras = new Bundle();
@@ -630,9 +642,8 @@
                                 + ", extras " + newExtras);
                     }
                     scheduleSyncOperation(
-                            new SyncOperation(account, source, authority, newExtras, 0,
-                                    backoffTime, delayUntil,
-                                    allowParallelSyncs));
+                            new SyncOperation(account.account, account.userId, source, authority,
+                                    newExtras, 0, backoffTime, delayUntil, allowParallelSyncs));
                 }
                 if (!onlyThoseWithUnkownSyncableState) {
                     if (isLoggable) {
@@ -644,18 +655,17 @@
                                 + ", extras " + extras);
                     }
                     scheduleSyncOperation(
-                            new SyncOperation(account, source, authority, extras, delay,
-                                    backoffTime, delayUntil,
-                                    allowParallelSyncs));
+                            new SyncOperation(account.account, account.userId, source, authority,
+                                    extras, delay, backoffTime, delayUntil, allowParallelSyncs));
                 }
             }
         }
     }
 
-    public void scheduleLocalSync(Account account, String authority) {
+    public void scheduleLocalSync(Account account, int userId, String authority) {
         final Bundle extras = new Bundle();
         extras.putBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, true);
-        scheduleSync(account, authority, extras, LOCAL_SYNC_DELAY,
+        scheduleSync(account, userId, authority, extras, LOCAL_SYNC_DELAY,
                 false /* onlyThoseWithUnkownSyncableState */);
     }
 
@@ -691,11 +701,13 @@
         mSyncHandler.sendMessage(msg);
     }
 
-    private void sendCancelSyncsMessage(final Account account, final String authority) {
+    private void sendCancelSyncsMessage(final Account account, final int userId,
+            final String authority) {
         if (Log.isLoggable(TAG, Log.VERBOSE)) Log.v(TAG, "sending MESSAGE_CANCEL");
         Message msg = mSyncHandler.obtainMessage();
         msg.what = SyncHandler.MESSAGE_CANCEL;
         msg.obj = Pair.create(account, authority);
+        msg.arg1 = userId;
         mSyncHandler.sendMessage(msg);
     }
 
@@ -717,10 +729,10 @@
     }
 
     private void clearBackoffSetting(SyncOperation op) {
-        mSyncStorageEngine.setBackoff(op.account, op.authority,
+        mSyncStorageEngine.setBackoff(op.account, op.userId, op.authority,
                 SyncStorageEngine.NOT_IN_BACKOFF_MODE, SyncStorageEngine.NOT_IN_BACKOFF_MODE);
         synchronized (mSyncQueue) {
-            mSyncQueue.onBackoffChanged(op.account, op.authority, 0);
+            mSyncQueue.onBackoffChanged(op.account, op.userId, op.authority, 0);
         }
     }
 
@@ -728,7 +740,7 @@
         final long now = SystemClock.elapsedRealtime();
 
         final Pair<Long, Long> previousSettings =
-                mSyncStorageEngine.getBackoff(op.account, op.authority);
+                mSyncStorageEngine.getBackoff(op.account, op.userId, op.authority);
         long newDelayInMs = -1;
         if (previousSettings != null) {
             // don't increase backoff before current backoff is expired. This will happen for op's
@@ -759,14 +771,14 @@
 
         final long backoff = now + newDelayInMs;
 
-        mSyncStorageEngine.setBackoff(op.account, op.authority,
+        mSyncStorageEngine.setBackoff(op.account, op.userId, op.authority,
                 backoff, newDelayInMs);
 
         op.backoff = backoff;
         op.updateEffectiveRunTime();
 
         synchronized (mSyncQueue) {
-            mSyncQueue.onBackoffChanged(op.account, op.authority, backoff);
+            mSyncQueue.onBackoffChanged(op.account, op.userId, op.authority, backoff);
         }
     }
 
@@ -779,7 +791,8 @@
         } else {
             newDelayUntilTime = 0;
         }
-        mSyncStorageEngine.setDelayUntilTime(op.account, op.authority, newDelayUntilTime);
+        mSyncStorageEngine
+                .setDelayUntilTime(op.account, op.userId, op.authority, newDelayUntilTime);
         synchronized (mSyncQueue) {
             mSyncQueue.onDelayUntilTimeChanged(op.account, op.authority, newDelayUntilTime);
         }
@@ -790,8 +803,8 @@
      * @param account limit the cancelations to syncs with this account, if non-null
      * @param authority limit the cancelations to syncs with this authority, if non-null
      */
-    public void cancelActiveSync(Account account, String authority) {
-        sendCancelSyncsMessage(account, authority);
+    public void cancelActiveSync(Account account, int userId, String authority) {
+        sendCancelSyncsMessage(account, userId, authority);
     }
 
     /**
@@ -823,11 +836,11 @@
      * @param account limit the removals to operations with this account, if non-null
      * @param authority limit the removals to operations with this authority, if non-null
      */
-    public void clearScheduledSyncOperations(Account account, String authority) {
+    public void clearScheduledSyncOperations(Account account, int userId, String authority) {
         synchronized (mSyncQueue) {
-            mSyncQueue.remove(account, authority);
+            mSyncQueue.remove(account, userId, authority);
         }
-        mSyncStorageEngine.setBackoff(account, authority,
+        mSyncStorageEngine.setBackoff(account, userId, authority,
                 SyncStorageEngine.NOT_IN_BACKOFF_MODE, SyncStorageEngine.NOT_IN_BACKOFF_MODE);
     }
 
@@ -875,7 +888,8 @@
                 Log.d(TAG, "retrying sync operation that failed because there was already a "
                         + "sync in progress: " + operation);
             }
-            scheduleSyncOperation(new SyncOperation(operation.account, operation.syncSource,
+            scheduleSyncOperation(new SyncOperation(operation.account, operation.userId,
+                    operation.syncSource,
                     operation.authority, operation.extras,
                     DELAY_RETRY_SYNC_IN_PROGRESS_IN_SECONDS * 1000,
                     operation.backoff, operation.delayUntil, operation.allowParallelSyncs));
@@ -979,7 +993,8 @@
             mBound = true;
             final boolean bindResult = mContext.bindService(intent, this,
                     Context.BIND_AUTO_CREATE | Context.BIND_NOT_FOREGROUND
-                    | Context.BIND_ALLOW_OOM_MANAGEMENT);
+                    | Context.BIND_ALLOW_OOM_MANAGEMENT,
+                    mSyncOperation.userId);
             if (!bindResult) {
                 mBound = false;
             }
@@ -1034,10 +1049,19 @@
 
     protected void dumpSyncState(PrintWriter pw) {
         pw.print("data connected: "); pw.println(mDataConnectionIsConnected);
-        pw.print("auto sync: "); pw.println(mSyncStorageEngine.getMasterSyncAutomatically());
+        pw.print("auto sync: ");
+        List<UserInfo> users = getAllUsers();
+        if (users != null) {
+            for (UserInfo user : users) {
+                pw.print("u" + user.id + "="
+                        + mSyncStorageEngine.getMasterSyncAutomatically(user.id));
+            }
+            pw.println();
+        }
         pw.print("memory low: "); pw.println(mStorageIsLow);
 
-        final Account[] accounts = mAccounts;
+        final AccountAndUser[] accounts = mAccounts;
+
         pw.print("accounts: ");
         if (accounts != INITIAL_ACCOUNTS_ARRAY) {
             pw.println(accounts.length);
@@ -1090,18 +1114,20 @@
         // join the installed sync adapter with the accounts list and emit for everything
         pw.println();
         pw.println("Sync Status");
-        for (Account account : accounts) {
-            pw.print("  Account "); pw.print(account.name);
-                    pw.print(" "); pw.print(account.type);
+        for (AccountAndUser account : accounts) {
+            pw.print("  Account "); pw.print(account.account.name);
+                    pw.print(" u"); pw.print(account.userId);
+                    pw.print(" "); pw.print(account.account.type);
                     pw.println(":");
             for (RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterType :
                     mSyncAdapters.getAllServices()) {
-                if (!syncAdapterType.type.accountType.equals(account.type)) {
+                if (!syncAdapterType.type.accountType.equals(account.account.type)) {
                     continue;
                 }
 
-                SyncStorageEngine.AuthorityInfo settings = mSyncStorageEngine.getOrCreateAuthority(
-                        account, syncAdapterType.type.authority);
+                SyncStorageEngine.AuthorityInfo settings =
+                        mSyncStorageEngine.getOrCreateAuthority(
+                                account.account, account.userId, syncAdapterType.type.authority);
                 SyncStatusInfo status = mSyncStorageEngine.getOrCreateSyncStatus(settings);
                 pw.print("    "); pw.print(settings.authority);
                 pw.println(":");
@@ -1554,7 +1580,16 @@
         private volatile CountDownLatch mReadyToRunLatch = new CountDownLatch(1);
         public void onBootCompleted() {
             mBootCompleted = true;
-            mSyncStorageEngine.doDatabaseCleanup(AccountManager.get(mContext).getAccounts());
+            // TODO: Handle bootcompleted event for specific user. Now let's just iterate through
+            // all the users.
+            List<UserInfo> users = getAllUsers();
+            if (users != null) {
+                for (UserInfo user : users) {
+                    mSyncStorageEngine.doDatabaseCleanup(
+                            AccountManagerService.getSingleton().getAccounts(user.id),
+                            user.id);
+                }
+            }
             if (mReadyToRunLatch != null) {
                 mReadyToRunLatch.countDown();
             }
@@ -1635,7 +1670,7 @@
                             Log.d(TAG, "handleSyncHandlerMessage: MESSAGE_SERVICE_CANCEL: "
                                     + payload.first + ", " + payload.second);
                         }
-                        cancelActiveSyncLocked(payload.first, payload.second);
+                        cancelActiveSyncLocked(payload.first, msg.arg1, payload.second);
                         nextPendingSyncTime = maybeStartNextSyncLocked();
                         break;
                     }
@@ -1740,22 +1775,28 @@
             final boolean backgroundDataUsageAllowed =
                     getConnectivityManager().getBackgroundDataSetting();
             long earliestFuturePollTime = Long.MAX_VALUE;
-            if (!backgroundDataUsageAllowed || !mSyncStorageEngine.getMasterSyncAutomatically()) {
+            if (!backgroundDataUsageAllowed) {
                 return earliestFuturePollTime;
             }
+
+            AccountAndUser[] accounts = mAccounts;
+
             final long nowAbsolute = System.currentTimeMillis();
             ArrayList<SyncStorageEngine.AuthorityInfo> infos = mSyncStorageEngine.getAuthorities();
             for (SyncStorageEngine.AuthorityInfo info : infos) {
                 // skip the sync if the account of this operation no longer exists
-                if (!ArrayUtils.contains(mAccounts, info.account)) {
+                if (!containsAccountAndUser(accounts, info.account, info.userId)) {
                     continue;
                 }
 
-                if (!mSyncStorageEngine.getSyncAutomatically(info.account, info.authority)) {
+                if (!mSyncStorageEngine.getMasterSyncAutomatically(info.userId)
+                        || !mSyncStorageEngine.getSyncAutomatically(info.account, info.userId,
+                                info.authority)) {
                     continue;
                 }
 
-                if (mSyncStorageEngine.getIsSyncable(info.account, info.authority) == 0) {
+                if (mSyncStorageEngine.getIsSyncable(info.account, info.userId, info.authority)
+                        == 0) {
                     continue;
                 }
 
@@ -1772,8 +1813,8 @@
                             : lastPollTimeAbsolute + periodInSeconds * 1000;
                     // if it is ready to run then schedule it and mark it as having been scheduled
                     if (nextPollTimeAbsolute <= nowAbsolute) {
-                        final Pair<Long, Long> backoff =
-                                mSyncStorageEngine.getBackoff(info.account, info.authority);
+                        final Pair<Long, Long> backoff = mSyncStorageEngine.getBackoff(
+                                info.account, info.userId, info.authority);
                         final RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo =
                                 mSyncAdapters.getServiceInfo(
                                         SyncAdapterType.newKey(info.authority, info.account.type));
@@ -1781,11 +1822,12 @@
                             continue;
                         }
                         scheduleSyncOperation(
-                                new SyncOperation(info.account, SyncStorageEngine.SOURCE_PERIODIC,
+                                new SyncOperation(info.account, info.userId,
+                                        SyncStorageEngine.SOURCE_PERIODIC,
                                         info.authority, extras, 0 /* delay */,
                                         backoff != null ? backoff.first : 0,
                                         mSyncStorageEngine.getDelayUntilTime(
-                                                info.account, info.authority),
+                                                info.account, info.userId, info.authority),
                                         syncAdapterInfo.type.allowParallelSyncs()));
                         status.setPeriodicSyncTime(i, nowAbsolute);
                     } else {
@@ -1830,7 +1872,7 @@
 
             // If the accounts aren't known yet then we aren't ready to run. We will be kicked
             // when the account lookup request does complete.
-            Account[] accounts = mAccounts;
+            AccountAndUser[] accounts = mAccounts;
             if (accounts == INITIAL_ACCOUNTS_ARRAY) {
                 if (isLoggable) {
                     Log.v(TAG, "maybeStartNextSync: accounts not known, skipping");
@@ -1843,7 +1885,6 @@
             // start it, otherwise just get out.
             final boolean backgroundDataUsageAllowed =
                     getConnectivityManager().getBackgroundDataSetting();
-            final boolean masterSyncAutomatically = mSyncStorageEngine.getMasterSyncAutomatically();
 
             final long now = SystemClock.elapsedRealtime();
 
@@ -1863,14 +1904,15 @@
                     final SyncOperation op = operationIterator.next();
 
                     // drop the sync if the account of this operation no longer exists
-                    if (!ArrayUtils.contains(mAccounts, op.account)) {
+                    if (!containsAccountAndUser(accounts, op.account, op.userId)) {
                         operationIterator.remove();
                         mSyncStorageEngine.deleteFromPending(op.pendingOperation);
                         continue;
                     }
 
                     // drop this sync request if it isn't syncable
-                    int syncableState = mSyncStorageEngine.getIsSyncable(op.account, op.authority);
+                    int syncableState = mSyncStorageEngine.getIsSyncable(
+                            op.account, op.userId, op.authority);
                     if (syncableState == 0) {
                         operationIterator.remove();
                         mSyncStorageEngine.deleteFromPending(op.pendingOperation);
@@ -1905,11 +1947,11 @@
                     // disconnected for the target UID.
                     if (!op.extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_SETTINGS, false)
                             && (syncableState > 0)
-                            && (!masterSyncAutomatically
+                            && (!mSyncStorageEngine.getMasterSyncAutomatically(op.userId)
                                 || !backgroundDataUsageAllowed
                                 || !uidNetworkConnected
                                 || !mSyncStorageEngine.getSyncAutomatically(
-                                       op.account, op.authority))) {
+                                       op.account, op.userId, op.authority))) {
                         operationIterator.remove();
                         mSyncStorageEngine.deleteFromPending(op.pendingOperation);
                         continue;
@@ -1946,6 +1988,7 @@
                     }
                     if (activeOp.account.type.equals(candidate.account.type)
                             && activeOp.authority.equals(candidate.authority)
+                            && activeOp.userId == candidate.userId
                             && (!activeOp.allowParallelSyncs
                                 || activeOp.account.name.equals(candidate.account.name))) {
                         conflict = activeSyncContext;
@@ -2033,7 +2076,7 @@
             if (syncAdapterInfo == null) {
                 Log.d(TAG, "can't find a sync adapter for " + syncAdapterType
                         + ", removing settings for it");
-                mSyncStorageEngine.removeAuthority(op.account, op.authority);
+                mSyncStorageEngine.removeAuthority(op.account, op.userId, op.authority);
                 return false;
             }
 
@@ -2074,7 +2117,7 @@
             }
         }
 
-        private void cancelActiveSyncLocked(Account account, String authority) {
+        private void cancelActiveSyncLocked(Account account, int userId, String authority) {
             ArrayList<ActiveSyncContext> activeSyncs =
                     new ArrayList<ActiveSyncContext>(mActiveSyncContexts);
             for (ActiveSyncContext activeSyncContext : activeSyncs) {
@@ -2082,15 +2125,20 @@
                     // if an authority was specified then only cancel the sync if it matches
                     if (account != null) {
                         if (!account.equals(activeSyncContext.mSyncOperation.account)) {
-                            return;
+                            continue;
                         }
                     }
                     // if an account was specified then only cancel the sync if it matches
                     if (authority != null) {
                         if (!authority.equals(activeSyncContext.mSyncOperation.authority)) {
-                            return;
+                            continue;
                         }
                     }
+                    // check if the userid matches
+                    if (userId != UserId.USER_ALL
+                            && userId != activeSyncContext.mSyncOperation.userId) {
+                        continue;
+                    }
                     runSyncFinishedOrCanceledLocked(null /* no result since this is a cancel */,
                             activeSyncContext);
                 }
@@ -2169,7 +2217,7 @@
             }
 
             if (syncResult != null && syncResult.fullSyncRequested) {
-                scheduleSyncOperation(new SyncOperation(syncOperation.account,
+                scheduleSyncOperation(new SyncOperation(syncOperation.account, syncOperation.userId,
                         syncOperation.syncSource, syncOperation.authority, new Bundle(), 0,
                         syncOperation.backoff, syncOperation.delayUntil,
                         syncOperation.allowParallelSyncs));
@@ -2180,7 +2228,8 @@
         private void closeActiveSyncContext(ActiveSyncContext activeSyncContext) {
             activeSyncContext.close();
             mActiveSyncContexts.remove(activeSyncContext);
-            mSyncStorageEngine.removeActiveSync(activeSyncContext.mSyncInfo);
+            mSyncStorageEngine.removeActiveSync(activeSyncContext.mSyncInfo,
+                    activeSyncContext.mSyncOperation.userId);
         }
 
         /**
@@ -2446,7 +2495,8 @@
                                 syncOperation.account.name.hashCode());
 
             return mSyncStorageEngine.insertStartSyncEvent(
-                    syncOperation.account, syncOperation.authority, now, source);
+                    syncOperation.account, syncOperation.userId, syncOperation.authority,
+                    now, source);
         }
 
         public void stopSyncEvent(long rowId, SyncOperation syncOperation, String resultMessage,
diff --git a/core/java/android/content/SyncOperation.java b/core/java/android/content/SyncOperation.java
index 94c2247..4e86ef8 100644
--- a/core/java/android/content/SyncOperation.java
+++ b/core/java/android/content/SyncOperation.java
@@ -26,6 +26,7 @@
  */
 public class SyncOperation implements Comparable {
     public final Account account;
+    public final int userId;
     public int syncSource;
     public String authority;
     public final boolean allowParallelSyncs;
@@ -38,9 +39,10 @@
     public long delayUntil;
     public long effectiveRunTime;
 
-    public SyncOperation(Account account, int source, String authority, Bundle extras,
+    public SyncOperation(Account account, int userId, int source, String authority, Bundle extras,
             long delayInMs, long backoff, long delayUntil, boolean allowParallelSyncs) {
         this.account = account;
+        this.userId = userId;
         this.syncSource = source;
         this.authority = authority;
         this.allowParallelSyncs = allowParallelSyncs;
@@ -75,6 +77,7 @@
 
     SyncOperation(SyncOperation other) {
         this.account = other.account;
+        this.userId = other.userId;
         this.syncSource = other.syncSource;
         this.authority = other.authority;
         this.extras = new Bundle(other.extras);
@@ -120,7 +123,8 @@
     private String toKey() {
         StringBuilder sb = new StringBuilder();
         sb.append("authority: ").append(authority);
-        sb.append(" account {name=" + account.name + ", type=" + account.type + "}");
+        sb.append(" account {name=" + account.name + ", user=" + userId + ", type=" + account.type
+                + "}");
         sb.append(" extras: ");
         extrasToStringBuilder(extras, sb);
         return sb.toString();
diff --git a/core/java/android/content/SyncQueue.java b/core/java/android/content/SyncQueue.java
index bfdf4a1..06da6fa 100644
--- a/core/java/android/content/SyncQueue.java
+++ b/core/java/android/content/SyncQueue.java
@@ -49,7 +49,8 @@
         final int N = ops.size();
         for (int i=0; i<N; i++) {
             SyncStorageEngine.PendingOperation op = ops.get(i);
-            final Pair<Long, Long> backoff = syncStorageEngine.getBackoff(op.account, op.authority);
+            final Pair<Long, Long> backoff =
+                    syncStorageEngine.getBackoff(op.account, op.userId, op.authority);
             final RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo =
                     syncAdapters.getServiceInfo(
                             SyncAdapterType.newKey(op.authority, op.account.type));
@@ -57,9 +58,9 @@
                 continue;
             }
             SyncOperation syncOperation = new SyncOperation(
-                    op.account, op.syncSource, op.authority, op.extras, 0 /* delay */,
+                    op.account, op.userId, op.syncSource, op.authority, op.extras, 0 /* delay */,
                     backoff != null ? backoff.first : 0,
-                    syncStorageEngine.getDelayUntilTime(op.account, op.authority),
+                    syncStorageEngine.getDelayUntilTime(op.account, op.userId, op.authority),
                     syncAdapterInfo.type.allowParallelSyncs());
             syncOperation.expedited = op.expedited;
             syncOperation.pendingOperation = op;
@@ -102,8 +103,8 @@
         operation.pendingOperation = pop;
         if (operation.pendingOperation == null) {
             pop = new SyncStorageEngine.PendingOperation(
-                            operation.account, operation.syncSource,
-                            operation.authority, operation.extras, operation.expedited);
+                    operation.account, operation.userId, operation.syncSource,
+                    operation.authority, operation.extras, operation.expedited);
             pop = mSyncStorageEngine.insertIntoPending(pop);
             if (pop == null) {
                 throw new IllegalStateException("error adding pending sync operation "
@@ -131,11 +132,12 @@
         }
     }
 
-    public void onBackoffChanged(Account account, String providerName, long backoff) {
+    public void onBackoffChanged(Account account, int userId, String providerName, long backoff) {
         // for each op that matches the account and provider update its
         // backoff and effectiveStartTime
         for (SyncOperation op : mOperationsMap.values()) {
-            if (op.account.equals(account) && op.authority.equals(providerName)) {
+            if (op.account.equals(account) && op.authority.equals(providerName)
+                    && op.userId == userId) {
                 op.backoff = backoff;
                 op.updateEffectiveRunTime();
             }
@@ -153,7 +155,7 @@
         }
     }
 
-    public void remove(Account account, String authority) {
+    public void remove(Account account, int userId, String authority) {
         Iterator<Map.Entry<String, SyncOperation>> entries = mOperationsMap.entrySet().iterator();
         while (entries.hasNext()) {
             Map.Entry<String, SyncOperation> entry = entries.next();
@@ -164,6 +166,9 @@
             if (authority != null && !syncOperation.authority.equals(authority)) {
                 continue;
             }
+            if (userId != syncOperation.userId) {
+                continue;
+            }
             entries.remove();
             if (!mSyncStorageEngine.deleteFromPending(syncOperation.pendingOperation)) {
                 final String errorMessage = "unable to find pending row for " + syncOperation;
diff --git a/core/java/android/content/SyncStorageEngine.java b/core/java/android/content/SyncStorageEngine.java
index a1e174b..7bb9866 100644
--- a/core/java/android/content/SyncStorageEngine.java
+++ b/core/java/android/content/SyncStorageEngine.java
@@ -25,6 +25,7 @@
 import org.xmlpull.v1.XmlSerializer;
 
 import android.accounts.Account;
+import android.content.SyncManager.AccountAndUser;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteException;
@@ -58,9 +59,16 @@
  * @hide
  */
 public class SyncStorageEngine extends Handler {
+
     private static final String TAG = "SyncManager";
     private static final boolean DEBUG_FILE = false;
 
+    private static final String XML_ATTR_NEXT_AUTHORITY_ID = "nextAuthorityId";
+    private static final String XML_ATTR_LISTEN_FOR_TICKLES = "listen-for-tickles";
+    private static final String XML_ATTR_ENABLED = "enabled";
+    private static final String XML_ATTR_USER = "user";
+    private static final String XML_TAG_LISTEN_FOR_TICKLES = "listenForTickles";
+
     private static final long DEFAULT_POLL_FREQUENCY_SECONDS = 60 * 60 * 24; // One day
 
     // @VisibleForTesting
@@ -133,6 +141,7 @@
 
     public static class PendingOperation {
         final Account account;
+        final int userId;
         final int syncSource;
         final String authority;
         final Bundle extras;        // note: read-only.
@@ -141,9 +150,10 @@
         int authorityId;
         byte[] flatExtras;
 
-        PendingOperation(Account account, int source,
+        PendingOperation(Account account, int userId, int source,
                 String authority, Bundle extras, boolean expedited) {
             this.account = account;
+            this.userId = userId;
             this.syncSource = source;
             this.authority = authority;
             this.extras = extras != null ? new Bundle(extras) : extras;
@@ -153,6 +163,7 @@
 
         PendingOperation(PendingOperation other) {
             this.account = other.account;
+            this.userId = other.userId;
             this.syncSource = other.syncSource;
             this.authority = other.authority;
             this.extras = other.extras;
@@ -162,17 +173,18 @@
     }
 
     static class AccountInfo {
-        final Account account;
+        final AccountAndUser accountAndUser;
         final HashMap<String, AuthorityInfo> authorities =
                 new HashMap<String, AuthorityInfo>();
 
-        AccountInfo(Account account) {
-            this.account = account;
+        AccountInfo(AccountAndUser accountAndUser) {
+            this.accountAndUser = accountAndUser;
         }
     }
 
     public static class AuthorityInfo {
         final Account account;
+        final int userId;
         final String authority;
         final int ident;
         boolean enabled;
@@ -182,8 +194,9 @@
         long delayUntil;
         final ArrayList<Pair<Bundle, Long>> periodicSyncs;
 
-        AuthorityInfo(Account account, String authority, int ident) {
+        AuthorityInfo(Account account, int userId, String authority, int ident) {
             this.account = account;
+            this.userId = userId;
             this.authority = authority;
             this.ident = ident;
             enabled = SYNC_ENABLED_DEFAULT;
@@ -219,17 +232,29 @@
         }
     }
 
+    interface OnSyncRequestListener {
+        /**
+         * Called when a sync is needed on an account(s) due to some change in state.
+         * @param account
+         * @param userId
+         * @param authority
+         * @param extras
+         */
+        public void onSyncRequest(Account account, int userId, String authority, Bundle extras);
+    }
+
     // Primary list of all syncable authorities.  Also our global lock.
     private final SparseArray<AuthorityInfo> mAuthorities =
             new SparseArray<AuthorityInfo>();
 
-    private final HashMap<Account, AccountInfo> mAccounts =
-        new HashMap<Account, AccountInfo>();
+    private final HashMap<AccountAndUser, AccountInfo> mAccounts
+            = new HashMap<AccountAndUser, AccountInfo>();
 
     private final ArrayList<PendingOperation> mPendingOperations =
             new ArrayList<PendingOperation>();
 
-    private final ArrayList<SyncInfo> mCurrentSyncs = new ArrayList<SyncInfo>();
+    private final SparseArray<ArrayList<SyncInfo>> mCurrentSyncs
+            = new SparseArray<ArrayList<SyncInfo>>();
 
     private final SparseArray<SyncStatusInfo> mSyncStatus =
             new SparseArray<SyncStatusInfo>();
@@ -282,7 +307,9 @@
     private int mNumPendingFinished = 0;
 
     private int mNextHistoryId = 0;
-    private boolean mMasterSyncAutomatically = true;
+    private SparseArray<Boolean> mMasterSyncAutomatically = new SparseArray<Boolean>();
+
+    private OnSyncRequestListener mSyncRequestListener;
 
     private SyncStorageEngine(Context context, File dataDir) {
         mContext = context;
@@ -330,6 +357,12 @@
         return sSyncStorageEngine;
     }
 
+    protected void setOnSyncRequestListener(OnSyncRequestListener listener) {
+        if (mSyncRequestListener == null) {
+            mSyncRequestListener = listener;
+        }
+    }
+
     @Override public void handleMessage(Message msg) {
         if (msg.what == MSG_WRITE_STATUS) {
             synchronized (mAuthorities) {
@@ -389,10 +422,10 @@
         }
     }
 
-    public boolean getSyncAutomatically(Account account, String providerName) {
+    public boolean getSyncAutomatically(Account account, int userId, String providerName) {
         synchronized (mAuthorities) {
             if (account != null) {
-                AuthorityInfo authority = getAuthorityLocked(account, providerName,
+                AuthorityInfo authority = getAuthorityLocked(account, userId, providerName,
                         "getSyncAutomatically");
                 return authority != null && authority.enabled;
             }
@@ -402,6 +435,7 @@
                 i--;
                 AuthorityInfo authority = mAuthorities.valueAt(i);
                 if (authority.authority.equals(providerName)
+                        && authority.userId == userId
                         && authority.enabled) {
                     return true;
                 }
@@ -410,11 +444,13 @@
         }
     }
 
-    public void setSyncAutomatically(Account account, String providerName, boolean sync) {
-        Log.d(TAG, "setSyncAutomatically: " + /*account +*/ ", provider " + providerName
-                + " -> " + sync);
+    public void setSyncAutomatically(Account account, int userId, String providerName,
+            boolean sync) {
+        Log.d(TAG, "setSyncAutomatically: " + /* account + */" provider " + providerName
+                + ", user " + userId + " -> " + sync);
         synchronized (mAuthorities) {
-            AuthorityInfo authority = getOrCreateAuthorityLocked(account, providerName, -1, false);
+            AuthorityInfo authority = getOrCreateAuthorityLocked(account, userId, providerName, -1,
+                    false);
             if (authority.enabled == sync) {
                 Log.d(TAG, "setSyncAutomatically: already set to " + sync + ", doing nothing");
                 return;
@@ -424,15 +460,15 @@
         }
 
         if (sync) {
-            ContentResolver.requestSync(account, providerName, new Bundle());
+            requestSync(account, userId, providerName, new Bundle());
         }
         reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
     }
 
-    public int getIsSyncable(Account account, String providerName) {
+    public int getIsSyncable(Account account, int userId, String providerName) {
         synchronized (mAuthorities) {
             if (account != null) {
-                AuthorityInfo authority = getAuthorityLocked(account, providerName,
+                AuthorityInfo authority = getAuthorityLocked(account, userId, providerName,
                         "getIsSyncable");
                 if (authority == null) {
                     return -1;
@@ -452,15 +488,17 @@
         }
     }
 
-    public void setIsSyncable(Account account, String providerName, int syncable) {
+    public void setIsSyncable(Account account, int userId, String providerName, int syncable) {
         if (syncable > 1) {
             syncable = 1;
         } else if (syncable < -1) {
             syncable = -1;
         }
-        Log.d(TAG, "setIsSyncable: " + account + ", provider " + providerName + " -> " + syncable);
+        Log.d(TAG, "setIsSyncable: " + account + ", provider " + providerName
+                + ", user " + userId + " -> " + syncable);
         synchronized (mAuthorities) {
-            AuthorityInfo authority = getOrCreateAuthorityLocked(account, providerName, -1, false);
+            AuthorityInfo authority = getOrCreateAuthorityLocked(account, userId, providerName, -1,
+                    false);
             if (authority.syncable == syncable) {
                 Log.d(TAG, "setIsSyncable: already set to " + syncable + ", doing nothing");
                 return;
@@ -470,14 +508,15 @@
         }
 
         if (syncable > 0) {
-            ContentResolver.requestSync(account, providerName, new Bundle());
+            requestSync(account, userId, providerName, new Bundle());
         }
         reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
     }
 
-    public Pair<Long, Long> getBackoff(Account account, String providerName) {
+    public Pair<Long, Long> getBackoff(Account account, int userId, String providerName) {
         synchronized (mAuthorities) {
-            AuthorityInfo authority = getAuthorityLocked(account, providerName, "getBackoff");
+            AuthorityInfo authority = getAuthorityLocked(account, userId, providerName,
+                    "getBackoff");
             if (authority == null || authority.backoffTime < 0) {
                 return null;
             }
@@ -485,17 +524,21 @@
         }
     }
 
-    public void setBackoff(Account account, String providerName,
+    public void setBackoff(Account account, int userId, String providerName,
             long nextSyncTime, long nextDelay) {
         if (Log.isLoggable(TAG, Log.VERBOSE)) {
             Log.v(TAG, "setBackoff: " + account + ", provider " + providerName
+                    + ", user " + userId
                     + " -> nextSyncTime " + nextSyncTime + ", nextDelay " + nextDelay);
         }
         boolean changed = false;
         synchronized (mAuthorities) {
             if (account == null || providerName == null) {
                 for (AccountInfo accountInfo : mAccounts.values()) {
-                    if (account != null && !account.equals(accountInfo.account)) continue;
+                    if (account != null && !account.equals(accountInfo.accountAndUser.account)
+                            && userId != accountInfo.accountAndUser.userId) {
+                        continue;
+                    }
                     for (AuthorityInfo authorityInfo : accountInfo.authorities.values()) {
                         if (providerName != null && !providerName.equals(authorityInfo.authority)) {
                             continue;
@@ -510,7 +553,8 @@
                 }
             } else {
                 AuthorityInfo authority =
-                        getOrCreateAuthorityLocked(account, providerName, -1 /* ident */, true);
+                        getOrCreateAuthorityLocked(account, userId, providerName, -1 /* ident */,
+                                true);
                 if (authority.backoffTime == nextSyncTime && authority.backoffDelay == nextDelay) {
                     return;
                 }
@@ -535,13 +579,15 @@
                         if (Log.isLoggable(TAG, Log.VERBOSE)) {
                             Log.v(TAG, "clearAllBackoffs:"
                                     + " authority:" + authorityInfo.authority
-                                    + " account:" + accountInfo.account.name
+                                    + " account:" + accountInfo.accountAndUser.account.name
+                                    + " user:" + accountInfo.accountAndUser.userId
                                     + " backoffTime was: " + authorityInfo.backoffTime
                                     + " backoffDelay was: " + authorityInfo.backoffDelay);
                         }
                         authorityInfo.backoffTime = NOT_IN_BACKOFF_MODE;
                         authorityInfo.backoffDelay = NOT_IN_BACKOFF_MODE;
-                        syncQueue.onBackoffChanged(accountInfo.account, authorityInfo.authority, 0);
+                        syncQueue.onBackoffChanged(accountInfo.accountAndUser.account,
+                                accountInfo.accountAndUser.userId, authorityInfo.authority, 0);
                         changed = true;
                     }
                 }
@@ -553,14 +599,15 @@
         }
     }
 
-    public void setDelayUntilTime(Account account, String providerName, long delayUntil) {
+    public void setDelayUntilTime(Account account, int userId, String providerName,
+            long delayUntil) {
         if (Log.isLoggable(TAG, Log.VERBOSE)) {
             Log.v(TAG, "setDelayUntil: " + account + ", provider " + providerName
-                    + " -> delayUntil " + delayUntil);
+                    + ", user " + userId + " -> delayUntil " + delayUntil);
         }
         synchronized (mAuthorities) {
             AuthorityInfo authority = getOrCreateAuthorityLocked(
-                    account, providerName, -1 /* ident */, true);
+                    account, userId, providerName, -1 /* ident */, true);
             if (authority.delayUntil == delayUntil) {
                 return;
             }
@@ -570,9 +617,10 @@
         reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
     }
 
-    public long getDelayUntilTime(Account account, String providerName) {
+    public long getDelayUntilTime(Account account, int userId, String providerName) {
         synchronized (mAuthorities) {
-            AuthorityInfo authority = getAuthorityLocked(account, providerName, "getDelayUntil");
+            AuthorityInfo authority = getAuthorityLocked(account, userId, providerName,
+                    "getDelayUntil");
             if (authority == null) {
                 return 0;
             }
@@ -580,7 +628,8 @@
         }
     }
 
-    private void updateOrRemovePeriodicSync(Account account, String providerName, Bundle extras,
+    private void updateOrRemovePeriodicSync(Account account, int userId, String providerName,
+            Bundle extras,
             long period, boolean add) {
         if (period <= 0) {
             period = 0;
@@ -589,13 +638,14 @@
             extras = new Bundle();
         }
         if (Log.isLoggable(TAG, Log.VERBOSE)) {
-            Log.v(TAG, "addOrRemovePeriodicSync: " + account + ", provider " + providerName
+            Log.v(TAG, "addOrRemovePeriodicSync: " + account + ", user " + userId
+                    + ", provider " + providerName
                     + " -> period " + period + ", extras " + extras);
         }
         synchronized (mAuthorities) {
             try {
                 AuthorityInfo authority =
-                        getOrCreateAuthorityLocked(account, providerName, -1, false);
+                        getOrCreateAuthorityLocked(account, userId, providerName, -1, false);
                 if (add) {
                     // add this periodic sync if one with the same extras doesn't already
                     // exist in the periodicSyncs array
@@ -652,61 +702,67 @@
         reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
     }
 
-    public void addPeriodicSync(Account account, String providerName, Bundle extras,
+    public void addPeriodicSync(Account account, int userId, String providerName, Bundle extras,
             long pollFrequency) {
-        updateOrRemovePeriodicSync(account, providerName, extras, pollFrequency, true /* add */);
+        updateOrRemovePeriodicSync(account, userId, providerName, extras, pollFrequency,
+                true /* add */);
     }
 
-    public void removePeriodicSync(Account account, String providerName, Bundle extras) {
-        updateOrRemovePeriodicSync(account, providerName, extras, 0 /* period, ignored */,
+    public void removePeriodicSync(Account account, int userId, String providerName,
+            Bundle extras) {
+        updateOrRemovePeriodicSync(account, userId, providerName, extras, 0 /* period, ignored */,
                 false /* remove */);
     }
 
-    public List<PeriodicSync> getPeriodicSyncs(Account account, String providerName) {
+    public List<PeriodicSync> getPeriodicSyncs(Account account, int userId, String providerName) {
         ArrayList<PeriodicSync> syncs = new ArrayList<PeriodicSync>();
         synchronized (mAuthorities) {
-            AuthorityInfo authority = getAuthorityLocked(account, providerName, "getPeriodicSyncs");
+            AuthorityInfo authority = getAuthorityLocked(account, userId, providerName,
+                    "getPeriodicSyncs");
             if (authority != null) {
                 for (Pair<Bundle, Long> item : authority.periodicSyncs) {
-                    syncs.add(new PeriodicSync(account, providerName, item.first, item.second));
+                    syncs.add(new PeriodicSync(account, providerName, item.first,
+                            item.second));
                 }
             }
         }
         return syncs;
     }
 
-    public void setMasterSyncAutomatically(boolean flag) {
+    public void setMasterSyncAutomatically(boolean flag, int userId) {
         synchronized (mAuthorities) {
-            if (mMasterSyncAutomatically == flag) {
+            Boolean auto = mMasterSyncAutomatically.get(userId);
+            if (auto != null && (boolean) auto == flag) {
                 return;
             }
-            mMasterSyncAutomatically = flag;
+            mMasterSyncAutomatically.put(userId, flag);
             writeAccountInfoLocked();
         }
         if (flag) {
-            ContentResolver.requestSync(null, null, new Bundle());
+            requestSync(null, userId, null, new Bundle());
         }
         reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
         mContext.sendBroadcast(SYNC_CONNECTION_SETTING_CHANGED_INTENT);
     }
 
-    public boolean getMasterSyncAutomatically() {
+    public boolean getMasterSyncAutomatically(int userId) {
         synchronized (mAuthorities) {
-            return mMasterSyncAutomatically;
+            Boolean auto = mMasterSyncAutomatically.get(userId);
+            return auto == null ? true : auto;
         }
     }
 
-    public AuthorityInfo getOrCreateAuthority(Account account, String authority) {
+    public AuthorityInfo getOrCreateAuthority(Account account, int userId, String authority) {
         synchronized (mAuthorities) {
-            return getOrCreateAuthorityLocked(account, authority,
+            return getOrCreateAuthorityLocked(account, userId, authority,
                     -1 /* assign a new identifier if creating a new authority */,
                     true /* write to storage if this results in a change */);
         }
     }
 
-    public void removeAuthority(Account account, String authority) {
+    public void removeAuthority(Account account, int userId, String authority) {
         synchronized (mAuthorities) {
-            removeAuthorityLocked(account, authority, true /* doWrite */);
+            removeAuthorityLocked(account, userId, authority, true /* doWrite */);
         }
     }
 
@@ -720,12 +776,13 @@
      * Returns true if there is currently a sync operation for the given
      * account or authority actively being processed.
      */
-    public boolean isSyncActive(Account account, String authority) {
+    public boolean isSyncActive(Account account, int userId, String authority) {
         synchronized (mAuthorities) {
-            for (SyncInfo syncInfo : mCurrentSyncs) {
+            for (SyncInfo syncInfo : getCurrentSyncs(userId)) {
                 AuthorityInfo ainfo = getAuthority(syncInfo.authorityId);
                 if (ainfo != null && ainfo.account.equals(account)
-                        && ainfo.authority.equals(authority)) {
+                        && ainfo.authority.equals(authority)
+                        && ainfo.userId == userId) {
                     return true;
                 }
             }
@@ -738,12 +795,13 @@
         synchronized (mAuthorities) {
             if (Log.isLoggable(TAG, Log.VERBOSE)) {
                 Log.v(TAG, "insertIntoPending: account=" + op.account
-                    + " auth=" + op.authority
-                    + " src=" + op.syncSource
-                    + " extras=" + op.extras);
+                        + " user=" + op.userId
+                        + " auth=" + op.authority
+                        + " src=" + op.syncSource
+                        + " extras=" + op.extras);
             }
 
-            AuthorityInfo authority = getOrCreateAuthorityLocked(op.account,
+            AuthorityInfo authority = getOrCreateAuthorityLocked(op.account, op.userId,
                     op.authority,
                     -1 /* desired identifier */,
                     true /* write accounts to storage */);
@@ -769,6 +827,7 @@
         synchronized (mAuthorities) {
             if (Log.isLoggable(TAG, Log.VERBOSE)) {
                 Log.v(TAG, "deleteFromPending: account=" + op.account
+                    + " user=" + op.userId
                     + " auth=" + op.authority
                     + " src=" + op.syncSource
                     + " extras=" + op.extras);
@@ -782,7 +841,7 @@
                     mNumPendingFinished++;
                 }
 
-                AuthorityInfo authority = getAuthorityLocked(op.account, op.authority,
+                AuthorityInfo authority = getAuthorityLocked(op.account, op.userId, op.authority,
                         "deleteFromPending");
                 if (authority != null) {
                     if (Log.isLoggable(TAG, Log.VERBOSE)) Log.v(TAG, "removing - " + authority);
@@ -791,7 +850,8 @@
                     for (int i=0; i<N; i++) {
                         PendingOperation cur = mPendingOperations.get(i);
                         if (cur.account.equals(op.account)
-                                && cur.authority.equals(op.authority)) {
+                                && cur.authority.equals(op.authority)
+                                && cur.userId == op.userId) {
                             morePending = true;
                             break;
                         }
@@ -812,24 +872,6 @@
         return res;
     }
 
-    public int clearPending() {
-        int num;
-        synchronized (mAuthorities) {
-            if (Log.isLoggable(TAG, Log.VERBOSE)) {
-                Log.v(TAG, "clearPending");
-            }
-            num = mPendingOperations.size();
-            mPendingOperations.clear();
-            final int N = mSyncStatus.size();
-            for (int i=0; i<N; i++) {
-                mSyncStatus.valueAt(i).pending = false;
-            }
-            writePendingOperationsLocked();
-        }
-        reportChange(ContentResolver.SYNC_OBSERVER_TYPE_PENDING);
-        return num;
-    }
-
     /**
      * Return a copy of the current array of pending operations.  The
      * PendingOperation objects are the real objects stored inside, so that
@@ -854,17 +896,18 @@
      * Called when the set of account has changed, given the new array of
      * active accounts.
      */
-    public void doDatabaseCleanup(Account[] accounts) {
+    public void doDatabaseCleanup(Account[] accounts, int userId) {
         synchronized (mAuthorities) {
             if (Log.isLoggable(TAG, Log.VERBOSE)) Log.w(TAG, "Updating for new accounts...");
             SparseArray<AuthorityInfo> removing = new SparseArray<AuthorityInfo>();
             Iterator<AccountInfo> accIt = mAccounts.values().iterator();
             while (accIt.hasNext()) {
                 AccountInfo acc = accIt.next();
-                if (!ArrayUtils.contains(accounts, acc.account)) {
+                if (!ArrayUtils.contains(accounts, acc.accountAndUser.account)
+                        && acc.accountAndUser.userId == userId) {
                     // This account no longer exists...
                     if (Log.isLoggable(TAG, Log.VERBOSE)) {
-                        Log.w(TAG, "Account removed: " + acc.account);
+                        Log.w(TAG, "Account removed: " + acc.accountAndUser);
                     }
                     for (AuthorityInfo auth : acc.authorities.values()) {
                         removing.put(auth.ident, auth);
@@ -919,13 +962,14 @@
             }
             AuthorityInfo authority = getOrCreateAuthorityLocked(
                     activeSyncContext.mSyncOperation.account,
+                    activeSyncContext.mSyncOperation.userId,
                     activeSyncContext.mSyncOperation.authority,
                     -1 /* assign a new identifier if creating a new authority */,
                     true /* write to storage if this results in a change */);
             syncInfo = new SyncInfo(authority.ident,
                     authority.account, authority.authority,
                     activeSyncContext.mStartTime);
-            mCurrentSyncs.add(syncInfo);
+            getCurrentSyncs(authority.userId).add(syncInfo);
         }
 
         reportActiveChange();
@@ -935,13 +979,14 @@
     /**
      * Called to indicate that a previously active sync is no longer active.
      */
-    public void removeActiveSync(SyncInfo syncInfo) {
+    public void removeActiveSync(SyncInfo syncInfo, int userId) {
         synchronized (mAuthorities) {
             if (Log.isLoggable(TAG, Log.VERBOSE)) {
-                Log.v(TAG, "removeActiveSync: account="
-                        + syncInfo.account + " auth=" + syncInfo.authority);
+                Log.v(TAG, "removeActiveSync: account=" + syncInfo.account
+                        + " user=" + userId
+                        + " auth=" + syncInfo.authority);
             }
-            mCurrentSyncs.remove(syncInfo);
+            getCurrentSyncs(userId).remove(syncInfo);
         }
 
         reportActiveChange();
@@ -957,15 +1002,15 @@
     /**
      * Note that sync has started for the given account and authority.
      */
-    public long insertStartSyncEvent(Account accountName, String authorityName,
+    public long insertStartSyncEvent(Account accountName, int userId, String authorityName,
             long now, int source) {
         long id;
         synchronized (mAuthorities) {
             if (Log.isLoggable(TAG, Log.VERBOSE)) {
-                Log.v(TAG, "insertStartSyncEvent: account=" + accountName
+                Log.v(TAG, "insertStartSyncEvent: account=" + accountName + "user=" + userId
                     + " auth=" + authorityName + " source=" + source);
             }
-            AuthorityInfo authority = getAuthorityLocked(accountName, authorityName,
+            AuthorityInfo authority = getAuthorityLocked(accountName, userId, authorityName,
                     "insertStartSyncEvent");
             if (authority == null) {
                 return -1;
@@ -1119,9 +1164,14 @@
      * Return a list of the currently active syncs. Note that the returned items are the
      * real, live active sync objects, so be careful what you do with it.
      */
-    public List<SyncInfo> getCurrentSyncs() {
+    public List<SyncInfo> getCurrentSyncs(int userId) {
         synchronized (mAuthorities) {
-            return new ArrayList<SyncInfo>(mCurrentSyncs);
+            ArrayList<SyncInfo> syncs = mCurrentSyncs.get(userId);
+            if (syncs == null) {
+                syncs = new ArrayList<SyncInfo>();
+                mCurrentSyncs.put(userId, syncs);
+            }
+            return new ArrayList<SyncInfo>(syncs);
         }
     }
 
@@ -1164,7 +1214,8 @@
      * @param authority the authority whose row should be selected
      * @return the SyncStatusInfo for the authority
      */
-    public SyncStatusInfo getStatusByAccountAndAuthority(Account account, String authority) {
+    public SyncStatusInfo getStatusByAccountAndAuthority(Account account, int userId,
+            String authority) {
         if (account == null || authority == null) {
           throw new IllegalArgumentException();
         }
@@ -1174,8 +1225,9 @@
                 SyncStatusInfo cur = mSyncStatus.valueAt(i);
                 AuthorityInfo ainfo = mAuthorities.get(cur.authorityId);
 
-                if (ainfo != null && ainfo.authority.equals(authority) &&
-                    account.equals(ainfo.account)) {
+                if (ainfo != null && ainfo.authority.equals(authority)
+                        && ainfo.userId == userId
+                        && account.equals(ainfo.account)) {
                   return cur;
                 }
             }
@@ -1186,7 +1238,7 @@
     /**
      * Return true if the pending status is true of any matching authorities.
      */
-    public boolean isSyncPending(Account account, String authority) {
+    public boolean isSyncPending(Account account, int userId, String authority) {
         synchronized (mAuthorities) {
             final int N = mSyncStatus.size();
             for (int i=0; i<N; i++) {
@@ -1195,6 +1247,9 @@
                 if (ainfo == null) {
                     continue;
                 }
+                if (userId != ainfo.userId) {
+                    continue;
+                }
                 if (account != null && !ainfo.account.equals(account)) {
                     continue;
                 }
@@ -1235,34 +1290,6 @@
         }
     }
 
-    /**
-     * If sync is failing for any of the provider/accounts then determine the time at which it
-     * started failing and return the earliest time over all the provider/accounts. If none are
-     * failing then return 0.
-     */
-    public long getInitialSyncFailureTime() {
-        synchronized (mAuthorities) {
-            if (!mMasterSyncAutomatically) {
-                return 0;
-            }
-
-            long oldest = 0;
-            int i = mSyncStatus.size();
-            while (i > 0) {
-                i--;
-                SyncStatusInfo stats = mSyncStatus.valueAt(i);
-                AuthorityInfo authority = mAuthorities.get(stats.authorityId);
-                if (authority != null && authority.enabled) {
-                    if (oldest == 0 || stats.initialFailureTime < oldest) {
-                        oldest = stats.initialFailureTime;
-                    }
-                }
-            }
-
-            return oldest;
-        }
-    }
-
     private int getCurrentDayLocked() {
         mCal.setTimeInMillis(System.currentTimeMillis());
         final int dayOfYear = mCal.get(Calendar.DAY_OF_YEAR);
@@ -1283,18 +1310,19 @@
      * @param tag If non-null, this will be used in a log message if the
      * requested authority does not exist.
      */
-    private AuthorityInfo getAuthorityLocked(Account accountName, String authorityName,
+    private AuthorityInfo getAuthorityLocked(Account accountName, int userId, String authorityName,
             String tag) {
-        AccountInfo account = mAccounts.get(accountName);
-        if (account == null) {
+        AccountAndUser au = new AccountAndUser(accountName, userId);
+        AccountInfo accountInfo = mAccounts.get(au);
+        if (accountInfo == null) {
             if (tag != null) {
                 if (Log.isLoggable(TAG, Log.VERBOSE)) {
-                    Log.v(TAG, tag + ": unknown account " + accountName);
+                    Log.v(TAG, tag + ": unknown account " + au);
                 }
             }
             return null;
         }
-        AuthorityInfo authority = account.authorities.get(authorityName);
+        AuthorityInfo authority = accountInfo.authorities.get(authorityName);
         if (authority == null) {
             if (tag != null) {
                 if (Log.isLoggable(TAG, Log.VERBOSE)) {
@@ -1307,12 +1335,13 @@
         return authority;
     }
 
-    private AuthorityInfo getOrCreateAuthorityLocked(Account accountName,
+    private AuthorityInfo getOrCreateAuthorityLocked(Account accountName, int userId,
             String authorityName, int ident, boolean doWrite) {
-        AccountInfo account = mAccounts.get(accountName);
+        AccountAndUser au = new AccountAndUser(accountName, userId);
+        AccountInfo account = mAccounts.get(au);
         if (account == null) {
-            account = new AccountInfo(accountName);
-            mAccounts.put(accountName, account);
+            account = new AccountInfo(au);
+            mAccounts.put(au, account);
         }
         AuthorityInfo authority = account.authorities.get(authorityName);
         if (authority == null) {
@@ -1323,9 +1352,10 @@
             }
             if (Log.isLoggable(TAG, Log.VERBOSE)) {
                 Log.v(TAG, "created a new AuthorityInfo for " + accountName
-                    + ", provider " + authorityName);
+                        + ", user " + userId
+                        + ", provider " + authorityName);
             }
-            authority = new AuthorityInfo(accountName, authorityName, ident);
+            authority = new AuthorityInfo(accountName, userId, authorityName, ident);
             account.authorities.put(authorityName, authority);
             mAuthorities.put(ident, authority);
             if (doWrite) {
@@ -1336,8 +1366,9 @@
         return authority;
     }
 
-    private void removeAuthorityLocked(Account account, String authorityName, boolean doWrite) {
-        AccountInfo accountInfo = mAccounts.get(account);
+    private void removeAuthorityLocked(Account account, int userId, String authorityName,
+            boolean doWrite) {
+        AccountInfo accountInfo = mAccounts.get(new AccountAndUser(account, userId));
         if (accountInfo != null) {
             final AuthorityInfo authorityInfo = accountInfo.authorities.remove(authorityName);
             if (authorityInfo != null) {
@@ -1419,8 +1450,7 @@
             }
             String tagName = parser.getName();
             if ("accounts".equals(tagName)) {
-                String listen = parser.getAttributeValue(
-                        null, "listen-for-tickles");
+                String listen = parser.getAttributeValue(null, XML_ATTR_LISTEN_FOR_TICKLES);
                 String versionString = parser.getAttributeValue(null, "version");
                 int version;
                 try {
@@ -1428,14 +1458,14 @@
                 } catch (NumberFormatException e) {
                     version = 0;
                 }
-                String nextIdString = parser.getAttributeValue(null, "nextAuthorityId");
+                String nextIdString = parser.getAttributeValue(null, XML_ATTR_NEXT_AUTHORITY_ID);
                 try {
                     int id = (nextIdString == null) ? 0 : Integer.parseInt(nextIdString);
                     mNextAuthorityId = Math.max(mNextAuthorityId, id);
                 } catch (NumberFormatException e) {
                     // don't care
                 }
-                mMasterSyncAutomatically = listen == null || Boolean.parseBoolean(listen);
+                mMasterSyncAutomatically.put(0, listen == null || Boolean.parseBoolean(listen));
                 eventType = parser.next();
                 AuthorityInfo authority = null;
                 Pair<Bundle, Long> periodicSync = null;
@@ -1449,6 +1479,8 @@
                                 if (authority.ident > highestAuthorityId) {
                                     highestAuthorityId = authority.ident;
                                 }
+                            } else if (XML_TAG_LISTEN_FOR_TICKLES.equals(tagName)) {
+                                parseListenForTickles(parser);
                             }
                         } else if (parser.getDepth() == 3) {
                             if ("periodicSync".equals(tagName) && authority != null) {
@@ -1511,25 +1543,41 @@
             }
 
             // if we already have a record of this new authority then don't copy over the settings
-            if (getAuthorityLocked(authority.account, newAuthorityName, "cleanup") != null) {
+            if (getAuthorityLocked(authority.account, authority.userId, newAuthorityName, "cleanup")
+                    != null) {
                 continue;
             }
 
             AuthorityInfo newAuthority = getOrCreateAuthorityLocked(authority.account,
-                    newAuthorityName, -1 /* ident */, false /* doWrite */);
+                    authority.userId, newAuthorityName, -1 /* ident */, false /* doWrite */);
             newAuthority.enabled = true;
             writeNeeded = true;
         }
 
         for (AuthorityInfo authorityInfo : authoritiesToRemove) {
-            removeAuthorityLocked(authorityInfo.account, authorityInfo.authority,
-                    false /* doWrite */);
+            removeAuthorityLocked(authorityInfo.account, authorityInfo.userId,
+                    authorityInfo.authority, false /* doWrite */);
             writeNeeded = true;
         }
 
         return writeNeeded;
     }
 
+    private void parseListenForTickles(XmlPullParser parser) {
+        String user = parser.getAttributeValue(null, XML_ATTR_USER);
+        int userId = 0;
+        try {
+            userId = Integer.parseInt(user);
+        } catch (NumberFormatException e) {
+            Log.e(TAG, "error parsing the user for listen-for-tickles", e);
+        } catch (NullPointerException e) {
+            Log.e(TAG, "the user in listen-for-tickles is null", e);
+        }
+        String enabled = parser.getAttributeValue(null, XML_ATTR_ENABLED);
+        boolean listen = enabled == null || Boolean.parseBoolean(enabled);
+        mMasterSyncAutomatically.put(userId, listen);
+    }
+
     private AuthorityInfo parseAuthority(XmlPullParser parser, int version) {
         AuthorityInfo authority = null;
         int id = -1;
@@ -1543,10 +1591,12 @@
         }
         if (id >= 0) {
             String authorityName = parser.getAttributeValue(null, "authority");
-            String enabled = parser.getAttributeValue(null, "enabled");
+            String enabled = parser.getAttributeValue(null, XML_ATTR_ENABLED);
             String syncable = parser.getAttributeValue(null, "syncable");
             String accountName = parser.getAttributeValue(null, "account");
             String accountType = parser.getAttributeValue(null, "type");
+            String user = parser.getAttributeValue(null, XML_ATTR_USER);
+            int userId = user == null ? 0 : Integer.parseInt(user);
             if (accountType == null) {
                 accountType = "com.google";
                 syncable = "unknown";
@@ -1554,12 +1604,13 @@
             authority = mAuthorities.get(id);
             if (DEBUG_FILE) Log.v(TAG, "Adding authority: account="
                     + accountName + " auth=" + authorityName
+                    + " user=" + userId
                     + " enabled=" + enabled
                     + " syncable=" + syncable);
             if (authority == null) {
                 if (DEBUG_FILE) Log.v(TAG, "Creating entry");
                 authority = getOrCreateAuthorityLocked(
-                        new Account(accountName, accountType), authorityName, id, false);
+                        new Account(accountName, accountType), userId, authorityName, id, false);
                 // If the version is 0 then we are upgrading from a file format that did not
                 // know about periodic syncs. In that case don't clear the list since we
                 // want the default, which is a daily periodioc sync.
@@ -1653,9 +1704,17 @@
 
             out.startTag(null, "accounts");
             out.attribute(null, "version", Integer.toString(ACCOUNTS_VERSION));
-            out.attribute(null, "nextAuthorityId", Integer.toString(mNextAuthorityId));
-            if (!mMasterSyncAutomatically) {
-                out.attribute(null, "listen-for-tickles", "false");
+            out.attribute(null, XML_ATTR_NEXT_AUTHORITY_ID, Integer.toString(mNextAuthorityId));
+
+            // Write the Sync Automatically flags for each user
+            final int M = mMasterSyncAutomatically.size();
+            for (int m = 0; m < M; m++) {
+                int userId = mMasterSyncAutomatically.keyAt(m);
+                Boolean listen = mMasterSyncAutomatically.valueAt(m);
+                out.startTag(null, XML_TAG_LISTEN_FOR_TICKLES);
+                out.attribute(null, XML_ATTR_USER, Integer.toString(userId));
+                out.attribute(null, XML_ATTR_ENABLED, Boolean.toString(listen));
+                out.endTag(null, XML_TAG_LISTEN_FOR_TICKLES);
             }
 
             final int N = mAuthorities.size();
@@ -1664,9 +1723,10 @@
                 out.startTag(null, "authority");
                 out.attribute(null, "id", Integer.toString(authority.ident));
                 out.attribute(null, "account", authority.account.name);
+                out.attribute(null, XML_ATTR_USER, Integer.toString(authority.userId));
                 out.attribute(null, "type", authority.account.type);
                 out.attribute(null, "authority", authority.authority);
-                out.attribute(null, "enabled", Boolean.toString(authority.enabled));
+                out.attribute(null, XML_ATTR_ENABLED, Boolean.toString(authority.enabled));
                 if (authority.syncable < 0) {
                     out.attribute(null, "syncable", "unknown");
                 } else {
@@ -1788,7 +1848,7 @@
                 }
                 String authorityName = c.getString(c.getColumnIndex("authority"));
                 AuthorityInfo authority = this.getOrCreateAuthorityLocked(
-                        new Account(accountName, accountType),
+                        new Account(accountName, accountType), 0 /* legacy is single-user */,
                         authorityName, -1, false);
                 if (authority != null) {
                     int i = mSyncStatus.size();
@@ -1833,7 +1893,7 @@
                 String value = c.getString(c.getColumnIndex("value"));
                 if (name == null) continue;
                 if (name.equals("listen_for_tickles")) {
-                    setMasterSyncAutomatically(value == null || Boolean.parseBoolean(value));
+                    setMasterSyncAutomatically(value == null || Boolean.parseBoolean(value), 0);
                 } else if (name.startsWith("sync_provider_")) {
                     String provider = name.substring("sync_provider_".length(),
                             name.length());
@@ -1964,7 +2024,7 @@
                         extras = new Bundle();
                     }
                     PendingOperation op = new PendingOperation(
-                            authority.account, syncSource,
+                            authority.account, authority.userId, syncSource,
                             authority.authority, extras, expedited);
                     op.authorityId = authorityId;
                     op.flatExtras = flatExtras;
@@ -2084,6 +2144,19 @@
         return bundle;
     }
 
+    private void requestSync(Account account, int userId, String authority, Bundle extras) {
+        // If this is happening in the system process, then call the syncrequest listener
+        // to make a request back to the SyncManager directly.
+        // If this is probably a test instance, then call back through the ContentResolver
+        // which will know which userId to apply based on the Binder id.
+        if (android.os.Process.myUid() == android.os.Process.SYSTEM_UID
+                && mSyncRequestListener != null) {
+            mSyncRequestListener.onSyncRequest(account, userId, authority, extras);
+        } else {
+            ContentResolver.requestSync(account, authority, extras);
+        }
+    }
+
     public static final int STATISTICS_FILE_END = 0;
     public static final int STATISTICS_FILE_ITEM_OLD = 100;
     public static final int STATISTICS_FILE_ITEM = 101;