forked from Blendman974/faceclaw
Terminal feature; raw screenshots.
This commit is contained in:
1 parent
d2a942f5a3
commit
fab6f6689e
19 files changed
+1075
-10
No files matched your search
@@ -0,0 +1,116 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.util.Log;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.WebSocket;
|
||||
import okhttp3.WebSocketListener;
|
||||
|
||||
/**
|
||||
* Thin okhttp WebSocket wrapper for the TypeScript side. Text frames only
|
||||
* (the g2mirror protocol is JSON-in-text-frames). All listener callbacks are
|
||||
* posted to the main thread, where the NativeScript runtime expects them.
|
||||
*/
|
||||
public class FaceclawWebSocket {
|
||||
private static final String TAG = "FaceclawWebSocket";
|
||||
private static final Handler mainHandler = new Handler(Looper.getMainLooper());
|
||||
private static volatile OkHttpClient sharedClient;
|
||||
|
||||
private final WebSocket socket;
|
||||
private volatile boolean closeRequested;
|
||||
|
||||
public FaceclawWebSocket(String url, final FaceclawWebSocketListener listener) {
|
||||
if (url == null || url.trim().isEmpty()) {
|
||||
throw new IllegalArgumentException("url is required");
|
||||
}
|
||||
if (listener == null) {
|
||||
throw new IllegalArgumentException("listener is required");
|
||||
}
|
||||
Request request = new Request.Builder().url(url.trim()).build();
|
||||
socket = getClient().newWebSocket(request, new WebSocketListener() {
|
||||
@Override public void onOpen(WebSocket webSocket, Response response) {
|
||||
mainHandler.post(() -> {
|
||||
try {
|
||||
listener.onOpen();
|
||||
} catch (Throwable t) {
|
||||
Log.w(TAG, "listener onOpen failed", t);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override public void onMessage(WebSocket webSocket, String text) {
|
||||
mainHandler.post(() -> {
|
||||
try {
|
||||
listener.onTextMessage(text);
|
||||
} catch (Throwable t) {
|
||||
Log.w(TAG, "listener onTextMessage failed", t);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override public void onClosing(WebSocket webSocket, int code, String reason) {
|
||||
webSocket.close(code, reason);
|
||||
}
|
||||
|
||||
@Override public void onClosed(WebSocket webSocket, int code, String reason) {
|
||||
mainHandler.post(() -> {
|
||||
try {
|
||||
listener.onClosed(code, reason == null ? "" : reason);
|
||||
} catch (Throwable t) {
|
||||
Log.w(TAG, "listener onClosed failed", t);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override public void onFailure(WebSocket webSocket, Throwable t, Response response) {
|
||||
if (closeRequested) {
|
||||
return;
|
||||
}
|
||||
final String message = t == null ? "unknown websocket failure" : String.valueOf(t);
|
||||
mainHandler.post(() -> {
|
||||
try {
|
||||
listener.onFailure(message);
|
||||
} catch (Throwable inner) {
|
||||
Log.w(TAG, "listener onFailure failed", inner);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static OkHttpClient getClient() {
|
||||
OkHttpClient client = sharedClient;
|
||||
if (client == null) {
|
||||
synchronized (FaceclawWebSocket.class) {
|
||||
if (sharedClient == null) {
|
||||
sharedClient = new OkHttpClient.Builder()
|
||||
.pingInterval(20, TimeUnit.SECONDS)
|
||||
.build();
|
||||
}
|
||||
client = sharedClient;
|
||||
}
|
||||
}
|
||||
return client;
|
||||
}
|
||||
|
||||
public boolean sendText(String message) {
|
||||
return socket.send(message == null ? "" : message);
|
||||
}
|
||||
|
||||
public void close(int code, String reason) {
|
||||
closeRequested = true;
|
||||
try {
|
||||
if (!socket.close(code, reason)) {
|
||||
socket.cancel();
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
socket.cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
/** Callbacks for FaceclawWebSocket, delivered on the Android main thread. */
|
||||
public interface FaceclawWebSocketListener {
|
||||
void onOpen();
|
||||
void onTextMessage(String message);
|
||||
void onClosed(int code, String reason);
|
||||
void onFailure(String message);
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Saves raw screenshots for compression experiments. The output is the exact
|
||||
* headerless top-down 4bpp frame buffer that gets zlib-compressed on the wire
|
||||
* (build4bppBmp followed by pack4bppFromBmp), deliberately untransformed — no
|
||||
* resize, no gamma, no header.
|
||||
*/
|
||||
public final class ScreenshotUtil {
|
||||
private ScreenshotUtil() {}
|
||||
|
||||
/** Returns the absolute path of the written file. */
|
||||
public static String saveRawScreenshot(Context context, ByteBuffer image8bpp, int width, int height) throws java.io.IOException {
|
||||
if (image8bpp == null || width <= 0 || height <= 0 || image8bpp.remaining() < width * height) {
|
||||
throw new IllegalArgumentException("invalid screenshot buffer");
|
||||
}
|
||||
byte[] gray = new byte[image8bpp.remaining()];
|
||||
image8bpp.get(gray);
|
||||
byte[] packed = BmpUtil.pack4bppFromBmp(BmpUtil.build4bppBmp(gray, width, height));
|
||||
if (packed.length == 0) {
|
||||
throw new IllegalStateException("failed to pack 4bpp frame");
|
||||
}
|
||||
|
||||
File dir = new File(context.getExternalFilesDir(null), "screenshots");
|
||||
if (!dir.exists() && !dir.mkdirs()) {
|
||||
throw new java.io.IOException("failed to create " + dir);
|
||||
}
|
||||
String stamp = new SimpleDateFormat("yyyyMMdd-HHmmss-SSS", Locale.US).format(new Date());
|
||||
File file = new File(dir, "screen-" + stamp + "-" + width + "x" + height + ".4bpp");
|
||||
try (FileOutputStream out = new FileOutputStream(file)) {
|
||||
out.write(packed);
|
||||
}
|
||||
return file.getAbsolutePath();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user