Example usage for android.net LocalSocket getOutputStream

List of usage examples for android.net LocalSocket getOutputStream

Introduction

In this page you can find the example usage for android.net LocalSocket getOutputStream.

Prototype

public OutputStream getOutputStream() throws IOException 

Source Link

Document

Retrieves the output stream for this instance.

Usage

From source file:com.github.chenxiaolong.dualbootpatcher.socket.MbtoolConnection.java

public static boolean replaceViaSignedExec(Context context) {
    ThreadUtils.enforceExecutionOnNonMainThread();

    PatcherUtils.extractPatcher(context);

    String abi = getAbi();//from  ww  w.  j ava  2 s.  c om

    File mbtool = new File(PatcherUtils.getTargetDirectory(context) + File.separator + "binaries"
            + File.separator + "android" + File.separator + abi + File.separator + "mbtool");
    File mbtoolSig = new File(PatcherUtils.getTargetDirectory(context) + File.separator + "binaries"
            + File.separator + "android" + File.separator + abi + File.separator + "mbtool.sig");

    for (int i = SIGNED_EXEC_MAX_PROTOCOL_VERSION; i >= SIGNED_EXEC_MIN_PROTOCOL_VERSION; i--) {
        LocalSocket socket = null;
        InputStream socketIS = null;
        OutputStream socketOS = null;

        try {
            // Try connecting to the socket
            socket = initConnectToSocket();
            socketIS = socket.getInputStream();
            socketOS = socket.getOutputStream();

            // mbtool will immediately send a message telling us whether the app signature is
            // allowed or denied
            initVerifyCredentials(socketIS);

            // Request interface version
            initRequestInterface(socketIS, socketOS, i);

            // Create interface
            MbtoolInterface iface = createInterface(socketIS, socketOS, i);
            if (iface == null) {
                throw new IllegalStateException("Failed to create interface for version: " + i);
            }

            // Use signed exec to replace mbtool. This purposely sets argv[0] to "mbtool" and
            // argv[1] to "daemon" instead of just setting argv[0] to "daemon" because --replace
            // kills processes with cmdlines matching the former case.
            SignedExecCompletion completion = iface.signedExec(mbtool.getAbsolutePath(),
                    mbtoolSig.getAbsolutePath(), "mbtool",
                    new String[] { "daemon", "--replace", "--daemonize" }, null);

            switch (completion.result) {
            case SignedExecResult.PROCESS_EXITED:
                Log.d(TAG, "mbtool signed exec exited with status: " + completion.exitStatus);
                return completion.exitStatus == 0;
            case SignedExecResult.PROCESS_KILLED_BY_SIGNAL:
                Log.d(TAG, "mbtool signed exec killed by signal: " + completion.termSig);
                return false;
            case SignedExecResult.INVALID_SIGNATURE:
                Log.d(TAG, "mbtool signed exec failed due to invalid signature");
                return false;
            case SignedExecResult.OTHER_ERROR:
            default:
                Log.d(TAG, "mbtool signed exec failed: " + completion.errorMsg);
                return false;
            }
        } catch (IOException e) {
            // Keep trying
            Log.w(TAG, "mbtool connection error", e);
        } catch (MbtoolException e) {
            // Keep trying unless the daemon is not running or if the signature check fails
            Log.w(TAG, "mbtool error", e);
            if (e.getReason() == Reason.DAEMON_NOT_RUNNING || e.getReason() == Reason.SIGNATURE_CHECK_FAIL) {
                break;
            }
        } catch (MbtoolCommandException e) {
            // Keep trying
            Log.w(TAG, "mbtool command error", e);
        } finally {
            IOUtils.closeQuietly(socketIS);
            IOUtils.closeQuietly(socketOS);
            IOUtils.closeQuietly(socket);
        }
    }

    return false;
}