Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async task/async task to coroutines #3236

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions owncloudApp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,7 +43,7 @@
/**
* AsyncTask to copy a file from a uri in a temporal file
*/
public class CopyAndUploadContentUrisTask extends AsyncTask<Object, Void, ResultCode> {
public class CopyAndUploadContentUrisTask extends CoroutinesTask<Object, Void, ResultCode> {

/**
* Helper method building a correct array of parameters to be passed to {@link #execute(Object[])} )}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.owncloud.android.ui.asynctasks

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

abstract class CoroutinesTask<Params, Progress, Result> {

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


}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -33,7 +33,7 @@
/**
* Task for prepare video player asynchronously
*/
public class PrepareVideoPlayerAsyncTask extends AsyncTask<Object, Void, MediaSource> {
public class PrepareVideoPlayerAsyncTask extends CoroutinesTask<Object, Void, MediaSource> {

private Context mContext;
private final WeakReference<OnPrepareVideoPlayerTaskListener> mListener;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<Object, Void, StringWriter> {
private class TextLoadAsyncTask extends CoroutinesTask<Object, Void, StringWriter> {
private final String DIALOG_WAIT_TAG = "DIALOG_WAIT";
private final WeakReference<TextView> mTextViewReference;

Expand Down