[HybridWebApp]update HybridWebApp(tizen_2.1)
[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://floralicense.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 gServicePortName = "SAMPLE_PORT";
20 var gLocalMessagePortName = "SAMPLE_PORT_REPLY";
21
22 var gLocalMessagePort;
23 var gRemoteMessagePort;
24
25 var gLocalMessagePortWatchId;
26
27 var isStarting = false;
28
29 $(document).delegate("#main", "pageinit", function() {
30         $("#main .ui-btn-back").bind("vclick", function() {
31                 tizen.application.getCurrentApplication().exit();
32                 return false;
33         });
34
35         $("#btn-start").bind("vclick", function(){
36
37                 if(gLocalMessagePort) {
38                         alert("Already Started");
39                 } else if(isStarting){
40                         alert("Now starting...");
41                 } else {
42                         isStarting = true;
43                         start();
44                 }
45                 return false;
46         });
47         $("#btn-stop").bind("vclick", function(){
48                 if(gRemoteMessagePort) {
49                         sendCommand("stop");
50                 } else {
51                         alert("Not Started");
52                 }
53                 return false;
54         });
55         $("#btn-clear").bind("vclick", function(){
56                 $("#logs").empty().listview("refresh");
57                 return false;
58         });
59 });
60
61 function startMessagePort() {
62         try {
63                 gLocalMessagePort = tizen.messageport.requestLocalMessagePort(gLocalMessagePortName);
64                 gLocalMessagePortWatchId = gLocalMessagePort.addMessagePortListener( function(data, remote) {
65                         onReceive(data, remote);
66                 });
67         } catch (e) {
68                 writeToScreen(e.name);
69         }
70
71         try {
72                 gRemoteMessagePort = tizen.messageport.requestRemoteMessagePort(gServiceAppId, gServicePortName);
73         } catch (e) {
74                 writeToScreen(e.name);
75         }
76
77         isStarting = false;
78
79         sendCommand("connect");
80 }
81
82 function sendCommand(command){
83         var jsondata = '{"command" : "' + command + '"}';
84
85         gRemoteMessagePort.sendMessage([ { key:"command", value:command } ], gLocalMessagePort);
86         writeToScreen("Sending: " + command);
87 }
88
89 function onReceive(data, remote) {
90         var message;
91
92         for(var i in data) {
93                 if(data[i].key == "server")
94                         message = data[i].value;
95         }
96
97         writeToScreen("Received : " + message);
98
99         if(message == "WELCOME"){
100                 sendCommand("start");
101         }else if(message == "stopped"){
102                 sendCommand("exit");
103         }else if(message == "exit"){
104                 if(gRemoteMessagePort)
105                         gRemoteMessagePort = null;
106                 if(gLocalMessagePort) {
107                         gLocalMessagePort.removeMessagePortListener(gLocalMessagePortWatchId);
108                         gLocalMessagePort = null;
109                 }
110         }
111 }
112
113 function writeToScreen(message) {
114         var today = new Date(),
115                 time = today.getFullYear() + "-" + (today.getMonth() + 1) + "-" + today.getDate() + " "
116                                 + today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds() + "." + today.getMilliseconds(),
117                 str = '<li class="ui-li-has-multiline ui-li-text-ellipsis">'
118                                 + message
119                                 + '<span class="ui-li-text-sub">'
120                                 + time
121                                 + '</span></li>';
122
123         $("#logs").append(str).listview("refresh");
124 }
125
126 function start() {
127         try {
128                 tizen.application.getAppsContext(onGetAppsContextSuccess, onGetAppsContextError);
129         } catch (exc) {
130                 writeToScreen("Get AppContext Error");
131         }
132 }
133
134 function onGetAppsContextSuccess(contexts) {
135         for (var i = 0; i < contexts.length; i++) {
136                 var appInfo = tizen.application.getAppInfo(contexts[i].appId);
137                 if(appInfo.id == gServiceAppId){
138                         console.log("Running Service App found");
139                         break;
140                 }
141         }
142         if (i >= contexts.length) {
143                 console.log("Running Service App not found. Trying to launch it");
144                 launchServiceApp();
145                 //listInstalledApps();
146         }else{
147                 startMessagePort();
148         }
149 }
150
151 function onGetAppsContextError(err) {
152         console.log("getAppsContext exc");
153 }
154
155 function listInstalledApps() {
156         try {
157                 tizen.application.getAppsInfo(getAppsInfoSuccessCB, getAppsInfoErrorCB);
158         } catch (exc) {
159                 writeToScreen("Get Installed App Info Error");
160         }
161 }
162
163 function getAppsInfoSuccessCB(apps) {
164         for (var i = 0; i < apps.length; i++) {
165                 if(apps[i].id == gServiceAppId){
166                         console.log("Found installed Service App")
167                         break;
168                 }
169         }
170         if(i >= apps.length){
171                 writeToScreen("Service App not installed");
172                 isStarting = false;
173                 return;
174         }
175         launchServiceApp();
176 }
177
178 function getAppsInfoErrorCB(err) {
179         console.log("getAppsInfo failed");
180         isStarting = false;
181 }
182
183 function launchServiceApp() {
184         function onSuccess() {
185                 console.log("Service App launched successfully!");
186                 console.log("Restart...");
187                 start();
188         }
189
190         function onError(err) {
191                 console.log("Service Applaunch failed");
192                 isStarting = false;
193         }
194
195         try {
196                 console.log("Launching [" + gServiceAppId + "] ...");
197                 tizen.application.launch(gServiceAppId, onSuccess, onError);
198         } catch (exc) {
199                 alert("launch exc:" + exc.message);
200         }
201 }