8987212e53675ecb491915476041921c73ab15e8
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / XamlBinding / Device.cs
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.IO;
5 using System.Reflection;
6 using System.Threading;
7 using System.Threading.Tasks;
8 using Tizen.NUI.Binding.Internals;
9
10 namespace Tizen.NUI.Binding
11 {
12     internal static class Device
13     {
14         public const string iOS = "iOS";
15         public const string Android = "Android";
16         public const string UWP = "UWP";
17         public const string macOS = "macOS";
18         public const string GTK = "GTK";
19         public const string Tizen = "Tizen";
20         public const string WPF = "WPF";
21
22         [EditorBrowsable(EditorBrowsableState.Never)]
23         public static DeviceInfo info;
24
25         static IPlatformServices s_platformServices;
26
27         [EditorBrowsable(EditorBrowsableState.Never)]
28         public static void SetIdiom(TargetIdiom value) => Idiom = value;
29         public static TargetIdiom Idiom { get; internal set; }
30
31         //TODO: Why are there two of these? This is never used...?
32         [EditorBrowsable(EditorBrowsableState.Never)]
33         public static void SetTargetIdiom(TargetIdiom value) => Idiom = value;
34
35         [Obsolete("TargetPlatform is obsolete as of version 2.3.4. Please use RuntimePlatform instead.")]
36 #pragma warning disable 0618
37         public static TargetPlatform OS
38         {
39             get
40             {
41                 TargetPlatform platform;
42                 if (Enum.TryParse(RuntimePlatform, out platform))
43                     return platform;
44
45                 // In the old TargetPlatform, there was no distinction between WinRT/UWP
46                 if (RuntimePlatform == UWP)
47                 {
48                     return TargetPlatform.Windows;
49                 }
50
51                 return TargetPlatform.Other;
52             }
53         }
54 #pragma warning restore 0618
55
56         public static string RuntimePlatform => PlatformServices?.RuntimePlatform;
57
58         [EditorBrowsable(EditorBrowsableState.Never)]
59         public static DeviceInfo Info
60         {
61             get
62             {
63                 // if (info == null)
64                 //      throw new InvalidOperationException("You MUST call Tizen.NUI.Xaml.Init(); prior to using it.");
65                 return info;
66             }
67             set { info = value; }
68         }
69
70         [EditorBrowsable(EditorBrowsableState.Never)]
71         public static void SetFlowDirection(FlowDirection value) => FlowDirection = value;
72         public static FlowDirection FlowDirection { get; internal set; }
73
74         [EditorBrowsable(EditorBrowsableState.Never)]
75         public static bool IsInvokeRequired
76         {
77             get { return PlatformServices.IsInvokeRequired; }
78         }
79
80         [EditorBrowsable(EditorBrowsableState.Never)]
81         public static IPlatformServices PlatformServices
82         {
83             get
84             {
85                 if (s_platformServices == null)
86                     throw new InvalidOperationException("You MUST call Tizen.NUI.Init(); prior to using it.");
87                 return s_platformServices;
88             }
89             set
90             {
91                 s_platformServices = value;
92                 Console.WriteLine("Device s_platformServices : " + s_platformServices );
93             }
94         }
95
96         [EditorBrowsable(EditorBrowsableState.Never)]
97         public static IReadOnlyList<string> Flags { get; private set; }
98
99         [EditorBrowsable(EditorBrowsableState.Never)]
100         public static void SetFlags(IReadOnlyList<string> flags)
101         {
102             Flags = flags;
103         }
104
105         public static void BeginInvokeOnMainThread(Action action)
106         {
107             PlatformServices?.BeginInvokeOnMainThread(action);
108             action();
109             Console.WriteLine("Device BeginInvokeOnMainThread action called");
110         }
111
112         // public static double GetNamedSize(NamedSize size, Element targetElement)
113         // {
114         //     return GetNamedSize(size, targetElement.GetType());
115         // }
116
117         // public static double GetNamedSize(NamedSize size, Type targetElementType)
118         // {
119         //     return GetNamedSize(size, targetElementType, false);
120         // }
121
122         [Obsolete("OnPlatform is obsolete as of version 2.3.4. Please use switch(RuntimePlatform) instead.")]
123         public static void OnPlatform(Action iOS = null, Action Android = null, Action WinPhone = null, Action Default = null)
124         {
125             switch (OS)
126             {
127                 case TargetPlatform.iOS:
128                     if (iOS != null)
129                         iOS();
130                     else if (Default != null)
131                         Default();
132                     break;
133                 case TargetPlatform.Android:
134                     if (Android != null)
135                         Android();
136                     else if (Default != null)
137                         Default();
138                     break;
139                 case TargetPlatform.Windows:
140                 case TargetPlatform.WinPhone:
141                     if (WinPhone != null)
142                         WinPhone();
143                     else if (Default != null)
144                         Default();
145                     break;
146                 case TargetPlatform.Other:
147                     if (Default != null)
148                         Default();
149                     break;
150             }
151         }
152
153         [Obsolete("OnPlatform<> (generic) is obsolete as of version 2.3.4. Please use switch(RuntimePlatform) instead.")]
154         public static T OnPlatform<T>(T iOS, T Android, T WinPhone)
155         {
156             switch (OS)
157             {
158                 case TargetPlatform.iOS:
159                     return iOS;
160                 case TargetPlatform.Android:
161                     return Android;
162                 case TargetPlatform.Windows:
163                 case TargetPlatform.WinPhone:
164                     return WinPhone;
165             }
166
167             return iOS;
168         }
169
170         public static void OpenUri(Uri uri)
171         {
172             // PlatformServices?.OpenUriAction(uri);
173         }
174
175         public static void StartTimer(TimeSpan interval, Func<bool> callback)
176         {
177             PlatformServices.StartTimer(interval, callback);
178         }
179
180         [EditorBrowsable(EditorBrowsableState.Never)]
181         public static Assembly[] GetAssemblies()
182         {
183             return PlatformServices?.GetAssemblies();
184         }
185
186         // [EditorBrowsable(EditorBrowsableState.Never)]
187         // public static double GetNamedSize(NamedSize size, Type targetElementType, bool useOldSizes)
188         // {
189         //     return PlatformServices.GetNamedSize(size, targetElementType, useOldSizes);
190         // }
191
192         internal static Task<Stream> GetStreamAsync(Uri uri, CancellationToken cancellationToken)
193         {
194             return PlatformServices?.GetStreamAsync(uri, cancellationToken);
195         }
196
197         public static class Styles
198         {
199             public static readonly string TitleStyleKey = "TitleStyle";
200
201             public static readonly string SubtitleStyleKey = "SubtitleStyle";
202
203             public static readonly string BodyStyleKey = "BodyStyle";
204
205             public static readonly string ListItemTextStyleKey = "ListItemTextStyle";
206
207             public static readonly string ListItemDetailTextStyleKey = "ListItemDetailTextStyle";
208
209             public static readonly string CaptionStyleKey = "CaptionStyle";
210
211             public static readonly Style TitleStyle = new Style(typeof(Tizen.NUI.BaseComponents.TextLabel)) { BaseResourceKey = TitleStyleKey };
212
213             public static readonly Style SubtitleStyle = new Style(typeof(Tizen.NUI.BaseComponents.TextLabel)) { BaseResourceKey = SubtitleStyleKey };
214
215             public static readonly Style BodyStyle = new Style(typeof(Tizen.NUI.BaseComponents.TextLabel)) { BaseResourceKey = BodyStyleKey };
216
217             public static readonly Style ListItemTextStyle = new Style(typeof(Tizen.NUI.BaseComponents.TextLabel)) { BaseResourceKey = ListItemTextStyleKey };
218
219             public static readonly Style ListItemDetailTextStyle = new Style(typeof(Tizen.NUI.BaseComponents.TextLabel)) { BaseResourceKey = ListItemDetailTextStyleKey };
220
221             public static readonly Style CaptionStyle = new Style(typeof(Tizen.NUI.BaseComponents.TextLabel)) { BaseResourceKey = CaptionStyleKey };
222         }
223     }
224 }