blob: 75523680d33540b0d9136d953c13f3f614a268f6 [file] [log] [blame]
Fred Quintana60307342009-03-24 22:48:12 -07001/*
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 Sharkey7a96c392012-11-15 14:01:46 -080017package com.android.server.accounts;
Fred Quintana60307342009-03-24 22:48:12 -070018
Jeff Sharkey7a96c392012-11-15 14:01:46 -080019import android.accounts.AccountManager;
20import android.accounts.AuthenticatorDescription;
21import android.accounts.IAccountAuthenticator;
Jeff Sharkey6ab72d72012-10-08 16:44:37 -070022import android.content.Context;
Fred Quintana60307342009-03-24 22:48:12 -070023import android.content.pm.PackageManager;
Fred Quintana718d8a22009-04-29 17:53:20 -070024import android.content.pm.RegisteredServicesCache;
Fred Quintana5ebbb4a2009-11-09 15:42:20 -080025import android.content.pm.XmlSerializerAndParser;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -080026import android.content.res.Resources;
Fred Quintanaa698f422009-04-08 19:14:54 -070027import android.content.res.TypedArray;
Fred Quintanac298a852009-08-27 18:28:17 -070028import android.text.TextUtils;
Jeff Sharkey6ab72d72012-10-08 16:44:37 -070029import android.util.AttributeSet;
30
Fred Quintana5ebbb4a2009-11-09 15:42:20 -080031import org.xmlpull.v1.XmlPullParser;
Fred Quintana5ebbb4a2009-11-09 15:42:20 -080032import org.xmlpull.v1.XmlPullParserException;
Jeff Sharkey6ab72d72012-10-08 16:44:37 -070033import org.xmlpull.v1.XmlSerializer;
Fred Quintana5ebbb4a2009-11-09 15:42:20 -080034
35import java.io.IOException;
Fred Quintana60307342009-03-24 22:48:12 -070036
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 Quintana718d8a22009-04-29 17:53:20 -070041 * are made available via the {@link RegisteredServicesCache#getServiceInfo} method.
42 * @hide
Fred Quintana60307342009-03-24 22:48:12 -070043 */
Fred Quintana97889762009-06-15 12:29:24 -070044/* package private */ class AccountAuthenticatorCache
Costin Manolachea40c6302010-12-13 14:50:45 -080045 extends RegisteredServicesCache<AuthenticatorDescription>
Fred Quintana56285a62010-12-02 14:20:51 -080046 implements IAccountAuthenticatorCache {
Fred Quintana60307342009-03-24 22:48:12 -070047 private static final String TAG = "Account";
Fred Quintana5ebbb4a2009-11-09 15:42:20 -080048 private static final MySerializer sSerializer = new MySerializer();
Fred Quintana60307342009-03-24 22:48:12 -070049
Fred Quintana60307342009-03-24 22:48:12 -070050 public AccountAuthenticatorCache(Context context) {
Fred Quintanaf7ae77c2009-10-02 17:19:31 -070051 super(context, AccountManager.ACTION_AUTHENTICATOR_INTENT,
Fred Quintana5ebbb4a2009-11-09 15:42:20 -080052 AccountManager.AUTHENTICATOR_META_DATA_NAME,
53 AccountManager.AUTHENTICATOR_ATTRIBUTES_NAME, sSerializer);
Fred Quintana60307342009-03-24 22:48:12 -070054 }
55
Dianne Hackborn20cb56e2010-03-04 00:58:29 -080056 public AuthenticatorDescription parseServiceAttributes(Resources res,
57 String packageName, AttributeSet attrs) {
58 TypedArray sa = res.obtainAttributes(attrs,
Fred Quintana718d8a22009-04-29 17:53:20 -070059 com.android.internal.R.styleable.AccountAuthenticator);
Fred Quintana60307342009-03-24 22:48:12 -070060 try {
Fred Quintana97889762009-06-15 12:29:24 -070061 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 Miller70e1ad72009-09-09 22:45:47 -070067 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 Manolachea40c6302010-12-13 14:50:45 -080071 final boolean customTokens = sa.getBoolean(
72 com.android.internal.R.styleable.AccountAuthenticator_customTokens, false);
Fred Quintanac298a852009-08-27 18:28:17 -070073 if (TextUtils.isEmpty(accountType)) {
74 return null;
75 }
Costin Manolachea40c6302010-12-13 14:50:45 -080076 return new AuthenticatorDescription(accountType, packageName, labelId, iconId,
77 smallIconId, prefId, customTokens);
Fred Quintana60307342009-03-24 22:48:12 -070078 } finally {
Fred Quintana718d8a22009-04-29 17:53:20 -070079 sa.recycle();
Fred Quintana60307342009-03-24 22:48:12 -070080 }
Fred Quintana60307342009-03-24 22:48:12 -070081 }
Fred Quintana5ebbb4a2009-11-09 15:42:20 -080082
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 Quintana60307342009-03-24 22:48:12 -070094}