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 | |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 17 | package com.android.server.accounts; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 18 | |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 19 | import android.accounts.AccountManager; |
| 20 | import android.accounts.AuthenticatorDescription; |
| 21 | import android.accounts.IAccountAuthenticator; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 22 | import android.content.Context; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 23 | import android.content.pm.PackageManager; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 24 | import android.content.pm.RegisteredServicesCache; |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 25 | import android.content.pm.XmlSerializerAndParser; |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 26 | import android.content.res.Resources; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 27 | import android.content.res.TypedArray; |
Fred Quintana | c298a85 | 2009-08-27 18:28:17 -0700 | [diff] [blame] | 28 | import android.text.TextUtils; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 29 | import android.util.AttributeSet; |
| 30 | |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 31 | import org.xmlpull.v1.XmlPullParser; |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 32 | import org.xmlpull.v1.XmlPullParserException; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 33 | import org.xmlpull.v1.XmlSerializer; |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 34 | |
| 35 | import java.io.IOException; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * A cache of services that export the {@link IAccountAuthenticator} interface. This cache |
| 39 | * is built by interrogating the {@link PackageManager} and is updated as packages are added, |
| 40 | * removed and changed. The authenticators are referred to by their account type and |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 41 | * are made available via the {@link RegisteredServicesCache#getServiceInfo} method. |
| 42 | * @hide |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 43 | */ |
Fred Quintana | 9788976 | 2009-06-15 12:29:24 -0700 | [diff] [blame] | 44 | /* package private */ class AccountAuthenticatorCache |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 45 | extends RegisteredServicesCache<AuthenticatorDescription> |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 46 | implements IAccountAuthenticatorCache { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 47 | private static final String TAG = "Account"; |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 48 | private static final MySerializer sSerializer = new MySerializer(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 49 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 50 | public AccountAuthenticatorCache(Context context) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 51 | super(context, AccountManager.ACTION_AUTHENTICATOR_INTENT, |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 52 | AccountManager.AUTHENTICATOR_META_DATA_NAME, |
| 53 | AccountManager.AUTHENTICATOR_ATTRIBUTES_NAME, sSerializer); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 56 | public AuthenticatorDescription parseServiceAttributes(Resources res, |
| 57 | String packageName, AttributeSet attrs) { |
| 58 | TypedArray sa = res.obtainAttributes(attrs, |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 59 | com.android.internal.R.styleable.AccountAuthenticator); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 60 | try { |
Fred Quintana | 9788976 | 2009-06-15 12:29:24 -0700 | [diff] [blame] | 61 | final String accountType = |
| 62 | sa.getString(com.android.internal.R.styleable.AccountAuthenticator_accountType); |
| 63 | final int labelId = sa.getResourceId( |
| 64 | com.android.internal.R.styleable.AccountAuthenticator_label, 0); |
| 65 | final int iconId = sa.getResourceId( |
| 66 | com.android.internal.R.styleable.AccountAuthenticator_icon, 0); |
Jim Miller | 70e1ad7 | 2009-09-09 22:45:47 -0700 | [diff] [blame] | 67 | final int smallIconId = sa.getResourceId( |
| 68 | com.android.internal.R.styleable.AccountAuthenticator_smallIcon, 0); |
| 69 | final int prefId = sa.getResourceId( |
| 70 | com.android.internal.R.styleable.AccountAuthenticator_accountPreferences, 0); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 71 | final boolean customTokens = sa.getBoolean( |
| 72 | com.android.internal.R.styleable.AccountAuthenticator_customTokens, false); |
Fred Quintana | c298a85 | 2009-08-27 18:28:17 -0700 | [diff] [blame] | 73 | if (TextUtils.isEmpty(accountType)) { |
| 74 | return null; |
| 75 | } |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 76 | return new AuthenticatorDescription(accountType, packageName, labelId, iconId, |
| 77 | smallIconId, prefId, customTokens); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 78 | } finally { |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 79 | sa.recycle(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 80 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 81 | } |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 82 | |
| 83 | private static class MySerializer implements XmlSerializerAndParser<AuthenticatorDescription> { |
| 84 | public void writeAsXml(AuthenticatorDescription item, XmlSerializer out) |
| 85 | throws IOException { |
| 86 | out.attribute(null, "type", item.type); |
| 87 | } |
| 88 | |
| 89 | public AuthenticatorDescription createFromXml(XmlPullParser parser) |
| 90 | throws IOException, XmlPullParserException { |
| 91 | return AuthenticatorDescription.newKey(parser.getAttributeValue(null, "type")); |
| 92 | } |
| 93 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 94 | } |