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