AbstractAccountAuthenticator.java revision a698f4276968d078b1b9e2f3738c4f559a3307b2
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 android.accounts;
18
19import android.os.Bundle;
20import android.os.RemoteException;
21
22/**
23 * Base class for creating AccountAuthenticators. This implements the IAccountAuthenticator
24 * binder interface and also provides helper libraries to simplify the creation of
25 * AccountAuthenticators.
26 */
27public abstract class AbstractAccountAuthenticator {
28    class Transport extends IAccountAuthenticator.Stub {
29        public void addAccount(IAccountAuthenticatorResponse response, String accountType,
30                String authTokenType, Bundle options)
31                throws RemoteException {
32            final Bundle result;
33            try {
34                result = AbstractAccountAuthenticator.this.addAccount(
35                    new AccountAuthenticatorResponse(response),
36                        accountType, authTokenType, options);
37            } catch (NetworkErrorException e) {
38                response.onError(Constants.ERROR_CODE_NETWORK_ERROR, e.getMessage());
39                return;
40            } catch (UnsupportedOperationException e) {
41                response.onError(Constants.ERROR_CODE_UNSUPPORTED_OPERATION,
42                        "addAccount not supported");
43                return;
44            }
45            if (result != null) {
46                response.onResult(result);
47            }
48        }
49
50        public void confirmPassword(IAccountAuthenticatorResponse response,
51                Account account, String password) throws RemoteException {
52            boolean result;
53            try {
54                result = AbstractAccountAuthenticator.this.confirmPassword(
55                    new AccountAuthenticatorResponse(response),
56                        account, password);
57            } catch (UnsupportedOperationException e) {
58                response.onError(Constants.ERROR_CODE_UNSUPPORTED_OPERATION,
59                        "confirmPassword not supported");
60                return;
61            } catch (NetworkErrorException e) {
62                response.onError(Constants.ERROR_CODE_NETWORK_ERROR, e.getMessage());
63                return;
64            }
65            Bundle bundle = new Bundle();
66            bundle.putBoolean(Constants.BOOLEAN_RESULT_KEY, result);
67            response.onResult(bundle);
68        }
69
70        public void confirmCredentials(IAccountAuthenticatorResponse response,
71                Account account) throws RemoteException {
72            final Bundle result;
73            try {
74                result = AbstractAccountAuthenticator.this.confirmCredentials(
75                    new AccountAuthenticatorResponse(response), account);
76            } catch (UnsupportedOperationException e) {
77                response.onError(Constants.ERROR_CODE_UNSUPPORTED_OPERATION,
78                        "confirmCredentials not supported");
79                return;
80            }
81            if (result != null) {
82                response.onResult(result);
83            }
84        }
85
86        public void getAuthToken(IAccountAuthenticatorResponse response,
87                Account account, String authTokenType, Bundle loginOptions)
88                throws RemoteException {
89            try {
90                final Bundle result = AbstractAccountAuthenticator.this.getAuthToken(
91                        new AccountAuthenticatorResponse(response), account,
92                        authTokenType, loginOptions);
93                if (result != null) {
94                    response.onResult(result);
95                }
96            } catch (UnsupportedOperationException e) {
97                response.onError(Constants.ERROR_CODE_UNSUPPORTED_OPERATION,
98                        "getAuthToken not supported");
99            } catch (NetworkErrorException e) {
100                response.onError(Constants.ERROR_CODE_NETWORK_ERROR, e.getMessage());
101            }
102        }
103
104        public void updateCredentials(IAccountAuthenticatorResponse response, Account account,
105                String authTokenType, Bundle loginOptions) throws RemoteException {
106            final Bundle result;
107            try {
108                result = AbstractAccountAuthenticator.this.updateCredentials(
109                    new AccountAuthenticatorResponse(response), account,
110                        authTokenType, loginOptions);
111            } catch (UnsupportedOperationException e) {
112                response.onError(Constants.ERROR_CODE_UNSUPPORTED_OPERATION,
113                        "updateCredentials not supported");
114                return;
115            }
116            if (result != null) {
117                response.onResult(result);
118            }
119        }
120
121        public void editProperties(IAccountAuthenticatorResponse response,
122                String accountType) throws RemoteException {
123            final Bundle result;
124            try {
125                result = AbstractAccountAuthenticator.this.editProperties(
126                    new AccountAuthenticatorResponse(response), accountType);
127            } catch (UnsupportedOperationException e) {
128                response.onError(Constants.ERROR_CODE_UNSUPPORTED_OPERATION,
129                        "editProperties not supported");
130                return;
131            }
132            if (result != null) {
133                response.onResult(result);
134            }
135        }
136    }
137
138    Transport mTransport = new Transport();
139
140    /**
141     * @return the IAccountAuthenticator binder transport object
142     */
143    public final IAccountAuthenticator getIAccountAuthenticator()
144    {
145        return mTransport;
146    }
147
148    /**
149     * Returns a Bundle that contains the Intent of the activity that can be used to edit the
150     * properties. In order to indicate success the activity should call response.setResult()
151     * with a non-null Bundle.
152     * @param response used to set the result for the request. If the Constants.INTENT_KEY
153     *   is set in the bundle then this response field is to be used for sending future
154     *   results if and when the Intent is started.
155     * @param accountType the AccountType whose properties are to be edited.
156     * @return a Bundle containing the result or the Intent to start to continue the request.
157     *   If this is null then the request is considered to still be active and the result should
158     *   sent later using response.
159     */
160    public abstract Bundle editProperties(AccountAuthenticatorResponse response,
161            String accountType);
162    public abstract Bundle addAccount(AccountAuthenticatorResponse response, String accountType,
163            String authTokenType, Bundle options) throws NetworkErrorException;
164    /* @deprecated */
165    public abstract boolean confirmPassword(AccountAuthenticatorResponse response,
166            Account account, String password) throws NetworkErrorException;
167    public abstract Bundle confirmCredentials(AccountAuthenticatorResponse response,
168            Account account);
169    public abstract Bundle getAuthToken(AccountAuthenticatorResponse response,
170            Account account, String authTokenType, Bundle loginOptions)
171            throws NetworkErrorException;
172    public abstract Bundle updateCredentials(AccountAuthenticatorResponse response,
173            Account account, String authTokenType, Bundle loginOptions);
174}
175