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