Workaround to show hidden accents in options menu
[pub/Android/ownCloud.git] / src / com / owncloud / android / authenticator / AccountAuthenticator.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19 package com.owncloud.android.authenticator;
20
21 import com.owncloud.android.ui.activity.AuthenticatorActivity;
22
23 import android.accounts.*;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.os.Bundle;
27 import android.util.Log;
28
29 public class AccountAuthenticator extends AbstractAccountAuthenticator {
30 /**
31 * Is used by android system to assign accounts to authenticators. Should be
32 * used by application and all extensions.
33 */
34 public static final String ACCOUNT_TYPE = "owncloud";
35 public static final String AUTH_TOKEN_TYPE = "org.owncloud";
36
37 public static final String KEY_AUTH_TOKEN_TYPE = "authTokenType";
38 public static final String KEY_REQUIRED_FEATURES = "requiredFeatures";
39 public static final String KEY_LOGIN_OPTIONS = "loginOptions";
40 public static final String KEY_ACCOUNT = "account";
41 /**
42 * Value under this key should handle path to webdav php script. Will be
43 * removed and usage should be replaced by combining
44 * {@link com.owncloud.android.authenticator.AuthenticatorActivity.KEY_OC_BASE_URL} and
45 * {@link com.owncloud.android.utils.OwnCloudVersion}
46 *
47 * @deprecated
48 */
49 public static final String KEY_OC_URL = "oc_url";
50 /**
51 * Version should be 3 numbers separated by dot so it can be parsed by
52 * {@link com.owncloud.android.utils.OwnCloudVersion}
53 */
54 public static final String KEY_OC_VERSION = "oc_version";
55 /**
56 * Base url should point to owncloud installation without trailing / ie:
57 * http://server/path or https://owncloud.server
58 */
59 public static final String KEY_OC_BASE_URL = "oc_base_url";
60
61 private static final String TAG = "AccountAuthenticator";
62 private Context mContext;
63
64 public AccountAuthenticator(Context context) {
65 super(context);
66 mContext = context;
67 }
68
69 /**
70 * {@inheritDoc}
71 */
72 @Override
73 public Bundle addAccount(AccountAuthenticatorResponse response,
74 String accountType, String authTokenType,
75 String[] requiredFeatures, Bundle options)
76 throws NetworkErrorException {
77 Log.i(TAG, "Adding account with type " + accountType
78 + " and auth token " + authTokenType);
79 try {
80 validateAccountType(accountType);
81 } catch (AuthenticatorException e) {
82 Log.e(TAG, "Failed to validate account type " + accountType + ": "
83 + e.getMessage());
84 e.printStackTrace();
85 return e.getFailureBundle();
86 }
87 final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
88 intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE,
89 response);
90 intent.putExtra(KEY_AUTH_TOKEN_TYPE, authTokenType);
91 intent.putExtra(KEY_REQUIRED_FEATURES, requiredFeatures);
92 intent.putExtra(KEY_LOGIN_OPTIONS, options);
93
94 setIntentFlags(intent);
95 final Bundle bundle = new Bundle();
96 bundle.putParcelable(AccountManager.KEY_INTENT, intent);
97 return bundle;
98 }
99
100 /**
101 * {@inheritDoc}
102 */
103 @Override
104 public Bundle confirmCredentials(AccountAuthenticatorResponse response,
105 Account account, Bundle options) throws NetworkErrorException {
106 try {
107 validateAccountType(account.type);
108 } catch (AuthenticatorException e) {
109 Log.e(TAG, "Failed to validate account type " + account.type + ": "
110 + e.getMessage());
111 e.printStackTrace();
112 return e.getFailureBundle();
113 }
114 Intent intent = new Intent(mContext, AuthenticatorActivity.class);
115 intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE,
116 response);
117 intent.putExtra(KEY_ACCOUNT, account);
118 intent.putExtra(KEY_LOGIN_OPTIONS, options);
119
120 setIntentFlags(intent);
121
122 Bundle resultBundle = new Bundle();
123 resultBundle.putParcelable(AccountManager.KEY_INTENT, intent);
124 return resultBundle;
125 }
126
127 @Override
128 public Bundle editProperties(AccountAuthenticatorResponse response,
129 String accountType) {
130 return null;
131 }
132
133 @Override
134 public Bundle getAuthToken(AccountAuthenticatorResponse response,
135 Account account, String authTokenType, Bundle options)
136 throws NetworkErrorException {
137 try {
138 validateAccountType(account.type);
139 validateAuthTokenType(authTokenType);
140 } catch (AuthenticatorException e) {
141 Log.e(TAG, "Failed to validate account type " + account.type + ": "
142 + e.getMessage());
143 e.printStackTrace();
144 return e.getFailureBundle();
145 }
146 final AccountManager am = AccountManager.get(mContext);
147 final String password = am.getPassword(account);
148 if (password != null) {
149 final Bundle result = new Bundle();
150 result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
151 result.putString(AccountManager.KEY_ACCOUNT_TYPE, ACCOUNT_TYPE);
152 result.putString(AccountManager.KEY_AUTHTOKEN, password);
153 return result;
154 }
155
156 final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
157 intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE,
158 response);
159 intent.putExtra(KEY_AUTH_TOKEN_TYPE, authTokenType);
160 intent.putExtra(KEY_LOGIN_OPTIONS, options);
161 intent.putExtra(AuthenticatorActivity.PARAM_USERNAME, account.name);
162
163 final Bundle bundle = new Bundle();
164 bundle.putParcelable(AccountManager.KEY_INTENT, intent);
165 return bundle;
166 }
167
168 @Override
169 public String getAuthTokenLabel(String authTokenType) {
170 return null;
171 }
172
173 @Override
174 public Bundle hasFeatures(AccountAuthenticatorResponse response,
175 Account account, String[] features) throws NetworkErrorException {
176 final Bundle result = new Bundle();
177 result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false);
178 return result;
179 }
180
181 @Override
182 public Bundle updateCredentials(AccountAuthenticatorResponse response,
183 Account account, String authTokenType, Bundle options)
184 throws NetworkErrorException {
185 final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
186 intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE,
187 response);
188 intent.putExtra(KEY_ACCOUNT, account);
189 intent.putExtra(KEY_AUTH_TOKEN_TYPE, authTokenType);
190 intent.putExtra(KEY_LOGIN_OPTIONS, options);
191 setIntentFlags(intent);
192
193 final Bundle bundle = new Bundle();
194 bundle.putParcelable(AccountManager.KEY_INTENT, intent);
195 return bundle;
196 }
197
198 @Override
199 public Bundle getAccountRemovalAllowed(
200 AccountAuthenticatorResponse response, Account account)
201 throws NetworkErrorException {
202 return super.getAccountRemovalAllowed(response, account);
203 }
204
205 private void setIntentFlags(Intent intent) {
206 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
207 intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
208 intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
209 intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
210 intent.addFlags(Intent.FLAG_FROM_BACKGROUND);
211 }
212
213 private void validateAccountType(String type)
214 throws UnsupportedAccountTypeException {
215 if (!type.equals(ACCOUNT_TYPE)) {
216 throw new UnsupportedAccountTypeException();
217 }
218 }
219
220 private void validateAuthTokenType(String authTokenType)
221 throws UnsupportedAuthTokenTypeException {
222 if (!authTokenType.equals(AUTH_TOKEN_TYPE)) {
223 throw new UnsupportedAuthTokenTypeException();
224 }
225 }
226
227 public static class AuthenticatorException extends Exception {
228 private static final long serialVersionUID = 1L;
229 private Bundle mFailureBundle;
230
231 public AuthenticatorException(int code, String errorMsg) {
232 mFailureBundle = new Bundle();
233 mFailureBundle.putInt(AccountManager.KEY_ERROR_CODE, code);
234 mFailureBundle
235 .putString(AccountManager.KEY_ERROR_MESSAGE, errorMsg);
236 }
237
238 public Bundle getFailureBundle() {
239 return mFailureBundle;
240 }
241 }
242
243 public static class UnsupportedAccountTypeException extends
244 AuthenticatorException {
245 private static final long serialVersionUID = 1L;
246
247 public UnsupportedAccountTypeException() {
248 super(AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION,
249 "Unsupported account type");
250 }
251 }
252
253 public static class UnsupportedAuthTokenTypeException extends
254 AuthenticatorException {
255 private static final long serialVersionUID = 1L;
256
257 public UnsupportedAuthTokenTypeException() {
258 super(AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION,
259 "Unsupported auth token type");
260 }
261 }
262
263 public static class UnsupportedFeaturesException extends
264 AuthenticatorException {
265 public static final long serialVersionUID = 1L;
266
267 public UnsupportedFeaturesException() {
268 super(AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION,
269 "Unsupported features");
270 }
271 }
272
273 public static class AccessDeniedException extends AuthenticatorException {
274 public AccessDeniedException(int code, String errorMsg) {
275 super(AccountManager.ERROR_CODE_INVALID_RESPONSE, "Access Denied");
276 }
277
278 private static final long serialVersionUID = 1L;
279
280 }
281 }