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