/*
* 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 System;
using System.Collections.Generic;
using System.ComponentModel;
namespace Tizen.System
{
///
/// The Feedback API provides functions to control haptic and sound.
/// The Feedback API provides the way to play and stop feedback, and get the information whether a specific pattern is supported.
/// Below is the supported pattern string:
/// Tap
/// SoftInputPanel
/// Key0
/// Key1
/// Key2
/// Key3
/// Key4
/// Key5
/// Key6
/// Key7
/// Key8
/// Key9
/// KeyStar
/// KeySharp
/// KeyBack
/// Hold
/// HardwareKeyPressed
/// HardwareKeyHold
/// Message
/// Email
/// WakeUp
/// Schedule
/// Timer
/// General
/// PowerOn
/// PowerOff
/// ChargerConnected
/// ChargingError
/// FullyCharged
/// LowBattery
/// Lock
/// UnLock
/// VibrationModeAbled
/// SilentModeDisabled
/// BluetoothDeviceConnected
/// BluetoothDeviceDisconnected
/// ListReorder
/// ListSlider
/// VolumeKeyPressed
///
///
/// For controlling the haptic device:
/// http://tizen.org/privilege/haptic
/// For controlling the sound, privilege is not needed.
///
///
///
/// Feedback feedback = new Feedback();
/// bool res = feedback.IsSupportedPattern(FeedbackType.Vibration, "Tap");
///
///
/// 3
public class Feedback
{
private const string LogTag = "Tizen.System.Feedback";
private readonly Dictionary Pattern = new Dictionary
{
{"Tap", 0},
{"SoftInputPanel", 1},
{"Key0", 6},
{"Key1", 7},
{"Key2", 8},
{"Key3", 9},
{"Key4", 10},
{"Key5", 11},
{"Key6", 12},
{"Key7", 13},
{"Key8", 14},
{"Key9", 15},
{"KeyStar", 16},
{"KeySharp", 17},
{"KeyBack", 18},
{"Hold", 19},
{"HardwareKeyPressed", 21},
{"HardwareKeyHold", 22},
{"Message", 23},
{"Email", 25},
{"WakeUp", 27},
{"Schedule", 29},
{"Timer", 31},
{"General", 33},
{"PowerOn", 36},
{"PowerOff", 37},
{"ChargerConnected", 38},
{"ChargingError", 40},
{"FullyCharged", 42},
{"LowBattery", 44},
{"Lock", 46},
{"UnLock", 47},
{"VibrationModeAbled", 55},
{"SilentModeDisabled", 56},
{"BluetoothDeviceConnected", 57},
{"BluetoothDeviceDisconnected", 58},
{"ListReorder", 62},
{"ListSlider", 63},
{"VolumeKeyPressed", 64},
{"Basic", 40001},
{"ToggleOn", 40002},
{"ToggleOff", 40003},
{"LongPressOn", 40004},
{"LongPressOff", 40005},
{"Invalid", 40006},
{"Confirm", 40007},
{"PopUp", 40008},
{"PreheatEnding", 40009},
{"TaskEnding", 40010},
{"Scroll", 40011},
{"PageTurn", 40012},
{"OpStart", 40013},
{"OpPause", 40014},
{"OpStop", 40015},
{"Default", 40016},
{"DefaultLeve1", 40017},
{"Level1", 40018},
{"Level2", 40019},
{"Level3", 40020},
{"Level4", 40021},
{"Level5", 40022},
{"Level6", 40023},
{"Level7", 40024},
{"Level8", 40025},
{"Level9", 40026},
{"Level10", 40027},
{"TimerEnding", 40028},
{"BurnerDetected", 40029},
{"BurnerMoved", 40030},
{"Connected", 40031},
{"Disconnected", 40032},
};
///
/// Constructor of Feedback class
///
/// 3
///
/// http://tizen.org/feature/feedback.vibration for FeedbackType.Vibration
///
/// Thrown when failed because the devices (vibration and sound) are not supported.
/// Thrown when failed because of a system error.
/// http://tizen.org/privilege/haptic
///
///
/// Feedback feedback = new Feedback();
///
///
public Feedback()
{
Interop.Feedback.FeedbackError res = (Interop.Feedback.FeedbackError)Interop.Feedback.Initialize();
if (res != Interop.Feedback.FeedbackError.None)
{
Log.Warn(LogTag, string.Format("Failed to initialize feedback. err = {0}", res));
switch (res)
{
case Interop.Feedback.FeedbackError.NotSupported:
throw new NotSupportedException("Device is not supported");
default:
throw new InvalidOperationException("Failed to initialize");
}
}
}
///
/// Finalizes an instance of the Feedback class.
///
~Feedback()
{
Interop.Feedback.FeedbackError res = (Interop.Feedback.FeedbackError)Interop.Feedback.Deinitialize();
if (res != Interop.Feedback.FeedbackError.None)
{
Log.Warn(LogTag, string.Format("Failed to deinitialize feedback. err = {0}", res));
}
}
///
/// Gets the supported information about a specific type and pattern.
///
///
/// Now, IsSupportedPattern is not working for FeedbackType.All.
/// This API is working for FeedbackType.Sound and FeedbackType.Vibration only.
/// If you use FeedbackType.All for type parameter, this API will throw ArgumentException.
/// To get the supported information for Vibration type, the application should have http://tizen.org/privilege/haptic privilege.
///
/// 3
/// The feedback type.
/// The feedback pattern string.
///
/// http://tizen.org/feature/feedback.vibration for FeedbackType.Vibration
///
/// Information whether a pattern is supported.
/// Thrown when failed because the feedback is not initialized.
/// Thrown when failed because of an invalid arguament.
/// Thrown when failed becuase the device (haptic, sound) is not supported.
/// Thrown when failed because the access is not granted (No privilege).
/// Thrown when failed because of a system error.
/// http://tizen.org/privilege/haptic
///
///
/// Feedback feedback = new Feedback();
/// bool res = feedback.IsSupportedPattern(FeedbackType.Vibration, "Tap");
///
///
public bool IsSupportedPattern(FeedbackType type, String pattern)
{
bool supported = false;
int number;
Interop.Feedback.FeedbackError res;
if (!Pattern.TryGetValue(pattern, out number))
throw new ArgumentException($"Not supported pattern string : {pattern}", nameof(pattern));
res = (Interop.Feedback.FeedbackError)Interop.Feedback.IsSupportedPattern((Interop.Feedback.FeedbackType)type, number, out supported);
if (res != Interop.Feedback.FeedbackError.None)
{
Log.Warn(LogTag, string.Format("Failed to get supported information. err = {0}", res));
switch (res)
{
case Interop.Feedback.FeedbackError.NotInitialized:
throw new Exception("Not initialized");
case Interop.Feedback.FeedbackError.InvalidParameter:
throw new ArgumentException("Invalid Arguments");
case Interop.Feedback.FeedbackError.NotSupported:
throw new NotSupportedException("Device is not supported");
case Interop.Feedback.FeedbackError.PermissionDenied:
throw new UnauthorizedAccessException("Access is not granted");
case Interop.Feedback.FeedbackError.OperationFailed:
default:
throw new InvalidOperationException("Failed to get supported information");
}
}
return supported;
}
///
/// Plays a specific feedback pattern.
///
///
/// To play Vibration type, app should have http://tizen.org/privilege/haptic privilege.
///
/// 3
/// The feedback type.
/// The feedback pattern string.
///
/// http://tizen.org/feature/feedback.vibration for FeedbackType.Vibration
///
/// Thrown when failed because feedback is not initialized.
/// Thrown when failed because of an invalid arguament.
/// Thrown when failed because the device (haptic, sound) or a specific pattern is not supported.
/// Thrown when failed because the access is not granted(No privilege)
/// Thrown when failed because of a system error.
/// http://tizen.org/privilege/haptic
///
///
/// Feedback feedback = new Feedback();
/// feedback.Play(FeedbackType.All, "Tap");
///
///
public void Play(FeedbackType type, String pattern)
{
int number;
Interop.Feedback.FeedbackError res;
if (!Pattern.TryGetValue(pattern, out number))
throw new ArgumentException($"Not supported pattern string : {pattern}", nameof(pattern));
if (type == FeedbackType.All)
res = (Interop.Feedback.FeedbackError)Interop.Feedback.Play(number);
else
res = (Interop.Feedback.FeedbackError)Interop.Feedback.PlayType((Interop.Feedback.FeedbackType)type, number);
if (res != Interop.Feedback.FeedbackError.None)
{
Log.Warn(LogTag, string.Format("Failed to play feedback. err = {0}", res));
switch (res)
{
case Interop.Feedback.FeedbackError.NotInitialized:
throw new Exception("Not initialized");
case Interop.Feedback.FeedbackError.InvalidParameter:
throw new ArgumentException("Invalid Arguments");
case Interop.Feedback.FeedbackError.NotSupported:
throw new NotSupportedException("Not supported");
case Interop.Feedback.FeedbackError.PermissionDenied:
throw new UnauthorizedAccessException("Access is not granted");
case Interop.Feedback.FeedbackError.OperationFailed:
default:
throw new InvalidOperationException("Failed to play pattern");
}
}
}
///
/// Stops to play the feedback.
///
///
/// To stop vibration, the application should have http://tizen.org/privilege/haptic privilege.
///
/// 3
///
/// http://tizen.org/feature/feedback.vibration
///
/// Thrown when failed because the feedback is not initialized.
/// Thrown when failed because of an invalid arguament
/// Thrown when failed because the device (haptic, sound) or a specific pattern is not supported.
/// Thrown when failed because the access is not granted (No privilege).
/// Thrown when failed because of a system error.
/// http://tizen.org/privilege/haptic
///
///
/// Feedback Feedback1 = new Feedback();
/// Feedback1.Stop();
///
///
public void Stop()
{
Interop.Feedback.FeedbackError res = (Interop.Feedback.FeedbackError)Interop.Feedback.Stop();
if (res != Interop.Feedback.FeedbackError.None)
{
Log.Warn(LogTag, string.Format("Failed to stop feedback. err = {0}", res));
switch (res)
{
case Interop.Feedback.FeedbackError.NotInitialized:
throw new Exception("Not initialized");
case Interop.Feedback.FeedbackError.InvalidParameter:
throw new ArgumentException("Invalid Arguments");
case Interop.Feedback.FeedbackError.NotSupported:
throw new NotSupportedException("Not supported");
case Interop.Feedback.FeedbackError.PermissionDenied:
throw new UnauthorizedAccessException("Access is not granted");
case Interop.Feedback.FeedbackError.OperationFailed:
default:
throw new InvalidOperationException("Failed to stop pattern");
}
}
}
///
/// Gets the count of theme can be used according to feedback type.
///
///
/// Now this internal API works for FeedbackType.Sound only, FeedbackType.Vibration is not supported.
///
/// 10
/// The feedback type.
/// The counf of theme can be used according to feedback type.
/// Thrown when failed because the feedback is not initialized.
/// Thrown when failed because of an invalid arguament.
/// Thrown when failed becuase the device (haptic, sound) is not supported.
/// Thrown when failed because of a system error.
///
///
/// Feedback feedback = new Feedback();
/// uint coundOfTheme = feedback.GetCountOfThemeInternal(FeedbackType.Sound);
///
///
[EditorBrowsable(EditorBrowsableState.Never)]
public uint GetCountOfThemeInternal(FeedbackType type)
{
uint countOfTheme = 0;
Interop.Feedback.FeedbackError res;
res = (Interop.Feedback.FeedbackError)Interop.Feedback.GetCountOfThemeInternal((Interop.Feedback.FeedbackType)type, out countOfTheme);
if (res != Interop.Feedback.FeedbackError.None)
{
Log.Warn(LogTag, string.Format("Failed to get count of theme internal. err = {0}", res));
switch (res)
{
case Interop.Feedback.FeedbackError.NotInitialized:
throw new Exception("Not initialized");
case Interop.Feedback.FeedbackError.InvalidParameter:
throw new ArgumentException("Invalid Arguments");
case Interop.Feedback.FeedbackError.NotSupported:
throw new NotSupportedException("Device is not supported");
case Interop.Feedback.FeedbackError.OperationFailed:
default:
throw new InvalidOperationException("Failed to get count of theme internal");
}
}
return countOfTheme;
}
///
/// Gets the index of theme selected.
///
///
/// Now this internal API works for FeedbackType.Sound only, FeedbackType.Vibration is not supported.
///
/// 10
/// The feedback type.
/// The index of theme selected as default theme according to feedback type.
/// Thrown when failed because of an invalid arguament.
/// Thrown when failed becuase the device (haptic, sound) is not supported.
/// Thrown when failed because of a system error.
///
///
/// Feedback feedback = new Feedback();
/// uint indexOfTheme = feedback. GetThemeIndexInternal(FeedbackType.Sound);
///
///
[EditorBrowsable(EditorBrowsableState.Never)]
public uint GetThemeIndexInternal(FeedbackType type)
{
uint countOfTheme = 0;
Interop.Feedback.FeedbackError res;
res = (Interop.Feedback.FeedbackError)Interop.Feedback.GetThemeIndexInternal((Interop.Feedback.FeedbackType)type, out countOfTheme);
if (res != Interop.Feedback.FeedbackError.None)
{
Log.Warn(LogTag, string.Format("Failed to get index of theme internal. err = {0}", res));
switch (res)
{
case Interop.Feedback.FeedbackError.InvalidParameter:
throw new ArgumentException("Invalid Arguments");
case Interop.Feedback.FeedbackError.NotSupported:
throw new NotSupportedException("Device is not supported");
case Interop.Feedback.FeedbackError.OperationFailed:
default:
throw new InvalidOperationException("Failed to get index of theme internal");
}
}
return countOfTheme;
}
///
/// Sets the index of theme according to feedback type.
///
///
/// Now this internal API works for FeedbackType.Sound only, FeedbackType.Vibration is not supported.
/// To set the index of theme for Sound type, the application should have http://tizen.org/privilege/systemsettings.admin privilege.
///
/// 10
/// The feedback type.
/// The index of theme will be set.
/// Thrown when failed because of an invalid arguament.
/// Thrown when failed becuase the device (haptic, sound) is not supported.
/// Thrown when failed because the access is not granted(No privilege)
/// Thrown when failed because of a system error.
///
///
/// Feedback feedback = new Feedback();
/// uint indexOfTheme = 0;
/// feedback.SetThemeIndexInternal(FeedbackType.Sound, indexOfTheme);
///
///
[EditorBrowsable(EditorBrowsableState.Never)]
public void SetThemeIndexInternal(FeedbackType type, uint indexOfTheme)
{
Interop.Feedback.FeedbackError res;
res = (Interop.Feedback.FeedbackError)Interop.Feedback.SetThemeIndexInternal((Interop.Feedback.FeedbackType)type, indexOfTheme);
if (res != Interop.Feedback.FeedbackError.None)
{
Log.Warn(LogTag, string.Format("Failed to set index of theme internal. err = {0}", res));
switch (res)
{
case Interop.Feedback.FeedbackError.InvalidParameter:
throw new ArgumentException("Invalid Arguments");
case Interop.Feedback.FeedbackError.NotSupported:
throw new NotSupportedException("Device is not supported");
case Interop.Feedback.FeedbackError.PermissionDenied:
throw new UnauthorizedAccessException("Access is not granted");
case Interop.Feedback.FeedbackError.OperationFailed:
default:
throw new InvalidOperationException("Failed to set index of theme internal");
}
}
}
}
}