From dd4f5b2bcb42a6272babe28178ac26dd2c219054 Mon Sep 17 00:00:00 2001 From: Farhazul Mullick Date: Sat, 15 May 2021 23:10:24 +0530 Subject: [PATCH 1/2] Added Coroutines Implementation --- .../android/ui/asynctasks/CoroutinesTask.kt | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 owncloudApp/src/main/java/com/owncloud/android/ui/asynctasks/CoroutinesTask.kt diff --git a/owncloudApp/src/main/java/com/owncloud/android/ui/asynctasks/CoroutinesTask.kt b/owncloudApp/src/main/java/com/owncloud/android/ui/asynctasks/CoroutinesTask.kt new file mode 100644 index 00000000000..c69c4409cdd --- /dev/null +++ b/owncloudApp/src/main/java/com/owncloud/android/ui/asynctasks/CoroutinesTask.kt @@ -0,0 +1,39 @@ +package com.owncloud.android.ui.preview + +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext + +abstract class CoroutinesTask { + + protected var isCancelled = false + + protected open fun onPreExecute(){} + + protected abstract fun doInBackground(vararg params: Params?): Result + + open fun onProgressUpdate(vararg progress: Progress?) {} + + protected open fun onPostExecute(result: Result?) {} + + fun execute(vararg params: Params?) { + GlobalScope.launch(Dispatchers.Default) { + val result = doInBackground(params[0]) + + withContext(Dispatchers.Main){ + onPostExecute(result) + } + } + } + + protected fun publishProgress(vararg progress: Progress?){ + GlobalScope.launch(Dispatchers.Main) { + onProgressUpdate(progress[0]) + } + } + + fun cancel(mayInterruptIfRunning: Boolean) {} + + +} \ No newline at end of file From 37b836baa2215cdca7a64f215c6d482ba1f984e9 Mon Sep 17 00:00:00 2001 From: Farhazul Mullick Date: Sat, 15 May 2021 23:17:11 +0530 Subject: [PATCH 2/2] Added Coroutines Support --- build.gradle | 1 + owncloudApp/build.gradle | 4 ++++ .../android/ui/asynctasks/CopyAndUploadContentUrisTask.java | 3 +-- .../java/com/owncloud/android/ui/asynctasks/CoroutinesTask.kt | 2 +- .../android/ui/preview/PrepareVideoPlayerAsyncTask.java | 4 ++-- .../com/owncloud/android/ui/preview/PreviewTextFragment.java | 4 ++-- 6 files changed, 11 insertions(+), 7 deletions(-) diff --git a/build.gradle b/build.gradle index 50ddd268aa3..35279600ce0 100644 --- a/build.gradle +++ b/build.gradle @@ -12,6 +12,7 @@ buildscript { sqliteVersion = "2.1.0" lifecycleLiveData = "2.3.1" biometricVersion="1.0.1" + coroutinesVersion= "1.4.2" // Kotlin kotlinVersion = "1.4.32" diff --git a/owncloudApp/build.gradle b/owncloudApp/build.gradle index 241e07ae951..17571887731 100644 --- a/owncloudApp/build.gradle +++ b/owncloudApp/build.gradle @@ -26,6 +26,10 @@ dependencies { implementation "androidx.sqlite:sqlite-ktx:$sqliteVersion" implementation "androidx.biometric:biometric:$biometricVersion" + // Kotlin Coroutines + implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion" + + // Image loading implementation 'com.github.bumptech.glide:glide:4.12.0' diff --git a/owncloudApp/src/main/java/com/owncloud/android/ui/asynctasks/CopyAndUploadContentUrisTask.java b/owncloudApp/src/main/java/com/owncloud/android/ui/asynctasks/CopyAndUploadContentUrisTask.java index f37a0d903fa..86f44279b90 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/ui/asynctasks/CopyAndUploadContentUrisTask.java +++ b/owncloudApp/src/main/java/com/owncloud/android/ui/asynctasks/CopyAndUploadContentUrisTask.java @@ -25,7 +25,6 @@ import android.content.ContentResolver; import android.content.Context; import android.net.Uri; -import android.os.AsyncTask; import android.widget.Toast; import com.owncloud.android.R; @@ -44,7 +43,7 @@ /** * AsyncTask to copy a file from a uri in a temporal file */ -public class CopyAndUploadContentUrisTask extends AsyncTask { +public class CopyAndUploadContentUrisTask extends CoroutinesTask { /** * Helper method building a correct array of parameters to be passed to {@link #execute(Object[])} )} diff --git a/owncloudApp/src/main/java/com/owncloud/android/ui/asynctasks/CoroutinesTask.kt b/owncloudApp/src/main/java/com/owncloud/android/ui/asynctasks/CoroutinesTask.kt index c69c4409cdd..4d1dc93cbc6 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/ui/asynctasks/CoroutinesTask.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/ui/asynctasks/CoroutinesTask.kt @@ -1,4 +1,4 @@ -package com.owncloud.android.ui.preview +package com.owncloud.android.ui.asynctasks import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope diff --git a/owncloudApp/src/main/java/com/owncloud/android/ui/preview/PrepareVideoPlayerAsyncTask.java b/owncloudApp/src/main/java/com/owncloud/android/ui/preview/PrepareVideoPlayerAsyncTask.java index a6b34c5e63a..b6ad956ad0a 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/ui/preview/PrepareVideoPlayerAsyncTask.java +++ b/owncloudApp/src/main/java/com/owncloud/android/ui/preview/PrepareVideoPlayerAsyncTask.java @@ -5,7 +5,6 @@ import android.accounts.OperationCanceledException; import android.content.Context; import android.net.Uri; -import android.os.AsyncTask; import android.os.Handler; import android.util.Base64; @@ -23,6 +22,7 @@ import com.owncloud.android.lib.common.authentication.OwnCloudBasicCredentials; import com.owncloud.android.lib.common.authentication.OwnCloudBearerCredentials; import com.owncloud.android.lib.common.authentication.OwnCloudCredentials; +import com.owncloud.android.ui.asynctasks.CoroutinesTask; import timber.log.Timber; import java.io.IOException; @@ -33,7 +33,7 @@ /** * Task for prepare video player asynchronously */ -public class PrepareVideoPlayerAsyncTask extends AsyncTask { +public class PrepareVideoPlayerAsyncTask extends CoroutinesTask { private Context mContext; private final WeakReference mListener; diff --git a/owncloudApp/src/main/java/com/owncloud/android/ui/preview/PreviewTextFragment.java b/owncloudApp/src/main/java/com/owncloud/android/ui/preview/PreviewTextFragment.java index 5abfa87ac53..5d51ba178f7 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/ui/preview/PreviewTextFragment.java +++ b/owncloudApp/src/main/java/com/owncloud/android/ui/preview/PreviewTextFragment.java @@ -21,7 +21,6 @@ package com.owncloud.android.ui.preview; import android.accounts.Account; -import android.os.AsyncTask; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; @@ -39,6 +38,7 @@ import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.files.FileMenuFilter; +import com.owncloud.android.ui.asynctasks.CoroutinesTask; import com.owncloud.android.ui.controller.TransferProgressController; import com.owncloud.android.ui.dialog.ConfirmationDialogFragment; import com.owncloud.android.ui.dialog.LoadingDialog; @@ -178,7 +178,7 @@ private void loadAndShowTextPreview() { /** * Reads the file to preview and shows its contents. Too critical to be anonymous. */ - private class TextLoadAsyncTask extends AsyncTask { + private class TextLoadAsyncTask extends CoroutinesTask { private final String DIALOG_WAIT_TAG = "DIALOG_WAIT"; private final WeakReference mTextViewReference;