Skip to content

Commit db26d5c

Browse files
committed
Minor fixes
1 parent bf6b4fe commit db26d5c

4 files changed

Lines changed: 41 additions & 23 deletions

File tree

core/src/main/java/com/kirich1409/androidnotificationdsl/action/Actions.kt

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,68 +18,70 @@ class Actions @PublishedApi internal constructor(private val builder: Notificati
1818
/**
1919
* Add an action to this notification. Actions are typically displayed by
2020
* the system as a button adjacent to the notification content.
21+
*
22+
* On pre-Lollpop devices invisible actions will be ignored
2123
*/
2224
fun action(
2325
title: CharSequence?,
2426
intent: PendingIntent?,
2527
icon: IconCompat? = null,
26-
visible: Boolean = false
28+
visible: Boolean = true
2729
) {
28-
if (visible || isInvisibleActionsSupported()) {
29-
val action = NotificationCompat.Action.Builder(icon, title, intent).build()
30-
addAction(action, visible)
30+
addAction(visible) {
31+
NotificationCompat.Action.Builder(icon, title, intent)
3132
}
3233
}
3334

3435
/**
3536
* Add an action to this notification. Actions are typically displayed by
3637
* the system as a button adjacent to the notification content.
38+
*
39+
* On pre-Lollpop devices invisible actions will be ignored
3740
*/
3841
fun action(
3942
title: CharSequence?,
4043
intent: PendingIntent?,
4144
@DrawableRes icon: Int,
42-
visible: Boolean = false
45+
visible: Boolean = true
4346
) {
44-
if (visible || isInvisibleActionsSupported()) {
45-
val action = NotificationCompat.Action.Builder(icon, title, intent).build()
46-
addAction(action, visible)
47+
addAction(visible) {
48+
NotificationCompat.Action.Builder(icon, title, intent)
4749
}
4850
}
4951

5052
/**
5153
* Add an action to this notification. Actions are typically displayed by
5254
* the system as a button adjacent to the notification content.
55+
*
56+
* On pre-Lollpop devices invisible actions will be ignored
5357
*/
5458
inline fun action(
5559
title: CharSequence?,
5660
intent: PendingIntent?,
5761
icon: IconCompat? = null,
58-
visible: Boolean = false,
59-
body: @NotificationActionsMarker Action.() -> Unit
62+
visible: Boolean = true,
63+
crossinline body: @NotificationActionsMarker Action.() -> Unit
6064
) {
61-
if (visible || isInvisibleActionsSupported()) {
62-
val actionBuilder = NotificationCompat.Action.Builder(icon, title, intent)
63-
Action(actionBuilder).body()
64-
addAction(actionBuilder.build(), visible)
65+
addAction(visible) {
66+
NotificationCompat.Action.Builder(icon, title, intent).apply { Action(this).body() }
6567
}
6668
}
6769

6870
/**
6971
* Add an action to this notification. Actions are typically displayed by
7072
* the system as a button adjacent to the notification content.
73+
*
74+
* On pre-Lollpop devices invisible actions will be ignored
7175
*/
7276
inline fun action(
7377
title: CharSequence?,
7478
intent: PendingIntent?,
7579
@DrawableRes icon: Int,
76-
visible: Boolean = false,
77-
body: @NotificationActionsMarker Action.() -> Unit
80+
visible: Boolean = true,
81+
crossinline body: @NotificationActionsMarker Action.() -> Unit
7882
) {
79-
if (visible || isInvisibleActionsSupported()) {
80-
val actionBuilder = NotificationCompat.Action.Builder(icon, title, intent)
81-
Action(actionBuilder).body()
82-
addAction(actionBuilder.build(), visible)
83+
addAction(visible) {
84+
NotificationCompat.Action.Builder(icon, title, intent).apply { Action(this).body() }
8385
}
8486
}
8587

@@ -89,14 +91,23 @@ class Actions @PublishedApi internal constructor(private val builder: Notificati
8991
*
9092
* On pre-Lollpop devices invisible actions will be ignored
9193
*
92-
* @param visible is actions are never displayed by the system,
93-
* but can be retrieved and used by other application listening to system notifications
94+
* @param visible is actions are never displayed by the system, but can be retrieved
95+
* and used by other application listening to system notifications
9496
*/
95-
fun addAction(action: NotificationCompat.Action, visible: Boolean = false) {
97+
fun addAction(action: NotificationCompat.Action, visible: Boolean = true) {
9698
if (visible) {
9799
builder.addAction(action)
98100
} else if (isInvisibleActionsSupported()) {
99101
builder.addInvisibleAction(action)
100102
}
101103
}
104+
105+
@PublishedApi
106+
internal fun addAction(visible: Boolean = true, action: () -> NotificationCompat.Action.Builder) {
107+
if (visible) {
108+
builder.addAction(action().build())
109+
} else if (isInvisibleActionsSupported()) {
110+
builder.addInvisibleAction(action().build())
111+
}
112+
}
102113
}

core/src/main/java/com/kirich1409/androidnotificationdsl/internal/Collections.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import androidx.collection.SparseArrayCompat
1010
*/
1111
@PublishedApi
1212
@Suppress("RemoveExplicitTypeArguments", "UNCHECKED_CAST")
13+
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
1314
internal inline fun <reified T> Iterable<T>.toArray(): Array<T> {
1415
return when (this) {
1516
is List<T> -> toTypedArray<T>()
@@ -19,6 +20,7 @@ internal inline fun <reified T> Iterable<T>.toArray(): Array<T> {
1920
}
2021

2122
@PublishedApi
23+
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
2224
internal inline fun <E> SparseArrayCompat<E>.forEach(body: (Int, E) -> Unit) {
2325
for (index in 0 until size()) {
2426
body(keyAt(index), valueAt(index))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.kirich1409.androidnotificationdsl.internal
22

3+
import androidx.annotation.RestrictTo
4+
35
/**
46
* Maximum length of CharSequences accepted by Builder and friends.
57
*
68
* Avoids spamming the system with overly large strings such as full e-mails.
79
*/
10+
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
811
internal const val MAX_CHARSEQUENCE_LENGTH: Long = 5 * 1024

core/src/main/java/com/kirich1409/androidnotificationdsl/internal/NotificationUtils.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
package com.kirich1409.androidnotificationdsl.internal
44

55
import android.os.Build
6+
import androidx.annotation.RestrictTo
67

78
/**
89
* Check is invisible actions are supported on current device
910
*/
1011
@PublishedApi
12+
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
1113
internal fun isInvisibleActionsSupported(): Boolean {
1214
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
1315
}

0 commit comments

Comments
 (0)