2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (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
8 * http://floralicense.org/license
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.
17 var app = tizen.application.getCurrentApplication();
18 var gServiceAppId = "r9vrpxzuyp.HybridServiceApp";
19 var gServicePortName = "SAMPLE_PORT";
20 var gLocalMessagePortName = "SAMPLE_PORT_REPLY";
22 var gLocalMessagePort;
23 var gRemoteMessagePort;
25 var gLocalMessagePortWatchId;
27 var isStarting = false;
29 $(document).delegate("#main", "pageinit", function() {
30 $("#main .ui-btn-back").bind("vclick", function() {
31 tizen.application.getCurrentApplication().exit();
35 $("#btn-start").bind("vclick", function(){
37 if(gLocalMessagePort) {
38 alert("Already Started");
39 } else if(isStarting){
40 alert("Now starting...");
47 $("#btn-stop").bind("vclick", function(){
48 if(gRemoteMessagePort) {
55 $("#btn-clear").bind("vclick", function(){
56 $("#logs").empty().listview("refresh");
61 function startMessagePort() {
63 gLocalMessagePort = tizen.messageport.requestLocalMessagePort(gLocalMessagePortName);
64 gLocalMessagePortWatchId = gLocalMessagePort.addMessagePortListener( function(data, remote) {
65 onReceive(data, remote);
68 writeToScreen(e.name);
72 gRemoteMessagePort = tizen.messageport.requestRemoteMessagePort(gServiceAppId, gServicePortName);
74 writeToScreen(e.name);
79 sendCommand("connect");
82 function sendCommand(command){
83 var jsondata = '{"command" : "' + command + '"}';
85 gRemoteMessagePort.sendMessage([ { key:"command", value:command } ], gLocalMessagePort);
86 writeToScreen("Sending: " + command);
89 function onReceive(data, remote) {
93 if(data[i].key == "server")
94 message = data[i].value;
97 writeToScreen("Received : " + message);
99 if(message == "WELCOME"){
100 sendCommand("start");
101 }else if(message == "stopped"){
103 }else if(message == "exit"){
104 if(gRemoteMessagePort)
105 gRemoteMessagePort = null;
106 if(gLocalMessagePort) {
107 gLocalMessagePort.removeMessagePortListener(gLocalMessagePortWatchId);
108 gLocalMessagePort = null;
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">'
119 + '<span class="ui-li-text-sub">'
123 $("#logs").append(str).listview("refresh");
128 tizen.application.getAppsContext(onGetAppsContextSuccess, onGetAppsContextError);
130 writeToScreen("Get AppContext Error");
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");
142 if (i >= contexts.length) {
143 console.log("Running Service App not found. Trying to launch it");
145 //listInstalledApps();
151 function onGetAppsContextError(err) {
152 console.log("getAppsContext exc");
155 function listInstalledApps() {
157 tizen.application.getAppsInfo(getAppsInfoSuccessCB, getAppsInfoErrorCB);
159 writeToScreen("Get Installed App Info Error");
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")
170 if(i >= apps.length){
171 writeToScreen("Service App not installed");
178 function getAppsInfoErrorCB(err) {
179 console.log("getAppsInfo failed");
183 function launchServiceApp() {
184 function onSuccess() {
185 console.log("Service App launched successfully!");
186 console.log("Restart...");
190 function onError(err) {
191 console.log("Service Applaunch failed");
196 console.log("Launching [" + gServiceAppId + "] ...");
197 tizen.application.launch(gServiceAppId, onSuccess, onError);
199 alert("launch exc:" + exc.message);