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