diff options
author | Aditya Wasan <adityawasan55@gmail.com> | 2020-12-02 09:11:42 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-02 09:11:42 +0530 |
commit | 993b2036765dfcabf7fb5cf8679a6f1479cab7f3 (patch) | |
tree | 29c56d761d4c9320f4551a50f2d425ec1c732ee2 /app | |
parent | 31a11a1ebb54ec88d13004c6b4669d7501724909 (diff) |
Use countdown chronometer on API 24 and above (#1228)
* Use countdown chronometer on API 24 and above
Signed-off-by: Aditya Wasan <adityawasan55@gmail.com>
* 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 <adityawasan55@gmail.com>
Diffstat (limited to 'app')
-rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/ClipboardService.kt | 46 | ||||
-rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/crypto/BasePgpActivity.kt | 1 |
2 files changed, 37 insertions, 10 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/ClipboardService.kt b/app/src/main/java/com/zeapo/pwdstore/ClipboardService.kt index 0f27e5ef..f50ac7ff 100644 --- a/app/src/main/java/com/zeapo/pwdstore/ClipboardService.kt +++ b/app/src/main/java/com/zeapo/pwdstore/ClipboardService.kt @@ -4,6 +4,7 @@ */ package com.zeapo.pwdstore +import android.app.Notification import android.app.NotificationChannel import android.app.NotificationManager import android.app.PendingIntent @@ -12,12 +13,12 @@ import android.content.ClipData import android.content.Intent import android.os.Build import android.os.IBinder +import androidx.annotation.RequiresApi import androidx.core.app.NotificationCompat import androidx.core.content.getSystemService import com.github.ajalt.timberkt.d import com.zeapo.pwdstore.utils.PreferenceKeys import com.zeapo.pwdstore.utils.clipboard -import com.zeapo.pwdstore.utils.getString import com.zeapo.pwdstore.utils.sharedPrefs import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -43,13 +44,13 @@ class ClipboardService : Service() { } ACTION_START -> { - val time = sharedPrefs.getString(PreferenceKeys.GENERAL_SHOW_TIME)?.toIntOrNull() ?: 45 + val time = intent.getIntExtra(EXTRA_NOTIFICATION_TIME, 45) if (time == 0) { stopSelf() } - createNotification() + createNotification(time) scope.launch { withContext(Dispatchers.IO) { startTimer(time) @@ -109,17 +110,28 @@ class ClipboardService : Service() { } } - private fun createNotification() { - createNotificationChannel() - val clearIntent = Intent(this, ClipboardService::class.java) - clearIntent.action = ACTION_CLEAR + private fun createNotification(clearTime: Int) { + val clearTimeMs = clearTime * 1000L + val clearIntent = Intent(this, ClipboardService::class.java).apply { + action = ACTION_CLEAR + } val pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { PendingIntent.getForegroundService(this, 0, clearIntent, PendingIntent.FLAG_UPDATE_CURRENT) } else { PendingIntent.getService(this, 0, clearIntent, PendingIntent.FLAG_UPDATE_CURRENT) } + val notification = if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) { + createNotificationApi23(pendingIntent) + } else { + createNotificationApi24(pendingIntent, clearTimeMs) + } + + createNotificationChannel() + startForeground(1, notification) + } - val notification = NotificationCompat.Builder(this, CHANNEL_ID) + private fun createNotificationApi23(pendingIntent: PendingIntent): Notification { + return NotificationCompat.Builder(this, CHANNEL_ID) .setContentTitle(getString(R.string.app_name)) .setContentText(getString(R.string.tap_clear_clipboard)) .setSmallIcon(R.drawable.ic_action_secure_24dp) @@ -127,8 +139,21 @@ class ClipboardService : Service() { .setUsesChronometer(true) .setPriority(NotificationCompat.PRIORITY_LOW) .build() + } - startForeground(1, notification) + @RequiresApi(Build.VERSION_CODES.N) + private fun createNotificationApi24(pendingIntent: PendingIntent, clearTimeMs: Long): Notification { + return NotificationCompat.Builder(this, CHANNEL_ID) + .setContentTitle(getString(R.string.app_name)) + .setContentText(getString(R.string.tap_clear_clipboard)) + .setSmallIcon(R.drawable.ic_action_secure_24dp) + .setContentIntent(pendingIntent) + .setUsesChronometer(true) + .setChronometerCountDown(true) + .setShowWhen(true) + .setWhen(System.currentTimeMillis() + clearTimeMs) + .setPriority(NotificationCompat.PRIORITY_LOW) + .build() } private fun createNotificationChannel() { @@ -149,8 +174,9 @@ class ClipboardService : Service() { companion object { - private const val ACTION_CLEAR = "ACTION_CLEAR_CLIPBOARD" const val ACTION_START = "ACTION_START_CLIPBOARD_TIMER" + const val EXTRA_NOTIFICATION_TIME = "EXTRA_NOTIFICATION_TIME" + private const val ACTION_CLEAR = "ACTION_CLEAR_CLIPBOARD" private const val CHANNEL_ID = "NotificationService" } } diff --git a/app/src/main/java/com/zeapo/pwdstore/crypto/BasePgpActivity.kt b/app/src/main/java/com/zeapo/pwdstore/crypto/BasePgpActivity.kt index 17c18998..412a54f2 100644 --- a/app/src/main/java/com/zeapo/pwdstore/crypto/BasePgpActivity.kt +++ b/app/src/main/java/com/zeapo/pwdstore/crypto/BasePgpActivity.kt @@ -254,6 +254,7 @@ open class BasePgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBou if (clearAfter != 0) { val service = Intent(this, ClipboardService::class.java).apply { action = ClipboardService.ACTION_START + putExtra(ClipboardService.EXTRA_NOTIFICATION_TIME, clearAfter) } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startForegroundService(service) |