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