|
12 | 12 | import android.os.Environment; |
13 | 13 |
|
14 | 14 | import androidx.appcompat.app.AppCompatActivity; |
15 | | -import androidx.core.content.ContextCompat; |
16 | 15 |
|
17 | 16 | import com.termux.shared.R; |
18 | 17 | import com.termux.shared.data.DataUtils; |
@@ -81,27 +80,82 @@ public static void shareText(final Context context, final String subject, final |
81 | 80 | openSystemAppChooser(context, shareTextIntent, DataUtils.isNullOrEmpty(title) ? context.getString(R.string.title_share_with) : title); |
82 | 81 | } |
83 | 82 |
|
| 83 | + |
| 84 | + |
| 85 | + /** Wrapper for {@link #copyTextToClipboard(Context, String, String, String)} with `null` `clipDataLabel` and `toastString`. */ |
| 86 | + public static void copyTextToClipboard(Context context, final String text) { |
| 87 | + copyTextToClipboard(context, null, text, null); |
| 88 | + } |
| 89 | + |
| 90 | + /** Wrapper for {@link #copyTextToClipboard(Context, String, String, String)} with `null` `clipDataLabel`. */ |
| 91 | + public static void copyTextToClipboard(Context context, final String text, final String toastString) { |
| 92 | + copyTextToClipboard(context, null, text, toastString); |
| 93 | + } |
| 94 | + |
84 | 95 | /** |
85 | | - * Copy the text to clipboard. |
| 96 | + * Copy the text to primary clip of the clipboard. |
86 | 97 | * |
87 | 98 | * @param context The context for operations. |
| 99 | + * @param clipDataLabel The label to show to the user describing the copied text. |
88 | 100 | * @param text The text to copy. |
89 | 101 | * @param toastString If this is not {@code null} or empty, then a toast is shown if copying to |
90 | 102 | * clipboard is successful. |
91 | 103 | */ |
92 | | - public static void copyTextToClipboard(final Context context, final String text, final String toastString) { |
| 104 | + public static void copyTextToClipboard(Context context, @Nullable final String clipDataLabel, |
| 105 | + final String text, final String toastString) { |
93 | 106 | if (context == null || text == null) return; |
94 | 107 |
|
95 | | - final ClipboardManager clipboardManager = ContextCompat.getSystemService(context, ClipboardManager.class); |
| 108 | + ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); |
| 109 | + if (clipboardManager == null) return; |
96 | 110 |
|
97 | | - if (clipboardManager != null) { |
98 | | - clipboardManager.setPrimaryClip(ClipData.newPlainText(null, DataUtils.getTruncatedCommandOutput(text, DataUtils.TRANSACTION_SIZE_LIMIT_IN_BYTES, true, false, false))); |
99 | | - if (toastString != null && !toastString.isEmpty()) |
100 | | - Logger.showToast(context, toastString, true); |
101 | | - } |
| 111 | + clipboardManager.setPrimaryClip(ClipData.newPlainText(clipDataLabel, |
| 112 | + DataUtils.getTruncatedCommandOutput(text, DataUtils.TRANSACTION_SIZE_LIMIT_IN_BYTES, |
| 113 | + true, false, false))); |
| 114 | + |
| 115 | + if (toastString != null && !toastString.isEmpty()) |
| 116 | + Logger.showToast(context, toastString, true); |
102 | 117 | } |
103 | 118 |
|
| 119 | + |
| 120 | + |
104 | 121 | /** |
| 122 | + * Wrapper for {@link #getTextFromClipboard(Context, boolean)} that returns primary text {@link String} |
| 123 | + * if its set and not empty. |
| 124 | + */ |
| 125 | + @Nullable |
| 126 | + public static String getTextStringFromClipboardIfSet(Context context, boolean coerceToText) { |
| 127 | + CharSequence textCharSequence = getTextFromClipboard(context, coerceToText); |
| 128 | + if (textCharSequence == null) return null; |
| 129 | + String textString = textCharSequence.toString(); |
| 130 | + return !textString.isEmpty() ? textString : null; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Get the text from primary clip of the clipboard. |
| 135 | + * |
| 136 | + * @param context The context for operations. |
| 137 | + * @param coerceToText Whether to call {@link ClipData.Item#coerceToText(Context)} to coerce |
| 138 | + * non-text data to text. |
| 139 | + * @return Returns the {@link CharSequence} of primary text. This will be `null` if failed to get it. |
| 140 | + */ |
| 141 | + @Nullable |
| 142 | + public static CharSequence getTextFromClipboard(Context context, boolean coerceToText) { |
| 143 | + if (context == null) return null; |
| 144 | + |
| 145 | + ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); |
| 146 | + if (clipboardManager == null) return null; |
| 147 | + |
| 148 | + ClipData clipData = clipboardManager.getPrimaryClip(); |
| 149 | + if (clipData == null) return null; |
| 150 | + |
| 151 | + ClipData.Item clipItem = clipData.getItemAt(0); |
| 152 | + if (clipItem == null) return null; |
| 153 | + |
| 154 | + return coerceToText ? clipItem.coerceToText(context) : clipItem.getText(); |
| 155 | + } |
| 156 | + |
| 157 | + |
| 158 | + |
105 | 159 | * Open a url. |
106 | 160 | * |
107 | 161 | * @param context The context for operations. |
|
0 commit comments