blob: 474755c64cb4946af63bf21d9201a8e25f07e132 [file] [log] [blame]
Fred Quintana60307342009-03-24 22:48:12 -07001/*
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
Fred Quintanaa698f422009-04-08 19:14:54 -070019import android.os.Bundle;
Fred Quintana60307342009-03-24 22:48:12 -070020import 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 {
Fred Quintana60307342009-03-24 22:48:12 -070028 class Transport extends IAccountAuthenticator.Stub {
Fred Quintanaa698f422009-04-08 19:14:54 -070029 public void addAccount(IAccountAuthenticatorResponse response, String accountType,
Fred Quintana33269202009-04-20 16:05:10 -070030 String authTokenType, String[] requiredFeatures, Bundle options)
Fred Quintana60307342009-03-24 22:48:12 -070031 throws RemoteException {
Fred Quintanaa698f422009-04-08 19:14:54 -070032 final Bundle result;
33 try {
34 result = AbstractAccountAuthenticator.this.addAccount(
35 new AccountAuthenticatorResponse(response),
Fred Quintana33269202009-04-20 16:05:10 -070036 accountType, authTokenType, requiredFeatures, options);
Fred Quintanaa698f422009-04-08 19:14:54 -070037 } 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 }
Fred Quintana60307342009-03-24 22:48:12 -070048 }
49
Fred Quintanaa698f422009-04-08 19:14:54 -070050 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 }
Fred Quintana60307342009-03-24 22:48:12 -070084 }
85
86 public void getAuthToken(IAccountAuthenticatorResponse response,
Fred Quintanaa698f422009-04-08 19:14:54 -070087 Account account, String authTokenType, Bundle loginOptions)
Fred Quintana60307342009-03-24 22:48:12 -070088 throws RemoteException {
Fred Quintanaa698f422009-04-08 19:14:54 -070089 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 }
Fred Quintana60307342009-03-24 22:48:12 -0700102 }
103
Fred Quintanaa698f422009-04-08 19:14:54 -0700104 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 }
Fred Quintana60307342009-03-24 22:48:12 -0700119 }
120
Fred Quintanaa698f422009-04-08 19:14:54 -0700121 public void editProperties(IAccountAuthenticatorResponse response,
122 String accountType) throws RemoteException {
123 final Bundle result;
124 try {
125 result = AbstractAccountAuthenticator.this.editProperties(
Fred Quintana60307342009-03-24 22:48:12 -0700126 new AccountAuthenticatorResponse(response), accountType);
Fred Quintanaa698f422009-04-08 19:14:54 -0700127 } 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 }
Fred Quintana60307342009-03-24 22:48:12 -0700135 }
Fred Quintana33269202009-04-20 16:05:10 -0700136
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 }
Fred Quintana60307342009-03-24 22:48:12 -0700155 }
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 /**
Fred Quintanaa698f422009-04-08 19:14:54 -0700168 * 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.
Fred Quintana60307342009-03-24 22:48:12 -0700178 */
Fred Quintanaa698f422009-04-08 19:14:54 -0700179 public abstract Bundle editProperties(AccountAuthenticatorResponse response,
180 String accountType);
181 public abstract Bundle addAccount(AccountAuthenticatorResponse response, String accountType,
Fred Quintana33269202009-04-20 16:05:10 -0700182 String authTokenType, String[] requiredFeatures, Bundle options)
183 throws NetworkErrorException;
Fred Quintanaa698f422009-04-08 19:14:54 -0700184 /* @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);
Fred Quintana33269202009-04-20 16:05:10 -0700194 public abstract Bundle hasFeatures(AccountAuthenticatorResponse response,
195 Account account, String[] features) throws NetworkErrorException;
Fred Quintana60307342009-03-24 22:48:12 -0700196}