diff --git a/app/src/main/kotlin/com/github/gotify/GotifyApplication.kt b/app/src/main/kotlin/com/github/gotify/GotifyApplication.kt index 9c683b7..6e157b8 100644 --- a/app/src/main/kotlin/com/github/gotify/GotifyApplication.kt +++ b/app/src/main/kotlin/com/github/gotify/GotifyApplication.kt @@ -4,9 +4,13 @@ import android.app.Application import android.app.NotificationManager import android.os.Build import androidx.preference.PreferenceManager +import com.github.gotify.api.CertUtils import com.github.gotify.log.LoggerHelper import com.github.gotify.log.UncaughtExceptionHandler import com.github.gotify.settings.ThemeHelper +import java.io.File +import java.io.FileOutputStream +import java.io.IOException import org.tinylog.kotlin.Logger class GotifyApplication : Application() { @@ -26,6 +30,24 @@ class GotifyApplication : Application() { ) } + val settings = Settings(this) + if (settings.legacyCert != null) { + Logger.info("Migrating legacy CA cert to new location") + var legacyCert: String? = null + try { + legacyCert = settings.legacyCert + settings.legacyCert = null + val caCertFile = File(settings.filesDir, CertUtils.CA_CERT_NAME) + FileOutputStream(caCertFile).use { + it.write(legacyCert?.encodeToByteArray()) + } + settings.caCertPath = caCertFile.absolutePath + Logger.info("Migration of legacy CA cert succeeded") + } catch (e: IOException) { + Logger.error(e, "Migration of legacy CA cert failed") + if (legacyCert != null) settings.legacyCert = legacyCert + } + } super.onCreate() } } diff --git a/app/src/main/kotlin/com/github/gotify/Settings.kt b/app/src/main/kotlin/com/github/gotify/Settings.kt index a6e0fd5..36aca23 100644 --- a/app/src/main/kotlin/com/github/gotify/Settings.kt +++ b/app/src/main/kotlin/com/github/gotify/Settings.kt @@ -29,10 +29,10 @@ internal class Settings(context: Context) { set(value) = sharedPreferences.edit().putString("version", value).apply() var legacyCert: String? get() = sharedPreferences.getString("cert", null) - set(value) = sharedPreferences.edit().putString("cert", value).apply() + set(value) = sharedPreferences.edit().putString("cert", value).commit().toUnit() var caCertPath: String? get() = sharedPreferences.getString("caCertPath", null) - set(value) = sharedPreferences.edit().putString("caCertPath", value).apply() + set(value) = sharedPreferences.edit().putString("caCertPath", value).commit().toUnit() var caCertCN: String? get() = sharedPreferences.getString("caCertCN", null) set(value) = sharedPreferences.edit().putString("caCertCN", value).apply() @@ -76,4 +76,7 @@ internal class Settings(context: Context) { clientCertPassword ) } + + @Suppress("UnusedReceiverParameter") + private fun Any?.toUnit() = Unit } diff --git a/app/src/main/kotlin/com/github/gotify/api/ClientFactory.kt b/app/src/main/kotlin/com/github/gotify/api/ClientFactory.kt index 936b88e..9cb550b 100644 --- a/app/src/main/kotlin/com/github/gotify/api/ClientFactory.kt +++ b/app/src/main/kotlin/com/github/gotify/api/ClientFactory.kt @@ -7,10 +7,6 @@ import com.github.gotify.client.api.UserApi import com.github.gotify.client.api.VersionApi import com.github.gotify.client.auth.ApiKeyAuth import com.github.gotify.client.auth.HttpBasicAuth -import java.io.File -import java.io.FileOutputStream -import java.io.IOException -import org.tinylog.kotlin.Logger internal object ClientFactory { private fun unauthorized( @@ -60,23 +56,6 @@ internal object ClientFactory { baseUrl: String = settings.url ): ApiClient { val client = ApiClient(authentications) - if (settings.legacyCert != null) { - Logger.info("Migrating legacy CA cert to new location") - var legacyCert: String? = null - try { - legacyCert = settings.legacyCert - settings.legacyCert = null - val caCertFile = File(settings.filesDir, CertUtils.CA_CERT_NAME) - FileOutputStream(caCertFile).use { - it.write(legacyCert?.encodeToByteArray()) - } - settings.caCertPath = caCertFile.absolutePath - Logger.info("Migration of legacy CA cert succeeded") - } catch (e: IOException) { - Logger.error(e, "Migration of legacy CA cert failed") - if (legacyCert != null) settings.legacyCert = legacyCert - } - } CertUtils.applySslSettings(client.okBuilder, sslSettings) client.adapterBuilder.baseUrl("$baseUrl/") return client