1 package eu
.alefzero
.owncloud
.authenticator
;
3 import android
.accounts
.*;
4 import android
.content
.Context
;
5 import android
.content
.Intent
;
6 import android
.os
.Bundle
;
7 import android
.util
.Log
;
9 public class AccountAuthenticator
extends AbstractAccountAuthenticator
{
10 public static final String OPTIONS_USERNAME
= "username";
11 public static final String OPTIONS_PASSWORD
= "password";
12 public static final String OPTIONS_FILE_LIST_SYNC_ENABLED
= "filelist";
13 public static final String OPTIONS_PINNED_FILE_SYNC_ENABLED
= "pinned";
15 public static final String ACCOUNT_TYPE
= "owncloud";
16 public static final String AUTH_TOKEN_TYPE
= "org.owncloud";
18 public static final String KEY_AUTH_TOKEN_TYPE
= "authTokenType";
19 public static final String KEY_REQUIRED_FEATURES
= "requiredFeatures";
20 public static final String KEY_LOGIN_OPTIONS
= "loginOptions";
21 public static final String KEY_ACCOUNT
= "account";
22 public static final String KEY_OC_URL
= "oc_url";
23 public static final String KEY_CONTACT_URL
= "oc_contact_url";
25 private Context mContext
;
27 public AccountAuthenticator(Context context
) {
36 public Bundle
addAccount(AccountAuthenticatorResponse response
,
37 String accountType
, String authTokenType
, String
[] requiredFeatures
,
38 Bundle options
) throws NetworkErrorException
{
39 Log
.i(getClass().getName(), "Adding account with type " + accountType
+
40 " and auth token " + authTokenType
);
42 validateAccountType(accountType
);
43 //validateAuthTokenType(authTokenType);
44 validateRequiredFeatures(requiredFeatures
);
45 } catch (AuthenticatorException e
) {
47 return e
.getFailureBundle();
49 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
50 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
, response
);
51 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
52 intent
.putExtra(KEY_REQUIRED_FEATURES
, requiredFeatures
);
53 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
55 setIntentFlags(intent
);
56 Log
.i(getClass().getName(), intent
.toString());
57 final Bundle bundle
= new Bundle();
58 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
66 public Bundle
confirmCredentials(AccountAuthenticatorResponse response
,
67 Account account
, Bundle options
) throws NetworkErrorException
{
69 validateAccountType(account
.type
);
70 } catch (AuthenticatorException e
) {
71 return e
.getFailureBundle();
73 Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
74 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
, response
);
75 intent
.putExtra(KEY_ACCOUNT
, account
);
76 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
78 setIntentFlags(intent
);
80 Bundle resultBundle
= new Bundle();
81 resultBundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
86 public Bundle
editProperties(AccountAuthenticatorResponse response
,
88 throw new UnsupportedOperationException();
92 public Bundle
getAuthToken(AccountAuthenticatorResponse response
,
93 Account account
, String authTokenType
, Bundle options
)
94 throws NetworkErrorException
{
95 Log
.i(getClass().getName(), "Getting authToken");
97 validateAccountType(account
.type
);
98 validateAuthTokenType(authTokenType
);
99 } catch (AuthenticatorException e
) {
100 Log
.w(getClass().getName(), "Validating failded in getAuthToken");
101 return e
.getFailureBundle();
103 final AccountManager am
= AccountManager
.get(mContext
);
104 final String password
= am
.getPassword(account
);
105 if (password
!= null
) {
106 final Bundle result
= new Bundle();
107 result
.putString(AccountManager
.KEY_ACCOUNT_NAME
, account
.name
);
108 result
.putString(AccountManager
.KEY_ACCOUNT_TYPE
, ACCOUNT_TYPE
);
109 result
.putString(AccountManager
.KEY_AUTHTOKEN
, password
);
113 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
114 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
, response
);
115 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
116 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
117 intent
.putExtra(AuthenticatorActivity
.PARAM_USERNAME
, account
.name
);
119 final Bundle bundle
= new Bundle();
120 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
125 public String
getAuthTokenLabel(String authTokenType
) {
130 public Bundle
hasFeatures(AccountAuthenticatorResponse response
,
131 Account account
, String
[] features
) throws NetworkErrorException
{
132 final Bundle result
= new Bundle();
133 result
.putBoolean(AccountManager
.KEY_BOOLEAN_RESULT
, false
);
138 public Bundle
updateCredentials(AccountAuthenticatorResponse response
,
139 Account account
, String authTokenType
, Bundle options
)
140 throws NetworkErrorException
{
141 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
142 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
, response
);
143 intent
.putExtra(KEY_ACCOUNT
, account
);
144 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
145 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
146 setIntentFlags(intent
);
148 final Bundle bundle
= new Bundle();
149 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
154 public Bundle
getAccountRemovalAllowed(AccountAuthenticatorResponse response
,
155 Account account
) throws NetworkErrorException
{
156 return super.getAccountRemovalAllowed(response
, account
);
159 private void setIntentFlags(Intent intent
) {
160 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
161 intent
.addFlags(Intent
.FLAG_ACTIVITY_MULTIPLE_TASK
);
162 intent
.addFlags(Intent
.FLAG_ACTIVITY_NO_HISTORY
);
163 intent
.addFlags(Intent
.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
);
164 intent
.addFlags(Intent
.FLAG_FROM_BACKGROUND
);
167 private void validateAccountType(String type
) throws UnsupportedAccountTypeException
{
168 if (!type
.equals(ACCOUNT_TYPE
)) {
169 throw new UnsupportedAccountTypeException();
173 private void validateAuthTokenType(String authTokenType
) throws UnsupportedAuthTokenTypeException
{
174 if (!authTokenType
.equals(AUTH_TOKEN_TYPE
)) {
175 throw new UnsupportedAuthTokenTypeException();
179 private void validateRequiredFeatures(String
[] requiredFeatures
) throws UnsupportedFeaturesException
{
183 private void validateCreaditials(String username
, String password
, String path
) throws AccessDeniedException
{
187 public static class AuthenticatorException
extends Exception
{
188 private static final long serialVersionUID
= 1L;
189 private Bundle mFailureBundle
;
191 public AuthenticatorException(int code
, String errorMsg
) {
192 mFailureBundle
= new Bundle();
193 mFailureBundle
.putInt(AccountManager
.KEY_ERROR_CODE
, code
);
194 mFailureBundle
.putString(AccountManager
.KEY_ERROR_MESSAGE
, errorMsg
);
197 public Bundle
getFailureBundle() {
198 return mFailureBundle
;
202 public static class UnsupportedAccountTypeException
extends AuthenticatorException
{
203 private static final long serialVersionUID
= 1L;
205 public UnsupportedAccountTypeException() {
206 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
, "Unsupported account type");
210 public static class UnsupportedAuthTokenTypeException
extends AuthenticatorException
{
211 private static final long serialVersionUID
= 1L;
213 public UnsupportedAuthTokenTypeException() {
214 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
, "Unsupported auth token type");
218 public static class UnsupportedFeaturesException
extends AuthenticatorException
{
219 public static final long serialVersionUID
= 1L;
221 public UnsupportedFeaturesException() {
222 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
, "Unsupported features");
226 public static class AccessDeniedException
extends AuthenticatorException
{
227 public AccessDeniedException(int code
, String errorMsg
) {
228 super(AccountManager
.ERROR_CODE_INVALID_RESPONSE
, "Access Denied");
231 private static final long serialVersionUID
= 1L;