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