cffb5b1c21105d5a902575483f70f54ba7783108
[platform/core/messaging/email-service.git] / email-core / email-core-sound.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7
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
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
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.
19 *
20 */
21
22
23 #include <stdlib.h>
24 #include <pthread.h>
25
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-core-alarm.h"
32 #include "email-utilities.h"
33
34 #define TIMER 30000   // 30 seconds
35 #define HAPTIC_TEST_ITERATION 1
36 #define EMAIL_ALARM_REFERENCE_ID_FOR_ALERT_TONE -1
37
38 static MMHandleType email_mmhandle = 0;
39 static int setting_noti_status = 0;
40
41 static char *filename;
42
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;
47
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);
52
53 bool emcore_set_mp_filepath(const char *key)
54 {
55         filename = vconf_get_str(key);
56         if (filename == NULL)
57                 return false;
58
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");
62                 return false;
63         }
64
65         return true;
66 }
67
68 int emcore_alert_sound_init()
69 {
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");
73
74         return ret;
75 }
76
77 int emcore_alert_sound_filepath_init()
78 {
79         filename = (char  *)em_malloc(MAX_PATH);
80         if (filename == NULL) {
81                 EM_DEBUG_EXCEPTION("Memory malloc error");      
82                 return false;
83         }
84
85         if (!emcore_set_mp_filepath(VCONFKEY_SETAPPL_NOTI_EMAIL_RINGTONE_PATH_STR)) {
86                 /* TODO : Add code to set default ringtone path */
87                 EM_DEBUG_EXCEPTION("emcore_set_mp_filepath failed.");
88                 return false;
89         }
90
91         return true;
92 }
93
94 void emcore_global_noti_key_changed_cb(keynode_t *key_node, void *data)
95 {
96         EM_DEBUG_FUNC_BEGIN();
97         int err = EMAIL_ERROR_NONE;
98
99         switch (vconf_keynode_get_type(key_node)) {
100         case VCONF_TYPE_INT:
101                 if ((err = emcore_delete_alram_data_by_reference_id(EMAIL_ALARM_CLASS_NEW_MAIL_ALERT, EMAIL_ALARM_REFERENCE_ID_FOR_ALERT_TONE)) != EMAIL_ERROR_NONE) {
102                         EM_DEBUG_EXCEPTION("emcore_delete_alram_data_by_reference_id failed [%d]", err);
103                 }
104                 emcore_set_repetition_alarm(vconf_keynode_get_int(key_node));
105                 break;
106         case VCONF_TYPE_STRING:
107                 filename = vconf_keynode_get_str(key_node);
108                 break;
109         default:
110                 EM_DEBUG_EXCEPTION("Invalid key type");
111                 break;
112         }
113
114         EM_DEBUG_FUNC_END();
115 }
116
117 void emcore_email_noti_key_changed_cb(keynode_t *key_node, void *data)
118 {
119         EM_DEBUG_FUNC_BEGIN();
120         int err = EMAIL_ERROR_NONE;
121
122         switch (vconf_keynode_get_type(key_node)) {
123         case VCONF_TYPE_INT:
124                 if ((err = emcore_delete_alram_data_by_reference_id(EMAIL_ALARM_CLASS_NEW_MAIL_ALERT, EMAIL_ALARM_REFERENCE_ID_FOR_ALERT_TONE)) != EMAIL_ERROR_NONE) {
125                         EM_DEBUG_EXCEPTION("emcore_delete_alram_data_by_reference_id failed [%d]", err);
126                 }
127
128                 emcore_set_repetition_alarm(vconf_keynode_get_int(key_node));
129                 break;
130         case VCONF_TYPE_STRING:
131                 filename = vconf_keynode_get_str(key_node);
132                 break;
133         default:
134                 EM_DEBUG_EXCEPTION("Invalid key type");
135                 break;
136         }
137         EM_DEBUG_FUNC_END();
138 }
139
140 bool emcore_update_noti_status()
141 {
142         EM_DEBUG_FUNC_BEGIN();
143         int ticker_noti = 0;
144
145         /* Get the priority noti ticker */
146         if (vconf_get_bool(VCONF_VIP_NOTI_NOTIFICATION_TICKER, &ticker_noti) != 0) {
147                 EM_DEBUG_EXCEPTION("vconf_get_bool failed");
148                 return false;
149         }
150
151         EM_DEBUG_LOG("ticker_noti of vip : [%d]", ticker_noti);
152
153         if (ticker_noti <= 0) {
154                 /* Get the Global noti ticker */
155                 if (vconf_get_bool(VCONFKEY_SETAPPL_STATE_TICKER_NOTI_EMAIL_BOOL, &ticker_noti) != 0) {
156                                 EM_DEBUG_EXCEPTION("Not display the noti of email");
157                                 return false;
158                 }
159
160                 EM_DEBUG_LOG("ticker_noti of global : [%d]", ticker_noti);
161
162                 if (!ticker_noti) {
163                         EM_DEBUG_LOG("Not use the notification");
164                         setting_noti_status = SETTING_NOTI_STATUS_OFF;
165                         return true;
166                 }
167
168                 setting_noti_status = SETTING_NOTI_STATUS_GLOBAL;
169         } else {
170                 setting_noti_status = SETTING_NOTI_STATUS_EMAIL;
171         }
172
173         EM_DEBUG_FUNC_END();
174         return true;
175 }
176
177 void emcore_noti_status_changed_cb(keynode_t *key_node, void *data)
178 {
179         EM_DEBUG_FUNC_BEGIN();
180         if (!emcore_update_noti_status()) {
181                 EM_DEBUG_EXCEPTION("emcore_update_noti_status failed");
182                 return;
183         }
184         EM_DEBUG_FUNC_END();
185 }
186
187 bool emcore_noti_init(void *data)
188 {
189         EM_DEBUG_FUNC_BEGIN();
190         struct appdata *ap = data;
191
192         if (!emcore_update_noti_status()) {
193                 EM_DEBUG_EXCEPTION("emcore_update_noti_status failed");
194                 return false;
195         }
196
197         /* Noti callback registration */
198         if (vconf_notify_key_changed(VCONFKEY_SETAPPL_NOTI_EMAIL_ALERT_REP_TYPE_INT, emcore_global_noti_key_changed_cb, ap) < 0) {
199                 EM_DEBUG_EXCEPTION("Register failed : alert type");
200         }
201
202         if (vconf_notify_key_changed(VCONFKEY_SETAPPL_NOTI_EMAIL_RINGTONE_PATH_STR, emcore_global_noti_key_changed_cb, ap) < 0) {
203                 EM_DEBUG_EXCEPTION("Register failed : Ringtone path");
204         }
205
206         if (vconf_notify_key_changed(VCONF_VIP_NOTI_REP_TYPE, emcore_email_noti_key_changed_cb, ap) < 0) {
207                 EM_DEBUG_EXCEPTION("Register failed : alert type");
208         }
209
210         if (vconf_notify_key_changed(VCONF_VIP_NOTI_RINGTONE_PATH, emcore_email_noti_key_changed_cb, ap) < 0) {
211                 EM_DEBUG_EXCEPTION("Register failed : Ringtone path");
212         }
213
214         if (vconf_notify_key_changed(VCONF_VIP_NOTI_NOTIFICATION_TICKER, emcore_noti_status_changed_cb, ap) < 0) {
215                 EM_DEBUG_EXCEPTION("Register failed : Ringtone path");
216         }
217
218         if (vconf_notify_key_changed(VCONFKEY_SETAPPL_STATE_TICKER_NOTI_EMAIL_BOOL, emcore_noti_status_changed_cb, ap) < 0) {
219                 EM_DEBUG_EXCEPTION("Register failed : Ringtone path");
220         }
221
222         EM_DEBUG_FUNC_END();
223         return true;
224 }
225
226 int emcore_alert_init()
227 {
228         EM_DEBUG_FUNC_BEGIN();
229
230         int err = 0;
231         
232         if (!emcore_alert_sound_filepath_init()) {
233                 EM_DEBUG_EXCEPTION("emcore_alert_sound_filepath_init failed");
234                 return false;
235         }
236
237         if ((err = emcore_alert_sound_init()) != MM_ERROR_NONE) {
238                 EM_DEBUG_EXCEPTION("emcore_alert_sound_init failed : [%d]", err);
239                 return false;
240         }
241
242         if (!emcore_noti_init(NULL)) {
243                 EM_DEBUG_EXCEPTION("emcore_noti_init failed");
244                 return false;           
245         }
246
247         EM_DEBUG_FUNC_END();    
248         return true;
249 }
250
251 int emcore_mp_player_state_cb(int message, void *param, void *user_param)
252 {
253         switch (message)
254         {
255                 case MM_MESSAGE_ERROR:
256                         EM_DEBUG_LOG("Error is happened.");
257                         if (email_mmhandle) {
258                                 emcore_sound_mp_player_destory();
259                         }
260                         break;
261                 case MM_MESSAGE_BEGIN_OF_STREAM:
262                         EM_DEBUG_LOG("Play is started.");
263                         break;
264                 case MM_MESSAGE_END_OF_STREAM:
265                         EM_DEBUG_LOG("End of stream.");
266                         ENTER_CRITICAL_SECTION(mmhandle_mutex);
267                         if (email_mmhandle)
268                         {                       
269                                 emcore_sound_mp_player_stop();
270                                 emcore_sound_mp_player_destory();
271                         }
272                         LEAVE_CRITICAL_SECTION(mmhandle_mutex);
273                         break;
274                 default: 
275                         EM_DEBUG_LOG("Message = %d", message);
276                         break;
277         }
278         return 1;
279 }
280
281 bool emcore_sound_mp_player_create() 
282 {       
283         EM_DEBUG_FUNC_BEGIN();
284         int err = 0;
285         
286         if (email_mmhandle) {
287                 EM_DEBUG_LOG("already create the handle");
288                 return true;
289         }
290
291         if ((err = mm_player_create(&email_mmhandle)) != MM_ERROR_NONE) {
292                 EM_DEBUG_EXCEPTION("mm_player create fail [%d]", err);
293                 return false;
294         }       
295         EM_DEBUG_FUNC_END();
296         return true;
297 }
298
299 bool emcore_alert_create()
300 {
301         EM_DEBUG_FUNC_BEGIN();
302
303 #if 0   
304         /* Set the music file in alert */
305         if (!emcore_set_mp_filepath(VCONFKEY_SETAPPL_NOTI_EMAIL_RINGTONE_PATH_STR)) {
306                 /* TODO : Add code to set default ringtone path */
307                 EM_DEBUG_EXCEPTION("emcore_set_mp_filepath failed.");
308                 return false;
309         }
310 #endif  
311         EM_DEBUG_FUNC_END();
312         return true;
313 }
314
315 bool emcore_alert_destory()
316 {
317         EM_DEBUG_FUNC_BEGIN();
318
319         /* Destroy the music player handle */
320         if (!emcore_sound_mp_player_destory()) {
321                 EM_DEBUG_EXCEPTION("emcore_sound_mp_player_destory fail");
322                 return false;
323         }                       
324         
325         EM_DEBUG_FUNC_END();
326         return true;
327 }
328
329 gboolean mp_player_timeout_cb(void *data)
330 {
331         EM_DEBUG_FUNC_BEGIN();
332
333         ENTER_CRITICAL_SECTION(mmhandle_mutex); 
334         if (email_mmhandle)
335         {                       
336                 emcore_sound_mp_player_stop();
337                 emcore_sound_mp_player_destory();
338         }
339         LEAVE_CRITICAL_SECTION(mmhandle_mutex);
340         
341         EM_DEBUG_FUNC_END();
342         return false;
343 }
344
345 bool emcore_vibration_start()
346 {
347         EM_DEBUG_FUNC_BEGIN();
348         int ret = false;
349         int error = FEEDBACK_ERROR_NONE;
350         int call_state = 0;
351
352         error = vconf_get_int(VCONFKEY_CALL_STATE, &call_state);
353         if (error == -1) {
354                 EM_DEBUG_EXCEPTION("vconf_get_int failed");
355                 goto FINISH_OFF;
356         }
357
358         error = feedback_initialize();
359         if (error != FEEDBACK_ERROR_NONE) {
360                 EM_DEBUG_EXCEPTION("feedback_initialize failed : [%d]", error);
361                 goto FINISH_OFF;
362         }
363
364         if (call_state > VCONFKEY_CALL_OFF && call_state < VCONFKEY_CALL_STATE_MAX) {   
365                 error = feedback_play_type(FEEDBACK_TYPE_VIBRATION, FEEDBACK_PATTERN_EMAIL_ON_CALL);
366         } else {
367                 error = feedback_play_type(FEEDBACK_TYPE_VIBRATION, FEEDBACK_PATTERN_EMAIL);
368         }
369
370         if (error != FEEDBACK_ERROR_NONE) {
371                 EM_DEBUG_EXCEPTION("feedback_play failed : [%d]", error);
372                 goto FINISH_OFF;
373         }
374
375         ret = true;
376
377 FINISH_OFF:
378
379         error = feedback_deinitialize();
380         if (error != FEEDBACK_ERROR_NONE) {
381                 EM_DEBUG_EXCEPTION("feedback_deinitialize failed : [%d]", error);
382         }
383         
384         EM_DEBUG_FUNC_END();
385         return ret;
386 }
387
388 int emcore_sound_mp_player_start(char *filepath)
389 {
390         EM_DEBUG_FUNC_BEGIN();
391
392         int err = MM_ERROR_NONE;
393
394         mm_player_set_message_callback(email_mmhandle, emcore_mp_player_state_cb, (void  *)email_mmhandle);
395
396         EM_DEBUG_LOG("Before mm_player_set_attribute filepath = %s", filepath);
397         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)
398         {
399                 EM_DEBUG_EXCEPTION("mm_player_set_attribute faile [ %d ] ", err);
400                 return err;
401         }
402
403         EM_DEBUG_LOG("After mm_player_set_attribute");  
404
405         if ((err = mm_player_realize(email_mmhandle)) != MM_ERROR_NONE)
406         {
407                 EM_DEBUG_EXCEPTION("mm_player_realize fail [%d]", err);
408                 return err;
409         }
410
411         if ((err = mm_player_start(email_mmhandle)) != MM_ERROR_NONE)
412         {
413                 EM_DEBUG_EXCEPTION("mm_player_start fail [%d]", err);
414                 return err;
415         }
416
417         if ((err = g_timeout_add(TIMER, (GSourceFunc)mp_player_timeout_cb, NULL) <= 0))
418         {
419                 EM_DEBUG_EXCEPTION("g_timeout_add - Failed to start timer");
420                 return err;             
421         }
422         
423         EM_DEBUG_FUNC_END();
424         return err;
425 }
426
427 int emcore_sound_mp_player_stop()
428 {
429         EM_DEBUG_FUNC_BEGIN();
430
431         int err = MM_ERROR_NONE;
432
433         if ((err = mm_player_stop(email_mmhandle)) != MM_ERROR_NONE)
434         {
435                 EM_DEBUG_EXCEPTION("mm_player_stop fail [%d]", err);
436                 return err;
437         }
438
439         if ((err = mm_player_unrealize(email_mmhandle)) != MM_ERROR_NONE)
440         {
441                 EM_DEBUG_EXCEPTION("mm_player_unrealize [%d]", err);
442                 return err;
443         }
444
445         EM_DEBUG_FUNC_END();
446         return err;
447 }
448
449 bool emcore_sound_mp_player_destory()
450 {
451         EM_DEBUG_FUNC_BEGIN();
452
453         int err = MM_ERROR_NONE;
454
455         if ((err = mm_player_destroy(email_mmhandle)) != MM_ERROR_NONE) {
456                 EM_DEBUG_EXCEPTION("mm_player_destory [%d]", err);
457                 return false;
458         }
459
460         email_mmhandle = 0;
461
462         EM_DEBUG_FUNC_END();
463         return true;
464 }       
465
466 int get_vconf_data(int key, int *return_value)
467 {
468         EM_DEBUG_FUNC_BEGIN();
469         int err = -1, value = 0;
470         
471         switch (key)
472         {
473                 case EMAIL_SOUND_STATUS:
474                         err = vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &value);
475                         EM_DEBUG_LOG("EMAIL_SOUND_STATUS[%d]", value);
476                         break;
477                 case EMAIL_VIBE_STATUS:
478                         err = vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &value);
479                         EM_DEBUG_LOG("EMAIL_VIBRATION_STATUS[%d]", value);
480                         break;
481                 case EMAIL_ALERT_REP_TYPE:
482                         err = vconf_get_int(VCONFKEY_SETAPPL_NOTI_EMAIL_ALERT_REP_TYPE_INT, &value);
483                         EM_DEBUG_LOG("EMAIL_ALERT_REP_TYPE[%d]", value);
484                         break;
485                 case EMAIL_ALERT_VOLUME:
486                         err = vconf_get_int(VCONFKEY_SETAPPL_NOTI_SOUND_VOLUME_INT, &value);
487                         EM_DEBUG_LOG("EMAIL_ALERT_VOLUME[%d]", value);
488                         break;
489                 case EMAIL_ALERT_VIBE_STENGTH:
490                         err = vconf_get_int(VCONFKEY_SETAPPL_NOTI_VIBRATION_LEVEL_INT, &value);
491                         EM_DEBUG_LOG("EMAIL_ALERT_VIBE_STENGTH[%d]", value);
492                         break;
493                 default: 
494                 {
495                         EM_DEBUG_LOG("Uuknown request\n");
496                         return false;                   
497                 }
498         }
499         
500         if (err == -1)
501         {
502                 EM_DEBUG_LOG("Vconf_get_int failed\n");
503                 return false;
504         }
505
506         *return_value = value;
507         EM_DEBUG_FUNC_END();
508         return true;
509 }
510
511 int emcore_get_alert_type()
512 {
513         EM_DEBUG_FUNC_BEGIN();
514         int sound_status = 0, vibe_status = 0;
515         int err;
516         int alert_type = -1;
517
518         if (!(err = get_vconf_data(EMAIL_SOUND_STATUS, &sound_status))) {
519                 EM_DEBUG_EXCEPTION("Don't get sound status");
520                 return err;
521         }
522
523         if (!(err = get_vconf_data(EMAIL_VIBE_STATUS, &vibe_status)))
524         {
525                 EM_DEBUG_EXCEPTION("Don't get vibration status");
526                 return err;
527         }
528
529         if (sound_status && vibe_status)
530                 alert_type = EMAIL_ALERT_TYPE_MELODY_AND_VIB;
531         else if (sound_status)
532                 alert_type = EMAIL_ALERT_TYPE_MELODY;
533         else if (vibe_status)
534                 alert_type = EMAIL_ALERT_TYPE_VIB;
535         else
536                 alert_type = EMAIL_ALERT_TYPE_MUTE;
537
538         return alert_type;
539 }
540
541
542 INTERNAL_FUNC int emcore_start_thread_for_alerting_new_mails(int *err_code)
543 {
544         EM_DEBUG_FUNC_BEGIN();
545
546         int thread_error;
547
548         if (err_code != NULL)
549                 *err_code = EMAIL_ERROR_NONE;
550         
551         if (g_alert_thread)
552         {
553                 EM_DEBUG_EXCEPTION("Alert service is already running...");
554                 if (err_code != NULL)
555                         *err_code = EMAIL_ERROR_UNKNOWN;
556                 
557                 return 1;
558         }
559         
560         THREAD_CREATE(g_alert_thread, start_alert_thread, NULL, thread_error);
561         if (thread_error != 0)
562         {
563                 EM_DEBUG_EXCEPTION("Cannot create alert thread");
564                 if (err_code != NULL)
565                         *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
566
567                 return -1;
568         }
569                 
570         if (err_code != NULL)
571                 *err_code = EMAIL_ERROR_NONE;
572
573         return 0;
574 }
575
576 int emcore_alarm_timeout_cb_for_alert(int timer_id, void *user_parm)
577 {
578         EM_DEBUG_FUNC_BEGIN();
579
580         int err = EMAIL_ERROR_NONE;
581         int total_unread_count = 0;
582         int total_mail_count = 0;
583         email_mailbox_t mailbox;
584
585         memset(&mailbox, 0x00, sizeof(email_mailbox_t));
586
587         mailbox.account_id = ALL_ACCOUNT;
588         mailbox.mailbox_name = NULL;
589
590         if (!emcore_get_mail_count(&mailbox, &total_mail_count, &total_unread_count, &err)) {
591                 EM_DEBUG_EXCEPTION("emcore_get_mail_count failed - %d\n", err);
592                 return false;
593         }
594
595         EM_DEBUG_LOG(">>>> total_unread_count : [%d]\n", total_unread_count);
596         
597         if (total_unread_count) {
598                 emcore_start_alert();
599         }
600
601         EM_DEBUG_FUNC_END();    
602         return true;
603 }
604
605 /* repetition_time in minutes */
606 static int emcore_set_alarm_for_alert(int alert_time_in_minute)
607 {
608         EM_DEBUG_FUNC_BEGIN();
609
610         int err = EMAIL_ERROR_NONE;
611         time_t current_time;
612         time_t trigger_at_time;
613
614         time(&current_time);
615
616         trigger_at_time = current_time + (alert_time_in_minute * 60);
617
618         if ((err = emcore_add_alarm(trigger_at_time, EMAIL_ALARM_CLASS_NEW_MAIL_ALERT, EMAIL_ALARM_REFERENCE_ID_FOR_ALERT_TONE, emcore_alarm_timeout_cb_for_alert, NULL)) != EMAIL_ERROR_NONE) {
619                 EM_DEBUG_EXCEPTION("emcore_add_alarm failed [%d]", err);
620                 goto FINISH_OFF;
621         }
622
623 FINISH_OFF:
624
625         EM_DEBUG_FUNC_END("err [%d]", err);
626         return err;
627 }
628
629 void emcore_set_repetition_alarm(int repetition)
630 {
631         EM_DEBUG_FUNC_BEGIN();
632
633         int repetition_time = 0;
634         
635         switch (repetition) {
636         case EMAIL_GCONF_VALUE_REPEAT_NONE:
637                 repetition_time = 0;
638                 break;
639         case EMAIL_GCONF_VALUE_REPEAT_2MINS:
640                 repetition_time = 2;
641                 break;
642         case EMAIL_GCONF_VALUE_REPEAT_5MINS:
643                 repetition_time = 5;
644                 break;
645         case EMAIL_GCONF_VALUE_REPEAT_10MINS:           
646                 repetition_time = 10;
647                 break;
648         default:
649                 EM_DEBUG_EXCEPTION("Invalid repetition time");
650                 return;
651         }
652
653         EM_DEBUG_LOG("repetition time is %d", repetition_time);
654
655         if (repetition_time > 0) {
656                 emcore_set_alarm_for_alert(repetition_time);
657         } 
658
659         EM_DEBUG_FUNC_END();
660 }
661
662 void *start_alert_thread(void *arg)
663 {
664         EM_DEBUG_FUNC_BEGIN();
665         
666         int err = 0;
667         int repetition = 0;
668
669         if (!emcore_alert_init())
670         {
671                 EM_DEBUG_EXCEPTION("Error : emcore_alert_init failed");
672                 return 0;
673         }
674
675         while (1) {
676                 if (!emcore_alert_create()) {
677                         EM_DEBUG_EXCEPTION("Error : emcore_alert_create failed");
678                         return 0;
679                 }
680
681                 err = get_vconf_data(EMAIL_ALERT_REP_TYPE, &repetition);
682                 emcore_set_repetition_alarm(repetition);
683
684                 ENTER_CRITICAL_SECTION(sound_mutex);
685                 SLEEP_CONDITION_VARIABLE(sound_condition , sound_mutex);
686
687                 switch (emcore_get_alert_type())
688                 {
689                         case EMAIL_ALERT_TYPE_MELODY:
690                                 if (!emcore_sound_mp_player_create()) {
691                                         EM_DEBUG_LOG("emcore_sound_mp_player_create failed : [%d]", email_mmhandle);
692                                         break;
693                                 }
694                                 emcore_sound_mp_player_start(filename);
695                                 break;
696                         case EMAIL_ALERT_TYPE_VIB:
697                                 emcore_vibration_start();
698                                 break;
699                         case EMAIL_ALERT_TYPE_MELODY_AND_VIB:
700                                 emcore_vibration_start();
701                                 if (!emcore_sound_mp_player_create()) {
702                                         EM_DEBUG_LOG("emcore_sound_mp_player_create failed : [%d]", email_mmhandle);
703                                         break;
704                                 }
705                                 emcore_sound_mp_player_start(filename);
706                                 break;
707                         case EMAIL_ALERT_TYPE_MUTE:
708                                 EM_DEBUG_LOG("Alert type is mute!!");
709                                 break;
710                         default: 
711                                 EM_DEBUG_EXCEPTION("alert type is strange");
712                                 emcore_alert_destory();
713                                 break;
714                 }
715                 LEAVE_CRITICAL_SECTION(sound_mutex);
716                 EM_DEBUG_LOG("Start FINISH");
717         }
718
719         EM_DEBUG_FUNC_END();
720         return 0;
721 }       
722
723 INTERNAL_FUNC void emcore_start_alert()
724 {
725         EM_DEBUG_FUNC_BEGIN("setting_noti_status : [%d]", setting_noti_status);
726
727 #ifdef __FEATURE_BLOCKING_MODE__
728         if (emcore_get_blocking_mode_status())
729                 return;
730 #endif /* __FEATURE_BLOCKING_MODE__ */
731
732         if (setting_noti_status == SETTING_NOTI_STATUS_OFF)
733                 return;
734
735         ENTER_CRITICAL_SECTION(sound_mutex);
736         WAKE_CONDITION_VARIABLE(sound_condition);
737         LEAVE_CRITICAL_SECTION(sound_mutex);
738 }