bug fix: TC-1152
[profile/ivi/ico-uxf-homescreen.git] / src / homescreen / CicoHSControlBarWindow.cpp
1 /*
2  * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
3  *
4  * This program is licensed under the terms and conditions of the
5  * Apache License, version 2.0.  The full text of the Apache License is at
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  */
9 /**
10  * @brief   control bar window
11  *
12  * @date    Aug-08-2013
13  */
14 #include "CicoHSControlBarWindow.h"
15 #include "CicoHSControlBarTouch.h"
16 #include "CicoGKeyFileConfig.h"
17 #include "CicoResourceConfig.h"
18 #include "CicoHomeScreen.h"
19 #include "CicoHSSystemState.h"
20 #include "CicoSound.h"
21
22 /*============================================================================*/
23 /* functions                                                                  */
24 /*============================================================================*/
25 /*--------------------------------------------------------------------------*/
26 /**
27  * @brief   CicoHSControlBarWindow::CicoHSControlBarWindow
28  *          Constractor
29  *
30  * @param[in]   none
31  * @return      none
32  */
33 /*--------------------------------------------------------------------------*/
34 CicoHSControlBarWindow::CicoHSControlBarWindow(void)
35 {
36     evas = NULL;
37
38     CicoResourceConfig::GetImagePath(img_dir_path, ICO_HS_MAX_PATH_BUFF_LEN);
39
40     CicoGKeyFileConfig config;
41     config.Initialize(ICO_HOMESCREEN_CONFIG_FILE, ICO_SYC_PACKAGE_HOMESCREEN);
42
43     const char *value = config.ConfigGetString("switchzone", "keyname", "m");
44     if (strlen(value) > (sizeof(changeZoneKeyName) - 1)) {
45         ICO_WRN("[switchzone] keyname is strlen overflow. use default keyname(m)");
46     }
47     else {
48         memset(changeZoneKeyName, 0, sizeof(changeZoneKeyName));
49         strncpy(changeZoneKeyName, value, strlen(value));
50     }
51
52     value = config.ConfigGetString("standardswitch", "homekeyname", "h");
53     if (strlen(value) > (sizeof(homeKeyName) - 1)) {
54         ICO_WRN("[standardswitch] keyname is strlen overflow. use default keyname(h)");
55     }
56     else {
57         memset(homeKeyName, 0, sizeof(homeKeyName));
58         strncpy(homeKeyName, value, strlen(value));
59     }
60
61     value = config.ConfigGetString("standardswitch", "backkeyname", "b");
62     if (strlen(value) > (sizeof(backKeyName) - 1)) {
63         ICO_WRN("[standardswitch] keyname is strlen overflow. use default keyname(b)");
64     }
65     else {
66         memset(backKeyName, 0, sizeof(backKeyName));
67         strncpy(backKeyName, value, strlen(value));
68     }
69
70     value = config.ConfigGetString("standardswitch", "menukeyname", "l");
71     menuKeyName[0] = value[0];
72     if (strlen(value) > (sizeof(menuKeyName) - 1)) {
73         ICO_WRN("[standardswitch] keyname is strlen overflow. use default keyname(l)");
74     }
75     else {
76         memset(menuKeyName, 0, sizeof(menuKeyName));
77         strncpy(menuKeyName, value, strlen(value));
78     }
79     ICO_DBG("Assigned key config : changeZone[%s]", changeZoneKeyName);
80     ICO_DBG("Assigned key config : home[%s]", homeKeyName);
81     ICO_DBG("Assigned key config : back[%s]", backKeyName);
82     ICO_DBG("Assigned key config : menu[%s]", menuKeyName);
83
84     char tmp_str[16];
85     for (unsigned int ii = 0; ii < ICO_HS_CONTROL_BAR_SHORTCUT_MAX_NUM; ii++) {
86         sprintf(tmp_str, ICO_HS_CONTROL_BAR_CONFIG_SHORTCUT_APP"%d", ii);
87         value = config.ConfigGetString(ICO_HS_CONFIG_CONTROL_BAR, tmp_str, NULL);
88         if ((NULL != value) && (0 != strcmp(value, "none"))) {
89             shortcut_appid[ii] = value;
90             ICO_DBG("shortcut_appid[%d] = [%s]", ii, shortcut_appid[ii]);
91         }
92         else {
93             shortcut_appid[ii] = NULL;
94             ICO_DBG("shortcut_appid[%d] = NULL", ii);
95         }
96     }
97 }
98
99 /*--------------------------------------------------------------------------*/
100 /**
101  * @brief   CicoHSControlBarWindow::~CicoHSControlBarWindow
102  *          Destractor
103  *
104  * @param[in]   none
105  * @return      none
106  */
107 /*--------------------------------------------------------------------------*/
108 CicoHSControlBarWindow::~CicoHSControlBarWindow(void)
109 {
110     /* not somthing to do */
111 }
112
113 /*--------------------------------------------------------------------------*/
114 /**
115  * @brief   CicoHSControlBarWindow::CreateControlBarWindow
116  *          crate window (control bar)
117  *
118  * @param[in]   pos_x    position x
119  * @param[in]   pos_y    position y
120  * @param[in]   width    width
121  * @param[in]   height   height
122  * @return      OK or ERRROR
123  */
124 /*--------------------------------------------------------------------------*/
125 int
126 CicoHSControlBarWindow::CreateControlBarWindow(int pos_x, int pos_y,
127                                                int width, int height)
128 {
129     int ret;
130     char img_path[ICO_HS_MAX_PATH_BUFF_LEN];
131
132     /*create window*/
133     ret = CreateWindow(ICO_HS_CONTROL_BAR_WINDOW_TITLE,
134                        pos_x, pos_y, width, height, EINA_TRUE);
135     if(ret != ICO_OK){
136        return ret;
137     }
138
139     /* get evas */
140     evas = ecore_evas_get(window);
141     if (!evas) {
142         ICO_ERR("ecore_evas_get failed.");
143         ICO_DBG("CicoHSControlBarWindow::CreateControlBarWindow Leave(ERROR)");
144         return ICO_ERROR;
145     }
146
147     // create background evas object
148     background = evas_object_rectangle_add(evas);
149
150     // add callback functions
151     evas_object_event_callback_add(background, EVAS_CALLBACK_KEY_DOWN,
152                                    CicoHSControlBarWindow::evasKeyDownCB, this);
153
154     // key grab
155     evas_object_focus_set(background, EINA_FALSE);
156     Eina_Bool eret = evas_object_key_grab(background, (const char*)changeZoneKeyName,
157                                           0, 0, EINA_TRUE);
158     if (EINA_FALSE == eret) {
159         ICO_WRN("evas_object_key_grab failed.");
160     }
161     eret = evas_object_key_grab(background, (const char*)homeKeyName,
162                                 0, 0, EINA_TRUE);
163     if (EINA_FALSE == eret) {
164         ICO_WRN("evas_object_key_grab failed.");
165     }
166     eret = evas_object_key_grab(background, (const char*)backKeyName,
167                                 0, 0, EINA_TRUE);
168     if (EINA_FALSE == eret) {
169         ICO_WRN("evas_object_key_grab failed.");
170     }
171     eret = evas_object_key_grab(background, (const char*)menuKeyName,
172                                 0, 0, EINA_TRUE);
173     if (EINA_FALSE == eret) {
174         ICO_WRN("evas_object_key_grab failed.");
175     }
176
177     // set background coloer
178     evas_object_color_set(background,128,128,128,255);
179
180     // change window geometry
181     evas_object_move(background, 0, 0);
182     evas_object_resize(background, width,height);
183
184     // show window
185     evas_object_show(background);
186
187     // home button
188     // image file name
189     snprintf(img_path, sizeof(img_path), "%s%s",
190              img_dir_path, ICO_HS_IMAGE_FILE_CONTROL_BAR_BUTTON_DAY);
191
192     // set object
193     menu_btn = evas_object_image_filled_add(evas);
194
195     // preload image
196     snprintf(img_path,sizeof(img_path),"%s%s",img_dir_path,
197              ICO_HS_IMAGE_FILE_CONTROL_BAR_BUTTON_DAY2);
198     evas_object_image_file_set(menu_btn, img_path, NULL);
199
200     snprintf(img_path,sizeof(img_path),"%s%s",img_dir_path,
201              ICO_HS_IMAGE_FILE_CONTROL_BAR_BUTTON_NIHGT);
202     evas_object_image_file_set(menu_btn, img_path, NULL);
203
204     snprintf(img_path,sizeof(img_path),"%s%s",img_dir_path,
205              ICO_HS_IMAGE_FILE_CONTROL_BAR_BUTTON_NIHGT2);
206     evas_object_image_file_set(menu_btn, img_path, NULL);
207
208     // load fisrt show icon image
209     snprintf(img_path,sizeof(img_path),"%s%s",img_dir_path,
210              ICO_HS_IMAGE_FILE_CONTROL_BAR_BUTTON_DAY);
211     evas_object_image_file_set(menu_btn, img_path, NULL);
212
213     evas_object_move(menu_btn,
214                      (width/2) - (ICO_HS_CONTROL_BAR_MENU_BTN_WIDTH /2),
215                      ICO_HS_CONTROL_BAR_MENU_BTN_START_POS_Y);
216     evas_object_resize(menu_btn, ICO_HS_CONTROL_BAR_MENU_BTN_WIDTH,
217                        ICO_HS_CONTROL_BAR_MENU_BTN_HEIGHT);
218     evas_object_event_callback_add(menu_btn, EVAS_CALLBACK_MOUSE_DOWN,
219                                    CicoHSControlBarTouch::TouchDownControlBar,
220                                    NULL);
221     evas_object_event_callback_add(menu_btn, EVAS_CALLBACK_MOUSE_UP,
222                                    CicoHSControlBarTouch::TouchUpControlBar,
223                                    NULL);
224     evas_object_show(menu_btn);
225
226     /* shortcut */
227     AddShortcut(evas, width);
228
229     return ICO_OK;
230 }
231
232 /*--------------------------------------------------------------------------*/
233 /**
234  * @brief   CicoHSControlBarWindow::AddShortcut
235  *          shortcut addition (control bar)
236  *
237  * @param[in]   evas     Evas Object
238  * @param[in]   width    width
239  * @return      none
240  */
241 /*--------------------------------------------------------------------------*/
242 void
243 CicoHSControlBarWindow::AddShortcut(Evas *evas, int width)
244 {
245     ICO_TRA("CicoHSControlBarWindow::AddShortcut Enter");
246
247     int escPosX;
248     int x;
249     int tmp_space;
250     int s_cnt = 0;
251     const char *tmp_appid;
252     CicoHSLifeCycleController *life_cycle_controller;
253
254     /* menu button x position */
255     escPosX = (width / 2) - (ICO_HS_CONTROL_BAR_MENU_BTN_WIDTH / 2);
256
257     life_cycle_controller = CicoHSLifeCycleController::getInstance();
258
259     tmp_space = (width - (ICO_HS_CONTROL_BAR_MENU_BTN_WIDTH
260                 * ICO_HS_CONTROL_BAR_BTN_MAX_NUM))
261                 / (ICO_HS_CONTROL_BAR_BTN_MAX_NUM + 1);
262     ICO_DBG("CicoHSControlBarWindow::AddShortcut tmp_space = [%d]",
263             tmp_space);
264
265     for (unsigned ii = 0; ii < ICO_HS_CONTROL_BAR_BTN_MAX_NUM; ii++) {
266         x = (ICO_HS_CONTROL_BAR_SHORTCUT_BTN_WIDTH * ii) +
267             (tmp_space * (ii + 1));
268
269         if ((x <= escPosX) &&
270            ((x + ICO_HS_CONTROL_BAR_SHORTCUT_BTN_WIDTH) >= escPosX)) {
271             /* The position of a menu button is skipped */
272             continue;
273         }
274         ICO_DBG("CicoHSControlBarWindow::AddShortcut x = [%d]", x);
275
276         tmp_appid = (const char *)(shortcut_appid[s_cnt]);
277         if (tmp_appid == NULL) {
278             /* No shortcut appid */
279             s_cnt++;
280             continue;
281         }
282
283         /* get APP information */
284         std::vector<CicoAilItems> aillist =
285         life_cycle_controller->getAilList();
286
287         /* add shortcut object */
288         for (unsigned int kk = 0; kk < aillist.size(); kk++ ) {
289             if (strncmp(aillist[kk].m_appid.c_str(), tmp_appid,
290                 ICO_HS_MAX_PROCESS_NAME) == 0) {
291                 ICO_DBG("CicoHSControlBarWindow::AddShortcut tmp_appid = [%s]",
292                         tmp_appid);
293                 shortcut[s_cnt] = evas_object_image_filled_add(evas);
294                 evas_object_image_file_set(shortcut[s_cnt],
295                                   aillist[kk].m_icon.c_str(), NULL);
296                 evas_object_move(shortcut[s_cnt], x,
297                                   ICO_HS_CONTROL_BAR_SHORTCUT_BTN_START_POS_Y);
298                 evas_object_resize(shortcut[s_cnt],
299                                   ICO_HS_CONTROL_BAR_SHORTCUT_BTN_WIDTH,
300                                   ICO_HS_CONTROL_BAR_SHORTCUT_BTN_HEIGHT);
301                 evas_object_event_callback_add(shortcut[s_cnt],
302                                   EVAS_CALLBACK_MOUSE_UP,
303                                   CicoHSControlBarTouch::TouchUpControlBar,
304                                   tmp_appid);
305                 evas_object_show(shortcut[s_cnt]);
306             }
307         }
308         s_cnt++;
309     }
310
311     ICO_TRA("CicoHSControlBarWindow::AddShortcut Leave");
312     return;
313 }
314
315 /*--------------------------------------------------------------------------*/
316 /**
317  * @brief   CicoHSControlBarWindow::FreeControlBarWindow
318  *          free window (control bar)
319  *
320  * @param[in]   none
321  * @return      none
322  */
323 /*--------------------------------------------------------------------------*/
324 void
325 CicoHSControlBarWindow::FreeControlBarWindow(void)
326 {
327     evas_object_del(background);
328     evas_object_del(menu_btn);
329     FreeWindow();
330 }
331
332 /*--------------------------------------------------------------------------*/
333 /**
334  * @brief   CicoHSControlBarWindow::TouchHome
335  *          touch home button
336  *
337  * @param[in]   none
338  * @return      none
339  */
340 /*--------------------------------------------------------------------------*/
341 void
342 CicoHSControlBarWindow::TouchHome(void)
343 {
344     ActivationUpdate();
345     if (true == CicoHSSystemState::getInstance()->getRegulation()) {
346         CicoSound::GetInstance()->PlayFailureSound();
347     }
348     else {
349         CicoSound::GetInstance()->PlayOperationSound();
350     }
351     CicoHomeScreen::ChangeMode(ICO_HS_SHOW_HIDE_PATTERN_SLIDE);
352 }
353
354 /*--------------------------------------------------------------------------*/
355 /**
356  * @brief   CicoHSControlBarWindow::SetNightMode
357  *          set night mode color theme chagne
358  *
359  * @param   none
360  * @return  none
361  */
362 /*--------------------------------------------------------------------------*/
363 void
364 CicoHSControlBarWindow::SetNightMode(void)
365 {
366     ICO_TRA("CicoHSControlBarWindow::SetNightMode Enter");
367
368     bool state = CicoHSSystemState::getInstance()->getNightMode();
369
370     char img_path[ICO_HS_MAX_PATH_BUFF_LEN];
371     if (true == state) {
372         ICO_DBG("ICO_SYC_STATE_ON");
373         evas_object_color_set(background,0,0,0,255);
374         if (true == CicoHSSystemState::getInstance()->getRegulation()) {
375             ICO_DBG("Regulation=ON");
376             snprintf(img_path,sizeof(img_path),"%s%s",img_dir_path,
377                      ICO_HS_IMAGE_FILE_CONTROL_BAR_BUTTON_NIHGT2);
378         }
379         else {
380             ICO_DBG("Regulation=OFF");
381             snprintf(img_path,sizeof(img_path),"%s%s",img_dir_path,
382                      ICO_HS_IMAGE_FILE_CONTROL_BAR_BUTTON_NIHGT);
383         }
384         evas_object_image_file_set(menu_btn, img_path, NULL);
385     }
386     else {
387         ICO_DBG("ICO_SYC_STATE_OFF");
388         evas_object_color_set(background,128,128,128,255);
389         if (true == CicoHSSystemState::getInstance()->getRegulation()) {
390             ICO_DBG("Regulation=ON");
391             snprintf(img_path,sizeof(img_path),"%s%s",img_dir_path,
392                      ICO_HS_IMAGE_FILE_CONTROL_BAR_BUTTON_DAY2);
393         }
394         else {
395             ICO_DBG("Regulation=OFF");
396             snprintf(img_path,sizeof(img_path),"%s%s",img_dir_path,
397                      ICO_HS_IMAGE_FILE_CONTROL_BAR_BUTTON_DAY);
398         }
399         evas_object_image_file_set(menu_btn, img_path, NULL);
400     }
401
402     ICO_TRA("CicoHSControlBarWindow::SetNightMode Leave");
403 }
404
405 /*--------------------------------------------------------------------------*/
406 /**
407  * @brief   CicoHSControlBarWindow::SetRegulation
408  *          set regulation color theme chagne
409  *
410  * @param   none
411  * @return  none
412  */
413 /*--------------------------------------------------------------------------*/
414 void
415 CicoHSControlBarWindow::SetRegulation(void)
416 {
417     ICO_TRA("CicoHSControlBarWindow::SetRegulation Enter");
418
419     char img_path[ICO_HS_MAX_PATH_BUFF_LEN];
420     if (true == CicoHSSystemState::getInstance()->getNightMode()) {
421         ICO_DBG("NightMode=ON");
422         if (true == CicoHSSystemState::getInstance()->getRegulation()) {
423             ICO_DBG("Regulation=ON");
424             snprintf(img_path,sizeof(img_path),"%s%s",img_dir_path,
425                      ICO_HS_IMAGE_FILE_CONTROL_BAR_BUTTON_NIHGT2);
426         }
427         else {
428             ICO_DBG("Regulation=OFF");
429             snprintf(img_path,sizeof(img_path),"%s%s",img_dir_path,
430                      ICO_HS_IMAGE_FILE_CONTROL_BAR_BUTTON_NIHGT);
431         }
432     }
433     else {
434         ICO_DBG("NightMode=OFF");
435         if (true == CicoHSSystemState::getInstance()->getRegulation()) {
436             ICO_DBG("Regulation=ON");
437             snprintf(img_path,sizeof(img_path),"%s%s",img_dir_path,
438                      ICO_HS_IMAGE_FILE_CONTROL_BAR_BUTTON_DAY2);
439         }
440         else {
441             ICO_DBG("Regulation=OFF");
442             snprintf(img_path,sizeof(img_path),"%s%s",img_dir_path,
443                      ICO_HS_IMAGE_FILE_CONTROL_BAR_BUTTON_DAY);
444         }
445     }
446     evas_object_image_file_set(menu_btn, img_path, NULL);
447
448     ICO_TRA("CicoHSControlBarWindow::SetRegulation Leave");
449 }
450
451 /*--------------------------------------------------------------------------*/
452 /**
453  * @brief   CicoHSControlBarWindow::SetWindowID
454  *          set appid and surface
455  *
456  * @param[in]   none
457  * @return      none
458  */
459 /*--------------------------------------------------------------------------*/
460 void
461 CicoHSControlBarWindow::SetWindowID(const char *appid,int surface)
462 {
463     strncpy(this->appid,appid,ICO_HS_MAX_PROCESS_NAME);
464     this->surface = surface;
465 }
466
467 /*--------------------------------------------------------------------------*/
468 /**
469  * @brief   CicoHSControlBarWindow::GetSurfaceId
470  *          get surface id of control bar window
471  *
472  * @return  surface id
473  */
474 /*--------------------------------------------------------------------------*/
475 int
476 CicoHSControlBarWindow::GetSurfaceId(void)
477 {
478     return this->surface;
479 }
480
481 /*--------------------------------------------------------------------------*/
482 /**
483  * @brief   CicoHSControlBarWindow::GetAppId
484  *          get application id of control bar
485  *
486  * @return  application id
487  */
488 /*--------------------------------------------------------------------------*/
489 const char *
490 CicoHSControlBarWindow::GetAppId(void)
491 {
492     return this->appid;
493 }
494
495 /*--------------------------------------------------------------------------*/
496 /**
497  * @brief   CicoHSControlBarWindow::TouchShortcut
498  *          touch shortcut button
499  *
500  * @param[in]   none
501  * @return      none
502  */
503 /*--------------------------------------------------------------------------*/
504 void
505 CicoHSControlBarWindow::TouchShortcut(const char *appid)
506 {
507     ICO_TRA("CicoHSControlBarWindow::TouchShortcut Enter");
508     ActivationUpdate();
509
510     CicoSound::GetInstance()->PlayOperationSound();
511
512     if (appid != NULL) {
513         ICO_DBG("CicoHSControlBarWindow::TouchShortcut appid = [%s]", appid);
514         CicoHomeScreen::ExecuteApp(appid);
515     }
516
517     ICO_TRA("CicoHSControlBarWindow::TouchShortcut Leave");
518 }
519
520 //--------------------------------------------------------------------------
521 /**
522  *  @brief  key up event callback function
523  *
524  *  @pamam [in] data    user data
525  *  @param [in] evas    evas instcance
526  *  @param [in] obj     evas object instcance
527  *  @param [in] info    event information(Evas_Event_Key_Down)
528  */
529 //--------------------------------------------------------------------------
530 void
531 CicoHSControlBarWindow::onKeyDown(void *data, Evas *evas,
532                                   Evas_Object *obj, void *info)
533 {
534     Evas_Event_Key_Down *evinfo = NULL;
535     evinfo = (Evas_Event_Key_Down*)info;
536
537     CicoSound::GetInstance()->PlayOperationSound();
538
539     ICO_PRF("TOUCH_EVENT Key Down keyname=%s, key=%d",
540             evinfo->keyname, (char)*evinfo->key);
541
542     if (0 == strcmp(evinfo->keyname, changeZoneKeyName)) {
543         CicoHomeScreen::ChangeZone();
544     }
545     else if (0 == strcmp(evinfo->keyname, homeKeyName)) {
546         TouchHome();
547     }
548     else if (0 == strcmp(evinfo->keyname, backKeyName)) {
549         // TODO not assinded funciton
550     }
551     else if (0 == strcmp(evinfo->keyname, menuKeyName)) {
552         // TODO not assinded funciton
553     }
554 }
555
556 //--------------------------------------------------------------------------
557 /**
558  *  @brief  key up event callback function
559  *
560  *  @pamam [in] data    user data
561  *  @param [in] evas    evas instcance
562  *  @param [in] obj     evas object instcance
563  *  @param [in] info    event information(Evas_Event_Key_Down)
564  */
565 //--------------------------------------------------------------------------
566 void
567 CicoHSControlBarWindow::evasKeyDownCB(void *data, Evas *evas,
568                                       Evas_Object *obj, void *info)
569 {
570     CicoHSControlBarWindow *ctrlbarwin = (CicoHSControlBarWindow*)(data);
571     ctrlbarwin->onKeyDown(data, evas, obj, info);
572 }
573
574 //--------------------------------------------------------------------------
575 /**
576  *  @brief  Activation update swipe app
577  *
578  *  @return bool
579  *  @retval true update
580  *  @retval false no update
581  */
582 //--------------------------------------------------------------------------
583 bool
584 CicoHSControlBarWindow::ActivationUpdate(void)
585 {
586     return CicoHomeScreen::ActivationUpdate();
587 }
588 // vim: set expandtab ts=4 sw=4: