/* 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
\r
package com.owncloud.android.ui.activity;\r
\r
-import java.net.MalformedURLException;\r
-import java.net.URL;\r
import java.util.HashMap;\r
import java.util.Map;\r
\r
-import org.json.JSONException;\r
-import org.json.JSONObject;\r
-\r
import com.owncloud.android.AccountUtils;\r
import com.owncloud.android.authenticator.AccountAuthenticator;\r
-import com.owncloud.android.authenticator.AuthenticationRunnable;\r
-import com.owncloud.android.authenticator.OnAuthenticationResultListener;\r
-import com.owncloud.android.authenticator.OnConnectCheckListener;\r
import com.owncloud.android.authenticator.oauth2.OAuth2Context;\r
-import com.owncloud.android.authenticator.oauth2.OAuth2GetCodeRunnable;\r
-import com.owncloud.android.authenticator.oauth2.OnOAuth2GetCodeResultListener;\r
-import com.owncloud.android.authenticator.oauth2.connection.ConnectorOAuth2;\r
-import com.owncloud.android.authenticator.oauth2.services.OAuth2GetTokenService;\r
import com.owncloud.android.ui.dialog.SslValidatorDialog;\r
import com.owncloud.android.ui.dialog.SslValidatorDialog.OnSslValidatorListener;\r
import com.owncloud.android.utils.OwnCloudVersion;\r
import com.owncloud.android.network.OwnCloudClientUtils;\r
-import com.owncloud.android.operations.ConnectionCheckOperation;\r
+import com.owncloud.android.operations.OwnCloudServerCheckOperation;\r
import com.owncloud.android.operations.ExistenceCheckOperation;\r
-import com.owncloud.android.operations.GetOAuth2AccessToken;\r
+import com.owncloud.android.operations.OAuth2GetAccessToken;\r
import com.owncloud.android.operations.OnRemoteOperationListener;\r
import com.owncloud.android.operations.RemoteOperation;\r
import com.owncloud.android.operations.RemoteOperationResult;\r
import android.app.AlertDialog;\r
import android.app.Dialog;\r
import android.app.ProgressDialog;\r
-import android.content.BroadcastReceiver;\r
import android.content.ContentResolver;\r
-import android.content.Context;\r
import android.content.DialogInterface;\r
import android.content.Intent;\r
-import android.content.IntentFilter;\r
import android.content.SharedPreferences;\r
import android.net.Uri;\r
import android.os.Bundle;\r
import android.text.InputType;\r
import android.util.Log;\r
import android.view.View;\r
-import android.view.View.OnClickListener;\r
import android.view.View.OnFocusChangeListener;\r
import android.view.Window;\r
import android.widget.CheckBox;\r
* This Activity is used to add an ownCloud account to the App\r
* \r
* @author Bartek Przybylski\r
- * \r
+ * @author David A. Velasco\r
*/\r
public class AuthenticatorActivity extends AccountAuthenticatorActivity\r
- implements OnAuthenticationResultListener, OnConnectCheckListener, OnRemoteOperationListener, OnSslValidatorListener, \r
- OnFocusChangeListener, OnClickListener, OnOAuth2GetCodeResultListener {\r
+ implements OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeListener {\r
+\r
+ private static final String TAG = AuthenticatorActivity.class.getSimpleName();\r
+\r
+ public static final String EXTRA_ACCOUNT = "ACCOUNT";\r
+ public static final String EXTRA_USER_NAME = "USER_NAME";\r
+ public static final String EXTRA_HOST_NAME = "HOST_NAME";\r
+\r
+ private static final String KEY_HOST_URL_TEXT = "HOST_URL_TEXT";\r
+ private static final String KEY_OC_VERSION = "OC_VERSION";\r
+ private static final String KEY_STATUS_TEXT = "STATUS_TEXT";\r
+ private static final String KEY_STATUS_ICON = "STATUS_ICON";\r
+ private static final String KEY_STATUS_CORRECT = "STATUS_CORRECT";\r
+ private static final String KEY_IS_SSL_CONN = "IS_SSL_CONN";\r
+ private static final String KEY_OAUTH2_STATUS_TEXT = "OAUTH2_STATUS_TEXT";\r
+ private static final String KEY_OAUTH2_STATUS_ICON = "OAUTH2_STATUS_ICON";\r
\r
private static final int DIALOG_LOGIN_PROGRESS = 0;\r
private static final int DIALOG_SSL_VALIDATOR = 1;\r
private static final int DIALOG_CERT_NOT_SAVED = 2;\r
+ private static final int DIALOG_OAUTH2_LOGIN_PROGRESS = 3;\r
\r
- private static final String TAG = "AuthActivity";\r
-\r
- private Thread mAuthThread;\r
- private AuthenticationRunnable mAuthRunnable;\r
- private ConnectionCheckOperation mConnChkRunnable;\r
- private ExistenceCheckOperation mAuthChkOperation;\r
- private final Handler mHandler = new Handler();\r
- private String mBaseUrl;\r
+ \r
+ private String mHostBaseUrl;\r
private OwnCloudVersion mDiscoveredVersion;\r
\r
- private static final String STATUS_TEXT = "STATUS_TEXT";\r
- private static final String STATUS_ICON = "STATUS_ICON";\r
- private static final String STATUS_CORRECT = "STATUS_CORRECT";\r
- private static final String IS_SSL_CONN = "IS_SSL_CONN";\r
- private static final String OC_VERSION = "OC_VERSION";\r
private int mStatusText, mStatusIcon;\r
private boolean mStatusCorrect, mIsSslConn;\r
+ private int mOAuth2StatusText, mOAuth2StatusIcon; \r
+ \r
+ private final Handler mHandler = new Handler();\r
+ private Thread mOperationThread;\r
+ private OwnCloudServerCheckOperation mOcServerChkOperation;\r
+ private ExistenceCheckOperation mAuthCheckOperation;\r
private RemoteOperationResult mLastSslUntrustedServerResult;\r
\r
- public static final String PARAM_ACCOUNTNAME = "param_Accountname";\r
- \r
- public static final String PARAM_USERNAME = "param_Username";\r
- public static final String PARAM_HOSTNAME = "param_Hostname";\r
-\r
- // oAuth2 variables.\r
- private static final int OAUTH2_LOGIN_PROGRESS = 3;\r
- private static final String OAUTH2_STATUS_TEXT = "OAUTH2_STATUS_TEXT";\r
- private static final String OAUTH2_STATUS_ICON = "OAUTH2_STATUS_ICON";\r
- private static final String OAUTH2_CODE_RESULT = "CODE_RESULT";\r
- private static final String OAUTH2_IS_CHECKED = "OAUTH2_IS_CHECKED"; \r
- private Thread mOAuth2GetCodeThread;\r
- private OAuth2GetCodeRunnable mOAuth2GetCodeRunnable; \r
- private TokenReceiver tokenReceiver;\r
- private JSONObject codeResponseJson; \r
- private int mOAuth2StatusText, mOAuth2StatusIcon; \r
+ //private Thread mOAuth2GetCodeThread;\r
+ //private OAuth2GetAuthorizationToken mOAuth2GetCodeRunnable; \r
+ //private TokenReceiver tokenReceiver;\r
+ //private JSONObject mCodeResponseJson; \r
+ private Uri mNewCapturedUriFromOAuth2Redirection;\r
\r
- public ConnectorOAuth2 connectorOAuth2;\r
+ private AccountManager mAccountMgr;\r
\r
- // Variables used to save the on the state the contents of all fields.\r
- private static final String HOST_URL_TEXT = "HOST_URL_TEXT";\r
- private static final String ACCOUNT_USERNAME = "ACCOUNT_USERNAME";\r
- private static final String ACCOUNT_PASSWORD = "ACCOUNT_PASSWORD";\r
+ private ImageView mRefreshButton;\r
+ private ImageView mViewPasswordButton;\r
+ private EditText mHostUrlInput;\r
+ private EditText mUsernameInput;\r
+ private EditText mPasswordInput;\r
+ private CheckBox mOAuth2Check;\r
+ private String mOAuthAccessToken;\r
+ private View mOkButton;\r
\r
- //private boolean mNewRedirectUriCaptured;\r
- private Uri mNewCapturedUriFromOAuth2Redirection;\r
+ private TextView mOAuthAuthEndpointText;\r
+ private TextView mOAuthTokenEndpointText;\r
\r
- // END of oAuth2 variables.\r
+\r
+ \r
\r
+ /**\r
+ * {@inheritDoc}\r
+ * \r
+ * IMPORTANT ENTRY POINT 1: activity is shown to the user\r
+ */\r
@Override\r
protected void onCreate(Bundle savedInstanceState) {\r
super.onCreate(savedInstanceState);\r
getWindow().requestFeature(Window.FEATURE_NO_TITLE);\r
+ \r
+ /// set view and get references to view elements\r
setContentView(R.layout.account_setup);\r
- ImageView iv = (ImageView) findViewById(R.id.refreshButton);\r
- ImageView iv2 = (ImageView) findViewById(R.id.viewPassword);\r
- TextView tv = (TextView) findViewById(R.id.host_URL);\r
- TextView tv2 = (TextView) findViewById(R.id.account_password);\r
- EditText oauth2Url = (EditText)findViewById(R.id.oAuth_URL);\r
- oauth2Url.setText("OWNCLOUD AUTHORIZATION PROVIDER IN TEST");\r
-\r
- if (savedInstanceState != null) {\r
- mStatusIcon = savedInstanceState.getInt(STATUS_ICON);\r
- mStatusText = savedInstanceState.getInt(STATUS_TEXT);\r
- mStatusCorrect = savedInstanceState.getBoolean(STATUS_CORRECT);\r
- mIsSslConn = savedInstanceState.getBoolean(IS_SSL_CONN);\r
- setResultIconAndText(mStatusIcon, mStatusText);\r
- findViewById(R.id.buttonOK).setEnabled(mStatusCorrect);\r
- if (!mStatusCorrect)\r
- iv.setVisibility(View.VISIBLE);\r
- else\r
- iv.setVisibility(View.INVISIBLE); \r
- \r
- String ocVersion = savedInstanceState.getString(OC_VERSION, null);\r
- if (ocVersion != null)\r
- mDiscoveredVersion = new OwnCloudVersion(ocVersion);\r
- \r
- // Getting the state of oAuth2 components.\r
- mOAuth2StatusIcon = savedInstanceState.getInt(OAUTH2_STATUS_ICON);\r
- mOAuth2StatusText = savedInstanceState.getInt(OAUTH2_STATUS_TEXT);\r
- // We set this to true if the rotation happens when the user is validating oAuth2 user_code.\r
- changeViewByOAuth2Check(savedInstanceState.getBoolean(OAUTH2_IS_CHECKED));\r
- // We store a JSon object with all the data returned from oAuth2 server when we get user_code.\r
- // Is better than store variable by variable. We use String object to serialize from/to it.\r
- try {\r
- if (savedInstanceState.containsKey(OAUTH2_CODE_RESULT)) {\r
- codeResponseJson = new JSONObject(savedInstanceState.getString(OAUTH2_CODE_RESULT));\r
- }\r
- } catch (JSONException e) {\r
- Log.e(TAG, "onCreate->JSONException: " + e.toString());\r
- }\r
- // END of getting the state of oAuth2 components.\r
- \r
- // Getting contents of each field.\r
- EditText hostUrl = (EditText)findViewById(R.id.host_URL);\r
- hostUrl.setText(savedInstanceState.getString(HOST_URL_TEXT), TextView.BufferType.EDITABLE);\r
- EditText accountUsername = (EditText)findViewById(R.id.account_username);\r
- accountUsername.setText(savedInstanceState.getString(ACCOUNT_USERNAME), TextView.BufferType.EDITABLE);\r
- EditText accountPassword = (EditText)findViewById(R.id.account_password);\r
- accountPassword.setText(savedInstanceState.getString(ACCOUNT_PASSWORD), TextView.BufferType.EDITABLE);\r
- // END of getting contents of each field\r
+ mRefreshButton = (ImageView) findViewById(R.id.refreshButton);\r
+ mViewPasswordButton = (ImageView) findViewById(R.id.viewPasswordButton);\r
+ mHostUrlInput = (EditText) findViewById(R.id.hostUrlInput);\r
+ mUsernameInput = (EditText) findViewById(R.id.account_username);\r
+ mPasswordInput = (EditText) findViewById(R.id.account_password);\r
+ mOAuthAuthEndpointText = (TextView)findViewById(R.id.oAuthEntryPoint_1);\r
+ mOAuthTokenEndpointText = (TextView)findViewById(R.id.oAuthEntryPoint_2);\r
+ mOAuth2Check = (CheckBox) findViewById(R.id.oauth_onOff_check);\r
+ mOkButton = findViewById(R.id.buttonOK);\r
+\r
+ /// complete label for 'register account' button\r
+ Button b = (Button) findViewById(R.id.account_register);\r
+ if (b != null) {\r
+ b.setText(String.format(getString(R.string.auth_register), getString(R.string.app_name)));\r
+ }\r
\r
- } else {\r
+ /// bind view elements to listeners\r
+ mHostUrlInput.setOnFocusChangeListener(this);\r
+ mPasswordInput.setOnFocusChangeListener(this);\r
+ \r
+ /// initialization\r
+ mAccountMgr = AccountManager.get(this);\r
+ mNewCapturedUriFromOAuth2Redirection = null; // TODO save?\r
+\r
+ if (savedInstanceState == null) {\r
+ /// connection state and info\r
mStatusText = mStatusIcon = 0;\r
mStatusCorrect = false;\r
mIsSslConn = false;\r
\r
- String accountName = getIntent().getExtras().getString(PARAM_ACCOUNTNAME);\r
+ /// retrieve extras from intent\r
String tokenType = getIntent().getExtras().getString(AccountAuthenticator.KEY_AUTH_TOKEN_TYPE);\r
- if (AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN.equals(tokenType)) {\r
- CheckBox oAuth2Check = (CheckBox) findViewById(R.id.oauth_onOff_check);\r
- oAuth2Check.setChecked(true);\r
- changeViewByOAuth2Check(true);\r
- } \r
+ boolean oAuthRequired = AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN.equals(tokenType);\r
+ mOAuth2Check.setChecked(oAuthRequired);\r
+ changeViewByOAuth2Check(oAuthRequired);\r
\r
- if (accountName != null) {\r
- ((TextView) findViewById(R.id.account_username)).setText(accountName.substring(0, accountName.lastIndexOf('@')));\r
- tv.setText(accountName.substring(accountName.lastIndexOf('@') + 1));\r
+ Account account = getIntent().getExtras().getParcelable(EXTRA_ACCOUNT);\r
+ if (account != null) {\r
+ String ocVersion = mAccountMgr.getUserData(account, AccountAuthenticator.KEY_OC_VERSION);\r
+ if (ocVersion != null) {\r
+ mDiscoveredVersion = new OwnCloudVersion(ocVersion);\r
+ }\r
+ mHostBaseUrl = mAccountMgr.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL);\r
+ mHostUrlInput.setText(mHostBaseUrl);\r
+ String userName = account.name.substring(0, account.name.lastIndexOf('@'));\r
+ mUsernameInput.setText(userName);\r
}\r
+\r
+ } else {\r
+ loadSavedInstanceState(savedInstanceState);\r
}\r
- iv.setOnClickListener(this);\r
- iv2.setOnClickListener(this);\r
- tv.setOnFocusChangeListener(this);\r
- tv2.setOnFocusChangeListener(this);\r
\r
- Button b = (Button) findViewById(R.id.account_register);\r
- if (b != null) {\r
- b.setText(String.format(getString(R.string.auth_register), getString(R.string.app_name)));\r
+ mPasswordInput.setText(""); // clean password to avoid social hacking (disadvantage: password in removed if the device is turned aside)\r
+ }\r
+\r
+\r
+ /**\r
+ * Saves relevant state before {@link #onPause()}\r
+ * \r
+ * Do NOT save {@link #mNewCapturedUriFromOAuth2Redirection}; it keeps a temporal flag, intended to defer the \r
+ * processing of the redirection caught in {@link #onNewIntent(Intent)} until {@link #onResume()} \r
+ * \r
+ * See {@link #loadSavedInstanceState(Bundle)}\r
+ */\r
+ @Override\r
+ protected void onSaveInstanceState(Bundle outState) {\r
+ super.onSaveInstanceState(outState);\r
+ \r
+ /// connection state and info\r
+ outState.putInt(KEY_STATUS_TEXT, mStatusText);\r
+ outState.putInt(KEY_STATUS_ICON, mStatusIcon);\r
+ outState.putBoolean(KEY_STATUS_CORRECT, mStatusCorrect);\r
+ outState.putBoolean(KEY_IS_SSL_CONN, mIsSslConn);\r
+\r
+ /// server data\r
+ if (mDiscoveredVersion != null) \r
+ outState.putString(KEY_OC_VERSION, mDiscoveredVersion.toString());\r
+ outState.putString(KEY_HOST_URL_TEXT, mHostBaseUrl);\r
+ \r
+ // Saving the state of oAuth2 components.\r
+ outState.putInt(KEY_OAUTH2_STATUS_ICON, mOAuth2StatusIcon);\r
+ outState.putInt(KEY_OAUTH2_STATUS_TEXT, mOAuth2StatusText);\r
+ \r
+ /* Leave old OAuth flow\r
+ if (codeResponseJson != null){\r
+ outState.putString(KEY_OAUTH2_CODE_RESULT, codeResponseJson.toString());\r
}\r
+ */\r
+ }\r
\r
- mNewCapturedUriFromOAuth2Redirection = null;\r
+\r
+ /**\r
+ * Loads saved state\r
+ * \r
+ * See {@link #onSaveInstanceState(Bundle)}.\r
+ * \r
+ * @param savedInstanceState Saved state, as received in {@link #onCreate(Bundle)}.\r
+ */\r
+ private void loadSavedInstanceState(Bundle savedInstanceState) {\r
+ /// connection state and info\r
+ mStatusCorrect = savedInstanceState.getBoolean(KEY_STATUS_CORRECT);\r
+ mIsSslConn = savedInstanceState.getBoolean(KEY_IS_SSL_CONN);\r
+ mStatusText = savedInstanceState.getInt(KEY_STATUS_TEXT);\r
+ mStatusIcon = savedInstanceState.getInt(KEY_STATUS_ICON);\r
+ updateOcServerCheckIconAndText();\r
+ \r
+ /// UI settings depending upon connection\r
+ mOkButton.setEnabled(mStatusCorrect); // TODO really necessary?\r
+ if (!mStatusCorrect)\r
+ mRefreshButton.setVisibility(View.VISIBLE); // seems that setting visibility is necessary\r
+ else\r
+ mRefreshButton.setVisibility(View.INVISIBLE);\r
+ \r
+ /// server data\r
+ String ocVersion = savedInstanceState.getString(KEY_OC_VERSION);\r
+ if (ocVersion != null)\r
+ mDiscoveredVersion = new OwnCloudVersion(ocVersion);\r
+ mHostBaseUrl = savedInstanceState.getString(KEY_HOST_URL_TEXT);\r
+ \r
+ // state of oAuth2 components\r
+ mOAuth2StatusIcon = savedInstanceState.getInt(KEY_OAUTH2_STATUS_ICON);\r
+ mOAuth2StatusText = savedInstanceState.getInt(KEY_OAUTH2_STATUS_TEXT);\r
+ changeViewByOAuth2Check(mOAuth2Check.isChecked());\r
+ \r
+ /* Leave old OAuth flow\r
+ // We store a JSon object with all the data returned from oAuth2 server when we get user_code.\r
+ // Is better than store variable by variable. We use String object to serialize from/to it.\r
+ try {\r
+ if (savedInstanceState.containsKey(KEY_OAUTH2_CODE_RESULT)) {\r
+ codeResponseJson = new JSONObject(savedInstanceState.getString(KEY_OAUTH2_CODE_RESULT));\r
+ }\r
+ } catch (JSONException e) {\r
+ Log.e(TAG, "onCreate->JSONException: " + e.toString());\r
+ }*/\r
+ // END of getting the state of oAuth2 components.\r
+ \r
}\r
\r
\r
+ /**\r
+ * The redirection triggered by the OAuth authentication server as response to the GET AUTHORIZATION request\r
+ * is caught here.\r
+ * \r
+ * To make this possible, this activity needs to be qualified with android:launchMode = "singleTask" in the\r
+ * AndroidManifest.xml file.\r
+ */\r
@Override\r
protected void onNewIntent (Intent intent) {\r
+ Log.d(TAG, "onNewIntent()");\r
Uri data = intent.getData();\r
- //mNewRedirectUriCaptured = (data != null && data.toString().startsWith(OAuth2Context.MY_REDIRECT_URI));\r
if (data != null && data.toString().startsWith(OAuth2Context.MY_REDIRECT_URI)) {\r
mNewCapturedUriFromOAuth2Redirection = data;\r
}\r
- Log.d(TAG, "onNewIntent()");\r
- \r
- }\r
- \r
- \r
- @Override\r
- protected void onSaveInstanceState(Bundle outState) {\r
- outState.putInt(STATUS_ICON, mStatusIcon);\r
- outState.putInt(STATUS_TEXT, mStatusText);\r
- outState.putBoolean(STATUS_CORRECT, mStatusCorrect);\r
- if (mDiscoveredVersion != null) \r
- outState.putString(OC_VERSION, mDiscoveredVersion.toString());\r
- \r
- // Saving the state of oAuth2 components.\r
- outState.putInt(OAUTH2_STATUS_ICON, mOAuth2StatusIcon);\r
- outState.putInt(OAUTH2_STATUS_TEXT, mOAuth2StatusText);\r
- CheckBox oAuth2Check = (CheckBox) findViewById(R.id.oauth_onOff_check);\r
- outState.putBoolean(OAUTH2_IS_CHECKED, oAuth2Check.isChecked());\r
- if (codeResponseJson != null){\r
- outState.putString(OAUTH2_CODE_RESULT, codeResponseJson.toString());\r
+ }\r
+\r
+ \r
+ /**\r
+ * The redirection triggered by the OAuth authentication server as response to the GET AUTHORIZATION, and \r
+ * deferred in {@link #onNewIntent(Intent)}, is processed here.\r
+ */\r
+ @Override\r
+ protected void onResume() {\r
+ super.onResume();\r
+ /* LEAVE OLD OAUTH FLOW ; \r
+ // (old oauth code) Registering token receiver. We must listening to the service that is pooling to the oAuth server for a token.\r
+ if (tokenReceiver == null) {\r
+ IntentFilter tokenFilter = new IntentFilter(OAuth2GetTokenService.TOKEN_RECEIVED_MESSAGE); \r
+ tokenReceiver = new TokenReceiver();\r
+ this.registerReceiver(tokenReceiver,tokenFilter);\r
+ } */\r
+ // (new oauth code)\r
+ if (mNewCapturedUriFromOAuth2Redirection != null) {\r
+ getOAuth2AccessTokenFromCapturedRedirection(); \r
+ }\r
+ }\r
+ \r
+ \r
+ @Override protected void onDestroy() { \r
+ super.onDestroy();\r
+\r
+ /* LEAVE OLD OAUTH FLOW\r
+ // We must stop the service thats it's pooling to oAuth2 server for a token.\r
+ Intent tokenService = new Intent(this, OAuth2GetTokenService.class);\r
+ stopService(tokenService);\r
+ \r
+ // We stop listening the result of the pooling service.\r
+ if (tokenReceiver != null) {\r
+ unregisterReceiver(tokenReceiver);\r
+ tokenReceiver = null;\r
+ }*/\r
+\r
+ } \r
+ \r
+ \r
+ /**\r
+ * Parses the redirection with the response to the GET AUTHORIZATION request to the \r
+ * oAuth server and requests for the access token (GET ACCESS TOKEN)\r
+ */\r
+ private void getOAuth2AccessTokenFromCapturedRedirection() {\r
+ /// Parse data from OAuth redirection\r
+ Map<String, String> responseValues = new HashMap<String, String>();\r
+ String queryParameters = mNewCapturedUriFromOAuth2Redirection.getQuery();\r
+ mNewCapturedUriFromOAuth2Redirection = null;\r
+ String[] pairs = queryParameters.split("&");\r
+ int i = 0;\r
+ String key = "";\r
+ String value = "";\r
+ StringBuilder sb = new StringBuilder();\r
+ while (pairs.length > i) {\r
+ int j = 0;\r
+ String[] part = pairs[i].split("=");\r
+ while (part.length > j) {\r
+ String p = part[j];\r
+ if (j == 0) {\r
+ key = p;\r
+ sb.append(key + " = ");\r
+ } else if (j == 1) {\r
+ value = p;\r
+ responseValues.put(key, value);\r
+ sb.append(value + "\n");\r
+ }\r
+\r
+ Log.v(TAG, "[" + i + "," + j + "] = " + p);\r
+ j++;\r
+ }\r
+ i++;\r
+ }\r
+ \r
+ /// Updating status widget to OK.\r
+ updateOAuth2IconAndText(R.drawable.ic_ok, R.string.auth_connection_established);\r
+ \r
+ /// Showing the dialog with instructions for the user.\r
+ showDialog(DIALOG_OAUTH2_LOGIN_PROGRESS);\r
+\r
+ /// GET ACCESS TOKEN to the oAuth server \r
+ RemoteOperation operation = new OAuth2GetAccessToken(responseValues);\r
+ WebdavClient client = OwnCloudClientUtils.createOwnCloudClient(Uri.parse(getString(R.string.oauth_url_endpoint_access)), getApplicationContext());\r
+ operation.execute(client, this, mHandler);\r
+ }\r
+ \r
+\r
+ \r
+ /**\r
+ * Handles the change of focus on the text inputs for the server URL and the password\r
+ */\r
+ public void onFocusChange(View view, boolean hasFocus) {\r
+ if (view.getId() == R.id.hostUrlInput) {\r
+ onUrlInputFocusChanged((TextView) view, hasFocus);\r
+ \r
+ } else if (view.getId() == R.id.account_password) {\r
+ onPasswordFocusChanged((TextView) view, hasFocus);\r
+ }\r
+ }\r
+ \r
+\r
+ /**\r
+ * Handles changes in focus on the text input for the server URL.\r
+ * \r
+ * IMPORTANT ENTRY POINT 2: When (!hasFocus), user wrote the server URL and changed to \r
+ * other field. The operation to check the existence of the server in the entered URL is\r
+ * started. \r
+ * \r
+ * When hasFocus: user 'comes back' to write again the server URL.\r
+ * \r
+ * @param hostInput TextView with the URL input field receiving the change of focus.\r
+ * @param hasFocus 'True' if focus is received, 'false' if is lost\r
+ */\r
+ private void onUrlInputFocusChanged(TextView hostInput, boolean hasFocus) {\r
+ if (!hasFocus) {\r
+ String uri = hostInput.getText().toString().trim();\r
+ if (uri.length() != 0) {\r
+ mStatusText = R.string.auth_testing_connection;\r
+ mStatusIcon = R.drawable.progress_small;\r
+ updateOcServerCheckIconAndText();\r
+ /** TODO cancel previous connection check if the user tries to ammend a wrong URL \r
+ if(mConnChkOperation != null) {\r
+ mConnChkOperation.cancel();\r
+ } */\r
+ mOcServerChkOperation = new OwnCloudServerCheckOperation(uri, this);\r
+ WebdavClient client = OwnCloudClientUtils.createOwnCloudClient(Uri.parse(uri), this);\r
+ mHostBaseUrl = "";\r
+ mDiscoveredVersion = null;\r
+ mOperationThread = mOcServerChkOperation.execute(client, this, mHandler);\r
+ } else {\r
+ mRefreshButton.setVisibility(View.INVISIBLE);\r
+ mStatusText = 0;\r
+ mStatusIcon = 0;\r
+ updateOcServerCheckIconAndText();\r
+ }\r
+ } else {\r
+ // avoids that the 'connect' button can be clicked if the test was previously passed\r
+ mOkButton.setEnabled(false); \r
+ }\r
+ }\r
+\r
+\r
+ /**\r
+ * Handles changes in focus on the text input for the password (basic authorization).\r
+ * \r
+ * When (hasFocus), the button to toggle password visibility is shown.\r
+ * \r
+ * When (!hasFocus), the button is made invisible and the password is hidden.\r
+ * \r
+ * @param passwordInput TextView with the password input field receiving the change of focus.\r
+ * @param hasFocus 'True' if focus is received, 'false' if is lost\r
+ */\r
+ private void onPasswordFocusChanged(TextView passwordInput, boolean hasFocus) {\r
+ if (hasFocus) {\r
+ mViewPasswordButton.setVisibility(View.VISIBLE);\r
+ } else {\r
+ int input_type = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD;\r
+ passwordInput.setInputType(input_type);\r
+ mViewPasswordButton.setVisibility(View.INVISIBLE);\r
+ }\r
+ }\r
+\r
+\r
+ \r
+ /**\r
+ * Cancels the authenticator activity\r
+ * \r
+ * IMPORTANT ENTRY POINT 3: Never underestimate the importance of cancellation\r
+ * \r
+ * This method is bound in the layout/acceoun_setup.xml resource file.\r
+ * \r
+ * @param view Cancel button\r
+ */\r
+ public void onCancelClick(View view) {\r
+ setResult(RESULT_CANCELED); // TODO review how is this related to AccountAuthenticator\r
+ finish();\r
+ }\r
+ \r
+ \r
+ \r
+ /**\r
+ * Checks the credentials of the user in the root of the ownCloud server\r
+ * before creating a new local account.\r
+ * \r
+ * For basic authorization, a check of existence of the root folder is\r
+ * performed.\r
+ * \r
+ * For OAuth, starts the flow to get an access token; the credentials test \r
+ * is postponed until it is available.\r
+ * \r
+ * IMPORTANT ENTRY POINT 4\r
+ * \r
+ * @param view OK button\r
+ */\r
+ public void onOkClick(View view) {\r
+ // this check should be unnecessary\r
+ if (mDiscoveredVersion == null || !mDiscoveredVersion.isVersionValid() || mHostBaseUrl == null || mHostBaseUrl.length() == 0) {\r
+ mStatusIcon = R.drawable.common_error;\r
+ mStatusText = R.string.auth_wtf_reenter_URL;\r
+ updateOcServerCheckIconAndText();\r
+ mOkButton.setEnabled(false);\r
+ Log.wtf(TAG, "The user was allowed to click 'connect' to an unchecked server!!");\r
+ return;\r
+ }\r
+ \r
+ if (mOAuth2Check.isChecked()) {\r
+ startOauthorization();\r
+ \r
+ } else {\r
+ checkBasicAuthorization();\r
+ }\r
+ }\r
+ \r
+ \r
+ /**\r
+ * Tests the credentials entered by the user performing a check of existence on \r
+ * the root folder of the ownCloud server.\r
+ */\r
+ private void checkBasicAuthorization() {\r
+ /// get the path to the root folder through WebDAV from the version server\r
+ String webdav_path = AccountUtils.getWebdavPath(mDiscoveredVersion, false);\r
+ \r
+ /// get basic credentials entered by user\r
+ String username = mUsernameInput.getText().toString();\r
+ String password = mPasswordInput.getText().toString();\r
+ \r
+ /// be gentle with the user\r
+ showDialog(DIALOG_LOGIN_PROGRESS);\r
+ \r
+ /// test credentials accessing the root folder\r
+ mAuthCheckOperation = new ExistenceCheckOperation("", this, false);\r
+ WebdavClient client = OwnCloudClientUtils.createOwnCloudClient(Uri.parse(mHostBaseUrl + webdav_path), this);\r
+ client.setBasicCredentials(username, password);\r
+ mOperationThread = mAuthCheckOperation.execute(client, this, mHandler);\r
+ }\r
+\r
+\r
+ /**\r
+ * Starts the OAuth 'grant type' flow to get an access token, with \r
+ * a GET AUTHORIZATION request to the BUILT-IN authorization server. \r
+ */\r
+ private void startOauthorization() {\r
+ // be gentle with the user\r
+ updateOAuth2IconAndText(R.drawable.progress_small, R.string.oauth_login_connection);\r
+ \r
+ // GET AUTHORIZATION request\r
+ /*\r
+ mOAuth2GetCodeRunnable = new OAuth2GetAuthorizationToken(, this);\r
+ mOAuth2GetCodeRunnable.setListener(this, mHandler);\r
+ mOAuth2GetCodeThread = new Thread(mOAuth2GetCodeRunnable);\r
+ mOAuth2GetCodeThread.start();\r
+ */\r
+ \r
+ //if (mGrantType.equals(OAuth2Context.OAUTH2_AUTH_CODE_GRANT_TYPE)) {\r
+ Uri uri = Uri.parse(getString(R.string.oauth_url_endpoint_auth));\r
+ Uri.Builder uriBuilder = uri.buildUpon();\r
+ uriBuilder.appendQueryParameter(OAuth2Context.CODE_RESPONSE_TYPE, OAuth2Context.OAUTH2_CODE_RESPONSE_TYPE);\r
+ uriBuilder.appendQueryParameter(OAuth2Context.CODE_REDIRECT_URI, OAuth2Context.MY_REDIRECT_URI); \r
+ uriBuilder.appendQueryParameter(OAuth2Context.CODE_CLIENT_ID, OAuth2Context.OAUTH2_F_CLIENT_ID);\r
+ uriBuilder.appendQueryParameter(OAuth2Context.CODE_SCOPE, OAuth2Context.OAUTH2_F_SCOPE);\r
+ //uriBuilder.appendQueryParameter(OAuth2Context.CODE_STATE, whateverwewant);\r
+ uri = uriBuilder.build();\r
+ Log.d(TAG, "Starting browser to view " + uri.toString());\r
+ Intent i = new Intent(Intent.ACTION_VIEW, uri);\r
+ startActivity(i);\r
+ //}\r
+ }\r
+\r
+ \r
+ /**\r
+ * Callback method invoked when a RemoteOperation executed by this Activity finishes.\r
+ * \r
+ * Dispatches the operation flow to the right method.\r
+ */\r
+ @Override\r
+ public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {\r
+\r
+ if (operation instanceof OwnCloudServerCheckOperation) {\r
+ onOcServerCheckFinish((OwnCloudServerCheckOperation) operation, result);\r
+ \r
+ } else if (operation instanceof OAuth2GetAccessToken) {\r
+ onGetOAuthAccessTokenFinish((OAuth2GetAccessToken)operation, result);\r
+ \r
+ } else if (operation instanceof ExistenceCheckOperation) {\r
+ onAuthorizationCheckFinish((ExistenceCheckOperation)operation, result);\r
+ \r
+ }\r
+ }\r
+ \r
+\r
+ /**\r
+ * Processes the result of the server check performed when the user finishes the enter of the\r
+ * server URL.\r
+ * \r
+ * @param operation Server check performed.\r
+ * @param result Result of the check.\r
+ */\r
+ private void onOcServerCheckFinish(OwnCloudServerCheckOperation operation, RemoteOperationResult result) {\r
+ /// update status connection icon and text\r
+ mStatusText = mStatusIcon = 0;\r
+ mStatusCorrect = false;\r
+ \r
+ switch (result.getCode()) {\r
+ case OK_SSL:\r
+ mIsSslConn = true;\r
+ mStatusIcon = android.R.drawable.ic_secure;\r
+ mStatusText = R.string.auth_secure_connection;\r
+ mStatusCorrect = true;\r
+ break;\r
+ \r
+ case OK_NO_SSL:\r
+ case OK:\r
+ mIsSslConn = false;\r
+ mStatusCorrect = true;\r
+ if (mHostUrlInput.getText().toString().trim().toLowerCase().startsWith("http://") ) {\r
+ mStatusText = R.string.auth_connection_established;\r
+ mStatusIcon = R.drawable.ic_ok;\r
+ } else {\r
+ mStatusText = R.string.auth_nossl_plain_ok_title;\r
+ mStatusIcon = android.R.drawable.ic_partial_secure;\r
+ }\r
+ break;\r
+ \r
+ /// very special case (TODO: move to a common place for all the remote operations)\r
+ case SSL_RECOVERABLE_PEER_UNVERIFIED:\r
+ mStatusIcon = R.drawable.common_error;\r
+ mStatusText = R.string.auth_ssl_unverified_server_title;\r
+ mLastSslUntrustedServerResult = result;\r
+ showDialog(DIALOG_SSL_VALIDATOR); \r
+ break;\r
+ \r
+ case BAD_OC_VERSION:\r
+ mStatusIcon = R.drawable.common_error;\r
+ mStatusText = R.string.auth_bad_oc_version_title;\r
+ break;\r
+ case WRONG_CONNECTION:\r
+ mStatusIcon = R.drawable.common_error;\r
+ mStatusText = R.string.auth_wrong_connection_title;\r
+ break;\r
+ case TIMEOUT:\r
+ mStatusIcon = R.drawable.common_error;\r
+ mStatusText = R.string.auth_timeout_title;\r
+ break;\r
+ case INCORRECT_ADDRESS:\r
+ mStatusIcon = R.drawable.common_error;\r
+ mStatusText = R.string.auth_incorrect_address_title;\r
+ break;\r
+ \r
+ case SSL_ERROR:\r
+ mStatusIcon = R.drawable.common_error;\r
+ mStatusText = R.string.auth_ssl_general_error_title;\r
+ break;\r
+ \r
+ case HOST_NOT_AVAILABLE:\r
+ mStatusIcon = R.drawable.common_error;\r
+ mStatusText = R.string.auth_unknown_host_title;\r
+ break;\r
+ case NO_NETWORK_CONNECTION:\r
+ mStatusIcon = R.drawable.no_network;\r
+ mStatusText = R.string.auth_no_net_conn_title;\r
+ break;\r
+ case INSTANCE_NOT_CONFIGURED:\r
+ mStatusIcon = R.drawable.common_error;\r
+ mStatusText = R.string.auth_not_configured_title;\r
+ break;\r
+ case FILE_NOT_FOUND:\r
+ mStatusIcon = R.drawable.common_error;\r
+ mStatusText = R.string.auth_incorrect_path_title;\r
+ break;\r
+ case UNHANDLED_HTTP_CODE:\r
+ case UNKNOWN_ERROR:\r
+ mStatusIcon = R.drawable.common_error;\r
+ mStatusText = R.string.auth_unknown_error_title;\r
+ break;\r
+ default:\r
+ Log.e(TAG, "Incorrect connection checker result type: " + result.getHttpCode());\r
+ }\r
+ updateOcServerCheckIconAndText();\r
+ \r
+ /// update the visibility of the 'retry connection' button\r
+ if (!mStatusCorrect)\r
+ mRefreshButton.setVisibility(View.VISIBLE);\r
+ else\r
+ mRefreshButton.setVisibility(View.INVISIBLE);\r
+ \r
+ /// retrieve discovered version and normalize server URL\r
+ mDiscoveredVersion = operation.getDiscoveredVersion();\r
+ mHostBaseUrl = mHostUrlInput.getText().toString().trim();\r
+ if (!mHostBaseUrl.toLowerCase().startsWith("http://") &&\r
+ !mHostBaseUrl.toLowerCase().startsWith("https://")) {\r
+ \r
+ if (mIsSslConn) {\r
+ mHostBaseUrl = "https://" + mHostBaseUrl;\r
+ } else {\r
+ mHostBaseUrl = "http://" + mHostBaseUrl;\r
+ }\r
+ \r
+ }\r
+ if (mHostBaseUrl.endsWith("/"))\r
+ mHostBaseUrl = mHostBaseUrl.substring(0, mHostBaseUrl.length() - 1);\r
+ \r
+ /// allow or not the user try to access the server\r
+ mOkButton.setEnabled(mStatusCorrect);\r
+ }\r
+\r
+\r
+ /**\r
+ * Processes the result of the request for and access token send \r
+ * to an OAuth authorization server.\r
+ * \r
+ * @param operation Operation performed requesting the access token.\r
+ * @param result Result of the operation.\r
+ */\r
+ private void onGetOAuthAccessTokenFinish(OAuth2GetAccessToken operation, RemoteOperationResult result) {\r
+ try {\r
+ dismissDialog(DIALOG_OAUTH2_LOGIN_PROGRESS);\r
+ } catch (IllegalArgumentException e) {\r
+ // NOTHING TO DO ; can't find out what situation that leads to the exception in this code, but user logs signal that it happens\r
+ }\r
+\r
+ String webdav_path = AccountUtils.getWebdavPath(mDiscoveredVersion, false);\r
+ if (result.isSuccess() && webdav_path != null) {\r
+ /// be gentle with the user\r
+ showDialog(DIALOG_LOGIN_PROGRESS);\r
+ \r
+ /// time to test the retrieved access token on the ownCloud server\r
+ mOAuthAccessToken = ((OAuth2GetAccessToken)operation).getResultTokenMap().get(OAuth2Context.KEY_ACCESS_TOKEN);\r
+ Log.d(TAG, "Got ACCESS TOKEN: " + mOAuthAccessToken);\r
+ mAuthCheckOperation = new ExistenceCheckOperation("", this, false);\r
+ WebdavClient client = OwnCloudClientUtils.createOwnCloudClient(Uri.parse(mHostBaseUrl + webdav_path), this);\r
+ client.setBearerCredentials(mOAuthAccessToken);\r
+ mAuthCheckOperation.execute(client, this, mHandler);\r
+ \r
+ } else {\r
+ if (webdav_path != null) {\r
+ mOAuthAuthEndpointText.setError("A valid authorization could not be obtained");\r
+ } else {\r
+ mOAuthAuthEndpointText.setError(getString(R.string.auth_bad_oc_version_title)); // should never happen \r
+ }\r
+ }\r
+ }\r
+\r
+ \r
+ /**\r
+ * Processes the result of the access check performed to try the user credentials.\r
+ * \r
+ * Creates a new account through the AccountManager.\r
+ * \r
+ * @param operation Access check performed.\r
+ * @param result Result of the operation.\r
+ */\r
+ private void onAuthorizationCheckFinish(ExistenceCheckOperation operation, RemoteOperationResult result) {\r
+ try {\r
+ dismissDialog(DIALOG_LOGIN_PROGRESS);\r
+ } catch (IllegalArgumentException e) {\r
+ // NOTHING TO DO ; can't find out what situation that leads to the exception in this code, but user logs signal that it happens\r
+ }\r
+ \r
+ boolean isOAuth = mOAuth2Check.isChecked();\r
+\r
+ if (result.isSuccess()) {\r
+ Log.d(TAG, "Successful access - time to save the account");\r
+\r
+ /// create and save new ownCloud account\r
+ Uri uri = Uri.parse(mHostBaseUrl);\r
+ String username = isOAuth ?\r
+ "OAuth_user" + (new java.util.Random(System.currentTimeMillis())).nextLong() :\r
+ mUsernameInput.getText().toString().trim();\r
+ // TODO a better way to set an account name\r
+ String accountName = username + "@" + uri.getHost();\r
+ if (uri.getPort() >= 0) {\r
+ accountName += ":" + uri.getPort();\r
+ }\r
+ Account account = new Account(accountName, AccountAuthenticator.ACCOUNT_TYPE);\r
+ AccountManager accManager = AccountManager.get(this);\r
+ if (isOAuth) {\r
+ accManager.addAccountExplicitly(account, "", null); // with our implementation, the password is never input in the app\r
+ } else {\r
+ accManager.addAccountExplicitly(account, mPasswordInput.getText().toString(), null);\r
+ }\r
+\r
+ /// add the new account as default in preferences, if there is none already\r
+ Account defaultAccount = AccountUtils.getCurrentOwnCloudAccount(this);\r
+ if (defaultAccount == null) {\r
+ SharedPreferences.Editor editor = PreferenceManager\r
+ .getDefaultSharedPreferences(this).edit();\r
+ editor.putString("select_oc_account", accountName);\r
+ editor.commit();\r
+ }\r
+\r
+\r
+ /// prepare result to return to the Authenticator\r
+ // TODO check again what the Authenticator makes with it; probably has the same effect as addAccountExplicitly, but it's not well done\r
+ final Intent intent = new Intent(); // TODO check if the intent can be retrieved from getIntent(), passed from AccountAuthenticator \r
+ intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, AccountAuthenticator.ACCOUNT_TYPE);\r
+ intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, account.name);\r
+ if (!isOAuth)\r
+ intent.putExtra(AccountManager.KEY_AUTHTOKEN, AccountAuthenticator.ACCOUNT_TYPE); // TODO check this; not sure it's right; maybe\r
+ intent.putExtra(AccountManager.KEY_USERDATA, username);\r
+ if (isOAuth) {\r
+ accManager.setAuthToken(account, AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN, mOAuthAccessToken);\r
+ }\r
+ /// add user data to the new account; TODO probably can be done in the last parameter addAccountExplicitly, or in KEY_USERDATA\r
+ accManager.setUserData(account, AccountAuthenticator.KEY_OC_VERSION, mDiscoveredVersion.toString());\r
+ accManager.setUserData(account, AccountAuthenticator.KEY_OC_BASE_URL, mHostBaseUrl);\r
+ if (isOAuth)\r
+ accManager.setUserData(account, AccountAuthenticator.KEY_SUPPORTS_OAUTH2, "TRUE"); // TODO this flag should be unnecessary\r
+\r
+ setAccountAuthenticatorResult(intent.getExtras());\r
+ setResult(RESULT_OK, intent);\r
+ \r
+ /// immediately request for the synchronization of the new account\r
+ Bundle bundle = new Bundle();\r
+ bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);\r
+ ContentResolver.requestSync(account, AccountAuthenticator.AUTHORITY, bundle);\r
+\r
+ finish();\r
+ \r
+ } else {\r
+ if (!isOAuth) {\r
+ mUsernameInput.setError(result.getLogMessage() + " "); \r
+ // the extra spaces are a workaround for an ugly bug: \r
+ // 1. insert wrong credentials and connect\r
+ // 2. put the focus on the user name field with using hardware controls (don't touch the screen); the error is shown UNDER the field\r
+ // 3. touch the user name field; the software keyboard appears; the error popup is moved OVER the field and SHRINKED in width, losing the last word\r
+ // Seen, at least, in Android 2.x devices \r
+ \r
+ } else {\r
+ mOAuthAuthEndpointText.setError(result.getLogMessage() + " ");\r
+ }\r
+ Log.d(TAG, "Access failed: " + result.getLogMessage());\r
+ }\r
+ }\r
+\r
+ \r
+ \r
+ /**\r
+ * {@inheritDoc}\r
+ * \r
+ * Necessary to update the contents of the SSL Dialog\r
+ * \r
+ * TODO move to some common place for all possible untrusted SSL failures\r
+ */\r
+ @Override\r
+ protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {\r
+ switch (id) {\r
+ case DIALOG_LOGIN_PROGRESS:\r
+ case DIALOG_CERT_NOT_SAVED:\r
+ case DIALOG_OAUTH2_LOGIN_PROGRESS:\r
+ break;\r
+ case DIALOG_SSL_VALIDATOR: {\r
+ ((SslValidatorDialog)dialog).updateResult(mLastSslUntrustedServerResult);\r
+ break;\r
+ }\r
+ default:\r
+ Log.e(TAG, "Incorrect dialog called with id = " + id);\r
}\r
- // END of saving the state of oAuth2 components.\r
- \r
- // Saving contents of each field.\r
- outState.putString(HOST_URL_TEXT,((TextView) findViewById(R.id.host_URL)).getText().toString().trim());\r
- outState.putString(ACCOUNT_USERNAME,((TextView) findViewById(R.id.account_username)).getText().toString().trim());\r
- outState.putString(ACCOUNT_PASSWORD,((TextView) findViewById(R.id.account_password)).getText().toString().trim());\r
- \r
- super.onSaveInstanceState(outState);\r
}\r
\r
+ \r
+ /**\r
+ * {@inheritDoc}\r
+ */\r
@Override\r
protected Dialog onCreateDialog(int id) {\r
Dialog dialog = null;\r
switch (id) {\r
case DIALOG_LOGIN_PROGRESS: {\r
+ /// simple progress dialog\r
ProgressDialog working_dialog = new ProgressDialog(this);\r
- working_dialog.setMessage(getResources().getString(\r
- R.string.auth_trying_to_login));\r
+ working_dialog.setMessage(getResources().getString(R.string.auth_trying_to_login));\r
working_dialog.setIndeterminate(true);\r
working_dialog.setCancelable(true);\r
working_dialog\r
.setOnCancelListener(new DialogInterface.OnCancelListener() {\r
@Override\r
public void onCancel(DialogInterface dialog) {\r
+ /// TODO study if this is enough\r
Log.i(TAG, "Login canceled");\r
- if (mAuthThread != null) {\r
- mAuthThread.interrupt();\r
+ if (mOperationThread != null) {\r
+ mOperationThread.interrupt();\r
finish();\r
}\r
}\r
dialog = working_dialog;\r
break;\r
}\r
- // oAuth2 dialog. We show here to the user the URL and user_code that the user must validate in a web browser.\r
- case OAUTH2_LOGIN_PROGRESS: {\r
+ case DIALOG_OAUTH2_LOGIN_PROGRESS: {\r
+ /// oAuth2 dialog. We show here to the user the URL and user_code that the user must validate in a web browser. - OLD!\r
+ // TODO optimize this dialog\r
ProgressDialog working_dialog = new ProgressDialog(this);\r
+ /* Leave the old OAuth flow\r
try {\r
- if (codeResponseJson != null && codeResponseJson.has(OAuth2GetCodeRunnable.CODE_VERIFICATION_URL)) {\r
+ if (mCodeResponseJson != null && mCodeResponseJson.has(OAuth2GetCodeRunnable.CODE_VERIFICATION_URL)) {\r
working_dialog.setMessage(String.format(getString(R.string.oauth_code_validation_message), \r
- codeResponseJson.getString(OAuth2GetCodeRunnable.CODE_VERIFICATION_URL), \r
- codeResponseJson.getString(OAuth2GetCodeRunnable.CODE_USER_CODE)));\r
- } else {\r
- working_dialog.setMessage(String.format("Getting authorization"));\r
- }\r
+ mCodeResponseJson.getString(OAuth2GetCodeRunnable.CODE_VERIFICATION_URL), \r
+ mCodeResponseJson.getString(OAuth2GetCodeRunnable.CODE_USER_CODE)));\r
+ } else {*/\r
+ working_dialog.setMessage(String.format("Getting authorization")); \r
+ /*}\r
} catch (JSONException e) {\r
Log.e(TAG, "onCreateDialog->JSONException: " + e.toString());\r
- }\r
+ }*/\r
working_dialog.setIndeterminate(true);\r
working_dialog.setCancelable(true);\r
working_dialog\r
@Override\r
public void onCancel(DialogInterface dialog) {\r
Log.i(TAG, "Login canceled");\r
- if (mOAuth2GetCodeThread != null) {\r
+ /*if (mOAuth2GetCodeThread != null) {\r
mOAuth2GetCodeThread.interrupt();\r
finish();\r
- } \r
- if (tokenReceiver != null) {\r
+ } */\r
+ /*if (tokenReceiver != null) {\r
unregisterReceiver(tokenReceiver);\r
tokenReceiver = null;\r
finish();\r
- }\r
+ }*/\r
+ finish();\r
}\r
});\r
dialog = working_dialog;\r
break;\r
}\r
case DIALOG_SSL_VALIDATOR: {\r
+ /// TODO start to use new dialog interface, at least for this (it is a FragmentDialog already)\r
dialog = SslValidatorDialog.newInstance(this, mLastSslUntrustedServerResult, this);\r
break;\r
}\r
return dialog;\r
}\r
\r
- @Override\r
- protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {\r
- switch (id) {\r
- case DIALOG_LOGIN_PROGRESS:\r
- case DIALOG_CERT_NOT_SAVED:\r
- case OAUTH2_LOGIN_PROGRESS:\r
- break;\r
- case DIALOG_SSL_VALIDATOR: {\r
- ((SslValidatorDialog)dialog).updateResult(mLastSslUntrustedServerResult);\r
- break;\r
- }\r
- default:\r
- Log.e(TAG, "Incorrect dialog called with id = " + id);\r
- }\r
- }\r
- \r
- @Override\r
- protected void onResume() {\r
- Log.d(TAG, "onResume() start");\r
- // (old oauth code) Registering token receiver. We must listening to the service that is pooling to the oAuth server for a token.\r
- if (tokenReceiver == null) {\r
- IntentFilter tokenFilter = new IntentFilter(OAuth2GetTokenService.TOKEN_RECEIVED_MESSAGE); \r
- tokenReceiver = new TokenReceiver();\r
- this.registerReceiver(tokenReceiver,tokenFilter);\r
- }\r
- // (new oauth code)\r
- /*if (mNewRedirectUriCaptured) {\r
- mNewRedirectUriCaptured = false;*/\r
- if (mNewCapturedUriFromOAuth2Redirection != null) {\r
- getOAuth2AccessTokenFromCapturedRedirection(); \r
- \r
- }\r
- super.onResume();\r
- }\r
-\r
- @Override\r
- protected void onPause() {\r
- Log.d(TAG, "onPause() start");\r
- super.onPause();\r
- } \r
- \r
-\r
- public void onAuthenticationResult(boolean success, String message) {\r
- if (success) {\r
- TextView username_text = (TextView) findViewById(R.id.account_username), password_text = (TextView) findViewById(R.id.account_password);\r
-\r
- URL url;\r
- try {\r
- url = new URL(message);\r
- } catch (MalformedURLException e) {\r
- // should never happen\r
- Log.e(getClass().getName(), "Malformed URL: " + message);\r
- return;\r
- }\r
-\r
- String username = username_text.getText().toString().trim();\r
- String accountName = username + "@" + url.getHost();\r
- if (url.getPort() >= 0) {\r
- accountName += ":" + url.getPort();\r
- }\r
- Account account = new Account(accountName,\r
- AccountAuthenticator.ACCOUNT_TYPE);\r
- AccountManager accManager = AccountManager.get(this);\r
- accManager.addAccountExplicitly(account, password_text.getText()\r
- .toString(), null);\r
-\r
- // Add this account as default in the preferences, if there is none\r
- // already\r
- Account defaultAccount = AccountUtils\r
- .getCurrentOwnCloudAccount(this);\r
- if (defaultAccount == null) {\r
- SharedPreferences.Editor editor = PreferenceManager\r
- .getDefaultSharedPreferences(this).edit();\r
- editor.putString("select_oc_account", accountName);\r
- editor.commit();\r
- }\r
-\r
- final Intent intent = new Intent();\r
- intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE,\r
- AccountAuthenticator.ACCOUNT_TYPE);\r
- intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, account.name);\r
- intent.putExtra(AccountManager.KEY_AUTHTOKEN,\r
- AccountAuthenticator.ACCOUNT_TYPE);\r
- intent.putExtra(AccountManager.KEY_USERDATA, username);\r
-\r
- accManager.setUserData(account, AccountAuthenticator.KEY_OC_URL,\r
- url.toString());\r
- accManager.setUserData(account,\r
- AccountAuthenticator.KEY_OC_VERSION, mDiscoveredVersion.toString());\r
- \r
- accManager.setUserData(account,\r
- AccountAuthenticator.KEY_OC_BASE_URL, mBaseUrl);\r
-\r
- setAccountAuthenticatorResult(intent.getExtras());\r
- setResult(RESULT_OK, intent);\r
- Bundle bundle = new Bundle();\r
- bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);\r
- //getContentResolver().startSync(ProviderTableMeta.CONTENT_URI,\r
- // bundle);\r
- ContentResolver.requestSync(account, "org.owncloud", bundle);\r
-\r
- /*\r
- * if\r
- * (mConnChkRunnable.getDiscoveredVersion().compareTo(OwnCloudVersion\r
- * .owncloud_v2) >= 0) { Intent i = new Intent(this,\r
- * ExtensionsAvailableActivity.class); startActivity(i); }\r
- */\r
-\r
- finish();\r
- } else {\r
- try {\r
- dismissDialog(DIALOG_LOGIN_PROGRESS);\r
- } catch (IllegalArgumentException e) {\r
- // NOTHING TO DO ; can't find out what situation that leads to the exception in this code, but user logs signal that it happens\r
- }\r
- TextView tv = (TextView) findViewById(R.id.account_username);\r
- tv.setError(message + " "); // the extra spaces are a workaround for an ugly bug: \r
- // 1. insert wrong credentials and connect\r
- // 2. put the focus on the user name field with using hardware controls (don't touch the screen); the error is shown UNDER the field\r
- // 3. touch the user name field; the software keyboard appears; the error popup is moved OVER the field and SHRINKED in width, losing the last word\r
- // Seen, at least, in Android 2.x devices\r
- }\r
- }\r
- public void onCancelClick(View view) {\r
- setResult(RESULT_CANCELED);\r
- finish();\r
- }\r
- \r
- public void onOkClick(View view) {\r
- String prefix = "";\r
- String url = ((TextView) findViewById(R.id.host_URL)).getText()\r
- .toString().trim();\r
- if (mIsSslConn) {\r
- prefix = "https://";\r
- } else {\r
- prefix = "http://";\r
- }\r
- if (url.toLowerCase().startsWith("http://")\r
- || url.toLowerCase().startsWith("https://")) {\r
- prefix = "";\r
- }\r
- CheckBox oAuth2Check = (CheckBox) findViewById(R.id.oauth_onOff_check);\r
- if (oAuth2Check != null && oAuth2Check.isChecked()) {\r
- startOauthorization();\r
- \r
- } else {\r
- continueConnection(prefix);\r
- }\r
- }\r
\r
- private void startOauthorization() {\r
- // We start a thread to get an authorization code from the oAuth2 server.\r
- setOAuth2ResultIconAndText(R.drawable.progress_small, R.string.oauth_login_connection);\r
- mOAuth2GetCodeRunnable = new OAuth2GetCodeRunnable(OAuth2Context.OAUTH2_F_AUTHORIZATION_ENDPOINT_URL, this);\r
- //mOAuth2GetCodeRunnable = new OAuth2GetCodeRunnable(OAuth2Context.OAUTH2_G_DEVICE_GETCODE_URL, this);\r
- mOAuth2GetCodeRunnable.setListener(this, mHandler);\r
- mOAuth2GetCodeThread = new Thread(mOAuth2GetCodeRunnable);\r
- mOAuth2GetCodeThread.start();\r
- }\r
-\r
+ /**\r
+ * Starts and activity to open the 'new account' page in the ownCloud web site\r
+ * \r
+ * @param view 'Account register' button\r
+ */\r
public void onRegisterClick(View view) {\r
Intent register = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.url_account_register)));\r
setResult(RESULT_CANCELED);\r
startActivity(register);\r
}\r
\r
- private void continueConnection(String prefix) {\r
- String url = ((TextView) findViewById(R.id.host_URL)).getText()\r
- .toString().trim();\r
- String username = ((TextView) findViewById(R.id.account_username))\r
- .getText().toString();\r
- String password = ((TextView) findViewById(R.id.account_password))\r
- .getText().toString();\r
- if (url.endsWith("/"))\r
- url = url.substring(0, url.length() - 1);\r
-\r
- URL uri = null;\r
- mDiscoveredVersion = mConnChkRunnable.getDiscoveredVersion();\r
- String webdav_path = AccountUtils.getWebdavPath(mDiscoveredVersion, false);\r
- \r
- if (webdav_path == null) {\r
- onAuthenticationResult(false, getString(R.string.auth_bad_oc_version_title));\r
- return;\r
- }\r
- \r
- try {\r
- mBaseUrl = prefix + url;\r
- String url_str = prefix + url + webdav_path;\r
- uri = new URL(url_str);\r
- } catch (MalformedURLException e) {\r
- // should never happen\r
- onAuthenticationResult(false, getString(R.string.auth_incorrect_address_title));\r
- return;\r
- }\r
-\r
- showDialog(DIALOG_LOGIN_PROGRESS);\r
- mAuthRunnable = new AuthenticationRunnable(uri, username, password, this);\r
- mAuthRunnable.setOnAuthenticationResultListener(this, mHandler);\r
- mAuthThread = new Thread(mAuthRunnable);\r
- mAuthThread.start();\r
- }\r
-\r
- @Override\r
- public void onConnectionCheckResult(ResultType type) {\r
- mStatusText = mStatusIcon = 0;\r
- mStatusCorrect = false;\r
- String t_url = ((TextView) findViewById(R.id.host_URL)).getText()\r
- .toString().trim().toLowerCase();\r
-\r
- switch (type) {\r
- case OK_SSL:\r
- mIsSslConn = true;\r
- mStatusIcon = android.R.drawable.ic_secure;\r
- mStatusText = R.string.auth_secure_connection;\r
- mStatusCorrect = true;\r
- break;\r
- case OK_NO_SSL:\r
- mIsSslConn = false;\r
- mStatusCorrect = true;\r
- if (t_url.startsWith("http://") ) {\r
- mStatusText = R.string.auth_connection_established;\r
- mStatusIcon = R.drawable.ic_ok;\r
- } else {\r
- mStatusText = R.string.auth_nossl_plain_ok_title;\r
- mStatusIcon = android.R.drawable.ic_partial_secure;\r
- }\r
- break;\r
- case BAD_OC_VERSION:\r
- mStatusIcon = R.drawable.common_error;\r
- mStatusText = R.string.auth_bad_oc_version_title;\r
- break;\r
- case WRONG_CONNECTION:\r
- mStatusIcon = R.drawable.common_error;\r
- mStatusText = R.string.auth_wrong_connection_title;\r
- break;\r
- case TIMEOUT:\r
- mStatusIcon = R.drawable.common_error;\r
- mStatusText = R.string.auth_timeout_title;\r
- break;\r
- case INCORRECT_ADDRESS:\r
- mStatusIcon = R.drawable.common_error;\r
- mStatusText = R.string.auth_incorrect_address_title;\r
- break;\r
- case SSL_UNVERIFIED_SERVER:\r
- mStatusIcon = R.drawable.common_error;\r
- mStatusText = R.string.auth_ssl_unverified_server_title;\r
- break;\r
- case SSL_INIT_ERROR:\r
- mStatusIcon = R.drawable.common_error;\r
- mStatusText = R.string.auth_ssl_general_error_title;\r
- break;\r
- case HOST_NOT_AVAILABLE:\r
- mStatusIcon = R.drawable.common_error;\r
- mStatusText = R.string.auth_unknown_host_title;\r
- break;\r
- case NO_NETWORK_CONNECTION:\r
- mStatusIcon = R.drawable.no_network;\r
- mStatusText = R.string.auth_no_net_conn_title;\r
- break;\r
- case INSTANCE_NOT_CONFIGURED:\r
- mStatusIcon = R.drawable.common_error;\r
- mStatusText = R.string.auth_not_configured_title;\r
- break;\r
- case UNKNOWN_ERROR:\r
- mStatusIcon = R.drawable.common_error;\r
- mStatusText = R.string.auth_unknown_error_title;\r
- break;\r
- case FILE_NOT_FOUND:\r
- mStatusIcon = R.drawable.common_error;\r
- mStatusText = R.string.auth_incorrect_path_title;\r
- break;\r
- default:\r
- Log.e(TAG, "Incorrect connection checker result type: " + type);\r
- }\r
- setResultIconAndText(mStatusIcon, mStatusText);\r
- if (!mStatusCorrect)\r
- findViewById(R.id.refreshButton).setVisibility(View.VISIBLE);\r
- else\r
- findViewById(R.id.refreshButton).setVisibility(View.INVISIBLE);\r
- findViewById(R.id.buttonOK).setEnabled(mStatusCorrect);\r
- }\r
-\r
- public void onFocusChange(View view, boolean hasFocus) {\r
- if (view.getId() == R.id.host_URL) {\r
- if (!hasFocus) {\r
- TextView tv = ((TextView) findViewById(R.id.host_URL));\r
- String uri = tv.getText().toString().trim();\r
- if (uri.length() != 0) {\r
- setResultIconAndText(R.drawable.progress_small,\r
- R.string.auth_testing_connection);\r
- //mConnChkRunnable = new ConnectionCheckerRunnable(uri, this);\r
- mConnChkRunnable = new ConnectionCheckOperation(uri, this);\r
- //mConnChkRunnable.setListener(this, mHandler);\r
- //mAuthThread = new Thread(mConnChkRunnable);\r
- //mAuthThread.start();\r
- WebdavClient client = OwnCloudClientUtils.createOwnCloudClient(Uri.parse(uri), this);\r
- mDiscoveredVersion = null;\r
- mAuthThread = mConnChkRunnable.execute(client, this, mHandler);\r
- } else {\r
- findViewById(R.id.refreshButton).setVisibility(\r
- View.INVISIBLE);\r
- setResultIconAndText(0, 0);\r
- }\r
- } else {\r
- // avoids that the 'connect' button can be clicked if the test was previously passed\r
- findViewById(R.id.buttonOK).setEnabled(false); \r
- }\r
- } else if (view.getId() == R.id.account_password) {\r
- ImageView iv = (ImageView) findViewById(R.id.viewPassword);\r
- if (hasFocus) {\r
- iv.setVisibility(View.VISIBLE);\r
- } else {\r
- TextView v = (TextView) findViewById(R.id.account_password);\r
- int input_type = InputType.TYPE_CLASS_TEXT\r
- | InputType.TYPE_TEXT_VARIATION_PASSWORD;\r
- v.setInputType(input_type);\r
- iv.setVisibility(View.INVISIBLE);\r
- }\r
- }\r
- }\r
-\r
- private void setResultIconAndText(int drawable_id, int text_id) {\r
+ \r
+ /**\r
+ * Updates the content and visibility state of the icon and text associated\r
+ * to the last check on the ownCloud server.\r
+ */\r
+ private void updateOcServerCheckIconAndText() {\r
ImageView iv = (ImageView) findViewById(R.id.action_indicator);\r
TextView tv = (TextView) findViewById(R.id.status_text);\r
\r
- if (drawable_id == 0 && text_id == 0) {\r
+ if (mStatusIcon == 0 && mStatusText == 0) {\r
iv.setVisibility(View.INVISIBLE);\r
tv.setVisibility(View.INVISIBLE);\r
} else {\r
- iv.setImageResource(drawable_id);\r
- tv.setText(text_id);\r
+ iv.setImageResource(mStatusIcon);\r
+ tv.setText(mStatusText);\r
iv.setVisibility(View.VISIBLE);\r
tv.setVisibility(View.VISIBLE);\r
}\r
}\r
\r
- @Override\r
- public void onClick(View v) {\r
- if (v.getId() == R.id.refreshButton) {\r
- onFocusChange(findViewById(R.id.host_URL), false);\r
- } else if (v.getId() == R.id.viewPassword) {\r
- EditText view = (EditText) findViewById(R.id.account_password);\r
- int selectionStart = view.getSelectionStart();\r
- int selectionEnd = view.getSelectionEnd();\r
- int input_type = view.getInputType();\r
- if ((input_type & InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) {\r
- input_type = InputType.TYPE_CLASS_TEXT\r
- | InputType.TYPE_TEXT_VARIATION_PASSWORD;\r
- } else {\r
- input_type = InputType.TYPE_CLASS_TEXT\r
- | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD;\r
- }\r
- view.setInputType(input_type);\r
- view.setSelection(selectionStart, selectionEnd);\r
- }\r
+ \r
+ /**\r
+ * Called when the refresh button in the input field for ownCloud host is clicked.\r
+ * \r
+ * Performs a new check on the URL in the input field.\r
+ * \r
+ * @param view Refresh 'button'\r
+ */\r
+ public void onRefreshClick(View view) {\r
+ onFocusChange(mRefreshButton, false);\r
}\r
\r
- @Override protected void onDestroy() { \r
- // We must stop the service thats it's pooling to oAuth2 server for a token.\r
- Intent tokenService = new Intent(this, OAuth2GetTokenService.class);\r
- stopService(tokenService);\r
- \r
- // We stop listening the result of the pooling service.\r
- if (tokenReceiver != null) {\r
- unregisterReceiver(tokenReceiver);\r
- tokenReceiver = null;\r
- finish();\r
+ \r
+ /**\r
+ * Called when the eye icon in the password field is clicked.\r
+ * \r
+ * Toggles the visibility of the password in the field. \r
+ * \r
+ * @param view 'View password' 'button'\r
+ */\r
+ public void onViewPasswordClick(View view) {\r
+ int selectionStart = mPasswordInput.getSelectionStart();\r
+ int selectionEnd = mPasswordInput.getSelectionEnd();\r
+ int input_type = mPasswordInput.getInputType();\r
+ if ((input_type & InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) {\r
+ input_type = InputType.TYPE_CLASS_TEXT\r
+ | InputType.TYPE_TEXT_VARIATION_PASSWORD;\r
+ } else {\r
+ input_type = InputType.TYPE_CLASS_TEXT\r
+ | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD;\r
}\r
-\r
- super.onDestroy();\r
+ mPasswordInput.setInputType(input_type);\r
+ mPasswordInput.setSelection(selectionStart, selectionEnd);\r
} \r
\r
- // Controlling the oAuth2 checkbox on the activity: hide and show widgets.\r
- public void onOff_check_Click(View view) {\r
+ \r
+ /**\r
+ * Called when the checkbox for OAuth authorization is clicked.\r
+ * \r
+ * Hides or shows the input fields for user & password. \r
+ * \r
+ * @param view 'View password' 'button'\r
+ */\r
+ public void onCheckClick(View view) {\r
CheckBox oAuth2Check = (CheckBox)view; \r
changeViewByOAuth2Check(oAuth2Check.isChecked());\r
\r
}\r
\r
+ /**\r
+ * Changes the visibility of input elements depending upon the kind of authorization\r
+ * chosen by the user: basic or OAuth\r
+ * \r
+ * @param checked 'True' when OAuth is selected.\r
+ */\r
public void changeViewByOAuth2Check(Boolean checked) {\r
\r
- EditText oAuth2Url = (EditText) findViewById(R.id.oAuth_URL);\r
- EditText accountUsername = (EditText) findViewById(R.id.account_username);\r
- EditText accountPassword = (EditText) findViewById(R.id.account_password);\r
- ImageView viewPassword = (ImageView) findViewById(R.id.viewPassword); \r
ImageView auth2ActionIndicator = (ImageView) findViewById(R.id.auth2_action_indicator); \r
TextView oauth2StatusText = (TextView) findViewById(R.id.oauth2_status_text); \r
\r
if (checked) {\r
- oAuth2Url.setVisibility(View.VISIBLE);\r
- accountUsername.setVisibility(View.GONE);\r
- accountPassword.setVisibility(View.GONE);\r
- viewPassword.setVisibility(View.GONE);\r
+ mOAuthAuthEndpointText.setVisibility(View.VISIBLE);\r
+ mOAuthTokenEndpointText.setVisibility(View.VISIBLE);\r
+ mUsernameInput.setVisibility(View.GONE);\r
+ mPasswordInput.setVisibility(View.GONE);\r
+ mViewPasswordButton.setVisibility(View.GONE);\r
auth2ActionIndicator.setVisibility(View.INVISIBLE);\r
oauth2StatusText.setVisibility(View.INVISIBLE);\r
} else {\r
- oAuth2Url.setVisibility(View.GONE);\r
- accountUsername.setVisibility(View.VISIBLE);\r
- accountPassword.setVisibility(View.VISIBLE);\r
- viewPassword.setVisibility(View.INVISIBLE);\r
+ mOAuthAuthEndpointText.setVisibility(View.GONE);\r
+ mOAuthTokenEndpointText.setVisibility(View.GONE);\r
+ mUsernameInput.setVisibility(View.VISIBLE);\r
+ mPasswordInput.setVisibility(View.VISIBLE);\r
+ mViewPasswordButton.setVisibility(View.INVISIBLE);\r
auth2ActionIndicator.setVisibility(View.GONE);\r
oauth2StatusText.setVisibility(View.GONE);\r
} \r
\r
} \r
\r
- // Controlling the oAuth2 result of server connection.\r
- private void setOAuth2ResultIconAndText(int drawable_id, int text_id) {\r
+ /**\r
+ * Updates the content and visibility state of the icon and text associated\r
+ * to the interactions with the OAuth authorization server.\r
+ * \r
+ * @param drawable_id Resource id for the icon.\r
+ * @param text_id Resource id for the text.\r
+ */\r
+ private void updateOAuth2IconAndText(int drawable_id, int text_id) {\r
ImageView iv = (ImageView) findViewById(R.id.auth2_action_indicator);\r
TextView tv = (TextView) findViewById(R.id.oauth2_status_text);\r
\r
}\r
} \r
\r
+ /* Leave the old OAuth flow\r
// Results from the first call to oAuth2 server : getting the user_code and verification_url.\r
@Override\r
public void onOAuth2GetCodeResult(ResultOAuthType type, JSONObject responseJson) {\r
if ((type == ResultOAuthType.OK_SSL)||(type == ResultOAuthType.OK_NO_SSL)) {\r
- codeResponseJson = responseJson;\r
- if (codeResponseJson != null) {\r
+ mCodeResponseJson = responseJson;\r
+ if (mCodeResponseJson != null) {\r
getOAuth2AccessTokenFromJsonResponse();\r
} // else - nothing to do here - wait for callback !!!\r
\r
} else if (type == ResultOAuthType.HOST_NOT_AVAILABLE) {\r
- setOAuth2ResultIconAndText(R.drawable.common_error, R.string.oauth_connection_url_unavailable);\r
+ updateOAuth2IconAndText(R.drawable.common_error, R.string.oauth_connection_url_unavailable);\r
}\r
}\r
\r
int expiresIn = -1;\r
int interval = -1;\r
\r
- Log.d(TAG, "ResponseOAuth2->" + codeResponseJson.toString());\r
+ Log.d(TAG, "ResponseOAuth2->" + mCodeResponseJson.toString());\r
\r
try {\r
// We get data that we must show to the user or we will use internally.\r
- verificationUrl = codeResponseJson.getString(OAuth2GetCodeRunnable.CODE_VERIFICATION_URL);\r
- userCode = codeResponseJson.getString(OAuth2GetCodeRunnable.CODE_USER_CODE);\r
- expiresIn = codeResponseJson.getInt(OAuth2GetCodeRunnable.CODE_EXPIRES_IN); \r
+ verificationUrl = mCodeResponseJson.getString(OAuth2GetAuthorizationToken.CODE_VERIFICATION_URL);\r
+ userCode = mCodeResponseJson.getString(OAuth2GetAuthorizationToken.CODE_USER_CODE);\r
+ expiresIn = mCodeResponseJson.getInt(OAuth2GetAuthorizationToken.CODE_EXPIRES_IN); \r
\r
// And we get data that we must use to get a token.\r
- deviceCode = codeResponseJson.getString(OAuth2GetCodeRunnable.CODE_DEVICE_CODE);\r
- interval = codeResponseJson.getInt(OAuth2GetCodeRunnable.CODE_INTERVAL);\r
+ deviceCode = mCodeResponseJson.getString(OAuth2GetAuthorizationToken.CODE_DEVICE_CODE);\r
+ interval = mCodeResponseJson.getInt(OAuth2GetAuthorizationToken.CODE_INTERVAL);\r
\r
} catch (JSONException e) {\r
Log.e(TAG, "Exception accesing data in Json object" + e.toString());\r
}\r
\r
// Updating status widget to OK.\r
- setOAuth2ResultIconAndText(R.drawable.ic_ok, R.string.auth_connection_established);\r
+ updateOAuth2IconAndText(R.drawable.ic_ok, R.string.auth_connection_established);\r
\r
// Showing the dialog with instructions for the user.\r
- showDialog(OAUTH2_LOGIN_PROGRESS);\r
+ showDialog(DIALOG_OAUTH2_LOGIN_PROGRESS);\r
\r
// Loggin all the data.\r
Log.d(TAG, "verificationUrl->" + verificationUrl);\r
}\r
\r
} \r
+ */\r
\r
- private void getOAuth2AccessTokenFromCapturedRedirection() {\r
- Map<String, String> responseValues = new HashMap<String, String>();\r
- //String queryParameters = getIntent().getData().getQuery();\r
- String queryParameters = mNewCapturedUriFromOAuth2Redirection.getQuery();\r
- mNewCapturedUriFromOAuth2Redirection = null;\r
- \r
- Log.v(TAG, "Queryparameters (Code) = " + queryParameters);\r
-\r
- String[] pairs = queryParameters.split("&");\r
- Log.v(TAG, "Pairs (Code) = " + pairs.toString());\r
-\r
- int i = 0;\r
- String key = "";\r
- String value = "";\r
-\r
- StringBuilder sb = new StringBuilder();\r
-\r
- while (pairs.length > i) {\r
- int j = 0;\r
- String[] part = pairs[i].split("=");\r
-\r
- while (part.length > j) {\r
- String p = part[j];\r
- if (j == 0) {\r
- key = p;\r
- sb.append(key + " = ");\r
- } else if (j == 1) {\r
- value = p;\r
- responseValues.put(key, value);\r
- sb.append(value + "\n");\r
- }\r
-\r
- Log.v(TAG, "[" + i + "," + j + "] = " + p);\r
- j++;\r
- }\r
- i++;\r
- }\r
- \r
- \r
- // Updating status widget to OK.\r
- setOAuth2ResultIconAndText(R.drawable.ic_ok, R.string.auth_connection_established);\r
- \r
- // Showing the dialog with instructions for the user.\r
- showDialog(OAUTH2_LOGIN_PROGRESS);\r
-\r
- // \r
- RemoteOperation operation = new GetOAuth2AccessToken(responseValues);\r
- WebdavClient client = OwnCloudClientUtils.createOwnCloudClient(Uri.parse(OAuth2Context.OAUTH2_F_TOKEN_ENDPOINT_URL), getApplicationContext());\r
- operation.execute(client, this, mHandler);\r
- }\r
-\r
- \r
-\r
+ /* Leave the old OAuth flow\r
// We get data from the oAuth2 token service with this broadcast receiver.\r
private class TokenReceiver extends BroadcastReceiver {\r
/**\r
* The token is received.\r
* @author\r
* {@link BroadcastReceiver} to enable oAuth2 token receiving.\r
- */\r
+ *-/\r
@Override\r
public void onReceive(Context context, Intent intent) {\r
@SuppressWarnings("unchecked")\r
HashMap<String, String> tokenResponse = (HashMap<String, String>)intent.getExtras().get(OAuth2GetTokenService.TOKEN_RECEIVED_DATA);\r
Log.d(TAG, "TokenReceiver->" + tokenResponse.get(OAuth2GetTokenService.TOKEN_ACCESS_TOKEN));\r
- dismissDialog(OAUTH2_LOGIN_PROGRESS);\r
+ dismissDialog(DIALOG_OAUTH2_LOGIN_PROGRESS);\r
\r
}\r
}\r
+ */\r
\r
- @Override\r
- public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {\r
- if (operation instanceof ConnectionCheckOperation) {\r
- \r
- mStatusText = mStatusIcon = 0;\r
- mStatusCorrect = false;\r
- String t_url = ((TextView) findViewById(R.id.host_URL)).getText()\r
- .toString().trim().toLowerCase();\r
- \r
- switch (result.getCode()) {\r
- case OK_SSL:\r
- mIsSslConn = true;\r
- mStatusIcon = android.R.drawable.ic_secure;\r
- mStatusText = R.string.auth_secure_connection;\r
- mStatusCorrect = true;\r
- break;\r
- \r
- case OK_NO_SSL:\r
- case OK:\r
- mIsSslConn = false;\r
- mStatusCorrect = true;\r
- if (t_url.startsWith("http://") ) {\r
- mStatusText = R.string.auth_connection_established;\r
- mStatusIcon = R.drawable.ic_ok;\r
- } else {\r
- mStatusText = R.string.auth_nossl_plain_ok_title;\r
- mStatusIcon = android.R.drawable.ic_partial_secure;\r
- }\r
- break;\r
- \r
- \r
- case BAD_OC_VERSION:\r
- mStatusIcon = R.drawable.common_error;\r
- mStatusText = R.string.auth_bad_oc_version_title;\r
- break;\r
- case WRONG_CONNECTION:\r
- mStatusIcon = R.drawable.common_error;\r
- mStatusText = R.string.auth_wrong_connection_title;\r
- break;\r
- case TIMEOUT:\r
- mStatusIcon = R.drawable.common_error;\r
- mStatusText = R.string.auth_timeout_title;\r
- break;\r
- case INCORRECT_ADDRESS:\r
- mStatusIcon = R.drawable.common_error;\r
- mStatusText = R.string.auth_incorrect_address_title;\r
- break;\r
- \r
- case SSL_RECOVERABLE_PEER_UNVERIFIED:\r
- mStatusIcon = R.drawable.common_error;\r
- mStatusText = R.string.auth_ssl_unverified_server_title;\r
- mLastSslUntrustedServerResult = result;\r
- showDialog(DIALOG_SSL_VALIDATOR); \r
- break;\r
- \r
- case SSL_ERROR:\r
- mStatusIcon = R.drawable.common_error;\r
- mStatusText = R.string.auth_ssl_general_error_title;\r
- break;\r
- \r
- case HOST_NOT_AVAILABLE:\r
- mStatusIcon = R.drawable.common_error;\r
- mStatusText = R.string.auth_unknown_host_title;\r
- break;\r
- case NO_NETWORK_CONNECTION:\r
- mStatusIcon = R.drawable.no_network;\r
- mStatusText = R.string.auth_no_net_conn_title;\r
- break;\r
- case INSTANCE_NOT_CONFIGURED:\r
- mStatusIcon = R.drawable.common_error;\r
- mStatusText = R.string.auth_not_configured_title;\r
- break;\r
- case FILE_NOT_FOUND:\r
- mStatusIcon = R.drawable.common_error;\r
- mStatusText = R.string.auth_incorrect_path_title;\r
- break;\r
- case UNHANDLED_HTTP_CODE:\r
- case UNKNOWN_ERROR:\r
- mStatusIcon = R.drawable.common_error;\r
- mStatusText = R.string.auth_unknown_error_title;\r
- break;\r
- default:\r
- Log.e(TAG, "Incorrect connection checker result type: " + result.getHttpCode());\r
- }\r
- setResultIconAndText(mStatusIcon, mStatusText);\r
- if (!mStatusCorrect)\r
- findViewById(R.id.refreshButton).setVisibility(View.VISIBLE);\r
- else\r
- findViewById(R.id.refreshButton).setVisibility(View.INVISIBLE);\r
- findViewById(R.id.buttonOK).setEnabled(mStatusCorrect);\r
- \r
- } else if (operation instanceof GetOAuth2AccessToken) {\r
-\r
- try {\r
- dismissDialog(OAUTH2_LOGIN_PROGRESS);\r
- } catch (IllegalArgumentException e) {\r
- // NOTHING TO DO ; can't find out what situation that leads to the exception in this code, but user logs signal that it happens\r
- }\r
-\r
- if (result.isSuccess()) {\r
- \r
- /// time to test the retrieved access token on the ownCloud server\r
- String url = ((TextView) findViewById(R.id.host_URL)).getText()\r
- .toString().trim();\r
- if (url.endsWith("/"))\r
- url = url.substring(0, url.length() - 1);\r
-\r
- Uri uri = null;\r
- /*String webdav_path = AccountUtils.getWebdavPath(mDiscoveredVersion);\r
- \r
- if (webdav_path == null) {\r
- onAuthenticationResult(false, getString(R.string.auth_bad_oc_version_title));\r
- return;\r
- }*/\r
- \r
- String prefix = "";\r
- if (mIsSslConn) {\r
- prefix = "https://";\r
- } else {\r
- prefix = "http://";\r
- }\r
- if (url.toLowerCase().startsWith("http://")\r
- || url.toLowerCase().startsWith("https://")) {\r
- prefix = "";\r
- }\r
- \r
- try {\r
- mBaseUrl = prefix + url;\r
- //String url_str = prefix + url + webdav_path;\r
- String url_str = prefix + url + "/remote.php/odav";\r
- uri = Uri.parse(url_str);\r
- \r
- } catch (Exception e) {\r
- // should never happen\r
- onAuthenticationResult(false, getString(R.string.auth_incorrect_address_title));\r
- return;\r
- }\r
-\r
- showDialog(DIALOG_LOGIN_PROGRESS);\r
- String accessToken = ((GetOAuth2AccessToken)operation).getResultTokenMap().get(OAuth2Context.KEY_ACCESS_TOKEN);\r
- Log.d(TAG, "Got ACCESS TOKEN: " + accessToken);\r
- mAuthChkOperation = new ExistenceCheckOperation("", this, accessToken);\r
- WebdavClient client = OwnCloudClientUtils.createOwnCloudClient(uri, getApplicationContext());\r
- mAuthChkOperation.execute(client, this, mHandler);\r
- \r
- \r
- } else {\r
- TextView tv = (TextView) findViewById(R.id.oAuth_URL);\r
- tv.setError("A valid authorization could not be obtained");\r
-\r
- }\r
- \r
- } else if (operation instanceof ExistenceCheckOperation) {\r
- \r
- try {\r
- dismissDialog(DIALOG_LOGIN_PROGRESS);\r
- } catch (IllegalArgumentException e) {\r
- // NOTHING TO DO ; can't find out what situation that leads to the exception in this code, but user logs signal that it happens\r
- }\r
- \r
- if (result.isSuccess()) {\r
- TextView tv = (TextView) findViewById(R.id.oAuth_URL);\r
- Log.d(TAG, "Checked access - time to save the account");\r
- \r
- Uri uri = Uri.parse(mBaseUrl);\r
- String username = "OAuth_user" + (new java.util.Random(System.currentTimeMillis())).nextLong(); \r
- String accountName = username + "@" + uri.getHost();\r
- if (uri.getPort() >= 0) {\r
- accountName += ":" + uri.getPort();\r
- }\r
- // TODO - check that accountName does not exist\r
- Account account = new Account(accountName, AccountAuthenticator.ACCOUNT_TYPE);\r
- AccountManager accManager = AccountManager.get(this);\r
- accManager.addAccountExplicitly(account, "", null); // with our implementation, the password is never input in the app\r
-\r
- // Add this account as default in the preferences, if there is none\r
- Account defaultAccount = AccountUtils.getCurrentOwnCloudAccount(this);\r
- if (defaultAccount == null) {\r
- SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();\r
- editor.putString("select_oc_account", accountName);\r
- editor.commit();\r
- }\r
-\r
- /// account data to save by the AccountManager\r
- final Intent intent = new Intent();\r
- intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, AccountAuthenticator.ACCOUNT_TYPE);\r
- intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, account.name);\r
- intent.putExtra(AccountManager.KEY_USERDATA, username);\r
-\r
- accManager.setAuthToken(account, AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN, ((ExistenceCheckOperation) operation).getAccessToken());\r
- \r
- accManager.setUserData(account, AccountAuthenticator.KEY_OC_VERSION, mConnChkRunnable.getDiscoveredVersion().toString());\r
- accManager.setUserData(account, AccountAuthenticator.KEY_OC_BASE_URL, mBaseUrl);\r
- accManager.setUserData(account, AccountAuthenticator.KEY_SUPPORTS_OAUTH2, "TRUE");\r
-\r
- setAccountAuthenticatorResult(intent.getExtras());\r
- setResult(RESULT_OK, intent);\r
- \r
- /// enforce the first account synchronization\r
- Bundle bundle = new Bundle();\r
- bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);\r
- ContentResolver.requestSync(account, "org.owncloud", bundle);\r
-\r
- finish();\r
- \r
- } else { \r
- TextView tv = (TextView) findViewById(R.id.oAuth_URL);\r
- tv.setError(result.getLogMessage());\r
- Log.d(TAG, "Access failed: " + result.getLogMessage());\r
- }\r
- }\r
- }\r
-\r
- \r
+ \r
+ /**\r
+ * Called from SslValidatorDialog when a new server certificate was correctly saved.\r
+ */\r
public void onSavedCertificate() {\r
- mAuthThread = mConnChkRunnable.retry(this, mHandler); \r
+ mOperationThread = mOcServerChkOperation.retry(this, mHandler); \r
}\r
\r
+ /**\r
+ * Called from SslValidatorDialog when a new server certificate could not be saved \r
+ * when the user requested it.\r
+ */\r
@Override\r
public void onFailedSavingCertificate() {\r
showDialog(DIALOG_CERT_NOT_SAVED);\r