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