summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Tizen/TizenPlatformServices.cs
blob: 449b0938c1931983d394889cdf5ebf9c23d4d8bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
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 Xamarin.Forms.Internals;
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 TizenTicker : Ticker
		{
			readonly Timer _timer;

			public TizenTicker()
			{
				_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)
		{
			// In case of TV profile The base named size sholud be lager than mobile profile
			if (Device.Idiom != TargetIdiom.Phone)
			{
				switch (size)
				{
					case NamedSize.Micro:
						return Forms.ConvertToDPFont(24);
					case NamedSize.Small:
						return Forms.ConvertToDPFont(26);
					case NamedSize.Default:
					case NamedSize.Medium:
						return Forms.ConvertToDPFont(28);
					case NamedSize.Large:
						return Forms.ConvertToDPFont(84);
					default:
						throw new ArgumentOutOfRangeException();
				}

			}

			double baseSize = Forms.ConvertToDPFont(19);
			double baseSizeSpan = 3;
			switch (size)
			{
				case NamedSize.Micro:
					return baseSize;
				case NamedSize.Small:
					return baseSize + baseSizeSpan;
				case NamedSize.Default:
				case NamedSize.Medium:
					return baseSize + (baseSizeSpan * 2);
				case NamedSize.Large:
					return baseSize + (baseSizeSpan * 4);
				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 Ticker CreateTicker()
		{
			return new TizenTicker();
		}

		public void StartTimer(TimeSpan interval, Func<bool> 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<Stream> 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 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;
			}
		}

		public string RuntimePlatform => Device.Tizen;

		#endregion

		// 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<Assembly> _assemblies;

			public static bool IsTizenSpecificAvailable { get; private set; }

			static AppDomain()
			{
				CurrentDomain = new AppDomain();
			}

			AppDomain()
			{
				_assemblies = new List<Assembly>();

				// Add this renderer assembly to the list
				_assemblies.Add(GetType().GetTypeInfo().Assembly);
			}

			internal void RegisterAssemblyRecursively(Assembly asm)
			{
				if (_assemblies.Contains(asm))
					return;

				_assemblies.Add(asm);

				foreach (var refName in asm.GetReferencedAssemblies())
				{
					if (!refName.Name.StartsWith("System.") && !refName.Name.StartsWith("Microsoft.") && !refName.Name.StartsWith("mscorlib"))
					{
						try
						{
							Assembly refAsm = Assembly.Load(refName);
							RegisterAssemblyRecursively(refAsm);
							if (refName.Name == "Xamarin.Forms.Core")
							{
								if (refAsm.GetType("Xamarin.Forms.PlatformConfiguration.TizenSpecific.VisualElement") != null)
								{
									IsTizenSpecificAvailable = true;
								}
							}
						}
						catch
						{
							Log.Warn("Reference Assembly can not be loaded. {0}", refName.FullName);
						}
					}
				}
			}

			public Assembly[] GetAssemblies()
			{
				return _assemblies.ToArray();
			}
		}
	}
}