Эх сурвалжийг харах

Merge pull request #13905 from nextcloud/feature/two_way_sync_stop_all

Add disable all button to InternalTwoWaySyncActivity menu
Tobias Kaminsky 6 сар өмнө
parent
commit
861787348c

+ 1 - 0
app/build.gradle

@@ -312,6 +312,7 @@ dependencies {
     implementation 'com.caverock:androidsvg:1.4'
     implementation 'androidx.annotation:annotation:1.9.0'
     implementation 'com.vanniktech:emoji-google:0.21.0'
+    implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.8.6")
 
     // document scanner not available on FDroid (generic) due to OpenCV binaries
     gplayImplementation project(':appscan')

+ 2 - 0
app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManager.kt

@@ -130,6 +130,8 @@ interface BackgroundJobManager {
         changedFiles: Array<String> = arrayOf<String>()
     )
 
+    fun cancelTwoWaySyncJob()
+
     fun scheduleOfflineSync()
 
     fun scheduleMediaFoldersDetectionJob()

+ 4 - 0
app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt

@@ -503,6 +503,10 @@ internal class BackgroundJobManagerImpl(
         )
     }
 
+    override fun cancelTwoWaySyncJob() {
+        workManager.cancelJob(JOB_INTERNAL_TWO_WAY_SYNC)
+    }
+
     override fun scheduleOfflineSync() {
         val constrains = Constraints.Builder()
             .setRequiredNetworkType(NetworkType.UNMETERED)

+ 13 - 0
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()

+ 43 - 6
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
                     }
                 }

+ 17 - 0
app/src/main/res/menu/activity_internal_two_way_sync.xml

@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Nextcloud - Android Client
+  ~
+  ~ SPDX-FileCopyrightText: 2024 ZetaTom
+  ~ SPDX-License-Identifier: AGPL-3.0-or-later
+  -->
+<menu xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto">
+
+    <item
+        android:id="@+id/action_dismiss_two_way_sync"
+        android:contentDescription="@string/two_way_sync_activity_disable_all_button_title"
+        android:orderInCategory="1"
+        android:title="@string/two_way_sync_activity_disable_all_button_title"
+        app:showAsAction="never" />
+</menu>

+ 5 - 3
app/src/main/res/values/strings.xml

@@ -1248,7 +1248,9 @@
     <string name="please_select_a_server">Please select a server…</string>
     <string name="notification_icon_description">Unread notifications exist</string>
     <string name="unset_internal_two_way_sync_description">Remove folder from internal two way sync</string>
-    <string name="internal_two_way_sync_list_empty_headline">Two way sync not set up</string>
-    <string name="internal_two_way_sync_text">To set up a two way sync folder, please enable it in the details tab of the folder in question.</string>
-    <string name="internal_two_way_sync_headline">Internal two way sync</string>
+
+    <string name="two_way_sync_activity_empty_list_title">Two way sync not set up</string>
+    <string name="two_way_sync_activity_empty_list_desc">To set up a two way sync folder, please enable it in the details tab of the folder in question.</string>
+    <string name="two_way_sync_activity_title">Internal two way sync</string>
+    <string name="two_way_sync_activity_disable_all_button_title">Disable for all folders</string>
 </resources>