|
| 1 | +/** |
| 2 | + * ownCloud Android client application |
| 3 | + * |
| 4 | + * @author Abel García de Prada |
| 5 | + * @author Juan Carlos Garrote Gascón |
| 6 | + * |
| 7 | + * Copyright (C) 2023 ownCloud GmbH. |
| 8 | + * |
| 9 | + * This program is free software: you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU General Public License version 2, |
| 11 | + * as published by the Free Software Foundation. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU General Public License |
| 19 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | + */ |
| 21 | + |
| 22 | +package com.owncloud.android.workers |
| 23 | + |
| 24 | +import android.content.Context |
| 25 | +import androidx.work.CoroutineWorker |
| 26 | +import androidx.work.WorkerParameters |
| 27 | +import com.owncloud.android.domain.capabilities.usecases.GetStoredCapabilitiesUseCase |
| 28 | +import com.owncloud.android.domain.capabilities.usecases.RefreshCapabilitiesFromServerAsyncUseCase |
| 29 | +import com.owncloud.android.domain.files.model.OCFile |
| 30 | +import com.owncloud.android.domain.files.model.OCFile.Companion.ROOT_PATH |
| 31 | +import com.owncloud.android.domain.files.usecases.GetFileByRemotePathUseCase |
| 32 | +import com.owncloud.android.domain.spaces.usecases.GetPersonalAndProjectSpacesForAccountUseCase |
| 33 | +import com.owncloud.android.domain.spaces.usecases.RefreshSpacesFromServerAsyncUseCase |
| 34 | +import com.owncloud.android.presentation.authentication.AccountUtils |
| 35 | +import com.owncloud.android.usecases.synchronization.SynchronizeFolderUseCase |
| 36 | +import org.koin.core.component.KoinComponent |
| 37 | +import org.koin.core.component.inject |
| 38 | +import timber.log.Timber |
| 39 | + |
| 40 | +class AccountDiscoveryWorker( |
| 41 | + private val appContext: Context, |
| 42 | + private val workerParameters: WorkerParameters |
| 43 | +) : CoroutineWorker( |
| 44 | + appContext, |
| 45 | + workerParameters |
| 46 | +), KoinComponent { |
| 47 | + |
| 48 | + private val refreshCapabilitiesFromServerAsyncUseCase: RefreshCapabilitiesFromServerAsyncUseCase by inject() |
| 49 | + private val getStoredCapabilitiesUseCase: GetStoredCapabilitiesUseCase by inject() |
| 50 | + private val refreshSpacesFromServerAsyncUseCase: RefreshSpacesFromServerAsyncUseCase by inject() |
| 51 | + private val getPersonalAndProjectSpacesForAccountUseCase: GetPersonalAndProjectSpacesForAccountUseCase by inject() |
| 52 | + private val getFileByRemotePathUseCase: GetFileByRemotePathUseCase by inject() |
| 53 | + private val synchronizeFolderUseCase: SynchronizeFolderUseCase by inject() |
| 54 | + |
| 55 | + override suspend fun doWork(): Result { |
| 56 | + val accountName = workerParameters.inputData.getString(KEY_PARAM_DISCOVERY_ACCOUNT) |
| 57 | + val account = AccountUtils.getOwnCloudAccountByName(appContext, accountName) |
| 58 | + Timber.d("Account Discovery for account: $accountName and accountName: ${account.name}") |
| 59 | + |
| 60 | + if (accountName.isNullOrBlank() || account == null) return Result.failure() |
| 61 | + |
| 62 | + // 1. Refresh capabilities for account |
| 63 | + refreshCapabilitiesFromServerAsyncUseCase.execute(RefreshCapabilitiesFromServerAsyncUseCase.Params(accountName)) |
| 64 | + val capabilities = getStoredCapabilitiesUseCase.execute(GetStoredCapabilitiesUseCase.Params(accountName)) |
| 65 | + |
| 66 | + val spacesAvailableForAccount = AccountUtils.isSpacesFeatureAllowedForAccount(appContext, account, capabilities) |
| 67 | + |
| 68 | + // 2.1 Account does not support spaces |
| 69 | + if (!spacesAvailableForAccount) { |
| 70 | + val rootLegacyFolder = getFileByRemotePathUseCase.execute(GetFileByRemotePathUseCase.Params(accountName, ROOT_PATH, null)).getDataOrNull() |
| 71 | + rootLegacyFolder?.let { |
| 72 | + discoverRootFolder(it) |
| 73 | + } |
| 74 | + } else { |
| 75 | + val spacesRootFoldersToDiscover = mutableListOf<OCFile>() |
| 76 | + |
| 77 | + // 2.2 Account does support spaces |
| 78 | + refreshSpacesFromServerAsyncUseCase.execute(RefreshSpacesFromServerAsyncUseCase.Params(accountName)) |
| 79 | + val spaces = getPersonalAndProjectSpacesForAccountUseCase.execute(GetPersonalAndProjectSpacesForAccountUseCase.Params(accountName)) |
| 80 | + |
| 81 | + // First we discover the root of the personal space since it is the first thing seen after login |
| 82 | + val personalSpace = spaces.firstOrNull { it.isPersonal } |
| 83 | + personalSpace?.let { space -> |
| 84 | + val rootFolderForSpace = |
| 85 | + getFileByRemotePathUseCase.execute(GetFileByRemotePathUseCase.Params(accountName, ROOT_PATH, space.root.id)).getDataOrNull() |
| 86 | + rootFolderForSpace?.let { |
| 87 | + discoverRootFolder(it) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // Then we discover the root of the rest of spaces |
| 92 | + val spacesWithoutPersonal = spaces.filterNot { it.isPersonal } |
| 93 | + spacesWithoutPersonal.forEach { space -> |
| 94 | + // Create the root file for each space |
| 95 | + val rootFolderForSpace = |
| 96 | + getFileByRemotePathUseCase.execute(GetFileByRemotePathUseCase.Params(accountName, ROOT_PATH, space.root.id)).getDataOrNull() |
| 97 | + rootFolderForSpace?.let { |
| 98 | + spacesRootFoldersToDiscover.add(it) |
| 99 | + } |
| 100 | + } |
| 101 | + spacesRootFoldersToDiscover.forEach { |
| 102 | + discoverRootFolder(it) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return Result.success() |
| 107 | + } |
| 108 | + |
| 109 | + private fun discoverRootFolder(folder: OCFile) { |
| 110 | + synchronizeFolderUseCase.execute( |
| 111 | + SynchronizeFolderUseCase.Params( |
| 112 | + accountName = folder.owner, |
| 113 | + remotePath = folder.remotePath, |
| 114 | + spaceId = folder.spaceId, |
| 115 | + syncMode = SynchronizeFolderUseCase.SyncFolderMode.REFRESH_FOLDER |
| 116 | + ) |
| 117 | + ) |
| 118 | + } |
| 119 | + |
| 120 | + companion object { |
| 121 | + const val KEY_PARAM_DISCOVERY_ACCOUNT = "KEY_PARAM_DISCOVERY_ACCOUNT" |
| 122 | + } |
| 123 | +} |
0 commit comments