Home | History | Annotate | Download | only in accounts
      1 /*
      2  * Copyright (C) 2014 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 com.android.tv.settings.accounts;
     18 
     19 import android.accounts.Account;
     20 import android.accounts.AccountManager;
     21 import android.accounts.AuthenticatorDescription;
     22 import android.content.Context;
     23 import android.content.pm.PackageManager;
     24 import android.content.res.Resources;
     25 import android.graphics.drawable.Drawable;
     26 import android.util.Log;
     27 
     28 import java.util.ArrayList;
     29 import java.util.HashMap;
     30 import java.util.Map;
     31 
     32 public class AuthenticatorHelper {
     33 
     34     private static final String TAG = "AuthenticatorHelper";
     35     private final Map<String, AuthenticatorDescription> mTypeToAuthDescription = new HashMap<>();
     36     private AuthenticatorDescription[] mAuthDescs;
     37     private final ArrayList<String> mEnabledAccountTypes = new ArrayList<>();
     38     private final Map<String, Drawable> mAccTypeIconCache = new HashMap<>();
     39 
     40     public AuthenticatorHelper() {
     41     }
     42 
     43     public String[] getEnabledAccountTypes() {
     44         return mEnabledAccountTypes.toArray(new String[mEnabledAccountTypes.size()]);
     45     }
     46 
     47     /**
     48      * Gets an icon associated with a particular account type. If none found, return null.
     49      * @param accountType the type of account
     50      * @return a drawable for the icon or null if one cannot be found.
     51      */
     52     public Drawable getDrawableForType(Context context, final String accountType) {
     53         Drawable icon = null;
     54         if (mAccTypeIconCache.containsKey(accountType)) {
     55             return mAccTypeIconCache.get(accountType);
     56         }
     57         if (mTypeToAuthDescription.containsKey(accountType)) {
     58             try {
     59                 AuthenticatorDescription desc = mTypeToAuthDescription.get(accountType);
     60                 Context authContext = context.createPackageContext(desc.packageName, 0);
     61                 icon = authContext.getResources().getDrawable(desc.iconId);
     62                 mAccTypeIconCache.put(accountType, icon);
     63             } catch (PackageManager.NameNotFoundException e) {
     64             } catch (Resources.NotFoundException e) {
     65             }
     66         }
     67         if (icon == null) {
     68             icon = context.getPackageManager().getDefaultActivityIcon();
     69         }
     70         return icon;
     71     }
     72 
     73     /**
     74      * Gets the label associated with a particular account type. If none found, return null.
     75      * @param accountType the type of account
     76      * @return a CharSequence for the label or null if one cannot be found.
     77      */
     78     public CharSequence getLabelForType(Context context, final String accountType) {
     79         CharSequence label = null;
     80         if (mTypeToAuthDescription.containsKey(accountType)) {
     81             try {
     82                 AuthenticatorDescription desc = mTypeToAuthDescription.get(accountType);
     83                 Context authContext = context.createPackageContext(desc.packageName, 0);
     84                 label = authContext.getResources().getText(desc.labelId);
     85             } catch (PackageManager.NameNotFoundException e) {
     86                 Log.w(TAG, "No label name for account type " + accountType);
     87             } catch (Resources.NotFoundException e) {
     88                 Log.w(TAG, "No label icon for account type " + accountType);
     89             }
     90         }
     91         return label;
     92     }
     93 
     94     /**
     95      * Updates provider icons. Subclasses should call this in onCreate()
     96      * and update any UI that depends on AuthenticatorDescriptions in onAuthDescriptionsUpdated().
     97      */
     98     public void updateAuthDescriptions(Context context) {
     99         mAuthDescs = AccountManager.get(context).getAuthenticatorTypes();
    100         for (int i = 0; i < mAuthDescs.length; i++) {
    101             mTypeToAuthDescription.put(mAuthDescs[i].type, mAuthDescs[i]);
    102         }
    103     }
    104 
    105     public void onAccountsUpdated(Context context, Account[] accounts) {
    106         if (accounts == null) {
    107             accounts = AccountManager.get(context).getAccounts();
    108         }
    109         mEnabledAccountTypes.clear();
    110         mAccTypeIconCache.clear();
    111         for (Account account: accounts) {
    112             if (!mEnabledAccountTypes.contains(account.type)) {
    113                 mEnabledAccountTypes.add(account.type);
    114             }
    115         }
    116     }
    117 
    118     public boolean containsAccountType(String accountType) {
    119         return mTypeToAuthDescription.containsKey(accountType);
    120     }
    121 
    122     public AuthenticatorDescription getAccountTypeDescription(String accountType) {
    123         return mTypeToAuthDescription.get(accountType);
    124     }
    125 
    126     public boolean hasAccountPreferences(final String accountType) {
    127         if (containsAccountType(accountType)) {
    128             AuthenticatorDescription desc = getAccountTypeDescription(accountType);
    129             if (desc != null && desc.accountPreferencesId != 0) {
    130                 return true;
    131             }
    132         }
    133         return false;
    134     }
    135 }
    136