1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "base/android/jni_generator/sample_for_tests.h" 6 7 #include "base/android/jni_android.h" 8 #include "base/android/jni_string.h" 9 #include "base/android/scoped_java_ref.h" 10 #include "jni/SampleForTests_jni.h" 11 12 13 using base::android::AttachCurrentThread; 14 using base::android::ScopedJavaLocalRef; 15 16 namespace base { 17 namespace android { 18 19 jdouble CPPClass::InnerClass::MethodOtherP0(JNIEnv* env, jobject obj) { 20 return 0.0; 21 } 22 23 CPPClass::CPPClass() { 24 } 25 26 CPPClass::~CPPClass() { 27 } 28 29 void CPPClass::Destroy(JNIEnv* env, jobject obj) { 30 delete this; 31 } 32 33 jint CPPClass::Method(JNIEnv* env, jobject obj) { 34 return 0; 35 } 36 37 void CPPClass::AddStructB(JNIEnv* env, jobject obj, jobject structb) { 38 long key = Java_InnerStructB_getKey(env, structb); 39 std::string value = ConvertJavaStringToUTF8( 40 env, Java_InnerStructB_getValue(env, structb).obj()); 41 map_[key] = value; 42 } 43 44 void CPPClass::IterateAndDoSomethingWithStructB(JNIEnv* env, jobject obj) { 45 // Iterate over the elements and do something with them. 46 for (std::map<long, std::string>::const_iterator it = map_.begin(); 47 it != map_.end(); ++it) { 48 long key = it->first; 49 std::string value = it->second; 50 } 51 map_.clear(); 52 } 53 54 // Static free functions declared and called directly from java. 55 static jint Init(JNIEnv* env, jobject obj, jstring param) { 56 return 0; 57 } 58 59 static jdouble GetDoubleFunction(JNIEnv*, jobject) { 60 return 0; 61 } 62 63 static jfloat GetFloatFunction(JNIEnv*, jclass) { 64 return 0; 65 } 66 67 static void SetNonPODDatatype(JNIEnv*, jobject, jobject) { 68 } 69 70 static jobject GetNonPODDatatype(JNIEnv*, jobject) { 71 return NULL; 72 } 73 74 static jint InnerFunction(JNIEnv*, jclass) { 75 return 0; 76 } 77 78 } // namespace android 79 } // namespace base 80 81 int main() { 82 // On a regular application, you'd call AttachCurrentThread(). This sample is 83 // not yet linking with all the libraries. 84 JNIEnv* env = /* AttachCurrentThread() */ NULL; 85 86 // This is how you call a java static method from C++. 87 bool foo = base::android::Java_SampleForTests_staticJavaMethod(env); 88 89 // This is how you call a java method from C++. Note that you must have 90 // obtained the jobject somehow. 91 jobject my_java_object = NULL; 92 int bar = base::android::Java_SampleForTests_javaMethod( 93 env, my_java_object, 1, 2); 94 95 for (int i = 0; i < 10; ++i) { 96 // Creates a "struct" that will then be used by the java side. 97 ScopedJavaLocalRef<jobject> struct_a = 98 base::android::Java_InnerStructA_create( 99 env, 0, 1, 100 base::android::ConvertUTF8ToJavaString(env, "test").obj()); 101 base::android::Java_SampleForTests_addStructA( 102 env, my_java_object, struct_a.obj()); 103 } 104 base::android::Java_SampleForTests_iterateAndDoSomething(env, my_java_object); 105 return 0; 106 } 107