diff --git a/README.md b/README.md index 44b39fb80..f44064c4a 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,11 @@ The following is a list of all third-party dependencies included as part of the - Purpose: Safely manage keys and encrypt files and sharedpreferences. - License: Apache 2.0 +- **AndroidX WorkManager** + - https://developer.android.com/jetpack/androidx/releases/work + - Purpose: The WorkManager is used to schedule deferrable, asynchronous tasks that must be run reliably. + - License: Apache 2.0 + - **Core SplashScreen** - https://developer.android.com/jetpack/androidx/releases/core - Purpose: Backwards compatible SplashScreen API implementation. diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 65f146094..cf278f107 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -173,6 +173,7 @@ dependencies { implementation(libs.androidx.room.runtime) implementation(libs.androidx.security.crypto) implementation(libs.androidx.splashscreen) + implementation(libs.androidx.work.runtime.ktx) implementation(libs.bitwarden.sdk) implementation(libs.bumptech.glide) implementation(libs.google.hilt.android) diff --git a/app/src/main/java/com/x8bit/bitwarden/data/platform/manager/clipboard/BitwardenClipboardManagerImpl.kt b/app/src/main/java/com/x8bit/bitwarden/data/platform/manager/clipboard/BitwardenClipboardManagerImpl.kt index db6b42b6c..2b4488150 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/platform/manager/clipboard/BitwardenClipboardManagerImpl.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/platform/manager/clipboard/BitwardenClipboardManagerImpl.kt @@ -8,10 +8,15 @@ import android.widget.Toast import androidx.compose.ui.text.AnnotatedString import androidx.core.content.getSystemService import androidx.core.os.persistableBundleOf +import androidx.work.ExistingWorkPolicy +import androidx.work.OneTimeWorkRequest +import androidx.work.WorkManager import com.x8bit.bitwarden.R import com.x8bit.bitwarden.data.platform.annotation.OmitFromCoverage +import com.x8bit.bitwarden.data.platform.repository.SettingsRepository import com.x8bit.bitwarden.ui.platform.base.util.Text import com.x8bit.bitwarden.ui.platform.base.util.toAnnotatedString +import java.util.concurrent.TimeUnit /** * Default implementation of the [BitwardenClipboardManager] interface. @@ -19,9 +24,13 @@ import com.x8bit.bitwarden.ui.platform.base.util.toAnnotatedString @OmitFromCoverage class BitwardenClipboardManagerImpl( private val context: Context, + private val settingsRepository: SettingsRepository, ) : BitwardenClipboardManager { private val clipboardManager: ClipboardManager = requireNotNull(context.getSystemService()) + private val clearClipboardFrequencySeconds: Int? + get() = settingsRepository.clearClipboardFrequency.frequencySeconds + override fun setText( text: AnnotatedString, isSensitive: Boolean, @@ -46,6 +55,19 @@ class BitwardenClipboardManagerImpl( ) .show() } + + val frequency = clearClipboardFrequencySeconds ?: return + val clearClipboardRequest: OneTimeWorkRequest = + OneTimeWorkRequest + .Builder(ClearClipboardWorker::class.java) + .setInitialDelay(frequency.toLong(), TimeUnit.SECONDS) + .build() + + WorkManager.getInstance(context).enqueueUniqueWork( + "ClearClipboard", + ExistingWorkPolicy.REPLACE, + clearClipboardRequest, + ) } override fun setText(text: String, isSensitive: Boolean, toastDescriptorOverride: String?) { diff --git a/app/src/main/java/com/x8bit/bitwarden/data/platform/manager/clipboard/ClearClipboardWorker.kt b/app/src/main/java/com/x8bit/bitwarden/data/platform/manager/clipboard/ClearClipboardWorker.kt new file mode 100644 index 000000000..6803f4016 --- /dev/null +++ b/app/src/main/java/com/x8bit/bitwarden/data/platform/manager/clipboard/ClearClipboardWorker.kt @@ -0,0 +1,22 @@ +package com.x8bit.bitwarden.data.platform.manager.clipboard + +import android.content.ClipboardManager +import android.content.Context +import android.content.Context.CLIPBOARD_SERVICE +import androidx.work.Worker +import androidx.work.WorkerParameters + +/** + * A worker to clear the clipboard manager. + */ +class ClearClipboardWorker(appContext: Context, workerParams: WorkerParameters) : + Worker(appContext, workerParams) { + + private val clipboardManager = + appContext.getSystemService(CLIPBOARD_SERVICE) as ClipboardManager + + override fun doWork(): Result { + clipboardManager.clearPrimaryClip() + return Result.success() + } +} diff --git a/app/src/main/java/com/x8bit/bitwarden/data/platform/manager/di/PlatformManagerModule.kt b/app/src/main/java/com/x8bit/bitwarden/data/platform/manager/di/PlatformManagerModule.kt index f5d032efa..50b8a289e 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/platform/manager/di/PlatformManagerModule.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/platform/manager/di/PlatformManagerModule.kt @@ -83,7 +83,11 @@ object PlatformManagerModule { @Singleton fun provideBitwardenClipboardManager( @ApplicationContext context: Context, - ): BitwardenClipboardManager = BitwardenClipboardManagerImpl(context) + settingsRepository: SettingsRepository, + ): BitwardenClipboardManager = BitwardenClipboardManagerImpl( + context, + settingsRepository, + ) @Provides @Singleton diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 406bd2461..8b20c081e 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -26,6 +26,7 @@ androidXSecurityCrypto = "1.1.0-alpha06" androidxSplash = "1.1.0-alpha02" androidXAppCompat = "1.6.1" androdixAutofill = "1.1.0" +androidxWork = "2.9.0" bitwardenSdk = "0.4.0-20240131.132449-88" crashlytics = "2.9.9" detekt = "1.23.1" @@ -82,6 +83,7 @@ androidx-room-ktx = { module = "androidx.room:room-ktx", version.ref = "androidx androidx-room-runtime = { module = "androidx.room:room-runtime", version.ref = "androidxRoom" } androidx-security-crypto = { module = "androidx.security:security-crypto", version.ref = "androidXSecurityCrypto" } androidx-splashscreen = { module = "androidx.core:core-splashscreen", version.ref = "androidxSplash" } +androidx-work-runtime-ktx = { module = "androidx.work:work-runtime-ktx", version.ref = "androidxWork" } bitwarden-sdk = { module = "com.bitwarden:sdk-android", version.ref = "bitwardenSdk" } bumptech-glide = { module = "com.github.bumptech.glide:compose", version.ref = "glide" } detekt-detekt-formatting = { module = "io.gitlab.arturbosch.detekt:detekt-formatting", version.ref = "detekt" }