SyncAdapterService(W) Sample Overview

Wearable native

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

Prerequisites

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

    In addition, the Sync Adapter Service package name must be org.example.syncadapter (same as the Sync Adapter).

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

    In the Project Explorer view, a with SyncAdapter message appears next to the Sync Adapter Service 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:

/*
 * App create callback
 */
bool
service_app_create(void *data)
{
   dlog_print(DLOG_INFO, LOG_TAG, "Sync Adapter Service : service_app_create is invoked");
   sync_adapter_set_callbacks(handle_start_sync, handle_stop_sync);
   return true;
}

Sync Start

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

/*
 * Callback for receiving the result of sync start operation
 * It is invoked in the following cases
 * - sync_manager_on_demand_sync_job() which was called in Sync Adapter is complete
 * - In the case of using sync_manager_add_periodic_sync_job() in Sync Adapter,
 *   it is called after some times which is set as period interval later
 * - In the case of using sync_manager_add_data_change_sync_job() in Sync Adapter,
 *   it is called when status change is detected in DB for specific capability
 */
static bool
handle_start_sync(account_h account, const char *sync_job_name, const char *sync_capability, bundle *sync_job_user_data)
{
   dlog_print(DLOG_INFO, LOG_TAG, "handle_start_sync() is invoked in service application");

   /* Account handle which is transfered from the Sync Adapter */
   if (account) {
      account_get_account_id(account, &account_id);
      dlog_print(DLOG_INFO, LOG_TAG, "Account ID [%d]", account_id);
   } else {
      dlog_print(DLOG_INFO, LOG_TAG, "Accountless case");
   }

   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);

   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, "Sync Adapter application ID is set as [%s]", SYNC_ADAPTER_APP_ID);
   else
      dlog_print(DLOG_INFO, LOG_TAG, "app_control_set_app_id() is failed [%d : %s]", ret, get_error_message(ret));

   /*
    * If there is sync_capability instead of sync_job_name,
    * this calling is for notifying data change sync operation is over to Sync Adapter Service
    */
   bool is_data_change = sync_capability ? true : false;

   if (is_data_change) {
      ret = app_control_set_operation(app_control, "http://tizen.org/appcontrol/operation/data_change_sync_complete");
   } else {
      /*
       * If there is sync_job_name instead of sync_capability,
       * this calling is for notifying on demand or periodic sync operation is over to Sync Adapter Service
       */
      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");
   }

   /*
    * Insert your code for data synchronization between this application with server
    * For this application, Deliver the result of sync operation to Sync Adapter
    * for verifying proper operation of sync-manager APIs by using App Control handle
    */
   app_control_send_launch_request(app_control, NULL, NULL);

   dlog_print(DLOG_INFO, LOG_TAG, "Sync start operation is completed");

   return true;
}

Sync Stop

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

/*
 * Callback for receiving the result of sync stop operation
 */
static void
handle_stop_sync(account_h account, const char *sync_job_name, const char *sync_capability, bundle *sync_job_user_data)
{
   dlog_print(DLOG_INFO, LOG_TAG, "handle_stop_sync() is invoked in service application");

   if (account) {
      account_get_account_id(account, &account_id);
      dlog_print(DLOG_INFO, LOG_TAG, "Account ID [%d]", account_id);
   } else {
      dlog_print(DLOG_INFO, LOG_TAG, "Accountless case");
   }

   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);

   /*
    * Insert your code for the case of canceling sync operation
    * This callback can receive the result of what sync jobs is canceled
    */

   dlog_print(DLOG_INFO, LOG_TAG, "Sync stop operation is complete");
}