Setting since_tizen 3/4 on Tizen.NET API
[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
19 using Tizen.Applications.CoreBackend;
20
21 namespace Tizen.Applications
22 {
23     /// <summary>
24     /// This class represents an application controlled lifecycles by the backend system.
25     /// </summary>
26     /// <since_tizen> 3 </since_tizen>
27     public class CoreApplication : Application
28     {
29         private readonly ICoreBackend _backend;
30         private bool _disposedValue = false;
31
32         /// <summary>
33         /// Initializes the CoreApplication class.
34         /// </summary>
35         /// <param name="backend">The backend instance implementing ICoreBacked interface.</param>
36         /// <since_tizen> 3 </since_tizen>
37         public CoreApplication(ICoreBackend backend)
38         {
39             _backend = backend;
40         }
41
42         /// <summary>
43         /// Occurs when the application is launched.
44         /// </summary>
45         /// <since_tizen> 3 </since_tizen>
46         public event EventHandler Created;
47
48         /// <summary>
49         /// Occurs when the application is about to shutdown.
50         /// </summary>
51         /// <since_tizen> 3 </since_tizen>
52         public event EventHandler Terminated;
53
54         /// <summary>
55         /// Occurs whenever the application receives the appcontrol message.
56         /// </summary>
57         /// <since_tizen> 3 </since_tizen>
58         public event EventHandler<AppControlReceivedEventArgs> AppControlReceived;
59
60         /// <summary>
61         /// Occurs when the system memory is low.
62         /// </summary>
63         /// <since_tizen> 3 </since_tizen>
64         public event EventHandler<LowMemoryEventArgs> LowMemory;
65
66         /// <summary>
67         /// Occurs when the system battery is low.
68         /// </summary>
69         /// <since_tizen> 3 </since_tizen>
70         public event EventHandler<LowBatteryEventArgs> LowBattery;
71
72         /// <summary>
73         /// Occurs when the system language is chagned.
74         /// </summary>
75         /// <since_tizen> 3 </since_tizen>
76         public event EventHandler<LocaleChangedEventArgs> LocaleChanged;
77
78         /// <summary>
79         /// Occurs when the region format is changed.
80         /// </summary>
81         /// <since_tizen> 3 </since_tizen>
82         public event EventHandler<RegionFormatChangedEventArgs> RegionFormatChanged;
83
84         /// <summary>
85         /// Occurs when the device orientation is changed.
86         /// </summary>
87         /// <since_tizen> 3 </since_tizen>
88         public event EventHandler<DeviceOrientationEventArgs> DeviceOrientationChanged;
89
90         /// <summary>
91         /// The backend instance.
92         /// </summary>
93         /// <since_tizen> 3 </since_tizen>
94         protected ICoreBackend Backend { get { return _backend; } }
95
96         /// <summary>
97         /// Runs the application's main loop.
98         /// </summary>
99         /// <param name="args">Arguments from commandline.</param>
100         /// <since_tizen> 3 </since_tizen>
101         public override void Run(string[] args)
102         {
103             base.Run(args);
104
105             _backend.AddEventHandler(EventType.Created, OnCreate);
106             _backend.AddEventHandler(EventType.Terminated, OnTerminate);
107             _backend.AddEventHandler<AppControlReceivedEventArgs>(EventType.AppControlReceived, OnAppControlReceived);
108             _backend.AddEventHandler<LowMemoryEventArgs>(EventType.LowMemory, OnLowMemory);
109             _backend.AddEventHandler<LowBatteryEventArgs>(EventType.LowBattery, OnLowBattery);
110             _backend.AddEventHandler<LocaleChangedEventArgs>(EventType.LocaleChanged, OnLocaleChanged);
111             _backend.AddEventHandler<RegionFormatChangedEventArgs>(EventType.RegionFormatChanged, OnRegionFormatChanged);
112             _backend.AddEventHandler<DeviceOrientationEventArgs>(EventType.DeviceOrientationChanged, OnDeviceOrientationChanged);
113
114             string[] argsClone = null;
115
116             if (args == null)
117             {
118                 argsClone = new string[1];
119             }
120             else
121             {
122                 argsClone = new string[args.Length + 1];
123                 args.CopyTo(argsClone, 1);
124             }
125             argsClone[0] = string.Empty;
126             _backend.Run(argsClone);
127         }
128
129         /// <summary>
130         /// Exits the main loop of the application.
131         /// </summary>
132         /// <since_tizen> 3 </since_tizen>
133         public override void Exit()
134         {
135             _backend.Exit();
136         }
137
138         /// <summary>
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.
141         /// </summary>
142         /// <since_tizen> 3 </since_tizen>
143         protected virtual void OnCreate()
144         {
145             Created?.Invoke(this, EventArgs.Empty);
146         }
147
148         /// <summary>
149         /// Overrides this method if want to handle behavior when the application is terminated.
150         /// If base.OnTerminate() is not called, the event 'Terminated' will not be emitted.
151         /// </summary>
152         /// <since_tizen> 3 </since_tizen>
153         protected virtual void OnTerminate()
154         {
155             Terminated?.Invoke(this, EventArgs.Empty);
156         }
157
158         /// <summary>
159         /// Overrides this method if want to handle behavior when the application receives the appcontrol message.
160         /// If base.OnAppControlReceived() is not called, the event 'AppControlReceived' will not be emitted.
161         /// </summary>
162         /// <param name="e"></param>
163         /// <since_tizen> 3 </since_tizen>
164         protected virtual void OnAppControlReceived(AppControlReceivedEventArgs e)
165         {
166             AppControlReceived?.Invoke(this, e);
167         }
168
169         /// <summary>
170         /// Overrides this method if want to handle behavior when the system memory is low.
171         /// If base.OnLowMemory() is not called, the event 'LowMemory' will not be emitted.
172         /// </summary>
173         /// <since_tizen> 3 </since_tizen>
174         protected virtual void OnLowMemory(LowMemoryEventArgs e)
175         {
176             LowMemory?.Invoke(this, e);
177             System.GC.Collect();
178         }
179
180         /// <summary>
181         /// Overrides this method if want to handle behavior when the system battery is low.
182         /// If base.OnLowBattery() is not called, the event 'LowBattery' will not be emitted.
183         /// </summary>
184         /// <since_tizen> 3 </since_tizen>
185         protected virtual void OnLowBattery(LowBatteryEventArgs e)
186         {
187             LowBattery?.Invoke(this, e);
188         }
189
190         /// <summary>
191         /// Overrides this method if want to handle behavior when the system language is changed.
192         /// If base.OnLocaleChanged() is not called, the event 'LocaleChanged' will not be emitted.
193         /// </summary>
194         /// <since_tizen> 3 </since_tizen>
195         protected virtual void OnLocaleChanged(LocaleChangedEventArgs e)
196         {
197             LocaleChanged?.Invoke(this, e);
198         }
199
200         /// <summary>
201         /// Overrides this method if want to handle behavior when the region format is changed.
202         /// If base.OnRegionFormatChanged() is not called, the event 'RegionFormatChanged' will not be emitted.
203         /// </summary>
204         /// <since_tizen> 3 </since_tizen>
205         protected virtual void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
206         {
207             RegionFormatChanged?.Invoke(this, e);
208         }
209
210         /// <summary>
211         /// Overrides this method if want to handle behavior when the device orientation is changed.
212         /// If base.OnRegionFormatChanged() is not called, the event 'RegionFormatChanged' will not be emitted.
213         /// </summary>
214         /// <since_tizen> 3 </since_tizen>
215         protected virtual void OnDeviceOrientationChanged(DeviceOrientationEventArgs e)
216         {
217             DeviceOrientationChanged?.Invoke(this, e);
218         }
219
220         /// <summary>
221         /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
222         /// </summary>
223         /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
224         /// <since_tizen> 3 </since_tizen>
225         protected override void Dispose(bool disposing)
226         {
227             if (!_disposedValue)
228             {
229                 if (disposing)
230                 {
231                     _backend.Dispose();
232                 }
233
234                 _disposedValue = true;
235             }
236             base.Dispose(disposing);
237         }
238     }
239 }