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