EasAuthenticatorService.java revision bcff14acf25d3a999b7448e317604e694c204f47
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.exchange.Eas;
21import com.android.exchange.SyncManager;
22
23import android.accounts.AbstractAccountAuthenticator;
24import android.accounts.Account;
25import android.accounts.AccountAuthenticatorResponse;
26import android.accounts.AccountManager;
27import android.accounts.Constants;
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.  We will need to implement confirmPassword, confirmCredentials, and updateCredentials.
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
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                        Eas.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,
74                        ContactsContract.AUTHORITY, syncContacts ? 1 : 0);
75                ContentResolver.setSyncAutomatically(account,
76                        ContactsContract.AUTHORITY, syncContacts);
77
78                // Make sure the SyncManager is running
79                Service service = EasAuthenticatorService.this;
80                service.startService(new Intent(service, SyncManager.class));
81
82                Bundle b = new Bundle();
83                b.putString(Constants.ACCOUNT_NAME_KEY, options.getString(OPTIONS_USERNAME));
84                b.putString(Constants.ACCOUNT_TYPE_KEY, Eas.ACCOUNT_MANAGER_TYPE);
85                return b;
86            // 2) The other case is that we're creating a new account from an Account manager
87            //    activity.  In this case, we add an intent that will be used to gather the
88            //    account information...
89            } else {
90                Bundle b = new Bundle();
91                Intent intent =
92                    AccountSetupBasics.actionSetupExchangeIntent(EasAuthenticatorService.this);
93                // Add extras that indicate this is an Exchange account creation
94                // So we'll skip the "account type" activity, and we'll use the response when
95                // we're done
96                intent.putExtra(Constants.ACCOUNT_AUTHENTICATOR_RESPONSE_KEY, response);
97                b.putParcelable(Constants.INTENT_KEY, intent);
98                return b;
99            }
100        }
101
102        @Override
103        public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account) {
104            // TODO Auto-generated method stub
105            return null;
106        }
107
108        @Override
109        public boolean confirmPassword(AccountAuthenticatorResponse response, Account account,
110                String password) throws NetworkErrorException {
111            // TODO Auto-generated method stub
112            return false;
113        }
114
115        @Override
116        public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
117            return null;
118        }
119
120        @Override
121        public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account,
122                String authTokenType, Bundle loginOptions) throws NetworkErrorException {
123            return null;
124        }
125
126        @Override
127        public String getAuthTokenLabel(String authTokenType) {
128            // null means we don't have compartmentalized authtoken types
129            return null;
130        }
131
132        @Override
133        public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account,
134                String[] features) throws NetworkErrorException {
135            return null;
136        }
137
138        @Override
139        public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account,
140                String authTokenType, Bundle loginOptions) {
141            // TODO Auto-generated method stub
142            return null;
143        }
144
145    }
146
147    @Override
148    public IBinder onBind(Intent intent) {
149        // TODO Replace this with an appropriate constant in AccountManager, when it's created
150        String authenticatorIntent = "android.accounts.AccountAuthenticator";
151
152        if (authenticatorIntent.equals(intent.getAction())) {
153            return new EasAuthenticator(this).getIAccountAuthenticator().asBinder();
154        } else {
155            return null;
156        }
157    }
158}
159