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 /***
25 * Contains information about an audio file.
26 *
27 * @author Martin Vysny
28 */
29 public abstract class AbstractAudio {
30
31 private final String uri;
32
33 /***
34 * Creates new audio object instance.
35 *
36 * @param uri
37 * the URI
38 */
39 protected AbstractAudio(final String uri) {
40 this.uri = uri;
41 }
42
43 /***
44 * Returns location of the audio file.
45 *
46 * @return the location URI
47 */
48 public final String getLocation() {
49 return uri;
50 }
51
52 /***
53 * Checks if the file denoted by this object exists.
54 *
55 * @return <code>true</code> if the file exists, <code>false</code>
56 * otherwise.
57 */
58 public abstract boolean exists();
59
60 /***
61 * Checks if the file denoted by this object is readable (i.e. user has
62 * rights to read the file, etc).
63 *
64 * @return <code>true</code> if the file is readable, <code>false</code>
65 * otherwise.
66 */
67 public abstract boolean isReadable();
68
69 /***
70 * Reads the file contents.
71 *
72 * @return input stream instance, never <code>null</code>.
73 * @throws IOException
74 * if i/o error occurs.
75 */
76 public abstract InputStream openInputStream() throws IOException;
77
78 /***
79 * Returns the file MIME type.
80 *
81 * @return MIME type, never <code>null</code>.
82 */
83 public abstract String getMimeType();
84
85 /***
86 * Returns file size, in bytes.
87 *
88 * @return the file size.
89 */
90 public abstract long getSize();
91
92 /***
93 * Returns the audio object instance from a file denoted by given URI.
94 *
95 * @param fileUri
96 * the URI
97 * @return audio instance.
98 */
99 public static AbstractAudio fromUri(final String fileUri) {
100 if (fileUri.startsWith("content://")) {
101 return new MediaStoreAudio(fileUri);
102 }
103 return new FileAudio(fileUri);
104 }
105 }