From 5806b17de551a255ae4f19edd45e246f89b8ffc3 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Thu, 2 Nov 2023 14:12:04 +0100 Subject: [PATCH 1/7] Rename .java to .kt Signed-off-by: alperozturk --- .../{AuthenticatorUrlUtils.java => AuthenticatorUrlUtils.kt} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename app/src/main/java/com/owncloud/android/authentication/{AuthenticatorUrlUtils.java => AuthenticatorUrlUtils.kt} (100%) diff --git a/app/src/main/java/com/owncloud/android/authentication/AuthenticatorUrlUtils.java b/app/src/main/java/com/owncloud/android/authentication/AuthenticatorUrlUtils.kt similarity index 100% rename from app/src/main/java/com/owncloud/android/authentication/AuthenticatorUrlUtils.java rename to app/src/main/java/com/owncloud/android/authentication/AuthenticatorUrlUtils.kt From a94940a0715f7649a37a2c573a6bfec217f4d4c6 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Thu, 2 Nov 2023 14:12:05 +0100 Subject: [PATCH 2/7] Convert AuthenticatorUrlUtils to kt Signed-off-by: alperozturk --- .../authentication/AuthenticatorActivity.java | 12 +-- .../authentication/AuthenticatorUrlUtils.kt | 92 ++++++------------- 2 files changed, 35 insertions(+), 69 deletions(-) diff --git a/app/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.java b/app/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.java index 5e5f1f897acb..ff81dc253f5e 100644 --- a/app/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.java +++ b/app/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.java @@ -495,7 +495,7 @@ private void parseAndLoginFromWebView(String dataString) { if (accountSetupBinding != null) { accountSetupBinding.hostUrlInput.setText(""); } - mServerInfo.mBaseUrl = AuthenticatorUrlUtils.normalizeUrlSuffix(loginUrlInfo.serverAddress); + mServerInfo.mBaseUrl = AuthenticatorUrlUtils.INSTANCE.normalizeUrlSuffix(loginUrlInfo.serverAddress); webViewUser = loginUrlInfo.username; webViewPassword = loginUrlInfo.password; } catch (Exception e) { @@ -783,12 +783,12 @@ private void checkOcServer() { if (uri.length() != 0) { if (accountSetupBinding != null) { - uri = AuthenticatorUrlUtils.stripIndexPhpOrAppsFiles(uri); + uri = AuthenticatorUrlUtils.INSTANCE.stripIndexPhpOrAppsFiles(uri); accountSetupBinding.hostUrlInput.setText(uri); } try { - uri = AuthenticatorUrlUtils.normalizeScheme(uri); + uri = AuthenticatorUrlUtils.INSTANCE.normalizeScheme(uri); } catch (IllegalArgumentException ex) { // Let the Nextcloud library check the error of the malformed URI Log_OC.e(TAG, "Invalid URL", ex); @@ -812,7 +812,7 @@ private void checkOcServer() { Intent getServerInfoIntent = new Intent(); getServerInfoIntent.setAction(OperationsService.ACTION_GET_SERVER_INFO); getServerInfoIntent.putExtra(OperationsService.EXTRA_SERVER_URL, - AuthenticatorUrlUtils.normalizeUrlSuffix(uri)); + AuthenticatorUrlUtils.INSTANCE.normalizeUrlSuffix(uri)); if (mOperationsServiceBinder != null) { mWaitingForOpId = mOperationsServiceBinder.queueNewOperation(getServerInfoIntent); @@ -1291,7 +1291,7 @@ protected boolean createAccount(RemoteOperationResult authResult) { // create and save new ownCloud account String lastPermanentLocation = authResult.getLastPermanentLocation(); if (lastPermanentLocation != null) { - mServerInfo.mBaseUrl = AuthenticatorUrlUtils.trimWebdavSuffix(lastPermanentLocation); + mServerInfo.mBaseUrl = AuthenticatorUrlUtils.INSTANCE.trimWebdavSuffix(lastPermanentLocation); } Uri uri = Uri.parse(mServerInfo.mBaseUrl); @@ -1488,7 +1488,7 @@ public void onServiceConnected(ComponentName component, IBinder service) { String prefix = getString(R.string.login_data_own_scheme) + PROTOCOL_SUFFIX + "login/"; LoginUrlInfo loginUrlInfo = parseLoginDataUrl(prefix, data.toString()); - mServerInfo.mBaseUrl = AuthenticatorUrlUtils.normalizeUrlSuffix(loginUrlInfo.serverAddress); + mServerInfo.mBaseUrl = AuthenticatorUrlUtils.INSTANCE.normalizeUrlSuffix(loginUrlInfo.serverAddress); webViewUser = loginUrlInfo.username; webViewPassword = loginUrlInfo.password; doOnResumeAndBound(); diff --git a/app/src/main/java/com/owncloud/android/authentication/AuthenticatorUrlUtils.kt b/app/src/main/java/com/owncloud/android/authentication/AuthenticatorUrlUtils.kt index 2e502c138e4a..2d77dfcf21cc 100644 --- a/app/src/main/java/com/owncloud/android/authentication/AuthenticatorUrlUtils.kt +++ b/app/src/main/java/com/owncloud/android/authentication/AuthenticatorUrlUtils.kt @@ -18,94 +18,60 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ +package com.owncloud.android.authentication -package com.owncloud.android.authentication; - -import android.text.TextUtils; - -import java.net.URI; -import java.util.Locale; +import java.net.URI /** * Helper class for authenticator-URL related logic. */ -public final class AuthenticatorUrlUtils { - public static final String WEBDAV_PATH_4_0_AND_LATER = "/remote.php/webdav"; +object AuthenticatorUrlUtils { - private static final String HTTPS_PROTOCOL = "https://"; - private static final String HTTP_PROTOCOL = "http://"; - - private AuthenticatorUrlUtils() { - } + const val WEBDAV_PATH_4_0_AND_LATER = "/remote.php/webdav" - public static String normalizeUrlSuffix(String url) { - String normalizedUrl = url; + fun normalizeUrlSuffix(url: String): String { + var normalizedUrl = url if (normalizedUrl.endsWith("/")) { - normalizedUrl = normalizedUrl.substring(0, normalizedUrl.length() - 1); + normalizedUrl = normalizedUrl.substring(0, normalizedUrl.length - 1) } - return trimUrlWebdav(normalizedUrl); + return trimUrlWebdav(normalizedUrl) } - public static String normalizeUrl(String url, boolean sslWhenUnprefixed) { - String normalizedUrl = url; - - if (!TextUtils.isEmpty(normalizedUrl)) { - normalizedUrl = normalizedUrl.trim(); - - if (!normalizedUrl.toLowerCase(Locale.ROOT).startsWith(HTTP_PROTOCOL) && - !normalizedUrl.toLowerCase(Locale.ROOT).startsWith(HTTPS_PROTOCOL)) { - if (sslWhenUnprefixed) { - normalizedUrl = HTTPS_PROTOCOL + normalizedUrl; - } else { - normalizedUrl = HTTP_PROTOCOL + normalizedUrl; - } - } - - normalizedUrl = normalizeUrlSuffix(normalizedUrl); - } - return normalizedUrl != null ? normalizedUrl : ""; - } - - public static String trimWebdavSuffix(String url) { - String trimmedUrl = url; + fun trimWebdavSuffix(url: String): String { + var trimmedUrl = url while (trimmedUrl.endsWith("/")) { - trimmedUrl = trimmedUrl.substring(0, url.length() - 1); + trimmedUrl = trimmedUrl.substring(0, url.length - 1) } - - int pos = trimmedUrl.lastIndexOf(WEBDAV_PATH_4_0_AND_LATER); + val pos = trimmedUrl.lastIndexOf(WEBDAV_PATH_4_0_AND_LATER) if (pos >= 0) { - trimmedUrl = trimmedUrl.substring(0, pos); - + trimmedUrl = trimmedUrl.substring(0, pos) } - return trimmedUrl; + return trimmedUrl } - private static String trimUrlWebdav(String url) { - if (url.toLowerCase(Locale.ROOT).endsWith(WEBDAV_PATH_4_0_AND_LATER)) { - return url.substring(0, url.length() - WEBDAV_PATH_4_0_AND_LATER.length()); - } - - return url; + private fun trimUrlWebdav(url: String): String { + return if (url.lowercase().endsWith(WEBDAV_PATH_4_0_AND_LATER)) { + url.substring(0, url.length - WEBDAV_PATH_4_0_AND_LATER.length) + } else url } - public static String stripIndexPhpOrAppsFiles(String url) { - String strippedUrl = url; + fun stripIndexPhpOrAppsFiles(url: String): String { + var strippedUrl = url if (strippedUrl.endsWith("/index.php")) { - strippedUrl = strippedUrl.substring(0, strippedUrl.lastIndexOf("/index.php")); + strippedUrl = strippedUrl.substring(0, strippedUrl.lastIndexOf("/index.php")) } else if (strippedUrl.contains("/index.php/apps/")) { - strippedUrl = strippedUrl.substring(0, strippedUrl.lastIndexOf("/index.php/apps/")); + strippedUrl = strippedUrl.substring(0, strippedUrl.lastIndexOf("/index.php/apps/")) } - - return strippedUrl; + return strippedUrl } - public static String normalizeScheme(String url) { - if (url.matches("[a-zA-Z][a-zA-Z0-9+.-]+://.+")) { - URI uri = URI.create(url); - String lcScheme = uri.getScheme().toLowerCase(Locale.ROOT); - return String.format("%s:%s", lcScheme, uri.getRawSchemeSpecificPart()); + fun normalizeScheme(url: String): String { + return if (url.matches("[a-zA-Z][a-zA-Z0-9+.-]+://.+".toRegex())) { + val uri = URI.create(url) + val lcScheme = uri.scheme.lowercase() + String.format("%s:%s", lcScheme, uri.rawSchemeSpecificPart) } else { - return url; + url } } } From 305af05ef7fd6e8d397f6d56651eaad0092db560 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Thu, 2 Nov 2023 14:19:27 +0100 Subject: [PATCH 3/7] Rename .java to .kt Signed-off-by: alperozturk --- .../{AuthenticatorAsyncTask.java => AuthenticatorAsyncTask.kt} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename app/src/main/java/com/owncloud/android/authentication/{AuthenticatorAsyncTask.java => AuthenticatorAsyncTask.kt} (100%) diff --git a/app/src/main/java/com/owncloud/android/authentication/AuthenticatorAsyncTask.java b/app/src/main/java/com/owncloud/android/authentication/AuthenticatorAsyncTask.kt similarity index 100% rename from app/src/main/java/com/owncloud/android/authentication/AuthenticatorAsyncTask.java rename to app/src/main/java/com/owncloud/android/authentication/AuthenticatorAsyncTask.kt From 57564073b8c282239796ea61397e372ac766fde4 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Thu, 2 Nov 2023 14:19:27 +0100 Subject: [PATCH 4/7] Convert AuthenticatorAsyncTask to kt Signed-off-by: alperozturk --- .../authentication/AuthenticatorAsyncTask.kt | 130 ++++++++---------- 1 file changed, 61 insertions(+), 69 deletions(-) diff --git a/app/src/main/java/com/owncloud/android/authentication/AuthenticatorAsyncTask.kt b/app/src/main/java/com/owncloud/android/authentication/AuthenticatorAsyncTask.kt index e2f88f793296..0fc9025dd3fa 100644 --- a/app/src/main/java/com/owncloud/android/authentication/AuthenticatorAsyncTask.kt +++ b/app/src/main/java/com/owncloud/android/authentication/AuthenticatorAsyncTask.kt @@ -17,105 +17,97 @@ * along with this program. If not, see . * */ -package com.owncloud.android.authentication; - -import android.app.Activity; -import android.content.Context; -import android.net.Uri; -import android.os.AsyncTask; - -import com.nextcloud.common.NextcloudClient; -import com.owncloud.android.lib.common.OwnCloudClient; -import com.owncloud.android.lib.common.OwnCloudClientFactory; -import com.owncloud.android.lib.common.OwnCloudCredentials; -import com.owncloud.android.lib.common.UserInfo; -import com.owncloud.android.lib.common.network.RedirectionPath; -import com.owncloud.android.lib.common.operations.RemoteOperationResult; -import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation; -import com.owncloud.android.lib.resources.users.GetUserInfoRemoteOperation; - -import java.lang.ref.WeakReference; - -import static com.owncloud.android.datamodel.OCFile.ROOT_PATH; - +@file:Suppress("DEPRECATION") + +package com.owncloud.android.authentication + +import android.app.Activity +import android.content.Context +import android.net.Uri +import android.os.AsyncTask +import com.owncloud.android.datamodel.OCFile +import com.owncloud.android.lib.common.OwnCloudClientFactory +import com.owncloud.android.lib.common.OwnCloudCredentials +import com.owncloud.android.lib.common.UserInfo +import com.owncloud.android.lib.common.operations.RemoteOperationResult +import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation +import com.owncloud.android.lib.resources.users.GetUserInfoRemoteOperation +import java.lang.ref.WeakReference /** * Async Task to verify the credentials of a user */ -public class AuthenticatorAsyncTask extends AsyncTask> { - - private static final boolean SUCCESS_IF_ABSENT = false; - private final WeakReference mWeakContext; - private final WeakReference mListener; +class AuthenticatorAsyncTask(activity: Activity) : AsyncTask?>() { + private val mWeakContext: WeakReference + private val mListener: WeakReference - public AuthenticatorAsyncTask(Activity activity) { - mWeakContext = new WeakReference<>(activity.getApplicationContext()); - mListener = new WeakReference<>((OnAuthenticatorTaskListener) activity); + init { + mWeakContext = WeakReference(activity.applicationContext) + mListener = WeakReference(activity as OnAuthenticatorTaskListener) } - @Override - protected RemoteOperationResult doInBackground(Object... params) { + @Deprecated("Deprecated in Java") + override fun doInBackground(vararg params: Any?): RemoteOperationResult { + val result: RemoteOperationResult - RemoteOperationResult result; - if (params != null && params.length == 2 && mWeakContext.get() != null) { - String url = (String) params[0]; - Context context = mWeakContext.get(); - OwnCloudCredentials credentials = (OwnCloudCredentials) params[1]; + if (params.size == 2 && mWeakContext.get() != null) { + val url = params[0] as String + val context = mWeakContext.get() + val credentials = params[1] as OwnCloudCredentials // Client - Uri uri = Uri.parse(url); - NextcloudClient nextcloudClient = OwnCloudClientFactory.createNextcloudClient(uri, - credentials.getUsername(), - credentials.toOkHttpCredentials(), - context, - true); - + val uri = Uri.parse(url) + val nextcloudClient = OwnCloudClientFactory.createNextcloudClient( + uri, + credentials.username, + credentials.toOkHttpCredentials(), + context, + true + ) // Operation - get display name - RemoteOperationResult userInfoResult = new GetUserInfoRemoteOperation().execute(nextcloudClient); + val userInfoResult = GetUserInfoRemoteOperation().execute(nextcloudClient) // Operation - try credentials - if (userInfoResult.isSuccess()) { - OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(uri, context, true); - client.setUserId(userInfoResult.getResultData().getId()); - client.setCredentials(credentials); - - ExistenceCheckRemoteOperation operation = new ExistenceCheckRemoteOperation(ROOT_PATH, SUCCESS_IF_ABSENT); - result = operation.execute(client); - + if (userInfoResult.isSuccess) { + val client = OwnCloudClientFactory.createOwnCloudClient(uri, context, true) + client.userId = userInfoResult.resultData!!.id + client.credentials = credentials + val operation = ExistenceCheckRemoteOperation(OCFile.ROOT_PATH, SUCCESS_IF_ABSENT) + result = operation.execute(client) as RemoteOperationResult if (operation.wasRedirected()) { - RedirectionPath redirectionPath = operation.getRedirectionPath(); - String permanentLocation = redirectionPath.getLastPermanentLocation(); - result.setLastPermanentLocation(permanentLocation); + val redirectionPath = operation.redirectionPath + val permanentLocation = redirectionPath.lastPermanentLocation + result.lastPermanentLocation = permanentLocation } - - result.setResultData(userInfoResult.getResultData()); + result.setResultData(userInfoResult.resultData) } else { - result = userInfoResult; + result = userInfoResult } } else { - result = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR); + result = RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR) } - return result; + return result } - @Override - protected void onPostExecute(RemoteOperationResult result) { - + @Deprecated("Deprecated in Java") + override fun onPostExecute(result: RemoteOperationResult?) { if (result != null) { - OnAuthenticatorTaskListener listener = mListener.get(); - if (listener != null) { - listener.onAuthenticatorTaskCallback(result); - } + val listener = mListener.get() + listener?.onAuthenticatorTaskCallback(result) } } + /* * Interface to retrieve data from recognition task */ - public interface OnAuthenticatorTaskListener{ + interface OnAuthenticatorTaskListener { + fun onAuthenticatorTaskCallback(result: RemoteOperationResult?) + } - void onAuthenticatorTaskCallback(RemoteOperationResult result); + companion object { + private const val SUCCESS_IF_ABSENT = false } } From 3010f78f793d410b1208c7c25363dd0ec2a88d16 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Thu, 2 Nov 2023 14:42:56 +0100 Subject: [PATCH 5/7] Code cleanup Signed-off-by: alperozturk --- .../android/authentication/AuthenticatorAsyncTask.kt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/owncloud/android/authentication/AuthenticatorAsyncTask.kt b/app/src/main/java/com/owncloud/android/authentication/AuthenticatorAsyncTask.kt index 0fc9025dd3fa..fa7e3032067d 100644 --- a/app/src/main/java/com/owncloud/android/authentication/AuthenticatorAsyncTask.kt +++ b/app/src/main/java/com/owncloud/android/authentication/AuthenticatorAsyncTask.kt @@ -53,8 +53,8 @@ class AuthenticatorAsyncTask(activity: Activity) : AsyncTask if (operation.wasRedirected()) { val redirectionPath = operation.redirectionPath @@ -94,9 +96,9 @@ class AuthenticatorAsyncTask(activity: Activity) : AsyncTask?) { - if (result != null) { + result?.let { val listener = mListener.get() - listener?.onAuthenticatorTaskCallback(result) + listener?.onAuthenticatorTaskCallback(it) } } From a2d002a2fc755fba7a65782440d2ca6a4243c6c2 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Thu, 2 Nov 2023 14:45:01 +0100 Subject: [PATCH 6/7] Fix kotlin spotless check Signed-off-by: alperozturk --- .../owncloud/android/authentication/AuthenticatorUrlUtils.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/owncloud/android/authentication/AuthenticatorUrlUtils.kt b/app/src/main/java/com/owncloud/android/authentication/AuthenticatorUrlUtils.kt index 2d77dfcf21cc..b946a12f4589 100644 --- a/app/src/main/java/com/owncloud/android/authentication/AuthenticatorUrlUtils.kt +++ b/app/src/main/java/com/owncloud/android/authentication/AuthenticatorUrlUtils.kt @@ -52,7 +52,9 @@ object AuthenticatorUrlUtils { private fun trimUrlWebdav(url: String): String { return if (url.lowercase().endsWith(WEBDAV_PATH_4_0_AND_LATER)) { url.substring(0, url.length - WEBDAV_PATH_4_0_AND_LATER.length) - } else url + } else { + url + } } fun stripIndexPhpOrAppsFiles(url: String): String { From 36e02b14c115542dce00231b23930a61367d262e Mon Sep 17 00:00:00 2001 From: alperozturk Date: Wed, 8 Nov 2023 15:17:12 +0100 Subject: [PATCH 7/7] Fix kotlin object call Signed-off-by: alperozturk --- .../authentication/AuthenticatorUrlUtilsTest.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/app/src/test/java/com/owncloud/android/authentication/AuthenticatorUrlUtilsTest.java b/app/src/test/java/com/owncloud/android/authentication/AuthenticatorUrlUtilsTest.java index 998160bce758..bf9a58d5629d 100644 --- a/app/src/test/java/com/owncloud/android/authentication/AuthenticatorUrlUtilsTest.java +++ b/app/src/test/java/com/owncloud/android/authentication/AuthenticatorUrlUtilsTest.java @@ -38,7 +38,7 @@ public void noScheme() { // WHEN // scheme is normalized - String normalized = AuthenticatorUrlUtils.normalizeScheme(url); + String normalized = AuthenticatorUrlUtils.INSTANCE.normalizeScheme(url); // THEN // input is returned unchanged @@ -54,7 +54,7 @@ public void lowercaseScheme() { // WHEN // scheme is normalized - String normalized = AuthenticatorUrlUtils.normalizeScheme(url); + String normalized = AuthenticatorUrlUtils.INSTANCE.normalizeScheme(url); // THEN // output is equal @@ -70,7 +70,7 @@ public void uppercaseScheme() { // WHEN // scheme is normalized - String normalized = AuthenticatorUrlUtils.normalizeScheme(mixedCaseUrl); + String normalized = AuthenticatorUrlUtils.INSTANCE.normalizeScheme(mixedCaseUrl); // THEN // scheme has been lower-cased @@ -87,7 +87,7 @@ public void emptyInput() { // WHEN // scheme is normalized - String normalized = AuthenticatorUrlUtils.normalizeScheme(emptyUrl); + String normalized = AuthenticatorUrlUtils.INSTANCE.normalizeScheme(emptyUrl); // THEN // output is empty @@ -103,7 +103,7 @@ public void ipAddress() { // WHEN // scheme is normalized - String normalized = AuthenticatorUrlUtils.normalizeScheme(url); + String normalized = AuthenticatorUrlUtils.INSTANCE.normalizeScheme(url); // THEN // output is equal @@ -119,7 +119,7 @@ public void withPort() { // WHEN // scheme is normalized - String normalized = AuthenticatorUrlUtils.normalizeScheme(url); + String normalized = AuthenticatorUrlUtils.INSTANCE.normalizeScheme(url); // THEN // output is equal @@ -136,7 +136,7 @@ public void ipAddressWithPort() { // WHEN // scheme is normalized - String normalized = AuthenticatorUrlUtils.normalizeScheme(url); + String normalized = AuthenticatorUrlUtils.INSTANCE.normalizeScheme(url); // THEN // output is equal