d05bf086738e4a4d5a70144eeea4bba27a60393a
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / XamlBinding / TizenPlatformServices.cs
1 // using ElmSharp;
2 using System;
3 using System.Collections.Generic;
4 using System.IO;
5 using System.Linq;
6 using System.Net.Http;
7 using System.Reflection;
8 using System.Security.Cryptography;
9 using System.Threading;
10 using System.Threading.Tasks;
11 using Tizen.NUI.Binding.Internals;
12 // using TAppControl = Tizen.Applications.AppControl;
13
14 namespace Tizen.NUI.Binding
15 {
16     internal class TizenPlatformServices : IPlatformServices
17     {
18         static MD5 checksum = MD5.Create();
19
20         static SynchronizationContext s_context;
21
22         public TizenPlatformServices()
23         {
24             s_context = SynchronizationContext.Current;
25         }
26
27         public class TizenTicker : Ticker
28         {
29             readonly System.Threading.Timer _timer;
30
31             public TizenTicker()
32             {
33                 _timer = new System.Threading.Timer((object o) => HandleElapsed(o), this, Timeout.Infinite, Timeout.Infinite);
34             }
35
36             protected override void EnableTimer()
37             {
38                 _timer.Change(16, 16);
39             }
40
41             protected override void DisableTimer()
42             {
43                 _timer.Change(-1, -1);
44             }
45
46             void HandleElapsed(object state)
47             {
48                 s_context.Post((o) => SendSignals(-1), null);
49             }
50         }
51         #region IPlatformServices implementation
52
53         // public double GetNamedSize(NamedSize size, Type targetElementType, bool useOldSizes)
54         // {
55         //  int pt;
56         //  // Actual font size depends on the target idiom.
57         //  switch (size)
58         //  {
59         //      case NamedSize.Micro:
60         //          pt = Device.Idiom == TargetIdiom.TV || Device.Idiom == TargetIdiom.Watch ? 24 : 19;
61         //          break;
62         //      case NamedSize.Small:
63         //          pt = Device.Idiom == TargetIdiom.TV ? 26 : (Device.Idiom == TargetIdiom.Watch ? 30 : 22);
64         //          break;
65         //      case NamedSize.Default:
66         //      case NamedSize.Medium:
67         //          pt = Device.Idiom == TargetIdiom.TV ? 28 : (Device.Idiom == TargetIdiom.Watch ? 32 : 25);
68         //          break;
69         //      case NamedSize.Large:
70         //          pt = Device.Idiom == TargetIdiom.TV ? 84 : (Device.Idiom == TargetIdiom.Watch ? 36 : 31);
71         //          break;
72         //      default:
73         //          throw new ArgumentOutOfRangeException();
74         //  }
75         //  return Forms.ConvertToDPFont(pt);
76         // }
77
78         // public void OpenUriAction(Uri uri)
79         // {
80         //  if (uri == null || uri.AbsoluteUri == null)
81         //  {
82         //      throw new ArgumentNullException(nameof(uri));
83         //  }
84         //  TAppControl tAppControl = new TAppControl() { Operation = "%", Uri = uri.AbsoluteUri };
85         //  var matchedApplications = TAppControl.GetMatchedApplicationIds(tAppControl);
86         //  if (matchedApplications.Count() > 0)
87         //  {
88         //      TAppControl.SendLaunchRequest(tAppControl);
89         //      return;
90         //  }
91         //  throw new PlatformNotSupportedException();
92         // }
93
94         public void BeginInvokeOnMainThread(Action action)
95         {
96             s_context.Post((o) => action(), null);
97         }
98
99         public Ticker CreateTicker()
100         {
101             return new TizenTicker();
102         }
103
104         public void StartTimer(TimeSpan interval, Func<bool> callback)
105         {
106             Console.WriteLine("TizenPlatformServices StartTimer ...");
107             System.Threading.Timer timer = null;
108             bool invoking = false;
109             TimerCallback onTimeout = o =>
110             {
111                 if (!invoking)
112                 {
113                     invoking = true;
114                     BeginInvokeOnMainThread(() =>
115                         {
116                             if (!callback())
117                             {
118                                 timer.Dispose();
119                             }
120                             invoking = false;
121                         }
122                     );
123                 }
124             };
125             timer = new System.Threading.Timer(onTimeout, null, Timeout.Infinite, Timeout.Infinite);
126             // set interval separarately to prevent calling onTimeout before `timer' is assigned
127             timer.Change(interval, interval);
128         }
129
130         public async Task<Stream> GetStreamAsync(Uri uri, CancellationToken cancellationToken)
131         {
132             using (var client = new HttpClient())
133             using (HttpResponseMessage response = await client.GetAsync(uri, cancellationToken))
134                 return await response.Content.ReadAsStreamAsync();
135         }
136
137         public Assembly[] GetAssemblies()
138         {
139             return AppDomain.CurrentDomain.GetAssemblies();
140         }
141
142         // public IIsolatedStorageFile GetUserStoreForApplication()
143         // {
144         //      return new TizenIsolatedStorageFile();
145         // }
146
147         static readonly char[] HexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
148         public string GetMD5Hash(string input)
149         {
150             byte[] bin = checksum.ComputeHash(System.Text.Encoding.UTF8.GetBytes(input));
151             char[] hex = new char[32];
152             for (var i = 0; i < 16; ++i)
153             {
154                 hex[2 * i] = HexDigits[bin[i] >> 4];
155                 hex[2 * i + 1] = HexDigits[bin[i] & 0xf];
156             }
157             return new string(hex);
158         }
159
160         public void QuitApplication()
161         {
162             // Forms.Context.Exit();
163             Console.WriteLine("!!!!!!!!!!!! Exit !!!!!!!!!!!!!!");
164         }
165
166         public bool IsInvokeRequired
167         {
168             get
169             {
170                 // return !EcoreMainloop.IsMainThread;
171                 return true;
172             }
173         }
174
175         public string RuntimePlatform => Device.Tizen;
176
177         #endregion
178
179         // In .NETCore, AppDomain is not supported. The list of the assemblies should be generated manually.
180         internal class AppDomain
181         {
182             public static AppDomain CurrentDomain { get; private set; }
183
184             List<Assembly> _assemblies;
185
186             public static bool IsTizenSpecificAvailable { get; private set; }
187
188             static AppDomain()
189             {
190                 CurrentDomain = new AppDomain();
191             }
192
193             AppDomain()
194             {
195                 _assemblies = new List<Assembly>();
196
197                 // Add this renderer assembly to the list
198                 _assemblies.Add(GetType().GetTypeInfo().Assembly);
199             }
200
201             internal void RegisterAssemblyRecursively(Assembly asm)
202             {
203                 if (_assemblies.Contains(asm))
204                     return;
205
206                 _assemblies.Add(asm);
207
208                 foreach (var refName in asm.GetReferencedAssemblies())
209                 {
210                     if (!refName.Name.StartsWith("System.") && !refName.Name.StartsWith("Microsoft.") && !refName.Name.StartsWith("mscorlib"))
211                     {
212                         try
213                         {
214                             Assembly refAsm = Assembly.Load(refName);
215                             if (refAsm != null)
216                             {
217                                 RegisterAssemblyRecursively(refAsm);
218                                 if (refName.Name == "Tizen.NUI.Xaml.Core")
219                                 {
220                                     if (refAsm.GetType("Tizen.NUI.Xaml.PlatformConfiguration.TizenSpecific.VisualElement") != null)
221                                     {
222                                         IsTizenSpecificAvailable = true;
223                                     }
224                                 }
225                             }
226                         }
227                         catch
228                         {
229                             Log.Warn("Reference Assembly can not be loaded. {0}", refName.FullName);
230                         }
231                     }
232                 }
233             }
234
235             public Assembly[] GetAssemblies()
236             {
237                 return _assemblies.ToArray();
238             }
239         }
240     }
241 }
242