From 4c429c8615a4784e89d23394a0d81d44d2fb6143 Mon Sep 17 00:00:00 2001 From: kilig Date: Wed, 26 Aug 2020 09:47:32 +0900 Subject: [PATCH] [Tizen.Applications.ThemeManager] add new apis for ThemeManager (#1913) Signed-off-by: Inkyun Kil Co-authored-by: TizenAPI-Bot <37820187+TizenAPI-Bot@users.noreply.github.com> Co-authored-by: pjh9216 --- .../Interop/Interop.Libraries.cs | 23 ++ .../Interop/Interop.ThemeManager.cs | 130 ++++++++ .../Tizen.Applications.ThemeManager.csproj | 12 + .../Tizen.Applications.ThemeManager.sln | 54 ++++ .../Tizen.Applications.ThemeManager/Theme.cs | 336 +++++++++++++++++++++ .../ThemeEventArgs.cs | 40 +++ .../Tizen.Applications.ThemeManager/ThemeLoader.cs | 218 +++++++++++++ 7 files changed, 813 insertions(+) create mode 100755 internals/src/Tizen.Applications.ThemeManager/Interop/Interop.Libraries.cs create mode 100755 internals/src/Tizen.Applications.ThemeManager/Interop/Interop.ThemeManager.cs create mode 100755 internals/src/Tizen.Applications.ThemeManager/Tizen.Applications.ThemeManager.csproj create mode 100755 internals/src/Tizen.Applications.ThemeManager/Tizen.Applications.ThemeManager.sln create mode 100755 internals/src/Tizen.Applications.ThemeManager/Tizen.Applications.ThemeManager/Theme.cs create mode 100755 internals/src/Tizen.Applications.ThemeManager/Tizen.Applications.ThemeManager/ThemeEventArgs.cs create mode 100755 internals/src/Tizen.Applications.ThemeManager/Tizen.Applications.ThemeManager/ThemeLoader.cs diff --git a/internals/src/Tizen.Applications.ThemeManager/Interop/Interop.Libraries.cs b/internals/src/Tizen.Applications.ThemeManager/Interop/Interop.Libraries.cs new file mode 100755 index 0000000..c5b50fb --- /dev/null +++ b/internals/src/Tizen.Applications.ThemeManager/Interop/Interop.Libraries.cs @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2020 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. + */ + +internal static partial class Interop +{ + internal static partial class Libraries + { + public const string ThemeManager = "libcapi-appfw-tizen-theme.so"; + } +} diff --git a/internals/src/Tizen.Applications.ThemeManager/Interop/Interop.ThemeManager.cs b/internals/src/Tizen.Applications.ThemeManager/Interop/Interop.ThemeManager.cs new file mode 100755 index 0000000..f5f4728 --- /dev/null +++ b/internals/src/Tizen.Applications.ThemeManager/Interop/Interop.ThemeManager.cs @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2020 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.Runtime.InteropServices; +using Tizen; +using Tizen.Applications; + +internal static partial class Interop +{ + internal static partial class ThemeManager + { + private const string LogTag = "Tizen.Applications.ThemeManager"; + + internal enum ErrorCode : int + { + None = Tizen.Internals.Errors.ErrorCode.None, + InvalidParameter = Tizen.Internals.Errors.ErrorCode.InvalidParameter, + PermissionDenied = Tizen.Internals.Errors.ErrorCode.PermissionDenied, + IoError = Tizen.Internals.Errors.ErrorCode.IoError, + } + + internal static class ThemeManagerErrorFactory + { + internal static Exception GetException(Interop.ThemeManager.ErrorCode err, string message) + { + string errMessage = string.Format("{0} err = {1}", message, err); + Log.Warn(LogTag, errMessage); + switch (err) + { + case Interop.ThemeManager.ErrorCode.InvalidParameter: + return new ArgumentException(errMessage); + case Interop.ThemeManager.ErrorCode.PermissionDenied: + return new UnauthorizedAccessException(errMessage); + case Interop.ThemeManager.ErrorCode.IoError: + default: + return new InvalidOperationException(errMessage); + } + } + } + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void ThemeLoaderChangedCallback(IntPtr handle, IntPtr userData); + + [DllImport(Libraries.ThemeManager, EntryPoint = "theme_get_id")] + internal static extern ErrorCode GetId(IntPtr handle, out string id); + + [DllImport(Libraries.ThemeManager, EntryPoint = "theme_get_version")] + internal static extern ErrorCode GetVersion(IntPtr handle, out string version); + + [DllImport(Libraries.ThemeManager, EntryPoint = "theme_get_tool_version")] + internal static extern ErrorCode GetToolVersion(IntPtr handle, out string version); + + [DllImport(Libraries.ThemeManager, EntryPoint = "theme_get_title")] + internal static extern ErrorCode GetTitle(IntPtr handle, out string title); + + [DllImport(Libraries.ThemeManager, EntryPoint = "theme_get_resolution")] + internal static extern ErrorCode GetResolution(IntPtr handle, out string resolution); + + [DllImport(Libraries.ThemeManager, EntryPoint = "theme_get_preview")] + internal static extern ErrorCode GetPreview(IntPtr handle, out string preview); + + [DllImport(Libraries.ThemeManager, EntryPoint = "theme_get_description")] + internal static extern ErrorCode GetDescription(IntPtr handle, out string description); + + [DllImport(Libraries.ThemeManager, EntryPoint = "theme_get_string")] + internal static extern ErrorCode GetString(IntPtr handle, string key, out string val); + + [DllImport(Libraries.ThemeManager, EntryPoint = "theme_get_string_array")] + internal static extern ErrorCode GetStringArray(IntPtr handle, string key, out IntPtr val, out int count); + + [DllImport(Libraries.ThemeManager, EntryPoint = "theme_get_path")] + internal static extern ErrorCode GetPath(IntPtr handle, string key, out string val); + + [DllImport(Libraries.ThemeManager, EntryPoint = "theme_get_path_array")] + internal static extern ErrorCode GetPathArray(IntPtr handle, string key, out IntPtr val, out int count); + + [DllImport(Libraries.ThemeManager, EntryPoint = "theme_get_int")] + internal static extern ErrorCode GetInt(IntPtr handle, string key, out int val); + + [DllImport(Libraries.ThemeManager, EntryPoint = "theme_get_float")] + internal static extern ErrorCode GetFloat(IntPtr handle, string key, out float val); + + [DllImport(Libraries.ThemeManager, EntryPoint = "theme_get_bool")] + internal static extern ErrorCode GetBool(IntPtr handle, string key, out bool val); + + [DllImport(Libraries.ThemeManager, EntryPoint = "theme_clone")] + internal static extern ErrorCode ThemeClone(IntPtr handle, out IntPtr cloned); + + [DllImport(Libraries.ThemeManager, EntryPoint = "theme_destroy")] + internal static extern ErrorCode ThemeDestroy(IntPtr handle); + + [DllImport(Libraries.ThemeManager, EntryPoint = "theme_loader_create")] + internal static extern ErrorCode LoaderCreate(out IntPtr loaderHandle); + + [DllImport(Libraries.ThemeManager, EntryPoint = "theme_loader_destroy")] + internal static extern ErrorCode LoaderDestroy(IntPtr loaderHandle); + + [DllImport(Libraries.ThemeManager, EntryPoint = "theme_loader_add_event")] + internal static extern ErrorCode LoaderAddEvent(IntPtr loaderHandle, ThemeLoaderChangedCallback callback, IntPtr userData, out string eventId); + + [DllImport(Libraries.ThemeManager, EntryPoint = "theme_loader_remove_event")] + internal static extern ErrorCode LoaderRemoveEvent(IntPtr loaderHandle, string eventId); + + [DllImport(Libraries.ThemeManager, EntryPoint = "theme_loader_load_current")] + internal static extern ErrorCode LoaderLoadCurrentTheme(IntPtr loaderHandle, out IntPtr handle); + + [DllImport(Libraries.ThemeManager, EntryPoint = "theme_loader_load")] + internal static extern ErrorCode LoaderLoadTheme(IntPtr loaderHandle, string id, out IntPtr handle); + + [DllImport(Libraries.ThemeManager, EntryPoint = "theme_loader_query_id")] + internal static extern ErrorCode LoaderQueryId(IntPtr loaderHandle, out IntPtr ids, out int count); + + [DllImport(Libraries.ThemeManager, EntryPoint = "theme_loader_set_current")] + internal static extern ErrorCode LoaderSetCurrentTheme(IntPtr loaderHandle, string id); + } +} diff --git a/internals/src/Tizen.Applications.ThemeManager/Tizen.Applications.ThemeManager.csproj b/internals/src/Tizen.Applications.ThemeManager/Tizen.Applications.ThemeManager.csproj new file mode 100755 index 0000000..4111fa5 --- /dev/null +++ b/internals/src/Tizen.Applications.ThemeManager/Tizen.Applications.ThemeManager.csproj @@ -0,0 +1,12 @@ + + + + netstandard2.0 + + + + + + + + diff --git a/internals/src/Tizen.Applications.ThemeManager/Tizen.Applications.ThemeManager.sln b/internals/src/Tizen.Applications.ThemeManager/Tizen.Applications.ThemeManager.sln new file mode 100755 index 0000000..b059d91 --- /dev/null +++ b/internals/src/Tizen.Applications.ThemeManager/Tizen.Applications.ThemeManager.sln @@ -0,0 +1,54 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29806.167 +MinimumVisualStudioVersion = 15.0.26124.0 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tizen.Applications.ThemeManager", "Tizen.Applications.ThemeManager.csproj", "{C37063A6-0105-43F6-B3EA-CA4786F62BD7}" + ProjectSection(ProjectDependencies) = postProject + {A0671F32-9031-4F70-B888-D3A27ABC05F3} = {A0671F32-9031-4F70-B888-D3A27ABC05F3} + EndProjectSection +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tizen", "..\..\..\src\Tizen\Tizen.csproj", "{A0671F32-9031-4F70-B888-D3A27ABC05F3}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C37063A6-0105-43F6-B3EA-CA4786F62BD7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C37063A6-0105-43F6-B3EA-CA4786F62BD7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C37063A6-0105-43F6-B3EA-CA4786F62BD7}.Debug|x64.ActiveCfg = Debug|Any CPU + {C37063A6-0105-43F6-B3EA-CA4786F62BD7}.Debug|x64.Build.0 = Debug|Any CPU + {C37063A6-0105-43F6-B3EA-CA4786F62BD7}.Debug|x86.ActiveCfg = Debug|Any CPU + {C37063A6-0105-43F6-B3EA-CA4786F62BD7}.Debug|x86.Build.0 = Debug|Any CPU + {C37063A6-0105-43F6-B3EA-CA4786F62BD7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C37063A6-0105-43F6-B3EA-CA4786F62BD7}.Release|Any CPU.Build.0 = Release|Any CPU + {C37063A6-0105-43F6-B3EA-CA4786F62BD7}.Release|x64.ActiveCfg = Release|Any CPU + {C37063A6-0105-43F6-B3EA-CA4786F62BD7}.Release|x64.Build.0 = Release|Any CPU + {C37063A6-0105-43F6-B3EA-CA4786F62BD7}.Release|x86.ActiveCfg = Release|Any CPU + {C37063A6-0105-43F6-B3EA-CA4786F62BD7}.Release|x86.Build.0 = Release|Any CPU + {A0671F32-9031-4F70-B888-D3A27ABC05F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A0671F32-9031-4F70-B888-D3A27ABC05F3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A0671F32-9031-4F70-B888-D3A27ABC05F3}.Debug|x64.ActiveCfg = Debug|Any CPU + {A0671F32-9031-4F70-B888-D3A27ABC05F3}.Debug|x64.Build.0 = Debug|Any CPU + {A0671F32-9031-4F70-B888-D3A27ABC05F3}.Debug|x86.ActiveCfg = Debug|Any CPU + {A0671F32-9031-4F70-B888-D3A27ABC05F3}.Debug|x86.Build.0 = Debug|Any CPU + {A0671F32-9031-4F70-B888-D3A27ABC05F3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A0671F32-9031-4F70-B888-D3A27ABC05F3}.Release|Any CPU.Build.0 = Release|Any CPU + {A0671F32-9031-4F70-B888-D3A27ABC05F3}.Release|x64.ActiveCfg = Release|Any CPU + {A0671F32-9031-4F70-B888-D3A27ABC05F3}.Release|x64.Build.0 = Release|Any CPU + {A0671F32-9031-4F70-B888-D3A27ABC05F3}.Release|x86.ActiveCfg = Release|Any CPU + {A0671F32-9031-4F70-B888-D3A27ABC05F3}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {0131746E-72CC-496D-9774-A02B519FDB0C} + EndGlobalSection +EndGlobal diff --git a/internals/src/Tizen.Applications.ThemeManager/Tizen.Applications.ThemeManager/Theme.cs b/internals/src/Tizen.Applications.ThemeManager/Tizen.Applications.ThemeManager/Theme.cs new file mode 100755 index 0000000..d33372f --- /dev/null +++ b/internals/src/Tizen.Applications.ThemeManager/Tizen.Applications.ThemeManager/Theme.cs @@ -0,0 +1,336 @@ +/* + * Copyright (c) 2020 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; +using System.Collections.Generic; +using System.Runtime.InteropServices; + +namespace Tizen.Applications.ThemeManager +{ + /// + /// + /// + /// 8 + public class Theme : IDisposable + { + private bool _disposed = false; + private IntPtr _handle; + private string _id = String.Empty; + private string _version = String.Empty; + private string _toolVersion = String.Empty; + private string _title = String.Empty; + private string _resolution = String.Empty; + private string _preview = String.Empty; + private string _description = String.Empty; + + /// + /// A copy constructor of Theme. + /// + /// Theme class. + /// Thrown when failed because of an invalid argument. + /// Thrown when failed because of system error. + /// Thrown when failed because of out of memory. + /// 8 + public Theme(Theme theme) + { + if (theme == null || theme._handle == IntPtr.Zero) + { + throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(Interop.ThemeManager.ErrorCode.InvalidParameter, "Invalid parameter"); + } + + var err = Interop.ThemeManager.ErrorCode.None; + err = Interop.ThemeManager.ThemeClone(theme._handle, out _handle); + if (err != Interop.ThemeManager.ErrorCode.None) + { + throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to clone handle"); + } + } + + internal Theme(IntPtr handle) + { + _handle = handle; + var err = Interop.ThemeManager.ErrorCode.None; + err = Interop.ThemeManager.GetId(handle, out _id); + if (err != Interop.ThemeManager.ErrorCode.None) + { + throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to get id"); + } + + err = Interop.ThemeManager.GetVersion(handle, out _version); + if (err != Interop.ThemeManager.ErrorCode.None) + { + throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to get version"); + } + + err = Interop.ThemeManager.GetToolVersion(handle, out _toolVersion); + if (err != Interop.ThemeManager.ErrorCode.None) + { + throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to get tool version"); + } + + err = Interop.ThemeManager.GetTitle(handle, out _title); + if (err != Interop.ThemeManager.ErrorCode.None) + { + throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to get title"); + } + + err = Interop.ThemeManager.GetResolution(handle, out _resolution); + if (err != Interop.ThemeManager.ErrorCode.None) + { + throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to get resolution"); + } + + err = Interop.ThemeManager.GetPreview(handle, out _preview); + if (err != Interop.ThemeManager.ErrorCode.None) + { + throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to get preview"); + } + + err = Interop.ThemeManager.GetDescription(handle, out _description); + if (err != Interop.ThemeManager.ErrorCode.None) + { + throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to get description"); + } + } + + /// + /// A Theme ID + /// + /// 8 + public string Id { get { return _id; } } + + /// + /// A Theme Version + /// + /// 8 + public string Version { get { return _version; } } + + /// + /// A Theme ToolVersion + /// + /// 8 + public string ToolVersion { get { return _toolVersion; } } + + /// + /// A Theme Title + /// + /// 8 + public string Title { get { return _title; } } + + /// + /// A Theme Resolution + /// + /// 8 + public string Resolution { get { return _resolution; } } + + /// + /// A Theme Preview + /// + /// 8 + public string Preview { get { return _preview; } } + + /// + /// A Theme Description + /// + /// 8 + public string Description { get { return _description; } } + + + /// + /// Gets the string corresponding with given key. + /// + /// The string key to find information. + /// 8 + /// Thrown when failed because of an invalid argument. + /// Thrown when failed because of the system error. + /// Thrown when failed because of out of memory. + public string GetString(string key) + { + string str; + var err = Interop.ThemeManager.GetString(_handle, key, out str); + if (err != Interop.ThemeManager.ErrorCode.None) + { + throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to get string value of the key"); + } + + return str; + } + + /// + /// Gets the string array corresponding with given key. + /// + /// The string key to find information. + /// 8 + /// Thrown when failed because of an invalid argument. + /// Thrown when failed because of the system error. + /// Thrown when failed because of out of memory. + public IEnumerable GetStrings(string key) + { + IntPtr val; + int count; + string[] strings; + var err = Interop.ThemeManager.GetStringArray(_handle, key, out val, out count); + if (err != Interop.ThemeManager.ErrorCode.None) + { + throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to get string array value of the key"); + } + + IntPtrToStringArray(val, count, out strings); + return strings; + } + + /// + /// Gets the integer corresponding with given key. + /// + /// The string key to find information. + /// 8 + /// Thrown when failed because of an invalid argument. + /// Thrown when failed because of the system error. + /// Thrown when failed because of out of memory. + public int GetInt(string key) + { + int val; + var err = Interop.ThemeManager.GetInt(_handle, key, out val); + if (err != Interop.ThemeManager.ErrorCode.None) + { + throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to get integer value of the key"); + } + + return val; + } + + /// + /// Gets the float corresponding with given key. + /// + /// The string key to find information. + /// 8 + /// Thrown when failed because of an invalid argument. + /// Thrown when failed because of the system error. + /// Thrown when failed because of out of memory. + public float GetFloat(string key) + { + float val; + var err = Interop.ThemeManager.GetFloat(_handle, key, out val); + if (err != Interop.ThemeManager.ErrorCode.None) + { + throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to get float value of the key"); + } + + return val; + } + + /// + /// Gets the bool corresponding with given key. + /// + /// The string key to find information. + /// 8 + /// Thrown when failed because of an invalid argument. + /// Thrown when failed because of the system error. + /// Thrown when failed because of out of memory. + public bool GetBool(string key) + { + bool val; + var err = Interop.ThemeManager.GetBool(_handle, key, out val); + if (err != Interop.ThemeManager.ErrorCode.None) + { + throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to get bool value of the key"); + } + + return val; + } + + /// + /// Gets the path corresponding with given key. + /// + /// The string key to find information. + /// 8 + /// Thrown when failed because of an invalid argument. + /// Thrown when failed because of the system error. + /// Thrown when failed because of out of memory. + public string GetPath(string key) + { + string val; + var err = Interop.ThemeManager.GetPath(_handle, key, out val); + if (err != Interop.ThemeManager.ErrorCode.None) + { + throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to get path value of the key"); + } + + return val; + } + + /// + /// Gets the path array corresponding with given key. + /// + /// The string key to find information. + /// 8 + /// Thrown when failed because of an invalid argument. + /// Thrown when failed because of the system error. + /// Thrown when failed because of out of memory. + public IEnumerable GetPaths(string key) + { + IntPtr val; + int count; + string[] paths; + var err = Interop.ThemeManager.GetPathArray(_handle, key, out val, out count); + if (err != Interop.ThemeManager.ErrorCode.None) + { + throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to get path array value of the key"); + } + + IntPtrToStringArray(val, count, out paths); + return paths; + } + + static void IntPtrToStringArray(IntPtr unmanagedArray, int size, out string[] managedArray) + { + managedArray = new string[size]; + IntPtr[] IntPtrArray = new IntPtr[size]; + Marshal.Copy(unmanagedArray, IntPtrArray, 0, size); + for (int iterator = 0; iterator < size; iterator++) + { + managedArray[iterator] = Marshal.PtrToStringAnsi(IntPtrArray[iterator]); + } + } + /// + /// Releases all resources used by the Theme class. + /// + /// 8 + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + /// + /// Releases the unmanaged resources used by the Theme class specifying whether to perform a normal dispose operation. + /// + /// true for a normal dispose operation; false to finalize the handle. + protected virtual void Dispose(bool disposing) + { + if (_disposed) + return; + + if (_handle != IntPtr.Zero) + { + Interop.ThemeManager.ThemeDestroy(_handle); + _handle = IntPtr.Zero; + } + _disposed = true; + } + } +} diff --git a/internals/src/Tizen.Applications.ThemeManager/Tizen.Applications.ThemeManager/ThemeEventArgs.cs b/internals/src/Tizen.Applications.ThemeManager/Tizen.Applications.ThemeManager/ThemeEventArgs.cs new file mode 100755 index 0000000..67a753c --- /dev/null +++ b/internals/src/Tizen.Applications.ThemeManager/Tizen.Applications.ThemeManager/ThemeEventArgs.cs @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2020 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.Text; + +namespace Tizen.Applications.ThemeManager +{ + /// + /// ThemeEventArgs class. This class is an event arguments of the ThemeLoaderChanged events. + /// + /// 8 + public class ThemeEventArgs : EventArgs + { + internal ThemeEventArgs(Theme theme) + { + Theme = theme; + } + + /// + /// A Theme + /// + /// 8 + public Theme Theme { get; } + } +} diff --git a/internals/src/Tizen.Applications.ThemeManager/Tizen.Applications.ThemeManager/ThemeLoader.cs b/internals/src/Tizen.Applications.ThemeManager/Tizen.Applications.ThemeManager/ThemeLoader.cs new file mode 100755 index 0000000..274f799 --- /dev/null +++ b/internals/src/Tizen.Applications.ThemeManager/Tizen.Applications.ThemeManager/ThemeLoader.cs @@ -0,0 +1,218 @@ +/* + * Copyright (c) 2020 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.Runtime.InteropServices; + +namespace Tizen.Applications.ThemeManager +{ + /// + /// + /// + /// 8 + public class ThemeLoader : IDisposable + { + private const string LogTag = "Tizen.Applications.ThemeManager"; + private bool _disposed = false; + private event EventHandler _changedEventHandler; + private Interop.ThemeManager.ThemeLoaderChangedCallback _callback; + private string _eventId; + private Theme _currentTheme = null; + internal IntPtr _loaderHandle = IntPtr.Zero; + + /// + /// Creates ThemeLoader. + /// + /// 8 + /// Failed to create handle. + public ThemeLoader() + { + Interop.ThemeManager.ErrorCode err = Interop.ThemeManager.LoaderCreate(out _loaderHandle); + if (err != Interop.ThemeManager.ErrorCode.None) + { + throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to create themeloader"); + } + } + + private void OnThemeChanged(IntPtr handle, IntPtr userData) + { + Interop.ThemeManager.ThemeClone(handle, out IntPtr cloned); + _changedEventHandler?.Invoke(this, new ThemeEventArgs(new Theme(cloned))); + } + + /// + /// Adds or removes events for theme changed. + /// + /// 8 + /// Thrown when failed because of an invalid argument. + /// Thrown when failed because of out of memory. + public event EventHandler ThemeChanged + { + add + { + if (_changedEventHandler == null) + { + if (_callback == null) + _callback = new Interop.ThemeManager.ThemeLoaderChangedCallback(OnThemeChanged); + Interop.ThemeManager.ErrorCode err = Interop.ThemeManager.LoaderAddEvent(_loaderHandle, _callback, IntPtr.Zero, out _eventId); + + if (err != Interop.ThemeManager.ErrorCode.None) + { + throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to add event"); + } + + } + _changedEventHandler += value; + + } + remove + { + _changedEventHandler -= value; + if (_changedEventHandler == null) + { + Interop.ThemeManager.ErrorCode err = Interop.ThemeManager.LoaderRemoveEvent(_loaderHandle, _eventId); + + if (err != Interop.ThemeManager.ErrorCode.None) + { + throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to remove event"); + } + _callback = null; + } + } + } + + /// + /// Sets current theme. + /// + /// 8 + /// Thrown when failed because of an invalid argument. + public Theme CurrentTheme + { + get + { + if (_currentTheme != null && _currentTheme.Id.Length > 0) + return _currentTheme; + + Interop.ThemeManager.ErrorCode err = Interop.ThemeManager.LoaderLoadCurrentTheme(_loaderHandle, out IntPtr _themeHandle); + if (err != Interop.ThemeManager.ErrorCode.None) + { + throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to load current theme"); + } + + _currentTheme = new Theme(_themeHandle); + return _currentTheme; + } + set + { + if (value == null) + throw new ArgumentException("value is null"); + + Interop.ThemeManager.ErrorCode err = Interop.ThemeManager.LoaderSetCurrentTheme(_loaderHandle, value.Id); + if (err != Interop.ThemeManager.ErrorCode.None) + { + Log.Warn(LogTag, "Failed to set current. Err = " + err); + throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to set current theme"); + } + + if(_currentTheme != value) + _currentTheme = value; + } + } + + /// + /// Loads theme. + /// + /// 8 + /// Thrown when failed because of an invalid argument. + public Theme LoadTheme(string id) + { + IntPtr _themeHandle; + + Interop.ThemeManager.ErrorCode err = Interop.ThemeManager.LoaderLoadTheme(_loaderHandle, id, out _themeHandle); + if (err != Interop.ThemeManager.ErrorCode.None) + { + throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to load theme"); + } + + return new Theme(_themeHandle); + } + + /// + /// Gets bundle of theme IDs. + /// + /// 8 + /// Thrown when failed because of an invalid argument. + /// Thrown when failed because of out of memory. + public IEnumerable QueryIds() + { + IntPtr ids; + string[] stringArray; + int count; + + Interop.ThemeManager.ErrorCode err = Interop.ThemeManager.LoaderQueryId(_loaderHandle, out ids, out count); + if (err != Interop.ThemeManager.ErrorCode.None) + { + throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to query ids"); + } + + IntPtrToStringArray(ids, count, out stringArray); + return stringArray; + } + + static void IntPtrToStringArray(IntPtr unmanagedArray, int size, out string[] managedArray) + { + managedArray = new string[size]; + IntPtr[] IntPtrArray = new IntPtr[size]; + Marshal.Copy(unmanagedArray, IntPtrArray, 0, size); + for (int iterator = 0; iterator < size; iterator++) + { + managedArray[iterator] = Marshal.PtrToStringAnsi(IntPtrArray[iterator]); + } + } + + /// + /// Releases all resources used by the ThemeLoader class. + /// + /// 8 + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + /// + /// Releases the unmanaged resources used by the ThemeLoader class specifying whether to perform a normal dispose operation. + /// + /// true for a normal dispose operation; false to finalize the handle. + protected virtual void Dispose(bool disposing) + { + if (_disposed) + return; + + if (_loaderHandle != IntPtr.Zero) + { + Interop.ThemeManager.LoaderDestroy(_loaderHandle); + _loaderHandle = IntPtr.Zero; + } + + if (disposing && _currentTheme != null) + _currentTheme.Dispose(); + + _disposed = true; + } + } +} -- 2.7.4