[Tizen.Applications.ComponentBased][TCSACR-265][Add] Add ComponentBased application...
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.ComponentBased / Tizen.Applications.ComponentBased.Common / BaseComponent.cs
1 /*
2  * Copyright (c) 2019 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.Threading.Tasks;
19
20 namespace Tizen.Applications.ComponentBased.Common
21 {
22     /// <summary>
23     /// This is a base-component class.
24     /// It provides common functions of FrameComponent and ServiceComponent.
25     /// </summary>
26     /// <remarks>
27     /// This class cannot be registered by ComponentBased applications.
28     /// </remarks>
29     /// <since_tizen> 6 </since_tizen>
30     public abstract class BaseComponent
31     {
32         internal IntPtr Handle;
33
34         /// <summary>
35         /// Occurs when the system memory is low.
36         /// </summary>
37         /// <since_tizen> 6 </since_tizen>
38         public event EventHandler<LowMemoryEventArgs> LowMemory;
39
40         /// <summary>
41         /// Occurs when the system battery is low.
42         /// </summary>
43         /// <since_tizen> 6 </since_tizen>
44         public event EventHandler<LowBatteryEventArgs> LowBattery;
45
46         /// <summary>
47         /// Occurs when the system language is chagned.
48         /// </summary>
49         /// <since_tizen> 6 </since_tizen>
50         public event EventHandler<LocaleChangedEventArgs> LocaleChanged;
51
52         /// <summary>
53         /// Occurs when the region format is changed.
54         /// </summary>
55         /// <since_tizen> 6 </since_tizen>
56         public event EventHandler<RegionFormatChangedEventArgs> RegionFormatChanged;
57
58         /// <summary>
59         /// Occurs when the device orientation is changed.
60         /// </summary>
61         /// <since_tizen> 6 </since_tizen>
62         public event EventHandler<DeviceOrientationEventArgs> DeviceOrientationChanged;
63
64         /// <summary>
65         /// Occurs when the device orientation is changed.
66         /// </summary>
67         /// <since_tizen> 6 </since_tizen>
68         public event EventHandler<SuspendedStateEventArgs> SuspendedStateChanged;
69
70         /// <summary>
71         /// A component instance ID.
72         /// It will be created after OnCreate method is invoked.
73         /// </summary>
74         /// <since_tizen> 6 </since_tizen>
75         public string Id { get; private set; }
76
77         /// <summary>
78         /// A component ID
79         /// </summary>
80         /// <since_tizen> 6 </since_tizen>
81         public string ComponentId { get; private set; }
82
83         /// <summary>
84         /// Parent object
85         /// </summary>
86         /// <since_tizen> 6 </since_tizen>
87         public ComponentBasedApplication Parent { get; private set; }
88
89         /// <summary>
90         /// Finish current component
91         /// </summary>
92         /// <since_tizen> 6 </since_tizen>
93         public void Finish()
94         {
95             Interop.CBApplication.ComponentFinish(Handle);
96         }
97
98         internal void Bind(IntPtr handle, string compId, string instanceId, ComponentBasedApplication parent)
99         {
100             Handle = handle;
101             Id = instanceId;
102             ComponentId = compId;
103             Parent = parent;
104         }
105
106         /// <summary>
107         /// Overrides this method if want to handle behavior to restore the previous status.
108         /// </summary>
109         /// <param name="c">Contents. It can be used only in the callback. To use outside, make a copy. </param>
110         /// <since_tizen> 6 </since_tizen>
111         public virtual void OnRestoreContents(Bundle c)
112         {
113         }
114
115         /// <summary>
116         /// Overrides this method if want to handle behavior to save current status.
117         /// </summary>
118         /// <param name="c">Contents. It can be used only in the callback. To use outside, make a copy. </param>
119         /// <since_tizen> 6 </since_tizen>
120         public virtual void OnSaveContent(Bundle c)
121         {
122         }
123
124         internal void OnLanguageChangedCallback(string language)
125         {
126             LocaleChanged?.Invoke(this, new LocaleChangedEventArgs(language));
127         }
128
129         internal void OnDeviceOrientationChangedCallback(int orientation)
130         {
131             DeviceOrientationChanged?.Invoke(this, new DeviceOrientationEventArgs((DeviceOrientation)orientation));
132         }
133
134         internal void OnLowBatteryCallback(int status)
135         {
136             LowBattery?.Invoke(this, new LowBatteryEventArgs((LowBatteryStatus)status));
137         }
138
139         internal void OnLowMemoryCallback(int status)
140         {
141             LowMemory?.Invoke(this, new LowMemoryEventArgs((LowMemoryStatus)status));
142         }
143
144         internal void OnRegionFormatChangedCallback(string region)
145         {
146             RegionFormatChanged?.Invoke(this, new RegionFormatChangedEventArgs(region));
147         }
148
149         internal void OnSuspendedStateCallback(int state)
150         {
151             SuspendedStateChanged?.Invoke(this, new SuspendedStateEventArgs((SuspendedState)state));
152         }
153
154         /// <summary>
155         /// Sends the launch request asynchronously.
156         /// </summary>
157         /// <remarks>
158         /// To use group mode, you must use this function instead of SendLaunchRequestAsync().
159         /// </remarks>
160         /// <param name="control">appcontrol object</param>
161         /// <param name="replyAfterLaunching">The callback function to be called when the reply is delivered.</param>
162         /// <returns>A task with the result of the launch request.</returns>
163         /// <exception cref="ArgumentException">Thrown when failed because of the argument is invalid.</exception>
164         /// <exception cref="InvalidOperationException">Thrown when fail to set component information to the AppControl.</exception>
165         /// <exception cref="Exceptions.AppNotFoundException">Thrown when the application to run is not found.</exception>
166         /// <exception cref="Exceptions.LaunchRejectedException">Thrown when the launch request is rejected.</exception>
167         /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
168         /// <since_tizen> 6 </since_tizen>
169         public Task<AppControlResult> SendLaunchRequestAsync(AppControl control, AppControlReplyCallback replyAfterLaunching)
170         {
171             int ret = Interop.AppControl.SetCallerInstanceId(control.SafeAppControlHandle, Id);
172             if (ret != 0)
173                 throw new InvalidOperationException("Failed to set id");
174
175             return AppControl.SendLaunchRequestAsync(control, replyAfterLaunching);
176         }
177     }
178 }