Skip to content

Commit fb84d33

Browse files
committed
refactor(notifications): move the Not spam action into its own function
09e0cef4 gave onReceive a seventh return, one past detekt's ReturnCount limit. The Not spam branch now calls allowFromAlert, which does the same work: cancel the alert, confirm, allow the number for 24 hours, and report it not spam when the shared data blocked it. The test file only picks up ktlint's formatting.
1 parent 884649e commit fb84d33

2 files changed

Lines changed: 29 additions & 18 deletions

File tree

‎app/src/main/java/com/sysadmindoc/callshield/service/SpamActionReceiver.kt‎

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,8 @@ class SpamActionReceiver : BroadcastReceiver() {
6262
}
6363

6464
NotificationHelper.ACTION_NOT_SPAM -> {
65-
val number = intent.getStringExtra(NotificationHelper.EXTRA_NUMBER) ?: return
66-
val notifId = intent.getIntExtra(NotificationHelper.EXTRA_NOTIF_ID, -1)
67-
val reasonCode = BlockReasonCode.fromStored(intent.getStringExtra(NotificationHelper.EXTRA_REASON_CODE))
68-
if (notifId >= 0) {
69-
notificationManager.cancel(notifId)
70-
}
71-
Toast.makeText(appContext, appContext.getString(R.string.notif_not_spam_allowed), Toast.LENGTH_SHORT).show()
72-
suspend {
73-
repo.temporaryAllowNumber(
74-
number,
75-
System.currentTimeMillis() + NotificationHelper.NOT_SPAM_ALLOW_MS,
76-
appContext.getString(R.string.desc_allowed_from_notification),
77-
)
78-
if (NotificationHelper.notSpamReachesCommunity(reasonCode)) {
79-
CommunityContributor.reportNotSpam(appContext, repo.normalizeNumber(number))
80-
}
65+
intent.getStringExtra(NotificationHelper.EXTRA_NUMBER)?.let { number ->
66+
allowFromAlert(appContext, notificationManager, repo, intent, number)
8167
}
8268
}
8369

@@ -144,6 +130,32 @@ class SpamActionReceiver : BroadcastReceiver() {
144130
}
145131
}
146132

133+
/** "Not spam" on a blocked-call alert: let the number ring for a day, and tell the community when its data was wrong. */
134+
private fun allowFromAlert(
135+
appContext: Context,
136+
notificationManager: NotificationManager,
137+
repo: SpamRepository,
138+
intent: Intent,
139+
number: String,
140+
): suspend () -> Unit {
141+
val notifId = intent.getIntExtra(NotificationHelper.EXTRA_NOTIF_ID, -1)
142+
val reasonCode = BlockReasonCode.fromStored(intent.getStringExtra(NotificationHelper.EXTRA_REASON_CODE))
143+
if (notifId >= 0) {
144+
notificationManager.cancel(notifId)
145+
}
146+
Toast.makeText(appContext, appContext.getString(R.string.notif_not_spam_allowed), Toast.LENGTH_SHORT).show()
147+
return {
148+
repo.temporaryAllowNumber(
149+
number,
150+
System.currentTimeMillis() + NotificationHelper.NOT_SPAM_ALLOW_MS,
151+
appContext.getString(R.string.desc_allowed_from_notification),
152+
)
153+
if (NotificationHelper.notSpamReachesCommunity(reasonCode)) {
154+
CommunityContributor.reportNotSpam(appContext, repo.normalizeNumber(number))
155+
}
156+
}
157+
}
158+
147159
private fun Intent.reportType(): String =
148160
if (getBooleanExtra(NotificationHelper.EXTRA_IS_CALL, true)) {
149161
"spam"

‎app/src/test/java/com/sysadmindoc/callshield/service/BlockedAlertActionsTest.kt‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ class BlockedAlertActionsTest {
4343
notificationManager.cancelAll()
4444
}
4545

46-
private fun postedAlert(): Notification =
47-
shadowOf(notificationManager).allNotifications.single { it.flags and Notification.FLAG_GROUP_SUMMARY == 0 }
46+
private fun postedAlert(): Notification = shadowOf(notificationManager).allNotifications.single { it.flags and Notification.FLAG_GROUP_SUMMARY == 0 }
4847

4948
@Test
5049
fun `a blocked call's alert offers Not spam instead of Block forever`() {

0 commit comments

Comments
 (0)