Pārlūkot izejas kodu

Implement WorkerState for replacing ServiceConnection

Signed-off-by: alperozturk <alper_ozturk@proton.me>
alperozturk 1 gadu atpakaļ
vecāks
revīzija
ce463aff0b

+ 7 - 0
app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt

@@ -36,6 +36,8 @@ import com.nextcloud.client.account.User
 import com.nextcloud.client.account.UserAccountManager
 import com.nextcloud.client.notifications.download.DownloadNotificationManager
 import com.nextcloud.java.util.Optional
+import com.nextcloud.model.WorkerState
+import com.nextcloud.model.WorkerStateLiveData
 import com.owncloud.android.datamodel.FileDataStorageManager
 import com.owncloud.android.datamodel.OCFile
 import com.owncloud.android.datamodel.UploadsStorageManager
@@ -138,6 +140,7 @@ class FileDownloadWorker(
         val behaviour = inputData.keyValueMap[BEHAVIOUR] as String
         val activityName = inputData.keyValueMap[ACTIVITY_NAME] as String
         val packageName = inputData.keyValueMap[PACKAGE_NAME] as String
+        setWorkerState(user, file)
 
         val requestedDownloads: AbstractList<String> = Vector()
 
@@ -173,6 +176,10 @@ class FileDownloadWorker(
         }
     }
 
+    private fun setWorkerState(user: User, file: OCFile) {
+        WorkerStateLiveData.instance?.setWorkState(WorkerState.Download(user, file))
+    }
+
     private fun addAccountUpdateListener() {
         val am = AccountManager.get(context)
         am.addOnAccountsUpdatedListener(this, null, false)

+ 30 - 0
app/src/main/java/com/nextcloud/model/WorkerState.kt

@@ -0,0 +1,30 @@
+/*
+ * Nextcloud Android client application
+ *
+ * @author Alper Ozturk
+ * Copyright (C) 2023 Alper Ozturk
+ * Copyright (C) 2023 Nextcloud GmbH
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package com.nextcloud.model
+
+import com.nextcloud.client.account.User
+import com.owncloud.android.datamodel.OCFile
+
+sealed class WorkerState {
+    object Idle
+    class Download(var user: User, var file: OCFile): WorkerState()
+}

+ 42 - 0
app/src/main/java/com/nextcloud/model/WorkerStateLiveData.kt

@@ -0,0 +1,42 @@
+/*
+ * Nextcloud Android client application
+ *
+ * @author Alper Ozturk
+ * Copyright (C) 2023 Alper Ozturk
+ * Copyright (C) 2023 Nextcloud GmbH
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package com.nextcloud.model
+
+import androidx.lifecycle.LiveData
+
+class WorkerStateLiveData private constructor() : LiveData<WorkerState>() {
+
+    fun setWorkState(state: WorkerState) {
+        postValue(state)
+    }
+
+    companion object {
+        var instance: WorkerStateLiveData? = null
+            get() {
+                if (field == null) {
+                    field = WorkerStateLiveData()
+                }
+                return field
+            }
+            private set
+    }
+}

+ 23 - 11
app/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java

@@ -73,6 +73,8 @@ import com.nextcloud.client.network.ConnectivityService;
 import com.nextcloud.client.preferences.AppPreferences;
 import com.nextcloud.client.utils.IntentUtil;
 import com.nextcloud.java.util.Optional;
+import com.nextcloud.model.WorkerState;
+import com.nextcloud.model.WorkerStateLiveData;
 import com.nextcloud.utils.extensions.BundleExtensionsKt;
 import com.nextcloud.utils.extensions.IntentExtensionsKt;
 import com.nextcloud.utils.view.FastScrollUtils;
@@ -160,6 +162,7 @@ import androidx.core.view.MenuItemCompat;
 import androidx.fragment.app.Fragment;
 import androidx.fragment.app.FragmentManager;
 import androidx.fragment.app.FragmentTransaction;
+import androidx.lifecycle.Observer;
 import androidx.localbroadcastmanager.content.LocalBroadcastManager;
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import kotlin.Unit;
@@ -285,6 +288,7 @@ public class FileDisplayActivity extends FileActivity
         checkStoragePath();
 
         initSyncBroadcastReceiver();
+        observeWorkerState();
     }
 
     @SuppressWarnings("unchecked")
@@ -1559,6 +1563,24 @@ public class FileDisplayActivity extends FileActivity
         return isRoot(getCurrentDir());
     }
 
+    private void observeWorkerState() {
+        WorkerStateLiveData.Companion.getInstance().observe(this, state -> {
+            if (state instanceof WorkerState.Download) {
+                Log_OC.d(TAG, "Download worker started");
+                handleDownloadWorkerState();
+            }
+        });
+    }
+
+    private void handleDownloadWorkerState() {
+        if (mWaitingToPreview != null && getStorageManager() != null) {
+            mWaitingToPreview = getStorageManager().getFileById(mWaitingToPreview.getFileId());
+            if (mWaitingToPreview != null && !mWaitingToPreview.isDown()) {
+                requestForDownload();
+            }
+        }
+    }
+
     @Override
     protected ServiceConnection newTransferenceServiceConnection() {
         return new ListServiceConnection();
@@ -1572,17 +1594,7 @@ public class FileDisplayActivity extends FileActivity
 
         @Override
         public void onServiceConnected(ComponentName component, IBinder service) {
-            if (component.equals(new ComponentName(FileDisplayActivity.this, FileDownloadWorker.class))) {
-                Log_OC.d(TAG, "Download service connected");
-                mDownloaderBinder = (FileDownloadWorker.FileDownloaderBinder) service;
-                if (mWaitingToPreview != null && getStorageManager() != null) {
-                    // update the file
-                    mWaitingToPreview = getStorageManager().getFileById(mWaitingToPreview.getFileId());
-                    if (mWaitingToPreview != null && !mWaitingToPreview.isDown()) {
-                        requestForDownload();
-                    }
-                }
-            } else if (component.equals(new ComponentName(FileDisplayActivity.this, FileUploader.class))) {
+            if (component.equals(new ComponentName(FileDisplayActivity.this, FileUploader.class))) {
                 Log_OC.d(TAG, "Upload service connected");
                 mUploaderBinder = (FileUploaderBinder) service;
             } else {