[Tizen.Applications.ComponentBased][TCSACR-265][Add] Add ComponentBased application...
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.ComponentBased / Tizen.Applications.ComponentBased.Common / ServiceComponentStateManager.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace Tizen.Applications.ComponentBased.Common
6 {
7     internal class ServiceComponentStateManager : ComponentStateManger
8     {
9         private Interop.CBApplication.ServiceLifecycleCallbacks _callbacks;
10
11         internal ServiceComponentStateManager(Type ctype, string id, ComponentBasedApplication parent) : base(ctype, id, parent)
12         {
13             _callbacks.OnAction = new Interop.CBApplication.ServiceActionCallback(OnActionCallback);
14             _callbacks.OnDeviceOrientationChanged = new Interop.CBApplication.ServiceDeviceOrientationChangedCallback(OnDeviceOrientationChangedCallback);
15             _callbacks.OnLanguageChanged = new Interop.CBApplication.ServiceLanguageChangedCallback(OnLanguageChangedCallback);
16             _callbacks.OnLowBattery = new Interop.CBApplication.ServiceLowBatteryCallback(OnLowBatteryCallback);
17             _callbacks.OnLowMemory = new Interop.CBApplication.ServiceLowMemoryCallback(OnLowMemoryCallback);
18             _callbacks.OnRegionFormatChanged = new Interop.CBApplication.ServiceRegionFormatChangedCallback(OnRegionFormatChangedCallback);
19             _callbacks.OnRestore = new Interop.CBApplication.ServiceRestoreCallback(OnRestoreCallback);
20             _callbacks.OnSave = new Interop.CBApplication.ServiceSaveCallback(OnSaveCallback);
21             _callbacks.OnSuspendedState = new Interop.CBApplication.ServiceSuspendedStateCallback(OnSuspendedStateCallback);
22             _callbacks.OnCreate = new Interop.CBApplication.ServiceCreateCallback(OnCreateCallback);
23             _callbacks.OnDestroy = new Interop.CBApplication.ServiceDestroyCallback(OnDestroyCallback);
24             _callbacks.OnStart = new Interop.CBApplication.ServiceStartCommandCallback(OnStartCallback);
25             Parent = parent;
26         }
27
28         private bool OnCreateCallback(IntPtr context, IntPtr userData)
29         {
30             ServiceComponent sc = Activator.CreateInstance(ComponentClassType) as ServiceComponent;
31             if (sc == null)
32                 return false;
33
34             string id;
35             Interop.CBApplication.GetInstanceId(context, out id);
36             sc.Bind(context, ComponentId, id, Parent);
37
38             if (!sc.OnCreate())
39             {
40                 return false;
41             }
42
43             AddComponent(sc);
44             return true;
45         }
46
47         private void OnStartCallback(IntPtr context, IntPtr appControl, bool restarted, IntPtr userData)
48         {
49             foreach (ServiceComponent sc in Instances)
50             {
51                 if (sc.Handle == context)
52                 {
53                     SafeAppControlHandle handle = new SafeAppControlHandle(appControl, false);
54                     AppControl control = new AppControl(handle);
55                     sc.OnStartCommand(control, restarted);
56                     break;
57                 }
58             }
59         }
60
61         private void OnDestroyCallback(IntPtr context, IntPtr userData)
62         {
63             foreach (ServiceComponent sc in Instances)
64             {
65                 if (sc.Handle == context)
66                 {
67                     sc.OnDestroy();
68                     RemoveComponent(sc);
69                     break;
70                 }
71             }
72             return;
73         }
74
75         private void OnActionCallback(IntPtr context, string action, IntPtr appControl, IntPtr userData)
76         {
77         }
78
79         internal override IntPtr Bind(IntPtr h)
80         {
81             return Interop.CBApplication.BaseAddServiceComponent(h, ComponentId, ref _callbacks, IntPtr.Zero);
82         }
83     }
84 }