[NUI] Catch DllNotFound exception for capi-appfw-tizen-theme.so (#2711)
[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 Tizen.Applications.ThemeManager.ThemeLoader themeLoader = InitializeThemeLoader();
29
30         private static string sharedResourcePath;
31 #if DEBUG
32         private static IExternalTheme theme;
33 #endif
34         static ExternalThemeManager() { }
35
36         public static string SharedResourcePath
37         {
38             get
39             {
40                 if (themeLoader == null)
41                 {
42                     return string.Empty;
43                 }
44 #if DEBUG
45                 if (theme != null)
46                 {
47                     return string.Empty;
48                 }
49 #endif
50                 if (sharedResourcePath != null)
51                 {
52                     return sharedResourcePath;
53                 }
54
55                 var tizenTheme = themeLoader.CurrentTheme;
56
57                 if (tizenTheme == null || string.IsNullOrEmpty(tizenTheme.Id) || string.IsNullOrEmpty(tizenTheme.Version))
58                 {
59                     sharedResourcePath = string.Empty;
60                 }
61                 else
62                 {
63                     ApplicationInfo themePkgInfo;
64                     try
65                     {
66                         themePkgInfo = ApplicationManager.GetInstalledApplication(tizenTheme.Id);
67                     }
68                     catch (ArgumentException e)
69                     {
70                         Tizen.Log.Error("NUI", $"{e.GetType().Name} occured while getting theme application info.");
71                         throw;
72                     }
73                     catch (InvalidOperationException e)
74                     {
75                         Tizen.Log.Error("NUI", $"{e.GetType().Name} occured while getting theme application info.");
76                         throw;
77                     }
78
79                     if (themePkgInfo == null)
80                     {
81                         sharedResourcePath = string.Empty;
82                     }
83                     else
84                     {
85                         sharedResourcePath = themePkgInfo.SharedResourcePath;
86                     }
87                 }
88
89                 return sharedResourcePath;
90             }
91         }
92
93         public static void Initialize()
94         {
95             if (themeLoader == null)
96             {
97                 return;
98             }
99
100             themeLoader.ThemeChanged += OnTizenThemeChanged;
101         }
102
103         public static IExternalTheme GetCurrentTheme()
104         {
105             if (themeLoader == null)
106             {
107                 return null;
108             }
109
110 #if DEBUG
111             if (theme != null)
112             {
113                 return theme;
114             }
115 #endif
116             Tizen.Applications.ThemeManager.Theme tizenTheme = null;
117
118             try
119             {
120                 tizenTheme = themeLoader.CurrentTheme;
121             }
122             catch (Exception e)
123             {
124                 Tizen.Log.Info("NUI", $"[Ignorable] {e.GetType().Name} occured while getting current theme using {themeLoader.GetType().FullName}: {e.Message}");
125             }
126
127             if (tizenTheme == null || string.IsNullOrEmpty(tizenTheme.Id) || string.IsNullOrEmpty(tizenTheme.Version))
128             {
129                 return null;
130             }
131
132             Tizen.Log.Info("NUI", $"TizenTheme: Id({tizenTheme.Id}), Version({tizenTheme.Version}), Title({tizenTheme.Title})");
133
134             return new TizenExternalTheme(tizenTheme);
135         }
136
137         public static IExternalTheme GetTheme(string id)
138         {
139             if (themeLoader == null)
140             {
141                 return null;
142             }
143
144             Tizen.Applications.ThemeManager.Theme tizenTheme = null;
145
146             try
147             {
148                 tizenTheme = themeLoader.LoadTheme(id);
149             }
150             catch (Exception e)
151             {
152                 Tizen.Log.Info("NUI", $"[Ignorable] {e.GetType().Name} occured while getting load theme using {themeLoader.GetType().FullName}: {e.Message}");
153             }
154
155             return tizenTheme == null ? null : new TizenExternalTheme(tizenTheme);
156         }
157
158 #if DEBUG
159         public static void SetTestTheme(IExternalTheme testTheme)
160         {
161             if (testTheme == null)
162             {
163                 throw new ArgumentNullException(nameof(testTheme));
164             }
165
166             if (string.IsNullOrEmpty(testTheme.Id) || string.IsNullOrEmpty(testTheme.Version))
167             {
168                 throw new ArgumentException();
169             }
170
171             theme = testTheme;
172             ThemeManager.ApplyExternalTheme(theme);
173         }
174
175         public static void SetTestTheme(string id, string version, Dictionary<string, string> testData)
176         {
177             if (id == null)
178             {
179                 throw new ArgumentNullException(nameof(id));
180             }
181             if (version == null)
182             {
183                 throw new ArgumentNullException(nameof(version));
184             }
185             if (testData == null)
186             {
187                 throw new ArgumentNullException(nameof(testData));
188             }
189
190             theme = new DictionaryExternalTheme(id, version, testData);
191             ThemeManager.ApplyExternalTheme(theme);
192         }
193 #endif
194
195         private static void OnTizenThemeChanged(object sender, Tizen.Applications.ThemeManager.ThemeEventArgs e)
196         {
197 #if DEBUG
198             theme = null;
199 #endif
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));
203         }
204
205         private static Tizen.Applications.ThemeManager.ThemeLoader InitializeThemeLoader()
206         {
207             try
208             {
209                 return new Tizen.Applications.ThemeManager.ThemeLoader();
210             }
211             catch (DllNotFoundException e)
212             {
213                 Tizen.Log.Info("NUI", $"[Ignorable] {e.GetType().Name} occured while setting Tizen.Applications.ThemeManager: {e.Message}");
214             }
215
216             return null;
217         }
218     }
219 }
220