EasAuthenticatorService.java revision 64b64cca01c1a827c1b3824f099fd638cfb15826
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.email.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                // Register our GAL provider
101                ContactsContract.Directory.notifyDirectoryChange(getContentResolver());
102
103                Bundle b = new Bundle();
104                b.putString(AccountManager.KEY_ACCOUNT_NAME, options.getString(OPTIONS_USERNAME));
105                b.putString(AccountManager.KEY_ACCOUNT_TYPE, Email.EXCHANGE_ACCOUNT_MANAGER_TYPE);
106                return b;
107            // 2) The other case is that we're creating a new account from an Account manager
108            //    activity.  In this case, we add an intent that will be used to gather the
109            //    account information...
110            } else {
111                Bundle b = new Bundle();
112                Intent intent =
113                    AccountSetupBasics.actionSetupExchangeIntent(EasAuthenticatorService.this);
114                intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
115                b.putParcelable(AccountManager.KEY_INTENT, intent);
116                return b;
117            }
118        }
119
120        @Override
121        public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account,
122                Bundle options) {
123            return null;
124        }
125
126        @Override
127        public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
128            return null;
129        }
130
131        @Override
132        public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account,
133                String authTokenType, Bundle loginOptions) throws NetworkErrorException {
134            return null;
135        }
136
137        @Override
138        public String getAuthTokenLabel(String authTokenType) {
139            // null means we don't have compartmentalized authtoken types
140            return null;
141        }
142
143        @Override
144        public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account,
145                String[] features) throws NetworkErrorException {
146            return null;
147        }
148
149        @Override
150        public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account,
151                String authTokenType, Bundle loginOptions) {
152            return null;
153        }
154
155    }
156
157    @Override
158    public IBinder onBind(Intent intent) {
159        if (AccountManager.ACTION_AUTHENTICATOR_INTENT.equals(intent.getAction())) {
160            return new EasAuthenticator(this).getIBinder();
161        } else {
162            return null;
163        }
164    }
165}
166