How to retrieve and process the data in launched application using the application control interface

How to retrieve and process the data in launched application using the application control interface

In this tutorial, we’ll learn how to retrieve the data in the launched or provider application, process it and give back the control (with the processed data) to the requested application using the Tizen’s “Application” API’s application control interface. Application control allows you to launch other applications, whose functionalities are needed in your application.

For more details on different types of application control requests, refer to Application control. The below example uses the explicit launch method.

The base application which needs the services of another application will be doing something like below,

var appControl =  new tizen.ApplicationControl(
        "http://tizen.org/appcontrol/operation/share",
        "shareimage.html",
        "image/*",
         null,
        [new tizen.ApplicationControlData("images", [imagedata1, imagedata2])] ); 

	//Explicit launch with control data
	tizen.application.launchAppControl( appControl, "3lqX1bKgbi.ServiceApp",
	function() {console.log("launch application control succeed"); },
	function(e) {console.log("launch application control failed. reason: " + e.message); },
	appControlReplyCallback );

The appControlReplyCallback is the callback interface specifying the success or failure callbacks that are invoked as a reply from the requested application control.

var appControlReplyCallback = {
	    onsuccess: function(data) {
	    	console.log('Received reply from the provider app..');
	    	for (var i = 0; i < data.length; i++) {
	           console.log("#" + i + " key:" + data[i].key);
	           for (var j = 0; j < data[i].value.length; j++) {
	              console.log("   value#" + j + ":"+data[i].value[j]);
	           }
	       }
	    },
	    // Function to handle the failure case
	    onfailure: function() {
	       console.log('The launch application control failed');
	    }
	 }

On successful launch, the control is passed to the provider application. And the provider application has to call the getRequestedAppControl() method of the Application interface to get the reference of the RequestedApplicationControl object that has been passed to it with the control request.

reqAppControl = tizen.application.getCurrentApplication().getRequestedAppControl();

Once you have the reference of the requested application control object, you can use the same object to retrieve the data passed.

//Function which processes the data passed
function processData(appControl) {
   if (reqAppControl) {
     var reqData = reqAppControl.appControl.data;
        for (var i = 0; i < data.length; i++) {
           console.log("#" + i + " key:" + data[i].key);
           for (var j = 0; j < data[i].value.length; j++) {
              console.log("   value#" + j + ":"+data[i].value[j]);
              // Process the data
           }
	 }
    }
}

After processing on the data, we need to pass the control back to the requesting application using the replyResult() method (in case of success) or the replyFailure() method (in case of failure) of the RequestedApplicationControl interface.

 reqAppControl.replyResult(processedData);

Now, the control is passed to the requesting application and the onsuccess function of the appControlReplyCallback callback interface will be invoked.

In the provider application, you should be defining the app-control in config file as below,

<tizen:app-control>
    <tizen:src name="shareimage.html"/>
    <tizen:operation name="http://tizen.org/appcontrol/operation/share"/>
    <tizen:mime name="image/*"/>
</tizen:app-control>

You can also use the read-only shared directory of application to export data to other applications, as every application maintains its own shared directory. Below is how you can retrieve the shared directory URI.

function getSharedDirURI() {
	var sharedDir = tizen.application.getAppSharedURI("kto5jikgul");
	console.log("shared directory : " + sharedDir);
}

The getAppSharedURI method takes the application ID (as the parameter) of the application whose shared directory URI is to be retrieved. And, if the ID is not provided (or set as NULL), the shared directory URI of the calling application will be returned.