1
0
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:
Fredrik Fornwall 2016-01-12 03:01:00 +01:00
parent 153fd6354c
commit 631401f1fb

View File

@ -1,21 +1,53 @@
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();
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.
@ -34,11 +66,8 @@ public class ClipboardAPI {
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();
}
if (!TextUtils.isEmpty(text)) {
out.println(text);
}
}
}
@ -46,5 +75,6 @@ public class ClipboardAPI {
}
});
}
}
}