Contacts db changed provider added 12/62112/5
authorSomin Kim <somin926.kim@samsung.com>
Mon, 14 Mar 2016 05:27:40 +0000 (14:27 +0900)
committerSomin Kim <somin926.kim@samsung.com>
Tue, 15 Mar 2016 02:52:13 +0000 (11:52 +0900)
Change-Id: Ie09bf1ed628486dd763e6b69aa80c46d1512cf49
Signed-off-by: Somin Kim <somin926.kim@samsung.com>
src/device/device_context_provider.cpp
src/device/social/contacts.cpp [new file with mode: 0644]
src/device/social/contacts.h [new file with mode: 0644]
src/device/social/social_types.h

index 98e734c8f3ffe08bf18f603c3c96e8f186dff8fc..be6d9110e92d7157de762c5f03791db02e366508 100644 (file)
@@ -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<typename provider>
 void register_provider(const char *subject, const char *privilege)
@@ -91,6 +93,7 @@ EXTAPI bool ctx::init_device_context_provider()
        register_provider<social_status_call>(SOCIAL_ST_SUBJ_CALL, PRIV_TELEPHONY);
        register_provider<social_status_email>(SOCIAL_ST_SUBJ_EMAIL, NULL);
        register_provider<social_status_message>(SOCIAL_ST_SUBJ_MESSAGE, PRIV_MESSAGE);
+       register_provider<social_status_contacts>(SOCIAL_ST_SUBJ_CONTACTS, PRIV_CONTACT);
 
        register_provider<user_activity_stationary>(USER_ACT_SUBJ_STATIONARY, NULL);
        register_provider<user_activity_walking>(USER_ACT_SUBJ_WALKING, NULL);
diff --git a/src/device/social/contacts.cpp b/src/device/social/contacts.cpp
new file mode 100644 (file)
index 0000000..4e11e5a
--- /dev/null
@@ -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 <Json.h>
+#include <context_mgr.h>
+#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<social_status_contacts*>(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 (file)
index 0000000..52adb14
--- /dev/null
@@ -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 <contacts.h>
+#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_
index 55c015fa0652bee7d19d5edb265e93aff6b2c52a..47c512179d4f1bad3ed90dcd43e4ce2823661b43 100644 (file)
@@ -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