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