Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PhoneNumber: Do not allow empty strings #505

Merged
merged 1 commit into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions app/src/main/java/com/chiller3/bcr/Contact.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.Context
import android.net.Uri
import android.provider.ContactsContract
import androidx.annotation.RequiresPermission
import com.chiller3.bcr.output.PhoneNumber

private val PROJECTION = arrayOf(
ContactsContract.PhoneLookup.LOOKUP_KEY,
Expand All @@ -17,12 +18,14 @@ data class ContactInfo(
)

@RequiresPermission(Manifest.permission.READ_CONTACTS)
fun findContactsByPhoneNumber(context: Context, number: String): Iterator<ContactInfo> {
fun findContactsByPhoneNumber(context: Context, number: PhoneNumber): Iterator<ContactInfo> {
val rawNumber = number.toString()

// Same heuristic as InCallUI's PhoneNumberHelper.isUriNumber()
val numberIsSip = number.contains("@") || number.contains("%40")
val numberIsSip = rawNumber.contains("@") || rawNumber.contains("%40")

val uri = ContactsContract.PhoneLookup.ENTERPRISE_CONTENT_FILTER_URI.buildUpon()
.appendPath(number)
.appendPath(rawNumber)
.appendQueryParameter(
ContactsContract.PhoneLookup.QUERY_PARAMETER_SIP_ADDRESS,
numberIsSip.toString())
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/java/com/chiller3/bcr/RecorderThread.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import com.chiller3.bcr.output.OutputDirUtils
import com.chiller3.bcr.output.OutputFile
import com.chiller3.bcr.output.OutputFilenameGenerator
import com.chiller3.bcr.output.OutputPath
import com.chiller3.bcr.output.PhoneNumber
import com.chiller3.bcr.output.Retention
import com.chiller3.bcr.rule.RecordRule
import org.json.JSONObject
Expand Down Expand Up @@ -153,14 +154,14 @@ class RecorderThread(
return
}

val numbers = hashSetOf<String>()
val numbers = hashSetOf<PhoneNumber>()

if (parentCall.details.hasProperty(Call.Details.PROPERTY_CONFERENCE)) {
for (childCall in parentCall.children) {
childCall.details?.phoneNumber?.let { numbers.add(it.toString()) }
childCall.details?.phoneNumber?.let { numbers.add(it) }
}
} else {
parentCall.details?.phoneNumber?.let { numbers.add(it.toString()) }
parentCall.details?.phoneNumber?.let { numbers.add(it) }
}

Log.i(tag, "Evaluating record rules for ${numbers.size} phone number(s)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@ import android.telecom.Call
import com.chiller3.bcr.output.PhoneNumber

val Call.Details.phoneNumber: PhoneNumber?
get() = handle?.phoneNumber?.let { PhoneNumber(it) }
get() = handle?.phoneNumber?.let {
try {
PhoneNumber(it)
} catch (e: IllegalArgumentException) {
null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class CallMetadataCollector(

Log.d(TAG, "Performing manual contact lookup")

for (contact in findContactsByPhoneNumber(context, number.toString())) {
for (contact in findContactsByPhoneNumber(context, number)) {
Log.d(TAG, "Found contact display name via manual lookup")
return contact.displayName
}
Expand Down Expand Up @@ -144,7 +144,12 @@ class CallMetadataCollector(
if (index != -1) {
number = cursor.getStringOrNull(index)?.let {
Log.d(TAG, "${prefix}Found call log phone number")
PhoneNumber(it)
try {
PhoneNumber(it)
} catch (e: IllegalArgumentException) {
Log.w(TAG, "${prefix}Invalid call log phone number", e)
null
}
}
} else {
Log.d(TAG, "${prefix}Call log entry has no phone number")
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/chiller3/bcr/output/PhoneNumber.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import android.util.Log
import java.util.Locale

data class PhoneNumber(private val number: String) {
init {
require(number.isNotEmpty()) { "Number cannot be empty" }
}

fun format(context: Context, format: Format) = when (format) {
Format.DIGITS_ONLY -> number.filter { Character.digit(it, 10) != -1 }
Format.COUNTRY_SPECIFIC -> {
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/chiller3/bcr/rule/RecordRule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.content.SharedPreferences
import android.content.pm.PackageManager
import android.util.Log
import com.chiller3.bcr.findContactsByPhoneNumber
import com.chiller3.bcr.output.PhoneNumber

sealed class RecordRule {
abstract val record: Boolean
Expand Down Expand Up @@ -105,7 +106,7 @@ sealed class RecordRule {
* @throws IllegalArgumentException if [rules] does not contain [AllCalls]
*/
fun evaluate(context: Context, rules: List<RecordRule>,
numbers: Collection<String>): Boolean {
numbers: Collection<PhoneNumber>): Boolean {
val contactsAllowed = context.checkSelfPermission(Manifest.permission.READ_CONTACTS) ==
PackageManager.PERMISSION_GRANTED
val contactLookupKeys = if (contactsAllowed) {
Expand Down
Loading