Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 17 | package com.android.server.accounts; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 18 | |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 19 | import android.Manifest; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 20 | import android.accounts.Account; |
| 21 | import android.accounts.AccountAndUser; |
| 22 | import android.accounts.AccountAuthenticatorResponse; |
| 23 | import android.accounts.AccountManager; |
| 24 | import android.accounts.AuthenticatorDescription; |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 25 | import android.accounts.CantAddAccountActivity; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 26 | import android.accounts.GrantCredentialsPermissionActivity; |
| 27 | import android.accounts.IAccountAuthenticator; |
| 28 | import android.accounts.IAccountAuthenticatorResponse; |
| 29 | import android.accounts.IAccountManager; |
| 30 | import android.accounts.IAccountManagerResponse; |
Brett Chabot | 3b4fcbc | 2011-01-09 13:41:02 -0800 | [diff] [blame] | 31 | import android.app.ActivityManager; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 32 | import android.app.ActivityManagerNative; |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 33 | import android.app.AppGlobals; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 34 | import android.app.Notification; |
| 35 | import android.app.NotificationManager; |
| 36 | import android.app.PendingIntent; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 37 | import android.content.BroadcastReceiver; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 38 | import android.content.ComponentName; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 39 | import android.content.ContentValues; |
| 40 | import android.content.Context; |
| 41 | import android.content.Intent; |
| 42 | import android.content.IntentFilter; |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 43 | import android.content.ServiceConnection; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 44 | import android.content.pm.ApplicationInfo; |
| 45 | import android.content.pm.PackageInfo; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 46 | import android.content.pm.PackageManager; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 47 | import android.content.pm.PackageManager.NameNotFoundException; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 48 | import android.content.pm.RegisteredServicesCache; |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 49 | import android.content.pm.RegisteredServicesCacheListener; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 50 | import android.content.pm.UserInfo; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 51 | import android.database.Cursor; |
| 52 | import android.database.DatabaseUtils; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 53 | import android.database.sqlite.SQLiteDatabase; |
| 54 | import android.database.sqlite.SQLiteOpenHelper; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 55 | import android.os.Binder; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 56 | import android.os.Bundle; |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 57 | import android.os.Environment; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 58 | import android.os.Handler; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 59 | import android.os.IBinder; |
| 60 | import android.os.Looper; |
| 61 | import android.os.Message; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 62 | import android.os.Process; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 63 | import android.os.RemoteException; |
| 64 | import android.os.SystemClock; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 65 | import android.os.UserHandle; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 66 | import android.os.UserManager; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 67 | import android.text.TextUtils; |
| 68 | import android.util.Log; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 69 | import android.util.Pair; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 70 | import android.util.Slog; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 71 | import android.util.SparseArray; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 72 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 73 | import com.android.internal.R; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 74 | import com.android.internal.util.ArrayUtils; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 75 | import com.android.internal.util.IndentingPrintWriter; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 76 | import com.android.server.FgThread; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 77 | import com.google.android.collect.Lists; |
| 78 | import com.google.android.collect.Sets; |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 79 | |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 80 | import java.io.File; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 81 | import java.io.FileDescriptor; |
| 82 | import java.io.PrintWriter; |
| 83 | import java.util.ArrayList; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 84 | import java.util.Arrays; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 85 | import java.util.Collection; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 86 | import java.util.HashMap; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 87 | import java.util.HashSet; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 88 | import java.util.LinkedHashMap; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 89 | import java.util.List; |
Andy McFadden | 2f36229 | 2012-01-20 14:43:38 -0800 | [diff] [blame] | 90 | import java.util.Map; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 91 | import java.util.concurrent.atomic.AtomicInteger; |
| 92 | import java.util.concurrent.atomic.AtomicReference; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 93 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 94 | /** |
| 95 | * A system service that provides account, password, and authtoken management for all |
| 96 | * accounts on the device. Some of these calls are implemented with the help of the corresponding |
| 97 | * {@link IAccountAuthenticator} services. This service is not accessed by users directly, |
| 98 | * instead one uses an instance of {@link AccountManager}, which can be accessed as follows: |
Brian Carlstrom | 46703b0 | 2011-04-06 15:41:29 -0700 | [diff] [blame] | 99 | * AccountManager accountManager = AccountManager.get(context); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 100 | * @hide |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 101 | */ |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 102 | public class AccountManagerService |
| 103 | extends IAccountManager.Stub |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 104 | implements RegisteredServicesCacheListener<AuthenticatorDescription> { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 105 | private static final String TAG = "AccountManagerService"; |
| 106 | |
| 107 | private static final int TIMEOUT_DELAY_MS = 1000 * 60; |
| 108 | private static final String DATABASE_NAME = "accounts.db"; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 109 | private static final int DATABASE_VERSION = 5; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 110 | |
| 111 | private final Context mContext; |
| 112 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 113 | private final PackageManager mPackageManager; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 114 | private UserManager mUserManager; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 115 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 116 | private final MessageHandler mMessageHandler; |
| 117 | |
| 118 | // Messages that can be sent on mHandler |
| 119 | private static final int MESSAGE_TIMED_OUT = 3; |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 120 | private static final int MESSAGE_COPY_SHARED_ACCOUNT = 4; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 121 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 122 | private final IAccountAuthenticatorCache mAuthenticatorCache; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 123 | |
| 124 | private static final String TABLE_ACCOUNTS = "accounts"; |
| 125 | private static final String ACCOUNTS_ID = "_id"; |
| 126 | private static final String ACCOUNTS_NAME = "name"; |
| 127 | private static final String ACCOUNTS_TYPE = "type"; |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 128 | private static final String ACCOUNTS_TYPE_COUNT = "count(type)"; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 129 | private static final String ACCOUNTS_PASSWORD = "password"; |
| 130 | |
| 131 | private static final String TABLE_AUTHTOKENS = "authtokens"; |
| 132 | private static final String AUTHTOKENS_ID = "_id"; |
| 133 | private static final String AUTHTOKENS_ACCOUNTS_ID = "accounts_id"; |
| 134 | private static final String AUTHTOKENS_TYPE = "type"; |
| 135 | private static final String AUTHTOKENS_AUTHTOKEN = "authtoken"; |
| 136 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 137 | private static final String TABLE_GRANTS = "grants"; |
| 138 | private static final String GRANTS_ACCOUNTS_ID = "accounts_id"; |
| 139 | private static final String GRANTS_AUTH_TOKEN_TYPE = "auth_token_type"; |
| 140 | private static final String GRANTS_GRANTEE_UID = "uid"; |
| 141 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 142 | private static final String TABLE_EXTRAS = "extras"; |
| 143 | private static final String EXTRAS_ID = "_id"; |
| 144 | private static final String EXTRAS_ACCOUNTS_ID = "accounts_id"; |
| 145 | private static final String EXTRAS_KEY = "key"; |
| 146 | private static final String EXTRAS_VALUE = "value"; |
| 147 | |
| 148 | private static final String TABLE_META = "meta"; |
| 149 | private static final String META_KEY = "key"; |
| 150 | private static final String META_VALUE = "value"; |
| 151 | |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 152 | private static final String TABLE_SHARED_ACCOUNTS = "shared_accounts"; |
| 153 | |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 154 | private static final String[] ACCOUNT_TYPE_COUNT_PROJECTION = |
| 155 | new String[] { ACCOUNTS_TYPE, ACCOUNTS_TYPE_COUNT}; |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 156 | private static final Intent ACCOUNTS_CHANGED_INTENT; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 157 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 158 | private static final String COUNT_OF_MATCHING_GRANTS = "" |
| 159 | + "SELECT COUNT(*) FROM " + TABLE_GRANTS + ", " + TABLE_ACCOUNTS |
| 160 | + " WHERE " + GRANTS_ACCOUNTS_ID + "=" + ACCOUNTS_ID |
| 161 | + " AND " + GRANTS_GRANTEE_UID + "=?" |
| 162 | + " AND " + GRANTS_AUTH_TOKEN_TYPE + "=?" |
| 163 | + " AND " + ACCOUNTS_NAME + "=?" |
| 164 | + " AND " + ACCOUNTS_TYPE + "=?"; |
| 165 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 166 | private static final String SELECTION_AUTHTOKENS_BY_ACCOUNT = |
| 167 | AUTHTOKENS_ACCOUNTS_ID + "=(select _id FROM accounts WHERE name=? AND type=?)"; |
| 168 | private static final String[] COLUMNS_AUTHTOKENS_TYPE_AND_AUTHTOKEN = {AUTHTOKENS_TYPE, |
| 169 | AUTHTOKENS_AUTHTOKEN}; |
| 170 | |
| 171 | private static final String SELECTION_USERDATA_BY_ACCOUNT = |
| 172 | EXTRAS_ACCOUNTS_ID + "=(select _id FROM accounts WHERE name=? AND type=?)"; |
| 173 | private static final String[] COLUMNS_EXTRAS_KEY_AND_VALUE = {EXTRAS_KEY, EXTRAS_VALUE}; |
| 174 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 175 | private final LinkedHashMap<String, Session> mSessions = new LinkedHashMap<String, Session>(); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 176 | private final AtomicInteger mNotificationIds = new AtomicInteger(1); |
| 177 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 178 | static class UserAccounts { |
| 179 | private final int userId; |
| 180 | private final DatabaseHelper openHelper; |
| 181 | private final HashMap<Pair<Pair<Account, String>, Integer>, Integer> |
| 182 | credentialsPermissionNotificationIds = |
| 183 | new HashMap<Pair<Pair<Account, String>, Integer>, Integer>(); |
| 184 | private final HashMap<Account, Integer> signinRequiredNotificationIds = |
| 185 | new HashMap<Account, Integer>(); |
| 186 | private final Object cacheLock = new Object(); |
| 187 | /** protected by the {@link #cacheLock} */ |
Amith Yamasani | b483a99 | 2012-05-22 13:14:25 -0700 | [diff] [blame] | 188 | private final HashMap<String, Account[]> accountCache = |
| 189 | new LinkedHashMap<String, Account[]>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 190 | /** protected by the {@link #cacheLock} */ |
| 191 | private HashMap<Account, HashMap<String, String>> userDataCache = |
| 192 | new HashMap<Account, HashMap<String, String>>(); |
| 193 | /** protected by the {@link #cacheLock} */ |
| 194 | private HashMap<Account, HashMap<String, String>> authTokenCache = |
| 195 | new HashMap<Account, HashMap<String, String>>(); |
| 196 | |
| 197 | UserAccounts(Context context, int userId) { |
| 198 | this.userId = userId; |
| 199 | synchronized (cacheLock) { |
| 200 | openHelper = new DatabaseHelper(context, userId); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | private final SparseArray<UserAccounts> mUsers = new SparseArray<UserAccounts>(); |
| 206 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 207 | private static AtomicReference<AccountManagerService> sThis = |
| 208 | new AtomicReference<AccountManagerService>(); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 209 | private static final Account[] EMPTY_ACCOUNT_ARRAY = new Account[]{}; |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 210 | |
| 211 | static { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 212 | ACCOUNTS_CHANGED_INTENT = new Intent(AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION); |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 213 | ACCOUNTS_CHANGED_INTENT.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 214 | } |
| 215 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 216 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 217 | /** |
| 218 | * This should only be called by system code. One should only call this after the service |
| 219 | * has started. |
| 220 | * @return a reference to the AccountManagerService instance |
| 221 | * @hide |
| 222 | */ |
| 223 | public static AccountManagerService getSingleton() { |
| 224 | return sThis.get(); |
| 225 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 226 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 227 | public AccountManagerService(Context context) { |
| 228 | this(context, context.getPackageManager(), new AccountAuthenticatorCache(context)); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 231 | public AccountManagerService(Context context, PackageManager packageManager, |
| 232 | IAccountAuthenticatorCache authenticatorCache) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 233 | mContext = context; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 234 | mPackageManager = packageManager; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 235 | |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 236 | mMessageHandler = new MessageHandler(FgThread.get().getLooper()); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 237 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 238 | mAuthenticatorCache = authenticatorCache; |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 239 | mAuthenticatorCache.setListener(this, null /* Handler */); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 240 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 241 | sThis.set(this); |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 242 | |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 243 | IntentFilter intentFilter = new IntentFilter(); |
| 244 | intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 245 | intentFilter.addDataScheme("package"); |
| 246 | mContext.registerReceiver(new BroadcastReceiver() { |
| 247 | @Override |
| 248 | public void onReceive(Context context1, Intent intent) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 249 | purgeOldGrantsAll(); |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 250 | } |
| 251 | }, intentFilter); |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 252 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 253 | IntentFilter userFilter = new IntentFilter(); |
| 254 | userFilter.addAction(Intent.ACTION_USER_REMOVED); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 255 | userFilter.addAction(Intent.ACTION_USER_STARTED); |
| 256 | mContext.registerReceiverAsUser(new BroadcastReceiver() { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 257 | @Override |
| 258 | public void onReceive(Context context, Intent intent) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 259 | String action = intent.getAction(); |
| 260 | if (Intent.ACTION_USER_REMOVED.equals(action)) { |
| 261 | onUserRemoved(intent); |
| 262 | } else if (Intent.ACTION_USER_STARTED.equals(action)) { |
| 263 | onUserStarted(intent); |
| 264 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 265 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 266 | }, UserHandle.ALL, userFilter, null, null); |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Kenny Root | 26ff662 | 2012-07-30 12:58:03 -0700 | [diff] [blame] | 269 | public void systemReady() { |
Kenny Root | 26ff662 | 2012-07-30 12:58:03 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 272 | private UserManager getUserManager() { |
| 273 | if (mUserManager == null) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 274 | mUserManager = UserManager.get(mContext); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 275 | } |
| 276 | return mUserManager; |
| 277 | } |
| 278 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 279 | private UserAccounts initUser(int userId) { |
| 280 | synchronized (mUsers) { |
| 281 | UserAccounts accounts = mUsers.get(userId); |
| 282 | if (accounts == null) { |
| 283 | accounts = new UserAccounts(mContext, userId); |
| 284 | mUsers.append(userId, accounts); |
| 285 | purgeOldGrants(accounts); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 286 | validateAccountsInternal(accounts, true /* invalidateAuthenticatorCache */); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 287 | } |
| 288 | return accounts; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | private void purgeOldGrantsAll() { |
| 293 | synchronized (mUsers) { |
| 294 | for (int i = 0; i < mUsers.size(); i++) { |
| 295 | purgeOldGrants(mUsers.valueAt(i)); |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | private void purgeOldGrants(UserAccounts accounts) { |
| 301 | synchronized (accounts.cacheLock) { |
| 302 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 303 | final Cursor cursor = db.query(TABLE_GRANTS, |
| 304 | new String[]{GRANTS_GRANTEE_UID}, |
| 305 | null, null, GRANTS_GRANTEE_UID, null, null); |
| 306 | try { |
| 307 | while (cursor.moveToNext()) { |
| 308 | final int uid = cursor.getInt(0); |
| 309 | final boolean packageExists = mPackageManager.getPackagesForUid(uid) != null; |
| 310 | if (packageExists) { |
| 311 | continue; |
| 312 | } |
| 313 | Log.d(TAG, "deleting grants for UID " + uid |
| 314 | + " because its package is no longer installed"); |
| 315 | db.delete(TABLE_GRANTS, GRANTS_GRANTEE_UID + "=?", |
| 316 | new String[]{Integer.toString(uid)}); |
| 317 | } |
| 318 | } finally { |
| 319 | cursor.close(); |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 324 | /** |
| 325 | * Validate internal set of accounts against installed authenticators for |
| 326 | * given user. Clears cached authenticators before validating. |
| 327 | */ |
| 328 | public void validateAccounts(int userId) { |
| 329 | final UserAccounts accounts = getUserAccounts(userId); |
| 330 | |
| 331 | // Invalidate user-specific cache to make sure we catch any |
| 332 | // removed authenticators. |
| 333 | validateAccountsInternal(accounts, true /* invalidateAuthenticatorCache */); |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Validate internal set of accounts against installed authenticators for |
| 338 | * given user. Clear cached authenticators before validating when requested. |
| 339 | */ |
| 340 | private void validateAccountsInternal( |
| 341 | UserAccounts accounts, boolean invalidateAuthenticatorCache) { |
| 342 | if (invalidateAuthenticatorCache) { |
| 343 | mAuthenticatorCache.invalidateCache(accounts.userId); |
| 344 | } |
| 345 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 346 | final HashSet<AuthenticatorDescription> knownAuth = Sets.newHashSet(); |
| 347 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> service : |
| 348 | mAuthenticatorCache.getAllServices(accounts.userId)) { |
| 349 | knownAuth.add(service.type); |
| 350 | } |
| 351 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 352 | synchronized (accounts.cacheLock) { |
| 353 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 354 | boolean accountDeleted = false; |
| 355 | Cursor cursor = db.query(TABLE_ACCOUNTS, |
| 356 | new String[]{ACCOUNTS_ID, ACCOUNTS_TYPE, ACCOUNTS_NAME}, |
| 357 | null, null, null, null, null); |
| 358 | try { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 359 | accounts.accountCache.clear(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 360 | final HashMap<String, ArrayList<String>> accountNamesByType = |
Amith Yamasani | b483a99 | 2012-05-22 13:14:25 -0700 | [diff] [blame] | 361 | new LinkedHashMap<String, ArrayList<String>>(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 362 | while (cursor.moveToNext()) { |
| 363 | final long accountId = cursor.getLong(0); |
| 364 | final String accountType = cursor.getString(1); |
| 365 | final String accountName = cursor.getString(2); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 366 | |
| 367 | if (!knownAuth.contains(AuthenticatorDescription.newKey(accountType))) { |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 368 | Slog.w(TAG, "deleting account " + accountName + " because type " |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 369 | + accountType + " no longer has a registered authenticator"); |
| 370 | db.delete(TABLE_ACCOUNTS, ACCOUNTS_ID + "=" + accountId, null); |
| 371 | accountDeleted = true; |
| 372 | final Account account = new Account(accountName, accountType); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 373 | accounts.userDataCache.remove(account); |
| 374 | accounts.authTokenCache.remove(account); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 375 | } else { |
| 376 | ArrayList<String> accountNames = accountNamesByType.get(accountType); |
| 377 | if (accountNames == null) { |
| 378 | accountNames = new ArrayList<String>(); |
| 379 | accountNamesByType.put(accountType, accountNames); |
| 380 | } |
| 381 | accountNames.add(accountName); |
| 382 | } |
| 383 | } |
Andy McFadden | 2f36229 | 2012-01-20 14:43:38 -0800 | [diff] [blame] | 384 | for (Map.Entry<String, ArrayList<String>> cur |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 385 | : accountNamesByType.entrySet()) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 386 | final String accountType = cur.getKey(); |
| 387 | final ArrayList<String> accountNames = cur.getValue(); |
| 388 | final Account[] accountsForType = new Account[accountNames.size()]; |
| 389 | int i = 0; |
| 390 | for (String accountName : accountNames) { |
| 391 | accountsForType[i] = new Account(accountName, accountType); |
| 392 | ++i; |
| 393 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 394 | accounts.accountCache.put(accountType, accountsForType); |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 395 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 396 | } finally { |
| 397 | cursor.close(); |
| 398 | if (accountDeleted) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 399 | sendAccountsChangedBroadcast(accounts.userId); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 400 | } |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 401 | } |
| 402 | } |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 405 | private UserAccounts getUserAccountsForCaller() { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 406 | return getUserAccounts(UserHandle.getCallingUserId()); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | protected UserAccounts getUserAccounts(int userId) { |
| 410 | synchronized (mUsers) { |
| 411 | UserAccounts accounts = mUsers.get(userId); |
| 412 | if (accounts == null) { |
| 413 | accounts = initUser(userId); |
| 414 | mUsers.append(userId, accounts); |
| 415 | } |
| 416 | return accounts; |
| 417 | } |
| 418 | } |
| 419 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 420 | private void onUserRemoved(Intent intent) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 421 | int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 422 | if (userId < 1) return; |
| 423 | |
| 424 | UserAccounts accounts; |
| 425 | synchronized (mUsers) { |
| 426 | accounts = mUsers.get(userId); |
| 427 | mUsers.remove(userId); |
| 428 | } |
| 429 | if (accounts == null) { |
| 430 | File dbFile = new File(getDatabaseName(userId)); |
| 431 | dbFile.delete(); |
| 432 | return; |
| 433 | } |
| 434 | |
| 435 | synchronized (accounts.cacheLock) { |
| 436 | accounts.openHelper.close(); |
| 437 | File dbFile = new File(getDatabaseName(userId)); |
| 438 | dbFile.delete(); |
| 439 | } |
| 440 | } |
| 441 | |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 442 | private void onUserStarted(Intent intent) { |
| 443 | int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 444 | if (userId < 1) return; |
| 445 | |
| 446 | // Check if there's a shared account that needs to be created as an account |
| 447 | Account[] sharedAccounts = getSharedAccountsAsUser(userId); |
| 448 | if (sharedAccounts == null || sharedAccounts.length == 0) return; |
| 449 | Account[] accounts = getAccountsAsUser(null, userId); |
| 450 | for (Account sa : sharedAccounts) { |
| 451 | if (ArrayUtils.contains(accounts, sa)) continue; |
| 452 | // Account doesn't exist. Copy it now. |
| 453 | copyAccountToUser(sa, UserHandle.USER_OWNER, userId); |
| 454 | } |
| 455 | } |
| 456 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 457 | @Override |
| 458 | public void onServiceChanged(AuthenticatorDescription desc, int userId, boolean removed) { |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 459 | validateAccountsInternal(getUserAccounts(userId), false /* invalidateAuthenticatorCache */); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 462 | public String getPassword(Account account) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 463 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 464 | Log.v(TAG, "getPassword: " + account |
| 465 | + ", caller's uid " + Binder.getCallingUid() |
| 466 | + ", pid " + Binder.getCallingPid()); |
| 467 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 468 | if (account == null) throw new IllegalArgumentException("account is null"); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 469 | checkAuthenticateAccountsPermission(account); |
| 470 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 471 | UserAccounts accounts = getUserAccountsForCaller(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 472 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 473 | try { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 474 | return readPasswordInternal(accounts, account); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 475 | } finally { |
| 476 | restoreCallingIdentity(identityToken); |
| 477 | } |
| 478 | } |
| 479 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 480 | private String readPasswordInternal(UserAccounts accounts, Account account) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 481 | if (account == null) { |
| 482 | return null; |
| 483 | } |
| 484 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 485 | synchronized (accounts.cacheLock) { |
| 486 | final SQLiteDatabase db = accounts.openHelper.getReadableDatabase(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 487 | Cursor cursor = db.query(TABLE_ACCOUNTS, new String[]{ACCOUNTS_PASSWORD}, |
| 488 | ACCOUNTS_NAME + "=? AND " + ACCOUNTS_TYPE+ "=?", |
| 489 | new String[]{account.name, account.type}, null, null, null); |
| 490 | try { |
| 491 | if (cursor.moveToNext()) { |
| 492 | return cursor.getString(0); |
| 493 | } |
| 494 | return null; |
| 495 | } finally { |
| 496 | cursor.close(); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 497 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | |
| 501 | public String getUserData(Account account, String key) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 502 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 503 | Log.v(TAG, "getUserData: " + account |
| 504 | + ", key " + key |
| 505 | + ", caller's uid " + Binder.getCallingUid() |
| 506 | + ", pid " + Binder.getCallingPid()); |
| 507 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 508 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 509 | if (key == null) throw new IllegalArgumentException("key is null"); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 510 | checkAuthenticateAccountsPermission(account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 511 | UserAccounts accounts = getUserAccountsForCaller(); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 512 | long identityToken = clearCallingIdentity(); |
| 513 | try { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 514 | return readUserDataInternal(accounts, account, key); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 515 | } finally { |
| 516 | restoreCallingIdentity(identityToken); |
| 517 | } |
| 518 | } |
| 519 | |
Fred Quintana | 9788976 | 2009-06-15 12:29:24 -0700 | [diff] [blame] | 520 | public AuthenticatorDescription[] getAuthenticatorTypes() { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 521 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 522 | Log.v(TAG, "getAuthenticatorTypes: " |
| 523 | + "caller's uid " + Binder.getCallingUid() |
| 524 | + ", pid " + Binder.getCallingPid()); |
| 525 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 526 | final int userId = UserHandle.getCallingUserId(); |
| 527 | final long identityToken = clearCallingIdentity(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 528 | try { |
Fred Quintana | 9788976 | 2009-06-15 12:29:24 -0700 | [diff] [blame] | 529 | Collection<AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription>> |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 530 | authenticatorCollection = mAuthenticatorCache.getAllServices(userId); |
Fred Quintana | 9788976 | 2009-06-15 12:29:24 -0700 | [diff] [blame] | 531 | AuthenticatorDescription[] types = |
| 532 | new AuthenticatorDescription[authenticatorCollection.size()]; |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 533 | int i = 0; |
Fred Quintana | 9788976 | 2009-06-15 12:29:24 -0700 | [diff] [blame] | 534 | for (AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription> authenticator |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 535 | : authenticatorCollection) { |
| 536 | types[i] = authenticator.type; |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 537 | i++; |
| 538 | } |
| 539 | return types; |
| 540 | } finally { |
| 541 | restoreCallingIdentity(identityToken); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 542 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Jatin Lodhia | 3df7d69 | 2013-03-27 10:57:23 -0700 | [diff] [blame] | 545 | @Override |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 546 | public boolean addAccountExplicitly(Account account, String password, Bundle extras) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 547 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 548 | Log.v(TAG, "addAccountExplicitly: " + account |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 549 | + ", caller's uid " + Binder.getCallingUid() |
| 550 | + ", pid " + Binder.getCallingPid()); |
| 551 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 552 | if (account == null) throw new IllegalArgumentException("account is null"); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 553 | checkAuthenticateAccountsPermission(account); |
Jatin Lodhia | 3df7d69 | 2013-03-27 10:57:23 -0700 | [diff] [blame] | 554 | /* |
| 555 | * Child users are not allowed to add accounts. Only the accounts that are |
| 556 | * shared by the parent profile can be added to child profile. |
| 557 | * |
| 558 | * TODO: Only allow accounts that were shared to be added by |
| 559 | * a limited user. |
| 560 | */ |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 561 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 562 | UserAccounts accounts = getUserAccountsForCaller(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 563 | // fails if the account already exists |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 564 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 565 | try { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 566 | return addAccountInternal(accounts, account, password, extras, false); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 567 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 568 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 572 | private boolean copyAccountToUser(final Account account, int userFrom, int userTo) { |
| 573 | final UserAccounts fromAccounts = getUserAccounts(userFrom); |
| 574 | final UserAccounts toAccounts = getUserAccounts(userTo); |
| 575 | if (fromAccounts == null || toAccounts == null) { |
| 576 | return false; |
| 577 | } |
| 578 | |
| 579 | long identityToken = clearCallingIdentity(); |
| 580 | try { |
| 581 | new Session(fromAccounts, null, account.type, false, |
| 582 | false /* stripAuthTokenFromResult */) { |
| 583 | protected String toDebugString(long now) { |
| 584 | return super.toDebugString(now) + ", getAccountCredentialsForClone" |
| 585 | + ", " + account.type; |
| 586 | } |
| 587 | |
| 588 | public void run() throws RemoteException { |
| 589 | mAuthenticator.getAccountCredentialsForCloning(this, account); |
| 590 | } |
| 591 | |
| 592 | public void onResult(Bundle result) { |
| 593 | if (result != null) { |
| 594 | if (result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)) { |
| 595 | // Create a Session for the target user and pass in the bundle |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 596 | completeCloningAccount(result, account, toAccounts); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 597 | } |
| 598 | return; |
| 599 | } else { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 600 | super.onResult(result); |
| 601 | } |
| 602 | } |
| 603 | }.bind(); |
| 604 | } finally { |
| 605 | restoreCallingIdentity(identityToken); |
| 606 | } |
| 607 | return true; |
| 608 | } |
| 609 | |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 610 | void completeCloningAccount(final Bundle result, final Account account, |
| 611 | final UserAccounts targetUser) { |
| 612 | long id = clearCallingIdentity(); |
| 613 | try { |
| 614 | new Session(targetUser, null, account.type, false, |
| 615 | false /* stripAuthTokenFromResult */) { |
| 616 | protected String toDebugString(long now) { |
| 617 | return super.toDebugString(now) + ", getAccountCredentialsForClone" |
| 618 | + ", " + account.type; |
| 619 | } |
| 620 | |
| 621 | public void run() throws RemoteException { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 622 | // Confirm that the owner's account still exists before this step. |
| 623 | UserAccounts owner = getUserAccounts(UserHandle.USER_OWNER); |
| 624 | synchronized (owner.cacheLock) { |
| 625 | Account[] ownerAccounts = getAccounts(UserHandle.USER_OWNER); |
| 626 | for (Account acc : ownerAccounts) { |
| 627 | if (acc.equals(account)) { |
| 628 | mAuthenticator.addAccountFromCredentials(this, account, result); |
| 629 | break; |
| 630 | } |
| 631 | } |
| 632 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | public void onResult(Bundle result) { |
| 636 | if (result != null) { |
| 637 | if (result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)) { |
| 638 | // TODO: Anything? |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 639 | } else { |
| 640 | // TODO: Show error notification |
| 641 | // TODO: Should we remove the shadow account to avoid retries? |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 642 | } |
| 643 | return; |
| 644 | } else { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 645 | super.onResult(result); |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | public void onError(int errorCode, String errorMessage) { |
| 650 | super.onError(errorCode, errorMessage); |
| 651 | // TODO: Show error notification to user |
| 652 | // TODO: Should we remove the shadow account so that it doesn't keep trying? |
| 653 | } |
| 654 | |
| 655 | }.bind(); |
| 656 | } finally { |
| 657 | restoreCallingIdentity(id); |
| 658 | } |
| 659 | } |
| 660 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 661 | private boolean addAccountInternal(UserAccounts accounts, Account account, String password, |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 662 | Bundle extras, boolean restricted) { |
Fred Quintana | 743dfad | 2010-07-15 10:59:25 -0700 | [diff] [blame] | 663 | if (account == null) { |
| 664 | return false; |
| 665 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 666 | synchronized (accounts.cacheLock) { |
| 667 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 668 | db.beginTransaction(); |
| 669 | try { |
| 670 | long numMatches = DatabaseUtils.longForQuery(db, |
| 671 | "select count(*) from " + TABLE_ACCOUNTS |
| 672 | + " WHERE " + ACCOUNTS_NAME + "=? AND " + ACCOUNTS_TYPE+ "=?", |
| 673 | new String[]{account.name, account.type}); |
| 674 | if (numMatches > 0) { |
| 675 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 676 | + ", skipping since the account already exists"); |
| 677 | return false; |
| 678 | } |
| 679 | ContentValues values = new ContentValues(); |
| 680 | values.put(ACCOUNTS_NAME, account.name); |
| 681 | values.put(ACCOUNTS_TYPE, account.type); |
| 682 | values.put(ACCOUNTS_PASSWORD, password); |
| 683 | long accountId = db.insert(TABLE_ACCOUNTS, ACCOUNTS_NAME, values); |
| 684 | if (accountId < 0) { |
| 685 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 686 | + ", skipping the DB insert failed"); |
| 687 | return false; |
| 688 | } |
| 689 | if (extras != null) { |
| 690 | for (String key : extras.keySet()) { |
| 691 | final String value = extras.getString(key); |
| 692 | if (insertExtraLocked(db, accountId, key, value) < 0) { |
| 693 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 694 | + ", skipping since insertExtra failed for key " + key); |
| 695 | return false; |
| 696 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 697 | } |
| 698 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 699 | db.setTransactionSuccessful(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 700 | insertAccountIntoCacheLocked(accounts, account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 701 | } finally { |
| 702 | db.endTransaction(); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 703 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 704 | sendAccountsChangedBroadcast(accounts.userId); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 705 | } |
| 706 | if (accounts.userId == UserHandle.USER_OWNER) { |
| 707 | addAccountToLimitedUsers(account); |
| 708 | } |
| 709 | return true; |
| 710 | } |
| 711 | |
| 712 | /** |
| 713 | * Adds the account to all limited users as shared accounts. If the user is currently |
| 714 | * running, then clone the account too. |
| 715 | * @param account the account to share with limited users |
| 716 | */ |
| 717 | private void addAccountToLimitedUsers(Account account) { |
Mita Yun | f4c240e | 2013-04-01 21:12:43 -0700 | [diff] [blame] | 718 | List<UserInfo> users = getUserManager().getUsers(); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 719 | for (UserInfo user : users) { |
| 720 | if (user.isRestricted()) { |
| 721 | addSharedAccountAsUser(account, user.id); |
| 722 | try { |
| 723 | if (ActivityManagerNative.getDefault().isUserRunning(user.id, false)) { |
| 724 | mMessageHandler.sendMessage(mMessageHandler.obtainMessage( |
| 725 | MESSAGE_COPY_SHARED_ACCOUNT, UserHandle.USER_OWNER, user.id, |
| 726 | account)); |
| 727 | } |
| 728 | } catch (RemoteException re) { |
| 729 | // Shouldn't happen |
| 730 | } |
| 731 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 732 | } |
| 733 | } |
| 734 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 735 | private long insertExtraLocked(SQLiteDatabase db, long accountId, String key, String value) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 736 | ContentValues values = new ContentValues(); |
| 737 | values.put(EXTRAS_KEY, key); |
| 738 | values.put(EXTRAS_ACCOUNTS_ID, accountId); |
| 739 | values.put(EXTRAS_VALUE, value); |
| 740 | return db.insert(TABLE_EXTRAS, EXTRAS_KEY, values); |
| 741 | } |
| 742 | |
Fred Quintana | 3084a6f | 2010-01-14 18:02:03 -0800 | [diff] [blame] | 743 | public void hasFeatures(IAccountManagerResponse response, |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 744 | Account account, String[] features) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 745 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 746 | Log.v(TAG, "hasFeatures: " + account |
| 747 | + ", response " + response |
| 748 | + ", features " + stringArrayToString(features) |
| 749 | + ", caller's uid " + Binder.getCallingUid() |
| 750 | + ", pid " + Binder.getCallingPid()); |
| 751 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 752 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 753 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 754 | if (features == null) throw new IllegalArgumentException("features is null"); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 755 | checkReadAccountsPermission(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 756 | UserAccounts accounts = getUserAccountsForCaller(); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 757 | long identityToken = clearCallingIdentity(); |
| 758 | try { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 759 | new TestFeaturesSession(accounts, response, account, features).bind(); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 760 | } finally { |
| 761 | restoreCallingIdentity(identityToken); |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | private class TestFeaturesSession extends Session { |
| 766 | private final String[] mFeatures; |
| 767 | private final Account mAccount; |
| 768 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 769 | public TestFeaturesSession(UserAccounts accounts, IAccountManagerResponse response, |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 770 | Account account, String[] features) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 771 | super(accounts, response, account.type, false /* expectActivityLaunch */, |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 772 | true /* stripAuthTokenFromResult */); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 773 | mFeatures = features; |
| 774 | mAccount = account; |
| 775 | } |
| 776 | |
| 777 | public void run() throws RemoteException { |
| 778 | try { |
| 779 | mAuthenticator.hasFeatures(this, mAccount, mFeatures); |
| 780 | } catch (RemoteException e) { |
| 781 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "remote exception"); |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | public void onResult(Bundle result) { |
| 786 | IAccountManagerResponse response = getResponseAndClose(); |
| 787 | if (response != null) { |
| 788 | try { |
| 789 | if (result == null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 790 | response.onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, "null bundle"); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 791 | return; |
| 792 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 793 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 794 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 795 | + response); |
| 796 | } |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 797 | final Bundle newResult = new Bundle(); |
| 798 | newResult.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, |
| 799 | result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)); |
| 800 | response.onResult(newResult); |
| 801 | } catch (RemoteException e) { |
| 802 | // if the caller is dead then there is no one to care about remote exceptions |
| 803 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 804 | Log.v(TAG, "failure while notifying response", e); |
| 805 | } |
| 806 | } |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | protected String toDebugString(long now) { |
Fred Quintana | 3084a6f | 2010-01-14 18:02:03 -0800 | [diff] [blame] | 811 | return super.toDebugString(now) + ", hasFeatures" |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 812 | + ", " + mAccount |
| 813 | + ", " + (mFeatures != null ? TextUtils.join(",", mFeatures) : null); |
| 814 | } |
| 815 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 816 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 817 | public void removeAccount(IAccountManagerResponse response, Account account) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 818 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 819 | Log.v(TAG, "removeAccount: " + account |
| 820 | + ", response " + response |
| 821 | + ", caller's uid " + Binder.getCallingUid() |
| 822 | + ", pid " + Binder.getCallingPid()); |
| 823 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 824 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 825 | if (account == null) throw new IllegalArgumentException("account is null"); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 826 | checkManageAccountsPermission(); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 827 | UserHandle user = Binder.getCallingUserHandle(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 828 | UserAccounts accounts = getUserAccountsForCaller(); |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 829 | if (!canUserModifyAccounts(Binder.getCallingUid())) { |
| 830 | try { |
| 831 | response.onError(AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION, |
| 832 | "User cannot modify accounts"); |
| 833 | } catch (RemoteException re) { |
| 834 | } |
| 835 | } |
| 836 | |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 837 | long identityToken = clearCallingIdentity(); |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 838 | |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 839 | cancelNotification(getSigninRequiredNotificationId(accounts, account), user); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 840 | synchronized(accounts.credentialsPermissionNotificationIds) { |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 841 | for (Pair<Pair<Account, String>, Integer> pair: |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 842 | accounts.credentialsPermissionNotificationIds.keySet()) { |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 843 | if (account.equals(pair.first.first)) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 844 | int id = accounts.credentialsPermissionNotificationIds.get(pair); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 845 | cancelNotification(id, user); |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 846 | } |
| 847 | } |
| 848 | } |
| 849 | |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 850 | try { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 851 | new RemoveAccountSession(accounts, response, account).bind(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 852 | } finally { |
| 853 | restoreCallingIdentity(identityToken); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 854 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 855 | } |
| 856 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 857 | private class RemoveAccountSession extends Session { |
| 858 | final Account mAccount; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 859 | public RemoveAccountSession(UserAccounts accounts, IAccountManagerResponse response, |
| 860 | Account account) { |
| 861 | super(accounts, response, account.type, false /* expectActivityLaunch */, |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 862 | true /* stripAuthTokenFromResult */); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 863 | mAccount = account; |
| 864 | } |
| 865 | |
| 866 | protected String toDebugString(long now) { |
| 867 | return super.toDebugString(now) + ", removeAccount" |
| 868 | + ", account " + mAccount; |
| 869 | } |
| 870 | |
| 871 | public void run() throws RemoteException { |
| 872 | mAuthenticator.getAccountRemovalAllowed(this, mAccount); |
| 873 | } |
| 874 | |
| 875 | public void onResult(Bundle result) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 876 | if (result != null && result.containsKey(AccountManager.KEY_BOOLEAN_RESULT) |
| 877 | && !result.containsKey(AccountManager.KEY_INTENT)) { |
| 878 | final boolean removalAllowed = result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 879 | if (removalAllowed) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 880 | removeAccountInternal(mAccounts, mAccount); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 881 | } |
| 882 | IAccountManagerResponse response = getResponseAndClose(); |
| 883 | if (response != null) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 884 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 885 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 886 | + response); |
| 887 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 888 | Bundle result2 = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 889 | result2.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, removalAllowed); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 890 | try { |
| 891 | response.onResult(result2); |
| 892 | } catch (RemoteException e) { |
| 893 | // ignore |
| 894 | } |
| 895 | } |
| 896 | } |
| 897 | super.onResult(result); |
| 898 | } |
| 899 | } |
| 900 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 901 | /* For testing */ |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 902 | protected void removeAccountInternal(Account account) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 903 | removeAccountInternal(getUserAccountsForCaller(), account); |
| 904 | } |
| 905 | |
| 906 | private void removeAccountInternal(UserAccounts accounts, Account account) { |
| 907 | synchronized (accounts.cacheLock) { |
| 908 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 909 | db.delete(TABLE_ACCOUNTS, ACCOUNTS_NAME + "=? AND " + ACCOUNTS_TYPE+ "=?", |
| 910 | new String[]{account.name, account.type}); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 911 | removeAccountFromCacheLocked(accounts, account); |
| 912 | sendAccountsChangedBroadcast(accounts.userId); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 913 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 914 | if (accounts.userId == UserHandle.USER_OWNER) { |
| 915 | // Owner's account was removed, remove from any users that are sharing |
| 916 | // this account. |
| 917 | long id = Binder.clearCallingIdentity(); |
| 918 | try { |
| 919 | List<UserInfo> users = mUserManager.getUsers(true); |
| 920 | for (UserInfo user : users) { |
| 921 | if (!user.isPrimary() && user.isRestricted()) { |
| 922 | removeSharedAccountAsUser(account, user.id); |
| 923 | } |
| 924 | } |
| 925 | } finally { |
| 926 | Binder.restoreCallingIdentity(id); |
| 927 | } |
| 928 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 929 | } |
| 930 | |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 931 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 932 | public void invalidateAuthToken(String accountType, String authToken) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 933 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 934 | Log.v(TAG, "invalidateAuthToken: accountType " + accountType |
| 935 | + ", caller's uid " + Binder.getCallingUid() |
| 936 | + ", pid " + Binder.getCallingPid()); |
| 937 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 938 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
| 939 | if (authToken == null) throw new IllegalArgumentException("authToken is null"); |
Fred Quintana | b38eb14 | 2010-02-24 13:40:54 -0800 | [diff] [blame] | 940 | checkManageAccountsOrUseCredentialsPermissions(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 941 | UserAccounts accounts = getUserAccountsForCaller(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 942 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 943 | try { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 944 | synchronized (accounts.cacheLock) { |
| 945 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 946 | db.beginTransaction(); |
| 947 | try { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 948 | invalidateAuthTokenLocked(accounts, db, accountType, authToken); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 949 | db.setTransactionSuccessful(); |
| 950 | } finally { |
| 951 | db.endTransaction(); |
| 952 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 953 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 954 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 955 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 956 | } |
| 957 | } |
| 958 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 959 | private void invalidateAuthTokenLocked(UserAccounts accounts, SQLiteDatabase db, |
| 960 | String accountType, String authToken) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 961 | if (authToken == null || accountType == null) { |
| 962 | return; |
| 963 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 964 | Cursor cursor = db.rawQuery( |
| 965 | "SELECT " + TABLE_AUTHTOKENS + "." + AUTHTOKENS_ID |
| 966 | + ", " + TABLE_ACCOUNTS + "." + ACCOUNTS_NAME |
| 967 | + ", " + TABLE_AUTHTOKENS + "." + AUTHTOKENS_TYPE |
| 968 | + " FROM " + TABLE_ACCOUNTS |
| 969 | + " JOIN " + TABLE_AUTHTOKENS |
| 970 | + " ON " + TABLE_ACCOUNTS + "." + ACCOUNTS_ID |
| 971 | + " = " + AUTHTOKENS_ACCOUNTS_ID |
| 972 | + " WHERE " + AUTHTOKENS_AUTHTOKEN + " = ? AND " |
| 973 | + TABLE_ACCOUNTS + "." + ACCOUNTS_TYPE + " = ?", |
| 974 | new String[]{authToken, accountType}); |
| 975 | try { |
| 976 | while (cursor.moveToNext()) { |
| 977 | long authTokenId = cursor.getLong(0); |
| 978 | String accountName = cursor.getString(1); |
| 979 | String authTokenType = cursor.getString(2); |
| 980 | db.delete(TABLE_AUTHTOKENS, AUTHTOKENS_ID + "=" + authTokenId, null); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 981 | writeAuthTokenIntoCacheLocked(accounts, db, new Account(accountName, accountType), |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 982 | authTokenType, null); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 983 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 984 | } finally { |
| 985 | cursor.close(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 986 | } |
| 987 | } |
| 988 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 989 | private boolean saveAuthTokenToDatabase(UserAccounts accounts, Account account, String type, |
| 990 | String authToken) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 991 | if (account == null || type == null) { |
| 992 | return false; |
| 993 | } |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 994 | cancelNotification(getSigninRequiredNotificationId(accounts, account), |
| 995 | new UserHandle(accounts.userId)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 996 | synchronized (accounts.cacheLock) { |
| 997 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 998 | db.beginTransaction(); |
| 999 | try { |
| 1000 | long accountId = getAccountIdLocked(db, account); |
| 1001 | if (accountId < 0) { |
| 1002 | return false; |
| 1003 | } |
| 1004 | db.delete(TABLE_AUTHTOKENS, |
| 1005 | AUTHTOKENS_ACCOUNTS_ID + "=" + accountId + " AND " + AUTHTOKENS_TYPE + "=?", |
| 1006 | new String[]{type}); |
| 1007 | ContentValues values = new ContentValues(); |
| 1008 | values.put(AUTHTOKENS_ACCOUNTS_ID, accountId); |
| 1009 | values.put(AUTHTOKENS_TYPE, type); |
| 1010 | values.put(AUTHTOKENS_AUTHTOKEN, authToken); |
| 1011 | if (db.insert(TABLE_AUTHTOKENS, AUTHTOKENS_AUTHTOKEN, values) >= 0) { |
| 1012 | db.setTransactionSuccessful(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1013 | writeAuthTokenIntoCacheLocked(accounts, db, account, type, authToken); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1014 | return true; |
| 1015 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1016 | return false; |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1017 | } finally { |
| 1018 | db.endTransaction(); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1019 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1020 | } |
| 1021 | } |
| 1022 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1023 | public String peekAuthToken(Account account, String authTokenType) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1024 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1025 | Log.v(TAG, "peekAuthToken: " + account |
| 1026 | + ", authTokenType " + authTokenType |
| 1027 | + ", caller's uid " + Binder.getCallingUid() |
| 1028 | + ", pid " + Binder.getCallingPid()); |
| 1029 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1030 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 1031 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1032 | checkAuthenticateAccountsPermission(account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1033 | UserAccounts accounts = getUserAccountsForCaller(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1034 | long identityToken = clearCallingIdentity(); |
| 1035 | try { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1036 | return readAuthTokenInternal(accounts, account, authTokenType); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1037 | } finally { |
| 1038 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1039 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1040 | } |
| 1041 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1042 | public void setAuthToken(Account account, String authTokenType, String authToken) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1043 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1044 | Log.v(TAG, "setAuthToken: " + account |
| 1045 | + ", authTokenType " + authTokenType |
| 1046 | + ", caller's uid " + Binder.getCallingUid() |
| 1047 | + ", pid " + Binder.getCallingPid()); |
| 1048 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1049 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 1050 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1051 | checkAuthenticateAccountsPermission(account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1052 | UserAccounts accounts = getUserAccountsForCaller(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1053 | long identityToken = clearCallingIdentity(); |
| 1054 | try { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1055 | saveAuthTokenToDatabase(accounts, account, authTokenType, authToken); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1056 | } finally { |
| 1057 | restoreCallingIdentity(identityToken); |
| 1058 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1061 | public void setPassword(Account account, String password) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1062 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1063 | Log.v(TAG, "setAuthToken: " + account |
| 1064 | + ", caller's uid " + Binder.getCallingUid() |
| 1065 | + ", pid " + Binder.getCallingPid()); |
| 1066 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1067 | if (account == null) throw new IllegalArgumentException("account is null"); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1068 | checkAuthenticateAccountsPermission(account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1069 | UserAccounts accounts = getUserAccountsForCaller(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1070 | long identityToken = clearCallingIdentity(); |
| 1071 | try { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1072 | setPasswordInternal(accounts, account, password); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1073 | } finally { |
| 1074 | restoreCallingIdentity(identityToken); |
| 1075 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1076 | } |
| 1077 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1078 | private void setPasswordInternal(UserAccounts accounts, Account account, String password) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1079 | if (account == null) { |
| 1080 | return; |
| 1081 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1082 | synchronized (accounts.cacheLock) { |
| 1083 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1084 | db.beginTransaction(); |
| 1085 | try { |
| 1086 | final ContentValues values = new ContentValues(); |
| 1087 | values.put(ACCOUNTS_PASSWORD, password); |
| 1088 | final long accountId = getAccountIdLocked(db, account); |
| 1089 | if (accountId >= 0) { |
| 1090 | final String[] argsAccountId = {String.valueOf(accountId)}; |
| 1091 | db.update(TABLE_ACCOUNTS, values, ACCOUNTS_ID + "=?", argsAccountId); |
| 1092 | db.delete(TABLE_AUTHTOKENS, AUTHTOKENS_ACCOUNTS_ID + "=?", argsAccountId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1093 | accounts.authTokenCache.remove(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1094 | db.setTransactionSuccessful(); |
Costin Manolache | f5ffe89 | 2011-01-19 09:35:32 -0800 | [diff] [blame] | 1095 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1096 | } finally { |
| 1097 | db.endTransaction(); |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 1098 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1099 | sendAccountsChangedBroadcast(accounts.userId); |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 1100 | } |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 1101 | } |
| 1102 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1103 | private void sendAccountsChangedBroadcast(int userId) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1104 | Log.i(TAG, "the accounts changed, sending broadcast of " |
| 1105 | + ACCOUNTS_CHANGED_INTENT.getAction()); |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 1106 | mContext.sendBroadcastAsUser(ACCOUNTS_CHANGED_INTENT, new UserHandle(userId)); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1107 | } |
| 1108 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1109 | public void clearPassword(Account account) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1110 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1111 | Log.v(TAG, "clearPassword: " + account |
| 1112 | + ", caller's uid " + Binder.getCallingUid() |
| 1113 | + ", pid " + Binder.getCallingPid()); |
| 1114 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1115 | if (account == null) throw new IllegalArgumentException("account is null"); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1116 | checkManageAccountsPermission(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1117 | UserAccounts accounts = getUserAccountsForCaller(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1118 | long identityToken = clearCallingIdentity(); |
| 1119 | try { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1120 | setPasswordInternal(accounts, account, null); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1121 | } finally { |
| 1122 | restoreCallingIdentity(identityToken); |
| 1123 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1124 | } |
| 1125 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1126 | public void setUserData(Account account, String key, String value) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1127 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1128 | Log.v(TAG, "setUserData: " + account |
| 1129 | + ", key " + key |
| 1130 | + ", caller's uid " + Binder.getCallingUid() |
| 1131 | + ", pid " + Binder.getCallingPid()); |
| 1132 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1133 | if (key == null) throw new IllegalArgumentException("key is null"); |
| 1134 | if (account == null) throw new IllegalArgumentException("account is null"); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1135 | checkAuthenticateAccountsPermission(account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1136 | UserAccounts accounts = getUserAccountsForCaller(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1137 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1138 | try { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1139 | setUserdataInternal(accounts, account, key, value); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1140 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1141 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1142 | } |
| 1143 | } |
| 1144 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1145 | private void setUserdataInternal(UserAccounts accounts, Account account, String key, |
| 1146 | String value) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1147 | if (account == null || key == null) { |
| 1148 | return; |
| 1149 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1150 | synchronized (accounts.cacheLock) { |
| 1151 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1152 | db.beginTransaction(); |
| 1153 | try { |
| 1154 | long accountId = getAccountIdLocked(db, account); |
| 1155 | if (accountId < 0) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1156 | return; |
| 1157 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1158 | long extrasId = getExtrasIdLocked(db, accountId, key); |
| 1159 | if (extrasId < 0 ) { |
| 1160 | extrasId = insertExtraLocked(db, accountId, key, value); |
| 1161 | if (extrasId < 0) { |
| 1162 | return; |
| 1163 | } |
| 1164 | } else { |
| 1165 | ContentValues values = new ContentValues(); |
| 1166 | values.put(EXTRAS_VALUE, value); |
| 1167 | if (1 != db.update(TABLE_EXTRAS, values, EXTRAS_ID + "=" + extrasId, null)) { |
| 1168 | return; |
| 1169 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1170 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1171 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1172 | writeUserDataIntoCacheLocked(accounts, db, account, key, value); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1173 | db.setTransactionSuccessful(); |
| 1174 | } finally { |
| 1175 | db.endTransaction(); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1176 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1177 | } |
| 1178 | } |
| 1179 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1180 | private void onResult(IAccountManagerResponse response, Bundle result) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1181 | if (result == null) { |
| 1182 | Log.e(TAG, "the result is unexpectedly null", new Exception()); |
| 1183 | } |
| 1184 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1185 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 1186 | + response); |
| 1187 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1188 | try { |
| 1189 | response.onResult(result); |
| 1190 | } catch (RemoteException e) { |
| 1191 | // if the caller is dead then there is no one to care about remote |
| 1192 | // exceptions |
| 1193 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1194 | Log.v(TAG, "failure while notifying response", e); |
| 1195 | } |
| 1196 | } |
| 1197 | } |
| 1198 | |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 1199 | public void getAuthTokenLabel(IAccountManagerResponse response, final String accountType, |
| 1200 | final String authTokenType) |
| 1201 | throws RemoteException { |
| 1202 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 1203 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
| 1204 | |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 1205 | final int callingUid = getCallingUid(); |
| 1206 | clearCallingIdentity(); |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 1207 | if (callingUid != Process.SYSTEM_UID) { |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 1208 | throw new SecurityException("can only call from system"); |
| 1209 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1210 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(callingUid)); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 1211 | long identityToken = clearCallingIdentity(); |
| 1212 | try { |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 1213 | new Session(accounts, response, accountType, false, |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 1214 | false /* stripAuthTokenFromResult */) { |
| 1215 | protected String toDebugString(long now) { |
| 1216 | return super.toDebugString(now) + ", getAuthTokenLabel" |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 1217 | + ", " + accountType |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 1218 | + ", authTokenType " + authTokenType; |
| 1219 | } |
| 1220 | |
| 1221 | public void run() throws RemoteException { |
| 1222 | mAuthenticator.getAuthTokenLabel(this, authTokenType); |
| 1223 | } |
| 1224 | |
| 1225 | public void onResult(Bundle result) { |
| 1226 | if (result != null) { |
| 1227 | String label = result.getString(AccountManager.KEY_AUTH_TOKEN_LABEL); |
| 1228 | Bundle bundle = new Bundle(); |
| 1229 | bundle.putString(AccountManager.KEY_AUTH_TOKEN_LABEL, label); |
| 1230 | super.onResult(bundle); |
| 1231 | return; |
| 1232 | } else { |
| 1233 | super.onResult(result); |
| 1234 | } |
| 1235 | } |
| 1236 | }.bind(); |
| 1237 | } finally { |
| 1238 | restoreCallingIdentity(identityToken); |
| 1239 | } |
| 1240 | } |
| 1241 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1242 | public void getAuthToken(IAccountManagerResponse response, final Account account, |
| 1243 | final String authTokenType, final boolean notifyOnAuthFailure, |
Costin Manolache | c6684f9 | 2011-01-14 11:25:39 -0800 | [diff] [blame] | 1244 | final boolean expectActivityLaunch, Bundle loginOptionsIn) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1245 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1246 | Log.v(TAG, "getAuthToken: " + account |
| 1247 | + ", response " + response |
| 1248 | + ", authTokenType " + authTokenType |
| 1249 | + ", notifyOnAuthFailure " + notifyOnAuthFailure |
| 1250 | + ", expectActivityLaunch " + expectActivityLaunch |
| 1251 | + ", caller's uid " + Binder.getCallingUid() |
| 1252 | + ", pid " + Binder.getCallingPid()); |
| 1253 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1254 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 1255 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 1256 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1257 | checkBinderPermission(Manifest.permission.USE_CREDENTIALS); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1258 | final UserAccounts accounts = getUserAccountsForCaller(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1259 | final RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> authenticatorInfo; |
| 1260 | authenticatorInfo = mAuthenticatorCache.getServiceInfo( |
| 1261 | AuthenticatorDescription.newKey(account.type), accounts.userId); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 1262 | final boolean customTokens = |
| 1263 | authenticatorInfo != null && authenticatorInfo.type.customTokens; |
| 1264 | |
Amith Yamasani | d20ea2f | 2013-05-08 12:57:01 -0700 | [diff] [blame] | 1265 | // Check to see that the app is authorized to access the account, in case it's a |
| 1266 | // restricted account. |
| 1267 | if (!ArrayUtils.contains(getAccounts((String) null), account)) { |
| 1268 | throw new IllegalArgumentException("no such account"); |
| 1269 | } |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 1270 | // skip the check if customTokens |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 1271 | final int callerUid = Binder.getCallingUid(); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 1272 | final boolean permissionGranted = customTokens || |
| 1273 | permissionIsGranted(account, authTokenType, callerUid); |
| 1274 | |
Costin Manolache | c6684f9 | 2011-01-14 11:25:39 -0800 | [diff] [blame] | 1275 | final Bundle loginOptions = (loginOptionsIn == null) ? new Bundle() : |
| 1276 | loginOptionsIn; |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 1277 | // let authenticator know the identity of the caller |
| 1278 | loginOptions.putInt(AccountManager.KEY_CALLER_UID, callerUid); |
| 1279 | loginOptions.putInt(AccountManager.KEY_CALLER_PID, Binder.getCallingPid()); |
| 1280 | if (notifyOnAuthFailure) { |
| 1281 | loginOptions.putBoolean(AccountManager.KEY_NOTIFY_ON_FAILURE, true); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 1282 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1283 | |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1284 | long identityToken = clearCallingIdentity(); |
| 1285 | try { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1286 | // if the caller has permission, do the peek. otherwise go the more expensive |
| 1287 | // route of starting a Session |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 1288 | if (!customTokens && permissionGranted) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1289 | String authToken = readAuthTokenInternal(accounts, account, authTokenType); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1290 | if (authToken != null) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1291 | Bundle result = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1292 | result.putString(AccountManager.KEY_AUTHTOKEN, authToken); |
| 1293 | result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); |
| 1294 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1295 | onResult(response, result); |
| 1296 | return; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1297 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1298 | } |
| 1299 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1300 | new Session(accounts, response, account.type, expectActivityLaunch, |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 1301 | false /* stripAuthTokenFromResult */) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1302 | protected String toDebugString(long now) { |
| 1303 | if (loginOptions != null) loginOptions.keySet(); |
| 1304 | return super.toDebugString(now) + ", getAuthToken" |
| 1305 | + ", " + account |
| 1306 | + ", authTokenType " + authTokenType |
| 1307 | + ", loginOptions " + loginOptions |
| 1308 | + ", notifyOnAuthFailure " + notifyOnAuthFailure; |
| 1309 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1310 | |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1311 | public void run() throws RemoteException { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1312 | // If the caller doesn't have permission then create and return the |
| 1313 | // "grant permission" intent instead of the "getAuthToken" intent. |
| 1314 | if (!permissionGranted) { |
| 1315 | mAuthenticator.getAuthTokenLabel(this, authTokenType); |
| 1316 | } else { |
| 1317 | mAuthenticator.getAuthToken(this, account, authTokenType, loginOptions); |
| 1318 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | public void onResult(Bundle result) { |
| 1322 | if (result != null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1323 | if (result.containsKey(AccountManager.KEY_AUTH_TOKEN_LABEL)) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1324 | Intent intent = newGrantCredentialsPermissionIntent(account, callerUid, |
| 1325 | new AccountAuthenticatorResponse(this), |
| 1326 | authTokenType, |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1327 | result.getString(AccountManager.KEY_AUTH_TOKEN_LABEL)); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1328 | Bundle bundle = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1329 | bundle.putParcelable(AccountManager.KEY_INTENT, intent); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1330 | onResult(bundle); |
| 1331 | return; |
| 1332 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1333 | String authToken = result.getString(AccountManager.KEY_AUTHTOKEN); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1334 | if (authToken != null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1335 | String name = result.getString(AccountManager.KEY_ACCOUNT_NAME); |
| 1336 | String type = result.getString(AccountManager.KEY_ACCOUNT_TYPE); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1337 | if (TextUtils.isEmpty(type) || TextUtils.isEmpty(name)) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1338 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1339 | "the type and name should not be empty"); |
| 1340 | return; |
| 1341 | } |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 1342 | if (!customTokens) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1343 | saveAuthTokenToDatabase(mAccounts, new Account(name, type), |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 1344 | authTokenType, authToken); |
| 1345 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1346 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1347 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1348 | Intent intent = result.getParcelable(AccountManager.KEY_INTENT); |
Costin Manolache | d606045 | 2011-01-24 16:11:36 -0800 | [diff] [blame] | 1349 | if (intent != null && notifyOnAuthFailure && !customTokens) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1350 | doNotification(mAccounts, |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1351 | account, result.getString(AccountManager.KEY_AUTH_FAILED_MESSAGE), |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1352 | intent, accounts.userId); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1353 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1354 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1355 | super.onResult(result); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1356 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1357 | }.bind(); |
| 1358 | } finally { |
| 1359 | restoreCallingIdentity(identityToken); |
| 1360 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1361 | } |
| 1362 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1363 | private void createNoCredentialsPermissionNotification(Account account, Intent intent, |
| 1364 | int userId) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1365 | int uid = intent.getIntExtra( |
| 1366 | GrantCredentialsPermissionActivity.EXTRAS_REQUESTING_UID, -1); |
| 1367 | String authTokenType = intent.getStringExtra( |
| 1368 | GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_TYPE); |
| 1369 | String authTokenLabel = intent.getStringExtra( |
| 1370 | GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_LABEL); |
| 1371 | |
| 1372 | Notification n = new Notification(android.R.drawable.stat_sys_warning, null, |
| 1373 | 0 /* when */); |
Eric Fischer | ee452ee | 2009-08-31 17:58:06 -0700 | [diff] [blame] | 1374 | final String titleAndSubtitle = |
| 1375 | mContext.getString(R.string.permission_request_notification_with_subtitle, |
| 1376 | account.name); |
| 1377 | final int index = titleAndSubtitle.indexOf('\n'); |
Costin Manolache | 85e7279 | 2011-10-07 09:42:49 -0700 | [diff] [blame] | 1378 | String title = titleAndSubtitle; |
| 1379 | String subtitle = ""; |
| 1380 | if (index > 0) { |
| 1381 | title = titleAndSubtitle.substring(0, index); |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 1382 | subtitle = titleAndSubtitle.substring(index + 1); |
Costin Manolache | 85e7279 | 2011-10-07 09:42:49 -0700 | [diff] [blame] | 1383 | } |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 1384 | UserHandle user = new UserHandle(userId); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1385 | n.setLatestEventInfo(mContext, title, subtitle, |
| 1386 | PendingIntent.getActivityAsUser(mContext, 0, intent, |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 1387 | PendingIntent.FLAG_CANCEL_CURRENT, null, user)); |
| 1388 | installNotification(getCredentialPermissionNotificationId( |
| 1389 | account, authTokenType, uid), n, user); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1390 | } |
| 1391 | |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 1392 | private Intent newGrantCredentialsPermissionIntent(Account account, int uid, |
| 1393 | AccountAuthenticatorResponse response, String authTokenType, String authTokenLabel) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1394 | |
| 1395 | Intent intent = new Intent(mContext, GrantCredentialsPermissionActivity.class); |
Brian Carlstrom | 46703b0 | 2011-04-06 15:41:29 -0700 | [diff] [blame] | 1396 | // See FLAG_ACTIVITY_NEW_TASK docs for limitations and benefits of the flag. |
Costin Manolache | 9ec1736 | 2011-01-17 12:12:37 -0800 | [diff] [blame] | 1397 | // Since it was set in Eclair+ we can't change it without breaking apps using |
| 1398 | // the intent from a non-Activity context. |
| 1399 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1400 | intent.addCategory( |
| 1401 | String.valueOf(getCredentialPermissionNotificationId(account, authTokenType, uid))); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 1402 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1403 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_ACCOUNT, account); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1404 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_TYPE, authTokenType); |
| 1405 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_RESPONSE, response); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1406 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_REQUESTING_UID, uid); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 1407 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1408 | return intent; |
| 1409 | } |
| 1410 | |
| 1411 | private Integer getCredentialPermissionNotificationId(Account account, String authTokenType, |
| 1412 | int uid) { |
| 1413 | Integer id; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1414 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1415 | synchronized (accounts.credentialsPermissionNotificationIds) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1416 | final Pair<Pair<Account, String>, Integer> key = |
| 1417 | new Pair<Pair<Account, String>, Integer>( |
| 1418 | new Pair<Account, String>(account, authTokenType), uid); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1419 | id = accounts.credentialsPermissionNotificationIds.get(key); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1420 | if (id == null) { |
| 1421 | id = mNotificationIds.incrementAndGet(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1422 | accounts.credentialsPermissionNotificationIds.put(key, id); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1423 | } |
| 1424 | } |
| 1425 | return id; |
| 1426 | } |
| 1427 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1428 | private Integer getSigninRequiredNotificationId(UserAccounts accounts, Account account) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1429 | Integer id; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1430 | synchronized (accounts.signinRequiredNotificationIds) { |
| 1431 | id = accounts.signinRequiredNotificationIds.get(account); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1432 | if (id == null) { |
| 1433 | id = mNotificationIds.incrementAndGet(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1434 | accounts.signinRequiredNotificationIds.put(account, id); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1435 | } |
| 1436 | } |
| 1437 | return id; |
| 1438 | } |
| 1439 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 1440 | public void addAccount(final IAccountManagerResponse response, final String accountType, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1441 | final String authTokenType, final String[] requiredFeatures, |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 1442 | final boolean expectActivityLaunch, final Bundle optionsIn) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1443 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1444 | Log.v(TAG, "addAccount: accountType " + accountType |
| 1445 | + ", response " + response |
| 1446 | + ", authTokenType " + authTokenType |
| 1447 | + ", requiredFeatures " + stringArrayToString(requiredFeatures) |
| 1448 | + ", expectActivityLaunch " + expectActivityLaunch |
| 1449 | + ", caller's uid " + Binder.getCallingUid() |
| 1450 | + ", pid " + Binder.getCallingPid()); |
| 1451 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1452 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 1453 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1454 | checkManageAccountsPermission(); |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 1455 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 1456 | // Is user disallowed from modifying accounts? |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 1457 | if (!canUserModifyAccounts(Binder.getCallingUid())) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1458 | try { |
| 1459 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 1460 | "User is not allowed to add an account!"); |
| 1461 | } catch (RemoteException re) { |
| 1462 | } |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 1463 | Intent cantAddAccount = new Intent(mContext, CantAddAccountActivity.class); |
| 1464 | cantAddAccount.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 1465 | long identityToken = clearCallingIdentity(); |
| 1466 | try { |
| 1467 | mContext.startActivityAsUser(cantAddAccount, UserHandle.CURRENT); |
| 1468 | } finally { |
| 1469 | restoreCallingIdentity(identityToken); |
| 1470 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1471 | return; |
| 1472 | } |
| 1473 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1474 | UserAccounts accounts = getUserAccountsForCaller(); |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 1475 | final int pid = Binder.getCallingPid(); |
| 1476 | final int uid = Binder.getCallingUid(); |
| 1477 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 1478 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 1479 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 1480 | |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1481 | long identityToken = clearCallingIdentity(); |
| 1482 | try { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1483 | new Session(accounts, response, accountType, expectActivityLaunch, |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 1484 | true /* stripAuthTokenFromResult */) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1485 | public void run() throws RemoteException { |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 1486 | mAuthenticator.addAccount(this, mAccountType, authTokenType, requiredFeatures, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1487 | options); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1488 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1489 | |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1490 | protected String toDebugString(long now) { |
| 1491 | return super.toDebugString(now) + ", addAccount" |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1492 | + ", accountType " + accountType |
| 1493 | + ", requiredFeatures " |
| 1494 | + (requiredFeatures != null |
| 1495 | ? TextUtils.join(",", requiredFeatures) |
| 1496 | : null); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1497 | } |
| 1498 | }.bind(); |
| 1499 | } finally { |
| 1500 | restoreCallingIdentity(identityToken); |
| 1501 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1502 | } |
| 1503 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 1504 | @Override |
| 1505 | public void confirmCredentialsAsUser(IAccountManagerResponse response, |
| 1506 | final Account account, final Bundle options, final boolean expectActivityLaunch, |
| 1507 | int userId) { |
| 1508 | // Only allow the system process to read accounts of other users |
| 1509 | if (userId != UserHandle.getCallingUserId() |
Amith Yamasani | bb49e85 | 2013-03-30 19:20:18 -0700 | [diff] [blame] | 1510 | && Binder.getCallingUid() != Process.myUid() |
Jim Miller | 464f530 | 2013-02-27 18:33:25 -0800 | [diff] [blame] | 1511 | && mContext.checkCallingOrSelfPermission( |
| 1512 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 1513 | != PackageManager.PERMISSION_GRANTED) { |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 1514 | throw new SecurityException("User " + UserHandle.getCallingUserId() |
| 1515 | + " trying to confirm account credentials for " + userId); |
| 1516 | } |
| 1517 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1518 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1519 | Log.v(TAG, "confirmCredentials: " + account |
| 1520 | + ", response " + response |
| 1521 | + ", expectActivityLaunch " + expectActivityLaunch |
| 1522 | + ", caller's uid " + Binder.getCallingUid() |
| 1523 | + ", pid " + Binder.getCallingPid()); |
| 1524 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1525 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 1526 | if (account == null) throw new IllegalArgumentException("account is null"); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1527 | checkManageAccountsPermission(); |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 1528 | UserAccounts accounts = getUserAccounts(userId); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1529 | long identityToken = clearCallingIdentity(); |
| 1530 | try { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1531 | new Session(accounts, response, account.type, expectActivityLaunch, |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 1532 | true /* stripAuthTokenFromResult */) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1533 | public void run() throws RemoteException { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1534 | mAuthenticator.confirmCredentials(this, account, options); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1535 | } |
| 1536 | protected String toDebugString(long now) { |
| 1537 | return super.toDebugString(now) + ", confirmCredentials" |
| 1538 | + ", " + account; |
| 1539 | } |
| 1540 | }.bind(); |
| 1541 | } finally { |
| 1542 | restoreCallingIdentity(identityToken); |
| 1543 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1544 | } |
| 1545 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1546 | public void updateCredentials(IAccountManagerResponse response, final Account account, |
| 1547 | final String authTokenType, final boolean expectActivityLaunch, |
| 1548 | final Bundle loginOptions) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1549 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1550 | Log.v(TAG, "updateCredentials: " + account |
| 1551 | + ", response " + response |
| 1552 | + ", authTokenType " + authTokenType |
| 1553 | + ", expectActivityLaunch " + expectActivityLaunch |
| 1554 | + ", caller's uid " + Binder.getCallingUid() |
| 1555 | + ", pid " + Binder.getCallingPid()); |
| 1556 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1557 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 1558 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 1559 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1560 | checkManageAccountsPermission(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1561 | UserAccounts accounts = getUserAccountsForCaller(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1562 | long identityToken = clearCallingIdentity(); |
| 1563 | try { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1564 | new Session(accounts, response, account.type, expectActivityLaunch, |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 1565 | true /* stripAuthTokenFromResult */) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1566 | public void run() throws RemoteException { |
| 1567 | mAuthenticator.updateCredentials(this, account, authTokenType, loginOptions); |
| 1568 | } |
| 1569 | protected String toDebugString(long now) { |
| 1570 | if (loginOptions != null) loginOptions.keySet(); |
| 1571 | return super.toDebugString(now) + ", updateCredentials" |
| 1572 | + ", " + account |
| 1573 | + ", authTokenType " + authTokenType |
| 1574 | + ", loginOptions " + loginOptions; |
| 1575 | } |
| 1576 | }.bind(); |
| 1577 | } finally { |
| 1578 | restoreCallingIdentity(identityToken); |
| 1579 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1580 | } |
| 1581 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1582 | public void editProperties(IAccountManagerResponse response, final String accountType, |
| 1583 | final boolean expectActivityLaunch) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1584 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1585 | Log.v(TAG, "editProperties: accountType " + accountType |
| 1586 | + ", response " + response |
| 1587 | + ", expectActivityLaunch " + expectActivityLaunch |
| 1588 | + ", caller's uid " + Binder.getCallingUid() |
| 1589 | + ", pid " + Binder.getCallingPid()); |
| 1590 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1591 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 1592 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1593 | checkManageAccountsPermission(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1594 | UserAccounts accounts = getUserAccountsForCaller(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1595 | long identityToken = clearCallingIdentity(); |
| 1596 | try { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1597 | new Session(accounts, response, accountType, expectActivityLaunch, |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 1598 | true /* stripAuthTokenFromResult */) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1599 | public void run() throws RemoteException { |
| 1600 | mAuthenticator.editProperties(this, mAccountType); |
| 1601 | } |
| 1602 | protected String toDebugString(long now) { |
| 1603 | return super.toDebugString(now) + ", editProperties" |
| 1604 | + ", accountType " + accountType; |
| 1605 | } |
| 1606 | }.bind(); |
| 1607 | } finally { |
| 1608 | restoreCallingIdentity(identityToken); |
| 1609 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1610 | } |
| 1611 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1612 | private class GetAccountsByTypeAndFeatureSession extends Session { |
| 1613 | private final String[] mFeatures; |
| 1614 | private volatile Account[] mAccountsOfType = null; |
| 1615 | private volatile ArrayList<Account> mAccountsWithFeatures = null; |
| 1616 | private volatile int mCurrentAccount = 0; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1617 | private int mCallingUid; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1618 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1619 | public GetAccountsByTypeAndFeatureSession(UserAccounts accounts, |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1620 | IAccountManagerResponse response, String type, String[] features, int callingUid) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1621 | super(accounts, response, type, false /* expectActivityLaunch */, |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 1622 | true /* stripAuthTokenFromResult */); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1623 | mCallingUid = callingUid; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1624 | mFeatures = features; |
| 1625 | } |
| 1626 | |
| 1627 | public void run() throws RemoteException { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1628 | synchronized (mAccounts.cacheLock) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 1629 | mAccountsOfType = getAccountsFromCacheLocked(mAccounts, mAccountType, mCallingUid, |
| 1630 | null); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1631 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1632 | // check whether each account matches the requested features |
| 1633 | mAccountsWithFeatures = new ArrayList<Account>(mAccountsOfType.length); |
| 1634 | mCurrentAccount = 0; |
| 1635 | |
| 1636 | checkAccount(); |
| 1637 | } |
| 1638 | |
| 1639 | public void checkAccount() { |
| 1640 | if (mCurrentAccount >= mAccountsOfType.length) { |
| 1641 | sendResult(); |
| 1642 | return; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1643 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1644 | |
Fred Quintana | 29e94b8 | 2010-03-10 12:11:51 -0800 | [diff] [blame] | 1645 | final IAccountAuthenticator accountAuthenticator = mAuthenticator; |
| 1646 | if (accountAuthenticator == null) { |
| 1647 | // It is possible that the authenticator has died, which is indicated by |
| 1648 | // mAuthenticator being set to null. If this happens then just abort. |
| 1649 | // There is no need to send back a result or error in this case since |
| 1650 | // that already happened when mAuthenticator was cleared. |
| 1651 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1652 | Log.v(TAG, "checkAccount: aborting session since we are no longer" |
| 1653 | + " connected to the authenticator, " + toDebugString()); |
| 1654 | } |
| 1655 | return; |
| 1656 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1657 | try { |
Fred Quintana | 29e94b8 | 2010-03-10 12:11:51 -0800 | [diff] [blame] | 1658 | accountAuthenticator.hasFeatures(this, mAccountsOfType[mCurrentAccount], mFeatures); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1659 | } catch (RemoteException e) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1660 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "remote exception"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1661 | } |
| 1662 | } |
| 1663 | |
| 1664 | public void onResult(Bundle result) { |
| 1665 | mNumResults++; |
| 1666 | if (result == null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1667 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, "null bundle"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1668 | return; |
| 1669 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1670 | if (result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)) { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1671 | mAccountsWithFeatures.add(mAccountsOfType[mCurrentAccount]); |
| 1672 | } |
| 1673 | mCurrentAccount++; |
| 1674 | checkAccount(); |
| 1675 | } |
| 1676 | |
| 1677 | public void sendResult() { |
| 1678 | IAccountManagerResponse response = getResponseAndClose(); |
| 1679 | if (response != null) { |
| 1680 | try { |
| 1681 | Account[] accounts = new Account[mAccountsWithFeatures.size()]; |
| 1682 | for (int i = 0; i < accounts.length; i++) { |
| 1683 | accounts[i] = mAccountsWithFeatures.get(i); |
| 1684 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1685 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1686 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 1687 | + response); |
| 1688 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1689 | Bundle result = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1690 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, accounts); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1691 | response.onResult(result); |
| 1692 | } catch (RemoteException e) { |
| 1693 | // if the caller is dead then there is no one to care about remote exceptions |
| 1694 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1695 | Log.v(TAG, "failure while notifying response", e); |
| 1696 | } |
| 1697 | } |
| 1698 | } |
| 1699 | } |
| 1700 | |
| 1701 | |
| 1702 | protected String toDebugString(long now) { |
| 1703 | return super.toDebugString(now) + ", getAccountsByTypeAndFeatures" |
| 1704 | + ", " + (mFeatures != null ? TextUtils.join(",", mFeatures) : null); |
| 1705 | } |
| 1706 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1707 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1708 | /** |
| 1709 | * Returns the accounts for a specific user |
| 1710 | * @hide |
| 1711 | */ |
| 1712 | public Account[] getAccounts(int userId) { |
| 1713 | checkReadAccountsPermission(); |
| 1714 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1715 | int callingUid = Binder.getCallingUid(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1716 | long identityToken = clearCallingIdentity(); |
| 1717 | try { |
| 1718 | synchronized (accounts.cacheLock) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 1719 | return getAccountsFromCacheLocked(accounts, null, callingUid, null); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1720 | } |
| 1721 | } finally { |
| 1722 | restoreCallingIdentity(identityToken); |
| 1723 | } |
| 1724 | } |
| 1725 | |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 1726 | /** |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1727 | * Returns accounts for all running users. |
| 1728 | * |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 1729 | * @hide |
| 1730 | */ |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1731 | public AccountAndUser[] getRunningAccounts() { |
| 1732 | final int[] runningUserIds; |
| 1733 | try { |
| 1734 | runningUserIds = ActivityManagerNative.getDefault().getRunningUserIds(); |
| 1735 | } catch (RemoteException e) { |
| 1736 | // Running in system_server; should never happen |
| 1737 | throw new RuntimeException(e); |
| 1738 | } |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 1739 | return getAccounts(runningUserIds); |
| 1740 | } |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 1741 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 1742 | /** {@hide} */ |
| 1743 | public AccountAndUser[] getAllAccounts() { |
| 1744 | final List<UserInfo> users = getUserManager().getUsers(); |
| 1745 | final int[] userIds = new int[users.size()]; |
| 1746 | for (int i = 0; i < userIds.length; i++) { |
| 1747 | userIds[i] = users.get(i).id; |
| 1748 | } |
| 1749 | return getAccounts(userIds); |
| 1750 | } |
| 1751 | |
| 1752 | private AccountAndUser[] getAccounts(int[] userIds) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1753 | final ArrayList<AccountAndUser> runningAccounts = Lists.newArrayList(); |
| 1754 | synchronized (mUsers) { |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 1755 | for (int userId : userIds) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1756 | UserAccounts userAccounts = getUserAccounts(userId); |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 1757 | if (userAccounts == null) continue; |
| 1758 | synchronized (userAccounts.cacheLock) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1759 | Account[] accounts = getAccountsFromCacheLocked(userAccounts, null, |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 1760 | Binder.getCallingUid(), null); |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 1761 | for (int a = 0; a < accounts.length; a++) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1762 | runningAccounts.add(new AccountAndUser(accounts[a], userId)); |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 1763 | } |
| 1764 | } |
| 1765 | } |
| 1766 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1767 | |
| 1768 | AccountAndUser[] accountsArray = new AccountAndUser[runningAccounts.size()]; |
| 1769 | return runningAccounts.toArray(accountsArray); |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 1770 | } |
| 1771 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 1772 | @Override |
| 1773 | public Account[] getAccountsAsUser(String type, int userId) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 1774 | return getAccountsAsUser(type, userId, null, -1); |
| 1775 | } |
| 1776 | |
| 1777 | private Account[] getAccountsAsUser(String type, int userId, String callingPackage, |
| 1778 | int packageUid) { |
| 1779 | int callingUid = Binder.getCallingUid(); |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 1780 | // Only allow the system process to read accounts of other users |
| 1781 | if (userId != UserHandle.getCallingUserId() |
Amith Yamasani | bb49e85 | 2013-03-30 19:20:18 -0700 | [diff] [blame] | 1782 | && callingUid != Process.myUid() |
Jim Miller | 464f530 | 2013-02-27 18:33:25 -0800 | [diff] [blame] | 1783 | && mContext.checkCallingOrSelfPermission( |
| 1784 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 1785 | != PackageManager.PERMISSION_GRANTED) { |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 1786 | throw new SecurityException("User " + UserHandle.getCallingUserId() |
| 1787 | + " trying to get account for " + userId); |
| 1788 | } |
| 1789 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1790 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1791 | Log.v(TAG, "getAccounts: accountType " + type |
| 1792 | + ", caller's uid " + Binder.getCallingUid() |
| 1793 | + ", pid " + Binder.getCallingPid()); |
| 1794 | } |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 1795 | // If the original calling app was using the framework account chooser activity, we'll |
| 1796 | // be passed in the original caller's uid here, which is what should be used for filtering. |
| 1797 | if (packageUid != -1 && UserHandle.isSameApp(callingUid, Process.myUid())) { |
| 1798 | callingUid = packageUid; |
| 1799 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1800 | checkReadAccountsPermission(); |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 1801 | UserAccounts accounts = getUserAccounts(userId); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1802 | long identityToken = clearCallingIdentity(); |
| 1803 | try { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1804 | synchronized (accounts.cacheLock) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 1805 | return getAccountsFromCacheLocked(accounts, type, callingUid, callingPackage); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1806 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1807 | } finally { |
| 1808 | restoreCallingIdentity(identityToken); |
| 1809 | } |
| 1810 | } |
| 1811 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 1812 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1813 | public boolean addSharedAccountAsUser(Account account, int userId) { |
| 1814 | userId = handleIncomingUser(userId); |
| 1815 | SQLiteDatabase db = getUserAccounts(userId).openHelper.getWritableDatabase(); |
| 1816 | ContentValues values = new ContentValues(); |
| 1817 | values.put(ACCOUNTS_NAME, account.name); |
| 1818 | values.put(ACCOUNTS_TYPE, account.type); |
| 1819 | db.delete(TABLE_SHARED_ACCOUNTS, ACCOUNTS_NAME + "=? AND " + ACCOUNTS_TYPE+ "=?", |
| 1820 | new String[] {account.name, account.type}); |
| 1821 | long accountId = db.insert(TABLE_SHARED_ACCOUNTS, ACCOUNTS_NAME, values); |
| 1822 | if (accountId < 0) { |
| 1823 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1824 | + ", skipping the DB insert failed"); |
| 1825 | return false; |
| 1826 | } |
| 1827 | return true; |
| 1828 | } |
| 1829 | |
| 1830 | @Override |
| 1831 | public boolean removeSharedAccountAsUser(Account account, int userId) { |
| 1832 | userId = handleIncomingUser(userId); |
| 1833 | UserAccounts accounts = getUserAccounts(userId); |
| 1834 | SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
| 1835 | int r = db.delete(TABLE_SHARED_ACCOUNTS, ACCOUNTS_NAME + "=? AND " + ACCOUNTS_TYPE+ "=?", |
| 1836 | new String[] {account.name, account.type}); |
| 1837 | if (r > 0) { |
| 1838 | removeAccountInternal(accounts, account); |
| 1839 | } |
| 1840 | return r > 0; |
| 1841 | } |
| 1842 | |
| 1843 | @Override |
| 1844 | public Account[] getSharedAccountsAsUser(int userId) { |
| 1845 | userId = handleIncomingUser(userId); |
| 1846 | UserAccounts accounts = getUserAccounts(userId); |
| 1847 | ArrayList<Account> accountList = new ArrayList<Account>(); |
| 1848 | Cursor cursor = null; |
| 1849 | try { |
| 1850 | cursor = accounts.openHelper.getReadableDatabase() |
| 1851 | .query(TABLE_SHARED_ACCOUNTS, new String[]{ACCOUNTS_NAME, ACCOUNTS_TYPE}, |
| 1852 | null, null, null, null, null); |
| 1853 | if (cursor != null && cursor.moveToFirst()) { |
| 1854 | int nameIndex = cursor.getColumnIndex(ACCOUNTS_NAME); |
| 1855 | int typeIndex = cursor.getColumnIndex(ACCOUNTS_TYPE); |
| 1856 | do { |
| 1857 | accountList.add(new Account(cursor.getString(nameIndex), |
| 1858 | cursor.getString(typeIndex))); |
| 1859 | } while (cursor.moveToNext()); |
| 1860 | } |
| 1861 | } finally { |
| 1862 | if (cursor != null) { |
| 1863 | cursor.close(); |
| 1864 | } |
| 1865 | } |
| 1866 | Account[] accountArray = new Account[accountList.size()]; |
| 1867 | accountList.toArray(accountArray); |
| 1868 | return accountArray; |
| 1869 | } |
| 1870 | |
| 1871 | @Override |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 1872 | public Account[] getAccounts(String type) { |
| 1873 | return getAccountsAsUser(type, UserHandle.getCallingUserId()); |
| 1874 | } |
| 1875 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 1876 | @Override |
| 1877 | public Account[] getAccountsForPackage(String packageName, int uid) { |
| 1878 | int callingUid = Binder.getCallingUid(); |
| 1879 | if (!UserHandle.isSameApp(callingUid, Process.myUid())) { |
| 1880 | throw new SecurityException("getAccountsForPackage() called from unauthorized uid " |
| 1881 | + callingUid + " with uid=" + uid); |
| 1882 | } |
| 1883 | return getAccountsAsUser(null, UserHandle.getCallingUserId(), packageName, uid); |
| 1884 | } |
| 1885 | |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 1886 | @Override |
| 1887 | public Account[] getAccountsByTypeForPackage(String type, String packageName) { |
| 1888 | checkBinderPermission(android.Manifest.permission.INTERACT_ACROSS_USERS); |
| 1889 | int packageUid = -1; |
| 1890 | try { |
| 1891 | packageUid = AppGlobals.getPackageManager().getPackageUid( |
| 1892 | packageName, UserHandle.getCallingUserId()); |
| 1893 | } catch (RemoteException re) { |
| 1894 | Slog.e(TAG, "Couldn't determine the packageUid for " + packageName + re); |
| 1895 | return new Account[0]; |
| 1896 | } |
| 1897 | return getAccountsAsUser(type, UserHandle.getCallingUserId(), packageName, packageUid); |
| 1898 | } |
| 1899 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1900 | public void getAccountsByFeatures(IAccountManagerResponse response, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1901 | String type, String[] features) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1902 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1903 | Log.v(TAG, "getAccounts: accountType " + type |
| 1904 | + ", response " + response |
| 1905 | + ", features " + stringArrayToString(features) |
| 1906 | + ", caller's uid " + Binder.getCallingUid() |
| 1907 | + ", pid " + Binder.getCallingPid()); |
| 1908 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1909 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 1910 | if (type == null) throw new IllegalArgumentException("accountType is null"); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1911 | checkReadAccountsPermission(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1912 | UserAccounts userAccounts = getUserAccountsForCaller(); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1913 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1914 | long identityToken = clearCallingIdentity(); |
| 1915 | try { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1916 | if (features == null || features.length == 0) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1917 | Account[] accounts; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1918 | synchronized (userAccounts.cacheLock) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 1919 | accounts = getAccountsFromCacheLocked(userAccounts, type, callingUid, null); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1920 | } |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 1921 | Bundle result = new Bundle(); |
| 1922 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, accounts); |
| 1923 | onResult(response, result); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1924 | return; |
| 1925 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1926 | new GetAccountsByTypeAndFeatureSession(userAccounts, response, type, features, |
| 1927 | callingUid).bind(); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1928 | } finally { |
| 1929 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1930 | } |
| 1931 | } |
| 1932 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1933 | private long getAccountIdLocked(SQLiteDatabase db, Account account) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1934 | Cursor cursor = db.query(TABLE_ACCOUNTS, new String[]{ACCOUNTS_ID}, |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1935 | "name=? AND type=?", new String[]{account.name, account.type}, null, null, null); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1936 | try { |
| 1937 | if (cursor.moveToNext()) { |
| 1938 | return cursor.getLong(0); |
| 1939 | } |
| 1940 | return -1; |
| 1941 | } finally { |
| 1942 | cursor.close(); |
| 1943 | } |
| 1944 | } |
| 1945 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1946 | private long getExtrasIdLocked(SQLiteDatabase db, long accountId, String key) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1947 | Cursor cursor = db.query(TABLE_EXTRAS, new String[]{EXTRAS_ID}, |
| 1948 | EXTRAS_ACCOUNTS_ID + "=" + accountId + " AND " + EXTRAS_KEY + "=?", |
| 1949 | new String[]{key}, null, null, null); |
| 1950 | try { |
| 1951 | if (cursor.moveToNext()) { |
| 1952 | return cursor.getLong(0); |
| 1953 | } |
| 1954 | return -1; |
| 1955 | } finally { |
| 1956 | cursor.close(); |
| 1957 | } |
| 1958 | } |
| 1959 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1960 | private abstract class Session extends IAccountAuthenticatorResponse.Stub |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 1961 | implements IBinder.DeathRecipient, ServiceConnection { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1962 | IAccountManagerResponse mResponse; |
| 1963 | final String mAccountType; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1964 | final boolean mExpectActivityLaunch; |
| 1965 | final long mCreationTime; |
| 1966 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1967 | public int mNumResults = 0; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1968 | private int mNumRequestContinued = 0; |
| 1969 | private int mNumErrors = 0; |
| 1970 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1971 | IAccountAuthenticator mAuthenticator = null; |
| 1972 | |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 1973 | private final boolean mStripAuthTokenFromResult; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1974 | protected final UserAccounts mAccounts; |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 1975 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1976 | public Session(UserAccounts accounts, IAccountManagerResponse response, String accountType, |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 1977 | boolean expectActivityLaunch, boolean stripAuthTokenFromResult) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1978 | super(); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1979 | //if (response == null) throw new IllegalArgumentException("response is null"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1980 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1981 | mAccounts = accounts; |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 1982 | mStripAuthTokenFromResult = stripAuthTokenFromResult; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1983 | mResponse = response; |
| 1984 | mAccountType = accountType; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1985 | mExpectActivityLaunch = expectActivityLaunch; |
| 1986 | mCreationTime = SystemClock.elapsedRealtime(); |
| 1987 | synchronized (mSessions) { |
| 1988 | mSessions.put(toString(), this); |
| 1989 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1990 | if (response != null) { |
| 1991 | try { |
| 1992 | response.asBinder().linkToDeath(this, 0 /* flags */); |
| 1993 | } catch (RemoteException e) { |
| 1994 | mResponse = null; |
| 1995 | binderDied(); |
| 1996 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1997 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1998 | } |
| 1999 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2000 | IAccountManagerResponse getResponseAndClose() { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2001 | if (mResponse == null) { |
| 2002 | // this session has already been closed |
| 2003 | return null; |
| 2004 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2005 | IAccountManagerResponse response = mResponse; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2006 | close(); // this clears mResponse so we need to save the response before this call |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2007 | return response; |
| 2008 | } |
| 2009 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2010 | private void close() { |
| 2011 | synchronized (mSessions) { |
| 2012 | if (mSessions.remove(toString()) == null) { |
| 2013 | // the session was already closed, so bail out now |
| 2014 | return; |
| 2015 | } |
| 2016 | } |
| 2017 | if (mResponse != null) { |
| 2018 | // stop listening for response deaths |
| 2019 | mResponse.asBinder().unlinkToDeath(this, 0 /* flags */); |
| 2020 | |
| 2021 | // clear this so that we don't accidentally send any further results |
| 2022 | mResponse = null; |
| 2023 | } |
| 2024 | cancelTimeout(); |
| 2025 | unbind(); |
| 2026 | } |
| 2027 | |
| 2028 | public void binderDied() { |
| 2029 | mResponse = null; |
| 2030 | close(); |
| 2031 | } |
| 2032 | |
| 2033 | protected String toDebugString() { |
| 2034 | return toDebugString(SystemClock.elapsedRealtime()); |
| 2035 | } |
| 2036 | |
| 2037 | protected String toDebugString(long now) { |
| 2038 | return "Session: expectLaunch " + mExpectActivityLaunch |
| 2039 | + ", connected " + (mAuthenticator != null) |
| 2040 | + ", stats (" + mNumResults + "/" + mNumRequestContinued |
| 2041 | + "/" + mNumErrors + ")" |
| 2042 | + ", lifetime " + ((now - mCreationTime) / 1000.0); |
| 2043 | } |
| 2044 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2045 | void bind() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2046 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2047 | Log.v(TAG, "initiating bind to authenticator type " + mAccountType); |
| 2048 | } |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 2049 | if (!bindToAuthenticator(mAccountType)) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2050 | Log.d(TAG, "bind attempt failed for " + toDebugString()); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2051 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "bind failure"); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2052 | } |
| 2053 | } |
| 2054 | |
| 2055 | private void unbind() { |
| 2056 | if (mAuthenticator != null) { |
| 2057 | mAuthenticator = null; |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 2058 | mContext.unbindService(this); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2059 | } |
| 2060 | } |
| 2061 | |
| 2062 | public void scheduleTimeout() { |
| 2063 | mMessageHandler.sendMessageDelayed( |
| 2064 | mMessageHandler.obtainMessage(MESSAGE_TIMED_OUT, this), TIMEOUT_DELAY_MS); |
| 2065 | } |
| 2066 | |
| 2067 | public void cancelTimeout() { |
| 2068 | mMessageHandler.removeMessages(MESSAGE_TIMED_OUT, this); |
| 2069 | } |
| 2070 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 2071 | public void onServiceConnected(ComponentName name, IBinder service) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2072 | mAuthenticator = IAccountAuthenticator.Stub.asInterface(service); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2073 | try { |
| 2074 | run(); |
| 2075 | } catch (RemoteException e) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2076 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2077 | "remote exception"); |
| 2078 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2079 | } |
| 2080 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 2081 | public void onServiceDisconnected(ComponentName name) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2082 | mAuthenticator = null; |
| 2083 | IAccountManagerResponse response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2084 | if (response != null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 2085 | try { |
| 2086 | response.onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
| 2087 | "disconnected"); |
| 2088 | } catch (RemoteException e) { |
| 2089 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2090 | Log.v(TAG, "Session.onServiceDisconnected: " |
| 2091 | + "caught RemoteException while responding", e); |
| 2092 | } |
| 2093 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2094 | } |
| 2095 | } |
| 2096 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 2097 | public abstract void run() throws RemoteException; |
| 2098 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2099 | public void onTimedOut() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2100 | IAccountManagerResponse response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2101 | if (response != null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 2102 | try { |
| 2103 | response.onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
| 2104 | "timeout"); |
| 2105 | } catch (RemoteException e) { |
| 2106 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2107 | Log.v(TAG, "Session.onTimedOut: caught RemoteException while responding", |
| 2108 | e); |
| 2109 | } |
| 2110 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2111 | } |
| 2112 | } |
| 2113 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2114 | public void onResult(Bundle result) { |
| 2115 | mNumResults++; |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2116 | if (result != null && !TextUtils.isEmpty(result.getString(AccountManager.KEY_AUTHTOKEN))) { |
| 2117 | String accountName = result.getString(AccountManager.KEY_ACCOUNT_NAME); |
| 2118 | String accountType = result.getString(AccountManager.KEY_ACCOUNT_TYPE); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2119 | if (!TextUtils.isEmpty(accountName) && !TextUtils.isEmpty(accountType)) { |
| 2120 | Account account = new Account(accountName, accountType); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2121 | cancelNotification(getSigninRequiredNotificationId(mAccounts, account), |
| 2122 | new UserHandle(mAccounts.userId)); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2123 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2124 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2125 | IAccountManagerResponse response; |
| 2126 | if (mExpectActivityLaunch && result != null |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2127 | && result.containsKey(AccountManager.KEY_INTENT)) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2128 | response = mResponse; |
| 2129 | } else { |
| 2130 | response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2131 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2132 | if (response != null) { |
| 2133 | try { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2134 | if (result == null) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2135 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2136 | Log.v(TAG, getClass().getSimpleName() |
| 2137 | + " calling onError() on response " + response); |
| 2138 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2139 | response.onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2140 | "null bundle returned"); |
| 2141 | } else { |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 2142 | if (mStripAuthTokenFromResult) { |
| 2143 | result.remove(AccountManager.KEY_AUTHTOKEN); |
| 2144 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2145 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2146 | Log.v(TAG, getClass().getSimpleName() |
| 2147 | + " calling onResult() on response " + response); |
| 2148 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2149 | response.onResult(result); |
| 2150 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2151 | } catch (RemoteException e) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2152 | // if the caller is dead then there is no one to care about remote exceptions |
| 2153 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2154 | Log.v(TAG, "failure while notifying response", e); |
| 2155 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2156 | } |
| 2157 | } |
| 2158 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2159 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2160 | public void onRequestContinued() { |
| 2161 | mNumRequestContinued++; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2162 | } |
| 2163 | |
| 2164 | public void onError(int errorCode, String errorMessage) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2165 | mNumErrors++; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2166 | IAccountManagerResponse response = getResponseAndClose(); |
| 2167 | if (response != null) { |
| 2168 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2169 | Log.v(TAG, getClass().getSimpleName() |
| 2170 | + " calling onError() on response " + response); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2171 | } |
| 2172 | try { |
| 2173 | response.onError(errorCode, errorMessage); |
| 2174 | } catch (RemoteException e) { |
| 2175 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2176 | Log.v(TAG, "Session.onError: caught RemoteException while responding", e); |
| 2177 | } |
| 2178 | } |
| 2179 | } else { |
| 2180 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2181 | Log.v(TAG, "Session.onError: already closed"); |
| 2182 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2183 | } |
| 2184 | } |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 2185 | |
| 2186 | /** |
| 2187 | * find the component name for the authenticator and initiate a bind |
| 2188 | * if no authenticator or the bind fails then return false, otherwise return true |
| 2189 | */ |
| 2190 | private boolean bindToAuthenticator(String authenticatorType) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2191 | final AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription> authenticatorInfo; |
| 2192 | authenticatorInfo = mAuthenticatorCache.getServiceInfo( |
| 2193 | AuthenticatorDescription.newKey(authenticatorType), mAccounts.userId); |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 2194 | if (authenticatorInfo == null) { |
| 2195 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2196 | Log.v(TAG, "there is no authenticator for " + authenticatorType |
| 2197 | + ", bailing out"); |
| 2198 | } |
| 2199 | return false; |
| 2200 | } |
| 2201 | |
| 2202 | Intent intent = new Intent(); |
| 2203 | intent.setAction(AccountManager.ACTION_AUTHENTICATOR_INTENT); |
| 2204 | intent.setComponent(authenticatorInfo.componentName); |
| 2205 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2206 | Log.v(TAG, "performing bindService to " + authenticatorInfo.componentName); |
| 2207 | } |
Amith Yamasani | 27b89e6 | 2013-01-16 12:30:11 -0800 | [diff] [blame] | 2208 | if (!mContext.bindServiceAsUser(intent, this, Context.BIND_AUTO_CREATE, |
| 2209 | new UserHandle(mAccounts.userId))) { |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 2210 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2211 | Log.v(TAG, "bindService to " + authenticatorInfo.componentName + " failed"); |
| 2212 | } |
| 2213 | return false; |
| 2214 | } |
| 2215 | |
| 2216 | |
| 2217 | return true; |
| 2218 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2219 | } |
| 2220 | |
| 2221 | private class MessageHandler extends Handler { |
| 2222 | MessageHandler(Looper looper) { |
| 2223 | super(looper); |
| 2224 | } |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 2225 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2226 | public void handleMessage(Message msg) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2227 | switch (msg.what) { |
| 2228 | case MESSAGE_TIMED_OUT: |
| 2229 | Session session = (Session)msg.obj; |
| 2230 | session.onTimedOut(); |
| 2231 | break; |
| 2232 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 2233 | case MESSAGE_COPY_SHARED_ACCOUNT: |
| 2234 | copyAccountToUser((Account) msg.obj, msg.arg1, msg.arg2); |
| 2235 | break; |
| 2236 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2237 | default: |
| 2238 | throw new IllegalStateException("unhandled message: " + msg.what); |
| 2239 | } |
| 2240 | } |
| 2241 | } |
| 2242 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2243 | private static String getDatabaseName(int userId) { |
| 2244 | File systemDir = Environment.getSystemSecureDirectory(); |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 2245 | File databaseFile = new File(Environment.getUserSystemDirectory(userId), DATABASE_NAME); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2246 | if (userId == 0) { |
Amith Yamasani | a23bb38 | 2012-04-11 15:32:07 -0700 | [diff] [blame] | 2247 | // Migrate old file, if it exists, to the new location. |
| 2248 | // Make sure the new file doesn't already exist. A dummy file could have been |
| 2249 | // accidentally created in the old location, causing the new one to become corrupted |
| 2250 | // as well. |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2251 | File oldFile = new File(systemDir, DATABASE_NAME); |
Amith Yamasani | a23bb38 | 2012-04-11 15:32:07 -0700 | [diff] [blame] | 2252 | if (oldFile.exists() && !databaseFile.exists()) { |
Marc Blank | c6b0f99 | 2012-03-18 19:16:41 -0700 | [diff] [blame] | 2253 | // Check for use directory; create if it doesn't exist, else renameTo will fail |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 2254 | File userDir = Environment.getUserSystemDirectory(userId); |
Marc Blank | c6b0f99 | 2012-03-18 19:16:41 -0700 | [diff] [blame] | 2255 | if (!userDir.exists()) { |
| 2256 | if (!userDir.mkdirs()) { |
| 2257 | throw new IllegalStateException("User dir cannot be created: " + userDir); |
| 2258 | } |
| 2259 | } |
| 2260 | if (!oldFile.renameTo(databaseFile)) { |
| 2261 | throw new IllegalStateException("User dir cannot be migrated: " + databaseFile); |
| 2262 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2263 | } |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 2264 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2265 | return databaseFile.getPath(); |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 2266 | } |
| 2267 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2268 | static class DatabaseHelper extends SQLiteOpenHelper { |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 2269 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2270 | public DatabaseHelper(Context context, int userId) { |
| 2271 | super(context, AccountManagerService.getDatabaseName(userId), null, DATABASE_VERSION); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2272 | } |
| 2273 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2274 | /** |
| 2275 | * This call needs to be made while the mCacheLock is held. The way to |
| 2276 | * ensure this is to get the lock any time a method is called ont the DatabaseHelper |
| 2277 | * @param db The database. |
| 2278 | */ |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2279 | @Override |
| 2280 | public void onCreate(SQLiteDatabase db) { |
| 2281 | db.execSQL("CREATE TABLE " + TABLE_ACCOUNTS + " ( " |
| 2282 | + ACCOUNTS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 2283 | + ACCOUNTS_NAME + " TEXT NOT NULL, " |
| 2284 | + ACCOUNTS_TYPE + " TEXT NOT NULL, " |
| 2285 | + ACCOUNTS_PASSWORD + " TEXT, " |
| 2286 | + "UNIQUE(" + ACCOUNTS_NAME + "," + ACCOUNTS_TYPE + "))"); |
| 2287 | |
| 2288 | db.execSQL("CREATE TABLE " + TABLE_AUTHTOKENS + " ( " |
| 2289 | + AUTHTOKENS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 2290 | + AUTHTOKENS_ACCOUNTS_ID + " INTEGER NOT NULL, " |
| 2291 | + AUTHTOKENS_TYPE + " TEXT NOT NULL, " |
| 2292 | + AUTHTOKENS_AUTHTOKEN + " TEXT, " |
| 2293 | + "UNIQUE (" + AUTHTOKENS_ACCOUNTS_ID + "," + AUTHTOKENS_TYPE + "))"); |
| 2294 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2295 | createGrantsTable(db); |
| 2296 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2297 | db.execSQL("CREATE TABLE " + TABLE_EXTRAS + " ( " |
| 2298 | + EXTRAS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 2299 | + EXTRAS_ACCOUNTS_ID + " INTEGER, " |
| 2300 | + EXTRAS_KEY + " TEXT NOT NULL, " |
| 2301 | + EXTRAS_VALUE + " TEXT, " |
| 2302 | + "UNIQUE(" + EXTRAS_ACCOUNTS_ID + "," + EXTRAS_KEY + "))"); |
| 2303 | |
| 2304 | db.execSQL("CREATE TABLE " + TABLE_META + " ( " |
| 2305 | + META_KEY + " TEXT PRIMARY KEY NOT NULL, " |
| 2306 | + META_VALUE + " TEXT)"); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2307 | |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2308 | createSharedAccountsTable(db); |
| 2309 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2310 | createAccountsDeletionTrigger(db); |
| 2311 | } |
| 2312 | |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2313 | private void createSharedAccountsTable(SQLiteDatabase db) { |
| 2314 | db.execSQL("CREATE TABLE " + TABLE_SHARED_ACCOUNTS + " ( " |
| 2315 | + ACCOUNTS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 2316 | + ACCOUNTS_NAME + " TEXT NOT NULL, " |
| 2317 | + ACCOUNTS_TYPE + " TEXT NOT NULL, " |
| 2318 | + "UNIQUE(" + ACCOUNTS_NAME + "," + ACCOUNTS_TYPE + "))"); |
| 2319 | } |
| 2320 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2321 | private void createAccountsDeletionTrigger(SQLiteDatabase db) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2322 | db.execSQL("" |
| 2323 | + " CREATE TRIGGER " + TABLE_ACCOUNTS + "Delete DELETE ON " + TABLE_ACCOUNTS |
| 2324 | + " BEGIN" |
| 2325 | + " DELETE FROM " + TABLE_AUTHTOKENS |
| 2326 | + " WHERE " + AUTHTOKENS_ACCOUNTS_ID + "=OLD." + ACCOUNTS_ID + " ;" |
| 2327 | + " DELETE FROM " + TABLE_EXTRAS |
| 2328 | + " WHERE " + EXTRAS_ACCOUNTS_ID + "=OLD." + ACCOUNTS_ID + " ;" |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2329 | + " DELETE FROM " + TABLE_GRANTS |
| 2330 | + " WHERE " + GRANTS_ACCOUNTS_ID + "=OLD." + ACCOUNTS_ID + " ;" |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2331 | + " END"); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2332 | } |
| 2333 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2334 | private void createGrantsTable(SQLiteDatabase db) { |
| 2335 | db.execSQL("CREATE TABLE " + TABLE_GRANTS + " ( " |
| 2336 | + GRANTS_ACCOUNTS_ID + " INTEGER NOT NULL, " |
| 2337 | + GRANTS_AUTH_TOKEN_TYPE + " STRING NOT NULL, " |
| 2338 | + GRANTS_GRANTEE_UID + " INTEGER NOT NULL, " |
| 2339 | + "UNIQUE (" + GRANTS_ACCOUNTS_ID + "," + GRANTS_AUTH_TOKEN_TYPE |
| 2340 | + "," + GRANTS_GRANTEE_UID + "))"); |
| 2341 | } |
| 2342 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2343 | @Override |
| 2344 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2345 | Log.e(TAG, "upgrade from version " + oldVersion + " to version " + newVersion); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2346 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2347 | if (oldVersion == 1) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2348 | // no longer need to do anything since the work is done |
| 2349 | // when upgrading from version 2 |
| 2350 | oldVersion++; |
| 2351 | } |
| 2352 | |
| 2353 | if (oldVersion == 2) { |
| 2354 | createGrantsTable(db); |
| 2355 | db.execSQL("DROP TRIGGER " + TABLE_ACCOUNTS + "Delete"); |
| 2356 | createAccountsDeletionTrigger(db); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2357 | oldVersion++; |
| 2358 | } |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 2359 | |
| 2360 | if (oldVersion == 3) { |
| 2361 | db.execSQL("UPDATE " + TABLE_ACCOUNTS + " SET " + ACCOUNTS_TYPE + |
| 2362 | " = 'com.google' WHERE " + ACCOUNTS_TYPE + " == 'com.google.GAIA'"); |
| 2363 | oldVersion++; |
| 2364 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2365 | |
| 2366 | if (oldVersion == 4) { |
| 2367 | createSharedAccountsTable(db); |
| 2368 | oldVersion++; |
| 2369 | } |
| 2370 | |
| 2371 | if (oldVersion != newVersion) { |
| 2372 | Log.e(TAG, "failed to upgrade version " + oldVersion + " to version " + newVersion); |
| 2373 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2374 | } |
| 2375 | |
| 2376 | @Override |
| 2377 | public void onOpen(SQLiteDatabase db) { |
| 2378 | if (Log.isLoggable(TAG, Log.VERBOSE)) Log.v(TAG, "opened database " + DATABASE_NAME); |
| 2379 | } |
| 2380 | } |
| 2381 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2382 | public IBinder onBind(Intent intent) { |
| 2383 | return asBinder(); |
| 2384 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2385 | |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 2386 | /** |
| 2387 | * Searches array of arguments for the specified string |
| 2388 | * @param args array of argument strings |
| 2389 | * @param value value to search for |
| 2390 | * @return true if the value is contained in the array |
| 2391 | */ |
| 2392 | private static boolean scanArgs(String[] args, String value) { |
| 2393 | if (args != null) { |
| 2394 | for (String arg : args) { |
| 2395 | if (value.equals(arg)) { |
| 2396 | return true; |
| 2397 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2398 | } |
| 2399 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 2400 | return false; |
| 2401 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2402 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 2403 | @Override |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 2404 | protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 2405 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 2406 | != PackageManager.PERMISSION_GRANTED) { |
| 2407 | fout.println("Permission Denial: can't dump AccountsManager from from pid=" |
| 2408 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 2409 | + " without permission " + android.Manifest.permission.DUMP); |
| 2410 | return; |
| 2411 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2412 | final boolean isCheckinRequest = scanArgs(args, "--checkin") || scanArgs(args, "-c"); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 2413 | final IndentingPrintWriter ipw = new IndentingPrintWriter(fout, " "); |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 2414 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 2415 | final List<UserInfo> users = getUserManager().getUsers(); |
| 2416 | for (UserInfo user : users) { |
| 2417 | ipw.println("User " + user + ":"); |
| 2418 | ipw.increaseIndent(); |
| 2419 | dumpUser(getUserAccounts(user.id), fd, ipw, args, isCheckinRequest); |
| 2420 | ipw.println(); |
| 2421 | ipw.decreaseIndent(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2422 | } |
| 2423 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2424 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2425 | private void dumpUser(UserAccounts userAccounts, FileDescriptor fd, PrintWriter fout, |
| 2426 | String[] args, boolean isCheckinRequest) { |
| 2427 | synchronized (userAccounts.cacheLock) { |
| 2428 | final SQLiteDatabase db = userAccounts.openHelper.getReadableDatabase(); |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 2429 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2430 | if (isCheckinRequest) { |
| 2431 | // This is a checkin request. *Only* upload the account types and the count of each. |
| 2432 | Cursor cursor = db.query(TABLE_ACCOUNTS, ACCOUNT_TYPE_COUNT_PROJECTION, |
| 2433 | null, null, ACCOUNTS_TYPE, null, null); |
| 2434 | try { |
| 2435 | while (cursor.moveToNext()) { |
| 2436 | // print type,count |
| 2437 | fout.println(cursor.getString(0) + "," + cursor.getString(1)); |
| 2438 | } |
| 2439 | } finally { |
| 2440 | if (cursor != null) { |
| 2441 | cursor.close(); |
| 2442 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 2443 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2444 | } else { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2445 | Account[] accounts = getAccountsFromCacheLocked(userAccounts, null /* type */, |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 2446 | Process.myUid(), null); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2447 | fout.println("Accounts: " + accounts.length); |
| 2448 | for (Account account : accounts) { |
| 2449 | fout.println(" " + account); |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 2450 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 2451 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2452 | fout.println(); |
| 2453 | synchronized (mSessions) { |
| 2454 | final long now = SystemClock.elapsedRealtime(); |
| 2455 | fout.println("Active Sessions: " + mSessions.size()); |
| 2456 | for (Session session : mSessions.values()) { |
| 2457 | fout.println(" " + session.toDebugString(now)); |
| 2458 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 2459 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 2460 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2461 | fout.println(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2462 | mAuthenticatorCache.dump(fd, fout, args, userAccounts.userId); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2463 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 2464 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2465 | } |
| 2466 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2467 | private void doNotification(UserAccounts accounts, Account account, CharSequence message, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2468 | Intent intent, int userId) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2469 | long identityToken = clearCallingIdentity(); |
| 2470 | try { |
| 2471 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2472 | Log.v(TAG, "doNotification: " + message + " intent:" + intent); |
| 2473 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2474 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2475 | if (intent.getComponent() != null && |
| 2476 | GrantCredentialsPermissionActivity.class.getName().equals( |
| 2477 | intent.getComponent().getClassName())) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2478 | createNoCredentialsPermissionNotification(account, intent, userId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2479 | } else { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2480 | final Integer notificationId = getSigninRequiredNotificationId(accounts, account); |
Fred Quintana | 33f889a | 2009-09-14 17:31:26 -0700 | [diff] [blame] | 2481 | intent.addCategory(String.valueOf(notificationId)); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2482 | Notification n = new Notification(android.R.drawable.stat_sys_warning, null, |
| 2483 | 0 /* when */); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2484 | UserHandle user = new UserHandle(userId); |
Fred Quintana | 33f889a | 2009-09-14 17:31:26 -0700 | [diff] [blame] | 2485 | final String notificationTitleFormat = |
| 2486 | mContext.getText(R.string.notification_title).toString(); |
| 2487 | n.setLatestEventInfo(mContext, |
| 2488 | String.format(notificationTitleFormat, account.name), |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2489 | message, PendingIntent.getActivityAsUser( |
| 2490 | mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT, |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2491 | null, user)); |
| 2492 | installNotification(notificationId, n, user); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2493 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2494 | } finally { |
| 2495 | restoreCallingIdentity(identityToken); |
| 2496 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2497 | } |
| 2498 | |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2499 | protected void installNotification(final int notificationId, final Notification n, |
| 2500 | UserHandle user) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2501 | ((NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE)) |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2502 | .notifyAsUser(null, notificationId, n, user); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2503 | } |
| 2504 | |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2505 | protected void cancelNotification(int id, UserHandle user) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2506 | long identityToken = clearCallingIdentity(); |
| 2507 | try { |
| 2508 | ((NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE)) |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2509 | .cancelAsUser(null, id, user); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2510 | } finally { |
| 2511 | restoreCallingIdentity(identityToken); |
| 2512 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2513 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2514 | |
Fred Quintana | b38eb14 | 2010-02-24 13:40:54 -0800 | [diff] [blame] | 2515 | /** Succeeds if any of the specified permissions are granted. */ |
| 2516 | private void checkBinderPermission(String... permissions) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2517 | final int uid = Binder.getCallingUid(); |
Fred Quintana | b38eb14 | 2010-02-24 13:40:54 -0800 | [diff] [blame] | 2518 | |
| 2519 | for (String perm : permissions) { |
| 2520 | if (mContext.checkCallingOrSelfPermission(perm) == PackageManager.PERMISSION_GRANTED) { |
| 2521 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2522 | Log.v(TAG, " caller uid " + uid + " has " + perm); |
Fred Quintana | b38eb14 | 2010-02-24 13:40:54 -0800 | [diff] [blame] | 2523 | } |
| 2524 | return; |
| 2525 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2526 | } |
Fred Quintana | b38eb14 | 2010-02-24 13:40:54 -0800 | [diff] [blame] | 2527 | |
| 2528 | String msg = "caller uid " + uid + " lacks any of " + TextUtils.join(",", permissions); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2529 | Log.w(TAG, " " + msg); |
Fred Quintana | b38eb14 | 2010-02-24 13:40:54 -0800 | [diff] [blame] | 2530 | throw new SecurityException(msg); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2531 | } |
| 2532 | |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2533 | private int handleIncomingUser(int userId) { |
| 2534 | try { |
| 2535 | return ActivityManagerNative.getDefault().handleIncomingUser( |
| 2536 | Binder.getCallingPid(), Binder.getCallingUid(), userId, true, true, "", null); |
| 2537 | } catch (RemoteException re) { |
| 2538 | // Shouldn't happen, local. |
| 2539 | } |
| 2540 | return userId; |
| 2541 | } |
| 2542 | |
Christopher Tate | ccbf84f | 2013-05-08 15:25:41 -0700 | [diff] [blame^] | 2543 | private boolean isPrivileged(int callingUid) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2544 | final int callingUserId = UserHandle.getUserId(callingUid); |
| 2545 | |
| 2546 | final PackageManager userPackageManager; |
| 2547 | try { |
| 2548 | userPackageManager = mContext.createPackageContextAsUser( |
| 2549 | "android", 0, new UserHandle(callingUserId)).getPackageManager(); |
| 2550 | } catch (NameNotFoundException e) { |
| 2551 | return false; |
| 2552 | } |
| 2553 | |
| 2554 | String[] packages = userPackageManager.getPackagesForUid(callingUid); |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 2555 | for (String name : packages) { |
| 2556 | try { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2557 | PackageInfo packageInfo = userPackageManager.getPackageInfo(name, 0 /* flags */); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2558 | if (packageInfo != null |
Christopher Tate | ccbf84f | 2013-05-08 15:25:41 -0700 | [diff] [blame^] | 2559 | && (packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_PRIVILEGED) != 0) { |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 2560 | return true; |
| 2561 | } |
| 2562 | } catch (PackageManager.NameNotFoundException e) { |
| 2563 | return false; |
| 2564 | } |
| 2565 | } |
| 2566 | return false; |
| 2567 | } |
| 2568 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2569 | private boolean permissionIsGranted(Account account, String authTokenType, int callerUid) { |
Christopher Tate | ccbf84f | 2013-05-08 15:25:41 -0700 | [diff] [blame^] | 2570 | final boolean isPrivileged = isPrivileged(callerUid); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2571 | final boolean fromAuthenticator = account != null |
| 2572 | && hasAuthenticatorUid(account.type, callerUid); |
| 2573 | final boolean hasExplicitGrants = account != null |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2574 | && hasExplicitlyGrantedPermission(account, authTokenType, callerUid); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2575 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2576 | Log.v(TAG, "checkGrantsOrCallingUidAgainstAuthenticator: caller uid " |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2577 | + callerUid + ", " + account |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2578 | + ": is authenticator? " + fromAuthenticator |
| 2579 | + ", has explicit permission? " + hasExplicitGrants); |
| 2580 | } |
Christopher Tate | ccbf84f | 2013-05-08 15:25:41 -0700 | [diff] [blame^] | 2581 | return fromAuthenticator || hasExplicitGrants || isPrivileged; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2582 | } |
| 2583 | |
Fred Quintana | 1a23191 | 2009-10-15 11:31:30 -0700 | [diff] [blame] | 2584 | private boolean hasAuthenticatorUid(String accountType, int callingUid) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2585 | final int callingUserId = UserHandle.getUserId(callingUid); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2586 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> serviceInfo : |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2587 | mAuthenticatorCache.getAllServices(callingUserId)) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2588 | if (serviceInfo.type.type.equals(accountType)) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2589 | return (serviceInfo.uid == callingUid) || |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2590 | (mPackageManager.checkSignatures(serviceInfo.uid, callingUid) |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2591 | == PackageManager.SIGNATURE_MATCH); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2592 | } |
| 2593 | } |
| 2594 | return false; |
| 2595 | } |
| 2596 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2597 | private boolean hasExplicitlyGrantedPermission(Account account, String authTokenType, |
| 2598 | int callerUid) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 2599 | if (callerUid == Process.SYSTEM_UID) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2600 | return true; |
| 2601 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2602 | UserAccounts accounts = getUserAccountsForCaller(); |
| 2603 | synchronized (accounts.cacheLock) { |
| 2604 | final SQLiteDatabase db = accounts.openHelper.getReadableDatabase(); |
| 2605 | String[] args = { String.valueOf(callerUid), authTokenType, |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2606 | account.name, account.type}; |
| 2607 | final boolean permissionGranted = |
| 2608 | DatabaseUtils.longForQuery(db, COUNT_OF_MATCHING_GRANTS, args) != 0; |
| 2609 | if (!permissionGranted && ActivityManager.isRunningInTestHarness()) { |
| 2610 | // TODO: Skip this check when running automated tests. Replace this |
| 2611 | // with a more general solution. |
| 2612 | Log.d(TAG, "no credentials permission for usage of " + account + ", " |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2613 | + authTokenType + " by uid " + callerUid |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2614 | + " but ignoring since device is in test harness."); |
| 2615 | return true; |
| 2616 | } |
| 2617 | return permissionGranted; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2618 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2619 | } |
| 2620 | |
| 2621 | private void checkCallingUidAgainstAuthenticator(Account account) { |
| 2622 | final int uid = Binder.getCallingUid(); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2623 | if (account == null || !hasAuthenticatorUid(account.type, uid)) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2624 | String msg = "caller uid " + uid + " is different than the authenticator's uid"; |
| 2625 | Log.w(TAG, msg); |
| 2626 | throw new SecurityException(msg); |
| 2627 | } |
| 2628 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2629 | Log.v(TAG, "caller uid " + uid + " is the same as the authenticator's uid"); |
| 2630 | } |
| 2631 | } |
| 2632 | |
| 2633 | private void checkAuthenticateAccountsPermission(Account account) { |
| 2634 | checkBinderPermission(Manifest.permission.AUTHENTICATE_ACCOUNTS); |
| 2635 | checkCallingUidAgainstAuthenticator(account); |
| 2636 | } |
| 2637 | |
| 2638 | private void checkReadAccountsPermission() { |
| 2639 | checkBinderPermission(Manifest.permission.GET_ACCOUNTS); |
| 2640 | } |
| 2641 | |
| 2642 | private void checkManageAccountsPermission() { |
| 2643 | checkBinderPermission(Manifest.permission.MANAGE_ACCOUNTS); |
| 2644 | } |
| 2645 | |
Fred Quintana | b38eb14 | 2010-02-24 13:40:54 -0800 | [diff] [blame] | 2646 | private void checkManageAccountsOrUseCredentialsPermissions() { |
| 2647 | checkBinderPermission(Manifest.permission.MANAGE_ACCOUNTS, |
| 2648 | Manifest.permission.USE_CREDENTIALS); |
| 2649 | } |
| 2650 | |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 2651 | private boolean canUserModifyAccounts(int callingUid) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 2652 | if (callingUid != Process.myUid()) { |
| 2653 | if (getUserManager().getUserRestrictions( |
| 2654 | new UserHandle(UserHandle.getUserId(callingUid))) |
| 2655 | .getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS)) { |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 2656 | return false; |
| 2657 | } |
| 2658 | } |
| 2659 | return true; |
| 2660 | } |
| 2661 | |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2662 | public void updateAppPermission(Account account, String authTokenType, int uid, boolean value) |
| 2663 | throws RemoteException { |
| 2664 | final int callingUid = getCallingUid(); |
| 2665 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 2666 | if (callingUid != Process.SYSTEM_UID) { |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2667 | throw new SecurityException(); |
| 2668 | } |
| 2669 | |
| 2670 | if (value) { |
| 2671 | grantAppPermission(account, authTokenType, uid); |
| 2672 | } else { |
| 2673 | revokeAppPermission(account, authTokenType, uid); |
| 2674 | } |
| 2675 | } |
| 2676 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2677 | /** |
| 2678 | * Allow callers with the given uid permission to get credentials for account/authTokenType. |
| 2679 | * <p> |
| 2680 | * Although this is public it can only be accessed via the AccountManagerService object |
| 2681 | * which is in the system. This means we don't need to protect it with permissions. |
| 2682 | * @hide |
| 2683 | */ |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2684 | private void grantAppPermission(Account account, String authTokenType, int uid) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2685 | if (account == null || authTokenType == null) { |
| 2686 | Log.e(TAG, "grantAppPermission: called with invalid arguments", new Exception()); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2687 | return; |
| 2688 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2689 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2690 | synchronized (accounts.cacheLock) { |
| 2691 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2692 | db.beginTransaction(); |
| 2693 | try { |
| 2694 | long accountId = getAccountIdLocked(db, account); |
| 2695 | if (accountId >= 0) { |
| 2696 | ContentValues values = new ContentValues(); |
| 2697 | values.put(GRANTS_ACCOUNTS_ID, accountId); |
| 2698 | values.put(GRANTS_AUTH_TOKEN_TYPE, authTokenType); |
| 2699 | values.put(GRANTS_GRANTEE_UID, uid); |
| 2700 | db.insert(TABLE_GRANTS, GRANTS_ACCOUNTS_ID, values); |
| 2701 | db.setTransactionSuccessful(); |
| 2702 | } |
| 2703 | } finally { |
| 2704 | db.endTransaction(); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2705 | } |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2706 | cancelNotification(getCredentialPermissionNotificationId(account, authTokenType, uid), |
| 2707 | new UserHandle(accounts.userId)); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2708 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2709 | } |
| 2710 | |
| 2711 | /** |
| 2712 | * Don't allow callers with the given uid permission to get credentials for |
| 2713 | * account/authTokenType. |
| 2714 | * <p> |
| 2715 | * Although this is public it can only be accessed via the AccountManagerService object |
| 2716 | * which is in the system. This means we don't need to protect it with permissions. |
| 2717 | * @hide |
| 2718 | */ |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2719 | private void revokeAppPermission(Account account, String authTokenType, int uid) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2720 | if (account == null || authTokenType == null) { |
| 2721 | Log.e(TAG, "revokeAppPermission: called with invalid arguments", new Exception()); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2722 | return; |
| 2723 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2724 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2725 | synchronized (accounts.cacheLock) { |
| 2726 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2727 | db.beginTransaction(); |
| 2728 | try { |
| 2729 | long accountId = getAccountIdLocked(db, account); |
| 2730 | if (accountId >= 0) { |
| 2731 | db.delete(TABLE_GRANTS, |
| 2732 | GRANTS_ACCOUNTS_ID + "=? AND " + GRANTS_AUTH_TOKEN_TYPE + "=? AND " |
| 2733 | + GRANTS_GRANTEE_UID + "=?", |
| 2734 | new String[]{String.valueOf(accountId), authTokenType, |
| 2735 | String.valueOf(uid)}); |
| 2736 | db.setTransactionSuccessful(); |
| 2737 | } |
| 2738 | } finally { |
| 2739 | db.endTransaction(); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2740 | } |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2741 | cancelNotification(getCredentialPermissionNotificationId(account, authTokenType, uid), |
| 2742 | new UserHandle(accounts.userId)); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2743 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2744 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2745 | |
| 2746 | static final private String stringArrayToString(String[] value) { |
| 2747 | return value != null ? ("[" + TextUtils.join(",", value) + "]") : null; |
| 2748 | } |
| 2749 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2750 | private void removeAccountFromCacheLocked(UserAccounts accounts, Account account) { |
| 2751 | final Account[] oldAccountsForType = accounts.accountCache.get(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2752 | if (oldAccountsForType != null) { |
| 2753 | ArrayList<Account> newAccountsList = new ArrayList<Account>(); |
| 2754 | for (Account curAccount : oldAccountsForType) { |
| 2755 | if (!curAccount.equals(account)) { |
| 2756 | newAccountsList.add(curAccount); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2757 | } |
| 2758 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2759 | if (newAccountsList.isEmpty()) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2760 | accounts.accountCache.remove(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2761 | } else { |
| 2762 | Account[] newAccountsForType = new Account[newAccountsList.size()]; |
| 2763 | newAccountsForType = newAccountsList.toArray(newAccountsForType); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2764 | accounts.accountCache.put(account.type, newAccountsForType); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2765 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2766 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2767 | accounts.userDataCache.remove(account); |
| 2768 | accounts.authTokenCache.remove(account); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2769 | } |
| 2770 | |
| 2771 | /** |
| 2772 | * This assumes that the caller has already checked that the account is not already present. |
| 2773 | */ |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2774 | private void insertAccountIntoCacheLocked(UserAccounts accounts, Account account) { |
| 2775 | Account[] accountsForType = accounts.accountCache.get(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2776 | int oldLength = (accountsForType != null) ? accountsForType.length : 0; |
| 2777 | Account[] newAccountsForType = new Account[oldLength + 1]; |
| 2778 | if (accountsForType != null) { |
| 2779 | System.arraycopy(accountsForType, 0, newAccountsForType, 0, oldLength); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2780 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2781 | newAccountsForType[oldLength] = account; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2782 | accounts.accountCache.put(account.type, newAccountsForType); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2783 | } |
| 2784 | |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2785 | private Account[] filterSharedAccounts(UserAccounts userAccounts, Account[] unfiltered, |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 2786 | int callingUid, String callingPackage) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2787 | if (getUserManager() == null || userAccounts == null || userAccounts.userId < 0 |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 2788 | || callingUid == Process.myUid()) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2789 | return unfiltered; |
| 2790 | } |
| 2791 | if (mUserManager.getUserInfo(userAccounts.userId).isRestricted()) { |
| 2792 | String[] packages = mPackageManager.getPackagesForUid(callingUid); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 2793 | // If any of the packages is a white listed package, return the full set, |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2794 | // otherwise return non-shared accounts only. |
| 2795 | // This might be a temporary way to specify a whitelist |
| 2796 | String whiteList = mContext.getResources().getString( |
| 2797 | com.android.internal.R.string.config_appsAuthorizedForSharedAccounts); |
| 2798 | for (String packageName : packages) { |
| 2799 | if (whiteList.contains(";" + packageName + ";")) { |
| 2800 | return unfiltered; |
| 2801 | } |
| 2802 | } |
| 2803 | ArrayList<Account> allowed = new ArrayList<Account>(); |
| 2804 | Account[] sharedAccounts = getSharedAccountsAsUser(userAccounts.userId); |
| 2805 | if (sharedAccounts == null || sharedAccounts.length == 0) return unfiltered; |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 2806 | String requiredAccountType = ""; |
| 2807 | try { |
Amith Yamasani | e342309 | 2013-05-22 19:41:45 -0700 | [diff] [blame] | 2808 | // If there's an explicit callingPackage specified, check if that package |
| 2809 | // opted in to see restricted accounts. |
| 2810 | if (callingPackage != null) { |
| 2811 | PackageInfo pi = mPackageManager.getPackageInfo(callingPackage, 0); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 2812 | if (pi != null && pi.restrictedAccountType != null) { |
| 2813 | requiredAccountType = pi.restrictedAccountType; |
Amith Yamasani | e342309 | 2013-05-22 19:41:45 -0700 | [diff] [blame] | 2814 | } |
| 2815 | } else { |
| 2816 | // Otherwise check if the callingUid has a package that has opted in |
| 2817 | for (String packageName : packages) { |
| 2818 | PackageInfo pi = mPackageManager.getPackageInfo(packageName, 0); |
| 2819 | if (pi != null && pi.restrictedAccountType != null) { |
| 2820 | requiredAccountType = pi.restrictedAccountType; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 2821 | break; |
| 2822 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2823 | } |
| 2824 | } |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 2825 | } catch (NameNotFoundException nnfe) { |
| 2826 | } |
| 2827 | for (Account account : unfiltered) { |
| 2828 | if (account.type.equals(requiredAccountType)) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2829 | allowed.add(account); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 2830 | } else { |
| 2831 | boolean found = false; |
| 2832 | for (Account shared : sharedAccounts) { |
| 2833 | if (shared.equals(account)) { |
| 2834 | found = true; |
| 2835 | break; |
| 2836 | } |
| 2837 | } |
| 2838 | if (!found) { |
| 2839 | allowed.add(account); |
| 2840 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2841 | } |
| 2842 | } |
| 2843 | Account[] filtered = new Account[allowed.size()]; |
| 2844 | allowed.toArray(filtered); |
| 2845 | return filtered; |
| 2846 | } else { |
| 2847 | return unfiltered; |
| 2848 | } |
| 2849 | } |
| 2850 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 2851 | /* |
| 2852 | * packageName can be null. If not null, it should be used to filter out restricted accounts |
| 2853 | * that the package is not allowed to access. |
| 2854 | */ |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2855 | protected Account[] getAccountsFromCacheLocked(UserAccounts userAccounts, String accountType, |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 2856 | int callingUid, String callingPackage) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2857 | if (accountType != null) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2858 | final Account[] accounts = userAccounts.accountCache.get(accountType); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2859 | if (accounts == null) { |
| 2860 | return EMPTY_ACCOUNT_ARRAY; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2861 | } else { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2862 | return filterSharedAccounts(userAccounts, Arrays.copyOf(accounts, accounts.length), |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 2863 | callingUid, callingPackage); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2864 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2865 | } else { |
| 2866 | int totalLength = 0; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2867 | for (Account[] accounts : userAccounts.accountCache.values()) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2868 | totalLength += accounts.length; |
| 2869 | } |
| 2870 | if (totalLength == 0) { |
| 2871 | return EMPTY_ACCOUNT_ARRAY; |
| 2872 | } |
| 2873 | Account[] accounts = new Account[totalLength]; |
| 2874 | totalLength = 0; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2875 | for (Account[] accountsOfType : userAccounts.accountCache.values()) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2876 | System.arraycopy(accountsOfType, 0, accounts, totalLength, |
| 2877 | accountsOfType.length); |
| 2878 | totalLength += accountsOfType.length; |
| 2879 | } |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 2880 | return filterSharedAccounts(userAccounts, accounts, callingUid, callingPackage); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2881 | } |
| 2882 | } |
| 2883 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2884 | protected void writeUserDataIntoCacheLocked(UserAccounts accounts, final SQLiteDatabase db, |
| 2885 | Account account, String key, String value) { |
| 2886 | HashMap<String, String> userDataForAccount = accounts.userDataCache.get(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2887 | if (userDataForAccount == null) { |
| 2888 | userDataForAccount = readUserDataForAccountFromDatabaseLocked(db, account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2889 | accounts.userDataCache.put(account, userDataForAccount); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2890 | } |
| 2891 | if (value == null) { |
| 2892 | userDataForAccount.remove(key); |
| 2893 | } else { |
| 2894 | userDataForAccount.put(key, value); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2895 | } |
| 2896 | } |
| 2897 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2898 | protected void writeAuthTokenIntoCacheLocked(UserAccounts accounts, final SQLiteDatabase db, |
| 2899 | Account account, String key, String value) { |
| 2900 | HashMap<String, String> authTokensForAccount = accounts.authTokenCache.get(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2901 | if (authTokensForAccount == null) { |
| 2902 | authTokensForAccount = readAuthTokensForAccountFromDatabaseLocked(db, account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2903 | accounts.authTokenCache.put(account, authTokensForAccount); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2904 | } |
| 2905 | if (value == null) { |
| 2906 | authTokensForAccount.remove(key); |
| 2907 | } else { |
| 2908 | authTokensForAccount.put(key, value); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2909 | } |
| 2910 | } |
| 2911 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2912 | protected String readAuthTokenInternal(UserAccounts accounts, Account account, |
| 2913 | String authTokenType) { |
| 2914 | synchronized (accounts.cacheLock) { |
| 2915 | HashMap<String, String> authTokensForAccount = accounts.authTokenCache.get(account); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2916 | if (authTokensForAccount == null) { |
| 2917 | // need to populate the cache for this account |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2918 | final SQLiteDatabase db = accounts.openHelper.getReadableDatabase(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2919 | authTokensForAccount = readAuthTokensForAccountFromDatabaseLocked(db, account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2920 | accounts.authTokenCache.put(account, authTokensForAccount); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2921 | } |
| 2922 | return authTokensForAccount.get(authTokenType); |
| 2923 | } |
| 2924 | } |
| 2925 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2926 | protected String readUserDataInternal(UserAccounts accounts, Account account, String key) { |
| 2927 | synchronized (accounts.cacheLock) { |
| 2928 | HashMap<String, String> userDataForAccount = accounts.userDataCache.get(account); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2929 | if (userDataForAccount == null) { |
| 2930 | // need to populate the cache for this account |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2931 | final SQLiteDatabase db = accounts.openHelper.getReadableDatabase(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2932 | userDataForAccount = readUserDataForAccountFromDatabaseLocked(db, account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2933 | accounts.userDataCache.put(account, userDataForAccount); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2934 | } |
| 2935 | return userDataForAccount.get(key); |
| 2936 | } |
| 2937 | } |
| 2938 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2939 | protected HashMap<String, String> readUserDataForAccountFromDatabaseLocked( |
| 2940 | final SQLiteDatabase db, Account account) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2941 | HashMap<String, String> userDataForAccount = new HashMap<String, String>(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2942 | Cursor cursor = db.query(TABLE_EXTRAS, |
| 2943 | COLUMNS_EXTRAS_KEY_AND_VALUE, |
| 2944 | SELECTION_USERDATA_BY_ACCOUNT, |
| 2945 | new String[]{account.name, account.type}, |
| 2946 | null, null, null); |
| 2947 | try { |
| 2948 | while (cursor.moveToNext()) { |
| 2949 | final String tmpkey = cursor.getString(0); |
| 2950 | final String value = cursor.getString(1); |
| 2951 | userDataForAccount.put(tmpkey, value); |
| 2952 | } |
| 2953 | } finally { |
| 2954 | cursor.close(); |
| 2955 | } |
| 2956 | return userDataForAccount; |
| 2957 | } |
| 2958 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2959 | protected HashMap<String, String> readAuthTokensForAccountFromDatabaseLocked( |
| 2960 | final SQLiteDatabase db, Account account) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2961 | HashMap<String, String> authTokensForAccount = new HashMap<String, String>(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2962 | Cursor cursor = db.query(TABLE_AUTHTOKENS, |
| 2963 | COLUMNS_AUTHTOKENS_TYPE_AND_AUTHTOKEN, |
| 2964 | SELECTION_AUTHTOKENS_BY_ACCOUNT, |
| 2965 | new String[]{account.name, account.type}, |
| 2966 | null, null, null); |
| 2967 | try { |
| 2968 | while (cursor.moveToNext()) { |
| 2969 | final String type = cursor.getString(0); |
| 2970 | final String authToken = cursor.getString(1); |
| 2971 | authTokensForAccount.put(type, authToken); |
| 2972 | } |
| 2973 | } finally { |
| 2974 | cursor.close(); |
| 2975 | } |
| 2976 | return authTokensForAccount; |
| 2977 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2978 | } |