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