-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
BlockedNumbersImporter.kt
38 lines (34 loc) · 1.17 KB
/
BlockedNumbersImporter.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package org.fossify.commons.helpers
import android.app.Activity
import android.telephony.PhoneNumberUtils
import org.fossify.commons.extensions.addBlockedNumber
import org.fossify.commons.extensions.isPhoneNumber
import org.fossify.commons.extensions.showErrorToast
import java.io.File
class BlockedNumbersImporter(
private val activity: Activity,
) {
enum class ImportResult {
IMPORT_FAIL, IMPORT_OK
}
fun importBlockedNumbers(path: String): ImportResult {
return try {
val inputStream = File(path).inputStream()
val numbers = inputStream.bufferedReader().use {
val content = it.readText().trimEnd().split(BLOCKED_NUMBERS_EXPORT_DELIMITER)
content.filter { text -> text.isPhoneNumber() }
}
if (numbers.isNotEmpty()) {
numbers.forEach { number ->
activity.addBlockedNumber(number)
}
ImportResult.IMPORT_OK
} else {
ImportResult.IMPORT_FAIL
}
} catch (e: Exception) {
activity.showErrorToast(e)
ImportResult.IMPORT_FAIL
}
}
}