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