Android: fix Clipboard deprecated methods

This commit is contained in:
Sylvain Becker 2020-10-24 15:36:05 +02:00
parent 5682b066e9
commit 967041681b

View file

@ -2278,13 +2278,12 @@ class SDLInputConnection extends BaseInputConnection {
interface SDLClipboardHandler { interface SDLClipboardHandler {
public boolean clipboardHasText(); boolean clipboardHasText();
public String clipboardGetText(); String clipboardGetText();
public void clipboardSetText(String string); void clipboardSetText(String string);
} }
class SDLClipboardHandler_API11 implements class SDLClipboardHandler_API11 implements
SDLClipboardHandler, SDLClipboardHandler,
android.content.ClipboardManager.OnPrimaryClipChangedListener { android.content.ClipboardManager.OnPrimaryClipChangedListener {
@ -2298,23 +2297,29 @@ class SDLClipboardHandler_API11 implements
@Override @Override
public boolean clipboardHasText() { public boolean clipboardHasText() {
return mClipMgr.hasText(); return mClipMgr.hasPrimaryClip();
} }
@Override @Override
public String clipboardGetText() { public String clipboardGetText() {
CharSequence text; ClipData clip = mClipMgr.getPrimaryClip();
text = mClipMgr.getText(); if (clip != null) {
ClipData.Item item = clip.getItemAt(0);
if (item != null) {
CharSequence text = item.getText();
if (text != null) { if (text != null) {
return text.toString(); return text.toString();
} }
}
}
return null; return null;
} }
@Override @Override
public void clipboardSetText(String string) { public void clipboardSetText(String string) {
mClipMgr.removePrimaryClipChangedListener(this); mClipMgr.removePrimaryClipChangedListener(this);
mClipMgr.setText(string); ClipData clip = ClipData.newPlainText(null, string);
mClipMgr.setPrimaryClip(clip);
mClipMgr.addPrimaryClipChangedListener(this); mClipMgr.addPrimaryClipChangedListener(this);
} }
@ -2322,6 +2327,5 @@ class SDLClipboardHandler_API11 implements
public void onPrimaryClipChanged() { public void onPrimaryClipChanged() {
SDLActivity.onNativeClipboardChanged(); SDLActivity.onNativeClipboardChanged();
} }
} }