Tizen 2.0 Release
[samples/web/HybridWebApp.git] / js / main.js
1 /*
2  *      Copyright 2012  Samsung Electronics Co., Ltd
3  *
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
7  *
8  *              http://www.tizenopensource.org/license
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 var app = tizen.application.getCurrentApplication();
18 var gServiceAppId = "r9vrpxzuyp.HybridServiceApp";
19 var gWsUri = "ws://127.0.0.1:8080/ipc?src=" + app.appInfo.id + ".HybridWebApp&dest=r9vrpxzuyp.HybridServiceApp";
20 var gWebSocket;
21 var isStarting = false;
22
23 $(document).delegate("#main", "pageinit", function() {
24         $("#main .ui-btn-back").bind("vclick", function() {
25                 tizen.application.getCurrentApplication().exit();
26                 return false;
27         });
28
29         $("#btn-start").bind("vclick", function(){
30
31                 if(gWebSocket) {
32                         alert("Already Started");
33                 } else if(isStarting){
34                         alert("Now starting...");
35                 } else {
36                         isStarting = true;
37                         start();
38                 }
39                 return false;
40         });
41         $("#btn-stop").bind("vclick", function(){
42                 if(gWebSocket) {
43                         sendCommand("stop");
44                 } else {
45                         alert("Not Started");
46                 }
47                 return false;
48         });
49         $("#btn-clear").bind("vclick", function(){
50                 $("#logs").empty().listview("refresh");
51                 return false;
52         });
53 });
54
55 function startWebSocket() {
56         gWebSocket = new WebSocket(gWsUri);
57         isStarting = false;
58
59         gWebSocket.onopen = function(evt) {
60                 onOpen(evt);
61         };
62         gWebSocket.onclose = function(evt) {
63                 onClose(evt);
64         };
65         gWebSocket.onmessage = function(evt) {
66                 onMessage(evt);
67         };
68         gWebSocket.onerror = function(evt) {
69                 onError(evt);
70         };
71 }
72
73 function sendCommand(command){
74         var jsondata = '{"command" : "' + command + '"}';
75
76         gWebSocket.send(JSON.stringify(jsondata));
77         writeToScreen("Sending: " + command);
78 }
79
80 function onOpen(evt) {
81         writeToScreen("Ready");
82         sendCommand("connect");
83 }
84
85 function onClose(evt) {
86         writeToScreen("Disconnected");
87         gWebSocket = null;
88 }
89
90 function onMessage(evt) {
91         var message = JSON.parse(evt.data).server;
92
93         writeToScreen("Received : " + message);
94
95         if(message == "WELCOME"){
96                 sendCommand("start");
97         }else if(message == "stopped"){
98                 sendCommand("exit");
99                 gWebSocket.close();
100         }else if(message == "exit"){
101                 gWebSocket.close();
102         }
103 }
104
105 function onError(evt) {
106         writeToScreen("ERROR:" + evt.data);
107 }
108
109 function writeToScreen(message) {
110         var today = new Date(),
111                 time = today.getFullYear() + "-" + today.getMonth() + "-" + today.getDate() + " "
112                                 + today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds() + "." + today.getMilliseconds(),
113                 str = '<li class="ui-li-has-multiline ui-li-text-ellipsis">'
114                                 + message
115                                 + '<span class="ui-li-text-sub">'
116                                 + time
117                                 + '</span></li>';
118
119         $("#logs").append(str).listview("refresh");
120 }
121
122 function start() {
123         try {
124                 tizen.application.getAppsContext(onGetAppsContextSuccess, onGetAppsContextError);
125         } catch (exc) {
126                 writeToScreen("Get AppContext Error");
127         }
128 }
129
130 function onGetAppsContextSuccess(contexts) {
131         for (var i = 0; i < contexts.length; i++) {
132                 var appInfo = tizen.application.getAppInfo(contexts[i].appId);
133                 console.log(appInfo.id);
134                 if(appInfo.id == gServiceAppId){
135                         console.log("Running Service App found");
136                         break;
137                 }
138         }
139         if (i >= contexts.length) {
140                 console.log("Running Service App not found. Trying to launch it");
141                 listInstalledApps();
142         }else{
143                 startWebSocket();
144         }
145 }
146
147 function onGetAppsContextError(err) {
148         console.log("getAppsContext exc");
149 }
150
151 function listInstalledApps() {
152         try {
153                 tizen.application.getAppsInfo(getAppsInfoSuccessCB, getAppsInfoErrorCB);
154         } catch (exc) {
155                 writeToScreen("Get Installed App Info Error");
156         }
157 }
158
159 function getAppsInfoSuccessCB(apps) {
160         for (var i = 0; i < apps.length; i++) {
161                 if(apps[i].id == gServiceAppId){
162                         console.log("Found installed Service App")
163                         break;
164                 }
165         }
166         if(i >= apps.length){
167                 writeToScreen("Service App not installed");
168                 return;
169         }
170         launchServiceApp();
171 }
172
173 function getAppsInfoErrorCB(err) {
174         console.log("getAppsInfo failed");
175 }
176
177 function launchServiceApp(){
178         function onSuccess() {
179                 console.log("Service App launched successfully!");
180                 console.log("Restart...");
181                 start();
182         }
183
184         function onError(err) {
185                 console.log("Service Applaunch failed");
186         }
187
188         try {
189                 tizen.application.launch(gServiceAppId, onSuccess, onError);
190         } catch (exc) {
191                 alert("launch exc:" + exc.message);
192         }
193 }