4
4
*/
5
5
package com.zeapo.pwdstore
6
6
7
+ import android.app.Notification
7
8
import android.app.NotificationChannel
8
9
import android.app.NotificationManager
9
10
import android.app.PendingIntent
@@ -12,12 +13,12 @@ import android.content.ClipData
12
13
import android.content.Intent
13
14
import android.os.Build
14
15
import android.os.IBinder
16
+ import androidx.annotation.RequiresApi
15
17
import androidx.core.app.NotificationCompat
16
18
import androidx.core.content.getSystemService
17
19
import com.github.ajalt.timberkt.d
18
20
import com.zeapo.pwdstore.utils.PreferenceKeys
19
21
import com.zeapo.pwdstore.utils.clipboard
20
- import com.zeapo.pwdstore.utils.getString
21
22
import com.zeapo.pwdstore.utils.sharedPrefs
22
23
import kotlinx.coroutines.CoroutineScope
23
24
import kotlinx.coroutines.Dispatchers
@@ -43,13 +44,13 @@ class ClipboardService : Service() {
43
44
}
44
45
45
46
ACTION_START -> {
46
- val time = sharedPrefs.getString( PreferenceKeys . GENERAL_SHOW_TIME )?.toIntOrNull() ? : 45
47
+ val time = intent.getIntExtra( EXTRA_NOTIFICATION_TIME , 45 )
47
48
48
49
if (time == 0 ) {
49
50
stopSelf()
50
51
}
51
52
52
- createNotification()
53
+ createNotification(time )
53
54
scope.launch {
54
55
withContext(Dispatchers .IO ) {
55
56
startTimer(time)
@@ -109,26 +110,50 @@ class ClipboardService : Service() {
109
110
}
110
111
}
111
112
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
+ }
116
118
val pendingIntent = if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
117
119
PendingIntent .getForegroundService(this , 0 , clearIntent, PendingIntent .FLAG_UPDATE_CURRENT )
118
120
} else {
119
121
PendingIntent .getService(this , 0 , clearIntent, PendingIntent .FLAG_UPDATE_CURRENT )
120
122
}
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
+ }
121
132
122
- val notification = NotificationCompat .Builder (this , CHANNEL_ID )
133
+ private fun createNotificationApi23 (pendingIntent : PendingIntent ): Notification {
134
+ return NotificationCompat .Builder (this , CHANNEL_ID )
123
135
.setContentTitle(getString(R .string.app_name))
124
136
.setContentText(getString(R .string.tap_clear_clipboard))
125
137
.setSmallIcon(R .drawable.ic_action_secure_24dp)
126
138
.setContentIntent(pendingIntent)
127
139
.setUsesChronometer(true )
128
140
.setPriority(NotificationCompat .PRIORITY_LOW )
129
141
.build()
142
+ }
130
143
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()
132
157
}
133
158
134
159
private fun createNotificationChannel () {
@@ -149,8 +174,9 @@ class ClipboardService : Service() {
149
174
150
175
companion object {
151
176
152
- private const val ACTION_CLEAR = " ACTION_CLEAR_CLIPBOARD"
153
177
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"
154
180
private const val CHANNEL_ID = " NotificationService"
155
181
}
156
182
}
0 commit comments