AbstractAccountAuthenticator.java revision 3326920329cecb57c7ff1fc5c6add5c98aab9ed9
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, String[] requiredFeatures, Bundle options)
31                throws RemoteException {
32            final Bundle result;
33            try {
34                result = AbstractAccountAuthenticator.this.addAccount(
35                    new AccountAuthenticatorResponse(response),
36                        accountType, authTokenType, requiredFeatures, 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        public void hasFeatures(IAccountAuthenticatorResponse response,
138                Account account, String[] features) throws RemoteException {
139            final Bundle result;
140            try {
141                result = AbstractAccountAuthenticator.this.hasFeatures(
142                    new AccountAuthenticatorResponse(response), account, features);
143            } catch (UnsupportedOperationException e) {
144                response.onError(Constants.ERROR_CODE_UNSUPPORTED_OPERATION,
145                        "hasFeatures not supported");
146                return;
147            } catch (NetworkErrorException e) {
148                response.onError(Constants.ERROR_CODE_NETWORK_ERROR, e.getMessage());
149                return;
150            }
151            if (result != null) {
152                response.onResult(result);
153            }
154        }
155    }
156
157    Transport mTransport = new Transport();
158
159    /**
160     * @return the IAccountAuthenticator binder transport object
161     */
162    public final IAccountAuthenticator getIAccountAuthenticator()
163    {
164        return mTransport;
165    }
166
167    /**
168     * Returns a Bundle that contains the Intent of the activity that can be used to edit the
169     * properties. In order to indicate success the activity should call response.setResult()
170     * with a non-null Bundle.
171     * @param response used to set the result for the request. If the Constants.INTENT_KEY
172     *   is set in the bundle then this response field is to be used for sending future
173     *   results if and when the Intent is started.
174     * @param accountType the AccountType whose properties are to be edited.
175     * @return a Bundle containing the result or the Intent to start to continue the request.
176     *   If this is null then the request is considered to still be active and the result should
177     *   sent later using response.
178     */
179    public abstract Bundle editProperties(AccountAuthenticatorResponse response,
180            String accountType);
181    public abstract Bundle addAccount(AccountAuthenticatorResponse response, String accountType,
182            String authTokenType, String[] requiredFeatures, Bundle options)
183            throws NetworkErrorException;
184    /* @deprecated */
185    public abstract boolean confirmPassword(AccountAuthenticatorResponse response,
186            Account account, String password) throws NetworkErrorException;
187    public abstract Bundle confirmCredentials(AccountAuthenticatorResponse response,
188            Account account);
189    public abstract Bundle getAuthToken(AccountAuthenticatorResponse response,
190            Account account, String authTokenType, Bundle loginOptions)
191            throws NetworkErrorException;
192    public abstract Bundle updateCredentials(AccountAuthenticatorResponse response,
193            Account account, String authTokenType, Bundle loginOptions);
194    public abstract Bundle hasFeatures(AccountAuthenticatorResponse response,
195            Account account, String[] features) throws NetworkErrorException;
196}
197