Skip to content

Commit d08e917

Browse files
committed
: Add concurrent spam check with coroutines, returning on first true result; format app code
1 parent 9f03d8a commit d08e917

File tree

6 files changed

+203
-82
lines changed

6 files changed

+203
-82
lines changed

app/src/main/java/com/addev/listaspam/MainActivity.kt

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -108,29 +108,18 @@ class MainActivity : AppCompatActivity(), CallLogAdapter.OnItemChangedListener {
108108
private fun setLanguage() {
109109
if (getListaSpamApiLang(this) != null) return
110110

111-
val systemLanguage = Locale.getDefault().language.lowercase()
112-
val supportedLanguages = setOf(
113-
"en", "es", "fr", "de", "it", "ru", "sv", "pl", "pt",
114-
"nl", "no", "cz", "id", "zh", "ja", "he", "tr", "hu",
115-
"fi", "da", "th", "gk", "sk", "ro"
116-
)
111+
val systemLanguage = Locale.getDefault().language.uppercase()
112+
val supportedLanguages = resources.getStringArray(R.array.language_values).toSet()
117113

118-
val finalLang = if (supportedLanguages.contains(systemLanguage)) systemLanguage else "en"
114+
val finalLang = if (supportedLanguages.contains(systemLanguage)) systemLanguage else "EN"
119115
setListaSpamApiLang(this, finalLang.uppercase())
120116
}
121117

122118
private fun setCountry() {
123119
if (getTellowsApiCountry(this) != null) return
124120

125121
val systemCountry = Locale.getDefault().country.lowercase()
126-
val supportedCountries = setOf(
127-
"de", "sa", "dz", "ar", "au", "at", "be", "by", "br", "cl",
128-
"cn", "co", "kr", "dk", "eg", "ae", "si", "es", "ph", "fi",
129-
"fr", "gr", "hu", "in", "id", "ir", "ie", "il", "it", "jp",
130-
"mx", "ng", "no", "nz", "nl", "pk", "pe", "pl", "pt", "gb",
131-
"cz", "hk", "ru", "sg", "za", "se", "ch", "tw", "tr", "ua",
132-
"us", "ve"
133-
)
122+
val supportedCountries = resources.getStringArray(R.array.entryvalues_region_preference).toSet()
134123

135124
val finalCountry = if (supportedCountries.contains(systemCountry)) systemCountry else "us"
136125
setTellowsApiCountry(this, finalCountry)

app/src/main/java/com/addev/listaspam/SettingsActivity.kt

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,32 @@ class SettingsActivity : AppCompatActivity() {
4646
}
4747
}
4848

49-
private val exportFileLauncher = registerForActivityResult(ActivityResultContracts.CreateDocument("application/json")) { uri: Uri? ->
50-
uri?.let {
51-
if (exportAllSharedPreferences(it)) {
52-
Toast.makeText(this, "Preferencias exportadas con éxito", Toast.LENGTH_SHORT).show()
53-
} else {
54-
Toast.makeText(this, "Error al exportar preferencias", Toast.LENGTH_SHORT).show()
49+
private val exportFileLauncher =
50+
registerForActivityResult(ActivityResultContracts.CreateDocument("application/json")) { uri: Uri? ->
51+
uri?.let {
52+
if (exportAllSharedPreferences(it)) {
53+
Toast.makeText(this, "Preferencias exportadas con éxito", Toast.LENGTH_SHORT)
54+
.show()
55+
} else {
56+
Toast.makeText(this, "Error al exportar preferencias", Toast.LENGTH_SHORT)
57+
.show()
58+
}
5559
}
5660
}
57-
}
5861

59-
private val importFileLauncher = registerForActivityResult(ActivityResultContracts.OpenDocument()) { uri: Uri? ->
60-
uri?.let {
61-
if (importAllSharedPreferences(it)) {
62-
updateSettingsContainer()
63-
Toast.makeText(this, "Preferencias importadas con éxito", Toast.LENGTH_SHORT).show()
64-
} else {
65-
Toast.makeText(this, "Error al importar preferencias", Toast.LENGTH_SHORT).show()
62+
private val importFileLauncher =
63+
registerForActivityResult(ActivityResultContracts.OpenDocument()) { uri: Uri? ->
64+
uri?.let {
65+
if (importAllSharedPreferences(it)) {
66+
updateSettingsContainer()
67+
Toast.makeText(this, "Preferencias importadas con éxito", Toast.LENGTH_SHORT)
68+
.show()
69+
} else {
70+
Toast.makeText(this, "Error al importar preferencias", Toast.LENGTH_SHORT)
71+
.show()
72+
}
6673
}
6774
}
68-
}
6975

7076
private fun exportAllSharedPreferences(uri: Uri): Boolean {
7177
return try {
@@ -85,6 +91,7 @@ class SettingsActivity : AppCompatActivity() {
8591
val jsonArray = JSONArray(value)
8692
prefJsonObject.put(key, jsonArray)
8793
}
94+
8895
else -> prefJsonObject.put(key, value)
8996
}
9097
}
@@ -129,6 +136,7 @@ class SettingsActivity : AppCompatActivity() {
129136
}
130137
editor.putStringSet(key, set)
131138
}
139+
132140
is Int -> editor.putInt(key, value)
133141
is Long -> editor.putLong(key, value)
134142
is Float -> editor.putFloat(key, value)
@@ -164,9 +172,11 @@ class SettingsActivity : AppCompatActivity() {
164172
}
165173
}
166174
}
175+
167176
is Int, is Long, is Float, is Boolean, is String -> {
168177
// Tipos válidos, no hacer nada
169178
}
179+
170180
else -> {
171181
return false
172182
}

app/src/main/java/com/addev/listaspam/adapter/CallLogAdapter.kt

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import android.content.pm.PackageManager
99
import android.net.Uri
1010
import android.provider.CallLog
1111
import android.provider.ContactsContract
12-
import android.util.TypedValue
1312
import android.view.Gravity
1413
import android.view.LayoutInflater
1514
import android.view.View
@@ -22,12 +21,12 @@ import androidx.core.content.ContextCompat
2221
import androidx.recyclerview.widget.RecyclerView
2322
import com.addev.listaspam.R
2423
import com.addev.listaspam.util.CallLogEntry
25-
import com.addev.listaspam.util.SpamUtils
2624
import com.addev.listaspam.util.addNumberToWhitelist
2725
import com.addev.listaspam.util.removeSpamNumber
2826
import com.addev.listaspam.util.removeWhitelistNumber
2927
import com.addev.listaspam.util.saveSpamNumber
3028
import java.text.SimpleDateFormat
29+
import java.util.Locale
3130

3231
class CallLogAdapter(
3332
private val context: Context,
@@ -41,12 +40,18 @@ class CallLogAdapter(
4140
}
4241

4342
companion object {
44-
// URLs
4543
const val GOOGLE_URL_TEMPLATE = "https://www.google.com/search?q=%s"
4644
const val REPORT_URL_TEMPLATE = "https://www.listaspam.com/busca.php?Telefono=%s#denuncia"
4745
}
4846

49-
private val formatter = SimpleDateFormat("dd/MM/yyyy HH:mm:ss")
47+
private val locale = Locale.getDefault()
48+
49+
private val formatter: SimpleDateFormat = if (locale.language == "en") {
50+
SimpleDateFormat("MM/dd/yyyy hh:mm:ss a", locale)
51+
} else {
52+
SimpleDateFormat("dd/MM/yyyy HH:mm:ss", locale)
53+
}
54+
5055
private var onItemChangedListener: OnItemChangedListener? = null
5156

5257
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CallLogViewHolder {
@@ -56,7 +61,11 @@ class CallLogAdapter(
5661

5762
override fun onBindViewHolder(holder: CallLogViewHolder, position: Int) {
5863
val callLog = callLogs[position]
59-
holder.bind(callLog, blockedNumbers.contains(callLog.number), whitelistNumbers.contains(callLog.number))
64+
holder.bind(
65+
callLog,
66+
blockedNumbers.contains(callLog.number),
67+
whitelistNumbers.contains(callLog.number)
68+
)
6069
}
6170

6271
override fun getItemCount(): Int = callLogs.size
@@ -93,14 +102,36 @@ class CallLogAdapter(
93102
actionTextView.text = action
94103

95104
if (callLog.type == CallLog.Calls.BLOCKED_TYPE) {
96-
actionTextView.setTextColor(ContextCompat.getColor(context, android.R.color.holo_red_light))
105+
actionTextView.setTextColor(
106+
ContextCompat.getColor(
107+
context,
108+
android.R.color.holo_red_light
109+
)
110+
)
97111
} else {
98-
actionTextView.setTextColor(ContextCompat.getColor(context, android.R.color.darker_gray))
112+
actionTextView.setTextColor(
113+
ContextCompat.getColor(
114+
context,
115+
android.R.color.darker_gray
116+
)
117+
)
99118
}
100119

101120
when {
102-
isBlocked -> numberTextView.setTextColor(ContextCompat.getColor(context, android.R.color.holo_red_light))
103-
isWhitelisted -> numberTextView.setTextColor(ContextCompat.getColor(context, android.R.color.holo_blue_dark))
121+
isBlocked -> numberTextView.setTextColor(
122+
ContextCompat.getColor(
123+
context,
124+
android.R.color.holo_red_light
125+
)
126+
)
127+
128+
isWhitelisted -> numberTextView.setTextColor(
129+
ContextCompat.getColor(
130+
context,
131+
android.R.color.holo_blue_dark
132+
)
133+
)
134+
104135
else -> {
105136
numberTextView.setTextColor(ContextCompat.getColor(context, R.color.textColor))
106137
}
@@ -110,10 +141,16 @@ class CallLogAdapter(
110141
overflowMenuButton.visibility = View.GONE
111142
return
112143
}
113-
144+
114145
overflowMenuButton.visibility = View.VISIBLE
115146
overflowMenuButton.setOnClickListener {
116-
val popupMenu = PopupMenu(itemView.context, overflowMenuButton, Gravity.NO_GRAVITY, android.R.attr.popupMenuStyle, R.style.PopupMenuStyle)
147+
val popupMenu = PopupMenu(
148+
itemView.context,
149+
overflowMenuButton,
150+
Gravity.NO_GRAVITY,
151+
android.R.attr.popupMenuStyle,
152+
R.style.PopupMenuStyle
153+
)
117154
popupMenu.inflate(R.menu.item_actions)
118155

119156
setDynamicTitles(popupMenu, isBlocked, isWhitelisted)
@@ -124,10 +161,12 @@ class CallLogAdapter(
124161
searchAction(number)
125162
true
126163
}
164+
127165
R.id.report_action -> {
128166
reportAction(number)
129167
true
130168
}
169+
131170
R.id.whitelist_action -> {
132171
if (isWhitelisted) {
133172
removeWhitelistNumber(context, number)
@@ -137,6 +176,7 @@ class CallLogAdapter(
137176
onItemChangedListener?.onItemChanged(number)
138177
true
139178
}
179+
140180
R.id.block_action -> {
141181
if (isBlocked) {
142182
removeSpamNumber(context, number)
@@ -146,6 +186,7 @@ class CallLogAdapter(
146186
onItemChangedListener?.onItemChanged(number)
147187
true
148188
}
189+
149190
else -> false
150191
}
151192
}
@@ -181,9 +222,15 @@ class CallLogAdapter(
181222
}
182223

183224
private fun getContactName(context: Context, phoneNumber: String): String? {
184-
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
225+
if (ContextCompat.checkSelfPermission(
226+
context,
227+
Manifest.permission.READ_CONTACTS
228+
) == PackageManager.PERMISSION_GRANTED
229+
) {
185230
val contentResolver = context.contentResolver
186-
val uri = ContactsContract.PhoneLookup.CONTENT_FILTER_URI.buildUpon().appendPath(phoneNumber).build()
231+
val uri =
232+
ContactsContract.PhoneLookup.CONTENT_FILTER_URI.buildUpon().appendPath(phoneNumber)
233+
.build()
187234
val projection = arrayOf(ContactsContract.PhoneLookup.DISPLAY_NAME)
188235

189236
contentResolver.query(uri, projection, null, null, null)?.use { cursor ->

app/src/main/java/com/addev/listaspam/service/UpdateCheckerService.kt

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ class UpdateChecker(
3535
fun checkForUpdateSync() {
3636
try {
3737
val request = Request.Builder()
38-
.url("https://api.github.com/repos/$githubUser/$githubRepo/releases/latest")
39-
.build()
38+
.url("https://api.github.com/repos/$githubUser/$githubRepo/releases/latest").build()
4039

4140
val response = client.newCall(request).execute()
4241

@@ -54,13 +53,11 @@ class UpdateChecker(
5453
val currentVersion = getCurrentVersion()
5554

5655
if (isUpdateAvailable(currentVersion, latestTag)) {
57-
showAlert(
58-
context.getString(R.string.update_available_title),
56+
showAlert(context.getString(R.string.update_available_title),
5957
context.getString(R.string.update_available_message, latestTag),
6058
positiveAction = {
6159
redirectToGitHubReleasePage(context, downloadUrl)
62-
}
63-
)
60+
})
6461

6562
}
6663

@@ -76,12 +73,9 @@ class UpdateChecker(
7673

7774
private fun showAlert(title: String, message: String, positiveAction: (() -> Unit)? = null) {
7875
Handler(Looper.getMainLooper()).post {
79-
AlertDialog.Builder(context)
80-
.setTitle(title)
81-
.setMessage(message)
76+
AlertDialog.Builder(context).setTitle(title).setMessage(message)
8277
.setPositiveButton(R.string.aceptar) { _, _ -> positiveAction?.invoke() }
83-
.setNegativeButton(R.string.cancelar, null)
84-
.show()
78+
.setNegativeButton(R.string.cancelar, null).show()
8579
}
8680
}
8781

app/src/main/java/com/addev/listaspam/util/NotificationUtils.kt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ private const val NOTIFICATION_CHANNEL_ID = "NOTIFICATION_CHANNEL"
1414
private const val NOTIFICATION_ID = 1
1515

1616
fun sendBlockedCallNotification(context: Context, number: String, reason: String) {
17-
sendNotification(context,
17+
sendNotification(
18+
context,
1819
context.getString(R.string.notification_title_spam_blocked, number),
19-
context.getString(R.string.block_reason) + " " + reason)
20+
context.getString(R.string.block_reason) + " " + reason
21+
)
2022
}
2123

2224
fun sendNotification(context: Context, title: String, message: String) {
@@ -27,18 +29,14 @@ fun sendNotification(context: Context, title: String, message: String, durationM
2729
createNotificationChannel(context)
2830

2931
if (ActivityCompat.checkSelfPermission(
30-
context,
31-
Manifest.permission.POST_NOTIFICATIONS
32-
) != PackageManager.PERMISSION_GRANTED ||
33-
!shouldShowNotification(context)
32+
context, Manifest.permission.POST_NOTIFICATIONS
33+
) != PackageManager.PERMISSION_GRANTED || !shouldShowNotification(context)
3434
) {
3535
return
3636
}
3737

3838
val builder = NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
39-
.setSmallIcon(R.mipmap.ic_launcher)
40-
.setContentTitle(title)
41-
.setContentText(message)
39+
.setSmallIcon(R.mipmap.ic_launcher).setContentTitle(title).setContentText(message)
4240
.setPriority(NotificationCompat.PRIORITY_HIGH)
4341

4442
if (durationMs != 0L) {

0 commit comments

Comments
 (0)