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;
21 using Tizen.Applications.CoreBackend;
23 namespace Tizen.Applications
26 /// This class represents an application controlled lifecycles by the backend system.
28 /// <since_tizen> 3 </since_tizen>
29 public class CoreApplication : Application
31 private readonly ICoreBackend _backend;
32 private bool _disposedValue = false;
34 private static Timer sTimer;
37 /// Initializes the CoreApplication class.
39 /// <param name="backend">The backend instance implementing ICoreBacked interface.</param>
40 /// <since_tizen> 3 </since_tizen>
41 public CoreApplication(ICoreBackend backend)
47 /// Occurs when the application is launched.
49 /// <since_tizen> 3 </since_tizen>
50 public event EventHandler Created;
53 /// Occurs when the application is about to shutdown.
55 /// <since_tizen> 3 </since_tizen>
56 public event EventHandler Terminated;
59 /// Occurs whenever the application receives the appcontrol message.
61 /// <since_tizen> 3 </since_tizen>
62 public event EventHandler<AppControlReceivedEventArgs> AppControlReceived;
65 /// Occurs when the system memory is low.
67 /// <since_tizen> 3 </since_tizen>
68 public event EventHandler<LowMemoryEventArgs> LowMemory;
71 /// Occurs when the system battery is low.
73 /// <since_tizen> 3 </since_tizen>
74 public event EventHandler<LowBatteryEventArgs> LowBattery;
77 /// Occurs when the system language is chagned.
79 /// <since_tizen> 3 </since_tizen>
80 public event EventHandler<LocaleChangedEventArgs> LocaleChanged;
83 /// Occurs when the region format is changed.
85 /// <since_tizen> 3 </since_tizen>
86 public event EventHandler<RegionFormatChangedEventArgs> RegionFormatChanged;
89 /// Occurs when the device orientation is changed.
91 /// <since_tizen> 3 </since_tizen>
92 public event EventHandler<DeviceOrientationEventArgs> DeviceOrientationChanged;
95 /// The backend instance.
97 /// <since_tizen> 3 </since_tizen>
98 protected ICoreBackend Backend { get { return _backend; } }
101 /// Runs the application's main loop.
103 /// <param name="args">Arguments from commandline.</param>
104 /// <since_tizen> 3 </since_tizen>
105 public override void Run(string[] args)
109 _backend.AddEventHandler(EventType.Created, OnCreate);
110 _backend.AddEventHandler(EventType.Terminated, OnTerminate);
111 _backend.AddEventHandler<AppControlReceivedEventArgs>(EventType.AppControlReceived, OnAppControlReceived);
112 _backend.AddEventHandler<LowMemoryEventArgs>(EventType.LowMemory, OnLowMemory);
113 _backend.AddEventHandler<LowBatteryEventArgs>(EventType.LowBattery, OnLowBattery);
114 _backend.AddEventHandler<LocaleChangedEventArgs>(EventType.LocaleChanged, OnLocaleChanged);
115 _backend.AddEventHandler<RegionFormatChangedEventArgs>(EventType.RegionFormatChanged, OnRegionFormatChanged);
116 _backend.AddEventHandler<DeviceOrientationEventArgs>(EventType.DeviceOrientationChanged, OnDeviceOrientationChanged);
118 string[] argsClone = null;
122 argsClone = new string[1];
126 argsClone = new string[args.Length + 1];
127 args.CopyTo(argsClone, 1);
129 argsClone[0] = string.Empty;
130 _backend.Run(argsClone);
134 /// Exits the main loop of the application.
136 /// <since_tizen> 3 </since_tizen>
137 public override void Exit()
143 /// Overrides this method if want to handle behavior when the application is launched.
144 /// If base.OnCreated() is not called, the event 'Created' will not be emitted.
146 /// <since_tizen> 3 </since_tizen>
147 protected virtual void OnCreate()
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 /// <since_tizen> 3 </since_tizen>
178 protected virtual void OnLowMemory(LowMemoryEventArgs e)
180 LowMemory?.Invoke(this, e);
181 sTimer = new Timer(new Random().Next(10 * 1000));
182 sTimer.Elapsed += OnTimedEvent;
183 sTimer.AutoReset = false;
184 sTimer.Enabled = true;
187 private static void OnTimedEvent(Object source, ElapsedEventArgs e)
193 /// Overrides this method if want to handle behavior when the system battery is low.
194 /// If base.OnLowBattery() is not called, the event 'LowBattery' will not be emitted.
196 /// <since_tizen> 3 </since_tizen>
197 protected virtual void OnLowBattery(LowBatteryEventArgs e)
199 LowBattery?.Invoke(this, e);
203 /// Overrides this method if want to handle behavior when the system language is changed.
204 /// If base.OnLocaleChanged() is not called, the event 'LocaleChanged' will not be emitted.
206 /// <since_tizen> 3 </since_tizen>
207 protected virtual void OnLocaleChanged(LocaleChangedEventArgs e)
209 ChangeCurrentCultureInfo(e.Locale);
210 LocaleChanged?.Invoke(this, e);
214 /// Overrides this method if want to handle behavior when the region format is changed.
215 /// If base.OnRegionFormatChanged() is not called, the event 'RegionFormatChanged' will not be emitted.
217 /// <since_tizen> 3 </since_tizen>
218 protected virtual void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
220 RegionFormatChanged?.Invoke(this, e);
224 /// Overrides this method if want to handle behavior when the device orientation is changed.
225 /// If base.OnRegionFormatChanged() is not called, the event 'RegionFormatChanged' will not be emitted.
227 /// <since_tizen> 3 </since_tizen>
228 protected virtual void OnDeviceOrientationChanged(DeviceOrientationEventArgs e)
230 DeviceOrientationChanged?.Invoke(this, e);
234 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
236 /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
237 /// <since_tizen> 3 </since_tizen>
238 protected override void Dispose(bool disposing)
247 _disposedValue = true;
249 base.Dispose(disposing);
252 private void ChangeCurrentCultureInfo(string locale)
254 ULocale pLocale = new ULocale(locale);
255 CultureInfo currentCultureInfo = null;
259 currentCultureInfo = new CultureInfo(pLocale.Locale.Replace("_", "-"));
261 catch (CultureNotFoundException)
263 currentCultureInfo = GetFallbackCultureInfo(pLocale);
266 CultureInfo.CurrentCulture = currentCultureInfo;
269 private CultureInfo GetCultureInfo(string locale)
271 CultureInfo cultureInfo = null;
275 cultureInfo = new CultureInfo(locale);
277 catch (CultureNotFoundException)
285 private CultureInfo GetFallbackCultureInfo(ULocale uLocale)
287 string locale = string.Empty;
288 CultureInfo fallbackCultureInfo = null;
290 if (uLocale.Script != null && uLocale.Country != null)
292 locale = uLocale.Language + "-" + uLocale.Script + "-" + uLocale.Country;
293 fallbackCultureInfo = GetCultureInfo(locale);
296 if (fallbackCultureInfo == null && uLocale.Script != null)
298 locale = uLocale.Language + "-" + uLocale.Script;
299 fallbackCultureInfo = GetCultureInfo(locale);
302 if (fallbackCultureInfo == null && uLocale.Country != null)
304 locale = uLocale.Language + "-" + uLocale.Country;
305 fallbackCultureInfo = GetCultureInfo(locale);
308 if (fallbackCultureInfo == null)
312 fallbackCultureInfo = new CultureInfo(uLocale.Language);
314 catch (CultureNotFoundException)
316 fallbackCultureInfo = new CultureInfo("en");
320 return fallbackCultureInfo;
324 internal class ULocale
326 private const int ICU_ULOC_FULLNAME_CAPACITY = 157;
327 private const int ICU_ULOC_LANG_CAPACITY = 12;
328 private const int ICU_ULOC_SCRIPT_CAPACITY = 6;
329 private const int ICU_ULOC_COUNTRY_CAPACITY = 4;
330 private const int ICU_ULOC_VARIANT_CAPACITY = ICU_ULOC_FULLNAME_CAPACITY;
331 private const int ICU_U_ZERO_ERROR = 0;
333 internal ULocale(string locale)
335 Locale = Canonicalize(locale);
336 Language = GetLanguage(Locale);
337 Script = GetScript(Locale);
338 Country = GetCountry(Locale);
339 Variant = GetVariant(Locale);
342 internal string Locale { get; private set; }
343 internal string Language { get; private set; }
344 internal string Script { get; private set; }
345 internal string Country { get; private set; }
346 internal string Variant { get; private set; }
348 private string Canonicalize(string localeName)
350 int err = ICU_U_ZERO_ERROR;
352 // Get the locale name from ICU
353 StringBuilder sb = new StringBuilder(ICU_ULOC_FULLNAME_CAPACITY);
354 if (Interop.Icu.Canonicalize(localeName, sb, sb.Capacity, out err) <= 0)
359 return sb.ToString();
362 private string GetLanguage(string locale)
364 int err = ICU_U_ZERO_ERROR;
366 // Get the language name from ICU
367 StringBuilder sb = new StringBuilder(ICU_ULOC_LANG_CAPACITY);
368 if (Interop.Icu.GetLanguage(locale, sb, sb.Capacity, out err) <= 0)
373 return sb.ToString();
376 private string GetScript(string locale)
378 int err = ICU_U_ZERO_ERROR;
380 // Get the script name from ICU
381 StringBuilder sb = new StringBuilder(ICU_ULOC_SCRIPT_CAPACITY);
382 if (Interop.Icu.GetScript(locale, sb, sb.Capacity, out err) <= 0)
387 return sb.ToString();
390 private string GetCountry(string locale)
392 int err = ICU_U_ZERO_ERROR;
394 // Get the country name from ICU
395 StringBuilder sb = new StringBuilder(ICU_ULOC_SCRIPT_CAPACITY);
396 if (Interop.Icu.GetCountry(locale, sb, sb.Capacity, out err) <= 0)
401 return sb.ToString();
404 private string GetVariant(string locale)
406 int err = ICU_U_ZERO_ERROR;
408 // Get the variant name from ICU
409 StringBuilder sb = new StringBuilder(ICU_ULOC_VARIANT_CAPACITY);
410 if (Interop.Icu.GetVariant(locale, sb, sb.Capacity, out err) <= 0)
415 return sb.ToString();