-/* ownCloud Android client application\r
- * Copyright (C) 2012 Bartek Przybylski\r
- * Copyright (C) 2012-2013 ownCloud Inc.\r
- *\r
- * This program is free software: you can redistribute it and/or modify\r
- * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation, either version 2 of the License, or\r
- * (at your option) any later version.\r
- *\r
- * This program is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with this program. If not, see <http://www.gnu.org/licenses/>.\r
- *\r
- */\r
-\r
-package com.owncloud.android.authentication;\r
-\r
-\r
-import android.accounts.*;\r
-import android.content.Context;\r
-import android.content.Intent;\r
-import android.os.Bundle;\r
-import android.util.Log;\r
-\r
-\r
-/**\r
- * Authenticator for ownCloud accounts.\r
- * \r
- * Controller class accessed from the system AccountManager, providing integration of ownCloud accounts with the Android system.\r
- * \r
- * TODO - better separation in operations for OAuth-capable and regular ownCloud accounts.\r
- * TODO - review completeness \r
- * \r
- * @author David A. Velasco\r
- */\r
-public class AccountAuthenticator extends AbstractAccountAuthenticator {\r
- /**\r
- * Is used by android system to assign accounts to authenticators. Should be\r
- * used by application and all extensions.\r
- */\r
- public static final String ACCOUNT_TYPE = "owncloud";\r
- public static final String AUTHORITY = "org.owncloud";\r
- public static final String AUTH_TOKEN_TYPE = "org.owncloud";\r
- public static final String AUTH_TOKEN_TYPE_PASSWORD = "owncloud.password";\r
- public static final String AUTH_TOKEN_TYPE_ACCESS_TOKEN = "owncloud.oauth2.access_token";\r
- public static final String AUTH_TOKEN_TYPE_REFRESH_TOKEN = "owncloud.oauth2.refresh_token";\r
-\r
- public static final String KEY_AUTH_TOKEN_TYPE = "authTokenType";\r
- public static final String KEY_REQUIRED_FEATURES = "requiredFeatures";\r
- public static final String KEY_LOGIN_OPTIONS = "loginOptions";\r
- public static final String KEY_ACCOUNT = "account";\r
- /**\r
- * Value under this key should handle path to webdav php script. Will be\r
- * removed and usage should be replaced by combining\r
- * {@link com.owncloud.android.authentication.AuthenticatorActivity.KEY_OC_BASE_URL} and\r
- * {@link com.owncloud.android.utils.OwnCloudVersion}\r
- * \r
- * @deprecated\r
- */\r
- public static final String KEY_OC_URL = "oc_url";\r
- /**\r
- * Version should be 3 numbers separated by dot so it can be parsed by\r
- * {@link com.owncloud.android.utils.OwnCloudVersion}\r
- */\r
- public static final String KEY_OC_VERSION = "oc_version";\r
- /**\r
- * Base url should point to owncloud installation without trailing / ie:\r
- * http://server/path or https://owncloud.server\r
- */\r
- public static final String KEY_OC_BASE_URL = "oc_base_url";\r
- /**\r
- * Flag signaling if the ownCloud server can be accessed with OAuth2 access tokens.\r
- */\r
- public static final String KEY_SUPPORTS_OAUTH2 = "oc_supports_oauth2";\r
- \r
- private static final String TAG = AccountAuthenticator.class.getSimpleName();\r
- \r
- private Context mContext;\r
-\r
- public AccountAuthenticator(Context context) {\r
- super(context);\r
- mContext = context;\r
- }\r
-\r
- /**\r
- * {@inheritDoc}\r
- */\r
- @Override\r
- public Bundle addAccount(AccountAuthenticatorResponse response,\r
- String accountType, String authTokenType,\r
- String[] requiredFeatures, Bundle options)\r
- throws NetworkErrorException {\r
- Log.i(TAG, "Adding account with type " + accountType\r
- + " and auth token " + authTokenType);\r
- try {\r
- validateAccountType(accountType);\r
- } catch (AuthenticatorException e) {\r
- Log.e(TAG, "Failed to validate account type " + accountType + ": "\r
- + e.getMessage());\r
- e.printStackTrace();\r
- return e.getFailureBundle();\r
- }\r
- final Intent intent = new Intent(mContext, AuthenticatorActivity.class);\r
- intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);\r
- intent.putExtra(KEY_AUTH_TOKEN_TYPE, authTokenType);\r
- intent.putExtra(KEY_REQUIRED_FEATURES, requiredFeatures);\r
- intent.putExtra(KEY_LOGIN_OPTIONS, options);\r
- intent.putExtra(AuthenticatorActivity.EXTRA_ACTION, AuthenticatorActivity.ACTION_CREATE);\r
-\r
- setIntentFlags(intent);\r
- \r
- final Bundle bundle = new Bundle();\r
- bundle.putParcelable(AccountManager.KEY_INTENT, intent);\r
- return bundle;\r
- }\r
-\r
+/* ownCloud Android client application
+ * Copyright (C) 2012 Bartek Przybylski
+ * Copyright (C) 2012-2013 ownCloud Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+package com.owncloud.android.authentication;
+
+import com.owncloud.android.MainApp;
+import com.owncloud.android.R;
+
+import android.accounts.*;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+import android.os.Handler;
+import android.widget.Toast;
+
+import com.owncloud.android.lib.common.accounts.AccountTypeUtils;
+import com.owncloud.android.utils.Log_OC;
+
+
+/**
+ * Authenticator for ownCloud accounts.
+ *
+ * Controller class accessed from the system AccountManager, providing integration of ownCloud accounts with the Android system.
+ *
+ * TODO - better separation in operations for OAuth-capable and regular ownCloud accounts.
+ * TODO - review completeness
+ *
+ * @author David A. Velasco
+ */
+public class AccountAuthenticator extends AbstractAccountAuthenticator {
+
+ /**
+ * Is used by android system to assign accounts to authenticators. Should be
+ * used by application and all extensions.
+ */
+ public static final String KEY_AUTH_TOKEN_TYPE = "authTokenType";
+ public static final String KEY_REQUIRED_FEATURES = "requiredFeatures";
+ public static final String KEY_LOGIN_OPTIONS = "loginOptions";
+ public static final String KEY_ACCOUNT = "account";
+
+ private static final String TAG = AccountAuthenticator.class.getSimpleName();
+
+ private Context mContext;
+
+ private Handler mHandler;
+
+ public AccountAuthenticator(Context context) {
+ super(context);
+ mContext = context;
+ mHandler = new Handler();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Bundle addAccount(AccountAuthenticatorResponse response,
+ String accountType, String authTokenType,
+ String[] requiredFeatures, Bundle options)
+ throws NetworkErrorException {
+ Log_OC.i(TAG, "Adding account with type " + accountType
+ + " and auth token " + authTokenType);
+
+ final Bundle bundle = new Bundle();
+
+ AccountManager accountManager = AccountManager.get(mContext);
+ Account[] accounts = accountManager.getAccountsByType(MainApp.getAccountType());
+
+ if (mContext.getResources().getBoolean(R.bool.multiaccount_support) || accounts.length < 1) {
+ try {
+ validateAccountType(accountType);
+ } catch (AuthenticatorException e) {
+ Log_OC.e(TAG, "Failed to validate account type " + accountType + ": "
+ + e.getMessage());
+ e.printStackTrace();
+ return e.getFailureBundle();
+ }
+
+ final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
+ intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
+ intent.putExtra(KEY_AUTH_TOKEN_TYPE, authTokenType);
+ intent.putExtra(KEY_REQUIRED_FEATURES, requiredFeatures);
+ intent.putExtra(KEY_LOGIN_OPTIONS, options);
+ intent.putExtra(AuthenticatorActivity.EXTRA_ACTION, AuthenticatorActivity.ACTION_CREATE);
+
+ setIntentFlags(intent);
+
+ bundle.putParcelable(AccountManager.KEY_INTENT, intent);
+
+ } else {
+
+ // Return an error
+ bundle.putInt(AccountManager.KEY_ERROR_CODE, AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION);
+ final String message = String.format(mContext.getString(R.string.auth_unsupported_multiaccount), mContext.getString(R.string.app_name));
+ bundle.putString(AccountManager.KEY_ERROR_MESSAGE, message);
+
+ mHandler.post(new Runnable() {
+
+ @Override
+ public void run() {
+ Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
+ }
+ });
+
+ }
+
+ return bundle;
+ }
+