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";
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 foreach (var stateManager in _componentFactories)
71 if (stateManager.Value.ComponentId == compId)
73 throw new ArgumentException("Already exist component ID(" + compId + ")");
77 if (typeof(FrameComponent).IsAssignableFrom(compType))
79 Log.Info(LogTag, "Add frame component");
80 _componentFactories.Add(compType, new FrameComponentStateManager(compType, compId, this));
82 else if (typeof(ServiceComponent).IsAssignableFrom(compType))
84 Log.Info(LogTag, "Add service component");
85 _componentFactories.Add(compType, new ServiceComponentStateManager(compType, compId, this));
87 else if (typeof(WidgetComponent).IsAssignableFrom(compType))
89 Log.Info(LogTag, "Add widget component");
90 _componentFactories.Add(compType, new WidgetComponentStateManager(compType, compId, this));
94 throw new ArgumentException("compType must be sub type of FrameComponent or ServiceComponent", "compType");
99 /// Runs the application's main loop.
101 /// <param name="args">Arguments from commandline.</param>
102 /// <exception cref="InvalidOperationException">Thrown when component type is already added to the component.</exception>
103 /// <since_tizen> 6 </since_tizen>
104 public override void Run(string[] args)
108 string[] argsClone = new string[args.Length + 1];
111 args.CopyTo(argsClone, 1);
113 argsClone[0] = string.Empty;
115 Interop.CBApplication.ErrorCode err = Interop.CBApplication.BaseMain(argsClone.Length, argsClone, ref _callbacks, IntPtr.Zero);
116 if (err != Interop.CBApplication.ErrorCode.None)
118 Log.Error(LogTag, "Failed to run the application. Err = " + err);
119 throw new InvalidOperationException("Fail to run application : err(" + err + ")");
124 /// Exits the main loop of the application.
126 /// <since_tizen> 6 </since_tizen>
127 public override void Exit()
129 Interop.CBApplication.BaseExit();
132 private IntPtr OnCreateNative(IntPtr data)
134 Log.Debug(LogTag, "On create");
135 IntPtr nativeComponentFactoryMap = IntPtr.Zero;
136 foreach (KeyValuePair<Type, ComponentStateManger> entry in _componentFactories)
138 nativeComponentFactoryMap = entry.Value.Bind(nativeComponentFactoryMap);
141 return nativeComponentFactoryMap;
144 private void OnTerminateNative(IntPtr data)
148 private void OnRunNative(IntPtr data)
153 private void OnExitNative(IntPtr data)
158 private void OnInitNative(int argc, string[] argv, IntPtr userData)
163 private void OnFinishedNative(IntPtr data)
169 /// This method will be called before running main-loop
171 /// <param name="args"></param>
172 /// <since_tizen> 6 </since_tizen>
173 protected virtual void OnInit(string[] args)
178 /// This method will be called after exiting main-loop
180 /// <since_tizen> 6 </since_tizen>
181 protected virtual void OnFinished()
186 /// This method will be called to start main-loop
188 /// <since_tizen> 6 </since_tizen>
189 protected abstract void OnRun();
192 /// This method will be called to exit main-loop
194 /// <since_tizen> 6 </since_tizen>
195 protected virtual void OnExit()