# HG changeset patch # User Da Risk # Date 1683061313 14400 # Node ID 9fc6b6b2e0a6a92517a40316619f68d7dbfaa3ab # Parent 4e26459b46424ba2be123ff01fd7db50d4632649 core: LicenseInfoRepository allow to specify different licenses files diff -r 4e26459b4642 -r 9fc6b6b2e0a6 core/src/main/java/com/geekorum/aboutoss/core/LicenseInfoRepository.kt --- a/core/src/main/java/com/geekorum/aboutoss/core/LicenseInfoRepository.kt Tue May 02 15:15:25 2023 -0400 +++ b/core/src/main/java/com/geekorum/aboutoss/core/LicenseInfoRepository.kt Tue May 02 17:01:53 2023 -0400 @@ -32,6 +32,8 @@ 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 @@ -51,14 +53,14 @@ private suspend fun parseLicenses() = withContext(mainCoroutineDispatcher) { if (licensesInfo == null) { val licenses = withContext(ioCoroutineDispatcher) { - OssLicenseParser.openDefaultThirdPartyLicenses(appContext).use { licensesInput -> - OssLicenseParser.openDefaultThirdPartyLicensesMetadata(appContext) + OssLicenseParser.openRawResourcesByName(appContext, thirdPartyLicensesResourceName).use { licensesInput -> + OssLicenseParser.openRawResourcesByName(appContext, thirdPartyLicenseMetadataResourceName) .use { licensesMetadataInput -> - val parser = OssLicenseParser( + val parser = OssLicenseParser() + parser.parseLicenses( thirdPartyLicensesInput = licensesInput, thirdPartyLicensesMetadataInput = licensesMetadataInput ) - parser.parseLicenses() } } } diff -r 4e26459b4642 -r 9fc6b6b2e0a6 core/src/main/java/com/geekorum/aboutoss/core/OssLicenseParser.kt --- a/core/src/main/java/com/geekorum/aboutoss/core/OssLicenseParser.kt Tue May 02 15:15:25 2023 -0400 +++ b/core/src/main/java/com/geekorum/aboutoss/core/OssLicenseParser.kt Tue May 02 17:01:53 2023 -0400 @@ -30,19 +30,22 @@ /** * Parse licences data generated by the "com.google.android.gms.oss-licenses-plugin" gradle plugin. - * [thirdPartyLicensesInput] is usually res/raw/third_party_licenses file - * [thirdPartyLicensesMetadataInput] is usually res/raw/third_party_license_metadata file */ -class OssLicenseParser( - private val thirdPartyLicensesInput: InputStream, - private val thirdPartyLicensesMetadataInput: InputStream -) { - fun parseLicenses(): Map { - val licenses = readLicensesFile() - return buildLicenseInfo(licenses) +class OssLicenseParser { + + /** + * Parse licenses + * @param [thirdPartyLicensesInput] is usually res/raw/third_party_licenses file + * @param [thirdPartyLicensesMetadataInput] is usually res/raw/third_party_license_metadata file + */ + fun parseLicenses( + thirdPartyLicensesInput: InputStream, thirdPartyLicensesMetadataInput: InputStream + ): Map { + val licenses = readLicensesFile(thirdPartyLicensesInput) + return buildLicenseInfo(licenses, thirdPartyLicensesMetadataInput) } - private fun readLicensesFile(): ByteString { + private fun readLicensesFile(thirdPartyLicensesInput: InputStream): ByteString { return thirdPartyLicensesInput.source().use { source -> source.buffer().use { it.readByteString() @@ -50,7 +53,7 @@ } } - private fun buildLicenseInfo(license: ByteString): Map { + private fun buildLicenseInfo(license: ByteString, thirdPartyLicensesMetadataInput: InputStream): Map { return thirdPartyLicensesMetadataInput.source().use { source -> source.buffer().use { buildMap { @@ -77,16 +80,19 @@ companion object { @SuppressLint("DiscouragedApi") fun openDefaultThirdPartyLicenses(context: Context): InputStream { - val thirdPartyLicensesId = context.resources.getIdentifier("third_party_licenses", "raw", context.packageName) - check(thirdPartyLicensesId != 0) { "third_party_licenses was not found in resources raw of ${context.packageName}"} - return context.resources.openRawResource(thirdPartyLicensesId) + return openRawResourcesByName(context, "third_party_licenses") } @SuppressLint("DiscouragedApi") fun openDefaultThirdPartyLicensesMetadata(context: Context): InputStream { - val thirdPartyLicensesMetadataId = context.resources.getIdentifier("third_party_license_metadata", "raw", context.packageName) - check(thirdPartyLicensesMetadataId != 0) { "third_party_license_metadata was not found in resources raw of ${context.packageName}"} - return context.resources.openRawResource(thirdPartyLicensesMetadataId) + return openRawResourcesByName(context, "third_party_license_metadata") + } + + @SuppressLint("DiscouragedApi") + fun openRawResourcesByName(context: Context, name: String): InputStream { + val resourceId = context.resources.getIdentifier(name, "raw", context.packageName) + check(resourceId != 0) { "$name was not found in resources raw of ${context.packageName}"} + return context.resources.openRawResource(resourceId) } } }