Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit 993b203

Browse files
authored
Use countdown chronometer on API 24 and above (#1228)
* Use countdown chronometer on API 24 and above Signed-off-by: Aditya Wasan <[email protected]> * Do not use SharedPreferences inside ClipboardService ClipboardService run in it's own process and SharedPreferences do not support multiple processes. Due to this changes in notificaion clear time are not reflected to the ClipboardService. This commit fixes that by passing time explicitly from the main app process. Signed-off-by: Aditya Wasan <[email protected]>
1 parent 31a11a1 commit 993b203

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

app/src/main/java/com/zeapo/pwdstore/ClipboardService.kt

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
package com.zeapo.pwdstore
66

7+
import android.app.Notification
78
import android.app.NotificationChannel
89
import android.app.NotificationManager
910
import android.app.PendingIntent
@@ -12,12 +13,12 @@ import android.content.ClipData
1213
import android.content.Intent
1314
import android.os.Build
1415
import android.os.IBinder
16+
import androidx.annotation.RequiresApi
1517
import androidx.core.app.NotificationCompat
1618
import androidx.core.content.getSystemService
1719
import com.github.ajalt.timberkt.d
1820
import com.zeapo.pwdstore.utils.PreferenceKeys
1921
import com.zeapo.pwdstore.utils.clipboard
20-
import com.zeapo.pwdstore.utils.getString
2122
import com.zeapo.pwdstore.utils.sharedPrefs
2223
import kotlinx.coroutines.CoroutineScope
2324
import 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
}

app/src/main/java/com/zeapo/pwdstore/crypto/BasePgpActivity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ open class BasePgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBou
254254
if (clearAfter != 0) {
255255
val service = Intent(this, ClipboardService::class.java).apply {
256256
action = ClipboardService.ACTION_START
257+
putExtra(ClipboardService.EXTRA_NOTIFICATION_TIME, clearAfter)
257258
}
258259
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
259260
startForegroundService(service)

0 commit comments

Comments
 (0)