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.Globalization;
19 using System.Runtime.InteropServices;
22 using Tizen.Applications.CoreBackend;
24 namespace Tizen.Applications
27 /// This class represents an application controlled lifecycles by the backend system.
29 /// <since_tizen> 3 </since_tizen>
30 public class CoreApplication : Application
32 private readonly ICoreBackend _backend;
33 private bool _disposedValue = false;
35 private static Timer sTimer;
38 /// Initializes the CoreApplication class.
40 /// <param name="backend">The backend instance implementing ICoreBacked interface.</param>
41 /// <since_tizen> 3 </since_tizen>
42 public CoreApplication(ICoreBackend backend)
48 /// Occurs when the application is launched.
50 /// <since_tizen> 3 </since_tizen>
51 public event EventHandler Created;
54 /// Occurs when the application is about to shutdown.
56 /// <since_tizen> 3 </since_tizen>
57 public event EventHandler Terminated;
60 /// Occurs whenever the application receives the appcontrol message.
62 /// <since_tizen> 3 </since_tizen>
63 public event EventHandler<AppControlReceivedEventArgs> AppControlReceived;
66 /// Occurs when the system memory is low.
68 /// <since_tizen> 3 </since_tizen>
69 public event EventHandler<LowMemoryEventArgs> LowMemory;
72 /// Occurs when the system battery is low.
74 /// <since_tizen> 3 </since_tizen>
75 public event EventHandler<LowBatteryEventArgs> LowBattery;
78 /// Occurs when the system language is chagned.
80 /// <since_tizen> 3 </since_tizen>
81 public event EventHandler<LocaleChangedEventArgs> LocaleChanged;
84 /// Occurs when the region format is changed.
86 /// <since_tizen> 3 </since_tizen>
87 public event EventHandler<RegionFormatChangedEventArgs> RegionFormatChanged;
90 /// Occurs when the device orientation is changed.
92 /// <since_tizen> 3 </since_tizen>
93 public event EventHandler<DeviceOrientationEventArgs> DeviceOrientationChanged;
96 /// The backend instance.
98 /// <since_tizen> 3 </since_tizen>
99 protected ICoreBackend Backend { get { return _backend; } }
102 /// Runs the application's main loop.
104 /// <param name="args">Arguments from commandline.</param>
105 /// <since_tizen> 3 </since_tizen>
106 public override void Run(string[] args)
110 _backend.AddEventHandler(EventType.Created, OnCreate);
111 _backend.AddEventHandler(EventType.Terminated, OnTerminate);
112 _backend.AddEventHandler<AppControlReceivedEventArgs>(EventType.AppControlReceived, OnAppControlReceived);
113 _backend.AddEventHandler<LowMemoryEventArgs>(EventType.LowMemory, OnLowMemory);
114 _backend.AddEventHandler<LowBatteryEventArgs>(EventType.LowBattery, OnLowBattery);
115 _backend.AddEventHandler<LocaleChangedEventArgs>(EventType.LocaleChanged, OnLocaleChanged);
116 _backend.AddEventHandler<RegionFormatChangedEventArgs>(EventType.RegionFormatChanged, OnRegionFormatChanged);
117 _backend.AddEventHandler<DeviceOrientationEventArgs>(EventType.DeviceOrientationChanged, OnDeviceOrientationChanged);
119 string[] argsClone = new string[args.Length + 1];
122 args.CopyTo(argsClone, 1);
124 argsClone[0] = string.Empty;
126 _backend.Run(argsClone);
130 /// Exits the main loop of the application.
132 /// <since_tizen> 3 </since_tizen>
133 public override void Exit()
139 /// Overrides this method if want to handle behavior when the application is launched.
140 /// If base.OnCreated() is not called, the event 'Created' will not be emitted.
142 /// <since_tizen> 3 </since_tizen>
143 protected virtual void OnCreate()
145 string locale = ULocale.GetDefaultLocale();
146 ChangeCurrentUICultureInfo(locale);
147 ChangeCurrentCultureInfo(locale);
149 Created?.Invoke(this, EventArgs.Empty);
153 /// Overrides this method if want to handle behavior when the application is terminated.
154 /// If base.OnTerminate() is not called, the event 'Terminated' will not be emitted.
156 /// <since_tizen> 3 </since_tizen>
157 protected virtual void OnTerminate()
159 Terminated?.Invoke(this, EventArgs.Empty);
163 /// Overrides this method if want to handle behavior when the application receives the appcontrol message.
164 /// If base.OnAppControlReceived() is not called, the event 'AppControlReceived' will not be emitted.
166 /// <param name="e"></param>
167 /// <since_tizen> 3 </since_tizen>
168 protected virtual void OnAppControlReceived(AppControlReceivedEventArgs e)
170 AppControlReceived?.Invoke(this, e);
174 /// Overrides this method if want to handle behavior when the system memory is low.
175 /// If base.OnLowMemory() is not called, the event 'LowMemory' will not be emitted.
177 /// <param name="e">The low memory event argument</param>
178 /// <since_tizen> 3 </since_tizen>
179 protected virtual void OnLowMemory(LowMemoryEventArgs e)
181 LowMemory?.Invoke(this, e);
182 double interval = new Random().Next(10 * 1000);
184 interval = 10 * 1000;
186 sTimer = new Timer(interval);
187 sTimer.Elapsed += OnTimedEvent;
188 sTimer.AutoReset = false;
189 sTimer.Enabled = true;
192 private static void OnTimedEvent(Object source, ElapsedEventArgs e)
198 /// Overrides this method if want to handle behavior when the system battery is low.
199 /// If base.OnLowBattery() is not called, the event 'LowBattery' will not be emitted.
201 /// <param name="e">The low battery event argument</param>
202 /// <since_tizen> 3 </since_tizen>
203 protected virtual void OnLowBattery(LowBatteryEventArgs e)
205 LowBattery?.Invoke(this, e);
209 /// Overrides this method if want to handle behavior when the system language is changed.
210 /// If base.OnLocaleChanged() is not called, the event 'LocaleChanged' will not be emitted.
212 /// <param name="e">The locale changed event argument</param>
213 /// <since_tizen> 3 </since_tizen>
214 protected virtual void OnLocaleChanged(LocaleChangedEventArgs e)
216 ChangeCurrentUICultureInfo(e.Locale);
217 LocaleChanged?.Invoke(this, e);
221 /// Overrides this method if want to handle behavior when the region format is changed.
222 /// If base.OnRegionFormatChanged() is not called, the event 'RegionFormatChanged' will not be emitted.
224 /// <param name="e">The region format changed event argument</param>
225 /// <since_tizen> 3 </since_tizen>
226 protected virtual void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
228 ChangeCurrentCultureInfo(e.Region);
229 RegionFormatChanged?.Invoke(this, e);
233 /// Overrides this method if want to handle behavior when the device orientation is changed.
234 /// If base.OnRegionFormatChanged() is not called, the event 'RegionFormatChanged' will not be emitted.
236 /// <param name="e">The device orientation changed event argument</param>
237 /// <since_tizen> 3 </since_tizen>
238 protected virtual void OnDeviceOrientationChanged(DeviceOrientationEventArgs e)
240 DeviceOrientationChanged?.Invoke(this, e);
244 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
246 /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
247 /// <since_tizen> 3 </since_tizen>
248 protected override void Dispose(bool disposing)
257 _disposedValue = true;
259 base.Dispose(disposing);
262 private CultureInfo ConvertCultureInfo(string locale)
264 ULocale pLocale = new ULocale(locale);
268 return new CultureInfo(pLocale.LCID);
270 catch (ArgumentOutOfRangeException)
272 return GetFallbackCultureInfo(pLocale);
274 catch (CultureNotFoundException)
276 return GetFallbackCultureInfo(pLocale);
280 private void ChangeCurrentCultureInfo(string locale)
282 CultureInfo.CurrentCulture = ConvertCultureInfo(locale);
285 private void ChangeCurrentUICultureInfo(string locale)
287 CultureInfo.CurrentUICulture = ConvertCultureInfo(locale);
290 private bool ExistCultureInfo(string locale)
292 foreach (var cultureInfo in CultureInfo.GetCultures(CultureTypes.AllCultures))
294 if (cultureInfo.Name == locale)
303 private CultureInfo GetCultureInfo(string locale)
305 if (!ExistCultureInfo(locale))
312 return new CultureInfo(locale);
314 catch (CultureNotFoundException)
320 private CultureInfo GetFallbackCultureInfo(ULocale uLocale)
322 string locale = uLocale.Locale.Replace("_", "-");
323 CultureInfo fallbackCultureInfo = GetCultureInfo(locale);
325 if (fallbackCultureInfo == null && uLocale.Script != null && uLocale.Country != null)
327 locale = uLocale.Language + "-" + uLocale.Script + "-" + uLocale.Country;
328 fallbackCultureInfo = GetCultureInfo(locale);
331 if (fallbackCultureInfo == null && uLocale.Script != null)
333 locale = uLocale.Language + "-" + uLocale.Script;
334 fallbackCultureInfo = GetCultureInfo(locale);
337 if (fallbackCultureInfo == null && uLocale.Country != null)
339 locale = uLocale.Language + "-" + uLocale.Country;
340 fallbackCultureInfo = GetCultureInfo(locale);
343 if (fallbackCultureInfo == null)
347 fallbackCultureInfo = new CultureInfo(uLocale.Language);
349 catch (CultureNotFoundException)
351 fallbackCultureInfo = new CultureInfo("en");
355 return fallbackCultureInfo;
359 internal class ULocale
361 private const int ULOC_FULLNAME_CAPACITY = 157;
362 private const int ULOC_LANG_CAPACITY = 12;
363 private const int ULOC_SCRIPT_CAPACITY = 6;
364 private const int ULOC_COUNTRY_CAPACITY = 4;
365 private const int ULOC_VARIANT_CAPACITY = ULOC_FULLNAME_CAPACITY;
367 internal ULocale(string locale)
369 Locale = Canonicalize(locale);
370 Language = GetLanguage(Locale);
371 Script = GetScript(Locale);
372 Country = GetCountry(Locale);
373 Variant = GetVariant(Locale);
374 LCID = GetLCID(Locale);
377 internal string Locale { get; private set; }
378 internal string Language { get; private set; }
379 internal string Script { get; private set; }
380 internal string Country { get; private set; }
381 internal string Variant { get; private set; }
382 internal int LCID { get; private set; }
384 private string Canonicalize(string localeName)
386 // Get the locale name from ICU
387 StringBuilder sb = new StringBuilder(ULOC_FULLNAME_CAPACITY);
388 if (Interop.BaseUtilsi18n.Canonicalize(localeName, sb, sb.Capacity) <= 0)
393 return sb.ToString();
396 private string GetLanguage(string locale)
398 // Get the language name from ICU
399 StringBuilder sb = new StringBuilder(ULOC_LANG_CAPACITY);
400 if (Interop.BaseUtilsi18n.GetLanguage(locale, sb, sb.Capacity, out int bufSizeLanguage) != 0)
405 return sb.ToString();
408 private string GetScript(string locale)
410 // Get the script name from ICU
411 StringBuilder sb = new StringBuilder(ULOC_SCRIPT_CAPACITY);
412 if (Interop.BaseUtilsi18n.GetScript(locale, sb, sb.Capacity) <= 0)
417 return sb.ToString();
420 private string GetCountry(string locale)
424 // Get the country name from ICU
425 StringBuilder sb = new StringBuilder(ULOC_COUNTRY_CAPACITY);
426 if (Interop.BaseUtilsi18n.GetCountry(locale, sb, sb.Capacity, out err) <= 0)
431 return sb.ToString();
434 private string GetVariant(string locale)
436 // Get the variant name from ICU
437 StringBuilder sb = new StringBuilder(ULOC_VARIANT_CAPACITY);
438 if (Interop.BaseUtilsi18n.GetVariant(locale, sb, sb.Capacity) <= 0)
443 return sb.ToString();
446 private int GetLCID(string locale)
448 // Get the LCID from ICU
449 uint lcid = Interop.BaseUtilsi18n.GetLCID(locale);
453 internal static string GetDefaultLocale()
455 IntPtr stringPtr = IntPtr.Zero;
456 if (Interop.BaseUtilsi18n.GetDefault(out stringPtr) != 0)
461 if (stringPtr == IntPtr.Zero)
466 return Marshal.PtrToStringAnsi(stringPtr);