-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
- Loading branch information
1 parent
1013df7
commit 47ea22d
Showing
2 changed files
with
104 additions
and
20 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
app/src/main/java/com/nextcloud/client/jobs/clipboard/ClipboardClearWorker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Nextcloud - Android Client | ||
* | ||
* SPDX-FileCopyrightText: 2024 Alper Ozturk <alper.ozturk@nextcloud.com> | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
package com.nextcloud.client.jobs.clipboard | ||
|
||
import android.content.ClipData | ||
import android.content.ClipboardManager | ||
import android.content.Context | ||
import android.os.Build | ||
import androidx.work.Worker | ||
import androidx.work.WorkerParameters | ||
import com.owncloud.android.lib.common.utils.Log_OC | ||
|
||
class ClipboardClearWorker( | ||
private val context: Context, | ||
params: WorkerParameters | ||
) : Worker(context, params) { | ||
private val tag = ClipboardClearWorker::class.java.name | ||
|
||
companion object { | ||
const val CLIPBOARD_TEXT = "clipboard_text" | ||
} | ||
|
||
@Suppress("TooGenericExceptionCaught", "ReturnCount") | ||
override fun doWork(): Result { | ||
try { | ||
val clipboardManager = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager | ||
val currentClip = clipboardManager.primaryClip ?: return Result.success() | ||
val clipboardText = currentClip.getItemAt(0).text?.toString() ?: return Result.success() | ||
val copiedText = inputData.getString(CLIPBOARD_TEXT) | ||
if (copiedText != clipboardText) { | ||
return Result.success() | ||
} | ||
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { | ||
clipboardManager.clearPrimaryClip() | ||
} else { | ||
val newEmptyClip = ClipData.newPlainText("EmptyClipContent", "") | ||
clipboardManager.setPrimaryClip(newEmptyClip) | ||
} | ||
|
||
return Result.success() | ||
} catch (e: Exception) { | ||
Log_OC.e(tag, "Error in clipboard clear worker", e) | ||
return Result.retry() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters