|
@@ -10,44 +10,58 @@ package com.owncloud.android.ui.activity
|
|
|
import android.annotation.SuppressLint
|
|
|
import android.os.Bundle
|
|
|
import android.view.Menu
|
|
|
-import android.view.MenuInflater
|
|
|
import android.view.MenuItem
|
|
|
import android.view.View
|
|
|
-import androidx.core.view.MenuProvider
|
|
|
+import android.widget.ArrayAdapter
|
|
|
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.nextcloud.utils.extensions.hourPlural
|
|
|
+import com.nextcloud.utils.extensions.minPlural
|
|
|
+import com.nextcloud.utils.extensions.setVisibleIf
|
|
|
import com.owncloud.android.R
|
|
|
import com.owncloud.android.databinding.InternalTwoWaySyncLayoutBinding
|
|
|
+import com.owncloud.android.lib.common.utils.Log_OC
|
|
|
import com.owncloud.android.ui.adapter.InternalTwoWaySyncAdapter
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
import kotlinx.coroutines.launch
|
|
|
+import kotlinx.coroutines.withContext
|
|
|
import javax.inject.Inject
|
|
|
+import kotlin.time.Duration.Companion.hours
|
|
|
+import kotlin.time.Duration.Companion.minutes
|
|
|
+
|
|
|
+class InternalTwoWaySyncActivity :
|
|
|
+ DrawerActivity(),
|
|
|
+ Injectable,
|
|
|
+ InternalTwoWaySyncAdapter.InternalTwoWaySyncAdapterOnUpdate {
|
|
|
+ private val tag = "InternalTwoWaySyncActivity"
|
|
|
|
|
|
-class InternalTwoWaySyncActivity : DrawerActivity(), Injectable {
|
|
|
@Inject
|
|
|
lateinit var backgroundJobManager: BackgroundJobManager
|
|
|
|
|
|
lateinit var binding: InternalTwoWaySyncLayoutBinding
|
|
|
|
|
|
private lateinit var internalTwoWaySyncAdapter: InternalTwoWaySyncAdapter
|
|
|
+ private var disableForAllFoldersMenuButton: MenuItem? = null
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
|
|
- internalTwoWaySyncAdapter = InternalTwoWaySyncAdapter(fileDataStorageManager, user.get(), this)
|
|
|
+ internalTwoWaySyncAdapter = InternalTwoWaySyncAdapter(fileDataStorageManager, user.get(), this, this)
|
|
|
|
|
|
binding = InternalTwoWaySyncLayoutBinding.inflate(layoutInflater)
|
|
|
setContentView(binding.root)
|
|
|
|
|
|
setupToolbar()
|
|
|
setupActionBar()
|
|
|
- setupMenuProvider()
|
|
|
setupTwoWaySyncAdapter()
|
|
|
setupEmptyList()
|
|
|
+ setupTwoWaySyncToggle()
|
|
|
+ setupTwoWaySyncInterval()
|
|
|
+ checkLayoutVisibilities(preferences.isTwoWaySyncEnabled)
|
|
|
}
|
|
|
|
|
|
private fun setupActionBar() {
|
|
@@ -57,12 +71,14 @@ class InternalTwoWaySyncActivity : DrawerActivity(), Injectable {
|
|
|
|
|
|
@SuppressLint("NotifyDataSetChanged")
|
|
|
private fun setupTwoWaySyncAdapter() {
|
|
|
- binding.run {
|
|
|
- list.run {
|
|
|
- setEmptyView(emptyList.emptyListView)
|
|
|
- adapter = internalTwoWaySyncAdapter
|
|
|
- layoutManager = LinearLayoutManager(this@InternalTwoWaySyncActivity)
|
|
|
- adapter?.notifyDataSetChanged()
|
|
|
+ if (preferences.isTwoWaySyncEnabled) {
|
|
|
+ binding.run {
|
|
|
+ list.run {
|
|
|
+ setEmptyView(emptyList.emptyListView)
|
|
|
+ adapter = internalTwoWaySyncAdapter
|
|
|
+ layoutManager = LinearLayoutManager(this@InternalTwoWaySyncActivity)
|
|
|
+ adapter?.notifyDataSetChanged()
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -92,46 +108,129 @@ class InternalTwoWaySyncActivity : DrawerActivity(), Injectable {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Suppress("TooGenericExceptionCaught")
|
|
|
private fun disableTwoWaySyncAndWorkers() {
|
|
|
lifecycleScope.launch(Dispatchers.IO) {
|
|
|
- backgroundJobManager.cancelTwoWaySyncJob()
|
|
|
+ try {
|
|
|
+ backgroundJobManager.cancelTwoWaySyncJob()
|
|
|
+
|
|
|
+ val currentUser = user.get()
|
|
|
|
|
|
- val folders = fileDataStorageManager.getInternalTwoWaySyncFolders(user.get())
|
|
|
- folders.forEach { folder ->
|
|
|
- FileDownloadWorker.cancelOperation(user.get().accountName, folder.fileId)
|
|
|
- backgroundJobManager.cancelFilesDownloadJob(user.get(), folder.fileId)
|
|
|
+ val folders = fileDataStorageManager.getInternalTwoWaySyncFolders(currentUser)
|
|
|
+ folders.forEach { folder ->
|
|
|
+ FileDownloadWorker.cancelOperation(currentUser.accountName, folder.fileId)
|
|
|
+ backgroundJobManager.cancelFilesDownloadJob(currentUser, folder.fileId)
|
|
|
+
|
|
|
+ folder.internalFolderSyncTimestamp = -1L
|
|
|
+ fileDataStorageManager.saveFile(folder)
|
|
|
+ }
|
|
|
|
|
|
- folder.internalFolderSyncTimestamp = -1L
|
|
|
- fileDataStorageManager.saveFile(folder)
|
|
|
+ withContext(Dispatchers.Main) {
|
|
|
+ internalTwoWaySyncAdapter.update()
|
|
|
+ }
|
|
|
+ } catch (e: Exception) {
|
|
|
+ Log_OC.d(tag, "Error caught at disableTwoWaySyncAndWorkers: $e")
|
|
|
}
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Suppress("MagicNumber")
|
|
|
+ private fun setupTwoWaySyncInterval() {
|
|
|
+ val durations = listOf(
|
|
|
+ 15.minutes to minPlural(15),
|
|
|
+ 30.minutes to minPlural(30),
|
|
|
+ 45.minutes to minPlural(45),
|
|
|
+ 1.hours to hourPlural(1),
|
|
|
+ 2.hours to hourPlural(2),
|
|
|
+ 4.hours to hourPlural(4),
|
|
|
+ 6.hours to hourPlural(6),
|
|
|
+ 8.hours to hourPlural(8),
|
|
|
+ 12.hours to hourPlural(12),
|
|
|
+ 24.hours to hourPlural(24)
|
|
|
+ )
|
|
|
+ val selectedDuration = durations.find { it.first.inWholeMinutes == preferences.twoWaySyncInterval }
|
|
|
|
|
|
- launch(Dispatchers.Main) {
|
|
|
- internalTwoWaySyncAdapter.update()
|
|
|
+ val adapter = ArrayAdapter(
|
|
|
+ this,
|
|
|
+ android.R.layout.simple_dropdown_item_1line,
|
|
|
+ durations.map { it.second }
|
|
|
+ )
|
|
|
+
|
|
|
+ binding.twoWaySyncInterval.run {
|
|
|
+ setAdapter(adapter)
|
|
|
+ setText(selectedDuration?.second ?: minPlural(15), false)
|
|
|
+ setOnItemClickListener { _, _, position, _ ->
|
|
|
+ handleDurationSelected(durations[position].first.inWholeMinutes)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private fun setupMenuProvider() {
|
|
|
- addMenuProvider(
|
|
|
- object : MenuProvider {
|
|
|
- override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
|
|
|
- menuInflater.inflate(R.menu.activity_internal_two_way_sync, menu)
|
|
|
- }
|
|
|
+ private fun handleDurationSelected(duration: Long) {
|
|
|
+ preferences.twoWaySyncInterval = duration
|
|
|
+ backgroundJobManager.scheduleInternal2WaySync(duration)
|
|
|
+ }
|
|
|
|
|
|
- override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
|
|
|
- return when (menuItem.itemId) {
|
|
|
- android.R.id.home -> {
|
|
|
- onBackPressed()
|
|
|
- true
|
|
|
- }
|
|
|
- R.id.action_dismiss_two_way_sync -> {
|
|
|
- disableTwoWaySyncAndWorkers()
|
|
|
- true
|
|
|
- }
|
|
|
- else -> false
|
|
|
- }
|
|
|
- }
|
|
|
+ private fun setupTwoWaySyncToggle() {
|
|
|
+ binding.twoWaySyncToggle.isChecked = preferences.isTwoWaySyncEnabled
|
|
|
+ binding.twoWaySyncToggle.setOnCheckedChangeListener { _, isChecked ->
|
|
|
+ preferences.setTwoWaySyncStatus(isChecked)
|
|
|
+ setupTwoWaySyncAdapter()
|
|
|
+ checkLayoutVisibilities(isChecked)
|
|
|
+ checkDisableForAllFoldersMenuButtonVisibility()
|
|
|
+
|
|
|
+ if (isChecked) {
|
|
|
+ backgroundJobManager.scheduleInternal2WaySync(preferences.twoWaySyncInterval)
|
|
|
+ } else {
|
|
|
+ backgroundJobManager.cancelInternal2WaySyncJob()
|
|
|
}
|
|
|
- )
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun checkLayoutVisibilities(condition: Boolean) {
|
|
|
+ binding.listFrameLayout.setVisibleIf(condition)
|
|
|
+ binding.twoWaySyncIntervalLayout.setVisibleIf(condition)
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
|
|
+ menuInflater.inflate(R.menu.activity_internal_two_way_sync, menu)
|
|
|
+ disableForAllFoldersMenuButton = menu?.findItem(R.id.action_dismiss_two_way_sync)
|
|
|
+ checkDisableForAllFoldersMenuButtonVisibility()
|
|
|
+ return super.onCreateOptionsMenu(menu)
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
|
|
+ when (item.itemId) {
|
|
|
+ android.R.id.home -> {
|
|
|
+ onBackPressed()
|
|
|
+ }
|
|
|
+ R.id.action_dismiss_two_way_sync -> {
|
|
|
+ disableTwoWaySyncAndWorkers()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return super.onOptionsItemSelected(item)
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun checkDisableForAllFoldersMenuButtonVisibility() {
|
|
|
+ lifecycleScope.launch {
|
|
|
+ val folderSize = withContext(Dispatchers.IO) {
|
|
|
+ fileDataStorageManager.getInternalTwoWaySyncFolders(user.get()).size
|
|
|
+ }
|
|
|
+
|
|
|
+ checkDisableForAllFoldersMenuButtonVisibility(preferences.isTwoWaySyncEnabled, folderSize)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun checkDisableForAllFoldersMenuButtonVisibility(isTwoWaySyncEnabled: Boolean, folderSize: Int) {
|
|
|
+ val showDisableButton = isTwoWaySyncEnabled && folderSize > 0
|
|
|
+
|
|
|
+ disableForAllFoldersMenuButton?.let {
|
|
|
+ it.setVisible(showDisableButton)
|
|
|
+ it.setEnabled(showDisableButton)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onUpdate(folderSize: Int) {
|
|
|
+ checkDisableForAllFoldersMenuButtonVisibility(preferences.isTwoWaySyncEnabled, folderSize)
|
|
|
}
|
|
|
}
|