ddmlib

16:35:01.812 INFO  jd.cli.Main - Decompiling ddmlib.jar
package com.android.ddmlib;

public class AdbCommandRejectedException
  extends Exception
{
  private static final long serialVersionUID = 1L;
  private final boolean mIsDeviceOffline;
  private final boolean mErrorDuringDeviceSelection;
  
  AdbCommandRejectedException(String message)
  {
    super(message);
    mIsDeviceOffline = "device offline".equals(message);
    mErrorDuringDeviceSelection = false;
  }
  
  AdbCommandRejectedException(String message, boolean errorDuringDeviceSelection)
  {
    super(message);
    mErrorDuringDeviceSelection = errorDuringDeviceSelection;
    mIsDeviceOffline = "device offline".equals(message);
  }
  
  public boolean isDeviceOffline()
  {
    return mIsDeviceOffline;
  }
  
  public boolean wasErrorDuringDeviceSelection()
  {
    return mErrorDuringDeviceSelection;
  }
}

/* Location:
 * Qualified Name:     com.android.ddmlib.AdbCommandRejectedException
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package com.android.ddmlib;

class AdbHelper$AdbResponse
{
  public boolean okay;
  public String message;
  
  public AdbHelper$AdbResponse()
  {
    message = "";
  }
}

/* Location:
 * Qualified Name:     com.android.ddmlib.AdbHelper.AdbResponse
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package com.android.ddmlib;

import com.android.ddmlib.log.LogReceiver;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.SocketChannel;
import java.util.concurrent.TimeUnit;

final class AdbHelper
{
  static final int WAIT_TIME = 5;
  static final String DEFAULT_ENCODING = "ISO-8859-1";
  
  static class AdbResponse
  {
    public boolean okay;
    public String message;
    
    public AdbResponse()
    {
      message = "";
    }
  }
  
  public static SocketChannel open(InetSocketAddress adbSockAddr, Device device, int devicePort)
    throws IOException, TimeoutException, AdbCommandRejectedException
  {
    SocketChannel adbChan = SocketChannel.open(adbSockAddr);
    try
    {
      adbChan.socket().setTcpNoDelay(true);
      adbChan.configureBlocking(false);
      
      setDevice(adbChan, device);
      
      byte[] req = createAdbForwardRequest(null, devicePort);
      
      write(adbChan, req);
      
      AdbResponse resp = readAdbResponse(adbChan, false);
      if (!okay) {
        throw new AdbCommandRejectedException(message);
      }
      adbChan.configureBlocking(true);
    }
    catch (TimeoutException e)
    {
      adbChan.close();
      throw e;
    }
    catch (IOException e)
    {
      adbChan.close();
      throw e;
    }
    return adbChan;
  }
  
  public static SocketChannel createPassThroughConnection(InetSocketAddress adbSockAddr, Device device, int pid)
    throws TimeoutException, AdbCommandRejectedException, IOException
  {
    SocketChannel adbChan = SocketChannel.open(adbSockAddr);
    try
    {
      adbChan.socket().setTcpNoDelay(true);
      adbChan.configureBlocking(false);
      
      setDevice(adbChan, device);
      
      byte[] req = createJdwpForwardRequest(pid);
      
      write(adbChan, req);
      
      AdbResponse resp = readAdbResponse(adbChan, false);
      if (!okay) {
        throw new AdbCommandRejectedException(message);
      }
      adbChan.configureBlocking(true);
    }
    catch (TimeoutException e)
    {
      adbChan.close();
      throw e;
    }
    catch (IOException e)
    {
      adbChan.close();
      throw e;
    }
    return adbChan;
  }
  
  private static byte[] createAdbForwardRequest(String addrStr, int port)
  {
    String reqStr;
    String reqStr;
    if (addrStr == null) {
      reqStr = "tcp:" + port;
    } else {
      reqStr = "tcp:" + port + ":" + addrStr;
    }
    return formAdbRequest(reqStr);
  }
  
  private static byte[] createJdwpForwardRequest(int pid)
  {
    String reqStr = String.format("jdwp:%1$d", new Object[] { Integer.valueOf(pid) });
    return formAdbRequest(reqStr);
  }
  
  static byte[] formAdbRequest(String req)
  {
    String resultStr = String.format("%04X%s", new Object[] { Integer.valueOf(req.length()), req });
    byte[] result;
    try
    {
      result = resultStr.getBytes("ISO-8859-1");
    }
    catch (UnsupportedEncodingException uee)
    {
      uee.printStackTrace();
      return null;
    }
    assert (result.length == req.length() + 4);
    return result;
  }
  
  static AdbResponse readAdbResponse(SocketChannel chan, boolean readDiagString)
    throws TimeoutException, IOException
  {
    AdbResponse resp = new AdbResponse();
    
    byte[] reply = new byte[4];
    read(chan, reply);
    if (isOkay(reply))
    {
      okay = true;
    }
    else
    {
      readDiagString = true;
      okay = false;
    }
    label246:
    try
    {
      if (readDiagString)
      {
        byte[] lenBuf = new byte[4];
        read(chan, lenBuf);
        
        String lenStr = replyToString(lenBuf);
        int len;
        try
        {
          len = Integer.parseInt(lenStr, 16);
        }
        catch (NumberFormatException nfe)
        {
          Log.w("ddms", "Expected digits, got '" + lenStr + "': " + lenBuf[0] + " " + lenBuf[1] + " " + lenBuf[2] + " " + lenBuf[3]);
          
          Log.w("ddms", "reply was " + replyToString(reply));
          break label246;
        }
        byte[] msg = new byte[len];
        read(chan, msg);
        
        message = replyToString(msg);
        Log.v("ddms", "Got reply '" + replyToString(reply) + "', diag='" + message + "'");
      }
    }
    catch (Exception e) {}
    return resp;
  }
  
  static RawImage getFrameBuffer(InetSocketAddress adbSockAddr, Device device)
    throws TimeoutException, AdbCommandRejectedException, IOException
  {
    RawImage imageParams = new RawImage();
    byte[] request = formAdbRequest("framebuffer:");
    byte[] nudge = { 0 };
    
    SocketChannel adbChan = null;
    try
    {
      adbChan = SocketChannel.open(adbSockAddr);
      adbChan.configureBlocking(false);
      
      setDevice(adbChan, device);
      
      write(adbChan, request);
      
      AdbResponse resp = readAdbResponse(adbChan, false);
      if (!okay) {
        throw new AdbCommandRejectedException(message);
      }
      byte[] reply = new byte[4];
      read(adbChan, reply);
      
      ByteBuffer buf = ByteBuffer.wrap(reply);
      buf.order(ByteOrder.LITTLE_ENDIAN);
      
      int version = buf.getInt();
      
      int headerSize = RawImage.getHeaderSize(version);
      
      reply = new byte[headerSize * 4];
      read(adbChan, reply);
      
      buf = ByteBuffer.wrap(reply);
      buf.order(ByteOrder.LITTLE_ENDIAN);
      if (!imageParams.readHeader(version, buf))
      {
        Log.e("Screenshot", "Unsupported protocol: " + version);
        return null;
      }
      Log.d("ddms", "image params: bpp=" + bpp + ", size=" + size + ", width=" + width + ", height=" + height);
      
      write(adbChan, nudge);
      
      reply = new byte[size];
      read(adbChan, reply);
      
      data = reply;
    }
    finally
    {
      if (adbChan != null) {
        adbChan.close();
      }
    }
    return imageParams;
  }
  
  @Deprecated
  static void executeRemoteCommand(InetSocketAddress adbSockAddr, String command, IDevice device, IShellOutputReceiver rcvr, int maxTimeToOutputResponse)
    throws TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException, IOException
  {
    executeRemoteCommand(adbSockAddr, command, device, rcvr, maxTimeToOutputResponse, TimeUnit.MILLISECONDS);
  }
  
  static void executeRemoteCommand(InetSocketAddress adbSockAddr, String command, IDevice device, IShellOutputReceiver rcvr, long maxTimeToOutputResponse, TimeUnit maxTimeUnits)
    throws TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException, IOException
  {
    long maxTimeToOutputMs = 0L;
    if (maxTimeToOutputResponse > 0L)
    {
      if (maxTimeUnits == null) {
        throw new NullPointerException("Time unit must not be null for non-zero max.");
      }
      maxTimeToOutputMs = maxTimeUnits.toMillis(maxTimeToOutputResponse);
    }
    Log.v("ddms", "execute: running " + command);
    
    SocketChannel adbChan = null;
    try
    {
      adbChan = SocketChannel.open(adbSockAddr);
      adbChan.configureBlocking(false);
      
      setDevice(adbChan, device);
      
      byte[] request = formAdbRequest("shell:" + command);
      write(adbChan, request);
      
      AdbResponse resp = readAdbResponse(adbChan, false);
      if (!okay)
      {
        Log.e("ddms", "ADB rejected shell command (" + command + "): " + message);
        throw new AdbCommandRejectedException(message);
      }
      byte[] data = new byte['?'];
      ByteBuffer buf = ByteBuffer.wrap(data);
      long timeToResponseCount = 0L;
      for (;;)
      {
        if ((rcvr != null) && (rcvr.isCancelled()))
        {
          Log.v("ddms", "execute: cancelled");
          break;
        }
        int count = adbChan.read(buf);
        if (count < 0)
        {
          rcvr.flush();
          Log.v("ddms", "execute '" + command + "' on '" + device + "' : EOF hit. Read: " + count);
          
          break;
        }
        if (count == 0)
        {
          try
          {
            int wait = 25;
            timeToResponseCount += wait;
            if ((maxTimeToOutputMs > 0L) && (timeToResponseCount > maxTimeToOutputMs)) {
              throw new ShellCommandUnresponsiveException();
            }
            Thread.sleep(wait);
          }
          catch (InterruptedException ie) {}
        }
        else
        {
          timeToResponseCount = 0L;
          if (rcvr != null) {
            rcvr.addOutput(buf.array(), buf.arrayOffset(), buf.position());
          }
          buf.rewind();
        }
      }
    }
    finally
    {
      if (adbChan != null) {
        adbChan.close();
      }
      Log.v("ddms", "execute: returning");
    }
  }
  
  public static void runEventLogService(InetSocketAddress adbSockAddr, Device device, LogReceiver rcvr)
    throws TimeoutException, AdbCommandRejectedException, IOException
  {
    runLogService(adbSockAddr, device, "events", rcvr);
  }
  
  public static void runLogService(InetSocketAddress adbSockAddr, Device device, String logName, LogReceiver rcvr)
    throws TimeoutException, AdbCommandRejectedException, IOException
  {
    SocketChannel adbChan = null;
    try
    {
      adbChan = SocketChannel.open(adbSockAddr);
      adbChan.configureBlocking(false);
      
      setDevice(adbChan, device);
      
      byte[] request = formAdbRequest("log:" + logName);
      write(adbChan, request);
      
      AdbResponse resp = readAdbResponse(adbChan, false);
      if (!okay) {
        throw new AdbCommandRejectedException(message);
      }
      byte[] data = new byte['?'];
      ByteBuffer buf = ByteBuffer.wrap(data);
      while ((rcvr == null) || (!rcvr.isCancelled()))
      {
        int count = adbChan.read(buf);
        if (count < 0) {
          break;
        }
        if (count == 0)
        {
          try
          {
            Thread.sleep(25L);
          }
          catch (InterruptedException ie) {}
        }
        else
        {
          if (rcvr != null) {
            rcvr.parseNewData(buf.array(), buf.arrayOffset(), buf.position());
          }
          buf.rewind();
        }
      }
    }
    finally
    {
      if (adbChan != null) {
        adbChan.close();
      }
    }
  }
  
  public static void createForward(InetSocketAddress adbSockAddr, Device device, String localPortSpec, String remotePortSpec)
    throws TimeoutException, AdbCommandRejectedException, IOException
  {
    SocketChannel adbChan = null;
    try
    {
      adbChan = SocketChannel.open(adbSockAddr);
      adbChan.configureBlocking(false);
      
      byte[] request = formAdbRequest(String.format("host-serial:%1$s:forward:%2$s;%3$s", new Object[] { device.getSerialNumber(), localPortSpec, remotePortSpec }));
      
      write(adbChan, request);
      
      AdbResponse resp = readAdbResponse(adbChan, false);
      if (!okay)
      {
        Log.w("create-forward", "Error creating forward: " + message);
        throw new AdbCommandRejectedException(message);
      }
    }
    finally
    {
      if (adbChan != null) {
        adbChan.close();
      }
    }
  }
  
  public static void removeForward(InetSocketAddress adbSockAddr, Device device, String localPortSpec, String remotePortSpec)
    throws TimeoutException, AdbCommandRejectedException, IOException
  {
    SocketChannel adbChan = null;
    try
    {
      adbChan = SocketChannel.open(adbSockAddr);
      adbChan.configureBlocking(false);
      
      byte[] request = formAdbRequest(String.format("host-serial:%1$s:killforward:%2$s", new Object[] { device.getSerialNumber(), localPortSpec }));
      
      write(adbChan, request);
      
      AdbResponse resp = readAdbResponse(adbChan, false);
      if (!okay)
      {
        Log.w("remove-forward", "Error creating forward: " + message);
        throw new AdbCommandRejectedException(message);
      }
    }
    finally
    {
      if (adbChan != null) {
        adbChan.close();
      }
    }
  }
  
  static boolean isOkay(byte[] reply)
  {
    return (reply[0] == 79) && (reply[1] == 75) && (reply[2] == 65) && (reply[3] == 89);
  }
  
  static String replyToString(byte[] reply)
  {
    String result;
    try
    {
      result = new String(reply, "ISO-8859-1");
    }
    catch (UnsupportedEncodingException uee)
    {
      uee.printStackTrace();
      result = "";
    }
    return result;
  }
  
  static void read(SocketChannel chan, byte[] data)
    throws TimeoutException, IOException
  {
    read(chan, data, -1, DdmPreferences.getTimeOut());
  }
  
  static void read(SocketChannel chan, byte[] data, int length, int timeout)
    throws TimeoutException, IOException
  {
    ByteBuffer buf = ByteBuffer.wrap(data, 0, length != -1 ? length : data.length);
    int numWaits = 0;
    while (buf.position() != buf.limit())
    {
      int count = chan.read(buf);
      if (count < 0)
      {
        Log.d("ddms", "read: channel EOF");
        throw new IOException("EOF");
      }
      if (count == 0)
      {
        if ((timeout != 0) && (numWaits * 5 > timeout))
        {
          Log.d("ddms", "read: timeout");
          throw new TimeoutException();
        }
        try
        {
          Thread.sleep(5L);
        }
        catch (InterruptedException ie) {}
        numWaits++;
      }
      else
      {
        numWaits = 0;
      }
    }
  }
  
  static void write(SocketChannel chan, byte[] data)
    throws TimeoutException, IOException
  {
    write(chan, data, -1, DdmPreferences.getTimeOut());
  }
  
  static void write(SocketChannel chan, byte[] data, int length, int timeout)
    throws TimeoutException, IOException
  {
    ByteBuffer buf = ByteBuffer.wrap(data, 0, length != -1 ? length : data.length);
    int numWaits = 0;
    while (buf.position() != buf.limit())
    {
      int count = chan.write(buf);
      if (count < 0)
      {
        Log.d("ddms", "write: channel EOF");
        throw new IOException("channel EOF");
      }
      if (count == 0)
      {
        if ((timeout != 0) && (numWaits * 5 > timeout))
        {
          Log.d("ddms", "write: timeout");
          throw new TimeoutException();
        }
        try
        {
          Thread.sleep(5L);
        }
        catch (InterruptedException ie) {}
        numWaits++;
      }
      else
      {
        numWaits = 0;
      }
    }
  }
  
  static void setDevice(SocketChannel adbChan, IDevice device)
    throws TimeoutException, AdbCommandRejectedException, IOException
  {
    if (device != null)
    {
      String msg = "host:transport:" + device.getSerialNumber();
      byte[] device_query = formAdbRequest(msg);
      
      write(adbChan, device_query);
      
      AdbResponse resp = readAdbResponse(adbChan, false);
      if (!okay) {
        throw new AdbCommandRejectedException(message, true);
      }
    }
  }
  
  public static void reboot(String into, InetSocketAddress adbSockAddr, Device device)
    throws TimeoutException, AdbCommandRejectedException, IOException
  {
    byte[] request;
    byte[] request;
    if (into == null) {
      request = formAdbRequest("reboot:");
    } else {
      request = formAdbRequest("reboot:" + into);
    }
    SocketChannel adbChan = null;
    try
    {
      adbChan = SocketChannel.open(adbSockAddr);
      adbChan.configureBlocking(false);
      
      setDevice(adbChan, device);
      
      write(adbChan, request);
    }
    finally
    {
      if (adbChan != null) {
        adbChan.close();
      }
    }
  }
}

/* Location:
 * Qualified Name:     com.android.ddmlib.AdbHelper
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package com.android.ddmlib;

class AllocationInfo$1 {}

/* Location:
 * Qualified Name:     com.android.ddmlib.AllocationInfo.1
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package com.android.ddmlib;

import java.util.Comparator;

public final class AllocationInfo$AllocationSorter
  implements Comparator<AllocationInfo>
{
  private AllocationInfo.SortMode mSortMode = AllocationInfo.SortMode.SIZE;
  private boolean mDescending = true;
  
  public void setSortMode(AllocationInfo.SortMode mode)
  {
    if (mSortMode == mode) {
      mDescending = (!mDescending);
    } else {
      mSortMode = mode;
    }
  }
  
  public AllocationInfo.SortMode getSortMode()
  {
    return mSortMode;
  }
  
  public boolean isDescending()
  {
    return mDescending;
  }
  
  public int compare(AllocationInfo o1, AllocationInfo o2)
  {
    int diff = 0;
    switch (AllocationInfo.1.$SwitchMap$com$android$ddmlib$AllocationInfo$SortMode[mSortMode.ordinal()])
    {
    case 1: 
      diff = AllocationInfo.access$000(o1) - AllocationInfo.access$000(o2);
      break;
    case 2: 
      break;
    case 3: 
      diff = AllocationInfo.access$100(o1).compareTo(AllocationInfo.access$100(o2));
      break;
    case 4: 
      diff = AllocationInfo.access$200(o1) - AllocationInfo.access$200(o2);
      break;
    case 5: 
      String class1 = o1.getFirstTraceClassName();
      String class2 = o2.getFirstTraceClassName();
      diff = compareOptionalString(class1, class2);
      break;
    case 6: 
      String method1 = o1.getFirstTraceMethodName();
      String method2 = o2.getFirstTraceMethodName();
      diff = compareOptionalString(method1, method2);
    }
    if (diff == 0) {
      diff = AllocationInfo.access$300(o1) - AllocationInfo.access$300(o2);
    }
    if (mDescending) {
      diff = -diff;
    }
    return diff;
  }
  
  private int compareOptionalString(String str1, String str2)
  {
    if (str1 != null)
    {
      if (str2 == null) {
        return -1;
      }
      return str1.compareTo(str2);
    }
    if (str2 == null) {
      return 0;
    }
    return 1;
  }
}

/* Location:
 * Qualified Name:     com.android.ddmlib.AllocationInfo.AllocationSorter
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package com.android.ddmlib;

public enum AllocationInfo$SortMode
{
  NUMBER,  SIZE,  CLASS,  THREAD,  IN_CLASS,  IN_METHOD;
  
  private AllocationInfo$SortMode() {}
}

/* Location:
 * Qualified Name:     com.android.ddmlib.AllocationInfo.SortMode
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package com.android.ddmlib;

import java.util.Comparator;
import java.util.Locale;

public class AllocationInfo
  implements IStackTraceInfo
{
  private final String mAllocatedClass;
  private final int mAllocNumber;
  private final int mAllocationSize;
  private final short mThreadId;
  private final StackTraceElement[] mStackTrace;
  
  public static enum SortMode
  {
    NUMBER,  SIZE,  CLASS,  THREAD,  IN_CLASS,  IN_METHOD;
    
    private SortMode() {}
  }
  
  public static final class AllocationSorter
    implements Comparator<AllocationInfo>
  {
    private AllocationInfo.SortMode mSortMode = AllocationInfo.SortMode.SIZE;
    private boolean mDescending = true;
    
    public void setSortMode(AllocationInfo.SortMode mode)
    {
      if (mSortMode == mode) {
        mDescending = (!mDescending);
      } else {
        mSortMode = mode;
      }
    }
    
    public AllocationInfo.SortMode getSortMode()
    {
      return mSortMode;
    }
    
    public boolean isDescending()
    {
      return mDescending;
    }
    
    public int compare(AllocationInfo o1, AllocationInfo o2)
    {
      int diff = 0;
      switch (AllocationInfo.1.$SwitchMap$com$android$ddmlib$AllocationInfo$SortMode[mSortMode.ordinal()])
      {
      case 1: 
        diff = mAllocNumber - mAllocNumber;
        break;
      case 2: 
        break;
      case 3: 
        diff = mAllocatedClass.compareTo(mAllocatedClass);
        break;
      case 4: 
        diff = mThreadId - mThreadId;
        break;
      case 5: 
        String class1 = o1.getFirstTraceClassName();
        String class2 = o2.getFirstTraceClassName();
        diff = compareOptionalString(class1, class2);
        break;
      case 6: 
        String method1 = o1.getFirstTraceMethodName();
        String method2 = o2.getFirstTraceMethodName();
        diff = compareOptionalString(method1, method2);
      }
      if (diff == 0) {
        diff = mAllocationSize - mAllocationSize;
      }
      if (mDescending) {
        diff = -diff;
      }
      return diff;
    }
    
    private int compareOptionalString(String str1, String str2)
    {
      if (str1 != null)
      {
        if (str2 == null) {
          return -1;
        }
        return str1.compareTo(str2);
      }
      if (str2 == null) {
        return 0;
      }
      return 1;
    }
  }
  
  AllocationInfo(int allocNumber, String allocatedClass, int allocationSize, short threadId, StackTraceElement[] stackTrace)
  {
    mAllocNumber = allocNumber;
    mAllocatedClass = allocatedClass;
    mAllocationSize = allocationSize;
    mThreadId = threadId;
    mStackTrace = stackTrace;
  }
  
  public int getAllocNumber()
  {
    return mAllocNumber;
  }
  
  public String getAllocatedClass()
  {
    return mAllocatedClass;
  }
  
  public int getSize()
  {
    return mAllocationSize;
  }
  
  public short getThreadId()
  {
    return mThreadId;
  }
  
  public StackTraceElement[] getStackTrace()
  {
    return mStackTrace;
  }
  
  public int compareTo(AllocationInfo otherAlloc)
  {
    return mAllocationSize - mAllocationSize;
  }
  
  public String getFirstTraceClassName()
  {
    if (mStackTrace.length > 0) {
      return mStackTrace[0].getClassName();
    }
    return null;
  }
  
  public String getFirstTraceMethodName()
  {
    if (mStackTrace.length > 0) {
      return mStackTrace[0].getMethodName();
    }
    return null;
  }
  
  public boolean filter(String filter, boolean fullTrace, Locale locale)
  {
    if (mAllocatedClass.toLowerCase(locale).contains(filter)) {
      return true;
    }
    if (mStackTrace.length > 0)
    {
      int length = fullTrace ? mStackTrace.length : 1;
      for (int i = 0; i < length; i++)
      {
        if (mStackTrace[i].getClassName().toLowerCase(locale).contains(filter)) {
          return true;
        }
        if (mStackTrace[i].getMethodName().toLowerCase(locale).contains(filter)) {
          return true;
        }
      }
    }
    return false;
  }
}

/* Location:
 * Qualified Name:     com.android.ddmlib.AllocationInfo
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package com.android.ddmlib;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

class AndroidDebugBridge$1
  extends Thread
{
  AndroidDebugBridge$1(AndroidDebugBridge paramAndroidDebugBridge, String x0, Process paramProcess, ArrayList paramArrayList)
  {
    super(x0);
  }
  
  public void run()
  {
    InputStreamReader is = new InputStreamReader(val$process.getErrorStream());
    BufferedReader errReader = new BufferedReader(is);
    try
    {
      for (;;)
      {
        String line = errReader.readLine();
        if (line == null) {
          break;
        }
        Log.e("adb", line);
        val$errorOutput.add(line);
      }
    }
    catch (IOException e) {}
  }
}

/* Location:
 * Qualified Name:     com.android.ddmlib.AndroidDebugBridge.1
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package com.android.ddmlib;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

class AndroidDebugBridge$2
  extends Thread
{
  AndroidDebugBridge$2(AndroidDebugBridge paramAndroidDebugBridge, String x0, Process paramProcess, ArrayList paramArrayList)
  {
    super(x0);
  }
  
  public void run()
  {
    InputStreamReader is = new InputStreamReader(val$process.getInputStream());
    BufferedReader outReader = new BufferedReader(is);
    try
    {
      for (;;)
      {
        String line = outReader.readLine();
        if (line == null) {
          break;
        }
        Log.d("adb", line);
        val$stdOutput.add(line);
      }
    }
    catch (IOException e) {}
  }
}

/* Location:
 * Qualified Name:     com.android.ddmlib.AndroidDebugBridge.2
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package com.android.ddmlib;

public abstract interface AndroidDebugBridge$IClientChangeListener
{
  public abstract void clientChanged(Client paramClient, int paramInt);
}

/* Location:
 * Qualified Name:     com.android.ddmlib.AndroidDebugBridge.IClientChangeListener
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package com.android.ddmlib;

public abstract interface AndroidDebugBridge$IDebugBridgeChangeListener
{
  public abstract void bridgeChanged(AndroidDebugBridge paramAndroidDebugBridge);
}

/* Location:
 * Qualified Name:     com.android.ddmlib.AndroidDebugBridge.IDebugBridgeChangeListener
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package com.android.ddmlib;

public abstract interface AndroidDebugBridge$IDeviceChangeListener
{
  public abstract void deviceConnected(IDevice paramIDevice);
  
  public abstract void deviceDisconnected(IDevice paramIDevice);
  
  public abstract void deviceChanged(IDevice paramIDevice, int paramInt);
}

/* Location:
 * Qualified Name:     com.android.ddmlib.AndroidDebugBridge.IDeviceChangeListener
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package com.android.ddmlib;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class AndroidDebugBridge
{
  private static final int ADB_VERSION_MICRO_MIN = 20;
  private static final int ADB_VERSION_MICRO_MAX = -1;
  private static final Pattern sAdbVersion = Pattern.compile("^.*(\\d+)\\.(\\d+)\\.(\\d+)$");
  private static final String ADB = "adb";
  private static final String DDMS = "ddms";
  private static final String SERVER_PORT_ENV_VAR = "ANDROID_ADB_SERVER_PORT";
  static final String ADB_HOST = "127.0.0.1";
  static final int ADB_PORT = 5037;
  private static InetAddress sHostAddr;
  private static InetSocketAddress sSocketAddr;
  private static AndroidDebugBridge sThis;
  private static boolean sInitialized = false;
  private static boolean sClientSupport;
  private String mAdbOsLocation = null;
  private boolean mVersionCheck;
  private boolean mStarted = false;
  private DeviceMonitor mDeviceMonitor;
  private static final ArrayList<IDebugBridgeChangeListener> sBridgeListeners = new ArrayList();
  private static final ArrayList<IDeviceChangeListener> sDeviceListeners = new ArrayList();
  private static final ArrayList<IClientChangeListener> sClientListeners = new ArrayList();
  private static final Object sLock = sBridgeListeners;
  
  public static synchronized void initIfNeeded(boolean clientSupport)
  {
    if (sInitialized) {
      return;
    }
    init(clientSupport);
  }
  
  public static synchronized void init(boolean clientSupport)
  {
    if (sInitialized) {
      throw new IllegalStateException("AndroidDebugBridge.init() has already been called.");
    }
    sInitialized = true;
    sClientSupport = clientSupport;
    
    initAdbSocketAddr();
    
    MonitorThread monitorThread = MonitorThread.createInstance();
    monitorThread.start();
    
    HandleHello.register(monitorThread);
    HandleAppName.register(monitorThread);
    HandleTest.register(monitorThread);
    HandleThread.register(monitorThread);
    HandleHeap.register(monitorThread);
    HandleWait.register(monitorThread);
    HandleProfiling.register(monitorThread);
    HandleNativeHeap.register(monitorThread);
    HandleViewDebug.register(monitorThread);
  }
  
  public static synchronized void terminate()
  {
    if ((sThis != null) && (sThismDeviceMonitor != null))
    {
      sThismDeviceMonitor.stop();
      sThismDeviceMonitor = null;
    }
    MonitorThread monitorThread = MonitorThread.getInstance();
    if (monitorThread != null) {
      monitorThread.quit();
    }
    sInitialized = false;
  }
  
  static boolean getClientSupport()
  {
    return sClientSupport;
  }
  
  public static InetSocketAddress getSocketAddress()
  {
    return sSocketAddr;
  }
  
  public static AndroidDebugBridge createBridge()
  {
    synchronized (sLock)
    {
      if (sThis != null) {
        return sThis;
      }
      try
      {
        sThis = new AndroidDebugBridge();
        sThis.start();
      }
      catch (InvalidParameterException e)
      {
        sThis = null;
      }
      IDebugBridgeChangeListener[] listenersCopy = (IDebugBridgeChangeListener[])sBridgeListeners.toArray(new IDebugBridgeChangeListener[sBridgeListeners.size()]);
      for (IDebugBridgeChangeListener listener : listenersCopy) {
        try
        {
          listener.bridgeChanged(sThis);
        }
        catch (Exception e)
        {
          Log.e("ddms", e);
        }
      }
      return sThis;
    }
  }
  
  public static AndroidDebugBridge createBridge(String osLocation, boolean forceNewBridge)
  {
    synchronized (sLock)
    {
      if (sThis != null)
      {
        if ((sThismAdbOsLocation != null) && (sThismAdbOsLocation.equals(osLocation)) && (!forceNewBridge)) {
          return sThis;
        }
        sThis.stop();
      }
      try
      {
        sThis = new AndroidDebugBridge(osLocation);
        sThis.start();
      }
      catch (InvalidParameterException e)
      {
        sThis = null;
      }
      IDebugBridgeChangeListener[] listenersCopy = (IDebugBridgeChangeListener[])sBridgeListeners.toArray(new IDebugBridgeChangeListener[sBridgeListeners.size()]);
      for (IDebugBridgeChangeListener listener : listenersCopy) {
        try
        {
          listener.bridgeChanged(sThis);
        }
        catch (Exception e)
        {
          Log.e("ddms", e);
        }
      }
      return sThis;
    }
  }
  
  public static AndroidDebugBridge getBridge()
  {
    return sThis;
  }
  
  public static void disconnectBridge()
  {
    synchronized (sLock)
    {
      if (sThis != null)
      {
        sThis.stop();
        sThis = null;
        
        IDebugBridgeChangeListener[] listenersCopy = (IDebugBridgeChangeListener[])sBridgeListeners.toArray(new IDebugBridgeChangeListener[sBridgeListeners.size()]);
        for (IDebugBridgeChangeListener listener : listenersCopy) {
          try
          {
            listener.bridgeChanged(sThis);
          }
          catch (Exception e)
          {
            Log.e("ddms", e);
          }
        }
      }
    }
  }
  
  public static void addDebugBridgeChangeListener(IDebugBridgeChangeListener listener)
  {
    synchronized (sLock)
    {
      if (!sBridgeListeners.contains(listener))
      {
        sBridgeListeners.add(listener);
        if (sThis != null) {
          try
          {
            listener.bridgeChanged(sThis);
          }
          catch (Exception e)
          {
            Log.e("ddms", e);
          }
        }
      }
    }
  }
  
  public static void removeDebugBridgeChangeListener(IDebugBridgeChangeListener listener)
  {
    synchronized (sLock)
    {
      sBridgeListeners.remove(listener);
    }
  }
  
  public static void addDeviceChangeListener(IDeviceChangeListener listener)
  {
    synchronized (sLock)
    {
      if (!sDeviceListeners.contains(listener)) {
        sDeviceListeners.add(listener);
      }
    }
  }
  
  public static void removeDeviceChangeListener(IDeviceChangeListener listener)
  {
    synchronized (sLock)
    {
      sDeviceListeners.remove(listener);
    }
  }
  
  public static void addClientChangeListener(IClientChangeListener listener)
  {
    synchronized (sLock)
    {
      if (!sClientListeners.contains(listener)) {
        sClientListeners.add(listener);
      }
    }
  }
  
  public static void removeClientChangeListener(IClientChangeListener listener)
  {
    synchronized (sLock)
    {
      sClientListeners.remove(listener);
    }
  }
  
  public IDevice[] getDevices()
  {
    synchronized (sLock)
    {
      if (mDeviceMonitor != null) {
        return mDeviceMonitor.getDevices();
      }
    }
    return new IDevice[0];
  }
  
  public boolean hasInitialDeviceList()
  {
    if (mDeviceMonitor != null) {
      return mDeviceMonitor.hasInitialDeviceList();
    }
    return false;
  }
  
  public void setSelectedClient(Client selectedClient)
  {
    MonitorThread monitorThread = MonitorThread.getInstance();
    if (monitorThread != null) {
      monitorThread.setSelectedClient(selectedClient);
    }
  }
  
  public boolean isConnected()
  {
    MonitorThread monitorThread = MonitorThread.getInstance();
    if ((mDeviceMonitor != null) && (monitorThread != null)) {
      return (mDeviceMonitor.isMonitoring()) && (monitorThread.getState() != Thread.State.TERMINATED);
    }
    return false;
  }
  
  public int getConnectionAttemptCount()
  {
    if (mDeviceMonitor != null) {
      return mDeviceMonitor.getConnectionAttemptCount();
    }
    return -1;
  }
  
  public int getRestartAttemptCount()
  {
    if (mDeviceMonitor != null) {
      return mDeviceMonitor.getRestartAttemptCount();
    }
    return -1;
  }
  
  private AndroidDebugBridge(String osLocation)
    throws InvalidParameterException
  {
    if ((osLocation == null) || (osLocation.isEmpty())) {
      throw new InvalidParameterException();
    }
    mAdbOsLocation = osLocation;
    
    checkAdbVersion();
  }
  
  private AndroidDebugBridge() {}
  
  private void checkAdbVersion()
  {
    mVersionCheck = false;
    if (mAdbOsLocation == null) {
      return;
    }
    String[] command = new String[2];
    command[0] = mAdbOsLocation;
    command[1] = "version";
    Log.d("ddms", String.format("Checking '%1$s version'", new Object[] { mAdbOsLocation }));
    Process process = null;
    try
    {
      process = Runtime.getRuntime().exec(command);
    }
    catch (IOException e)
    {
      boolean exists = new File(mAdbOsLocation).exists();
      String msg;
      String msg;
      if (exists) {
        msg = String.format("Unexpected exception '%1$s' while attempting to get adb version from '%2$s'", new Object[] { e.getMessage(), mAdbOsLocation });
      } else {
        msg = "Unable to locate adb.\nPlease use SDK Manager and check if Android SDK platform-tools are installed.";
      }
      Log.logAndDisplay(Log.LogLevel.ERROR, "adb", msg);
      return;
    }
    ArrayList<String> errorOutput = new ArrayList();
    ArrayList<String> stdOutput = new ArrayList();
    int status;
    try
    {
      status = grabProcessOutput(process, errorOutput, stdOutput, true);
    }
    catch (InterruptedException e)
    {
      return;
    }
    if (status != 0)
    {
      StringBuilder builder = new StringBuilder("'adb version' failed!");
      for (String error : errorOutput)
      {
        builder.append('\n');
        builder.append(error);
      }
      Log.logAndDisplay(Log.LogLevel.ERROR, "adb", builder.toString());
    }
    boolean versionFound = false;
    for (String line : stdOutput)
    {
      versionFound = scanVersionLine(line);
      if (versionFound) {
        break;
      }
    }
    if (!versionFound) {
      for (String line : errorOutput)
      {
        versionFound = scanVersionLine(line);
        if (versionFound) {
          break;
        }
      }
    }
    if (!versionFound)
    {
      StringBuilder builder = new StringBuilder("Failed to parse the output of 'adb version':\n");
      
      builder.append("Standard Output was:\n");
      for (String line : stdOutput)
      {
        builder.append(line);
        builder.append('\n');
      }
      builder.append("\nError Output was:\n");
      for (String line : errorOutput)
      {
        builder.append(line);
        builder.append('\n');
      }
      Log.logAndDisplay(Log.LogLevel.ERROR, "adb", builder.toString());
    }
  }
  
  private boolean scanVersionLine(String line)
  {
    if (line != null)
    {
      Matcher matcher = sAdbVersion.matcher(line);
      if (matcher.matches())
      {
        int majorVersion = Integer.parseInt(matcher.group(1));
        int minorVersion = Integer.parseInt(matcher.group(2));
        int microVersion = Integer.parseInt(matcher.group(3));
        if (microVersion < 20)
        {
          String message = String.format("Required minimum version of adb: %1$d.%2$d.%3$d.Current version is %1$d.%2$d.%4$d", new Object[] { Integer.valueOf(majorVersion), Integer.valueOf(minorVersion), Integer.valueOf(20), Integer.valueOf(microVersion) });
          
          Log.logAndDisplay(Log.LogLevel.ERROR, "adb", message);
        }
        else
        {
          mVersionCheck = true;
        }
        return true;
      }
    }
    return false;
  }
  
  boolean start()
  {
    if ((mAdbOsLocation != null) && ((!mVersionCheck) || (!startAdb()))) {
      return false;
    }
    mStarted = true;
    
    mDeviceMonitor = new DeviceMonitor(this);
    mDeviceMonitor.start();
    
    return true;
  }
  
  boolean stop()
  {
    if (!mStarted) {
      return false;
    }
    if (mDeviceMonitor != null)
    {
      mDeviceMonitor.stop();
      mDeviceMonitor = null;
    }
    if (!stopAdb()) {
      return false;
    }
    mStarted = false;
    return true;
  }
  
  public boolean restart()
  {
    if (mAdbOsLocation == null)
    {
      Log.e("adb", "Cannot restart adb when AndroidDebugBridge is created without the location of adb.");
      
      return false;
    }
    if (!mVersionCheck)
    {
      Log.logAndDisplay(Log.LogLevel.ERROR, "adb", "Attempting to restart adb, but version check failed!");
      
      return false;
    }
    synchronized (this)
    {
      stopAdb();
      
      boolean restart = startAdb();
      if ((restart) && (mDeviceMonitor == null))
      {
        mDeviceMonitor = new DeviceMonitor(this);
        mDeviceMonitor.start();
      }
      return restart;
    }
  }
  
  void deviceConnected(IDevice device)
  {
    IDeviceChangeListener[] listenersCopy = null;
    synchronized (sLock)
    {
      listenersCopy = (IDeviceChangeListener[])sDeviceListeners.toArray(new IDeviceChangeListener[sDeviceListeners.size()]);
    }
    for (IDeviceChangeListener listener : listenersCopy) {
      try
      {
        listener.deviceConnected(device);
      }
      catch (Exception e)
      {
        Log.e("ddms", e);
      }
    }
  }
  
  void deviceDisconnected(IDevice device)
  {
    IDeviceChangeListener[] listenersCopy = null;
    synchronized (sLock)
    {
      listenersCopy = (IDeviceChangeListener[])sDeviceLis
1 2 3 4 5 6 7 8 9 10 11 12

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2017. Infinite Loop Ltd