Refactor Tizen.Applications
authorWonYoung Choi <wy80.choi@samsung.com>
Wed, 9 Mar 2016 06:22:34 +0000 (15:22 +0900)
committerWonYoung Choi <wy80.choi@samsung.com>
Wed, 9 Mar 2016 07:05:52 +0000 (16:05 +0900)
Change-Id: Id05845647c03ee7041f6a095a2df62cc6019c822

28 files changed:
AppFW.sln
Tizen.Application/Tizen.Application/Actor.cs [deleted file]
Tizen.Application/Tizen.Application/Application.cs [deleted file]
Tizen.Application/Tizen.Application/ApplicationContext.cs [deleted file]
Tizen.Application/Tizen.Application/Page.cs [deleted file]
Tizen.Application/Tizen.Application/Service.cs [deleted file]
Tizen.Application/Tizen.Application/ServiceManager.cs [deleted file]
Tizen.Applications/Interop/Interop.AppControl.cs [moved from Tizen.Application/Interop/Interop.AppControl.cs with 100% similarity]
Tizen.Applications/Interop/Interop.Application.cs [moved from Tizen.Application/Interop/Interop.Application.cs with 100% similarity]
Tizen.Applications/Interop/Interop.Aul.cs [moved from Tizen.Application/Interop/Interop.Aul.cs with 100% similarity]
Tizen.Applications/Interop/Interop.Glib.cs [moved from Tizen.Application/Interop/Interop.Glib.cs with 100% similarity]
Tizen.Applications/Interop/Interop.Libraries.cs [moved from Tizen.Application/Interop/Interop.Libraries.cs with 100% similarity]
Tizen.Applications/Interop/Interop.Window.cs [moved from Tizen.Application/Interop/Interop.Window.cs with 100% similarity]
Tizen.Applications/Properties/AssemblyInfo.cs [moved from Tizen.Application/Properties/AssemblyInfo.cs with 84% similarity]
Tizen.Applications/Tizen.Applications.csproj [moved from Tizen.Application/Tizen.Application.csproj with 74% similarity]
Tizen.Applications/Tizen.Applications.csproj.user [moved from Tizen.Application/Tizen.Application.csproj.user with 100% similarity]
Tizen.Applications/Tizen.Applications.snk [moved from Tizen.Application/Tizen.Application.snk with 100% similarity]
Tizen.Applications/Tizen.Applications/Actor.cs [new file with mode: 0755]
Tizen.Applications/Tizen.Applications/AppControl.cs [moved from Tizen.Application/Tizen.Application/AppControl.cs with 97% similarity]
Tizen.Applications/Tizen.Applications/AppControlFilter.cs [moved from Tizen.Application/Tizen.Application/AppControlFilter.cs with 98% similarity]
Tizen.Applications/Tizen.Applications/Application.cs [new file with mode: 0755]
Tizen.Applications/Tizen.Applications/CodeExample.cs [moved from Tizen.Application/Tizen.Application/CodeExample.cs with 71% similarity]
Tizen.Applications/Tizen.Applications/Context.cs [new file with mode: 0755]
Tizen.Applications/Tizen.Applications/ContextGroup.cs [new file with mode: 0755]
Tizen.Applications/Tizen.Applications/Service.cs [new file with mode: 0755]
Tizen.Applications/Tizen.Applications/TizenSynchronizationContext.cs [moved from Tizen.Application/Tizen.Application/TizenSynchronizationContext.cs with 98% similarity]
Tizen.Applications/Tizen.Applications/Window.cs [moved from Tizen.Application/Tizen.Application/Window.cs with 62% similarity]
packaging/csapi-application.spec

index cb2ee73..a7f4b55 100755 (executable)
--- a/AppFW.sln
+++ b/AppFW.sln
@@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
 # Visual Studio 14
 VisualStudioVersion = 14.0.24720.0
 MinimumVisualStudioVersion = 10.0.40219.1
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tizen.Application", "Tizen.Application\Tizen.Application.csproj", "{663C5A3D-E631-4987-AEE7-F498C56A40FC}"
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tizen.Applications", "Tizen.Applications\Tizen.Applications.csproj", "{663C5A3D-E631-4987-AEE7-F498C56A40FC}"
 EndProject
 Global
        GlobalSection(SolutionConfigurationPlatforms) = preSolution
diff --git a/Tizen.Application/Tizen.Application/Actor.cs b/Tizen.Application/Tizen.Application/Actor.cs
deleted file mode 100755 (executable)
index c4d4548..0000000
+++ /dev/null
@@ -1,102 +0,0 @@
-using System;
-
-namespace Tizen.Application
-{
-    public abstract class Actor
-    {
-        private enum State
-        {
-            None,
-            Created,
-            Started,
-            Resumed,
-            Pasued
-        }
-
-        private ApplicationContext _context;
-        private State _state = State.None;
-        private AppControl _appcontrol;
-
-        protected Page MainPage { get; set; }
-        protected AppControl ReceivedAppControl { get { return _appcontrol; } }
-        protected ApplicationContext Context { get { return _context; } }
-
-        protected virtual void OnCreate() { }
-        protected virtual void OnStart() { }
-        protected virtual void OnPause() { }
-        protected virtual void OnResume() { }
-        protected virtual void OnTerminate() { }
-
-        internal void Create(ApplicationContext context)
-        {
-            if (_state != State.Created)
-            {
-                _context = context;
-                OnCreate();
-                _state = State.Created;
-            }
-        }
-
-        internal void Start(AppControl appControl)
-        {
-            if (_state != State.Started)
-            {
-                _appcontrol = appControl;
-                OnStart();
-                _state = State.Started;
-            }
-        }
-
-        internal void Pause()
-        {
-            if (_state != State.Pasued)
-            {
-                OnPause();
-                _state = State.Pasued;
-            }
-        }
-
-        internal void Resume()
-        {
-            if (_state != State.Resumed)
-            {
-                OnResume();
-                _state = State.Resumed;
-            }
-        }
-
-        internal void Terminate()
-        {
-            if (_state != State.None)
-            {
-                OnTerminate();
-                _state = State.None;
-            }
-        }
-
-        protected void StartActor(Type actorType, AppControl appControl)
-        {
-            _context.StartActor(actorType, appControl);
-
-        }
-
-        protected void StartActor(Actor actor, AppControl appControl)
-        {
-            _context.StartActor(actor, appControl);
-        }
-
-        protected void StartService(Type serviceType, AppControl appControl)
-        {
-            Application.StartService(serviceType, appControl);
-        }
-        protected void StopService(Type serviceType)
-        {
-            Application.StopService(serviceType);
-        }
-
-        public void Finish()
-        {
-            throw new NotImplementedException();
-        }
-    }
-}
diff --git a/Tizen.Application/Tizen.Application/Application.cs b/Tizen.Application/Tizen.Application/Application.cs
deleted file mode 100755 (executable)
index 55d779a..0000000
+++ /dev/null
@@ -1,178 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-using System.Runtime.InteropServices;
-
-namespace Tizen.Application
-{
-    public static class Application
-    {
-        private static Dictionary<AppControlFilter, Type> _filterMap = new Dictionary<AppControlFilter, Type>();
-        private static List<ApplicationContext> _contextList = new List<ApplicationContext>();
-        private static Window _window = null;
-        private static ServiceManager _serviceManager = new ServiceManager();
-
-        public static event EventHandler ApplicationInit;
-        public static event EventHandler ApplicationExit;
-
-        private static ApplicationContext CurrentContext { get { return _contextList.LastOrDefault(null); } }
-
-        public static int Run(string[] args)
-        {
-            Interop.Application.UIAppLifecycleCallbacks ops;
-            ops.OnCreate = (userData) =>
-            {
-                ApplicationInit(null, null);
-                return true;
-            };
-            ops.OnPause = (userData) =>
-            {
-                if (CurrentContext != null)
-                {
-                    CurrentContext.Pause();
-                }
-            };
-            ops.OnResume = (userData) =>
-            {
-                if (CurrentContext != null)
-                {
-                    CurrentContext.Resume();
-                }
-            };
-            ops.OnAppControl = (appControlHandle, userData) =>
-            {
-                AppControl appControl = new AppControl(appControlHandle);
-                if (appControl.isService)
-                {
-                    _serviceManager.OnAppControl(appControl);
-                } else {
-                    foreach (var item in _filterMap)
-                    {
-                        if (item.Key.IsMatch(appControl))
-                        {
-                            // Window was created when the first UI Actor was created
-                            if (_window == null)
-                                _window = new Window();
-                            if (CurrentContext == null || !appControl.IsLaunchOperation())
-                            {
-                                ApplicationContext ctx = new ApplicationContext();
-                                ctx.ReleaseContext += Ctx_ReleaseContext;
-                                _contextList.Add(ctx);
-                                Actor actor = ctx.StartActor(item.Value, appControl);
-                                if (!_window.Visible)
-                                {
-                                    _window.Active();
-                                    _window.Show();
-                                    ctx.Resume();
-                                }
-                            }
-                            break;
-                        }
-                    }
-                }
-            };
-            ops.OnTerminate = (userData) =>
-            {
-                Exit();
-            };
-
-            TizenSynchronizationContext.Initialize();
-
-            int ret = Interop.Application.UIAppMain(args.Length, args, ref ops, IntPtr.Zero);
-
-            return ret;
-        }
-
-        public static void Hide()
-        {
-            if (_window != null)
-                _window.InActive();
-        }
-
-        public static void Exit()
-        {
-            ApplicationExit(null, null);
-            throw new NotImplementedException();
-        }
-
-        private static void Ctx_ReleaseContext(object sender, EventArgs e)
-        {
-            var ctx = sender as ApplicationContext;
-            if (ctx != null)
-            {
-                _contextList.Remove(ctx);
-                if (_contextList.Count > 0)
-                {
-                    Hide();
-                } else
-                {
-                    // TODO. If running service was existed, application should not terminated, but Send the message to AUL(UI App terminate)
-                    Exit();
-                }
-            }
-        }
-
-        public static void AddActor(Type clazz)
-        {
-            AddActor(clazz, new AppControlFilter[0] { });
-        }
-
-        public static void AddActor(Type clazz, AppControlFilter filter)
-        {
-            AddActor(clazz, new AppControlFilter[] { filter });
-        }
-
-        public static void AddActor(Type clazz, AppControlFilter[] filters)
-        {
-            if (!clazz.IsSubclassOf(typeof(Actor)))
-                throw new ArgumentException(clazz.FullName + " is not a subclass of Actor.");
-
-            foreach (var prop in clazz.GetProperties())
-            {
-                foreach (var attr in prop.GetCustomAttributes(false))
-                {
-                    var filter = attr as AppControlFilter;
-                    if (filter != null)
-                    {
-                        _filterMap.Add(filter, clazz);
-                    }
-                }
-            }
-            if (filters != null)
-            {
-                foreach (var filter in filters)
-                {
-                    _filterMap.Add(filter, clazz);
-                }
-            }
-        }
-
-        public static void AddServiceHandler(Type clazz)
-        {
-            AddServiceHandler(clazz, new AppControlFilter[0] { });
-        }
-
-        public static void AddServiceHandler(Type clazz, AppControlFilter filter)
-        {
-            AddServiceHandler(clazz, new AppControlFilter[] { filter });
-        }
-
-        public static void AddServiceHandler(Type clazz, AppControlFilter[] filters)
-        {
-            _serviceManager.AddServiceHandler(clazz, filters);
-        }
-
-        internal static void StartService(Type clazz, AppControl appControl)
-        {
-            _serviceManager.StartService(clazz, appControl);
-        }
-        internal static void StopService(Type clazz)
-        {
-            _serviceManager.StopService(clazz);
-        }
-    }
-
-
-}
diff --git a/Tizen.Application/Tizen.Application/ApplicationContext.cs b/Tizen.Application/Tizen.Application/ApplicationContext.cs
deleted file mode 100755 (executable)
index 7165707..0000000
+++ /dev/null
@@ -1,83 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace Tizen.Application
-{
-    public class ApplicationContext
-    {
-        private List<Actor> _actorList;
-
-        internal event EventHandler ReleaseContext;
-
-        internal Actor TopActor { get { return _actorList.LastOrDefault(null); } }
-
-        internal ApplicationContext()
-        {
-            _actorList = new List<Actor>();
-        }
-
-        internal Actor StartActor(Type actorType, AppControl appControl)
-        {
-            Actor newActor = (Actor)Activator.CreateInstance(actorType);
-            _actorList.Add(newActor);
-            newActor.Create(this);
-            newActor.Start(appControl);
-            return newActor;
-        }
-
-        internal Actor StartActor(Actor actor, AppControl appControl)
-        {
-            Actor found = _actorList.Find(s => s == actor);
-            if (found == null)
-            {
-                throw new ArgumentException("Could not found the actor in current context.");
-            }
-            _actorList.Remove(found);
-            _actorList.Add(found);
-            found.Start(appControl);
-            return found;
-        }
-
-        internal void FinishActor(Actor actor)
-        {
-            Actor found = _actorList.Find(s => s == actor);
-            if (found == null)
-            {
-                throw new ArgumentException("Could not found the actor in current context.");
-            }
-
-            found.Pause();
-            found.Terminate();
-            _actorList.Remove(found);
-
-            if (IsEmpty())
-            {
-                ReleaseContext(this, null);
-            }
-        }
-
-        internal void Pause()
-        {
-            if (TopActor != null)
-            {
-                TopActor.Pause();
-            }
-        }
-
-        internal void Resume()
-        {
-            if (TopActor != null)
-            {
-                TopActor.Resume();
-            }
-        }
-
-        internal bool IsEmpty()
-        {
-            return _actorList.Count == 0;
-        }
-    }
-}
diff --git a/Tizen.Application/Tizen.Application/Page.cs b/Tizen.Application/Tizen.Application/Page.cs
deleted file mode 100755 (executable)
index afbed07..0000000
+++ /dev/null
@@ -1,12 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace Tizen.Application
-{
-    public class Page
-    {
-    }
-}
diff --git a/Tizen.Application/Tizen.Application/Service.cs b/Tizen.Application/Tizen.Application/Service.cs
deleted file mode 100755 (executable)
index 3ae207d..0000000
+++ /dev/null
@@ -1,27 +0,0 @@
-namespace Tizen.Application {
-    public class Service {
-        public Service() { }
-
-        public void Terminate()
-        {
-            Application.StopService(GetType());
-        }
-
-        protected virtual void OnCreate() { }
-        protected virtual void OnTerminate() { }
-        protected virtual void OnAppControl(AppControl appControl) { }
-
-        internal void DidCreate()
-        {
-            OnCreate();
-        }
-        internal void DidTerminate()
-        {
-            OnTerminate();
-        }
-        internal void DidAppControl(AppControl appControl)
-        {
-            OnAppControl(appControl);
-        }
-    }
-}
diff --git a/Tizen.Application/Tizen.Application/ServiceManager.cs b/Tizen.Application/Tizen.Application/ServiceManager.cs
deleted file mode 100755 (executable)
index 7e14327..0000000
+++ /dev/null
@@ -1,87 +0,0 @@
-using System;
-using System.Collections.Generic;
-
-namespace Tizen.Application
-{
-    public class ServiceManager
-    {
-        private Dictionary<AppControlFilter, Type> _filterMap = new Dictionary<AppControlFilter, Type>();
-        private Dictionary<Type, Service> _runningService = new Dictionary<Type, Service>();
-
-        public ServiceManager() { }
-        internal void AddServiceHandler(Type clazz, AppControlFilter[] filters)
-        {
-            if (!clazz.IsSubclassOf(typeof(Service)))
-                throw new ArgumentException(clazz.FullName + " is not a subclass of Service.");
-
-            foreach (var prop in clazz.GetProperties())
-            {
-                foreach (var attr in prop.GetCustomAttributes(false))
-                {
-                    var filter = attr as AppControlFilter;
-                    if (filter != null)
-                    {
-                        _filterMap.Add(filter, clazz);
-                    }
-                }
-            }
-            if (filters != null)
-            {
-                foreach (var filter in filters)
-                {
-                    _filterMap.Add(filter, clazz);
-                }
-            }
-        }
-        internal void OnAppControl(AppControl appControl)
-        {
-            foreach (var item in _filterMap)
-            {
-                if (item.Key.IsMatch(appControl))
-                {
-                    StartService(item.Value, appControl);
-                    break;
-                }
-            }
-        }
-
-        internal void StartService(Type clazz, AppControl appControl)
-        {
-            lock (_runningService)
-            {
-                if (_runningService.ContainsKey(clazz))
-                {
-                    _runningService[clazz].DidAppControl(appControl);
-                } else
-                {
-                    _runningService[clazz] = (Service)Activator.CreateInstance(clazz);
-                    _runningService[clazz].DidCreate();
-                    _runningService[clazz].DidAppControl(appControl);
-                }
-            }
-        }
-
-        internal void StopService(Type clazz)
-        {
-            lock (_runningService)
-            {
-                if (_runningService.ContainsKey(clazz))
-                {
-                    _runningService[clazz].DidTerminate();
-                    _runningService.Remove(clazz);
-                }
-            }
-        }
-
-        internal bool ServiceAlived
-        {
-            get
-            {
-                lock (_runningService)
-                {
-                    return _runningService.Count > 0;
-                }
-            }
-        }
-    }
-}
similarity index 84%
rename from Tizen.Application/Properties/AssemblyInfo.cs
rename to Tizen.Applications/Properties/AssemblyInfo.cs
index 2279397..fe35548 100755 (executable)
@@ -2,11 +2,11 @@
 using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
 
-[assembly: AssemblyTitle("Tizen.Application")]
+[assembly: AssemblyTitle("Tizen.Applications")]
 [assembly: AssemblyDescription("")]
 [assembly: AssemblyConfiguration("")]
 [assembly: AssemblyCompany("Samsung Electronics")]
-[assembly: AssemblyProduct("Tizen.Application")]
+[assembly: AssemblyProduct("Tizen.Applications")]
 [assembly: AssemblyCopyright("Copyright (c) 2015 Samsung Electronics Co., Ltd")]
 [assembly: AssemblyTrademark("")]
 [assembly: AssemblyCulture("")]
similarity index 74%
rename from Tizen.Application/Tizen.Application.csproj
rename to Tizen.Applications/Tizen.Applications.csproj
index 5fef059..325cbe8 100755 (executable)
@@ -7,8 +7,8 @@
     <ProjectGuid>{663C5A3D-E631-4987-AEE7-F498C56A40FC}</ProjectGuid>
     <OutputType>Library</OutputType>
     <AppDesignerFolder>Properties</AppDesignerFolder>
-    <RootNamespace>Tizen.Application</RootNamespace>
-    <AssemblyName>Tizen.Application</AssemblyName>
+    <RootNamespace></RootNamespace>
+    <AssemblyName>Tizen.Applications</AssemblyName>
     <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
     <FileAlignment>512</FileAlignment>
   </PropertyGroup>
@@ -33,7 +33,7 @@
     <SignAssembly>true</SignAssembly>
   </PropertyGroup>
   <PropertyGroup>
-    <AssemblyOriginatorKeyFile>Tizen.Application.snk</AssemblyOriginatorKeyFile>
+    <AssemblyOriginatorKeyFile>Tizen.Applications.snk</AssemblyOriginatorKeyFile>
   </PropertyGroup>
   <ItemGroup>
     <Reference Include="System" />
     <Compile Include="Interop\Interop.Libraries.cs" />
     <Compile Include="Interop\Interop.Window.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
-    <Compile Include="Tizen.Application\Actor.cs" />
-    <Compile Include="Tizen.Application\AppControl.cs" />
-    <Compile Include="Tizen.Application\AppControlFilter.cs" />
-    <Compile Include="Tizen.Application\Application.cs" />
-    <Compile Include="Tizen.Application\ApplicationContext.cs" />
-    <Compile Include="Tizen.Application\CodeExample.cs" />
-    <Compile Include="Tizen.Application\Page.cs" />
-    <Compile Include="Tizen.Application\Service.cs" />
-    <Compile Include="Tizen.Application\ServiceManager.cs" />
-    <Compile Include="Tizen.Application\Window.cs" />
-    <Compile Include="Tizen.Application\TizenSynchronizationContext.cs" />
+    <Compile Include="Tizen.Applications\Actor.cs" />
+    <Compile Include="Tizen.Applications\AppControl.cs" />
+    <Compile Include="Tizen.Applications\AppControlFilter.cs" />
+    <Compile Include="Tizen.Applications\Application.cs" />
+    <Compile Include="Tizen.Applications\CodeExample.cs" />
+    <Compile Include="Tizen.Applications\Context.cs" />
+    <Compile Include="Tizen.Applications\ContextGroup.cs" />
+    <Compile Include="Tizen.Applications\Service.cs" />
+    <Compile Include="Tizen.Applications\Window.cs" />
+    <Compile Include="Tizen.Applications\TizenSynchronizationContext.cs" />
   </ItemGroup>
   <ItemGroup>
-    <None Include="Tizen.Application.snk" />
+    <None Include="Tizen.Applications.snk" />
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
-  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
+  <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
        Other similar extension points exist, see Microsoft.Common.targets.
   <Target Name="BeforeBuild">
   </Target>
diff --git a/Tizen.Applications/Tizen.Applications/Actor.cs b/Tizen.Applications/Tizen.Applications/Actor.cs
new file mode 100755 (executable)
index 0000000..4052cef
--- /dev/null
@@ -0,0 +1,43 @@
+using System;
+
+namespace Tizen.Applications
+{
+    public abstract class Actor : Context
+    {
+        protected virtual void OnPaused() { }
+        protected virtual void OnResumed() { }
+
+        internal void Pause()
+        {
+            OnPaused();
+        }
+
+        internal void Resume()
+        {
+            OnResumed();
+        }
+
+        protected void StartActor(Actor actor, AppControl control)
+        {
+            Actor target = (Actor)CurrentGroup.MoveToTop(actor);
+            if (target != null)
+            {
+                Pause();
+                target.Start(control);
+                target.Resume();
+            }
+        }
+
+        protected override void StartActor(Type actorType, AppControl control)
+        {
+            Application.StartActor(CurrentGroup, actorType, control);
+        }
+
+        protected override void Finish()
+        {
+            Application.StopActor(CurrentGroup, this);
+        }
+
+    }
+}
+
@@ -2,7 +2,7 @@ using System;
 using System.Collections.Generic;
 using System.Runtime.InteropServices;
 
-namespace Tizen.Application
+namespace Tizen.Applications
 {
     public class AppControl
     {
@@ -53,9 +53,9 @@ namespace Tizen.Application
             Interop.AppControl.GetUri(handle, out _uri);
         }
 
-        internal bool isService
+        internal bool IsService
         {
-            get {return false;}
+            get { return false; }
         }
 
         public AppControl(string operation, string mime, string uri)
@@ -1,7 +1,7 @@
 using System;
 using System.Text.RegularExpressions;
 
-namespace Tizen.Application
+namespace Tizen.Applications
 {
     [AttributeUsage(AttributeTargets.Class)]
     public class AppControlFilter : Attribute
diff --git a/Tizen.Applications/Tizen.Applications/Application.cs b/Tizen.Applications/Tizen.Applications/Application.cs
new file mode 100755 (executable)
index 0000000..39dfca6
--- /dev/null
@@ -0,0 +1,270 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Runtime.InteropServices;
+
+namespace Tizen.Applications
+{
+    public static class Application
+    {
+        private static Dictionary<AppControlFilter, Type> s_filterMap = new Dictionary<AppControlFilter, Type>();
+        private static ContextGroup s_serviceGroup = new ContextGroup();
+        private static List<ContextGroup> s_actorGroupList = new List<ContextGroup>();
+
+        private static Window s_window = null;
+
+        private static ContextGroup CurrentActorGroup
+        {
+            get
+            {
+                return s_actorGroupList.LastOrDefault(null);
+            }
+        }
+
+        public static event EventHandler Created = delegate { };
+        public static event EventHandler Exited = delegate { };
+
+        public static int Run(string[] args)
+        {
+            Interop.Application.UIAppLifecycleCallbacks ops;
+            ops.OnCreate = (userData) =>
+            {
+                Created(null, EventArgs.Empty);
+                return true;
+            };
+            ops.OnPause = (userData) =>
+            {
+                if (CurrentActorGroup != null && CurrentActorGroup.TopContext != null)
+                {
+                    Actor actor = CurrentActorGroup.TopContext as Actor;
+                    if (actor != null)
+                    {
+                        actor.Pause();
+                    }
+                }
+            };
+            ops.OnResume = (userData) =>
+            {
+                if (CurrentActorGroup != null && CurrentActorGroup.TopContext != null)
+                {
+                    Actor actor = CurrentActorGroup.TopContext as Actor;
+                    if (actor != null)
+                    {
+                        actor.Resume();
+                    }
+                }
+            };
+            ops.OnAppControl = (appControlHandle, userData) =>
+            {
+                AppControl appControl = new AppControl(appControlHandle);
+                if (appControl.IsService)
+                {
+                    foreach (var item in s_filterMap)
+                    {
+                        if (item.Key.IsMatch(appControl) && item.Value.IsSubclassOf(typeof(Service)))
+                        {
+                            StartService(item.Value, appControl);
+                            break;
+                        }
+                    }
+                }
+                else
+                {
+                    foreach (var item in s_filterMap)
+                    {
+                        if (item.Key.IsMatch(appControl) && item.Value.IsSubclassOf(typeof(Actor)))
+                        {
+                            // Window was created when the first UI Actor was created
+                            if (s_window == null)
+                            {
+                                s_window = new Window();
+                            }
+
+                            if (CurrentActorGroup == null || !appControl.IsLaunchOperation())
+                            {
+                                StartActor(null, item.Value, appControl);
+                            }
+                            break;
+                        }
+                    }
+                }
+            };
+            ops.OnTerminate = (userData) =>
+            {
+                Exit();
+            };
+
+            TizenSynchronizationContext.Initialize();
+
+            int ret = Interop.Application.UIAppMain(args.Length, args, ref ops, IntPtr.Zero);
+
+            return ret;
+        }
+
+        public static void Hide()
+        {
+            if (s_window != null)
+                s_window.InActive();
+        }
+
+        public static void Exit()
+        {
+            Exited(null, null);
+            throw new NotImplementedException();
+        }
+
+        public static void RegisterActor(Type clazz)
+        {
+            RegisterActor(clazz, new AppControlFilter[0] { });
+        }
+
+        public static void RegisterActor(Type clazz, AppControlFilter filter)
+        {
+            RegisterActor(clazz, new AppControlFilter[] { filter });
+        }
+
+        public static void RegisterActor(Type clazz, AppControlFilter[] filters)
+        {
+            if (!clazz.IsSubclassOf(typeof(Actor)))
+                throw new ArgumentException(clazz.FullName + " is not a subclass of Actor.");
+
+            RegisterContext(clazz, filters);
+        }
+
+        public static void RegisterService(Type clazz)
+        {
+            RegisterService(clazz, new AppControlFilter[0] { });
+        }
+
+        public static void RegisterService(Type clazz, AppControlFilter filter)
+        {
+            RegisterService(clazz, new AppControlFilter[] { filter });
+        }
+
+        public static void RegisterService(Type clazz, AppControlFilter[] filters)
+        {
+            if (!clazz.IsSubclassOf(typeof(Service)))
+                throw new ArgumentException(clazz.FullName + " is not a subclass of Service.");
+
+            RegisterContext(clazz, filters);
+        }
+
+        private static void RegisterContext(Type clazz, AppControlFilter[] filters)
+        {
+            foreach (var prop in clazz.GetProperties())
+            {
+                foreach (var attr in prop.GetCustomAttributes(false))
+                {
+                    var filter = attr as AppControlFilter;
+                    if (filter != null)
+                    {
+                        s_filterMap.Add(filter, clazz);
+                    }
+                }
+            }
+            if (filters != null)
+            {
+                foreach (var filter in filters)
+                {
+                    s_filterMap.Add(filter, clazz);
+                }
+            }
+        }
+
+        internal static void StartActor(ContextGroup group, Type actorType, AppControl control)
+        {
+            if (!actorType.IsSubclassOf(typeof(Actor)))
+            {
+                throw new ArgumentException(actorType.FullName + " is not a subclass of Actor.");
+            }
+
+            Actor actor = (Actor)Activator.CreateInstance(actorType);
+            ContextGroup ctxGroup = group;
+            if (ctxGroup == null)
+            {
+                ctxGroup = new ContextGroup();
+                s_actorGroupList.Add(ctxGroup);
+            }
+            ctxGroup.AddContext(actor);
+            actor.Create(ctxGroup);
+            actor.Start(control);
+
+            // TODO: consider resume operation
+            if (!s_window.Visible)
+            {
+                s_window.Active();
+                s_window.Show();
+                actor.Resume();
+            }
+        }
+
+        internal static void StopActor(ContextGroup group, Actor actor)
+        {
+            actor.Pause();
+            actor.Terminate();
+            group.RemoveContext(actor);
+            if (group.IsEmpty)
+            {
+                s_actorGroupList.Remove(group);
+                if (s_actorGroupList.Count == 0 && s_serviceGroup.IsEmpty)
+                {
+                    Exit();
+                }
+                else
+                {
+                    Hide();
+                }
+            }
+            else
+            {
+                Actor nextActor = group.TopContext as Actor;
+                if (nextActor != null)
+                {
+                    nextActor.Resume();
+                }
+            }
+        }
+
+        internal static void StartService(Type serviceType, AppControl control)
+        {
+            if (!serviceType.IsSubclassOf(typeof(Service)))
+            {
+                throw new ArgumentException(serviceType.FullName + " is not a subclass of Service.");
+            }
+
+            Context ctx = s_serviceGroup.FindContext(serviceType);
+            if (ctx == null)
+            {
+                // Register ContextRemoved Handler once
+                ctx = (Service)Activator.CreateInstance(serviceType);
+                s_serviceGroup.AddContext(ctx);
+                ctx.Create(s_serviceGroup);
+            }
+            ctx.Start(control);
+        }
+
+        internal static void StopService(Type serviceType)
+        {
+            if (!serviceType.IsSubclassOf(typeof(Service)))
+            {
+                throw new ArgumentException(serviceType.FullName + " is not a subclass of Service.");
+            }
+
+            Context ctx = s_serviceGroup.FindContext(serviceType);
+            if (ctx != null)
+            {
+                ctx.Terminate();
+                s_serviceGroup.RemoveContext(ctx);
+                if (s_serviceGroup.IsEmpty)
+                {
+                    if (s_actorGroupList.Count == 0)
+                    {
+                        Exit();
+                    }
+                }
+            }
+        }
+    }
+}
@@ -5,17 +5,17 @@ using System.Reflection;
 using System.Text;
 using System.Threading.Tasks;
 
-namespace Tizen.Application
+namespace Tizen.Applications
 {
     public class ViewActor : Actor
     {
         public string Type { get; set; }
-        protected override void OnCreate()
+        protected override void OnCreated()
         {
             Console.WriteLine();
         }
 
-        protected override void OnStart()
+        protected override void OnStarted()
         {
         }
 
@@ -27,11 +27,11 @@ namespace Tizen.Application
     [AppControlFilter("http://tizen.org/appcontrol/operation/default")]
     public class DefaultActor : Actor
     {
-        protected override void OnCreate()
+        protected override void OnCreated()
         {
         }
 
-        protected override void OnStart()
+        protected override void OnStarted()
         {
         }
     }
@@ -40,10 +40,10 @@ namespace Tizen.Application
     {
         static void test(string[] args) // main
         {
-            Application.ApplicationInit += Application_Create;
-            Application.ApplicationExit += Application_Terminate;
-            Application.AddActor(typeof(DefaultActor));
-            Application.AddActor(typeof(ViewActor), new AppControlFilter[] {
+            Application.Created += Application_Create;
+            Application.Exited += Application_Terminate;
+            Application.RegisterActor(typeof(DefaultActor));
+            Application.RegisterActor(typeof(ViewActor), new AppControlFilter[] {
                 new AppControlFilter("http://tizen.org/appcontrol/view", "image/*"),
                 new AppControlFilter("http://tizen.org/appcontrol/view", "text/*")
             });
diff --git a/Tizen.Applications/Tizen.Applications/Context.cs b/Tizen.Applications/Tizen.Applications/Context.cs
new file mode 100755 (executable)
index 0000000..9183239
--- /dev/null
@@ -0,0 +1,70 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Tizen.Applications
+{
+    public abstract class Context
+    {
+        private ContextGroup _group;
+        private AppControl _control;
+
+        internal ContextGroup CurrentGroup
+        {
+            get
+            {
+                return _group;
+            }
+        }
+
+        protected AppControl ReceivedAppControl
+        {
+            get
+            {
+                return _control;
+            }
+        }
+
+        protected virtual void OnCreated() { }
+        protected virtual void OnStarted() { }
+        protected virtual void OnTerminated() { }
+
+        internal void Create(ContextGroup group)
+        {
+            _group = group;
+            OnCreated();
+        }
+
+        internal void Start(AppControl control)
+        {
+            _control = control;
+            OnStarted();
+        }
+
+        internal void Terminate()
+        {
+            OnTerminated();
+        }
+
+        protected abstract void StartActor(Type actorType, AppControl control);
+
+        protected void StartService(Type serviceType, AppControl control)
+        {
+            Application.StartService(serviceType, control);
+        }
+
+        protected void StopService(Type serviceType)
+        {
+            Application.StopService(serviceType);
+        }
+
+        protected abstract void Finish();
+
+        protected void SendAppControl(AppControl control, string destination)
+        {
+            throw new NotImplementedException();
+        }
+    }
+}
diff --git a/Tizen.Applications/Tizen.Applications/ContextGroup.cs b/Tizen.Applications/Tizen.Applications/ContextGroup.cs
new file mode 100755 (executable)
index 0000000..afe0a59
--- /dev/null
@@ -0,0 +1,65 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Tizen.Applications
+{
+    internal class ContextGroup
+    {
+        private List<Context> _contextList;
+
+        public bool IsEmpty
+        {
+            get
+            {
+                return _contextList.Count == 0;
+            }
+        }
+
+        public Context TopContext
+        {
+            get
+            {
+                return _contextList.LastOrDefault(null);
+            }
+        }
+
+        public ContextGroup()
+        {
+            _contextList = new List<Context>();
+        }
+
+        public void AddContext(Context ctx)
+        {
+            _contextList.Add(ctx);
+        }
+
+        public void RemoveContext(Context ctx)
+        {
+            _contextList.Remove(ctx);
+        }
+
+        public Context FindContext(Type type)
+        {
+            return _contextList.Find(s => s.GetType() == type);
+        }
+
+        public Context FindContext(Context ctx)
+        {
+            return _contextList.Find(s => s == ctx);
+        }
+
+        public Context MoveToTop(Context ctx)
+        {
+            var found = FindContext(ctx);
+            if (found != null)
+            {
+                _contextList.Remove(found);
+                _contextList.Add(found);
+            }
+            return found;
+        }
+    }
+}
diff --git a/Tizen.Applications/Tizen.Applications/Service.cs b/Tizen.Applications/Tizen.Applications/Service.cs
new file mode 100755 (executable)
index 0000000..07f926c
--- /dev/null
@@ -0,0 +1,17 @@
+using System;
+
+namespace Tizen.Applications
+{
+    public abstract class Service : Context
+    {
+        protected override void StartActor(Type actorType, AppControl control)
+        {
+            Application.StartActor(null, actorType, control);
+        }
+
+        protected override void Finish()
+        {
+            Application.StopService(GetType());
+        }
+    }
+}
@@ -2,7 +2,7 @@ using System;
 using System.Collections.Generic;
 using System.Threading;
 
-namespace Tizen.Application
+namespace Tizen.Applications
 {
     internal class TizenSynchronizationContext : SynchronizationContext
     {
similarity index 62%
rename from Tizen.Application/Tizen.Application/Window.cs
rename to Tizen.Applications/Tizen.Applications/Window.cs
index 4743fa8..a2a61b6 100755 (executable)
@@ -1,35 +1,47 @@
 using System;
 
-namespace Tizen.Application {
-    class Window : IDisposable {
+namespace Tizen.Applications
+{
+    class Window : IDisposable
+    {
         private IntPtr _native_window = IntPtr.Zero;
 
-        public bool Visible {
-            get {
+        public bool Visible
+        {
+            get
+            {
                 return _native_window != IntPtr.Zero ? Interop.Window.evas_object_visible_get(_native_window) : false;
             }
         }
-        public Window() {
+        public Window()
+        {
             _native_window = Interop.Window.elm_win_add(IntPtr.Zero, "Window", 0);
         }
-        ~Window() {
+        ~Window()
+        {
             Dispose();
         }
-        public void Show() {
+        public void Show()
+        {
             Interop.Window.evas_object_show(_native_window);
         }
-        public void Hide() {
+        public void Hide()
+        {
             Interop.Window.evas_object_hide(_native_window);
         }
-        public void Active() {
+        public void Active()
+        {
             Interop.Window.elm_win_activate(_native_window);
         }
-        public void InActive() {
+        public void InActive()
+        {
             Interop.Window.elm_win_lower(_native_window);
         }
 
-        public void Dispose() {
-            if (_native_window != IntPtr.Zero) {
+        public void Dispose()
+        {
+            if (_native_window != IntPtr.Zero)
+            {
                 Interop.Window.evas_object_unref(_native_window);
                 _native_window = IntPtr.Zero;
             }
index 6a29fbf..c6acbc9 100644 (file)
@@ -1,5 +1,7 @@
+%define _use_internal_dependency_generator 0
+
 %define dllpath %{_libdir}/mono/tizen
-%define dllname Tizen.Application.dll
+%define dllname Tizen.Applications.dll
 
 Name:       csapi-application
 Summary:    Tizen Application API for C#
@@ -51,10 +53,10 @@ cp %{SOURCE1} .
 
 %build
 # build dll
-mcs -target:library -out:%{dllname} -keyfile:Tizen.Application/Tizen.Application.snk \
-  Tizen.Application/Properties/AssemblyInfo.cs \
-  Tizen.Application/Tizen.Application/*.cs \
-  Tizen.Application/Interop/*.cs
+mcs -target:library -out:%{dllname} -keyfile:Tizen.Applications/Tizen.Applications.snk \
+  Tizen.Applications/Properties/AssemblyInfo.cs \
+  Tizen.Applications/Tizen.Applications/*.cs \
+  Tizen.Applications/Interop/*.cs
 
 # check p/invoke
 if [ -x %{dllname} ]; then