2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.0 (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
8 * http://floralicense.org/license/
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.
20 #include <runtime_info.h>
22 #include <unicode/udat.h>
23 #include <unicode/udatpg.h>
24 #include <unicode/ustring.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"
34 #define SYSTEM_RESUME "system_wakeup"
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
41 #define AMPM_FONT_SIZE 24
42 #define AMPM_FONT_COLOR 243, 243, 243, 255
43 #define LABEL_STRING "<font_size=%d>%s" \
46 #define BATTERY_TIMER_INTERVAL 3
47 #define BATTERY_TIMER_INTERVAL_CHARGING 30
49 #define CLOCK_STR_LEN 256
52 INDICATOR_CLOCK_MODE_12H = 0,
53 INDICATOR_CLOCK_MODE_24H,
54 INDICATOR_CLOCK_MODE_MAX
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 static int battery_charging_first = 0;
67 static int register_clock_module(void *data);
68 static int unregister_clock_module(void);
69 static int language_changed_cb(void *data);
70 static int region_changed_cb(void *data);
71 static int wake_up_cb(void *data);
73 #define ICON_PRIORITY INDICATOR_PRIORITY_FIXED6
74 #define MODULE_NAME "clock"
76 static void indicator_get_time_by_region(char* output, void* data);
77 static void ICU_set_timezone(const char *timezone);
78 static void indicator_clock_display_battery_percentage(void *data,int win_type );
80 Indicator_Icon_Object sysclock[INDICATOR_WIN_MAX] = {
82 .win_type = INDICATOR_WIN_PORT,
83 .type = INDICATOR_TXT_ICON,
85 .priority = ICON_PRIORITY,
86 .always_top = EINA_FALSE,
89 .obj_exist = EINA_FALSE,
90 .exist_in_view = EINA_FALSE,
91 .init = register_clock_module,
92 .fini = unregister_clock_module,
94 .region_changed = region_changed_cb,
95 .lang_changed = language_changed_cb,
99 .win_type = INDICATOR_WIN_LAND,
100 .type = INDICATOR_TXT_ICON,
102 .priority = ICON_PRIORITY,
103 .always_top = EINA_FALSE,
106 .obj_exist = EINA_FALSE,
107 .exist_in_view = EINA_FALSE,
108 .init = register_clock_module,
109 .fini = unregister_clock_module,
110 .lang_changed = NULL,
111 .region_changed = region_changed_cb,
112 .lang_changed = language_changed_cb,
113 .wake_up = wake_up_cb
117 static void set_app_state(void* data)
121 for (i=0 ; i<INDICATOR_WIN_MAX ; i++)
123 sysclock[i].ad = data;
127 static void indicator_clock_changed_cb(void *data)
130 char time_buf[128], ampm_buf[128];
131 char buf[CLOCK_STR_LEN];
132 char icu_apm[CLOCK_STR_LEN] = {0,};
134 struct tm *ts = NULL;
139 retif(data == NULL, , "Invalid parameter!");
141 if(indicator_util_get_update_flag()==0)
143 DBG("need to update");
147 if (battery_timer != NULL || battery_charging_timer != NULL)
149 DBG("battery is displaying. ignore clock callback");
154 ts = localtime(&ctime);
159 ecore_timer_del(timer);
163 memset(time_str, 0x00, sizeof(time_str));
164 memset(time_buf, 0x00, sizeof(time_buf));
165 memset(ampm_buf, 0x00, sizeof(ampm_buf));
166 memset(buf, 0x00, sizeof(buf));
169 ecore_timer_add(60 - ts->tm_sec, (void *)indicator_clock_changed_cb,
172 indicator_get_time_by_region(icu_apm,data);
174 if (clock_mode == INDICATOR_CLOCK_MODE_12H) {
175 char bf1[32] = { 0, };
178 if(apm_length>0 && apm_length<=4)
180 snprintf(ampm_buf, sizeof(ampm_buf),LABEL_STRING, AMPM_FONT_SIZE,icu_apm);
184 if (ts->tm_hour >= 0 && ts->tm_hour < 12)
185 snprintf(ampm_buf, sizeof(ampm_buf),
186 LABEL_STRING, AMPM_FONT_SIZE,
189 snprintf(ampm_buf, sizeof(ampm_buf),
190 LABEL_STRING, AMPM_FONT_SIZE,
194 strftime(bf1, sizeof(bf1), "%l", ts);
196 strftime(bf1, sizeof(bf1), ":%M", ts);
198 snprintf(time_str, sizeof(time_str), "%d%s", hour, bf1);
199 font_size = TIME_FONT_SIZE_12;
200 indicator_signal_emit(data,"indicator.clock.ampm","indicator.prog");
203 font_size = TIME_FONT_SIZE_24;
204 strftime(time_str, sizeof(time_str), "%H:%M", ts);
205 indicator_signal_emit(data,"indicator.clock.default","indicator.prog");
208 snprintf(time_buf, sizeof(time_buf), LABEL_STRING, font_size, time_str);
210 if(apm_position == 0)
211 len = snprintf(buf, sizeof(buf), "%s%s", ampm_buf, time_buf);
213 len = snprintf(buf, sizeof(buf), "%s%s", time_buf, ampm_buf);
216 ERR("Unexpected ERROR!");
220 INFO("[CLOCK MODULE] Timer Status : %d Time: %s", timer, buf);
222 indicator_part_text_emit(data,"elm.text.clock", buf);
227 static void indicator_clock_format_changed_cb(keynode_t *node, void *data)
229 retif(data == NULL, , "Invalid parameter!");
233 bool is_24hour_enabled = false;
235 INFO("[Enter] indicator_clock_format_changed_cb");
237 r = runtime_info_get_value_bool(
238 RUNTIME_INFO_KEY_24HOUR_CLOCK_FORMAT_ENABLED, &is_24hour_enabled);
240 if( r==RUNTIME_INFO_ERROR_NONE&&is_24hour_enabled==true)
242 clock_mode = INDICATOR_CLOCK_MODE_24H;
246 clock_mode = INDICATOR_CLOCK_MODE_12H;
249 char *timezone = vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_ID);
250 ICU_set_timezone(timezone);
251 indicator_clock_changed_cb(data);
255 static void indicator_clock_pm_state_change_cb(keynode_t *node, void *data)
259 retif(data == NULL, , "Invalid parameter!");
261 vconf_get_int(VCONFKEY_PM_STATE, &status);
265 case VCONFKEY_PM_STATE_LCDOFF:
267 ecore_timer_del(timer);
271 if (battery_timer != NULL) {
272 ecore_timer_del(battery_timer);
273 battery_timer = NULL;
276 if (battery_charging_timer != NULL) {
277 ecore_timer_del(battery_charging_timer);
278 battery_charging_timer = NULL;
287 static void indicator_clock_battery_disp_changed_cb(keynode_t *node, void *data)
291 vconf_get_int(VCONFKEY_BATTERY_DISP_STATE,&status);
293 DBG("indicator_clock_battery_disp_changed_cb(%d)",status);
297 indicator_clock_display_battery_percentage(data,0);
298 indicator_clock_display_battery_percentage(data,1);
302 indicator_clock_display_battery_percentage(data,status);
306 static void indicator_clock_charging_now_cb(keynode_t *node, void *data)
311 retif(data == NULL, , "Invalid parameter!");
313 vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &lock_state);
315 vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW, &status);
317 battery_charging = status;
319 DBG("indicator_clock_charging_now_cb(%d)",status);
321 if(lock_state==VCONFKEY_IDLE_LOCK)
323 DBG("indicator_clock_charging_now_cb:lock_state(%d)",lock_state);
327 if(battery_charging_first == 1&&status==1)
329 DBG("indicator_clock_charging_now_cb : ignore(%d)",status);
334 battery_charging_first = 1;
335 indicator_clock_display_battery_percentage(data,0);
339 static void indicator_clock_battery_capacity_cb(keynode_t *node, void *data)
341 retif(data == NULL, , "Invalid parameter!");
343 if(battery_charging==1&&battery_charging_timer!=NULL)
345 DBG("indicator_clock_battery_capacity_cb:battery_charging(%d)",battery_charging);
346 indicator_clock_display_battery_percentage(data,0);
351 static void indicator_clock_usb_cb(keynode_t *node, void *data)
355 retif(data == NULL, , "Invalid parameter!");
357 vconf_get_int(VCONFKEY_SYSMAN_USB_STATUS, &status);
359 DBG("indicator_clock_usb_cb(%d)",status);
361 if(status==VCONFKEY_SYSMAN_USB_DISCONNECTED)
363 battery_charging_first = 0;
364 if (battery_charging_timer != NULL)
366 ecore_timer_del(battery_charging_timer);
367 battery_charging_timer = NULL;
369 indicator_clock_changed_cb(data);
373 static void indicator_clock_battery_display_cb(void *data)
375 INFO("indicator_clock_battery_charging_stop_cb");
377 if (battery_timer != NULL) {
378 ecore_timer_del(battery_timer);
379 battery_timer = NULL;
382 indicator_clock_changed_cb(data);
385 static void indicator_clock_battery_charging_stop_cb(void *data)
388 INFO("indicator_clock_battery_charging_stop_cb");
390 if (battery_charging_timer != NULL) {
391 ecore_timer_del(battery_charging_timer);
392 battery_charging_timer = NULL;
395 indicator_clock_changed_cb(data);
398 static void indicator_clock_lock_state_cb(keynode_t *node, void *data)
402 retif(data == NULL, , "Invalid parameter!");
404 vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &status);
406 DBG("indicator_clock_lock_state_cb(%d)",status);
408 if(status==VCONFKEY_IDLE_UNLOCK && battery_charging==1)
410 battery_charging_first = 1;
411 indicator_clock_display_battery_percentage(data,0);
415 static void indicator_clock_battery_precentage_setting_cb(keynode_t *node, void *data)
420 retif(data == NULL, , "Invalid parameter!");
422 ret = vconf_get_bool(VCONFKEY_SETAPPL_BATTERY_PERCENTAGE_BOOL, &status);
425 ERR("Fail to get [%s: %d]",VCONFKEY_SETAPPL_BATTERY_PERCENTAGE_BOOL, ret);
430 if (battery_charging_timer != NULL) {
431 ecore_timer_del(battery_charging_timer);
432 battery_charging_timer = NULL;
434 if (battery_timer != NULL) {
435 ecore_timer_del(battery_timer);
436 battery_timer = NULL;
438 indicator_clock_changed_cb(data);
442 static void indicator_clock_display_battery_percentage(void *data,int win_type )
446 int battery_capa = 0;
447 char buf[256] = {0,};
448 char temp[256] = {0,};
449 struct appdata *ad = (struct appdata *)data;
452 if(battery_charging_timer!=NULL)
454 INFO("30sec timer alive");
458 ret = vconf_get_bool(VCONFKEY_SETAPPL_BATTERY_PERCENTAGE_BOOL, &status);
460 ERR("Fail to get [%s: %d]",
461 VCONFKEY_SETAPPL_BATTERY_PERCENTAGE_BOOL, ret);
465 ret = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CAPACITY, &battery_capa);
468 ERR("Fail to get [VCONFKEY_SYSMAN_BATTERY_CAPACITY:%d]", ret);
471 if (battery_capa < 0)
473 INFO("Invalid Battery Capacity: %d", battery_capa);
477 INFO("Battery Capacity: %d", battery_capa);
479 if (battery_capa > 100)
482 snprintf(temp, sizeof(temp), "%d%%",battery_capa);
484 snprintf(buf, sizeof(buf), LABEL_STRING, TIME_FONT_SIZE_BATTERY, temp);
486 INFO("indicator_clock_display_battery_percentage %s", buf);
488 indicator_part_text_emit_by_win(&(ad->win[win_type]),"elm.text.clock", buf);
490 if(battery_charging == 1)
493 battery_charging_timer = ecore_timer_add(BATTERY_TIMER_INTERVAL_CHARGING, (void *)indicator_clock_battery_charging_stop_cb,data);
497 if (battery_timer != NULL) {
498 ecore_timer_del(battery_timer);
499 battery_timer = NULL;
502 battery_timer = ecore_timer_add(BATTERY_TIMER_INTERVAL, (void *)indicator_clock_battery_display_cb,data);
509 static int language_changed_cb(void *data)
511 DBG("language_changed_cb");
512 indicator_clock_changed_cb(data);
516 static int region_changed_cb(void *data)
518 DBG("region_changed_cb");
519 indicator_clock_format_changed_cb(NULL, data);
523 static int wake_up_cb(void *data)
527 INFO("CLOCK wake_up_cb");
529 retif(data == NULL, FAIL, "Invalid parameter!");
531 vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &status);
533 DBG("wake_up_cb(%d)",status);
535 if(status==VCONFKEY_IDLE_UNLOCK && battery_charging==1)
537 indicator_clock_display_battery_percentage(data,0);
541 indicator_clock_changed_cb(data);
546 static int register_clock_module(void *data)
550 retif(data == NULL, FAIL, "Invalid parameter!");
554 ret = vconf_notify_key_changed(VCONFKEY_SYSTEM_TIME_CHANGED,
555 indicator_clock_format_changed_cb, data);
557 ERR("Fail: register VCONFKEY_SYSTEM_TIME_CHANGED");
561 ret = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT_TIME1224,
562 indicator_clock_format_changed_cb, data);
564 ERR("Fail: register VCONFKEY_REGIONFORMAT_TIME1224");
568 ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_TIMEZONE_INT,
569 indicator_clock_format_changed_cb, data);
571 ERR("Fail: register VCONFKEY_SETAPPL_TIMEZONE_INT");
575 ret = vconf_notify_key_changed(VCONFKEY_PM_STATE, indicator_clock_pm_state_change_cb, (void *)data);
578 ERR("Fail: register VCONFKEY_PM_STATE");
582 ret = vconf_notify_key_changed(VCONFKEY_BATTERY_DISP_STATE,
583 indicator_clock_battery_disp_changed_cb, data);
585 ERR("Fail: register VCONFKEY_SETAPPL_TIMEZONE_INT");
589 ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_CAPACITY,
590 indicator_clock_battery_capacity_cb, data);
592 ERR("Failed to register callback!");
596 ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW,
597 indicator_clock_charging_now_cb, data);
599 ERR("Failed to register callback!");
603 ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_USB_STATUS,
604 indicator_clock_usb_cb, data);
606 ERR("Failed to register callback!");
611 ret = vconf_notify_key_changed(VCONFKEY_IDLE_LOCK_STATE,
612 indicator_clock_lock_state_cb, data);
614 ERR("Failed to register callback!");
618 ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_BATTERY_PERCENTAGE_BOOL,
619 indicator_clock_battery_precentage_setting_cb, data);
621 ERR("Failed to register callback!");
625 indicator_clock_format_changed_cb(NULL, data);
630 static int unregister_clock_module(void)
634 ret = vconf_ignore_key_changed(VCONFKEY_SYSTEM_TIME_CHANGED,
635 indicator_clock_format_changed_cb);
637 ERR("Fail: unregister VCONFKEY_SYSTEM_TIME_CHANGED");
639 ret = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT_TIME1224,
640 indicator_clock_format_changed_cb);
642 ERR("Fail: unregister VCONFKEY_REGIONFORMAT_TIME1224");
644 ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_TIMEZONE_INT,
645 indicator_clock_format_changed_cb);
647 ERR("Fail: unregister VCONFKEY_SETAPPL_TIMEZONE_INT");
649 ret = vconf_ignore_key_changed(VCONFKEY_PM_STATE,
650 indicator_clock_battery_disp_changed_cb);
652 ERR("Fail: unregister VCONFKEY_PM_STATE");
654 ret = vconf_ignore_key_changed(VCONFKEY_BATTERY_DISP_STATE,
655 indicator_clock_pm_state_change_cb);
657 ERR("Fail: unregister VCONFKEY_BATTERY_DISP_STATE");
659 ret = vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_CAPACITY,
660 indicator_clock_battery_capacity_cb);
662 ERR("Fail: unregister VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW");
664 ret = vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW,
665 indicator_clock_charging_now_cb);
667 ERR("Fail: unregister VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW");
669 ret = vconf_ignore_key_changed(VCONFKEY_SYSMAN_USB_STATUS,
670 indicator_clock_usb_cb);
672 ERR("Fail: unregister VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW");
675 ret = vconf_ignore_key_changed(VCONFKEY_IDLE_LOCK_STATE,
676 indicator_clock_lock_state_cb);
678 ERR("Fail: unregister VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW");
680 ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_BATTERY_PERCENTAGE_BOOL,
681 indicator_clock_battery_precentage_setting_cb);
683 ERR("Fail: unregister VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW");
686 ecore_timer_del(timer);
690 if (battery_timer != NULL) {
691 ecore_timer_del(battery_timer);
692 battery_timer = NULL;
695 if (battery_charging_timer != NULL) {
696 ecore_timer_del(battery_charging_timer);
697 battery_charging_timer = NULL;
703 void indicator_get_time_by_region(char* output,void *data)
705 retif(data == NULL, , "Data parameter is NULL");
706 retif(output == NULL, , "output parameter is NULL");
709 UChar customSkeleton[CLOCK_STR_LEN] = { 0, };
710 UErrorCode status = U_ZERO_ERROR;
711 UDateFormat *formatter = NULL;
713 UChar bestPattern[CLOCK_STR_LEN] = { 0, };
714 UChar formatted[CLOCK_STR_LEN] = { 0, };
716 char bestPatternString[CLOCK_STR_LEN] = { 0, };
717 char formattedString[CLOCK_STR_LEN] = { 0, };
719 UDateTimePatternGenerator *pattern_generator = NULL;
721 char *time_skeleton = "hhmm";
723 char *locale = vconf_get_str(VCONFKEY_REGIONFORMAT);
724 if (locale == NULL) {
725 DBG("[Error] get value of VCONFKEY_REGIONFORMAT fail.");
728 u_uastrncpy(customSkeleton, time_skeleton, strlen(time_skeleton));
730 pattern_generator = udatpg_open(locale, &status);
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);
737 u_austrcpy(bestPatternString, bestPattern);
738 u_uastrcpy(bestPattern,"a");
740 if(bestPatternString[0] == 'a')
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);
755 DBG("DATE & TIME is %s %s %d %s", locale, formattedString, u_strlen(formatted), bestPatternString);
757 apm_length = u_strlen(formatted);
759 udatpg_close(pattern_generator);
761 udat_close(formatter);
763 if(strlen(formattedString)<CLOCK_STR_LEN)
765 strncpy(output,formattedString,strlen(formattedString));
769 strncpy(output,formattedString,CLOCK_STR_LEN-1);
775 static UChar *uastrcpy(const char *chars)
780 str = (UChar *) malloc(sizeof(UChar) *(len + 1));
783 u_uastrcpy(str, chars);
787 static void ICU_set_timezone(const char *timezone)
791 ERR("TIMEZONE is NULL");
795 DBG("ICU_set_timezone = %s ", timezone);
796 UErrorCode ec = U_ZERO_ERROR;
797 UChar *str = uastrcpy(timezone);
799 ucal_setDefaultTimeZone(str, &ec);
801 DBG("ucal_setDefaultTimeZone() SUCCESS ");
803 DBG("ucal_setDefaultTimeZone() FAILED : %s ",