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