[Refactor] (android-plugin/app, MG_Util/SelfTest): simplify Android controls and driver diagnostics

This commit is contained in:
BZLZHH committed 2026-11-29 09:08:03 +08:00
1 parent 0059ccdd79
commit adfed91a1e
6 files changed
+352 -499

No files matched your search

@@ -35,8 +35,8 @@ public final class NavBar {
LinearLayout bar = new LinearLayout(activity);
bar.setOrientation(LinearLayout.HORIZONTAL);
bar.setBackgroundColor(COLOR_BAR);
addTab(activity, bar, "POST", PostActivity.class, currentTab == TAB_POST);
addTab(activity, bar, "Render Server", ServerControlActivity.class, currentTab == TAB_SERVER);
addTab(activity, bar, "Diagnostics", PostActivity.class, currentTab == TAB_POST);
addTab(activity, bar, "Server", ServerControlActivity.class, currentTab == TAB_SERVER);
root.addView(bar, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, dp(activity, BAR_HEIGHT_DP)));
return root;
@@ -98,7 +98,7 @@ public final class PostActivity extends Activity {
setContentView(NavBar.wrap(this, scrollView, NavBar.TAB_POST));
addText("MobileGL Driver POST", 20, COLOR_TEXT, true, 0);
statusView = addText("Running self-test...", 14, COLOR_INFO, false, dp(8));
statusView = addText("Checking drivers…", 14, COLOR_INFO, false, dp(8));
progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
progressBar.setIndeterminate(true);
@@ -119,8 +119,6 @@ public final class PostActivity extends Activity {
postInFlight = true;
startWorker = true;
}
// else: a probe is already in flight; leave "Running self-test..."
// showing and wait for delivery to this (the current) activity.
}
if (renderNow) {
renderCachedResult();
@@ -174,7 +172,7 @@ public final class PostActivity extends Activity {
Button button = benchButtons.get(backendType);
if (button != null) {
button.setEnabled(false);
button.setText("Bench running... (can take a minute)");
button.setText("Benchmark running…");
}
LinearLayout container = benchResultContainers.get(backendType);
if (container != null) {
@@ -191,7 +189,7 @@ public final class PostActivity extends Activity {
Button button = benchButtons.get(backendType);
if (button != null) {
button.setEnabled(true);
button.setText("Run Bench");
button.setText("Run benchmark");
}
LinearLayout container = benchResultContainers.get(backendType);
if (container == null) {
@@ -202,7 +200,7 @@ public final class PostActivity extends Activity {
JSONObject root = new JSONObject(json);
String error = root.optString("error", "");
if (!error.isEmpty()) {
container.addView(makeText("Bench failed: " + error, 12, COLOR_FAIL, false));
container.addView(makeText("Benchmark failed: " + error, 12, COLOR_FAIL, false));
return;
}
container.addView(makeText(root.optString("renderer", ""), 11, COLOR_INFO, false));
@@ -228,7 +226,7 @@ public final class PostActivity extends Activity {
container.addView(view);
}
} catch (JSONException error) {
container.addView(makeText("Bench result unparsable: " + error, 12, COLOR_FAIL, false));
container.addView(makeText("Could not read benchmark result: " + error, 12, COLOR_FAIL, false));
}
}
@@ -317,7 +315,9 @@ public final class PostActivity extends Activity {
failure = cachedFailure;
}
if (failure != null) {
showError("Driver POST failed: " + failure);
showError("Could not start driver diagnostics.");
addDisclosure(contentLayout, "Error details", dp(4))
.addView(makeText(failure.toString(), 12, COLOR_DETAIL, false));
return;
}
renderReport(json);
@@ -325,34 +325,37 @@ public final class PostActivity extends Activity {
private void renderReport(String json) {
if (json == null || json.isEmpty()) {
showError("Driver POST returned an empty report.");
showError("No diagnostics report was returned.");
return;
}
JSONObject root;
try {
root = new JSONObject(json);
} catch (JSONException error) {
showError("Failed to parse POST report: " + error.getMessage() + "\n\n" + json);
showError("Could not read the diagnostics report.");
addRawJsonSection(json);
return;
}
String nativeError = root.optString("error", "");
if (!nativeError.isEmpty()) {
showError(nativeError);
showError("Driver diagnostics failed.");
addRawJsonSection(json);
return;
}
JSONArray backends = extractBackends(root);
if (backends.length() == 0) {
showError("POST report contains no backend sections:\n\n" + json);
return;
}
statusView.setText("Self-test complete.");
boolean hasBackend = false;
for (int i = 0; i < backends.length(); ++i) {
JSONObject backend = backends.optJSONObject(i);
if (backend != null) {
renderBackendSection(backend);
hasBackend = true;
}
}
if (hasBackend) {
statusView.setVisibility(View.GONE);
} else {
showError("No backend results were reported.");
}
addRawJsonSection(json);
}
@@ -381,88 +384,56 @@ public final class PostActivity extends Activity {
private void renderBackendSection(JSONObject backend) {
String name = backend.optString("backend", backend.optString("name", "Unknown backend"));
addText(sectionTitle(name), 16, COLOR_TEXT, true, dp(20));
if (!backend.optBoolean("available", true)) {
addText("Driver not present / not probeable", 12, COLOR_INFO, false, dp(4));
return;
}
String verdict = backend.optString("verdict", "UNKNOWN");
addText("Verdict: " + verdict, 14, verdictColor(verdict), true, dp(4));
String renderer = backend.optString("renderer", "");
if (!renderer.isEmpty()) {
addText(renderer, 12, COLOR_INFO, false, dp(2));
}
addBenchControls(name);
boolean available = backend.optBoolean("available", true);
String verdict = available ? backend.optString("verdict", "Unknown") : "Unavailable";
addText(sectionTitle(name) + " · " + verdict, 16, verdictColor(verdict), true, dp(16));
JSONArray checks = backend.optJSONArray("checks");
JSONArray bugs = backend.optJSONArray("knownDriverBugs");
LinearLayout issues = newColumn();
LinearLayout otherChecks = newColumn();
if (checks != null) {
LinearLayout table = new LinearLayout(this);
table.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams tableParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
tableParams.topMargin = dp(6);
contentLayout.addView(table, tableParams);
int rowIndex = 0;
for (int i = 0; i < checks.length(); ++i) {
JSONObject check = checks.optJSONObject(i);
if (check == null) {
continue;
}
addCheckRow(table, check, rowIndex++);
String status = check.optString("status", "INFO");
boolean issue = "WARN".equalsIgnoreCase(status) || "FAIL".equalsIgnoreCase(status);
addCheckRow(issue ? issues : otherChecks, check.optString("name", "Unnamed check"), status,
check.optString("detail", check.optString("message", "")));
}
}
renderKnownDriverBugs(backend.optJSONArray("knownDriverBugs"));
renderFormatCapabilities(backend.optJSONObject("formatCapabilities"));
}
/**
* The "Known Driver Bugs" section: core functionality this driver advertises, accepts,
* and then does not perform. Separate from the capability checks above because it answers
* a different question and uses its own vocabulary.
*
* Only bugs the device actually HAS are reported, so a clean driver renders no section at
* all rather than a list of reassurances - which is why the verdicts are FIXED (a MobileGL
* quirk makes application behaviour correct anyway) and UNFIXABLE (no substitute; the
* one-liner says what MobileGL does defensively), never PASS/FAIL.
*/
private void renderKnownDriverBugs(JSONArray bugs) {
if (bugs == null || bugs.length() == 0) {
return;
if (bugs != null) {
for (int i = 0; i < bugs.length(); ++i) {
JSONObject bug = bugs.optJSONObject(i);
if (bug == null) {
continue;
}
String status = bug.optString("verdict", "UNFIXABLE");
addCheckRow("FIXED".equalsIgnoreCase(status) ? otherChecks : issues,
bug.optString("name", "Unnamed driver bug"), status, bug.optString("detail", ""));
}
}
if (issues.getChildCount() != 0) {
contentLayout.addView(issues);
}
addText("Known driver bugs", 14, COLOR_TEXT, true, dp(16));
LinearLayout table = new LinearLayout(this);
table.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams tableParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
tableParams.topMargin = dp(6);
contentLayout.addView(table, tableParams);
int rowIndex = 0;
for (int i = 0; i < bugs.length(); ++i) {
JSONObject bug = bugs.optJSONObject(i);
if (bug == null) {
continue;
String renderer = backend.optString("renderer", "");
String backendType = available ? backendTypeForSection(name) : null;
JSONObject formats = backend.optJSONObject("formatCapabilities");
if (!renderer.isEmpty() || otherChecks.getChildCount() != 0 || formats != null || backendType != null) {
LinearLayout details = addDisclosure(contentLayout, "Details", dp(4));
if (!renderer.isEmpty()) {
details.addView(makeText(renderer, 12, COLOR_INFO, false));
}
// addCheckRow renders name + chip + collapsible detail, which is exactly this
// section's shape; the chip text is the verdict rather than a status.
JSONObject row = new JSONObject();
try {
row.put("name", bug.optString("name", "unnamed bug"));
row.put("status", bug.optString("verdict", "UNFIXABLE"));
row.put("detail", bug.optString("detail", ""));
} catch (JSONException ignored) {
continue;
renderFormatCapabilities(details, formats);
if (backendType != null) {
addBenchControls(details, backendType);
}
if (otherChecks.getChildCount() != 0) {
addDisclosure(details, "Other checks", dp(4)).addView(otherChecks);
}
addCheckRow(table, row, rowIndex++);
}
}
@@ -485,13 +456,9 @@ public final class PostActivity extends Activity {
* runs the Minecraft-shaped MG_Benchmark cases through the full MobileGL stack
* on this backend, in a throwaway service process (see BenchService).
*/
private void addBenchControls(String sectionName) {
final String backendType = backendTypeForSection(sectionName);
if (backendType == null) {
return;
}
private void addBenchControls(LinearLayout parent, String backendType) {
Button button = new Button(this);
button.setText("Run Bench");
button.setText("Run benchmark");
button.setAllCaps(false);
button.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
button.setOnClickListener(v -> startBench(backendType));
@@ -500,7 +467,7 @@ public final class PostActivity extends Activity {
LinearLayout.LayoutParams.WRAP_CONTENT
);
buttonParams.topMargin = dp(6);
contentLayout.addView(button, buttonParams);
parent.addView(button, buttonParams);
benchButtons.put(backendType, button);
LinearLayout results = new LinearLayout(this);
@@ -510,7 +477,7 @@ public final class PostActivity extends Activity {
LinearLayout.LayoutParams.WRAP_CONTENT
);
resultParams.topMargin = dp(4);
contentLayout.addView(results, resultParams);
parent.addView(results, resultParams);
benchResultContainers.put(backendType, results);
}
@@ -519,15 +486,12 @@ public final class PostActivity extends Activity {
* a non-empty detail get a collapse indicator and toggle the detail text on tap;
* rows without detail show no indicator and are not tappable.
*/
private void addCheckRow(LinearLayout table, JSONObject check, int rowIndex) {
String status = check.optString("status", "INFO");
String checkName = check.optString("name", "unnamed check");
String detail = check.optString("detail", check.optString("message", ""));
private void addCheckRow(LinearLayout table, String checkName, String status, String detail) {
boolean hasDetail = !detail.isEmpty();
LinearLayout row = new LinearLayout(this);
row.setOrientation(LinearLayout.VERTICAL);
row.setBackgroundColor(rowIndex % 2 == 0 ? COLOR_ROW_EVEN : COLOR_ROW_ODD);
row.setBackgroundColor(table.getChildCount() % 2 == 0 ? COLOR_ROW_EVEN : COLOR_ROW_ODD);
table.addView(row, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
@@ -543,7 +507,8 @@ public final class PostActivity extends Activity {
LinearLayout.LayoutParams.WRAP_CONTENT
));
TextView indicatorView = makeText(hasDetail ? INDICATOR_COLLAPSED : "", 12, COLOR_INFO, false);
String indicator = hasDetail ? INDICATOR_COLLAPSED : "";
TextView indicatorView = makeText(indicator, 12, COLOR_INFO, false);
line.addView(indicatorView, new LinearLayout.LayoutParams(
dp(16),
LinearLayout.LayoutParams.WRAP_CONTENT
@@ -570,8 +535,9 @@ public final class PostActivity extends Activity {
return;
}
TextView detailView = makeText(detail, 11, COLOR_DETAIL, false);
detailView.setPadding(dp(24), 0, dp(8), dp(8));
TextView detailView = makeText(detail, 13, COLOR_DETAIL, false);
detailView.setTypeface(Typeface.DEFAULT);
detailView.setPadding(dp(24), 0, dp(12), dp(10));
detailView.setVisibility(View.GONE);
row.addView(detailView, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
@@ -586,24 +552,22 @@ public final class PostActivity extends Activity {
}
/** Adds one initially-collapsed capability matrix for every reported target. */
private void renderFormatCapabilities(JSONObject formatCapabilities) {
addText("Format capabilities", 14, COLOR_TEXT, true, dp(16));
private void renderFormatCapabilities(LinearLayout parent, JSONObject formatCapabilities) {
if (formatCapabilities == null) {
addText("Format capability table unavailable.", 12, COLOR_INFO, false, dp(4));
return;
}
JSONArray capabilities = formatCapabilities.optJSONArray("capabilities");
JSONArray targets = formatCapabilities.optJSONArray("targets");
if (capabilities == null || capabilities.length() == 0 || targets == null || targets.length() == 0) {
addText("Format capability table is empty.", 12, COLOR_INFO, false, dp(4));
return;
}
LinearLayout formats = addDisclosure(parent, "Format capabilities", dp(6));
for (int targetIndex = 0; targetIndex < targets.length(); ++targetIndex) {
JSONObject target = targets.optJSONObject(targetIndex);
if (target != null) {
addFormatTargetTable(target, capabilities);
addFormatTargetTable(formats, target, capabilities);
}
}
}
@@ -612,7 +576,7 @@ public final class PostActivity extends Activity {
* Adds a target header whose table is created only while expanded. Removing the
* table again on collapse avoids retaining all status cells for both backends.
*/
private void addFormatTargetTable(JSONObject target, JSONArray capabilities) {
private void addFormatTargetTable(LinearLayout parent, JSONObject target, JSONArray capabilities) {
String targetName = target.optString("name", "Unknown target");
JSONArray rows = target.optJSONArray("rows");
int formatCount = rows == null ? 0 : rows.length();
@@ -624,7 +588,7 @@ public final class PostActivity extends Activity {
LinearLayout.LayoutParams.WRAP_CONTENT
);
sectionParams.topMargin = dp(6);
contentLayout.addView(section, sectionParams);
parent.addView(section, sectionParams);
TextView toggleView = makeText(
INDICATOR_COLLAPSED + " " + targetName + " (" + formatCount + " formats)",
@@ -757,22 +721,35 @@ public final class PostActivity extends Activity {
row.addView(cell, params);
}
/** Adds the collapsed "Raw report" toggle plus the (initially hidden) raw JSON dump. */
private void addRawJsonSection(String json) {
TextView toggleView = addText(INDICATOR_COLLAPSED + " Raw report (tap to expand)", 12, COLOR_INFO, true, dp(24));
toggleView.setMinimumHeight(dp(44));
toggleView.setGravity(Gravity.CENTER_VERTICAL);
LinearLayout details = addDisclosure(contentLayout, "Raw report", dp(12));
details.addView(makeText(json, 10, COLOR_INFO, false));
}
TextView rawView = addText(json, 10, COLOR_INFO, false, dp(4));
rawView.setVisibility(View.GONE);
private LinearLayout addDisclosure(LinearLayout parent, String label, int margin) {
TextView toggle = makeText(INDICATOR_COLLAPSED + " " + label, 12, COLOR_TEXT, true);
toggle.setMinimumHeight(dp(44));
toggle.setGravity(Gravity.CENTER_VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.topMargin = margin;
parent.addView(toggle, params);
toggleView.setOnClickListener(view -> {
boolean expanded = rawView.getVisibility() == View.VISIBLE;
rawView.setVisibility(expanded ? View.GONE : View.VISIBLE);
toggleView.setText(expanded
? INDICATOR_COLLAPSED + " Raw report (tap to expand)"
: INDICATOR_EXPANDED + " Raw report (tap to collapse)");
LinearLayout section = newColumn();
section.setVisibility(View.GONE);
parent.addView(section);
toggle.setOnClickListener(view -> {
boolean expanded = section.getVisibility() == View.VISIBLE;
section.setVisibility(expanded ? View.GONE : View.VISIBLE);
toggle.setText((expanded ? INDICATOR_COLLAPSED : INDICATOR_EXPANDED) + " " + label);
});
return section;
}
private LinearLayout newColumn() {
LinearLayout column = new LinearLayout(this);
column.setOrientation(LinearLayout.VERTICAL);
return column;
}
private static String sectionTitle(String backendName) {
@@ -793,6 +770,7 @@ public final class PostActivity extends Activity {
case "DEGRADED":
return COLOR_WARN;
case "UNSUPPORTED":
case "UNAVAILABLE":
return COLOR_FAIL;
default:
return COLOR_INFO;
@@ -823,9 +801,8 @@ public final class PostActivity extends Activity {
private void showError(String message) {
Log.e(TAG, message);
statusView.setText("Self-test failed.");
statusView.setText(message);
statusView.setTextColor(COLOR_FAIL);
addText(message, 12, COLOR_FAIL, false, dp(8));
}
private TextView addText(String text, int sizeSp, int color, boolean bold, int topMarginPx) {
@@ -11,6 +11,7 @@ import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.CheckBox;
@@ -75,32 +76,44 @@ public final class ServerControlActivity extends Activity {
setContentView(NavBar.wrap(this, scrollView, NavBar.TAB_SERVER));
contentLayout.addView(makeText("MobileGL Render Server", 20, COLOR_TEXT, true));
listenField = addField(contentLayout, "Listen address", DEFAULT_ENDPOINT, dp(12));
listenField.setHint("tcp://host:port or @name");
LinearLayout tokenRow = new LinearLayout(this);
tokenRow.setGravity(android.view.Gravity.CENTER_VERTICAL);
tokenField = addField(contentLayout, "Access token", "", dp(8));
tokenField.setHint("16+ bytes for remote access");
contentLayout.removeView(tokenField);
contentLayout.addView(tokenRow);
tokenRow.addView(tokenField, new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1));
TextView hint = makeText(
"Starts the render server. Offscreen (default): the supervisor "
+ "(libMobileGLServer.so --serve) runs as a foreground service. On-screen "
+ "window: the server runs in a display Activity that renders the client's "
+ "frames onto this screen (clients opt in with MOBILEGL_IPC_SURFACE=server). "
+ "Endpoint forms: tcp://host:port for remote clients, @name for an "
+ "on-device abstract socket, or a filesystem path (relative to the app's "
+ "files dir) for on-device clients. Non-loopback TCP requires a token of at "
+ "least " + MIN_TOKEN_BYTES + " bytes; unix endpoints need none, but GL programs "
+ "from other apps (Termux, adb shell) reach the server through its broker, which "
+ "needs one (see the launch command below). Extra env "
+ "uses the KEY=VALUE;KEY grammar (e.g. MOBILEGL_BACKEND_TYPE=DirectVulkan). "
+ "One server per device: the two modes replace each other.",
12, COLOR_INFO, false);
LinearLayout.LayoutParams hintParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
hintParams.topMargin = dp(8);
contentLayout.addView(hint, hintParams);
Button generate = new Button(this);
generate.setText("Generate");
generate.setAllCaps(false);
generate.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
generate.setOnClickListener(v -> {
byte[] bytes = new byte[16];
new java.security.SecureRandom().nextBytes(bytes);
StringBuilder hex = new StringBuilder();
for (byte b : bytes)
hex.append(String.format(Locale.ROOT, "%02x", b & 0xff));
tokenField.setText(hex.toString());
});
tokenRow.addView(generate);
listenField = addField(contentLayout, "Listen endpoint", DEFAULT_ENDPOINT, dp(16));
tokenField = addField(contentLayout, "Auth token (empty = loopback only)", "", dp(8));
envField = addField(contentLayout, "Extra env (optional)", "", dp(8));
Button advanced = new Button(this);
advanced.setText("Advanced settings");
advanced.setAllCaps(false);
contentLayout.addView(advanced);
LinearLayout advancedFields = new LinearLayout(this);
advancedFields.setOrientation(LinearLayout.VERTICAL);
advancedFields.setVisibility(View.GONE);
contentLayout.addView(advancedFields);
envField = addField(advancedFields, "Environment variables", "", 0);
envField.setHint("KEY=VALUE;KEY");
advanced.setOnClickListener(v -> toggleSection(advanced, advancedFields, "Advanced settings"));
onScreenBox = new CheckBox(this);
onScreenBox.setText("On-screen window (render client frames onto this screen)");
onScreenBox.setText("Show output on screen");
onScreenBox.setTextColor(COLOR_TEXT);
onScreenBox.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
LinearLayout.LayoutParams boxParams = new LinearLayout.LayoutParams(
@@ -141,7 +154,7 @@ public final class ServerControlActivity extends Activity {
addExternalClientSection(contentLayout);
if (isServerRunning()) {
setStatus("A server is running (started outside this screen).", COLOR_WARN);
setStatus("Running", COLOR_WARN);
} else {
setStatus("Stopped.", COLOR_INFO);
}
@@ -384,42 +397,35 @@ public final class ServerControlActivity extends Activity {
// helper class, the broker component, and how the token travels (the environment, never argv).
private TextView launchCommandView;
private Button copyLaunchButton;
private void addExternalClientSection(LinearLayout parent) {
TextView title = makeText("Same-device GL programs (Termux, adb shell, other apps)", 14, COLOR_TEXT, true);
LinearLayout.LayoutParams titleParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
titleParams.topMargin = dp(20);
parent.addView(title, titleParams);
Button localClients = new Button(this);
localClients.setText("Local clients");
localClients.setAllCaps(false);
parent.addView(localClients);
LinearLayout details = new LinearLayout(this);
details.setOrientation(LinearLayout.VERTICAL);
details.setVisibility(View.GONE);
parent.addView(details);
localClients.setOnClickListener(v -> toggleSection(localClients, details, "Local clients"));
launchCommandView = makeText("", 12, COLOR_INFO, false);
launchCommandView.setTextIsSelectable(true);
LinearLayout.LayoutParams commandParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
commandParams.topMargin = dp(8);
parent.addView(launchCommandView, commandParams);
details.addView(launchCommandView, commandParams);
LinearLayout row = new LinearLayout(this);
row.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams rowParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
rowParams.topMargin = dp(8);
parent.addView(row, rowParams);
Button generate = new Button(this);
generate.setText("Generate token");
generate.setAllCaps(false);
generate.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
generate.setOnClickListener(v -> {
byte[] bytes = new byte[16];
new java.security.SecureRandom().nextBytes(bytes);
StringBuilder hex = new StringBuilder();
for (byte b : bytes) hex.append(String.format(Locale.ROOT, "%02x", b & 0xff));
tokenField.setText(hex.toString());
});
row.addView(generate);
details.addView(row, rowParams);
Button copy = new Button(this);
copyLaunchButton = copy;
copy.setText("Copy launch command");
copy.setAllCaps(false);
copy.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
@@ -431,8 +437,7 @@ public final class ServerControlActivity extends Activity {
android.content.ClipboardManager clipboard = getSystemService(android.content.ClipboardManager.class);
clipboard.setPrimaryClip(android.content.ClipData.newPlainText("MobileGL launch command",
launchCommand(tokenField.getText().toString())));
setStatus("Launch command copied (it carries the token: paste it only where you run the program).",
COLOR_INFO);
android.widget.Toast.makeText(this, "Command copied", android.widget.Toast.LENGTH_SHORT).show();
});
tokenField.addTextChangedListener(new android.text.TextWatcher() {
@@ -453,19 +458,15 @@ public final class ServerControlActivity extends Activity {
if (launchCommandView == null) return;
String token = tokenField.getText().toString();
boolean usable = token.getBytes().length >= ExternalClientBroker.MIN_TOKEN_BYTES;
StringBuilder text = new StringBuilder();
if (!usable) {
text.append("Needs an Auth token of at least ").append(ExternalClientBroker.MIN_TOKEN_BYTES)
.append(" bytes (Generate token), then start the server offscreen.\n\n");
}
text.append(launchCommand(usable ? token : "<token>")).append("\n\n")
.append("Broker: ").append(getPackageName()).append('/').append(ExternalClientBroker.class.getName())
.append("\nThe token travels in the environment (MOBILEGL_IPC_TOKEN, or MOBILEGL_IPC_TOKEN_FILE=<file>)")
.append(", never on the command line. The program runs over shared memory, off the one cpu core")
.append(" the server's render thread takes (--keep-affinity to opt out); the server keeps")
.append(" running as a foreground service. Battery: set both this app and the client's app to")
.append(" \"No restrictions\".");
launchCommandView.setText(text.toString());
launchCommandView.setText(usable ? launchCommand("<token>")
: "Generate a token, then start the server with on-screen output off.");
copyLaunchButton.setEnabled(usable);
}
private void toggleSection(Button button, LinearLayout section, String title) {
boolean opening = section.getVisibility() != View.VISIBLE;
section.setVisibility(opening ? View.VISIBLE : View.GONE);
button.setText(opening ? "Hide " + title.toLowerCase(Locale.ROOT) : title);
}
/** {host, port} for tcp://host:port, or null. */