From ed5753381d79a742a4aecbf2b2948692b351b427 Mon Sep 17 00:00:00 2001 From: ZetaTom <70907959+ZetaTom@users.noreply.github.com> Date: Wed, 30 Oct 2024 09:51:23 +0100 Subject: [PATCH 1/6] Add disable all button to InternalTwoWaySyncActivity menu Signed-off-by: ZetaTom <70907959+ZetaTom@users.noreply.github.com> --- .../ui/activity/InternalTwoWaySyncActivity.kt | 8 ++++++++ .../res/menu/activity_internal_two_way_sync.xml | 17 +++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 app/src/main/res/menu/activity_internal_two_way_sync.xml 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..3efdcf990430 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 @@ -20,13 +20,21 @@ import com.nextcloud.client.di.Injectable import com.owncloud.android.R import com.owncloud.android.databinding.InternalTwoWaySyncLayoutBinding import com.owncloud.android.ui.adapter.InternalTwoWaySyncAdapter +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import javax.inject.Inject class InternalTwoWaySyncActivity : DrawerActivity(), Injectable { 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) 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..926c3515ab63 --- /dev/null +++ b/app/src/main/res/menu/activity_internal_two_way_sync.xml @@ -0,0 +1,17 @@ + + + + + + \ No newline at end of file From 4fbb6eb855e5b38c1660ca77787f2c4bed6e6de0 Mon Sep 17 00:00:00 2001 From: ZetaTom <70907959+ZetaTom@users.noreply.github.com> Date: Wed, 30 Oct 2024 09:53:32 +0100 Subject: [PATCH 2/6] Stop background job when cancelling two-way sync Signed-off-by: ZetaTom <70907959+ZetaTom@users.noreply.github.com> --- .../nextcloud/client/jobs/BackgroundJobManager.kt | 5 +++++ .../client/jobs/BackgroundJobManagerImpl.kt | 4 ++++ .../nextcloud/client/jobs/InternalTwoWaySyncWork.kt | 13 +++++++++++++ 3 files changed, 22 insertions(+) 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..5b6751b9a5de 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,11 @@ interface BackgroundJobManager { changedFiles: Array = arrayOf() ) + /** + * Cancel two-way sync. Existing tasks might finish, but no new invocations will occur. + */ + fun cancelTwoWaySyncJob(user: User) + 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..8013e961a667 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(user: User) { + workManager.cancelJob(JOB_INTERNAL_TWO_WAY_SYNC, user) + } + 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() From 769a9004f1bc9141e5606f98b6aa4feade34592b Mon Sep 17 00:00:00 2001 From: alperozturk Date: Wed, 30 Oct 2024 12:02:18 +0100 Subject: [PATCH 3/6] use lifeCycleScope instead Coroutine scope Signed-off-by: alperozturk --- app/build.gradle | 1 + .../owncloud/android/ui/activity/InternalTwoWaySyncActivity.kt | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) 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/owncloud/android/ui/activity/InternalTwoWaySyncActivity.kt b/app/src/main/java/com/owncloud/android/ui/activity/InternalTwoWaySyncActivity.kt index 3efdcf990430..204187b1968c 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 @@ -20,7 +20,6 @@ import com.nextcloud.client.di.Injectable import com.owncloud.android.R import com.owncloud.android.databinding.InternalTwoWaySyncLayoutBinding import com.owncloud.android.ui.adapter.InternalTwoWaySyncAdapter -import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import javax.inject.Inject From c304b88771b20e43f13a9e34ebdf156e2dd72334 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Wed, 30 Oct 2024 12:02:28 +0100 Subject: [PATCH 4/6] better and more clear translation keys Signed-off-by: alperozturk --- app/src/main/res/menu/activity_internal_two_way_sync.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 index 926c3515ab63..57ed52bc5d2d 100644 --- a/app/src/main/res/menu/activity_internal_two_way_sync.xml +++ b/app/src/main/res/menu/activity_internal_two_way_sync.xml @@ -10,8 +10,8 @@ \ No newline at end of file From a7713f385b7c85b0a63307e47728319f4fc74850 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Wed, 30 Oct 2024 12:07:26 +0100 Subject: [PATCH 5/6] every user can cancel the worker Signed-off-by: alperozturk --- .../java/com/nextcloud/client/jobs/BackgroundJobManager.kt | 5 +---- .../com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt | 4 ++-- 2 files changed, 3 insertions(+), 6 deletions(-) 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 5b6751b9a5de..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,10 +130,7 @@ interface BackgroundJobManager { changedFiles: Array = arrayOf() ) - /** - * Cancel two-way sync. Existing tasks might finish, but no new invocations will occur. - */ - fun cancelTwoWaySyncJob(user: User) + fun cancelTwoWaySyncJob() fun scheduleOfflineSync() 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 8013e961a667..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,8 +455,8 @@ internal class BackgroundJobManagerImpl( ) } - override fun cancelTwoWaySyncJob(user: User) { - workManager.cancelJob(JOB_INTERNAL_TWO_WAY_SYNC, user) + override fun cancelTwoWaySyncJob() { + workManager.cancelJob(JOB_INTERNAL_TWO_WAY_SYNC) } override fun scheduleOfflineSync() { From f2ab28498c12ef38b35eb46c721974d739e81530 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Wed, 30 Oct 2024 16:01:00 +0100 Subject: [PATCH 6/6] add missing backport Signed-off-by: alperozturk --- .../ui/activity/InternalTwoWaySyncActivity.kt | 42 ++++++++++++++++--- app/src/main/res/values/strings.xml | 8 ++-- 2 files changed, 41 insertions(+), 9 deletions(-) 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 204187b1968c..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,9 +14,12 @@ 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 @@ -25,6 +28,9 @@ 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 @@ -45,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) } @@ -54,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() } @@ -65,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 { @@ -86,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) { @@ -97,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/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