fix battery percentage display error
[apps/core/preloaded/indicator-win.git] / modules / clock / clock.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *  http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <vconf.h>
20 #include <runtime_info.h>
21 #include <Ecore_X.h>
22 #include <unicode/udat.h>
23 #include <unicode/udatpg.h>
24 #include <unicode/ustring.h>
25
26 #include "common.h"
27 #include "indicator.h"
28 #include "indicator_ui.h"
29 #include "indicator_gui.h"
30 #include "indicator_icon_util.h"
31 #include "indicator_util.h"
32 #include "modules.h"
33
34 #define SYSTEM_RESUME                           "system_wakeup"
35
36 #define TIME_FONT_SIZE_24       34
37 #define TIME_FONT_SIZE_12       30
38 #define TIME_FONT_SIZE_BATTERY  32
39 #define TIME_FONT_COLOR         243, 243, 243, 255
40
41 #define AMPM_FONT_SIZE          24
42 #define AMPM_FONT_COLOR         243, 243, 243, 255
43 #define LABEL_STRING            "<font_size=%d>%s" \
44                                 "</font_size></font>"
45
46 #define BATTERY_TIMER_INTERVAL          3
47 #define BATTERY_TIMER_INTERVAL_CHARGING 30
48
49 #define CLOCK_STR_LEN 256
50
51 enum {
52         INDICATOR_CLOCK_MODE_12H = 0,
53         INDICATOR_CLOCK_MODE_24H,
54         INDICATOR_CLOCK_MODE_MAX
55 };
56
57 static int notifd;
58 static int clock_mode = INDICATOR_CLOCK_MODE_12H;
59 static int apm_length = 0;
60 static int apm_position = 0;
61 static Ecore_Timer *timer = NULL;
62 static Ecore_Timer *battery_timer = NULL;
63 static Ecore_Timer *battery_charging_timer = NULL;
64 static int battery_charging = 0;
65
66 static int register_clock_module(void *data);
67 static int unregister_clock_module(void);
68 static int language_changed_cb(void *data);
69 static int region_changed_cb(void *data);
70 static int wake_up_cb(void *data);
71
72 #define ICON_PRIORITY   INDICATOR_PRIORITY_FIXED6
73 #define MODULE_NAME             "clock"
74
75 static void indicator_get_time_by_region(char* output, void* data);
76 static void ICU_set_timezone(const char *timezone);
77 static void indicator_clock_display_battery_percentage(void *data,int win_type );
78
79 Indicator_Icon_Object sysclock[INDICATOR_WIN_MAX] = {
80 {
81         .win_type = INDICATOR_WIN_PORT,
82         .type = INDICATOR_TXT_ICON,
83         .name = MODULE_NAME,
84         .priority = ICON_PRIORITY,
85         .always_top = EINA_FALSE,
86         .txt_obj = {0,},
87         .img_obj = {0,},
88         .obj_exist = EINA_FALSE,
89         .exist_in_view = EINA_FALSE,
90         .init = register_clock_module,
91         .fini = unregister_clock_module,
92         .lang_changed = NULL,
93         .region_changed = region_changed_cb,
94         .lang_changed = language_changed_cb,
95         .wake_up = wake_up_cb
96 },
97 {
98         .win_type = INDICATOR_WIN_LAND,
99         .type = INDICATOR_TXT_ICON,
100         .name = MODULE_NAME,
101         .priority = ICON_PRIORITY,
102         .always_top = EINA_FALSE,
103         .txt_obj = {0,},
104         .img_obj = {0,},
105         .obj_exist = EINA_FALSE,
106         .exist_in_view = EINA_FALSE,
107         .init = register_clock_module,
108         .fini = unregister_clock_module,
109         .lang_changed = NULL,
110         .region_changed = region_changed_cb,
111         .lang_changed = language_changed_cb,
112         .wake_up = wake_up_cb
113 }
114 };
115
116 static void set_app_state(void* data)
117 {
118         int i = 0;
119
120         for (i=0 ; i<INDICATOR_WIN_MAX ; i++)
121         {
122                 sysclock[i].ad = data;
123         }
124 }
125
126 static void indicator_clock_changed_cb(void *data)
127 {
128         char time_str[32];
129         char time_buf[128], ampm_buf[128];
130         char buf[CLOCK_STR_LEN];
131         char icu_apm[CLOCK_STR_LEN] = {0,};
132
133         struct tm *ts = NULL;
134         time_t ctime;
135         int len;
136         int font_size;
137
138         retif(data == NULL, , "Invalid parameter!");
139
140         if(indicator_util_get_update_flag()==0)
141         {
142                 DBG("need to update");
143                 return;
144         }
145
146         if (battery_timer != NULL || battery_charging_timer != NULL)
147         {
148                 DBG("battery is displaying. ignore clock callback");
149                 return;
150         }
151
152         ctime = time(NULL);
153         ts = localtime(&ctime);
154         if (ts == NULL)
155                 return;
156
157         if (timer != NULL) {
158                 ecore_timer_del(timer);
159                 timer = NULL;
160         }
161
162         memset(time_str, 0x00, sizeof(time_str));
163         memset(time_buf, 0x00, sizeof(time_buf));
164         memset(ampm_buf, 0x00, sizeof(ampm_buf));
165         memset(buf, 0x00, sizeof(buf));
166
167         timer =
168             ecore_timer_add(60 - ts->tm_sec, (void *)indicator_clock_changed_cb,
169                             data);
170
171         indicator_get_time_by_region(icu_apm,data);
172
173         if (clock_mode == INDICATOR_CLOCK_MODE_12H) {
174                 char bf1[32] = { 0, };
175                 int hour;
176
177                 if(apm_length>0 && apm_length<=4)
178                 {
179                         snprintf(ampm_buf, sizeof(ampm_buf),LABEL_STRING, AMPM_FONT_SIZE,icu_apm);
180                 }
181                 else
182                 {
183                         if (ts->tm_hour >= 0 && ts->tm_hour < 12)
184                                 snprintf(ampm_buf, sizeof(ampm_buf),
185                                          LABEL_STRING, AMPM_FONT_SIZE,
186                                          "AM");
187                         else
188                                 snprintf(ampm_buf, sizeof(ampm_buf),
189                                          LABEL_STRING, AMPM_FONT_SIZE,
190                                          "PM");
191                 }
192
193                 strftime(bf1, sizeof(bf1), "%l", ts);
194                 hour = atoi(bf1);
195                 strftime(bf1, sizeof(bf1), ":%M", ts);
196
197                 snprintf(time_str, sizeof(time_str), "%d%s", hour, bf1);
198                 font_size = TIME_FONT_SIZE_12;
199                 indicator_signal_emit(data,"indicator.clock.ampm","indicator.prog");
200         }
201         else{
202                 font_size = TIME_FONT_SIZE_24;
203                 strftime(time_str, sizeof(time_str), "%H:%M", ts);
204                 indicator_signal_emit(data,"indicator.clock.default","indicator.prog");
205         }
206
207         snprintf(time_buf, sizeof(time_buf), LABEL_STRING, font_size, time_str);
208
209         if(apm_position == 0)
210                 len = snprintf(buf, sizeof(buf), "%s%s", ampm_buf, time_buf);
211         else
212                 len = snprintf(buf, sizeof(buf), "%s%s", time_buf, ampm_buf);
213
214         if (len < 0) {
215                 ERR("Unexpected ERROR!");
216                 return;
217         }
218
219         INFO("[CLOCK MODULE] Timer Status : %d Time: %s", timer, buf);
220
221         indicator_part_text_emit(data,"elm.text.clock", buf);
222
223         return;
224 }
225
226 static void indicator_clock_format_changed_cb(keynode_t *node, void *data)
227 {
228         retif(data == NULL, , "Invalid parameter!");
229
230         int r = -1;
231
232         bool is_24hour_enabled = false;
233
234         INFO("[Enter] indicator_clock_format_changed_cb");
235
236         r = runtime_info_get_value_bool(
237                         RUNTIME_INFO_KEY_24HOUR_CLOCK_FORMAT_ENABLED, &is_24hour_enabled);
238
239         if( r==RUNTIME_INFO_ERROR_NONE&&is_24hour_enabled==true)
240         {
241                 clock_mode = INDICATOR_CLOCK_MODE_24H;
242         }
243         else
244         {
245                 clock_mode = INDICATOR_CLOCK_MODE_12H;
246         }
247
248         char *timezone = vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_ID);
249         ICU_set_timezone(timezone);
250         indicator_clock_changed_cb(data);
251         free(timezone);
252 }
253
254 static void indicator_clock_pm_state_change_cb(keynode_t *node, void *data)
255 {
256         int status = 0;
257
258         retif(data == NULL, , "Invalid parameter!");
259
260         vconf_get_int(VCONFKEY_PM_STATE, &status);
261
262         switch(status)
263         {
264                 case VCONFKEY_PM_STATE_LCDOFF:
265                         if (timer != NULL) {
266                                 ecore_timer_del(timer);
267                                 timer = NULL;
268                         }
269
270                         if (battery_timer != NULL) {
271                                 ecore_timer_del(battery_timer);
272                                 battery_timer = NULL;
273                         }
274
275                         if (battery_charging_timer != NULL) {
276                                 ecore_timer_del(battery_charging_timer);
277                                 battery_charging_timer = NULL;
278                         }
279                         break;
280                 default:
281                         break;
282         }
283
284 }
285
286 static void indicator_clock_battery_display_cb(void *data)
287 {
288         INFO("indicator_clock_battery_display_cb");
289
290         if (battery_timer != NULL) {
291                 ecore_timer_del(battery_timer);
292                 battery_timer = NULL;
293         }
294
295         indicator_clock_changed_cb(data);
296 }
297
298 static void indicator_clock_battery_disp_changed_cb(keynode_t *node, void *data)
299 {
300         int status = 0;
301
302         vconf_get_int(VCONFKEY_BATTERY_DISP_STATE,&status);
303
304         DBG("indicator_clock_battery_disp_changed_cb(%d)",status);
305
306         if(battery_charging_timer!=NULL)
307         {
308                 ERR("30s timer alive");
309                 return;
310         }
311         if (battery_timer != NULL) {
312                 ecore_timer_del(battery_timer);
313                 battery_timer = NULL;
314         }
315
316         battery_timer =  ecore_timer_add(BATTERY_TIMER_INTERVAL, (void *)indicator_clock_battery_display_cb,data);
317
318         if(status==2)
319         {
320                 indicator_clock_display_battery_percentage(data,0);
321                 indicator_clock_display_battery_percentage(data,1);
322         }
323         else
324         {
325                 indicator_clock_display_battery_percentage(data,status);
326         }
327 }
328
329 static void indicator_clock_battery_charging_stop_cb(void *data)
330 {
331
332         INFO("indicator_clock_battery_charging_stop_cb");
333
334         if (battery_charging_timer != NULL) {
335                 ecore_timer_del(battery_charging_timer);
336                 battery_charging_timer = NULL;
337         }
338
339         indicator_clock_changed_cb(data);
340 }
341
342 static void indicator_clock_charging_now_cb(keynode_t *node, void *data)
343 {
344         int status = 0;
345         int lock_state = 0;
346
347         retif(data == NULL, , "Invalid parameter!");
348
349         vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &lock_state);
350
351         vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW, &status);
352
353         battery_charging = status;
354
355         DBG("indicator_clock_charging_now_cb(%d)",status);
356
357         if(lock_state==VCONFKEY_IDLE_LOCK)
358         {
359                 DBG("indicator_clock_charging_now_cb:lock_state(%d)",lock_state);
360                 return;
361         }
362
363         if(status==1)
364         {
365                 if (battery_charging_timer != NULL)
366                 {
367                         ecore_timer_del(battery_charging_timer);
368                         battery_charging_timer = NULL;
369                 }
370                 battery_charging_timer =  ecore_timer_add(BATTERY_TIMER_INTERVAL_CHARGING, (void *)indicator_clock_battery_charging_stop_cb,data);
371
372                 indicator_clock_display_battery_percentage(data,0);
373         }
374         else
375         {
376                 indicator_clock_battery_charging_stop_cb(data);
377         }
378
379 }
380
381 static void indicator_clock_battery_capacity_cb(keynode_t *node, void *data)
382 {
383         retif(data == NULL, , "Invalid parameter!");
384
385         if(battery_charging_timer!=NULL||battery_timer!=NULL)
386         {
387                 DBG("indicator_clock_battery_capacity_cb:battery_charging(%d)",battery_charging);
388                 indicator_clock_display_battery_percentage(data,0);
389         }
390 }
391
392
393 static void indicator_clock_usb_cb(keynode_t *node, void *data)
394 {
395         int status = 0;
396
397         retif(data == NULL, , "Invalid parameter!");
398
399         vconf_get_int(VCONFKEY_SYSMAN_USB_STATUS, &status);
400
401         DBG("indicator_clock_usb_cb(%d)",status);
402
403         if(status==VCONFKEY_SYSMAN_USB_DISCONNECTED)
404         {
405                 if (battery_charging_timer != NULL)
406                 {
407                         ecore_timer_del(battery_charging_timer);
408                         battery_charging_timer = NULL;
409                 }
410                 indicator_clock_changed_cb(data);
411         }
412 }
413
414 static void indicator_clock_lock_state_cb(keynode_t *node, void *data)
415 {
416         int status = 0;
417
418         retif(data == NULL, , "Invalid parameter!");
419
420         vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &status);
421
422         DBG("indicator_clock_lock_state_cb(%d)",status);
423
424         if(status==VCONFKEY_IDLE_UNLOCK && battery_charging==1)
425         {
426                 indicator_clock_display_battery_percentage(data,0);
427         }
428
429 }
430 static void indicator_clock_battery_precentage_setting_cb(keynode_t *node, void *data)
431 {
432         int ret = 0;
433         int status = 0;
434
435         retif(data == NULL, , "Invalid parameter!");
436
437         ret = vconf_get_bool(VCONFKEY_SETAPPL_BATTERY_PERCENTAGE_BOOL, &status);
438         if (ret != OK)
439         {
440                 ERR("Fail to get [%s: %d]",VCONFKEY_SETAPPL_BATTERY_PERCENTAGE_BOOL, ret);
441                 return;
442         }
443         if(status==0)
444         {
445                 if (battery_charging_timer != NULL) {
446                         ecore_timer_del(battery_charging_timer);
447                         battery_charging_timer = NULL;
448                 }
449                 if (battery_timer != NULL) {
450                         ecore_timer_del(battery_timer);
451                         battery_timer = NULL;
452                 }
453                 indicator_clock_changed_cb(data);
454         }
455 }
456
457 static void indicator_clock_display_battery_percentage(void *data,int win_type )
458 {
459         int ret = FAIL;
460         int status = 0;
461         int battery_capa = 0;
462         char buf[256] = {0,};
463         char temp[256] = {0,};
464         struct appdata *ad = (struct appdata *)data;
465
466         ret = vconf_get_bool(VCONFKEY_SETAPPL_BATTERY_PERCENTAGE_BOOL, &status);
467         if (ret != OK)
468                 ERR("Fail to get [%s: %d]",
469                         VCONFKEY_SETAPPL_BATTERY_PERCENTAGE_BOOL, ret);
470
471         if(status)
472         {
473                 ret = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CAPACITY, &battery_capa);
474                 if (ret != OK)
475                 {
476                         ERR("Fail to get [VCONFKEY_SYSMAN_BATTERY_CAPACITY:%d]", ret);
477                         return;
478                 }
479                 if (battery_capa < 0)
480                 {
481                         INFO("Invalid Battery Capacity: %d", battery_capa);
482                         return;
483                 }
484
485                 INFO("Battery Capacity: %d", battery_capa);
486
487                 if (battery_capa > 100)
488                         battery_capa = 100;
489
490                 snprintf(temp, sizeof(temp), "%d%%",battery_capa);
491
492                 snprintf(buf, sizeof(buf), LABEL_STRING, TIME_FONT_SIZE_BATTERY, temp);
493
494                 INFO("indicator_clock_display_battery_percentage %s", buf);
495
496                 indicator_part_text_emit_by_win(&(ad->win[win_type]),"elm.text.clock", buf);
497
498         }
499
500 }
501
502
503 static int language_changed_cb(void *data)
504 {
505         DBG("language_changed_cb");
506         indicator_clock_changed_cb(data);
507         return OK;
508 }
509
510 static int region_changed_cb(void *data)
511 {
512         DBG("region_changed_cb");
513         indicator_clock_format_changed_cb(NULL, data);
514         return OK;
515 }
516
517 static int wake_up_cb(void *data)
518 {
519         int status = 0;
520         int lock_type = 0;
521
522         INFO("CLOCK wake_up_cb");
523
524         retif(data == NULL, FAIL, "Invalid parameter!");
525
526         vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &lock_type);
527
528         vconf_get_bool(VCONFKEY_SETAPPL_BATTERY_PERCENTAGE_BOOL, &status);
529
530         DBG("wake_up_cb(%d,%d)",lock_type,status);
531
532         if(status == 1)
533         {
534                 if(lock_type == VCONFKEY_IDLE_UNLOCK && battery_charging==1)
535                 {
536                         indicator_clock_display_battery_percentage(data,0);
537                         return OK;
538                 }
539         }
540
541         indicator_clock_changed_cb(data);
542
543         return OK;
544 }
545
546 static int register_clock_module(void *data)
547 {
548         int r = 0, ret = -1;
549
550         retif(data == NULL, FAIL, "Invalid parameter!");
551
552         set_app_state(data);
553
554         ret = vconf_notify_key_changed(VCONFKEY_SYSTEM_TIME_CHANGED,
555                                        indicator_clock_format_changed_cb, data);
556         if (ret != OK) {
557                 ERR("Fail: register VCONFKEY_SYSTEM_TIME_CHANGED");
558                 r = r | ret;
559         }
560
561         ret = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT_TIME1224,
562                                        indicator_clock_format_changed_cb, data);
563         if (ret != OK) {
564                 ERR("Fail: register VCONFKEY_REGIONFORMAT_TIME1224");
565                 r = r | ret;
566         }
567
568         ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_TIMEZONE_INT,
569                                        indicator_clock_format_changed_cb, data);
570         if (ret != OK) {
571                 ERR("Fail: register VCONFKEY_SETAPPL_TIMEZONE_INT");
572                 r = r | ret;
573         }
574
575         ret = vconf_notify_key_changed(VCONFKEY_PM_STATE, indicator_clock_pm_state_change_cb, (void *)data);
576
577         if (ret != OK) {
578                 ERR("Fail: register VCONFKEY_PM_STATE");
579                 r = r | ret;
580         }
581
582         ret = vconf_notify_key_changed(VCONFKEY_BATTERY_DISP_STATE,
583                                        indicator_clock_battery_disp_changed_cb, data);
584         if (ret != OK) {
585                 ERR("Fail: register VCONFKEY_SETAPPL_TIMEZONE_INT");
586                 r = r | ret;
587         }
588
589         ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_CAPACITY,
590                                        indicator_clock_battery_capacity_cb, data);
591         if (ret != OK) {
592                 ERR("Failed to register callback!");
593                 r = r | ret;
594         }
595
596         ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW,
597                                        indicator_clock_charging_now_cb, data);
598         if (ret != OK) {
599                 ERR("Failed to register callback!");
600                 r = r | ret;
601         }
602
603         ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_USB_STATUS,
604                                        indicator_clock_usb_cb, data);
605         if (ret != OK) {
606                 ERR("Failed to register callback!");
607                 r = r | ret;
608         }
609
610
611         ret = vconf_notify_key_changed(VCONFKEY_IDLE_LOCK_STATE,
612                                        indicator_clock_lock_state_cb, data);
613         if (ret != OK) {
614                 ERR("Failed to register callback!");
615                 r = r | ret;
616         }
617
618         ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_BATTERY_PERCENTAGE_BOOL,
619                                         indicator_clock_battery_precentage_setting_cb, data);
620         if (ret != OK) {
621                 ERR("Failed to register callback!");
622                 r = r | ret;
623         }
624
625         indicator_clock_format_changed_cb(NULL, data);
626
627         return r;
628 }
629
630 static int unregister_clock_module(void)
631 {
632         int ret;
633
634         ret = vconf_ignore_key_changed(VCONFKEY_SYSTEM_TIME_CHANGED,
635                                                indicator_clock_format_changed_cb);
636         if (ret != OK)
637                 ERR("Fail: unregister VCONFKEY_SYSTEM_TIME_CHANGED");
638
639         ret = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT_TIME1224,
640                                        indicator_clock_format_changed_cb);
641         if (ret != OK)
642                 ERR("Fail: unregister VCONFKEY_REGIONFORMAT_TIME1224");
643
644         ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_TIMEZONE_INT,
645                                        indicator_clock_format_changed_cb);
646         if (ret != OK)
647                 ERR("Fail: unregister VCONFKEY_SETAPPL_TIMEZONE_INT");
648
649         ret = vconf_ignore_key_changed(VCONFKEY_PM_STATE,
650                                                indicator_clock_pm_state_change_cb);
651         if (ret != OK)
652                 ERR("Fail: unregister VCONFKEY_PM_STATE");
653
654         ret = vconf_ignore_key_changed(VCONFKEY_BATTERY_DISP_STATE,
655                                               indicator_clock_battery_disp_changed_cb);
656         if (ret != OK)
657                 ERR("Fail: unregister VCONFKEY_BATTERY_DISP_STATE");
658
659         ret = vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_CAPACITY,
660                                                indicator_clock_battery_capacity_cb);
661         if (ret != OK)
662                 ERR("Fail: unregister VCONFKEY_SYSMAN_BATTERY_CAPACITY");
663
664         ret = vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW,
665                                                indicator_clock_charging_now_cb);
666         if (ret != OK)
667                 ERR("Fail: unregister VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW");
668
669         ret = vconf_ignore_key_changed(VCONFKEY_SYSMAN_USB_STATUS,
670                                                indicator_clock_usb_cb);
671         if (ret != OK)
672                 ERR("Fail: unregister VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW");
673
674
675         ret = vconf_ignore_key_changed(VCONFKEY_IDLE_LOCK_STATE,
676                                                indicator_clock_lock_state_cb);
677         if (ret != OK)
678                 ERR("Fail: unregister VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW");
679
680         ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_BATTERY_PERCENTAGE_BOOL,
681                                                indicator_clock_battery_precentage_setting_cb);
682         if (ret != OK)
683                 ERR("Fail: unregister VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW");
684
685         if (timer != NULL) {
686                 ecore_timer_del(timer);
687                 timer = NULL;
688         }
689
690         if (battery_timer != NULL) {
691                 ecore_timer_del(battery_timer);
692                 battery_timer = NULL;
693         }
694
695         if (battery_charging_timer != NULL) {
696                 ecore_timer_del(battery_charging_timer);
697                 battery_charging_timer = NULL;
698         }
699
700         return OK;
701 }
702
703 void indicator_get_time_by_region(char* output,void *data)
704 {
705         retif(data == NULL, , "Data parameter is NULL");
706         retif(output == NULL, , "output parameter is NULL");
707
708
709         UChar customSkeleton[CLOCK_STR_LEN] = { 0, };
710         UErrorCode status = U_ZERO_ERROR;
711         UDateFormat *formatter = NULL;
712
713         UChar bestPattern[CLOCK_STR_LEN] = { 0, };
714         UChar formatted[CLOCK_STR_LEN] = { 0, };
715
716         char bestPatternString[CLOCK_STR_LEN] = { 0, };
717         char formattedString[CLOCK_STR_LEN] = { 0, };
718
719         UDateTimePatternGenerator *pattern_generator = NULL;
720
721         char *time_skeleton = "hhmm";
722
723         char *locale = vconf_get_str(VCONFKEY_REGIONFORMAT);
724         if (locale == NULL) {
725                 DBG("[Error] get value of VCONFKEY_REGIONFORMAT fail.");
726         }
727
728         u_uastrncpy(customSkeleton, time_skeleton, strlen(time_skeleton));
729
730         pattern_generator = udatpg_open(locale, &status);
731
732         int32_t bestPatternCapacity = (int32_t) (sizeof(bestPattern) / sizeof((bestPattern)[0]));
733         (void)udatpg_getBestPattern(pattern_generator, customSkeleton,
734                                     u_strlen(customSkeleton), bestPattern,
735                                     bestPatternCapacity, &status);
736
737         u_austrcpy(bestPatternString, bestPattern);
738         u_uastrcpy(bestPattern,"a");
739
740         if(bestPatternString[0] == 'a')
741         {
742                 apm_position = 0;
743         }
744         else
745         {
746                 apm_position = 1;
747         }
748
749         UDate date = ucal_getNow();
750         formatter = udat_open(UDAT_IGNORE, UDAT_IGNORE, locale, NULL, -1, bestPattern, -1, &status);
751         int32_t formattedCapacity = (int32_t) (sizeof(formatted) / sizeof((formatted)[0]));
752         (void)udat_format(formatter, date, formatted, formattedCapacity, NULL, &status);
753         u_austrcpy(formattedString, formatted);
754
755         DBG("DATE & TIME is %s %s %d %s", locale, formattedString, u_strlen(formatted), bestPatternString);
756
757         apm_length = u_strlen(formatted);
758
759         udatpg_close(pattern_generator);
760
761         udat_close(formatter);
762
763         if(strlen(formattedString)<CLOCK_STR_LEN)
764         {
765                 strncpy(output,formattedString,strlen(formattedString));
766         }
767         else
768         {
769                 strncpy(output,formattedString,CLOCK_STR_LEN-1);
770         }
771
772         return;
773 }
774
775 static UChar *uastrcpy(const char *chars)
776 {
777         int len = 0;
778         UChar *str = NULL;
779         len = strlen(chars);
780         str = (UChar *) malloc(sizeof(UChar) *(len + 1));
781         if (!str)
782                 return NULL;
783         u_uastrcpy(str, chars);
784         return str;
785 }
786
787 static void ICU_set_timezone(const char *timezone)
788 {
789         if(timezone == NULL)
790         {
791                 ERR("TIMEZONE is NULL");
792                 return;
793         }
794
795         DBG("ICU_set_timezone = %s ", timezone);
796         UErrorCode ec = U_ZERO_ERROR;
797         UChar *str = uastrcpy(timezone);
798
799         ucal_setDefaultTimeZone(str, &ec);
800         if (U_SUCCESS(ec)) {
801                 DBG("ucal_setDefaultTimeZone() SUCCESS ");
802         } else {
803                 DBG("ucal_setDefaultTimeZone() FAILED : %s ",
804                               u_errorName(ec));
805         }
806         free(str);
807 }
808
809