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 = app.appInfo.packageId + ".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 $("#btn-start").bind("vclick", function(){
32 if(gLocalMessagePort) {
33 showAlert("Already running.");
34 } else if(isStarting){
35 showAlert("Now starting...");
42 $("#btn-stop").bind("vclick", function(){
43 if(gRemoteMessagePort) {
46 showAlert("Not running.");
50 $("#btn-clear").bind("vclick", function(){
51 $("#logs").empty().listview("refresh");
54 $(window).on('tizenhwkey', function (e) {
55 if (e.originalEvent.keyName === "back") {
56 if ($.mobile.activePage.attr('id') === 'main') {
57 tizen.application.getCurrentApplication().exit();
65 function startMessagePort() {
67 gLocalMessagePort = tizen.messageport.requestLocalMessagePort(gLocalMessagePortName);
68 gLocalMessagePortWatchId = gLocalMessagePort.addMessagePortListener( function(data, remote) {
69 onReceive(data, remote);
72 writeToScreen(e.name);
76 gRemoteMessagePort = tizen.messageport.requestRemoteMessagePort(gServiceAppId, gServicePortName);
78 writeToScreen(e.name);
83 sendCommand("connect");
86 function sendCommand(command){
87 var jsondata = '{"command" : "' + command + '"}';
89 gRemoteMessagePort.sendMessage([ { key:"command", value:command } ], gLocalMessagePort);
90 writeToScreen("Sending: " + command);
93 function onReceive(data, remote) {
97 if(data[i].key == "server")
98 message = data[i].value;
101 writeToScreen("Received : " + message);
103 if(message == "WELCOME"){
104 sendCommand("start");
105 }else if(message == "stopped"){
107 }else if(message == "exit"){
108 if(gRemoteMessagePort)
109 gRemoteMessagePort = null;
110 if(gLocalMessagePort) {
111 gLocalMessagePort.removeMessagePortListener(gLocalMessagePortWatchId);
112 gLocalMessagePort = null;
117 function writeToScreen(message) {
118 var today = new Date(),
119 time = today.getFullYear() + "-" + (today.getMonth() + 1) + "-" + today.getDate() + " "
120 + today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds() + "." + today.getMilliseconds(),
121 str = '<li class="ui-li-has-multiline ui-li-text-ellipsis">'
123 + '<span class="ui-li-text-sub">'
127 $("#logs").append(str).listview("refresh");
132 tizen.application.getAppsContext(onGetAppsContextSuccess, onGetAppsContextError);
134 writeToScreen("Get AppContext Error");
138 function onGetAppsContextSuccess(contexts) {
139 for (var i = 0; i < contexts.length; i++) {
140 var appInfo = tizen.application.getAppInfo(contexts[i].appId);
141 if(appInfo.id == gServiceAppId){
142 console.log("Running Service App found");
146 if (i >= contexts.length) {
147 console.log("Running Service App not found. Trying to launch it");
149 //listInstalledApps();
155 function onGetAppsContextError(err) {
156 console.log("getAppsContext exc");
159 function listInstalledApps() {
161 tizen.application.getAppsInfo(getAppsInfoSuccessCB, getAppsInfoErrorCB);
163 writeToScreen("Get Installed App Info Error");
167 function getAppsInfoSuccessCB(apps) {
168 for (var i = 0; i < apps.length; i++) {
169 if(apps[i].id == gServiceAppId){
170 console.log("Found installed Service App")
174 if(i >= apps.length){
175 writeToScreen("Service App not installed");
182 function getAppsInfoErrorCB(err) {
183 console.log("getAppsInfo failed");
187 function launchServiceApp() {
188 function onSuccess() {
189 console.log("Service App launched successfully!");
190 console.log("Restart...");
194 function onError(err) {
195 console.log("Service Applaunch failed");
197 showAlert("Failed to launch HybridServiceApp!");
201 console.log("Launching [" + gServiceAppId + "] ...");
202 tizen.application.launch(gServiceAppId, onSuccess, onError);
204 showAlert("launch exc:" + exc.message);
208 function showAlert(message) {
209 var alertPopup = $("#alert-popup");
210 alertPopup.find("#message").text(message);
211 alertPopup.popup("open", {positionTo: "window"});