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