Browse Source

add onTransferProgress for FilesUploadWorker

Signed-off-by: Jonas Mayer <jonas.a.mayer@gmx.net>
Jonas Mayer 1 năm trước cách đây
mục cha
commit
4f08a4737b

+ 3 - 0
app/src/main/java/com/nextcloud/client/jobs/FilesUploadWorker.kt

@@ -155,6 +155,7 @@ class FilesUploadWorker(
         }
     }
 
+
     @Suppress("TooGenericExceptionCaught")
     private fun upload(uploadFileOperation: UploadFileOperation, user: User): RemoteOperationResult<Any?> {
         lateinit var uploadResult: RemoteOperationResult<Any?>
@@ -378,8 +379,10 @@ class FilesUploadWorker(
             val text = String.format(context.getString(R.string.uploader_upload_in_progress_content), percent, fileName)
             notificationBuilder.setContentText(text)
             notificationManager.notify(FOREGROUND_SERVICE_ID, notificationBuilder.build())
+            //FilesUploadHelper.onTransferProgress(,progressRate,totalTransferredSoFar,totalToTransfer,fileAbsoluteName)
         }
         lastPercent = percent
+
     }
 
     override fun onStopped() {

+ 26 - 8
app/src/main/java/com/owncloud/android/files/services/FileUploader.java

@@ -1274,9 +1274,13 @@ public class FileUploader extends Service
             if (user == null || file == null || listener == null) {
                 return;
             }
-
             String targetKey = buildRemoteName(user.getAccountName(), file.getRemotePath());
-            mBoundListeners.put(targetKey, listener);
+
+            if (useFilesUploadWorker(getApplicationContext())) {
+                new FilesUploadHelper().addDatatransferProgressListener(listener,targetKey);
+            }else {
+                mBoundListeners.put(targetKey, listener);
+            }
         }
 
         /**
@@ -1294,7 +1298,11 @@ public class FileUploader extends Service
             }
 
             String targetKey = buildRemoteName(ocUpload.getAccountName(), ocUpload.getRemotePath());
-            mBoundListeners.put(targetKey, listener);
+            if (useFilesUploadWorker(getApplicationContext())) {
+                new FilesUploadHelper().addDatatransferProgressListener(listener,targetKey);
+            }else {
+                mBoundListeners.put(targetKey, listener);
+            }
         }
 
         /**
@@ -1314,8 +1322,13 @@ public class FileUploader extends Service
             }
 
             String targetKey = buildRemoteName(user.getAccountName(), file.getRemotePath());
-            if (mBoundListeners.get(targetKey) == listener) {
-                mBoundListeners.remove(targetKey);
+
+            if (useFilesUploadWorker(getApplicationContext())) {
+                new FilesUploadHelper().removeDatatransferProgressListener(listener,targetKey);
+            }else {
+                if (mBoundListeners.get(targetKey) == listener) {
+                    mBoundListeners.remove(targetKey);
+                }
             }
         }
 
@@ -1334,8 +1347,13 @@ public class FileUploader extends Service
             }
 
             String targetKey = buildRemoteName(ocUpload.getAccountName(), ocUpload.getRemotePath());
-            if (mBoundListeners.get(targetKey) == listener) {
-                mBoundListeners.remove(targetKey);
+
+            if (useFilesUploadWorker(getApplicationContext())) {
+                new FilesUploadHelper().removeDatatransferProgressListener(listener,targetKey);
+            }else {
+                if (mBoundListeners.get(targetKey) == listener) {
+                    mBoundListeners.remove(targetKey);
+                }
             }
         }
 
@@ -1385,7 +1403,7 @@ public class FileUploader extends Service
          * @param remotePath  Remote path to upload the file to.
          * @return Key
          */
-        private String buildRemoteName(String accountName, String remotePath) {
+        public static String buildRemoteName(String accountName, String remotePath) {
             return accountName + remotePath;
         }
     }

+ 32 - 0
app/src/main/java/com/owncloud/android/utils/FilesUploadHelper.kt

@@ -28,7 +28,9 @@ import com.owncloud.android.MainApp
 import com.owncloud.android.datamodel.OCFile
 import com.owncloud.android.datamodel.UploadsStorageManager
 import com.owncloud.android.db.OCUpload
+import com.owncloud.android.files.services.FileUploader.FileUploaderBinder
 import com.owncloud.android.files.services.NameCollisionPolicy
+import com.owncloud.android.lib.common.network.OnDatatransferProgressListener
 import com.owncloud.android.lib.common.utils.Log_OC
 import javax.inject.Inject
 
@@ -114,4 +116,34 @@ class FilesUploadHelper {
 
         backgroundJobManager.startFilesUploadJob(user)
     }
+
+    fun addDatatransferProgressListener(
+        listener: OnDatatransferProgressListener,
+        targetKey : String
+    ) {
+        mBoundListeners[targetKey] = listener
+    }
+
+    fun removeDatatransferProgressListener(
+        listener: OnDatatransferProgressListener,
+        targetKey : String
+    ){
+        if (mBoundListeners[targetKey] === listener) {
+            mBoundListeners.remove(targetKey)
+        }
+    }
+
+    companion object Progress{
+        val mBoundListeners = HashMap<String, OnDatatransferProgressListener>()
+
+        fun onTransferProgress(accountName : String, remotePath : String, progressRate: Long, totalTransferredSoFar: Long, totalToTransfer: Long, fileName: String?) {
+            val key: String =
+                FileUploaderBinder.buildRemoteName(accountName,remotePath)
+            val boundListener = mBoundListeners[key]
+
+            boundListener?.onTransferProgress(progressRate, totalTransferredSoFar, totalToTransfer, fileName)
+            Log_OC.d("TAG","Hello")
+        }
+    }
+
 }