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";
13 public static final String OPTIONS_FILE_LIST_SYNC_ENABLED
= "filelist";
14 public static final String OPTIONS_PINNED_FILE_SYNC_ENABLED
= "pinned";
16 public static final String ACCOUNT_TYPE
= "owncloud";
17 public static final String AUTH_TOKEN_TYPE
= "org.owncloud";
19 public static final String KEY_AUTH_TOKEN_TYPE
= "authTokenType";
20 public static final String KEY_REQUIRED_FEATURES
= "requiredFeatures";
21 public static final String KEY_LOGIN_OPTIONS
= "loginOptions";
22 public static final String KEY_ACCOUNT
= "account";
23 public static final String KEY_OC_URL
= "oc_url";
24 public static final String KEY_CONTACT_URL
= "oc_contact_url";
26 private Context mContext
;
28 public AccountAuthenticator(Context context
) {
37 public Bundle
addAccount(AccountAuthenticatorResponse response
,
38 String accountType
, String authTokenType
, String
[] requiredFeatures
,
39 Bundle options
) throws NetworkErrorException
{
40 Log
.i(getClass().getName(), "Adding account with type " + accountType
+
41 " and auth token " + authTokenType
);
43 validateAccountType(accountType
);
44 //validateAuthTokenType(authTokenType);
45 validateRequiredFeatures(requiredFeatures
);
46 } catch (AuthenticatorException e
) {
48 return e
.getFailureBundle();
50 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
51 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
, response
);
52 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
53 intent
.putExtra(KEY_REQUIRED_FEATURES
, requiredFeatures
);
54 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
56 setIntentFlags(intent
);
57 Log
.i(getClass().getName(), intent
.toString());
58 final Bundle bundle
= new Bundle();
59 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
67 public Bundle
confirmCredentials(AccountAuthenticatorResponse response
,
68 Account account
, Bundle options
) throws NetworkErrorException
{
70 validateAccountType(account
.type
);
71 } catch (AuthenticatorException e
) {
72 return e
.getFailureBundle();
74 Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
75 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
, response
);
76 intent
.putExtra(KEY_ACCOUNT
, account
);
77 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
79 setIntentFlags(intent
);
81 Bundle resultBundle
= new Bundle();
82 resultBundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
87 public Bundle
editProperties(AccountAuthenticatorResponse response
,
89 throw new UnsupportedOperationException();
93 public Bundle
getAuthToken(AccountAuthenticatorResponse response
,
94 Account account
, String authTokenType
, Bundle options
)
95 throws NetworkErrorException
{
96 Log
.i(getClass().getName(), "Getting authToken");
98 validateAccountType(account
.type
);
99 validateAuthTokenType(authTokenType
);
100 } catch (AuthenticatorException e
) {
101 Log
.w(getClass().getName(), "Validating failded in getAuthToken");
102 return e
.getFailureBundle();
104 final AccountManager am
= AccountManager
.get(mContext
);
105 final String password
= am
.getPassword(account
);
106 if (password
!= null
) {
107 final Bundle result
= new Bundle();
108 result
.putString(AccountManager
.KEY_ACCOUNT_NAME
, account
.name
);
109 result
.putString(AccountManager
.KEY_ACCOUNT_TYPE
, ACCOUNT_TYPE
);
110 result
.putString(AccountManager
.KEY_AUTHTOKEN
, password
);
114 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
115 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
, response
);
116 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
117 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
118 intent
.putExtra(AuthenticatorActivity
.PARAM_USERNAME
, account
.name
);
120 final Bundle bundle
= new Bundle();
121 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
126 public String
getAuthTokenLabel(String authTokenType
) {
131 public Bundle
hasFeatures(AccountAuthenticatorResponse response
,
132 Account account
, String
[] features
) throws NetworkErrorException
{
133 final Bundle result
= new Bundle();
134 result
.putBoolean(AccountManager
.KEY_BOOLEAN_RESULT
, false
);
139 public Bundle
updateCredentials(AccountAuthenticatorResponse response
,
140 Account account
, String authTokenType
, Bundle options
)
141 throws NetworkErrorException
{
142 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
143 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
, response
);
144 intent
.putExtra(KEY_ACCOUNT
, account
);
145 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
146 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
147 setIntentFlags(intent
);
149 final Bundle bundle
= new Bundle();
150 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
155 public Bundle
getAccountRemovalAllowed(AccountAuthenticatorResponse response
,
156 Account account
) throws NetworkErrorException
{
157 return super.getAccountRemovalAllowed(response
, account
);
160 private void setIntentFlags(Intent intent
) {
161 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
162 intent
.addFlags(Intent
.FLAG_ACTIVITY_MULTIPLE_TASK
);
163 intent
.addFlags(Intent
.FLAG_ACTIVITY_NO_HISTORY
);
164 intent
.addFlags(Intent
.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
);
165 intent
.addFlags(Intent
.FLAG_FROM_BACKGROUND
);
168 private void validateAccountType(String type
) throws UnsupportedAccountTypeException
{
169 if (!type
.equals(ACCOUNT_TYPE
)) {
170 throw new UnsupportedAccountTypeException();
174 private void validateAuthTokenType(String authTokenType
) throws UnsupportedAuthTokenTypeException
{
175 if (!authTokenType
.equals(AUTH_TOKEN_TYPE
)) {
176 throw new UnsupportedAuthTokenTypeException();
180 private void validateRequiredFeatures(String
[] requiredFeatures
) throws UnsupportedFeaturesException
{
184 private void validateCreaditials(String username
, String password
, String path
) throws AccessDeniedException
{
188 public static class AuthenticatorException
extends Exception
{
189 private static final long serialVersionUID
= 1L;
190 private Bundle mFailureBundle
;
192 public AuthenticatorException(int code
, String errorMsg
) {
193 mFailureBundle
= new Bundle();
194 mFailureBundle
.putInt(AccountManager
.KEY_ERROR_CODE
, code
);
195 mFailureBundle
.putString(AccountManager
.KEY_ERROR_MESSAGE
, errorMsg
);
198 public Bundle
getFailureBundle() {
199 return mFailureBundle
;
203 public static class UnsupportedAccountTypeException
extends AuthenticatorException
{
204 private static final long serialVersionUID
= 1L;
206 public UnsupportedAccountTypeException() {
207 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
, "Unsupported account type");
211 public static class UnsupportedAuthTokenTypeException
extends AuthenticatorException
{
212 private static final long serialVersionUID
= 1L;
214 public UnsupportedAuthTokenTypeException() {
215 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
, "Unsupported auth token type");
219 public static class UnsupportedFeaturesException
extends AuthenticatorException
{
220 public static final long serialVersionUID
= 1L;
222 public UnsupportedFeaturesException() {
223 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
, "Unsupported features");
227 public static class AccessDeniedException
extends AuthenticatorException
{
228 public AccessDeniedException(int code
, String errorMsg
) {
229 super(AccountManager
.ERROR_CODE_INVALID_RESPONSE
, "Access Denied");
232 private static final long serialVersionUID
= 1L;