2 * Copyright(c) 2021 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 using System.Collections.Generic;
20 using System.Diagnostics.CodeAnalysis;
21 using Tizen.Applications;
25 [SuppressMessage("Microsoft.Design", "CA1031: Do not catch general exception types", Justification = "This method is to handle external resources that may throw an exception but ignorable. This method should not interupt the main stream.")]
26 internal static class ExternalThemeManager
28 private static Tizen.Applications.ThemeManager.ThemeLoader themeLoader = InitializeThemeLoader();
30 private static string sharedResourcePath;
32 private static IExternalTheme theme;
34 static ExternalThemeManager() { }
36 public static string SharedResourcePath
40 if (themeLoader == null)
50 if (sharedResourcePath != null)
52 return sharedResourcePath;
55 var tizenTheme = themeLoader.CurrentTheme;
57 if (tizenTheme == null || string.IsNullOrEmpty(tizenTheme.Id) || string.IsNullOrEmpty(tizenTheme.Version))
59 sharedResourcePath = string.Empty;
63 ApplicationInfo themePkgInfo;
66 themePkgInfo = ApplicationManager.GetInstalledApplication(tizenTheme.Id);
68 catch (ArgumentException e)
70 Tizen.Log.Error("NUI", $"{e.GetType().Name} occured while getting theme application info.");
73 catch (InvalidOperationException e)
75 Tizen.Log.Error("NUI", $"{e.GetType().Name} occured while getting theme application info.");
79 if (themePkgInfo == null)
81 sharedResourcePath = string.Empty;
85 sharedResourcePath = themePkgInfo.SharedResourcePath;
89 return sharedResourcePath;
93 public static void Initialize()
95 if (themeLoader == null)
100 themeLoader.ThemeChanged += OnTizenThemeChanged;
103 public static IExternalTheme GetCurrentTheme()
105 if (themeLoader == null)
116 Tizen.Applications.ThemeManager.Theme tizenTheme = null;
120 tizenTheme = themeLoader.CurrentTheme;
124 Tizen.Log.Info("NUI", $"[Ignorable] {e.GetType().Name} occured while getting current theme using {themeLoader.GetType().FullName}: {e.Message}");
127 if (tizenTheme == null || string.IsNullOrEmpty(tizenTheme.Id) || string.IsNullOrEmpty(tizenTheme.Version))
132 Tizen.Log.Info("NUI", $"TizenTheme: Id({tizenTheme.Id}), Version({tizenTheme.Version}), Title({tizenTheme.Title})");
134 return new TizenExternalTheme(tizenTheme);
137 public static IExternalTheme GetTheme(string id)
139 if (themeLoader == null)
144 Tizen.Applications.ThemeManager.Theme tizenTheme = null;
148 tizenTheme = themeLoader.LoadTheme(id);
152 Tizen.Log.Info("NUI", $"[Ignorable] {e.GetType().Name} occured while getting load theme using {themeLoader.GetType().FullName}: {e.Message}");
155 return tizenTheme == null ? null : new TizenExternalTheme(tizenTheme);
159 public static void SetTestTheme(IExternalTheme testTheme)
161 if (testTheme == null)
163 throw new ArgumentNullException(nameof(testTheme));
166 if (string.IsNullOrEmpty(testTheme.Id) || string.IsNullOrEmpty(testTheme.Version))
168 throw new ArgumentException();
172 ThemeManager.ApplyExternalTheme(theme);
175 public static void SetTestTheme(string id, string version, Dictionary<string, string> testData)
179 throw new ArgumentNullException(nameof(id));
183 throw new ArgumentNullException(nameof(version));
185 if (testData == null)
187 throw new ArgumentNullException(nameof(testData));
190 theme = new DictionaryExternalTheme(id, version, testData);
191 ThemeManager.ApplyExternalTheme(theme);
195 private static void OnTizenThemeChanged(object sender, Tizen.Applications.ThemeManager.ThemeEventArgs e)
200 Tizen.Log.Info("NUI", $"TizenTheme: Id({e.Theme.Id}), Version({e.Theme.Version}), Title({e.Theme.Title})");
201 sharedResourcePath = null;
202 ThemeManager.ApplyExternalTheme(new TizenExternalTheme(e.Theme));
205 private static Tizen.Applications.ThemeManager.ThemeLoader InitializeThemeLoader()
209 return new Tizen.Applications.ThemeManager.ThemeLoader();
211 catch (DllNotFoundException e)
213 Tizen.Log.Info("NUI", $"[Ignorable] {e.GetType().Name} occured while setting Tizen.Applications.ThemeManager: {e.Message}");