Implement Lifecycles of Application
authorWonYoung Choi <wy80.choi@samsung.com>
Wed, 2 Mar 2016 11:21:57 +0000 (20:21 +0900)
committerWonYoung Choi <wy80.choi@samsung.com>
Fri, 4 Mar 2016 06:03:48 +0000 (15:03 +0900)
Change-Id: I43eec8c56b7e1123c7fa5fc38759ab171feb1631

Tizen.Application/Interop/Interop.AppControl.cs
Tizen.Application/Interop/Interop.Aul.cs
Tizen.Application/Tizen.Application/Actor.cs
Tizen.Application/Tizen.Application/AppControl.cs
Tizen.Application/Tizen.Application/Application.cs
Tizen.Application/Tizen.Application/ApplicationContext.cs

index 7995260..c5a6013 100755 (executable)
@@ -8,7 +8,6 @@
 
 using System;
 using System.Runtime.InteropServices;
-using System.Diagnostics;
 
 internal static partial class Interop
 {
index ff2d00c..260892a 100755 (executable)
@@ -6,7 +6,6 @@
 /// it only in accordance with the terms of the license agreement
 /// you entered into with Samsung.
 
-using System;
 using System.Text;
 using System.Runtime.InteropServices;
 
index a2c6b5d..da7a17e 100755 (executable)
@@ -6,7 +6,7 @@ namespace Tizen.Application
     {
         private enum State
         {
-            Unknown,
+            None,
             Created,
             Started,
             Resumed,
@@ -14,11 +14,12 @@ namespace Tizen.Application
         }
 
         private ApplicationContext _context;
-        private State _state = State.Unknown;
+        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() { }
@@ -64,6 +65,15 @@ namespace Tizen.Application
             }
         }
 
+        internal void Terminate()
+        {
+            if (_state != State.None)
+            {
+                OnTerminate();
+                _state = State.None;
+            }
+        }
+
         protected void StartActor(Type actorType, AppControl appControl)
         {
             _context.StartActor(actorType, appControl);
index 7e18891..741b73d 100755 (executable)
@@ -4,13 +4,11 @@ using System.Runtime.InteropServices;
 
 namespace Tizen.Application
 {
-    // TODO To be implemented
     public class AppControl
     {
         private string _operation;
         private string _mime;
         private string _uri;
-        private Dictionary<string, object> _data;
 
         public static class Operations
         {
index 35fc114..af1ec16 100755 (executable)
@@ -9,7 +9,6 @@ namespace Tizen.Application
 {
     public static class Application
     {
-        private static Dictionary<string, Type> _actorMap = new Dictionary<string, Type>();
         private static Dictionary<AppControlFilter, Type> _filterMap = new Dictionary<AppControlFilter, Type>();
         private static List<ApplicationContext> _contextList = new List<ApplicationContext>();
         private static Window _window = null;
@@ -17,14 +16,13 @@ namespace Tizen.Application
         public static event EventHandler ApplicationInit;
         public static event EventHandler ApplicationExit;
 
-        public static ApplicationContext CurrentContext { get { return _contextList.LastOrDefault(null); } }
+        private static ApplicationContext CurrentContext { get { return _contextList.LastOrDefault(null); } }
 
         public static int Run(string[] args)
         {
             Interop.Application.UIAppLifecycleCallbacks ops;
             ops.OnCreate = (userData) =>
             {
-                _contextList.Add(new ApplicationContext());
                 ApplicationInit(null, null);
                 if (_window == null)
                     _window = new Window();
@@ -51,24 +49,16 @@ namespace Tizen.Application
                 {
                     if (item.Key.IsMatch(appControl))
                     {
-                        // Relaunch?
-                        if (appControl.IsLaunchOperation() && CurrentContext != null && !CurrentContext.Empty())
-                        {
-                            // TODO: Resume should be called by windows event
-                            CurrentContext.Resume();
-                        }
-                        // Create new context and new actor when launching first or receiving appcontrol except launch.
-                        else
+                        if (CurrentContext == null || !appControl.IsLaunchOperation())
                         {
                             ApplicationContext ctx = new ApplicationContext();
+                            ctx.ReleaseContext += Ctx_ReleaseContext;
                             _contextList.Add(ctx);
                             Actor actor = ctx.StartActor(item.Value, appControl);
-
-                            // TODO: Resume should be called by windows event
                             if (!_window.Visible) {
                                 _window.Active();
                                 _window.Show();
-                                actor.Resume();
+                                ctx.Resume();
                             }
                         }
                         break;
@@ -77,8 +67,7 @@ namespace Tizen.Application
             };
             ops.OnTerminate = (userData) =>
             {
-                // TODO: Remove context and actors
-                ApplicationExit(null, null);
+                Exit();
             };
 
             TizenSynchronizationContext.Initialize();
@@ -88,7 +77,32 @@ namespace Tizen.Application
             return ret;
         }
 
+        public static void Hide()
+        {
+            throw new NotImplementedException();
+        }
+
+        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
+                {
+                    Exit();
+                }
+            }
+        }
 
         public static void AddActor(Type clazz)
         {
@@ -105,8 +119,6 @@ namespace Tizen.Application
             if (!clazz.IsSubclassOf(typeof(Actor)))
                 throw new ArgumentException(clazz.FullName + " is not a subclass of Actor.");
 
-            _actorMap[clazz.FullName] = clazz;
-
             foreach (var prop in clazz.GetProperties())
             {
                 foreach (var attr in prop.GetCustomAttributes(false))
index d9864e4..7165707 100755 (executable)
@@ -10,6 +10,8 @@ namespace Tizen.Application
     {
         private List<Actor> _actorList;
 
+        internal event EventHandler ReleaseContext;
+
         internal Actor TopActor { get { return _actorList.LastOrDefault(null); } }
 
         internal ApplicationContext()
@@ -39,6 +41,24 @@ namespace Tizen.Application
             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)
@@ -55,7 +75,7 @@ namespace Tizen.Application
             }
         }
 
-        internal bool Empty()
+        internal bool IsEmpty()
         {
             return _actorList.Count == 0;
         }