bug fix: TC-1152
[profile/ivi/ico-uxf-homescreen.git] / src / onscreen / CicoOSPopWindow.cpp
1 /*
2  * Copyright (c) 2014, 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   On Screen Popup Window
11  *
12  * @date    Feb-17-2014
13  */
14 #include <Ecore.h>
15 #include <Ecore_Wayland.h>
16 #include <appsvc/appsvc.h>
17 #include <tzplatform_config.h>
18 #include "CicoOnScreen.h"
19 #include "CicoNotification.h"
20 #include "CicoOSPopWindow.h"
21 #include "ico_syc_type.h"
22 #include "ico_syc_private.h"
23 #include "ico_syc_appresctl.h"
24 #include "CicoOSClient.h"
25
26 using namespace std;
27
28 //==========================================================================
29 // static members
30 //==========================================================================
31 Ecore_Evas*  CicoOSPopWindow::m_window = NULL;
32
33 //==========================================================================
34 // functions
35 //==========================================================================
36
37 //--------------------------------------------------------------------------
38 /**
39  * @brief   CicoOnScreen::CicoOnScreen
40  *          Constractor
41  *
42  * @param[in]   none
43  * @return      none
44  */
45 //--------------------------------------------------------------------------
46 CicoOSPopWindow::CicoOSPopWindow(notification_type_e type) : CicoNotification(type)
47 {
48     ICO_TRA("Enter type(%d)", (int)type);
49     m_icon        = NULL;
50     m_theme       = NULL;
51     m_resourceId  = CicoOnScreen::GetResourceId();
52     m_context     = NULL;
53     m_buttonTouch = false;
54     ICO_TRA("Leave");
55 }
56
57 CicoOSPopWindow::CicoOSPopWindow(notification_h noti) : CicoNotification(noti)
58 {
59     ICO_TRA("Enter");
60     m_icon        = NULL;
61     m_theme       = NULL;
62     m_resourceId  = CicoOnScreen::GetResourceId();
63     m_context     = NULL;
64     m_buttonTouch = false;
65     ICO_TRA("Leave");
66 }
67
68 //--------------------------------------------------------------------------
69 /**
70  * @brief   CicoOnScreen::~CicoOnScreen
71  *          Destractor
72  *
73  * @param[in]   none
74  * @return      none
75  */
76 //--------------------------------------------------------------------------
77 CicoOSPopWindow::~CicoOSPopWindow(void)
78 {
79     ICO_TRA("Enter sur:%08x, priv_id:%d", m_resourceId, GetPrivId());
80
81     if (NULL != m_context) {
82         int r = ico_syc_release_res(m_context);
83         ICO_DBG("_____ %d = ico_syc_release_res", r);
84     }
85     ICO_DBG("_____ %d, %d", (int)m_buttonTouch, (int)m_appsvc_pkgname.empty());
86
87     if ((true == m_buttonTouch) && (false == m_appsvc_pkgname.empty())) {
88         CicoOSClient* cosc = CicoOSClient::getInstance();
89         if (NULL != cosc) {
90             int r = cosc->sendLaunchMessage(m_appsvc_pkgname);
91             ICO_DBG("_____ SendMsg:%d, %s", r, m_appsvc_pkgname.c_str());
92         }
93     }
94
95     if (m_theme)    {
96         usleep(POPUP_DELETE_WAIT*1000);
97         evas_object_hide(m_theme);
98         evas_object_del(m_theme);
99         m_theme = NULL;
100     }
101     if (m_icon) {
102         evas_object_hide(m_icon);
103         evas_object_del(m_icon);
104         m_icon = NULL;
105     }
106     ICO_TRA("Leave");
107 }
108
109 //--------------------------------------------------------------------------
110 /**
111  * @brief   CicoOnScreen::ShowPopup
112  *          Show popup window
113  *
114  * @param[in]   none
115  * @return      none
116  */
117 //--------------------------------------------------------------------------
118 bool
119 CicoOSPopWindow::showPopup()
120 {
121     ICO_TRA("Enter");
122
123     if (NULL == m_theme) {
124         if (false == InitializeWindow()) {
125             ICO_TRA("Leave false");
126             return false;
127         }
128     }
129     // get caller pachage name
130     const char *pkgname = GetPkgname();
131
132     // get priv_id
133     int priv_id = GetPrivId();
134
135     // Get title
136     const char *title = GetTitle();
137
138     // Get content
139     const char *content = GetContent();
140
141     // Get execute option
142     const char *text = NULL;
143     bundle *service_handle = NULL;
144     (void)GetExecuteOption(NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, &text,
145                            &service_handle);
146
147     // Get icon path
148     const char *icon = GetIconPath();
149     ICO_DBG("Received: %s : %i : %s : %s : %s : %p",
150             pkgname, priv_id, title, content, text, (void *)service_handle);
151
152     if (NULL != service_handle) {
153         const char* pn = appsvc_get_pkgname(service_handle);
154         if (NULL != pn) {
155             ICO_DBG("Received: appsvc_get_pkgname:%s", pn);
156             m_appsvc_pkgname = pn;
157             m_buttonTouch = false;
158         }
159     }
160
161     if (icon) {
162         if (NULL != m_icon) {
163             evas_object_image_file_set(m_icon, icon, NULL);
164             evas_object_show(m_icon);
165         }
166     }
167
168     if (title) {
169         if (!edje_object_part_text_set(m_theme, "title_text", title)) {
170             ICO_WRN("could not set the text. Maybe part 'text' does not exist?");
171         }
172     }
173
174     if (content) {
175         if (strlen(content) <= 25) {
176             ICO_DBG("content text center");
177             if (!edje_object_part_text_set(m_theme, "content_text_center", content)) {
178                 ICO_WRN("could not set the text. Maybe part 'text' does not exist?");
179             }
180             if (!edje_object_part_text_set(m_theme, "content_text_left", "")) {
181                 ICO_WRN("could not set the text. Maybe part 'text' does not exist?");
182             }
183         }
184         else {
185             ICO_DBG("content text left");
186             if (!edje_object_part_text_set(m_theme, "content_text_left", content)) {
187                 ICO_WRN("could not set the text. Maybe part 'text' does not exist?");
188             }
189             if (!edje_object_part_text_set(m_theme, "content_text_center", "")) {
190                 ICO_WRN("could not set the text. Maybe part 'text' does not exist?");
191             }
192         }
193     }
194
195     if (text) {
196         if (!edje_object_part_text_set(m_theme, "button_text", text)) {
197             ICO_WRN("could not set the text. Maybe part 'text' does not exist?");
198         }
199     }
200     else {
201         if (!edje_object_part_text_set(m_theme, "button_text", "OK")) {
202             ICO_WRN("could not set the text. Maybe part 'text' does not exist?");
203         }
204     }
205
206     evas_object_show(m_theme);
207     ICO_TRA("Leave true");
208     return true;
209 }
210
211 bool
212 CicoOSPopWindow::acquireRes()
213 {
214     ICO_TRA("Enter");
215     if (NULL != m_context) {
216         ICO_TRA("Leave false");
217         return false;
218     }
219     ico_syc_res_window_t w;
220     makeResWindowT(w);
221     m_context = ico_syc_acquire_res(&w, NULL, NULL, ICO_SYC_RES_ONSCREEN);
222     if (NULL == m_context) {
223         ICO_TRA("Leave false");
224         return false;
225     }
226     ICO_TRA("Leave true");
227     return true;
228 }
229
230 //--------------------------------------------------------------------------
231 /**
232  * @brief   CicoOnScreen::HidePopup
233  *          Hide popup window
234  *
235  * @param[in]   data        The user data passed from the callback
236  *                          registration function
237  * @param[in]   e           The handle to the popup window
238  * @param[in]   obj         The handle to the Evas object
239  * @param[in]   event_info  Event information
240  * @return      none
241  */
242 //--------------------------------------------------------------------------
243 void
244 CicoOSPopWindow::hidePopup(bool buttonTouch)
245 {
246     ICO_TRA("Enter %s", buttonTouch? "true": "false");
247     m_buttonTouch = buttonTouch;
248     releaseRes();
249     ICO_TRA("Leave");
250 }
251
252 bool
253 CicoOSPopWindow::releaseRes()
254 {
255     ICO_TRA("Enter");
256     if (NULL != m_context) {
257         int r = ico_syc_release_res(m_context);
258         ICO_DBG("_____ %d = ico_syc_release_res", r);
259         if (ICO_SYC_ERR_NONE == r) {
260             m_context = NULL;
261             ICO_TRA("Leave true");
262             return true;
263         }
264     }
265     ICO_TRA("Leave false");
266     return false;
267 }
268
269 //--------------------------------------------------------------------------
270 /**
271  * @brief   CicoOnScreen::InitializeWindow
272  *          Initialize popup window
273  *
274  * @param[in]   none
275  * @return      none
276  */
277 //--------------------------------------------------------------------------
278 bool
279 CicoOSPopWindow::InitializeWindow(void)
280 {
281     ICO_TRA("Enter");
282
283     if (NULL == m_window) {
284         if (false == createMainWindow()) {
285             ICO_TRA("Leave(ERR)");
286             return false;
287         }
288     }
289     m_theme = edje_object_add(ecore_evas_get(m_window));
290     if (NULL == m_theme) {
291         ICO_ERR("could not create edje object!");
292         ICO_TRA("Leave(ERR)");
293         return false;
294     }
295     if (! edje_object_file_set(m_theme,
296                                tzplatform_mkpath(TZ_SYS_RO_APP,
297                                                  ICO_OS_THEMES_EDJ_FILEPATH), "main")) {
298         Edje_Load_Error err = edje_object_load_error_get(m_theme);
299         const char *errmsg = edje_load_error_str(err);
300         ICO_ERR("could not load 'main' from onscreen.edj: %s", errmsg);
301         evas_object_del(m_theme);
302         m_theme = NULL;
303         return false;
304     }
305     // icon setup
306     m_icon = evas_object_image_filled_add(ecore_evas_get(m_window));
307     evas_object_pointer_mode_set(m_icon, EVAS_OBJECT_POINTER_MODE_NOGRAB);
308     edje_object_part_swallow(m_theme, "icon", m_icon);
309
310     /* getting size of screen */
311     /* home screen size is full of display */
312     int display_width  = 0;
313     int display_height = 0;
314     ecore_wl_screen_size_get(&display_width, &display_height);
315
316     ICO_DBG("display size w/h=%d/%d", display_width, display_height);
317     int popup_x = (display_width / 2) - (POPUP_FRAME_WIDTH / 2);
318     int popup_y = (display_height / 2) - (POPUP_FRAME_HEIGHT/ 2);
319     ICO_DBG("popup postion x/y=%d/%d", popup_x, popup_y);
320     evas_object_move(m_theme, popup_x, popup_y);
321     evas_object_resize(m_theme, POPUP_FRAME_WIDTH, POPUP_FRAME_HEIGHT);
322
323     Evas_Object* obj = NULL;
324     obj = (Evas_Object*)edje_object_part_object_get(m_theme, "button_text");
325     if (NULL != obj) {
326         evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
327                                        evasMouseUpCB, this);
328     }
329
330     obj = (Evas_Object*)edje_object_part_object_get(m_theme, "button");
331     if (NULL != obj) {
332         evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
333                                        evasMouseUpCB, this);
334     }
335     evas_object_show(m_theme);
336     ICO_TRA("Leave(OK)");
337     return true;
338 }
339
340 bool
341 CicoOSPopWindow::createMainWindow()
342 {
343     ICO_TRA("Enter");
344
345     // Window setup
346     m_window = ecore_evas_new(NULL, 0, 0, WIDTH, HEIGHT, "frame=0");
347     if (NULL == m_window) {
348         ICO_ERR("ecore_evas_new() error");
349         ICO_TRA("Leave(ERR)");
350         return false;
351     }
352
353     ecore_evas_title_set(m_window, POPUP_WINDOW_TITLE);
354     ecore_evas_alpha_set(m_window, EINA_TRUE);
355     ecore_evas_show(m_window);
356
357     ICO_TRA("Leave");
358     return true;
359 }
360
361 void
362 CicoOSPopWindow::removeMainWindow()
363 {
364     ICO_TRA("Enter");
365
366     if (NULL != m_window) {
367         ecore_evas_free(m_window);
368         m_window = NULL;
369     }
370     ICO_TRA("Leave");
371 }
372
373 //--------------------------------------------------------------------------
374 /**
375  * @brief   callback function of evas mouse up event
376  *
377  * @param [in] data         The user data passed from the callback
378  *                          registration function
379  * @param [in] e            The handle to the popup window
380  * @param [in] obj          The handle to the Evas object
381  * @param [in] event_info   Event information
382  *
383  */
384 //--------------------------------------------------------------------------
385 void
386 CicoOSPopWindow::evasMouseUpCB(void *data, Evas *e, Evas_Object *obj,
387                                void *event_info)
388 {
389     ICO_TRA("Enter %x, %x, %x, %x", data, e, obj, event_info);
390     if (NULL != obj) {
391         ICO_DBG("_____ obj name=%s", evas_object_name_get(obj));
392     }
393     static_cast<CicoOSPopWindow*>(data)->hidePopup(true);
394     ICO_TRA("Leave");
395 }
396
397
398 static const char id0_ECU[]        = "Center";
399
400 static const char id00_display[]   = "Center";
401 static const char id006_layer[]    = "OnScreen";
402 static const char id00611_layout[] = "Whole";
403 static const char id006111_area[]  = "Full";
404
405 static const char role_incoming[]  = "incoming";
406 static const char role_message[]   = "message";
407
408 void
409 CicoOSPopWindow::makeResWindowT(ico_syc_res_window_t& w)
410 {
411     w.ECU         = id0_ECU;
412     w.display     = id00_display;
413     w.layer       = id006_layer;
414     w.layout      = id00611_layout;
415     w.area        = id006111_area;
416     w.dispatchApp = (char*)GetPkgname();
417     if (0 == strcmp(w.dispatchApp, ICO_SYC_PACKAGE_DIALER)) {
418         w.role    = role_incoming;
419     }
420     else {
421         w.role    = role_message;
422     }
423     w.resourceId  = m_resourceId;
424 }
425
426 // vim: set expandtab ts=4 sw=4: