FileDocCategorySizeDatePackage
ViewServer.javaAPI DocAndroid 5.1 API11404Thu Mar 12 22:22:42 GMT 2015com.android.server.wm

ViewServer

public class ViewServer extends Object implements Runnable
The ViewServer is local socket server that can be used to communicate with the views of the opened windows. Communication with the views is ensured by the {@link com.android.server.wm.WindowManagerService} and is a cross-process operation. {@hide}

Fields Summary
public static final int
VIEW_SERVER_DEFAULT_PORT
The default port used to start view servers.
private static final int
VIEW_SERVER_MAX_CONNECTIONS
private static final String
LOG_TAG
private static final String
VALUE_PROTOCOL_VERSION
private static final String
VALUE_SERVER_VERSION
private static final String
COMMAND_PROTOCOL_VERSION
private static final String
COMMAND_SERVER_VERSION
private static final String
COMMAND_WINDOW_MANAGER_LIST
private static final String
COMMAND_WINDOW_MANAGER_AUTOLIST
private static final String
COMMAND_WINDOW_MANAGER_GET_FOCUS
private ServerSocket
mServer
private Thread
mThread
private final WindowManagerService
mWindowManager
private final int
mPort
private ExecutorService
mThreadPool
Constructors Summary
ViewServer(WindowManagerService windowManager, int port)
Creates a new ViewServer associated with the specified window manager on the specified local port. The server is not started by default.

param
windowManager The window manager used to communicate with the views.
param
port The port for the server to listen to.
see
#start()


                                                      
        
        mWindowManager = windowManager;
        mPort = port;
    
Methods Summary
booleanisRunning()
Indicates whether the server is currently running.

return
True if the server is running, false otherwise.
see
#start()
see
#stop()
see
WindowManagerService#isViewServerRunning()

        return mThread != null && mThread.isAlive();
    
public voidrun()
Main server loop.

        while (Thread.currentThread() == mThread) {
            // Any uncaught exception will crash the system process
            try {
                Socket client = mServer.accept();
                if (mThreadPool != null) {
                    mThreadPool.submit(new ViewServerWorker(client));
                } else {
                    try {
                        client.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            } catch (Exception e) {
                Slog.w(LOG_TAG, "Connection error: ", e);
            }
        }
    
booleanstart()
Starts the server.

return
True if the server was successfully created, or false if it already exists.
throws
IOException If the server cannot be created.
see
#stop()
see
#isRunning()
see
WindowManagerService#startViewServer(int)

        if (mThread != null) {
            return false;
        }

        mServer = new ServerSocket(mPort, VIEW_SERVER_MAX_CONNECTIONS, InetAddress.getLocalHost());
        mThread = new Thread(this, "Remote View Server [port=" + mPort + "]");
        mThreadPool = Executors.newFixedThreadPool(VIEW_SERVER_MAX_CONNECTIONS);
        mThread.start();

        return true;
    
booleanstop()
Stops the server.

return
True if the server was stopped, false if an error occured or if the server wasn't started.
see
#start()
see
#isRunning()
see
WindowManagerService#stopViewServer()

        if (mThread != null) {

            mThread.interrupt();
            if (mThreadPool != null) {
                try {
                    mThreadPool.shutdownNow();
                } catch (SecurityException e) {
                    Slog.w(LOG_TAG, "Could not stop all view server threads");
                }
            }
            mThreadPool = null;
            mThread = null;
            try {
                mServer.close();
                mServer = null;
                return true;
            } catch (IOException e) {
                Slog.w(LOG_TAG, "Could not close the view server");
            }
        }
        return false;
    
private static booleanwriteValue(java.net.Socket client, java.lang.String value)

        boolean result;
        BufferedWriter out = null;
        try {
            OutputStream clientStream = client.getOutputStream();
            out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
            out.write(value);
            out.write("\n");
            out.flush();
            result = true;
        } catch (Exception e) {
            result = false;
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    result = false;
                }
            }
        }
        return result;