16b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine/*
26b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine * Copyright (C) 2008 The Android Open Source Project
36b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine *
46b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine * Licensed under the Apache License, Version 2.0 (the "License");
56b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine * you may not use this file except in compliance with the License.
66b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine * You may obtain a copy of the License at
76b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine *
86b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine *      http://www.apache.org/licenses/LICENSE-2.0
96b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine *
106b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine * Unless required by applicable law or agreed to in writing, software
116b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine * distributed under the License is distributed on an "AS IS" BASIS,
126b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine * See the License for the specific language governing permissions and
146b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine * limitations under the License.
156b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine */
166b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine
176b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine#define LOG_TAG "pmtest64 native.cpp"
186b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine#include <utils/Log.h>
196b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine
206b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine#include <stdio.h>
216b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine
226b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine#include "jni.h"
236b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine
246b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavinestatic jint
251c8cbb54407b9c1d56626de80a796a395012a92cAurimas Liutikasadd(JNIEnv */* env */, jobject /* thiz */, jint a, jint b) {
266b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavineint result = a + b;
276b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine    ALOGI("%d + %d = %d", a, b, result);
286b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine    return result;
296b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine}
306b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine
316b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavinestatic const char *classPathName = "com/framework/shareduid/bit64/Native";
326b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine
3376f6a86de25e1bf74717e047e55fd44b089673f3Daniel Micaystatic const JNINativeMethod methods[] = {
346b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine  {"add", "(II)I", (void*)add },
356b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine};
366b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine
376b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine/*
386b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine * Register several native methods for one class.
396b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine */
406b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavinestatic int registerNativeMethods(JNIEnv* env, const char* className,
4176f6a86de25e1bf74717e047e55fd44b089673f3Daniel Micay    const JNINativeMethod* gMethods, int numMethods)
426b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine{
436b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine    jclass clazz;
446b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine
456b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine    clazz = env->FindClass(className);
466b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine    if (clazz == NULL) {
476b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine        ALOGE("Native registration unable to find class '%s'", className);
486b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine        return JNI_FALSE;
496b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine    }
506b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine    if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
516b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine        ALOGE("RegisterNatives failed for '%s'", className);
526b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine        return JNI_FALSE;
536b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine    }
546b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine
556b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine    return JNI_TRUE;
566b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine}
576b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine
586b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine/*
596b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine * Register native methods for all classes we know about.
606b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine *
616b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine * returns JNI_TRUE on success.
626b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine */
636b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavinestatic int registerNatives(JNIEnv* env)
646b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine{
656b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine  if (!registerNativeMethods(env, classPathName,
666b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine                 methods, sizeof(methods) / sizeof(methods[0]))) {
676b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine    return JNI_FALSE;
686b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine  }
696b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine
706b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine  return JNI_TRUE;
716b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine}
726b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine
736b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine
746b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine// ----------------------------------------------------------------------------
756b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine
766b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine/*
776b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine * This is called by the VM when the shared library is first loaded.
786b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine */
796b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine
806b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavinetypedef union {
816b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine    JNIEnv* env;
826b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine    void* venv;
836b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine} UnionJNIEnvToVoid;
846b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine
851c8cbb54407b9c1d56626de80a796a395012a92cAurimas Liutikasjint JNI_OnLoad(JavaVM* vm, void* /* reserved */)
866b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine{
876b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine    UnionJNIEnvToVoid uenv;
886b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine    uenv.venv = NULL;
896b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine    jint result = -1;
906b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine    JNIEnv* env = NULL;
916b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine
926b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine    ALOGI("JNI_OnLoad");
936b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine
946b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine    if (vm->GetEnv(&uenv.venv, JNI_VERSION_1_4) != JNI_OK) {
956b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine        ALOGE("ERROR: GetEnv failed");
966b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine        goto bail;
976b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine    }
986b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine    env = uenv.env;
996b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine
1006b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine    if (registerNatives(env) != JNI_TRUE) {
1016b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine        ALOGE("ERROR: registerNatives failed");
1026b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine        goto bail;
1036b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine    }
1046b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine
1056b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine    result = JNI_VERSION_1_4;
1066b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine
1076b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavinebail:
1086b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine    return result;
1096b1e88e5d09a88dd77e631918aa7122a95a7ff4bMaxim Siniavine}
110