App 2.19.4: Export CarPlay data to the USB stick
Read-only diagnostic for CarPlay's resolution: window, SurfaceFlinger and display state captured while CarPlay is in front, display-related properties without identifiers, Allgo's APK and libraries and CarPlay-named system files, written to the app's folder on the stick. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LhzAVaUyqcw5PkGjz4mWW6
This commit is contained in:
@@ -0,0 +1,234 @@
|
||||
package com.evsuite.abrp;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.os.Environment;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Read-only diagnostic for the CarPlay resolution question: copies what decides CarPlay's size
|
||||
* to a folder on the USB stick, for reading on a computer. It reads the car and writes only to
|
||||
* the stick.
|
||||
*
|
||||
* What it writes to {@code <stick>/Android/data/<package>/files/nilsmg4-carplay-<time>/}:
|
||||
* <ul>
|
||||
* <li>the CarPlay app's APK files and its own native libraries;</li>
|
||||
* <li>libraries and configuration files under /system, /vendor, /odm and /product whose path
|
||||
* names CarPlay (see {@link #isCarPlayPath});</li>
|
||||
* <li>the display-related system properties, never identifiers ({@link #keepProperty});</li>
|
||||
* <li>the window manager, SurfaceFlinger and display state, captured while CarPlay is in front.</li>
|
||||
* </ul>
|
||||
* Only the system user (the car build) may run dumpsys; elsewhere those files say why not.
|
||||
*/
|
||||
final class CarPlayExport {
|
||||
|
||||
private static final String TAG = "EVABRP.CarPlayExport";
|
||||
static final String CARPLAY_PACKAGE = "com.allgo.carplay.service";
|
||||
|
||||
private static final long MAX_FILE_BYTES = 64L << 20;
|
||||
private static final int MAX_FOUND_FILES = 200;
|
||||
private static final String[] SEARCH_ROOTS = {
|
||||
"/system/etc", "/system/lib", "/system/lib64", "/system/priv-app", "/system/app",
|
||||
"/vendor/etc", "/vendor/lib", "/vendor/lib64", "/vendor/app", "/vendor/priv-app",
|
||||
"/odm/etc", "/product/etc", "/product/app", "/product/priv-app",
|
||||
};
|
||||
|
||||
private static final Pattern CARPLAY_PATH = Pattern.compile("(?i)carplay|allgo|iap2|airplay");
|
||||
private static final Pattern PROPERTY_KEEP = Pattern.compile(
|
||||
"(?i)carplay|allgo|iap|airplay|display|lcd|screen|resolution|density|surface|hwui|"
|
||||
+ "\\bsf\\.|\\.sf\\.|width|height|ro\\.build\\.(version|display|fingerprint|id)|"
|
||||
+ "vehicle_type|projection");
|
||||
private static final Pattern PROPERTY_DROP = Pattern.compile(
|
||||
"(?i)serial|vin\\b|\\.vin|imei|iccid|imsi|mac|bt\\.addr|bdaddr|wifi|ssid|password|token|"
|
||||
+ "key|account|phone|msisdn|sim");
|
||||
|
||||
private CarPlayExport() {}
|
||||
|
||||
/** A path whose name says it belongs to CarPlay. */
|
||||
static boolean isCarPlayPath(String path) {
|
||||
return path != null && CARPLAY_PATH.matcher(path).find();
|
||||
}
|
||||
|
||||
/** A {@code [key]: [value]} getprop line worth exporting: display-related, never an identifier. */
|
||||
static boolean keepProperty(String getpropLine) {
|
||||
if (getpropLine == null) return false;
|
||||
int end = getpropLine.indexOf("]:");
|
||||
String key = end > 0 ? getpropLine.substring(0, end) : getpropLine;
|
||||
return PROPERTY_KEEP.matcher(key).find() && !PROPERTY_DROP.matcher(key).find();
|
||||
}
|
||||
|
||||
/** Runs the export; returns one line for the screen. Blocking: call it off the main thread. */
|
||||
static String run(Context context) {
|
||||
File stick = stickFolder(context);
|
||||
if (stick == null) {
|
||||
Log.w(TAG, "no USB stick");
|
||||
return "No USB stick found";
|
||||
}
|
||||
String stamp = new SimpleDateFormat("yyyyMMdd-HHmmss", Locale.US).format(new Date());
|
||||
File out = new File(stick, "nilsmg4-carplay-" + stamp);
|
||||
if (!out.mkdirs()) {
|
||||
Log.w(TAG, "cannot create " + out);
|
||||
return "Could not create " + out;
|
||||
}
|
||||
List<String> notes = new ArrayList<>();
|
||||
int copied = 0;
|
||||
|
||||
// The live state first, while CarPlay is still in front.
|
||||
command(out, "dumpsys-window.txt", notes, "dumpsys", "window", "windows");
|
||||
command(out, "dumpsys-surfaceflinger.txt", notes, "dumpsys", "SurfaceFlinger");
|
||||
command(out, "dumpsys-display.txt", notes, "dumpsys", "display");
|
||||
command(out, "dumpsys-activity.txt", notes, "dumpsys", "activity", "activities");
|
||||
command(out, "wm.txt", notes, "sh", "-c", "wm size; wm density");
|
||||
properties(out, notes);
|
||||
|
||||
try {
|
||||
ApplicationInfo app = context.getPackageManager().getApplicationInfo(CARPLAY_PACKAGE, 0);
|
||||
notes.add("uid " + app.uid + ", flags 0x" + Integer.toHexString(app.flags)
|
||||
+ ", source " + app.sourceDir + ", native " + app.nativeLibraryDir);
|
||||
File apkDir = new File(out, "apk");
|
||||
copied += copy(new File(app.sourceDir), apkDir, notes);
|
||||
if (app.splitSourceDirs != null) {
|
||||
for (String split : app.splitSourceDirs) copied += copy(new File(split), apkDir, notes);
|
||||
}
|
||||
File[] libs = app.nativeLibraryDir == null ? null : new File(app.nativeLibraryDir).listFiles();
|
||||
if (libs != null) {
|
||||
for (File lib : libs) copied += copy(lib, new File(out, "native"), notes);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
notes.add("CarPlay app not found: " + e);
|
||||
}
|
||||
|
||||
List<File> found = new ArrayList<>();
|
||||
for (String root : SEARCH_ROOTS) find(new File(root), 0, found);
|
||||
for (File f : found) {
|
||||
copied += copy(f, new File(out, "found" + f.getParent()), notes);
|
||||
}
|
||||
notes.add(found.size() + " CarPlay-named files under the system partitions");
|
||||
|
||||
write(new File(out, "notes.txt"), String.join("\n", notes) + "\n");
|
||||
String result = "Exported " + copied + " files to " + out;
|
||||
Log.i(TAG, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* This app's own folder on the removable volume, or on any volume (the emulator) when there is
|
||||
* none: <stick>/Android/data/<package>/files. Android 9 lets an app write there and nowhere
|
||||
* else on a removable volume — the stick's top folder is read-only to it.
|
||||
*/
|
||||
private static File stickFolder(Context context) {
|
||||
File fallback = null;
|
||||
for (File dir : context.getExternalFilesDirs(null)) {
|
||||
if (dir == null || UsbVolume.root(dir) == null) continue;
|
||||
boolean removable;
|
||||
try {
|
||||
removable = Environment.isExternalStorageRemovable(dir);
|
||||
} catch (IllegalArgumentException e) {
|
||||
removable = false;
|
||||
}
|
||||
if (removable) return dir;
|
||||
if (fallback == null) fallback = dir;
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
|
||||
private static void find(File dir, int depth, List<File> found) {
|
||||
if (depth > 5 || found.size() >= MAX_FOUND_FILES) return;
|
||||
File[] entries = dir.listFiles();
|
||||
if (entries == null) return;
|
||||
for (File f : entries) {
|
||||
if (found.size() >= MAX_FOUND_FILES) return;
|
||||
if (f.isDirectory()) {
|
||||
if (isCarPlayPath(f.getName())) {
|
||||
File[] inner = f.listFiles();
|
||||
if (inner != null) for (File g : inner) if (g.isFile()) found.add(g);
|
||||
}
|
||||
find(f, depth + 1, found);
|
||||
} else if (isCarPlayPath(f.getName())) {
|
||||
found.add(f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void properties(File out, List<String> notes) {
|
||||
String all = exec(notes, "getprop");
|
||||
if (all == null) return;
|
||||
StringBuilder kept = new StringBuilder();
|
||||
for (String line : all.split("\n")) if (keepProperty(line)) kept.append(line).append('\n');
|
||||
write(new File(out, "properties.txt"), kept.toString());
|
||||
}
|
||||
|
||||
private static void command(File out, String name, List<String> notes, String... argv) {
|
||||
String text = exec(notes, argv);
|
||||
if (text != null) write(new File(out, name), text);
|
||||
}
|
||||
|
||||
private static String exec(List<String> notes, String... argv) {
|
||||
Process p = null;
|
||||
try {
|
||||
p = new ProcessBuilder(argv).redirectErrorStream(true).start();
|
||||
p.getOutputStream().close();
|
||||
byte[] bytes = readAll(p.getInputStream(), 8 << 20);
|
||||
if (!p.waitFor(20, TimeUnit.SECONDS)) notes.add(String.join(" ", argv) + ": timed out");
|
||||
return new String(bytes, StandardCharsets.UTF_8);
|
||||
} catch (Exception e) {
|
||||
notes.add(String.join(" ", argv) + ": " + e);
|
||||
return null;
|
||||
} finally {
|
||||
if (p != null) p.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
private static int copy(File source, File dir, List<String> notes) {
|
||||
if (!source.isFile()) return 0;
|
||||
if (source.length() > MAX_FILE_BYTES) {
|
||||
notes.add("skipped (too large): " + source + " " + source.length());
|
||||
return 0;
|
||||
}
|
||||
if (!dir.isDirectory() && !dir.mkdirs()) {
|
||||
notes.add("no folder for " + source);
|
||||
return 0;
|
||||
}
|
||||
try (InputStream in = new FileInputStream(source);
|
||||
OutputStream os = new FileOutputStream(new File(dir, source.getName()))) {
|
||||
byte[] buffer = new byte[64 * 1024];
|
||||
int n;
|
||||
while ((n = in.read(buffer)) != -1) os.write(buffer, 0, n);
|
||||
return 1;
|
||||
} catch (IOException e) {
|
||||
notes.add("not copied: " + source + ": " + e.getMessage());
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] readAll(InputStream in, int max) throws IOException {
|
||||
java.io.ByteArrayOutputStream buffer = new java.io.ByteArrayOutputStream();
|
||||
byte[] chunk = new byte[16 * 1024];
|
||||
int n;
|
||||
while ((n = in.read(chunk)) != -1 && buffer.size() < max) buffer.write(chunk, 0, n);
|
||||
return buffer.toByteArray();
|
||||
}
|
||||
|
||||
private static void write(File file, String text) {
|
||||
try (OutputStream os = new FileOutputStream(file)) {
|
||||
os.write(text.getBytes(StandardCharsets.UTF_8));
|
||||
} catch (IOException e) {
|
||||
Log.w(TAG, "not written: " + file, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user