@@ -42,12 +42,15 @@ import eu.opencloud.android.presentation.security.biometric.BiometricManager
4242import eu.opencloud.android.presentation.security.passcode.PassCodeActivity
4343import eu.opencloud.android.presentation.security.pattern.PatternActivity
4444import eu.opencloud.android.presentation.settings.SettingsFragment.Companion.removePreferenceFromScreen
45+ import eu.opencloud.android.providers.WorkManagerProvider
46+ import org.koin.android.ext.android.inject
4547import org.koin.androidx.viewmodel.ext.android.viewModel
4648
4749class SettingsSecurityFragment : PreferenceFragmentCompat () {
4850
4951 // ViewModel
5052 private val securityViewModel by viewModel<SettingsSecurityViewModel >()
53+ private val workManagerProvider: WorkManagerProvider by inject()
5154
5255 private var screenSecurity: PreferenceScreen ? = null
5356 private var prefPasscode: CheckBoxPreference ? = null
@@ -56,6 +59,9 @@ class SettingsSecurityFragment : PreferenceFragmentCompat() {
5659 private var prefLockApplication: ListPreference ? = null
5760 private var prefLockAccessDocumentProvider: CheckBoxPreference ? = null
5861 private var prefTouchesWithOtherVisibleWindows: CheckBoxPreference ? = null
62+ private var prefDownloadEverything: CheckBoxPreference ? = null
63+ private var prefAutoSync: CheckBoxPreference ? = null
64+ private var prefPreferLocalOnConflict: CheckBoxPreference ? = null
5965
6066 private val enablePasscodeLauncher =
6167 registerForActivityResult(ActivityResultContracts .StartActivityForResult ()) { result ->
@@ -111,6 +117,16 @@ class SettingsSecurityFragment : PreferenceFragmentCompat() {
111117
112118 override fun onCreatePreferences (savedInstanceState : Bundle ? , rootKey : String? ) {
113119 setPreferencesFromResource(R .xml.settings_security, rootKey)
120+ initializePreferences(rootKey)
121+ configureLockPreferences()
122+ configureBiometricPreference()
123+ configureSecurityPreferences()
124+ configureDownloadAndSyncPreferences()
125+ }
126+
127+
128+ @Suppress(" UnusedParameter" )
129+ private fun initializePreferences (rootKey : String? ) {
114130 screenSecurity = findPreference(SCREEN_SECURITY )
115131 prefPasscode = findPreference(PassCodeActivity .PREFERENCE_SET_PASSCODE )
116132 prefPattern = findPreference(PatternActivity .PREFERENCE_SET_PATTERN )
@@ -132,10 +148,15 @@ class SettingsSecurityFragment : PreferenceFragmentCompat() {
132148 }
133149 prefLockAccessDocumentProvider = findPreference(PREFERENCE_LOCK_ACCESS_FROM_DOCUMENT_PROVIDER )
134150 prefTouchesWithOtherVisibleWindows = findPreference(PREFERENCE_TOUCHES_WITH_OTHER_VISIBLE_WINDOWS )
151+ prefDownloadEverything = findPreference(PREFERENCE_DOWNLOAD_EVERYTHING )
152+ prefAutoSync = findPreference(PREFERENCE_AUTO_SYNC )
153+ prefPreferLocalOnConflict = findPreference(PREFERENCE_PREFER_LOCAL_ON_CONFLICT )
135154
136155 prefPasscode?.isVisible = ! securityViewModel.isSecurityEnforcedEnabled()
137156 prefPattern?.isVisible = ! securityViewModel.isSecurityEnforcedEnabled()
157+ }
138158
159+ private fun configureLockPreferences () {
139160 // Passcode lock
140161 prefPasscode?.setOnPreferenceChangeListener { _: Preference ? , newValue: Any ->
141162 if (securityViewModel.isPatternSet()) {
@@ -169,8 +190,9 @@ class SettingsSecurityFragment : PreferenceFragmentCompat() {
169190 }
170191 false
171192 }
193+ }
172194
173- // Biometric lock
195+ private fun configureBiometricPreference () {
174196 if (prefBiometric != null ) {
175197 if (! BiometricManager .isHardwareDetected()) { // Biometric not supported
176198 screenSecurity?.removePreferenceFromScreen(prefBiometric)
@@ -192,8 +214,12 @@ class SettingsSecurityFragment : PreferenceFragmentCompat() {
192214 }
193215
194216 // Lock application
195- if (prefPasscode?.isChecked == false && prefPattern?.isChecked == false ) { prefLockApplication?.isEnabled = false }
217+ if (prefPasscode?.isChecked == false && prefPattern?.isChecked == false ) {
218+ prefLockApplication?.isEnabled = false
219+ }
220+ }
196221
222+ private fun configureSecurityPreferences () {
197223 // Lock access from document provider
198224 prefLockAccessDocumentProvider?.setOnPreferenceChangeListener { _: Preference ? , newValue: Any ->
199225 securityViewModel.setPrefLockAccessDocumentProvider(true )
@@ -224,6 +250,62 @@ class SettingsSecurityFragment : PreferenceFragmentCompat() {
224250 }
225251 }
226252
253+ private fun configureDownloadAndSyncPreferences () {
254+ // Download Everything Feature
255+ prefDownloadEverything?.setOnPreferenceChangeListener { _: Preference ? , newValue: Any ->
256+ if (newValue as Boolean ) {
257+ activity?.let {
258+ AlertDialog .Builder (it)
259+ .setTitle(getString(R .string.download_everything_warning_title))
260+ .setMessage(getString(R .string.download_everything_warning_message))
261+ .setNegativeButton(getString(R .string.common_no), null )
262+ .setPositiveButton(getString(R .string.common_yes)) { _, _ ->
263+ securityViewModel.setDownloadEverything(true )
264+ prefDownloadEverything?.isChecked = true
265+ workManagerProvider.enqueueDownloadEverythingWorker()
266+ }
267+ .show()
268+ .avoidScreenshotsIfNeeded()
269+ }
270+ return @setOnPreferenceChangeListener false
271+ } else {
272+ securityViewModel.setDownloadEverything(false )
273+ workManagerProvider.cancelDownloadEverythingWorker()
274+ true
275+ }
276+ }
277+
278+ // Auto-Sync Feature
279+ prefAutoSync?.setOnPreferenceChangeListener { _: Preference ? , newValue: Any ->
280+ if (newValue as Boolean ) {
281+ activity?.let {
282+ AlertDialog .Builder (it)
283+ .setTitle(getString(R .string.auto_sync_warning_title))
284+ .setMessage(getString(R .string.auto_sync_warning_message))
285+ .setNegativeButton(getString(R .string.common_no), null )
286+ .setPositiveButton(getString(R .string.common_yes)) { _, _ ->
287+ securityViewModel.setAutoSync(true )
288+ prefAutoSync?.isChecked = true
289+ workManagerProvider.enqueueLocalFileSyncWorker()
290+ }
291+ .show()
292+ .avoidScreenshotsIfNeeded()
293+ }
294+ return @setOnPreferenceChangeListener false
295+ } else {
296+ securityViewModel.setAutoSync(false )
297+ workManagerProvider.cancelLocalFileSyncWorker()
298+ true
299+ }
300+ }
301+
302+ // Conflict Resolution Strategy
303+ prefPreferLocalOnConflict?.setOnPreferenceChangeListener { _: Preference ? , newValue: Any ->
304+ securityViewModel.setPreferLocalOnConflict(newValue as Boolean )
305+ true
306+ }
307+ }
308+
227309 private fun enableBiometricAndLockApplication () {
228310 prefBiometric?.apply {
229311 isEnabled = true
@@ -246,5 +328,8 @@ class SettingsSecurityFragment : PreferenceFragmentCompat() {
246328 const val PREFERENCE_TOUCHES_WITH_OTHER_VISIBLE_WINDOWS = " touches_with_other_visible_windows"
247329 const val EXTRAS_LOCK_ENFORCED = " EXTRAS_LOCK_ENFORCED"
248330 const val PREFERENCE_LOCK_ATTEMPTS = " PrefLockAttempts"
331+ const val PREFERENCE_DOWNLOAD_EVERYTHING = " download_everything"
332+ const val PREFERENCE_AUTO_SYNC = " auto_sync_local_changes"
333+ const val PREFERENCE_PREFER_LOCAL_ON_CONFLICT = " prefer_local_on_conflict"
249334 }
250335}
0 commit comments