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