1/* 2 * Copyright (C) 2008 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#define LOG_TAG "pmtest64 native.cpp" 18#include <android/log.h> 19 20#include <stdio.h> 21 22#include "jni.h" 23 24#define ALOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) 25#define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__) 26 27static jint 28add(JNIEnv */* env */, jobject /* thiz */, jint a, jint b) { 29int result = a + b; 30 ALOGI("%d + %d = %d", a, b, result); 31 return result; 32} 33 34static const char *classPathName = "com/framework/shareduid/bit64/Native"; 35 36static const JNINativeMethod methods[] = { 37 {"add", "(II)I", (void*)add }, 38}; 39 40/* 41 * Register several native methods for one class. 42 */ 43static int registerNativeMethods(JNIEnv* env, const char* className, 44 const JNINativeMethod* gMethods, int numMethods) 45{ 46 jclass clazz; 47 48 clazz = env->FindClass(className); 49 if (clazz == NULL) { 50 ALOGE("Native registration unable to find class '%s'", className); 51 return JNI_FALSE; 52 } 53 if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) { 54 ALOGE("RegisterNatives failed for '%s'", className); 55 return JNI_FALSE; 56 } 57 58 return JNI_TRUE; 59} 60 61/* 62 * Register native methods for all classes we know about. 63 * 64 * returns JNI_TRUE on success. 65 */ 66static int registerNatives(JNIEnv* env) 67{ 68 if (!registerNativeMethods(env, classPathName, 69 methods, sizeof(methods) / sizeof(methods[0]))) { 70 return JNI_FALSE; 71 } 72 73 return JNI_TRUE; 74} 75 76 77// ---------------------------------------------------------------------------- 78 79/* 80 * This is called by the VM when the shared library is first loaded. 81 */ 82 83typedef union { 84 JNIEnv* env; 85 void* venv; 86} UnionJNIEnvToVoid; 87 88jint JNI_OnLoad(JavaVM* vm, void* /* reserved */) 89{ 90 UnionJNIEnvToVoid uenv; 91 uenv.venv = NULL; 92 jint result = -1; 93 JNIEnv* env = NULL; 94 95 ALOGI("JNI_OnLoad"); 96 97 if (vm->GetEnv(&uenv.venv, JNI_VERSION_1_4) != JNI_OK) { 98 ALOGE("ERROR: GetEnv failed"); 99 goto bail; 100 } 101 env = uenv.env; 102 103 if (registerNatives(env) != JNI_TRUE) { 104 ALOGE("ERROR: registerNatives failed"); 105 goto bail; 106 } 107 108 result = JNI_VERSION_1_4; 109 110bail: 111 return result; 112} 113