[Applications.Common] Add a new property of AppControl (#1002)
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.Common / Tizen.Applications / AppControl.cs
index e591205..76e0bf3 100755 (executable)
@@ -18,6 +18,7 @@ using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Runtime.InteropServices;
+using System.Threading.Tasks;
 
 namespace Tizen.Applications
 {
@@ -40,12 +41,14 @@ namespace Tizen.Applications
     /// }
     /// </code>
     /// </example>
+    /// <since_tizen> 3 </since_tizen>
     public class AppControl
     {
         private const string LogTag = "Tizen.Applications";
 
-        private static Dictionary<int, Interop.AppControl.ReplyCallback> s_replyNativeCallbackMaps = new Dictionary<int, Interop.AppControl.ReplyCallback>();
-        private static int s_replyNativeCallbackId = 0;
+        private static Dictionary<int, Interop.AppControl.ResultCallback> s_resultNativeCallbackMaps = new Dictionary<int, Interop.AppControl.ResultCallback>();
+        private static Dictionary<int, AppControlReplyCallback> s_replyCallbackMaps = new Dictionary<int, AppControlReplyCallback>();
+        private static int s_reaustId = 0;
 
         private readonly SafeAppControlHandle _handle;
 
@@ -55,11 +58,13 @@ namespace Tizen.Applications
         private string _category = null;
         private string _applicationId = null;
         private ExtraDataCollection _extraData = null;
+        private string _componentId = null;
 
         /// <summary>
         /// Initializes the instance of the AppControl class.
         /// </summary>
         /// <exception cref="InvalidOperationException">Thrown when failed to create the AppControl handle.</exception>
+        /// <since_tizen> 3 </since_tizen>
         public AppControl()
         {
             Interop.AppControl.ErrorCode err = Interop.AppControl.Create(out _handle);
@@ -74,6 +79,7 @@ namespace Tizen.Applications
         /// </summary>
         /// <param name="enableAppStartedResultEvent">The flag value to receive an additional launch result event on the launch request.</param>
         /// <exception cref="InvalidOperationException">Thrown when failed to create the AppControl handle.</exception>
+        /// <since_tizen> 3 </since_tizen>
         public AppControl(bool enableAppStartedResultEvent)
         {
             Interop.AppControl.ErrorCode err = Interop.AppControl.Create(out _handle);
@@ -96,6 +102,7 @@ namespace Tizen.Applications
         /// Initializes the instance of the AppControl class with the SafeAppControlHandle.
         /// </summary>
         /// <param name="handle"></param>
+        /// <since_tizen> 3 </since_tizen>
         public AppControl(SafeAppControlHandle handle)
         {
             if (handle == null)
@@ -119,11 +126,31 @@ namespace Tizen.Applications
             }
         }
 
-        #region Public Properties
+        private static Interop.AppControl.ReplyCallback s_replyNativeCallback = (launchHandle, replyHandle, result, userData) =>
+        {
+            int requestId = (int)userData;
+            lock (s_replyCallbackMaps)
+            {
+                if (s_replyCallbackMaps.ContainsKey(requestId))
+                {
+                    s_replyCallbackMaps[requestId](new AppControl(launchHandle), new AppControl(replyHandle), (AppControlReplyResult)result);
+                    if (result != Interop.AppControl.AppStartedStatus)
+                    {
+                        lock (s_replyCallbackMaps)
+                        {
+                            s_replyCallbackMaps.Remove(requestId);
+                        }
+                    }
+                }
+            }
+        };
+
+#region Public Properties
 
         /// <summary>
         /// Gets the SafeAppControlHandle instance.
         /// </summary>
+        /// <since_tizen> 3 </since_tizen>
         public SafeAppControlHandle SafeAppControlHandle
         {
             get
@@ -148,6 +175,7 @@ namespace Tizen.Applications
         /// Log.Debug(LogTag, "Operation: " + appControl.Operation);
         /// </code>
         /// </example>
+        /// <since_tizen> 3 </since_tizen>
         public string Operation
         {
             get
@@ -189,6 +217,7 @@ namespace Tizen.Applications
         /// Log.Debug(LogTag, "Mime: " + appControl.Mime);
         /// </code>
         /// </example>
+        /// <since_tizen> 3 </since_tizen>
         public string Mime
         {
             get
@@ -245,6 +274,7 @@ namespace Tizen.Applications
         /// }
         /// </code>
         /// </example>
+        /// <since_tizen> 3 </since_tizen>
         public string Uri
         {
             get
@@ -279,6 +309,7 @@ namespace Tizen.Applications
         /// <value>
         /// (if the category is null for setter, it clears the previous value.)
         /// </value>
+        /// <since_tizen> 3 </since_tizen>
         public string Category
         {
             get
@@ -320,6 +351,7 @@ namespace Tizen.Applications
         /// Log.Debug(LogTag, "ApplicationId: " + appControl.ApplicationId);
         /// </code>
         /// </example>
+        /// <since_tizen> 3 </since_tizen>
         public string ApplicationId
         {
             get
@@ -366,6 +398,7 @@ namespace Tizen.Applications
         /// appControl.LaunchMode = AppControlLaunchMode.Group;
         /// </code>
         /// </example>
+        /// <since_tizen> 3 </since_tizen>
         public AppControlLaunchMode LaunchMode
         {
             get
@@ -401,6 +434,7 @@ namespace Tizen.Applications
         /// ...
         /// </code>
         /// </example>
+        /// <since_tizen> 3 </since_tizen>
         public ExtraDataCollection ExtraData
         {
             get
@@ -411,7 +445,53 @@ namespace Tizen.Applications
             }
         }
 
-        #endregion // Public Properties
+        /// <summary>
+        /// Gets and sets the component ID to explicitly launch a component.
+        /// </summary>
+        /// <value>
+        /// (if the component ID is null for setter, it clears the previous value.)
+        /// From Tizen 5.5, a new application model is supported that is component-based application.
+        /// This property is for launching component-based application. If it's not set, the main component of component-based application will be launched.
+        /// If the target app is not component-based application, setting property is meaningless.
+        /// </value>
+        /// <example>
+        /// <code>
+        /// AppControl appControl = new AppControl();
+        /// appControl.ApplicationId = "org.tizen.component-based-app"; // component-based application
+        /// appControl.ComponentId = "org.tizen.frame-component";
+        /// AppControl.SendLaunchRequest(appControl);
+        /// </code>
+        /// </example>
+        /// <since_tizen> 6 </since_tizen>
+        public string ComponentId
+        {
+            get
+            {
+                if (String.IsNullOrEmpty(_componentId))
+                {
+                    Interop.AppControl.ErrorCode err = Interop.AppControl.GetComponentId(_handle, out _componentId);
+                    if (err != Interop.AppControl.ErrorCode.None)
+                    {
+                        Log.Warn(LogTag, "Failed to get the component id from the AppControl. Err = " + err);
+                    }
+                }
+                return _componentId;
+            }
+            set
+            {
+                Interop.AppControl.ErrorCode err = Interop.AppControl.SetComponentId(_handle, value);
+                if (err == Interop.AppControl.ErrorCode.None)
+                {
+                    _componentId = value;
+                }
+                else
+                {
+                    Log.Warn(LogTag, "Failed to set the component id to the AppControl. Err = " + err);
+                }
+            }
+        }
+
+#endregion // Public Properties
 
         /// <summary>
         /// Retrieves all applications that can be launched to handle the given app_control request.
@@ -431,6 +511,7 @@ namespace Tizen.Applications
         /// }
         /// </code>
         /// </example>
+        /// <since_tizen> 3 </since_tizen>
         public static IEnumerable<string> GetMatchedApplicationIds(AppControl control)
         {
             if (control == null)
@@ -465,7 +546,7 @@ namespace Tizen.Applications
         /// <remarks>
         /// The operation is mandatory information for the launch request.
         /// If the operation is not specified, AppControlOperations.Default is used by default.
-        /// If the operation is AppControlOperations.Default, the application ID is mandatory to explicitly launch the application. \n
+        /// If the operation is AppControlOperations.Default, the application ID is mandatory to explicitly launch the application.<br/>
         /// Since Tizen 2.4, the launch request of the service application over out of packages is restricted by the platform.
         /// Also, implicit launch requests are NOT delivered to service applications since 2.4.
         /// To launch a service application, an explicit launch request with the application ID given by property ApplicationId MUST be sent.
@@ -482,6 +563,7 @@ namespace Tizen.Applications
         /// AppControl.SendLaunchRequest(appControl);
         /// </code>
         /// </example>
+        /// <since_tizen> 3 </since_tizen>
         public static void SendLaunchRequest(AppControl launchRequest)
         {
             SendLaunchRequest(launchRequest, null);
@@ -493,7 +575,7 @@ namespace Tizen.Applications
         /// <remarks>
         /// The operation is mandatory information for the launch request.
         /// If the operation is not specified, AppControlOperations.Default is used by default.
-        /// If the operation is AppControlOperations.Default, the application ID is mandatory to explicitly launch the application. \n
+        /// If the operation is AppControlOperations.Default, the application ID is mandatory to explicitly launch the application.<br/>
         /// Since Tizen 2.4, the launch request of the service application over out of packages is restricted by the platform.
         /// Also, implicit launch requests are NOT delivered to service applications since 2.4.
         /// To launch a service application, an explicit launch request with the application ID given by property ApplicationId MUST be sent.
@@ -517,6 +599,7 @@ namespace Tizen.Applications
         /// });
         /// </code>
         /// </example>
+        /// <since_tizen> 3 </since_tizen>
         public static void SendLaunchRequest(AppControl launchRequest, AppControlReplyCallback replyAfterLaunching)
         {
             if (launchRequest == null)
@@ -529,26 +612,12 @@ namespace Tizen.Applications
             if (replyAfterLaunching != null)
             {
                 int id = 0;
-                lock (s_replyNativeCallbackMaps)
+                lock (s_replyCallbackMaps)
                 {
-                    id = s_replyNativeCallbackId++;
-                    s_replyNativeCallbackMaps[id] = (launchRequestHandle, replyRequestHandle, result, userData) =>
-                    {
-                        if (replyAfterLaunching != null)
-                        {
-                            Log.Debug(LogTag, "Reply Callback is launched");
-                            replyAfterLaunching(new AppControl(launchRequestHandle), new AppControl(replyRequestHandle), (AppControlReplyResult)result);
-                            if (result != Interop.AppControl.AppStartedStatus)
-                            {
-                                lock (s_replyNativeCallbackMaps)
-                                {
-                                    s_replyNativeCallbackMaps.Remove(id);
-                                }
-                            }
-                        }
-                    };
+                    id = s_reaustId++;
+                    s_replyCallbackMaps[id] = replyAfterLaunching;
                 }
-                err = Interop.AppControl.SendLaunchRequest(launchRequest._handle, s_replyNativeCallbackMaps[id], IntPtr.Zero);
+                err = Interop.AppControl.SendLaunchRequest(launchRequest._handle, s_replyNativeCallback, (IntPtr)id);
             }
             else
             {
@@ -566,7 +635,7 @@ namespace Tizen.Applications
                     case Interop.AppControl.ErrorCode.OutOfMemory:
                         throw new Exceptions.OutOfMemoryException("Out-of-memory");
                     case Interop.AppControl.ErrorCode.AppNotFound:
-                        throw new Exceptions.AppNotFoundException("App not found");
+                        throw new Exceptions.AppNotFoundException("App(" + launchRequest.ApplicationId + ") not found. Operation(" + launchRequest.Operation + ")");
                     case Interop.AppControl.ErrorCode.LaunchRejected:
                         throw new Exceptions.LaunchRejectedException("Launch rejected");
                     case Interop.AppControl.ErrorCode.LaunchFailed:
@@ -600,6 +669,7 @@ namespace Tizen.Applications
         /// AppControl.SendTerminateRequest(terminateRequest);
         /// </code>
         /// </example>
+        /// <since_tizen> 3 </since_tizen>
         public static void SendTerminateRequest(AppControl terminateRequest)
         {
             if (terminateRequest == null)
@@ -625,8 +695,86 @@ namespace Tizen.Applications
         }
 
         /// <summary>
+        /// Sends the launch request asynchronously.
+        /// </summary>
+        /// <remarks>
+        /// The operation is mandatory information for the launch request.
+        /// If the operation is not specified, AppControlOperations.Default is used by default.
+        /// If the operation is AppControlOperations.Default, the application ID is mandatory to explicitly launch the application.<br/>
+        /// Since Tizen 2.4, the launch request of the service application over out of packages is restricted by the platform.
+        /// Also, implicit launch requests are NOT delivered to service applications since 2.4.
+        /// To launch a service application, an explicit launch request with the application ID given by property ApplicationId MUST be sent.
+        /// </remarks>
+        /// <param name="launchRequest">The AppControl.</param>
+        /// <param name="replyAfterLaunching">The callback function to be called when the reply is delivered.</param>
+        /// <returns>A task with the result of the launch request.</returns>
+        /// <exception cref="ArgumentException">Thrown when failed because of the argument is invalid.</exception>
+        /// <exception cref="Exceptions.AppNotFoundException">Thrown when the application to run is not found.</exception>
+        /// <exception cref="Exceptions.LaunchRejectedException">Thrown when the launch request is rejected.</exception>
+        /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
+        /// <since_tizen> 6 </since_tizen>
+        public static Task<AppControlResult> SendLaunchRequestAsync(AppControl launchRequest, AppControlReplyCallback replyAfterLaunching)
+        {
+            if (launchRequest == null)
+            {
+                throw new ArgumentNullException(nameof(launchRequest));
+            }
+
+            var task = new TaskCompletionSource<AppControlResult>();
+            Interop.AppControl.ErrorCode err;
+            int requestId = 0;
+
+            lock (s_resultNativeCallbackMaps)
+            {
+                requestId = s_reaustId++;
+                s_resultNativeCallbackMaps[requestId] = (handle, result, userData) =>
+                {
+                    task.SetResult((AppControlResult)result);
+                    lock (s_resultNativeCallbackMaps)
+                    {
+                        s_resultNativeCallbackMaps.Remove((int)userData);
+                    }
+                };
+            }
+
+            if (replyAfterLaunching != null)
+            {
+                lock (s_replyCallbackMaps)
+                {
+                    s_replyCallbackMaps[requestId] = replyAfterLaunching;
+                }
+                err = Interop.AppControl.SendLaunchRequestAsync(launchRequest.SafeAppControlHandle, s_resultNativeCallbackMaps[requestId], s_replyNativeCallback, (IntPtr)requestId);
+            }
+            else
+            {
+                err = Interop.AppControl.SendLaunchRequestAsync(launchRequest.SafeAppControlHandle, s_resultNativeCallbackMaps[requestId], null, (IntPtr)requestId);
+            }
+
+            if (err != Interop.AppControl.ErrorCode.None)
+            {
+                switch (err)
+                {
+                    case Interop.AppControl.ErrorCode.InvalidParameter:
+                        throw new ArgumentException("Invalid Arguments");
+                    case Interop.AppControl.ErrorCode.AppNotFound:
+                        throw new Exceptions.AppNotFoundException("App(" + launchRequest.ApplicationId + ") not found. Operation(" + launchRequest.Operation + ")");
+                    case Interop.AppControl.ErrorCode.LaunchRejected:
+                        throw new Exceptions.LaunchRejectedException("Launch rejected");
+                    case Interop.AppControl.ErrorCode.PermissionDenied:
+                        throw new Exceptions.PermissionDeniedException("Permission denied");
+
+                    default:
+                        throw new Exceptions.LaunchRejectedException("Launch rejected");
+                }
+            }
+
+            return task.Task;
+        }
+
+        /// <summary>
         /// Class for extra data.
         /// </summary>
+        /// <since_tizen> 3 </since_tizen>
         public class ExtraDataCollection
         {
             private readonly SafeAppControlHandle _handle;
@@ -652,6 +800,7 @@ namespace Tizen.Applications
             /// appControl.ExtraData.Add("myKey", "myValue");
             /// </code>
             /// </example>
+            /// <since_tizen> 3 </since_tizen>
             public void Add(string key, string value)
             {
                 if (string.IsNullOrEmpty(key))
@@ -694,6 +843,7 @@ namespace Tizen.Applications
             /// appControl.ExtraData.Add("myKey", myValues);
             /// </code>
             /// </example>
+            /// <since_tizen> 3 </since_tizen>
             public void Add(string key, IEnumerable<string> value)
             {
                 if (string.IsNullOrEmpty(key))
@@ -735,6 +885,7 @@ namespace Tizen.Applications
             /// string myValue = appControl.ExtraData.Get&lt;string&gt;("myKey");
             /// </code>
             /// </example>
+            /// <since_tizen> 3 </since_tizen>
             public T Get<T>(string key)
             {
                 object ret = Get(key);
@@ -759,6 +910,7 @@ namespace Tizen.Applications
             /// }
             /// </code>
             /// </example>
+            /// <since_tizen> 3 </since_tizen>
             public object Get(string key)
             {
                 if (IsCollection(key))
@@ -789,6 +941,7 @@ namespace Tizen.Applications
             /// }
             /// </code>
             /// </example>
+            /// <since_tizen> 3 </since_tizen>
             public IEnumerable<string> GetKeys()
             {
                 List<string> keys = new List<string>();
@@ -832,6 +985,7 @@ namespace Tizen.Applications
             /// }
             /// </code>
             /// </example>
+            /// <since_tizen> 3 </since_tizen>
             public bool TryGet(string key, out string value)
             {
                 if (string.IsNullOrEmpty(key))
@@ -873,6 +1027,7 @@ namespace Tizen.Applications
             /// }
             /// </code>
             /// </example>
+            /// <since_tizen> 3 </since_tizen>
             public bool TryGet(string key, out IEnumerable<string> value)
             {
                 if (string.IsNullOrEmpty(key))
@@ -915,6 +1070,7 @@ namespace Tizen.Applications
             /// appControl.ExtraData.Remove("myKey");
             /// </code>
             /// </example>
+            /// <since_tizen> 3 </since_tizen>
             public void Remove(string key)
             {
                 if (string.IsNullOrEmpty(key))
@@ -949,6 +1105,7 @@ namespace Tizen.Applications
             /// int numberOfKeys = appControl.ExtraData.Count();
             /// </code>
             /// </example>
+            /// <since_tizen> 3 </since_tizen>
             public int Count()
             {
                 return GetKeys().Count();
@@ -967,6 +1124,7 @@ namespace Tizen.Applications
             /// bool result = appControl.ExtraData.IsCollection("myKey");
             /// </code>
             /// </example>
+            /// <since_tizen> 3 </since_tizen>
             public bool IsCollection(string key)
             {
                 if (string.IsNullOrEmpty(key))