Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Conversation

@justinmc
Copy link
Contributor

@justinmc justinmc commented Jan 28, 2021

Currently, some Xiaomi devices show a permissions prompt when the clipboard is accessed. This is similar to iOS14, where we added the hasStrings method to not trigger this prompt (see flutter/flutter#60145). This uses native Android's hasText method to do the same sort of workaround in Android's hasStrings.

For flutter/flutter#74139, but probably needs a small framework change too.

@justinmc justinmc force-pushed the xiao-mi-clipboard-permission branch from bee9c60 to 4923268 Compare January 28, 2021 19:06

clipboardManager.clearPrimaryClip();
// TODO(justinmc): This fails too. Also, directly running
// clipboardManager.hasPrimaryClip() here returns true too!
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does anyone know why clipboardHasStrings and hasPrimaryClip are always returning true? Is something being mocked here that I'm missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving to hasText fixed this, so I'm going with that.

@justinmc justinmc marked this pull request as ready for review February 10, 2021 17:43
@hamdikahloun
Copy link
Member

Maybe the permission will be prompt here if the clipboard data is URI :

activity.getContentResolver().openTypedAssetFileDescriptor(item.getUri(), "text/*", null);

private boolean clipboardHasText() {
ClipboardManager clipboard =
(ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
return clipboard.hasText();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried hasPrimaryClip but it completely wasn't working for me in the tests. It seemed to always return true, even if I setPrimaryClip to null or did clearPrimaryClip. You can see how I had it before in 72c32b8. CC @blasten who was helping me with this on Discord. Is that a robolectric problem?

@hamdikahloun
Copy link
Member

@justinmc Can you please clarify how hastext make the permission bypassed.
Hastext function throw security exception if the clip data is URI (image URI for example).

@justinmc
Copy link
Contributor Author

justinmc commented Feb 10, 2021

@hamdikahloun It seemed to stop the Xiaomi permission prompt (see flutter/flutter#74139), but it was hard to verify. Are you sure this won't help if an image has been copied? We didn't try that I guess.

This is just for starting the app, where the framework checks the clipboard to decide if it should display the Paste button. If you actually paste, then the prompt will still show, which is what it should do.

Edit: The general idea is that previously, Android was always calling getPrimaryClip just to check if there was a string on the clipboard to paste, which triggered the prompt. With this PR, it would only call hasText.

@hamdikahloun
Copy link
Member

Can you check this please : #21290 & flutter/flutter#66108 (comment)

@pedromassangocode
Copy link

pedromassangocode commented Feb 11, 2021

Hastext function throw security exception if the clip data is URI (image URI for example).

@justinmc there is a hint here to check whether the clipboard data is a text or not:
https://developer.android.com/guide/topics/text/copy-paste#PastePlainText

It seems that the current implementation is not working flutter/flutter#74139 (comment). Can we build an release apk with hasPrimaryClip() just to verify its behavior? From my understanding it is not working just in tests.

@chinmaygarde
Copy link
Member

@justinmc Any updates? If this issue is stalled, can we mark it WIP or close it till we can resume work it?

@chinmaygarde
Copy link
Member

Closing it as stale. Feel free to re-open when progress can be made.

@justinmc
Copy link
Contributor Author

justinmc commented Apr 2, 2021

Sorry I missed the notifications from this PR. It was the right decision to close for now. The problems are:

  1. This PR doesn't seem to prevent the clipboard access dialog on XiaoMi.
  2. Really we should be using hasPrimaryClip, but it doesn't seem to work with our engine tests.

I'm having trouble building an example with this using hasPrimaryClip due to engine compilation failing. Hopefully it's a temporary problem with the engine or my setup.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants