/*
* Copyright (c) 2018 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 Tizen.Applications;
using System;
using System.Collections.Generic;
using Native = Interop.MediaControllerServer;
namespace Tizen.Multimedia.Remoting
{
public static partial class MediaControlServer
{
private static Native.PlaybackActionCommandReceivedCallback _playbackActionCommandCallback;
private static Native.PlaybackPositionCommandReceivedCallback _playbackPositionCommandCallback;
private static Native.PlaylistCommandReceivedCallback _playlistCommandCallback;
private static Native.ShuffleModeCommandReceivedCallback _shuffleModeCommandCallback;
private static Native.RepeatModeCommandReceivedCallback _repeatModeCommandCallback;
private static Native.SimpleCommandReceivedCallback _subtitleModeCommandCallback;
private static Native.SimpleCommandReceivedCallback _mode360CommandCallback;
private static Native.DisplayModeCommandReceivedCallback _displayModeCommandCallback;
private static Native.DisplayRotationCommandReceivedCallback _displayRotationCommandCallback;
private static Native.CustomCommandReceivedCallback _customCommandCallback;
private static Native.SearchCommandReceivedCallback _searchCommandCallback;
private static Native.CommandCompletedCallback _commandCompletedCallback;
///
/// Occurs when a client sends playback command.
///
/// 4
[Obsolete("Please do not use! This will be deprecated. Please use PlaybackActionCommandReceived instead.")]
public static event EventHandler PlaybackCommandReceived;
///
/// Occurs when a client sends playback command.
///
/// 5
public static event EventHandler PlaybackActionCommandReceived;
///
/// Occurs when a client sends playback position command.
///
/// 5
public static event EventHandler PlaybackPositionCommandReceived;
///
/// Occurs when a client sends playlist command.
///
/// 5
public static event EventHandler PlaylistCommandReceived;
///
/// Occurs when a client sends shuffle mode command.
///
/// 5
public static event EventHandler ShuffleModeCommandReceived;
///
/// Occurs when a client sends repeat mode command.
///
/// 5
public static event EventHandler RepeatModeCommandReceived;
///
/// Occurs when a client sends subtitle mode command.
///
/// 6
public static event EventHandler SubtitleModeCommandReceived;
///
/// Occurs when a client sends mode 360 command.
///
/// 6
public static event EventHandler Mode360CommandReceived;
///
/// Occurs when a client sends display mode command.
///
/// 6
public static event EventHandler DisplayModeCommandReceived;
///
/// Occurs when a client sends display rotation command.
///
/// 6
public static event EventHandler DisplayRotationCommandReceived;
///
/// Occurs when a client sends custom command.
///
/// 5
public static event EventHandler CustomCommandReceived;
///
/// Occurs when a client sends search command.
///
/// 5
public static event EventHandler SearchCommandReceived;
///
/// Occurs when a client sends custom command.
///
/// 5
internal static event EventHandler CommandCompleted;
private static void RegisterPlaybackActionCommandReceivedEvent()
{
_playbackActionCommandCallback = (clientName, requestId, playbackCommand, _) =>
{
// SendPlaybackCommand doesn't use requestId. It'll be removed in Level 7.
if (requestId == null)
{
#pragma warning disable CS0618 // Type or member is obsolete
PlaybackCommandReceived?.Invoke(null, new PlaybackCommandReceivedEventArgs(clientName, playbackCommand.ToPublic()));
#pragma warning restore CS0618 // Type or member is obsolete
}
else
{
var command = new PlaybackCommand(playbackCommand.ToPublic());
command.SetResponseInformation(clientName, requestId);
PlaybackActionCommandReceived?.Invoke(null, new PlaybackActionCommandReceivedEventArgs(command));
}
};
Native.SetPlaybackActionCommandReceivedCb(Handle, _playbackActionCommandCallback).
ThrowIfError("Failed to init PlaybackActionCommandReceived event.");
}
private static void RegisterPlaybackPositionCommandReceivedEvent()
{
_playbackPositionCommandCallback = (clientName, requestId, playbackPosition, _) =>
{
var command = new PlaybackPositionCommand(playbackPosition);
command.SetResponseInformation(clientName, requestId);
PlaybackPositionCommandReceived?.Invoke(null, new PlaybackPositionCommandReceivedEventArgs(command));
};
Native.SetPlaybackPosotionCommandReceivedCb(Handle, _playbackPositionCommandCallback).
ThrowIfError("Failed to init PlaybackPositionCommandReceived event.");
}
private static void RegisterPlaylistCommandReceivedEvent()
{
_playlistCommandCallback = (clientName, requestId, playlistName, index, playbackCommand, playbackPosition, _) =>
{
var command = new PlaylistCommand(playbackCommand.ToPublic(), playlistName, index, playbackPosition);
command.SetResponseInformation(clientName, requestId);
PlaylistCommandReceived?.Invoke(null, new PlaylistCommandReceivedEventArgs(command));
};
Native.SetPlaylistCommandReceivedCb(Handle, _playlistCommandCallback).
ThrowIfError("Failed to init PlaylistCommandReceived event.");
}
private static void RegisterShuffleModeCommandReceivedEvent()
{
_shuffleModeCommandCallback = (clientName, requestId, mode, _) =>
{
var command = new ShuffleModeCommand(mode == MediaControllerNativeShuffleMode.On ? true : false);
command.SetResponseInformation(clientName, requestId);
ShuffleModeCommandReceived?.Invoke(null, new ShuffleModeCommandReceivedEventArgs(command));
};
Native.SetShuffleModeCommandReceivedCb(Handle, _shuffleModeCommandCallback).
ThrowIfError("Failed to init ShuffleModeCommandReceived event.");
}
private static void RegisterRepeatModeCommandReceivedEvent()
{
_repeatModeCommandCallback = (clientName, requestId, mode, _) =>
{
var command = new RepeatModeCommand(mode.ToPublic());
command.SetResponseInformation(clientName, requestId);
RepeatModeCommandReceived?.Invoke(null, new RepeatModeCommandReceivedEventArgs(command));
};
Native.SetRepeatModeCommandReceivedCb(Handle, _repeatModeCommandCallback).
ThrowIfError("Failed to init RepeatModeCommandReceived event.");
}
private static void RegisterSubtitleModeCommandReceivedEvent()
{
_subtitleModeCommandCallback = (clientName, requestId, isEnabled, _) =>
{
var command = new SubtitleModeCommand(isEnabled);
command.SetResponseInformation(clientName, requestId);
SubtitleModeCommandReceived?.Invoke(null, new SubtitleModeCommandReceivedEventArgs(command));
};
Native.SetSubtitleModeCommandReceivedCb(Handle, _subtitleModeCommandCallback).
ThrowIfError("Failed to init SubtitleModeCommandReceived event.");
}
private static void RegisterMode360CommandReceivedEvent()
{
_mode360CommandCallback = (clientName, requestId, isEnabled, _) =>
{
var command = new Mode360Command(isEnabled);
command.SetResponseInformation(clientName, requestId);
Mode360CommandReceived?.Invoke(null, new Mode360CommandReceivedEventArgs(command));
};
Native.SetMode360CommandReceivedCb(Handle, _mode360CommandCallback).
ThrowIfError("Failed to init Mode360CommandReceived event.");
}
private static void RegisterDisplayModeCommandReceivedEvent()
{
_displayModeCommandCallback = (clientName, requestId, mode, _) =>
{
var command = new DisplayModeCommand(mode.ToPublic());
command.SetResponseInformation(clientName, requestId);
DisplayModeCommandReceived?.Invoke(null, new DisplayModeCommandReceivedEventArgs(command));
};
Native.SetDisplayModeCommandReceivedCb(Handle, _displayModeCommandCallback).
ThrowIfError("Failed to init DisplayModeCommandReceived event.");
}
private static void RegisterDisplayRotationCommandReceivedEvent()
{
_displayRotationCommandCallback = (clientName, requestId, rotation, _) =>
{
var command = new DisplayRotationCommand(rotation.ToPublic());
command.SetResponseInformation(clientName, requestId);
DisplayRotationCommandReceived?.Invoke(null, new DisplayRotationCommandReceivedEventArgs(command));
};
Native.SetDisplayRotationCommandReceivedCb(Handle, _displayRotationCommandCallback).
ThrowIfError("Failed to init DisplayRotationCommandReceived event.");
}
private static void RegisterCustomCommandReceivedEvent()
{
_customCommandCallback = (clientName, requestId, customCommand, bundleHandle, _) =>
{
CustomCommand command = null;
if (bundleHandle != IntPtr.Zero)
{
command = new CustomCommand(customCommand, new Bundle(new SafeBundleHandle(bundleHandle, true)));
}
else
{
command = new CustomCommand(customCommand);
}
command.SetResponseInformation(clientName, requestId);
CustomCommandReceived?.Invoke(null, new CustomCommandReceivedEventArgs(command));
};
Native.SetCustomCommandReceivedCb(Handle, _customCommandCallback).
ThrowIfError("Failed to init CustomCommandReceived event.");
}
private static SearchCommand CreateSearchCommandReceivedEventArgs(IntPtr searchHandle)
{
var searchConditions = new List();
Native.SearchItemCallback searchItemCallback = (type, category, keyword, bundleHandle, _) =>
{
Bundle bundle = null;
if (bundleHandle != IntPtr.Zero)
{
bundle = new Bundle(new SafeBundleHandle(bundleHandle, true));
}
searchConditions.Add(new MediaControlSearchCondition(type, category, keyword, bundle));
return true;
};
Native.ForeachSearchCondition(searchHandle, searchItemCallback).
ThrowIfError("Failed to get search items.");
return new SearchCommand(searchConditions, searchHandle);
}
private static void RegisterSearchCommandReceivedEvent()
{
_searchCommandCallback = (clientName, requestId, searchHandle, _) =>
{
var command = CreateSearchCommandReceivedEventArgs(searchHandle);
command.SetResponseInformation(clientName, requestId);
SearchCommandReceived?.Invoke(null, new SearchCommandReceivedEventArgs(command));
};
Native.SetSearchCommandReceivedCb(Handle, _searchCommandCallback).
ThrowIfError("Failed to init SearchCommandReceived event.");
}
private static void RegisterCommandCompletedEvent()
{
_commandCompletedCallback = (clientName, requestId, result, bundleHandle, _) =>
{
if (bundleHandle != IntPtr.Zero)
{
CommandCompleted?.Invoke(null, new CommandCompletedEventArgs(requestId, result, new Bundle(new SafeBundleHandle(bundleHandle, true))));
}
else
{
CommandCompleted?.Invoke(null, new CommandCompletedEventArgs(requestId, result));
}
};
Native.SetEventReceivedCb(Handle, _commandCompletedCallback).
ThrowIfError("Failed to init RegisterEventCompletedEvent.");
}
}
}