mirror of
https://github.com/danog/termux-api.git
synced 2024-11-30 04:19:20 +01:00
Add a v2 of termux-clipboard for using stdin
This commit is contained in:
parent
153fd6354c
commit
631401f1fb
@ -1,50 +1,80 @@
|
||||
package com.termux.api;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
|
||||
import android.content.ClipData;
|
||||
import android.content.ClipData.Item;
|
||||
import android.content.ClipboardManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.termux.api.util.ResultReturner;
|
||||
import com.termux.api.util.ResultReturner.ResultWriter;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
|
||||
public class ClipboardAPI {
|
||||
|
||||
static void onReceive(TermuxApiReceiver apiReceiver, final Context context, Intent intent) {
|
||||
final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
|
||||
final ClipData clipData = clipboard.getPrimaryClip();
|
||||
final String newClipText = intent.getStringExtra("text");
|
||||
if (newClipText != null) {
|
||||
// Set clip.
|
||||
clipboard.setPrimaryClip(ClipData.newPlainText("", newClipText));
|
||||
}
|
||||
|
||||
ResultReturner.returnData(apiReceiver, intent, new ResultWriter() {
|
||||
@Override
|
||||
public void writeResult(PrintWriter out) {
|
||||
if (newClipText == null) {
|
||||
// Get clip.
|
||||
if (clipData == null) {
|
||||
out.println();
|
||||
} else {
|
||||
int itemCount = clipData.getItemCount();
|
||||
for (int i = 0; i < itemCount; i++) {
|
||||
Item item = clipData.getItemAt(i);
|
||||
CharSequence text = item.coerceToText(context);
|
||||
if (text != null) {
|
||||
out.print(text);
|
||||
if (i + 1 != itemCount) {
|
||||
out.println();
|
||||
boolean version2 = "2".equals(intent.getStringExtra("api_version"));
|
||||
if (version2) {
|
||||
boolean set = intent.getBooleanExtra("set", false);
|
||||
if (set) {
|
||||
ResultReturner.returnData(apiReceiver, intent, new ResultReturner.WithStringInput() {
|
||||
@Override
|
||||
public void writeResult(PrintWriter out) throws Exception {
|
||||
clipboard.setPrimaryClip(ClipData.newPlainText("", inputString));
|
||||
}
|
||||
});
|
||||
} else {
|
||||
ResultReturner.returnData(apiReceiver, intent, new ResultWriter() {
|
||||
@Override
|
||||
public void writeResult(PrintWriter out) {
|
||||
if (clipData == null) {
|
||||
out.println();
|
||||
} else {
|
||||
int itemCount = clipData.getItemCount();
|
||||
for (int i = 0; i < itemCount; i++) {
|
||||
Item item = clipData.getItemAt(i);
|
||||
CharSequence text = item.coerceToText(context);
|
||||
if (!TextUtils.isEmpty(text)) {
|
||||
out.println(text);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
} else {
|
||||
final String newClipText = intent.getStringExtra("text");
|
||||
if (newClipText != null) {
|
||||
// Set clip.
|
||||
clipboard.setPrimaryClip(ClipData.newPlainText("", newClipText));
|
||||
}
|
||||
|
||||
ResultReturner.returnData(apiReceiver, intent, new ResultWriter() {
|
||||
@Override
|
||||
public void writeResult(PrintWriter out) {
|
||||
if (newClipText == null) {
|
||||
// Get clip.
|
||||
if (clipData == null) {
|
||||
out.println();
|
||||
} else {
|
||||
int itemCount = clipData.getItemCount();
|
||||
for (int i = 0; i < itemCount; i++) {
|
||||
Item item = clipData.getItemAt(i);
|
||||
CharSequence text = item.coerceToText(context);
|
||||
if (!TextUtils.isEmpty(text)) {
|
||||
out.println(text);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user