2 * Copyright (c) 2017 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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;
19 using System.Threading.Tasks;
20 using Tizen.Content.MediaContent;
21 using TVMediaHub.Tizen.Utils;
22 using TVMediaHub.Tizen.DataModels;
24 namespace TVMediaHub.Tizen.Models
27 /// An enumeration for sort option of contents
29 public enum SortOption
31 Date, // Video, Image, Music
32 Title, // Video, Image, Music
35 Genre, // Video, Music
41 /// A class for providing media contents
43 public abstract class ContentProvider
46 /// A method for getting condition for selection query
48 /// <returns>A condition string</returns>
49 abstract protected string GetConditionStringForSelection();
52 /// An abstract method to determine the availability of the content.
54 /// <param name="mediaContent">A media content to be checked the availability</param>
55 abstract protected void CheckUnavailableContent(MediaInformationEx mediaContent);
58 /// A method for handling ContentUpdatedEvent
60 /// <param name="listener">A handling method</param>
61 abstract public void SetContentUpdatedEventListener(EventHandler listener);
64 /// An EventHandler be sent to Image, Video and MusicProvider when ContentUpdated event is triggered
66 public EventHandler ContentUpdateFinished;
70 /// Connect to the media database to search, insert, remove or modify media information.
72 public ContentProvider()
76 MediaDatabase.MediaInfoUpdated += MediaDatabaseMediaUpdated;
86 /// Handles ContentUpdated event that is triggered when db is changed.
88 /// <param name="sender">The source of the event</param>
89 /// <param name="e">A ContentUpdatedEvent arguments</param>
90 private void MediaDatabaseMediaUpdated(object sender, MediaInfoUpdatedEventArgs e)
92 if (e.OperationType == OperationType.Update)
94 MediaHubImpl.GetInstance.StorageProviderInstance.CheckStorage();
95 ContentUpdateFinished?.Invoke(this, null);
100 /// Converts the value of the DateTime object to its equivalent string representation.
102 /// <param name="date">A DateTime information</param>
103 /// <returns>A string to be displayed</returns>
104 private string GetDateString(DateTime date)
106 if (date.Equals(DateTime.Today))
110 else if (date.Equals(DateTime.Today.AddDays(-1)))
115 return date.ToString("ddd. d MMMM");
119 /// A method for creating group item to be added to the proper group
121 /// <param name="sortOption">A current sort option</param>
122 /// <param name="lastGroupItem">A latest group item of current group</param>
123 /// <param name="mediaInformationEx">A media information to be converted to group item</param>
124 /// <returns>A group item to be added</returns>
125 private GroupItem GetGroupItem(SortOption sortOption, GroupItem lastGroupItem, MediaInformationEx mediaInformationEx)
127 GroupItem currentGroupItem = lastGroupItem;
128 string newTitle = null;
129 bool newGroupFlag = false;
133 case SortOption.Title:
134 if (lastGroupItem == null || lastGroupItem.Title != mediaInformationEx.MediaContentInformation.DisplayName[0].ToString())
137 newTitle = mediaInformationEx.MediaContentInformation.DisplayName[0].ToString();
141 case SortOption.Date:
142 if (lastGroupItem == null || lastGroupItem.Title != GetDateString(mediaInformationEx.MediaContentInformation.Timeline.DateTime))
145 newTitle = GetDateString(mediaInformationEx.MediaContentInformation.Timeline.DateTime);
149 case SortOption.Genre:
150 if (lastGroupItem == null || lastGroupItem.Title != mediaInformationEx.MediaContentInformation.Category)
153 newTitle = mediaInformationEx.MediaContentInformation.Category;
157 case SortOption.Type:
158 if (lastGroupItem == null || lastGroupItem.Title != mediaInformationEx.MediaContentInformation.MediaType.ToString())
161 newTitle = mediaInformationEx.MediaContentInformation.MediaType.ToString();
165 case SortOption.Album:
166 if (lastGroupItem == null || lastGroupItem.Title != (mediaInformationEx.MediaContentInformation as AudioInfo)?.Album)
169 newTitle = (mediaInformationEx.MediaContentInformation as AudioInfo)?.Album.ToString();
173 case SortOption.Artist:
174 if (lastGroupItem == null || lastGroupItem.Title != (mediaInformationEx.MediaContentInformation as AudioInfo)?.Artist)
177 newTitle = (mediaInformationEx.MediaContentInformation as AudioInfo)?.Artist.ToString();
182 throw new System.ArgumentException("Invalid sorting option.");
185 if (newGroupFlag == true)
187 currentGroupItem = new GroupItem();
188 currentGroupItem.Title = newTitle;
189 currentGroupItem.Contents = new List<MediaShortcutInfo>();
192 return currentGroupItem;
196 /// A method for making group to be displayed from MediaInformationEx list
198 /// <param name="mediaInformationExList">A list of MediaInformationEx</param>
199 /// <param name="sortOption">The current sort option</param>
200 /// <returns>A list of group item</returns>
201 private async Task<IEnumerable<GroupItem>> MakeGroupAsync(IEnumerable<MediaInformationEx> mediaInformationExList, SortOption sortOption)
203 return await Task.Run(() =>
205 List<GroupItem> result = new List<GroupItem>();
206 GroupItem lastGroupItem = null;
207 GroupItem currentGroupItem = null;
209 if (mediaInformationExList == null)
211 throw new System.ArgumentException("mediaInformationExList must not be null.");
214 foreach (MediaInformationEx mediaInformationEx in mediaInformationExList)
216 var currentInformation = mediaInformationEx;
217 var shortcutInfo = new MediaShortcutInfo(currentInformation);
219 // TODO : The catch implementation should be checked once again.
222 currentGroupItem = GetGroupItem(sortOption, currentGroupItem, mediaInformationEx);
226 DbgPort.E(e.Message);
230 if (lastGroupItem != currentGroupItem)
232 result.Add(currentGroupItem);
233 lastGroupItem = currentGroupItem;
236 if (currentGroupItem != null)
238 currentGroupItem.Contents.Add(shortcutInfo);
247 /// A method for reading media informations from database without group information
249 /// <param name="sortOption">The current sort option</param>
250 /// <param name="storageId">The current storage id</param>
251 /// <param name="offset">The start position of the given filter Starting from zero</param>
252 /// <param name="count">The number of items to be searched with respect to the offset</param>
253 /// <returns>A list of media informations</returns>
254 public IEnumerable<MediaInformationEx> ReadWithoutGroup(SortOption sortOption, string storageId = null, int offset = -1, int count = -1)
256 // Makes Content Filter by arguments
257 var selectArguments = new SelectArguments();
261 case SortOption.Title:
262 selectArguments.SortOrder = "MEDIA_DISPLAY_NAME";
264 case SortOption.Date:
265 selectArguments.SortOrder = "MEDIA_RECORDED_DATE";
267 case SortOption.Genre:
268 selectArguments.SortOrder = "MEDIA_GENRE";
270 case SortOption.Type:
271 selectArguments.SortOrder = "MEDIA_TYPE";
273 case SortOption.Album:
274 selectArguments.SortOrder = "MEDIA_ALBUM";
276 case SortOption.Artist:
277 selectArguments.SortOrder = "MEDIA_ARTIST";
280 throw new System.ArgumentException("Invalid sorting option.");
285 selectArguments.StartRowIndex = offset;
290 selectArguments.TotalRowCount = count;
293 if (storageId != null)
295 selectArguments.StorageId = storageId;
298 selectArguments.FilterExpression = GetConditionStringForSelection();
299 // Executes the 'select' query
300 List<MediaInfo> mediaInformationList = new List<MediaInfo>();
301 List<MediaInformationEx> mediaInformationExList = new List<MediaInformationEx>();
304 var reader = MediaHubImpl.GetInstance.MediaInfoCommand.SelectMedia(selectArguments);
306 while (reader.Read())
308 mediaInformationList.Add(reader.Current);
311 foreach (MediaInfo mediaInformation in mediaInformationList)
313 var mediaInformationEx = new MediaInformationEx
315 MediaContentInformation = mediaInformation
318 CheckUnavailableContent(mediaInformationEx);
319 mediaInformationExList.Add(mediaInformationEx);
322 catch (Exception exception)
324 DbgPort.E(exception.Message);
327 return mediaInformationExList;
331 /// A method for making thumbnail of Media contents.
333 /// <param name="list">A list of media contents</param>
334 /// <returns>A path of thumbnail</returns>
335 public async Task CheckThumbnail(IEnumerable<MediaInformationEx> list)
337 foreach (var info in list)
339 if (string.IsNullOrEmpty(info.MediaContentInformation.ThumbnailPath))
343 string path = await MediaHubImpl.GetInstance.MediaInfoCommand.CreateThumbnailAsync(info.MediaContentInformation.Id);
347 DbgPort.D(e.Message);
354 /// A method for reading media informations from database without group information asynchronously
356 /// <param name="sortOption">The current sort option</param>
357 /// <param name="storageId">The current storage id</param>
358 /// <param name="offset">The start position of the given filter Starting from zero</param>
359 /// <param name="count">The number of items to be searched with respect to the offset</param>
360 /// <returns>A list of media informations</returns>
361 public async Task<IEnumerable<MediaInformationEx>> ReadWithoutGroupAsync(SortOption sortOption, string storageId = null, int offset = -1, int count = -1)
363 return await Task.Run(() => {
364 // Makes Content Filter by arguments
365 var selectArguments = new SelectArguments();
369 case SortOption.Title:
370 selectArguments.SortOrder = "MEDIA_DISPLAY_NAME";
372 case SortOption.Date:
373 selectArguments.SortOrder = "MEDIA_RECORDED_DATE";
375 case SortOption.Genre:
376 selectArguments.SortOrder = "MEDIA_GENRE";
378 case SortOption.Type:
379 selectArguments.SortOrder = "MEDIA_TYPE";
381 case SortOption.Album:
382 selectArguments.SortOrder = "MEDIA_ALBUM";
384 case SortOption.Artist:
385 selectArguments.SortOrder = "MEDIA_ARTIST";
388 throw new System.ArgumentException("Invalid sorting option.");
393 selectArguments.StartRowIndex = offset;
398 selectArguments.TotalRowCount = count;
401 if (storageId != null)
403 selectArguments.StorageId = storageId;
406 selectArguments.FilterExpression = GetConditionStringForSelection();
408 // Executes the 'select' query
409 List<MediaInfo> mediaInformationList = new List<MediaInfo>();
410 List<MediaInformationEx> mediaInformationExList = new List<MediaInformationEx>();
414 var reader = MediaHubImpl.GetInstance.MediaInfoCommand.SelectMedia(selectArguments);
416 while (reader.Read())
418 mediaInformationList.Add(reader.Current);
422 foreach (MediaInfo mediaInformation in mediaInformationList)
424 var mediaInformationEx = new MediaInformationEx();
426 mediaInformationEx.MediaContentInformation = mediaInformation;
427 CheckUnavailableContent(mediaInformationEx);
428 mediaInformationExList.Add(mediaInformationEx);
431 catch (Exception exception)
433 DbgPort.E(exception.Message);
436 return mediaInformationExList;
441 /// A method for reading media contents from database and make a group
443 /// <param name="sortOption">The current sort option</param>
444 /// <param name="storageId">The current storage id</param>
445 /// <param name="offset">The start position of the given filter Starting from zero</param>
446 /// <param name="count">The number of items to be searched with respect to the offset</param>
447 /// <returns>A list of group item</returns>
448 public async Task<IEnumerable<GroupItem>> ReadAsync(SortOption sortOption, string storageId = null, int offset = -1, int count = -1)
450 IEnumerable<MediaInformationEx> list = await ReadWithoutGroupAsync(sortOption, storageId, offset, count);
452 return await MakeGroupAsync(list, sortOption);
456 /// A method for reading media content with specific media id
458 /// <param name="mediaID">A media Id to be read</param>
459 /// <returns>A media content with specific media id</returns>
460 public MediaInformationEx Read(String mediaID)
462 var mediaInformationEx = new MediaInformationEx();
463 mediaInformationEx.MediaContentInformation = MediaHubImpl.GetInstance.MediaInfoCommand.SelectMedia(mediaID);
464 return mediaInformationEx;
468 /// A method for deleting media content
470 /// <param name="media">A media content to be deleted</param>
471 /// <returns>Always returns true</returns>
472 public bool Delete(MediaInformationEx media)
476 MediaHubImpl.GetInstance.MediaInfoCommand.Delete(media.MediaContentInformation.Id);
478 catch (Exception exception)
480 DbgPort.E(exception.Message);
487 /// A method for updating media content
489 /// <param name="media">A media content to be updated</param>
490 /// <returns>Always returns true</returns>
491 public bool Update(MediaInformationEx media)
495 // @TODO : CHECK POINT 170824
496 //MediaHubImpl.GetInstance.MediaInfoCommand.Update(media.MediaContentInformation.Id);
498 catch (Exception exception)
500 DbgPort.E(exception.Message);