|
| 1 | +package com.capacitorjs.plugins.clipboard; |
| 2 | + |
| 3 | +import android.content.ClipData; |
| 4 | +import android.content.ClipDescription; |
| 5 | +import android.content.ClipboardManager; |
| 6 | +import android.content.Context; |
| 7 | +import android.graphics.Bitmap; |
| 8 | +import android.net.Uri; |
| 9 | +import android.provider.MediaStore; |
| 10 | +import android.util.Base64; |
| 11 | +import com.getcapacitor.Logger; |
| 12 | +import java.io.ByteArrayOutputStream; |
| 13 | +import java.io.InputStream; |
| 14 | + |
| 15 | +public class Clipboard { |
| 16 | + |
| 17 | + private static final String TAG = "Clipboard"; |
| 18 | + |
| 19 | + private Context context; |
| 20 | + private ClipboardManager clipboard; |
| 21 | + |
| 22 | + public Clipboard(Context context) { |
| 23 | + this.context = context; |
| 24 | + this.clipboard = |
| 25 | + (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Writes provided content to the clipboard. |
| 30 | + * |
| 31 | + * @param label User-visible label for the clip data. |
| 32 | + * @param content The content to be written to the clipboard. |
| 33 | + * @return A response indicating the success status of the write request. |
| 34 | + */ |
| 35 | + public ClipboardWriteResponse write(String label, String content) { |
| 36 | + ClipData data = ClipData.newPlainText(label, content); |
| 37 | + |
| 38 | + if (data != null && clipboard != null) { |
| 39 | + try { |
| 40 | + clipboard.setPrimaryClip(data); |
| 41 | + } catch (Exception e) { |
| 42 | + Logger.error(TAG, e); |
| 43 | + return new ClipboardWriteResponse(false, |
| 44 | + "Writing to the clipboard failed"); |
| 45 | + } |
| 46 | + |
| 47 | + return new ClipboardWriteResponse(true); |
| 48 | + } else if (clipboard == null) { |
| 49 | + return new ClipboardWriteResponse( |
| 50 | + false, "Problem getting a reference to the system clipboard"); |
| 51 | + } else { |
| 52 | + return new ClipboardWriteResponse(false, "Problem formatting data"); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Reads data from the clipboard. |
| 58 | + * @return Data from the clipboard or null if no reference to the system |
| 59 | + * clipboard. |
| 60 | + */ |
| 61 | + public ClipboardData read() { |
| 62 | + if (clipboard != null) { |
| 63 | + CharSequence value = null; |
| 64 | + String type = "text/plain"; |
| 65 | + |
| 66 | + if (clipboard.hasPrimaryClip()) { |
| 67 | + ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0); |
| 68 | + |
| 69 | + if (clipboard.getPrimaryClipDescription().hasMimeType( |
| 70 | + ClipDescription.MIMETYPE_TEXT_PLAIN)) { |
| 71 | + Logger.debug(TAG, "Got plain text"); |
| 72 | + value = item.getText(); |
| 73 | + } else if (clipboard.getPrimaryClipDescription().hasMimeType( |
| 74 | + ClipDescription.MIMETYPE_TEXT_HTML)) { |
| 75 | + Logger.debug(TAG, "Got HTML text"); |
| 76 | + value = item.coerceToText(context).toString(); |
| 77 | + type = "text/html"; |
| 78 | + } else if (clipboard.getPrimaryClipDescription().hasMimeType( |
| 79 | + "image/*")) { |
| 80 | + Logger.debug(TAG, "Got image"); |
| 81 | + Uri imageUri = item.getUri(); |
| 82 | + |
| 83 | + try { |
| 84 | + Bitmap bitmap = MediaStore.Images.Media.getBitmap( |
| 85 | + context.getContentResolver(), imageUri); |
| 86 | + if (bitmap != null) { |
| 87 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 88 | + bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream); |
| 89 | + byte[] imageBytes = outputStream.toByteArray(); |
| 90 | + String base64Image = |
| 91 | + Base64.encodeToString(imageBytes, Base64.NO_WRAP); |
| 92 | + value = "data:image/png;base64," + base64Image; |
| 93 | + type = "image/png"; |
| 94 | + } |
| 95 | + } catch (Exception e) { |
| 96 | + Logger.error(TAG, "Failed to read image from clipboard", e); |
| 97 | + } |
| 98 | + } else { |
| 99 | + Logger.debug(TAG, "Unknown MIME type, trying to coerce to text"); |
| 100 | + value = item.coerceToText(context).toString(); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + ClipboardData clipboardData = new ClipboardData(); |
| 105 | + if (value != null) { |
| 106 | + clipboardData.setValue(value.toString()); |
| 107 | + } |
| 108 | + clipboardData.setType(type); |
| 109 | + return clipboardData; |
| 110 | + } |
| 111 | + |
| 112 | + return null; |
| 113 | + } |
| 114 | +} |
0 commit comments