EasAuthenticatorService.java revision a4482e865dd3b219be672898df13dbbf1f4ae48d
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;
21
22import android.accounts.AbstractAccountAuthenticator;
23import android.accounts.Account;
24import android.accounts.AccountAuthenticatorResponse;
25import android.accounts.AccountManager;
26import android.accounts.Constants;
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.ContactsContract;
35
36/**
37 * A very basic authenticator service for EAS.  At the moment, it has no UI hooks.  When called
38 * with addAccount, it simply adds the account to AccountManager directly with a username and
39 * password.  We will need to implement confirmPassword, confirmCredentials, and updateCredentials.
40 */
41public class EasAuthenticatorService extends Service {
42    public static final String OPTIONS_USERNAME = "username";
43    public static final String OPTIONS_PASSWORD = "password";
44    public static final String OPTIONS_CONTACTS_SYNC_ENABLED = "contacts";
45
46    class EasAuthenticator extends AbstractAccountAuthenticator {
47        public EasAuthenticator(Context context) {
48            super(context);
49        }
50
51        @Override
52        public Bundle addAccount(AccountAuthenticatorResponse response, String accountType,
53                String authTokenType, String[] requiredFeatures, Bundle options)
54                throws NetworkErrorException {
55            // There are two cases here:
56            // 1) We are called with a username/password; this comes from the traditional email
57            //    app UI; we simply create the account and return the proper bundle
58            if (options != null && options.containsKey(OPTIONS_PASSWORD)
59                    && options.containsKey(OPTIONS_USERNAME)) {
60                final Account account = new Account(options.getString(OPTIONS_USERNAME),
61                        Eas.ACCOUNT_MANAGER_TYPE);
62                AccountManager.get(EasAuthenticatorService.this).addAccountExplicitly(
63                            account, options.getString(OPTIONS_PASSWORD), null);
64
65                // Set up contacts syncing.  SyncManager will use information from ContentResolver
66                // to determine syncability of Contacts for Exchange
67                boolean syncContacts = false;
68                if (options.containsKey(OPTIONS_CONTACTS_SYNC_ENABLED) &&
69                        options.getBoolean(OPTIONS_CONTACTS_SYNC_ENABLED)) {
70                    syncContacts = true;
71                }
72                ContentResolver.setIsSyncable(account,
73                        ContactsContract.AUTHORITY, 1);
74                ContentResolver.setSyncAutomatically(account,
75                        ContactsContract.AUTHORITY, syncContacts);
76
77                Bundle b = new Bundle();
78                b.putString(Constants.ACCOUNT_NAME_KEY, options.getString(OPTIONS_USERNAME));
79                b.putString(Constants.ACCOUNT_TYPE_KEY, Eas.ACCOUNT_MANAGER_TYPE);
80                return b;
81            // 2) The other case is that we're creating a new account from an Account manager
82            //    activity.  In this case, we add an intent that will be used to gather the
83            //    account information...
84            } else {
85                Bundle b = new Bundle();
86                Intent intent =
87                    AccountSetupBasics.actionSetupExchangeIntent(EasAuthenticatorService.this);
88                // Add extras that indicate this is an Exchange account creation
89                // So we'll skip the "account type" activity, and we'll use the response when
90                // we're done
91                intent.putExtra(Constants.ACCOUNT_AUTHENTICATOR_RESPONSE_KEY, response);
92                b.putParcelable(Constants.INTENT_KEY, intent);
93                return b;
94            }
95        }
96
97        @Override
98        public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account) {
99            // TODO Auto-generated method stub
100            return null;
101        }
102
103        @Override
104        public boolean confirmPassword(AccountAuthenticatorResponse response, Account account,
105                String password) throws NetworkErrorException {
106            // TODO Auto-generated method stub
107            return false;
108        }
109
110        @Override
111        public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
112            return null;
113        }
114
115        @Override
116        public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account,
117                String authTokenType, Bundle loginOptions) throws NetworkErrorException {
118            return null;
119        }
120
121        @Override
122        public String getAuthTokenLabel(String authTokenType) {
123            // null means we don't have compartmentalized authtoken types
124            return null;
125        }
126
127        @Override
128        public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account,
129                String[] features) throws NetworkErrorException {
130            return null;
131        }
132
133        @Override
134        public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account,
135                String authTokenType, Bundle loginOptions) {
136            // TODO Auto-generated method stub
137            return null;
138        }
139
140    }
141
142    @Override
143    public IBinder onBind(Intent intent) {
144        // TODO Replace this with an appropriate constant in AccountManager, when it's created
145        String authenticatorIntent = "android.accounts.AccountAuthenticator";
146
147        if (authenticatorIntent.equals(intent.getAction())) {
148            return new EasAuthenticator(this).getIAccountAuthenticator().asBinder();
149        } else {
150            return null;
151        }
152    }
153}
154