[SecureRepository] Add support of InvalidFormat error in ckmc api
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.Common / Tizen.Applications / ApplicationManager.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 using System.Collections.Generic;
19 using System.ComponentModel;
20 using System.Runtime.InteropServices;
21 using System.Threading.Tasks;
22
23 namespace Tizen.Applications
24 {
25     /// <summary>
26     /// This class has the methods and events of the ApplicationManager.
27     /// </summary>
28     /// <since_tizen> 3 </since_tizen>
29     public static class ApplicationManager
30     {
31         private const string LogTag = "Tizen.Applications";
32         private static EventHandler<ApplicationLaunchedEventArgs> s_launchedHandler;
33         private static EventHandler<ApplicationTerminatedEventArgs> s_terminatedHandler;
34         private static Interop.ApplicationManager.AppManagerAppContextEventCallback s_applicationChangedEventCallback;
35         private static EventHandler<ApplicationEnabledEventArgs> _enabledHandler;
36         private static EventHandler<ApplicationDisabledEventArgs> _disabledHandler;
37         private static Interop.ApplicationManager.AppManagerEventCallback _eventCallback;
38         private static IntPtr _eventHandle = IntPtr.Zero;
39
40         /// <summary>
41         /// Occurs whenever the installed application is enabled.
42         /// </summary>
43         /// <since_tizen> 3 </since_tizen>
44         public static event EventHandler<ApplicationEnabledEventArgs> ApplicationEnabled
45         {
46             add
47             {
48                 if (_enabledHandler == null && _disabledHandler == null)
49                 {
50                     RegisterApplicationEvent();
51                 }
52                 _enabledHandler += value;
53             }
54             remove
55             {
56                 _enabledHandler -= value;
57                 if (_enabledHandler == null && _disabledHandler == null)
58                 {
59                     UnRegisterApplicationEvent();
60                 }
61             }
62         }
63
64         /// <summary>
65         /// Occurs whenever the installed application is disabled.
66         /// </summary>
67         /// <since_tizen> 3 </since_tizen>
68         public static event EventHandler<ApplicationDisabledEventArgs> ApplicationDisabled
69         {
70             add
71             {
72                 if (_disabledHandler == null && _enabledHandler == null)
73                 {
74                     RegisterApplicationEvent();
75                 }
76                 _disabledHandler += value;
77             }
78             remove
79             {
80                 _disabledHandler -= value;
81                 if (_disabledHandler == null && _enabledHandler == null)
82                 {
83                     UnRegisterApplicationEvent();
84                 }
85             }
86         }
87
88         /// <summary>
89         /// Occurs whenever the installed applications get launched.
90         /// </summary>
91         /// <since_tizen> 3 </since_tizen>
92         public static event EventHandler<ApplicationLaunchedEventArgs> ApplicationLaunched
93         {
94             add
95             {
96                 if (s_launchedHandler == null && s_terminatedHandler == null)
97                 {
98                     RegisterApplicationChangedEvent();
99                 }
100                 s_launchedHandler += value;
101             }
102             remove
103             {
104                 s_launchedHandler -= value;
105                 if (s_launchedHandler == null && s_terminatedHandler == null)
106                 {
107                     UnRegisterApplicationChangedEvent();
108                 }
109             }
110         }
111
112         /// <summary>
113         /// Occurs whenever the installed applications get terminated.
114         /// </summary>
115         /// <since_tizen> 3 </since_tizen>
116         public static event EventHandler<ApplicationTerminatedEventArgs> ApplicationTerminated
117         {
118             add
119             {
120                 if (s_launchedHandler == null && s_terminatedHandler == null)
121                 {
122                     RegisterApplicationChangedEvent();
123                 }
124                 s_terminatedHandler += value;
125             }
126             remove
127             {
128                 s_terminatedHandler -= value;
129                 if (s_launchedHandler == null && s_terminatedHandler == null)
130                 {
131                     UnRegisterApplicationChangedEvent();
132                 }
133             }
134         }
135
136         /// <summary>
137         /// Gets the information of the installed applications asynchronously.
138         /// </summary>
139         /// <since_tizen> 3 </since_tizen>
140         public static async Task<IEnumerable<ApplicationInfo>> GetInstalledApplicationsAsync()
141         {
142             return await Task.Run(() =>
143             {
144                 Interop.ApplicationManager.ErrorCode err = Interop.ApplicationManager.ErrorCode.None;
145                 List<ApplicationInfo> result = new List<ApplicationInfo>();
146
147                 Interop.ApplicationManager.AppManagerAppInfoCallback cb = (IntPtr infoHandle, IntPtr userData) =>
148                 {
149                     if (infoHandle != IntPtr.Zero)
150                     {
151                         IntPtr clonedHandle = IntPtr.Zero;
152                         err = Interop.ApplicationManager.AppInfoClone(out clonedHandle, infoHandle);
153                         if (err != Interop.ApplicationManager.ErrorCode.None)
154                         {
155                             Log.Warn(LogTag, "Failed to clone the appinfo. err = " + err);
156                             return false;
157                         }
158                         ApplicationInfo app = new ApplicationInfo(clonedHandle);
159                         result.Add(app);
160                         return true;
161                     }
162                     return false;
163                 };
164                 err = Interop.ApplicationManager.AppManagerForeachAppInfo(cb, IntPtr.Zero);
165                 if (err != Interop.ApplicationManager.ErrorCode.None)
166                 {
167                     throw ApplicationManagerErrorFactory.GetException(err, "Failed to foreach the appinfo.");
168                 }
169                 return result;
170             });
171         }
172
173         /// <summary>
174         /// Gets the information of the installed applications with the ApplicationInfoFilter asynchronously.
175         /// </summary>
176         /// <param name="filter">Key-value pairs for filtering.</param>
177         /// <since_tizen> 3 </since_tizen>
178         public static async Task<IEnumerable<ApplicationInfo>> GetInstalledApplicationsAsync(ApplicationInfoFilter filter)
179         {
180             return await Task.Run(() =>
181             {
182                 List<ApplicationInfo> result = new List<ApplicationInfo>();
183
184                 Interop.ApplicationManager.AppInfoFilterCallback cb = (IntPtr infoHandle, IntPtr userData) =>
185                 {
186                     if (infoHandle != IntPtr.Zero)
187                     {
188                         IntPtr clonedHandle = IntPtr.Zero;
189                         Interop.ApplicationManager.ErrorCode err = Interop.ApplicationManager.AppInfoClone(out clonedHandle, infoHandle);
190                         if (err != Interop.ApplicationManager.ErrorCode.None)
191                         {
192                             Log.Warn(LogTag, "Failed to clone the appinfo. err = " + err);
193                             return false;
194                         }
195                         ApplicationInfo app = new ApplicationInfo(clonedHandle);
196                         result.Add(app);
197                         return true;
198                     }
199                     return false;
200                 };
201                 filter.Fetch(cb);
202                 return result;
203             });
204         }
205
206         /// <summary>
207         /// Gets the information of the installed applications with the ApplicationInfoMetadataFilter asynchronously.
208         /// </summary>
209         /// <param name="filter">Key-value pairs for filtering.</param>
210         /// <since_tizen> 3 </since_tizen>
211         public static async Task<IEnumerable<ApplicationInfo>> GetInstalledApplicationsAsync(ApplicationInfoMetadataFilter filter)
212         {
213             return await Task.Run(() =>
214             {
215                 List<ApplicationInfo> result = new List<ApplicationInfo>();
216
217                 Interop.ApplicationManager.AppInfoFilterCallback cb = (IntPtr infoHandle, IntPtr userData) =>
218                 {
219                     if (infoHandle != IntPtr.Zero)
220                     {
221                         IntPtr clonedHandle = IntPtr.Zero;
222                         Interop.ApplicationManager.ErrorCode err = Interop.ApplicationManager.AppInfoClone(out clonedHandle, infoHandle);
223                         if (err != Interop.ApplicationManager.ErrorCode.None)
224                         {
225                             Log.Warn(LogTag, "Failed to clone the appinfo. err = " + err);
226                             return false;
227                         }
228                         ApplicationInfo app = new ApplicationInfo(clonedHandle);
229                         result.Add(app);
230                         return true;
231                     }
232                     return false;
233                 };
234                 filter.Fetch(cb);
235                 return result;
236             });
237         }
238
239         /// <summary>
240         /// Gets the information of the running applications asynchronously.
241         /// </summary>
242         /// <since_tizen> 3 </since_tizen>
243         public static async Task<IEnumerable<ApplicationRunningContext>> GetRunningApplicationsAsync()
244         {
245             return await Task.Run(() =>
246             {
247                 Interop.ApplicationManager.ErrorCode err = Interop.ApplicationManager.ErrorCode.None;
248                 List<ApplicationRunningContext> result = new List<ApplicationRunningContext>();
249
250                 Interop.ApplicationManager.AppManagerAppContextCallback cb = (IntPtr contextHandle, IntPtr userData) =>
251                 {
252                     if (contextHandle != IntPtr.Zero)
253                     {
254                         IntPtr clonedHandle = IntPtr.Zero;
255                         err = Interop.ApplicationManager.AppContextClone(out clonedHandle, contextHandle);
256                         if (err != Interop.ApplicationManager.ErrorCode.None)
257                         {
258                             Log.Warn(LogTag, "Failed to clone the app context. err = " + err);
259                             return false;
260                         }
261                         ApplicationRunningContext context = new ApplicationRunningContext(clonedHandle);
262                         result.Add(context);
263                         return true;
264                     }
265                     return false;
266                 };
267
268                 err = Interop.ApplicationManager.AppManagerForeachAppContext(cb, IntPtr.Zero);
269                 if (err != Interop.ApplicationManager.ErrorCode.None)
270                 {
271                     throw ApplicationManagerErrorFactory.GetException(err, "Failed to foreach appcontext.");
272                 }
273                 return result;
274             });
275         }
276
277         /// <summary>
278         /// Gets the information of the running applications including subapp asynchronously.
279         /// </summary>
280         /// <since_tizen> 3 </since_tizen>
281         public static async Task<IEnumerable<ApplicationRunningContext>> GetAllRunningApplicationsAsync()
282         {
283             return await Task.Run(() =>
284             {
285                 Interop.ApplicationManager.ErrorCode err = Interop.ApplicationManager.ErrorCode.None;
286                 List<ApplicationRunningContext> result = new List<ApplicationRunningContext>();
287
288                 Interop.ApplicationManager.AppManagerAppContextCallback cb = (IntPtr contextHandle, IntPtr userData) =>
289                 {
290                     if (contextHandle != IntPtr.Zero)
291                     {
292                         IntPtr clonedHandle = IntPtr.Zero;
293                         err = Interop.ApplicationManager.AppContextClone(out clonedHandle, contextHandle);
294                         if (err != Interop.ApplicationManager.ErrorCode.None)
295                         {
296                             Log.Warn(LogTag, "Failed to clone the app context. err = " + err);
297                             return false;
298                         }
299                         ApplicationRunningContext context = new ApplicationRunningContext(clonedHandle);
300                         result.Add(context);
301                         return true;
302                     }
303                     return false;
304                 };
305
306                 err = Interop.ApplicationManager.AppManagerForeachRunningAppContext(cb, IntPtr.Zero);
307                 if (err != Interop.ApplicationManager.ErrorCode.None)
308                 {
309                     throw ApplicationManagerErrorFactory.GetException(err, "Failed to foreach appcontext.");
310                 }
311                 return result;
312             });
313         }
314
315         /// <summary>
316         /// Gets the information of the specified application with the application ID.
317         /// </summary>
318         /// <param name="applicationId">Application ID.</param>
319         /// <since_tizen> 3 </since_tizen>
320         public static ApplicationInfo GetInstalledApplication(string applicationId)
321         {
322             IntPtr infoHandle = IntPtr.Zero;
323             Interop.ApplicationManager.ErrorCode err = Interop.ApplicationManager.AppManagerGetAppInfo(applicationId, out infoHandle);
324             if (err != Interop.ApplicationManager.ErrorCode.None)
325             {
326                 throw ApplicationManagerErrorFactory.GetException(err, "Failed to get the installed application information of " + applicationId + ".");
327             }
328             ApplicationInfo app = new ApplicationInfo(infoHandle);
329             return app;
330         }
331
332         /// <summary>
333         /// Returns if the specified application is running or not.
334         /// </summary>
335         /// <param name="applicationId">The application ID.</param>
336         /// <returns>Returns true if the given application is running, otherwise false.</returns>
337         /// <exception cref="ArgumentException">Thrown when the given parameter is invalid.</exception>
338         /// <since_tizen> 3 </since_tizen>
339         public static bool IsRunning(string applicationId)
340         {
341             bool isRunning = false;
342             Interop.ApplicationManager.ErrorCode err = Interop.ApplicationManager.AppManagerIsRunning(applicationId, out isRunning);
343             if (err != Interop.ApplicationManager.ErrorCode.None)
344             {
345                 throw ApplicationManagerErrorFactory.GetException(Interop.ApplicationManager.ErrorCode.InvalidParameter, "Invalid parameter");
346             }
347             return isRunning;
348         }
349
350         private static void RegisterApplicationChangedEvent()
351         {
352             Interop.ApplicationManager.ErrorCode err = Interop.ApplicationManager.ErrorCode.None;
353             s_applicationChangedEventCallback = (IntPtr contextHandle, Interop.ApplicationManager.AppContextEvent state, IntPtr userData) =>
354             {
355                 if (contextHandle == IntPtr.Zero) return;
356
357                 IntPtr clonedHandle = IntPtr.Zero;
358                 err = Interop.ApplicationManager.AppContextClone(out clonedHandle, contextHandle);
359                 if (err != Interop.ApplicationManager.ErrorCode.None)
360                 {
361                     throw ApplicationManagerErrorFactory.GetException(err, "Failed to register the application context event.");
362                 }
363                 using (ApplicationRunningContext context = new ApplicationRunningContext(clonedHandle))
364                 {
365                     if (state == Interop.ApplicationManager.AppContextEvent.Launched)
366                     {
367                         s_launchedHandler?.Invoke(null, new ApplicationLaunchedEventArgs { ApplicationRunningContext = context });
368                     }
369                     else if (state == Interop.ApplicationManager.AppContextEvent.Terminated)
370                     {
371                         s_terminatedHandler?.Invoke(null, new ApplicationTerminatedEventArgs { ApplicationRunningContext = context });
372                     }
373                 }
374             };
375             err = Interop.ApplicationManager.AppManagerSetAppContextEvent(s_applicationChangedEventCallback, IntPtr.Zero);
376             if (err != Interop.ApplicationManager.ErrorCode.None)
377             {
378                 throw ApplicationManagerErrorFactory.GetException(err, "Failed to register the application context event.");
379             }
380         }
381
382         private static void UnRegisterApplicationChangedEvent()
383         {
384             Interop.ApplicationManager.AppManagerUnSetAppContextEvent();
385         }
386
387         private static void RegisterApplicationEvent()
388         {
389             Interop.ApplicationManager.ErrorCode err = Interop.ApplicationManager.ErrorCode.None;
390             err = Interop.ApplicationManager.AppManagerEventCreate(out _eventHandle);
391             if (err != Interop.ApplicationManager.ErrorCode.None)
392             {
393                 throw ApplicationManagerErrorFactory.GetException(err, "Failed to create the application event handle");
394             }
395
396             err = Interop.ApplicationManager.AppManagerEventSetStatus(_eventHandle, Interop.ApplicationManager.AppManagerEventStatusType.All);
397             if (err != Interop.ApplicationManager.ErrorCode.None)
398             {
399                 Interop.ApplicationManager.AppManagerEventDestroy(_eventHandle);
400                 _eventHandle = IntPtr.Zero;
401                 throw ApplicationManagerErrorFactory.GetException(err, "Failed to set the application event");
402             }
403
404             _eventCallback = (string appType, string appId, Interop.ApplicationManager.AppManagerEventType eventType, Interop.ApplicationManager.AppManagerEventState eventState, IntPtr eventHandle, IntPtr UserData) =>
405             {
406                 if (eventType == Interop.ApplicationManager.AppManagerEventType.Enable)
407                 {
408                     _enabledHandler?.Invoke(null, new ApplicationEnabledEventArgs(appId, (ApplicationEventState)eventState));
409                 }
410                 else if (eventType == Interop.ApplicationManager.AppManagerEventType.Disable)
411                 {
412                     _disabledHandler?.Invoke(null, new ApplicationDisabledEventArgs(appId, (ApplicationEventState)eventState));
413                 }
414             };
415             err = Interop.ApplicationManager.AppManagerSetEventCallback(_eventHandle, _eventCallback, IntPtr.Zero);
416             if (err != Interop.ApplicationManager.ErrorCode.None)
417             {
418                 Interop.ApplicationManager.AppManagerEventDestroy(_eventHandle);
419                 _eventHandle = IntPtr.Zero;
420                 throw ApplicationManagerErrorFactory.GetException(err, "Failed to set the application event callback");
421             }
422         }
423
424         private static void UnRegisterApplicationEvent()
425         {
426             if (_eventHandle != IntPtr.Zero)
427             {
428                 Interop.ApplicationManager.AppManagerUnSetEventCallback(_eventHandle);
429                 Interop.ApplicationManager.AppManagerEventDestroy(_eventHandle);
430                 _eventHandle = IntPtr.Zero;
431             }
432         }
433
434         /// <summary>
435         /// Gets the information of the recent applications.
436         /// </summary>
437         /// <returns>Returns a dictionary containing all the recent application info.</returns>
438         /// <exception cref="InvalidOperationException">Thrown when failed because of an invalid operation.</exception>
439         /// <since_tizen> 3 </since_tizen>
440         [EditorBrowsable(EditorBrowsableState.Never)]
441         public static IEnumerable<RecentApplicationInfo> GetRecentApplications()
442         {
443             Interop.ApplicationManager.ErrorCode err = Interop.ApplicationManager.ErrorCode.None;
444
445             List<RecentApplicationInfo> result = new List<RecentApplicationInfo>();
446             IntPtr table;
447             int nrows, ncols;
448
449             err = Interop.ApplicationManager.RuaHistoryLoadDb(out table, out nrows, out ncols);
450             if (err != Interop.ApplicationManager.ErrorCode.None)
451             {
452                 throw ApplicationManagerErrorFactory.GetException(err, "Failed to load a table for the recent application list.");
453             }
454
455             for (int row = 0; row < nrows; ++row)
456             {
457                 Interop.ApplicationManager.RuaRec record;
458
459                 err = Interop.ApplicationManager.RuaHistoryGetRecord(out record, table, nrows, ncols, row);
460                 if (err != Interop.ApplicationManager.ErrorCode.None)
461                 {
462                     throw ApplicationManagerErrorFactory.GetException(err, "Failed to get record.");
463                 }
464
465                 RecentApplicationInfo info = new RecentApplicationInfo(record);
466                 result.Add(info);
467             }
468
469             err = Interop.ApplicationManager.RuaHistoryUnLoadDb(ref table);
470             if (err != Interop.ApplicationManager.ErrorCode.None)
471             {
472                 throw ApplicationManagerErrorFactory.GetException(err, "Failed to unload a table for the recent application list.");
473             }
474
475             return result;
476         }
477     }
478
479     internal static class FilterExtension
480     {
481         private const string LogTag = "Tizen.Applications";
482         internal static void Fetch(this ApplicationInfoFilter filter, Interop.ApplicationManager.AppInfoFilterCallback callback)
483         {
484             if (filter is ApplicationInfoMetadataFilter)
485             {
486                 ApplicationInfoMetadataFilter metaFilter = (ApplicationInfoMetadataFilter)filter;
487                 metaFilter.Fetch(callback);
488                 return;
489             }
490
491             IntPtr nativeHandle = MakeNativeAppInfoFilter(filter.Filter);
492             if (nativeHandle == IntPtr.Zero)
493             {
494                 throw ApplicationManagerErrorFactory.NativeFilterHandleIsInvalid();
495             }
496             try
497             {
498                 Interop.ApplicationManager.ErrorCode err = Interop.ApplicationManager.AppInfoFilterForeachAppinfo(nativeHandle, callback, IntPtr.Zero);
499                 if (err != Interop.ApplicationManager.ErrorCode.None)
500                 {
501                     throw ApplicationManagerErrorFactory.GetException(err, "Failed to get application information list with filter.");
502                 }
503             }
504             finally
505             {
506                 Interop.ApplicationManager.AppInfoFilterDestroy(nativeHandle);
507             }
508         }
509
510         internal static void Fetch(this ApplicationInfoMetadataFilter filter, Interop.ApplicationManager.AppInfoFilterCallback callback)
511         {
512             IntPtr nativeHandle = MakeNativeAppMetadataFilter(filter.Filter);
513             if (nativeHandle == IntPtr.Zero)
514             {
515                 throw ApplicationManagerErrorFactory.NativeFilterHandleIsInvalid();
516             }
517             try
518             {
519                 Interop.ApplicationManager.ErrorCode err = Interop.ApplicationManager.AppInfoMetadataFilterForeach(nativeHandle, callback, IntPtr.Zero);
520                 if (err != Interop.ApplicationManager.ErrorCode.None)
521                 {
522                     throw ApplicationManagerErrorFactory.GetException(err, "Failed to get metadata list with filter.");
523                 }
524             }
525             finally
526             {
527                 Interop.ApplicationManager.AppInfoMetadataFilterDestroy(nativeHandle);
528             }
529         }
530
531         private static IntPtr MakeNativeAppInfoFilter(IDictionary<string, string> filter)
532         {
533             if (filter == null || filter.Count == 0)
534             {
535                 throw ApplicationManagerErrorFactory.FilterIsInvalid();
536             }
537
538             IntPtr infoHandle = IntPtr.Zero;
539             Interop.ApplicationManager.ErrorCode err = Interop.ApplicationManager.AppInfoFilterCreate(out infoHandle);
540             if (err != Interop.ApplicationManager.ErrorCode.None)
541             {
542                 throw ApplicationManagerErrorFactory.GetException(err, "Failed to create the filter handle.");
543             }
544
545             foreach (var item in filter)
546             {
547                 if ((item.Key == ApplicationInfoFilter.Keys.Id) ||
548                     (item.Key == ApplicationInfoFilter.Keys.Type) ||
549                     (item.Key == ApplicationInfoFilter.Keys.Category))
550                 {
551                     err = Interop.ApplicationManager.AppInfoFilterAddString(infoHandle, item.Key, item.Value);
552                 }
553                 else if ((item.Key == ApplicationInfoFilter.Keys.NoDisplay) ||
554                          (item.Key == ApplicationInfoFilter.Keys.TaskManage))
555                 {
556                     err = Interop.ApplicationManager.AppInfoFilterAddBool(infoHandle, item.Key, Convert.ToBoolean(item.Value));
557                 }
558                 else
559                 {
560                     Log.Warn(LogTag, string.Format("'{0}' is not supported key for the filter.", item.Key));
561                 }
562                 if (err != Interop.ApplicationManager.ErrorCode.None)
563                 {
564                     Interop.ApplicationManager.AppInfoFilterDestroy(infoHandle);
565                     throw ApplicationManagerErrorFactory.GetException(err, "Failed to add item to the filter.");
566                 }
567             }
568             return infoHandle;
569         }
570
571         private static IntPtr MakeNativeAppMetadataFilter(IDictionary<string, string> filter)
572         {
573             if (filter == null || filter.Count == 0)
574             {
575                 throw ApplicationManagerErrorFactory.FilterIsInvalid();
576             }
577
578             IntPtr infoHandle = IntPtr.Zero;
579             Interop.ApplicationManager.ErrorCode err = Interop.ApplicationManager.AppInfoMetadataFilterCreate(out infoHandle);
580             if (err != Interop.ApplicationManager.ErrorCode.None)
581             {
582                 throw ApplicationManagerErrorFactory.GetException(err, "Failed to create the filter for searching with metadata.");
583             }
584             foreach (var item in filter)
585             {
586                 err = Interop.ApplicationManager.AppInfoMetadataFilterAdd(infoHandle, item.Key, item.Value);
587                 if (err != Interop.ApplicationManager.ErrorCode.None)
588                 {
589                     Interop.ApplicationManager.AppInfoMetadataFilterDestroy(infoHandle);
590                     throw ApplicationManagerErrorFactory.GetException(err, "Failed to add the item to the filter.");
591                 }
592             }
593             return infoHandle;
594         }
595     }
596
597     internal static class ApplicationManagerErrorFactory
598     {
599         internal static Exception NativeFilterHandleIsInvalid()
600         {
601             return new InvalidOperationException("The native handle for filtering is invalid.");
602         }
603
604         internal static Exception FilterIsInvalid()
605         {
606             return new ArgumentException("The filter is invalid.");
607         }
608
609         internal static Exception GetException(Interop.ApplicationManager.ErrorCode err, string message)
610         {
611             string errMessage = String.Format("{0} err = {1}", message, err);
612             switch (err)
613             {
614                 case Interop.ApplicationManager.ErrorCode.InvalidParameter:
615                     return new ArgumentException(errMessage);
616                 default:
617                     return new InvalidOperationException(errMessage);
618             }
619         }
620     }
621 }