@@ -12,6 +12,8 @@ import android.content.Context
1212import android.content.Intent
1313import android.graphics.drawable.Icon
1414import android.os.Build
15+ import java.util.concurrent.TimeUnit.SECONDS
16+ import com.d4rk.musicsleeptimer.plus.BuildConfig
1517import com.d4rk.musicsleeptimer.plus.R
1618import com.d4rk.musicsleeptimer.plus.notifications.SleepNotification.Action.CANCEL
1719import com.d4rk.musicsleeptimer.plus.notifications.SleepNotification.Action.DECREMENT
@@ -28,92 +30,98 @@ import java.util.concurrent.TimeUnit.MILLISECONDS
2830import java.util.concurrent.TimeUnit.MINUTES
2931
3032object SleepNotification {
31- private val TIMEOUT_INITIAL_MILLIS : Long = MINUTES .toMillis(30 )
32- private val TIMEOUT_INCREMENT_MILLIS : Long = MINUTES .toMillis(10 )
33- private val TIMEOUT_DECREMENT_MILLIS : Long = MINUTES .toMillis(10 )
33+ private val TIMEOUT_INITIAL_MILLIS : Long =
34+ if (BuildConfig .DEBUG ) SECONDS .toMillis(10 ) else MINUTES .toMillis(30 )
35+ private val TIMEOUT_INCREMENT_MILLIS : Long = MINUTES .toMillis(10 )
36+ private val TIMEOUT_DECREMENT_MILLIS : Long = MINUTES .toMillis(10 )
3437
35- private enum class Action (private val value : String ) {
38+ private enum class Action (private val value : String ) {
3639 CANCEL (" com.d4rk.musicsleeptimer.plus.notifications.SleepNotification.Action.CANCEL" ) {
37- override fun title (context : Context ) = context.getText(android.R .string.cancel)
38- } ,
40+ override fun title (context : Context ) = context.getText(android.R .string.cancel)
41+ },
3942 INCREMENT (" com.d4rk.musicsleeptimer.plus.notifications.SleepNotification.Action.INCREMENT" ) {
40- override fun title (context : Context ) = context.getString(
41- R .string.notification_action_increment , MILLISECONDS .toMinutes(TIMEOUT_INCREMENT_MILLIS )
43+ override fun title (context : Context ) = context.getString(
44+ R .string.notification_action_increment,
45+ MILLISECONDS .toMinutes(TIMEOUT_INCREMENT_MILLIS )
4246 )
43- } ,
47+ },
4448 DECREMENT (" com.d4rk.musicsleeptimer.plus.notifications.SleepNotification.Action.DECREMENT" ) {
45- override fun title (context : Context ) = context.getString(
46- R .string.notification_action_decrement , MILLISECONDS .toMinutes(TIMEOUT_DECREMENT_MILLIS )
49+ override fun title (context : Context ) = context.getString(
50+ R .string.notification_action_decrement,
51+ MILLISECONDS .toMinutes(TIMEOUT_DECREMENT_MILLIS )
4752 )
48- } , ;
53+ }, ;
4954
5055 companion object {
51- fun parse (value : String? ) : Action ? = entries.firstOrNull { it.value == value }
56+ fun parse (value : String? ): Action ? = entries.firstOrNull { it.value == value }
5257 }
5358
54- fun intent (context : Context ) : Intent = if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .N ) {
59+ fun intent (context : Context ): Intent = if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .N ) {
5560 serviceIntent(context)
5661 } else {
5762 broadcastIntent(context)
5863 }
5964
6065 @RequiresApi(Build .VERSION_CODES .N )
61- private fun serviceIntent (context : Context ) : Intent =
62- Intent (context , SleepTileService ::class .java).setAction(value)
66+ private fun serviceIntent (context : Context ): Intent =
67+ Intent (context, SleepTileService ::class .java).setAction(value)
6368
64- private fun broadcastIntent (context : Context ) : Intent =
65- Intent (context , SleepNotificationReceiver ::class .java).setAction(value)
69+ private fun broadcastIntent (context : Context ): Intent =
70+ Intent (context, SleepNotificationReceiver ::class .java).setAction(value)
6671
67- fun pendingIntent (context : Context , cancel : Boolean = false) : PendingIntent ? {
72+ fun pendingIntent (context : Context , cancel : Boolean = false): PendingIntent ? {
6873 val requestCode = value.hashCode()
6974 val intent = intent(context)
7075 val pendingIntent = if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .N ) {
71- PendingIntent .getService(context , requestCode , intent , FLAG_IMMUTABLE )
76+ PendingIntent .getService(context, requestCode, intent, FLAG_IMMUTABLE )
7277 } else {
73- PendingIntent .getBroadcast(context , requestCode , intent , FLAG_IMMUTABLE )
78+ PendingIntent .getBroadcast(context, requestCode, intent, FLAG_IMMUTABLE )
7479 }
7580 return pendingIntent.apply { if (cancel) cancel() }
7681 }
7782
78- fun action (context : Context , cancel : Boolean = false) : Notification .Action .Builder = Notification .Action .Builder (
79- Icon .createWithResource(context , 0 ) , title(context) , pendingIntent(context , cancel)
80- )
83+ fun action (context : Context , cancel : Boolean = false): Notification .Action .Builder =
84+ Notification .Action .Builder (
85+ Icon .createWithResource(context, 0 ), title(context), pendingIntent(context, cancel)
86+ )
8187
82- abstract fun title (context : Context ) : CharSequence?
88+ abstract fun title (context : Context ): CharSequence?
8389 }
8490
85- fun Context.notificationManager () : NotificationManager ? = getSystemService(NotificationManager ::class .java)
91+ fun Context.notificationManager (): NotificationManager ? =
92+ getSystemService(NotificationManager ::class .java)
8693
87- fun Context.find () = notificationManager()?.activeNotifications?.firstOrNull { it.id == R .id.notification_id }?.notification
94+ fun Context.find () =
95+ notificationManager()?.activeNotifications?.firstOrNull { it.id == R .id.notification_id }?.notification
8896
89- fun Context.handle (intent : Intent ? ) = when (Action .parse(intent?.action)) {
97+ fun Context.handle (intent : Intent ? ) = when (Action .parse(intent?.action)) {
9098 INCREMENT -> update(TIMEOUT_INCREMENT_MILLIS )
91- DECREMENT -> update(- TIMEOUT_DECREMENT_MILLIS )
99+ DECREMENT -> update(- TIMEOUT_DECREMENT_MILLIS )
92100 CANCEL -> cancel()
93101 null -> Unit
94102 }
95103
96104 fun Context.toggle () = if (find() == null ) show() else cancel()
97105 private fun Context.cancel () = notificationManager()?.cancel(R .id.notification_id) ? : Unit
98- private fun Context.update (timeout : Long ) = find()?.let {
106+ private fun Context.update (timeout : Long ) = find()?.let {
99107 it.`when ` - currentTimeMillis()
100108 }?.let {
101- if (it > - timeout) it + timeout else it
109+ if (it > - timeout) it + timeout else it
102110 }?.let {
103111 show(it)
104112 }
105113
106- private fun Context.show (timeout : Long = TIMEOUT_INITIAL_MILLIS ) {
114+ private fun Context.show (timeout : Long = TIMEOUT_INITIAL_MILLIS ) {
107115 require(timeout > 0 )
108- val eta : Long = currentTimeMillis() + timeout
109- val builder : Notification .Builder = if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
116+ val eta: Long = currentTimeMillis() + timeout
117+ val builder: Notification .Builder = if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
110118 Notification .Builder (this , getString(R .string.notification_channel_id))
111119 } else {
112120 @Suppress(" DEPRECATION" )
113121 Notification .Builder (this )
114122 }
115123
116- val notification : Notification = builder
124+ val notification: Notification = builder
117125 .setCategory(CATEGORY_EVENT )
118126 .setVisibility(VISIBILITY_PUBLIC )
119127 .setOnlyAlertOnce(true )
@@ -150,9 +158,9 @@ object SleepNotification {
150158
151159 private fun Context.createNotificationChannel () {
152160 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
153- val id : String = getString(R .string.notification_channel_id)
154- val name : CharSequence = getString(R .string.app_name)
155- val channel : NotificationChannel = NotificationChannel (id, name, IMPORTANCE_LOW ).apply {
161+ val id: String = getString(R .string.notification_channel_id)
162+ val name: CharSequence = getString(R .string.app_name)
163+ val channel: NotificationChannel = NotificationChannel (id, name, IMPORTANCE_LOW ).apply {
156164 setBypassDnd(true )
157165 lockscreenVisibility = VISIBILITY_PUBLIC
158166 }
0 commit comments