package android.ddm;
import org.apache.harmony.dalvik.ddmc.Chunk;
import org.apache.harmony.dalvik.ddmc.ChunkHandler;
import org.apache.harmony.dalvik.ddmc.DdmServer;
import android.util.Log;
import android.os.Debug;
import android.os.UserHandle;
import dalvik.system.VMRuntime;
import java.nio.ByteBuffer;
public class DdmHandleHello extends ChunkHandler {
public static final int CHUNK_HELO = type("HELO");
public static final int CHUNK_WAIT = type("WAIT");
public static final int CHUNK_FEAT = type("FEAT");
private static DdmHandleHello mInstance = new DdmHandleHello();
private static final String[] FRAMEWORK_FEATURES = new String[] {
"opengl-tracing",
"view-hierarchy",
};
private DdmHandleHello() {}
public static void register() {
DdmServer.registerHandler(CHUNK_HELO, mInstance);
DdmServer.registerHandler(CHUNK_FEAT, mInstance);
}
public void connected() {
if (false)
Log.v("ddm-hello", "Connected!");
if (false) {
byte[] data = new byte[] { 0, 1, 2, 3, 4, -4, -3, -2, -1, 127 };
Chunk testChunk =
new Chunk(ChunkHandler.type("TEST"), data, 1, data.length-2);
DdmServer.sendChunk(testChunk);
}
}
public void disconnected() {
if (false)
Log.v("ddm-hello", "Disconnected!");
}
public Chunk handleChunk(Chunk request) {
if (false)
Log.v("ddm-heap", "Handling " + name(request.type) + " chunk");
int type = request.type;
if (type == CHUNK_HELO) {
return handleHELO(request);
} else if (type == CHUNK_FEAT) {
return handleFEAT(request);
} else {
throw new RuntimeException("Unknown packet "
+ ChunkHandler.name(type));
}
}
private Chunk handleHELO(Chunk request) {
if (false)
return createFailChunk(123, "This is a test");
ByteBuffer in = wrapChunk(request);
int serverProtoVers = in.getInt();
if (false)
Log.v("ddm-hello", "Server version is " + serverProtoVers);
String vmName = System.getProperty("java.vm.name", "?");
String vmVersion = System.getProperty("java.vm.version", "?");
String vmIdent = vmName + " v" + vmVersion;
String appName = DdmHandleAppName.getAppName();
VMRuntime vmRuntime = VMRuntime.getRuntime();
String instructionSetDescription =
vmRuntime.is64Bit() ? "64-bit" : "32-bit";
String vmInstructionSet = vmRuntime.vmInstructionSet();
if (vmInstructionSet != null && vmInstructionSet.length() > 0) {
instructionSetDescription += " (" + vmInstructionSet + ")";
}
String vmFlags = "CheckJNI="
+ (vmRuntime.isCheckJniEnabled() ? "true" : "false");
boolean isNativeDebuggable = vmRuntime.isNativeDebuggable();
ByteBuffer out = ByteBuffer.allocate(28
+ vmIdent.length() * 2
+ appName.length() * 2
+ instructionSetDescription.length() * 2
+ vmFlags.length() * 2
+ 1);
out.order(ChunkHandler.CHUNK_ORDER);
out.putInt(DdmServer.CLIENT_PROTOCOL_VERSION);
out.putInt(android.os.Process.myPid());
out.putInt(vmIdent.length());
out.putInt(appName.length());
putString(out, vmIdent);
putString(out, appName);
out.putInt(UserHandle.myUserId());
out.putInt(instructionSetDescription.length());
putString(out, instructionSetDescription);
out.putInt(vmFlags.length());
putString(out, vmFlags);
out.put((byte)(isNativeDebuggable ? 1 : 0));
Chunk reply = new Chunk(CHUNK_HELO, out);
if (Debug.waitingForDebugger())
sendWAIT(0);
return reply;
}
private Chunk handleFEAT(Chunk request) {
final String[] vmFeatures = Debug.getVmFeatureList();
if (false)
Log.v("ddm-heap", "Got feature list request");
int size = 4 + 4 * (vmFeatures.length + FRAMEWORK_FEATURES.length);
for (int i = vmFeatures.length-1; i >= 0; i--)
size += vmFeatures[i].length() * 2;
for (int i = FRAMEWORK_FEATURES.length-1; i>= 0; i--)
size += FRAMEWORK_FEATURES[i].length() * 2;
ByteBuffer out = ByteBuffer.allocate(size);
out.order(ChunkHandler.CHUNK_ORDER);
out.putInt(vmFeatures.length + FRAMEWORK_FEATURES.length);
for (int i = vmFeatures.length-1; i >= 0; i--) {
out.putInt(vmFeatures[i].length());
putString(out, vmFeatures[i]);
}
for (int i = FRAMEWORK_FEATURES.length-1; i >= 0; i--) {
out.putInt(FRAMEWORK_FEATURES[i].length());
putString(out, FRAMEWORK_FEATURES[i]);
}
return new Chunk(CHUNK_FEAT, out);
}
public static void sendWAIT(int reason) {
byte[] data = new byte[] { (byte) reason };
Chunk waitChunk = new Chunk(CHUNK_WAIT, data, 0, 1);
DdmServer.sendChunk(waitChunk);
}
}