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.service.EmailServiceUtils.EmailServiceInfo;
20import com.android.email.setup.AuthenticatorSetupIntentHelper;
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.CalendarContract;
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 AuthenticatorService 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 Authenticator extends AbstractAccountAuthenticator {
50
51        public Authenticator(Context context) {
52            super(context);
53        }
54
55        @Override
56        public Bundle addAccount(AccountAuthenticatorResponse response, String accountType,
57                String authTokenType, String[] requiredFeatures, Bundle options)
58                throws NetworkErrorException {
59
60            final String protocol = EmailServiceUtils.getProtocolFromAccountType(
61                    AuthenticatorService.this, accountType);
62            final EmailServiceInfo info = EmailServiceUtils.getServiceInfo(
63                    AuthenticatorService.this, protocol);
64
65                    // There are two cases here:
66            // 1) We are called with a username/password; this comes from the traditional email
67            //    app UI; we simply create the account and return the proper bundle
68            if (options != null && options.containsKey(OPTIONS_PASSWORD)
69                    && options.containsKey(OPTIONS_USERNAME)) {
70                final Account account = new Account(options.getString(OPTIONS_USERNAME),
71                        accountType);
72                AccountManager.get(AuthenticatorService.this).addAccountExplicitly(
73                            account, options.getString(OPTIONS_PASSWORD), null);
74
75                // Set up contacts syncing, if appropriate
76                if (info != null && info.syncContacts) {
77                    boolean syncContacts = options.getBoolean(OPTIONS_CONTACTS_SYNC_ENABLED, false);
78                    ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 1);
79                    ContentResolver.setSyncAutomatically(account, ContactsContract.AUTHORITY,
80                            syncContacts);
81                }
82
83                // Set up calendar syncing, if appropriate
84                if (info != null && info.syncCalendar) {
85                    boolean syncCalendar = options.getBoolean(OPTIONS_CALENDAR_SYNC_ENABLED, false);
86                    ContentResolver.setIsSyncable(account, CalendarContract.AUTHORITY, 1);
87                    ContentResolver.setSyncAutomatically(account, CalendarContract.AUTHORITY,
88                            syncCalendar);
89                }
90
91                // Set up email syncing (it's always syncable, but we respect the user's choice
92                // for whether to enable it now)
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, accountType);
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                        AuthenticatorSetupIntentHelper.actionGetCreateAccountIntent(
113                                AuthenticatorService.this, accountType);
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 Authenticator(this).getIBinder();
161        } else {
162            return null;
163        }
164    }
165}
166