Merge remote-tracking branch 'origin/master' into tizen
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.ComponentBased / Tizen.Applications.ComponentBased.Common / ServiceComponentStateManager.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
19 namespace Tizen.Applications.ComponentBased.Common
20 {
21     internal class ServiceComponentStateManager : ComponentStateManger
22     {
23         private Interop.CBApplication.ServiceLifecycleCallbacks _callbacks;
24
25         internal ServiceComponentStateManager(Type ctype, string id, ComponentBasedApplication parent) : base(ctype, id, parent)
26         {
27             _callbacks.OnAction = new Interop.CBApplication.ServiceActionCallback(OnActionCallback);
28             _callbacks.OnDeviceOrientationChanged = new Interop.CBApplication.ServiceDeviceOrientationChangedCallback(OnDeviceOrientationChangedCallback);
29             _callbacks.OnLanguageChanged = new Interop.CBApplication.ServiceLanguageChangedCallback(OnLanguageChangedCallback);
30             _callbacks.OnLowBattery = new Interop.CBApplication.ServiceLowBatteryCallback(OnLowBatteryCallback);
31             _callbacks.OnLowMemory = new Interop.CBApplication.ServiceLowMemoryCallback(OnLowMemoryCallback);
32             _callbacks.OnRegionFormatChanged = new Interop.CBApplication.ServiceRegionFormatChangedCallback(OnRegionFormatChangedCallback);
33             _callbacks.OnRestore = new Interop.CBApplication.ServiceRestoreCallback(OnRestoreCallback);
34             _callbacks.OnSave = new Interop.CBApplication.ServiceSaveCallback(OnSaveCallback);
35             _callbacks.OnSuspendedState = new Interop.CBApplication.ServiceSuspendedStateCallback(OnSuspendedStateCallback);
36             _callbacks.OnCreate = new Interop.CBApplication.ServiceCreateCallback(OnCreateCallback);
37             _callbacks.OnDestroy = new Interop.CBApplication.ServiceDestroyCallback(OnDestroyCallback);
38             _callbacks.OnStart = new Interop.CBApplication.ServiceStartCommandCallback(OnStartCallback);
39             _callbacks.OnTimeZoneChanged = new Interop.CBApplication.ServiceTimeZoneChangedCallback(OnTimeZoneChangedCallback);
40             Parent = parent;
41         }
42
43         private bool OnCreateCallback(IntPtr context, IntPtr userData)
44         {
45             ServiceComponent sc = Activator.CreateInstance(ComponentClassType) as ServiceComponent;
46             if (sc == null)
47                 return false;
48
49             string id;
50             Interop.CBApplication.GetInstanceId(context, out id);
51             sc.Bind(context, ComponentId, id, Parent);
52
53             if (!sc.OnCreate())
54             {
55                 return false;
56             }
57
58             AddComponent(sc);
59             return true;
60         }
61
62         private void OnStartCallback(IntPtr context, IntPtr appControl, bool restarted, IntPtr userData)
63         {
64             foreach (ServiceComponent sc in Instances)
65             {
66                 if (sc.Handle == context)
67                 {
68                     using SafeAppControlHandle handle = new SafeAppControlHandle(appControl, false);
69                     AppControl control = new AppControl(handle);
70                     sc.OnStartCommand(control, restarted);
71                     break;
72                 }
73             }
74         }
75
76         private void OnDestroyCallback(IntPtr context, IntPtr userData)
77         {
78             foreach (ServiceComponent sc in Instances)
79             {
80                 if (sc.Handle == context)
81                 {
82                     sc.OnDestroy();
83                     RemoveComponent(sc);
84                     break;
85                 }
86             }
87             return;
88         }
89
90         private void OnActionCallback(IntPtr context, string action, IntPtr appControl, IntPtr userData)
91         {
92         }
93
94         internal override IntPtr Bind(IntPtr h)
95         {
96             return Interop.CBApplication.BaseAddServiceComponent(h, ComponentId, ref _callbacks, IntPtr.Zero);
97         }
98     }
99 }