|
| 1 | +package com.sysadmindoc.callshield.data |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import androidx.annotation.StringRes |
| 5 | +import com.sysadmindoc.callshield.R |
| 6 | + |
| 7 | +/** |
| 8 | + * A spam database description in words. The importers write their own field |
| 9 | + * names into it ("FCC callback_business: Unwanted Calls"), and a row that |
| 10 | + * several sources or complaint roles reported repeats itself, so Number details |
| 11 | + * and Lookup showed raw keys and the same complaint twice. The stored text and |
| 12 | + * its report count don't change; this is only how it reads, one line per kind |
| 13 | + * of complaint, and running it twice changes nothing. |
| 14 | + */ |
| 15 | +internal object SourceDescriptions { |
| 16 | + private val complaint = Regex("""^(FCC|FTC)(?:\s+(caller_id|callback_business|caller ID))?:\s*(.+)$""") |
| 17 | + private val separator = Regex("""\s*[;\n]\s*""") |
| 18 | + private val whitespace = Regex("""\s+""") |
| 19 | + |
| 20 | + private enum class Kind( |
| 21 | + @param:StringRes val format: Int, |
| 22 | + ) { |
| 23 | + FCC_CALLER_ID(R.string.source_fcc_caller_id), |
| 24 | + FCC_CALLBACK(R.string.source_fcc_callback), |
| 25 | + FCC_UNSPECIFIED(R.string.source_fcc_unspecified), |
| 26 | + FTC_CALLER_ID(R.string.source_ftc_caller_id), |
| 27 | + } |
| 28 | + |
| 29 | + fun readable( |
| 30 | + context: Context, |
| 31 | + raw: String, |
| 32 | + ): String { |
| 33 | + val subjects = linkedMapOf<Kind, LinkedHashSet<String>>() |
| 34 | + val other = LinkedHashSet<String>() |
| 35 | + for (segment in raw.split(separator)) { |
| 36 | + val text = segment.replace(whitespace, " ").trim() |
| 37 | + if (text.isEmpty()) continue |
| 38 | + val match = complaint.matchEntire(text) |
| 39 | + if (match == null) { |
| 40 | + other += text |
| 41 | + continue |
| 42 | + } |
| 43 | + val (source, role, subject) = match.destructured |
| 44 | + val kind = |
| 45 | + when { |
| 46 | + source == "FTC" -> Kind.FTC_CALLER_ID |
| 47 | + role == "callback_business" -> Kind.FCC_CALLBACK |
| 48 | + role == "caller_id" -> Kind.FCC_CALLER_ID |
| 49 | + else -> Kind.FCC_UNSPECIFIED |
| 50 | + } |
| 51 | + subjects.getOrPut(kind) { LinkedHashSet() } += subject |
| 52 | + } |
| 53 | + // "FCC: X" predates the importer splitting complaints by role, and it |
| 54 | + // repeats a subject one of the roles already names. |
| 55 | + val named = subjects[Kind.FCC_CALLER_ID].orEmpty() + subjects[Kind.FCC_CALLBACK].orEmpty() |
| 56 | + subjects[Kind.FCC_UNSPECIFIED]?.removeAll(named) |
| 57 | + val lines = |
| 58 | + Kind.entries.mapNotNull { kind -> |
| 59 | + subjects[kind]?.takeIf { it.isNotEmpty() }?.let { context.getString(kind.format, it.joinToString(", ")) } |
| 60 | + } |
| 61 | + return (lines + other).joinToString("\n") |
| 62 | + } |
| 63 | +} |
0 commit comments