839660d80a5671e0c86d0f035a426ff184f94bde
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Theme / ExternalThemeManager.cs
1 /*
2  * Copyright(c) 2021 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 using System;
19 using System.Collections.Generic;
20 using System.Diagnostics.CodeAnalysis;
21 using Tizen.Applications;
22
23 namespace Tizen.NUI
24 {
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
27     {
28         private static readonly Tizen.Applications.ThemeManager.ThemeLoader themeLoader = new Tizen.Applications.ThemeManager.ThemeLoader();
29         private static string sharedResourcePath;
30 #if DEBUG
31         private static IExternalTheme theme;
32 #endif
33         static ExternalThemeManager() { }
34
35         public static string SharedResourcePath
36         {
37             get
38             {
39 #if DEBUG
40                 if (theme != null)
41                 {
42                     return string.Empty;
43                 }
44 #endif
45                 if (sharedResourcePath != null)
46                 {
47                     return sharedResourcePath;
48                 }
49
50                 var tizenTheme = themeLoader.CurrentTheme;
51
52                 if (tizenTheme == null || string.IsNullOrEmpty(tizenTheme.Id) || string.IsNullOrEmpty(tizenTheme.Version))
53                 {
54                     sharedResourcePath = string.Empty;
55                 }
56                 else
57                 {
58                     ApplicationInfo themePkgInfo;
59                     try
60                     {
61                         themePkgInfo = ApplicationManager.GetInstalledApplication(tizenTheme.Id);
62                     }
63                     catch (ArgumentException e)
64                     {
65                         Tizen.Log.Error("NUI", $"{e.GetType().Name} occured while getting theme application info.");
66                         throw e;
67                     }
68                     catch (InvalidOperationException e)
69                     {
70                         Tizen.Log.Error("NUI", $"{e.GetType().Name} occured while getting theme application info.");
71                         throw e;
72                     }
73
74                     if (themePkgInfo == null)
75                     {
76                         sharedResourcePath = string.Empty;
77                     }
78                     else
79                     {
80                         sharedResourcePath = themePkgInfo.SharedResourcePath;
81                     }
82                 }
83
84                 return sharedResourcePath;
85             }
86         }
87
88         public static void Initialize()
89         {
90             themeLoader.ThemeChanged += OnTizenThemeChanged;
91         }
92
93         public static IExternalTheme GetCurrentTheme()
94         {
95 #if DEBUG
96             if (theme != null)
97             {
98                 return theme;
99             }
100 #endif
101             Tizen.Applications.ThemeManager.Theme tizenTheme = null;
102
103             try
104             {
105                 tizenTheme = themeLoader.CurrentTheme;
106             }
107             catch (Exception e)
108             {
109                 Tizen.Log.Info("NUI", $"[Ignorable] {e.GetType().Name} occured while getting current theme using {themeLoader.GetType().FullName}: {e.Message}");
110             }
111
112             if (tizenTheme == null || string.IsNullOrEmpty(tizenTheme.Id) || string.IsNullOrEmpty(tizenTheme.Version))
113             {
114                 return null;
115             }
116
117             Tizen.Log.Info("NUI", $"TizenTheme: Id({tizenTheme.Id}), Version({tizenTheme.Version}), Title({tizenTheme.Title})");
118
119             return new TizenExternalTheme(tizenTheme);
120         }
121
122         public static IExternalTheme GetTheme(string id)
123         {
124             Tizen.Applications.ThemeManager.Theme tizenTheme = null;
125
126             try
127             {
128                 tizenTheme = themeLoader.LoadTheme(id);
129             }
130             catch (Exception e)
131             {
132                 Tizen.Log.Info("NUI", $"[Ignorable] {e.GetType().Name} occured while getting load theme using {themeLoader.GetType().FullName}: {e.Message}");
133             }
134
135             return tizenTheme == null ? null : new TizenExternalTheme(tizenTheme);
136         }
137
138 #if DEBUG
139         public static void SetTestTheme(IExternalTheme testTheme)
140         {
141             if (testTheme == null)
142             {
143                 throw new ArgumentNullException(nameof(testTheme));
144             }
145
146             if (string.IsNullOrEmpty(testTheme.Id) || string.IsNullOrEmpty(testTheme.Version))
147             {
148                 throw new ArgumentException();
149             }
150
151             theme = testTheme;
152             ThemeManager.ApplyExternalTheme(theme);
153         }
154
155         public static void SetTestTheme(string id, string version, Dictionary<string, string> testData)
156         {
157             if (id == null)
158             {
159                 throw new ArgumentNullException(nameof(id));
160             }
161             if (version == null)
162             {
163                 throw new ArgumentNullException(nameof(version));
164             }
165             if (testData == null)
166             {
167                 throw new ArgumentNullException(nameof(testData));
168             }
169
170             theme = new DictionaryExternalTheme(id, version, testData);
171             ThemeManager.ApplyExternalTheme(theme);
172         }
173 #endif
174
175         private static void OnTizenThemeChanged(object sender, Tizen.Applications.ThemeManager.ThemeEventArgs e)
176         {
177 #if DEBUG
178             theme = null;
179 #endif
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));
183         }
184     }
185 }
186