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     private static final String TAG = "Account";
     43     private static final MySerializer sSerializer = new MySerializer();
     44 
     45     public AccountAuthenticatorCache(Context context) {
     46         super(context, AccountManager.ACTION_AUTHENTICATOR_INTENT,
     47                 AccountManager.AUTHENTICATOR_META_DATA_NAME,
     48                 AccountManager.AUTHENTICATOR_ATTRIBUTES_NAME, sSerializer);
     49     }
     50 
     51     public AuthenticatorDescription parseServiceAttributes(Resources res,
     52             String packageName, AttributeSet attrs) {
     53         TypedArray sa = res.obtainAttributes(attrs,
     54                 com.android.internal.R.styleable.AccountAuthenticator);
     55         try {
     56             final String accountType =
     57                     sa.getString(com.android.internal.R.styleable.AccountAuthenticator_accountType);
     58             final int labelId = sa.getResourceId(
     59                     com.android.internal.R.styleable.AccountAuthenticator_label, 0);
     60             final int iconId = sa.getResourceId(
     61                     com.android.internal.R.styleable.AccountAuthenticator_icon, 0);
     62             final int smallIconId = sa.getResourceId(
     63                     com.android.internal.R.styleable.AccountAuthenticator_smallIcon, 0);
     64             final int prefId = sa.getResourceId(
     65                     com.android.internal.R.styleable.AccountAuthenticator_accountPreferences, 0);
     66             if (TextUtils.isEmpty(accountType)) {
     67                 return null;
     68             }
     69             return new AuthenticatorDescription(accountType, packageName, labelId, iconId,
     70                     smallIconId, prefId);
     71         } finally {
     72             sa.recycle();
     73         }
     74     }
     75 
     76     private static class MySerializer implements XmlSerializerAndParser<AuthenticatorDescription> {
     77         public void writeAsXml(AuthenticatorDescription item, XmlSerializer out)
     78                 throws IOException {
     79             out.attribute(null, "type", item.type);
     80         }
     81 
     82         public AuthenticatorDescription createFromXml(XmlPullParser parser)
     83                 throws IOException, XmlPullParserException {
     84             return AuthenticatorDescription.newKey(parser.getAttributeValue(null, "type"));
     85         }
     86     }
     87 }
     88