geekdroid: remove osslicenses package. Use AboutOss library geekttrss-1.6.3
authorDa Risk <da_risk@geekorum.com>
Mon, 17 Apr 2023 12:26:09 -0400
changeset 42 4bed43bccf24
parent 41 cb7959f8d352
child 43 bb189d80c39c
geekdroid: remove osslicenses package. Use AboutOss library
geekdroid/src/main/java/com/geekorum/geekdroid/osslicenses/LicenseInfoRepository.kt
geekdroid/src/main/java/com/geekorum/geekdroid/osslicenses/OssLicenseParser.kt
--- a/geekdroid/src/main/java/com/geekorum/geekdroid/osslicenses/LicenseInfoRepository.kt	Thu Mar 23 11:09:50 2023 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,63 +0,0 @@
-/*
- * Geekdroid is a utility library for development on the Android
- * Platform.
- *
- * Copyright (C) 2017-2023 by Frederic-Charles Barthelery.
- *
- * This file is part of Geekdroid.
- *
- * Geekdroid 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.
- *
- * Geekdroid 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 Geekdroid.  If not, see <http://www.gnu.org/licenses/>.
- */
-package com.geekorum.geekdroid.osslicenses
-
-import android.content.Context
-import kotlinx.coroutines.CoroutineDispatcher
-import kotlinx.coroutines.withContext
-
-class LicenseInfoRepository(
-    private val appContext: Context,
-    private val mainCoroutineDispatcher: CoroutineDispatcher,
-    private val ioCoroutineDispatcher: CoroutineDispatcher,
-) {
-
-    private var licensesInfo: Map<String, String>? = null
-
-    suspend fun getLicensesInfo(): Map<String, String> = 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.openDefaultThirdPartyLicenses(appContext).use  { licensesInput ->
-                    OssLicenseParser.openDefaultThirdPartyLicensesMetadata(appContext).use { licensesMetadataInput ->
-                        val parser = OssLicenseParser(
-                            thirdPartyLicensesInput = licensesInput,
-                            thirdPartyLicensesMetadataInput = licensesMetadataInput)
-                        parser.parseLicenses()
-                    }
-                }
-            }
-            licensesInfo = licenses
-        }
-    }
-}
--- a/geekdroid/src/main/java/com/geekorum/geekdroid/osslicenses/OssLicenseParser.kt	Thu Mar 23 11:09:50 2023 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,122 +0,0 @@
-/*
- * Geekdroid is a utility library for development on the Android
- * Platform.
- *
- * Copyright (C) 2017-2023 by Frederic-Charles Barthelery.
- *
- * This file is part of Geekdroid.
- *
- * Geekdroid 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.
- *
- * Geekdroid 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 Geekdroid.  If not, see <http://www.gnu.org/licenses/>.
- */
-package com.geekorum.geekdroid.osslicenses
-
-import android.annotation.SuppressLint
-import android.content.Context
-import okio.ByteString
-import okio.buffer
-import okio.source
-import java.io.InputStream
-
-/**
- * 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)
-    }
-
-    private fun readLicensesFile(): ByteString {
-        return thirdPartyLicensesInput.source().use { source ->
-            source.buffer().use {
-                it.readByteString()
-            }
-        }
-    }
-
-    private fun buildLicenseInfo(license: ByteString): Map<String, String> {
-        return thirdPartyLicensesMetadataInput.source().use { source ->
-            source.buffer().use {
-                buildMap {
-                    while (true) {
-                        val line = it.readUtf8Line() ?: break
-                        if (line.isNotBlank()) {
-                            with(line.toLineParser()) {
-                                val start = readStartIdx()
-                                val length = readLength()
-                                val dependency = readName()
-                                val licenseTxt = license.substring(
-                                    beginIndex = start,
-                                    endIndex = start + length + 1
-                                ).string(Charsets.UTF_8)
-                                put(dependency, licenseTxt)
-                            }
-                        }
-                    }
-                }
-            }
-        }
-    }
-
-    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)
-        }
-
-        @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)
-        }
-    }
-}
-
-private class LicenseMetadataLineParser(
-    private val line: String
-) {
-
-    private var idx = 0
-
-    fun readStartIdx(): Int {
-        val end = line.indexOf(':', startIndex = idx)
-        val result = line.substring(idx, end).toInt()
-        idx = end + 1
-        return result
-    }
-
-    fun readLength(): Int {
-        val end = line.indexOf(' ', startIndex = idx)
-        val result = line.substring(idx, end).toInt()
-        idx = end + 1
-        return result
-    }
-
-    fun readName(): String {
-        val result = line.substring(idx)
-        idx = line.length + 1
-        return result
-    }
-
-}
-
-private fun String.toLineParser() = LicenseMetadataLineParser(this)