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