From f52eb08606fa38806e7b0836fcc7cd4fe0a1db39 Mon Sep 17 00:00:00 2001 From: tobiasKaminsky Date: Thu, 26 Oct 2017 08:20:30 +0200 Subject: [PATCH] changes according to codacy --- .../authentication/AccountAuthenticator.java | 5 +- .../services/AccountManagerService.java | 152 +++++++++--------- .../ui/asynctasks/AsyncTaskHelper.java | 2 +- .../helper/InputStreamBinder.java | 2 +- .../helper/NextcloudRequest.java | 2 +- .../helper/ParcelFileDescriptorUtil.java | 8 +- 6 files changed, 83 insertions(+), 88 deletions(-) diff --git a/src/main/java/com/owncloud/android/authentication/AccountAuthenticator.java b/src/main/java/com/owncloud/android/authentication/AccountAuthenticator.java index f3bf0c5073..d7bb24be84 100644 --- a/src/main/java/com/owncloud/android/authentication/AccountAuthenticator.java +++ b/src/main/java/com/owncloud/android/authentication/AccountAuthenticator.java @@ -57,6 +57,7 @@ public class AccountAuthenticator extends AbstractAccountAuthenticator { 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 NEXTCLOUD_SSO = "NextcloudSSO"; private static final String TAG = AccountAuthenticator.class.getSimpleName(); @@ -158,7 +159,7 @@ public class AccountAuthenticator extends AbstractAccountAuthenticator { Account account, String authTokenType, Bundle options) throws NetworkErrorException { - if(authTokenType.equals("NextcloudSSO")) { + if (NEXTCLOUD_SSO.equals(authTokenType)) { AccountManager am = AccountManager.get(mContext); final Bundle result = new Bundle(); @@ -167,7 +168,7 @@ public class AccountAuthenticator extends AbstractAccountAuthenticator { result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); result.putString(AccountManager.KEY_ACCOUNT_TYPE, MainApp.getAccountType()); - result.putString(AccountManager.KEY_AUTHTOKEN, "NextcloudSSO"); + result.putString(AccountManager.KEY_AUTHTOKEN, NEXTCLOUD_SSO); result.putString("username", username); result.putString("password", am.getPassword(account)); result.putString("server_url", "https://" + server); diff --git a/src/main/java/com/owncloud/android/services/AccountManagerService.java b/src/main/java/com/owncloud/android/services/AccountManagerService.java index b2f5ae7dc9..8f574b5135 100644 --- a/src/main/java/com/owncloud/android/services/AccountManagerService.java +++ b/src/main/java/com/owncloud/android/services/AccountManagerService.java @@ -33,7 +33,6 @@ import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketAddress; import java.util.Map; -import java.util.concurrent.Callable; import de.luhmer.owncloud.accountimporter.helper.InputStreamBinder; import de.luhmer.owncloud.accountimporter.helper.NextcloudRequest; @@ -42,19 +41,17 @@ public class AccountManagerService extends Service { private static final String TAG = AccountManagerService.class.getCanonicalName(); - public AccountManagerService() { } - - @Override - public IBinder onBind(Intent intent) { - return (new InputStreamBinder(this)); - } - /** Command to the service to display a message */ private static final int MSG_CREATE_NEW_ACCOUNT = 3; private static final int MSG_REQUEST_NETWORK_REQUEST = 4; private static final int MSG_RESPONSE_NETWORK_REQUEST = 5; + @Override + public IBinder onBind(Intent intent) { + return (new InputStreamBinder(this)); + } + /** * Handler of incoming messages from clients. */ @@ -80,82 +77,79 @@ public class AccountManagerService extends Service { Object result = null; Exception exception = null; try { - result = AsyncTaskHelper.ExecuteBlockingRequest(new Callable() { - @Override - public Object call() throws Exception { - Account account = AccountUtils.getOwnCloudAccountByName(AccountManagerService.this, accountName); // TODO handle case that account is not found! - OwnCloudAccount ocAccount = new OwnCloudAccount(account, AccountManagerService.this); - OwnCloudClient client = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(ocAccount, AccountManagerService.this); + result = AsyncTaskHelper.executeBlockingRequest(() -> { + Account account = AccountUtils.getOwnCloudAccountByName(AccountManagerService.this, accountName); // TODO handle case that account is not found! + OwnCloudAccount ocAccount = new OwnCloudAccount(account, AccountManagerService.this); + OwnCloudClient client = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(ocAccount, AccountManagerService.this); - //OwnCloudVersion version = AccountUtils.getServerVersion(account); - //client.setOwnCloudVersion(version); + //OwnCloudVersion version = AccountUtils.getServerVersion(account); + //client.setOwnCloudVersion(version); - // TODO do some checks if url is correct!! (prevent ../ in url etc.. - request.url = client.getBaseUri() + request.url; + // TODO do some checks if url is correct!! (prevent ../ in url etc.. + request.url = client.getBaseUri() + request.url; - INetworkInterface network = (stream) ? new StreamingRequest(port) : new PlainRequest(); + INetworkInterface network = (stream) ? new StreamingRequest(port) : new PlainRequest(); - switch(request.method) { - case "GET": - GetMethod get = new GetMethod(request.url); - get.setQueryString(convertMapToNVP(request.parameter)); - get.addRequestHeader("OCS-APIREQUEST", "true"); - int status = client.executeMethod(get); - if(status == 200) { - return network.handleGetRequest(get); - } else { - throw new Exception("Network error!!"); - } - case "POST": - PostMethod post = new PostMethod(request.url); - post.setQueryString(convertMapToNVP(request.parameter)); - post.addRequestHeader("OCS-APIREQUEST", "true"); + switch(request.method) { + case "GET": + GetMethod get = new GetMethod(request.url); + get.setQueryString(convertMapToNVP(request.parameter)); + get.addRequestHeader("OCS-APIREQUEST", "true"); + int status = client.executeMethod(get); + if(status == 200) { + return network.handleGetRequest(get); + } else { + throw new Exception("Network error!!"); + } + case "POST": + PostMethod post = new PostMethod(request.url); + post.setQueryString(convertMapToNVP(request.parameter)); + post.addRequestHeader("OCS-APIREQUEST", "true"); - if(request.requestBody != null) { - StringRequestEntity requestEntity = new StringRequestEntity( - request.requestBody, - "application/json", - "UTF-8"); - post.setRequestEntity(requestEntity); - } - int status2 = client.executeMethod(post); - if(status2 == 200) { - return network.handlePostRequest(post); - } else { - throw new Exception("Network error!!"); - } - case "PUT": - PutMethod put = new PutMethod(request.url); - put.setQueryString(convertMapToNVP(request.parameter)); - put.addRequestHeader("OCS-APIREQUEST", "true"); + if(request.requestBody != null) { + StringRequestEntity requestEntity = new StringRequestEntity( + request.requestBody, + "application/json", + "UTF-8"); + post.setRequestEntity(requestEntity); + } + int status2 = client.executeMethod(post); + if(status2 == 200) { + return network.handlePostRequest(post); + } else { + throw new Exception("Network error!!"); + } + case "PUT": + PutMethod put = new PutMethod(request.url); + put.setQueryString(convertMapToNVP(request.parameter)); + put.addRequestHeader("OCS-APIREQUEST", "true"); - if(request.requestBody != null) { - StringRequestEntity requestEntity = new StringRequestEntity( - request.requestBody, - "application/json", - "UTF-8"); - put.setRequestEntity(requestEntity); - } - int status4 = client.executeMethod(put); - if(status4 == 200) { - return network.handlePutRequest(put); - } else { - throw new Exception("Network error!!"); - } + if(request.requestBody != null) { + StringRequestEntity requestEntity = new StringRequestEntity( + request.requestBody, + "application/json", + "UTF-8"); + put.setRequestEntity(requestEntity); + } + int status4 = client.executeMethod(put); + if(status4 == 200) { + return network.handlePutRequest(put); + } else { + throw new Exception("Network error!!"); + } - case "DELETE": - DeleteMethod delete = new DeleteMethod(request.url); - delete.setQueryString(convertMapToNVP(request.parameter)); - delete.addRequestHeader("OCS-APIREQUEST", "true"); - int status3 = client.executeMethod(delete); - if(status3 == 200) { - return true; - } else { - throw new Exception("Network error!!"); - } - default: - throw new Exception("Unexpected type!!"); - } + case "DELETE": + DeleteMethod delete = new DeleteMethod(request.url); + delete.setQueryString(convertMapToNVP(request.parameter)); + delete.addRequestHeader("OCS-APIREQUEST", "true"); + int status3 = client.executeMethod(delete); + if(status3 == 200) { + return true; + } else { + throw new Exception("Network error!!"); + } + default: + throw new Exception("Unexpected type!!"); } }); } catch (Exception e) { @@ -225,7 +219,7 @@ public class AccountManagerService extends Service { public class StreamingRequest implements INetworkInterface { - int port; + private int port; public StreamingRequest(int port) { this.port = port; @@ -245,7 +239,7 @@ public class AccountManagerService extends Service { InputStream in = get.getResponseBodyAsStream(); OutputStream out = m_activity_socket.getOutputStream(); // the header describes the frame data (type, width, height, length) - // frame width and heigth have been previously decoded + // frame width and height have been previously decoded //byte[] header = new byte[16]; //build_header( 1, width, height, frameData.length, header, 1 ); //m_nos.write( header ); // message header diff --git a/src/main/java/com/owncloud/android/ui/asynctasks/AsyncTaskHelper.java b/src/main/java/com/owncloud/android/ui/asynctasks/AsyncTaskHelper.java index 6b996b54eb..e227a2c011 100644 --- a/src/main/java/com/owncloud/android/ui/asynctasks/AsyncTaskHelper.java +++ b/src/main/java/com/owncloud/android/ui/asynctasks/AsyncTaskHelper.java @@ -17,7 +17,7 @@ import java.util.concurrent.Callable; public class AsyncTaskHelper { @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB) - public static T ExecuteBlockingRequest(Callable callable) throws Exception { + public static T executeBlockingRequest(Callable callable) throws Exception { GenericAsyncTaskWithCallable at = new GenericAsyncTaskWithCallable<>(callable); T result = at.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR).get(); diff --git a/src/main/java/de/luhmer/owncloud/accountimporter/helper/InputStreamBinder.java b/src/main/java/de/luhmer/owncloud/accountimporter/helper/InputStreamBinder.java index 56b141a096..f060f542b7 100644 --- a/src/main/java/de/luhmer/owncloud/accountimporter/helper/InputStreamBinder.java +++ b/src/main/java/de/luhmer/owncloud/accountimporter/helper/InputStreamBinder.java @@ -89,7 +89,7 @@ public class InputStreamBinder extends IInputStreamService.Stub { private InputStream processRequest(final NextcloudRequest request) { try { - return AsyncTaskHelper.ExecuteBlockingRequest(new Callable() { + return AsyncTaskHelper.executeBlockingRequest(new Callable() { @Override public InputStream call() throws Exception { Account account = AccountUtils.getOwnCloudAccountByName(context, request.accountName); // TODO handle case that account is not found! diff --git a/src/main/java/de/luhmer/owncloud/accountimporter/helper/NextcloudRequest.java b/src/main/java/de/luhmer/owncloud/accountimporter/helper/NextcloudRequest.java index f0303bc9c5..6a05ee0b08 100644 --- a/src/main/java/de/luhmer/owncloud/accountimporter/helper/NextcloudRequest.java +++ b/src/main/java/de/luhmer/owncloud/accountimporter/helper/NextcloudRequest.java @@ -11,7 +11,7 @@ import java.util.Map; public class NextcloudRequest implements Serializable { - static final long serialVersionUID = 215521212534236L; //assign a long value + private static final long serialVersionUID = 215521212534236L; //assign a long value public String method; public Map> header = new HashMap<>(); diff --git a/src/main/java/de/luhmer/owncloud/accountimporter/helper/ParcelFileDescriptorUtil.java b/src/main/java/de/luhmer/owncloud/accountimporter/helper/ParcelFileDescriptorUtil.java index 719743cb49..4bf3e729f4 100644 --- a/src/main/java/de/luhmer/owncloud/accountimporter/helper/ParcelFileDescriptorUtil.java +++ b/src/main/java/de/luhmer/owncloud/accountimporter/helper/ParcelFileDescriptorUtil.java @@ -37,10 +37,10 @@ public class ParcelFileDescriptorUtil { return writeSide; } - static class TransferThread extends Thread { - final InputStream mIn; - final OutputStream mOut; - final IThreadListener mListener; + public static class TransferThread extends Thread { + private final InputStream mIn; + private final OutputStream mOut; + private final IThreadListener mListener; TransferThread(InputStream in, OutputStream out, IThreadListener listener) { super("ParcelFileDescriptor Transfer Thread");