# HG changeset patch # User Da Risk # Date 1741828891 14400 # Node ID 59006722b7045ae74d3e17daec4a027ebd5f1596 # Parent a645b4ebb054731cb60e96210efcf870527ba986 core: extract LicenseInfoRepository interface diff -r a645b4ebb054 -r 59006722b704 core/src/commonMain/kotlin/LicenseInfoRepository.kt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/src/commonMain/kotlin/LicenseInfoRepository.kt Wed Mar 12 21:21:31 2025 -0400 @@ -0,0 +1,37 @@ +/* + * AboutOss is an utility library to retrieve and display + * opensource licenses in Android applications. + * + * Copyright (C) 2023 by Frederic-Charles Barthelery. + * + * This file is part of AboutOss. + * + * AboutOss is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AboutOss is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with AboutOss. If not, see . + */ +package com.geekorum.aboutoss.core + +/** + * Retrieve License information + */ +interface LicenseInfoRepository { + /** + * Get a map of license text per dependency + */ + suspend fun getLicensesInfo(): Map + + /** + * Get license text for a specific dependency + */ + suspend fun getLicenseFor(dependency: String): String +} diff -r a645b4ebb054 -r 59006722b704 core/src/main/java/com/geekorum/aboutoss/core/LicenseInfoRepository.kt --- a/core/src/main/java/com/geekorum/aboutoss/core/LicenseInfoRepository.kt Wed Mar 12 20:17:28 2025 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,73 +0,0 @@ -/* - * AboutOss is an utility library to retrieve and display - * opensource licenses in Android applications. - * - * Copyright (C) 2023 by Frederic-Charles Barthelery. - * - * This file is part of AboutOss. - * - * AboutOss is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * AboutOss is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with AboutOss. If not, see . - */ -package com.geekorum.aboutoss.core - -import android.content.Context -import com.geekorum.aboutoss.core.gms.OssLicenseParser -import com.geekorum.aboutoss.core.gms.openRawResourcesByName -import kotlinx.coroutines.CoroutineDispatcher -import kotlinx.coroutines.withContext -import okio.source - -/** - * Retrieve License information stored in application resources - */ -class LicenseInfoRepository( - private val appContext: Context, - private val mainCoroutineDispatcher: CoroutineDispatcher, - private val ioCoroutineDispatcher: CoroutineDispatcher, - private val thirdPartyLicensesResourceName: String = "third_party_licenses", - private val thirdPartyLicenseMetadataResourceName: String = "third_party_license_metadata" -) { - - private var licensesInfo: Map? = null - - suspend fun getLicensesInfo(): Map = withContext(mainCoroutineDispatcher) { - parseLicenses() - checkNotNull(licensesInfo) - } - - suspend fun getLicenseFor(dependency: String): String = withContext(mainCoroutineDispatcher) { - parseLicenses() - checkNotNull(licensesInfo).let { - return@withContext it[dependency] ?: error("Dependency not found") - } - } - - private suspend fun parseLicenses() = withContext(mainCoroutineDispatcher) { - if (licensesInfo == null) { - val licenses = withContext(ioCoroutineDispatcher) { - OssLicenseParser.openRawResourcesByName(appContext, thirdPartyLicensesResourceName).use { licensesInput -> - OssLicenseParser.openRawResourcesByName(appContext, thirdPartyLicenseMetadataResourceName) - .use { licensesMetadataInput -> - val parser = OssLicenseParser() - parser.parseLicenses( - thirdPartyLicensesInput = licensesInput.source(), - thirdPartyLicensesMetadataInput = licensesMetadataInput.source() - ) - } - } - } - licensesInfo = licenses - } - } -} \ No newline at end of file diff -r a645b4ebb054 -r 59006722b704 core/src/main/java/com/geekorum/aboutoss/core/gms/GmsLicenseInfoRepository.kt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/src/main/java/com/geekorum/aboutoss/core/gms/GmsLicenseInfoRepository.kt Wed Mar 12 21:21:31 2025 -0400 @@ -0,0 +1,72 @@ +/* + * AboutOss is an utility library to retrieve and display + * opensource licenses in Android applications. + * + * Copyright (C) 2023 by Frederic-Charles Barthelery. + * + * This file is part of AboutOss. + * + * AboutOss is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AboutOss is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with AboutOss. If not, see . + */ +package com.geekorum.aboutoss.core.gms + +import android.content.Context +import com.geekorum.aboutoss.core.LicenseInfoRepository +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.withContext +import okio.source + +/** + * Retrieve License information stored in application resources + */ +class GmsLicenseInfoRepository( + private val appContext: Context, + private val mainCoroutineDispatcher: CoroutineDispatcher, + private val ioCoroutineDispatcher: CoroutineDispatcher, + private val thirdPartyLicensesResourceName: String = "third_party_licenses", + private val thirdPartyLicenseMetadataResourceName: String = "third_party_license_metadata" +) : LicenseInfoRepository { + + private var licensesInfo: Map? = null + + override suspend fun getLicensesInfo(): Map = withContext(mainCoroutineDispatcher) { + parseLicenses() + checkNotNull(licensesInfo) + } + + override suspend fun getLicenseFor(dependency: String): String = withContext(mainCoroutineDispatcher) { + parseLicenses() + checkNotNull(licensesInfo).let { + return@withContext it[dependency] ?: error("Dependency not found") + } + } + + private suspend fun parseLicenses() = withContext(mainCoroutineDispatcher) { + if (licensesInfo == null) { + val licenses = withContext(ioCoroutineDispatcher) { + OssLicenseParser.openRawResourcesByName(appContext, thirdPartyLicensesResourceName).use { licensesInput -> + OssLicenseParser.openRawResourcesByName(appContext, thirdPartyLicenseMetadataResourceName) + .use { licensesMetadataInput -> + val parser = OssLicenseParser() + parser.parseLicenses( + thirdPartyLicensesInput = licensesInput.source(), + thirdPartyLicensesMetadataInput = licensesMetadataInput.source() + ) + } + } + } + licensesInfo = licenses + } + } +} \ No newline at end of file diff -r a645b4ebb054 -r 59006722b704 sample/src/main/java/com/geekorum/aboutoss/sampleapp/PrebuiltLicencesActivities.kt --- a/sample/src/main/java/com/geekorum/aboutoss/sampleapp/PrebuiltLicencesActivities.kt Wed Mar 12 20:17:28 2025 -0400 +++ b/sample/src/main/java/com/geekorum/aboutoss/sampleapp/PrebuiltLicencesActivities.kt Wed Mar 12 21:21:31 2025 -0400 @@ -26,7 +26,7 @@ import androidx.lifecycle.viewmodel.CreationExtras import androidx.lifecycle.viewmodel.initializer import androidx.lifecycle.viewmodel.viewModelFactory -import com.geekorum.aboutoss.core.LicenseInfoRepository +import com.geekorum.aboutoss.core.gms.GmsLicenseInfoRepository import com.geekorum.aboutoss.ui.common.OpenSourceLicensesViewModel import com.geekorum.aboutoss.ui.material3.OpenSourceLicensesActivity import com.geekorum.geekdroid.network.BrowserLauncher @@ -73,7 +73,7 @@ fun CreationExtras.createPrebuildOpenSourceLicensesViewModel(): OpenSourceLicensesViewModel { val application = this[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY]!! - val licenseInfoRepository = LicenseInfoRepository( + val licenseInfoRepository = GmsLicenseInfoRepository( appContext = application, mainCoroutineDispatcher = Dispatchers.Main, ioCoroutineDispatcher = Dispatchers.IO, diff -r a645b4ebb054 -r 59006722b704 ui/common/src/main/java/com/geekorum/aboutoss/ui/common/OpenSourceLicensesViewModel.kt --- a/ui/common/src/main/java/com/geekorum/aboutoss/ui/common/OpenSourceLicensesViewModel.kt Wed Mar 12 20:17:28 2025 -0400 +++ b/ui/common/src/main/java/com/geekorum/aboutoss/ui/common/OpenSourceLicensesViewModel.kt Wed Mar 12 21:21:31 2025 -0400 @@ -30,6 +30,7 @@ import androidx.lifecycle.viewmodel.initializer import androidx.lifecycle.viewmodel.viewModelFactory import com.geekorum.aboutoss.core.LicenseInfoRepository +import com.geekorum.aboutoss.core.gms.GmsLicenseInfoRepository import com.geekorum.geekdroid.network.BrowserLauncher import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.SharingStarted @@ -74,7 +75,7 @@ val Factory = viewModelFactory { initializer { val application = this[APPLICATION_KEY]!! - val licenseInfoRepository = LicenseInfoRepository( + val licenseInfoRepository = GmsLicenseInfoRepository( appContext = application, mainCoroutineDispatcher = Dispatchers.Main, ioCoroutineDispatcher = Dispatchers.IO