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