Skip to content

Commit

Permalink
Implement WorkerState for replacing ServiceConnection
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 committed Dec 29, 2023
1 parent c453235 commit b0a82c5
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions app/src/main/java/com/nextcloud/model/WorkerState.kt
Original file line number Diff line number Diff line change
@@ -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 changes: 42 additions & 0 deletions app/src/main/java/com/nextcloud/model/WorkerStateLiveData.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
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;
Expand Down Expand Up @@ -160,6 +162,7 @@
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;
Expand Down Expand Up @@ -285,6 +288,7 @@ protected void onCreate(Bundle savedInstanceState) {
checkStoragePath();

initSyncBroadcastReceiver();
observeWorkerState();
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -1559,6 +1563,24 @@ public boolean isDrawerIndicatorAvailable() {
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();
Expand All @@ -1572,17 +1594,7 @@ private class ListServiceConnection implements ServiceConnection {

@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 {
Expand Down

0 comments on commit b0a82c5

Please sign in to comment.