Exemple #1
0
        /// <summary>
        /// Runs a log service on the <seealso cref="Device"/>, and provides its output to the <seealso cref="LogReceiver"/>.
        /// <p/>This call is blocking until <seealso cref="LogReceiver#isCancelled()"/> returns true. </summary>
        /// <param name="adbSockAddr"> the socket address to connect to adb </param>
        /// <param name="device"> the Device on which to run the service </param>
        /// <param name="logName"> the name of the log file to output </param>
        /// <param name="rcvr"> the <seealso cref="LogReceiver"/> to receive the log output </param>
        /// <exception cref="TimeoutException"> in case of timeout on the connection. </exception>
        /// <exception cref="AdbCommandRejectedException"> if adb rejects the command </exception>
        /// <exception cref="IOException"> in case of I/O error on the connection. </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public static void runLogService(java.net.InetSocketAddress adbSockAddr, Device device, String logName, com.android.ddmlib.log.LogReceiver rcvr) throws TimeoutException, AdbCommandRejectedException, java.io.IOException
        public static void runLogService(EndPoint adbSockAddr, Device device, string logName, LogReceiver rcvr)
        {
            SocketChannel adbChan = null;

            try
            {
                adbChan = SocketChannel.open(adbSockAddr);
                adbChan.configureBlocking(false);

                // if the device is not -1, then we first tell adb we're looking to talk
                // to a specific device
                setDevice(adbChan, device);

                var request = formAdbRequest("log:" + logName);
                write(adbChan, request);

                AdbResponse resp = readAdbResponse(adbChan, false);                 // readDiagString
                if (resp.okay == false)
                {
                    throw new AdbCommandRejectedException(resp.message);
                }

                var        data = new byte[16384];
                ByteBuffer buf  = ByteBuffer.wrap(data);
                while (true)
                {
                    int count;

                    if (rcvr != null && rcvr.cancelled)
                    {
                        break;
                    }

                    count = adbChan.read(buf);
                    if (count < 0)
                    {
                        break;
                    }
                    else if (count == 0)
                    {
                        Thread.Sleep(WAIT_TIME * 5);
                    }
                    else
                    {
                        if (rcvr != null)
                        {
                            rcvr.parseNewData(buf.array(), buf.arrayOffset(), buf.position);
                        }
                        buf.rewind();
                    }
                }
            }
            finally
            {
                if (adbChan != null)
                {
                    adbChan.close();
                }
            }
        }