[NUI] Update theme system
[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.Diagnostics.CodeAnalysis;
20 using System.IO;
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 interrupt the main stream.")]
26     internal static class ExternalThemeManager
27     {
28         private static Tizen.Applications.ThemeManager.ThemeLoader themeLoader;
29         private static string id;
30         private static string version;
31
32         static ExternalThemeManager() { }
33
34         public static void Initialize()
35         {
36             if (themeLoader != null)
37             {
38                 return;
39             }
40
41             try
42             {
43                 themeLoader = new Tizen.Applications.ThemeManager.ThemeLoader();
44                 themeLoader.ThemeChanged += OnExternalPlatformThemeChanged;
45             }
46             catch (Exception e)
47             {
48                 Tizen.Log.Info("NUI", $"[Ignorable] {e.GetType().Name} occurred while setting Tizen.Applications.ThemeManager: {e.Message}");
49             }
50         }
51
52
53         /// <summary> Returns the theme's shared resource path that is currently loading. </summary>
54         public static string SharedResourcePath { get; set; } = String.Empty;
55
56         // FIXME Please remove this API after fix ThemeLoader.CurrentTheme is fixed.
57         public static string CurrentThemeId
58         {
59             get
60             {
61                 if (id == null) UpdateCurrentThemeIdAndVersion();
62                 return id;
63             }
64         }
65
66         // FIXME Please remove this API after fix ThemeLoader.CurrentTheme is fixed.
67         public static string CurrentThemeVersion
68         {
69             get
70             {
71                 if (version == null) UpdateCurrentThemeIdAndVersion();
72                 return version;
73             }
74         }
75
76         public static bool SetTheme(string id)
77         {
78             if (themeLoader != null)
79             {
80                 try
81                 {
82                     themeLoader.CurrentTheme = themeLoader.LoadTheme(id);
83                     return true;
84                 }
85                 catch (Exception e)
86                 {
87                     Tizen.Log.Info("NUI", $"[Ignorable] {e.GetType().Name} occurred while getting load theme using {themeLoader.GetType().FullName}: {e.Message}");
88                 }
89             }
90
91             return false;
92         }
93
94         public static string GetSharedResourcePath(string id)
95         {
96             if (!string.IsNullOrEmpty(id))
97             {
98                 try
99                 {
100                     using (var themePkgInfo = ApplicationManager.GetInstalledApplication(id))
101                     {
102                         return themePkgInfo.SharedResourcePath;
103                     }
104                 }
105                 catch (ArgumentException e)
106                 {
107                     Tizen.Log.Error("NUI", $"{e.GetType().Name} occurred while getting theme application info.");
108                 }
109                 catch (InvalidOperationException e)
110                 {
111                     Tizen.Log.Error("NUI", $"{e.GetType().Name} occurred while getting theme application info.");
112                 }
113             }
114
115             return null;
116         }
117
118         private static void UpdateCurrentThemeIdAndVersion()
119         {
120             if (themeLoader == null)
121             {
122                 return;
123             }
124
125             Tizen.Applications.ThemeManager.Theme tizenTheme = null;
126
127             try
128             {
129                 tizenTheme = themeLoader.CurrentTheme;
130             }
131             catch (Exception e)
132             {
133                 Tizen.Log.Info("NUI", $"[Ignorable] {e.GetType().Name} occurred while getting current theme using {themeLoader.GetType().FullName}: {e.Message}");
134             }
135
136             if (tizenTheme == null || string.IsNullOrEmpty(tizenTheme.Id) || string.IsNullOrEmpty(tizenTheme.Version))
137             {
138                 return;
139             }
140
141             Tizen.Log.Info("NUI", $"TizenTheme: Id({tizenTheme.Id}), Version({tizenTheme.Version}), Title({tizenTheme.Title})");
142
143             id = tizenTheme.Id;
144             version = tizenTheme.Version;
145         }
146
147         private static void OnExternalPlatformThemeChanged(object sender, Tizen.Applications.ThemeManager.ThemeEventArgs e)
148         {
149             Tizen.Log.Info("NUI", $"TizenTheme: Id({e.Theme.Id}), Version({e.Theme.Version}), Title({e.Theme.Title})");
150
151             id = e.Theme.Id;
152             version = e.Theme.Version;
153
154             if (!ThemeManager.PlatformThemeEnabled) return;
155
156             ThemeManager.ApplyExternalPlatformTheme(id, version);
157         }
158     }
159 }
160