EasAuthenticatorService.java revision 948c36f47ac5bb3c47c85cd6269b188a82f458c3
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.exchange.Eas;
20
21import android.accounts.AbstractAccountAuthenticator;
22import android.accounts.Account;
23import android.accounts.AccountAuthenticatorResponse;
24import android.accounts.AccountManager;
25import android.accounts.Constants;
26import android.accounts.NetworkErrorException;
27import android.app.Service;
28import android.content.Context;
29import android.content.Intent;
30import android.os.Bundle;
31import android.os.IBinder;
32
33/**
34 * A very basic authenticator service for EAS.  At the moment, it has no UI hooks.  When called
35 * with addAccount, it simply adds the account to AccountManager directly with a username and
36 * password.  We will need to implement confirmPassword, confirmCredentials, and updateCredentials.
37 */
38public class EasAuthenticatorService extends Service {
39    public static final String OPTIONS_USERNAME = "username";
40    public static final String OPTIONS_PASSWORD = "password";
41
42    class EasAuthenticator extends AbstractAccountAuthenticator {
43        public EasAuthenticator(Context context) {
44            super(context);
45        }
46
47        @Override
48        public Bundle addAccount(AccountAuthenticatorResponse response, String accountType,
49                String authTokenType, String[] requiredFeatures, Bundle options)
50                throws NetworkErrorException {
51            // The Bundle we are passed has username and password set
52            AccountManager.get(EasAuthenticatorService.this).blockingAddAccountExplicitly(
53                    new Account(options.getString(OPTIONS_USERNAME), Eas.ACCOUNT_MANAGER_TYPE),
54                    options.getString(OPTIONS_PASSWORD), null);
55            Bundle b = new Bundle();
56            b.putString(Constants.ACCOUNT_NAME_KEY, options.getString("username"));
57            b.putString(Constants.ACCOUNT_TYPE_KEY, Eas.ACCOUNT_MANAGER_TYPE);
58            return b;
59        }
60
61        @Override
62        public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account) {
63            // TODO Auto-generated method stub
64            return null;
65        }
66
67        @Override
68        public boolean confirmPassword(AccountAuthenticatorResponse response, Account account,
69                String password) throws NetworkErrorException {
70            // TODO Auto-generated method stub
71            return false;
72        }
73
74        @Override
75        public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
76            return null;
77        }
78
79        @Override
80        public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account,
81                String authTokenType, Bundle loginOptions) throws NetworkErrorException {
82            return null;
83        }
84
85        @Override
86        public String getAuthTokenLabel(String authTokenType) {
87            // null means we don't have compartmentalized authtoken types
88            return null;
89        }
90
91        @Override
92        public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account,
93                String[] features) throws NetworkErrorException {
94            return null;
95        }
96
97        @Override
98        public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account,
99                String authTokenType, Bundle loginOptions) {
100            // TODO Auto-generated method stub
101            return null;
102        }
103
104    }
105
106    @Override
107    public IBinder onBind(Intent intent) {
108        // TODO Replace this with an appropriate constant in AccountManager, when it's created
109        String authenticatorIntent = "android.accounts.AccountAuthenticator";
110
111        if (authenticatorIntent.equals(intent.getAction())) {
112            return new EasAuthenticator(this).getIAccountAuthenticator().asBinder();
113        } else {
114            return null;
115        }
116    }
117}
118