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