EasAuthenticatorService.java revision 56e48981f0093c737b38b7757410283be9ed823f
1/* 2 * Copyright (C) 2009 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 17package com.android.email.service; 18 19import com.android.email.activity.setup.AccountSetupBasics; 20import com.android.exchange.Eas; 21 22import android.accounts.AbstractAccountAuthenticator; 23import android.accounts.Account; 24import android.accounts.AccountAuthenticatorResponse; 25import android.accounts.AccountManager; 26import android.accounts.NetworkErrorException; 27import android.app.Service; 28import android.content.ContentResolver; 29import android.content.Context; 30import android.content.Intent; 31import android.os.Bundle; 32import android.os.IBinder; 33import android.provider.ContactsContract; 34 35/** 36 * A very basic authenticator service for EAS. At the moment, it has no UI hooks. When called 37 * with addAccount, it simply adds the account to AccountManager directly with a username and 38 * password. We will need to implement confirmPassword, confirmCredentials, and updateCredentials. 39 */ 40public class EasAuthenticatorService extends Service { 41 public static final String OPTIONS_USERNAME = "username"; 42 public static final String OPTIONS_PASSWORD = "password"; 43 public static final String OPTIONS_CONTACTS_SYNC_ENABLED = "contacts"; 44 45 class EasAuthenticator extends AbstractAccountAuthenticator { 46 public EasAuthenticator(Context context) { 47 super(context); 48 } 49 50 @Override 51 public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, 52 String authTokenType, String[] requiredFeatures, Bundle options) 53 throws NetworkErrorException { 54 // There are two cases here: 55 // 1) We are called with a username/password; this comes from the traditional email 56 // app UI; we simply create the account and return the proper bundle 57 if (options != null && options.containsKey(OPTIONS_PASSWORD) 58 && options.containsKey(OPTIONS_USERNAME)) { 59 final Account account = new Account(options.getString(OPTIONS_USERNAME), 60 Eas.ACCOUNT_MANAGER_TYPE); 61 AccountManager.get(EasAuthenticatorService.this).addAccountExplicitly( 62 account, options.getString(OPTIONS_PASSWORD), null); 63 64 // Set up contacts syncing. SyncManager will use information from ContentResolver 65 // to determine syncability of Contacts for Exchange 66 boolean syncContacts = false; 67 if (options.containsKey(OPTIONS_CONTACTS_SYNC_ENABLED) && 68 options.getBoolean(OPTIONS_CONTACTS_SYNC_ENABLED)) { 69 syncContacts = true; 70 } 71 ContentResolver.setIsSyncable(account, 72 ContactsContract.AUTHORITY, 1); 73 ContentResolver.setSyncAutomatically(account, 74 ContactsContract.AUTHORITY, syncContacts); 75 76 Bundle b = new Bundle(); 77 b.putString(AccountManager.KEY_ACCOUNT_NAME, options.getString(OPTIONS_USERNAME)); 78 b.putString(AccountManager.KEY_ACCOUNT_TYPE, Eas.ACCOUNT_MANAGER_TYPE); 79 return b; 80 // 2) The other case is that we're creating a new account from an Account manager 81 // activity. In this case, we add an intent that will be used to gather the 82 // account information... 83 } else { 84 Bundle b = new Bundle(); 85 Intent intent = 86 AccountSetupBasics.actionSetupExchangeIntent(EasAuthenticatorService.this); 87 // Add extras that indicate this is an Exchange account creation 88 // So we'll skip the "account type" activity, and we'll use the response when 89 // we're done 90 intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response); 91 b.putParcelable(AccountManager.KEY_INTENT, intent); 92 return b; 93 } 94 } 95 96 @Override 97 public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, 98 Bundle options) { 99 // TODO Auto-generated method stub 100 return null; 101 } 102 103 @Override 104 public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) { 105 return null; 106 } 107 108 @Override 109 public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, 110 String authTokenType, Bundle loginOptions) throws NetworkErrorException { 111 return null; 112 } 113 114 @Override 115 public String getAuthTokenLabel(String authTokenType) { 116 // null means we don't have compartmentalized authtoken types 117 return null; 118 } 119 120 @Override 121 public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account, 122 String[] features) throws NetworkErrorException { 123 return null; 124 } 125 126 @Override 127 public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, 128 String authTokenType, Bundle loginOptions) { 129 // TODO Auto-generated method stub 130 return null; 131 } 132 133 } 134 135 @Override 136 public IBinder onBind(Intent intent) { 137 // TODO Replace this with an appropriate constant in AccountManager, when it's created 138 String authenticatorIntent = "android.accounts.AccountAuthenticator"; 139 140 if (authenticatorIntent.equals(intent.getAction())) { 141 return new EasAuthenticator(this).getIBinder(); 142 } else { 143 return null; 144 } 145 } 146} 147