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