11package com.addev.listaspam.adapter
22
33import android.Manifest
4+ import android.app.AlertDialog
45import android.content.ClipData
56import android.content.ClipboardManager
67import android.content.Context
@@ -13,18 +14,28 @@ import android.view.Gravity
1314import android.view.LayoutInflater
1415import android.view.View
1516import android.view.ViewGroup
17+ import android.widget.EditText
1618import android.widget.ImageButton
1719import android.widget.PopupMenu
20+ import android.widget.ProgressBar
21+ import android.widget.RadioButton
1822import android.widget.TextView
1923import android.widget.Toast
2024import androidx.core.content.ContextCompat
2125import androidx.recyclerview.widget.RecyclerView
2226import com.addev.listaspam.R
27+ import com.addev.listaspam.util.ApiUtils
2328import com.addev.listaspam.util.CallLogEntry
2429import com.addev.listaspam.util.addNumberToWhitelist
30+ import com.addev.listaspam.util.getListaSpamApiLang
31+ import com.addev.listaspam.util.getTellowsApiCountry
2532import com.addev.listaspam.util.removeSpamNumber
2633import com.addev.listaspam.util.removeWhitelistNumber
2734import com.addev.listaspam.util.saveSpamNumber
35+ import kotlinx.coroutines.CoroutineScope
36+ import kotlinx.coroutines.Dispatchers
37+ import kotlinx.coroutines.launch
38+ import kotlinx.coroutines.withContext
2839import java.text.SimpleDateFormat
2940import java.util.Locale
3041
@@ -41,7 +52,8 @@ class CallLogAdapter(
4152
4253 companion object {
4354 const val GOOGLE_URL_TEMPLATE = " https://www.google.com/search?q=%s"
44- const val REPORT_URL_TEMPLATE = " https://www.listaspam.com/busca.php?Telefono=%s#denuncia"
55+ const val LISTA_SPAM_URL_TEMPLATE = " https://www.listaspam.com/busca.php?Telefono=%s"
56+ const val UNKNOWN_PHONE_URL_TEMPLATE = " https://www.unknownphone.com/phone/%s"
4557 }
4658
4759 private val locale = Locale .getDefault()
@@ -162,8 +174,18 @@ class CallLogAdapter(
162174 true
163175 }
164176
165- R .id.report_action -> {
166- reportAction(number)
177+ R .id.open_report_alert -> {
178+ openReportAlert(number)
179+ true
180+ }
181+
182+ R .id.open_in_lista_spam_action -> {
183+ openInListaSpam(number)
184+ true
185+ }
186+
187+ R .id.open_in_unknown_phone_action -> {
188+ openInUnknownPhone(number)
167189 true
168190 }
169191
@@ -254,8 +276,14 @@ class CallLogAdapter(
254276 ).show()
255277 }
256278
257- private fun reportAction (number : String ) {
258- val url = String .format(REPORT_URL_TEMPLATE , number)
279+ private fun openInListaSpam (number : String ) {
280+ val url = String .format(LISTA_SPAM_URL_TEMPLATE , number)
281+ val intent = Intent (Intent .ACTION_VIEW , Uri .parse(url))
282+ context.startActivity(intent)
283+ }
284+
285+ private fun openInUnknownPhone (number : String ) {
286+ val url = String .format(UNKNOWN_PHONE_URL_TEMPLATE , number)
259287 val intent = Intent (Intent .ACTION_VIEW , Uri .parse(url))
260288 context.startActivity(intent)
261289 }
@@ -269,4 +297,99 @@ class CallLogAdapter(
269297 fun setOnItemChangedListener (listener : OnItemChangedListener ) {
270298 this .onItemChangedListener = listener
271299 }
300+
301+ private fun openReportAlert (number : String ) {
302+ val dialogView = LayoutInflater .from(context).inflate(R .layout.dialog_report, null )
303+ val messageEditText = dialogView.findViewById<EditText >(R .id.messageEditText)
304+ val spamRadio = dialogView.findViewById<RadioButton >(R .id.radioSpam)
305+ val noSpamRadio = dialogView.findViewById<RadioButton >(R .id.radioNoSpam)
306+
307+ messageEditText.hint = context.getString(R .string.report_hint)
308+ spamRadio.text = context.getString(R .string.report_spam)
309+ noSpamRadio.text = context.getString(R .string.report_not_spam)
310+
311+ AlertDialog .Builder (context)
312+ .setTitle(context.getString(R .string.report_title))
313+ .setView(dialogView)
314+ .setPositiveButton(context.getString(R .string.accept), null )
315+ .setNegativeButton(context.getString(R .string.cancel), null )
316+ .create()
317+ .also { alertDialog ->
318+ alertDialog.setOnShowListener {
319+ val button = alertDialog.getButton(AlertDialog .BUTTON_POSITIVE )
320+ button.setOnClickListener {
321+ val message = messageEditText.text.toString().trim()
322+ val wordCount = message.split(" \\ s+" .toRegex()).size
323+ val charCount = message.replace(" \\ s" .toRegex(), " " ).length
324+
325+ if (wordCount < 2 || charCount < 10 ) {
326+ messageEditText.error =
327+ context.getString(R .string.report_validation_message)
328+ return @setOnClickListener
329+ }
330+
331+ if (! spamRadio.isChecked && ! noSpamRadio.isChecked) {
332+ Toast .makeText(
333+ context,
334+ context.getString(R .string.report_radio_validation),
335+ Toast .LENGTH_SHORT
336+ ).show()
337+ return @setOnClickListener
338+ }
339+
340+ val progressBar = dialogView.findViewById<ProgressBar >(R .id.progressBar)
341+ progressBar.visibility = View .VISIBLE
342+ button.isEnabled = false // Disable button to prevent duplicate submissions
343+
344+ // Launch background work
345+ CoroutineScope (Dispatchers .IO ).launch {
346+ val reportedTo = mutableListOf<String >()
347+
348+ getListaSpamApiLang(context)?.let {
349+ if (ApiUtils .reportToUnknownPhone(
350+ number,
351+ message,
352+ spamRadio.isChecked,
353+ it
354+ )
355+ ) {
356+ reportedTo.add(" UnknownPhone" )
357+ }
358+ }
359+
360+ getTellowsApiCountry(context)?.let {
361+ if (ApiUtils .reportToTellows(
362+ number,
363+ message,
364+ spamRadio.isChecked,
365+ it
366+ )
367+ ) {
368+ reportedTo.add(" Tellows" )
369+ }
370+ }
371+
372+ val reportMessage = if (reportedTo.isNotEmpty()) {
373+ context.getString(R .string.report_success_prefix) + " " + reportedTo.joinToString(
374+ context.getString(R .string.report_title)
375+ )
376+ } else {
377+ context.getString(R .string.report_failure)
378+ }
379+
380+ // Switch to main thread to show Toast and dismiss dialog
381+ withContext(Dispatchers .Main ) {
382+ progressBar.visibility = View .GONE
383+ button.isEnabled = true
384+ Toast .makeText(context, reportMessage, Toast .LENGTH_SHORT ).show()
385+ alertDialog.dismiss()
386+ }
387+ }
388+ }
389+ }
390+ }
391+ .show()
392+ }
393+
394+
272395}
0 commit comments