Home | History | Annotate | Download | only in settings
      1 /*
      2  * Copyright (C) 2008 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.settings;
     18 
     19 import com.android.providers.subscribedfeeds.R;
     20 
     21 import android.accounts.AccountManager;
     22 import android.accounts.AuthenticatorException;
     23 import android.accounts.AccountManagerFuture;
     24 import android.accounts.OperationCanceledException;
     25 import android.accounts.AccountManagerCallback;
     26 import android.graphics.drawable.Drawable;
     27 import android.os.Bundle;
     28 import android.preference.Preference;
     29 import android.preference.PreferenceGroup;
     30 import android.preference.PreferenceScreen;
     31 import android.util.Log;
     32 
     33 import java.io.IOException;
     34 import java.util.ArrayList;
     35 
     36 public class AddAccountSettings extends AccountPreferenceBase {
     37     private static final String TAG = "AccountSettings";
     38     private static final boolean LDEBUG = Log.isLoggable(TAG, Log.DEBUG);
     39     private String[] mAuthorities;
     40     private PreferenceGroup mAddAccountGroup;
     41     private ArrayList<ProviderEntry> mProviderList = new ArrayList<ProviderEntry>();;
     42 
     43     private static class ProviderEntry {
     44         private final CharSequence name;
     45         private final String type;
     46         ProviderEntry(CharSequence providerName, String accountType) {
     47             name = providerName;
     48             type = accountType;
     49         }
     50     }
     51 
     52     @Override
     53     public void onCreate(Bundle icicle) {
     54         super.onCreate(icicle);
     55 
     56         setContentView(R.layout.add_account_screen);
     57         addPreferencesFromResource(R.xml.add_account_settings);
     58         mAuthorities = getIntent().getStringArrayExtra(AUTHORITIES_FILTER_KEY);
     59         mAddAccountGroup = getPreferenceScreen();
     60         updateAuthDescriptions();
     61     }
     62 
     63     @Override
     64     protected void onAuthDescriptionsUpdated() {
     65         // Create list of providers to show on preference screen
     66         for (int i = 0; i < mAuthDescs.length; i++) {
     67             String accountType = mAuthDescs[i].type;
     68             CharSequence providerName = getLabelForType(accountType);
     69 
     70             // Skip preferences for authorities not specified. If no authorities specified,
     71             // then include them all.
     72             ArrayList<String> accountAuths = getAuthoritiesForAccountType(accountType);
     73             boolean addAccountPref = true;
     74             if (mAuthorities != null && mAuthorities.length > 0 && accountAuths != null) {
     75                 addAccountPref = false;
     76                 for (int k = 0; k < mAuthorities.length; k++) {
     77                     if (accountAuths.contains(mAuthorities[k])) {
     78                         addAccountPref = true;
     79                         break;
     80                     }
     81                 }
     82             }
     83             if (addAccountPref) {
     84                 mProviderList.add(new ProviderEntry(providerName, accountType));
     85             } else {
     86                 if (LDEBUG) Log.v(TAG, "Skipped pref " + providerName + ": has no authority we need");
     87             }
     88         }
     89 
     90         if (mProviderList.size() == 1) {
     91             // If there's only one provider that matches, just run it.
     92             addAccount(mProviderList.get(0).type);
     93             finish();
     94         } else if (mProviderList.size() > 0) {
     95             mAddAccountGroup.removeAll();
     96             for (ProviderEntry pref : mProviderList) {
     97                 Drawable drawable = getDrawableForType(pref.type);
     98                 ProviderPreference p = new ProviderPreference(this, pref.type, drawable, pref.name);
     99                 mAddAccountGroup.addPreference(p);
    100             }
    101         } else {
    102             String auths = new String();
    103             for (String a : mAuthorities) auths += a + " ";
    104             Log.w(TAG, "No providers found for authorities: " + auths);
    105         }
    106     }
    107 
    108     private AccountManagerCallback<Bundle> mCallback = new AccountManagerCallback<Bundle>() {
    109         public void run(AccountManagerFuture<Bundle> future) {
    110             boolean accountAdded = false;
    111             try {
    112                 Bundle bundle = future.getResult();
    113                 bundle.keySet();
    114                 accountAdded = true;
    115                 if (LDEBUG) Log.d(TAG, "account added: " + bundle);
    116             } catch (OperationCanceledException e) {
    117                 if (LDEBUG) Log.d(TAG, "addAccount was canceled");
    118             } catch (IOException e) {
    119                 if (LDEBUG) Log.d(TAG, "addAccount failed: " + e);
    120             } catch (AuthenticatorException e) {
    121                 if (LDEBUG) Log.d(TAG, "addAccount failed: " + e);
    122             } finally {
    123                 finish();
    124             }
    125         }
    126     };
    127 
    128     @Override
    129     public boolean onPreferenceTreeClick(PreferenceScreen preferences, Preference preference) {
    130         if (preference instanceof ProviderPreference) {
    131             ProviderPreference pref = (ProviderPreference) preference;
    132             if (LDEBUG) Log.v(TAG, "Attempting to add account of type " + pref.getAccountType());
    133             addAccount(pref.getAccountType());
    134         }
    135         return true;
    136     }
    137 
    138     private void addAccount(String accountType) {
    139         AccountManager.get(this).addAccount(
    140                 accountType,
    141                 null, /* authTokenType */
    142                 null, /* requiredFeatures */
    143                 null, /* addAccountOptions */
    144                 this,
    145                 mCallback,
    146                 null /* handler */);
    147     }
    148 }
    149