| OLD | NEW |
| (Empty) |
| 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 // Generated file for JNI bindings from C++ to Java @CalledByNative methods. | |
| 11 // Only to be included in one .cc file. | |
| 12 // Name is based on the java file name: *.java -> jni/*_jni.h | |
| 13 #include "jni/SampleForTests_jni.h" // Generated by JNI. | |
| 14 | |
| 15 using base::android::AttachCurrentThread; | |
| 16 using base::android::ScopedJavaLocalRef; | |
| 17 | |
| 18 namespace base { | |
| 19 namespace android { | |
| 20 | |
| 21 jdouble CPPClass::InnerClass::MethodOtherP0(JNIEnv* env, jobject caller) { | |
| 22 return 0.0; | |
| 23 } | |
| 24 | |
| 25 CPPClass::CPPClass() { | |
| 26 } | |
| 27 | |
| 28 CPPClass::~CPPClass() { | |
| 29 } | |
| 30 | |
| 31 // static | |
| 32 bool CPPClass::RegisterJNI(JNIEnv* env) { | |
| 33 return RegisterNativesImpl(env); // Generated in SampleForTests_jni.h | |
| 34 } | |
| 35 | |
| 36 void CPPClass::Destroy(JNIEnv* env, jobject caller) { | |
| 37 delete this; | |
| 38 } | |
| 39 | |
| 40 jint CPPClass::Method(JNIEnv* env, jobject caller) { | |
| 41 return 0; | |
| 42 } | |
| 43 | |
| 44 void CPPClass::AddStructB(JNIEnv* env, jobject caller, jobject structb) { | |
| 45 long key = Java_InnerStructB_getKey(env, structb); | |
| 46 std::string value = ConvertJavaStringToUTF8( | |
| 47 env, Java_InnerStructB_getValue(env, structb).obj()); | |
| 48 map_[key] = value; | |
| 49 } | |
| 50 | |
| 51 void CPPClass::IterateAndDoSomethingWithStructB(JNIEnv* env, jobject caller) { | |
| 52 // Iterate over the elements and do something with them. | |
| 53 for (std::map<long, std::string>::const_iterator it = map_.begin(); | |
| 54 it != map_.end(); ++it) { | |
| 55 long key = it->first; | |
| 56 std::string value = it->second; | |
| 57 } | |
| 58 map_.clear(); | |
| 59 } | |
| 60 | |
| 61 base::android::ScopedJavaLocalRef<jstring> CPPClass::ReturnAString( | |
| 62 JNIEnv* env, | |
| 63 jobject caller) { | |
| 64 base::android::ScopedJavaLocalRef<jstring> ret = ConvertUTF8ToJavaString( | |
| 65 env, "test"); | |
| 66 return ret; | |
| 67 } | |
| 68 | |
| 69 // Static free functions declared and called directly from java. | |
| 70 static jlong Init(JNIEnv* env, jobject caller, jstring param) { | |
| 71 return 0; | |
| 72 } | |
| 73 | |
| 74 static jdouble GetDoubleFunction(JNIEnv*, jobject) { | |
| 75 return 0; | |
| 76 } | |
| 77 | |
| 78 static jfloat GetFloatFunction(JNIEnv*, jclass) { | |
| 79 return 0; | |
| 80 } | |
| 81 | |
| 82 static void SetNonPODDatatype(JNIEnv*, jobject, jobject) { | |
| 83 } | |
| 84 | |
| 85 static jobject GetNonPODDatatype(JNIEnv*, jobject) { | |
| 86 return NULL; | |
| 87 } | |
| 88 | |
| 89 static jint InnerFunction(JNIEnv*, jclass) { | |
| 90 return 0; | |
| 91 } | |
| 92 | |
| 93 } // namespace android | |
| 94 } // namespace base | |
| 95 | |
| 96 int main() { | |
| 97 // On a regular application, you'd call AttachCurrentThread(). This sample is | |
| 98 // not yet linking with all the libraries. | |
| 99 JNIEnv* env = /* AttachCurrentThread() */ NULL; | |
| 100 | |
| 101 // This is how you call a java static method from C++. | |
| 102 bool foo = base::android::Java_SampleForTests_staticJavaMethod(env); | |
| 103 | |
| 104 // This is how you call a java method from C++. Note that you must have | |
| 105 // obtained the jobject somehow. | |
| 106 jobject my_java_object = NULL; | |
| 107 int bar = base::android::Java_SampleForTests_javaMethod( | |
| 108 env, my_java_object, 1, 2); | |
| 109 | |
| 110 for (int i = 0; i < 10; ++i) { | |
| 111 // Creates a "struct" that will then be used by the java side. | |
| 112 ScopedJavaLocalRef<jobject> struct_a = | |
| 113 base::android::Java_InnerStructA_create( | |
| 114 env, 0, 1, | |
| 115 base::android::ConvertUTF8ToJavaString(env, "test").obj()); | |
| 116 base::android::Java_SampleForTests_addStructA( | |
| 117 env, my_java_object, struct_a.obj()); | |
| 118 } | |
| 119 base::android::Java_SampleForTests_iterateAndDoSomething(env, my_java_object); | |
| 120 return 0; | |
| 121 } | |
| OLD | NEW |