4 * Copyright (c) 2000 - 2011 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 email_vibe_handle = 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_vibration_destory();
50 int emcore_vibration_stop();
51 int emcore_sound_mp_player_stop();
52 bool emcore_sound_mp_player_destory();
53 void *start_alert_thread(void *arg);
55 int emcore_alert_sound_init()
57 int ret = MM_ERROR_NONE;
58 if ((ret = mm_session_init(MM_SESSION_TYPE_NOTIFY)) != MM_ERROR_NONE)
59 EM_DEBUG_EXCEPTION("mm_session_int failed");
64 int emcore_alert_alarm_init()
66 int ret = ALARMMGR_RESULT_SUCCESS;
68 ret = alarmmgr_init("email-service-0");
70 EM_DEBUG_EXCEPTION("alarmmgr_init failed : [%d]", ret);
75 int emcore_alert_sound_filepath_init()
77 filename = (char *)em_malloc(MAX_PATH);
78 if (filename == NULL) {
79 EM_DEBUG_EXCEPTION("Memory malloc error");
83 memset(filename, 0, MAX_PATH);
87 int emcore_alert_vibe_init()
89 email_vibe_handle = device_haptic_open(DEV_IDX_0, 0);
90 if (!email_vibe_handle) {
91 EM_DEBUG_EXCEPTION("device_haptic_open failed");
98 void emcore_noti_key_changed_cb(keynode_t *key_node, void *data)
102 switch (vconf_keynode_get_type(key_node)) {
104 ret = alarmmgr_remove_alarm(email_alarm_id);
105 if (ret != ALARMMGR_RESULT_SUCCESS) {
106 EM_DEBUG_EXCEPTION("delete of alarm id failed");
108 emcore_set_repetition_alarm(vconf_keynode_get_int(key_node));
110 case VCONF_TYPE_STRING:
111 filename = vconf_keynode_get_str(key_node);
114 EM_DEBUG_EXCEPTION("Invalid key type");
120 bool emcore_noti_init(void *data)
122 struct appdata *ap = data;
124 if (vconf_notify_key_changed(VCONFKEY_SETAPPL_NOTI_EMAIL_ALERT_REP_TYPE_INT, emcore_noti_key_changed_cb, ap) < 0) {
125 EM_DEBUG_EXCEPTION("Register failed : alert type");
129 if (vconf_notify_key_changed(VCONFKEY_SETAPPL_NOTI_EMAIL_RINGTONE_PATH_STR, emcore_noti_key_changed_cb, ap) < 0) {
130 EM_DEBUG_EXCEPTION("Register failed : Ringtone path");
137 int emcore_alert_init()
139 EM_DEBUG_FUNC_BEGIN();
143 if (!emcore_alert_sound_filepath_init()) {
144 EM_DEBUG_EXCEPTION("emcore_alert_sound_filepath_init failed");
148 if ((err = emcore_alert_sound_init()) != MM_ERROR_NONE) {
149 EM_DEBUG_EXCEPTION("emcore_alert_sound_init failed : [%d]", err);
153 if ((err = emcore_alert_alarm_init()) != ALARMMGR_RESULT_SUCCESS) {
154 EM_DEBUG_EXCEPTION("emcore_alert_alarm_init failed : [%d]", err);
158 if (!emcore_alert_vibe_init()) {
159 EM_DEBUG_EXCEPTION("emcore_alert_vibe_init failed");
163 if (!emcore_noti_init(NULL)) {
164 EM_DEBUG_EXCEPTION("emcore_noti_init failed");
172 bool emcore_set_mp_filepath(const char *key)
174 filename = vconf_get_str(key);
175 if (filename == NULL)
181 int emcore_mp_player_state_cb(int message, void *param, void *user_param)
185 case MM_MESSAGE_ERROR:
186 EM_DEBUG_LOG("Error is happened.");
187 if (email_mmhandle) {
188 emcore_sound_mp_player_destory();
191 case MM_MESSAGE_BEGIN_OF_STREAM:
192 EM_DEBUG_LOG("Play is started.");
194 case MM_MESSAGE_END_OF_STREAM:
195 EM_DEBUG_LOG("End of stream.");
196 ENTER_CRITICAL_SECTION(mmhandle_mutex);
199 emcore_sound_mp_player_stop();
200 emcore_sound_mp_player_destory();
202 LEAVE_CRITICAL_SECTION(mmhandle_mutex);
205 EM_DEBUG_LOG("Message = %d", message);
211 bool emcore_sound_mp_player_create()
213 EM_DEBUG_FUNC_BEGIN();
216 if (email_mmhandle) {
217 EM_DEBUG_LOG("already create the handle");
221 if ((err = mm_player_create(&email_mmhandle)) != MM_ERROR_NONE) {
222 EM_DEBUG_EXCEPTION("mm_player create fail [%d]", err);
229 bool emcore_vibration_create()
231 EM_DEBUG_FUNC_BEGIN();
233 email_vibe_handle = device_haptic_open(DEV_IDX_0, 0);
235 if (email_vibe_handle < 0) {
236 EM_DEBUG_EXCEPTION("vibration create failed");
243 bool emcore_alarm_create()
245 EM_DEBUG_FUNC_BEGIN();
247 alarm_info = alarmmgr_create_alarm();
249 if (alarm_info == NULL) {
250 EM_DEBUG_EXCEPTION("alarm create failed");
258 bool emcore_alarm_destory()
260 EM_DEBUG_FUNC_BEGIN();
263 ret = alarmmgr_free_alarm(alarm_info);
265 if (ret != ALARMMGR_RESULT_SUCCESS) {
266 EM_DEBUG_EXCEPTION("alarm free failed");
274 bool emcore_alert_create()
276 EM_DEBUG_FUNC_BEGIN();
278 /* Create the alarm handle */
279 if (!emcore_alarm_create()) {
280 EM_DEBUG_EXCEPTION("emcore_alarm_create failed.");
284 /* Set the music file in alert */
285 if (!emcore_set_mp_filepath(VCONFKEY_SETAPPL_NOTI_EMAIL_RINGTONE_PATH_STR)) {
286 /* TODO : Add code to set default ringtone path */
287 EM_DEBUG_EXCEPTION("emcore_set_mp_filepath failed.");
295 bool emcore_alert_destory()
297 EM_DEBUG_FUNC_BEGIN();
300 /* Destroy the music player handle */
301 if (!emcore_sound_mp_player_destory()) {
302 EM_DEBUG_EXCEPTION("emcore_sound_mp_player_destory fail");
306 /* Destroy the vibration handle */
307 if (!emcore_vibration_destory()) {
308 EM_DEBUG_EXCEPTION("emcore_vibration_destory fail");
312 /* Destroy the alarm handle */
313 ret = alarmmgr_free_alarm(alarm_info);
314 if (ret != ALARMMGR_RESULT_SUCCESS) {
315 EM_DEBUG_EXCEPTION("alarmmgr_free_alarm fail");
319 /* Set the music file in alert */
320 EM_SAFE_FREE(filename);
326 gboolean mp_player_timeout_cb(void *data)
328 EM_DEBUG_FUNC_BEGIN();
330 ENTER_CRITICAL_SECTION(mmhandle_mutex);
333 emcore_sound_mp_player_stop();
334 emcore_sound_mp_player_destory();
336 LEAVE_CRITICAL_SECTION(mmhandle_mutex);
342 gboolean vibration_timeout_cb(void *data)
344 EM_DEBUG_FUNC_BEGIN();
346 emcore_vibration_stop();
347 emcore_vibration_destory();
353 bool emcore_vibration_start(int haptic_level)
355 EM_DEBUG_FUNC_BEGIN();
357 int vibPattern = EFFCTVIBE_NOTIFICATION;
359 if (haptic_level == 0) {
360 EM_DEBUG_LOG("The level of haptic is zero");
364 ret = device_haptic_play_pattern(email_vibe_handle, vibPattern, HAPTIC_TEST_ITERATION, haptic_level);
367 EM_DEBUG_EXCEPTION("Fail to play haptic : [%d]", ret);
371 if ((ret = g_timeout_add(TIMER, (GSourceFunc) vibration_timeout_cb, NULL) <= 0))
373 EM_DEBUG_EXCEPTION("play_alert - Failed to start timer [%d]", ret);
381 int emcore_vibration_stop()
383 int err = MM_ERROR_NONE;
384 if ((err = device_haptic_stop_play(email_vibe_handle)) != 0)
385 EM_DEBUG_EXCEPTION("Fail to stop haptic : [%d]", err);
390 int emcore_vibration_destory()
392 int err = MM_ERROR_NONE;
393 if ((err = device_haptic_close(email_vibe_handle)) != 0)
394 EM_DEBUG_EXCEPTION("Fail to close haptic : [%d]", err);
398 int emcore_sound_mp_player_start(char *filepath)
400 EM_DEBUG_FUNC_BEGIN();
402 int err = MM_ERROR_NONE;
408 if ((err = vconf_get_int(VCONFKEY_SETAPPL_NOTI_SOUND_VOLUME_INT, &volume)) == -1)
410 EM_DEBUG_LOG("vconf_get_int failed \n");
414 mm_player_set_message_callback(email_mmhandle, emcore_mp_player_state_cb, (void *)email_mmhandle);
416 EM_DEBUG_LOG("Before mm_player_set_attribute filepath = %s", filepath);
417 if ((err = mm_player_set_attribute(email_mmhandle, NULL, "sound_volume_type", MM_SOUND_VOLUME_TYPE_NOTIFICATION, "profile_uri", filepath, strlen(filepath), NULL)) != MM_ERROR_NONE)
419 EM_DEBUG_EXCEPTION("mm_player_set_attribute faile [ %d ] ", err);
423 EM_DEBUG_LOG("After mm_player_set_attribute");
425 if ((err = mm_player_realize(email_mmhandle)) != MM_ERROR_NONE)
427 EM_DEBUG_EXCEPTION("mm_player_realize fail [%d]", err);
431 if ((err = mm_player_start(email_mmhandle)) != MM_ERROR_NONE)
433 EM_DEBUG_EXCEPTION("mm_player_start fail [%d]", err);
437 if ((err = g_timeout_add(TIMER, (GSourceFunc)mp_player_timeout_cb, NULL) <= 0))
439 EM_DEBUG_EXCEPTION("g_timeout_add - Failed to start timer");
447 int emcore_sound_mp_player_stop()
449 EM_DEBUG_FUNC_BEGIN();
451 int err = MM_ERROR_NONE;
453 if ((err = mm_player_stop(email_mmhandle)) != MM_ERROR_NONE)
455 EM_DEBUG_EXCEPTION("mm_player_stop fail [%d]", err);
459 if ((err = mm_player_unrealize(email_mmhandle)) != MM_ERROR_NONE)
461 EM_DEBUG_EXCEPTION("mm_player_unrealize [%d]", err);
469 bool emcore_sound_mp_player_destory()
471 EM_DEBUG_FUNC_BEGIN();
473 int err = MM_ERROR_NONE;
475 if ((err = mm_player_destroy(email_mmhandle)) != MM_ERROR_NONE) {
476 EM_DEBUG_EXCEPTION("mm_player_destory [%d]", err);
486 int get_vconf_data(int key, int *return_value)
488 EM_DEBUG_FUNC_BEGIN();
489 int err = -1, value = 0;
493 case EMAIL_SOUND_STATUS:
494 err = vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &value);
495 EM_DEBUG_LOG("EMAIL_SOUND_STATUS[%d]", value);
497 case EMAIL_VIBE_STATUS:
498 err = vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &value);
499 EM_DEBUG_LOG("EMAIL_VIBRATION_STATUS[%d]", value);
501 case EMAIL_ALERT_REP_TYPE:
502 err = vconf_get_int(VCONFKEY_SETAPPL_NOTI_EMAIL_ALERT_REP_TYPE_INT, &value);
503 EM_DEBUG_LOG("EMAIL_ALERT_REP_TYPE[%d]", value);
505 case EMAIL_ALERT_VOLUME:
506 err = vconf_get_int(VCONFKEY_SETAPPL_NOTI_SOUND_VOLUME_INT, &value);
507 EM_DEBUG_LOG("EMAIL_ALERT_VOLUME[%d]", value);
509 case EMAIL_ALERT_VIBE_STENGTH:
510 err = vconf_get_int(VCONFKEY_SETAPPL_NOTI_VIBRATION_LEVEL_INT, &value);
511 EM_DEBUG_LOG("EMAIL_ALERT_VIBE_STENGTH[%d]", value);
515 EM_DEBUG_LOG("Uuknown request\n");
522 EM_DEBUG_LOG("Vconf_get_int failed\n");
526 *return_value = value;
531 int emcore_get_alert_type()
533 EM_DEBUG_FUNC_BEGIN();
534 int sound_status = 0, vibe_status = 0;
538 if (!(err = get_vconf_data(EMAIL_SOUND_STATUS, &sound_status)))
540 EM_DEBUG_EXCEPTION("Don't get sound status");
544 if (!(err = get_vconf_data(EMAIL_VIBE_STATUS, &vibe_status)))
546 EM_DEBUG_EXCEPTION("Don't get vibration status");
550 if (sound_status && vibe_status)
551 alert_type = EMAIL_ALERT_TYPE_MELODY_AND_VIB;
552 else if (sound_status)
553 alert_type = EMAIL_ALERT_TYPE_MELODY;
554 else if (vibe_status)
555 alert_type = EMAIL_ALERT_TYPE_VIB;
557 alert_type = EMAIL_ALERT_TYPE_MUTE;
563 INTERNAL_FUNC int emcore_start_thread_for_alerting_new_mails(int *err_code)
565 EM_DEBUG_FUNC_BEGIN();
569 if (err_code != NULL)
570 *err_code = EMAIL_ERROR_NONE;
574 EM_DEBUG_EXCEPTION("Alert service is already running...");
575 if (err_code != NULL)
576 *err_code = EMAIL_ERROR_UNKNOWN;
581 THREAD_CREATE(g_alert_thread, start_alert_thread, NULL, thread_error);
582 if (thread_error != 0)
584 EM_DEBUG_EXCEPTION("Cannot create alert thread");
585 if (err_code != NULL)
586 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
591 if (err_code != NULL)
592 *err_code = EMAIL_ERROR_NONE;
597 int emcore_alarm_timeout_cb(int timer_id, void *user_parm)
599 EM_DEBUG_FUNC_BEGIN();
601 int err = EMAIL_ERROR_NONE;
602 int total_unread_count = 0;
603 int total_mail_count = 0;
604 email_mailbox_t mailbox;
606 memset(&mailbox, 0x00, sizeof(email_mailbox_t));
608 mailbox.account_id = ALL_ACCOUNT;
609 mailbox.mailbox_name = NULL;
611 if (!emcore_get_mail_count(&mailbox, &total_mail_count, &total_unread_count, &err)) {
612 EM_DEBUG_EXCEPTION("emcore_get_mail_count failed - %d\n", err);
616 EM_DEBUG_LOG(">>>> total_unread_count : [%d]\n", total_unread_count);
618 if (total_unread_count) {
619 emcore_start_alert();
626 bool set_alarm(int repetition_time)
628 EM_DEBUG_FUNC_BEGIN();
631 alarm_date_t alarm_date;
633 struct tm current_tm;
636 localtime_r(¤t_time, ¤t_tm);
639 alarm_date.month = 0;
642 EM_DEBUG_LOG("Current time : [%d]-[%d]-[%d]", current_tm.tm_hour, current_tm.tm_min, current_tm.tm_sec);
644 if (current_tm.tm_min + repetition_time < 60) {
645 alarm_date.hour = current_tm.tm_hour;
646 alarm_date.min = current_tm.tm_min + repetition_time;
648 if (current_tm.tm_hour < 12) {
649 alarm_date.hour = current_tm.tm_hour + 1;
651 alarm_date.hour = (current_tm.tm_hour + 1) % 12;
654 alarm_date.min = (current_tm.tm_min + repetition_time) % 60;
657 alarm_date.sec = current_tm.tm_sec;
659 alarmmgr_set_time(alarm_info, alarm_date);
660 alarmmgr_set_repeat_mode(alarm_info, ALARM_REPEAT_MODE_ONCE, 0);
661 alarmmgr_set_type(alarm_info, ALARM_TYPE_VOLATILE);
662 alarmmgr_add_alarm_with_localtime(alarm_info, NULL, &email_alarm_id);
664 ret = alarmmgr_set_cb(emcore_alarm_timeout_cb, NULL);
667 EM_DEBUG_EXCEPTION("Failed : alarmmgr_set_cb() -> error[%d]", ret);
671 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);
676 void emcore_set_repetition_alarm(int repetition)
678 EM_DEBUG_FUNC_BEGIN();
680 int repetition_time = 0;
682 switch (repetition) {
683 case EMAIL_GCONF_VALUE_REPEAT_NONE:
686 case EMAIL_GCONF_VALUE_REPEAT_2MINS:
689 case EMAIL_GCONF_VALUE_REPEAT_5MINS:
692 case EMAIL_GCONF_VALUE_REPEAT_10MINS:
693 repetition_time = 10;
696 EM_DEBUG_EXCEPTION("Invalid repetition time");
700 EM_DEBUG_LOG("repetition time is %d", repetition_time);
702 if (repetition_time > 0) {
703 set_alarm(repetition_time);
709 void *start_alert_thread(void *arg)
716 if (!emcore_alert_init())
718 EM_DEBUG_EXCEPTION("Error : emcore_alert_init failed");
724 if (!emcore_alert_create()) {
725 EM_DEBUG_EXCEPTION("Error : emcore_alert_create failed");
729 err = get_vconf_data(EMAIL_ALERT_REP_TYPE, &level);
730 emcore_set_repetition_alarm(level);
732 ENTER_CRITICAL_SECTION(sound_mutex);
733 SLEEP_CONDITION_VARIABLE(sound_condition , sound_mutex);
735 err = get_vconf_data(EMAIL_ALERT_VIBE_STENGTH, &level);
737 switch (emcore_get_alert_type())
739 case EMAIL_ALERT_TYPE_MELODY:
740 if (!emcore_sound_mp_player_create()) {
741 EM_DEBUG_LOG("emcore_sound_mp_player_create failed : [%d]", email_mmhandle);
744 emcore_sound_mp_player_start(filename);
746 case EMAIL_ALERT_TYPE_VIB:
747 emcore_vibration_create();
748 emcore_vibration_start(level);
750 case EMAIL_ALERT_TYPE_MELODY_AND_VIB:
751 emcore_vibration_create();
752 emcore_vibration_start(level);
753 if (!emcore_sound_mp_player_create()) {
754 EM_DEBUG_LOG("emcore_sound_mp_player_create failed : [%d]", email_mmhandle);
757 emcore_sound_mp_player_start(filename);
759 case EMAIL_ALERT_TYPE_MUTE:
760 EM_DEBUG_LOG("Alert type is mute!!");
763 EM_DEBUG_EXCEPTION("alert type is strange");
764 emcore_alert_destory();
767 LEAVE_CRITICAL_SECTION(sound_mutex);
768 EM_DEBUG_LOG("Start FINISH");
769 emcore_alarm_destory();
774 INTERNAL_FUNC void emcore_start_alert()
776 ENTER_CRITICAL_SECTION(sound_mutex);
777 WAKE_CONDITION_VARIABLE(sound_condition);
778 LEAVE_CRITICAL_SECTION(sound_mutex);