1 /***
2 * Ambient - A music player for the Android platform
3 Copyright (C) 2007 Martin Vysny
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package sk.baka.ambient.collection;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23
24 import sk.baka.ambient.AmbientApplication;
25 import android.content.ContentResolver;
26 import android.database.Cursor;
27 import android.net.Uri;
28 import android.provider.MediaStore;
29 import android.provider.MediaStore.MediaColumns;
30 import android.provider.MediaStore.Audio.Media;
31
32 /***
33 * Denotes a file in the {@link MediaStore}.
34 *
35 * @author Martin Vysny
36 */
37 public final class MediaStoreAudio extends AbstractAudio {
38 @Override
39 public boolean exists() {
40 return mime != null;
41 }
42
43 @Override
44 public String getMimeType() {
45 return mime;
46 }
47
48 @Override
49 public long getSize() {
50 return size;
51 }
52
53 @Override
54 public boolean isReadable() {
55 return mime != null;
56 }
57
58 @Override
59 public InputStream openInputStream() throws IOException {
60 final ContentResolver resolver = AmbientApplication.getInstance()
61 .getContentResolver();
62 return resolver.openInputStream(uri);
63 }
64
65 private String mime = null;
66 private long size = -1;
67 private Uri uri;
68
69 /***
70 * Creates new object instance.
71 *
72 * @param uri
73 * the "content://" URI.
74 */
75 MediaStoreAudio(final String uri) {
76 super(uri);
77 if (uri.startsWith(Media.EXTERNAL_CONTENT_URI.toString())) {
78 this.uri = Uri.parse(uri);
79 final ContentResolver resolver = AmbientApplication.getInstance()
80 .getContentResolver();
81 final Cursor c = resolver.query(this.uri, new String[] {
82 MediaColumns.MIME_TYPE, MediaColumns.SIZE }, null, null,
83 null);
84 if (c.moveToFirst()) {
85 mime = c.getString(0);
86 size = c.getLong(1);
87 }
88 c.close();
89 }
90 }
91 }