diff --git a/app/build.gradle b/app/build.gradle index 32cbf17fc7dd..0b27a73f40b5 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -312,6 +312,7 @@ dependencies { implementation 'com.caverock:androidsvg:1.4' implementation 'androidx.annotation:annotation:1.8.1' implementation 'com.vanniktech:emoji-google:0.21.0' + implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.8.6") implementation "com.github.nextcloud-deps.hwsecurity:hwsecurity-fido:$fidoVersion" implementation "com.github.nextcloud-deps.hwsecurity:hwsecurity-fido2:$fidoVersion" diff --git a/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManager.kt b/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManager.kt index d460fd623f7c..0a876754f656 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManager.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManager.kt @@ -130,6 +130,8 @@ interface BackgroundJobManager { changedFiles: Array = arrayOf() ) + fun cancelTwoWaySyncJob() + fun scheduleOfflineSync() fun scheduleMediaFoldersDetectionJob() diff --git a/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt b/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt index af345d832bfd..19999ee2088a 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt @@ -455,6 +455,10 @@ internal class BackgroundJobManagerImpl( ) } + override fun cancelTwoWaySyncJob() { + workManager.cancelJob(JOB_INTERNAL_TWO_WAY_SYNC) + } + override fun scheduleOfflineSync() { val constrains = Constraints.Builder() .setRequiredNetworkType(NetworkType.UNMETERED) diff --git a/app/src/main/java/com/nextcloud/client/jobs/InternalTwoWaySyncWork.kt b/app/src/main/java/com/nextcloud/client/jobs/InternalTwoWaySyncWork.kt index a29c9ecdfdf7..1f273d6cd703 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/InternalTwoWaySyncWork.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/InternalTwoWaySyncWork.kt @@ -29,6 +29,8 @@ class InternalTwoWaySyncWork( private val powerManagementService: PowerManagementService, private val connectivityService: ConnectivityService ) : Worker(context, params) { + private var shouldRun = true + override fun doWork(): Result { Log_OC.d(TAG, "Worker started!") @@ -50,6 +52,11 @@ class InternalTwoWaySyncWork( val folders = fileDataStorageManager.getInternalTwoWaySyncFolders(user) for (folder in folders) { + if (!shouldRun) { + Log_OC.d(TAG, "Worker was stopped!") + return Result.failure() + } + checkFreeSpace(folder)?.let { checkFreeSpaceResult -> return checkFreeSpaceResult } @@ -90,6 +97,12 @@ class InternalTwoWaySyncWork( } } + override fun onStopped() { + Log_OC.d(TAG, "OnStopped of worker called!") + shouldRun = false + super.onStopped() + } + @Suppress("TooGenericExceptionCaught") private fun checkFreeSpace(folder: OCFile): Result? { val storagePath = folder.storagePath ?: MainApp.getStoragePath() diff --git a/app/src/main/java/com/owncloud/android/ui/activity/InternalTwoWaySyncActivity.kt b/app/src/main/java/com/owncloud/android/ui/activity/InternalTwoWaySyncActivity.kt index db2bdf26ba3a..fbd0dcc26a87 100644 --- a/app/src/main/java/com/owncloud/android/ui/activity/InternalTwoWaySyncActivity.kt +++ b/app/src/main/java/com/owncloud/android/ui/activity/InternalTwoWaySyncActivity.kt @@ -14,19 +14,32 @@ import android.view.MenuInflater import android.view.MenuItem import android.view.View import androidx.core.view.MenuProvider +import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.LinearLayoutManager import com.nextcloud.android.common.ui.theme.utils.ColorRole import com.nextcloud.client.di.Injectable +import com.nextcloud.client.jobs.BackgroundJobManager +import com.nextcloud.client.jobs.download.FileDownloadWorker import com.owncloud.android.R import com.owncloud.android.databinding.InternalTwoWaySyncLayoutBinding import com.owncloud.android.ui.adapter.InternalTwoWaySyncAdapter +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import javax.inject.Inject class InternalTwoWaySyncActivity : DrawerActivity(), Injectable { + @Inject + lateinit var backgroundJobManager: BackgroundJobManager + lateinit var binding: InternalTwoWaySyncLayoutBinding + private lateinit var internalTwoWaySyncAdapter: InternalTwoWaySyncAdapter + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + internalTwoWaySyncAdapter = InternalTwoWaySyncAdapter(fileDataStorageManager, user.get(), this) + binding = InternalTwoWaySyncLayoutBinding.inflate(layoutInflater) setContentView(binding.root) @@ -38,7 +51,7 @@ class InternalTwoWaySyncActivity : DrawerActivity(), Injectable { } private fun setupActionBar() { - updateActionBarTitleAndHomeButtonByString(getString(R.string.internal_two_way_sync_headline)) + updateActionBarTitleAndHomeButtonByString(getString(R.string.two_way_sync_activity_title)) supportActionBar?.setDisplayHomeAsUpEnabled(true) } @@ -47,7 +60,7 @@ class InternalTwoWaySyncActivity : DrawerActivity(), Injectable { binding.run { list.run { setEmptyView(emptyList.emptyListView) - adapter = InternalTwoWaySyncAdapter(fileDataStorageManager, user.get(), this@InternalTwoWaySyncActivity) + adapter = internalTwoWaySyncAdapter layoutManager = LinearLayoutManager(this@InternalTwoWaySyncActivity) adapter?.notifyDataSetChanged() } @@ -58,12 +71,12 @@ class InternalTwoWaySyncActivity : DrawerActivity(), Injectable { binding.emptyList.run { emptyListViewHeadline.run { visibility = View.VISIBLE - setText(R.string.internal_two_way_sync_list_empty_headline) + setText(R.string.two_way_sync_activity_empty_list_title) } emptyListViewText.run { visibility = View.VISIBLE - setText(R.string.internal_two_way_sync_text) + setText(R.string.two_way_sync_activity_empty_list_desc) } emptyListIcon.run { @@ -79,10 +92,31 @@ class InternalTwoWaySyncActivity : DrawerActivity(), Injectable { } } + private fun disableTwoWaySyncAndWorkers() { + lifecycleScope.launch(Dispatchers.IO) { + backgroundJobManager.cancelTwoWaySyncJob() + + val folders = fileDataStorageManager.getInternalTwoWaySyncFolders(user.get()) + folders.forEach { folder -> + FileDownloadWorker.cancelOperation(user.get().accountName, folder.fileId) + backgroundJobManager.cancelFilesDownloadJob(user.get(), folder.fileId) + + folder.internalFolderSyncTimestamp = -1L + fileDataStorageManager.saveFile(folder) + } + + launch(Dispatchers.Main) { + internalTwoWaySyncAdapter.update() + } + } + } + private fun setupMenuProvider() { addMenuProvider( object : MenuProvider { - override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) = Unit + override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) { + menuInflater.inflate(R.menu.activity_internal_two_way_sync, menu) + } override fun onMenuItemSelected(menuItem: MenuItem): Boolean { return when (menuItem.itemId) { @@ -90,7 +124,10 @@ class InternalTwoWaySyncActivity : DrawerActivity(), Injectable { onBackPressed() true } - + R.id.action_dismiss_two_way_sync -> { + disableTwoWaySyncAndWorkers() + true + } else -> false } } diff --git a/app/src/main/res/menu/activity_internal_two_way_sync.xml b/app/src/main/res/menu/activity_internal_two_way_sync.xml new file mode 100644 index 000000000000..57ed52bc5d2d --- /dev/null +++ b/app/src/main/res/menu/activity_internal_two_way_sync.xml @@ -0,0 +1,17 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7da1d7011128..6108d7ccc76f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1238,7 +1238,9 @@ Sync Please select a server… Remove folder from internal two way sync - Two way sync not set up - To set up a two way sync folder, please enable it in the details tab of the folder in question. - Internal two way sync + + Two way sync not set up + To set up a two way sync folder, please enable it in the details tab of the folder in question. + Internal two way sync + Disable for all folders