• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.android.server.accounts;
18 
19 import android.accounts.AccountManager;
20 import android.accounts.AuthenticatorDescription;
21 import android.accounts.IAccountAuthenticator;
22 import android.content.Context;
23 import android.content.pm.PackageManager;
24 import android.content.pm.RegisteredServicesCache;
25 import android.content.pm.XmlSerializerAndParser;
26 import android.content.res.Resources;
27 import android.content.res.TypedArray;
28 import android.text.TextUtils;
29 import android.util.AttributeSet;
30 
31 import org.xmlpull.v1.XmlPullParser;
32 import org.xmlpull.v1.XmlPullParserException;
33 import org.xmlpull.v1.XmlSerializer;
34 
35 import java.io.IOException;
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
41  * are made available via the {@link RegisteredServicesCache#getServiceInfo} method.
42  * @hide
43  */
44 /* package private */ class AccountAuthenticatorCache
45         extends RegisteredServicesCache<AuthenticatorDescription>
46         implements IAccountAuthenticatorCache {
47     private static final String TAG = "Account";
48     private static final MySerializer sSerializer = new MySerializer();
49 
AccountAuthenticatorCache(Context context)50     public AccountAuthenticatorCache(Context context) {
51         super(context, AccountManager.ACTION_AUTHENTICATOR_INTENT,
52                 AccountManager.AUTHENTICATOR_META_DATA_NAME,
53                 AccountManager.AUTHENTICATOR_ATTRIBUTES_NAME, sSerializer);
54     }
55 
parseServiceAttributes(Resources res, String packageName, AttributeSet attrs)56     public AuthenticatorDescription parseServiceAttributes(Resources res,
57             String packageName, AttributeSet attrs) {
58         TypedArray sa = res.obtainAttributes(attrs,
59                 com.android.internal.R.styleable.AccountAuthenticator);
60         try {
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);
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);
71             final boolean customTokens = sa.getBoolean(
72                     com.android.internal.R.styleable.AccountAuthenticator_customTokens, false);
73             if (TextUtils.isEmpty(accountType)) {
74                 return null;
75             }
76             return new AuthenticatorDescription(accountType, packageName, labelId, iconId,
77                     smallIconId, prefId, customTokens);
78         } finally {
79             sa.recycle();
80         }
81     }
82 
83     private static class MySerializer implements XmlSerializerAndParser<AuthenticatorDescription> {
writeAsXml(AuthenticatorDescription item, XmlSerializer out)84         public void writeAsXml(AuthenticatorDescription item, XmlSerializer out)
85                 throws IOException {
86             out.attribute(null, "type", item.type);
87         }
88 
createFromXml(XmlPullParser parser)89         public AuthenticatorDescription createFromXml(XmlPullParser parser)
90                 throws IOException, XmlPullParserException {
91             return AuthenticatorDescription.newKey(parser.getAttributeValue(null, "type"));
92         }
93     }
94 }
95