Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 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 | |
| 17 | package android.accounts; |
| 18 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 19 | import android.os.Bundle; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 20 | import 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 | */ |
| 27 | public abstract class AbstractAccountAuthenticator { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 28 | class Transport extends IAccountAuthenticator.Stub { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 29 | public void addAccount(IAccountAuthenticatorResponse response, String accountType, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 30 | String authTokenType, String[] requiredFeatures, Bundle options) |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 31 | throws RemoteException { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 32 | final Bundle result; |
| 33 | try { |
| 34 | result = AbstractAccountAuthenticator.this.addAccount( |
| 35 | new AccountAuthenticatorResponse(response), |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 36 | accountType, authTokenType, requiredFeatures, options); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 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 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 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 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | public void getAuthToken(IAccountAuthenticatorResponse response, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 87 | Account account, String authTokenType, Bundle loginOptions) |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 88 | throws RemoteException { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 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 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 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 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 121 | public void editProperties(IAccountAuthenticatorResponse response, |
| 122 | String accountType) throws RemoteException { |
| 123 | final Bundle result; |
| 124 | try { |
| 125 | result = AbstractAccountAuthenticator.this.editProperties( |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 126 | new AccountAuthenticatorResponse(response), accountType); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 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 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 135 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 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 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 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 | /** |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 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. |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 178 | */ |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 179 | public abstract Bundle editProperties(AccountAuthenticatorResponse response, |
| 180 | String accountType); |
| 181 | public abstract Bundle addAccount(AccountAuthenticatorResponse response, String accountType, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 182 | String authTokenType, String[] requiredFeatures, Bundle options) |
| 183 | throws NetworkErrorException; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 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); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 194 | public abstract Bundle hasFeatures(AccountAuthenticatorResponse response, |
| 195 | Account account, String[] features) throws NetworkErrorException; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 196 | } |