summaryrefslogtreecommitdiff
path: root/Tizen.Content.MediaContent/Tizen.Content.MediaContent/Album.cs
blob: 5cc3b6153677e196ed8b014ca9f0fa45c86a862c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/// Copyright 2016 by Samsung Electronics, Inc.,
///
/// This software is the confidential and proprietary information
/// of Samsung Electronics, Inc.("Confidential Information"). You
/// shall not disclose such Confidential Information and shall use
/// it only in accordance with the terms of the license agreement
/// you entered into with Samsung.


using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Tizen.Content.MediaContent
{
    /// <summary>
    /// An album is a logical collection or grouping of related audio files. It is also used for filtering media items.
    /// The Media Album API allows to manage media albums which contains all video and audio items from the same album.
    /// </summary>
    public class Album : ContentCollection
    {
        internal IntPtr _albumHandle = IntPtr.Zero;
        /// <summary>
        /// The media album ID
        /// </summary>
        public int Id
        {
            get
            {
                int id = 0;
                MediaContentError res = (MediaContentError)Interop.Group.MediaAlbumGetAlbumId(_albumHandle, out id);
                if (res != MediaContentError.None)
                {
                    Log.Warn(MediaContentErrorFactory.LogTag, "Failed to get Id for the Album");
                }
                return id;
            }
        }

        /// <summary>
        /// The name of the media artist
        /// </summary>
        public string Artist
        {
            get
            {
                string artist = "";
                MediaContentError res = (MediaContentError)Interop.Group.MediaAlbumGetArtist(_albumHandle, out artist);
                if (res != MediaContentError.None)
                {
                    Log.Warn(MediaContentErrorFactory.LogTag, "Failed to get Artist for the Album");
                }
                return artist;
            }
        }

        /// <summary>
        /// The path of the media album art
        /// </summary>
        public string Art
        {
            get
            {
                string art = "";
                MediaContentError res = (MediaContentError)Interop.Group.MediaAlbumGetAlbumArt(_albumHandle, out art);
                Tizen.Log.Info("TCT", "Album Art Property: " + art);
                if (res != MediaContentError.None)
                {
                    Log.Warn(MediaContentErrorFactory.LogTag, "Failed to get Album Art for the Album");
                }
                return art;
            }
        }

        /// <summary>
        /// The name of the media album
        /// </summary>
        public string Name
        {
            get
            {
                string name = "";
                MediaContentError res = (MediaContentError)Interop.Group.MediaAlbumGetArtist(_albumHandle, out name);
                if (res != MediaContentError.None)
                {
                    Log.Warn(MediaContentErrorFactory.LogTag, "Failed to get Name for the Album");
                }
                return name;
            }
        }

        internal Album(IntPtr handle)
        {
            this._albumHandle = handle;
        }

        /// <summary>
        /// Gets the number of MediaInformation Items for the given album present in the media database.
        /// If NULL is passed to the filter, no filtering is applied.
        /// </summary>
        /// <param name="filter">ContentFilter used to match media content from the media database.</param>
        /// <returns>The number of media contents matching the filter passed</returns>
        public override int GetMediaInformationCount(ContentFilter filter)
        {
            int mediaCount = 0;
            IntPtr handle = (filter != null) ? filter.Handle : IntPtr.Zero;
            MediaContentError res = (MediaContentError)Interop.Group.MediaAlbumGetMediaCountFromDb(Id, handle, out mediaCount);
            if (res != MediaContentError.None)
            {
                Log.Warn(MediaContentErrorFactory.LogTag, "Failed to get media count for the album");
            }
            return mediaCount;
        }

        public override void Dispose()
        {
            MediaContentError res = (MediaContentError)Interop.Group.MediaAlbumDestroy(_albumHandle);
            if (res != MediaContentError.None)
            {
                Log.Warn(MediaContentErrorFactory.LogTag, "Failed to dispose the album");
            }
            _albumHandle = IntPtr.Zero;
        }

        /// <summary>
        /// Iterates through the media files with a filter in the given media album from the media database.
        /// This function gets all media files associated with the given media album and meeting desired filter option.
        /// If NULL is passed to the filter, no filtering is applied.
        /// </summary>
        /// <param name="filter">ContentFilter used to match media content from the media database.</param>
        /// <returns>List of content media items matching the passed filter</returns>
        public override Task<IEnumerable<MediaInformation>> GetMediaInformationsAsync(ContentFilter filter)
        {
            var tcs = new TaskCompletionSource<IEnumerable<MediaInformation>>();
            List<MediaInformation> mediaContents = new List<MediaInformation>();
            MediaContentError res;
            IntPtr handle = (filter != null) ? filter.Handle : IntPtr.Zero;
            Interop.Group.MediaInfoCallback callback = (IntPtr mediaHandle, IntPtr data) =>
            {
                Interop.MediaInformation.SafeMediaInformationHandle newHandle;
                res = (MediaContentError)Interop.MediaInformation.Clone(out newHandle, mediaHandle);
                if (res != MediaContentError.None)
                {
                    throw MediaContentErrorFactory.CreateException(res, "Failed to clone media");
                }

                MediaInformation info = new MediaInformation(newHandle);
                mediaContents.Add(info);
                return true;
            };
            res = (MediaContentError)Interop.Group.MediaAlbumForeachMediaFromDb(Id, handle, callback, IntPtr.Zero);
            if (res != MediaContentError.None)
            {
                throw MediaContentErrorFactory.CreateException(res, "Failed to get media information for the album");
            }
            tcs.TrySetResult(mediaContents);
            return tcs.Task;
        }
    }
}