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 readonly Tizen.Applications.ThemeManager.ThemeLoader themeLoader = new Tizen.Applications.ThemeManager.ThemeLoader();
29 private static string sharedResourcePath;
31 private static IExternalTheme theme;
33 static ExternalThemeManager() { }
35 public static string SharedResourcePath
45 if (sharedResourcePath != null)
47 return sharedResourcePath;
50 var tizenTheme = themeLoader.CurrentTheme;
52 if (tizenTheme == null || string.IsNullOrEmpty(tizenTheme.Id) || string.IsNullOrEmpty(tizenTheme.Version))
54 sharedResourcePath = string.Empty;
58 ApplicationInfo themePkgInfo;
61 themePkgInfo = ApplicationManager.GetInstalledApplication(tizenTheme.Id);
63 catch (ArgumentException e)
65 Tizen.Log.Error("NUI", $"{e.GetType().Name} occured while getting theme application info.");
68 catch (InvalidOperationException e)
70 Tizen.Log.Error("NUI", $"{e.GetType().Name} occured while getting theme application info.");
74 if (themePkgInfo == null)
76 sharedResourcePath = string.Empty;
80 sharedResourcePath = themePkgInfo.SharedResourcePath;
84 return sharedResourcePath;
88 public static void Initialize()
90 themeLoader.ThemeChanged += OnTizenThemeChanged;
93 public static IExternalTheme GetCurrentTheme()
101 Tizen.Applications.ThemeManager.Theme tizenTheme = null;
105 tizenTheme = themeLoader.CurrentTheme;
109 Tizen.Log.Info("NUI", $"[Ignorable] {e.GetType().Name} occured while getting current theme using {themeLoader.GetType().FullName}: {e.Message}");
112 if (tizenTheme == null || string.IsNullOrEmpty(tizenTheme.Id) || string.IsNullOrEmpty(tizenTheme.Version))
117 Tizen.Log.Info("NUI", $"TizenTheme: Id({tizenTheme.Id}), Version({tizenTheme.Version}), Title({tizenTheme.Title})");
119 return new TizenExternalTheme(tizenTheme);
122 public static IExternalTheme GetTheme(string id)
124 Tizen.Applications.ThemeManager.Theme tizenTheme = null;
128 tizenTheme = themeLoader.LoadTheme(id);
132 Tizen.Log.Info("NUI", $"[Ignorable] {e.GetType().Name} occured while getting load theme using {themeLoader.GetType().FullName}: {e.Message}");
135 return tizenTheme == null ? null : new TizenExternalTheme(tizenTheme);
139 public static void SetTestTheme(IExternalTheme testTheme)
141 if (testTheme == null)
143 throw new ArgumentNullException(nameof(testTheme));
146 if (string.IsNullOrEmpty(testTheme.Id) || string.IsNullOrEmpty(testTheme.Version))
148 throw new ArgumentException();
152 ThemeManager.ApplyExternalTheme(theme);
155 public static void SetTestTheme(string id, string version, Dictionary<string, string> testData)
159 throw new ArgumentNullException(nameof(id));
163 throw new ArgumentNullException(nameof(version));
165 if (testData == null)
167 throw new ArgumentNullException(nameof(testData));
170 theme = new DictionaryExternalTheme(id, version, testData);
171 ThemeManager.ApplyExternalTheme(theme);
175 private static void OnTizenThemeChanged(object sender, Tizen.Applications.ThemeManager.ThemeEventArgs e)
180 Tizen.Log.Info("NUI", $"TizenTheme: Id({e.Theme.Id}), Version({e.Theme.Version}), Title({e.Theme.Title})");
181 sharedResourcePath = null;
182 ThemeManager.ApplyExternalTheme(new TizenExternalTheme(e.Theme));