static bool rho_cast_java_ruby_init(JNIEnv *env) { static rho::common::CMutex rho_fd_mtx; static bool initialized = false; if (initialized) return true; rho::common::CMutexLock guard(rho_cast_java_ruby_mtx); if (initialized) return true; clsString = getJNIClass(RHODES_JAVA_CLASS_STRING); if (!clsString) return false; clsMap = getJNIClass(RHODES_JAVA_CLASS_MAP); if (!clsMap) return false; clsSet = getJNIClass(RHODES_JAVA_CLASS_SET); if (!clsSet) return false; clsIterator = getJNIClass(RHODES_JAVA_CLASS_ITERATOR); if (!clsIterator) return false; midMapGet = getJNIClassMethod(env, clsMap, "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); if (!midMapGet) return false; midMapKeySet = getJNIClassMethod(env, clsMap, "keySet", "()Ljava/util/Set;"); if (!midMapKeySet) return false; midSetIterator = getJNIClassMethod(env, clsSet, "iterator", "()Ljava/util/Iterator;"); if (!midSetIterator) return false; midIteratorHasNext = getJNIClassMethod(env, clsIterator, "hasNext", "()Z"); if (!midIteratorHasNext) return false; midIteratorNext = getJNIClassMethod(env, clsIterator, "next", "()Ljava/lang/Object;"); if (!midIteratorNext) return false; initialized = true; return true; }
RHO_GLOBAL jobject JNICALL Java_com_rhomobile_rhodes_socket_SSLImpl_getRemoteSockAddr (JNIEnv *env, jobject, jint sock) { jclass clsSockAddr = getJNIClass(RHODES_JAVA_CLASS_RHOSOCKADDR); if (!clsSockAddr) return NULL; jclass clsInetAddr = getJNIClass(RHODES_JAVA_CLASS_INET4ADDRESS); if (!clsInetAddr) return NULL; jmethodID midSockAddr = getJNIClassMethod(env, clsSockAddr, "<init>", "()V"); if (!midSockAddr) return NULL; jmethodID midInetAddr = getJNIClassMethod(env, clsInetAddr, "<init>", "([BLjava/lang/String;)V"); if (!midInetAddr) return NULL; jfieldID fidInetAddr = getJNIClassField(env, clsSockAddr, "host", "Ljava/net/InetAddress;"); if (!fidInetAddr) return NULL; jfieldID fidPort = getJNIClassField(env, clsSockAddr, "port", "I"); if (!fidPort) return NULL; jbyteArray array = env->NewByteArray(4); if (!array) return NULL; jbyte *arr = env->GetByteArrayElements(array, NULL); if (!arr) return NULL; sockaddr_in sa; socklen_t salen = sizeof(sa); int e = ::getpeername(sock, (sockaddr*)&sa, &salen); if (e == -1) { RAWLOG_ERROR1("getpeername failed: %d", errno); return NULL; } uint32_t addr = ntohl(sa.sin_addr.s_addr); arr[3] = (jbyte)(addr & 0xFF); addr >>= 8; arr[2] = (jbyte)(addr & 0xFF); addr >>= 8; arr[1] = (jbyte)(addr & 0xFF); addr >>= 8; arr[0] = (jbyte)(addr & 0xFF); env->ReleaseByteArrayElements(array, arr, 0); jstring ipaddrObj = rho_cast<jstring>(::inet_ntoa(sa.sin_addr)); jobject inetaddrObj = env->NewObject(clsInetAddr, midInetAddr, array, ipaddrObj); env->DeleteLocalRef(ipaddrObj); if (!inetaddrObj) return NULL; jobject sockaddrObj = env->NewObject(clsSockAddr, midSockAddr); if (!sockaddrObj) return NULL; env->SetObjectField(sockaddrObj, fidInetAddr, inetaddrObj); env->SetIntField(sockaddrObj, fidPort, ntohs(sa.sin_port)); env->DeleteLocalRef(inetaddrObj); return sockaddrObj; }
void AndroidImage::init(JNIEnv *env) { jclass cls = getJNIClass(RHODES_JAVA_CLASS_BITMAP); if (!cls) return; jmethodID midWidth = getJNIClassMethod(env, cls, "getWidth", "()I"); if (!midWidth) return; jmethodID midHeight = getJNIClassMethod(env, cls, "getHeight", "()I"); if (!midHeight) return; m_width = env->CallIntMethod(*m_bitmap, midWidth); m_height = env->CallIntMethod(*m_bitmap, midHeight); }
static VALUE createHashFromContact(jobject contactObj) { if (logging_enable) RAWLOG_INFO("createHashFromContact() START"); JNIEnv *env = jnienv(); jclass contactCls = getJNIClass(RHODES_JAVA_CLASS_CONTACT); if (!contactCls) return Qnil; jclass fieldCls = getJNIClass(RHODES_JAVA_CLASS_CONTACT_FIELD); if (!fieldCls) return Qnil; jmethodID contactGetFieldMID = getJNIClassMethod(env, contactCls, "getField", "(I)Ljava/lang/String;"); if (!contactGetFieldMID) return Qnil; CHoldRubyValue contactHash(rho_ruby_createHash()); // contact.moveToBegin(); int i; for (i = 0; i < PB_FIELDS_COUNT; i++) { jstring value = (jstring)env->CallObjectMethod(contactObj, contactGetFieldMID, i); if (value != NULL) { addStrToHash(contactHash, field_names[i], rho_cast<std::string>(value).c_str()); } env->DeleteLocalRef(value); } if (logging_enable) RAWLOG_INFO("createHashFromContact() FINISH"); return contactHash; }
SSLImpl::SSLImpl() { JNIEnv *env = jnienv(); cls = getJNIClass(RHODES_JAVA_CLASS_SSLIMPL); if (!cls) return; midConstructor = getJNIClassMethod(env, cls, "<init>", "()V"); if (!midConstructor) return; midConnect = getJNIClassMethod(env, cls, "connect", "(IZ)Z"); if (!midConnect) return; midShutdown = getJNIClassMethod(env, cls, "shutdown", "()V"); if (!midShutdown) return; midSend = getJNIClassMethod(env, cls, "send", "([B)Z"); if (!midSend) return; midRecv = getJNIClassMethod(env, cls, "recv", "([B)I"); if (!midRecv) return; }
static VALUE getRecord(void *pb, const char *name) { if (logging_enable) RAWLOG_INFO("getRecord() START"); jobject obj = (jobject)pb; JNIEnv *env = jnienv(); if (!env) { if (logging_enable) RAWLOG_INFO("getRecord() FINISH return NIL0"); return Qnil; } jclass cls = getJNIClass(RHODES_JAVA_CLASS_PHONEBOOK); if (!cls) { if (logging_enable) RAWLOG_INFO("getRecord() FINISH return NIL1"); return Qnil; } jmethodID mid = getJNIClassMethod(env, cls, name, "()Lcom/rhomobile/rhodes/phonebook/Contact;"); if (!mid) { if (logging_enable) RAWLOG_INFO("getRecord() FINISH return NIL2"); return Qnil; } jobject recordObj = env->CallObjectMethod(obj, mid); if (!recordObj) { if (logging_enable) RAWLOG_INFO("getRecord() FINISH return NIL"); return Qnil; } if (logging_enable) RAWLOG_INFO("getRecord() FINISH"); return createHashFromContact(recordObj); }
RHO_GLOBAL int rho_sysimpl_get_property(char* szPropName, VALUE* resValue) { JNIEnv *env = jnienv(); jclass cls = getJNIClass(RHODES_JAVA_CLASS_RHODES_SERVICE); if (!cls) return 0; jmethodID mid = getJNIClassStaticMethod(env, cls, "getProperty", "(Ljava/lang/String;)Ljava/lang/Object;"); if (!mid) return 0; jhstring propNameObj = rho_cast<jhstring>(szPropName); jhobject result = jhobject(env->CallStaticObjectMethod(cls, mid, propNameObj.get())); if (!result) return 0; jclass clsBoolean = getJNIClass(RHODES_JAVA_CLASS_BOOLEAN); jclass clsInteger = getJNIClass(RHODES_JAVA_CLASS_INTEGER); jclass clsFloat = getJNIClass(RHODES_JAVA_CLASS_FLOAT); jclass clsDouble = getJNIClass(RHODES_JAVA_CLASS_DOUBLE); jclass clsString = getJNIClass(RHODES_JAVA_CLASS_STRING); if (env->IsInstanceOf(result.get(), clsBoolean)) { jmethodID midValue = getJNIClassMethod(env, clsBoolean, "booleanValue", "()Z"); *resValue = rho_ruby_create_boolean((int)env->CallBooleanMethod(result.get(), midValue)); return 1; } else if (env->IsInstanceOf(result.get(), clsInteger)) { jmethodID midValue = getJNIClassMethod(env, clsInteger, "intValue", "()I"); *resValue = rho_ruby_create_integer((int)env->CallIntMethod(result.get(), midValue)); return 1; } else if (env->IsInstanceOf(result.get(), clsFloat)) { jmethodID midValue = getJNIClassMethod(env, clsFloat, "floatValue", "()F"); *resValue = rho_ruby_create_double((double)env->CallFloatMethod(result.get(), midValue)); return 1; } else if (env->IsInstanceOf(result.get(), clsDouble)) { jmethodID midValue = getJNIClassMethod(env, clsDouble, "doubleValue", "()D"); *resValue = rho_ruby_create_double((double)env->CallDoubleMethod(result.get(), midValue)); return 1; } else if (env->IsInstanceOf(result.get(), clsString)) { jstring resStrObj = (jstring)result.get(); *resValue = rho_ruby_create_string(rho_cast<std::string>(resStrObj).c_str()); return 1; } return 0; }
RhoValueConverter::RhoValueConverter(JNIEnv *e) :env(e), init(false) { clsHashMap = getJNIClass(RHODES_JAVA_CLASS_HASHMAP); if (!clsHashMap) return; clsVector = getJNIClass(RHODES_JAVA_CLASS_VECTOR); if (!clsVector) return; midHashMapConstructor = getJNIClassMethod(env, clsHashMap, "<init>", "()V"); if (!midHashMapConstructor) return; midVectorConstructor = getJNIClassMethod(env, clsVector, "<init>", "()V"); if (!midVectorConstructor) return; midPut = getJNIClassMethod(env, clsHashMap, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); if (!midPut) return; midAddElement = getJNIClassMethod(env, clsVector, "addElement", "(Ljava/lang/Object;)V"); if (!midAddElement) return; init = true; }
VALUE convertJavaMapToRubyHash(jobject objMap) { jclass clsMap = getJNIClass(RHODES_JAVA_CLASS_MAP); if (!clsMap) return Qnil; jclass clsSet = getJNIClass(RHODES_JAVA_CLASS_SET); if (!clsSet) return Qnil; jclass clsIterator = getJNIClass(RHODES_JAVA_CLASS_ITERATOR); if (!clsIterator) return Qnil; JNIEnv *env = jnienv(); jmethodID midGet = getJNIClassMethod(env, clsMap, "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); if (!midGet) return Qnil; jmethodID midKeySet = getJNIClassMethod(env, clsMap, "keySet", "()Ljava/util/Set;"); if (!midKeySet) return Qnil; jmethodID midIterator = getJNIClassMethod(env, clsSet, "iterator", "()Ljava/util/Iterator;"); if (!midIterator) return Qnil; jmethodID midHasNext = getJNIClassMethod(env, clsIterator, "hasNext", "()Z"); if (!midHasNext) return Qnil; jmethodID midNext = getJNIClassMethod(env, clsIterator, "next", "()Ljava/lang/Object;"); if (!midNext) return Qnil; jobject objSet = env->CallObjectMethod(objMap, midKeySet); if (!objSet) return Qnil; jobject objIterator = env->CallObjectMethod(objSet, midIterator); if (!objIterator) return Qnil; VALUE retval = createHash(); while(env->CallBooleanMethod(objIterator, midHasNext)) { jstring objKey = (jstring)env->CallObjectMethod(objIterator, midNext); if (!objKey) return Qnil; jstring objValue = (jstring)env->CallObjectMethod(objMap, midGet, objKey); if (!objValue) return Qnil; std::string const &strKey = rho_cast<std::string>(objKey); std::string const &strValue = rho_cast<std::string>(objValue); addStrToHash(retval, strKey.c_str(), strValue.c_str()); env->DeleteLocalRef(objKey); env->DeleteLocalRef(objValue); } return retval; }
static void doContactOp(jobject pbObj, jobject contactObj, const char *name) { JNIEnv *env = jnienv(); jclass pbCls = getJNIClass(RHODES_JAVA_CLASS_PHONEBOOK); if (!pbCls) return; jmethodID mid = getJNIClassMethod(env, pbCls, name, "(Lcom/rhomobile/rhodes/phonebook/Contact;)V"); if (!mid) return; env->CallVoidMethod(pbObj, mid, contactObj); }
RHO_GLOBAL void closePhonebook(void* pb) { JNIEnv *env = jnienv(); jobject obj = (jobject)pb; jclass cls = getJNIClass(RHODES_JAVA_CLASS_PHONEBOOK); if (!cls) return; jmethodID mid = getJNIClassMethod(env, cls, "close", "()V"); if (!mid) return; env->CallVoidMethod(obj, mid); env->DeleteGlobalRef(obj); }
RHO_GLOBAL void* openPhonebook() { JNIEnv *env = jnienv(); jclass cls = getJNIClass(RHODES_JAVA_CLASS_PHONEBOOK); if (!cls) return NULL; jmethodID cid = getJNIClassMethod(env, cls, "<init>", "()V"); if (!cid) return NULL; jobject local = env->NewObject(cls, cid); jobject obj = env->NewGlobalRef(local); env->DeleteLocalRef(local); return obj; }
RHO_GLOBAL void* createRecord(void* pb) { JNIEnv *env = jnienv(); jclass cls = getJNIClass(RHODES_JAVA_CLASS_CONTACT); if (!cls) return NULL; jmethodID cid = getJNIClassMethod(env, cls, "<init>", "()V"); if (!cid) return NULL; jobject local = env->NewObject(cls, cid); jobject obj = env->NewGlobalRef(local); env->DeleteLocalRef(local); return obj; }
jobject date_cast<jobject, VALUE>(VALUE rDate) { RHO_TRACE("dateFromRuby"); if (NIL_P(rDate)) { RHO_TRACE("dateFromRuby - NIL object"); return NULL; } RHO_TRACE("dateFromRuby - check for string type"); if (TYPE(rDate) == T_STRING) { RHO_TRACE("dateFromRuby - converting from string"); if (strlen(RSTRING_PTR(rDate)) == 0) { RHO_TRACE("dateFromRuby - empty string"); return NULL; } rDate = rb_funcall(rb_cTime, rb_intern("parse"), 1, rDate); RHO_TRACE("dateFromRuby - converted to ruby date"); } VALUE cDate = rb_class_of(rDate); if (!rb_equal(cDate, rb_cTime)) rb_raise(rb_eArgError, "Wrong type of parameter: %s (Time expected)", rb_class2name(cDate)); RHO_TRACE("dateFromRuby (2)"); int year = NUM2INT(rb_funcall(rDate, rb_intern("year"), 0)) - 1900; RHO_TRACE("dateFromRuby (3)"); int month = NUM2INT(rb_funcall(rDate, rb_intern("month"), 0)) - 1; RHO_TRACE("dateFromRuby (4)"); int day = NUM2INT(rb_funcall(rDate, rb_intern("day"), 0)); RHO_TRACE("dateFromRuby (5)"); int hour = NUM2INT(rb_funcall(rDate, rb_intern("hour"), 0)); RHO_TRACE("dateFromRuby (6)"); int minute = NUM2INT(rb_funcall(rDate, rb_intern("min"), 0)); RHO_TRACE("dateFromRuby (7)"); int second = NUM2INT(rb_funcall(rDate, rb_intern("sec"), 0)); RHO_TRACE("dateFromRuby (8)"); JNIEnv *env = jnienv(); jclass cls = getJNIClass(RHODES_JAVA_CLASS_DATE); if (!cls) return NULL; jmethodID mid = getJNIClassMethod(env, cls, "<init>", "(IIIIII)V"); if (!mid) return NULL; RHO_TRACE("dateFromRuby (9)"); jobject jDate = env->NewObject(cls, mid, year, month, day, hour, minute, second); return jDate; }
void AndroidDrawingContext::drawText(int x, int y, int width, int height, String const &text, int color) { RHO_MAP_TRACE4("drawText: x=%d, y=%d, text=%s, color=%d", x, y, text.c_str(), color); JNIEnv *env = jnienv(); jclass cls = env->GetObjectClass(m_device); if (!cls) return; jmethodID mid = getJNIClassMethod(env, cls, "drawText", "(Landroid/graphics/Canvas;IIIILjava/lang/String;I)V"); if (!mid) return; env->CallVoidMethod(m_device, mid, m_canvas, x, y, width, height, rho_cast<jhstring>(text).get(), color); RHO_MAP_TRACE("drawText done"); }
void AndroidMapDevice::requestRedraw() { RHO_MAP_TRACE1("requestRedraw: m_jdevice=%p", m_jdevice); if (m_jdevice) { JNIEnv *env = jnienv(); jclass cls = getJNIClass(RHODES_JAVA_CLASS_MAPVIEW); if (!cls) return; jmethodID mid = getJNIClassMethod(env, cls, "redraw", "()V"); if (!mid) return; env->CallVoidMethod(m_jdevice, mid); } RHO_MAP_TRACE("requestRedraw done"); }
AndroidMapDevice::~AndroidMapDevice() { RHO_MAP_TRACE("AndroidMapDevice: dtor start"); rho_param_free(m_params); JNIEnv *env = jnienv(); if (m_jdevice) { JNIEnv *env = jnienv(); jclass cls = getJNIClass(RHODES_JAVA_CLASS_MAPVIEW); if (!cls) return; jmethodID mid = getJNIClassMethod(env, cls, "destroyDevice", "()V"); if (!mid) return; env->CallVoidMethod(m_jdevice, mid); env->DeleteGlobalRef(m_jdevice); } RHO_MAP_TRACE("AndroidMapDevice: dtor finish"); }
RHO_GLOBAL void* openPhonebookRecord(void* pb, char* id) { if (logging_enable) RAWLOG_INFO("openPhonebookRecord() START"); JNIEnv *env = jnienv(); jobject obj = (jobject)pb; jclass cls = getJNIClass(RHODES_JAVA_CLASS_PHONEBOOK); if (!cls) return NULL; jmethodID mid = getJNIClassMethod(env, cls, "getRecord", "(Ljava/lang/String;)Lcom/rhomobile/rhodes/phonebook/Contact;"); if (!mid) return NULL; jhstring objId = rho_cast<jhstring>(id); jhobject recordObj = jhobject(env->CallObjectMethod(obj, mid, objId.get())); if (!recordObj) return NULL; jhobject retval = jhobject(env->NewGlobalRef(recordObj.get())); if (logging_enable) RAWLOG_INFO("openPhonebookRecord() FINISH"); if (!retval) return NULL; return retval.release(); }
void AndroidDrawingContext::drawImage(int x, int y, IDrawingImage *image) { if (!image) return; RHO_MAP_TRACE3("drawImage: x=%d, y=%d, image=%p", x, y, image); jobject bitmap = ((AndroidImage*)image)->bitmap(); JNIEnv *env = jnienv(); jclass cls = env->GetObjectClass(m_device); if (!cls) return; jmethodID mid = getJNIClassMethod(env, cls, "drawImage", "(Landroid/graphics/Canvas;IILandroid/graphics/Bitmap;)V"); if (!mid) return; env->CallVoidMethod(m_device, mid, m_canvas, x, y, bitmap); RHO_MAP_TRACE("drawImage done"); }
RHO_GLOBAL int setRecordValue(void* record, char* property, char* value) { jobject contactObj = (jobject)record; JNIEnv *env = jnienv(); jclass contactCls = getJNIClass(RHODES_JAVA_CLASS_CONTACT); if (!contactCls) return 0; jmethodID mid = getJNIClassMethod(env, contactCls, "setField", "(ILjava/lang/String;)V"); if (!mid) return 0; jhstring objValue = rho_cast<jhstring>(value); int index = get_value_index_by_name(property); if (index >= 0) { env->CallVoidMethod(contactObj, mid, index, objValue.get()); } else { RAWLOG_ERROR("Phonebook.cpp invalid property name for Contact Record"); } return 1; }
VALUE date_cast<VALUE, jobject>(jobject jDate) { if (!jDate) return Qnil; RHO_TRACE("dateToRuby (1)"); JNIEnv *env = jnienv(); jclass cls = getJNIClass(RHODES_JAVA_CLASS_DATE); if (!cls) return Qnil; jmethodID midYear = getJNIClassMethod(env, cls, "getYear", "()I"); if (!midYear) return Qnil; jmethodID midMonth = getJNIClassMethod(env, cls, "getMonth", "()I"); if (!midMonth) return Qnil; jmethodID midDay = getJNIClassMethod(env, cls, "getDate", "()I"); if (!midDay) return Qnil; jmethodID midHours = getJNIClassMethod(env, cls, "getHours", "()I"); if (!midHours) return Qnil; jmethodID midMinutes = getJNIClassMethod(env, cls, "getMinutes", "()I"); if (!midMinutes) return Qnil; jmethodID midSeconds = getJNIClassMethod(env, cls, "getSeconds", "()I"); if (!midSeconds) return Qnil; RHO_TRACE("dateToRuby (2)"); int year = env->CallIntMethod(jDate, midYear) + 1900; int month = env->CallIntMethod(jDate, midMonth) + 1; int day = env->CallIntMethod(jDate, midDay); int hour = env->CallIntMethod(jDate, midHours); int minute = env->CallIntMethod(jDate, midMinutes); int second = env->CallIntMethod(jDate, midSeconds); RHO_TRACE("dateToRuby (3)"); VALUE rDate = rb_funcall(rb_cTime, rb_intern("mktime"), 7, INT2FIX(year), INT2FIX(month), INT2FIX(day), INT2FIX(hour), INT2FIX(minute), INT2FIX(second), INT2FIX(0)); RHO_TRACE("dateToRuby (4)"); return rDate; }
RHO_GLOBAL VALUE event_fetch(VALUE rParams) { RHO_TRACE("event_fetch"); JNIEnv *env = jnienv(); jclass& cls = getJNIClass(RHODES_JAVA_CLASS_EVENT_STORE); if (!cls) return Qnil; jmethodID mid = getJNIClassStaticMethod(env, cls, "fetch", "(Ljava/util/Date;Ljava/util/Date;Z)Ljava/lang/Object;"); if (!mid) return Qnil; VALUE start_date = rb_hash_aref(rParams, rb_str_new2(RUBY_EV_START_DATE)); VALUE end_date = rb_hash_aref(rParams, rb_str_new2(RUBY_EV_END_DATE)); VALUE include_repeating = rb_hash_aref(rParams, rb_str_new2(RUBY_FETCH_include_repeating)); RHO_TRACE("event_fetch: start date"); jobject jStartDate = date_cast<jobject>(start_date); RHO_TRACE("event_fetch: end date"); jobject jEndDate = date_cast<jobject>(end_date); RHO_TRACE("event_fetch: EventStore.fetch()"); jobject jRet = env->CallStaticObjectMethod(cls, mid, jStartDate, jEndDate, (jboolean)rho_ruby_get_bool(include_repeating)); RHO_TRACE("event_fetch: release java references"); env->DeleteLocalRef(jStartDate); env->DeleteLocalRef(jEndDate); jclass& clsString = getJNIClass(RHODES_JAVA_CLASS_STRING); if (!clsString) return Qnil; if (env->IsInstanceOf(jRet, clsString)) { std::string error = rho_cast<std::string>(env, (jstring)jRet); env->DeleteLocalRef(jRet); rb_raise(rb_eRuntimeError, "Can't fetch events: %s", error.c_str()); return Qnil; } jclass clsVector = getJNIClass(RHODES_JAVA_CLASS_VECTOR); if (!clsVector) return Qnil; jmethodID midSize = getJNIClassMethod(env, clsVector, "size", "()I"); if (!midSize) return Qnil; jmethodID midGet = getJNIClassMethod(env, clsVector, "get", "(I)Ljava/lang/Object;"); if (!midGet) return Qnil; RHO_TRACE("event_fetch (5)"); VALUE ret = rb_ary_new(); RHO_TRACE("event_fetch (6)"); for (int i = 0, lim = env->CallIntMethod(jRet, midSize); i != lim; ++i) { RHO_TRACE("event_fetch (6.1)"); jobject jEvent = env->CallObjectMethod(jRet, midGet, i); RHO_TRACE("event_fetch (6.2)"); VALUE rEvent = event_cast<VALUE>(jEvent); RHO_TRACE("event_fetch (6.3)"); env->DeleteLocalRef(jEvent); RHO_TRACE("event_fetch (6.4)"); rb_ary_push(ret, rEvent); RHO_TRACE("event_fetch (6.5)"); } RHO_TRACE("event_fetch (7)"); env->DeleteLocalRef(jRet); return ret; }
jobject rhoconnect_jni_parseobjectnotify(JNIEnv * env, const char* res) { LOG(TRACE) + "rhoconnect_jni_parseobjectnotify: " + res; RHO_CONNECT_OBJECT_NOTIFY notify; memset(¬ify, 0, sizeof(notify)); rho_connectclient_parse_objectnotify(res, ¬ify); static jclass clsNotify = getJNIClass(RHOCONNECT_JAVA_CLASS_OBJECTNOTIFY); if (!clsNotify) { LOG(ERROR) + "Cannot get RhoConnectObjectNotify class"; return NULL; } static jmethodID midNotify = getJNIClassMethod(env, clsNotify, "<init>", "()V"); if (!midNotify) { LOG(ERROR) + "Cannot get RhoConnectObjectNotify constructor method id"; return NULL; } static jfieldID fidDeletedObjects = getJNIClassField(env, clsNotify, "mDeletedObjects", "[Ljava/lang/String;"); if (!fidDeletedObjects) { LOG(ERROR) + "Cannot get RhoConnectObjectNotify.mDeletedObjects field id"; return NULL; } static jfieldID fidUpdatedObjects = getJNIClassField(env, clsNotify, "mUpdatedObjects", "[Ljava/lang/String;"); if (!fidUpdatedObjects) { LOG(ERROR) + "Cannot get RhoConnectObjectNotify.mUpdatedObjects field id"; return NULL; } static jfieldID fidCreatedObjects = getJNIClassField(env, clsNotify, "mCreatedObjects", "[Ljava/lang/String;"); if (!fidCreatedObjects) { LOG(ERROR) + "Cannot get RhoConnectObjectNotify.mCreatedObjects field id"; return NULL; } static jfieldID fidDeletedIds = getJNIClassField(env, clsNotify, "mDeletedSrcIds", "[I"); if (!fidDeletedIds) { LOG(ERROR) + "Cannot get RhoConnectObjectNotify.mDeletedSrcIds field id"; return NULL; } static jfieldID fidUpdatedIds = getJNIClassField(env, clsNotify, "mUpdatedSrcIds", "[I"); if (!fidUpdatedIds) { LOG(ERROR) + "Cannot get RhoConnectObjectNotify.mUpdatedSrcIds field id"; return NULL; } static jfieldID fidCreatedIds = getJNIClassField(env, clsNotify, "mCreatedSrcIds", "[I"); if (!fidCreatedIds) { LOG(ERROR) + "Cannot get RhoConnectObjectNotify.mCreatedSrcIds field id"; return NULL; } jobject jNotify = env->NewObject(clsNotify, midNotify); if (!jNotify) { LOG(ERROR) + "Cannot create RhoConnectObjectNotify instance"; return NULL; } rhoconnect_jni_fill_objectnotify(env, jNotify, notify.deleted_count, notify.deleted_objects, notify.deleted_source_ids, fidDeletedObjects, fidDeletedIds); rhoconnect_jni_fill_objectnotify(env, jNotify, notify.updated_count, notify.updated_objects, notify.updated_source_ids, fidUpdatedObjects, fidUpdatedIds); rhoconnect_jni_fill_objectnotify(env, jNotify, notify.created_count, notify.created_objects, notify.created_source_ids, fidCreatedObjects, fidCreatedIds); rho_connectclient_free_sync_objectnotify(¬ify); LOG(TRACE) + "Returning Java RhoConnectObjectNotify reference: " + (int)jNotify; return jNotify; }
jobject rhoconnect_jni_parsenotify(JNIEnv * env, const char* res) { LOG(TRACE) + "rhoconnect_jni_parsenotify: " + res; RHO_CONNECT_NOTIFY notify; memset(¬ify, 0, sizeof(notify)); rho_connectclient_parsenotify(res, ¬ify); static jclass clsNotify = getJNIClass(RHOCONNECT_JAVA_CLASS_NOTIFY); if (!clsNotify) return NULL; static jmethodID midNotify = getJNIClassMethod(env, clsNotify, "<init>", "()V"); if (!midNotify) return NULL; static jfieldID fidTotalCount = getJNIClassField(env, clsNotify, "mTotalCount", "I"); if (!fidTotalCount) return NULL; static jfieldID fidProcCount = getJNIClassField(env, clsNotify, "mProcessedCount", "I"); if (!fidProcCount) return NULL; static jfieldID fidCumulativeCount = getJNIClassField(env, clsNotify, "mCumulativeCount", "I"); if (!fidCumulativeCount) return NULL; static jfieldID fidSrcId = getJNIClassField(env, clsNotify, "mSourceId", "I"); if (!fidSrcId) return NULL; static jfieldID fidErrorCode = getJNIClassField(env, clsNotify, "mErrorCode", "I"); if (!fidErrorCode) return NULL; static jfieldID fidSrcName = getJNIClassField(env, clsNotify, "mSourceName", "Ljava/lang/String;"); if (!fidSrcName) return NULL; static jfieldID fidStatus = getJNIClassField(env, clsNotify, "mStatus", "Ljava/lang/String;"); if (!fidStatus) return NULL; static jfieldID fidSyncType = getJNIClassField(env, clsNotify, "mSyncType", "Ljava/lang/String;"); if (!fidSyncType) return NULL; static jfieldID fidErrMsg = getJNIClassField(env, clsNotify, "mErrorMessage", "Ljava/lang/String;"); if (!fidErrMsg) return NULL; static jfieldID fidParams = getJNIClassField(env, clsNotify, "mParams", "Ljava/lang/String;"); if (!fidParams) return NULL; jobject jNotify = env->NewObject(clsNotify, midNotify); if (!jNotify) return NULL; env->SetIntField(jNotify, fidTotalCount, notify.total_count); env->SetIntField(jNotify, fidProcCount, notify.processed_count); env->SetIntField(jNotify, fidCumulativeCount, notify.cumulative_count); env->SetIntField(jNotify, fidSrcId, notify.source_id); env->SetIntField(jNotify, fidErrorCode, notify.error_code); jhstring jhSrcName = rho_cast<jhstring>(env, notify.source_name); jhstring jhStatus = rho_cast<jhstring>(env, notify.status); jhstring jhSyncType = rho_cast<jhstring>(env, notify.sync_type); jhstring jhErrMsg = rho_cast<jhstring>(env, notify.error_message); jhstring jhParams = rho_cast<jhstring>(env, notify.callback_params); env->SetObjectField(jNotify, fidSrcName, jhSrcName.get()); env->SetObjectField(jNotify, fidStatus, jhStatus.get()); env->SetObjectField(jNotify, fidSyncType, jhSyncType.get()); env->SetObjectField(jNotify, fidErrMsg, jhErrMsg.get()); env->SetObjectField(jNotify, fidParams, jhParams.get()); rho_connectclient_free_syncnotify(¬ify); return jNotify; }
RHO_GLOBAL VALUE getallPhonebookRecords(void* pb) { if (logging_enable) RAWLOG_INFO("getallPhonebookRecords() START"); jobject phonebookObj = (jobject)pb; JNIEnv *env = jnienv(); jclass phonebookCls = getJNIClass(RHODES_JAVA_CLASS_PHONEBOOK); if (!phonebookCls) return Qnil; jclass contactCls = getJNIClass(RHODES_JAVA_CLASS_CONTACT); if (!contactCls) return Qnil; jmethodID phonebookPrepareFullListMID = getJNIClassMethod(env, phonebookCls, "prepareFullList", "()V"); if (!phonebookPrepareFullListMID) return Qnil; jmethodID phonebookMoveToBeginMID = getJNIClassMethod(env, phonebookCls, "moveToBegin", "()V"); if (!phonebookMoveToBeginMID) return Qnil; jmethodID hasNextMID = getJNIClassMethod(env, phonebookCls, "hasNext", "()Z"); if (!hasNextMID) return Qnil; jmethodID nextMID = getJNIClassMethod(env, phonebookCls, "next", "()Ljava/lang/Object;"); if (!nextMID) return Qnil; jmethodID contactIdMID = getJNIClassMethod(env, contactCls, "id", "()Ljava/lang/String;"); if (!contactIdMID) return Qnil; jmethodID contactGetFieldMID = getJNIClassMethod(env, contactCls, "getField", "(I)Ljava/lang/String;"); if (!contactGetFieldMID) return Qnil; env->CallVoidMethod(phonebookObj, phonebookPrepareFullListMID); // pb.moveToBegin(); env->CallVoidMethod(phonebookObj, phonebookMoveToBeginMID); VALUE valGc = rho_ruby_disable_gc(); CHoldRubyValue hash(rho_ruby_createHash()); // while(pb.hasNext()) while(env->CallBooleanMethod(phonebookObj, hasNextMID)) { // Contact contact = (Contact)pb.next(); jobject contactObj = env->CallObjectMethod(phonebookObj, nextMID); if (!contactObj) return Qnil; // String id = contact.id(); jstring idObj = (jstring)env->CallObjectMethod(contactObj, contactIdMID); if (!idObj) return Qnil; //addHashToHash(hash, rho_cast<std::string>(idObj).c_str(), createHashFromContact(contactObj)); CHoldRubyValue contactHash(rho_ruby_createHash()); // contact.moveToBegin(); int i; for (i = 0; i < PB_FIELDS_COUNT; i++) { jstring value = (jstring)env->CallObjectMethod(contactObj, contactGetFieldMID, i); if (value != NULL) { addStrToHash(contactHash, field_names[i], rho_cast<std::string>(value).c_str()); env->DeleteLocalRef(value); } } addHashToHash(hash, rho_cast<std::string>(idObj).c_str(), contactHash); env->DeleteLocalRef(idObj); env->DeleteLocalRef(contactObj); } rho_ruby_enable_gc(valGc); if (logging_enable) RAWLOG_INFO("getallPhonebookRecords() FINISH"); return hash; }
bool RhoJniConvertor::initConvertor(JNIEnv *env) { RAWTRACE("RhoJniConvertor::initConvertor"); m_env = env; static bool initialized = false; if (initialized) return initialized; static rho::common::CMutex rho_cast_java_ruby_mtx; rho::common::CMutexLock guard(rho_cast_java_ruby_mtx); if (initialized) return initialized; clsBoolean = getJNIClass(RHODES_JAVA_CLASS_BOOLEAN); if (!clsBoolean) return false; clsInteger = getJNIClass(RHODES_JAVA_CLASS_INTEGER); if (!clsInteger) return false; clsDouble = getJNIClass(RHODES_JAVA_CLASS_DOUBLE); if (!clsDouble) return false; clsString = getJNIClass(RHODES_JAVA_CLASS_STRING); if (!clsString) return false; clsCollection = getJNIClass(RHODES_JAVA_CLASS_COLLECTION); if (!clsCollection) return false; clsMap = getJNIClass(RHODES_JAVA_CLASS_MAP); if (!clsMap) return false; clsSet = getJNIClass(RHODES_JAVA_CLASS_SET); if (!clsSet) return false; clsArrayList = getJNIClass(RHODES_JAVA_CLASS_ARRAYLIST); if (!clsArrayList) return false; clsHashMap = getJNIClass(RHODES_JAVA_CLASS_HASHMAP); if (!clsHashMap) return false; clsIterator = getJNIClass(RHODES_JAVA_CLASS_ITERATOR); if (!clsIterator) return false; midCollectionIterator = getJNIClassMethod(env, clsCollection, "iterator", "()Ljava/util/Iterator;"); if (!midCollectionIterator) return false; midMapGet = getJNIClassMethod(env, clsMap, "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); if (!midMapGet) return false; midMapKeySet = getJNIClassMethod(env, clsMap, "keySet", "()Ljava/util/Set;"); if (!midMapKeySet) return false; midArrayList = getJNIClassMethod(env, clsArrayList, "<init>", "()V"); if (!midArrayList) return false; midArrayListAdd = getJNIClassMethod(env, clsArrayList, "add", "(Ljava/lang/Object;)Z"); if (!midArrayListAdd) return false; midHashMap = getJNIClassMethod(env, clsHashMap, "<init>", "()V"); if (!midHashMap) return false; midHashMapPut = getJNIClassMethod(env, clsHashMap, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); if (!midHashMapPut) return false; midSetIterator = getJNIClassMethod(env, clsSet, "iterator", "()Ljava/util/Iterator;"); if (!midSetIterator) return false; midIteratorHasNext = getJNIClassMethod(env, clsIterator, "hasNext", "()Z"); if (!midIteratorHasNext) return false; midIteratorNext = getJNIClassMethod(env, clsIterator, "next", "()Ljava/lang/Object;"); if (!midIteratorNext) return false; midBooleanValue = getJNIClassMethod(env, clsBoolean, "booleanValue", "()Z"); if (!midBooleanValue) return false; midBooleanValueOf = getJNIClassStaticMethod(env, clsBoolean, "valueOf", "(Z)Ljava/lang/Boolean;"); if (!midBooleanValueOf) return false; midIntValue = getJNIClassMethod(env, clsInteger, "intValue", "()I"); if (!midIntValue) return false; midInteger = getJNIClassMethod(env, clsInteger, "<init>", "(I)V"); if (!midInteger) return false; midDoubleValue = getJNIClassMethod(env, clsDouble, "doubleValue", "()D"); if (!midDoubleValue) return false; midDoubleValueOf = getJNIClassStaticMethod(env, clsDouble, "valueOf", "(D)Ljava/lang/Double;"); if (!midDoubleValueOf) return false; return initialized = true; }
jobject event_cast<jobject, VALUE>(VALUE rEvent) { if (NIL_P(rEvent)) return NULL; RHO_TRACE("eventFromRuby (1)"); JNIEnv *env = jnienv(); if (!init_event_stuff(env)) return NULL; RHO_TRACE("eventFromRuby (2)"); VALUE rId = rb_hash_aref(rEvent, rb_str_new2(RUBY_EV_ID)); if (NIL_P(rId)) rId = rb_str_new2(""); Check_Type(rId, T_STRING); RHO_TRACE("eventFromRuby (3)"); jmethodID mid = getJNIClassMethod(env, clsEvent, "<init>", "(Ljava/lang/String;)V"); if (!mid) return NULL; jobject jEvent = env->NewObject(clsEvent, mid, rho_cast<jhstring>(RSTRING_PTR(rId)).get()); if (!jEvent) return NULL; RHO_TRACE("eventFromRuby (4)"); VALUE rTitle = rb_hash_aref(rEvent, rb_str_new2(RUBY_EV_TITLE)); if (!NIL_P(rTitle)) { Check_Type(rTitle, T_STRING); env->SetObjectField(jEvent, fidTitle, rho_cast<jhstring>(RSTRING_PTR(rTitle)).get()); } RHO_TRACE("eventFromRuby (5)"); VALUE rStartDate = rb_hash_aref(rEvent, rb_str_new2(RUBY_EV_START_DATE)); if (!NIL_P(rStartDate)) env->SetObjectField(jEvent, fidStartDate, date_cast<jobject>(rStartDate)); RHO_TRACE("eventFromRuby (6)"); VALUE rEndDate = rb_hash_aref(rEvent, rb_str_new2(RUBY_EV_END_DATE)); if (!NIL_P(rEndDate)) env->SetObjectField(jEvent, fidEndDate, date_cast<jobject>(rEndDate)); RHO_TRACE("eventFromRuby (7)"); VALUE rLastModified = rb_hash_aref(rEvent, rb_str_new2(RUBY_EV_LAST_MODIFIED)); if (!NIL_P(rLastModified)) env->SetObjectField(jEvent, fidLastModified, date_cast<jobject>(rLastModified)); RHO_TRACE("eventFromRuby (8)"); VALUE rLocation = rb_hash_aref(rEvent, rb_str_new2(RUBY_EV_LOCATION)); if (!NIL_P(rLocation)) { Check_Type(rLocation, T_STRING); env->SetObjectField(jEvent, fidLocation, rho_cast<jhstring>(RSTRING_PTR(rLocation)).get()); } RHO_TRACE("eventFromRuby (9)"); VALUE rNotes = rb_hash_aref(rEvent, rb_str_new2(RUBY_EV_NOTES)); if (!NIL_P(rNotes)) { Check_Type(rNotes, T_STRING); env->SetObjectField(jEvent, fidNotes, rho_cast<jhstring>(RSTRING_PTR(rNotes)).get()); } RHO_TRACE("eventFromRuby privacy"); VALUE rPrivacy = rb_hash_aref(rEvent, rb_str_new2(RUBY_EV_PRIVACY)); if (!NIL_P(rPrivacy)) { Check_Type(rPrivacy, T_STRING); env->SetObjectField(jEvent, fidPrivacy, rho_cast<jhstring>(RSTRING_PTR(rPrivacy)).get()); } RHO_TRACE("eventFromRuby recurrence"); VALUE rRecurrence = rb_hash_aref(rEvent, rb_str_new2(RUBY_EV_RECURRENCE)); if (!NIL_P(rRecurrence)) { Check_Type(rRecurrence, T_HASH); VALUE rFrequency = rb_hash_aref(rRecurrence, rb_str_new2(RUBY_EV_RECURRENCE_FREQUENCY)); Check_Type(rFrequency, T_STRING); const char *frequency = RSTRING_PTR(rFrequency); if ( strcasecmp(frequency, RUBY_EV_RECURRENCE_FREQUENCY_DAILY) != 0 && strcasecmp(frequency, RUBY_EV_RECURRENCE_FREQUENCY_WEEKLY) != 0 && strcasecmp(frequency, RUBY_EV_RECURRENCE_FREQUENCY_MONTHLY) != 0 && strcasecmp(frequency, RUBY_EV_RECURRENCE_FREQUENCY_YEARLY) != 0) { rb_raise(rb_eArgError, "Wrong recurrence frequency: %s", frequency); } env->SetObjectField(jEvent, fidFrequency, rho_cast<jhstring>(RSTRING_PTR(rFrequency)).get()); VALUE rInterval = rb_hash_aref(rRecurrence, rb_str_new2(RUBY_EV_RECURRENCE_INTERVAL)); rInterval = rb_funcall(rInterval, rb_intern("to_i"), 0); int interval = NUM2INT(rInterval); env->SetIntField(jEvent, fidInterval, interval); RAWTRACE1("eventFromRuby recurrence interval: %d", interval); VALUE rUntilDate = rb_hash_aref(rRecurrence, rb_str_new2(RUBY_EV_RECURRENCE_END)); if (!NIL_P(rUntilDate)) { env->SetObjectField(jEvent, fidRecurrenceEnd, date_cast<jobject>(rUntilDate)); RAWTRACE("eventFromRuby recurrence until date"); } VALUE rTimes = rb_funcall(rb_hash_aref(rRecurrence, rb_str_new2(RUBY_EV_RECURRENCE_COUNT)), rb_intern("to_i"), 0);; int times = NUM2INT(rTimes); env->SetIntField(jEvent, fidRecurrenceTimes, times); RAWTRACE1("eventFromRuby recurrence count: %d", times); } RHO_TRACE("eventFromRuby: return"); return jEvent; }