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