e1378114e3efdbda0ad09562ad3b089fb1288696
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.Common / Tizen.Applications / ReceivedAppControl.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18
19 namespace Tizen.Applications
20 {
21     /// <summary>
22     /// Represents the received AppControl.
23     /// </summary>
24     /// <example>
25     /// <code>
26     /// public class ReceivedAppControlExample : UIApplication
27     /// {
28     ///     // ...
29     ///     protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
30     ///     {
31     ///         ReceivedAppControl control = e.ReceivedAppControl;
32     ///         if (control.Operation == AppControlOperations.Pick)
33     ///         {
34     ///             Log.Debug(LogTag, "Received AppControl is Pick");
35     ///         }
36     ///         if (control.IsReplyRequest)
37     ///         {
38     ///             AppControl replyRequest = new AppControl();
39     ///             replyRequest.ExtraData.Add("myKey", "I'm replying");
40     ///             control.ReplyToLaunchRequest(replyRequest, AppControlReplyResult.Succeeded);
41     ///         }
42     ///     }
43     /// }
44     /// </code>
45     /// </example>
46     /// <since_tizen> 3 </since_tizen>
47     public class ReceivedAppControl : AppControl
48     {
49         private const string LogTag = "Tizen.Applications";
50
51         /// <summary>
52         /// Initializes a ReceivedAppControl class.
53         /// </summary>
54         /// <param name="handle">App control handle</param>
55         /// <since_tizen> 3 </since_tizen>
56         public ReceivedAppControl(SafeAppControlHandle handle) : base(handle)
57         {
58         }
59
60         /// <summary>
61         /// Gets the application ID of the caller from the launch request.
62         /// </summary>
63         /// <value>
64         /// The application ID of the caller.
65         /// </value>
66         /// <example>
67         /// <code>
68         ///     protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
69         ///     {
70         ///         ReceivedAppControl control = e.ReceivedAppControl;
71         ///         string caller = control.CallerApplicationId;
72         ///     }
73         /// </code>
74         /// </example>
75         /// <since_tizen> 3 </since_tizen>
76         public string CallerApplicationId
77         {
78             get
79             {
80                 string value = String.Empty;
81                 Interop.AppControl.ErrorCode err = Interop.AppControl.GetCaller(SafeAppControlHandle, out value);
82                 if (err != Interop.AppControl.ErrorCode.None)
83                 {
84                     Log.Warn(LogTag, "Failed to get the caller application id from the AppControl. Err = " + err);
85                 }
86                 return value;
87             }
88         }
89
90         /// <summary>
91         /// Checks whether the caller is requesting a reply from the launch request.
92         /// </summary>
93         /// <value>
94         /// If true, this ReceivedAppControl is requested by the caller, otherwise false
95         /// </value>
96         /// <example>
97         /// <code>
98         ///     protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
99         ///     {
100         ///         ReceivedAppControl control = e.ReceivedAppControl;
101         ///         bool isReply = control.IsReplyRequest;
102         ///     }
103         /// </code>
104         /// </example>
105         /// <since_tizen> 3 </since_tizen>
106         public bool IsReplyRequest
107         {
108             get
109             {
110                 bool value = false;
111                 Interop.AppControl.ErrorCode err = Interop.AppControl.IsReplyRequested(SafeAppControlHandle, out value);
112                 if (err != Interop.AppControl.ErrorCode.None)
113                 {
114                     Log.Warn(LogTag, "Failed to check the reply  of the AppControl is requested. Err = " + err);
115                 }
116                 return value;
117             }
118         }
119
120         /// <summary>
121         /// Replies to the launch request sent by the caller.
122         /// If the caller application sends the launch request to receive the result, the callee application can return the result back to the caller.
123         /// </summary>
124         /// <param name="replyRequest">The AppControl in which the results of the callee are contained.</param>
125         /// <param name="result">The result code of the launch request.</param>
126         /// <example>
127         /// <code>
128         ///     protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
129         ///     {
130         ///         ReceivedAppControl control = e.ReceivedAppControl;
131         ///         if (control.IsReplyRequest)
132         ///         {
133         ///             AppControl replyRequest = new AppControl();
134         ///             replyRequest.ExtraData.Add("myKey", "I'm replying");
135         ///             control.ReplyToLaunchRequest(replyRequest, AppControlReplyResult.Succeeded);
136         ///         }
137         ///     }
138         /// </code>
139         /// </example>
140         /// <since_tizen> 3 </since_tizen>
141         public void ReplyToLaunchRequest(AppControl replyRequest, AppControlReplyResult result)
142         {
143             if (replyRequest == null)
144             {
145                 throw new ArgumentNullException("replyRequest");
146             }
147             Interop.AppControl.ErrorCode err = Interop.AppControl.ReplyToLaunchRequest(replyRequest.SafeAppControlHandle, this.SafeAppControlHandle, (int)result);
148             if (err != Interop.AppControl.ErrorCode.None)
149                 throw new InvalidOperationException("Failed to reply. Err = " + err);
150         }
151     }
152 }