@@ -14,6 +14,7 @@ import android.widget.Toast
1414import androidx.core.content.ContextCompat
1515import com.addev.listaspam.R
1616import com.google.i18n.phonenumbers.PhoneNumberUtil
17+ import com.google.i18n.phonenumbers.Phonenumber
1718import kotlinx.coroutines.CoroutineScope
1819import kotlinx.coroutines.Dispatchers
1920import kotlinx.coroutines.launch
@@ -50,17 +51,28 @@ class ReportDialogManager(private val context: Context) {
5051 spamRadio.text = context.getString(R .string.report_spam)
5152 noSpamRadio.text = context.getString(R .string.report_not_spam)
5253
53- checkboxUnknownPhone.text = buildProviderText(context, " UnknownPhone" , getLanguageDisplayName())
54+ checkboxUnknownPhone.text =
55+ buildProviderText(context, " UnknownPhone" , getLanguageDisplayName())
5456 checkboxTellows.text = buildProviderText(context, " Tellows" , getCountryDisplayName())
55- if (! number.startsWith(" +" )) {
56- val prefix = getPrefix()
57- checkboxTruecaller.text = context.getString(R .string.truecaller_prefix_repository, prefix)
58- } else {
59- checkboxTruecaller.text = context.getString(R .string.truecaller_default_repository)
60- }
57+ val prefix = extractPrefixFromNumber(number)
58+ checkboxTruecaller.text = context.getString(
59+ R .string.truecaller_prefix_repository,
60+ " +$prefix "
61+ )
6162
6263 }
6364
65+ private fun extractPrefixFromNumber (number : String ): String {
66+ val phoneUtil = PhoneNumberUtil .getInstance()
67+ return try {
68+ val parsedNumber: Phonenumber .PhoneNumber = phoneUtil.parse(number, null )
69+ val countryCode = parsedNumber.countryCode
70+ countryCode.toString()
71+ } catch (e: Exception ) {
72+ getDevicePrefix().toString()
73+ }
74+ }
75+
6476 private fun setupDialogButtons (dialog : AlertDialog , dialogView : View , number : String ) {
6577 dialog.setOnShowListener {
6678 val button = dialog.getButton(AlertDialog .BUTTON_POSITIVE )
@@ -151,7 +163,7 @@ class ReportDialogManager(private val context: Context) {
151163
152164 if (checkboxTruecaller.isChecked) {
153165 // Add country code prefix if missing
154- val prefix = getPrefix ()
166+ val prefix = getDevicePrefix ()
155167 val formattedPhone = if (! number.startsWith(" +" )) {
156168 " $prefix$number "
157169 } else {
@@ -174,11 +186,11 @@ class ReportDialogManager(private val context: Context) {
174186 }
175187 }
176188
177- private fun getPrefix (): String {
189+ private fun getDevicePrefix (): Int {
178190 val telephonyManager = ContextCompat .getSystemService(context, TelephonyManager ::class .java)
179191 val countryIso = telephonyManager?.networkCountryIso?.uppercase()
180192 val phoneUtil = PhoneNumberUtil .getInstance()
181- return " + " + phoneUtil.getCountryCodeForRegion(countryIso)
193+ return phoneUtil.getCountryCodeForRegion(countryIso)
182194 }
183195
184196 private fun buildReportMessage (reportedTo : List <String >): String {
@@ -199,13 +211,14 @@ class ReportDialogManager(private val context: Context) {
199211 }
200212
201213 private fun getCountryDisplayName (): String {
202- val country = getTellowsApiCountry(context)
214+ val country = getTellowsApiCountry(context)?.lowercase()
203215 val countryValues = context.resources.getStringArray(R .array.entryvalues_region_preference)
204216 val countryNames = context.resources.getStringArray(R .array.entries_region_preference)
205217 return getDisplayName(country, countryValues, countryNames)
206218 ? : context.getString(R .string.unknown_value)
207219 }
208220
221+
209222 private fun getDisplayName (
210223 value : String? ,
211224 values : Array <String >,
@@ -217,7 +230,11 @@ class ReportDialogManager(private val context: Context) {
217230 }
218231 }
219232
220- private fun buildProviderText (context : Context , providerName : String , displayName : String ): String {
233+ private fun buildProviderText (
234+ context : Context ,
235+ providerName : String ,
236+ displayName : String
237+ ): String {
221238 return context.getString(R .string.provider_repository_format, providerName, displayName)
222239 }
223240}
0 commit comments