Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3a56fe50 authored by Simon Chan's avatar Simon Chan
Browse files

feat(bin): add initial bugreportz support

parent 67738d20
Loading
Loading
Loading
Loading
+0 −5
Original line number Diff line number Diff line
@@ -86,11 +86,6 @@ export function parsePrivateKey(key: ArrayBuffer): [n: bigint, d: bigint] {
// I can't understand, but it does work
// Only used with numbers less than 2^32 so doesn't need BigInt
export function modInverse(a: number, m: number) {
    // validate inputs
    [a, m] = [Number(a), Number(m)];
    if (Number.isNaN(a) || Number.isNaN(m)) {
        return NaN; // invalid input
    }
    a = (a % m + m) % m;
    if (!a || m < 2) {
        return NaN; // invalid input
+7 −3
Original line number Diff line number Diff line
import { once } from '@yume-chan/event';
import { ValueOrPromise } from '@yume-chan/struct';
import { AdbSocket, AdbSocketInfo } from '../socket';
import { EventQueue } from '../utils';
import { EventQueue, EventQueueEndedError } from '../utils';
import { StreamEndedError } from "./buffered-stream";

export class AdbSocketStream implements AdbSocketInfo {
@@ -36,9 +36,13 @@ export class AdbSocketStream implements AdbSocketInfo {
    public async read(): Promise<ArrayBuffer> {
        try {
            return await this.queue.dequeue();
        } catch {
        } catch (e) {
            if (e instanceof EventQueueEndedError) {
                throw new StreamEndedError();
            }

            throw e;
        }
    }

    public write(data: ArrayBuffer): Promise<void> {
+10 −1
Original line number Diff line number Diff line
@@ -58,6 +58,15 @@ class LinkedList<T>{
    }
}

export class EventQueueEndedError extends Error {
    public constructor() {
        super('Event queue ended');

        // Fix Error's prototype chain when compiling to ES5
        Object.setPrototypeOf(this, new.target.prototype);
    }
}

/**
 * Basically an object-mode ReadableStream with Promise-based API
 *
@@ -123,7 +132,7 @@ export class EventQueue<T> {
        }

        if (this.ended) {
            return Promise.reject(new Error('The EventQueue has already ended'));
            return Promise.reject(new EventQueueEndedError());
        }

        if (this.awaiters.length === this.options.maxWaitCount - 1) {
+23 −1
Original line number Diff line number Diff line
// cspell: ignore bugreport
// cspell: ignore bugreportz

import { AdbCommandBase, AdbShellProtocol } from "@yume-chan/adb";
import { AdbCommandBase, AdbShellProtocol, EventQueue, EventQueueEndedError } from "@yume-chan/adb";

export interface BugReportVersion {
    major: number;
@@ -32,4 +32,26 @@ export class BugReport extends AdbCommandBase {
            minor: parseInt(match[2]!, 10),
        };
    }

    public bugReportZSupportStream(version: BugReportVersion): boolean {
        return version.major > 1 || version.minor >= 2;
    }

    public async *bugReportZStream(): AsyncGenerator<ArrayBuffer, void, void> {
        const process = await this.adb.childProcess.spawn(['bugreportz', '-s']);
        const queue = new EventQueue<ArrayBuffer>();
        process.onStdout(buffer => queue.enqueue(buffer));
        process.onExit(() => queue.end());
        try {
            while (true) {
                yield await queue.dequeue();
            }
        } catch (e) {
            if (e instanceof EventQueueEndedError) {
                return;
            }

            throw e;
        }
    }
}
+18 −1
Original line number Diff line number Diff line
@@ -2,10 +2,11 @@ import { AdbCommandBase } from "@yume-chan/adb";

export type SettingsNamespace = 'system' | 'secure' | 'global';

export type SettingsResetMode = 'untrusted_defaults' | 'untrusted_clear' | 'trusted_defaults';

// frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/SettingsService.java
export class Settings extends AdbCommandBase {
    // TODO: `--user <user>` argument
    // TODO: `reset` command

    public base(command: string, namespace: SettingsNamespace, ...args: string[]) {
        return this.adb.childProcess.spawnAndWaitLegacy(['settings', command, namespace, ...args]);
@@ -29,4 +30,20 @@ export class Settings extends AdbCommandBase {
            ...(makeDefault ? ['default'] : []),
        );
    }

    public reset(namespace: SettingsNamespace, mode: SettingsResetMode): Promise<string>;
    public reset(namespace: SettingsNamespace, packageName: string, tag?: string): Promise<string>;
    public reset(namespace: SettingsNamespace, modeOrPackageName: string, tag?: string): Promise<string> {
        return this.base(
            'reset',
            namespace,
            modeOrPackageName,
            ...(tag ? [tag] : []),
        );
    }

    public async list(namespace: SettingsNamespace): Promise<string[]> {
        const output = await this.base('list', namespace);
        return output.split('\n');
    }
}