Release 4.0.0-preview1-00051
[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     public class ReceivedAppControl : AppControl
47     {
48         private const string LogTag = "Tizen.Applications";
49
50         /// <summary>
51         /// Initializes a ReceivedAppControl class.
52         /// </summary>
53         public ReceivedAppControl(SafeAppControlHandle handle) : base(handle)
54         {
55         }
56
57         /// <summary>
58         /// Gets the application ID of the caller from the launch request.
59         /// </summary>
60         /// <value>
61         /// The application ID of the caller
62         /// </value>
63         /// <example>
64         /// <code>
65         ///     protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
66         ///     {
67         ///         ReceivedAppControl control = e.ReceivedAppControl;
68         ///         string caller = control.CallerApplicationId;
69         ///     }
70         /// </code>
71         /// </example>
72         public string CallerApplicationId
73         {
74             get
75             {
76                 string value = String.Empty;
77                 Interop.AppControl.ErrorCode err = Interop.AppControl.GetCaller(SafeAppControlHandle, out value);
78                 if (err != Interop.AppControl.ErrorCode.None)
79                 {
80                     Log.Warn(LogTag, "Failed to get the caller application id from the AppControl. Err = " + err);
81                 }
82                 return value;
83             }
84         }
85
86         /// <summary>
87         /// Checks whether the caller is requesting a reply from the launch request.
88         /// </summary>
89         /// <value>
90         /// If true this ReceivedAppControl is requested by the caller, otherwise false
91         /// </value>
92         /// <example>
93         /// <code>
94         ///     protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
95         ///     {
96         ///         ReceivedAppControl control = e.ReceivedAppControl;
97         ///         bool isReply = control.IsReplyRequest;
98         ///     }
99         /// </code>
100         /// </example>
101         public bool IsReplyRequest
102         {
103             get
104             {
105                 bool value = false;
106                 Interop.AppControl.ErrorCode err = Interop.AppControl.IsReplyRequested(SafeAppControlHandle, out value);
107                 if (err != Interop.AppControl.ErrorCode.None)
108                 {
109                     Log.Warn(LogTag, "Failed to check the reply  of the AppControl is requested. Err = " + err);
110                 }
111                 return value;
112             }
113         }
114
115         /// <summary>
116         /// Replies to the launch request sent by the caller.
117         /// If the caller application sent the launch request to receive the result, the callee application can return the result back to the caller.
118         /// </summary>
119         /// <param name="replyRequest">The AppControl in which the results of the callee are contained</param>
120         /// <param name="result">The result code of the launch request</param>
121         /// <example>
122         /// <code>
123         ///     protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
124         ///     {
125         ///         ReceivedAppControl control = e.ReceivedAppControl;
126         ///         if (control.IsReplyRequest)
127         ///         {
128         ///             AppControl replyRequest = new AppControl();
129         ///             replyRequest.ExtraData.Add("myKey", "I'm replying");
130         ///             control.ReplyToLaunchRequest(replyRequest, AppControlReplyResult.Succeeded);
131         ///         }
132         ///     }
133         /// </code>
134         /// </example>
135         public void ReplyToLaunchRequest(AppControl replyRequest, AppControlReplyResult result)
136         {
137             if (replyRequest == null)
138             {
139                 throw new ArgumentNullException("replyRequest");
140             }
141             Interop.AppControl.ErrorCode err = Interop.AppControl.ReplyToLaunchRequest(replyRequest.SafeAppControlHandle, this.SafeAppControlHandle, (int)result);
142             if (err != Interop.AppControl.ErrorCode.None)
143                 throw new InvalidOperationException("Failed to reply. Err = " + err);
144         }
145     }
146 }