Merge pull request #467 from asmorkalov:manager_jni_overflow

This commit is contained in:
Andrey Kamaev 2013-02-11 19:17:41 +04:00 committed by OpenCV Buildbot
commit 80d760c96e
2 changed files with 23 additions and 4 deletions

View File

@ -46,7 +46,9 @@ bool JavaBasedPackageManager::InstallPackage(const PackageInfo& package)
LOGD("Calling java package manager with package name %s\n", package.GetFullName().c_str());
jobject jpkgname = jenv->NewStringUTF(package.GetFullName().c_str());
bool result = jenv->CallNonvirtualBooleanMethod(JavaPackageManager, jclazz, jmethod, jpkgname);
jenv->DeleteLocalRef(jpkgname);
jenv->DeleteLocalRef(jclazz);
if (self_attached)
{
@ -104,9 +106,12 @@ vector<PackageInfo> JavaBasedPackageManager::GetInstalledPackages()
if (tmp.IsValid())
result.push_back(tmp);
jenv->DeleteLocalRef(jtmp);
}
jenv->DeleteLocalRef(jpkgs);
jenv->DeleteLocalRef(jclazz);
if (self_attached)
{
@ -118,6 +123,16 @@ vector<PackageInfo> JavaBasedPackageManager::GetInstalledPackages()
return result;
}
static jint GetAndroidVersion(JNIEnv* jenv)
{
jclass jclazz = jenv->FindClass("android/os/Build$VERSION");
jfieldID jfield = jenv->GetStaticFieldID(jclazz, "SDK_INT", "I");
jint api_level = jenv->GetStaticIntField(jclazz, jfield);
jenv->DeleteLocalRef(jclazz);
return api_level;
}
// IMPORTANT: This method can be called only if thread is attached to Dalvik
PackageInfo JavaBasedPackageManager::ConvertPackageFromJava(jobject package, JNIEnv* jenv)
{
@ -133,23 +148,27 @@ PackageInfo JavaBasedPackageManager::ConvertPackageFromJava(jobject package, JNI
const char* jversionstr = jenv->GetStringUTFChars(jversionobj, NULL);
string verison(jversionstr);
jenv->DeleteLocalRef(jversionobj);
jenv->DeleteLocalRef(jclazz);
static const jint api_level = GetAndroidVersion(jenv);
string path;
jclazz = jenv->FindClass("android/os/Build$VERSION");
jfield = jenv->GetStaticFieldID(jclazz, "SDK_INT", "I");
jint api_level = jenv->GetStaticIntField(jclazz, jfield);
if (api_level > 8)
{
jclazz = jenv->GetObjectClass(package);
jfield = jenv->GetFieldID(jclazz, "applicationInfo", "Landroid/content/pm/ApplicationInfo;");
jobject japp_info = jenv->GetObjectField(package, jfield);
jenv->DeleteLocalRef(jclazz);
jclazz = jenv->GetObjectClass(japp_info);
jfield = jenv->GetFieldID(jclazz, "nativeLibraryDir", "Ljava/lang/String;");
jstring jpathobj = static_cast<jstring>(jenv->GetObjectField(japp_info, jfield));
const char* jpathstr = jenv->GetStringUTFChars(jpathobj, NULL);
path = string(jpathstr);
jenv->ReleaseStringUTFChars(jpathobj, jpathstr);
jenv->DeleteLocalRef(japp_info);
jenv->DeleteLocalRef(jpathobj);
jenv->DeleteLocalRef(jclazz);
}
else
{

View File

@ -19,4 +19,4 @@ private:
JavaBasedPackageManager();
PackageInfo ConvertPackageFromJava(jobject package, JNIEnv* jenv);
};
};