/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using Tizen.Applications;
using Tizen.Account.AccountManager;
namespace Tizen.Account.SyncManager
{
///
/// This class contains the delegates to be called upon scheduling a sync operation.
///
/// 4
public class SyncAdapter
{
Interop.Adapter.SyncAdapterStartSyncCallback _startSyncCallback;
Interop.Adapter.SyncAdapterCancelSyncCallback _cancelSyncCallback;
///
/// The callback function for the sync adapter's start sync request.
///
/// 4
/// The sync job parameters corresponding to the sync request.
/// true if the sync operation is success, @c false otherwise.
public delegate bool StartSyncCallback(SyncJobData syncParameters);
///
/// The callback function for the sync adapter's cancel sync request.
///
/// 4
/// The sync job parameters corresponding to the sync request.
public delegate void CancelSyncCallback(SyncJobData syncParameters);
///
/// Sets the client (sync adapter) callback functions.
///
/// 4
/// A callback function to be called by the sync manager for performing the sync operation.
/// A callback function to be called by the sync manager for cancelling the sync operation.
/// Thrown when any of the arguments are null.
/// Thrown when the application calling this API cannot be a sync adapter.
public void SetSyncEventCallbacks(StartSyncCallback startSyncCb, CancelSyncCallback cancelSyncCb)
{
if (startSyncCb == null || cancelSyncCb == null)
{
throw new ArgumentNullException();
}
_startSyncCallback = (IntPtr accountHandle, string syncJobName, string syncCapability, IntPtr syncJobUserData) =>
{
Log.Debug(ErrorFactory.LogTag, "Start sync event received");
AccountManager.Account account = new AccountManager.Account(new SafeAccountHandle(accountHandle, true));
Bundle bundle = new Bundle(new SafeBundleHandle(syncJobUserData, true));
SyncJobData syncJobData = new SyncJobData();
syncJobData.Account = account;
if (syncJobName == null)
syncJobData.SyncJobName = syncCapability;
else
syncJobData.SyncJobName = syncJobName;
syncJobData.UserData = bundle;
return startSyncCb(syncJobData);
};
_cancelSyncCallback = (IntPtr accountHandle, string syncJobName, string syncCapability, IntPtr syncJobUserData) =>
{
Log.Debug(ErrorFactory.LogTag, "cancel sync event received");
AccountManager.Account account = new AccountManager.Account(new SafeAccountHandle(accountHandle, true));
Bundle bundle = new Bundle(new SafeBundleHandle(syncJobUserData, true));
SyncJobData syncJobData = new SyncJobData();
syncJobData.Account = account;
if (syncJobName == null)
syncJobData.SyncJobName = syncCapability;
else
syncJobData.SyncJobName = syncJobName;
syncJobData.UserData = bundle;
cancelSyncCb(syncJobData);
};
int ret = Interop.Adapter.SetCallbacks(_startSyncCallback, _cancelSyncCallback);
if (ret != (int)SyncManagerErrorCode.None)
{
Log.Error(ErrorFactory.LogTag, "Failed to set callbacks");
throw ErrorFactory.GetException(ret);
}
}
///
/// Unsets the client (sync adapter) callback functions.
///
/// 4
/// Thrown when sync manager internal error occurs.
public void UnsetSyncEventCallbacks()
{
int ret = Interop.Adapter.UnsetCallbacks();
if (ret != (int)SyncManagerErrorCode.None)
{
Log.Error(ErrorFactory.LogTag, "Failed to unset callbacks");
throw ErrorFactory.GetException(ret);
}
}
}
}