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.emailcommon.AccountManagerTypes;
21import com.android.emailcommon.CalendarProviderStub;
22import com.android.emailcommon.provider.EmailContent;
23
24import android.accounts.AbstractAccountAuthenticator;
25import android.accounts.Account;
26import android.accounts.AccountAuthenticatorResponse;
27import android.accounts.AccountManager;
28import android.accounts.NetworkErrorException;
29import android.app.Service;
30import android.content.ContentResolver;
31import android.content.Context;
32import android.content.Intent;
33import android.os.Bundle;
34import android.os.IBinder;
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                        AccountManagerTypes.TYPE_EXCHANGE);
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, CalendarProviderStub.AUTHORITY, 1);
89                ContentResolver.setSyncAutomatically(account, CalendarProviderStub.AUTHORITY,
90                        syncCalendar);
91
92                // Set up email syncing, as above
93                boolean syncEmail = false;
94                if (options.containsKey(OPTIONS_EMAIL_SYNC_ENABLED) &&
95                        options.getBoolean(OPTIONS_EMAIL_SYNC_ENABLED)) {
96                    syncEmail = true;
97                }
98                ContentResolver.setIsSyncable(account, EmailContent.AUTHORITY, 1);
99                ContentResolver.setSyncAutomatically(account, EmailContent.AUTHORITY,
100                        syncEmail);
101
102                Bundle b = new Bundle();
103                b.putString(AccountManager.KEY_ACCOUNT_NAME, options.getString(OPTIONS_USERNAME));
104                b.putString(AccountManager.KEY_ACCOUNT_TYPE, AccountManagerTypes.TYPE_EXCHANGE);
105                return b;
106            // 2) The other case is that we're creating a new account from an Account manager
107            //    activity.  In this case, we add an intent that will be used to gather the
108            //    account information...
109            } else {
110                Bundle b = new Bundle();
111                Intent intent =
112                    AccountSetupBasics.actionSetupExchangeIntent(EasAuthenticatorService.this);
113                intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
114                b.putParcelable(AccountManager.KEY_INTENT, intent);
115                return b;
116            }
117        }
118
119        @Override
120        public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account,
121                Bundle options) {
122            return null;
123        }
124
125        @Override
126        public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
127            return null;
128        }
129
130        @Override
131        public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account,
132                String authTokenType, Bundle loginOptions) throws NetworkErrorException {
133            return null;
134        }
135
136        @Override
137        public String getAuthTokenLabel(String authTokenType) {
138            // null means we don't have compartmentalized authtoken types
139            return null;
140        }
141
142        @Override
143        public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account,
144                String[] features) throws NetworkErrorException {
145            return null;
146        }
147
148        @Override
149        public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account,
150                String authTokenType, Bundle loginOptions) {
151            return null;
152        }
153
154    }
155
156    @Override
157    public IBinder onBind(Intent intent) {
158        if (AccountManager.ACTION_AUTHENTICATOR_INTENT.equals(intent.getAction())) {
159            return new EasAuthenticator(this).getIBinder();
160        } else {
161            return null;
162        }
163    }
164}
165