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