[Tizen.Applications.Common] Add a timer for handling GC collection (#710)
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.Common / Tizen.Applications / CoreApplication.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18 using System.Globalization;
19 using System.Text;
20 using System.Timers;
21 using Tizen.Applications.CoreBackend;
22
23 namespace Tizen.Applications
24 {
25     /// <summary>
26     /// This class represents an application controlled lifecycles by the backend system.
27     /// </summary>
28     /// <since_tizen> 3 </since_tizen>
29     public class CoreApplication : Application
30     {
31         private readonly ICoreBackend _backend;
32         private bool _disposedValue = false;
33
34         private static Timer sTimer;
35
36         /// <summary>
37         /// Initializes the CoreApplication class.
38         /// </summary>
39         /// <param name="backend">The backend instance implementing ICoreBacked interface.</param>
40         /// <since_tizen> 3 </since_tizen>
41         public CoreApplication(ICoreBackend backend)
42         {
43             _backend = backend;
44         }
45
46         /// <summary>
47         /// Occurs when the application is launched.
48         /// </summary>
49         /// <since_tizen> 3 </since_tizen>
50         public event EventHandler Created;
51
52         /// <summary>
53         /// Occurs when the application is about to shutdown.
54         /// </summary>
55         /// <since_tizen> 3 </since_tizen>
56         public event EventHandler Terminated;
57
58         /// <summary>
59         /// Occurs whenever the application receives the appcontrol message.
60         /// </summary>
61         /// <since_tizen> 3 </since_tizen>
62         public event EventHandler<AppControlReceivedEventArgs> AppControlReceived;
63
64         /// <summary>
65         /// Occurs when the system memory is low.
66         /// </summary>
67         /// <since_tizen> 3 </since_tizen>
68         public event EventHandler<LowMemoryEventArgs> LowMemory;
69
70         /// <summary>
71         /// Occurs when the system battery is low.
72         /// </summary>
73         /// <since_tizen> 3 </since_tizen>
74         public event EventHandler<LowBatteryEventArgs> LowBattery;
75
76         /// <summary>
77         /// Occurs when the system language is chagned.
78         /// </summary>
79         /// <since_tizen> 3 </since_tizen>
80         public event EventHandler<LocaleChangedEventArgs> LocaleChanged;
81
82         /// <summary>
83         /// Occurs when the region format is changed.
84         /// </summary>
85         /// <since_tizen> 3 </since_tizen>
86         public event EventHandler<RegionFormatChangedEventArgs> RegionFormatChanged;
87
88         /// <summary>
89         /// Occurs when the device orientation is changed.
90         /// </summary>
91         /// <since_tizen> 3 </since_tizen>
92         public event EventHandler<DeviceOrientationEventArgs> DeviceOrientationChanged;
93
94         /// <summary>
95         /// The backend instance.
96         /// </summary>
97         /// <since_tizen> 3 </since_tizen>
98         protected ICoreBackend Backend { get { return _backend; } }
99
100         /// <summary>
101         /// Runs the application's main loop.
102         /// </summary>
103         /// <param name="args">Arguments from commandline.</param>
104         /// <since_tizen> 3 </since_tizen>
105         public override void Run(string[] args)
106         {
107             base.Run(args);
108
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);
117
118             string[] argsClone = new string[args.Length + 1];
119             if (args.Length > 1)
120             {
121                 args.CopyTo(argsClone, 1);
122             }
123             argsClone[0] = string.Empty;
124
125             _backend.Run(argsClone);
126         }
127
128         /// <summary>
129         /// Exits the main loop of the application.
130         /// </summary>
131         /// <since_tizen> 3 </since_tizen>
132         public override void Exit()
133         {
134             _backend.Exit();
135         }
136
137         /// <summary>
138         /// Overrides this method if want to handle behavior when the application is launched.
139         /// If base.OnCreated() is not called, the event 'Created' will not be emitted.
140         /// </summary>
141         /// <since_tizen> 3 </since_tizen>
142         protected virtual void OnCreate()
143         {
144             Created?.Invoke(this, EventArgs.Empty);
145         }
146
147         /// <summary>
148         /// Overrides this method if want to handle behavior when the application is terminated.
149         /// If base.OnTerminate() is not called, the event 'Terminated' will not be emitted.
150         /// </summary>
151         /// <since_tizen> 3 </since_tizen>
152         protected virtual void OnTerminate()
153         {
154             Terminated?.Invoke(this, EventArgs.Empty);
155         }
156
157         /// <summary>
158         /// Overrides this method if want to handle behavior when the application receives the appcontrol message.
159         /// If base.OnAppControlReceived() is not called, the event 'AppControlReceived' will not be emitted.
160         /// </summary>
161         /// <param name="e"></param>
162         /// <since_tizen> 3 </since_tizen>
163         protected virtual void OnAppControlReceived(AppControlReceivedEventArgs e)
164         {
165             AppControlReceived?.Invoke(this, e);
166         }
167
168         /// <summary>
169         /// Overrides this method if want to handle behavior when the system memory is low.
170         /// If base.OnLowMemory() is not called, the event 'LowMemory' will not be emitted.
171         /// </summary>
172         /// <param name="e">The low memory event argument</param>
173         /// <since_tizen> 3 </since_tizen>
174         protected virtual void OnLowMemory(LowMemoryEventArgs e)
175         {
176             LowMemory?.Invoke(this, e);
177             sTimer = new Timer(new Random().Next(10 * 1000));
178             sTimer.Elapsed += OnTimedEvent;
179             sTimer.AutoReset = false;
180             sTimer.Enabled = true;
181         }
182
183         private static void OnTimedEvent(Object source, ElapsedEventArgs e)
184         {
185             System.GC.Collect();
186         }
187
188         /// <summary>
189         /// Overrides this method if want to handle behavior when the system battery is low.
190         /// If base.OnLowBattery() is not called, the event 'LowBattery' will not be emitted.
191         /// </summary>
192         /// <param name="e">The low battery event argument</param>
193         /// <since_tizen> 3 </since_tizen>
194         protected virtual void OnLowBattery(LowBatteryEventArgs e)
195         {
196             LowBattery?.Invoke(this, e);
197         }
198
199         /// <summary>
200         /// Overrides this method if want to handle behavior when the system language is changed.
201         /// If base.OnLocaleChanged() is not called, the event 'LocaleChanged' will not be emitted.
202         /// </summary>
203         /// <param name="e">The locale changed event argument</param>
204         /// <since_tizen> 3 </since_tizen>
205         protected virtual void OnLocaleChanged(LocaleChangedEventArgs e)
206         {
207             ChangeCurrentCultureInfo(e.Locale);
208             LocaleChanged?.Invoke(this, e);
209         }
210
211         /// <summary>
212         /// Overrides this method if want to handle behavior when the region format is changed.
213         /// If base.OnRegionFormatChanged() is not called, the event 'RegionFormatChanged' will not be emitted.
214         /// </summary>
215         /// <param name="e">The region format changed event argument</param>
216         /// <since_tizen> 3 </since_tizen>
217         protected virtual void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
218         {
219             RegionFormatChanged?.Invoke(this, e);
220         }
221
222         /// <summary>
223         /// Overrides this method if want to handle behavior when the device orientation is changed.
224         /// If base.OnRegionFormatChanged() is not called, the event 'RegionFormatChanged' will not be emitted.
225         /// </summary>
226         /// <param name="e">The device orientation changed event argument</param>
227         /// <since_tizen> 3 </since_tizen>
228         protected virtual void OnDeviceOrientationChanged(DeviceOrientationEventArgs e)
229         {
230             DeviceOrientationChanged?.Invoke(this, e);
231         }
232
233         /// <summary>
234         /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
235         /// </summary>
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)
239         {
240             if (!_disposedValue)
241             {
242                 if (disposing)
243                 {
244                     _backend.Dispose();
245                 }
246
247                 _disposedValue = true;
248             }
249             base.Dispose(disposing);
250         }
251
252         private void ChangeCurrentCultureInfo(string locale)
253         {
254             ULocale pLocale = new ULocale(locale);
255             CultureInfo currentCultureInfo = null;
256
257             try
258             {
259                 currentCultureInfo = new CultureInfo(pLocale.Locale.Replace("_", "-"));
260             }
261             catch (CultureNotFoundException)
262             {
263                 currentCultureInfo = GetFallbackCultureInfo(pLocale);
264             }
265
266             CultureInfo.CurrentCulture = currentCultureInfo;
267         }
268
269         private CultureInfo GetCultureInfo(string locale)
270         {
271             CultureInfo cultureInfo = null;
272
273             try
274             {
275                 cultureInfo = new CultureInfo(locale);
276             }
277             catch (CultureNotFoundException)
278             {
279                 return null;
280             }
281
282             return cultureInfo;
283         }
284
285         private CultureInfo GetFallbackCultureInfo(ULocale uLocale)
286         {
287             string locale = string.Empty;
288             CultureInfo fallbackCultureInfo = null;
289
290             if (uLocale.Script != null && uLocale.Country != null)
291             {
292                 locale = uLocale.Language + "-" + uLocale.Script + "-" + uLocale.Country;
293                 fallbackCultureInfo = GetCultureInfo(locale);
294             }
295
296             if (fallbackCultureInfo == null && uLocale.Script != null)
297             {
298                 locale = uLocale.Language + "-" + uLocale.Script;
299                 fallbackCultureInfo = GetCultureInfo(locale);
300             }
301
302             if (fallbackCultureInfo == null && uLocale.Country != null)
303             {
304                 locale = uLocale.Language + "-" + uLocale.Country;
305                 fallbackCultureInfo = GetCultureInfo(locale);
306             }
307
308             if (fallbackCultureInfo == null)
309             {
310                 try
311                 {
312                     fallbackCultureInfo = new CultureInfo(uLocale.Language);
313                 }
314                 catch (CultureNotFoundException)
315                 {
316                     fallbackCultureInfo = new CultureInfo("en");
317                 }
318             }
319
320             return fallbackCultureInfo;
321         }
322     }
323
324     internal class ULocale
325     {
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;
332
333         internal ULocale(string locale)
334         {
335             Locale = Canonicalize(locale);
336             Language = GetLanguage(Locale);
337             Script = GetScript(Locale);
338             Country = GetCountry(Locale);
339             Variant = GetVariant(Locale);
340         }
341
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; }
347
348         private string Canonicalize(string localeName)
349         {
350             int err = ICU_U_ZERO_ERROR;
351
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)
355             {
356                 return null;
357             }
358
359             return sb.ToString();
360         }
361
362         private string GetLanguage(string locale)
363         {
364             int err = ICU_U_ZERO_ERROR;
365
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)
369             {
370                 return null;
371             }
372
373             return sb.ToString();
374         }
375
376         private string GetScript(string locale)
377         {
378             int err = ICU_U_ZERO_ERROR;
379
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)
383             {
384                 return null;
385             }
386
387             return sb.ToString();
388         }
389
390         private string GetCountry(string locale)
391         {
392             int err = ICU_U_ZERO_ERROR;
393
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)
397             {
398                 return null;
399             }
400
401             return sb.ToString();
402         }
403
404         private string GetVariant(string locale)
405         {
406             int err = ICU_U_ZERO_ERROR;
407
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)
411             {
412                 return null;
413             }
414
415             return sb.ToString();
416         }
417     }
418 }