-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
AddCalendarEnterUrlFragment.kt
178 lines (147 loc) · 6.46 KB
/
AddCalendarEnterUrlFragment.kt
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
169
170
171
172
173
174
175
176
177
178
/***************************************************************************************************
* Copyright © All Contributors. See LICENSE and AUTHORS in the root directory for details.
**************************************************************************************************/
package at.bitfire.icsdroid.ui
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.*
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Observer
import at.bitfire.icsdroid.Constants
import at.bitfire.icsdroid.HttpUtils
import at.bitfire.icsdroid.HttpUtils.toUri
import at.bitfire.icsdroid.R
import at.bitfire.icsdroid.databinding.AddCalendarEnterUrlBinding
import okhttp3.HttpUrl.Companion.toHttpUrl
import java.net.URI
import java.net.URISyntaxException
class AddCalendarEnterUrlFragment: Fragment() {
private val titleColorModel by activityViewModels<TitleColorFragment.TitleColorModel>()
private val credentialsModel by activityViewModels<CredentialsFragment.CredentialsModel>()
private lateinit var binding: AddCalendarEnterUrlBinding
val pickFile = registerForActivityResult(ActivityResultContracts.OpenDocument()) { uri: Uri? ->
if (uri != null) {
// keep the picked file accessible after the first sync and reboots
requireActivity().contentResolver.takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION)
binding.url.editText?.setText(uri.toString())
}
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, inState: Bundle?): View {
val invalidate = Observer<Any> {
requireActivity().invalidateOptionsMenu()
}
arrayOf(
titleColorModel.url,
credentialsModel.requiresAuth,
credentialsModel.username,
credentialsModel.password
).forEach {
it.observe(viewLifecycleOwner, invalidate)
}
binding = AddCalendarEnterUrlBinding.inflate(inflater, container, false)
binding.lifecycleOwner = this
binding.model = titleColorModel
setHasOptionsMenu(true)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
binding.pickStorageFile.setOnClickListener {
pickFile.launch(arrayOf("text/calendar"))
}
validateUri()
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.enter_url_fragment, menu)
}
override fun onPrepareOptionsMenu(menu: Menu) {
val itemNext = menu.findItem(R.id.next)
val uri = validateUri()
val authOK =
if (credentialsModel.requiresAuth.value == true)
!credentialsModel.username.value.isNullOrEmpty() && !credentialsModel.password.value.isNullOrEmpty()
else
true
itemNext.isEnabled = uri != null && authOK
}
/* dynamic changes */
private fun validateUri(): URI? {
var errorMsg: String? = null
var uri: URI
try {
try {
uri = URI(titleColorModel.url.value ?: return null)
} catch (e: URISyntaxException) {
Log.d(Constants.TAG, "Invalid URL", e)
errorMsg = e.localizedMessage
return null
}
Log.i(Constants.TAG, uri.toString())
if (uri.scheme.equals("webcal", true)) {
uri = URI("http", uri.authority, uri.path, uri.query, null)
titleColorModel.url.value = uri.toString()
return null
} else if (uri.scheme.equals("webcals", true)) {
uri = URI("https", uri.authority, uri.path, uri.query, null)
titleColorModel.url.value = uri.toString()
return null
}
val supportsAuthenticate = HttpUtils.supportsAuthentication(uri.toUri())
binding.credentials.visibility = if (supportsAuthenticate) View.VISIBLE else View.GONE
when (uri.scheme?.lowercase()) {
"content" -> {
// SAF file, no need for auth
}
"http", "https" -> {
// check whether the URL is valid
try {
uri.toString().toHttpUrl()
} catch (e: IllegalArgumentException) {
Log.w(Constants.TAG, "Invalid URI", e)
errorMsg = e.localizedMessage
return null
}
// extract user name and password from URL
uri.userInfo?.let { userInfo ->
val credentials = userInfo.split(':')
credentialsModel.requiresAuth.value = true
credentialsModel.username.value = credentials.elementAtOrNull(0)
credentialsModel.password.value = credentials.elementAtOrNull(1)
val urlWithoutPassword = URI(uri.scheme, null, uri.host, uri.port, uri.path, uri.query, null)
titleColorModel.url.value = urlWithoutPassword.toString()
return null
}
}
else -> {
errorMsg = getString(R.string.add_calendar_need_valid_uri)
return null
}
}
// warn if auth. required and not using HTTPS
binding.insecureAuthenticationWarning.visibility =
if (credentialsModel.requiresAuth.value == true && !uri.scheme.equals("https", true))
View.VISIBLE
else
View.GONE
} finally {
binding.url.error = errorMsg
}
return uri
}
/* actions */
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == R.id.next) {
// flush the credentials if auth toggle is disabled
if (credentialsModel.requiresAuth.value != true) {
credentialsModel.username.value = null
credentialsModel.password.value = null
}
AddCalendarValidationFragment().show(parentFragmentManager, "validation")
return true
}
return false
}
}