2.0_alpha release commit
[framework/messaging/email-service.git] / email-core / email-core-sound.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2000 - 2011 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-utilities.h"
32
33 #define TIMER 30000   // 30 seconds
34 #define HAPTIC_TEST_ITERATION 1
35
36 static MMHandleType email_mmhandle = 0;
37 static alarm_id_t email_alarm_id = 0;
38 static int email_vibe_handle = 0;
39
40 static char *filename;
41 alarm_entry_t *alarm_info = NULL;
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_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);
54
55 int emcore_alert_sound_init()
56 {
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");
60
61         return ret;
62 }
63
64 int emcore_alert_alarm_init()
65 {
66         int ret = ALARMMGR_RESULT_SUCCESS;
67         
68         ret = alarmmgr_init("email-service-0");
69         if (ret != 0) 
70                 EM_DEBUG_EXCEPTION("alarmmgr_init failed : [%d]", ret);
71
72         return ret;
73 }
74
75 int emcore_alert_sound_filepath_init()
76 {
77         filename = (char  *)em_malloc(MAX_PATH);
78         if (filename == NULL) {
79                 EM_DEBUG_EXCEPTION("Memory malloc error");      
80                 return false;
81         }
82
83         memset(filename, 0, MAX_PATH);
84
85         return true;
86 }
87 int emcore_alert_vibe_init()
88 {
89         email_vibe_handle = device_haptic_open(DEV_IDX_0, 0);   
90         if (!email_vibe_handle) {
91                 EM_DEBUG_EXCEPTION("device_haptic_open failed");
92                 return false;
93         }
94
95         return true;
96 }
97
98 void emcore_noti_key_changed_cb(keynode_t *key_node, void *data)
99 {
100         int ret = 0;
101         
102         switch (vconf_keynode_get_type(key_node)) {
103         case VCONF_TYPE_INT:
104                 ret = alarmmgr_remove_alarm(email_alarm_id);
105                 if (ret != ALARMMGR_RESULT_SUCCESS) {
106                         EM_DEBUG_EXCEPTION("delete of alarm id failed");
107                 }
108                 emcore_set_repetition_alarm(vconf_keynode_get_int(key_node));
109                 break;
110         case VCONF_TYPE_STRING:
111                 filename = vconf_keynode_get_str(key_node);
112                 break;
113         default:
114                 EM_DEBUG_EXCEPTION("Invalid key type");
115                 break;
116         }
117         return;
118 }
119
120 bool emcore_noti_init(void *data)
121 {
122         struct appdata *ap = data;
123         
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");
126                 return false;
127         }
128
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");
131                 return false;
132         }
133
134         return true;
135 }
136
137 int emcore_alert_init()
138 {
139         EM_DEBUG_FUNC_BEGIN();
140
141         int err = 0;
142         
143         if (!emcore_alert_sound_filepath_init()) {
144                 EM_DEBUG_EXCEPTION("emcore_alert_sound_filepath_init failed");
145                 return false;
146         }
147
148         if ((err = emcore_alert_sound_init()) != MM_ERROR_NONE) {
149                 EM_DEBUG_EXCEPTION("emcore_alert_sound_init failed : [%d]", err);
150                 return false;
151         }
152
153         if ((err = emcore_alert_alarm_init()) != ALARMMGR_RESULT_SUCCESS) {
154                 EM_DEBUG_EXCEPTION("emcore_alert_alarm_init failed : [%d]", err);
155                 return false;           
156         }
157
158         if (!emcore_alert_vibe_init()) {
159                 EM_DEBUG_EXCEPTION("emcore_alert_vibe_init failed");
160                 return false;           
161         }
162
163         if (!emcore_noti_init(NULL)) {
164                 EM_DEBUG_EXCEPTION("emcore_noti_init failed");
165                 return false;           
166         }
167
168         EM_DEBUG_FUNC_END();    
169         return true;
170 }
171
172 bool emcore_set_mp_filepath(const char *key)
173 {
174         filename = vconf_get_str(key);
175         if (filename == NULL)
176                 return false;
177
178         return true;
179 }
180
181 int emcore_mp_player_state_cb(int message, void *param, void *user_param)
182 {
183         switch (message)
184         {
185                 case MM_MESSAGE_ERROR:
186                         EM_DEBUG_LOG("Error is happened.");
187                         if (email_mmhandle) {
188                                 emcore_sound_mp_player_destory();
189                         }
190                         break;
191                 case MM_MESSAGE_BEGIN_OF_STREAM:
192                         EM_DEBUG_LOG("Play is started.");
193                         break;
194                 case MM_MESSAGE_END_OF_STREAM:
195                         EM_DEBUG_LOG("End of stream.");
196                         ENTER_CRITICAL_SECTION(mmhandle_mutex);
197                         if (email_mmhandle)
198                         {                       
199                                 emcore_sound_mp_player_stop();
200                                 emcore_sound_mp_player_destory();
201                         }
202                         LEAVE_CRITICAL_SECTION(mmhandle_mutex);
203                         break;
204                 default: 
205                         EM_DEBUG_LOG("Message = %d", message);
206                         break;
207         }
208         return 1;
209 }
210
211 bool emcore_sound_mp_player_create() 
212 {       
213         EM_DEBUG_FUNC_BEGIN();
214         int err = 0;
215         
216         if (email_mmhandle) {
217                 EM_DEBUG_LOG("already create the handle");
218                 return false;
219         }
220
221         if ((err = mm_player_create(&email_mmhandle)) != MM_ERROR_NONE) {
222                 EM_DEBUG_EXCEPTION("mm_player create fail [%d]", err);
223                 return false;
224         }       
225         EM_DEBUG_FUNC_END();
226         return true;
227 }
228
229 bool emcore_vibration_create() 
230 {       
231         EM_DEBUG_FUNC_BEGIN();
232
233         email_vibe_handle = device_haptic_open(DEV_IDX_0, 0);
234
235         if (email_vibe_handle < 0) {
236                 EM_DEBUG_EXCEPTION("vibration create failed");
237                 return false;
238         }
239         EM_DEBUG_FUNC_END();
240         return true;
241 }
242
243 bool emcore_alarm_create() 
244 {       
245         EM_DEBUG_FUNC_BEGIN();
246
247         alarm_info = alarmmgr_create_alarm();
248
249         if (alarm_info == NULL) {
250                 EM_DEBUG_EXCEPTION("alarm create failed");
251                 return false;
252         }               
253
254         EM_DEBUG_FUNC_END();
255         return true;
256 }
257
258 bool emcore_alarm_destory()
259 {
260         EM_DEBUG_FUNC_BEGIN();
261
262         int ret;
263         ret = alarmmgr_free_alarm(alarm_info);
264
265         if (ret != ALARMMGR_RESULT_SUCCESS) {
266                 EM_DEBUG_EXCEPTION("alarm free failed");
267                 return false;
268         }               
269
270         EM_DEBUG_FUNC_END();
271         return true;
272 }
273
274 bool emcore_alert_create()
275 {
276         EM_DEBUG_FUNC_BEGIN();
277
278         /* Create the alarm handle */
279         if (!emcore_alarm_create()) {
280                 EM_DEBUG_EXCEPTION("emcore_alarm_create failed.");
281                 return false;
282         }
283         
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.");
288                 return false;
289         }
290         
291         EM_DEBUG_FUNC_END();
292         return true;
293 }
294
295 bool emcore_alert_destory()
296 {
297         EM_DEBUG_FUNC_BEGIN();
298         int ret = 0;
299
300         /* Destroy the music player handle */
301         if (!emcore_sound_mp_player_destory()) {
302                 EM_DEBUG_EXCEPTION("emcore_sound_mp_player_destory fail");
303                 return false;
304         }                       
305
306         /* Destroy the vibration handle */
307         if (!emcore_vibration_destory()) {
308                 EM_DEBUG_EXCEPTION("emcore_vibration_destory fail");
309                 return false;
310         }       
311
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");
316                 return false;
317         }                       
318         
319         /* Set the music file in alert */
320         EM_SAFE_FREE(filename);
321         
322         EM_DEBUG_FUNC_END();
323         return true;
324 }
325
326 gboolean mp_player_timeout_cb(void *data)
327 {
328         EM_DEBUG_FUNC_BEGIN();
329
330         ENTER_CRITICAL_SECTION(mmhandle_mutex); 
331         if (email_mmhandle)
332         {                       
333                 emcore_sound_mp_player_stop();
334                 emcore_sound_mp_player_destory();
335         }
336         LEAVE_CRITICAL_SECTION(mmhandle_mutex);
337         
338         EM_DEBUG_FUNC_END();
339         return false;
340 }
341
342 gboolean vibration_timeout_cb(void *data)
343 {
344         EM_DEBUG_FUNC_BEGIN();
345
346         emcore_vibration_stop();
347         emcore_vibration_destory();
348         
349         EM_DEBUG_FUNC_END();
350         return false;
351 }
352
353 bool emcore_vibration_start(int haptic_level)
354 {
355         EM_DEBUG_FUNC_BEGIN();
356         int ret = 0;
357         int vibPattern = EFFCTVIBE_NOTIFICATION;
358
359         if (haptic_level == 0) {
360                 EM_DEBUG_LOG("The level of haptic is zero");
361                 return true;
362         }
363
364         ret = device_haptic_play_pattern(email_vibe_handle, vibPattern, HAPTIC_TEST_ITERATION, haptic_level);
365
366         if (ret != 0) {
367                 EM_DEBUG_EXCEPTION("Fail to play haptic  :  [%d]", ret);
368                 return false;
369         }
370
371         if ((ret = g_timeout_add(TIMER, (GSourceFunc) vibration_timeout_cb, NULL) <= 0))
372         {
373                 EM_DEBUG_EXCEPTION("play_alert - Failed to start timer [%d]", ret);
374                 return false;           
375         }       
376         
377         EM_DEBUG_FUNC_END();
378         return true;
379 }
380
381 int emcore_vibration_stop()
382 {
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);
386
387         return err;
388 }
389
390 int emcore_vibration_destory()
391 {
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);
395
396         return err;
397 }
398 int emcore_sound_mp_player_start(char *filepath)
399 {
400         EM_DEBUG_FUNC_BEGIN();
401
402         int err = MM_ERROR_NONE;
403
404 /*      
405         int volume = -1;
406
407
408         if ((err = vconf_get_int(VCONFKEY_SETAPPL_NOTI_SOUND_VOLUME_INT, &volume)) == -1)
409         {
410                 EM_DEBUG_LOG("vconf_get_int failed \n");
411                 return err;
412         }
413 */
414         mm_player_set_message_callback(email_mmhandle, emcore_mp_player_state_cb, (void  *)email_mmhandle);
415
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)
418         {
419                 EM_DEBUG_EXCEPTION("mm_player_set_attribute faile [ %d ] ", err);
420                 return err;
421         }
422
423         EM_DEBUG_LOG("After mm_player_set_attribute");  
424
425         if ((err = mm_player_realize(email_mmhandle)) != MM_ERROR_NONE)
426         {
427                 EM_DEBUG_EXCEPTION("mm_player_realize fail [%d]", err);
428                 return err;
429         }
430
431         if ((err = mm_player_start(email_mmhandle)) != MM_ERROR_NONE)
432         {
433                 EM_DEBUG_EXCEPTION("mm_player_start fail [%d]", err);
434                 return err;
435         }
436
437         if ((err = g_timeout_add(TIMER, (GSourceFunc)mp_player_timeout_cb, NULL) <= 0))
438         {
439                 EM_DEBUG_EXCEPTION("g_timeout_add - Failed to start timer");
440                 return err;             
441         }
442         
443         EM_DEBUG_FUNC_END();
444         return err;
445 }
446
447 int emcore_sound_mp_player_stop()
448 {
449         EM_DEBUG_FUNC_BEGIN();
450
451         int err = MM_ERROR_NONE;
452
453         if ((err = mm_player_stop(email_mmhandle)) != MM_ERROR_NONE)
454         {
455                 EM_DEBUG_EXCEPTION("mm_player_stop fail [%d]", err);
456                 return err;
457         }
458
459         if ((err = mm_player_unrealize(email_mmhandle)) != MM_ERROR_NONE)
460         {
461                 EM_DEBUG_EXCEPTION("mm_player_unrealize [%d]", err);
462                 return err;
463         }
464
465         EM_DEBUG_FUNC_END();
466         return err;
467 }
468
469 bool emcore_sound_mp_player_destory()
470 {
471         EM_DEBUG_FUNC_BEGIN();
472
473         int err = MM_ERROR_NONE;
474
475         if ((err = mm_player_destroy(email_mmhandle)) != MM_ERROR_NONE) {
476                 EM_DEBUG_EXCEPTION("mm_player_destory [%d]", err);
477                 return false;
478         }
479
480         email_mmhandle = 0;
481
482         EM_DEBUG_FUNC_END();
483         return true;
484 }       
485
486 int get_vconf_data(int key, int *return_value)
487 {
488         EM_DEBUG_FUNC_BEGIN();
489         int err = -1, value = 0;
490         
491         switch (key)
492         {
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);
496                         break;
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);
500                         break;
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);
504                         break;
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);
508                         break;
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);
512                         break;
513                 default: 
514                 {
515                         EM_DEBUG_LOG("Uuknown request\n");
516                         return false;                   
517                 }
518         }
519         
520         if (err == -1)
521         {
522                 EM_DEBUG_LOG("Vconf_get_int failed\n");
523                 return false;
524         }
525
526         *return_value = value;
527         EM_DEBUG_FUNC_END();
528         return true;
529 }
530
531 int emcore_get_alert_type()
532 {
533         EM_DEBUG_FUNC_BEGIN();
534         int sound_status = 0, vibe_status = 0;
535         int err;
536         int alert_type = -1;
537
538         if (!(err = get_vconf_data(EMAIL_SOUND_STATUS, &sound_status)))
539         {
540                 EM_DEBUG_EXCEPTION("Don't get sound status");
541                 return err;
542         }
543
544         if (!(err = get_vconf_data(EMAIL_VIBE_STATUS, &vibe_status)))
545         {
546                 EM_DEBUG_EXCEPTION("Don't get vibration status");
547                 return err;
548         }
549
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;
556         else
557                 alert_type = EMAIL_ALERT_TYPE_MUTE;
558
559         return alert_type;
560 }
561
562
563 INTERNAL_FUNC int emcore_start_thread_for_alerting_new_mails(int *err_code)
564 {
565         EM_DEBUG_FUNC_BEGIN();
566
567         int thread_error;
568
569         if (err_code != NULL)
570                 *err_code = EMAIL_ERROR_NONE;
571         
572         if (g_alert_thread)
573         {
574                 EM_DEBUG_EXCEPTION("Alert service is already running...");
575                 if (err_code != NULL)
576                         *err_code = EMAIL_ERROR_UNKNOWN;
577                 
578                 return 1;
579         }
580         
581         THREAD_CREATE(g_alert_thread, start_alert_thread, NULL, thread_error);
582         if (thread_error != 0)
583         {
584                 EM_DEBUG_EXCEPTION("Cannot create alert thread");
585                 if (err_code != NULL)
586                         *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
587
588                 return -1;
589         }
590                 
591         if (err_code != NULL)
592                 *err_code = EMAIL_ERROR_NONE;
593
594         return 0;
595 }
596
597 int emcore_alarm_timeout_cb(int timer_id, void *user_parm)
598 {
599         EM_DEBUG_FUNC_BEGIN();
600
601         int err = EMAIL_ERROR_NONE;
602         int total_unread_count = 0;
603         int total_mail_count = 0;
604         email_mailbox_t mailbox;
605
606         memset(&mailbox, 0x00, sizeof(email_mailbox_t));
607
608         mailbox.account_id = ALL_ACCOUNT;
609         mailbox.mailbox_name = NULL;
610
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);
613                 return false;
614         }
615
616         EM_DEBUG_LOG(">>>> total_unread_count : [%d]\n", total_unread_count);
617         
618         if (total_unread_count) {
619                 emcore_start_alert();
620         }
621
622         EM_DEBUG_FUNC_END();    
623         return true;
624 }
625
626 bool set_alarm(int repetition_time)
627 {
628         EM_DEBUG_FUNC_BEGIN();
629
630         int ret = 0;
631         alarm_date_t alarm_date;
632         time_t current_time;
633         struct tm current_tm;
634
635         time(&current_time);
636         localtime_r(&current_time, &current_tm);
637         
638         alarm_date.year = 0;
639         alarm_date.month = 0;
640         alarm_date.day = 0;
641
642         EM_DEBUG_LOG("Current time : [%d]-[%d]-[%d]", current_tm.tm_hour, current_tm.tm_min, current_tm.tm_sec);
643         
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;
647         } else {
648                 if (current_tm.tm_hour < 12) {
649                         alarm_date.hour = current_tm.tm_hour + 1;
650                 } else {
651                         alarm_date.hour = (current_tm.tm_hour + 1) % 12;
652                 }
653
654                 alarm_date.min = (current_tm.tm_min + repetition_time) % 60;
655         }
656
657         alarm_date.sec = current_tm.tm_sec;
658         
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);
663
664         ret = alarmmgr_set_cb(emcore_alarm_timeout_cb, NULL);
665
666         if (ret != 0) {
667                 EM_DEBUG_EXCEPTION("Failed : alarmmgr_set_cb() -> error[%d]", ret);
668                 return false;
669         }
670
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);
672         EM_DEBUG_FUNC_END();
673         return true;
674 }
675
676 void emcore_set_repetition_alarm(int repetition)
677 {
678         EM_DEBUG_FUNC_BEGIN();
679
680         int repetition_time = 0;
681         
682         switch (repetition) {
683         case EMAIL_GCONF_VALUE_REPEAT_NONE:
684                 repetition_time = 0;
685                 break;
686         case EMAIL_GCONF_VALUE_REPEAT_2MINS:
687                 repetition_time = 2;
688                 break;
689         case EMAIL_GCONF_VALUE_REPEAT_5MINS:
690                 repetition_time = 5;
691                 break;
692         case EMAIL_GCONF_VALUE_REPEAT_10MINS:           
693                 repetition_time = 10;
694                 break;
695         default:
696                 EM_DEBUG_EXCEPTION("Invalid repetition time");
697                 return;
698         }
699
700         EM_DEBUG_LOG("repetition time is %d", repetition_time);
701
702         if (repetition_time > 0) {
703                 set_alarm(repetition_time);
704         } 
705
706         EM_DEBUG_FUNC_END();
707 }
708
709 void *start_alert_thread(void *arg)
710 {
711         EM_DEBUG_FUNC_END();
712         
713         int err = 0;
714         int level = 0;
715
716         if (!emcore_alert_init())
717         {
718                 EM_DEBUG_EXCEPTION("Error : emcore_alert_init failed");
719                 return 0;
720         }
721
722
723         while (1) {
724                 if (!emcore_alert_create()) {
725                         EM_DEBUG_EXCEPTION("Error : emcore_alert_create failed");
726                         return 0;
727                 }
728
729                 err = get_vconf_data(EMAIL_ALERT_REP_TYPE, &level);
730                 emcore_set_repetition_alarm(level);
731
732                 ENTER_CRITICAL_SECTION(sound_mutex);
733                 SLEEP_CONDITION_VARIABLE(sound_condition , sound_mutex);
734
735                 err = get_vconf_data(EMAIL_ALERT_VIBE_STENGTH, &level);
736
737                 switch (emcore_get_alert_type())
738                 {
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);
742                                         break;
743                                 }
744                                 emcore_sound_mp_player_start(filename);
745                                 break;
746                         case EMAIL_ALERT_TYPE_VIB:
747                                 emcore_vibration_create();
748                                 emcore_vibration_start(level);
749                                 break;
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);
755                                         break;
756                                 }
757                                 emcore_sound_mp_player_start(filename);
758                                 break;
759                         case EMAIL_ALERT_TYPE_MUTE:
760                                 EM_DEBUG_LOG("Alert type is mute!!");
761                                 break;
762                         default: 
763                                 EM_DEBUG_EXCEPTION("alert type is strange");
764                                 emcore_alert_destory();
765                                 break;
766                 }
767                 LEAVE_CRITICAL_SECTION(sound_mutex);
768                 EM_DEBUG_LOG("Start FINISH");
769                 emcore_alarm_destory();
770         }
771         return 0;
772 }       
773
774 INTERNAL_FUNC void emcore_start_alert()
775 {
776         ENTER_CRITICAL_SECTION(sound_mutex);
777         WAKE_CONDITION_VARIABLE(sound_condition);
778         LEAVE_CRITICAL_SECTION(sound_mutex);
779 }