Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,6 @@
<activity
android:name=".ui.activity.UserInfoActivity"
android:exported="false" />
<activity
android:name=".ui.activity.InternalTwoWaySyncActivity"
android:exported="false" />
<activity
android:name="com.nextcloud.client.widget.DashboardWidgetConfigurationActivity"
android:exported="false">
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/nextcloud/client/di/ComponentsModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ import com.owncloud.android.ui.activity.FileActivity
import com.owncloud.android.ui.activity.FileDisplayActivity
import com.owncloud.android.ui.activity.FilePickerActivity
import com.owncloud.android.ui.activity.FolderPickerActivity
import com.owncloud.android.ui.activity.InternalTwoWaySyncActivity
import com.owncloud.android.ui.activity.ManageAccountsActivity
import com.owncloud.android.ui.activity.ManageSpaceActivity
import com.owncloud.android.ui.activity.PassCodeActivity
Expand Down Expand Up @@ -122,6 +121,7 @@ import com.owncloud.android.ui.fragment.albums.bottomsheet.AlbumSharingBottomShe
import com.owncloud.android.ui.fragment.community.CommunityFragment
import com.owncloud.android.ui.fragment.contactsbackup.BackupFragment
import com.owncloud.android.ui.fragment.contactsbackup.BackupListFragment
import com.owncloud.android.ui.fragment.internalTwoWaySync.InternalTwoWaySyncFragment
import com.owncloud.android.ui.fragment.localfilelist.LocalFileListFragment
import com.owncloud.android.ui.fragment.notifications.NotificationsFragment
import com.owncloud.android.ui.fragment.autoUpload.AutoUploadFragment
Expand Down Expand Up @@ -494,7 +494,7 @@ internal interface ComponentsModule {
fun testJob(): TestJob

@ContributesAndroidInjector
fun internalTwoWaySyncActivity(): InternalTwoWaySyncActivity
fun internalTwoWaySyncFragment(): InternalTwoWaySyncFragment

@OptIn(UnstableApi::class)
@ContributesAndroidInjector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -694,8 +694,7 @@ private void setupInternalTwoWaySyncPreference() {
Preference twoWaySync = findPreference("internal_two_way_sync");

twoWaySync.setOnPreferenceClickListener(preference -> {
Intent intent = new Intent(this, InternalTwoWaySyncActivity.class);
startActivity(intent);
startActivity(NavigatorActivity.Companion.intent(this, NavigatorScreen.InternalTwoWaySync.INSTANCE));
return true;
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,94 +1,118 @@
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2026 Alper Ozturk <alper.ozturk@nextcloud.com>
* SPDX-FileCopyrightText: 2024 Tobias Kaminsky <tobias.kaminsky@nextcloud.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

package com.owncloud.android.ui.activity
package com.owncloud.android.ui.fragment.internalTwoWaySync

import android.annotation.SuppressLint
import android.content.res.ColorStateList
import android.os.Bundle
import android.view.Menu
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
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.account.User
import com.nextcloud.client.jobs.BackgroundJobManager
import com.nextcloud.client.jobs.download.FileDownloadWorker
import com.nextcloud.client.preferences.AppPreferences
import com.nextcloud.utils.extensions.getTypedActivity
import com.nextcloud.utils.extensions.hourPlural
import com.nextcloud.utils.extensions.minPlural
import com.nextcloud.utils.extensions.setVisibleIf
import com.owncloud.android.BuildConfig
import com.owncloud.android.R
import com.owncloud.android.databinding.InternalTwoWaySyncLayoutBinding
import com.owncloud.android.databinding.FragmentInternalTwoWaySyncBinding
import com.owncloud.android.datamodel.FileDataStorageManager
import com.owncloud.android.lib.common.utils.Log_OC
import com.owncloud.android.ui.activity.drawer.DrawerActivity
import com.owncloud.android.ui.adapter.InternalTwoWaySyncAdapter
import com.owncloud.android.ui.navigation.model.ActionBarStyle
import com.owncloud.android.ui.fragment.internalTwoWaySync.ui.InternalTwoWaySyncMenuProvider
import com.owncloud.android.ui.navigation.NavigatorActivity
import com.owncloud.android.utils.theme.ViewThemeUtils
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,
class InternalTwoWaySyncFragment :
Fragment(),
InternalTwoWaySyncAdapter.InternalTwoWaySyncAdapterOnUpdate {
private val tag = "InternalTwoWaySyncActivity"

@Inject
lateinit var backgroundJobManager: BackgroundJobManager
companion object {
private const val TAG = "InternalTwoWaySyncFragment"
}

@Inject lateinit var backgroundJobManager: BackgroundJobManager

@Inject lateinit var preferences: AppPreferences

@Inject lateinit var fileDataStorageManager: FileDataStorageManager

lateinit var binding: InternalTwoWaySyncLayoutBinding
@Inject lateinit var viewThemeUtils: ViewThemeUtils

private lateinit var internalTwoWaySyncAdapter: InternalTwoWaySyncAdapter
private var binding: FragmentInternalTwoWaySyncBinding? = null
private var internalTwoWaySyncAdapter: InternalTwoWaySyncAdapter? = null
private var disableForAllFoldersMenuButton: MenuItem? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
private val user: User?
get() = getTypedActivity(NavigatorActivity::class.java)?.user?.orElse(null)

internalTwoWaySyncAdapter =
InternalTwoWaySyncAdapter(fileDataStorageManager, user.get(), this, this, viewThemeUtils)
// region Lifecycle
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
val binding = FragmentInternalTwoWaySyncBinding.inflate(inflater, container, false)
this.binding = binding
return binding.root
}

binding = InternalTwoWaySyncLayoutBinding.inflate(layoutInflater)
setContentView(binding.root)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val user = user ?: return

internalTwoWaySyncAdapter =
InternalTwoWaySyncAdapter(fileDataStorageManager, user, requireContext(), this, viewThemeUtils)

showActionBar(ActionBarStyle.Plain)
setupActionBar()
setupTwoWaySyncAdapter()
setupEmptyList()
setupTwoWaySyncToggle()
setupTwoWaySyncInterval()
checkLayoutVisibilities(preferences.isTwoWaySyncEnabled)
addMenuProvider()
}

private fun setupActionBar() {
setActionBarTitle(getString(R.string.two_way_sync_activity_title))
supportActionBar?.setDisplayHomeAsUpEnabled(true)
override fun onDestroyView() {
disableForAllFoldersMenuButton = null
internalTwoWaySyncAdapter = null
super.onDestroyView()
binding = null
}
// endregion

// region Setup
@SuppressLint("NotifyDataSetChanged")
private fun setupTwoWaySyncAdapter() {
if (preferences.isTwoWaySyncEnabled) {
binding.run {
list.run {
setEmptyView(emptyList.emptyListView)
adapter = internalTwoWaySyncAdapter
layoutManager = LinearLayoutManager(this@InternalTwoWaySyncActivity)
adapter?.notifyDataSetChanged()
}
}
if (!preferences.isTwoWaySyncEnabled) return
val binding = binding ?: return

binding.list.run {
setEmptyView(binding.emptyList.emptyListView)
adapter = internalTwoWaySyncAdapter
layoutManager = LinearLayoutManager(requireContext())
adapter?.notifyDataSetChanged()
}
}

private fun setupEmptyList() {
val binding = binding ?: return

binding.emptyList.run {
emptyListViewHeadline.run {
visibility = View.VISIBLE
Expand All @@ -113,56 +137,34 @@ class InternalTwoWaySyncActivity :
}
}

private fun disableTwoWaySyncAndWorkers() {
lifecycleScope.launch(Dispatchers.IO) {
try {
backgroundJobManager.cancelTwoWaySyncJob()

val currentUser = user.get()

val folders = fileDataStorageManager.getInternalTwoWaySyncFolders(currentUser)
folders.forEach { folder ->
FileDownloadWorker.cancelOperation(currentUser.accountName, folder.fileId)
backgroundJobManager.cancelFilesDownloadJob(currentUser.accountName, folder.fileId)

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 binding = binding ?: return
val context = requireContext()

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)
15.minutes to context.minPlural(15),
30.minutes to context.minPlural(30),
45.minutes to context.minPlural(45),
1.hours to context.hourPlural(1),
2.hours to context.hourPlural(2),
4.hours to context.hourPlural(4),
6.hours to context.hourPlural(6),
8.hours to context.hourPlural(8),
12.hours to context.hourPlural(12),
24.hours to context.hourPlural(24)
)
val selectedDuration = durations.find { it.first.inWholeMinutes == preferences.twoWaySyncInterval }

val adapter = ArrayAdapter(
this,
context,
android.R.layout.simple_dropdown_item_1line,
durations.map { it.second }
)

binding.twoWaySyncInterval.run {
setAdapter(adapter)
setText(selectedDuration?.second ?: minPlural(15), false)
setText(selectedDuration?.second ?: context.minPlural(15), false)
setOnItemClickListener { _, _, position, _ ->
handleDurationSelected(durations[position].first.inWholeMinutes)
}
Expand All @@ -173,12 +175,9 @@ class InternalTwoWaySyncActivity :
}
}

private fun handleDurationSelected(duration: Long) {
preferences.twoWaySyncInterval = duration
backgroundJobManager.scheduleInternal2WaySync(duration)
}

private fun setupTwoWaySyncToggle() {
val binding = binding ?: return

binding.twoWaySyncToggle.isChecked = preferences.isTwoWaySyncEnabled
binding.twoWaySyncToggle.setOnCheckedChangeListener { _, isChecked ->
preferences.setTwoWaySyncStatus(isChecked)
Expand All @@ -195,42 +194,65 @@ class InternalTwoWaySyncActivity :
viewThemeUtils.material.colorMaterialSwitch(binding.twoWaySyncToggle)
}

private fun checkLayoutVisibilities(condition: Boolean) {
binding.listFrameLayout.setVisibleIf(condition)
binding.twoWaySyncIntervalLayout.setVisibleIf(condition)
private fun addMenuProvider() {
val menuProvider = InternalTwoWaySyncMenuProvider(
onDisableForAllFoldersItemCreated = { menuItem ->
disableForAllFoldersMenuButton = menuItem
checkDisableForAllFoldersMenuButtonVisibility()
},
onDisableForAllFolders = { disableTwoWaySyncAndWorkers() },
onRunNow = { backgroundJobManager.runNowInternal2WaySync() }
)
requireActivity().addMenuProvider(menuProvider, viewLifecycleOwner, Lifecycle.State.RESUMED)
}
// endregion

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()
menu?.findItem(R.id.action_run_two_way_sync)?.isVisible = BuildConfig.DEBUG

return super.onCreateOptionsMenu(menu)
// region Actions
private fun handleDurationSelected(duration: Long) {
preferences.twoWaySyncInterval = duration
backgroundJobManager.scheduleInternal2WaySync(duration)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
android.R.id.home -> {
onBackPressedDispatcher.onBackPressed()
}
private fun disableTwoWaySyncAndWorkers() {
val currentUser = user ?: return

R.id.action_dismiss_two_way_sync -> {
disableTwoWaySyncAndWorkers()
}
lifecycleScope.launch(Dispatchers.IO) {
try {
backgroundJobManager.cancelTwoWaySyncJob()

R.id.action_run_two_way_sync -> {
backgroundJobManager.runNowInternal2WaySync()
val folders = fileDataStorageManager.getInternalTwoWaySyncFolders(currentUser)
folders.forEach { folder ->
FileDownloadWorker.cancelOperation(currentUser.accountName, folder.fileId)
backgroundJobManager.cancelFilesDownloadJob(currentUser.accountName, folder.fileId)

folder.internalFolderSyncTimestamp = -1L
fileDataStorageManager.saveFile(folder)
}

withContext(Dispatchers.Main) {
internalTwoWaySyncAdapter?.update()
}
} catch (e: Exception) {
Log_OC.d(TAG, "Error caught at disableTwoWaySyncAndWorkers: $e")
}
}
}
// endregion

return super.onOptionsItemSelected(item)
// region Visibility
private fun checkLayoutVisibilities(condition: Boolean) {
val binding = binding ?: return

binding.listFrameLayout.setVisibleIf(condition)
binding.twoWaySyncIntervalLayout.setVisibleIf(condition)
}

private fun checkDisableForAllFoldersMenuButtonVisibility() {
lifecycleScope.launch {
val currentUser = user ?: return

viewLifecycleOwner.lifecycleScope.launch {
val folderSize = withContext(Dispatchers.IO) {
fileDataStorageManager.getInternalTwoWaySyncFolders(user.get()).size
fileDataStorageManager.getInternalTwoWaySyncFolders(currentUser).size
}

checkDisableForAllFoldersMenuButtonVisibility(preferences.isTwoWaySyncEnabled, folderSize)
Expand All @@ -249,4 +271,5 @@ class InternalTwoWaySyncActivity :
override fun onUpdate(folderSize: Int) {
checkDisableForAllFoldersMenuButtonVisibility(preferences.isTwoWaySyncEnabled, folderSize)
}
// endregion
}
Loading
Loading