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 com.android.server.accounts; 18 19import android.accounts.AccountManager; 20import android.accounts.AuthenticatorDescription; 21import android.accounts.IAccountAuthenticator; 22import android.content.Context; 23import android.content.pm.PackageManager; 24import android.content.pm.RegisteredServicesCache; 25import android.content.pm.XmlSerializerAndParser; 26import android.content.res.Resources; 27import android.content.res.TypedArray; 28import android.text.TextUtils; 29import android.util.AttributeSet; 30 31import org.xmlpull.v1.XmlPullParser; 32import org.xmlpull.v1.XmlPullParserException; 33import org.xmlpull.v1.XmlSerializer; 34 35import 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 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 56 @Override 57 public AuthenticatorDescription parseServiceAttributes(Resources res, 58 String packageName, AttributeSet attrs) { 59 TypedArray sa = res.obtainAttributes(attrs, 60 com.android.internal.R.styleable.AccountAuthenticator); 61 try { 62 final String accountType = 63 sa.getString(com.android.internal.R.styleable.AccountAuthenticator_accountType); 64 final int labelId = sa.getResourceId( 65 com.android.internal.R.styleable.AccountAuthenticator_label, 0); 66 final int iconId = sa.getResourceId( 67 com.android.internal.R.styleable.AccountAuthenticator_icon, 0); 68 final int smallIconId = sa.getResourceId( 69 com.android.internal.R.styleable.AccountAuthenticator_smallIcon, 0); 70 final int prefId = sa.getResourceId( 71 com.android.internal.R.styleable.AccountAuthenticator_accountPreferences, 0); 72 final boolean customTokens = sa.getBoolean( 73 com.android.internal.R.styleable.AccountAuthenticator_customTokens, false); 74 if (TextUtils.isEmpty(accountType)) { 75 return null; 76 } 77 return new AuthenticatorDescription(accountType, packageName, labelId, iconId, 78 smallIconId, prefId, customTokens); 79 } finally { 80 sa.recycle(); 81 } 82 } 83 84 private static class MySerializer implements XmlSerializerAndParser<AuthenticatorDescription> { 85 @Override 86 public void writeAsXml(AuthenticatorDescription item, XmlSerializer out) 87 throws IOException { 88 out.attribute(null, "type", item.type); 89 } 90 91 @Override 92 public AuthenticatorDescription createFromXml(XmlPullParser parser) 93 throws IOException, XmlPullParserException { 94 return AuthenticatorDescription.newKey(parser.getAttributeValue(null, "type")); 95 } 96 } 97} 98