Fixes for IVI-3190 - To get things to render normally
[profile/ivi/Modello_Homescreen.git] / js / installedApps.js
1 /*global ThemeKeyColor, Settings, loadScript */
2
3 /**
4  * @module HomescreenApplication
5  **/
6 /**
7  * Retrieves list of installed apps from [Tizen Application API](https://developer.tizen.org/dev-guide/2.2.0/org.tizen.web.device.apireference/tizen/application.html)
8  * and manages displaing it in app grid view. Class also provides launching of installed app by clicking on app representation in grid view.
9  * @class installedApps
10  * @static
11  **/
12 /**
13  * Global variable which holds the list component in the UI
14  * @property appList
15  * @type array
16  **/
17 var appList = [];
18 /**
19  * Global variable which holds the identifier of the application information event listener
20  * @property listenerID
21  * @type string
22  * @default null
23  **/
24 var listenerID = null;
25 /**
26  * Global variable which holds the current index of last element in appList
27  * @property index
28  * @type int
29  * @default 0
30  **/
31 var index = 0;
32
33 /**
34  * Provides hiding installed app grid afted click out of app cells.
35  * @method $
36  * @static
37  **/
38 $(function() {
39         "use strict";
40         $("#homeScrAppGridView").live("click", function() {
41                 $(this).fadeOut();
42         });
43 });
44
45 /**
46  * Provide logging of app launch success.
47  * @method onLaunchSuccess
48  * @static
49  **/
50 function onLaunchSuccess() {
51         "use strict";
52         console.log("App launched...");
53 }
54
55 /**
56  * Provide logging of app launch error.
57  * @method onError
58  * @param err {string} Error message.
59  * @static
60  **/
61 function onError(err) {
62         "use strict";
63         console.error(err.message);
64 }
65
66 /**
67  * Provide launch of application.
68  * @method onFrameClick
69  * @param appData {object} Contains Object of specific app.
70  * @static
71  **/
72 function onFrameClick(appData) {
73         "use strict";
74         //launch application
75         var i;
76         try {
77                 var scriptCallback = function(path, status) {
78                         if (status === "ok") {
79                                 Settings.init();
80                         }
81                 };
82
83                 for (i = 0; i < appList.length; ++i) {
84                         if (appList[i].id === appData.id) {
85                                 if (appData.id === "http://com.intel.tizen/intelPocSettings") {
86                                         if (typeof Settings === 'undefined') {
87                                                 loadScript('./css/car/components/settings/js/settings.js', scriptCallback);
88                                         } else {
89                                                 Settings.show();
90                                         }
91                                 } else {
92                                         tizen.application.launch(appData.id, onLaunchSuccess, onError);
93                                 }
94                                 break;
95                         }
96                 }
97         } catch (exc) {
98                 console.error(exc.message);
99         }
100 }
101
102 /**
103  * Create app grid view based on appList.
104  * @method insertAppFrame
105  * @param appFrame {object} Contains Object of specific app from appList property.
106  * @static
107  **/
108 function insertAppFrame(appFrame) {
109         "use strict";
110         var rootDiv = $("<div></div>").addClass("homeScrAppGridFrame boxShadow3").data("app-data", appFrame).click(function() {
111                 onFrameClick($(this).data("app-data"));
112         });
113
114         var innerDiv = $("<div></div>").addClass("homeScrAppGridImg").appendTo(rootDiv);
115         $("<img />").data("src", appFrame.iconPath).appendTo(innerDiv);
116         var textDiv = $("<div />").addClass("homeScrAppGridText").appendTo(rootDiv);
117         $("<div />").addClass("homeScrAppGridTitle fontColorNormal fontSizeSmaller fontWeightBold").text(appFrame.appName).appendTo(textDiv);
118         $("<div />").addClass("homeScrAppGridCategory").text(appFrame.appName).appendTo(textDiv);
119
120         $('#homeScrAppGridView').append(rootDiv);
121
122         var img = new Image();
123         var ctx = document.createElement('canvas').getContext('2d');
124
125         img.onload = function() {
126                 var w = ctx.canvas.width = img.width;
127                 var h = ctx.canvas.height = img.height;
128
129                 // Change icon only in case of Intel POC apps
130                 if (appFrame.appName.indexOf("Modello") >= 0) {
131                         ctx.fillStyle = ThemeKeyColor;
132                         ctx.fillRect(0, 0, w, h);
133                         ctx.globalCompositeOperation = 'destination-in';
134                 }
135                 ctx.drawImage(img, 0, 0);
136
137                 $("div.homeScrAppGridImg img").each(function() {
138                         if ($(this).data("src") === appFrame.iconPath) {
139                                 $(this)[0].src = ctx.canvas.toDataURL();
140                         }
141                 });
142         };
143
144         img.onerror = img.onabort = function() {
145                 $("div.homeScrAppGridImg img").each(function() {
146                         if ($(this).data("src") === appFrame.iconPath) {
147                                 $(this).attr("src", "./css/images/default_icon.png");
148                         }
149                 });
150         };
151
152         img.src = appFrame.iconPath;
153
154         index++;
155         appList.push(appFrame);
156 }
157
158 var evalInstalledApps = null;
159
160 /**
161  * Callback method for getting and resorting appList array for Homescreen app using.
162  * @method onAppInfoSuccess
163  * @param list {array} Contains Objects of apps from evalInstalledApps listener.
164  * @static
165  **/
166 function onAppInfoSuccess(list) {
167         "use strict";
168         var i = 0;
169         try {
170                 index = 0;
171                 var applications = [];
172
173                 applications.push({
174                         id: "http://com.intel.tizen/intelPocSettings",
175                         appName: "Settings",
176                         show: true,
177                         iconPath: "./css/car/components/settings/icon.png"
178                 });
179
180                 list.sort(function(x, y) {
181                         return x.appName > y.appName ? 1 : -1;
182                 });
183
184                 for (i = 0; i < list.length; i++) {
185
186                         var app = list[i];
187                         var newApp = {
188                                 id: app.id,
189                                 appName: app.name,
190                                 style: "background-image: url('file://" + app.iconPath + "');",
191                                 iconPath: app.iconPath,
192                                 css: "app_" + app.id.replace(/\./g, "_").replace(/\ /g, "_"),
193                                 installed: true
194                         };
195                         applications.push(newApp);
196                 }
197                 var equals = applications.length === appList.length;
198
199                 if (equals) {
200                         for (var j = 0; j < applications.length; j++) {
201                                 equals = applications[j].id === appList[j].id ? equals : false;
202                                 equals = applications[j].appName === appList[j].appName ? equals : false;
203                                 equals = applications[j].css === appList[j].css ? equals : false;
204                                 equals = applications[j].iconPath === appList[j].iconPath ? equals : false;
205                         }
206                 }
207
208                 if (!equals) {
209                         appList = [];
210                         $('#homeScrAppGridView .homeScrAppGridFrame').remove();
211
212                         for (i = 0; i < applications.length; i++) {
213                                 insertAppFrame(applications[i]);
214                         }
215                 }
216         } catch (exc) {
217                 console.log(exc.message);
218         } finally {
219                 //Workaround due to https://bugs.tizen.org/jira/browse/TIVI-2018
220                 window.setTimeout(function() {
221                         evalInstalledApps();
222                 }, 1000);
223
224                 if (null === listenerID) {
225                         listenerID = tizen.application.addAppInfoEventListener({
226                                 oninstalled: function(appInfo) {
227                                         console.log('The application ' + appInfo.name + ' is installed');
228                                         evalInstalledApps();
229                                 },
230                                 onupdated: function(appInfo) {
231                                         console.log('The application ' + appInfo.name + ' is updated');
232                                         evalInstalledApps();
233                                 },
234                                 onuninstalled: function(appid) {
235                                         console.log('The application ' + appid + ' is uninstalled');
236                                         evalInstalledApps();
237                                 }
238                         });
239                 }
240         }
241 }
242
243 /**
244  * Listener for installed apps events.
245  * @method evalInstalledApps
246  * @static
247  **/
248 evalInstalledApps = function() {
249         "use strict";
250         if (typeof tizen !== 'undefined') {
251                 try {
252                         // get the installed applications list
253                         tizen.application.getAppsInfo(onAppInfoSuccess, function(err) {
254                                 // Workaround due to https://bugs.tizen.org/jira/browse/TIVI-2018
255                                 window.setTimeout(function() {
256                                         evalInstalledApps();
257                                 }, 1000);
258
259                                 onError(err);
260                         });
261                 } catch (exc) {
262                         console.error(exc.message);
263                 }
264         }
265 };