From: Andrzej Popowski Date: Mon, 7 Mar 2016 13:14:04 +0000 (+0100) Subject: [Contact] - Implementing getUsageCount() function X-Git-Tag: accepted/tizen/common/20160502.183526^2~4^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=13c4b796ee7b6fa3c505cd4a3e9a7fbe091074cc;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [Contact] - Implementing getUsageCount() function Change-Id: Ibfc3f57f67653695dcdd75b0eeedd810b0cedd31 Signed-off-by: Andrzej Popowski --- diff --git a/src/contact/contact_instance.cc b/src/contact/contact_instance.cc index db68f34..229a111 100755 --- a/src/contact/contact_instance.cc +++ b/src/contact/contact_instance.cc @@ -85,6 +85,7 @@ ContactInstance::ContactInstance() // Person REGISTER_SYNC("Person_link", PersonLink); REGISTER_SYNC("Person_unlink", PersonUnlink); + REGISTER_SYNC("Person_getUsageCount", PersonGetUsageCount); #undef REGISTER_SYNC #undef REGISTER_ASYNC @@ -593,5 +594,17 @@ void ContactInstance::PersonUnlink(const JsonValue& args, JsonObject& out) { LogAndReportError(status, &out); } +void ContactInstance::PersonGetUsageCount(const JsonValue& args, JsonObject& out) { + CHECK_PRIVILEGE_ACCESS(kPrivilegeContactRead, &out); + JsonValue val{JsonObject{}}; + PlatformResult status = Person::GetUsageCount( + common::JsonCast(args), val.get()); + if (status.IsSuccess()) { + ReportSuccess(val, out); + } else { + LogAndReportError(status, &out); + } +} + } // namespace contact } // namespace extension diff --git a/src/contact/contact_instance.h b/src/contact/contact_instance.h index 4e58d6c..c2aee67 100755 --- a/src/contact/contact_instance.h +++ b/src/contact/contact_instance.h @@ -227,6 +227,7 @@ class ContactInstance : public common::ParsedInstance { void PersonLink(const JsonValue& args, JsonObject& out); void PersonUnlink(const JsonValue& args, JsonObject& out); + void PersonGetUsageCount(const JsonValue& args, JsonObject& out); int current_state_; bool is_listening_; diff --git a/src/contact/contact_manager.cc b/src/contact/contact_manager.cc index a2ea972..10ba538 100755 --- a/src/contact/contact_manager.cc +++ b/src/contact/contact_manager.cc @@ -188,14 +188,6 @@ PlatformResult ContactManagerGetInternal(int person_id, JsonObject* out) { return status; } - //get information from view _contacts_person_usage - status = ContactSearchEngine::GetPersonUsage(person_id, out); - if (CONTACTS_ERROR_NONE != contacts_record_destroy(contacts_record, true)) { - LoggerE("failed to destroy contacts_record_h"); - } - if (!status) { - return status; - } return PlatformResult(ErrorCode::NO_ERROR); } } @@ -449,14 +441,7 @@ PlatformResult ContactManagerFind(const JsonObject& args, JsonArray& out) { status = Person::PersonPropertyFromString(attribute, &property); if (status.IsError()) return status; - //As neither _contacts_person_usage nor _contacts_person cover all filter attributes - //which result can be sorted it has to be checked which view can be used here. - if ("usageCount" == attribute) { - error_code = contacts_query_create(_contacts_person_usage._uri, &contacts_query); - } else { - error_code = contacts_query_create(_contacts_person._uri, &contacts_query); - } - + error_code = contacts_query_create(_contacts_person._uri, &contacts_query); status = ContactUtil::ErrorChecker(error_code, "Failed contacts_query_create"); if (status.IsError()) return status; @@ -488,11 +473,7 @@ PlatformResult ContactManagerFind(const JsonObject& args, JsonArray& out) { contacts_filter_h contacts_filter = nullptr; int error_code = CONTACTS_ERROR_NONE; - if ("usageCount" != name) { - error_code = contacts_filter_create(_contacts_person._uri, &contacts_filter); - } else { - error_code = contacts_filter_create(_contacts_person_usage._uri, &contacts_filter); - } + error_code = contacts_filter_create(_contacts_person._uri, &contacts_filter); status = ContactUtil::ErrorChecker(error_code, "Failed contacts_query_set_filter"); if (status.IsError()) return status; @@ -583,11 +564,7 @@ PlatformResult ContactManagerFind(const JsonObject& args, JsonArray& out) { contacts_filter_h contacts_filter = nullptr; int error_code = CONTACTS_ERROR_NONE; - if ("usageCount" != name) { - error_code = contacts_filter_create(_contacts_person._uri, &contacts_filter); - } else { - error_code = contacts_filter_create(_contacts_person_usage._uri, &contacts_filter); - } + error_code = contacts_filter_create(_contacts_person._uri, &contacts_filter); status = ContactUtil::ErrorChecker(error_code, "Failed contacts_query_set_filter"); if (status.IsError()) return status; @@ -649,11 +626,7 @@ PlatformResult ContactManagerFind(const JsonObject& args, JsonArray& out) { if (initial_value_exists && end_value_exists) { contacts_filter_h sub_filter = NULL; - if ("usageCount" != name) { - error_code = contacts_filter_create(_contacts_person._uri, &sub_filter); - } else { - error_code = contacts_filter_create(_contacts_person_usage._uri, &sub_filter); - } + error_code = contacts_filter_create(_contacts_person._uri, &sub_filter); status = ContactUtil::ErrorChecker(error_code, "Failed contacts_filter_add_str"); if (status.IsError()) return status; @@ -726,11 +699,7 @@ PlatformResult ContactManagerFind(const JsonObject& args, JsonArray& out) { if (initial_value_exists && end_value_exists) { contacts_filter_h sub_filter = NULL; - if ("usageCount" != name) { - error_code = contacts_filter_create(_contacts_person._uri, &sub_filter); - } else { - error_code = contacts_filter_create(_contacts_person_usage._uri, &sub_filter); - } + error_code = contacts_filter_create(_contacts_person._uri, &sub_filter); status = ContactUtil::ErrorChecker(error_code, "Failed contacts_filter_add_bool"); if (status.IsError()) return status; diff --git a/src/contact/contact_manager.h b/src/contact/contact_manager.h index cc4c97f..8f74e9b 100755 --- a/src/contact/contact_manager.h +++ b/src/contact/contact_manager.h @@ -54,7 +54,6 @@ common::PlatformResult ContactManagerFind(const JsonObject& args, common::PlatformResult ContactManagerImportFromVCard(const JsonObject& args, JsonObject& out); - common::PlatformResult ContactManagerUpdateBatch(const JsonObject& args); common::PlatformResult ContactManagerRemoveBatch(const JsonObject& args); diff --git a/src/contact/contact_search_engine.cc b/src/contact/contact_search_engine.cc index 136c18a..e2581e9 100644 --- a/src/contact/contact_search_engine.cc +++ b/src/contact/contact_search_engine.cc @@ -633,80 +633,6 @@ PlatformResult ContactSearchEngine::GetContacts(Iterator begin, Iterator end, return PlatformResult(ErrorCode::NO_ERROR); } -PlatformResult ContactSearchEngine::GetPersonUsage(int person_id, JsonObject* out_ptr) { - LoggerD("Entered"); - - contacts_query_h query = nullptr; - contacts_filter_h filter = nullptr; - contacts_list_h list = nullptr; - PlatformResult status(ErrorCode::NO_ERROR); - - int error_code = contacts_query_create(_contacts_person_usage._uri, &query); - status = ContactUtil::ErrorChecker(error_code, "Failed contacts_query_create"); - if (!status) { - return status; - } - ContactUtil::ContactsQueryHPtr query_ptr(&query, ContactUtil::ContactsQueryDeleter); - - error_code = contacts_filter_create(_contacts_person_usage._uri, &filter); - status = ContactUtil::ErrorChecker(error_code, "Failed contacts_filter_create"); - if (!status) { - return status; - } - ContactUtil::ContactsFilterPtr filter_ptr(filter, ContactUtil::ContactsFilterDeleter); - - error_code = contacts_filter_add_int(filter, - _contacts_person_usage.person_id, - CONTACTS_MATCH_EQUAL, - person_id); - status = ContactUtil::ErrorChecker(error_code, "Failed contacts_filter_add_int"); - if (!status) { - return status; - } - - error_code = contacts_query_set_filter(query, filter); - status = ContactUtil::ErrorChecker(error_code, "Failed contacts_query_set_filter"); - if (!status) { - return status; - } - - error_code = contacts_db_get_records_with_query(query, 0, 0, &list); - status = ContactUtil::ErrorChecker(error_code, "Failed contacts_db_get_records_with_query"); - if (!status) { - return status; - } - ContactUtil::ContactsListHPtr list_ptr(&list, ContactUtil::ContactsListDeleter); - - int record_count = 0; - error_code = contacts_list_get_count(list, &record_count); - status = ContactUtil::ErrorChecker(error_code, "Failed contacts_list_get_count"); - if (!status) { - return status; - } - - if (record_count == 1) { - contacts_record_h record = nullptr; - - contacts_list_first(list); - error_code = contacts_list_get_current_record_p(list, &record); - status = ContactUtil::ErrorChecker(error_code, "Failed contacts_list_get_current_record_p"); - if (!status) { - return status; - } - - int times_used = 0; - error_code = contacts_record_get_int(record, _contacts_person_usage.times_used, ×_used); - status = ContactUtil::ErrorChecker(error_code, "Failed contacts_record_get_int"); - if (!status) { - return status; - } - out_ptr->insert(std::make_pair("usageCount", JsonValue(static_cast(times_used)))); - } else { - out_ptr->insert(std::make_pair("usageCount", JsonValue(static_cast(0)))); - } - - return PlatformResult(ErrorCode::NO_ERROR); -} common::PlatformResult ContactSearchEngine::GetQueryResults( contacts_query_h query, contacts_filter_h filter, unsigned int property_id, diff --git a/src/contact/contact_search_engine.h b/src/contact/contact_search_engine.h index f859b36..6dcfbc8 100644 --- a/src/contact/contact_search_engine.h +++ b/src/contact/contact_search_engine.h @@ -42,8 +42,6 @@ class ContactSearchEngine { common::PlatformResult Find(picojson::array* out); - static common::PlatformResult GetPersonUsage(int person_id, JsonObject* out_ptr); - private: typedef std::vector LongVector; typedef std::shared_ptr LongVectorPtr; diff --git a/src/contact/contact_util.cc b/src/contact/contact_util.cc index 6e91ef0..f6e7c5f 100755 --- a/src/contact/contact_util.cc +++ b/src/contact/contact_util.cc @@ -79,7 +79,7 @@ void ContactsListDeleter(contacts_list_h* contacts_list) { } void ContactsFilterDeleter(contacts_filter_h contacts_filter) { - if (CONTACTS_ERROR_NONE != contacts_filter_destroy(contacts_filter)) { + if (contacts_filter && CONTACTS_ERROR_NONE != contacts_filter_destroy(contacts_filter)) { LoggerE("failed to destroy contacts_filter_h"); } } @@ -3027,31 +3027,6 @@ PlatformResult ImportPersonFromContactsRecord(contacts_record_h record, return PlatformResult(ErrorCode::NO_ERROR); } -PlatformResult ImportPersonFromContactsUsageRecord(contacts_record_h record, - JsonObject* out_ptr) { - if (nullptr == record) { - LoggerW("Platform person record did not set"); - return PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Platform person record did not set"); - } - - JsonObject& arguments_obj = *out_ptr; - - int int_value = 0; - // usageCount - PlatformResult status = ContactUtil::GetIntFromRecord( - record, _contacts_person_usage.times_used, &int_value); - - if (status.IsError()) { - LoggerE("Error: %s", status.message().c_str()); - return status; - } - - arguments_obj.insert( - std::make_pair("usageCount", JsonValue(static_cast(int_value)))); - - return PlatformResult(ErrorCode::NO_ERROR); -} - /** * @brief Updates contacts_record_h with values from Person object * @param[out] contacts_record_h Record which is updated diff --git a/src/contact/contact_util.h b/src/contact/contact_util.h index 9e7b985..2089693 100644 --- a/src/contact/contact_util.h +++ b/src/contact/contact_util.h @@ -145,8 +145,6 @@ common::PlatformResult ExportContactGroupToContactsRecord( contacts_record_h contacts_record, const JsonObject &in); common::PlatformResult ImportPersonFromContactsRecord( contacts_record_h contacts_record, JsonObject *out); -common::PlatformResult ImportPersonFromContactsUsageRecord( - contacts_record_h contacts_record, JsonObject *out); common::PlatformResult CheckDBConnection(); diff --git a/src/contact/js/common.js b/src/contact/js/common.js index 3415c52..c4d8142 100755 --- a/src/contact/js/common.js +++ b/src/contact/js/common.js @@ -88,6 +88,19 @@ function _checkError(result) { var TypeEnum = ['VCARD_30']; +var PersonUsageTypeEnum = { + OUTGOING_CALL: 'OUTGOING_CALL', + OUTGOING_MSG: 'OUTGOING_MSG', + OUTGOING_EMAIL: 'OUTGOING_EMAIL', + INCOMING_CALL: 'INCOMING_CALL', + INCOMING_MSG: 'INCOMING_MSG', + INCOMING_EMAIL: 'INCOMING_EMAIL', + MISSED_CALL: 'MISSED_CALL', + REJECTED_CALL: 'REJECTED_CALL', + BLOCKED_CALL: 'BLOCKED_CALL', + BLOCKED_MSG: 'BLOCKED_MSG' +}; + // Edit Guard ////////////////////////////////////////////////////////////// // This flag is workaround. It is caused by specification // which tell to edit readonly fields. @@ -120,4 +133,4 @@ EditGuard.prototype.isEditEnabled = function() { return _canEdit > 0; }; -var _editGuard = new EditGuard(); \ No newline at end of file +var _editGuard = new EditGuard(); diff --git a/src/contact/js/person.js b/src/contact/js/person.js index ea78e3d..025a4d6 100755 --- a/src/contact/js/person.js +++ b/src/contact/js/person.js @@ -24,7 +24,6 @@ var Person = function(data) { var _hasEmail = false; var _isFavorite = false; var _displayContactId = ''; - var _usageCount = 0; if (data.hasOwnProperty('id') && type_.isString(data.id)) { _id = data.id; @@ -47,9 +46,6 @@ var Person = function(data) { if (data.hasOwnProperty('isFavorite') && type_.isBoolean(data.isFavorite)) { _isFavorite = data.isFavorite; } - if (data.hasOwnProperty('usageCount') && type_.isNumber(data.usageCount)) { - _usageCount = data.usageCount; - } Object.defineProperties(this, { id: { @@ -135,17 +131,6 @@ var Person = function(data) { _displayContactId = converter_.toString(v, false); }, enumerable: true - }, - usageCount: { - get: function() { - return _usageCount; - }, - set: function(v) { - if (_editGuard.isEditEnabled()) { - _usageCount = converter_.toLong(v, false); - } - }, - enumerable: true } }); }; @@ -204,3 +189,25 @@ Person.prototype.unlink = function(contactId) { return new Person(native_.getResultObject(result)); }); }; + +// Gets person's usage count +Person.prototype.getUsageCount = function() { + // validation + var args = validator_.validateArgs(arguments, [ + { + name: 'usage_type', + type: types_.ENUM, + values: Object.keys(PersonUsageTypeEnum), + optional: true, + nullable: true + }]); + + var usage_type = (args.usage_type === undefined ? null : args.usage_type); + + var result = native_.callSync('Person_getUsageCount', { personId: this.id, usage_type: usage_type }); + if (native_.isFailure(result)) { + throw native_.getErrorObject(result); + } + var res = native_.getResultObject(result); + return Number(res.usageCount); +}; diff --git a/src/contact/person.cc b/src/contact/person.cc index 3e6bf50..e76d880 100755 --- a/src/contact/person.cc +++ b/src/contact/person.cc @@ -34,7 +34,24 @@ static const PersonPropertyMap personPropertyMap = { {"photoURI", {_contacts_person.image_thumbnail_path, kPrimitiveTypeString}}, {"ringtoneURI", {_contacts_person.ringtone_path, kPrimitiveTypeString}}, {"displayContactId", {_contacts_person.display_contact_id, kPrimitiveTypeId}}, - {"usageCount", {_contacts_person_usage.times_used, kPrimitiveTypeLong}}, +}; + +extern const std::string kUsageTypeOutgoingCall = "OUTGOING_CALL"; +extern const std::string kUsageTypeOutgoingMsg = "OUTGOING_MSG"; +extern const std::string kUsageTypeOutgoingEmail = "OUTGOING_EMAIL"; +extern const std::string kUsageTypeIncomingCall = "INCOMING_CALL"; +extern const std::string kUsageTypeIncomingMsg = "INCOMING_MSG"; +extern const std::string kUsageTypeIncomingEmail = "INCOMING_EMAIL"; +extern const std::string kUsageTypeMissedCall = "MISSED_CALL"; +extern const std::string kUsageTypeRejectedCall = "REJECTED_CALL"; +extern const std::string kUsageTypeBlockedCall = "BLOCKED_CALL"; +extern const std::string kUsageTypeBlockedMsg = "BLOCKED_MSG"; + +namespace { + const char* kGetUsageCountArgPersonId = "personId"; + const char* kGetUsageCountArgUsageType = "usage_type"; + + const char* kGetUsageCountResultUsageCount = "usageCount"; }; PlatformResult PersonLink(const JsonObject& args, JsonObject&) { @@ -127,6 +144,113 @@ PlatformResult PersonUnlink(const JsonObject& args, JsonObject& out) { return PlatformResult(ErrorCode::NO_ERROR); } +PlatformResult GetUsageCount(const JsonObject& args, JsonObject& out) { + LoggerD("Entered"); + + // Retrieve person_id + long person_id = common::stol(FromJson(args, kGetUsageCountArgPersonId)); + if (person_id < 0) { + return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, "Negative person id"); + } + + PlatformResult status(ErrorCode::NO_ERROR); + + // Create query + contacts_query_h query = nullptr; + int ret = contacts_query_create(_contacts_person_usage._uri, &query); + status = ContactUtil::ErrorChecker(ret, "Failed contacts_query_create"); + if (!status) { + return status; + } + ContactUtil::ContactsQueryHPtr query_ptr(&query, ContactUtil::ContactsQueryDeleter); + + // Create and set filter on person_id + contacts_filter_h filter = nullptr; + ret = contacts_filter_create(_contacts_person_usage._uri, &filter); + status = ContactUtil::ErrorChecker(ret, "Failed contacts_filter_create"); + if (!status) { + return status; + } + ContactUtil::ContactsFilterPtr filter_ptr(filter, ContactUtil::ContactsFilterDeleter); + ret = contacts_filter_add_int(filter, + _contacts_person_usage.person_id, + CONTACTS_MATCH_EQUAL, + person_id); + status = ContactUtil::ErrorChecker(ret, "Failed contacts_filter_add_int"); + if (!status) { + return status; + } + + if (!IsNull(args, kGetUsageCountArgUsageType)) { + contacts_usage_type_e type = UsageTypeFromString(FromJson(args, kGetUsageCountArgUsageType)); + + ret = contacts_filter_add_operator(filter, CONTACTS_FILTER_OPERATOR_AND); + status = ContactUtil::ErrorChecker(ret, "Failed contacts_filter_add_operator"); + if (!status) { + return status; + } + + ret = contacts_filter_add_int(filter, + _contacts_person_usage.usage_type, + CONTACTS_MATCH_EQUAL, + type); + status = ContactUtil::ErrorChecker(ret, "Failed contacts_filter_add_int"); + if (!status) { + return status; + } + } + + // Setting filter + ret = contacts_query_set_filter(query, filter); + status = ContactUtil::ErrorChecker(ret, "Failed contacts_query_set_filter"); + if (!status) { + return status; + } + + // Call query + contacts_list_h list = nullptr; + ret = contacts_db_get_records_with_query(query, 0, 0, &list); + status = ContactUtil::ErrorChecker(ret, "Failed contacts_db_get_records_with_query"); + if (!status) { + return status; + } + ContactUtil::ContactsListHPtr list_ptr(&list, ContactUtil::ContactsListDeleter); + + int record_count = 0; + ret = contacts_list_get_count(list, &record_count); + status = ContactUtil::ErrorChecker(ret, "Failed contacts_list_get_count"); + if (!status) { + return status; + } + + int usage_count = 0; + int usage_count_tmp = 0; + contacts_record_h record = nullptr; + + contacts_list_first(list); + for (int i = 0; i < record_count; i++) { + ret = contacts_list_get_current_record_p(list, &record); + status = ContactUtil::ErrorChecker(ret, "Failed contacts_list_get_current_record_p"); + if (!status) { + return status; + } + + ret = contacts_record_get_int(record, _contacts_person_usage.times_used, &usage_count_tmp ); + status = ContactUtil::ErrorChecker(ret, "Failed contacts_record_get_int"); + if (!status) { + return status; + } + + usage_count += usage_count_tmp; + + contacts_list_next(list); + } + + out.insert(std::make_pair(kGetUsageCountResultUsageCount, JsonValue(static_cast(usage_count)))); + + return PlatformResult(ErrorCode::NO_ERROR); +} + PlatformResult PersonPropertyFromString(const std::string& name, PersonProperty* person_prop) { LoggerD("Enter"); @@ -142,6 +266,32 @@ PlatformResult PersonPropertyFromString(const std::string& name, return PlatformResult(ErrorCode::NO_ERROR); } +contacts_usage_type_e UsageTypeFromString(const std::string& type_str) { + if (type_str == kUsageTypeOutgoingCall) { + return CONTACTS_USAGE_STAT_TYPE_OUTGOING_CALL; + } else if (type_str == kUsageTypeOutgoingMsg) { + return CONTACTS_USAGE_STAT_TYPE_OUTGOING_MSG; + } else if (type_str == kUsageTypeOutgoingEmail) { + return CONTACTS_USAGE_STAT_TYPE_OUTGOING_EMAIL; + } else if (type_str == kUsageTypeIncomingCall) { + return CONTACTS_USAGE_STAT_TYPE_INCOMING_CALL; + } else if (type_str == kUsageTypeIncomingMsg) { + return CONTACTS_USAGE_STAT_TYPE_INCOMING_MSG; + } else if (type_str == kUsageTypeIncomingEmail) { + return CONTACTS_USAGE_STAT_TYPE_INCOMING_EMAIL; + } else if (type_str == kUsageTypeMissedCall) { + return CONTACTS_USAGE_STAT_TYPE_MISSED_CALL; + } else if (type_str == kUsageTypeRejectedCall) { + return CONTACTS_USAGE_STAT_TYPE_REJECTED_CALL; + } else if (type_str == kUsageTypeBlockedCall) { + return CONTACTS_USAGE_STAT_TYPE_BLOCKED_CALL; + } else if (type_str == kUsageTypeBlockedMsg) { + return CONTACTS_USAGE_STAT_TYPE_BLOCKED_MSG; + } else { + return CONTACTS_USAGE_STAT_TYPE_NONE; + } +} + } // Person } // contact } // extension diff --git a/src/contact/person.h b/src/contact/person.h index d71b2ed..1b1bf7e 100644 --- a/src/contact/person.h +++ b/src/contact/person.h @@ -30,6 +30,17 @@ enum PrimitiveType { kPrimitiveTypeId }; +extern const std::string kUsageTypeOutgoingCall; +extern const std::string kUsageTypeOutgoingMsg; +extern const std::string kUsageTypeOutgoingEmail; +extern const std::string kUsageTypeIncomingCall; +extern const std::string kUsageTypeIncomingMsg; +extern const std::string kUsageTypeIncomingEmail; +extern const std::string kUsageTypeMissedCall; +extern const std::string kUsageTypeRejectedCall; +extern const std::string kUsageTypeBlockedCall; +extern const std::string kUsageTypeBlockedMsg; + namespace Person { struct PersonProperty { @@ -41,9 +52,11 @@ typedef std::map PersonPropertyMap; common::PlatformResult PersonPropertyFromString(const std::string& name, PersonProperty* person_prop); +contacts_usage_type_e UsageTypeFromString(const std::string& type_str); common::PlatformResult PersonLink(const JsonObject& args, JsonObject&); common::PlatformResult PersonUnlink(const JsonObject& args, JsonObject&); +common::PlatformResult GetUsageCount(const JsonObject& args, JsonObject&); } // Person } // contact