Skip to content

Commit d86e6ee

Browse files
committed
Refactor SettingsProvider methods to properties
- Change `appInForeground()` to `appInForeground` property - Change `trustSystemCerts()` to `trustSystemCerts` property - Update usages in CustomCertManager and MainActivity
1 parent a54c504 commit d86e6ee

3 files changed

Lines changed: 29 additions & 13 deletions

File tree

lib/src/main/java/at/bitfire/cert4android/CustomCertManager.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ class CustomCertManager @JvmOverloads constructor(
5050
if (!certStore.isTrusted(
5151
chain,
5252
authType,
53-
trustSystemCerts = settings.trustSystemCerts(),
54-
appInForeground = settings.appInForeground()
53+
trustSystemCerts = settings.trustSystemCerts,
54+
appInForeground = settings.appInForeground
5555
)
5656
)
5757
throw CertificateException("Certificate chain not trusted")
@@ -81,7 +81,7 @@ class CustomCertManager @JvmOverloads constructor(
8181
arrayOf(cert),
8282
"RSA",
8383
trustSystemCerts = false,
84-
appInForeground = settings.appInForeground()
84+
appInForeground = settings.appInForeground
8585
)
8686
)
8787
return true

lib/src/main/java/at/bitfire/cert4android/SettingsProvider.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,27 @@
1111
package at.bitfire.cert4android
1212

1313
/**
14-
* Provides settings for cert4android.
14+
* Provides settings for cert4android. Implementations can override the getters.
1515
*
1616
* Usually implemented by the app which uses cert4android, and then passed to cert4android classes
1717
* which need it.
1818
*/
1919
interface SettingsProvider {
2020

2121
/**
22-
* Returns the app foreground status:
22+
* The app foreground status:
2323
*
2424
* - `true`: foreground – directly launch UI ([TrustCertificateActivity]) and show notification (if possible)
2525
* - `false`: background – only show notification (if possible)
2626
* - `null`: non-interactive mode – don't show notification or launch activity
2727
*/
28-
fun appInForeground(): Boolean?
28+
val appInForeground: Boolean?
2929

3030
/**
31-
* Returns whether system certificates shall be trusted.
31+
* Whether system certificates shall be trusted.
3232
*
3333
* @return `true` if system certificates are considered trustworthy, `false` otherwise
3434
*/
35-
fun trustSystemCerts(): Boolean
35+
val trustSystemCerts: Boolean
3636

3737
}

sample-app/src/main/java/at/bitfire/cert4android/demo/MainActivity.kt

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
/*
2+
* Copyright © All Contributors. See LICENSE and AUTHORS in the root directory for details.
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
7+
*
8+
* SPDX-License-Identifier: MPL-2.0
9+
*/
10+
111
package at.bitfire.cert4android.demo
212

313
import android.Manifest
@@ -33,6 +43,7 @@ import androidx.lifecycle.MutableLiveData
3343
import androidx.lifecycle.viewModelScope
3444
import at.bitfire.cert4android.CustomCertManager
3545
import at.bitfire.cert4android.CustomCertStore
46+
import at.bitfire.cert4android.SettingsProvider
3647
import at.bitfire.cert4android.ThemeManager
3748
import kotlinx.coroutines.Dispatchers
3849
import kotlinx.coroutines.flow.MutableStateFlow
@@ -62,7 +73,7 @@ class MainActivity : ComponentActivity() {
6273
.padding(8.dp)
6374
.verticalScroll(rememberScrollState())) {
6475
Row {
65-
Checkbox(model.appInForeground.collectAsState().value, onCheckedChange = { foreground ->
76+
Checkbox(model.foreground.collectAsState().value, onCheckedChange = { foreground ->
6677
model.setInForeground(foreground)
6778
})
6879
Text("App in foreground")
@@ -131,7 +142,8 @@ class MainActivity : ComponentActivity() {
131142
val context: Context
132143
get() = getApplication()
133144

134-
val appInForeground = MutableStateFlow(true)
145+
val foreground = MutableStateFlow(true)
146+
135147
val resultMessage = MutableLiveData<String>()
136148

137149
init {
@@ -145,7 +157,7 @@ class MainActivity : ComponentActivity() {
145157
}
146158

147159
fun setInForeground(foreground: Boolean) {
148-
appInForeground.value = foreground
160+
this.foreground.value = foreground
149161
}
150162

151163
fun testAccess(url: String, trustSystemCerts: Boolean = true) = viewModelScope.launch(Dispatchers.IO) {
@@ -177,8 +189,12 @@ class MainActivity : ComponentActivity() {
177189
// set cert4android TrustManager and HostnameVerifier
178190
val certManager = CustomCertManager(
179191
certStore = CustomCertStore.getInstance(context),
180-
trustSystemCerts = trustSystemCerts,
181-
appInForeground = appInForeground
192+
settings = object : SettingsProvider {
193+
override val appInForeground
194+
get() = foreground.value
195+
override val trustSystemCerts
196+
get() = trustSystemCerts
197+
}
182198
)
183199

184200
val sslContext = SSLContext.getInstance("TLS")

0 commit comments

Comments
 (0)