2 * Copyright (c) 2016 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.
20 using System.Collections.Generic;
21 using System.Runtime.InteropServices;
22 using System.Threading.Tasks;
24 namespace Tizen.Content.MediaContent
27 /// A Storage allows you to manage external storage.
28 /// The system generates the storage id when the external storage is added.And the system manages the media information in each of the storage by using storage id.
29 /// So you can get the information from the storage that you want to view.
31 public class Storage : ContentCollection
33 private IntPtr _storageHandle = IntPtr.Zero;
38 if (_storageHandle == IntPtr.Zero)
40 throw new ObjectDisposedException(nameof(Storage));
43 return _storageHandle;
47 /// The storage id of the media storage
49 /// <since_tizen> 3 </since_tizen>
54 IntPtr val = IntPtr.Zero;
57 MediaContentValidator.ThrowIfError(
58 Interop.Storage.GetId(Handle, out val), "Failed to get value");
60 return Marshal.PtrToStringAnsi(val);
64 Interop.Libc.Free(val);
70 /// The storage path of the media storage
72 /// <since_tizen> 3 </since_tizen>
73 public string StoragePath
77 IntPtr val = IntPtr.Zero;
80 MediaContentValidator.ThrowIfError(
81 Interop.Storage.GetPath(Handle, out val), "Failed to get value");
83 return Marshal.PtrToStringAnsi(val);
87 Interop.Libc.Free(val);
93 /// The storage name of the media storage
95 /// <since_tizen> 3 </since_tizen>
100 IntPtr val = IntPtr.Zero;
103 MediaContentValidator.ThrowIfError(
104 Interop.Storage.GetName(Handle, out val), "Failed to get value");
106 return Marshal.PtrToStringAnsi(val);
110 Interop.Libc.Free(val);
116 /// The storage type of the media storage
118 /// <since_tizen> 3 </since_tizen>
119 public ContentStorageType StorageType
123 ContentStorageType storageType;
124 MediaContentValidator.ThrowIfError(
125 Interop.Storage.GetType(Handle, out storageType), "Failed to get value");
131 internal Storage(IntPtr handle)
133 _storageHandle = handle;
137 /// Gets the count of media files for the passed filter in the given storage from the media database.
139 /// <since_tizen> 3 </since_tizen>
140 /// <param name="filter">ContentFilter used to match media content from the media database.</param>
141 /// <returns>The number of media contents matching the filter passed</returns>
142 public override int GetMediaInformationCount(ContentFilter filter)
145 IntPtr handle = (filter != null) ? filter.Handle : IntPtr.Zero;
146 MediaContentValidator.ThrowIfError(
147 Interop.Storage.GetMediaCountFromDb(Id, handle, out mediaCount), "Failed to get count");
152 public override void Dispose()
154 if (_storageHandle != IntPtr.Zero)
156 Interop.Storage.Destroy(_storageHandle);
157 _storageHandle = IntPtr.Zero;
162 /// Iterates through the media files with an optional filter in the given storage from the media database.
163 /// This function gets all media files associated with the given storage and meeting desired filter option.
164 /// If NULL is passed to the filter, no filtering is applied.
166 /// <since_tizen> 3 </since_tizen>
167 /// <param name="filter">ContentFilter used to match media content from the media database.</param>
168 /// <returns>List of content media items matching the passed filter</returns>
169 public override IEnumerable<MediaInformation> GetMediaInformations(ContentFilter filter)
171 List<MediaInformation> mediaContents = new List<MediaInformation>();
173 IntPtr handle = (filter != null) ? filter.Handle : IntPtr.Zero;
174 Interop.Storage.MediaInfoCallback callback = (IntPtr mediaHandle, IntPtr data) =>
176 Interop.MediaInformation.SafeMediaInformationHandle newHandle;
177 MediaContentValidator.ThrowIfError(
178 Interop.MediaInformation.Clone(out newHandle, mediaHandle), "Failed to clone media");
180 MediaContentType type;
181 Interop.MediaInformation.GetMediaType(newHandle, out type);
182 if (type == MediaContentType.Image)
184 Interop.ImageInformation.SafeImageInformationHandle imageInfo;
185 MediaContentValidator.ThrowIfError(
186 Interop.MediaInformation.GetImage(mediaHandle, out imageInfo), "Failed to get image information");
188 mediaContents.Add(new ImageInformation(imageInfo, newHandle));
190 else if ((type == MediaContentType.Music) || (type == MediaContentType.Sound))
192 Interop.AudioInformation.SafeAudioInformationHandle audioInfo;
193 MediaContentValidator.ThrowIfError(
194 Interop.MediaInformation.GetAudio(mediaHandle, out audioInfo), "Failed to get audio information");
196 mediaContents.Add(new AudioInformation(audioInfo, newHandle));
198 else if (type == MediaContentType.Video)
200 Interop.VideoInformation.SafeVideoInformationHandle videoInfo;
201 MediaContentValidator.ThrowIfError(
202 Interop.MediaInformation.GetVideo(mediaHandle, out videoInfo), "Failed to get video information");
204 mediaContents.Add(new VideoInformation(videoInfo, newHandle));
206 else if (type == MediaContentType.Others)
208 mediaContents.Add(new MediaInformation(newHandle));
213 MediaContentValidator.ThrowIfError(
214 Interop.Storage.ForeachMediaFromDb(Id, handle, callback, IntPtr.Zero), "Failed to get information");
216 return mediaContents;