2 * Copyright 2010, The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "ObbScanner"
19 #include <utils/Log.h>
20 #include <utils/String8.h>
21 #include <androidfw/ObbFile.h>
25 #include "utils/misc.h"
26 #include "android_runtime/AndroidRuntime.h"
39 static void android_content_res_ObbScanner_getObbInfo(JNIEnv* env, jobject clazz, jstring file,
42 const char* filePath = env->GetStringUTFChars(file, NULL);
44 sp<ObbFile> obb = new ObbFile();
45 if (!obb->readFrom(filePath)) {
46 env->ReleaseStringUTFChars(file, filePath);
47 jniThrowException(env, "java/io/IOException", "Could not read OBB file");
51 env->ReleaseStringUTFChars(file, filePath);
53 const char* packageNameStr = obb->getPackageName().string();
55 jstring packageName = env->NewStringUTF(packageNameStr);
56 if (packageName == NULL) {
57 jniThrowException(env, "java/io/IOException", "Could not read OBB file");
61 env->SetObjectField(obbInfo, gObbInfoClassInfo.packageName, packageName);
62 env->SetIntField(obbInfo, gObbInfoClassInfo.version, obb->getVersion());
63 env->SetIntField(obbInfo, gObbInfoClassInfo.flags, obb->getFlags());
66 const unsigned char* salt = obb->getSalt(&saltLen);
68 jbyteArray saltArray = env->NewByteArray(saltLen);
69 env->SetByteArrayRegion(saltArray, 0, saltLen, (jbyte*)salt);
70 env->SetObjectField(obbInfo, gObbInfoClassInfo.salt, saltArray);
77 static JNINativeMethod gMethods[] = {
78 /* name, signature, funcPtr */
79 { "getObbInfo_native", "(Ljava/lang/String;Landroid/content/res/ObbInfo;)V",
80 (void*) android_content_res_ObbScanner_getObbInfo },
83 #define FIND_CLASS(var, className) \
84 var = env->FindClass(className); \
85 LOG_FATAL_IF(! var, "Unable to find class " className);
87 #define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
88 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
89 LOG_FATAL_IF(! var, "Unable to find field " fieldName);
91 int register_android_content_res_ObbScanner(JNIEnv* env)
94 FIND_CLASS(clazz, "android/content/res/ObbInfo");
96 GET_FIELD_ID(gObbInfoClassInfo.packageName, clazz,
97 "packageName", "Ljava/lang/String;");
98 GET_FIELD_ID(gObbInfoClassInfo.version, clazz,
100 GET_FIELD_ID(gObbInfoClassInfo.flags, clazz,
102 GET_FIELD_ID(gObbInfoClassInfo.salt, clazz,
105 return AndroidRuntime::registerNativeMethods(env, "android/content/res/ObbScanner", gMethods,
109 }; // namespace android