2 * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Collections.Generic;
21 namespace Tizen.Applications.WatchfaceComplication
24 /// Represents the ComplicationProvider class for the complication provider service application.
26 /// <since_tizen> 6 </since_tizen>
27 public abstract class ComplicationProvider : IDisposable
29 private string _providerId;
30 private bool _disposed = false;
31 private const string LogTag = "WatchfaceComplication";
34 /// Initializes the ComplicationProvider class.
36 /// <param name="providerId">The id of the complication provider.</param>
37 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
38 /// <exception cref="ArgumentException">Thrown when providerId is invalid.</exception>
39 /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
40 /// <exception cref="NotSupportedException">Thrown when the watchface complication is not supported.</exception>
41 /// <exception cref="UnauthorizedAccessException">Thrown when the application does not have privilege to access this method.</exception>
44 /// public class MyComplicationProvider : ComplicationProvider
46 /// public MyComplicationProvider(string providerId)
47 /// : base(providerId)
50 /// protected override void OnDataUpdateRequested(string reqestAppId, ComplicationTypes type, Bundle contextData)
56 /// <since_tizen> 6 </since_tizen>
57 protected ComplicationProvider(string providerId)
59 ComplicationError err = Interop.WatchfaceComplication.AddUpdateRequestedCallback(providerId, DataUpdateRequested, IntPtr.Zero);
60 if (err != ComplicationError.None)
61 ErrorFactory.ThrowException(err, "fail to create provider");
62 _providerId = providerId;
66 /// Destructor of the provider class.
68 ~ComplicationProvider()
74 /// Gets the provider ID.
76 /// <since_tizen> 6 </since_tizen>
85 private void DataUpdateRequested(string providerId, string reqAppId, ComplicationTypes type,
86 IntPtr context, IntPtr sharedData, IntPtr userData)
88 Bundle bContext = new Bundle(new SafeBundleHandle(context, false));
89 ComplicationData data = OnDataUpdateRequested(reqAppId, type, bContext);
92 Log.Error(LogTag, "null complication data");
95 ComplicationError err = data.UpdateSharedData(sharedData);
96 if (err != ComplicationError.None)
97 Log.Error(LogTag, "Set complication data error : " + err);
101 /// Overrides this method to handle the behavior when the complication data update request event comes from watchface complication.
103 /// <param name="reqestAppId">The application ID of application which sent update request.</param>
104 /// <param name="type">The requested type.</param>
105 /// <param name="contextData">The complication's context which is set by complication setup application.</param>
106 /// <returns>The requested ComplicationData</returns>
107 /// <since_tizen> 6 </since_tizen>
108 protected abstract ComplicationData OnDataUpdateRequested(string reqestAppId, ComplicationTypes type, Bundle contextData);
111 /// Emits the update event for complications.
113 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
114 /// <exception cref="UnauthorizedAccessException">Thrown when the application does not have privilege to access this method.</exception>
115 /// <exception cref="NotSupportedException">Thrown when the watchface complication is not supported.</exception>
116 /// <since_tizen> 6 </since_tizen>
117 public void NotifyUpdate()
119 ComplicationError err = Interop.WatchfaceComplication.NotifyUpdate(_providerId);
120 if (err != ComplicationError.None)
121 ErrorFactory.ThrowException(err, "fail to notify");
125 /// Gets the received event type.
127 /// <param name="recvAppCtrl">The appcontrol received event args.</param>
128 /// <exception cref="ArgumentException">Thrown when the invalid parameter is passed.</exception>
129 /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
130 /// <exception cref="NotSupportedException">Thrown when the watchface complication is not supported.</exception>
133 /// protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
135 /// EventTypes type = ComplicationProvider.GetEventType(e.ReceivedAppControl);
136 /// if (type == EventTypes.EventDoubleTap)
140 /// base.OnAppControlReceived(e);
144 /// <returns>The type of received event</returns>
145 /// <since_tizen> 6 </since_tizen>
146 public static EventTypes GetEventType(ReceivedAppControl recvAppCtrl)
149 ComplicationError err = Interop.WatchfaceComplication.GetEventType(recvAppCtrl.SafeAppControlHandle, out type);
150 if (err != ComplicationError.None)
151 ErrorFactory.ThrowException(err, "fail to get event");
156 /// Gets the received event target provider ID.
158 /// <param name="recvAppCtrl">The appcontrol received event args.</param>
159 /// <exception cref="ArgumentException">Thrown when e is invalid.</exception>
160 /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
161 /// <exception cref="NotSupportedException">Thrown when the watchface complication is not supported.</exception>
164 /// protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
166 /// string providerId = ComplicationProvider.GetEventProviderId(e.ReceivedAppControl);
167 /// base.OnAppControlReceived(e);
171 /// <returns>The target provider ID of received event</returns>
172 /// <since_tizen> 6 </since_tizen>
173 public static string GetEventProviderId(ReceivedAppControl recvAppCtrl)
175 string providerId = string.Empty;
176 ComplicationError err = Interop.WatchfaceComplication.GetEventProviderId(recvAppCtrl.SafeAppControlHandle, out providerId);
177 if (err != ComplicationError.None && err != ComplicationError.NoData)
178 ErrorFactory.ThrowException(err, "fail to get event");
183 /// Gets the received event target complication type.
185 /// <param name="recvAppCtrl">The appcontrol received event args.</param>
186 /// <exception cref="ArgumentException">Thrown when e is invalid.</exception>
187 /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
188 /// <exception cref="NotSupportedException">Thrown when the watchface complication is not supported.</exception>
191 /// protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
193 /// ComplicationTypes type = ComplicationProvider.GetEventComplicationType(e.ReceivedAppControl);
194 /// base.OnAppControlReceived(e);
198 /// <returns>The target complication type of received event</returns>
199 /// <since_tizen> 6 </since_tizen>
200 public static ComplicationTypes GetEventComplicationType(ReceivedAppControl recvAppCtrl)
202 ComplicationTypes type;
203 ComplicationError err = Interop.WatchfaceComplication.GetEventComplicationType(recvAppCtrl.SafeAppControlHandle, out type);
204 if (err != ComplicationError.None && err != ComplicationError.NoData)
205 ErrorFactory.ThrowException(err, "fail to get complication type");
210 /// Gets the received event target complication context.
212 /// <param name="recvAppCtrl">The appcontrol received event args.</param>
213 /// <exception cref="ArgumentException">Thrown when e is invalid.</exception>
214 /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
215 /// <exception cref="NotSupportedException">Thrown when the watchface complication is not supported.</exception>
218 /// protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
220 /// Bundle context = ComplicationProvider.GetEventContext(e.ReceivedAppControl);
221 /// base.OnAppControlReceived(e);
225 /// <returns>The context of received event</returns>
226 /// <since_tizen> 6 </since_tizen>
227 public static Bundle GetEventContext(ReceivedAppControl recvAppCtrl)
229 SafeBundleHandle bHandle;
230 ComplicationError err = Interop.WatchfaceComplication.GetEventContext(recvAppCtrl.SafeAppControlHandle, out bHandle);
231 if (err != ComplicationError.None)
233 if (err == ComplicationError.NoData)
235 ErrorFactory.ThrowException(err, "fail to get complication context");
238 return new Bundle(bHandle);
242 /// Releases the unmanaged resources used by the ComplicationProvider class specifying whether to perform a normal dispose operation.
244 /// <param name="disposing">true for a normal dispose operation; false to finalize the handle.</param>
245 /// <since_tizen> 3 </since_tizen>
246 protected virtual void Dispose(bool disposing)
250 Interop.WatchfaceComplication.RemoveUpdateRequestedCallback(_providerId, DataUpdateRequested);
256 /// Releases all resources used by the ComplicationProvider class.
258 /// <since_tizen> 3 </since_tizen>
259 public void Dispose()
262 GC.SuppressFinalize(this);