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;
41 /// Initializes the CoreApplication class.
43 /// <param name="backend">The backend instance implementing ICoreBacked interface.</param>
44 /// <since_tizen> 3 </since_tizen>
45 public CoreApplication(ICoreBackend backend)
52 /// Initializes the CoreApplication class.
54 /// <param name="backend">The backend instance implementing ICoreBackend interface.</param>
55 /// <param name="task">The backend instance implmenting ICoreTask interface.</param>
56 /// <since_tizen> 10 </since_tizen>
57 [EditorBrowsable(EditorBrowsableState.Never)]
58 public CoreApplication(ICoreBackend backend, ICoreTask task)
65 /// Occurs when the application is launched.
67 /// <since_tizen> 3 </since_tizen>
68 public event EventHandler Created;
71 /// Occurs when the application is about to shutdown.
73 /// <since_tizen> 3 </since_tizen>
74 public event EventHandler Terminated;
77 /// Occurs whenever the application receives the appcontrol message.
79 /// <since_tizen> 3 </since_tizen>
80 public event EventHandler<AppControlReceivedEventArgs> AppControlReceived;
83 /// Occurs when the system memory is low.
85 /// <since_tizen> 3 </since_tizen>
86 public event EventHandler<LowMemoryEventArgs> LowMemory;
89 /// Occurs when the system battery is low.
91 /// <since_tizen> 3 </since_tizen>
92 public event EventHandler<LowBatteryEventArgs> LowBattery;
95 /// Occurs when the system language is chagned.
97 /// <since_tizen> 3 </since_tizen>
98 public event EventHandler<LocaleChangedEventArgs> LocaleChanged;
101 /// Occurs when the region format is changed.
103 /// <since_tizen> 3 </since_tizen>
104 public event EventHandler<RegionFormatChangedEventArgs> RegionFormatChanged;
107 /// Occurs when the device orientation is changed.
109 /// <since_tizen> 3 </since_tizen>
110 public event EventHandler<DeviceOrientationEventArgs> DeviceOrientationChanged;
113 /// The backend instance.
115 /// <since_tizen> 3 </since_tizen>
116 protected ICoreBackend Backend { get { return _backend; } }
119 /// Runs the application's main loop.
121 /// <param name="args">Arguments from commandline.</param>
122 /// <since_tizen> 3 </since_tizen>
123 public override void Run(string[] args)
127 _backend.AddEventHandler(EventType.Created, OnCreate);
128 _backend.AddEventHandler(EventType.Terminated, OnTerminate);
129 _backend.AddEventHandler<AppControlReceivedEventArgs>(EventType.AppControlReceived, OnAppControlReceived);
130 _backend.AddEventHandler<LowMemoryEventArgs>(EventType.LowMemory, OnLowMemory);
131 _backend.AddEventHandler<LowBatteryEventArgs>(EventType.LowBattery, OnLowBattery);
132 _backend.AddEventHandler<LocaleChangedEventArgs>(EventType.LocaleChanged, OnLocaleChanged);
133 _backend.AddEventHandler<RegionFormatChangedEventArgs>(EventType.RegionFormatChanged, OnRegionFormatChanged);
134 _backend.AddEventHandler<DeviceOrientationEventArgs>(EventType.DeviceOrientationChanged, OnDeviceOrientationChanged);
136 string[] argsClone = new string[args == null ? 1 : args.Length + 1];
137 if (args != null && args.Length > 1)
139 args.CopyTo(argsClone, 1);
141 argsClone[0] = string.Empty;
145 ICoreTaskBackend backend = (ICoreTaskBackend)_backend;
146 backend.SetCoreTask(_task);
147 backend.Run(argsClone);
151 _backend.Run(argsClone);
156 /// Exits the main loop of the application.
158 /// <since_tizen> 3 </since_tizen>
159 public override void Exit()
165 /// Overrides this method if want to handle behavior when the application is launched.
166 /// If base.OnCreated() is not called, the event 'Created' will not be emitted.
168 /// <since_tizen> 3 </since_tizen>
169 protected virtual void OnCreate()
173 TizenUISynchronizationContext.Initialize();
176 if (!GlobalizationMode.Invariant)
178 string locale = ULocale.GetDefaultLocale();
179 ChangeCurrentUICultureInfo(locale);
180 ChangeCurrentCultureInfo(locale);
184 Log.Warn(LogTag, "Run in invariant mode");
187 Created?.Invoke(this, EventArgs.Empty);
191 /// Overrides this method if want to handle behavior when the application is terminated.
192 /// If base.OnTerminate() is not called, the event 'Terminated' will not be emitted.
194 /// <since_tizen> 3 </since_tizen>
195 protected virtual void OnTerminate()
197 Terminated?.Invoke(this, EventArgs.Empty);
201 /// Overrides this method if want to handle behavior when the application receives the appcontrol message.
202 /// If base.OnAppControlReceived() is not called, the event 'AppControlReceived' will not be emitted.
204 /// <param name="e"></param>
205 /// <since_tizen> 3 </since_tizen>
206 protected virtual void OnAppControlReceived(AppControlReceivedEventArgs e)
208 AppControlReceived?.Invoke(this, e);
212 /// Overrides this method if want to handle behavior when the system memory is low.
213 /// If base.OnLowMemory() is not called, the event 'LowMemory' will not be emitted.
215 /// <param name="e">The low memory event argument</param>
216 /// <since_tizen> 3 </since_tizen>
217 protected virtual void OnLowMemory(LowMemoryEventArgs e)
219 LowMemory?.Invoke(this, e);
220 if (e.LowMemoryStatus == LowMemoryStatus.SoftWarning || e.LowMemoryStatus == LowMemoryStatus.HardWarning)
227 /// Overrides this method if want to handle behavior when the system battery is low.
228 /// If base.OnLowBattery() is not called, the event 'LowBattery' will not be emitted.
230 /// <param name="e">The low battery event argument</param>
231 /// <since_tizen> 3 </since_tizen>
232 protected virtual void OnLowBattery(LowBatteryEventArgs e)
234 LowBattery?.Invoke(this, e);
238 /// Overrides this method if want to handle behavior when the system language is changed.
239 /// If base.OnLocaleChanged() is not called, the event 'LocaleChanged' will not be emitted.
241 /// <param name="e">The locale changed event argument</param>
242 /// <since_tizen> 3 </since_tizen>
243 protected virtual void OnLocaleChanged(LocaleChangedEventArgs e)
245 if (!GlobalizationMode.Invariant)
247 ChangeCurrentUICultureInfo(e.Locale);
250 LocaleChanged?.Invoke(this, e);
254 /// Overrides this method if want to handle behavior when the region format is changed.
255 /// If base.OnRegionFormatChanged() is not called, the event 'RegionFormatChanged' will not be emitted.
257 /// <param name="e">The region format changed event argument</param>
258 /// <since_tizen> 3 </since_tizen>
259 protected virtual void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
261 if (!GlobalizationMode.Invariant)
263 ChangeCurrentCultureInfo(e.Region);
266 RegionFormatChanged?.Invoke(this, e);
270 /// Overrides this method if want to handle behavior when the device orientation is changed.
271 /// If base.OnRegionFormatChanged() is not called, the event 'RegionFormatChanged' will not be emitted.
273 /// <param name="e">The device orientation changed event argument</param>
274 /// <since_tizen> 3 </since_tizen>
275 protected virtual void OnDeviceOrientationChanged(DeviceOrientationEventArgs e)
277 DeviceOrientationChanged?.Invoke(this, e);
281 /// Dispatches an asynchronous message to a main loop of the CoreApplication.
284 /// If an application uses UI thread App Model, the asynchronous message will be delivered to the UI thread.
285 /// If not, the asynchronous message will be delivered to the main thread.
287 /// <param name="runner">The runner callaback.</param>
288 /// <exception cref="ArgumentNullException">Thrown when the runner is null.</exception>
289 /// <since_tizen> 10 </since_tizen>
290 [EditorBrowsable(EditorBrowsableState.Never)]
291 public static void Post(Action runner)
295 throw new ArgumentNullException(nameof(runner));
298 GSourceManager.Post(runner, true);
302 /// Dispatches an asynchronous message to a main loop of the CoreApplication.
305 /// If an application uses UI thread App Model, the asynchronous message will be delivered to the UI thread.
306 /// If not, the asynchronous message will be delivered to the main thread.
308 /// <typeparam name="T">The type of the result.</typeparam>
309 /// <param name="runner">The runner callback.</param>
310 /// <exception cref="ArgumentNullException">Thrown when the runner is null.</exception>
311 /// <returns>A task with the result.</returns>
312 /// <since_tizen> 10 </since_tizen>
313 [EditorBrowsable(EditorBrowsableState.Never)]
314 public static async Task<T> Post<T>(Func<T> runner)
318 throw new ArgumentNullException(nameof(runner));
321 var task = new TaskCompletionSource<T>();
322 GSourceManager.Post(() => { task.SetResult(runner()); }, true);
323 return await task.Task.ConfigureAwait(false);
327 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
329 /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
330 /// <since_tizen> 3 </since_tizen>
331 protected override void Dispose(bool disposing)
340 _disposedValue = true;
342 base.Dispose(disposing);
345 private CultureInfo ConvertCultureInfo(string locale)
347 ULocale pLocale = new ULocale(locale);
348 string cultureName = CultureInfoHelper.GetCultureName(pLocale.Locale.Replace("_", "-"));
350 if (!string.IsNullOrEmpty(cultureName))
354 return new CultureInfo(cultureName);
356 catch (CultureNotFoundException)
358 Log.Error(LogTag, "CultureNotFoundException occurs. CultureName: " + cultureName);
364 return new CultureInfo(pLocale.LCID);
366 catch (ArgumentOutOfRangeException)
368 return GetFallbackCultureInfo(pLocale);
370 catch (CultureNotFoundException)
372 return GetFallbackCultureInfo(pLocale);
376 private void ChangeCurrentCultureInfo(string locale)
378 CultureInfo cultureInfo = ConvertCultureInfo(locale);
379 if (cultureInfo != null)
381 CultureInfo.CurrentCulture = cultureInfo;
385 Log.Error(LogTag, "CultureInfo is null. locale: " + locale);
389 private void ChangeCurrentUICultureInfo(string locale)
391 CultureInfo cultureInfo = ConvertCultureInfo(locale);
392 if (cultureInfo != null)
394 CultureInfo.CurrentUICulture = cultureInfo;
398 Log.Error(LogTag, "CultureInfo is null. locale: " + locale);
402 private bool ExistCultureInfo(string locale)
404 foreach (var cultureInfo in CultureInfo.GetCultures(CultureTypes.AllCultures))
406 if (cultureInfo.Name == locale)
415 private CultureInfo GetCultureInfo(string locale)
417 if (!ExistCultureInfo(locale))
424 return new CultureInfo(locale);
426 catch (CultureNotFoundException)
432 private CultureInfo GetFallbackCultureInfo(ULocale uLocale)
434 CultureInfo fallbackCultureInfo = null;
435 string locale = string.Empty;
437 if (uLocale.Locale != null)
439 locale = uLocale.Locale.Replace("_", "-");
440 fallbackCultureInfo = GetCultureInfo(locale);
443 if (fallbackCultureInfo == null && uLocale.Language != null && uLocale.Script != null && uLocale.Country != null)
445 locale = uLocale.Language + "-" + uLocale.Script + "-" + uLocale.Country;
446 fallbackCultureInfo = GetCultureInfo(locale);
449 if (fallbackCultureInfo == null && uLocale.Language != null && uLocale.Script != null)
451 locale = uLocale.Language + "-" + uLocale.Script;
452 fallbackCultureInfo = GetCultureInfo(locale);
455 if (fallbackCultureInfo == null && uLocale.Language != null && uLocale.Country != null)
457 locale = uLocale.Language + "-" + uLocale.Country;
458 fallbackCultureInfo = GetCultureInfo(locale);
461 if (fallbackCultureInfo == null && uLocale.Language != null)
463 locale = uLocale.Language;
464 fallbackCultureInfo = GetCultureInfo(locale);
467 if (fallbackCultureInfo == null)
471 fallbackCultureInfo = new CultureInfo("en");
473 catch (CultureNotFoundException e)
475 Log.Error(LogTag, "Failed to create CultureInfo. err = " + e.Message);
479 return fallbackCultureInfo;
483 internal static class GlobalizationMode
485 private static int _invariant = -1;
487 internal static bool Invariant
491 if (_invariant == -1)
493 string value = Environment.GetEnvironmentVariable("DOTNET_SYSTEM_GLOBALIZATION_INVARIANT");
494 _invariant = value != null ? (value.Equals("1") ? 1 : 0) : 0;
497 return _invariant != 0;
502 internal class ULocale
504 private const int ULOC_FULLNAME_CAPACITY = 157;
505 private const int ULOC_LANG_CAPACITY = 12;
506 private const int ULOC_SCRIPT_CAPACITY = 6;
507 private const int ULOC_COUNTRY_CAPACITY = 4;
508 private const int ULOC_VARIANT_CAPACITY = ULOC_FULLNAME_CAPACITY;
510 internal ULocale(string locale)
512 Locale = Canonicalize(locale);
513 Language = GetLanguage(Locale);
514 Script = GetScript(Locale);
515 Country = GetCountry(Locale);
516 Variant = GetVariant(Locale);
517 LCID = GetLCID(Locale);
520 internal string Locale { get; private set; }
521 internal string Language { get; private set; }
522 internal string Script { get; private set; }
523 internal string Country { get; private set; }
524 internal string Variant { get; private set; }
525 internal int LCID { get; private set; }
527 private string Canonicalize(string localeName)
529 // Get the locale name from ICU
530 StringBuilder sb = new StringBuilder(ULOC_FULLNAME_CAPACITY);
531 if (Interop.BaseUtilsi18n.Canonicalize(localeName, sb, sb.Capacity) <= 0)
536 return sb.ToString();
539 private string GetLanguage(string locale)
541 // Get the language name from ICU
542 StringBuilder sb = new StringBuilder(ULOC_LANG_CAPACITY);
543 if (Interop.BaseUtilsi18n.GetLanguage(locale, sb, sb.Capacity, out int bufSizeLanguage) != 0)
548 return sb.ToString();
551 private string GetScript(string locale)
553 // Get the script name from ICU
554 StringBuilder sb = new StringBuilder(ULOC_SCRIPT_CAPACITY);
555 if (Interop.BaseUtilsi18n.GetScript(locale, sb, sb.Capacity) <= 0)
560 return sb.ToString();
563 private string GetCountry(string locale)
567 // Get the country name from ICU
568 StringBuilder sb = new StringBuilder(ULOC_COUNTRY_CAPACITY);
569 if (Interop.BaseUtilsi18n.GetCountry(locale, sb, sb.Capacity, out err) <= 0)
574 return sb.ToString();
577 private string GetVariant(string locale)
579 // Get the variant name from ICU
580 StringBuilder sb = new StringBuilder(ULOC_VARIANT_CAPACITY);
581 if (Interop.BaseUtilsi18n.GetVariant(locale, sb, sb.Capacity) <= 0)
586 return sb.ToString();
589 private int GetLCID(string locale)
591 // Get the LCID from ICU
592 uint lcid = Interop.BaseUtilsi18n.GetLCID(locale);
596 internal static string GetDefaultLocale()
598 IntPtr stringPtr = IntPtr.Zero;
599 if (Interop.BaseUtilsi18n.GetDefault(out stringPtr) != 0)
604 if (stringPtr == IntPtr.Zero)
609 return Marshal.PtrToStringAnsi(stringPtr);