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;
20 using Tizen.Applications.CoreBackend;
22 namespace Tizen.Applications
25 /// This class represents an application controlled lifecycles by the backend system.
27 /// <since_tizen> 3 </since_tizen>
28 public class CoreApplication : Application
30 private readonly ICoreBackend _backend;
31 private bool _disposedValue = false;
34 /// Initializes the CoreApplication class.
36 /// <param name="backend">The backend instance implementing ICoreBacked interface.</param>
37 /// <since_tizen> 3 </since_tizen>
38 public CoreApplication(ICoreBackend backend)
44 /// Occurs when the application is launched.
46 /// <since_tizen> 3 </since_tizen>
47 public event EventHandler Created;
50 /// Occurs when the application is about to shutdown.
52 /// <since_tizen> 3 </since_tizen>
53 public event EventHandler Terminated;
56 /// Occurs whenever the application receives the appcontrol message.
58 /// <since_tizen> 3 </since_tizen>
59 public event EventHandler<AppControlReceivedEventArgs> AppControlReceived;
62 /// Occurs when the system memory is low.
64 /// <since_tizen> 3 </since_tizen>
65 public event EventHandler<LowMemoryEventArgs> LowMemory;
68 /// Occurs when the system battery is low.
70 /// <since_tizen> 3 </since_tizen>
71 public event EventHandler<LowBatteryEventArgs> LowBattery;
74 /// Occurs when the system language is chagned.
76 /// <since_tizen> 3 </since_tizen>
77 public event EventHandler<LocaleChangedEventArgs> LocaleChanged;
80 /// Occurs when the region format is changed.
82 /// <since_tizen> 3 </since_tizen>
83 public event EventHandler<RegionFormatChangedEventArgs> RegionFormatChanged;
86 /// Occurs when the device orientation is changed.
88 /// <since_tizen> 3 </since_tizen>
89 public event EventHandler<DeviceOrientationEventArgs> DeviceOrientationChanged;
92 /// The backend instance.
94 /// <since_tizen> 3 </since_tizen>
95 protected ICoreBackend Backend { get { return _backend; } }
98 /// Runs the application's main loop.
100 /// <param name="args">Arguments from commandline.</param>
101 /// <since_tizen> 3 </since_tizen>
102 public override void Run(string[] args)
106 _backend.AddEventHandler(EventType.Created, OnCreate);
107 _backend.AddEventHandler(EventType.Terminated, OnTerminate);
108 _backend.AddEventHandler<AppControlReceivedEventArgs>(EventType.AppControlReceived, OnAppControlReceived);
109 _backend.AddEventHandler<LowMemoryEventArgs>(EventType.LowMemory, OnLowMemory);
110 _backend.AddEventHandler<LowBatteryEventArgs>(EventType.LowBattery, OnLowBattery);
111 _backend.AddEventHandler<LocaleChangedEventArgs>(EventType.LocaleChanged, OnLocaleChanged);
112 _backend.AddEventHandler<RegionFormatChangedEventArgs>(EventType.RegionFormatChanged, OnRegionFormatChanged);
113 _backend.AddEventHandler<DeviceOrientationEventArgs>(EventType.DeviceOrientationChanged, OnDeviceOrientationChanged);
115 string[] argsClone = null;
119 argsClone = new string[1];
123 argsClone = new string[args.Length + 1];
124 args.CopyTo(argsClone, 1);
126 argsClone[0] = string.Empty;
127 _backend.Run(argsClone);
131 /// Exits the main loop of the application.
133 /// <since_tizen> 3 </since_tizen>
134 public override void Exit()
140 /// Overrides this method if want to handle behavior when the application is launched.
141 /// If base.OnCreated() is not called, the event 'Created' will not be emitted.
143 /// <since_tizen> 3 </since_tizen>
144 protected virtual void OnCreate()
146 Created?.Invoke(this, EventArgs.Empty);
150 /// Overrides this method if want to handle behavior when the application is terminated.
151 /// If base.OnTerminate() is not called, the event 'Terminated' will not be emitted.
153 /// <since_tizen> 3 </since_tizen>
154 protected virtual void OnTerminate()
156 Terminated?.Invoke(this, EventArgs.Empty);
160 /// Overrides this method if want to handle behavior when the application receives the appcontrol message.
161 /// If base.OnAppControlReceived() is not called, the event 'AppControlReceived' will not be emitted.
163 /// <param name="e"></param>
164 /// <since_tizen> 3 </since_tizen>
165 protected virtual void OnAppControlReceived(AppControlReceivedEventArgs e)
167 AppControlReceived?.Invoke(this, e);
171 /// Overrides this method if want to handle behavior when the system memory is low.
172 /// If base.OnLowMemory() is not called, the event 'LowMemory' will not be emitted.
174 /// <since_tizen> 3 </since_tizen>
175 protected virtual void OnLowMemory(LowMemoryEventArgs e)
177 LowMemory?.Invoke(this, e);
182 /// Overrides this method if want to handle behavior when the system battery is low.
183 /// If base.OnLowBattery() is not called, the event 'LowBattery' will not be emitted.
185 /// <since_tizen> 3 </since_tizen>
186 protected virtual void OnLowBattery(LowBatteryEventArgs e)
188 LowBattery?.Invoke(this, e);
192 /// Overrides this method if want to handle behavior when the system language is changed.
193 /// If base.OnLocaleChanged() is not called, the event 'LocaleChanged' will not be emitted.
195 /// <since_tizen> 3 </since_tizen>
196 protected virtual void OnLocaleChanged(LocaleChangedEventArgs e)
198 ChangeCurrentCultureInfo(e.Locale);
199 LocaleChanged?.Invoke(this, e);
203 /// Overrides this method if want to handle behavior when the region format is changed.
204 /// If base.OnRegionFormatChanged() is not called, the event 'RegionFormatChanged' will not be emitted.
206 /// <since_tizen> 3 </since_tizen>
207 protected virtual void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
209 RegionFormatChanged?.Invoke(this, e);
213 /// Overrides this method if want to handle behavior when the device orientation is changed.
214 /// If base.OnRegionFormatChanged() is not called, the event 'RegionFormatChanged' will not be emitted.
216 /// <since_tizen> 3 </since_tizen>
217 protected virtual void OnDeviceOrientationChanged(DeviceOrientationEventArgs e)
219 DeviceOrientationChanged?.Invoke(this, e);
223 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
225 /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
226 /// <since_tizen> 3 </since_tizen>
227 protected override void Dispose(bool disposing)
236 _disposedValue = true;
238 base.Dispose(disposing);
241 private void ChangeCurrentCultureInfo(string locale)
243 ULocale pLocale = new ULocale(locale);
244 CultureInfo currentCultureInfo = null;
248 currentCultureInfo = new CultureInfo(pLocale.Locale.Replace("_", "-"));
250 catch (CultureNotFoundException)
252 currentCultureInfo = GetFallbackCultureInfo(pLocale);
255 CultureInfo.CurrentCulture = currentCultureInfo;
258 private CultureInfo GetCultureInfo(string locale)
260 CultureInfo cultureInfo = null;
264 cultureInfo = new CultureInfo(locale);
266 catch (CultureNotFoundException)
274 private CultureInfo GetFallbackCultureInfo(ULocale uLocale)
276 string locale = string.Empty;
277 CultureInfo fallbackCultureInfo = null;
279 if (uLocale.Script != null && uLocale.Country != null)
281 locale = uLocale.Language + "-" + uLocale.Script + "-" + uLocale.Country;
282 fallbackCultureInfo = GetCultureInfo(locale);
285 if (fallbackCultureInfo == null && uLocale.Script != null)
287 locale = uLocale.Language + "-" + uLocale.Script;
288 fallbackCultureInfo = GetCultureInfo(locale);
291 if (fallbackCultureInfo == null && uLocale.Country != null)
293 locale = uLocale.Language + "-" + uLocale.Country;
294 fallbackCultureInfo = GetCultureInfo(locale);
297 if (fallbackCultureInfo == null)
301 fallbackCultureInfo = new CultureInfo(uLocale.Language);
303 catch (CultureNotFoundException)
305 fallbackCultureInfo = new CultureInfo("en");
309 return fallbackCultureInfo;
313 internal class ULocale
315 private const int ICU_ULOC_FULLNAME_CAPACITY = 157;
316 private const int ICU_ULOC_LANG_CAPACITY = 12;
317 private const int ICU_ULOC_SCRIPT_CAPACITY = 6;
318 private const int ICU_ULOC_COUNTRY_CAPACITY = 4;
319 private const int ICU_ULOC_VARIANT_CAPACITY = ICU_ULOC_FULLNAME_CAPACITY;
320 private const int ICU_U_ZERO_ERROR = 0;
322 internal ULocale(string locale)
324 Locale = Canonicalize(locale);
325 Language = GetLanguage(Locale);
326 Script = GetScript(Locale);
327 Country = GetCountry(Locale);
328 Variant = GetVariant(Locale);
331 internal string Locale { get; private set; }
332 internal string Language { get; private set; }
333 internal string Script { get; private set; }
334 internal string Country { get; private set; }
335 internal string Variant { get; private set; }
337 private string Canonicalize(string localeName)
339 int err = ICU_U_ZERO_ERROR;
341 // Get the locale name from ICU
342 StringBuilder sb = new StringBuilder(ICU_ULOC_FULLNAME_CAPACITY);
343 if (Interop.Icu.Canonicalize(localeName, sb, sb.Capacity, out err) <= 0)
348 return sb.ToString();
351 private string GetLanguage(string locale)
353 int err = ICU_U_ZERO_ERROR;
355 // Get the language name from ICU
356 StringBuilder sb = new StringBuilder(ICU_ULOC_LANG_CAPACITY);
357 if (Interop.Icu.GetLanguage(locale, sb, sb.Capacity, out err) <= 0)
362 return sb.ToString();
365 private string GetScript(string locale)
367 int err = ICU_U_ZERO_ERROR;
369 // Get the script name from ICU
370 StringBuilder sb = new StringBuilder(ICU_ULOC_SCRIPT_CAPACITY);
371 if (Interop.Icu.GetScript(locale, sb, sb.Capacity, out err) <= 0)
376 return sb.ToString();
379 private string GetCountry(string locale)
381 int err = ICU_U_ZERO_ERROR;
383 // Get the country name from ICU
384 StringBuilder sb = new StringBuilder(ICU_ULOC_SCRIPT_CAPACITY);
385 if (Interop.Icu.GetCountry(locale, sb, sb.Capacity, out err) <= 0)
390 return sb.ToString();
393 private string GetVariant(string locale)
395 int err = ICU_U_ZERO_ERROR;
397 // Get the country name from ICU
398 StringBuilder sb = new StringBuilder(ICU_ULOC_VARIANT_CAPACITY);
399 if (Interop.Icu.GetVariant(locale, sb, sb.Capacity, out err) <= 0)
404 return sb.ToString();