3 using System.Collections.Generic;
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;
14 namespace Tizen.NUI.Binding
16 internal class TizenPlatformServices : IPlatformServices
18 static MD5 checksum = MD5.Create();
20 static SynchronizationContext s_context;
22 public TizenPlatformServices()
24 s_context = SynchronizationContext.Current;
27 public class TizenTicker : Ticker
29 readonly System.Threading.Timer _timer;
33 _timer = new System.Threading.Timer((object o) => HandleElapsed(o), this, Timeout.Infinite, Timeout.Infinite);
36 protected override void EnableTimer()
38 _timer.Change(16, 16);
41 protected override void DisableTimer()
43 _timer.Change(-1, -1);
46 void HandleElapsed(object state)
48 s_context.Post((o) => SendSignals(-1), null);
51 #region IPlatformServices implementation
53 // public double GetNamedSize(NamedSize size, Type targetElementType, bool useOldSizes)
56 // // Actual font size depends on the target idiom.
59 // case NamedSize.Micro:
60 // pt = Device.Idiom == TargetIdiom.TV || Device.Idiom == TargetIdiom.Watch ? 24 : 19;
62 // case NamedSize.Small:
63 // pt = Device.Idiom == TargetIdiom.TV ? 26 : (Device.Idiom == TargetIdiom.Watch ? 30 : 22);
65 // case NamedSize.Default:
66 // case NamedSize.Medium:
67 // pt = Device.Idiom == TargetIdiom.TV ? 28 : (Device.Idiom == TargetIdiom.Watch ? 32 : 25);
69 // case NamedSize.Large:
70 // pt = Device.Idiom == TargetIdiom.TV ? 84 : (Device.Idiom == TargetIdiom.Watch ? 36 : 31);
73 // throw new ArgumentOutOfRangeException();
75 // return Forms.ConvertToDPFont(pt);
78 // public void OpenUriAction(Uri uri)
80 // if (uri == null || uri.AbsoluteUri == null)
82 // throw new ArgumentNullException(nameof(uri));
84 // TAppControl tAppControl = new TAppControl() { Operation = "%", Uri = uri.AbsoluteUri };
85 // var matchedApplications = TAppControl.GetMatchedApplicationIds(tAppControl);
86 // if (matchedApplications.Count() > 0)
88 // TAppControl.SendLaunchRequest(tAppControl);
91 // throw new PlatformNotSupportedException();
94 public void BeginInvokeOnMainThread(Action action)
96 s_context.Post((o) => action(), null);
99 public Ticker CreateTicker()
101 return new TizenTicker();
104 public void StartTimer(TimeSpan interval, Func<bool> callback)
106 Console.WriteLine("TizenPlatformServices StartTimer ...");
107 System.Threading.Timer timer = null;
108 bool invoking = false;
109 TimerCallback onTimeout = o =>
114 BeginInvokeOnMainThread(() =>
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);
130 public async Task<Stream> GetStreamAsync(Uri uri, CancellationToken cancellationToken)
132 using (var client = new HttpClient())
133 using (HttpResponseMessage response = await client.GetAsync(uri, cancellationToken))
134 return await response.Content.ReadAsStreamAsync();
137 public Assembly[] GetAssemblies()
139 return AppDomain.CurrentDomain.GetAssemblies();
142 // public IIsolatedStorageFile GetUserStoreForApplication()
144 // return new TizenIsolatedStorageFile();
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)
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)
154 hex[2 * i] = HexDigits[bin[i] >> 4];
155 hex[2 * i + 1] = HexDigits[bin[i] & 0xf];
157 return new string(hex);
160 public void QuitApplication()
162 // Forms.Context.Exit();
163 Console.WriteLine("!!!!!!!!!!!! Exit !!!!!!!!!!!!!!");
166 public bool IsInvokeRequired
170 // return !EcoreMainloop.IsMainThread;
175 public string RuntimePlatform => Device.Tizen;
179 // In .NETCore, AppDomain is not supported. The list of the assemblies should be generated manually.
180 internal class AppDomain
182 public static AppDomain CurrentDomain { get; private set; }
184 List<Assembly> _assemblies;
186 public static bool IsTizenSpecificAvailable { get; private set; }
190 CurrentDomain = new AppDomain();
195 _assemblies = new List<Assembly>();
197 // Add this renderer assembly to the list
198 _assemblies.Add(GetType().GetTypeInfo().Assembly);
201 internal void RegisterAssemblyRecursively(Assembly asm)
203 if (_assemblies.Contains(asm))
206 _assemblies.Add(asm);
208 foreach (var refName in asm.GetReferencedAssemblies())
210 if (!refName.Name.StartsWith("System.") && !refName.Name.StartsWith("Microsoft.") && !refName.Name.StartsWith("mscorlib"))
214 Assembly refAsm = Assembly.Load(refName);
217 RegisterAssemblyRecursively(refAsm);
218 if (refName.Name == "Tizen.NUI.Xaml.Core")
220 if (refAsm.GetType("Tizen.NUI.Xaml.PlatformConfiguration.TizenSpecific.VisualElement") != null)
222 IsTizenSpecificAvailable = true;
229 Log.Warn("Reference Assembly can not be loaded. {0}", refName.FullName);
235 public Assembly[] GetAssemblies()
237 return _assemblies.ToArray();