From 5a2f730531ee82858b4e35099e94098f41038587 Mon Sep 17 00:00:00 2001 From: Somin Kim Date: Mon, 14 Mar 2016 14:27:40 +0900 Subject: [PATCH] Contacts db changed provider added Change-Id: Ie09bf1ed628486dd763e6b69aa80c46d1512cf49 Signed-off-by: Somin Kim --- src/device/device_context_provider.cpp | 3 + src/device/social/contacts.cpp | 134 +++++++++++++++++++++++++ src/device/social/contacts.h | 50 +++++++++ src/device/social/social_types.h | 4 + 4 files changed, 191 insertions(+) create mode 100644 src/device/social/contacts.cpp create mode 100644 src/device/social/contacts.h diff --git a/src/device/device_context_provider.cpp b/src/device/device_context_provider.cpp index 98e734c..be6d911 100644 --- a/src/device/device_context_provider.cpp +++ b/src/device/device_context_provider.cpp @@ -37,6 +37,7 @@ #include "social/call.h" #include "social/email.h" #include "social/message.h" +#include "social/contacts.h" #include "activity/activity.h" #endif @@ -61,6 +62,7 @@ #define PRIV_NETWORK "network.get" #define PRIV_TELEPHONY "telephony" #define PRIV_MESSAGE "message.read" +#define PRIV_CONTACT "contact.read" template void register_provider(const char *subject, const char *privilege) @@ -91,6 +93,7 @@ EXTAPI bool ctx::init_device_context_provider() register_provider(SOCIAL_ST_SUBJ_CALL, PRIV_TELEPHONY); register_provider(SOCIAL_ST_SUBJ_EMAIL, NULL); register_provider(SOCIAL_ST_SUBJ_MESSAGE, PRIV_MESSAGE); + register_provider(SOCIAL_ST_SUBJ_CONTACTS, PRIV_CONTACT); register_provider(USER_ACT_SUBJ_STATIONARY, NULL); register_provider(USER_ACT_SUBJ_WALKING, NULL); diff --git a/src/device/social/contacts.cpp b/src/device/social/contacts.cpp new file mode 100644 index 0000000..4e11e5a --- /dev/null +++ b/src/device/social/contacts.cpp @@ -0,0 +1,134 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include "social_types.h" +#include "contacts.h" + +#define MY_PROFILE_VIEW _contacts_my_profile._uri +#define PERSON_VIEW _contacts_person._uri +#define TIME_INTERVAL 1 + +GENERATE_PROVIDER_COMMON_IMPL(social_status_contacts); + +ctx::social_status_contacts::social_status_contacts() + : latest_my_profile(0) + , latest_person(0) +{ +} + +ctx::social_status_contacts::~social_status_contacts() +{ +} + +bool ctx::social_status_contacts::is_supported() +{ + return true; +} + +void ctx::social_status_contacts::submit_trigger_item() +{ + context_manager::register_trigger_item(SOCIAL_ST_SUBJ_CONTACTS, OPS_SUBSCRIBE, + "{" + "\"View\":{\"type\":\"string\",\"values\":[\"MyProfile\",\"Person\"]}" + "}", + NULL); +} + +void ctx::social_status_contacts::db_change_cb(const char* view_uri, void* user_data) +{ + social_status_contacts *instance = static_cast(user_data); + instance->handle_db_change(view_uri); +} + +void ctx::social_status_contacts::handle_db_change(const char* view_uri) +{ + if (!STR_EQ(view_uri, _contacts_my_profile._uri) && !STR_EQ(view_uri, _contacts_person._uri)) { + _W("Unknown view uri"); + return; + } + + std::string view = (STR_EQ(view_uri, _contacts_my_profile._uri)? SOCIAL_ST_MY_PROFILE : SOCIAL_ST_PERSON); + IF_FAIL_VOID_TAG(!is_consecutive_change(view_uri), _D, "Ignore consecutive db change: %s", view.c_str()); + + ctx::Json data; + data.set(NULL, SOCIAL_ST_VIEW, view); + context_manager::publish(SOCIAL_ST_SUBJ_CONTACTS, NULL, ERR_NONE, data); +} + +bool ctx::social_status_contacts::is_consecutive_change(const char* view_uri) +{ + time_t now = time(NULL); + double diff = 0; + + if (STR_EQ(view_uri, MY_PROFILE_VIEW)) { + diff = difftime(now, latest_my_profile); + latest_my_profile = now; + } else if (STR_EQ(view_uri, PERSON_VIEW)) { + diff = difftime(now, latest_person); + latest_person = now; + } + + if (diff < TIME_INTERVAL) + return true; + + return false; +} + +bool ctx::social_status_contacts::set_callback() +{ + int err; + + err = contacts_connect(); + IF_FAIL_RETURN_TAG(err == CONTACTS_ERROR_NONE, false, _E, "Connecting contacts failed"); + + err = contacts_db_add_changed_cb(MY_PROFILE_VIEW, db_change_cb, this); + IF_FAIL_CATCH_TAG(err == CONTACTS_ERROR_NONE, _E, "Setting my profile view changed callback failed"); + + err = contacts_db_add_changed_cb(PERSON_VIEW, db_change_cb, this); + IF_FAIL_CATCH_TAG(err == CONTACTS_ERROR_NONE, _E, "Setting person view changed callback failed"); + + return true; + +CATCH: + contacts_disconnect(); + return false; +} + +void ctx::social_status_contacts::unset_callback() +{ + contacts_db_remove_changed_cb(MY_PROFILE_VIEW, db_change_cb, this); + contacts_db_remove_changed_cb(PERSON_VIEW, db_change_cb, this); + + contacts_disconnect(); + + latest_my_profile = 0; + latest_person = 0; +} + +int ctx::social_status_contacts::subscribe() +{ + bool ret = set_callback(); + IF_FAIL_RETURN(ret, ERR_OPERATION_FAILED); + return ERR_NONE; +} + +int ctx::social_status_contacts::unsubscribe() +{ + unset_callback(); + return ERR_NONE; +} diff --git a/src/device/social/contacts.h b/src/device/social/contacts.h new file mode 100644 index 0000000..52adb14 --- /dev/null +++ b/src/device/social/contacts.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _CONTEXT_SOCIAL_STATUS_CONTACTS_H_ +#define _CONTEXT_SOCIAL_STATUS_CONTACTS_H_ + +#include +#include "../provider_base.h" + +namespace ctx { + + class social_status_contacts : public device_provider_base { + + GENERATE_PROVIDER_COMMON_DECL(social_status_contacts); + + public: + int subscribe(); + int unsubscribe(); + static bool is_supported(); + static void submit_trigger_item(); + + private: + time_t latest_my_profile; + time_t latest_person; + + social_status_contacts(); + ~social_status_contacts(); + + bool set_callback(); + void unset_callback(); + void handle_db_change(const char* view_uri); + static void db_change_cb(const char* view_uri, void* user_data); + bool is_consecutive_change(const char* view_uri); + }; +} + +#endif // _CONTEXT_SOCIAL_STATUS_CONTACTS_H_ diff --git a/src/device/social/social_types.h b/src/device/social/social_types.h index 55c015f..47c5121 100644 --- a/src/device/social/social_types.h +++ b/src/device/social/social_types.h @@ -21,6 +21,7 @@ #define SOCIAL_ST_SUBJ_CALL "social/call" #define SOCIAL_ST_SUBJ_EMAIL "social/email" #define SOCIAL_ST_SUBJ_MESSAGE "social/message" +#define SOCIAL_ST_SUBJ_CONTACTS "social/contacts" // Data Key #define SOCIAL_ST_STATE "State" @@ -28,6 +29,7 @@ #define SOCIAL_ST_TYPE "Type" #define SOCIAL_ST_MEDIUM "Medium" #define SOCIAL_ST_ADDRESS "Address" +#define SOCIAL_ST_VIEW "View" // Data Values #define SOCIAL_ST_IDLE "Idle" @@ -44,5 +46,7 @@ #define SOCIAL_ST_RECEIVED "Received" #define SOCIAL_ST_SMS "SMS" #define SOCIAL_ST_MMS "MMS" +#define SOCIAL_ST_MY_PROFILE "MyProfile" +#define SOCIAL_ST_PERSON "Person" #endif -- 2.34.1