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 } }