1 /// Copyright 2016 by Samsung Electronics, Inc.,
3 /// This software is the confidential and proprietary information
4 /// of Samsung Electronics, Inc.("Confidential Information"). You
5 /// shall not disclose such Confidential Information and shall use
6 /// it only in accordance with the terms of the license agreement
7 /// you entered into with Samsung.
11 using System.Collections.Generic;
12 using System.Threading.Tasks;
14 namespace Tizen.Content.MediaContent
17 /// A Folder is used to organize media content files i.e. image, audio, video files, in the physical storage of the device.
18 /// The Media Folder API provides functions to get basic information about existing folders e.g. folder name, path and storage type.
19 /// It also provides functions to get information related to media items present in the folder.
21 public class MediaFolder : ContentCollection
23 private IntPtr _folderHandle;
24 private bool _disposedValue = false;
25 internal IntPtr Handle
33 _folderHandle = value;
37 /// The ID of the media folder
44 MediaContentError res = (MediaContentError)Interop.Folder.GetFolderId(_folderHandle, out id);
45 if (res != MediaContentError.None)
47 Log.Warn(MediaContentErrorFactory.LogTag, "Failed to get Id for the MediaFolder");
54 /// The ID of the upper media folder
56 public string ParentId
61 MediaContentError res = (MediaContentError)Interop.Folder.GetParentFolderId(_folderHandle, out parentId);
62 if (res != MediaContentError.None)
64 Log.Warn(MediaContentErrorFactory.LogTag, "Failed to get parents folder Id for the MediaFolder");
71 /// The path of the media folder
78 MediaContentError res = (MediaContentError)Interop.Folder.GetPath(_folderHandle, out path);
79 if (res != MediaContentError.None)
81 Log.Warn(MediaContentErrorFactory.LogTag, "Failed to get path for the MediaFolder");
88 /// The name of the media folder
95 MediaContentError res = (MediaContentError)Interop.Folder.GetName(_folderHandle, out name);
96 if (res != MediaContentError.None)
98 Log.Warn(MediaContentErrorFactory.LogTag, "Failed to get name for the MediaFolder");
104 MediaContentError res = (MediaContentError)Interop.Folder.SetName(_folderHandle, value);
105 if (res != MediaContentError.None)
107 Log.Warn(MediaContentErrorFactory.LogTag, "Failed to set name for the MediaFolder");
113 /// The storage type of the media folder
115 public ContentStorageType StorageType
120 MediaContentError res = (MediaContentError)Interop.Folder.GetStorageType(_folderHandle, out type);
121 if (res != MediaContentError.None)
123 Log.Warn(MediaContentErrorFactory.LogTag, "Failed to get storage type for the MediaFolder");
125 return (ContentStorageType)type;
130 /// The storage id of the media folder
132 public string StorageId
137 MediaContentError res = (MediaContentError)Interop.Folder.GetStorageId(_folderHandle, out storageId);
138 if (res != MediaContentError.None)
140 Log.Warn(MediaContentErrorFactory.LogTag, "Failed to get storage Id for the MediaFolder");
147 /// The modified date of the media folder
149 public DateTime ModifiedTime
154 MediaContentError res = (MediaContentError)Interop.Folder.GetModifiedTime(_folderHandle, out date);
155 if (res != MediaContentError.None)
157 Log.Warn(MediaContentErrorFactory.LogTag, "Failed to get modified date for the MediaFolder");
164 /// The folder order info. Get/Set the folder viewing order
166 public ContentOrder Order
171 MediaContentError res = (MediaContentError)Interop.Folder.GetOrder(_folderHandle, out order);
172 if (res != MediaContentError.None)
174 Log.Warn(MediaContentErrorFactory.LogTag, "Failed to get modified date for the MediaFolder");
176 return (ContentOrder)order;
180 MediaContentError res = (MediaContentError)Interop.Folder.SetOrder(_folderHandle, (int)value);
181 if (res != MediaContentError.None)
183 Log.Warn(MediaContentErrorFactory.LogTag, "Failed to set viewing order for the MediaFolder");
188 internal MediaFolder(IntPtr _folderHandle)
190 this.Handle = _folderHandle;
194 /// Gets the count of media files for the passed filter in the given folder from the media database.
196 /// <param name="filter">ContentFilter used to match media content from teh media database.</param>
197 /// <returns>The number of media contents matching the filter passed</returns>
198 public override int GetMediaInformationCount(ContentFilter filter)
201 IntPtr handle = (filter != null) ? filter.Handle : IntPtr.Zero;
202 MediaContentError res = (MediaContentError)Interop.Folder.GetMediaCountFromDb(Id, handle, out mediaCount);
203 if (res != MediaContentError.None)
205 Log.Warn(MediaContentErrorFactory.LogTag, "Failed to get media count for the folder");
214 public override void Dispose()
217 GC.SuppressFinalize(this);
220 protected virtual void Dispose(bool disposing)
224 if (_folderHandle != IntPtr.Zero)
226 Console.WriteLine("Before destroy");
227 Interop.Folder.Destroy(_folderHandle);
228 _folderHandle = IntPtr.Zero;
229 Console.WriteLine("After destroy");
231 _disposedValue = true;
235 /// Iterates through the media files with an optional filter in the given folder from the media database.
236 /// This function gets all media files associated with the given folder and meeting desired filter option.
237 /// If NULL is passed to the filter, no filtering is applied.
239 /// <param name="filter">ContentFilter used to match media content from the media database.</param>
240 /// <returns>List of content media items matching the passed filter</returns>
241 public override Task<IEnumerable<MediaInformation>> GetMediaInformationsAsync(ContentFilter filter)
243 var tcs = new TaskCompletionSource<IEnumerable<MediaInformation>>();
244 List<MediaInformation> mediaContents = new List<MediaInformation>();
245 IntPtr handle = (filter != null) ? filter.Handle : IntPtr.Zero;
246 MediaContentError res;
247 Interop.Folder.MediaInfoCallback callback = (Interop.MediaInformation.SafeMediaInformationHandle mediaHandle, IntPtr data) =>
249 //TODO: Create MediaInformation using safehandle.
250 Interop.MediaInformation.SafeMediaInformationHandle newHandle;
251 res = (MediaContentError)Interop.MediaInformation.Clone(out newHandle, mediaHandle.DangerousGetHandle());
252 if (res != MediaContentError.None)
254 Log.Warn(MediaContentErrorFactory.LogTag, "Failed to clone media information");
256 MediaInformation info = new MediaInformation(newHandle);
257 mediaContents.Add(info);
259 res = (MediaContentError)Interop.Folder.ForeachMediaFromDb(Id, handle, callback, IntPtr.Zero);
260 if (res != MediaContentError.None)
262 Log.Warn(MediaContentErrorFactory.LogTag, "Failed to get media information for the folder");
264 tcs.TrySetResult(mediaContents);