Removing Tizen.Xamarin.Forms.Extensions
[profile/tv/apps/dotnet/mediahub.git] / TVMediaHub / TVMediaHub.Tizen / Models / ContentProvider.cs
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd
3  *
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
7  *
8  *     http://floralicense.org/license/
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.Threading.Tasks;
20 using Tizen.Content.MediaContent;
21 using TVMediaHub.Tizen.Utils;
22 using TVMediaHub.Tizen.DataModels;
23
24 namespace TVMediaHub.Tizen.Models
25 {
26     /// <summary>
27     /// An enumeration for sort option of contents
28     /// </summary>
29     public enum SortOption
30     {
31         Date,   // Video, Image, Music
32         Title,  // Video, Image, Music
33         Album,  // Music
34         Artist, // Music
35         Genre,  // Video, Music
36         Folder, // Music
37         Type,   // Image
38     }
39
40     /// <summary>
41     /// A class for providing media contents
42     /// </summary>
43     public abstract class ContentProvider
44     {
45         /// <summary>
46         /// A method for getting condition for selection query
47         /// </summary>
48         /// <returns>A condition string</returns>
49         abstract protected string GetConditionStringForSelection();
50
51         /// <summary>
52         /// An abstract method to determine the availability of the content.
53         /// </summary>
54         /// <param name="mediaContent">A media content to be checked the availability</param>
55         abstract protected void CheckUnavailableContent(MediaInformationEx mediaContent);
56
57         /// <summary>
58         /// A method for handling ContentUpdatedEvent
59         /// </summary>
60         /// <param name="listener">A handling method</param>
61         abstract public void SetContentUpdatedEventListener(EventHandler listener);
62
63         /// <summary>
64         /// An EventHandler be sent to Image, Video and MusicProvider when ContentUpdated event is triggered
65         /// </summary>
66         public EventHandler ContentUpdateFinished;
67
68         /// <summary>
69         /// A constructor
70         /// Connect to the media database to search, insert, remove or modify media information.
71         /// </summary>
72         public ContentProvider()
73         {
74             try
75             {
76                 MediaDatabase.MediaInfoUpdated += MediaDatabaseMediaUpdated;
77             }
78             catch (Exception e)
79             {
80                 DbgPort.E(e.Message);
81             }
82
83         }
84
85         /// <summary>
86         /// Handles ContentUpdated event that is triggered when db is changed.
87         /// </summary>
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)
91         {
92             if (e.OperationType == OperationType.Update)
93             {
94                 MediaHubImpl.GetInstance.StorageProviderInstance.CheckStorage();
95                 ContentUpdateFinished?.Invoke(this, null);
96             }
97         }
98
99         /// <summary>
100         /// Converts the value of the DateTime object to its equivalent string representation.
101         /// </summary>
102         /// <param name="date">A DateTime information</param>
103         /// <returns>A string to be displayed</returns>
104         private string GetDateString(DateTime date)
105         {
106             if (date.Equals(DateTime.Today))
107             {
108                 return "Today";
109             }
110             else if (date.Equals(DateTime.Today.AddDays(-1)))
111             {
112                 return "Yesterday";
113             }
114
115             return date.ToString("ddd. d MMMM");
116         }
117
118         /// <summary>
119         /// A method for creating group item to be added to the proper group
120         /// </summary>
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)
126         {
127             GroupItem currentGroupItem = lastGroupItem;
128             string newTitle = null;
129             bool newGroupFlag = false;
130
131             switch (sortOption)
132             {
133                 case SortOption.Title:
134                     if (lastGroupItem == null || lastGroupItem.Title != mediaInformationEx.MediaContentInformation.DisplayName[0].ToString())
135                     {
136                         newGroupFlag = true;
137                         newTitle = mediaInformationEx.MediaContentInformation.DisplayName[0].ToString();
138                     }
139
140                     break;
141                 case SortOption.Date:
142                     if (lastGroupItem == null || lastGroupItem.Title != GetDateString(mediaInformationEx.MediaContentInformation.Timeline.DateTime))
143                     {
144                         newGroupFlag = true;
145                         newTitle = GetDateString(mediaInformationEx.MediaContentInformation.Timeline.DateTime);
146                     }
147
148                     break;
149                 case SortOption.Genre:
150                     if (lastGroupItem == null || lastGroupItem.Title != mediaInformationEx.MediaContentInformation.Title)
151                     {
152                         newGroupFlag = true;
153                         newTitle = mediaInformationEx.MediaContentInformation.Title;
154                     }
155
156                     break;
157                 case SortOption.Type:
158                     if (lastGroupItem == null || lastGroupItem.Title != mediaInformationEx.MediaContentInformation.MediaType.ToString())
159                     {
160                         newGroupFlag = true;
161                         newTitle = mediaInformationEx.MediaContentInformation.MediaType.ToString();
162                     }
163
164                     break;
165                 case SortOption.Album:
166                     if (lastGroupItem == null || lastGroupItem.Title != (mediaInformationEx.MediaContentInformation as AudioInfo)?.Album)
167                     {
168                         newGroupFlag = true;
169                         newTitle = (mediaInformationEx.MediaContentInformation as AudioInfo)?.Album.ToString();
170                     }
171
172                     break;
173                 case SortOption.Artist:
174                     if (lastGroupItem == null || lastGroupItem.Title != (mediaInformationEx.MediaContentInformation as AudioInfo)?.Artist)
175                     {
176                         newGroupFlag = true;
177                         newTitle = (mediaInformationEx.MediaContentInformation as AudioInfo)?.Artist.ToString();
178                     }
179
180                     break;
181                 default:
182                     throw new System.ArgumentException("Invalid sorting option.");
183             }
184
185             if (newGroupFlag == true)
186             {
187                 currentGroupItem = new GroupItem();
188                 currentGroupItem.Title = newTitle;
189                 currentGroupItem.Contents = new List<MediaShortcutInfo>();
190             }
191
192             return currentGroupItem;
193         }
194
195         /// <summary>
196         /// A method for making group to be displayed from MediaInformationEx list
197         /// </summary>
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)
202         {
203             return await Task.Run(() =>
204             {
205                 List<GroupItem> result = new List<GroupItem>();
206                 GroupItem lastGroupItem = null;
207                 GroupItem currentGroupItem = null;
208
209                 if (mediaInformationExList == null)
210                 {
211                     throw new System.ArgumentException("mediaInformationExList must not be null.");
212                 }
213
214                 foreach (MediaInformationEx mediaInformationEx in mediaInformationExList)
215                 {
216                     var currentInformation = mediaInformationEx;
217                     var shortcutInfo = new MediaShortcutInfo(currentInformation);
218
219                     // TODO : The catch implementation should be checked once again.
220                     try
221                     {
222                         currentGroupItem = GetGroupItem(sortOption, currentGroupItem, mediaInformationEx);
223                     }
224                     catch (Exception e)
225                     {
226                         DbgPort.E(e.Message);
227                         return null;
228                     }
229
230                     if (lastGroupItem != currentGroupItem)
231                     {
232                         result.Add(currentGroupItem);
233                         lastGroupItem = currentGroupItem;
234                     }
235
236                     if (currentGroupItem != null)
237                     {
238                         currentGroupItem.Contents.Add(shortcutInfo);
239                     }
240                 }
241
242                 return result;
243             });
244         }
245
246         /// <summary>
247         /// A method for reading media informations from database without group information
248         /// </summary>
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)
255         {
256             // Makes Content Filter by arguments
257             var selectArguments = new SelectArguments();
258
259             switch (sortOption)
260             {
261                 case SortOption.Title:
262                     selectArguments.SortOrder = "MEDIA_DISPLAY_NAME";
263                     break;
264                 case SortOption.Date:
265                     selectArguments.SortOrder = "MEDIA_RECORDED_DATE";
266                     break;
267                 case SortOption.Genre:
268                     selectArguments.SortOrder = "MEDIA_GENRE";
269                     break;
270                 case SortOption.Type:
271                     selectArguments.SortOrder = "MEDIA_TYPE";
272                     break;
273                 case SortOption.Album:
274                     selectArguments.SortOrder = "MEDIA_ALBUM";
275                     break;
276                 case SortOption.Artist:
277                     selectArguments.SortOrder = "MEDIA_ARTIST";
278                     break;
279                 default:
280                     throw new System.ArgumentException("Invalid sorting option.");
281             }
282
283             if (offset >= 0)
284             {
285                 selectArguments.StartRowIndex = offset;
286             }
287
288             if (count >= 0)
289             {
290                 selectArguments.TotalRowCount = count;
291             }
292
293             if (storageId != null)
294             {
295                 selectArguments.StorageId = storageId;
296             }
297
298             selectArguments.FilterExpression = GetConditionStringForSelection();
299             // Executes the 'select' query
300             List<MediaInfo> mediaInformationList = new List<MediaInfo>();
301             List<MediaInformationEx> mediaInformationExList = new List<MediaInformationEx>();
302             try
303             {
304                 var reader = MediaHubImpl.GetInstance.MediaInfoCommand.SelectMedia(selectArguments);
305
306                 while (reader.Read())
307                 {
308                     mediaInformationList.Add(reader.Current);
309                 }
310
311                 foreach (MediaInfo mediaInformation in mediaInformationList)
312                 {
313                     var mediaInformationEx = new MediaInformationEx
314                     {
315                         MediaContentInformation = mediaInformation
316                     };
317
318                     CheckUnavailableContent(mediaInformationEx);
319                     mediaInformationExList.Add(mediaInformationEx);
320                 }
321             }
322             catch (Exception exception)
323             {
324                 DbgPort.E(exception.Message);
325             }
326
327             return mediaInformationExList;
328         }
329
330         /// <summary>
331         /// A method for making thumbnail of Media contents.
332         /// </summary>
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)
336         {
337             foreach (var info in list)
338             {
339                 if (string.IsNullOrEmpty(info.MediaContentInformation.ThumbnailPath))
340                 {
341                     try
342                     {
343                         string path = await MediaHubImpl.GetInstance.MediaInfoCommand.CreateThumbnailAsync(info.MediaContentInformation.Id);
344                     }
345                     catch (Exception e)
346                     {
347                         DbgPort.D(e.Message);
348                     }
349                 }
350             }
351         }
352
353         /// <summary>
354         /// A method for reading media informations from database without group information asynchronously
355         /// </summary>
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)
362         {
363             return await Task.Run(() => {
364                 // Makes Content Filter by arguments
365                 var selectArguments = new SelectArguments();
366
367                 switch (sortOption)
368                 {
369                     case SortOption.Title:
370                         selectArguments.SortOrder = "MEDIA_DISPLAY_NAME";
371                         break;
372                     case SortOption.Date:
373                         selectArguments.SortOrder = "MEDIA_RECORDED_DATE";
374                         break;
375                     case SortOption.Genre:
376                         selectArguments.SortOrder = "MEDIA_GENRE";
377                         break;
378                     case SortOption.Type:
379                         selectArguments.SortOrder = "MEDIA_TYPE";
380                         break;
381                     case SortOption.Album:
382                         selectArguments.SortOrder = "MEDIA_ALBUM";
383                         break;
384                     case SortOption.Artist:
385                         selectArguments.SortOrder = "MEDIA_ARTIST";
386                         break;
387                     default:
388                         throw new System.ArgumentException("Invalid sorting option.");
389                 }
390
391                 if (offset >= 0)
392                 {
393                     selectArguments.StartRowIndex = offset;
394                 }
395
396                 if (count >= 0)
397                 {
398                     selectArguments.TotalRowCount = count;
399                 }
400
401                 if (storageId != null)
402                 {
403                     selectArguments.StorageId = storageId;
404                 }
405
406                 selectArguments.FilterExpression = GetConditionStringForSelection();
407
408                 // Executes the 'select' query
409                 List<MediaInfo> mediaInformationList = new List<MediaInfo>();
410                 List<MediaInformationEx> mediaInformationExList = new List<MediaInformationEx>();
411                 try
412                 {
413
414                     var reader = MediaHubImpl.GetInstance.MediaInfoCommand.SelectMedia(selectArguments);
415
416                     while (reader.Read())
417                     {
418                         mediaInformationList.Add(reader.Current);
419                     }
420
421
422                     foreach (MediaInfo mediaInformation in mediaInformationList)
423                     {
424                         var mediaInformationEx = new MediaInformationEx();
425
426                         mediaInformationEx.MediaContentInformation = mediaInformation;
427                         CheckUnavailableContent(mediaInformationEx);
428                         mediaInformationExList.Add(mediaInformationEx);
429                     }
430                 }
431                 catch (Exception exception)
432                 {
433                     DbgPort.E(exception.Message);
434                 }
435
436                 return mediaInformationExList;
437             });
438         }
439
440         /// <summary>
441         /// A method for reading media contents from database and make a group
442         /// </summary>
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)
449         {
450             IEnumerable<MediaInformationEx> list = await ReadWithoutGroupAsync(sortOption, storageId, offset, count);
451
452             return await MakeGroupAsync(list, sortOption);
453         }
454
455         /// <summary>
456         /// A method for reading media content with specific media id
457         /// </summary>
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)
461         {
462             var mediaInformationEx = new MediaInformationEx();
463             mediaInformationEx.MediaContentInformation = MediaHubImpl.GetInstance.MediaInfoCommand.SelectMedia(mediaID);
464             return mediaInformationEx;
465         }
466
467         /// <summary>
468         /// A method for deleting media content
469         /// </summary>
470         /// <param name="media">A media content to be deleted</param>
471         /// <returns>Always returns true</returns>
472         public bool Delete(MediaInformationEx media)
473         {
474             try
475             {
476                 MediaHubImpl.GetInstance.MediaInfoCommand.Delete(media.MediaContentInformation.Id);
477             }
478             catch (Exception exception)
479             {
480                 DbgPort.E(exception.Message);
481             }
482
483             return true;
484         }
485
486         /// <summary>
487         /// A method for updating media content
488         /// </summary>
489         /// <param name="media">A media content to be updated</param>
490         /// <returns>Always returns true</returns>
491         public bool Update(MediaInformationEx media)
492         {
493             try
494             {
495                 // @TODO : CHECK POINT 170824
496                 //MediaHubImpl.GetInstance.MediaInfoCommand.Update(media.MediaContentInformation.Id);
497             }
498             catch (Exception exception)
499             {
500                 DbgPort.E(exception.Message);
501             }
502
503             return true;
504         }
505     }
506 }