[Dialog] add noti sound back.wav
[platform/core/api/cordova-plugins.git] / src / lib / plugins / cordova-plugin-dialogs / tizen / Notification.js
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
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 // TODO: remove when added to public cordova repository -> begin
18 var plugin_name = 'cordova-plugin-dialogs.tizen.Notification';
19
20 cordova.define(plugin_name, function(require, exports, module) {
21 // TODO: remove -> end
22
23 var _document = document || {};
24
25 var playback = (function() {
26   var soundElement;
27   var counter = 1;
28   var soundPathArray = ['/usr/share/feedback/sound/operation/operation.wav', '/usr/share/feedback/sound/operation/home.wav', '/usr/share/feedback/sound/operation/back.wav'];
29   var soundPathCount = 0;
30
31   function win(v) {
32     console.log('Success to get the notification sound: ' + v);
33     soundElement = new Audio(v);
34     soundElement.addEventListener('ended', function() {
35       if (--counter > 0) {
36         soundElement.play();
37       }
38     });
39   }
40
41   function fail(e) {
42     console.error('Failed to get the notification sound: ' + e);
43   }
44
45   function recursiveFailCallback(e) {
46     fail(e);
47     console.warn("Retry with another path");
48     
49     if(soundPathCount >= soundPathArray.length){
50       console.error('Failed to get the notification sound: There is no available sound file path');
51       return;
52     }
53
54     tizen.filesystem.resolve(soundPathArray[soundPathCount++], function(file) {
55       win(file.toURI());
56     }, recursiveFailCallback, 'r');
57
58   }
59
60   if ('tv' === tizen.systeminfo.getCapability('http://tizen.org/feature/profile').toLowerCase()) {
61     tizen.filesystem.resolve(soundPathArray[soundPathCount++], function(file) {
62       win(file.toURI());
63     }, recursiveFailCallback, 'r');
64   } else {
65     tizen.systemsetting.getProperty('NOTIFICATION_EMAIL', win, fail);
66   }
67
68   function beep(times) {
69     counter = times || 1;
70     if (soundElement) {
71       soundElement.play();
72     }
73   }
74
75   return {
76     beep: beep
77   }
78 })();
79
80 var popup = (function () {
81   var boxId = 'cordova-modal-box';
82   var overlayId = 'cordova-modal-overlay';
83   var popupId = 'cordova-modal-popup';
84   var buttonIdPrefix = 'cordova-modal-popup-button-';
85   var inputId = 'cordova-modal-popup-input';
86   var d = _document;
87   var sheet;
88   var box;
89   var overlay;
90   var popup;
91   var dismissCallback;
92   var tizenHwKeyListener = null;
93
94   function isPopupVisible() {
95     if (box) {
96       return box.style.display === 'block';
97     } else {
98       return false;
99     }
100   }
101
102   function tizenHwKeyFunc(evt) {
103     if (evt.keyName === "back") {
104       evt.stopImmediatePropagation();
105       if (dismissCallback) {
106         dismissCallback();
107       }
108     }
109   }
110
111   function showPopup() {
112     box.style.display = 'block';
113   }
114
115   function hidePopup() {
116     box.style.display = 'none';
117   }
118
119   function createCloseCallback(callback, id, prompt) {
120     return function() {
121       var text;
122       if (prompt) {
123         text = d.getElementById(inputId).value;
124       }
125
126       if (tizenHwKeyListener) {
127         window.removeEventListener('tizenhwkey', tizenHwKeyListener, true);
128         tizenHwKeyListener = null;
129       }
130
131       hidePopup();
132       callback(id, text);
133     }
134   }
135
136   function hasCssClass(name) {
137     for (var i = 0; i < d.styleSheets.length; ++i) {
138       for (var j = 0; j < d.styleSheets[i].rules.length; ++j) {
139         if (name === d.styleSheets[i].rules[j].selectorText) {
140           return true;
141         }
142       }
143     }
144     return false;
145   }
146
147   function createStyleSheet() {
148     if (!sheet) {
149       var style = document.createElement('style');
150       d.head.appendChild(style);
151       sheet = style.sheet;
152     }
153   }
154
155   function initCss() {
156     var hasOverlay = hasCssClass('.cordova-ui-popupwindow-overlay');
157     var hasPopup = hasCssClass('.cordova-ui-popupwindow');
158
159     if (!hasOverlay) {
160       createStyleSheet();
161
162       sheet.insertRule('.cordova-ui-popupwindow-overlay {' +
163         'background: #000000;' +
164         'opacity: 0.6;' +
165         'position: fixed;' +
166         'top: 0;' +
167         'left: 0;' +
168         'margin: 0;' +
169         'padding: 0;' +
170         'width: 100%;' +
171         'height: 100%;' +
172         'z-index: 1200;' +
173       '}');
174     }
175
176     if (!hasPopup) {
177       createStyleSheet();
178
179       sheet.insertRule('.cordova-ui-popupwindow {' +
180         'position: fixed;' +
181         'top: 50%;' +
182         'left: 50%;' +
183         'margin-right: -50%;' +
184         'transform: translate(-50%, -50%);' +
185         '-webkit-transform: translate(-50%, -50%);' +
186         'z-index: 1201 !important;' +
187         'color: #f8f6ef;' +
188         'background: #2a2d30;' +
189         'width: 90%;' +
190         'padding: 6px;' +
191       '}');
192       sheet.insertRule('.cordova-ui-popupwindow .popup-title {' +
193         'width: 100%;' +
194         'height: 100%;' +
195         'font-size: 1.0909090909090908rem;' +
196         'background: #5093b6;' +
197       '}');
198       sheet.insertRule('.cordova-ui-popupwindow .popup-title p {' +
199         'margin: 0rem 0rem;' +
200         'padding: 0.5909090909090909rem 0.5rem;' +
201       '}');
202       sheet.insertRule('.cordova-ui-popupwindow .popup-text {' +
203         'width: 100%;' +
204         'color: #f9f9f9;' +
205         'font-size: 1.0909090909090908rem;' +
206         'background: #2a2d30;' +
207       '}');
208       sheet.insertRule('.cordova-ui-popupwindow .popup-text p {' +
209         'text-align: left;' +
210         'padding: 0.4rem;' +
211       '}');
212       sheet.insertRule('.cordova-ui-popupwindow .popup-input {' +
213         'width: 100%;' +
214         'padding-bottom: 0.4rem;' +
215         'text-align: center;' +
216       '}');
217       sheet.insertRule('.cordova-ui-popupwindow .popup-input input {' +
218         'width: 90%;' +
219         'color: #222222;' +
220         'font-size: 1rem;' +
221         'line-height: 1.2;' +
222         'font-weight: normal;' +
223         'border: 1px solid #555;' +
224         'border-radius: 5px;' +
225         '-webkit-border-radius: 5px;' +
226       '}');
227       sheet.insertRule('.cordova-ui-popupwindow .popup-button-bg {' +
228         'font-size: 1.4545454545454546rem;' +
229         'background: #2a2d30;' +
230         'width: 100%;' +
231         'padding-top: 0.5rem;' +
232         'padding-bottom: 0.5rem;' +
233         'vertical-align: middle;' +
234         'text-align: right;' +
235       '}');
236       sheet.insertRule('.cordova-ui-popupwindow .popup-button-bg input {' +
237         'border: 1px solid #555;' +
238         'border-radius: 5px;' +
239         '-webkit-border-radius: 5px;' +
240         'text-align: center;' +
241         'display: inline-block;' +
242         'background: #444;' +
243         'color: #9ab;' +
244         'font-size: 0.8em;' +
245         'text-decoration: none;' +
246         'margin: 1px 1px 1px 3px;' +
247       '}');
248     }
249   }
250
251   function initHtml() {
252     var touchMoveDisable = function(e) {
253       e.preventDefault();  // disable the touch-move of the background
254     };
255
256     box = d.createElement('div');
257     box.id = boxId;
258
259     overlay = box.appendChild(d.createElement('div'));
260     overlay.id = overlayId;
261     overlay.className = 'cordova-ui-popupwindow-overlay';
262     overlay.addEventListener('click', function() {
263       if (dismissCallback) {
264         dismissCallback();
265       }
266     }, false);
267     overlay.addEventListener('touchmove', touchMoveDisable);
268
269     popup = box.appendChild(d.createElement('div'));
270     popup.id = popupId;
271     popup.className = 'cordova-ui-popupwindow';
272     popup.addEventListener('touchmove', touchMoveDisable);
273
274     hidePopup();
275
276     d.getElementsByTagName('body')[0].appendChild(box);
277   }
278
279   function init() {
280     if (!box) {
281       initCss();
282       initHtml();
283     }
284   }
285
286   function show(options) {
287     if (isPopupVisible()) {
288       console.warn('Popup is already shown, ignoring.');
289       return;
290     }
291
292     init();
293
294     options = options || {};
295
296     // prepare contents of the popup
297     var html = '';
298     html += '<div class="popup-title"><p>' + (options.title || 'Title') + '</p></div>';
299     html += '<div class="popup-text"><p>' + (options.message || 'Message') + '</p></div>';
300     if (options.prompt) {
301       html += '<div class="popup-input"><input id="' + inputId + '" type="text" value="' + options.promptDefaultText + '" /></div>';
302     }
303     var buttons = options.buttons || ['Button'];
304     html += '<div class="popup-button-bg">';
305     for (var i = 0; i < buttons.length; ++i) {
306       html += '<input id="' + buttonIdPrefix + (i + 1)
307           + '" type="button" value="' + buttons[i] + '" />';
308     }
309     html += '</div>';
310
311     popup.innerHTML = html;
312
313     // link callbacks
314     var callback = options.callback || function(){};
315     dismissCallback = createCloseCallback(callback, 0, options.prompt);
316
317     for (var i = 0; i < buttons.length; ++i) {
318       var e = d.getElementById(buttonIdPrefix + (i + 1));
319       e.addEventListener('click', createCloseCallback(callback, i + 1, options.prompt), false);
320     }
321
322     if (!tizenHwKeyListener) {
323       tizenHwKeyListener = tizenHwKeyFunc;
324       window.addEventListener('tizenhwkey', tizenHwKeyListener, true);
325     }
326
327     // show popup
328     showPopup();
329   }
330
331   return {
332     show: show
333   };
334 })();
335
336 exports = {
337   alert: function(successCallback, errorCallback, args) {
338     popup.show({
339       message: args[0],
340       callback: successCallback,
341       title: args[1],
342       buttons: [args[2]]
343     });
344   },
345   confirm: function(successCallback, errorCallback, args) {
346     popup.show({
347       message: args[0],
348       callback: successCallback,
349       title: args[1],
350       buttons: args[2]
351     });
352   },
353   prompt: function(successCallback, errorCallback, args) {
354     popup.show({
355       message: args[0],
356       callback: function(idx, text) {
357         successCallback({buttonIndex: idx, input1: text});
358       },
359       title: args[1],
360       buttons: args[2],
361       prompt: true,
362       promptDefaultText: args[3]
363     });
364   },
365   beep: function(successCallback, errorCallback, args) {
366     playback.beep(args[0]);
367   }
368 };
369
370 require("cordova/exec/proxy").add("Notification", exports);
371
372 console.log('Loaded cordova.dialog API');
373
374 // TODO: remove when added to public cordova repository -> begin
375 });
376 // TODO: remove -> end