using System; using System.IO; using System.Reflection; using System.Collections.Generic; using System.Security.Cryptography; using System.Threading; using System.Threading.Tasks; using System.Net.Http; using ElmSharp; namespace Xamarin.Forms.Platform.Tizen { internal class TizenPlatformServices : IPlatformServices { static MD5 checksum = MD5.Create(); static SynchronizationContext s_context; public TizenPlatformServices() { s_context = SynchronizationContext.Current; } public class _Timer : ITimer { readonly Timer timer; public _Timer(Timer timer) { this.timer = timer; } #region ITimer implementation public void Change(int dueTime, int period) { timer.Change(dueTime, period); } public void Change(long dueTime, long period) { timer.Change((int)dueTime, (int)period); } public void Change(TimeSpan dueTime, TimeSpan period) { timer.Change(dueTime, period); } public void Change(uint dueTime, uint period) { timer.Change((int)dueTime, (int)period); } #endregion } public class Ticker : Xamarin.Forms.Internals.Ticker { readonly ITimer _timer; public Ticker() { _timer = new _Timer(new Timer((object o) => HandleElapsed(o), this, Timeout.Infinite, Timeout.Infinite)); } protected override void EnableTimer() { _timer.Change(16, 16); } protected override void DisableTimer() { _timer.Change(-1, -1); } void HandleElapsed(object state) { s_context.Post((o) => SendSignals(-1), null); } } #region IPlatformServices implementation public double GetNamedSize(NamedSize size, Type targetElementType, bool useOldSizes) { switch (size) { case NamedSize.Micro: return 10; case NamedSize.Small: return 12; case NamedSize.Default: case NamedSize.Medium: return 14; case NamedSize.Large: return 18; default: throw new ArgumentOutOfRangeException(); } } public void OpenUriAction(Uri uri) { throw new NotImplementedException(); } public void BeginInvokeOnMainThread(Action action) { if (EcoreMainloop.IsMainThread) { action(); } else { s_context.Post((o) => action(), null); } } public Xamarin.Forms.Internals.Ticker CreateTicker() { return new Ticker(); } public void StartTimer(TimeSpan interval, Func callback) { Timer timer = null; bool invoking = false; TimerCallback onTimeout = o => { if (!invoking) { invoking = true; BeginInvokeOnMainThread(() => { if (!callback()) { timer.Dispose(); } invoking = false; } ); } }; timer = new Timer(onTimeout, null, Timeout.Infinite, Timeout.Infinite); // set interval separarately to prevent calling onTimeout before `timer' is assigned timer.Change(interval, interval); } public async Task GetStreamAsync(Uri uri, CancellationToken cancellationToken) { using (var client = new HttpClient()) using (HttpResponseMessage response = await client.GetAsync(uri, cancellationToken)) return await response.Content.ReadAsStreamAsync(); } public Assembly[] GetAssemblies() { return AppDomain.CurrentDomain.GetAssemblies(); } public ITimer CreateTimer(Action callback, object state, int dueTime, int period) { return new _Timer(new Timer((object o) => callback(o), state, dueTime, period)); } public ITimer CreateTimer(Action callback, object state, TimeSpan dueTime, TimeSpan period) { return new _Timer(new Timer((object o) => callback(o), state, dueTime, period)); } public IIsolatedStorageFile GetUserStoreForApplication() { return new TizenIsolatedStorageFile(); } static readonly char[] HexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; public string GetMD5Hash(string input) { byte[] bin = checksum.ComputeHash(System.Text.Encoding.UTF8.GetBytes(input)); char[] hex = new char[32]; for (var i = 0; i < 16; ++i) { hex[2 * i] = HexDigits[bin[i] >> 4]; hex[2 * i + 1] = HexDigits[bin[i] & 0xf]; } return new string(hex); } public bool IsInvokeRequired { get { return !EcoreMainloop.IsMainThread; } } #endregion #if !NET45 // In .NETCore, AppDomain is not supported. The list of the assemblies should be generated manually. internal class AppDomain { public static AppDomain CurrentDomain { get; private set; } List _assemblies; static AppDomain() { CurrentDomain = new AppDomain(); } AppDomain() { _assemblies = new List(); // Add this renderer assembly to the list RegisterAssembly(GetType().GetTypeInfo().Assembly); } internal void RegisterAssembly(Assembly asm) { if (!_assemblies.Contains(asm)) { _assemblies.Add(asm); } } public Assembly[] GetAssemblies() { return _assemblies.ToArray(); } } #endif } }