[Tizen.Applications.ComponentBased][TCSACR-400] Add WidgetComponent API (#2604)
authorChangGyu Choi <uppletaste@gmail.com>
Mon, 8 Feb 2021 08:59:25 +0000 (17:59 +0900)
committerGitHub <noreply@github.com>
Mon, 8 Feb 2021 08:59:25 +0000 (17:59 +0900)
* Add widget component

* Fix tizen version in description

* Remove unnecessary file

* Remove unnecessary tag

* Fix description

Co-authored-by: hjhun <36876573+hjhun@users.noreply.github.com>
src/Tizen.Applications.ComponentBased/Interop/Interop.CBApplication.cs
src/Tizen.Applications.ComponentBased/Interop/Interop.Libraries.cs
src/Tizen.Applications.ComponentBased/Tizen.Applications.ComponentBased.Common/ComponentBasedApplication.cs
src/Tizen.Applications.ComponentBased/Tizen.Applications.ComponentBased.Common/IWindowProxy.cs [new file with mode: 0644]
src/Tizen.Applications.ComponentBased/Tizen.Applications.ComponentBased.Common/WidgetComponent.cs [new file with mode: 0644]
src/Tizen.Applications.ComponentBased/Tizen.Applications.ComponentBased.Common/WidgetComponentStateManager.cs [new file with mode: 0644]

index 5abc2ac..eaad727 100644 (file)
@@ -60,7 +60,8 @@ internal static partial class Interop
         internal enum NativeComponentType
         {
             Frame = 0,
-            Service
+            Service,
+            Widget
         }
 
         internal enum NativeDisplayStatus {
@@ -132,6 +133,39 @@ internal static partial class Interop
             public ServiceSuspendedStateCallback OnSuspendedState;
         }
 
+        internal delegate IntPtr WidgetCreateCallback(IntPtr context, int width, int height, IntPtr userData);
+        internal delegate void WidgetStartCallback(IntPtr context, bool restarted, IntPtr userData);
+        internal delegate void WidgetResumeCallback(IntPtr context, IntPtr userData);
+        internal delegate void WidgetPauseCallback(IntPtr context, IntPtr userData);
+        internal delegate void WidgetStopCallback(IntPtr context, IntPtr userData);
+        internal delegate void WidgetDestroyCallback(IntPtr context, bool permanent, IntPtr userData);
+        internal delegate void WidgetRestoreCallback(IntPtr context, IntPtr content, IntPtr userData);
+        internal delegate void WidgetSaveCallback(IntPtr context, IntPtr content, IntPtr userData);
+        internal delegate void WidgetDeviceOrientationChangedCallback(IntPtr context, int orientation, IntPtr userData);
+        internal delegate void WidgetLanguageChangedCallback(IntPtr context, string language, IntPtr userData);
+        internal delegate void WidgetRegionFormatChangedCallback(IntPtr context, string region, IntPtr userData);
+        internal delegate void WidgetLowBatteryCallback(IntPtr context, int status, IntPtr userData);
+        internal delegate void WidgetLowMemoryCallback(IntPtr context, int status, IntPtr userData);
+        internal delegate void WidgetSuspendedStateCallback(IntPtr context, int state, IntPtr userData);
+
+        internal struct WidgetLifecycleCallbacks
+        {
+            public WidgetCreateCallback OnCreate;
+            public WidgetStartCallback OnStart;
+            public WidgetResumeCallback OnResume;
+            public WidgetPauseCallback OnPause;
+            public WidgetStopCallback OnStop;
+            public WidgetDestroyCallback OnDestroy;
+            public WidgetRestoreCallback OnRestore;
+            public WidgetSaveCallback OnSave;
+            public WidgetDeviceOrientationChangedCallback OnDeviceOrientationChanged;
+            public WidgetLanguageChangedCallback OnLanguageChanged;
+            public WidgetRegionFormatChangedCallback OnRegionFormatChanged;
+            public WidgetLowBatteryCallback OnLowBattery;
+            public WidgetLowMemoryCallback OnLowMemory;
+            public WidgetSuspendedStateCallback OnSuspendedState;
+        }
+
         internal delegate IntPtr BaseCreateCallback(IntPtr context, IntPtr userData);
         internal delegate void BaseDestroyCallback(IntPtr context, IntPtr userData);
         internal delegate void BaseRestoreCallback(IntPtr context, IntPtr content, IntPtr userData);
@@ -155,6 +189,9 @@ internal static partial class Interop
         [DllImport(Libraries.CompCoreBase, EntryPoint = "component_based_app_base_add_service_component")]
         internal static extern IntPtr BaseAddServiceComponent(IntPtr comp_class, string compId, ref ServiceLifecycleCallbacks callback, IntPtr userData);
 
+        [DllImport(Libraries.WidgetCompCoreBase, EntryPoint = "component_based_app_base_add_widget_component")]
+        internal static extern IntPtr BaseAddWidgetComponent(IntPtr comp_class, string compId, ref WidgetLifecycleCallbacks callback, IntPtr userData);
+
         [DllImport(Libraries.CompCoreBase, EntryPoint = "base_frame_create_window")]
         internal static extern IntPtr BaseFrameCreateWindow(out IntPtr winHandle, int winId, IntPtr raw);
 
@@ -167,6 +204,15 @@ internal static partial class Interop
         [DllImport(Libraries.CompCoreBase, EntryPoint = "base_frame_get_display_status")]
         internal static extern ErrorCode BaseFrameGetDisplayStatus(IntPtr context, out NativeDisplayStatus status);
 
+        [DllImport(Libraries.WidgetCompCoreBase, EntryPoint = "base_widget_create_window")]
+        internal static extern IntPtr BaseWidgetCreateWindow(out IntPtr winHandle, int winId, IntPtr raw);
+
+        [DllImport(Libraries.WidgetCompCoreBase, EntryPoint = "base_widget_window_get_id")]
+        internal static extern IntPtr BaseWidgetWindowGetId(IntPtr winHandle, out int winId);
+
+        [DllImport(Libraries.WidgetCompCoreBase, EntryPoint = "base_widget_window_get_raw")]
+        internal static extern IntPtr BaseWidgetWindowGetRaw(IntPtr winHandle, out IntPtr raw);
+
         [DllImport(Libraries.CompCoreBase, EntryPoint = "component_get_instance_id")]
         internal static extern ErrorCode GetInstanceId(IntPtr context, out string instanceId);
 
index 43a218d..f536eea 100755 (executable)
@@ -19,7 +19,9 @@ internal static partial class Interop
     internal static partial class Libraries
     {
         public const string CompCoreBase = "libcomponent-based-core-base.so.1";
+        public const string WidgetCompCoreBase = "libcomponent-based-core-widget-base.so.1";
         public const string CompApplication = "libcomponent-based-application.so.1";
+        public const string WidgetEflComp = "libcomponent-based-efl-widget.so.1";
         public const string CompAppControl = "libcomponent-based-app-control.so.1";
         public const string CompUri = "libcomponent-based-uri.so.1";
         public const string AppControl = "libcapi-appfw-app-control.so.0";
index 641817c..8b6cf70 100755 (executable)
@@ -84,6 +84,11 @@ namespace Tizen.Applications.ComponentBased.Common
                 Log.Info(LogTag, "Add service component");
                 _componentFactories.Add(compType, new ServiceComponentStateManager(compType, compId, this));
             }
+            else if (typeof(WidgetComponent).IsAssignableFrom(compType))
+            {
+                Log.Info(LogTag, "Add widget component");
+                _componentFactories.Add(compType, new WidgetComponentStateManager(compType, compId, this));
+            }
             else
             {
                 throw new ArgumentException("compType must be sub type of FrameComponent or ServiceComponent", "compType");
diff --git a/src/Tizen.Applications.ComponentBased/Tizen.Applications.ComponentBased.Common/IWindowProxy.cs b/src/Tizen.Applications.ComponentBased/Tizen.Applications.ComponentBased.Common/IWindowProxy.cs
new file mode 100644 (file)
index 0000000..08551dc
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+using System;
+
+namespace Tizen.Applications.ComponentBased.Common
+{
+    /// <summary>
+    /// Proxy for window
+    /// </summary>
+    /// <since_tizen> 9 </since_tizen>
+    public interface IWindowProxy : IWindowInfo
+    {
+        /// <summary>
+        /// Initialize window
+        /// </summary>
+        /// <param name="width">The width of the window</param>
+        /// <param name="height">The height of the window</param>
+        void InitializeWindow(int width, int height);
+    }
+}
diff --git a/src/Tizen.Applications.ComponentBased/Tizen.Applications.ComponentBased.Common/WidgetComponent.cs b/src/Tizen.Applications.ComponentBased/Tizen.Applications.ComponentBased.Common/WidgetComponent.cs
new file mode 100644 (file)
index 0000000..6f69360
--- /dev/null
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+using System;
+
+namespace Tizen.Applications.ComponentBased.Common
+{
+    /// <summary>
+    /// The class for showing UI module
+    /// </summary>
+    /// <since_tizen> 9 </since_tizen>
+    public abstract class WidgetComponent : BaseComponent
+    {
+
+        /// <summary>
+        /// Override this method to handle behavior when the component is launched.
+        /// </summary>
+        /// <param name="width">The width of the widget component instance</param>
+        /// <param name="height">The height of the widget component instance</param>
+        /// <returns>True if a service component is successfully created</returns>
+        /// <since_tizen> 9 </since_tizen>
+        public abstract bool OnCreate(int width, int height);
+
+        /// <summary>
+        /// Override this method to create window. It will be called before OnCreate method.
+        /// </summary>
+        /// <param name="width">The width of the widget window</param>
+        /// <param name="height">The height of the widget window</param>
+        /// <returns>Window object to use</returns>
+        /// <since_tizen> 9 </since_tizen>
+        public abstract IWindowProxy CreateWindowInfo(int width, int height);
+
+        /// <summary>
+        /// Overrid this method if want to handle behavior when the component is started.
+        /// </summary>
+        /// <param name="restarted">True if it was restarted</param>
+        /// <since_tizen> 9 </since_tizen>
+        public virtual void OnStart(bool restarted)
+        {
+        }
+
+        /// <summary>
+        /// Override this method if you want to handle the behavior when the component is resumed.
+        /// </summary>
+        /// <since_tizen> 9 </since_tizen>
+        public virtual void OnResume()
+        {
+        }
+
+        /// <summary>
+        /// Override this method if you want to handle the behavior when the component is paused.
+        /// </summary>
+        /// <since_tizen> 9 </since_tizen>
+        public virtual void OnPause()
+        {
+        }
+
+        /// <summary>
+        /// Override this method if you want to handle the behavior when the component is stopped.
+        /// </summary>
+        /// <since_tizen> 9 </since_tizen>
+        public virtual void OnStop()
+        {
+        }
+
+        /// <summary>
+        /// Override this method if want to handle behavior when the component is destroyed.
+        /// </summary>
+        /// <param name="permanent">True if the instance is permanent</param>
+        /// <since_tizen> 9 </since_tizen>
+        public virtual void OnDestroy(bool permanent)
+        {
+        }
+    }
+}
diff --git a/src/Tizen.Applications.ComponentBased/Tizen.Applications.ComponentBased.Common/WidgetComponentStateManager.cs b/src/Tizen.Applications.ComponentBased/Tizen.Applications.ComponentBased.Common/WidgetComponentStateManager.cs
new file mode 100644 (file)
index 0000000..8e6512c
--- /dev/null
@@ -0,0 +1,146 @@
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+using System;
+using System.Collections.Generic;
+
+namespace Tizen.Applications.ComponentBased.Common
+{
+    internal class WidgetComponentStateManager : ComponentStateManger
+    {
+        private Interop.CBApplication.WidgetLifecycleCallbacks _callbacks;
+        private const string LogTag = "Tizen.Applications";
+        private IDictionary<string, IWindowProxy> _winDic = new Dictionary<string, IWindowProxy>();
+
+        internal WidgetComponentStateManager(Type ctype, string id, ComponentBasedApplication parent) : base(ctype, id, parent)
+        {
+            _callbacks.OnDeviceOrientationChanged = new Interop.CBApplication.WidgetDeviceOrientationChangedCallback(OnDeviceOrientationChangedCallback);
+            _callbacks.OnLanguageChanged = new Interop.CBApplication.WidgetLanguageChangedCallback(OnLanguageChangedCallback);
+            _callbacks.OnLowBattery = new Interop.CBApplication.WidgetLowBatteryCallback(OnLowBatteryCallback);
+            _callbacks.OnLowMemory = new Interop.CBApplication.WidgetLowMemoryCallback(OnLowMemoryCallback);
+            _callbacks.OnRegionFormatChanged = new Interop.CBApplication.WidgetRegionFormatChangedCallback(OnRegionFormatChangedCallback);
+            _callbacks.OnRestore = new Interop.CBApplication.WidgetRestoreCallback(OnRestoreCallback);
+            _callbacks.OnSave = new Interop.CBApplication.WidgetSaveCallback(OnSaveCallback);
+            _callbacks.OnSuspendedState = new Interop.CBApplication.WidgetSuspendedStateCallback(OnSuspendedStateCallback);
+            _callbacks.OnCreate = new Interop.CBApplication.WidgetCreateCallback(OnCreateCallback);
+            _callbacks.OnDestroy = new Interop.CBApplication.WidgetDestroyCallback(OnDestroyCallback);
+            _callbacks.OnPause = new Interop.CBApplication.WidgetPauseCallback(OnPauseCallback);
+            _callbacks.OnResume = new Interop.CBApplication.WidgetResumeCallback(OnResumeCallback);
+            _callbacks.OnStart = new Interop.CBApplication.WidgetStartCallback(OnStartCallback);
+            _callbacks.OnStop = new Interop.CBApplication.WidgetStopCallback(OnStopCallback);
+            Parent = parent;
+        }
+
+        private IntPtr OnCreateCallback(IntPtr context, int width, int height, IntPtr userData)
+        {
+            WidgetComponent fc = Activator.CreateInstance(ComponentClassType) as WidgetComponent;
+            if (fc == null)
+            {
+                Log.Error(LogTag, "Fail to create instance");
+                return IntPtr.Zero;
+            }
+
+            string id;
+            Interop.CBApplication.GetInstanceId(context, out id);
+            fc.Bind(context, ComponentId, id, Parent);
+
+            IntPtr winHandle;
+            IWindowProxy win = fc.CreateWindowInfo(width, height);
+            if (win == null)
+                return IntPtr.Zero;
+
+            win.InitializeWindow(width, height);
+            _winDic.Add(id, win);
+            if (!fc.OnCreate(width, height))
+            {
+                Log.Error(LogTag, "OnCreate fail");
+                return IntPtr.Zero;
+            }
+            Interop.CBApplication.BaseWidgetCreateWindow(out winHandle, win.ResourceId, IntPtr.Zero);
+
+            AddComponent(fc);
+            return winHandle;
+        }
+
+        private void OnStartCallback(IntPtr context, bool restarted, IntPtr userData)
+        {
+            foreach (WidgetComponent fc in Instances)
+            {
+                if (fc.Handle == context)
+                {
+                    fc.OnStart(restarted);
+                    break;
+                }
+            }
+        }
+
+        private void OnResumeCallback(IntPtr context, IntPtr userData)
+        {
+            foreach (WidgetComponent fc in Instances)
+            {
+                if (fc.Handle == context)
+                {
+                    fc.OnResume();
+                    break;
+                }
+            }
+        }
+
+        private void OnPauseCallback(IntPtr context, IntPtr userData)
+        {
+            foreach (WidgetComponent fc in Instances)
+            {
+                if (fc.Handle == context)
+                {
+                    fc.OnPause();
+                    break;
+                }
+            }
+        }
+
+        private void OnStopCallback(IntPtr context, IntPtr userData)
+        {
+            foreach (WidgetComponent fc in Instances)
+            {
+                if (fc.Handle == context)
+                {
+                    fc.OnStop();
+                    break;
+                }
+            }
+        }
+
+        private void OnDestroyCallback(IntPtr context, bool permanent, IntPtr userData)
+        {
+            foreach (WidgetComponent fc in Instances)
+            {
+                if (fc.Handle == context)
+                {
+                    fc.OnDestroy(permanent);
+                    _winDic[fc.Id].Dispose();
+                    RemoveComponent(fc);
+                    break;
+                }
+            }
+            return;
+        }
+
+        internal override IntPtr Bind(IntPtr h)
+        {
+            return Interop.CBApplication.BaseAddWidgetComponent(h, ComponentId, ref _callbacks, IntPtr.Zero);
+        }
+    }
+}