44 */
55package com.zeapo.pwdstore
66
7+ import android.app.Notification
78import android.app.NotificationChannel
89import android.app.NotificationManager
910import android.app.PendingIntent
@@ -12,12 +13,12 @@ import android.content.ClipData
1213import android.content.Intent
1314import android.os.Build
1415import android.os.IBinder
16+ import androidx.annotation.RequiresApi
1517import androidx.core.app.NotificationCompat
1618import androidx.core.content.getSystemService
1719import com.github.ajalt.timberkt.d
1820import com.zeapo.pwdstore.utils.PreferenceKeys
1921import com.zeapo.pwdstore.utils.clipboard
20- import com.zeapo.pwdstore.utils.getString
2122import com.zeapo.pwdstore.utils.sharedPrefs
2223import kotlinx.coroutines.CoroutineScope
2324import kotlinx.coroutines.Dispatchers
@@ -43,13 +44,13 @@ class ClipboardService : Service() {
4344 }
4445
4546 ACTION_START -> {
46- val time = sharedPrefs.getString( PreferenceKeys . GENERAL_SHOW_TIME )?.toIntOrNull() ? : 45
47+ val time = intent.getIntExtra( EXTRA_NOTIFICATION_TIME , 45 )
4748
4849 if (time == 0 ) {
4950 stopSelf()
5051 }
5152
52- createNotification()
53+ createNotification(time )
5354 scope.launch {
5455 withContext(Dispatchers .IO ) {
5556 startTimer(time)
@@ -109,26 +110,50 @@ class ClipboardService : Service() {
109110 }
110111 }
111112
112- private fun createNotification () {
113- createNotificationChannel()
114- val clearIntent = Intent (this , ClipboardService ::class .java)
115- clearIntent.action = ACTION_CLEAR
113+ private fun createNotification (clearTime : Int ) {
114+ val clearTimeMs = clearTime * 1000L
115+ val clearIntent = Intent (this , ClipboardService ::class .java).apply {
116+ action = ACTION_CLEAR
117+ }
116118 val pendingIntent = if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
117119 PendingIntent .getForegroundService(this , 0 , clearIntent, PendingIntent .FLAG_UPDATE_CURRENT )
118120 } else {
119121 PendingIntent .getService(this , 0 , clearIntent, PendingIntent .FLAG_UPDATE_CURRENT )
120122 }
123+ val notification = if (Build .VERSION .SDK_INT <= Build .VERSION_CODES .M ) {
124+ createNotificationApi23(pendingIntent)
125+ } else {
126+ createNotificationApi24(pendingIntent, clearTimeMs)
127+ }
128+
129+ createNotificationChannel()
130+ startForeground(1 , notification)
131+ }
121132
122- val notification = NotificationCompat .Builder (this , CHANNEL_ID )
133+ private fun createNotificationApi23 (pendingIntent : PendingIntent ): Notification {
134+ return NotificationCompat .Builder (this , CHANNEL_ID )
123135 .setContentTitle(getString(R .string.app_name))
124136 .setContentText(getString(R .string.tap_clear_clipboard))
125137 .setSmallIcon(R .drawable.ic_action_secure_24dp)
126138 .setContentIntent(pendingIntent)
127139 .setUsesChronometer(true )
128140 .setPriority(NotificationCompat .PRIORITY_LOW )
129141 .build()
142+ }
130143
131- startForeground(1 , notification)
144+ @RequiresApi(Build .VERSION_CODES .N )
145+ private fun createNotificationApi24 (pendingIntent : PendingIntent , clearTimeMs : Long ): Notification {
146+ return NotificationCompat .Builder (this , CHANNEL_ID )
147+ .setContentTitle(getString(R .string.app_name))
148+ .setContentText(getString(R .string.tap_clear_clipboard))
149+ .setSmallIcon(R .drawable.ic_action_secure_24dp)
150+ .setContentIntent(pendingIntent)
151+ .setUsesChronometer(true )
152+ .setChronometerCountDown(true )
153+ .setShowWhen(true )
154+ .setWhen(System .currentTimeMillis() + clearTimeMs)
155+ .setPriority(NotificationCompat .PRIORITY_LOW )
156+ .build()
132157 }
133158
134159 private fun createNotificationChannel () {
@@ -149,8 +174,9 @@ class ClipboardService : Service() {
149174
150175 companion object {
151176
152- private const val ACTION_CLEAR = " ACTION_CLEAR_CLIPBOARD"
153177 const val ACTION_START = " ACTION_START_CLIPBOARD_TIMER"
178+ const val EXTRA_NOTIFICATION_TIME = " EXTRA_NOTIFICATION_TIME"
179+ private const val ACTION_CLEAR = " ACTION_CLEAR_CLIPBOARD"
154180 private const val CHANNEL_ID = " NotificationService"
155181 }
156182}
0 commit comments