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