1 package eu
.alefzero
.owncloud
.authenticator
;
3 import eu
.alefzero
.owncloud
.ui
.activity
.AuthenticatorActivity
;
4 import android
.accounts
.*;
5 import android
.content
.Context
;
6 import android
.content
.Intent
;
7 import android
.os
.Bundle
;
8 import android
.util
.Log
;
10 public class AccountAuthenticator
extends AbstractAccountAuthenticator
{
11 public static final String OPTIONS_USERNAME
= "username";
12 public static final String OPTIONS_PASSWORD
= "password";
14 public static final String ACCOUNT_TYPE
= "owncloud";
15 public static final String AUTH_TOKEN_TYPE
= "org.owncloud";
17 public static final String KEY_AUTH_TOKEN_TYPE
= "authTokenType";
18 public static final String KEY_REQUIRED_FEATURES
= "requiredFeatures";
19 public static final String KEY_LOGIN_OPTIONS
= "loginOptions";
20 public static final String KEY_ACCOUNT
= "account";
21 public static final String KEY_OC_URL
= "oc_url";
22 public static final String KEY_CONTACT_URL
= "oc_contact_url";
24 private Context mContext
;
26 public AccountAuthenticator(Context context
) {
35 public Bundle
addAccount(AccountAuthenticatorResponse response
,
36 String accountType
, String authTokenType
, String
[] requiredFeatures
,
37 Bundle options
) throws NetworkErrorException
{
38 Log
.i(getClass().getName(), "Adding account with type " + accountType
+
39 " and auth token " + authTokenType
);
41 validateAccountType(accountType
);
42 //validateAuthTokenType(authTokenType);
43 validateRequiredFeatures(requiredFeatures
);
44 } catch (AuthenticatorException e
) {
46 return e
.getFailureBundle();
48 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
49 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
, response
);
50 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
51 intent
.putExtra(KEY_REQUIRED_FEATURES
, requiredFeatures
);
52 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
54 setIntentFlags(intent
);
55 Log
.i(getClass().getName(), intent
.toString());
56 final Bundle bundle
= new Bundle();
57 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
65 public Bundle
confirmCredentials(AccountAuthenticatorResponse response
,
66 Account account
, Bundle options
) throws NetworkErrorException
{
68 validateAccountType(account
.type
);
69 } catch (AuthenticatorException e
) {
70 return e
.getFailureBundle();
72 Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
73 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
, response
);
74 intent
.putExtra(KEY_ACCOUNT
, account
);
75 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
77 setIntentFlags(intent
);
79 Bundle resultBundle
= new Bundle();
80 resultBundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
85 public Bundle
editProperties(AccountAuthenticatorResponse response
,
87 throw new UnsupportedOperationException();
91 public Bundle
getAuthToken(AccountAuthenticatorResponse response
,
92 Account account
, String authTokenType
, Bundle options
)
93 throws NetworkErrorException
{
94 Log
.i(getClass().getName(), "Getting authToken");
96 validateAccountType(account
.type
);
97 validateAuthTokenType(authTokenType
);
98 } catch (AuthenticatorException e
) {
99 Log
.w(getClass().getName(), "Validating failded in getAuthToken");
100 return e
.getFailureBundle();
102 final AccountManager am
= AccountManager
.get(mContext
);
103 final String password
= am
.getPassword(account
);
104 if (password
!= null
) {
105 final Bundle result
= new Bundle();
106 result
.putString(AccountManager
.KEY_ACCOUNT_NAME
, account
.name
);
107 result
.putString(AccountManager
.KEY_ACCOUNT_TYPE
, ACCOUNT_TYPE
);
108 result
.putString(AccountManager
.KEY_AUTHTOKEN
, password
);
112 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
113 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
, response
);
114 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
115 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
116 intent
.putExtra(AuthenticatorActivity
.PARAM_USERNAME
, account
.name
);
118 final Bundle bundle
= new Bundle();
119 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
124 public String
getAuthTokenLabel(String authTokenType
) {
129 public Bundle
hasFeatures(AccountAuthenticatorResponse response
,
130 Account account
, String
[] features
) throws NetworkErrorException
{
131 final Bundle result
= new Bundle();
132 result
.putBoolean(AccountManager
.KEY_BOOLEAN_RESULT
, false
);
137 public Bundle
updateCredentials(AccountAuthenticatorResponse response
,
138 Account account
, String authTokenType
, Bundle options
)
139 throws NetworkErrorException
{
140 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
141 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
, response
);
142 intent
.putExtra(KEY_ACCOUNT
, account
);
143 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
144 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
145 setIntentFlags(intent
);
147 final Bundle bundle
= new Bundle();
148 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
153 public Bundle
getAccountRemovalAllowed(AccountAuthenticatorResponse response
,
154 Account account
) throws NetworkErrorException
{
155 return super.getAccountRemovalAllowed(response
, account
);
158 private void setIntentFlags(Intent intent
) {
159 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
160 intent
.addFlags(Intent
.FLAG_ACTIVITY_MULTIPLE_TASK
);
161 intent
.addFlags(Intent
.FLAG_ACTIVITY_NO_HISTORY
);
162 intent
.addFlags(Intent
.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
);
163 intent
.addFlags(Intent
.FLAG_FROM_BACKGROUND
);
166 private void validateAccountType(String type
) throws UnsupportedAccountTypeException
{
167 if (!type
.equals(ACCOUNT_TYPE
)) {
168 throw new UnsupportedAccountTypeException();
172 private void validateAuthTokenType(String authTokenType
) throws UnsupportedAuthTokenTypeException
{
173 if (!authTokenType
.equals(AUTH_TOKEN_TYPE
)) {
174 throw new UnsupportedAuthTokenTypeException();
178 private void validateRequiredFeatures(String
[] requiredFeatures
) throws UnsupportedFeaturesException
{
181 private void validateCreaditials(String username
, String password
, String path
) throws AccessDeniedException
{
185 public static class AuthenticatorException
extends Exception
{
186 private static final long serialVersionUID
= 1L;
187 private Bundle mFailureBundle
;
189 public AuthenticatorException(int code
, String errorMsg
) {
190 mFailureBundle
= new Bundle();
191 mFailureBundle
.putInt(AccountManager
.KEY_ERROR_CODE
, code
);
192 mFailureBundle
.putString(AccountManager
.KEY_ERROR_MESSAGE
, errorMsg
);
195 public Bundle
getFailureBundle() {
196 return mFailureBundle
;
200 public static class UnsupportedAccountTypeException
extends AuthenticatorException
{
201 private static final long serialVersionUID
= 1L;
203 public UnsupportedAccountTypeException() {
204 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
, "Unsupported account type");
208 public static class UnsupportedAuthTokenTypeException
extends AuthenticatorException
{
209 private static final long serialVersionUID
= 1L;
211 public UnsupportedAuthTokenTypeException() {
212 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
, "Unsupported auth token type");
216 public static class UnsupportedFeaturesException
extends AuthenticatorException
{
217 public static final long serialVersionUID
= 1L;
219 public UnsupportedFeaturesException() {
220 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
, "Unsupported features");
224 public static class AccessDeniedException
extends AuthenticatorException
{
225 public AccessDeniedException(int code
, String errorMsg
) {
226 super(AccountManager
.ERROR_CODE_INVALID_RESPONSE
, "Access Denied");
229 private static final long serialVersionUID
= 1L;