2 * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Collections.Generic;
20 namespace Tizen.Applications.ComponentBased.Common
23 /// The class for supporting multi-components based application model.
25 /// <since_tizen> 6 </since_tizen>
26 public abstract class ComponentBasedApplication : Application
28 private const string LogTag = "Tizen.Applications.ComponentBasedApplicationBase";
29 private Dictionary<Type, ComponentStateManger> _componentFactories = new Dictionary<Type, ComponentStateManger>();
30 private Interop.CBApplication.CBAppLifecycleCallbacks _callbacks;
33 /// Initializes the ComponentBasedApplicationBase class.
35 /// <param name="typeInfo">The component type information.
36 /// The key should be a class type of FrameComponent or SubComponent subclass.
37 /// The value should be a component id which is declared in tizen-manifest.xml.
39 /// <since_tizen> 6 </since_tizen>
40 public ComponentBasedApplication(IDictionary<Type, string> typeInfo)
42 _callbacks.OnInit = new Interop.CBApplication.CBAppInitCallback(OnInitNative);
43 _callbacks.OnFinished = new Interop.CBApplication.CBAppFiniCallback(OnFinishedNative);
44 _callbacks.OnRun = new Interop.CBApplication.CBAppRunCallback(OnRunNative);
45 _callbacks.OnExit = new Interop.CBApplication.CBAppExitCallback(OnExitNative);
46 _callbacks.OnCreate = new Interop.CBApplication.CBAppCreateCallback(OnCreateNative);
47 _callbacks.OnTerminate = new Interop.CBApplication.CBAppTerminateCallback(OnTerminateNative);
49 foreach (var component in typeInfo)
51 RegisterComponent(component.Key, component.Value);
56 /// Registers a component.
58 /// <param name="compType">Class type</param>
59 /// <param name="compId">Component ID</param>
60 /// <exception cref="ArgumentException">Thrown when component type is already added or not sub-class of FrameComponent or ServiceComponent</exception>
61 /// <since_tizen> 6 </since_tizen>
62 public void RegisterComponent(Type compType, string compId)
64 if (_componentFactories.ContainsKey(compType))
66 throw new ArgumentException("Already exist type");
69 if (typeof(FrameComponent).IsAssignableFrom(compType))
71 Log.Info(LogTag, "Add frame component");
72 _componentFactories.Add(compType, new FrameComponentStateManager(compType, compId, this));
74 else if (typeof(ServiceComponent).IsAssignableFrom(compType))
76 Log.Info(LogTag, "Add service component");
77 _componentFactories.Add(compType, new ServiceComponentStateManager(compType, compId, this));
81 throw new ArgumentException("compType must be sub type of FrameComponent or ServiceComponent", "compType");
86 /// Runs the application's main loop.
88 /// <param name="args">Arguments from commandline.</param>
89 /// <exception cref="InvalidOperationException">Thrown when component type is already added to the component.</exception>
90 /// <since_tizen> 6 </since_tizen>
91 public override void Run(string[] args)
95 string[] argsClone = new string[args.Length + 1];
98 args.CopyTo(argsClone, 1);
100 argsClone[0] = string.Empty;
102 Interop.CBApplication.ErrorCode err = Interop.CBApplication.BaseMain(argsClone.Length, argsClone, ref _callbacks, IntPtr.Zero);
103 if (err != Interop.CBApplication.ErrorCode.None)
105 Log.Error(LogTag, "Failed to run the application. Err = " + err);
106 throw new InvalidOperationException("Fail to run application : err(" + err + ")");
111 /// Exits the main loop of the application.
113 /// <since_tizen> 6 </since_tizen>
114 public override void Exit()
116 Interop.CBApplication.BaseExit();
119 private IntPtr OnCreateNative(IntPtr data)
121 Log.Debug(LogTag, "On create");
122 IntPtr nativeComponentFactoryMap = IntPtr.Zero;
123 foreach (KeyValuePair<Type, ComponentStateManger> entry in _componentFactories)
125 nativeComponentFactoryMap = entry.Value.Bind(nativeComponentFactoryMap);
128 return nativeComponentFactoryMap;
131 private void OnTerminateNative(IntPtr data)
135 private void OnRunNative(IntPtr data)
140 private void OnExitNative(IntPtr data)
145 private void OnInitNative(int argc, string[] argv, IntPtr userData)
150 private void OnFinishedNative(IntPtr data)
156 /// This method will be called before running main-loop
158 /// <param name="args"></param>
159 /// <since_tizen> 6 </since_tizen>
160 protected virtual void OnInit(string[] args)
165 /// This method will be called after exiting main-loop
167 /// <since_tizen> 6 </since_tizen>
168 protected virtual void OnFinished()
173 /// This method will be called to start main-loop
175 /// <since_tizen> 6 </since_tizen>
176 protected abstract void OnRun();
179 /// This method will be called to exit main-loop
181 /// <since_tizen> 6 </since_tizen>
182 protected virtual void OnExit()