/*
* 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 System.Collections.Generic;
using System.Threading.Tasks;
namespace Tizen.Maps
{
///
/// Base class for a map service request.
///
/// 3
///
public abstract class MapServiceRequest : IDisposable
{
internal TaskCompletionSource> _requestTask;
internal string errMessage;
internal int? _requestID;
internal ServiceRequestType _type;
internal Action startExecutionAction;
internal Interop.ErrorCode errorCode;
internal MapService _service;
///
/// Creates a map service request.
///
/// Map service object.
/// Request type.
internal MapServiceRequest(MapService service, ServiceRequestType type)
{
_service = service;
_type = type;
}
///
/// Destroy the MapServiceRequest object.
///
~MapServiceRequest()
{
Dispose(false);
}
///
/// Sends a request to the map service provider.
///
/// 3
/// Response from the map service provider.
/// http://tizen.org/privilege/mapservice
/// http://tizen.org/privilege/internet
/// http://tizen.org/privilege/network.get
/// Thrown when the required feature is not supported.
/// Thrown when application does not have some privilege to access this method.
/// Thrown when the result is invalid.
/// Thrown when arguments are invalid.
public async Task> GetResponseAsync()
{
IEnumerable task = null;
if (_requestTask == null || _requestTask.Task.IsCanceled)
{
_requestTask = new TaskCompletionSource>();
startExecutionAction();
task = await _requestTask.Task.ConfigureAwait(false);
}
errorCode.WarnIfFailed(errMessage);
return task;
}
internal void Cancel()
{
if (_requestTask?.Task.IsCompleted == false)
{
_requestTask?.SetCanceled();
if (_requestID != null)
{
var err = Interop.CancelRequest(_service.handle, (int)_requestID);
err.ThrowIfFailed($"Unable to cancel service request, Type: {_type}, ID: {_requestID}");
}
errorCode = Interop.ErrorCode.Canceled;
}
}
#region IDisposable Support
private bool _disposedValue = false;
///
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
///
/// If true, managed and unmanaged resources can be disposed, otherwise only unmanaged resources can be disposed.
/// 3
protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
Cancel();
}
_disposedValue = true;
}
}
///
/// Releases all the resources used by this object.
///
/// 3
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
}