2012-06-29 00:23:19 +08:00
|
|
|
#include "JavaBasedPackageManager.h"
|
|
|
|
#include <utils/Log.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#undef LOG_TAG
|
|
|
|
#define LOG_TAG "JavaBasedPackageManager"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
JavaBasedPackageManager::JavaBasedPackageManager(JavaVM* JavaMashine, jobject MarketConnector):
|
2012-10-24 16:47:43 +08:00
|
|
|
JavaContext(JavaMashine),
|
|
|
|
JavaPackageManager(MarketConnector)
|
2012-06-29 00:23:19 +08:00
|
|
|
{
|
2012-10-17 07:18:30 +08:00
|
|
|
assert(JavaContext);
|
2012-06-29 00:23:19 +08:00
|
|
|
assert(JavaPackageManager);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JavaBasedPackageManager::InstallPackage(const PackageInfo& package)
|
|
|
|
{
|
|
|
|
JNIEnv* jenv;
|
|
|
|
bool self_attached;
|
|
|
|
LOGD("JavaBasedPackageManager::InstallPackage() begin\n");
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-06-29 00:23:19 +08:00
|
|
|
self_attached = (JNI_EDETACHED == JavaContext->GetEnv((void**)&jenv, JNI_VERSION_1_6));
|
|
|
|
if (self_attached)
|
|
|
|
{
|
2012-10-24 16:47:43 +08:00
|
|
|
JavaContext->AttachCurrentThread(&jenv, NULL);
|
2012-06-29 00:23:19 +08:00
|
|
|
}
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-06-29 00:23:19 +08:00
|
|
|
LOGD("GetObjectClass call\n");
|
|
|
|
jclass jclazz = jenv->GetObjectClass(JavaPackageManager);
|
|
|
|
if (!jclazz)
|
|
|
|
{
|
2012-10-24 16:47:43 +08:00
|
|
|
LOGE("MarketConnector class was not found!");
|
|
|
|
return false;
|
2012-06-29 00:23:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LOGD("GetMethodID call\n");
|
|
|
|
jmethodID jmethod = jenv->GetMethodID(jclazz, "InstallAppFromMarket", "(Ljava/lang/String;)Z");
|
|
|
|
if (!jmethod)
|
|
|
|
{
|
2012-10-24 16:47:43 +08:00
|
|
|
LOGE("MarketConnector::GetAppFormMarket method was not found!");
|
|
|
|
return false;
|
2012-06-29 00:23:19 +08:00
|
|
|
}
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-06-29 00:23:19 +08:00
|
|
|
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);
|
2012-10-17 07:18:30 +08:00
|
|
|
jenv->DeleteLocalRef(jpkgname);
|
|
|
|
|
2012-06-29 00:23:19 +08:00
|
|
|
if (self_attached)
|
|
|
|
{
|
2012-10-24 16:47:43 +08:00
|
|
|
JavaContext->DetachCurrentThread();
|
2012-06-29 00:23:19 +08:00
|
|
|
}
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-06-29 00:23:19 +08:00
|
|
|
LOGD("JavaBasedPackageManager::InstallPackage() end\n");
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-06-29 00:23:19 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<PackageInfo> JavaBasedPackageManager::GetInstalledPackages()
|
|
|
|
{
|
|
|
|
vector<PackageInfo> result;
|
|
|
|
JNIEnv* jenv;
|
|
|
|
bool self_attached;
|
|
|
|
|
|
|
|
LOGD("JavaBasedPackageManager::GetInstalledPackages() begin");
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-06-29 00:23:19 +08:00
|
|
|
self_attached = (JNI_EDETACHED == JavaContext->GetEnv((void**)&jenv, JNI_VERSION_1_6));
|
|
|
|
if (self_attached)
|
|
|
|
{
|
2012-10-24 16:47:43 +08:00
|
|
|
JavaContext->AttachCurrentThread(&jenv, NULL);
|
2012-06-29 00:23:19 +08:00
|
|
|
}
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-06-29 00:23:19 +08:00
|
|
|
LOGD("GetObjectClass call");
|
|
|
|
jclass jclazz = jenv->GetObjectClass(JavaPackageManager);
|
|
|
|
if (!jclazz)
|
|
|
|
{
|
2012-10-24 16:47:43 +08:00
|
|
|
LOGE("MarketConnector class was not found!");
|
|
|
|
return result;
|
2012-06-29 00:23:19 +08:00
|
|
|
}
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-06-29 00:23:19 +08:00
|
|
|
LOGD("GetMethodID call");
|
|
|
|
jmethodID jmethod = jenv->GetMethodID(jclazz, "GetInstalledOpenCVPackages", "()[Landroid/content/pm/PackageInfo;");
|
|
|
|
if (!jmethod)
|
|
|
|
{
|
2012-10-24 16:47:43 +08:00
|
|
|
LOGE("MarketConnector::GetInstalledOpenCVPackages method was not found!");
|
|
|
|
return result;
|
2012-06-29 00:23:19 +08:00
|
|
|
}
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-06-29 00:23:19 +08:00
|
|
|
LOGD("Java package manager call");
|
|
|
|
jobjectArray jpkgs = static_cast<jobjectArray>(jenv->CallNonvirtualObjectMethod(JavaPackageManager, jclazz, jmethod));
|
|
|
|
jsize size = jenv->GetArrayLength(jpkgs);
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-06-29 00:23:19 +08:00
|
|
|
LOGD("Package info conversion");
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-06-29 00:23:19 +08:00
|
|
|
result.reserve(size);
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-06-29 00:23:19 +08:00
|
|
|
for (jsize i = 0; i < size; i++)
|
|
|
|
{
|
2012-10-24 16:47:43 +08:00
|
|
|
jobject jtmp = jenv->GetObjectArrayElement(jpkgs, i);
|
|
|
|
PackageInfo tmp = ConvertPackageFromJava(jtmp, jenv);
|
|
|
|
jenv->DeleteLocalRef(jtmp);
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-10-24 16:47:43 +08:00
|
|
|
if (tmp.IsValid())
|
|
|
|
result.push_back(tmp);
|
2012-06-29 00:23:19 +08:00
|
|
|
}
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-09-04 14:39:44 +08:00
|
|
|
jenv->DeleteLocalRef(jpkgs);
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-06-29 00:23:19 +08:00
|
|
|
if (self_attached)
|
|
|
|
{
|
2012-10-24 16:47:43 +08:00
|
|
|
JavaContext->DetachCurrentThread();
|
2012-06-29 00:23:19 +08:00
|
|
|
}
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-06-29 00:23:19 +08:00
|
|
|
LOGD("JavaBasedPackageManager::GetInstalledPackages() end");
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-06-29 00:23:19 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-09-04 14:39:44 +08:00
|
|
|
// IMPORTANT: This method can be called only if thread is attached to Dalvik
|
2012-06-29 00:23:19 +08:00
|
|
|
PackageInfo JavaBasedPackageManager::ConvertPackageFromJava(jobject package, JNIEnv* jenv)
|
|
|
|
{
|
|
|
|
jclass jclazz = jenv->GetObjectClass(package);
|
|
|
|
jfieldID jfield = jenv->GetFieldID(jclazz, "packageName", "Ljava/lang/String;");
|
|
|
|
jstring jnameobj = static_cast<jstring>(jenv->GetObjectField(package, jfield));
|
|
|
|
const char* jnamestr = jenv->GetStringUTFChars(jnameobj, NULL);
|
|
|
|
string name(jnamestr);
|
2012-09-04 14:39:44 +08:00
|
|
|
jenv->DeleteLocalRef(jnameobj);
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-06-29 00:23:19 +08:00
|
|
|
jfield = jenv->GetFieldID(jclazz, "versionName", "Ljava/lang/String;");
|
|
|
|
jstring jversionobj = static_cast<jstring>(jenv->GetObjectField(package, jfield));
|
|
|
|
const char* jversionstr = jenv->GetStringUTFChars(jversionobj, NULL);
|
|
|
|
string verison(jversionstr);
|
2012-09-04 14:39:44 +08:00
|
|
|
jenv->DeleteLocalRef(jversionobj);
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-06-29 00:23:19 +08:00
|
|
|
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)
|
|
|
|
{
|
2012-10-24 16:47:43 +08:00
|
|
|
jclazz = jenv->GetObjectClass(package);
|
|
|
|
jfield = jenv->GetFieldID(jclazz, "applicationInfo", "Landroid/content/pm/ApplicationInfo;");
|
|
|
|
jobject japp_info = jenv->GetObjectField(package, jfield);
|
|
|
|
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(jpathobj);
|
2012-06-29 00:23:19 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-10-24 16:47:43 +08:00
|
|
|
path = "/data/data/" + name + "/lib";
|
2012-06-29 00:23:19 +08:00
|
|
|
}
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-06-29 00:23:19 +08:00
|
|
|
return PackageInfo(name, path, verison);
|
|
|
|
}
|
|
|
|
|
|
|
|
JavaBasedPackageManager::~JavaBasedPackageManager()
|
|
|
|
{
|
|
|
|
JNIEnv* jenv;
|
|
|
|
bool self_attached;
|
|
|
|
|
|
|
|
LOGD("JavaBasedPackageManager::~JavaBasedPackageManager() begin");
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-06-29 00:23:19 +08:00
|
|
|
JavaContext->GetEnv((void**)&jenv, JNI_VERSION_1_6);
|
|
|
|
self_attached = (JNI_EDETACHED == JavaContext->GetEnv((void**)&jenv, JNI_VERSION_1_6));
|
|
|
|
if (self_attached)
|
|
|
|
{
|
2012-10-24 16:47:43 +08:00
|
|
|
JavaContext->AttachCurrentThread(&jenv, NULL);
|
2012-06-29 00:23:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
jenv->DeleteGlobalRef(JavaPackageManager);
|
|
|
|
|
|
|
|
if (self_attached)
|
|
|
|
{
|
2012-10-24 16:47:43 +08:00
|
|
|
JavaContext->DetachCurrentThread();
|
2012-06-29 00:23:19 +08:00
|
|
|
}
|
|
|
|
LOGD("JavaBasedPackageManager::~JavaBasedPackageManager() end");
|
|
|
|
}
|