Example usage for android.net LocalSocket LocalSocket

List of usage examples for android.net LocalSocket LocalSocket

Introduction

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

Prototype

public LocalSocket() 

Source Link

Document

Creates a AF_LOCAL/UNIX domain stream socket.

Usage

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

/**
 * Bind to mbtool socket//from w w w  . j  av  a 2  s. co m
 *
 * @throws MbtoolException
 */
private static LocalSocket initConnectToSocket() throws MbtoolException {
    try {
        LocalSocket socket = new LocalSocket();
        socket.connect(new LocalSocketAddress(SOCKET_ADDRESS, Namespace.ABSTRACT));
        return socket;
    } catch (IOException e) {
        Log.e(TAG, "Could not connect to mbtool socket", e);
        throw new MbtoolException(Reason.DAEMON_NOT_RUNNING, e);
    }
}

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

/**
 * Connects to the mbtool socket//from w w w  .java  2  s  .com
 *
 * 1. Try connecting to the socket
 * 2. If that fails or if the version is too old, launch bundled mbtool and connect again
 * 3. If that fails, then return false
 *
 * @param context Application context
 */
public synchronized void connect(Context context) throws IOException {
    // If we're already connected, then we're good
    if (mSocket != null) {
        return;
    }

    // Try connecting to the socket
    try {
        mSocket = new LocalSocket();
        mSocket.connect(new LocalSocketAddress(SOCKET_ADDRESS, Namespace.ABSTRACT));
        initializeConnection();
        return;
    } catch (IOException e) {
        Log.e(TAG, "Could not connect to mbtool socket", e);
        disconnect();
    }

    Log.v(TAG, "Launching bundled mbtool");

    if (!executeMbtool(context)) {
        throw new IOException("Failed to execute mbtool");
    }

    // Give mbtool a little bit of time to start listening on the socket
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    try {
        mSocket = new LocalSocket();
        mSocket.connect(new LocalSocketAddress(SOCKET_ADDRESS, Namespace.ABSTRACT));
        initializeConnection();
    } catch (IOException e) {
        disconnect();
        throw new IOException("Could not connect to mbtool socket", e);
    }
}

From source file:com.koushikdutta.superuser.MultitaskSuRequestActivity.java

void manageSocket() {
    new Thread() {
        @Override/*from w w  w . j ava  2  s .c om*/
        public void run() {
            try {
                mSocket = new LocalSocket();
                mSocket.connect(new LocalSocketAddress(mSocketPath, Namespace.FILESYSTEM));

                DataInputStream is = new DataInputStream(mSocket.getInputStream());

                ContentValues payload = new ContentValues();

                for (int i = 0; i < SU_PROTOCOL_PARAM_MAX; i++) {
                    int nameLen = is.readInt();
                    if (nameLen > SU_PROTOCOL_NAME_MAX)
                        throw new IllegalArgumentException("name length too long: " + nameLen);
                    byte[] nameBytes = new byte[nameLen];
                    is.readFully(nameBytes);
                    String name = new String(nameBytes);
                    int dataLen = is.readInt();
                    if (dataLen > getValueMax(name))
                        throw new IllegalArgumentException(name + " data length too long: " + dataLen);
                    byte[] dataBytes = new byte[dataLen];
                    is.readFully(dataBytes);
                    String data = new String(dataBytes);
                    payload.put(name, data);
                    //                        Log.i(LOGTAG, name);
                    //                        Log.i(LOGTAG, data);
                    if ("eof".equals(name))
                        break;
                }

                int protocolVersion = payload.getAsInteger("version");
                mCallerUid = payload.getAsInteger("from.uid");
                mDesiredUid = payload.getAsByte("to.uid");
                mDesiredCmd = payload.getAsString("command");
                String calledBin = payload.getAsString("from.bin");
                mPid = payload.getAsInteger("pid");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mRequestReady = true;
                        requestReady();
                    }
                });

                if ("com.koushikdutta.superuser".equals(getPackageName())) {
                    if (!SuHelper.CURRENT_VERSION.equals(payload.getAsString("binary.version")))
                        SuCheckerReceiver.doNotification(MultitaskSuRequestActivity.this);
                }
            } catch (Exception ex) {
                Log.i(LOGTAG, ex.getMessage(), ex);
                try {
                    mSocket.close();
                } catch (Exception e) {
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        finish();
                    }
                });
            }
        }
    }.start();
}