Tech tip of this week

Tizen: Launching Applications ( Native)

Application framework starts the application by creating a new process and calling entry point of the application.

The main() function is entry of the application. In the Tizen application, the main task is to hand over control to the application framework by calling the ui_app_main() function





bool app_create(void *user_data)
{
   // Take necessary actions before the main event loop starts
   // Initialize UI resources and application data
   // If this function returns true, the application main loop starts
   // If this function returns false, the application terminates
   return true;
}

void app_control(app_control_h app_control, void *user_data)
{
   // Handle the launch request
}

void app_pause(void *user_data)
{
   // Take necessary actions when application becomes invisible
}

void app_resume(void *user_data)
{
   // Take necessary actions when application becomes visible
}

void app_terminate(void *user_data)
{
   // Release all resources
}

int main(int argc, char *argv[])
{
   struct appdata ad;
 
   ui_app_lifecycle_callback_s event_callback;
     
   event_callback.create = app_create;
   event_callback.terminate = app_terminate;
   event_callback.pause = app_pause;
   event_callback.resume = app_resume;
   event_callback.app_control = app_control;
     
   memset(&ad, 0x0, sizeof(struct appdata));
 
   return ui_app_main(argc, argv, &event_callback, &ad);
}

The App control API provides functions for launching other applications with a specific operation, URI, and MIME type.

To launch an application with the app control API, use one of the following methods:

  • Explicit launch: Launch the application with the application ID.
  • Implicit launch: Launch the application with an operation, URI, or MIME type.
The application launched by the app control can return the result to the caller application

Add below privilege in manifest file.
<privilege>http://tizen.org/privilege/appmanager.launch</privilege>
Explicit Launch:

When you request an explicit launch:If the underlying application launcher framework finds an application matched with the given application ID in the installed application list, it launches the application in a new process.

The following code example launches a calculator application explicitly with the application ID:


#include <app.h>
#include <dlog.h>
 
#define TAG "MY_TAG"

app_control_h app_control;
 
app_control_create(&app_control);
app_control_set_operation(app_control, APP_CONTROL_OPERATION_DEFAULT);
app_control_set_app_id(app_control, "org.tizen.calculator");
 
if (app_control_send_launch_request(app_control, NULL, NULL) == APP_CONTROL_ERROR_NONE) 
{
   dlog_print(DLOG_INFO, TAG, "Succeeded to launch a calculator app.");
} 
else 
{
   dlog_print(DLOG_ERROR, TAG, "Failed to launch a calculator app.");
}
 
app_control_destroy(app_control);

Implicit Launch:

When you request an implicit launch: Only 3 data categories are used to determine which application can be launched: Operation, URI scheme, and MIME type.

The application launcher framework iterates the desktop files of installed applications on the device to find applications where the 3 categories are exactly matched.

If only one application is matched for the given categories, that application is launched. If multiple matching applications are found, the application selector is shown and the user can select the proper application.

The following code example launches a camera application with the operation and MIME type:
#include <app.h>
#include <dlog.h>

#define TAG "MY_TAG"

app_control_h app_control;
 
app_control_create(&app_control);
app_control_set_operation(app_control, APP_CONTROL_OPERATION_CREATE_CONTENT);
app_control_set_mime(app_control, "image/jpg");
if (app_control_send_launch_request(app_control, NULL, NULL) == APP_CONTROL_ERROR_NONE) 
{
   dlog_print(DLOG_INFO, TAG, "Succeeded to launch a viewer app.");
} 
else 
{
   dlog_print(DLOG_ERROR, TAG, "Failed to launch a viewer app.");
}
 
app_control_destroy(app_control);


The following code example launches a gallery application with the operation, URI, and MIME type:
#include <app.h>
#include <dlog.h>

#define TAG "MY_TAG"
 
app_control_h app_control;
 
app_control_create(&app_control);
app_control_set_operation(app_control, APP_CONTROL_OPERATION_VIEW);
app_control_set_uri(app_control, "file:///home/myhome/Photos/1_photo.jpg");
app_control_set_mime(app_control, "image/*");
 
if (app_control_send_launch_request(app_control, NULL, NULL) == APP_CONTROL_ERROR_NONE) 
{
   dlog_print(DLOG_INFO, TAG, "Succeeded to launch a viewer app.");
} 
else 
{
   dlog_print(DLOG_ERROR, TAG, "Failed to launch a viewer app.");
}
 
app_control_destroy(app_control);

How to get Results Back:

The app control result from the requested application is delivered to the caller application in the app_control handle with extra data.

The following code example gets the result of an app control request by implementing an app_control result callback:
#include <app.h>
#include <dlog.h>
 
#define TAG "MY_TAG"
 
// Callback function to get result
static void app_control_result(app_control_h request, app_control_h reply, app_control_result_e result, void *user_data) 
{
   char *value;
 
   if (result == APP_CONTROL_RESULT_SUCCEEDED) 
   {
      if (app_control_get_extra_data(reply, APP_CONTROL_DATA_SELECTED, &value) == APP_CONTROL_ERROR_NONE)
      {
         dlog_print(DLOG_INFO, TAG, "[app_control_result_cb] Succeeded: value(%s)", value);
      } 
      else 
      {
         dlog_print(DLOG_ERROR, TAG, "[app_control_result_cb] Failed");
      }
 
   } 
   else 
   {
      dlog_print(DLOG_ERROR, TAG, "[app_control_result_cb] APP_CONTROL_RESULT_FAILED.");
   }
}

1 comment:

  1. Can you guide me, how to open home app?
    I mean, it should minimize all open apps.

    ReplyDelete