--- 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<String, String>? = 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()
}
}
}
--- 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<String, String> {
- 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<String, String> {
+ 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<String, String> {
+ private fun buildLicenseInfo(license: ByteString, thirdPartyLicensesMetadataInput: InputStream): Map<String, String> {
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)
}
}
}