2

Has anyone else tried to mark their clipboard copied data as sensitive as per the following recommendation?

https://developer.android.com/about/versions/13/features/copy-paste

clipData.apply {
    description.extras = PersistableBundle().apply {
        putBoolean(ClipDescription.EXTRA_IS_SENSITIVE, true)
    }
}

When I tried to do so, I don't find a clipData.apply method.

enter image description here

How can I set the sensitivity settings in an android app Java code?

Shahid Thaika
  • 2,133
  • 5
  • 23
  • 59

1 Answers1

1

apply() is a Kotlin scope function. You appear to be programming in Java, so the Kotlin syntax will not work for you.

By eyeball, the Java equivalent would be:

PersistableBundle extras = new PersistableBundle();

extras.putBoolean(ClipDescription.EXTRA_IS_SENSITIVE, true);

clipData.getDescription().setExtras(extras);
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • It is setExtras instead of putExtras, but otherwise the code seems correct. I now need to figure out an alternative as my minimum SDK is currently API 21 and setExtras requires minimum to be API 24 – Shahid Thaika Jun 22 '22 at 16:57
  • @ShahidThaika: Sorry, I fixed that typo. Since `EXTRA_IS_SENSITIVE` is new to API Level 33, you would just use an `if` block to only go through the above code on newer devices, using `Build.VERSION.SDK_INT`. – CommonsWare Jun 22 '22 at 17:13
  • The email I received from Google Play said, "Targeting the new API level is not required.". Doesn't this mean the code should work on older API versions as well? Or should I wrap it in an IF and use the above line for API >= 33 and for lower use the other: putBoolean("android.content.extra.IS_SENSITIVE", true) – Shahid Thaika Jun 22 '22 at 17:29
  • @CommonsWare — the [Add sensitive content to the clipboard](https://developer.android.com/develop/ui/views/touch-and-input/copy-paste#SensitiveContent) section indicates, "If your app targets a lower API level (than 33)" and then does this: `putBoolean("android.content.extra.IS_SENSITIVE", true)` -- perhaps that's what @ShahidThaika was referring to. – Ken Oct 18 '22 at 14:52
  • For me, setting EXTRA_IS_SENSITIVE only hides the preview but when you then click the share button, the sensitive text appears in the share window and in the soft keyboard of an app where you paste the text. Do you have the same behavior? – Oleh Romanenko Jun 04 '23 at 18:37
  • @OlehRomanenko: That behavior may vary by device model (and, in the case of the keyboard, by IME implementation). Personally, I have not used this extra; I was merely helping the OP with the syntax. – CommonsWare Jun 04 '23 at 18:39