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