removing android4 calls to actionbar and fragments manager
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / authenticator / AccountAuthenticator.java
1 package eu.alefzero.owncloud.authenticator;
2
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;
9
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";
15
16 public static final String ACCOUNT_TYPE = "owncloud";
17 public static final String AUTH_TOKEN_TYPE = "org.owncloud";
18
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";
25
26 private Context mContext;
27
28 public AccountAuthenticator(Context context) {
29 super(context);
30 mContext = context;
31 }
32
33 /**
34 * {@inheritDoc}
35 */
36 @Override
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);
42 try {
43 validateAccountType(accountType);
44 //validateAuthTokenType(authTokenType);
45 validateRequiredFeatures(requiredFeatures);
46 } catch (AuthenticatorException e) {
47 e.printStackTrace();
48 return e.getFailureBundle();
49 }
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);
55
56 setIntentFlags(intent);
57 Log.i(getClass().getName(), intent.toString());
58 final Bundle bundle = new Bundle();
59 bundle.putParcelable(AccountManager.KEY_INTENT, intent);
60 return bundle;
61 }
62
63 /**
64 * {@inheritDoc}
65 */
66 @Override
67 public Bundle confirmCredentials(AccountAuthenticatorResponse response,
68 Account account, Bundle options) throws NetworkErrorException {
69 try {
70 validateAccountType(account.type);
71 } catch (AuthenticatorException e) {
72 return e.getFailureBundle();
73 }
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);
78
79 setIntentFlags(intent);
80
81 Bundle resultBundle = new Bundle();
82 resultBundle.putParcelable(AccountManager.KEY_INTENT, intent);
83 return resultBundle;
84 }
85
86 @Override
87 public Bundle editProperties(AccountAuthenticatorResponse response,
88 String accountType) {
89 throw new UnsupportedOperationException();
90 }
91
92 @Override
93 public Bundle getAuthToken(AccountAuthenticatorResponse response,
94 Account account, String authTokenType, Bundle options)
95 throws NetworkErrorException {
96 Log.i(getClass().getName(), "Getting authToken");
97 try {
98 validateAccountType(account.type);
99 validateAuthTokenType(authTokenType);
100 } catch (AuthenticatorException e) {
101 Log.w(getClass().getName(), "Validating failded in getAuthToken");
102 return e.getFailureBundle();
103 }
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);
111 return result;
112 }
113
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);
119
120 final Bundle bundle = new Bundle();
121 bundle.putParcelable(AccountManager.KEY_INTENT, intent);
122 return bundle;
123 }
124
125 @Override
126 public String getAuthTokenLabel(String authTokenType) {
127 return null;
128 }
129
130 @Override
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);
135 return result;
136 }
137
138 @Override
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);
148
149 final Bundle bundle = new Bundle();
150 bundle.putParcelable(AccountManager.KEY_INTENT, intent);
151 return bundle;
152 }
153
154 @Override
155 public Bundle getAccountRemovalAllowed(AccountAuthenticatorResponse response,
156 Account account) throws NetworkErrorException {
157 return super.getAccountRemovalAllowed(response, account);
158 }
159
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);
166 }
167
168 private void validateAccountType(String type) throws UnsupportedAccountTypeException {
169 if (!type.equals(ACCOUNT_TYPE)) {
170 throw new UnsupportedAccountTypeException();
171 }
172 }
173
174 private void validateAuthTokenType(String authTokenType) throws UnsupportedAuthTokenTypeException {
175 if (!authTokenType.equals(AUTH_TOKEN_TYPE)) {
176 throw new UnsupportedAuthTokenTypeException();
177 }
178 }
179
180 private void validateRequiredFeatures(String[] requiredFeatures) throws UnsupportedFeaturesException {
181 // TODO
182 }
183
184 private void validateCreaditials(String username, String password, String path) throws AccessDeniedException {
185
186 }
187
188 public static class AuthenticatorException extends Exception {
189 private static final long serialVersionUID = 1L;
190 private Bundle mFailureBundle;
191
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);
196 }
197
198 public Bundle getFailureBundle() {
199 return mFailureBundle;
200 }
201 }
202
203 public static class UnsupportedAccountTypeException extends AuthenticatorException {
204 private static final long serialVersionUID = 1L;
205
206 public UnsupportedAccountTypeException() {
207 super(AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION, "Unsupported account type");
208 }
209 }
210
211 public static class UnsupportedAuthTokenTypeException extends AuthenticatorException {
212 private static final long serialVersionUID = 1L;
213
214 public UnsupportedAuthTokenTypeException() {
215 super(AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION, "Unsupported auth token type");
216 }
217 }
218
219 public static class UnsupportedFeaturesException extends AuthenticatorException {
220 public static final long serialVersionUID = 1L;
221
222 public UnsupportedFeaturesException() {
223 super(AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION, "Unsupported features");
224 }
225 }
226
227 public static class AccessDeniedException extends AuthenticatorException {
228 public AccessDeniedException(int code, String errorMsg) {
229 super(AccountManager.ERROR_CODE_INVALID_RESPONSE, "Access Denied");
230 }
231
232 private static final long serialVersionUID = 1L;
233
234 }
235 }