Page Example

SyncAdapterService Sample Overview

Mobile native

The Sync Adapter Service App sample application demonstrates how you can create an application that acts as a sync adapter to the Sync Adapter App UI application. The service application handles all sync requests from the UI application. The Sync Adapter Service App sample demonstrates how the sync callbacks are implemented to handle sync requests.

Prerequisites

  • Sync Adapter Service App's app ID must be org.tizen.syncadapterapp.service, because the Sync Adapter App uses this ID as a fixed value when it communicates with the Sync Adapter Service App. Otherwise, the application control communication does not operate properly.

    In addition, the Sync Adapter Service App package name must be org.tizen.syncadapterapp (same as the Sync Adapter App).

  • The Sync Adapter App and Sync Adapter Service App packages must be coupled during the build process. To couple the packages:
    1. On the Tizen IDE, right-click the Sync Adapter App project and select Properties.
    2. Select Project References and find Sync Adapter Service App.
    3. Select the Sync Adapter Service App check box and click OK.

    In the Project Explorer view, a with the Sync Adapter App message appears next to the Sync Adapter Service App project name showing that you have coupled it successfully.

  • To ensure proper application execution, the following privileges must be set:

    • http://tizen.org/privilege/account.read
    • http://tizen.org/privilege/account.write
    • http://tizen.org/privilege/appmanager.launch
    • http://tizen.org/privilege/content.write
    • http://tizen.org/privilege/packagemanager.info

Implementation

Application Initialization

Register the service application as a sync adapter by passing the sync callbacks to the sync_adapter_set_callbacks() function:

bool 
service_app_create(void *data)
{
   dlog_print(DLOG_INFO, LOG_TAG, "service_app_create called");

   sync_adapter_set_callbacks(handleStartSync, handleStopSync);

   return true;
}

Sync Start

Whenever the sync manager schedules a sync job for the Sync Adapter App UI application, the following callback is triggered to perform a sync job. The Sync Adapter Service App communicates the information to the Sync Adapter App UI application using an application control.

static bool 
handleStartSync(account_h account, const char *sync_job_name, const char *sync_capability, bundle *sync_job_user_data)
{
   dlog_print(DLOG_INFO, LOG_TAG, "HandleStartSync called in service app");

   bool is_data_change = sync_capability ? true : false;

   if (sync_job_name)
      dlog_print(DLOG_INFO, LOG_TAG, "Sync Job Name [%s]", sync_job_name);
   else
      dlog_print(DLOG_INFO, LOG_TAG, "Data Change Sync Job Capability [%s]", sync_capability);

   if (account)
   {
      int id;
      account_get_account_id(account, &id);

      dlog_print(DLOG_INFO, LOG_TAG, "account [%d]", id);
   } 
   else
      dlog_print(DLOG_INFO, LOG_TAG, "accountless case");

   app_control_h app_control;
   int ret = app_control_create(&app_control);
   ret = app_control_set_app_id(app_control, SYNC_ADAPTER_APP_ID);
   if (ret == APP_CONTROL_ERROR_NONE)
      dlog_print(DLOG_INFO, LOG_TAG, "Launching sync UI app successfully [%s]", SYNC_ADAPTER_APP_ID);
   else
      dlog_print(DLOG_INFO, LOG_TAG, "Launching sync UI app failed [%s]", get_error_message(ret));

   if (is_data_change) 
   {
      ret = app_control_set_operation(app_control, "http://tizen.org/appcontrol/operation/data_change_sync_complete");
   } 
   else 
   {
      bool is_periodic = !strcmp(sync_job_name, "Periodic") ? true : false;
      if (is_periodic)
         ret = app_control_set_operation(app_control, "http://tizen.org/appcontrol/operation/periodic_sync_complete");
      else
         ret = app_control_set_operation(app_control, "http://tizen.org/appcontrol/operation/on_demand_sync_complete");
   }
   dlog_print(DLOG_INFO, LOG_TAG, "sync operation is completed");

   app_control_send_launch_request(app_control, NULL, NULL);

   return true;
}

Sync Stop

To safely stop and cancel any ongoing sync jobs, use the following callback function:

static void 
handleStopSync(account_h account, const char *capability)
{
   dlog_print(DLOG_INFO, LOG_TAG, "HandleStopSync called in service app");

   int id;
   account_get_account_id(account, &id);
   dlog_print(DLOG_INFO, LOG_TAG, "details %d, %s ", id, capability);
   dlog_print(DLOG_INFO, LOG_TAG, "HandleStopSync called in client");
}