143c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani/*
243c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani * Copyright (C) 2010 The Android Open Source Project
343c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani *
443c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani * Licensed under the Apache License, Version 2.0 (the "License");
543c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani * you may not use this file except in compliance with the License.
643c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani * You may obtain a copy of the License at
743c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani *
843c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani *      http://www.apache.org/licenses/LICENSE-2.0
943c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani *
1043c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani * Unless required by applicable law or agreed to in writing, software
1143c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani * distributed under the License is distributed on an "AS IS" BASIS,
1243c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1343c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani * See the License for the specific language governing permissions and
1443c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani * limitations under the License.
1543c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani */
1643c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani
1743c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasanipackage com.android.settings.accounts;
1843c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani
1943c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasaniimport android.accounts.AccountManager;
2043c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasaniimport android.accounts.AuthenticatorDescription;
2143c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasaniimport android.content.ContentResolver;
2243c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasaniimport android.content.Context;
2343c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasaniimport android.content.Intent;
2443c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasaniimport android.content.SyncAdapterType;
2543c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasaniimport android.content.pm.PackageManager;
26d26321eecb481528c5e54a608f2f3c4cc26d8a77Brian Carlstromimport android.content.res.Resources;
2743c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasaniimport android.graphics.drawable.Drawable;
2843c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasaniimport android.os.Bundle;
2943c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasaniimport android.preference.Preference;
3043c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasaniimport android.preference.PreferenceActivity;
3143c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasaniimport android.preference.PreferenceGroup;
3243c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasaniimport android.preference.PreferenceScreen;
3343c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasaniimport android.util.Log;
34ab58d933a7cb00261f9746a9361447ad23d75ee4Fabrice Di Meglioimport com.android.internal.util.CharSequences;
35ab58d933a7cb00261f9746a9361447ad23d75ee4Fabrice Di Meglioimport com.android.settings.R;
36ab58d933a7cb00261f9746a9361447ad23d75ee4Fabrice Di Meglioimport com.google.android.collect.Maps;
3743c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani
3843c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasaniimport java.util.ArrayList;
39ab58d933a7cb00261f9746a9361447ad23d75ee4Fabrice Di Meglioimport java.util.Collections;
4043c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasaniimport java.util.HashMap;
4143c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasaniimport java.util.HashSet;
4243c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasaniimport java.util.Map;
4343c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani
4443c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani/**
4543c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani * Activity asking a user to select an account to be set up.
4643c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani */
4743c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasanipublic class ChooseAccountActivity extends PreferenceActivity {
4843c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani
4943c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani    private static final String TAG = "ChooseAccountActivity";
5043c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani    private String[] mAuthorities;
5143c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani    private PreferenceGroup mAddAccountGroup;
5243c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani    private final ArrayList<ProviderEntry> mProviderList = new ArrayList<ProviderEntry>();
5343c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani    public HashSet<String> mAccountTypesFilter;
5443c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani    private AuthenticatorDescription[] mAuthDescs;
5543c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani    private HashMap<String, ArrayList<String>> mAccountTypeToAuthorities = null;
5643c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani    private Map<String, AuthenticatorDescription> mTypeToAuthDescription
5743c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            = new HashMap<String, AuthenticatorDescription>();
5843c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani
59ab58d933a7cb00261f9746a9361447ad23d75ee4Fabrice Di Meglio    private static class ProviderEntry implements Comparable<ProviderEntry> {
6043c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        private final CharSequence name;
6143c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        private final String type;
6243c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        ProviderEntry(CharSequence providerName, String accountType) {
6343c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            name = providerName;
6443c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            type = accountType;
6543c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        }
66ab58d933a7cb00261f9746a9361447ad23d75ee4Fabrice Di Meglio
67ab58d933a7cb00261f9746a9361447ad23d75ee4Fabrice Di Meglio        public int compareTo(ProviderEntry another) {
68ab58d933a7cb00261f9746a9361447ad23d75ee4Fabrice Di Meglio            if (name == null) {
69ab58d933a7cb00261f9746a9361447ad23d75ee4Fabrice Di Meglio                return -1;
70ab58d933a7cb00261f9746a9361447ad23d75ee4Fabrice Di Meglio            }
71ab58d933a7cb00261f9746a9361447ad23d75ee4Fabrice Di Meglio            if (another.name == null) {
72ab58d933a7cb00261f9746a9361447ad23d75ee4Fabrice Di Meglio                return +1;
73ab58d933a7cb00261f9746a9361447ad23d75ee4Fabrice Di Meglio            }
74ab58d933a7cb00261f9746a9361447ad23d75ee4Fabrice Di Meglio            return CharSequences.compareToIgnoreCase(name, another.name);
75ab58d933a7cb00261f9746a9361447ad23d75ee4Fabrice Di Meglio        }
7643c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani    }
7743c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani
7843c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani    @Override
7943c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani    protected void onCreate(Bundle icicle) {
8043c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        super.onCreate(icicle);
8143c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani
8243c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        setContentView(R.layout.add_account_screen);
8343c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        addPreferencesFromResource(R.xml.add_account_settings);
8443c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        mAuthorities = getIntent().getStringArrayExtra(
8543c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                AccountPreferenceBase.AUTHORITIES_FILTER_KEY);
8643c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        String[] accountTypesFilter = getIntent().getStringArrayExtra(
8743c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                AccountPreferenceBase.ACCOUNT_TYPES_FILTER_KEY);
8843c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        if (accountTypesFilter != null) {
8943c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            mAccountTypesFilter = new HashSet<String>();
9043c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            for (String accountType : accountTypesFilter) {
9143c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                mAccountTypesFilter.add(accountType);
9243c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            }
9343c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        }
9443c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        mAddAccountGroup = getPreferenceScreen();
9543c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        updateAuthDescriptions();
9643c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani    }
9743c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani
9843c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani    /**
9943c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani     * Updates provider icons. Subclasses should call this in onCreate()
10043c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani     * and update any UI that depends on AuthenticatorDescriptions in onAuthDescriptionsUpdated().
10143c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani     */
10243c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani    private void updateAuthDescriptions() {
10343c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        mAuthDescs = AccountManager.get(this).getAuthenticatorTypes();
10443c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        for (int i = 0; i < mAuthDescs.length; i++) {
10543c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            mTypeToAuthDescription.put(mAuthDescs[i].type, mAuthDescs[i]);
10643c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        }
10743c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        onAuthDescriptionsUpdated();
10843c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani    }
10943c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani
11043c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani    private void onAuthDescriptionsUpdated() {
11143c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        // Create list of providers to show on preference screen
11243c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        for (int i = 0; i < mAuthDescs.length; i++) {
11343c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            String accountType = mAuthDescs[i].type;
11443c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            CharSequence providerName = getLabelForType(accountType);
11543c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani
11643c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            // Skip preferences for authorities not specified. If no authorities specified,
11743c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            // then include them all.
11843c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            ArrayList<String> accountAuths = getAuthoritiesForAccountType(accountType);
11943c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            boolean addAccountPref = true;
12043c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            if (mAuthorities != null && mAuthorities.length > 0 && accountAuths != null) {
12143c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                addAccountPref = false;
12243c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                for (int k = 0; k < mAuthorities.length; k++) {
12343c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                    if (accountAuths.contains(mAuthorities[k])) {
12443c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                        addAccountPref = true;
12543c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                        break;
12643c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                    }
12743c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                }
12843c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            }
12943c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            if (addAccountPref && mAccountTypesFilter != null
13043c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                    && !mAccountTypesFilter.contains(accountType)) {
13143c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                addAccountPref = false;
13243c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            }
13343c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            if (addAccountPref) {
13443c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                mProviderList.add(new ProviderEntry(providerName, accountType));
13543c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            } else {
13643c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                if (Log.isLoggable(TAG, Log.VERBOSE)) {
13743c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                    Log.v(TAG, "Skipped pref " + providerName + ": has no authority we need");
13843c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                }
13943c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            }
14043c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        }
14143c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani
14243c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        if (mProviderList.size() == 1) {
14343c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            // If there's only one provider that matches, just run it.
14443c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            finishWithAccountType(mProviderList.get(0).type);
14543c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        } else if (mProviderList.size() > 0) {
146ab58d933a7cb00261f9746a9361447ad23d75ee4Fabrice Di Meglio            Collections.sort(mProviderList);
14743c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            mAddAccountGroup.removeAll();
14843c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            for (ProviderEntry pref : mProviderList) {
14943c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                Drawable drawable = getDrawableForType(pref.type);
15043c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                ProviderPreference p =
15143c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                        new ProviderPreference(this, pref.type, drawable, pref.name);
15243c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                mAddAccountGroup.addPreference(p);
15343c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            }
15443c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        } else {
15543c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            if (Log.isLoggable(TAG, Log.VERBOSE)) {
15643c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                final StringBuilder auths = new StringBuilder();
15743c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                for (String a : mAuthorities) {
15843c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                    auths.append(a);
15943c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                    auths.append(' ');
16043c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                }
16143c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                Log.v(TAG, "No providers found for authorities: " + auths);
16243c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            }
16343c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            setResult(RESULT_CANCELED);
16443c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            finish();
16543c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        }
16643c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani    }
16743c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani
16843c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani    public ArrayList<String> getAuthoritiesForAccountType(String type) {
16943c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        if (mAccountTypeToAuthorities == null) {
17043c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            mAccountTypeToAuthorities = Maps.newHashMap();
17143c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            SyncAdapterType[] syncAdapters = ContentResolver.getSyncAdapterTypes();
17243c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            for (int i = 0, n = syncAdapters.length; i < n; i++) {
17343c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                final SyncAdapterType sa = syncAdapters[i];
17443c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                ArrayList<String> authorities = mAccountTypeToAuthorities.get(sa.accountType);
17543c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                if (authorities == null) {
17643c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                    authorities = new ArrayList<String>();
17743c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                    mAccountTypeToAuthorities.put(sa.accountType, authorities);
17843c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                }
17943c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                if (Log.isLoggable(TAG, Log.VERBOSE)) {
18043c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                    Log.d(TAG, "added authority " + sa.authority + " to accountType "
18143c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                            + sa.accountType);
18243c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                }
18343c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                authorities.add(sa.authority);
18443c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            }
18543c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        }
18643c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        return mAccountTypeToAuthorities.get(type);
18743c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani    }
18843c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani
18943c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani    /**
19043c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani     * Gets an icon associated with a particular account type. If none found, return null.
19143c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani     * @param accountType the type of account
19243c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani     * @return a drawable for the icon or null if one cannot be found.
19343c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani     */
19443c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani    protected Drawable getDrawableForType(final String accountType) {
19543c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        Drawable icon = null;
19643c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        if (mTypeToAuthDescription.containsKey(accountType)) {
19743c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            try {
19843c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                AuthenticatorDescription desc = mTypeToAuthDescription.get(accountType);
19943c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                Context authContext = createPackageContext(desc.packageName, 0);
20043c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                icon = authContext.getResources().getDrawable(desc.iconId);
20143c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            } catch (PackageManager.NameNotFoundException e) {
20243c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                // TODO: place holder icon for missing account icons?
203d26321eecb481528c5e54a608f2f3c4cc26d8a77Brian Carlstrom                Log.w(TAG, "No icon name for account type " + accountType);
204d26321eecb481528c5e54a608f2f3c4cc26d8a77Brian Carlstrom            } catch (Resources.NotFoundException e) {
205d26321eecb481528c5e54a608f2f3c4cc26d8a77Brian Carlstrom                // TODO: place holder icon for missing account icons?
206d26321eecb481528c5e54a608f2f3c4cc26d8a77Brian Carlstrom                Log.w(TAG, "No icon resource for account type " + accountType);
20743c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            }
20843c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        }
20943c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        return icon;
21043c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani    }
21143c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani
21243c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani    /**
21343c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani     * Gets the label associated with a particular account type. If none found, return null.
21443c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani     * @param accountType the type of account
21543c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani     * @return a CharSequence for the label or null if one cannot be found.
21643c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani     */
21743c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani    protected CharSequence getLabelForType(final String accountType) {
21843c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        CharSequence label = null;
21943c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        if (mTypeToAuthDescription.containsKey(accountType)) {
22043c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            try {
22143c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                AuthenticatorDescription desc = mTypeToAuthDescription.get(accountType);
22243c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                Context authContext = createPackageContext(desc.packageName, 0);
22343c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                label = authContext.getResources().getText(desc.labelId);
22443c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            } catch (PackageManager.NameNotFoundException e) {
225d26321eecb481528c5e54a608f2f3c4cc26d8a77Brian Carlstrom                Log.w(TAG, "No label name for account type " + accountType);
226d26321eecb481528c5e54a608f2f3c4cc26d8a77Brian Carlstrom            } catch (Resources.NotFoundException e) {
227d26321eecb481528c5e54a608f2f3c4cc26d8a77Brian Carlstrom                Log.w(TAG, "No label resource for account type " + accountType);
22843c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            }
22943c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        }
23043c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        return label;
23143c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani    }
23243c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani
23343c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani    @Override
23443c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani    public boolean onPreferenceTreeClick(PreferenceScreen preferences, Preference preference) {
23543c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        if (preference instanceof ProviderPreference) {
23643c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            ProviderPreference pref = (ProviderPreference) preference;
23743c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            if (Log.isLoggable(TAG, Log.VERBOSE)) {
23843c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani                Log.v(TAG, "Attempting to add account of type " + pref.getAccountType());
23943c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            }
24043c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani            finishWithAccountType(pref.getAccountType());
24143c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        }
24243c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        return true;
24343c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani    }
24443c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani
24543c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani    private void finishWithAccountType(String accountType) {
24643c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        Intent intent = new Intent();
24743c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        intent.putExtra(AddAccountSettings.EXTRA_SELECTED_ACCOUNT, accountType);
24843c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        setResult(RESULT_OK, intent);
24943c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani        finish();
25043c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani    }
25143c697854c7e373fbc1dae8b7a5259a32de346b4Amith Yamasani}
252