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.ResolveInfo;
     22 import android.content.pm.XmlSerializerAndParser;
     23 import android.content.res.Resources;
     24 import android.content.res.TypedArray;
     25 import android.content.ComponentName;
     26 import android.content.Context;
     27 import android.content.Intent;
     28 import android.util.AttributeSet;
     29 import android.util.Log;
     30 import android.text.TextUtils;
     31 import org.xmlpull.v1.XmlPullParser;
     32 import org.xmlpull.v1.XmlSerializer;
     33 import org.xmlpull.v1.XmlPullParserException;
     34 
     35 import java.io.IOException;
     36 import java.util.List;
     37 
     38 /**
     39  * A cache of services that export the {@link IAccountAuthenticator} interface. This cache
     40  * is built by interrogating the {@link PackageManager} and is updated as packages are added,
     41  * removed and changed. The authenticators are referred to by their account type and
     42  * are made available via the {@link RegisteredServicesCache#getServiceInfo} method.
     43  * @hide
     44  */
     45 /* package private */ class AccountAuthenticatorCache
     46         extends RegisteredServicesCache<AuthenticatorDescription> {
     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     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 
     72             boolean customTokens = false;
     73             try {
     74                 // In HC this will be an attribute in authenticator.xml, this is a workaround
     75                 // using meta-data to avoid changes to the API.
     76                 // If meta-data is absent the old behavior is preserved.
     77                 // Authenticator will know if AccountManager supports customTokens or not.
     78                 PackageManager pm = mContext.getPackageManager();
     79                 List<ResolveInfo> resolveInfos = pm.queryIntentServices(
     80                         new Intent(AccountManager.ACTION_AUTHENTICATOR_INTENT),
     81                         PackageManager.GET_META_DATA);
     82                 for (ResolveInfo resolveInfo: resolveInfos) {
     83                     android.content.pm.ServiceInfo si = resolveInfo.serviceInfo;
     84                     if (!packageName.equals(si.packageName)) {
     85                         continue;
     86                     }
     87                     Object ctString = si.metaData.get(AccountManager.ACTION_AUTHENTICATOR_INTENT
     88                             + ".customTokens");
     89                     if (ctString != null) {
     90                         customTokens = true;
     91                     }
     92                 }
     93             } catch (Throwable t) {
     94                 // Protected against invalid data in meta or unexpected
     95                 // conditions - the authenticator will not have the new
     96                 // features.
     97                 Log.e(TAG, "Error getting customTokens metadata " + t);
     98             }
     99 
    100             if (TextUtils.isEmpty(accountType)) {
    101                 return null;
    102             }
    103             return new AuthenticatorDescription(accountType, packageName, labelId, iconId,
    104                     smallIconId, prefId, customTokens);
    105         } finally {
    106             sa.recycle();
    107         }
    108     }
    109 
    110     private static class MySerializer implements XmlSerializerAndParser<AuthenticatorDescription> {
    111         public void writeAsXml(AuthenticatorDescription item, XmlSerializer out)
    112                 throws IOException {
    113             out.attribute(null, "type", item.type);
    114         }
    115 
    116         public AuthenticatorDescription createFromXml(XmlPullParser parser)
    117                 throws IOException, XmlPullParserException {
    118             return AuthenticatorDescription.newKey(parser.getAttributeValue(null, "type"));
    119         }
    120     }
    121 }
    122