DdmConnection.cpp revision 8afb7e39a83a3e31170612d562eb08508e328388
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <android_runtime/AndroidRuntime.h>
18
19#include "jni.h"
20#include "DdmConnection.h"
21
22extern "C" jint Java_com_android_internal_util_WithFramework_registerNatives(
23        JNIEnv* env, jclass clazz);
24
25namespace android {
26
27void DdmConnection::start(const char* name) {
28    JavaVM* vm;
29    JNIEnv* env;
30
31    // start a VM
32    JavaVMInitArgs args;
33    JavaVMOption opt;
34
35    opt.optionString =
36        "-agentlib:jdwp=transport=dt_android_adb,suspend=n,server=y";
37
38    args.version = JNI_VERSION_1_4;
39    args.options = &opt;
40    args.nOptions = 1;
41    args.ignoreUnrecognized = JNI_FALSE;
42
43    if (JNI_CreateJavaVM(&vm, &env, &args) == 0) {
44        jclass startClass;
45        jmethodID startMeth;
46
47        // register native code
48        if (Java_com_android_internal_util_WithFramework_registerNatives(env, 0) == 0) {
49            // set our name by calling DdmHandleAppName.setAppName()
50            startClass = env->FindClass("android/ddm/DdmHandleAppName");
51            if (startClass) {
52                startMeth = env->GetStaticMethodID(startClass,
53                        "setAppName", "(Ljava/lang/String;)V");
54                if (startMeth) {
55                    jstring str = env->NewStringUTF(name);
56                    env->CallStaticVoidMethod(startClass, startMeth, str);
57                    env->DeleteLocalRef(str);
58                }
59            }
60
61            // initialize DDMS communication by calling
62            // DdmRegister.registerHandlers()
63            startClass = env->FindClass("android/ddm/DdmRegister");
64            if (startClass) {
65                startMeth = env->GetStaticMethodID(startClass,
66                        "registerHandlers", "()V");
67                if (startMeth) {
68                    env->CallStaticVoidMethod(startClass, startMeth);
69                }
70            }
71        }
72    }
73}
74
75}; // namespace android
76