mirror of
https://github.com/opencv/opencv.git
synced 2025-01-18 06:03:15 +08:00
Merge pull request #467 from asmorkalov:manager_jni_overflow
This commit is contained in:
commit
80d760c96e
@ -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
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user