From: David A. Velasco Date: Thu, 17 Jan 2013 13:25:49 +0000 (+0100) Subject: Merge branch 'master' into oauth_login X-Git-Tag: oc-android-1.4.3~29^2~22 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/2b5785556e7f7c3cefc5f084949e091f80abed6d?ds=inline;hp=--cc Merge branch 'master' into oauth_login Conflicts: AndroidManifest.xml res/values-de-rDE/strings.xml res/values-de/strings.xml res/values/strings.xml src/com/owncloud/android/Uploader.java src/com/owncloud/android/datamodel/FileDataStorageManager.java src/com/owncloud/android/files/OwnCloudFileObserver.java src/com/owncloud/android/files/services/FileDownloader.java src/com/owncloud/android/files/services/FileUploader.java src/com/owncloud/android/operations/RemoteOperationResult.java src/com/owncloud/android/syncadapter/FileSyncAdapter.java src/com/owncloud/android/ui/activity/AuthenticatorActivity.java src/com/owncloud/android/ui/activity/FileDisplayActivity.java src/com/owncloud/android/ui/activity/UploadFilesActivity.java src/com/owncloud/android/ui/fragment/FileDetailFragment.java src/com/owncloud/android/ui/fragment/OCFileListFragment.java --- 2b5785556e7f7c3cefc5f084949e091f80abed6d diff --cc src/com/owncloud/android/Uploader.java index 0db4871d,e19a9724..6ec8c4fd --- a/src/com/owncloud/android/Uploader.java +++ b/src/com/owncloud/android/Uploader.java @@@ -30,7 -30,7 +30,6 @@@ import com.owncloud.android.datamodel.D import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.files.services.FileUploader; --import com.owncloud.android.network.OwnCloudClientUtils; import android.accounts.Account; import android.accounts.AccountManager; @@@ -60,7 -60,7 +59,6 @@@ import android.widget.SimpleAdapter import android.widget.Toast; import com.owncloud.android.R; --import eu.alefzero.webdav.WebdavClient; /** * This can be used to upload things to an ownCloud instance. diff --cc src/com/owncloud/android/files/OwnCloudFileObserver.java index 9dde939b,8a03fee6..d79a07e2 --- a/src/com/owncloud/android/files/OwnCloudFileObserver.java +++ b/src/com/owncloud/android/files/OwnCloudFileObserver.java @@@ -22,13 -22,13 +22,11 @@@ import java.io.File import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; --import com.owncloud.android.network.OwnCloudClientUtils; import com.owncloud.android.operations.RemoteOperationResult; import com.owncloud.android.operations.SynchronizeFileOperation; import com.owncloud.android.operations.RemoteOperationResult.ResultCode; import com.owncloud.android.ui.activity.ConflictsResolveActivity; --import eu.alefzero.webdav.WebdavClient; import android.accounts.Account; import android.content.Context; diff --cc src/com/owncloud/android/files/services/FileDownloader.java index 665290cf,7872c367..ed83d863 --- a/src/com/owncloud/android/files/services/FileDownloader.java +++ b/src/com/owncloud/android/files/services/FileDownloader.java @@@ -264,19 -263,17 +265,19 @@@ public class FileDownloader extends Ser notifyDownloadStart(mCurrentDownload); - /// prepare client object to send the request to the ownCloud server - if (mDownloadClient == null || !mLastAccount.equals(mCurrentDownload.getAccount())) { - mLastAccount = mCurrentDownload.getAccount(); - mStorageManager = new FileDataStorageManager(mLastAccount, getContentResolver()); - mDownloadClient = OwnCloudClientUtils.createOwnCloudClient(mLastAccount, getApplicationContext()); - } - - /// perform the download RemoteOperationResult downloadResult = null; try { - downloadResult = mCurrentDownload.execute(mDownloadClient); + /// prepare client object to send the request to the ownCloud server + if (mDownloadClient == null || !mLastAccount.equals(mCurrentDownload.getAccount())) { + mLastAccount = mCurrentDownload.getAccount(); + mStorageManager = new FileDataStorageManager(mLastAccount, getContentResolver()); - mDownloadClient = OwnCloudClientUtils.createOwnCloudClient(mLastAccount, this); ++ mDownloadClient = OwnCloudClientUtils.createOwnCloudClient(mLastAccount, getApplicationContext()); + } + + /// perform the download + if (downloadResult == null) { + downloadResult = mCurrentDownload.execute(mDownloadClient); + } if (downloadResult.isSuccess()) { saveDownloadedFile(); } diff --cc src/com/owncloud/android/operations/RemoteOperationResult.java index b26fc378,64703386..5a14fb87 --- a/src/com/owncloud/android/operations/RemoteOperationResult.java +++ b/src/com/owncloud/android/operations/RemoteOperationResult.java @@@ -45,7 -45,8 +45,7 @@@ import com.owncloud.android.network.Cer public class RemoteOperationResult implements Serializable { /** Generated - should be refreshed every time the class changes!! */ - private static final long serialVersionUID = 5336333154035462033L; + private static final long serialVersionUID = -7805531062432602444L; - public enum ResultCode { OK, diff --cc src/com/owncloud/android/ui/activity/AuthenticatorActivity.java index fb5c711b,bd36ed1c..fc84df60 --- a/src/com/owncloud/android/ui/activity/AuthenticatorActivity.java +++ b/src/com/owncloud/android/ui/activity/AuthenticatorActivity.java @@@ -697,210 -498,9 +699,211 @@@ public class AuthenticatorActivity exte | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD; } view.setInputType(input_type); + view.setSelection(selectionStart, selectionEnd); } } + + @Override protected void onDestroy() { + // We must stop the service thats it's pooling to oAuth2 server for a token. + Intent tokenService = new Intent(this, OAuth2GetTokenService.class); + stopService(tokenService); + + // We stop listening the result of the pooling service. + if (tokenReceiver != null) { + unregisterReceiver(tokenReceiver); + tokenReceiver = null; + finish(); + } + + super.onDestroy(); + } + + // Controlling the oAuth2 checkbox on the activity: hide and show widgets. + public void onOff_check_Click(View view) { + CheckBox oAuth2Check = (CheckBox)view; + changeViewByOAuth2Check(oAuth2Check.isChecked()); + + } + + public void changeViewByOAuth2Check(Boolean checked) { + + EditText oAuth2Url = (EditText) findViewById(R.id.oAuth_URL); + EditText accountUsername = (EditText) findViewById(R.id.account_username); + EditText accountPassword = (EditText) findViewById(R.id.account_password); + ImageView viewPassword = (ImageView) findViewById(R.id.viewPassword); + ImageView auth2ActionIndicator = (ImageView) findViewById(R.id.auth2_action_indicator); + TextView oauth2StatusText = (TextView) findViewById(R.id.oauth2_status_text); + + if (checked) { + oAuth2Url.setVisibility(View.VISIBLE); + accountUsername.setVisibility(View.GONE); + accountPassword.setVisibility(View.GONE); + viewPassword.setVisibility(View.GONE); + auth2ActionIndicator.setVisibility(View.INVISIBLE); + oauth2StatusText.setVisibility(View.INVISIBLE); + } else { + oAuth2Url.setVisibility(View.GONE); + accountUsername.setVisibility(View.VISIBLE); + accountPassword.setVisibility(View.VISIBLE); + viewPassword.setVisibility(View.INVISIBLE); + auth2ActionIndicator.setVisibility(View.GONE); + oauth2StatusText.setVisibility(View.GONE); + } + + } + + // Controlling the oAuth2 result of server connection. + private void setOAuth2ResultIconAndText(int drawable_id, int text_id) { + ImageView iv = (ImageView) findViewById(R.id.auth2_action_indicator); + TextView tv = (TextView) findViewById(R.id.oauth2_status_text); + + if (drawable_id == 0 && text_id == 0) { + iv.setVisibility(View.INVISIBLE); + tv.setVisibility(View.INVISIBLE); + } else { + iv.setImageResource(drawable_id); + tv.setText(text_id); + iv.setVisibility(View.VISIBLE); + tv.setVisibility(View.VISIBLE); + } + } + + // Results from the first call to oAuth2 server : getting the user_code and verification_url. + @Override + public void onOAuth2GetCodeResult(ResultOAuthType type, JSONObject responseJson) { + if ((type == ResultOAuthType.OK_SSL)||(type == ResultOAuthType.OK_NO_SSL)) { + codeResponseJson = responseJson; + if (codeResponseJson != null) { + getOAuth2AccessTokenFromJsonResponse(); + } // else - nothing to do here - wait for callback !!! + + } else if (type == ResultOAuthType.HOST_NOT_AVAILABLE) { + setOAuth2ResultIconAndText(R.drawable.common_error, R.string.oauth_connection_url_unavailable); + } + } + + // If the results of getting the user_code and verification_url are OK, we get the received data and we start + // the polling service to oAuth2 server to get a valid token. + private void getOAuth2AccessTokenFromJsonResponse() { + String deviceCode = null; + String verificationUrl = null; + String userCode = null; + int expiresIn = -1; + int interval = -1; + + Log.d(TAG, "ResponseOAuth2->" + codeResponseJson.toString()); + + try { + // We get data that we must show to the user or we will use internally. + verificationUrl = codeResponseJson.getString(OAuth2GetCodeRunnable.CODE_VERIFICATION_URL); + userCode = codeResponseJson.getString(OAuth2GetCodeRunnable.CODE_USER_CODE); + expiresIn = codeResponseJson.getInt(OAuth2GetCodeRunnable.CODE_EXPIRES_IN); + + // And we get data that we must use to get a token. + deviceCode = codeResponseJson.getString(OAuth2GetCodeRunnable.CODE_DEVICE_CODE); + interval = codeResponseJson.getInt(OAuth2GetCodeRunnable.CODE_INTERVAL); + + } catch (JSONException e) { + Log.e(TAG, "Exception accesing data in Json object" + e.toString()); + } + + // Updating status widget to OK. + setOAuth2ResultIconAndText(R.drawable.ic_ok, R.string.auth_connection_established); + + // Showing the dialog with instructions for the user. + showDialog(OAUTH2_LOGIN_PROGRESS); + + // Loggin all the data. + Log.d(TAG, "verificationUrl->" + verificationUrl); + Log.d(TAG, "userCode->" + userCode); + Log.d(TAG, "deviceCode->" + deviceCode); + Log.d(TAG, "expiresIn->" + expiresIn); + Log.d(TAG, "interval->" + interval); + + // Starting the pooling service. + try { + Intent tokenService = new Intent(this, OAuth2GetTokenService.class); + tokenService.putExtra(OAuth2GetTokenService.TOKEN_URI, OAuth2Context.OAUTH2_G_DEVICE_GETTOKEN_URL); + tokenService.putExtra(OAuth2GetTokenService.TOKEN_DEVICE_CODE, deviceCode); + tokenService.putExtra(OAuth2GetTokenService.TOKEN_INTERVAL, interval); + + startService(tokenService); + } + catch (Exception e) { + Log.e(TAG, "tokenService creation problem :", e); + } + + } + + private void getOAuth2AccessTokenFromCapturedRedirection() { + Map responseValues = new HashMap(); + //String queryParameters = getIntent().getData().getQuery(); + String queryParameters = mNewCapturedUriFromOAuth2Redirection.getQuery(); + mNewCapturedUriFromOAuth2Redirection = null; + + Log.v(TAG, "Queryparameters (Code) = " + queryParameters); + + String[] pairs = queryParameters.split("&"); + Log.v(TAG, "Pairs (Code) = " + pairs.toString()); + + int i = 0; + String key = ""; + String value = ""; + + StringBuilder sb = new StringBuilder(); + + while (pairs.length > i) { + int j = 0; + String[] part = pairs[i].split("="); + + while (part.length > j) { + String p = part[j]; + if (j == 0) { + key = p; + sb.append(key + " = "); + } else if (j == 1) { + value = p; + responseValues.put(key, value); + sb.append(value + "\n"); + } + + Log.v(TAG, "[" + i + "," + j + "] = " + p); + j++; + } + i++; + } + + + // Updating status widget to OK. + setOAuth2ResultIconAndText(R.drawable.ic_ok, R.string.auth_connection_established); + + // Showing the dialog with instructions for the user. + showDialog(OAUTH2_LOGIN_PROGRESS); + + // + RemoteOperation operation = new GetOAuth2AccessToken(responseValues); + WebdavClient client = OwnCloudClientUtils.createOwnCloudClient(Uri.parse(OAuth2Context.OAUTH2_F_TOKEN_ENDPOINT_URL), getApplicationContext()); + operation.execute(client, this, mHandler); + } + + + + // We get data from the oAuth2 token service with this broadcast receiver. + private class TokenReceiver extends BroadcastReceiver { + /** + * The token is received. + * @author + * {@link BroadcastReceiver} to enable oAuth2 token receiving. + */ + @Override + public void onReceive(Context context, Intent intent) { + @SuppressWarnings("unchecked") + HashMap tokenResponse = (HashMap)intent.getExtras().get(OAuth2GetTokenService.TOKEN_RECEIVED_DATA); + Log.d(TAG, "TokenReceiver->" + tokenResponse.get(OAuth2GetTokenService.TOKEN_ACCESS_TOKEN)); + dismissDialog(OAUTH2_LOGIN_PROGRESS); + + } + } @Override public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) { diff --cc src/com/owncloud/android/ui/fragment/FileDetailFragment.java index 8740f834,2c43278f..f6c25e2c --- a/src/com/owncloud/android/ui/fragment/FileDetailFragment.java +++ b/src/com/owncloud/android/ui/fragment/FileDetailFragment.java @@@ -18,9 -18,20 +18,8 @@@ package com.owncloud.android.ui.fragment; import java.io.File; -import java.util.ArrayList; -import java.util.List; - -import org.apache.commons.httpclient.methods.GetMethod; -import org.apache.commons.httpclient.methods.PostMethod; -import org.apache.commons.httpclient.methods.StringRequestEntity; -import org.apache.commons.httpclient.params.HttpConnectionManagerParams; -import org.apache.http.HttpStatus; -import org.apache.http.NameValuePair; -import org.apache.http.client.utils.URLEncodedUtils; -import org.apache.http.message.BasicNameValuePair; -import org.apache.http.protocol.HTTP; -import org.apache.jackrabbit.webdav.client.methods.PropFindMethod; -import org.json.JSONObject; + +import org.apache.commons.httpclient.Credentials; - import org.apache.commons.httpclient.UsernamePasswordCredentials; import android.accounts.Account; import android.accounts.AccountManager; @@@ -64,8 -76,7 +63,7 @@@ import com.owncloud.android.files.servi import com.owncloud.android.files.services.FileUploader; import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder; import com.owncloud.android.files.services.FileUploader.FileUploaderBinder; -import com.owncloud.android.network.OwnCloudClientUtils; +import com.owncloud.android.network.BearerCredentials; - import com.owncloud.android.network.OwnCloudClientUtils; import com.owncloud.android.operations.OnRemoteOperationListener; import com.owncloud.android.operations.RemoteOperation; import com.owncloud.android.operations.RemoteOperationResult; @@@ -409,8 -423,9 +407,7 @@@ public class FileDetailFragment extend mLastRemoteOperation = new RemoveFileOperation( mFile, true, mStorageManager); - WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getSherlockActivity().getApplicationContext()); - mLastRemoteOperation.execute(wc, this, mHandler); - + mLastRemoteOperation.execute(mAccount, getSherlockActivity(), this, mHandler, getSherlockActivity()); - boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity; getActivity().showDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT); } @@@ -721,7 -736,7 +718,7 @@@ if (mFile.getRemotePath().equals(uploadRemotePath) || renamedInUpload) { if (uploadWasFine) { - mFile = mStorageManager.getFileByPath(mFile.getRemotePath()); - mFile = mStorageManager.getFileByPath(uploadRemotePath); ++ mFile = mStorageManager.getFileByPath(uploadRemotePath); } if (renamedInUpload) { String newName = (new File(uploadRemotePath)).getName();