4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
26 #include "email-debug-log.h"
27 #include "email-storage.h"
28 #include "email-core-utils.h"
29 #include "email-core-mailbox.h"
30 #include "email-core-sound.h"
31 #include "email-utilities.h"
33 #define TIMER 30000 // 30 seconds
34 #define HAPTIC_TEST_ITERATION 1
36 static MMHandleType email_mmhandle = 0;
37 static alarm_id_t email_alarm_id = 0;
38 static int setting_noti_status = 0;
40 static char *filename;
41 alarm_entry_t *alarm_info = NULL;
43 static pthread_mutex_t sound_mutex = PTHREAD_MUTEX_INITIALIZER;
44 static pthread_cond_t sound_condition = PTHREAD_COND_INITIALIZER;
45 static pthread_mutex_t mmhandle_mutex = PTHREAD_MUTEX_INITIALIZER;
46 static thread_t g_alert_thread;
48 void emcore_set_repetition_alarm(int repetition);
49 int emcore_sound_mp_player_stop();
50 bool emcore_sound_mp_player_destory();
51 void *start_alert_thread(void *arg);
53 bool emcore_set_mp_filepath(const char *key)
55 filename = vconf_get_str(key);
59 /* initialize the ringtone path */
60 if (vconf_set_str(VCONF_VIP_NOTI_RINGTONE_PATH, filename) != 0) {
61 EM_DEBUG_EXCEPTION("vconf_set_str failed");
68 int emcore_alert_sound_init()
70 int ret = MM_ERROR_NONE;
71 if ((ret = mm_session_init(MM_SESSION_TYPE_NOTIFY)) != MM_ERROR_NONE)
72 EM_DEBUG_EXCEPTION("mm_session_int failed");
77 int emcore_alert_alarm_init()
79 EM_DEBUG_FUNC_BEGIN();
81 int ret = ALARMMGR_RESULT_SUCCESS;
83 ret = alarmmgr_init("email-service-0");
84 if (ret != ALARMMGR_RESULT_SUCCESS)
85 EM_DEBUG_EXCEPTION("alarmmgr_init failed : [%d]", ret);
91 int emcore_alert_sound_filepath_init()
93 filename = (char *)em_malloc(MAX_PATH);
94 if (filename == NULL) {
95 EM_DEBUG_EXCEPTION("Memory malloc error");
99 if (!emcore_set_mp_filepath(VCONFKEY_SETAPPL_NOTI_EMAIL_RINGTONE_PATH_STR)) {
100 /* TODO : Add code to set default ringtone path */
101 EM_DEBUG_EXCEPTION("emcore_set_mp_filepath failed.");
108 void emcore_global_noti_key_changed_cb(keynode_t *key_node, void *data)
110 EM_DEBUG_FUNC_BEGIN();
113 switch (vconf_keynode_get_type(key_node)) {
115 ret = alarmmgr_remove_alarm(email_alarm_id);
116 if (ret != ALARMMGR_RESULT_SUCCESS) {
117 EM_DEBUG_EXCEPTION("delete of alarm id failed");
119 emcore_set_repetition_alarm(vconf_keynode_get_int(key_node));
121 case VCONF_TYPE_STRING:
122 filename = vconf_keynode_get_str(key_node);
125 EM_DEBUG_EXCEPTION("Invalid key type");
132 void emcore_email_noti_key_changed_cb(keynode_t *key_node, void *data)
134 EM_DEBUG_FUNC_BEGIN();
137 switch (vconf_keynode_get_type(key_node)) {
139 ret = alarmmgr_remove_alarm(email_alarm_id);
140 if (ret != ALARMMGR_RESULT_SUCCESS) {
141 EM_DEBUG_EXCEPTION("delete of alarm id failed");
143 emcore_set_repetition_alarm(vconf_keynode_get_int(key_node));
145 case VCONF_TYPE_STRING:
146 filename = vconf_keynode_get_str(key_node);
149 EM_DEBUG_EXCEPTION("Invalid key type");
155 bool emcore_update_noti_status()
157 EM_DEBUG_FUNC_BEGIN();
160 /* Get the priority noti ticker */
161 if (vconf_get_bool(VCONF_VIP_NOTI_NOTIFICATION_TICKER, &ticker_noti) != 0) {
162 EM_DEBUG_EXCEPTION("vconf_get_bool failed");
166 EM_DEBUG_LOG("ticker_noti of vip : [%d]", ticker_noti);
168 if (ticker_noti <= 0) {
169 /* Get the Global noti ticker */
170 if (vconf_get_bool(VCONFKEY_SETAPPL_STATE_TICKER_NOTI_EMAIL_BOOL, &ticker_noti) != 0) {
171 EM_DEBUG_EXCEPTION("Not display the noti of email");
175 EM_DEBUG_LOG("ticker_noti of global : [%d]", ticker_noti);
178 EM_DEBUG_LOG("Not use the notification");
179 setting_noti_status = SETTING_NOTI_STATUS_OFF;
183 setting_noti_status = SETTING_NOTI_STATUS_GLOBAL;
185 setting_noti_status = SETTING_NOTI_STATUS_EMAIL;
192 void emcore_noti_status_changed_cb(keynode_t *key_node, void *data)
194 EM_DEBUG_FUNC_BEGIN();
195 if (!emcore_update_noti_status()) {
196 EM_DEBUG_EXCEPTION("emcore_update_noti_status failed");
202 bool emcore_noti_init(void *data)
204 EM_DEBUG_FUNC_BEGIN();
205 struct appdata *ap = data;
207 if (!emcore_update_noti_status()) {
208 EM_DEBUG_EXCEPTION("emcore_update_noti_status failed");
212 /* Noti callback registration */
213 if (vconf_notify_key_changed(VCONFKEY_SETAPPL_NOTI_EMAIL_ALERT_REP_TYPE_INT, emcore_global_noti_key_changed_cb, ap) < 0) {
214 EM_DEBUG_EXCEPTION("Register failed : alert type");
217 if (vconf_notify_key_changed(VCONFKEY_SETAPPL_NOTI_EMAIL_RINGTONE_PATH_STR, emcore_global_noti_key_changed_cb, ap) < 0) {
218 EM_DEBUG_EXCEPTION("Register failed : Ringtone path");
221 if (vconf_notify_key_changed(VCONF_VIP_NOTI_REP_TYPE, emcore_email_noti_key_changed_cb, ap) < 0) {
222 EM_DEBUG_EXCEPTION("Register failed : alert type");
225 if (vconf_notify_key_changed(VCONF_VIP_NOTI_RINGTONE_PATH, emcore_email_noti_key_changed_cb, ap) < 0) {
226 EM_DEBUG_EXCEPTION("Register failed : Ringtone path");
229 if (vconf_notify_key_changed(VCONF_VIP_NOTI_NOTIFICATION_TICKER, emcore_noti_status_changed_cb, ap) < 0) {
230 EM_DEBUG_EXCEPTION("Register failed : Ringtone path");
233 if (vconf_notify_key_changed(VCONFKEY_SETAPPL_STATE_TICKER_NOTI_EMAIL_BOOL, emcore_noti_status_changed_cb, ap) < 0) {
234 EM_DEBUG_EXCEPTION("Register failed : Ringtone path");
241 int emcore_alert_init()
243 EM_DEBUG_FUNC_BEGIN();
247 if (!emcore_alert_sound_filepath_init()) {
248 EM_DEBUG_EXCEPTION("emcore_alert_sound_filepath_init failed");
252 if ((err = emcore_alert_sound_init()) != MM_ERROR_NONE) {
253 EM_DEBUG_EXCEPTION("emcore_alert_sound_init failed : [%d]", err);
257 if ((err = emcore_alert_alarm_init()) != ALARMMGR_RESULT_SUCCESS) {
258 EM_DEBUG_EXCEPTION("emcore_alert_alarm_init failed : [%d]", err);
262 if (!emcore_noti_init(NULL)) {
263 EM_DEBUG_EXCEPTION("emcore_noti_init failed");
271 int emcore_mp_player_state_cb(int message, void *param, void *user_param)
275 case MM_MESSAGE_ERROR:
276 EM_DEBUG_LOG("Error is happened.");
277 if (email_mmhandle) {
278 emcore_sound_mp_player_destory();
281 case MM_MESSAGE_BEGIN_OF_STREAM:
282 EM_DEBUG_LOG("Play is started.");
284 case MM_MESSAGE_END_OF_STREAM:
285 EM_DEBUG_LOG("End of stream.");
286 ENTER_CRITICAL_SECTION(mmhandle_mutex);
289 emcore_sound_mp_player_stop();
290 emcore_sound_mp_player_destory();
292 LEAVE_CRITICAL_SECTION(mmhandle_mutex);
295 EM_DEBUG_LOG("Message = %d", message);
301 bool emcore_sound_mp_player_create()
303 EM_DEBUG_FUNC_BEGIN();
306 if (email_mmhandle) {
307 EM_DEBUG_LOG("already create the handle");
311 if ((err = mm_player_create(&email_mmhandle)) != MM_ERROR_NONE) {
312 EM_DEBUG_EXCEPTION("mm_player create fail [%d]", err);
319 bool emcore_alarm_create()
321 EM_DEBUG_FUNC_BEGIN();
323 alarm_info = alarmmgr_create_alarm();
325 if (alarm_info == NULL) {
326 EM_DEBUG_EXCEPTION("alarm create failed");
334 bool emcore_alarm_destory()
336 EM_DEBUG_FUNC_BEGIN();
339 ret = alarmmgr_free_alarm(alarm_info);
341 if (ret != ALARMMGR_RESULT_SUCCESS) {
342 EM_DEBUG_EXCEPTION("alarm free failed");
350 bool emcore_alert_create()
352 EM_DEBUG_FUNC_BEGIN();
354 /* Create the alarm handle */
355 if (!emcore_alarm_create()) {
356 EM_DEBUG_EXCEPTION("emcore_alarm_create failed.");
360 /* Set the music file in alert */
361 if (!emcore_set_mp_filepath(VCONFKEY_SETAPPL_NOTI_EMAIL_RINGTONE_PATH_STR)) {
362 /* TODO : Add code to set default ringtone path */
363 EM_DEBUG_EXCEPTION("emcore_set_mp_filepath failed.");
371 bool emcore_alert_destory()
373 EM_DEBUG_FUNC_BEGIN();
376 /* Destroy the music player handle */
377 if (!emcore_sound_mp_player_destory()) {
378 EM_DEBUG_EXCEPTION("emcore_sound_mp_player_destory fail");
382 /* Destroy the alarm handle */
383 ret = alarmmgr_free_alarm(alarm_info);
384 if (ret != ALARMMGR_RESULT_SUCCESS) {
385 EM_DEBUG_EXCEPTION("alarmmgr_free_alarm fail");
389 /* Set the music file in alert */
390 EM_SAFE_FREE(filename);
396 gboolean mp_player_timeout_cb(void *data)
398 EM_DEBUG_FUNC_BEGIN();
400 ENTER_CRITICAL_SECTION(mmhandle_mutex);
403 emcore_sound_mp_player_stop();
404 emcore_sound_mp_player_destory();
406 LEAVE_CRITICAL_SECTION(mmhandle_mutex);
412 bool emcore_vibration_start()
414 EM_DEBUG_FUNC_BEGIN();
416 int error = FEEDBACK_ERROR_NONE;
419 error = vconf_get_int(VCONFKEY_CALL_STATE, &call_state);
421 EM_DEBUG_EXCEPTION("vconf_get_int failed");
425 error = feedback_initialize();
426 if (error != FEEDBACK_ERROR_NONE) {
427 EM_DEBUG_EXCEPTION("feedback_initialize failed : [%d]", error);
431 if (call_state > VCONFKEY_CALL_OFF && call_state < VCONFKEY_CALL_STATE_MAX) {
432 error = feedback_play_type(FEEDBACK_TYPE_VIBRATION, FEEDBACK_PATTERN_EMAIL_ON_CALL);
434 error = feedback_play_type(FEEDBACK_TYPE_VIBRATION, FEEDBACK_PATTERN_EMAIL);
437 if (error != FEEDBACK_ERROR_NONE) {
438 EM_DEBUG_EXCEPTION("feedback_play failed : [%d]", error);
446 error = feedback_deinitialize();
447 if (error != FEEDBACK_ERROR_NONE) {
448 EM_DEBUG_EXCEPTION("feedback_deinitialize failed : [%d]", error);
455 int emcore_sound_mp_player_start(char *filepath)
457 EM_DEBUG_FUNC_BEGIN();
459 int err = MM_ERROR_NONE;
465 if ((err = vconf_get_int(VCONFKEY_SETAPPL_NOTI_SOUND_VOLUME_INT, &volume)) == -1)
467 EM_DEBUG_LOG("vconf_get_int failed \n");
471 mm_player_set_message_callback(email_mmhandle, emcore_mp_player_state_cb, (void *)email_mmhandle);
473 EM_DEBUG_LOG("Before mm_player_set_attribute filepath = %s", filepath);
474 if ((err = mm_player_set_attribute(email_mmhandle, NULL, "sound_volume_type", MM_SOUND_VOLUME_TYPE_NOTIFICATION, "profile_uri", filepath, EM_SAFE_STRLEN(filepath), NULL)) != MM_ERROR_NONE)
476 EM_DEBUG_EXCEPTION("mm_player_set_attribute faile [ %d ] ", err);
480 EM_DEBUG_LOG("After mm_player_set_attribute");
482 if ((err = mm_player_realize(email_mmhandle)) != MM_ERROR_NONE)
484 EM_DEBUG_EXCEPTION("mm_player_realize fail [%d]", err);
488 if ((err = mm_player_start(email_mmhandle)) != MM_ERROR_NONE)
490 EM_DEBUG_EXCEPTION("mm_player_start fail [%d]", err);
494 if ((err = g_timeout_add(TIMER, (GSourceFunc)mp_player_timeout_cb, NULL) <= 0))
496 EM_DEBUG_EXCEPTION("g_timeout_add - Failed to start timer");
504 int emcore_sound_mp_player_stop()
506 EM_DEBUG_FUNC_BEGIN();
508 int err = MM_ERROR_NONE;
510 if ((err = mm_player_stop(email_mmhandle)) != MM_ERROR_NONE)
512 EM_DEBUG_EXCEPTION("mm_player_stop fail [%d]", err);
516 if ((err = mm_player_unrealize(email_mmhandle)) != MM_ERROR_NONE)
518 EM_DEBUG_EXCEPTION("mm_player_unrealize [%d]", err);
526 bool emcore_sound_mp_player_destory()
528 EM_DEBUG_FUNC_BEGIN();
530 int err = MM_ERROR_NONE;
532 if ((err = mm_player_destroy(email_mmhandle)) != MM_ERROR_NONE) {
533 EM_DEBUG_EXCEPTION("mm_player_destory [%d]", err);
543 int get_vconf_data(int key, int *return_value)
545 EM_DEBUG_FUNC_BEGIN();
546 int err = -1, value = 0;
550 case EMAIL_SOUND_STATUS:
551 err = vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &value);
552 EM_DEBUG_LOG("EMAIL_SOUND_STATUS[%d]", value);
554 case EMAIL_VIBE_STATUS:
555 err = vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &value);
556 EM_DEBUG_LOG("EMAIL_VIBRATION_STATUS[%d]", value);
558 case EMAIL_ALERT_REP_TYPE:
559 err = vconf_get_int(VCONFKEY_SETAPPL_NOTI_EMAIL_ALERT_REP_TYPE_INT, &value);
560 EM_DEBUG_LOG("EMAIL_ALERT_REP_TYPE[%d]", value);
562 case EMAIL_ALERT_VOLUME:
563 err = vconf_get_int(VCONFKEY_SETAPPL_NOTI_SOUND_VOLUME_INT, &value);
564 EM_DEBUG_LOG("EMAIL_ALERT_VOLUME[%d]", value);
566 case EMAIL_ALERT_VIBE_STENGTH:
567 err = vconf_get_int(VCONFKEY_SETAPPL_NOTI_VIBRATION_LEVEL_INT, &value);
568 EM_DEBUG_LOG("EMAIL_ALERT_VIBE_STENGTH[%d]", value);
572 EM_DEBUG_LOG("Uuknown request\n");
579 EM_DEBUG_LOG("Vconf_get_int failed\n");
583 *return_value = value;
588 int emcore_get_alert_type()
590 EM_DEBUG_FUNC_BEGIN();
591 int sound_status = 0, vibe_status = 0;
595 if (!(err = get_vconf_data(EMAIL_SOUND_STATUS, &sound_status))) {
596 EM_DEBUG_EXCEPTION("Don't get sound status");
600 if (!(err = get_vconf_data(EMAIL_VIBE_STATUS, &vibe_status)))
602 EM_DEBUG_EXCEPTION("Don't get vibration status");
606 if (sound_status && vibe_status)
607 alert_type = EMAIL_ALERT_TYPE_MELODY_AND_VIB;
608 else if (sound_status)
609 alert_type = EMAIL_ALERT_TYPE_MELODY;
610 else if (vibe_status)
611 alert_type = EMAIL_ALERT_TYPE_VIB;
613 alert_type = EMAIL_ALERT_TYPE_MUTE;
619 INTERNAL_FUNC int emcore_start_thread_for_alerting_new_mails(int *err_code)
621 EM_DEBUG_FUNC_BEGIN();
625 if (err_code != NULL)
626 *err_code = EMAIL_ERROR_NONE;
630 EM_DEBUG_EXCEPTION("Alert service is already running...");
631 if (err_code != NULL)
632 *err_code = EMAIL_ERROR_UNKNOWN;
637 THREAD_CREATE(g_alert_thread, start_alert_thread, NULL, thread_error);
638 if (thread_error != 0)
640 EM_DEBUG_EXCEPTION("Cannot create alert thread");
641 if (err_code != NULL)
642 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
647 if (err_code != NULL)
648 *err_code = EMAIL_ERROR_NONE;
653 int emcore_alarm_timeout_cb(int timer_id, void *user_parm)
655 EM_DEBUG_FUNC_BEGIN();
657 int err = EMAIL_ERROR_NONE;
658 int total_unread_count = 0;
659 int total_mail_count = 0;
660 email_mailbox_t mailbox;
662 memset(&mailbox, 0x00, sizeof(email_mailbox_t));
664 mailbox.account_id = ALL_ACCOUNT;
665 mailbox.mailbox_name = NULL;
667 if (!emcore_get_mail_count(&mailbox, &total_mail_count, &total_unread_count, &err)) {
668 EM_DEBUG_EXCEPTION("emcore_get_mail_count failed - %d\n", err);
672 EM_DEBUG_LOG(">>>> total_unread_count : [%d]\n", total_unread_count);
674 if (total_unread_count) {
675 emcore_start_alert();
682 bool set_alarm(int repetition_time)
684 EM_DEBUG_FUNC_BEGIN();
687 alarm_date_t alarm_date;
689 struct tm current_tm;
692 localtime_r(¤t_time, ¤t_tm);
695 alarm_date.month = 0;
698 EM_DEBUG_LOG("Current time : [%d]-[%d]-[%d]", current_tm.tm_hour, current_tm.tm_min, current_tm.tm_sec);
700 if (current_tm.tm_min + repetition_time < 60) {
701 alarm_date.hour = current_tm.tm_hour;
702 alarm_date.min = current_tm.tm_min + repetition_time;
704 if (current_tm.tm_hour < 12) {
705 alarm_date.hour = current_tm.tm_hour + 1;
707 alarm_date.hour = (current_tm.tm_hour + 1) % 12;
710 alarm_date.min = (current_tm.tm_min + repetition_time) % 60;
713 alarm_date.sec = current_tm.tm_sec;
715 alarmmgr_set_time(alarm_info, alarm_date);
716 alarmmgr_set_repeat_mode(alarm_info, ALARM_REPEAT_MODE_ONCE, 0);
717 alarmmgr_set_type(alarm_info, ALARM_TYPE_VOLATILE);
718 alarmmgr_add_alarm_with_localtime(alarm_info, NULL, &email_alarm_id);
720 ret = alarmmgr_set_cb(emcore_alarm_timeout_cb, NULL);
722 if (ret != ALARMMGR_RESULT_SUCCESS) {
723 EM_DEBUG_EXCEPTION("Failed : alarmmgr_set_cb() -> error[%d]", ret);
727 EM_DEBUG_LOG("Alarm time : [%d]-[%d]-[%d]-[%d]-[%d]-[%d]", alarm_date.year, alarm_date.month, alarm_date.day, alarm_date.hour, alarm_date.min, alarm_date.sec);
732 void emcore_set_repetition_alarm(int repetition)
734 EM_DEBUG_FUNC_BEGIN();
736 int repetition_time = 0;
738 switch (repetition) {
739 case EMAIL_GCONF_VALUE_REPEAT_NONE:
742 case EMAIL_GCONF_VALUE_REPEAT_2MINS:
745 case EMAIL_GCONF_VALUE_REPEAT_5MINS:
748 case EMAIL_GCONF_VALUE_REPEAT_10MINS:
749 repetition_time = 10;
752 EM_DEBUG_EXCEPTION("Invalid repetition time");
756 EM_DEBUG_LOG("repetition time is %d", repetition_time);
758 if (repetition_time > 0) {
759 set_alarm(repetition_time);
765 void *start_alert_thread(void *arg)
767 EM_DEBUG_FUNC_BEGIN();
772 if (!emcore_alert_init())
774 EM_DEBUG_EXCEPTION("Error : emcore_alert_init failed");
779 if (!emcore_alert_create()) {
780 EM_DEBUG_EXCEPTION("Error : emcore_alert_create failed");
784 err = get_vconf_data(EMAIL_ALERT_REP_TYPE, &repetition);
785 emcore_set_repetition_alarm(repetition);
787 ENTER_CRITICAL_SECTION(sound_mutex);
788 SLEEP_CONDITION_VARIABLE(sound_condition , sound_mutex);
790 switch (emcore_get_alert_type())
792 case EMAIL_ALERT_TYPE_MELODY:
793 if (!emcore_sound_mp_player_create()) {
794 EM_DEBUG_LOG("emcore_sound_mp_player_create failed : [%d]", email_mmhandle);
797 emcore_sound_mp_player_start(filename);
799 case EMAIL_ALERT_TYPE_VIB:
800 emcore_vibration_start();
802 case EMAIL_ALERT_TYPE_MELODY_AND_VIB:
803 emcore_vibration_start();
804 if (!emcore_sound_mp_player_create()) {
805 EM_DEBUG_LOG("emcore_sound_mp_player_create failed : [%d]", email_mmhandle);
808 emcore_sound_mp_player_start(filename);
810 case EMAIL_ALERT_TYPE_MUTE:
811 EM_DEBUG_LOG("Alert type is mute!!");
814 EM_DEBUG_EXCEPTION("alert type is strange");
815 emcore_alert_destory();
818 LEAVE_CRITICAL_SECTION(sound_mutex);
819 EM_DEBUG_LOG("Start FINISH");
820 emcore_alarm_destory();
827 INTERNAL_FUNC void emcore_start_alert()
829 EM_DEBUG_FUNC_BEGIN("setting_noti_status : [%d]", setting_noti_status);
831 if (setting_noti_status == SETTING_NOTI_STATUS_OFF)
834 ENTER_CRITICAL_SECTION(sound_mutex);
835 WAKE_CONDITION_VARIABLE(sound_condition);
836 LEAVE_CRITICAL_SECTION(sound_mutex);