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