forked from nextcloud/android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChooseStorageLocationDialogFragment.kt
More file actions
168 lines (135 loc) · 6.5 KB
/
ChooseStorageLocationDialogFragment.kt
File metadata and controls
168 lines (135 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2024 ZetaTom <70907959+ZetaTom@users.noreply.github.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package com.nextcloud.ui
import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import android.preference.PreferenceManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.nextcloud.client.di.Injectable
import com.nextcloud.client.preferences.AppPreferencesImpl
import com.nmc.android.utils.DialogThemeUtils
import com.owncloud.android.MainApp
import com.owncloud.android.R
import com.owncloud.android.databinding.DialogDataStorageLocationBinding
import com.owncloud.android.datastorage.DataStorageProvider
import com.owncloud.android.datastorage.StoragePoint
import com.owncloud.android.datastorage.StoragePoint.PrivacyType
import com.owncloud.android.datastorage.StoragePoint.StorageType
import com.owncloud.android.ui.model.ExtendedSettingsActivityDialog
import com.owncloud.android.utils.DisplayUtils
import com.owncloud.android.utils.theme.ViewThemeUtils
import java.io.File
import javax.inject.Inject
class ChooseStorageLocationDialogFragment :
DialogFragment(),
Injectable {
private lateinit var binding: DialogDataStorageLocationBinding
@Inject
lateinit var viewThemeUtils: ViewThemeUtils
private val storagePoints = DataStorageProvider.getInstance().availableStoragePoints
private val selectedStorageType
get() = if (!binding.storageExternalRadio.isChecked) StorageType.INTERNAL else StorageType.EXTERNAL
private val selectedPrivacyType
get() = if (binding.allowMediaIndexSwitch.isChecked) PrivacyType.PUBLIC else PrivacyType.PRIVATE
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
binding = DialogDataStorageLocationBinding.inflate(layoutInflater)
viewThemeUtils.material.colorMaterialSwitch(binding.allowMediaIndexSwitch)
viewThemeUtils.platform.themeRadioButton(binding.storageInternalRadio)
viewThemeUtils.platform.themeRadioButton(binding.storageExternalRadio)
val builder = MaterialAlertDialogBuilder(requireContext()).setTitle(R.string.storage_choose_location)
.setPositiveButton(R.string.common_ok) { dialog: DialogInterface, _ ->
notifyResult()
dialog.dismiss()
}.setView(binding.root)
// NMC customization: customize dialog bg color
DialogThemeUtils.colorMaterialAlertDialogBackground(requireContext(), builder)
binding.storageRadioGroup.setOnCheckedChangeListener { _, _ ->
updateMediaIndexSwitch()
}
binding.allowMediaIndexSwitch.setOnCheckedChangeListener { _, _ ->
updateStorageTypeSelection()
}
return builder.create()
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View =
binding.root
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
setupLocationSelection()
super.onViewCreated(view, savedInstanceState)
}
override fun onDismiss(dialog: DialogInterface) {
super.onDismiss(dialog)
activity?.finish()
}
private fun setupLocationSelection() {
updateStorageTypeSelection()
val currentStorageLocation = getCurrentStorageLocation() ?: return
val radioButton = when (currentStorageLocation.storageType) {
StorageType.EXTERNAL -> binding.storageExternalRadio
else -> binding.storageInternalRadio
}
radioButton.isChecked = true
updateMediaIndexSwitch()
}
private fun getStoragePointLabel(storageType: StorageType, privacyType: PrivacyType): String {
val typeString = when (storageType) {
StorageType.INTERNAL -> getString(R.string.storage_internal_storage)
StorageType.EXTERNAL -> getString(R.string.storage_external_storage)
}
val storagePath =
storagePoints.find { it.storageType == storageType && it.privacyType == privacyType }?.path
return storagePath?.let {
val file = File(it)
val totalSpace = file.totalSpace
val usedSpace = totalSpace - file.freeSpace
return String.format(
getString(R.string.file_migration_free_space),
typeString,
DisplayUtils.bytesToHumanReadable(usedSpace),
DisplayUtils.bytesToHumanReadable(totalSpace)
)
} ?: typeString
}
private fun updateMediaIndexSwitch() {
val privacyTypes =
storagePoints.filter { it.storageType == selectedStorageType }.map { it.privacyType }.distinct()
binding.allowMediaIndexSwitch.isEnabled = privacyTypes.size > 1
binding.allowMediaIndexSwitch.isChecked = privacyTypes.contains(PrivacyType.PUBLIC)
}
private fun updateStorageTypeSelection() {
val hasInternalStorage = storagePoints.any { it.storageType == StorageType.INTERNAL }
val hasExternalStorage = storagePoints.any { it.storageType == StorageType.EXTERNAL }
binding.storageInternalRadio.isEnabled = hasInternalStorage
binding.storageInternalRadio.text = getStoragePointLabel(StorageType.INTERNAL, selectedPrivacyType)
binding.storageExternalRadio.isEnabled = hasExternalStorage
binding.storageExternalRadio.text = getStoragePointLabel(StorageType.EXTERNAL, selectedPrivacyType)
}
private fun getCurrentStorageLocation(): StoragePoint? {
val appContext = MainApp.getAppContext()
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(appContext)
val storagePath = sharedPreferences.getString(AppPreferencesImpl.STORAGE_PATH, appContext.filesDir.absolutePath)
return storagePoints.find { it.path == storagePath }
}
private fun notifyResult() {
val newPath =
storagePoints.find { it.storageType == selectedStorageType && it.privacyType == selectedPrivacyType }
?: return
val resultBundle = Bundle().apply {
putString(ExtendedSettingsActivityDialog.StorageLocation.key, newPath.path)
}
parentFragmentManager.setFragmentResult(ExtendedSettingsActivityDialog.StorageLocation.key, resultBundle)
}
companion object {
@JvmStatic
val TAG: String = Companion::class.java.simpleName
}
}