326bb83da0fec467ccee313b30957ccb88d992af
[platform/core/csapi/tizenfx.git] / Tizen.Applications / Tizen.Applications / CoreApplication.cs
1 // Copyright 2016 by Samsung Electronics, Inc.,
2 //
3 // This software is the confidential and proprietary information
4 // of Samsung Electronics, Inc. ("Confidential Information"). You
5 // shall not disclose such Confidential Information and shall use
6 // it only in accordance with the terms of the license agreement
7 // you entered into with Samsung.
8
9 using System;
10
11 using Tizen.Applications.CoreBackend;
12
13 namespace Tizen.Applications
14 {
15     /// <summary>
16     /// Class that represents an application controlled lifecycles by the backend system.
17     /// </summary>
18     public class CoreApplication : Application
19     {
20         private readonly ICoreBackend _backend;
21         private bool _disposedValue = false;
22
23         /// <summary>
24         /// Initializes the CoreApplication class.
25         /// </summary>
26         /// <param name="backend">The backend instance implementing ICoreBacked interface.</param>
27         public CoreApplication(ICoreBackend backend)
28         {
29             _backend = backend;
30         }
31
32         /// <summary>
33         /// Occurs when the application is launched.
34         /// </summary>
35         public event EventHandler Created;
36
37         /// <summary>
38         /// Occurs when the application is about to shutdown.
39         /// </summary>
40         public event EventHandler Terminated;
41
42         /// <summary>
43         /// Occurs whenever the application receives the appcontrol message.
44         /// </summary>
45         public event EventHandler<AppControlReceivedEventArgs> AppControlReceived;
46
47         /// <summary>
48         /// Occurs when the system memory is low.
49         /// </summary>
50         public event EventHandler<LowMemoryEventArgs> LowMemory;
51
52         /// <summary>
53         /// Occurs when the system battery is low.
54         /// </summary>
55         public event EventHandler<LowBatteryEventArgs> LowBattery;
56
57         /// <summary>
58         /// Occurs when the system language is chagned.
59         /// </summary>
60         public event EventHandler<LocaleChangedEventArgs> LocaleChanged;
61
62         /// <summary>
63         /// Occurs when the region format is changed.
64         /// </summary>
65         public event EventHandler<RegionFormatChangedEventArgs> RegionFormatChanged;
66
67         /// <summary>
68         /// The backend instance.
69         /// </summary>
70         protected ICoreBackend Backend { get; private set; }
71
72         /// <summary>
73         /// Runs the application's main loop.
74         /// </summary>
75         /// <param name="args">Arguments from commandline.</param>
76         public override void Run(string[] args)
77         {
78             base.Run(args);
79
80             _backend.AddEventHandler(EventType.Created, OnCreate);
81             _backend.AddEventHandler(EventType.Terminated, OnTerminate);
82             _backend.AddEventHandler<AppControlReceivedEventArgs>(EventType.AppControlReceived, OnAppControlReceived);
83             _backend.AddEventHandler<LowMemoryEventArgs>(EventType.LowMemory, OnLowMemory);
84             _backend.AddEventHandler<LowBatteryEventArgs>(EventType.LowBattery, OnLowBattery);
85             _backend.AddEventHandler<LocaleChangedEventArgs>(EventType.LocaleChanged, OnLocaleChanged);
86             _backend.AddEventHandler<RegionFormatChangedEventArgs>(EventType.RegionFormatChanged, OnRegionFormatChanged);
87
88             _backend.Run(args);
89         }
90
91         /// <summary>
92         /// Exits the main loop of the application. 
93         /// </summary>
94         public override void Exit()
95         {
96             _backend.Exit();
97         }
98
99         /// <summary>
100         /// Overrides this method if want to handle behavior when the application is launched.
101         /// If base.OnCreated() is not called, the event 'Created' will not be emitted.
102         /// </summary>
103         protected virtual void OnCreate()
104         {
105             Created?.Invoke(this, EventArgs.Empty);
106         }
107
108         /// <summary>
109         /// Overrides this method if want to handle behavior when the application is terminated.
110         /// If base.OnTerminate() is not called, the event 'Terminated' will not be emitted.
111         /// </summary>
112         protected virtual void OnTerminate()
113         {
114             Terminated?.Invoke(this, EventArgs.Empty);
115         }
116
117         /// <summary>
118         /// Overrides this method if want to handle behavior when the application receives the appcontrol message.
119         /// If base.OnAppControlReceived() is not called, the event 'AppControlReceived' will not be emitted.
120         /// </summary>
121         /// <param name="e"></param>
122         protected virtual void OnAppControlReceived(AppControlReceivedEventArgs e)
123         {
124             AppControlReceived?.Invoke(this, e);
125         }
126
127         /// <summary>
128         /// Overrides this method if want to handle behavior when the system memory is low.
129         /// If base.OnLowMemory() is not called, the event 'LowMemory' will not be emitted.
130         /// </summary>
131         protected virtual void OnLowMemory(LowMemoryEventArgs e)
132         {
133             LowMemory?.Invoke(this, e);
134         }
135
136         /// <summary>
137         /// Overrides this method if want to handle behavior when the system battery is low.
138         /// If base.OnLowBattery() is not called, the event 'LowBattery' will not be emitted.
139         /// </summary>
140         protected virtual void OnLowBattery(LowBatteryEventArgs e)
141         {
142             LowBattery?.Invoke(this, e);
143         }
144
145         /// <summary>
146         /// Overrides this method if want to handle behavior when the system language is changed.
147         /// If base.OnLocaleChanged() is not called, the event 'LocaleChanged' will not be emitted.
148         /// </summary>
149         protected virtual void OnLocaleChanged(LocaleChangedEventArgs e)
150         {
151             LocaleChanged?.Invoke(this, e);
152         }
153
154         /// <summary>
155         /// Overrides this method if want to handle behavior when the region format is changed.
156         /// If base.OnRegionFormatChanged() is not called, the event 'RegionFormatChanged' will not be emitted.
157         /// </summary>
158         protected virtual void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
159         {
160             RegionFormatChanged?.Invoke(this, e);
161         }
162
163         /// <summary>
164         /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
165         /// </summary>
166         /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
167         protected override void Dispose(bool disposing)
168         {
169             if (!_disposedValue)
170             {
171                 if (disposing)
172                 {
173                     _backend.Dispose();
174                 }
175
176                 _disposedValue = true;
177             }
178             base.Dispose(disposing);
179         }
180     }
181 }