2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Collections.Concurrent;
19 using System.ComponentModel;
20 using System.Globalization;
21 using System.Runtime.InteropServices;
23 using System.Threading;
24 using System.Threading.Tasks;
26 using Tizen.Applications.CoreBackend;
28 namespace Tizen.Applications
31 /// This class represents an application controlled lifecycles by the backend system.
33 /// <since_tizen> 3 </since_tizen>
34 public class CoreApplication : Application
36 private readonly ICoreBackend _backend;
37 private readonly ICoreTask _task;
38 private bool _disposedValue = false;
40 private static System.Timers.Timer sTimer;
43 /// Initializes the CoreApplication class.
45 /// <param name="backend">The backend instance implementing ICoreBacked interface.</param>
46 /// <since_tizen> 3 </since_tizen>
47 public CoreApplication(ICoreBackend backend)
54 /// Initializes the CoreApplication class.
56 /// <param name="backend">The backend instance implementing ICoreBackend interface.</param>
57 /// <param name="task">The backend instance implmenting ICoreTask interface.</param>
58 /// <since_tizen> 10 </since_tizen>
59 [EditorBrowsable(EditorBrowsableState.Never)]
60 public CoreApplication(ICoreBackend backend, ICoreTask task)
67 /// Occurs when the application is launched.
69 /// <since_tizen> 3 </since_tizen>
70 public event EventHandler Created;
73 /// Occurs when the application is about to shutdown.
75 /// <since_tizen> 3 </since_tizen>
76 public event EventHandler Terminated;
79 /// Occurs whenever the application receives the appcontrol message.
81 /// <since_tizen> 3 </since_tizen>
82 public event EventHandler<AppControlReceivedEventArgs> AppControlReceived;
85 /// Occurs when the system memory is low.
87 /// <since_tizen> 3 </since_tizen>
88 public event EventHandler<LowMemoryEventArgs> LowMemory;
91 /// Occurs when the system battery is low.
93 /// <since_tizen> 3 </since_tizen>
94 public event EventHandler<LowBatteryEventArgs> LowBattery;
97 /// Occurs when the system language is chagned.
99 /// <since_tizen> 3 </since_tizen>
100 public event EventHandler<LocaleChangedEventArgs> LocaleChanged;
103 /// Occurs when the region format is changed.
105 /// <since_tizen> 3 </since_tizen>
106 public event EventHandler<RegionFormatChangedEventArgs> RegionFormatChanged;
109 /// Occurs when the device orientation is changed.
111 /// <since_tizen> 3 </since_tizen>
112 public event EventHandler<DeviceOrientationEventArgs> DeviceOrientationChanged;
115 /// The backend instance.
117 /// <since_tizen> 3 </since_tizen>
118 protected ICoreBackend Backend { get { return _backend; } }
121 /// Runs the application's main loop.
123 /// <param name="args">Arguments from commandline.</param>
124 /// <since_tizen> 3 </since_tizen>
125 public override void Run(string[] args)
129 _backend.AddEventHandler(EventType.Created, OnCreate);
130 _backend.AddEventHandler(EventType.Terminated, OnTerminate);
131 _backend.AddEventHandler<AppControlReceivedEventArgs>(EventType.AppControlReceived, OnAppControlReceived);
132 _backend.AddEventHandler<LowMemoryEventArgs>(EventType.LowMemory, OnLowMemory);
133 _backend.AddEventHandler<LowBatteryEventArgs>(EventType.LowBattery, OnLowBattery);
134 _backend.AddEventHandler<LocaleChangedEventArgs>(EventType.LocaleChanged, OnLocaleChanged);
135 _backend.AddEventHandler<RegionFormatChangedEventArgs>(EventType.RegionFormatChanged, OnRegionFormatChanged);
136 _backend.AddEventHandler<DeviceOrientationEventArgs>(EventType.DeviceOrientationChanged, OnDeviceOrientationChanged);
138 string[] argsClone = new string[args == null ? 1 : args.Length + 1];
139 if (args != null && args.Length > 1)
141 args.CopyTo(argsClone, 1);
143 argsClone[0] = string.Empty;
147 ICoreTaskBackend backend = (ICoreTaskBackend)_backend;
148 backend.SetCoreTask(_task);
149 backend.Run(argsClone);
153 _backend.Run(argsClone);
158 /// Exits the main loop of the application.
160 /// <since_tizen> 3 </since_tizen>
161 public override void Exit()
167 /// Overrides this method if want to handle behavior when the application is launched.
168 /// If base.OnCreated() is not called, the event 'Created' will not be emitted.
170 /// <since_tizen> 3 </since_tizen>
171 protected virtual void OnCreate()
175 TizenUISynchronizationContext.Initialize();
178 if (!GlobalizationMode.Invariant)
180 string locale = ULocale.GetDefaultLocale();
181 ChangeCurrentUICultureInfo(locale);
182 ChangeCurrentCultureInfo(locale);
186 Log.Warn(LogTag, "Run in invariant mode");
189 Created?.Invoke(this, EventArgs.Empty);
193 /// Overrides this method if want to handle behavior when the application is terminated.
194 /// If base.OnTerminate() is not called, the event 'Terminated' will not be emitted.
196 /// <since_tizen> 3 </since_tizen>
197 protected virtual void OnTerminate()
199 Terminated?.Invoke(this, EventArgs.Empty);
203 /// Overrides this method if want to handle behavior when the application receives the appcontrol message.
204 /// If base.OnAppControlReceived() is not called, the event 'AppControlReceived' will not be emitted.
206 /// <param name="e"></param>
207 /// <since_tizen> 3 </since_tizen>
208 protected virtual void OnAppControlReceived(AppControlReceivedEventArgs e)
210 AppControlReceived?.Invoke(this, e);
214 /// Overrides this method if want to handle behavior when the system memory is low.
215 /// If base.OnLowMemory() is not called, the event 'LowMemory' will not be emitted.
217 /// <param name="e">The low memory event argument</param>
218 /// <since_tizen> 3 </since_tizen>
219 protected virtual void OnLowMemory(LowMemoryEventArgs e)
221 LowMemory?.Invoke(this, e);
222 if (e.LowMemoryStatus == LowMemoryStatus.SoftWarning || e.LowMemoryStatus == LowMemoryStatus.HardWarning)
229 /// Overrides this method if want to handle behavior when the system battery is low.
230 /// If base.OnLowBattery() is not called, the event 'LowBattery' will not be emitted.
232 /// <param name="e">The low battery event argument</param>
233 /// <since_tizen> 3 </since_tizen>
234 protected virtual void OnLowBattery(LowBatteryEventArgs e)
236 LowBattery?.Invoke(this, e);
240 /// Overrides this method if want to handle behavior when the system language is changed.
241 /// If base.OnLocaleChanged() is not called, the event 'LocaleChanged' will not be emitted.
243 /// <param name="e">The locale changed event argument</param>
244 /// <since_tizen> 3 </since_tizen>
245 protected virtual void OnLocaleChanged(LocaleChangedEventArgs e)
247 if (!GlobalizationMode.Invariant)
249 ChangeCurrentUICultureInfo(e.Locale);
252 LocaleChanged?.Invoke(this, e);
256 /// Overrides this method if want to handle behavior when the region format is changed.
257 /// If base.OnRegionFormatChanged() is not called, the event 'RegionFormatChanged' will not be emitted.
259 /// <param name="e">The region format changed event argument</param>
260 /// <since_tizen> 3 </since_tizen>
261 protected virtual void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
263 if (!GlobalizationMode.Invariant)
265 ChangeCurrentCultureInfo(e.Region);
268 RegionFormatChanged?.Invoke(this, e);
272 /// Overrides this method if want to handle behavior when the device orientation is changed.
273 /// If base.OnRegionFormatChanged() is not called, the event 'RegionFormatChanged' will not be emitted.
275 /// <param name="e">The device orientation changed event argument</param>
276 /// <since_tizen> 3 </since_tizen>
277 protected virtual void OnDeviceOrientationChanged(DeviceOrientationEventArgs e)
279 DeviceOrientationChanged?.Invoke(this, e);
283 /// Dispatches an asynchronous message to a main loop of the CoreApplication.
286 /// If an application uses UI thread App Model, the asynchronous message will be delivered to the UI thread.
287 /// If not, the asynchronous message will be delivered to the main thread.
289 /// <param name="runner">The runner callaback.</param>
290 /// <exception cref="ArgumentNullException">Thrown when the runner is null.</exception>
291 /// <since_tizen> 10 </since_tizen>
292 [EditorBrowsable(EditorBrowsableState.Never)]
293 public static void Post(Action runner)
297 throw new ArgumentNullException(nameof(runner));
300 GSourceManager.Post(runner, true);
304 /// Dispatches an asynchronous message to a main loop of the CoreApplication.
307 /// If an application uses UI thread App Model, the asynchronous message will be delivered to the UI thread.
308 /// If not, the asynchronous message will be delivered to the main thread.
310 /// <typeparam name="T">The type of the result.</typeparam>
311 /// <param name="runner">The runner callback.</param>
312 /// <exception cref="ArgumentNullException">Thrown when the runner is null.</exception>
313 /// <returns>A task with the result.</returns>
314 /// <since_tizen> 10 </since_tizen>
315 [EditorBrowsable(EditorBrowsableState.Never)]
316 public static async Task<T> Post<T>(Func<T> runner)
320 throw new ArgumentNullException(nameof(runner));
323 var task = new TaskCompletionSource<T>();
324 GSourceManager.Post(() => { task.SetResult(runner()); }, true);
325 return await task.Task.ConfigureAwait(false);
329 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
331 /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
332 /// <since_tizen> 3 </since_tizen>
333 protected override void Dispose(bool disposing)
342 _disposedValue = true;
344 base.Dispose(disposing);
347 private CultureInfo ConvertCultureInfo(string locale)
349 ULocale pLocale = new ULocale(locale);
350 string cultureName = CultureInfoHelper.GetCultureName(pLocale.Locale.Replace("_", "-"));
352 if (!string.IsNullOrEmpty(cultureName))
356 return new CultureInfo(cultureName);
358 catch (CultureNotFoundException)
360 Log.Error(LogTag, "CultureNotFoundException occurs. CultureName: " + cultureName);
366 return new CultureInfo(pLocale.LCID);
368 catch (ArgumentOutOfRangeException)
370 return GetFallbackCultureInfo(pLocale);
372 catch (CultureNotFoundException)
374 return GetFallbackCultureInfo(pLocale);
378 private void ChangeCurrentCultureInfo(string locale)
380 CultureInfo cultureInfo = ConvertCultureInfo(locale);
381 if (cultureInfo != null)
383 CultureInfo.CurrentCulture = cultureInfo;
387 Log.Error(LogTag, "CultureInfo is null. locale: " + locale);
391 private void ChangeCurrentUICultureInfo(string locale)
393 CultureInfo cultureInfo = ConvertCultureInfo(locale);
394 if (cultureInfo != null)
396 CultureInfo.CurrentUICulture = cultureInfo;
400 Log.Error(LogTag, "CultureInfo is null. locale: " + locale);
404 private bool ExistCultureInfo(string locale)
406 foreach (var cultureInfo in CultureInfo.GetCultures(CultureTypes.AllCultures))
408 if (cultureInfo.Name == locale)
417 private CultureInfo GetCultureInfo(string locale)
419 if (!ExistCultureInfo(locale))
426 return new CultureInfo(locale);
428 catch (CultureNotFoundException)
434 private CultureInfo GetFallbackCultureInfo(ULocale uLocale)
436 CultureInfo fallbackCultureInfo = null;
437 string locale = string.Empty;
439 if (uLocale.Locale != null)
441 locale = uLocale.Locale.Replace("_", "-");
442 fallbackCultureInfo = GetCultureInfo(locale);
445 if (fallbackCultureInfo == null && uLocale.Language != null && uLocale.Script != null && uLocale.Country != null)
447 locale = uLocale.Language + "-" + uLocale.Script + "-" + uLocale.Country;
448 fallbackCultureInfo = GetCultureInfo(locale);
451 if (fallbackCultureInfo == null && uLocale.Language != null && uLocale.Script != null)
453 locale = uLocale.Language + "-" + uLocale.Script;
454 fallbackCultureInfo = GetCultureInfo(locale);
457 if (fallbackCultureInfo == null && uLocale.Language != null && uLocale.Country != null)
459 locale = uLocale.Language + "-" + uLocale.Country;
460 fallbackCultureInfo = GetCultureInfo(locale);
463 if (fallbackCultureInfo == null && uLocale.Language != null)
465 locale = uLocale.Language;
466 fallbackCultureInfo = GetCultureInfo(locale);
469 if (fallbackCultureInfo == null)
473 fallbackCultureInfo = new CultureInfo("en");
475 catch (CultureNotFoundException e)
477 Log.Error(LogTag, "Failed to create CultureInfo. err = " + e.Message);
481 return fallbackCultureInfo;
485 internal static class GlobalizationMode
487 private static int _invariant = -1;
489 internal static bool Invariant
493 if (_invariant == -1)
495 string value = Environment.GetEnvironmentVariable("DOTNET_SYSTEM_GLOBALIZATION_INVARIANT");
496 _invariant = value != null ? (value.Equals("1") ? 1 : 0) : 0;
499 return _invariant != 0;
504 internal class ULocale
506 private const int ULOC_FULLNAME_CAPACITY = 157;
507 private const int ULOC_LANG_CAPACITY = 12;
508 private const int ULOC_SCRIPT_CAPACITY = 6;
509 private const int ULOC_COUNTRY_CAPACITY = 4;
510 private const int ULOC_VARIANT_CAPACITY = ULOC_FULLNAME_CAPACITY;
512 internal ULocale(string locale)
514 Locale = Canonicalize(locale);
515 Language = GetLanguage(Locale);
516 Script = GetScript(Locale);
517 Country = GetCountry(Locale);
518 Variant = GetVariant(Locale);
519 LCID = GetLCID(Locale);
522 internal string Locale { get; private set; }
523 internal string Language { get; private set; }
524 internal string Script { get; private set; }
525 internal string Country { get; private set; }
526 internal string Variant { get; private set; }
527 internal int LCID { get; private set; }
529 private string Canonicalize(string localeName)
531 // Get the locale name from ICU
532 StringBuilder sb = new StringBuilder(ULOC_FULLNAME_CAPACITY);
533 if (Interop.BaseUtilsi18n.Canonicalize(localeName, sb, sb.Capacity) <= 0)
538 return sb.ToString();
541 private string GetLanguage(string locale)
543 // Get the language name from ICU
544 StringBuilder sb = new StringBuilder(ULOC_LANG_CAPACITY);
545 if (Interop.BaseUtilsi18n.GetLanguage(locale, sb, sb.Capacity, out int bufSizeLanguage) != 0)
550 return sb.ToString();
553 private string GetScript(string locale)
555 // Get the script name from ICU
556 StringBuilder sb = new StringBuilder(ULOC_SCRIPT_CAPACITY);
557 if (Interop.BaseUtilsi18n.GetScript(locale, sb, sb.Capacity) <= 0)
562 return sb.ToString();
565 private string GetCountry(string locale)
569 // Get the country name from ICU
570 StringBuilder sb = new StringBuilder(ULOC_COUNTRY_CAPACITY);
571 if (Interop.BaseUtilsi18n.GetCountry(locale, sb, sb.Capacity, out err) <= 0)
576 return sb.ToString();
579 private string GetVariant(string locale)
581 // Get the variant name from ICU
582 StringBuilder sb = new StringBuilder(ULOC_VARIANT_CAPACITY);
583 if (Interop.BaseUtilsi18n.GetVariant(locale, sb, sb.Capacity) <= 0)
588 return sb.ToString();
591 private int GetLCID(string locale)
593 // Get the LCID from ICU
594 uint lcid = Interop.BaseUtilsi18n.GetLCID(locale);
598 internal static string GetDefaultLocale()
600 IntPtr stringPtr = IntPtr.Zero;
601 if (Interop.BaseUtilsi18n.GetDefault(out stringPtr) != 0)
606 if (stringPtr == IntPtr.Zero)
611 return Marshal.PtrToStringAnsi(stringPtr);